1/*
2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29/*
30 * MD5.C - RSA Data Security, Inc., MD5 message-digest algorithm
31 *
32 * Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
33 * rights reserved.
34 *
35 * License to copy and use this software is granted provided that it
36 * is identified as the "RSA Data Security, Inc. MD5 Message-Digest
37 * Algorithm" in all material mentioning or referencing this software
38 * or this function.
39 *
40 * License is also granted to make and use derivative works provided
41 * that such works are identified as "derived from the RSA Data
42 * Security, Inc. MD5 Message-Digest Algorithm" in all material
43 * mentioning or referencing the derived work.
44 *
45 * RSA Data Security, Inc. makes no representations concerning either
46 * the merchantability of this software or the suitability of this
47 * software for any particular purpose. It is provided "as is"
48 * without express or implied warranty of any kind.
49 *
50 * These notices must be retained in any copies of any part of this
51 * documentation and/or software.
52 *
53 * This code is the same as the code published by RSA Inc.  It has been
54 * edited for clarity and style only.
55 */
56
57#include <sys/types.h>
58#include <sys/systm.h>
59#include <libkern/crypto/md5.h>
60
61#define	memset(x, y, z)	bzero(x, z);
62#define	memcpy(x, y, z)	bcopy(y, x, z)
63
64/*
65 * The digest algorithm interprets the input message as a sequence of 32-bit
66 * little-endian words.  We must reverse bytes in each word on PPC and other
67 * big-endian platforms, but not on little-endian ones.  When we can, we try
68 * to load each word at once.  We don't quite care about alignment, since
69 * x86/x64 allows us to do 4-byte loads on non 4-byte aligned addresses,
70 * and on PPC we do 1-byte loads anyway.
71 *
72 * We could check against __LITLE_ENDIAN__ to generalize the 4-byte load
73 * optimization, but that might not tell us whether or not we need 4-byte
74 * aligned loads.  Since we know that __i386__ and __x86_64__ are the two
75 * little-endian architectures that are not alignment-restrictive, we check
76 * explicitly against them below.  Note that the byte-reversing code for
77 * big-endian will still work on little-endian, albeit much slower.
78 */
79#if defined(__i386__) || defined(__x86_64__)
80#define	FETCH_32(p)	(*(const u_int32_t *)(p))
81#else
82#define	FETCH_32(p)						\
83	(((u_int32_t)*((const u_int8_t *)(p))) |		\
84	(((u_int32_t)*((const u_int8_t *)(p) + 1)) << 8) |	\
85	(((u_int32_t)*((const u_int8_t *)(p) + 2)) << 16) |	\
86	(((u_int32_t)*((const u_int8_t *)(p) + 3)) << 24))
87#endif /* __i386__ || __x86_64__ */
88
89/*
90 * Encodes input (u_int32_t) into output (unsigned char). Assumes len is
91 * a multiple of 4. This is not compatible with memcpy().
92 */
93static void
94Encode(unsigned char *output, u_int32_t *input, unsigned int len)
95{
96	unsigned int i, j;
97
98	for (i = 0, j = 0; j < len; i++, j += 4) {
99#if defined(__i386__) || defined(__x86_64__)
100		*(u_int32_t *)(output + j) = input[i];
101#else
102		output[j] = input[i] & 0xff;
103		output[j + 1] = (input[i] >> 8) & 0xff;
104		output[j + 2] = (input[i] >> 16) & 0xff;
105		output[j + 3] = (input[i] >> 24) & 0xff;
106#endif /* __i386__ || __x86_64__ */
107	}
108}
109
110static unsigned char PADDING[64] = { 0x80, /* zeros */ };
111
112/* F, G, H and I are basic MD5 functions. */
113#define	F(x, y, z)	((((y) ^ (z)) & (x)) ^ (z))
114#define	G(x, y, z)	((((x) ^ (y)) & (z)) ^ (y))
115#define	H(x, y, z)	((x) ^ (y) ^ (z))
116#define	I(x, y, z)	(((~(z)) | (x)) ^ (y))
117
118/* ROTATE_LEFT rotates x left n bits. */
119#define	ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
120
121/*
122 * FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
123 * Rotation is separate from addition to prevent recomputation.
124 */
125#define	FF(a, b, c, d, x, s, ac) {					\
126	(a) += F((b), (c), (d)) + (x) + (unsigned long long)(ac);	\
127	(a) = ROTATE_LEFT((a), (s));					\
128	(a) += (b);							\
129}
130
131#define	GG(a, b, c, d, x, s, ac) {					\
132	(a) += G((b), (c), (d)) + (x) + (unsigned long long)(ac);	\
133	(a) = ROTATE_LEFT((a), (s));					\
134	(a) += (b);							\
135}
136
137#define	HH(a, b, c, d, x, s, ac) {					\
138	(a) += H((b), (c), (d)) + (x) + (unsigned long long)(ac);	\
139	(a) = ROTATE_LEFT((a), (s));					\
140	(a) += (b);							\
141}
142
143#define	II(a, b, c, d, x, s, ac) {					\
144	(a) += I((b), (c), (d)) + (x) + (unsigned long long)(ac);	\
145	(a) = ROTATE_LEFT((a), (s));					\
146	(a) += (b);							\
147}
148
149static void MD5Transform(u_int32_t, u_int32_t, u_int32_t, u_int32_t,
150    const u_int8_t [64], MD5_CTX *);
151
152/*
153 * MD5 initialization. Begins an MD5 operation, writing a new context.
154 */
155void
156MD5Init(MD5_CTX *context)
157{
158	context->count[0] = context->count[1] = 0;
159
160	/* Load magic initialization constants.  */
161	context->state[0] = 0x67452301UL;
162	context->state[1] = 0xefcdab89UL;
163	context->state[2] = 0x98badcfeUL;
164	context->state[3] = 0x10325476UL;
165}
166
167/*
168 * MD5 block update operation. Continues an MD5 message-digest
169 * operation, processing another message block, and updating the
170 * context.
171 */
172void
173MD5Update(MD5_CTX *context, const void *inpp, unsigned int inputLen)
174{
175	u_int32_t i, index, partLen;
176	const unsigned char *input = (const unsigned char *)inpp;
177
178	/* Compute number of bytes mod 64 */
179	index = (context->count[0] >> 3) & 0x3F;
180
181	/* Update number of bits */
182	if ((context->count[0] += (inputLen << 3)) < (inputLen << 3))
183		context->count[1]++;
184	context->count[1] += (inputLen >> 29);
185
186	partLen = 64 - index;
187
188	/* Transform as many times as possible. */
189	i = 0;
190	if (inputLen >= partLen) {
191		if (index != 0) {
192			memcpy(&context->buffer[index], input, partLen);
193			MD5Transform(context->state[0], context->state[1],
194			    context->state[2], context->state[3],
195			    context->buffer, context);
196			i = partLen;
197		}
198
199		for (; i + 63 < inputLen; i += 64)
200			MD5Transform(context->state[0], context->state[1],
201			    context->state[2], context->state[3],
202			    &input[i], context);
203
204		if (inputLen == i)
205			return;
206
207		index = 0;
208	}
209
210	/* Buffer remaining input */
211	memcpy(&context->buffer[index], &input[i], inputLen - i);
212}
213
214/*
215 * MD5 finalization. Ends an MD5 message-digest operation, writing the
216 * the message digest and zeroizing the context.
217 */
218void
219MD5Final(unsigned char digest[MD5_DIGEST_LENGTH], MD5_CTX *context)
220{
221	unsigned char bits[8];
222	u_int32_t index = (context->count[0] >> 3) & 0x3f;
223
224	/* Save number of bits */
225	Encode(bits, context->count, 8);
226
227	/* Pad out to 56 mod 64. */
228	MD5Update(context, PADDING, ((index < 56) ? 56 : 120) - index);
229
230	/* Append length (before padding) */
231	MD5Update(context, bits, 8);
232
233	/* Store state in digest */
234	Encode(digest, context->state, 16);
235
236	/* Zeroize sensitive information. */
237	memset(context, 0, sizeof (*context));
238}
239
240/*
241 * MD5 basic transformation. Transforms state based on block.
242 */
243static void
244MD5Transform(u_int32_t a, u_int32_t b, u_int32_t c, u_int32_t d,
245    const u_int8_t block[64], MD5_CTX *context)
246{
247	/* Register (instead of array) is a win in most cases */
248	register u_int32_t x0, x1, x2, x3, x4, x5, x6, x7;
249	register u_int32_t x8, x9, x10, x11, x12, x13, x14, x15;
250
251	x15 = FETCH_32(block + 60);
252	x14 = FETCH_32(block + 56);
253	x13 = FETCH_32(block + 52);
254	x12 = FETCH_32(block + 48);
255	x11 = FETCH_32(block + 44);
256	x10 = FETCH_32(block + 40);
257	x9  = FETCH_32(block + 36);
258	x8  = FETCH_32(block + 32);
259	x7  = FETCH_32(block + 28);
260	x6  = FETCH_32(block + 24);
261	x5  = FETCH_32(block + 20);
262	x4  = FETCH_32(block + 16);
263	x3  = FETCH_32(block + 12);
264	x2  = FETCH_32(block +  8);
265	x1  = FETCH_32(block +  4);
266	x0  = FETCH_32(block +  0);
267
268	/* Round 1 */
269#define	S11 7
270#define	S12 12
271#define	S13 17
272#define	S14 22
273	FF(a, b, c, d, x0,  S11, 0xd76aa478UL); /* 1 */
274	FF(d, a, b, c, x1,  S12, 0xe8c7b756UL); /* 2 */
275	FF(c, d, a, b, x2,  S13, 0x242070dbUL); /* 3 */
276	FF(b, c, d, a, x3,  S14, 0xc1bdceeeUL); /* 4 */
277	FF(a, b, c, d, x4,  S11, 0xf57c0fafUL); /* 5 */
278	FF(d, a, b, c, x5,  S12, 0x4787c62aUL); /* 6 */
279	FF(c, d, a, b, x6,  S13, 0xa8304613UL); /* 7 */
280	FF(b, c, d, a, x7,  S14, 0xfd469501UL); /* 8 */
281	FF(a, b, c, d, x8,  S11, 0x698098d8UL); /* 9 */
282	FF(d, a, b, c, x9,  S12, 0x8b44f7afUL); /* 10 */
283	FF(c, d, a, b, x10, S13, 0xffff5bb1UL); /* 11 */
284	FF(b, c, d, a, x11, S14, 0x895cd7beUL); /* 12 */
285	FF(a, b, c, d, x12, S11, 0x6b901122UL); /* 13 */
286	FF(d, a, b, c, x13, S12, 0xfd987193UL); /* 14 */
287	FF(c, d, a, b, x14, S13, 0xa679438eUL); /* 15 */
288	FF(b, c, d, a, x15, S14, 0x49b40821UL); /* 16 */
289
290	/* Round 2 */
291#define	S21 5
292#define	S22 9
293#define	S23 14
294#define	S24 20
295	GG(a, b, c, d, x1,  S21, 0xf61e2562UL); /* 17 */
296	GG(d, a, b, c, x6,  S22, 0xc040b340UL); /* 18 */
297	GG(c, d, a, b, x11, S23, 0x265e5a51UL); /* 19 */
298	GG(b, c, d, a, x0,  S24, 0xe9b6c7aaUL); /* 20 */
299	GG(a, b, c, d, x5,  S21, 0xd62f105dUL); /* 21 */
300	GG(d, a, b, c, x10, S22, 0x02441453UL); /* 22 */
301	GG(c, d, a, b, x15, S23, 0xd8a1e681UL); /* 23 */
302	GG(b, c, d, a, x4,  S24, 0xe7d3fbc8UL); /* 24 */
303	GG(a, b, c, d, x9,  S21, 0x21e1cde6UL); /* 25 */
304	GG(d, a, b, c, x14, S22, 0xc33707d6UL); /* 26 */
305	GG(c, d, a, b, x3,  S23, 0xf4d50d87UL); /* 27 */
306	GG(b, c, d, a, x8,  S24, 0x455a14edUL); /* 28 */
307	GG(a, b, c, d, x13, S21, 0xa9e3e905UL); /* 29 */
308	GG(d, a, b, c, x2,  S22, 0xfcefa3f8UL); /* 30 */
309	GG(c, d, a, b, x7,  S23, 0x676f02d9UL); /* 31 */
310	GG(b, c, d, a, x12, S24, 0x8d2a4c8aUL); /* 32 */
311
312	/* Round 3 */
313#define	S31 4
314#define	S32 11
315#define	S33 16
316#define	S34 23
317	HH(a, b, c, d, x5,  S31, 0xfffa3942UL); /* 33 */
318	HH(d, a, b, c, x8,  S32, 0x8771f681UL); /* 34 */
319	HH(c, d, a, b, x11, S33, 0x6d9d6122UL); /* 35 */
320	HH(b, c, d, a, x14, S34, 0xfde5380cUL); /* 36 */
321	HH(a, b, c, d, x1,  S31, 0xa4beea44UL); /* 37 */
322	HH(d, a, b, c, x4,  S32, 0x4bdecfa9UL); /* 38 */
323	HH(c, d, a, b, x7,  S33, 0xf6bb4b60UL); /* 39 */
324	HH(b, c, d, a, x10, S34, 0xbebfbc70UL); /* 40 */
325	HH(a, b, c, d, x13, S31, 0x289b7ec6UL); /* 41 */
326	HH(d, a, b, c, x0,  S32, 0xeaa127faUL); /* 42 */
327	HH(c, d, a, b, x3,  S33, 0xd4ef3085UL); /* 43 */
328	HH(b, c, d, a, x6,  S34, 0x04881d05UL); /* 44 */
329	HH(a, b, c, d, x9,  S31, 0xd9d4d039UL); /* 45 */
330	HH(d, a, b, c, x12, S32, 0xe6db99e5UL); /* 46 */
331	HH(c, d, a, b, x15, S33, 0x1fa27cf8UL); /* 47 */
332	HH(b, c, d, a, x2,  S34, 0xc4ac5665UL); /* 48 */
333
334	/* Round 4 */
335#define	S41 6
336#define	S42 10
337#define	S43 15
338#define	S44 21
339	II(a, b, c, d, x0,  S41, 0xf4292244UL); /* 49 */
340	II(d, a, b, c, x7,  S42, 0x432aff97UL); /* 50 */
341	II(c, d, a, b, x14, S43, 0xab9423a7UL); /* 51 */
342	II(b, c, d, a, x5,  S44, 0xfc93a039UL); /* 52 */
343	II(a, b, c, d, x12, S41, 0x655b59c3UL); /* 53 */
344	II(d, a, b, c, x3,  S42, 0x8f0ccc92UL); /* 54 */
345	II(c, d, a, b, x10, S43, 0xffeff47dUL); /* 55 */
346	II(b, c, d, a, x1,  S44, 0x85845dd1UL); /* 56 */
347	II(a, b, c, d, x8,  S41, 0x6fa87e4fUL); /* 57 */
348	II(d, a, b, c, x15, S42, 0xfe2ce6e0UL); /* 58 */
349	II(c, d, a, b, x6,  S43, 0xa3014314UL); /* 59 */
350	II(b, c, d, a, x13, S44, 0x4e0811a1UL); /* 60 */
351	II(a, b, c, d, x4,  S41, 0xf7537e82UL); /* 61 */
352	II(d, a, b, c, x11, S42, 0xbd3af235UL); /* 62 */
353	II(c, d, a, b, x2,  S43, 0x2ad7d2bbUL); /* 63 */
354	II(b, c, d, a, x9,  S44, 0xeb86d391UL); /* 64 */
355
356	context->state[0] += a;
357	context->state[1] += b;
358	context->state[2] += c;
359	context->state[3] += d;
360
361	/* Zeroize sensitive information. */
362	x15 = x14 = x13 = x12 = x11 = x10 = x9 = x8 = 0;
363	x7 = x6 = x5 = x4 = x3 = x2 = x1 = x0 = 0;
364}
365