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