xref: /freebsd/lib/libmd/md5c.c (revision 780fb4a2)
1 /*-
2  * SPDX-License-Identifier: RSA-MD
3  *
4  * MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
5  *
6  * Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
7  * rights reserved.
8  *
9  * License to copy and use this software is granted provided that it
10  * is identified as the "RSA Data Security, Inc. MD5 Message-Digest
11  * Algorithm" in all material mentioning or referencing this software
12  * or this function.
13  *
14  * License is also granted to make and use derivative works provided
15  * that such works are identified as "derived from the RSA Data
16  * Security, Inc. MD5 Message-Digest Algorithm" in all material
17  * mentioning or referencing the derived work.
18  *
19  * RSA Data Security, Inc. makes no representations concerning either
20  * the merchantability of this software or the suitability of this
21  * software for any particular purpose. It is provided "as is"
22  * without express or implied warranty of any kind.
23  *
24  * These notices must be retained in any copies of any part of this
25  * documentation and/or software.
26  *
27  * This code is the same as the code published by RSA Inc.  It has been
28  * edited for clarity and style only.
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/types.h>
35 
36 #ifdef _KERNEL
37 #include <sys/systm.h>
38 #else
39 #include <string.h>
40 #endif
41 
42 #include <machine/endian.h>
43 #include <sys/endian.h>
44 #include "md5.h"
45 
46 static void MD5Transform(u_int32_t [4], const unsigned char [64]);
47 
48 #ifdef _KERNEL
49 #define memset(x,y,z)	bzero(x,z);
50 #define memcpy(x,y,z)	bcopy(y, x, z)
51 #endif
52 
53 #if (BYTE_ORDER == LITTLE_ENDIAN)
54 #define Encode memcpy
55 #define Decode memcpy
56 #else
57 
58 /*
59  * Encodes input (u_int32_t) into output (unsigned char). Assumes len is
60  * a multiple of 4.
61  */
62 
63 static void
64 Encode (unsigned char *output, u_int32_t *input, unsigned int len)
65 {
66 	unsigned int i;
67 	u_int32_t *op = (u_int32_t *)output;
68 
69 	for (i = 0; i < len / 4; i++)
70 		op[i] = htole32(input[i]);
71 }
72 
73 /*
74  * Decodes input (unsigned char) into output (u_int32_t). Assumes len is
75  * a multiple of 4.
76  */
77 
78 static void
79 Decode (u_int32_t *output, const unsigned char *input, unsigned int len)
80 {
81 	unsigned int i;
82 	const u_int32_t *ip = (const u_int32_t *)input;
83 
84 	for (i = 0; i < len / 4; i++)
85 		output[i] = le32toh(ip[i]);
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) + (u_int32_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) + (u_int32_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) + (u_int32_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) + (u_int32_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, idx, partLen;
154 	const unsigned char *input = in;
155 
156 	/* Compute number of bytes mod 64 */
157 	idx = (unsigned int)((context->count[0] >> 3) & 0x3F);
158 
159 	/* Update number of bits */
160 	if ((context->count[0] += ((u_int32_t)inputLen << 3))
161 	    < ((u_int32_t)inputLen << 3))
162 		context->count[1]++;
163 	context->count[1] += ((u_int32_t)inputLen >> 29);
164 
165 	partLen = 64 - idx;
166 
167 	/* Transform as many times as possible. */
168 	if (inputLen >= partLen) {
169 		memcpy((void *)&context->buffer[idx], (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 		idx = 0;
177 	}
178 	else
179 		i = 0;
180 
181 	/* Buffer remaining input */
182 	memcpy ((void *)&context->buffer[idx], (const void *)&input[i],
183 	    inputLen-i);
184 }
185 
186 /*
187  * MD5 padding. Adds padding followed by original length.
188  */
189 
190 void
191 MD5Pad (MD5_CTX *context)
192 {
193 	unsigned char bits[8];
194 	unsigned int idx, padLen;
195 
196 	/* Save number of bits */
197 	Encode (bits, context->count, 8);
198 
199 	/* Pad out to 56 mod 64. */
200 	idx = (unsigned int)((context->count[0] >> 3) & 0x3f);
201 	padLen = (idx < 56) ? (56 - idx) : (120 - idx);
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[16], MD5_CTX *context)
215 {
216 	/* Do padding. */
217 	MD5Pad (context);
218 
219 	/* Store state in digest */
220 	Encode (digest, context->state, 16);
221 
222 	/* Zeroize sensitive information. */
223 	memset ((void *)context, 0, sizeof (*context));
224 }
225 
226 /* MD5 basic transformation. Transforms state based on block. */
227 
228 static void
229 MD5Transform (u_int32_t state[4], const unsigned char block[64])
230 {
231 	u_int32_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 
332 #ifdef WEAK_REFS
333 /* When building libmd, provide weak references. Note: this is not
334    activated in the context of compiling these sources for internal
335    use in libcrypt.
336  */
337 #undef MD5Init
338 __weak_reference(_libmd_MD5Init, MD5Init);
339 #undef MD5Update
340 __weak_reference(_libmd_MD5Update, MD5Update);
341 #undef MD5Pad
342 __weak_reference(_libmd_MD5Pad, MD5Pad);
343 #undef MD5Final
344 __weak_reference(_libmd_MD5Final, MD5Final);
345 #undef MD5Transform
346 __weak_reference(_libmd_MD5Transform, MD5Transform);
347 #endif
348