xref: /netbsd-src/common/lib/libc/md/md4c.c (revision e5548b402ae4c44fb816de42c7bba9581ce23ef5)
1 /*	$NetBSD: md4c.c,v 1.1 2005/12/20 19:28:51 christos 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 #if !defined(_KERNEL) && !defined(_STANDALONE)
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
34 __RCSID("$NetBSD: md4c.c,v 1.1 2005/12/20 19:28:51 christos Exp $");
35 #endif /* LIBC_SCCS and not lint */
36 
37 #include "namespace.h"
38 
39 #include <sys/types.h>
40 
41 #include <assert.h>
42 #include <md4.h>
43 #include <string.h>
44 
45 #if HAVE_NBTOOL_CONFIG_H
46 #include "nbtool_config.h"
47 #endif
48 
49 #else
50 
51 #include <sys/md4.h>
52 #include <lib/libkern/libkern.h>
53 
54 #endif /* !_KERNEL && !_STANDALONE */
55 
56 #if !HAVE_MD4_H
57 
58 typedef unsigned char *POINTER;
59 typedef u_int16_t UINT2;
60 typedef u_int32_t UINT4;
61 
62 /*
63  * Constants for MD4Transform routine.
64  */
65 #define S11 3
66 #define S12 7
67 #define S13 11
68 #define S14 19
69 #define S21 3
70 #define S22 5
71 #define S23 9
72 #define S24 13
73 #define S31 3
74 #define S32 9
75 #define S33 11
76 #define S34 15
77 
78 static void MD4Transform __P((UINT4 [4], const unsigned char [64]));
79 
80 static void Encode __P((unsigned char *, UINT4 *, unsigned int));
81 static void Decode __P((UINT4 *, const unsigned char *, unsigned int));
82 
83 static const unsigned char PADDING[64] = {
84 	0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
85 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
86 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
87 };
88 
89 /*
90  * F, G and H are basic MD4 functions.
91  */
92 #define F(x, y, z)	(((x) & (y)) | ((~x) & (z)))
93 #define G(x, y, z)	(((x) & (y)) | ((x) & (z)) | ((y) & (z)))
94 #define H(x, y, z)	((x) ^ (y) ^ (z))
95 
96 /*
97  * ROTATE_LEFT rotates x left n bits.
98  */
99 #define ROTATE_LEFT(x, n)	(((x) << (n)) | ((x) >> (32-(n))))
100 
101 /*
102  * FF, GG and HH are transformations for rounds 1, 2 and 3.
103  * Rotation is separate from addition to prevent recomputation.
104  */
105 #define FF(a, b, c, d, x, s) { \
106 	(a) += F ((b), (c), (d)) + (x); \
107 	(a) = ROTATE_LEFT ((a), (s)); \
108 }
109 
110 #define GG(a, b, c, d, x, s) { \
111 	(a) += G ((b), (c), (d)) + (x) + (UINT4)0x5a827999; \
112 	(a) = ROTATE_LEFT ((a), (s)); \
113 }
114 
115 #define HH(a, b, c, d, x, s) { \
116 	(a) += H ((b), (c), (d)) + (x) + (UINT4)0x6ed9eba1; \
117 	(a) = ROTATE_LEFT ((a), (s)); \
118 }
119 
120 #if !defined(_KERNEL) && !defined(_STANDALONE) && defined(__weak_alias)
121 __weak_alias(MD4Init,_MD4Init)
122 __weak_alias(MD4Update,_MD4Update)
123 __weak_alias(MD4Final,_MD4Final)
124 __weak_alias(MD4Transform,_MD4Transform)
125 #endif
126 
127 /*
128  * MD4 initialization. Begins an MD4 operation, writing a new context.
129  */
130 void
131 MD4Init(context)
132 	MD4_CTX *context;		/* context */
133 {
134 
135 	_DIAGASSERT(context != 0);
136 
137 	context->count[0] = context->count[1] = 0;
138 
139 	/* Load magic initialization constants. */
140 	context->state[0] = 0x67452301;
141 	context->state[1] = 0xefcdab89;
142 	context->state[2] = 0x98badcfe;
143 	context->state[3] = 0x10325476;
144 }
145 
146 /*
147  * MD4 block update operation.  Continues an MD4 message-digest
148  * operation, processing another message block, and updating the
149  * context.
150  */
151 void
152 MD4Update (context, input, inputLen)
153 	MD4_CTX *context;		/* context */
154 	const unsigned char *input;	/* input block */
155 	unsigned int inputLen;		/* length of input block */
156 {
157 	unsigned int i, idx, partLen;
158 
159 	_DIAGASSERT(context != 0);
160 	_DIAGASSERT(input != 0);
161 
162 	/* Compute number of bytes mod 64 */
163 	idx = (unsigned int)((context->count[0] >> 3) & 0x3F);
164 
165 	/* Update number of bits */
166 	if ((context->count[0] += ((UINT4)inputLen << 3))
167 	    < ((UINT4)inputLen << 3))
168 		context->count[1]++;
169 	context->count[1] += ((UINT4)inputLen >> 29);
170 
171 	partLen = 64 - idx;
172 
173 	/* Transform as many times as possible. */
174 	if (inputLen >= partLen) {
175 		memcpy(&context->buffer[idx], input, partLen);
176 		MD4Transform(context->state, context->buffer);
177 
178 		for (i = partLen; i + 63 < inputLen; i += 64)
179 			MD4Transform(context->state, &input[i]);
180 
181 		idx = 0;
182 	} else
183 		i = 0;
184 
185 	/* Buffer remaining input */
186 	memcpy(&context->buffer[idx], &input[i], inputLen - i);
187 }
188 
189 /*
190  * MD4 finalization.  Ends an MD4 message-digest operation, writing the
191  * message digest and zeroing the context.
192  */
193 void
194 MD4Final (digest, context)
195 	unsigned char digest[16];	/* message digest */
196 	MD4_CTX *context;		/* context */
197 {
198 	unsigned char bits[8];
199 	unsigned int idx, padLen;
200 
201 	_DIAGASSERT(digest != 0);
202 	_DIAGASSERT(context != 0);
203 
204 	/* Save number of bits */
205 	Encode(bits, context->count, 8);
206 
207 	/* Pad out to 56 mod 64. */
208 	idx = (unsigned int)((context->count[0] >> 3) & 0x3f);
209 	padLen = (idx < 56) ? (56 - idx) : (120 - idx);
210 	MD4Update(context, PADDING, padLen);
211 
212 	/* Append length (before padding) */
213 	MD4Update(context, bits, 8);
214 
215 	/* Store state in digest */
216 	Encode(digest, context->state, 16);
217 
218 	/* Zeroize sensitive information. */
219 	memset(context, 0, sizeof(*context));
220 }
221 
222 /*
223  * MD4 basic transformation.  Transforms state based on block.
224  */
225 static void
226 MD4Transform (state, block)
227 	UINT4 state[4];
228 	const unsigned char block[64];
229 {
230 	UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
231 
232 	Decode(x, block, 64);
233 
234 	/* Round 1 */
235 	FF (a, b, c, d, x[ 0], S11); /* 1 */
236 	FF (d, a, b, c, x[ 1], S12); /* 2 */
237 	FF (c, d, a, b, x[ 2], S13); /* 3 */
238 	FF (b, c, d, a, x[ 3], S14); /* 4 */
239 	FF (a, b, c, d, x[ 4], S11); /* 5 */
240 	FF (d, a, b, c, x[ 5], S12); /* 6 */
241 	FF (c, d, a, b, x[ 6], S13); /* 7 */
242 	FF (b, c, d, a, x[ 7], S14); /* 8 */
243 	FF (a, b, c, d, x[ 8], S11); /* 9 */
244 	FF (d, a, b, c, x[ 9], S12); /* 10 */
245 	FF (c, d, a, b, x[10], S13); /* 11 */
246 	FF (b, c, d, a, x[11], S14); /* 12 */
247 	FF (a, b, c, d, x[12], S11); /* 13 */
248 	FF (d, a, b, c, x[13], S12); /* 14 */
249 	FF (c, d, a, b, x[14], S13); /* 15 */
250 	FF (b, c, d, a, x[15], S14); /* 16 */
251 
252 	/* Round 2 */
253 	GG (a, b, c, d, x[ 0], S21); /* 17 */
254 	GG (d, a, b, c, x[ 4], S22); /* 18 */
255 	GG (c, d, a, b, x[ 8], S23); /* 19 */
256 	GG (b, c, d, a, x[12], S24); /* 20 */
257 	GG (a, b, c, d, x[ 1], S21); /* 21 */
258 	GG (d, a, b, c, x[ 5], S22); /* 22 */
259 	GG (c, d, a, b, x[ 9], S23); /* 23 */
260 	GG (b, c, d, a, x[13], S24); /* 24 */
261 	GG (a, b, c, d, x[ 2], S21); /* 25 */
262 	GG (d, a, b, c, x[ 6], S22); /* 26 */
263 	GG (c, d, a, b, x[10], S23); /* 27 */
264 	GG (b, c, d, a, x[14], S24); /* 28 */
265 	GG (a, b, c, d, x[ 3], S21); /* 29 */
266 	GG (d, a, b, c, x[ 7], S22); /* 30 */
267 	GG (c, d, a, b, x[11], S23); /* 31 */
268 	GG (b, c, d, a, x[15], S24); /* 32 */
269 
270 	/* Round 3 */
271 	HH (a, b, c, d, x[ 0], S31); /* 33 */
272 	HH (d, a, b, c, x[ 8], S32); /* 34 */
273 	HH (c, d, a, b, x[ 4], S33); /* 35 */
274 	HH (b, c, d, a, x[12], S34); /* 36 */
275 	HH (a, b, c, d, x[ 2], S31); /* 37 */
276 	HH (d, a, b, c, x[10], S32); /* 38 */
277 	HH (c, d, a, b, x[ 6], S33); /* 39 */
278 	HH (b, c, d, a, x[14], S34); /* 40 */
279 	HH (a, b, c, d, x[ 1], S31); /* 41 */
280 	HH (d, a, b, c, x[ 9], S32); /* 42 */
281 	HH (c, d, a, b, x[ 5], S33); /* 43 */
282 	HH (b, c, d, a, x[13], S34); /* 44 */
283 	HH (a, b, c, d, x[ 3], S31); /* 45 */
284 	HH (d, a, b, c, x[11], S32); /* 46 */
285 	HH (c, d, a, b, x[ 7], S33); /* 47 */
286 	HH (b, c, d, a, x[15], S34); /* 48 */
287 
288 	state[0] += a;
289 	state[1] += b;
290 	state[2] += c;
291 	state[3] += d;
292 
293 	/* Zeroize sensitive information. */
294 	memset(x, 0, sizeof (x));
295 }
296 
297 /*
298  * Encodes input (UINT4) into output (unsigned char). Assumes len is
299  * a multiple of 4.
300  */
301 static void
302 Encode(output, input, len)
303 	unsigned char *output;
304 	UINT4 *input;
305 	unsigned int len;
306 {
307 	unsigned int i, j;
308 
309 	for (i = 0, j = 0; j < len; i++, j += 4) {
310 		output[j] = (unsigned char)(input[i] & 0xff);
311 		output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
312 		output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
313 		output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
314 	}
315 }
316 
317 /*
318  * Decodes input (unsigned char) into output (UINT4). Assumes len is
319  * a multiple of 4.
320  */
321 static void
322 Decode(output, input, len)
323 	UINT4 *output;
324 	const unsigned char *input;
325 	unsigned int len;
326 {
327 	unsigned int i, j;
328 
329 	for (i = 0, j = 0; j < len; i++, j += 4)
330 		output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
331 		    (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
332 }
333 
334 #endif /* HAVE_MD4_H */
335