Chameleon

Chameleon Svn Source Tree

Root/branches/meklort/i386/libsaio/md5c.c

Source at commit 690 created 9 years 2 months ago.
By meklort, Compiles again. Only tested with Xcode 4.
1/*
2 * MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
3 *
4 * Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
5 * rights reserved.
6 *
7 * License to copy and use this software is granted provided that it
8 * is identified as the "RSA Data Security, Inc. MD5 Message-Digest
9 * Algorithm" in all material mentioning or referencing this software
10 * or this function.
11 *
12 * License is also granted to make and use derivative works provided
13 * that such works are identified as "derived from the RSA Data
14 * Security, Inc. MD5 Message-Digest Algorithm" in all material
15 * mentioning or referencing the derived work.
16 *
17 * RSA Data Security, Inc. makes no representations concerning either
18 * the merchantability of this software or the suitability of this
19 * software for any particular purpose. It is provided "as is"
20 * without express or implied warranty of any kind.
21 *
22 * These notices must be retained in any copies of any part of this
23 * documentation and/or software.
24 *
25 * $Id: md5c.c,v 1.1 2005/06/24 22:47:12 curtisg Exp $
26 *
27 * This code is the same as the code published by RSA Inc. It has been
28 * edited for clarity and style only.
29 */
30
31#include "libsaio.h"
32
33#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
34# include <Kernel/libkern/crypto/md5.h>
35#else
36# include <sys/md5.h>
37#endif
38
39
40#define Encode memcpy
41#define Decode memcpy
42
43static unsigned char PADDING[64] = {
44 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
45 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
46 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
47};
48
49/* F, G, H and I are basic MD5 functions. */
50#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
51#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
52#define H(x, y, z) ((x) ^ (y) ^ (z))
53#define I(x, y, z) ((y) ^ ((x) | (~z)))
54
55/* ROTATE_LEFT rotates x left n bits. */
56#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
57
58/*
59 * FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
60 * Rotation is separate from addition to prevent recomputation.
61 */
62#define FF(a, b, c, d, x, s, ac) { \
63(a) += F ((b), (c), (d)) + (x) + (u_int32_t)(ac); \
64(a) = ROTATE_LEFT ((a), (s)); \
65(a) += (b); \
66}
67#define GG(a, b, c, d, x, s, ac) { \
68(a) += G ((b), (c), (d)) + (x) + (u_int32_t)(ac); \
69(a) = ROTATE_LEFT ((a), (s)); \
70(a) += (b); \
71}
72#define HH(a, b, c, d, x, s, ac) { \
73(a) += H ((b), (c), (d)) + (x) + (u_int32_t)(ac); \
74(a) = ROTATE_LEFT ((a), (s)); \
75(a) += (b); \
76}
77#define II(a, b, c, d, x, s, ac) { \
78(a) += I ((b), (c), (d)) + (x) + (u_int32_t)(ac); \
79(a) = ROTATE_LEFT ((a), (s)); \
80(a) += (b); \
81}
82
83static void MD5Transform (u_int32_t state[4], const unsigned char block[64]);
84
85/* MD5 initialization. Begins an MD5 operation, writing a new context. */
86
87void
88MD5Init (context)
89MD5_CTX *context;
90{
91
92context->count[0] = context->count[1] = 0;
93
94/* Load magic initialization constants. */
95context->state[0] = 0x67452301;
96context->state[1] = 0xefcdab89;
97context->state[2] = 0x98badcfe;
98context->state[3] = 0x10325476;
99}
100
101/*
102 * MD5 block update operation. Continues an MD5 message-digest
103 * operation, processing another message block, and updating the
104 * context.
105 */
106
107void
108MD5Update (context, input, inputLen)
109MD5_CTX *context;
110#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
111const void *input;
112#else
113const unsigned char *input;
114#endif
115unsigned int inputLen;
116{
117unsigned int i, index, partLen;
118
119/* Compute number of bytes mod 64 */
120index = (unsigned int)((context->count[0] >> 3) & 0x3F);
121
122/* Update number of bits */
123if ((context->count[0] += ((u_int32_t)inputLen << 3))
124 < ((u_int32_t)inputLen << 3))
125context->count[1]++;
126context->count[1] += ((u_int32_t)inputLen >> 29);
127
128partLen = 64 - index;
129
130/* Transform as many times as possible. */
131if (inputLen >= partLen) {
132memcpy((void *)&context->buffer[index], (const void *)input,
133 partLen);
134MD5Transform (context->state, context->buffer);
135
136for (i = partLen; i + 63 < inputLen; i += 64)
137MD5Transform (context->state, &((const unsigned char *)input)[i]);
138
139index = 0;
140}
141else
142i = 0;
143
144/* Buffer remaining input */
145memcpy ((void *)&context->buffer[index], (const void *)&((const unsigned char*)input)[i],
146 inputLen-i);
147}
148
149/*
150 * MD5 padding. Adds padding followed by original length.
151 */
152
153void
154MD5Pad (context)
155MD5_CTX *context;
156{
157unsigned char bits[8];
158unsigned int index, padLen;
159
160/* Save number of bits */
161Encode (bits, context->count, 8);
162
163/* Pad out to 56 mod 64. */
164index = (unsigned int)((context->count[0] >> 3) & 0x3f);
165padLen = (index < 56) ? (56 - index) : (120 - index);
166MD5Update (context, PADDING, padLen);
167
168/* Append length (before padding) */
169MD5Update (context, bits, 8);
170}
171
172/*
173 * MD5 finalization. Ends an MD5 message-digest operation, writing the
174 * the message digest and zeroizing the context.
175 */
176
177void
178MD5Final (digest, context)
179unsigned char digest[16];
180MD5_CTX *context;
181{
182/* Do padding. */
183MD5Pad (context);
184
185/* Store state in digest */
186Encode (digest, context->state, 16);
187
188/* Zeroize sensitive information. */
189memset ((void *)context, 0, sizeof (*context));
190}
191
192/* MD5 basic transformation. Transforms state based on block. */
193
194static void
195MD5Transform (state, block)
196u_int32_t state[4];
197const unsigned char block[64];
198{
199u_int32_t a = state[0], b = state[1], c = state[2], d = state[3], x[16];
200
201Decode (x, block, 64);
202
203/* Round 1 */
204#define S11 7
205#define S12 12
206#define S13 17
207#define S14 22
208FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
209FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
210FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
211FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
212FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
213FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
214FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
215FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
216FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
217FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
218FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
219FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
220FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
221FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
222FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
223FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
224
225/* Round 2 */
226#define S21 5
227#define S22 9
228#define S23 14
229#define S24 20
230GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
231GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
232GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
233GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
234GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
235GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
236GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
237GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
238GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
239GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
240GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
241GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
242GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
243GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
244GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
245GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
246
247/* Round 3 */
248#define S31 4
249#define S32 11
250#define S33 16
251#define S34 23
252HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
253HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
254HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
255HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
256HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
257HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
258HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
259HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
260HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
261HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
262HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
263HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
264HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
265HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
266HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
267HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
268
269/* Round 4 */
270#define S41 6
271#define S42 10
272#define S43 15
273#define S44 21
274II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
275II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
276II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
277II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
278II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
279II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
280II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
281II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
282II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
283II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
284II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
285II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
286II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
287II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
288II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
289II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
290
291state[0] += a;
292state[1] += b;
293state[2] += c;
294state[3] += d;
295
296/* Zeroize sensitive information. */
297memset ((void *)x, 0, sizeof (x));
298}
299

Archive Download this file

Revision: 690