md2.c revision 1.3 1 1.3 christos /* $NetBSD: md2.c,v 1.3 2005/11/29 03:11:59 christos Exp $ */
2 1.1 elad
3 1.1 elad /*
4 1.1 elad * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 1.1 elad * All rights reserved.
6 1.1 elad *
7 1.1 elad * This code is derived from software contributed to The NetBSD Foundation
8 1.1 elad * by Andrew Brown.
9 1.1 elad *
10 1.1 elad * Redistribution and use in source and binary forms, with or without
11 1.1 elad * modification, are permitted provided that the following conditions
12 1.1 elad * are met:
13 1.1 elad * 1. Redistributions of source code must retain the above copyright
14 1.1 elad * notice, this list of conditions and the following disclaimer.
15 1.1 elad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 elad * notice, this list of conditions and the following disclaimer in the
17 1.1 elad * documentation and/or other materials provided with the distribution.
18 1.1 elad * 3. All advertising materials mentioning features or use of this software
19 1.1 elad * must display the following acknowledgement:
20 1.1 elad * This product includes software developed by the NetBSD
21 1.1 elad * Foundation, Inc. and its contributors.
22 1.1 elad * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 elad * contributors may be used to endorse or promote products derived
24 1.1 elad * from this software without specific prior written permission.
25 1.1 elad *
26 1.1 elad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 elad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 elad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 elad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 elad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 elad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 elad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 elad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 elad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 elad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 elad * POSSIBILITY OF SUCH DAMAGE.
37 1.1 elad */
38 1.1 elad
39 1.1 elad #include <sys/cdefs.h>
40 1.1 elad #if defined(LIBC_SCCS) && !defined(lint)
41 1.3 christos __RCSID("$NetBSD: md2.c,v 1.3 2005/11/29 03:11:59 christos Exp $");
42 1.1 elad #endif /* LIBC_SCCS and not lint */
43 1.1 elad
44 1.1 elad #include "namespace.h"
45 1.1 elad
46 1.1 elad #include <sys/types.h>
47 1.1 elad
48 1.1 elad #include <assert.h>
49 1.1 elad #include <md2.h>
50 1.1 elad #include <string.h>
51 1.1 elad
52 1.1 elad #if HAVE_NBTOOL_CONFIG_H
53 1.1 elad #include "nbtool_config.h"
54 1.1 elad #endif
55 1.1 elad
56 1.1 elad #if !HAVE_MD2_H
57 1.1 elad
58 1.1 elad /* cut-n-pasted from rfc1319 */
59 1.1 elad static unsigned char S[256] = {
60 1.1 elad 41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240, 6,
61 1.1 elad 19, 98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188,
62 1.1 elad 76, 130, 202, 30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24,
63 1.1 elad 138, 23, 229, 18, 190, 78, 196, 214, 218, 158, 222, 73, 160, 251,
64 1.1 elad 245, 142, 187, 47, 238, 122, 169, 104, 121, 145, 21, 178, 7, 63,
65 1.1 elad 148, 194, 16, 137, 11, 34, 95, 33, 128, 127, 93, 154, 90, 144, 50,
66 1.1 elad 39, 53, 62, 204, 231, 191, 247, 151, 3, 255, 25, 48, 179, 72, 165,
67 1.1 elad 181, 209, 215, 94, 146, 42, 172, 86, 170, 198, 79, 184, 56, 210,
68 1.1 elad 150, 164, 125, 182, 118, 252, 107, 226, 156, 116, 4, 241, 69, 157,
69 1.1 elad 112, 89, 100, 113, 135, 32, 134, 91, 207, 101, 230, 45, 168, 2, 27,
70 1.1 elad 96, 37, 173, 174, 176, 185, 246, 28, 70, 97, 105, 52, 64, 126, 15,
71 1.1 elad 85, 71, 163, 35, 221, 81, 175, 58, 195, 92, 249, 206, 186, 197,
72 1.1 elad 234, 38, 44, 83, 13, 110, 133, 40, 132, 9, 211, 223, 205, 244, 65,
73 1.1 elad 129, 77, 82, 106, 220, 55, 200, 108, 193, 171, 250, 36, 225, 123,
74 1.1 elad 8, 12, 189, 177, 74, 120, 136, 149, 139, 227, 99, 232, 109, 233,
75 1.1 elad 203, 213, 254, 59, 0, 29, 57, 242, 239, 183, 14, 102, 88, 208, 228,
76 1.1 elad 166, 119, 114, 248, 235, 117, 75, 10, 49, 68, 80, 180, 143, 237,
77 1.1 elad 31, 26, 219, 153, 141, 51, 159, 17, 131, 20
78 1.1 elad };
79 1.1 elad
80 1.1 elad /* cut-n-pasted from rfc1319 */
81 1.3 christos static const unsigned char *pad[] = {
82 1.3 christos (const unsigned char *)"",
83 1.3 christos (const unsigned char *)"\001",
84 1.3 christos (const unsigned char *)"\002\002",
85 1.3 christos (const unsigned char *)"\003\003\003",
86 1.3 christos (const unsigned char *)"\004\004\004\004",
87 1.3 christos (const unsigned char *)"\005\005\005\005\005",
88 1.3 christos (const unsigned char *)"\006\006\006\006\006\006",
89 1.3 christos (const unsigned char *)"\007\007\007\007\007\007\007",
90 1.3 christos (const unsigned char *)"\010\010\010\010\010\010\010\010",
91 1.3 christos (const unsigned char *)"\011\011\011\011\011\011\011\011\011",
92 1.3 christos (const unsigned char *)"\012\012\012\012\012\012\012\012\012\012",
93 1.3 christos (const unsigned char *)"\013\013\013\013\013\013\013\013\013\013\013",
94 1.3 christos (const unsigned char *)
95 1.3 christos "\014\014\014\014\014\014\014\014\014\014\014\014",
96 1.3 christos (const unsigned char *)
97 1.1 elad "\015\015\015\015\015\015\015\015\015\015\015\015\015",
98 1.3 christos (const unsigned char *)
99 1.1 elad "\016\016\016\016\016\016\016\016\016\016\016\016\016\016",
100 1.3 christos (const unsigned char *)
101 1.1 elad "\017\017\017\017\017\017\017\017\017\017\017\017\017\017\017",
102 1.3 christos (const unsigned char *)
103 1.1 elad "\020\020\020\020\020\020\020\020\020\020\020\020\020\020\020\020"
104 1.1 elad };
105 1.1 elad
106 1.1 elad static void MD2Transform __P((MD2_CTX *));
107 1.1 elad
108 1.1 elad #ifdef __weak_alias
109 1.1 elad __weak_alias(MD2Init,_MD2Init)
110 1.1 elad __weak_alias(MD2Update,_MD2Update)
111 1.1 elad __weak_alias(MD2Final,_MD2Final)
112 1.2 elad __weak_alias(MD2Transform,_MD2Transform)
113 1.1 elad #endif
114 1.1 elad
115 1.1 elad void
116 1.1 elad MD2Init(context)
117 1.1 elad MD2_CTX *context;
118 1.1 elad {
119 1.1 elad _DIAGASSERT(context != 0);
120 1.1 elad
121 1.1 elad context->i = 16;
122 1.1 elad memset(&context->C[0], 0, sizeof(context->C));
123 1.1 elad memset(&context->X[0], 0, sizeof(context->X));
124 1.1 elad }
125 1.1 elad
126 1.1 elad void
127 1.1 elad MD2Update(context, input, inputLen)
128 1.1 elad MD2_CTX *context;
129 1.1 elad const unsigned char *input;
130 1.1 elad unsigned int inputLen;
131 1.1 elad {
132 1.1 elad unsigned int idx, piece;
133 1.1 elad
134 1.1 elad _DIAGASSERT(context != 0);
135 1.1 elad _DIAGASSERT(input != 0);
136 1.1 elad
137 1.1 elad for (idx = 0; idx < inputLen; idx += piece) {
138 1.1 elad piece = 32 - context->i;
139 1.1 elad if ((inputLen - idx) < piece)
140 1.1 elad piece = inputLen - idx;
141 1.1 elad memcpy(&context->X[context->i], &input[idx], (size_t)piece);
142 1.1 elad if ((context->i += piece) == 32)
143 1.1 elad MD2Transform(context); /* resets i */
144 1.1 elad }
145 1.1 elad }
146 1.1 elad
147 1.1 elad void
148 1.1 elad MD2Final(digest, context)
149 1.1 elad unsigned char digest[16]; /* message digest */
150 1.1 elad MD2_CTX *context; /* context */
151 1.1 elad {
152 1.1 elad unsigned int padlen;
153 1.1 elad
154 1.1 elad _DIAGASSERT(digest != 0);
155 1.1 elad _DIAGASSERT(context != 0);
156 1.1 elad
157 1.1 elad /* padlen should be 1..16 */
158 1.1 elad padlen = 32 - context->i;
159 1.1 elad
160 1.1 elad /* add padding */
161 1.1 elad MD2Update(context, pad[padlen], padlen);
162 1.1 elad
163 1.1 elad /* add checksum */
164 1.1 elad MD2Update(context, &context->C[0], (unsigned int) sizeof(context->C));
165 1.1 elad
166 1.1 elad /* copy out final digest */
167 1.1 elad memcpy(digest, &context->X[0], (size_t)16);
168 1.1 elad
169 1.1 elad /* reset the context */
170 1.1 elad MD2Init(context);
171 1.1 elad }
172 1.1 elad
173 1.1 elad static void
174 1.1 elad MD2Transform(context)
175 1.1 elad MD2_CTX *context;
176 1.1 elad {
177 1.1 elad u_int32_t l, j, k, t;
178 1.1 elad
179 1.1 elad /* set block "3" and update "checksum" */
180 1.1 elad for (l = context->C[15], j = 0; j < 16; j++) {
181 1.1 elad context->X[32 + j] = context->X[j] ^ context->X[16 + j];
182 1.1 elad l = context->C[j] ^= S[context->X[16 + j] ^ l];
183 1.1 elad }
184 1.1 elad
185 1.1 elad /* mangle input block */
186 1.1 elad for (t = j = 0; j < 18; t = (t + j) % 256, j++)
187 1.1 elad for (k = 0; k < 48; k++)
188 1.1 elad t = context->X[k] = (context->X[k] ^ S[t]);
189 1.1 elad
190 1.1 elad /* reset input pointer */
191 1.1 elad context->i = 16;
192 1.1 elad }
193 1.1 elad
194 1.1 elad #endif /* !HAVE_MD2_H */
195