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