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