Root/
Source at commit 1078 created 13 years 1 month ago. By azimutz, Put back message removed by mistake on r1061. | |
---|---|
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 | ␊ |
43 | static 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 | ␊ |
83 | static 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 | ␊ |
87 | void␊ |
88 | MD5Init (context)␊ |
89 | ␉MD5_CTX *context;␊ |
90 | {␊ |
91 | ␊ |
92 | ␉context->count[0] = context->count[1] = 0;␊ |
93 | ␊ |
94 | ␉/* Load magic initialization constants. */␊ |
95 | ␉context->state[0] = 0x67452301;␊ |
96 | ␉context->state[1] = 0xefcdab89;␊ |
97 | ␉context->state[2] = 0x98badcfe;␊ |
98 | ␉context->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 | ␊ |
107 | void␊ |
108 | MD5Update (context, input, inputLen)␊ |
109 | ␉MD5_CTX *context;␊ |
110 | #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5␊ |
111 | ␉const void *input;␊ |
112 | #else␊ |
113 | ␉const unsigned char *input;␊ |
114 | #endif␊ |
115 | ␉unsigned int inputLen;␊ |
116 | {␊ |
117 | ␉unsigned int i, index, partLen;␊ |
118 | ␊ |
119 | ␉/* Compute number of bytes mod 64 */␊ |
120 | ␉index = (unsigned int)((context->count[0] >> 3) & 0x3F);␊ |
121 | ␊ |
122 | ␉/* Update number of bits */␊ |
123 | ␉if ((context->count[0] += ((u_int32_t)inputLen << 3))␊ |
124 | ␉ < ((u_int32_t)inputLen << 3))␊ |
125 | ␉␉context->count[1]++;␊ |
126 | ␉context->count[1] += ((u_int32_t)inputLen >> 29);␊ |
127 | ␊ |
128 | ␉partLen = 64 - index;␊ |
129 | ␊ |
130 | ␉/* Transform as many times as possible. */␊ |
131 | ␉if (inputLen >= partLen) {␊ |
132 | ␉␉memcpy((void *)&context->buffer[index], (const void *)input,␊ |
133 | ␉␉ partLen);␊ |
134 | ␉␉MD5Transform (context->state, context->buffer);␊ |
135 | ␊ |
136 | ␉␉for (i = partLen; i + 63 < inputLen; i += 64)␊ |
137 | ␉␉␉MD5Transform (context->state, &((const unsigned char *)input)[i]);␊ |
138 | ␊ |
139 | ␉␉index = 0;␊ |
140 | ␉}␊ |
141 | ␉else␊ |
142 | ␉␉i = 0;␊ |
143 | ␊ |
144 | ␉/* Buffer remaining input */␊ |
145 | ␉memcpy ((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 | ␊ |
153 | void␊ |
154 | MD5Pad (context)␊ |
155 | ␉MD5_CTX *context;␊ |
156 | {␊ |
157 | ␉unsigned char bits[8];␊ |
158 | ␉unsigned int index, padLen;␊ |
159 | ␊ |
160 | ␉/* Save number of bits */␊ |
161 | ␉Encode (bits, context->count, 8);␊ |
162 | ␊ |
163 | ␉/* Pad out to 56 mod 64. */␊ |
164 | ␉index = (unsigned int)((context->count[0] >> 3) & 0x3f);␊ |
165 | ␉padLen = (index < 56) ? (56 - index) : (120 - index);␊ |
166 | ␉MD5Update (context, PADDING, padLen);␊ |
167 | ␊ |
168 | ␉/* Append length (before padding) */␊ |
169 | ␉MD5Update (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 | ␊ |
177 | void␊ |
178 | MD5Final (digest, context)␊ |
179 | ␉unsigned char digest[16];␊ |
180 | ␉MD5_CTX *context;␊ |
181 | {␊ |
182 | ␉/* Do padding. */␊ |
183 | ␉MD5Pad (context);␊ |
184 | ␊ |
185 | ␉/* Store state in digest */␊ |
186 | ␉Encode (digest, context->state, 16);␊ |
187 | ␊ |
188 | ␉/* Zeroize sensitive information. */␊ |
189 | ␉memset ((void *)context, 0, sizeof (*context));␊ |
190 | }␊ |
191 | ␊ |
192 | /* MD5 basic transformation. Transforms state based on block. */␊ |
193 | ␊ |
194 | static void␊ |
195 | MD5Transform (state, block)␊ |
196 | ␉u_int32_t state[4];␊ |
197 | ␉const unsigned char block[64];␊ |
198 | {␊ |
199 | ␉u_int32_t a = state[0], b = state[1], c = state[2], d = state[3], x[16];␊ |
200 | ␊ |
201 | ␉Decode (x, block, 64);␊ |
202 | ␊ |
203 | ␉/* Round 1 */␊ |
204 | #define S11 7␊ |
205 | #define S12 12␊ |
206 | #define S13 17␊ |
207 | #define S14 22␊ |
208 | ␉FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */␊ |
209 | ␉FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */␊ |
210 | ␉FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */␊ |
211 | ␉FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */␊ |
212 | ␉FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */␊ |
213 | ␉FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */␊ |
214 | ␉FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */␊ |
215 | ␉FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */␊ |
216 | ␉FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */␊ |
217 | ␉FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */␊ |
218 | ␉FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */␊ |
219 | ␉FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */␊ |
220 | ␉FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */␊ |
221 | ␉FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */␊ |
222 | ␉FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */␊ |
223 | ␉FF (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␊ |
230 | ␉GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */␊ |
231 | ␉GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */␊ |
232 | ␉GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */␊ |
233 | ␉GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */␊ |
234 | ␉GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */␊ |
235 | ␉GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */␊ |
236 | ␉GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */␊ |
237 | ␉GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */␊ |
238 | ␉GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */␊ |
239 | ␉GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */␊ |
240 | ␉GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */␊ |
241 | ␉GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */␊ |
242 | ␉GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */␊ |
243 | ␉GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */␊ |
244 | ␉GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */␊ |
245 | ␉GG (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␊ |
252 | ␉HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */␊ |
253 | ␉HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */␊ |
254 | ␉HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */␊ |
255 | ␉HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */␊ |
256 | ␉HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */␊ |
257 | ␉HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */␊ |
258 | ␉HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */␊ |
259 | ␉HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */␊ |
260 | ␉HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */␊ |
261 | ␉HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */␊ |
262 | ␉HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */␊ |
263 | ␉HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */␊ |
264 | ␉HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */␊ |
265 | ␉HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */␊ |
266 | ␉HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */␊ |
267 | ␉HH (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␊ |
274 | ␉II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */␊ |
275 | ␉II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */␊ |
276 | ␉II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */␊ |
277 | ␉II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */␊ |
278 | ␉II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */␊ |
279 | ␉II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */␊ |
280 | ␉II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */␊ |
281 | ␉II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */␊ |
282 | ␉II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */␊ |
283 | ␉II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */␊ |
284 | ␉II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */␊ |
285 | ␉II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */␊ |
286 | ␉II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */␊ |
287 | ␉II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */␊ |
288 | ␉II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */␊ |
289 | ␉II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */␊ |
290 | ␊ |
291 | ␉state[0] += a;␊ |
292 | ␉state[1] += b;␊ |
293 | ␉state[2] += c;␊ |
294 | ␉state[3] += d;␊ |
295 | ␊ |
296 | ␉/* Zeroize sensitive information. */␊ |
297 | ␉memset ((void *)x, 0, sizeof (x));␊ |
298 | }␊ |
299 |