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