Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 1984