FreeBSD/Linux Kernel Cross Reference
sys/lib/libkern/md4c.c
1 /* $NetBSD: md4c.c,v 1.3 2003/12/04 12:42:54 keihan Exp $ */
2
3 /*
4 * This file is derived from the RSA Data Security, Inc. MD4 Message-Digest
5 * Algorithm and has been modified by Jason R. Thorpe <thorpej@NetBSD.org>
6 * for portability and formatting.
7 */
8
9 /*
10 * Copyright (C) 1990-2, RSA Data Security, Inc. All rights reserved.
11 *
12 * License to copy and use this software is granted provided that it
13 * is identified as the "RSA Data Security, Inc. MD4 Message-Digest
14 * Algorithm" in all material mentioning or referencing this software
15 * or this function.
16 *
17 * License is also granted to make and use derivative works provided
18 * that such works are identified as "derived from the RSA Data
19 * Security, Inc. MD4 Message-Digest Algorithm" in all material
20 * mentioning or referencing the derived work.
21 *
22 * RSA Data Security, Inc. makes no representations concerning either
23 * the merchantability of this software or the suitability of this
24 * software for any particular purpose. It is provided "as is"
25 * without express or implied warranty of any kind.
26 *
27 * These notices must be retained in any copies of any part of this
28 * documentation and/or software.
29 */
30
31 #include <sys/param.h>
32 #if defined(_KERNEL) || defined(_STANDALONE)
33 #include <sys/md4.h>
34 #include <lib/libkern/libkern.h>
35 #define _DIAGASSERT(x) (void)0
36 #else
37 #include <assert.h>
38 #include <md4.h>
39 #include <string.h>
40 #endif /* _KERNEL || _STANDALONE */
41
42 typedef unsigned char *POINTER;
43 typedef u_int16_t UINT2;
44 typedef u_int32_t UINT4;
45
46 /*
47 * Constants for MD4Transform routine.
48 */
49 #define S11 3
50 #define S12 7
51 #define S13 11
52 #define S14 19
53 #define S21 3
54 #define S22 5
55 #define S23 9
56 #define S24 13
57 #define S31 3
58 #define S32 9
59 #define S33 11
60 #define S34 15
61
62 static void MD4Transform __P((UINT4 [4], const unsigned char [64]));
63
64 static void Encode __P((unsigned char *, UINT4 *, unsigned int));
65 static void Decode __P((UINT4 *, const unsigned char *, unsigned int));
66
67 static const unsigned char PADDING[64] = {
68 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
69 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
70 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
71 };
72
73 /*
74 * F, G and H are basic MD4 functions.
75 */
76 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
77 #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
78 #define H(x, y, z) ((x) ^ (y) ^ (z))
79
80 /*
81 * ROTATE_LEFT rotates x left n bits.
82 */
83 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
84
85 /*
86 * FF, GG and HH are transformations for rounds 1, 2 and 3.
87 * Rotation is separate from addition to prevent recomputation.
88 */
89 #define FF(a, b, c, d, x, s) { \
90 (a) += F ((b), (c), (d)) + (x); \
91 (a) = ROTATE_LEFT ((a), (s)); \
92 }
93
94 #define GG(a, b, c, d, x, s) { \
95 (a) += G ((b), (c), (d)) + (x) + (UINT4)0x5a827999; \
96 (a) = ROTATE_LEFT ((a), (s)); \
97 }
98
99 #define HH(a, b, c, d, x, s) { \
100 (a) += H ((b), (c), (d)) + (x) + (UINT4)0x6ed9eba1; \
101 (a) = ROTATE_LEFT ((a), (s)); \
102 }
103
104 #if !defined(_KERNEL) && !defined(_STANDALONE) && defined(__weak_alias)
105 __weak_alias(MD4Init,_MD4Init)
106 __weak_alias(MD4Update,_MD4Update)
107 __weak_alias(MD4Final,_MD4Final)
108 #endif
109
110 /*
111 * MD4 initialization. Begins an MD4 operation, writing a new context.
112 */
113 void
114 MD4Init(context)
115 MD4_CTX *context; /* context */
116 {
117
118 _DIAGASSERT(context != 0);
119
120 context->count[0] = context->count[1] = 0;
121
122 /* Load magic initialization constants. */
123 context->state[0] = 0x67452301;
124 context->state[1] = 0xefcdab89;
125 context->state[2] = 0x98badcfe;
126 context->state[3] = 0x10325476;
127 }
128
129 /*
130 * MD4 block update operation. Continues an MD4 message-digest
131 * operation, processing another message block, and updating the
132 * context.
133 */
134 void
135 MD4Update (context, input, inputLen)
136 MD4_CTX *context; /* context */
137 const unsigned char *input; /* input block */
138 unsigned int inputLen; /* length of input block */
139 {
140 unsigned int i, idx, partLen;
141
142 _DIAGASSERT(context != 0);
143 _DIAGASSERT(input != 0);
144
145 /* Compute number of bytes mod 64 */
146 idx = (unsigned int)((context->count[0] >> 3) & 0x3F);
147
148 /* Update number of bits */
149 if ((context->count[0] += ((UINT4)inputLen << 3))
150 < ((UINT4)inputLen << 3))
151 context->count[1]++;
152 context->count[1] += ((UINT4)inputLen >> 29);
153
154 partLen = 64 - idx;
155
156 /* Transform as many times as possible. */
157 if (inputLen >= partLen) {
158 memcpy(&context->buffer[idx], input, partLen);
159 MD4Transform(context->state, context->buffer);
160
161 for (i = partLen; i + 63 < inputLen; i += 64)
162 MD4Transform(context->state, &input[i]);
163
164 idx = 0;
165 } else
166 i = 0;
167
168 /* Buffer remaining input */
169 memcpy(&context->buffer[idx], &input[i], inputLen - i);
170 }
171
172 /*
173 * MD4 finalization. Ends an MD4 message-digest operation, writing the
174 * message digest and zeroing the context.
175 */
176 void
177 MD4Final (digest, context)
178 unsigned char digest[16]; /* message digest */
179 MD4_CTX *context; /* context */
180 {
181 unsigned char bits[8];
182 unsigned int idx, padLen;
183
184 _DIAGASSERT(digest != 0);
185 _DIAGASSERT(context != 0);
186
187 /* Save number of bits */
188 Encode(bits, context->count, 8);
189
190 /* Pad out to 56 mod 64. */
191 idx = (unsigned int)((context->count[0] >> 3) & 0x3f);
192 padLen = (idx < 56) ? (56 - idx) : (120 - idx);
193 MD4Update(context, PADDING, padLen);
194
195 /* Append length (before padding) */
196 MD4Update(context, bits, 8);
197
198 /* Store state in digest */
199 Encode(digest, context->state, 16);
200
201 /* Zeroize sensitive information. */
202 memset(context, 0, sizeof(*context));
203 }
204
205 /*
206 * MD4 basic transformation. Transforms state based on block.
207 */
208 static void
209 MD4Transform (state, block)
210 UINT4 state[4];
211 const unsigned char block[64];
212 {
213 UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
214
215 Decode(x, block, 64);
216
217 /* Round 1 */
218 FF (a, b, c, d, x[ 0], S11); /* 1 */
219 FF (d, a, b, c, x[ 1], S12); /* 2 */
220 FF (c, d, a, b, x[ 2], S13); /* 3 */
221 FF (b, c, d, a, x[ 3], S14); /* 4 */
222 FF (a, b, c, d, x[ 4], S11); /* 5 */
223 FF (d, a, b, c, x[ 5], S12); /* 6 */
224 FF (c, d, a, b, x[ 6], S13); /* 7 */
225 FF (b, c, d, a, x[ 7], S14); /* 8 */
226 FF (a, b, c, d, x[ 8], S11); /* 9 */
227 FF (d, a, b, c, x[ 9], S12); /* 10 */
228 FF (c, d, a, b, x[10], S13); /* 11 */
229 FF (b, c, d, a, x[11], S14); /* 12 */
230 FF (a, b, c, d, x[12], S11); /* 13 */
231 FF (d, a, b, c, x[13], S12); /* 14 */
232 FF (c, d, a, b, x[14], S13); /* 15 */
233 FF (b, c, d, a, x[15], S14); /* 16 */
234
235 /* Round 2 */
236 GG (a, b, c, d, x[ 0], S21); /* 17 */
237 GG (d, a, b, c, x[ 4], S22); /* 18 */
238 GG (c, d, a, b, x[ 8], S23); /* 19 */
239 GG (b, c, d, a, x[12], S24); /* 20 */
240 GG (a, b, c, d, x[ 1], S21); /* 21 */
241 GG (d, a, b, c, x[ 5], S22); /* 22 */
242 GG (c, d, a, b, x[ 9], S23); /* 23 */
243 GG (b, c, d, a, x[13], S24); /* 24 */
244 GG (a, b, c, d, x[ 2], S21); /* 25 */
245 GG (d, a, b, c, x[ 6], S22); /* 26 */
246 GG (c, d, a, b, x[10], S23); /* 27 */
247 GG (b, c, d, a, x[14], S24); /* 28 */
248 GG (a, b, c, d, x[ 3], S21); /* 29 */
249 GG (d, a, b, c, x[ 7], S22); /* 30 */
250 GG (c, d, a, b, x[11], S23); /* 31 */
251 GG (b, c, d, a, x[15], S24); /* 32 */
252
253 /* Round 3 */
254 HH (a, b, c, d, x[ 0], S31); /* 33 */
255 HH (d, a, b, c, x[ 8], S32); /* 34 */
256 HH (c, d, a, b, x[ 4], S33); /* 35 */
257 HH (b, c, d, a, x[12], S34); /* 36 */
258 HH (a, b, c, d, x[ 2], S31); /* 37 */
259 HH (d, a, b, c, x[10], S32); /* 38 */
260 HH (c, d, a, b, x[ 6], S33); /* 39 */
261 HH (b, c, d, a, x[14], S34); /* 40 */
262 HH (a, b, c, d, x[ 1], S31); /* 41 */
263 HH (d, a, b, c, x[ 9], S32); /* 42 */
264 HH (c, d, a, b, x[ 5], S33); /* 43 */
265 HH (b, c, d, a, x[13], S34); /* 44 */
266 HH (a, b, c, d, x[ 3], S31); /* 45 */
267 HH (d, a, b, c, x[11], S32); /* 46 */
268 HH (c, d, a, b, x[ 7], S33); /* 47 */
269 HH (b, c, d, a, x[15], S34); /* 48 */
270
271 state[0] += a;
272 state[1] += b;
273 state[2] += c;
274 state[3] += d;
275
276 /* Zeroize sensitive information. */
277 memset(x, 0, sizeof (x));
278 }
279
280 /*
281 * Encodes input (UINT4) into output (unsigned char). Assumes len is
282 * a multiple of 4.
283 */
284 static void
285 Encode(output, input, len)
286 unsigned char *output;
287 UINT4 *input;
288 unsigned int len;
289 {
290 unsigned int i, j;
291
292 for (i = 0, j = 0; j < len; i++, j += 4) {
293 output[j] = (unsigned char)(input[i] & 0xff);
294 output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
295 output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
296 output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
297 }
298 }
299
300 /*
301 * Decodes input (unsigned char) into output (UINT4). Assumes len is
302 * a multiple of 4.
303 */
304 static void
305 Decode(output, input, len)
306 UINT4 *output;
307 const unsigned char *input;
308 unsigned int len;
309 {
310 unsigned int i, j;
311
312 for (i = 0, j = 0; j < len; i++, j += 4)
313 output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
314 (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
315 }
Cache object: f60b2074a1f31ea6ec991a420d99fcd1
|