Debian Codegraph @

Framework/Pantomime/CWMD5.m

raw
1 /*
2 ** CWMD5.m
3 **
4 ** Copyright (c) 2002-2007 Ludovic Marcotte
5 **
6 ** Author: Ludovic Marcotte <[email protected]>
7 **
8 ** This library is free software; you can redistribute it and/or
9 ** modify it under the terms of the GNU Lesser General Public
10 ** License as published by the Free Software Foundation; either
11 ** version 2.1 of the License, or (at your option) any later version.
12 **
13 ** This library is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ** Lesser General Public License for more details.
17 **
18 ** You should have received a copy of the GNU General Public License
19 ** along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #import <Pantomime/CWMD5.h>
23 #import <Pantomime/CWConstants.h>
24 #import <Pantomime/NSData+Extensions.h>
25
26 #include <string.h>
27 #include <sys/types.h> // For byte order on Mac OS X.
28
29 #define word32 unsigned int
30
31 struct MD5Context {
32 word32 buf[4];
33 word32 bits[2];
34 unsigned char in[64];
35 };
36
37 void MD5Init(struct MD5Context *context);
38 void MD5Update(struct MD5Context *context, unsigned char const *buf,
39 unsigned len);
40 void MD5Final(unsigned char digest[16], struct MD5Context *context);
41 void MD5Transform(word32 buf[4], word32 const in[16]);
42
43 void md5_hmac(unsigned char *digest, const unsigned char* text, int text_len, const unsigned char* key, int key_len);
44
45 //
46 //
47 //
48 @implementation CWMD5
49
50 - (id) initWithData: (NSData *) theData
51 {
52 self = [super init];
53 if (self)
54 {
55 _data = RETAIN(theData);
56 _has_computed_digest = NO;
57 }
58 return self;
59 }
60
61
62 //
63 //
64 //
65 - (void) dealloc
66 {
67 RELEASE(_data);
68 [super dealloc];
69 }
70
71
72 //
73 //
74 //
75 - (void) computeDigest
76 {
77 struct MD5Context ctx;
78 unsigned char *bytes;
79 unsigned len;
80
81 // If we already have computed the digest
82 if (_has_computed_digest)
83 {
84 return;
85 }
86
87 bytes = (unsigned char *)[_data bytes];
88 len = [_data length];
89
90 MD5Init(&ctx);
91 MD5Update(&ctx, bytes, len);
92 MD5Final(_digest, &ctx);
93
94 _has_computed_digest = YES;
95 }
96
97
98 //
99 //
100 //
101 - (NSData *) digest
102 {
103 if (!_has_computed_digest)
104 {
105 return nil;
106 }
107
108 return [NSData dataWithBytes: _digest length: 16];
109 }
110
111
112 //
113 //
114 //
115 - (NSString *) digestAsString
116 {
117 if (!_has_computed_digest)
118 {
119 return nil;
120 }
121 else
122 {
123 NSMutableString *aMutableString;
124 int i;
125
126 aMutableString = [[NSMutableString alloc] init];
127
128 for (i = 0; i < 16; i++)
129 {
130 [aMutableString appendFormat: @"%02x", (unsigned int)_digest[i]];
131 }
132
133 return AUTORELEASE(aMutableString);
134 }
135 }
136
137
138 //
139 // The challenge phrase used is the one that has been initialized
140 // with this object. The digest MUST have been computed first.
141 //
142 - (NSString *) hmacAsStringUsingPassword: (NSString *) thePassword
143 {
144 if (!_has_computed_digest)
145 {
146 return nil;
147 }
148 else
149 {
150 NSMutableString *aMutableString;
151 unsigned char result[16];
152 unsigned char *s;
153 int i;
154
155 s = (unsigned char*)[_data cString];
156 md5_hmac(result, s, strlen((char*)s), (unsigned char*)[thePassword cString], [thePassword length]);
157
158 aMutableString = [[NSMutableString alloc] init];
159
160 for (i = 0; i < 16; i++)
161 {
162 [aMutableString appendFormat: @"%02x", (unsigned int)result[i]];
163 }
164
165 return AUTORELEASE(aMutableString);
166 }
167 }
168
169 @end
170
171
172 /*
173 * This code implements the MD5 message-digest algorithm.
174 * The algorithm is due to Ron Rivest. This code was
175 * written by Colin Plumb in 1993, no copyright is claimed.
176 * This code is in the public domain; do with it what you wish.
177 *
178 * Equivalent code is available from RSA Data Security, Inc.
179 * This code has been tested against that, and is equivalent,
180 * except that you don't need to include two pages of legalese
181 * with every copy.
182 *
183 * To compute the message digest of a chunk of bytes, declare an
184 * MD5Context structure, pass it to MD5Init, call MD5Update as
185 * needed on buffers full of bytes, and then call MD5Final, which
186 * will fill a supplied 16-byte array with the digest.
187 */
188 #if BYTE_ORDER == BIG_ENDIAN
189 static void byteReverse(unsigned char *buf, unsigned longs);
190
191 /*
192 * Note: this code is harmless on little-endian machines.
193 */
194 static void byteReverse(unsigned char *buf, unsigned longs)
195 {
196 word32 t;
197 do {
198 t = (word32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
199 ((unsigned) buf[1] << 8 | buf[0]);
200 *(word32 *) buf = t;
201 buf += 4;
202 } while (--longs);
203 }
204 #elif BYTE_ORDER == LITTLE_ENDIAN
205 #define byteReverse(buf, len) /* Nothing */
206 #else
207 #warning BROKEN - Compiler byte order check failed for some reason. The MD5 code might not work properly.
208 #endif
209
210 /*
211 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
212 * initialization constants.
213 */
214 void MD5Init(struct MD5Context *ctx)
215 {
216 ctx->buf[0] = 0x67452301;
217 ctx->buf[1] = 0xefcdab89;
218 ctx->buf[2] = 0x98badcfe;
219 ctx->buf[3] = 0x10325476;
220
221 ctx->bits[0] = 0;
222 ctx->bits[1] = 0;
223 }
224
225 /*
226 * Update context to reflect the concatenation of another buffer full
227 * of bytes.
228 */
229 void MD5Update(struct MD5Context *ctx, unsigned char const *buf, unsigned len)
230 {
231 register word32 t;
232
233 /* Update bitcount */
234 t = ctx->bits[0];
235 if ((ctx->bits[0] = t + ((word32) len << 3)) < t)
236 ctx->bits[1]++; /* Carry from low to high */
237 ctx->bits[1] += len >> 29;
238
239 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
240
241 /* Handle any leading odd-sized chunks */
242 if (t) {
243 unsigned char *p = (unsigned char *) ctx->in + t;
244
245 t = 64 - t;
246 if (len < t) {
247 memmove(p, buf, len);
248 return;
249 }
250 memmove(p, buf, t);
251 byteReverse(ctx->in, 16);
252 MD5Transform(ctx->buf, (word32 *) ctx->in);
253 buf += t;
254 len -= t;
255 }
256 /* Process data in 64-byte chunks */
257
258 while (len >= 64) {
259 memmove(ctx->in, buf, 64);
260 byteReverse(ctx->in, 16);
261 MD5Transform(ctx->buf, (word32 *) ctx->in);
262 buf += 64;
263 len -= 64;
264 }
265
266 /* Handle any remaining bytes of data. */
267 memmove(ctx->in, buf, len);
268 }
269
270 /*
271 * Final wrapup - pad to 64-byte boundary with the bit pattern
272 * 1 0* (64-bit count of bits processed, MSB-first)
273 */
274 void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
275 {
276 unsigned int count;
277 unsigned char *p;
278
279 /* Compute number of bytes mod 64 */
280 count = (ctx->bits[0] >> 3) & 0x3F;
281
282 /* Set the first char of padding to 0x80. This is safe since there is
283 always at least one byte free */
284 p = ctx->in + count;
285 *p++ = 0x80;
286
287 /* Bytes of padding needed to make 64 bytes */
288 count = 64 - 1 - count;
289
290 /* Pad out to 56 mod 64 */
291 if (count < 8) {
292 /* Two lots of padding: Pad the first block to 64 bytes */
293 memset(p, 0, count);
294 byteReverse(ctx->in, 16);
295 MD5Transform(ctx->buf, (word32 *) ctx->in);
296
297 /* Now fill the next block with 56 bytes */
298 memset(ctx->in, 0, 56);
299 } else {
300 /* Pad block to 56 bytes */
301 memset(p, 0, count - 8);
302 }
303 byteReverse(ctx->in, 14);
304
305 /* Append length in bits and transform */
306 ((word32 *) ctx->in)[14] = ctx->bits[0];
307 ((word32 *) ctx->in)[15] = ctx->bits[1];
308
309 MD5Transform(ctx->buf, (word32 *) ctx->in);
310 byteReverse((unsigned char *) ctx->buf, 4);
311 memmove(digest, ctx->buf, 16);
312 memset(&ctx, 0, sizeof(ctx)); /* In case it's sensitive */
313 }
314
315 /* The four core functions - F1 is optimized somewhat */
316
317 /* #define F1(x, y, z) (x & y | ~x & z) */
318 #define F1(x, y, z) (z ^ (x & (y ^ z)))
319 #define F2(x, y, z) F1(z, x, y)
320 #define F3(x, y, z) (x ^ y ^ z)
321 #define F4(x, y, z) (y ^ (x | ~z))
322
323 /* This is the central step in the MD5 algorithm. */
324 #define MD5STEP(f, w, x, y, z, data, s) \
325 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
326
327 /*
328 * The core of the MD5 algorithm, this alters an existing MD5 hash to
329 * reflect the addition of 16 longwords of new data. MD5Update blocks
330 * the data and converts bytes into longwords for this routine.
331 */
332 void MD5Transform(word32 buf[4], word32 const in[16])
333 {
334 register word32 a, b, c, d;
335
336 a = buf[0];
337 b = buf[1];
338 c = buf[2];
339 d = buf[3];
340
341 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
342 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
343 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
344 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
345 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
346 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
347 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
348 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
349 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
350 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
351 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
352 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
353 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
354 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
355 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
356 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
357
358 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
359 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
360 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
361 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
362 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
363 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
364 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
365 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
366 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
367 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
368 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
369 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
370 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
371 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
372 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
373 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
374
375 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
376 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
377 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
378 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
379 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
380 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
381 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
382 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
383 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
384 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
385 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
386 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
387 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
388 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
389 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
390 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
391
392 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
393 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
394 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
395 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
396 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
397 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
398 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
399 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
400 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
401 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
402 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
403 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
404 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
405 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
406 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
407 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
408
409 buf[0] += a;
410 buf[1] += b;
411 buf[2] += c;
412 buf[3] += d;
413 }
414
415 /*
416 ** Function: md5_hmac
417 ** Taken from the file RFC2104
418 ** Written by Martin Schaaf <[email protected]>, modified by Ludovic Marcotte <[email protected]>
419 */
420 void
421 md5_hmac(unsigned char *digest,
422 const unsigned char* text, int text_len,
423 const unsigned char* key, int key_len)
424 {
425 struct MD5Context context;
426 unsigned char k_ipad[64]; /* inner padding -
427 * key XORd with ipad
428 */
429 unsigned char k_opad[64]; /* outer padding -
430 * key XORd with opad
431 */
432 /* unsigned char tk[16]; */
433 int i;
434
435 /* start out by storing key in pads */
436 memset(k_ipad, 0, sizeof k_ipad);
437 memset(k_opad, 0, sizeof k_opad);
438
439 if (key_len > 64)
440 {
441 /* if key is longer than 64 bytes reset it to key=MD5(key) */
442 struct MD5Context tctx;
443
444 MD5Init(&tctx);
445 MD5Update(&tctx, key, key_len);
446 MD5Final(k_ipad, &tctx);
447 MD5Final(k_opad, &tctx);
448 }
449 else
450 {
451 memcpy(k_ipad, key, key_len);
452 memcpy(k_opad, key, key_len);
453 }
454
455 /*
456 * the HMAC_MD5 transform looks like:
457 *
458 * MD5(K XOR opad, MD5(K XOR ipad, text))
459 *
460 * where K is an n byte key
461 * ipad is the byte 0x36 repeated 64 times
462 * opad is the byte 0x5c repeated 64 times
463 * and text is the data being protected
464 */
465
466
467 /* XOR key with ipad and opad values */
468 for (i = 0; i < 64; i++)
469 {
470 k_ipad[i] ^= 0x36;
471 k_opad[i] ^= 0x5c;
472 }
473
474 /*
475 * perform inner MD5
476 */
477 MD5Init(&context); /* init context for 1st
478 * pass */
479 MD5Update(&context, k_ipad, 64); /* start with inner pad */
480 MD5Update(&context, text, text_len); /* then text of datagram */
481 MD5Final(digest, &context); /* finish up 1st pass */
482
483 /*
484 * perform outer MD5
485 */
486 MD5Init(&context); /* init context for 2nd
487 * pass */
488 MD5Update(&context, k_opad, 64); /* start with outer pad */
489 MD5Update(&context, digest, 16); /* then results of 1st
490 * hash */
491 MD5Final(digest, &context); /* finish up 2nd pass */
492 }