Chameleon

Chameleon Svn Source Tree

Root/branches/azimutz/Chazi/i386/libsaio/md5c.c

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#defineMD5_DIGEST_LENGTH16
65#defineFETCH_32(p)(*(const u_int32_t *)(p))
66
67// MD5 context.
68typedef struct {
69u_int32_t state[4];// state (ABCD)
70u_int32_t count[2];// number of bits, modulo 2^64 (lsb first)
71unsigned 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 */
78static void
79Encode(unsigned char *output, u_int32_t *input, unsigned int len)
80{
81unsigned int i, j;
82
83for (i = 0, j = 0; j < len; i++, j += 4) {
84*(u_int32_t *)(output + j) = input[i];
85}
86}
87
88static unsigned char PADDING[64] = { 0x80, /* zeros */ };
89
90// F, G, H and I are basic MD5 functions.
91#defineF(x, y, z)((((y) ^ (z)) & (x)) ^ (z))
92#defineG(x, y, z)((((x) ^ (y)) & (z)) ^ (y))
93#defineH(x, y, z)((x) ^ (y) ^ (z))
94#defineI(x, y, z)(((~(z)) | (x)) ^ (y))
95
96// ROTATE_LEFT rotates x left n bits.
97#defineROTATE_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#defineFF(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#defineGG(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#defineHH(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#defineII(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
127static 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 */
133void
134MD5Init(MD5_CTX *context)
135{
136context->count[0] = context->count[1] = 0;
137
138// Load magic initialization constants.
139context->state[0] = 0x67452301UL;
140context->state[1] = 0xefcdab89UL;
141context->state[2] = 0x98badcfeUL;
142context->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 */
150void
151MD5Update(MD5_CTX *context, const void *inpp, unsigned int inputLen)
152{
153u_int32_t i, index, partLen;
154const unsigned char *input = (const unsigned char *)inpp;
155
156// Compute number of bytes mod 64
157index = (context->count[0] >> 3) & 0x3F;
158
159// Update number of bits
160if ((context->count[0] += (inputLen << 3)) < (inputLen << 3))
161context->count[1]++;
162context->count[1] += (inputLen >> 29);
163
164partLen = 64 - index;
165
166// Transform as many times as possible.
167i = 0;
168if (inputLen >= partLen) {
169if (index != 0) {
170memcpy(&context->buffer[index], input, partLen);
171MD5Transform(context->state[0], context->state[1],
172 context->state[2], context->state[3],
173 context->buffer, context);
174i = partLen;
175}
176
177for (; i + 63 < inputLen; i += 64)
178MD5Transform(context->state[0], context->state[1],
179 context->state[2], context->state[3],
180 &input[i], context);
181
182if (inputLen == i)
183return;
184
185index = 0;
186}
187
188// Buffer remaining input
189memcpy(&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 */
196void
197MD5Final(unsigned char digest[MD5_DIGEST_LENGTH], MD5_CTX *context)
198{
199unsigned char bits[8];
200u_int32_t index = (context->count[0] >> 3) & 0x3f;
201
202// Save number of bits
203Encode(bits, context->count, 8);
204
205// Pad out to 56 mod 64.
206MD5Update(context, PADDING, ((index < 56) ? 56 : 120) - index);
207
208// Append length (before padding)
209MD5Update(context, bits, 8);
210
211// Store state in digest
212Encode(digest, context->state, 16);
213
214// Zeroize sensitive information.
215memset(context, 0, sizeof (*context));
216}
217
218/*
219 * MD5 basic transformation. Transforms state based on block.
220 */
221static void
222MD5Transform(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
226register u_int32_t x0, x1, x2, x3, x4, x5, x6, x7;
227register u_int32_t x8, x9, x10, x11, x12, x13, x14, x15;
228
229x15 = FETCH_32(block + 60);
230x14 = FETCH_32(block + 56);
231x13 = FETCH_32(block + 52);
232x12 = FETCH_32(block + 48);
233x11 = FETCH_32(block + 44);
234x10 = FETCH_32(block + 40);
235x9 = FETCH_32(block + 36);
236x8 = FETCH_32(block + 32);
237x7 = FETCH_32(block + 28);
238x6 = FETCH_32(block + 24);
239x5 = FETCH_32(block + 20);
240x4 = FETCH_32(block + 16);
241x3 = FETCH_32(block + 12);
242x2 = FETCH_32(block + 8);
243x1 = FETCH_32(block + 4);
244x0 = FETCH_32(block + 0);
245
246// Round 1
247#defineS11 7
248#defineS12 12
249#defineS13 17
250#defineS14 22
251FF(a, b, c, d, x0, S11, 0xd76aa478UL); // 1
252FF(d, a, b, c, x1, S12, 0xe8c7b756UL); // 2
253FF(c, d, a, b, x2, S13, 0x242070dbUL); // 3
254FF(b, c, d, a, x3, S14, 0xc1bdceeeUL); // 4
255FF(a, b, c, d, x4, S11, 0xf57c0fafUL); // 5
256FF(d, a, b, c, x5, S12, 0x4787c62aUL); // 6
257FF(c, d, a, b, x6, S13, 0xa8304613UL); // 7
258FF(b, c, d, a, x7, S14, 0xfd469501UL); // 8
259FF(a, b, c, d, x8, S11, 0x698098d8UL); // 9
260FF(d, a, b, c, x9, S12, 0x8b44f7afUL); // 10
261FF(c, d, a, b, x10, S13, 0xffff5bb1UL); // 11
262FF(b, c, d, a, x11, S14, 0x895cd7beUL); // 12
263FF(a, b, c, d, x12, S11, 0x6b901122UL); // 13
264FF(d, a, b, c, x13, S12, 0xfd987193UL); // 14
265FF(c, d, a, b, x14, S13, 0xa679438eUL); // 15
266FF(b, c, d, a, x15, S14, 0x49b40821UL); // 16
267
268// Round 2
269#defineS21 5
270#defineS22 9
271#defineS23 14
272#defineS24 20
273GG(a, b, c, d, x1, S21, 0xf61e2562UL); // 17
274GG(d, a, b, c, x6, S22, 0xc040b340UL); // 18
275GG(c, d, a, b, x11, S23, 0x265e5a51UL); // 19
276GG(b, c, d, a, x0, S24, 0xe9b6c7aaUL); // 20
277GG(a, b, c, d, x5, S21, 0xd62f105dUL); // 21
278GG(d, a, b, c, x10, S22, 0x02441453UL); // 22
279GG(c, d, a, b, x15, S23, 0xd8a1e681UL); // 23
280GG(b, c, d, a, x4, S24, 0xe7d3fbc8UL); // 24
281GG(a, b, c, d, x9, S21, 0x21e1cde6UL); // 25
282GG(d, a, b, c, x14, S22, 0xc33707d6UL); // 26
283GG(c, d, a, b, x3, S23, 0xf4d50d87UL); // 27
284GG(b, c, d, a, x8, S24, 0x455a14edUL); // 28
285GG(a, b, c, d, x13, S21, 0xa9e3e905UL); // 29
286GG(d, a, b, c, x2, S22, 0xfcefa3f8UL); // 30
287GG(c, d, a, b, x7, S23, 0x676f02d9UL); // 31
288GG(b, c, d, a, x12, S24, 0x8d2a4c8aUL); // 32
289
290// Round 3
291#defineS31 4
292#defineS32 11
293#defineS33 16
294#defineS34 23
295HH(a, b, c, d, x5, S31, 0xfffa3942UL); // 33
296HH(d, a, b, c, x8, S32, 0x8771f681UL); // 34
297HH(c, d, a, b, x11, S33, 0x6d9d6122UL); // 35
298HH(b, c, d, a, x14, S34, 0xfde5380cUL); // 36
299HH(a, b, c, d, x1, S31, 0xa4beea44UL); // 37
300HH(d, a, b, c, x4, S32, 0x4bdecfa9UL); // 38
301HH(c, d, a, b, x7, S33, 0xf6bb4b60UL); // 39
302HH(b, c, d, a, x10, S34, 0xbebfbc70UL); // 40
303HH(a, b, c, d, x13, S31, 0x289b7ec6UL); // 41
304HH(d, a, b, c, x0, S32, 0xeaa127faUL); // 42
305HH(c, d, a, b, x3, S33, 0xd4ef3085UL); // 43
306HH(b, c, d, a, x6, S34, 0x04881d05UL); // 44
307HH(a, b, c, d, x9, S31, 0xd9d4d039UL); // 45
308HH(d, a, b, c, x12, S32, 0xe6db99e5UL); // 46
309HH(c, d, a, b, x15, S33, 0x1fa27cf8UL); // 47
310HH(b, c, d, a, x2, S34, 0xc4ac5665UL); // 48
311
312// Round 4
313#defineS41 6
314#defineS42 10
315#defineS43 15
316#defineS44 21
317II(a, b, c, d, x0, S41, 0xf4292244UL); // 49
318II(d, a, b, c, x7, S42, 0x432aff97UL); // 50
319II(c, d, a, b, x14, S43, 0xab9423a7UL); // 51
320II(b, c, d, a, x5, S44, 0xfc93a039UL); // 52
321II(a, b, c, d, x12, S41, 0x655b59c3UL); // 53
322II(d, a, b, c, x3, S42, 0x8f0ccc92UL); // 54
323II(c, d, a, b, x10, S43, 0xffeff47dUL); // 55
324II(b, c, d, a, x1, S44, 0x85845dd1UL); // 56
325II(a, b, c, d, x8, S41, 0x6fa87e4fUL); // 57
326II(d, a, b, c, x15, S42, 0xfe2ce6e0UL); // 58
327II(c, d, a, b, x6, S43, 0xa3014314UL); // 59
328II(b, c, d, a, x13, S44, 0x4e0811a1UL); // 60
329II(a, b, c, d, x4, S41, 0xf7537e82UL); // 61
330II(d, a, b, c, x11, S42, 0xbd3af235UL); // 62
331II(c, d, a, b, x2, S43, 0x2ad7d2bbUL); // 63
332II(b, c, d, a, x9, S44, 0xeb86d391UL); // 64
333
334context->state[0] += a;
335context->state[1] += b;
336context->state[2] += c;
337context->state[3] += d;
338
339// Zeroize sensitive information.
340x15 = x14 = x13 = x12 = x11 = x10 = x9 = x8 = 0;
341x7 = x6 = x5 = x4 = x3 = x2 = x1 = x0 = 0;
342}
343

Archive Download this file

Revision: 816