xref: /freebsd/sys/kern/md5c.c (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1 /*-
2  * MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
3  *
4  * Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
5  * rights reserved.
6  *
7  * License to copy and use this software is granted provided that it
8  * is identified as the "RSA Data Security, Inc. MD5 Message-Digest
9  * Algorithm" in all material mentioning or referencing this software
10  * or this function.
11  *
12  * License is also granted to make and use derivative works provided
13  * that such works are identified as "derived from the RSA Data
14  * Security, Inc. MD5 Message-Digest Algorithm" in all material
15  * mentioning or referencing the derived work.
16  *
17  * RSA Data Security, Inc. makes no representations concerning either
18  * the merchantability of this software or the suitability of this
19  * software for any particular purpose. It is provided "as is"
20  * without express or implied warranty of any kind.
21  *
22  * These notices must be retained in any copies of any part of this
23  * documentation and/or software.
24  *
25  * This code is the same as the code published by RSA Inc.  It has been
26  * edited for clarity and style only.
27  */
28 
29 /*
30  * This file should be kept in sync with src/lib/libmd/md5c.c
31  */
32 #include <sys/cdefs.h>
33 #include <sys/types.h>
34 
35 #ifdef _KERNEL
36 #include <sys/systm.h>
37 #else
38 #include <string.h>
39 #endif
40 
41 #include <machine/endian.h>
42 #include <sys/endian.h>
43 #include <sys/md5.h>
44 
45 static void MD5Transform(uint32_t [4], const unsigned char [64]);
46 
47 #if (BYTE_ORDER == LITTLE_ENDIAN)
48 #define Encode memcpy
49 #define Decode memcpy
50 #else
51 
52 /*
53  * Encodes input (uint32_t) into output (unsigned char). Assumes len is
54  * a multiple of 4.
55  */
56 
57 static void
58 Encode (unsigned char *output, uint32_t *input, unsigned int len)
59 {
60 	unsigned int i;
61 	uint32_t ip;
62 
63 	for (i = 0; i < len / 4; i++) {
64 		ip = input[i];
65 		*output++ = ip;
66 		*output++ = ip >> 8;
67 		*output++ = ip >> 16;
68 		*output++ = ip >> 24;
69 	}
70 }
71 
72 /*
73  * Decodes input (unsigned char) into output (uint32_t). Assumes len is
74  * a multiple of 4.
75  */
76 
77 static void
78 Decode (uint32_t *output, const unsigned char *input, unsigned int len)
79 {
80 	unsigned int i;
81 
82 	for (i = 0; i < len; i += 4) {
83 		*output++ = input[i] | (input[i+1] << 8) | (input[i+2] << 16) |
84 		    (input[i+3] << 24);
85 	}
86 }
87 #endif
88 
89 static unsigned char PADDING[64] = {
90   0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
91   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
92   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
93 };
94 
95 /* F, G, H and I are basic MD5 functions. */
96 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
97 #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
98 #define H(x, y, z) ((x) ^ (y) ^ (z))
99 #define I(x, y, z) ((y) ^ ((x) | (~z)))
100 
101 /* ROTATE_LEFT rotates x left n bits. */
102 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
103 
104 /*
105  * FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
106  * Rotation is separate from addition to prevent recomputation.
107  */
108 #define FF(a, b, c, d, x, s, ac) { \
109 	(a) += F ((b), (c), (d)) + (x) + (uint32_t)(ac); \
110 	(a) = ROTATE_LEFT ((a), (s)); \
111 	(a) += (b); \
112 	}
113 #define GG(a, b, c, d, x, s, ac) { \
114 	(a) += G ((b), (c), (d)) + (x) + (uint32_t)(ac); \
115 	(a) = ROTATE_LEFT ((a), (s)); \
116 	(a) += (b); \
117 	}
118 #define HH(a, b, c, d, x, s, ac) { \
119 	(a) += H ((b), (c), (d)) + (x) + (uint32_t)(ac); \
120 	(a) = ROTATE_LEFT ((a), (s)); \
121 	(a) += (b); \
122 	}
123 #define II(a, b, c, d, x, s, ac) { \
124 	(a) += I ((b), (c), (d)) + (x) + (uint32_t)(ac); \
125 	(a) = ROTATE_LEFT ((a), (s)); \
126 	(a) += (b); \
127 	}
128 
129 /* MD5 initialization. Begins an MD5 operation, writing a new context. */
130 
131 void
132 MD5Init(MD5_CTX *context)
133 {
134 
135 	context->count[0] = context->count[1] = 0;
136 
137 	/* Load magic initialization constants.  */
138 	context->state[0] = 0x67452301;
139 	context->state[1] = 0xefcdab89;
140 	context->state[2] = 0x98badcfe;
141 	context->state[3] = 0x10325476;
142 }
143 
144 /*
145  * MD5 block update operation. Continues an MD5 message-digest
146  * operation, processing another message block, and updating the
147  * context.
148  */
149 
150 void
151 MD5Update(MD5_CTX *context, const void *in, unsigned int inputLen)
152 {
153 	unsigned int i, index, partLen;
154 	const unsigned char *input = in;
155 
156 	/* Compute number of bytes mod 64 */
157 	index = (unsigned int)((context->count[0] >> 3) & 0x3F);
158 
159 	/* Update number of bits */
160 	if ((context->count[0] += ((uint32_t)inputLen << 3))
161 	    < ((uint32_t)inputLen << 3))
162 		context->count[1]++;
163 	context->count[1] += ((uint32_t)inputLen >> 29);
164 
165 	partLen = 64 - index;
166 
167 	/* Transform as many times as possible. */
168 	if (inputLen >= partLen) {
169 		memcpy((void *)&context->buffer[index], (const void *)input,
170 		    partLen);
171 		MD5Transform (context->state, context->buffer);
172 
173 		for (i = partLen; i + 63 < inputLen; i += 64)
174 			MD5Transform (context->state, &input[i]);
175 
176 		index = 0;
177 	}
178 	else
179 		i = 0;
180 
181 	/* Buffer remaining input */
182 	memcpy ((void *)&context->buffer[index], (const void *)&input[i],
183 	    inputLen-i);
184 }
185 
186 /*
187  * MD5 padding. Adds padding followed by original length.
188  */
189 
190 static void
191 MD5Pad(MD5_CTX *context)
192 {
193 	unsigned char bits[8];
194 	unsigned int index, padLen;
195 
196 	/* Save number of bits */
197 	Encode (bits, context->count, 8);
198 
199 	/* Pad out to 56 mod 64. */
200 	index = (unsigned int)((context->count[0] >> 3) & 0x3f);
201 	padLen = (index < 56) ? (56 - index) : (120 - index);
202 	MD5Update (context, PADDING, padLen);
203 
204 	/* Append length (before padding) */
205 	MD5Update (context, bits, 8);
206 }
207 
208 /*
209  * MD5 finalization. Ends an MD5 message-digest operation, writing the
210  * the message digest and zeroizing the context.
211  */
212 
213 void
214 MD5Final(unsigned char digest[static MD5_DIGEST_LENGTH], MD5_CTX *context)
215 {
216 	/* Do padding. */
217 	MD5Pad (context);
218 
219 	/* Store state in digest */
220 	Encode (digest, context->state, MD5_DIGEST_LENGTH);
221 
222 	/* Zeroize sensitive information. */
223 	memset (context, 0, sizeof (*context));
224 }
225 
226 /* MD5 basic transformation. Transforms state based on block. */
227 
228 static void
229 MD5Transform(uint32_t state[4], const unsigned char block[64])
230 {
231 	uint32_t a = state[0], b = state[1], c = state[2], d = state[3], x[16];
232 
233 	Decode (x, block, 64);
234 
235 	/* Round 1 */
236 #define S11 7
237 #define S12 12
238 #define S13 17
239 #define S14 22
240 	FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
241 	FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
242 	FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
243 	FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
244 	FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
245 	FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
246 	FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
247 	FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
248 	FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
249 	FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
250 	FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
251 	FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
252 	FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
253 	FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
254 	FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
255 	FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
256 
257 	/* Round 2 */
258 #define S21 5
259 #define S22 9
260 #define S23 14
261 #define S24 20
262 	GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
263 	GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
264 	GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
265 	GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
266 	GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
267 	GG (d, a, b, c, x[10], S22,  0x2441453); /* 22 */
268 	GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
269 	GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
270 	GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
271 	GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
272 	GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
273 	GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
274 	GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
275 	GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
276 	GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
277 	GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
278 
279 	/* Round 3 */
280 #define S31 4
281 #define S32 11
282 #define S33 16
283 #define S34 23
284 	HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
285 	HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
286 	HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
287 	HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
288 	HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
289 	HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
290 	HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
291 	HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
292 	HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
293 	HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
294 	HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
295 	HH (b, c, d, a, x[ 6], S34,  0x4881d05); /* 44 */
296 	HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
297 	HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
298 	HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
299 	HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
300 
301 	/* Round 4 */
302 #define S41 6
303 #define S42 10
304 #define S43 15
305 #define S44 21
306 	II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
307 	II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
308 	II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
309 	II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
310 	II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
311 	II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
312 	II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
313 	II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
314 	II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
315 	II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
316 	II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
317 	II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
318 	II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
319 	II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
320 	II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
321 	II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
322 
323 	state[0] += a;
324 	state[1] += b;
325 	state[2] += c;
326 	state[3] += d;
327 
328 	/* Zeroize sensitive information. */
329 	memset ((void *)x, 0, sizeof (x));
330 }
331