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