camellia: fix camellia_self_test failure on compilers where char is unsigned by default
[tropicssl.git] / library / md5.c
blobf8b21c8dd96cd370dd7f136db1de3496f7048e76
1 /*
2 * RFC 1321 compliant MD5 implementation
4 * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
6 * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
14 * * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * * Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * * Neither the names of PolarSSL or XySSL nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * The MD5 algorithm was designed by Ron Rivest in 1991.
38 * http://www.ietf.org/rfc/rfc1321.txt
41 #include "tropicssl/config.h"
43 #if defined(TROPICSSL_MD5_C)
45 #include "tropicssl/md5.h"
47 #include <string.h>
48 #include <stdio.h>
51 * 32-bit integer manipulation macros (little endian)
53 #ifndef GET_ULONG_LE
54 #define GET_ULONG_LE(n,b,i) \
55 { \
56 (n) = ( (unsigned long) (b)[(i) ] ) \
57 | ( (unsigned long) (b)[(i) + 1] << 8 ) \
58 | ( (unsigned long) (b)[(i) + 2] << 16 ) \
59 | ( (unsigned long) (b)[(i) + 3] << 24 ); \
61 #endif
63 #ifndef PUT_ULONG_LE
64 #define PUT_ULONG_LE(n,b,i) \
65 { \
66 (b)[(i) ] = (unsigned char) ( (n) ); \
67 (b)[(i) + 1] = (unsigned char) ( (n) >> 8 ); \
68 (b)[(i) + 2] = (unsigned char) ( (n) >> 16 ); \
69 (b)[(i) + 3] = (unsigned char) ( (n) >> 24 ); \
71 #endif
74 * MD5 context setup
76 void md5_starts(md5_context * ctx)
78 ctx->total[0] = 0;
79 ctx->total[1] = 0;
81 ctx->state[0] = 0x67452301;
82 ctx->state[1] = 0xEFCDAB89;
83 ctx->state[2] = 0x98BADCFE;
84 ctx->state[3] = 0x10325476;
87 static void md5_process(md5_context * ctx, const unsigned char data[64])
89 unsigned long X[16], A, B, C, D;
91 GET_ULONG_LE(X[0], data, 0);
92 GET_ULONG_LE(X[1], data, 4);
93 GET_ULONG_LE(X[2], data, 8);
94 GET_ULONG_LE(X[3], data, 12);
95 GET_ULONG_LE(X[4], data, 16);
96 GET_ULONG_LE(X[5], data, 20);
97 GET_ULONG_LE(X[6], data, 24);
98 GET_ULONG_LE(X[7], data, 28);
99 GET_ULONG_LE(X[8], data, 32);
100 GET_ULONG_LE(X[9], data, 36);
101 GET_ULONG_LE(X[10], data, 40);
102 GET_ULONG_LE(X[11], data, 44);
103 GET_ULONG_LE(X[12], data, 48);
104 GET_ULONG_LE(X[13], data, 52);
105 GET_ULONG_LE(X[14], data, 56);
106 GET_ULONG_LE(X[15], data, 60);
108 #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
110 #define P(a,b,c,d,k,s,t) \
112 a += F(b,c,d) + X[k] + t; a = S(a,s) + b; \
115 A = ctx->state[0];
116 B = ctx->state[1];
117 C = ctx->state[2];
118 D = ctx->state[3];
120 #define F(x,y,z) (z ^ (x & (y ^ z)))
122 P(A, B, C, D, 0, 7, 0xD76AA478);
123 P(D, A, B, C, 1, 12, 0xE8C7B756);
124 P(C, D, A, B, 2, 17, 0x242070DB);
125 P(B, C, D, A, 3, 22, 0xC1BDCEEE);
126 P(A, B, C, D, 4, 7, 0xF57C0FAF);
127 P(D, A, B, C, 5, 12, 0x4787C62A);
128 P(C, D, A, B, 6, 17, 0xA8304613);
129 P(B, C, D, A, 7, 22, 0xFD469501);
130 P(A, B, C, D, 8, 7, 0x698098D8);
131 P(D, A, B, C, 9, 12, 0x8B44F7AF);
132 P(C, D, A, B, 10, 17, 0xFFFF5BB1);
133 P(B, C, D, A, 11, 22, 0x895CD7BE);
134 P(A, B, C, D, 12, 7, 0x6B901122);
135 P(D, A, B, C, 13, 12, 0xFD987193);
136 P(C, D, A, B, 14, 17, 0xA679438E);
137 P(B, C, D, A, 15, 22, 0x49B40821);
139 #undef F
141 #define F(x,y,z) (y ^ (z & (x ^ y)))
143 P(A, B, C, D, 1, 5, 0xF61E2562);
144 P(D, A, B, C, 6, 9, 0xC040B340);
145 P(C, D, A, B, 11, 14, 0x265E5A51);
146 P(B, C, D, A, 0, 20, 0xE9B6C7AA);
147 P(A, B, C, D, 5, 5, 0xD62F105D);
148 P(D, A, B, C, 10, 9, 0x02441453);
149 P(C, D, A, B, 15, 14, 0xD8A1E681);
150 P(B, C, D, A, 4, 20, 0xE7D3FBC8);
151 P(A, B, C, D, 9, 5, 0x21E1CDE6);
152 P(D, A, B, C, 14, 9, 0xC33707D6);
153 P(C, D, A, B, 3, 14, 0xF4D50D87);
154 P(B, C, D, A, 8, 20, 0x455A14ED);
155 P(A, B, C, D, 13, 5, 0xA9E3E905);
156 P(D, A, B, C, 2, 9, 0xFCEFA3F8);
157 P(C, D, A, B, 7, 14, 0x676F02D9);
158 P(B, C, D, A, 12, 20, 0x8D2A4C8A);
160 #undef F
162 #define F(x,y,z) (x ^ y ^ z)
164 P(A, B, C, D, 5, 4, 0xFFFA3942);
165 P(D, A, B, C, 8, 11, 0x8771F681);
166 P(C, D, A, B, 11, 16, 0x6D9D6122);
167 P(B, C, D, A, 14, 23, 0xFDE5380C);
168 P(A, B, C, D, 1, 4, 0xA4BEEA44);
169 P(D, A, B, C, 4, 11, 0x4BDECFA9);
170 P(C, D, A, B, 7, 16, 0xF6BB4B60);
171 P(B, C, D, A, 10, 23, 0xBEBFBC70);
172 P(A, B, C, D, 13, 4, 0x289B7EC6);
173 P(D, A, B, C, 0, 11, 0xEAA127FA);
174 P(C, D, A, B, 3, 16, 0xD4EF3085);
175 P(B, C, D, A, 6, 23, 0x04881D05);
176 P(A, B, C, D, 9, 4, 0xD9D4D039);
177 P(D, A, B, C, 12, 11, 0xE6DB99E5);
178 P(C, D, A, B, 15, 16, 0x1FA27CF8);
179 P(B, C, D, A, 2, 23, 0xC4AC5665);
181 #undef F
183 #define F(x,y,z) (y ^ (x | ~z))
185 P(A, B, C, D, 0, 6, 0xF4292244);
186 P(D, A, B, C, 7, 10, 0x432AFF97);
187 P(C, D, A, B, 14, 15, 0xAB9423A7);
188 P(B, C, D, A, 5, 21, 0xFC93A039);
189 P(A, B, C, D, 12, 6, 0x655B59C3);
190 P(D, A, B, C, 3, 10, 0x8F0CCC92);
191 P(C, D, A, B, 10, 15, 0xFFEFF47D);
192 P(B, C, D, A, 1, 21, 0x85845DD1);
193 P(A, B, C, D, 8, 6, 0x6FA87E4F);
194 P(D, A, B, C, 15, 10, 0xFE2CE6E0);
195 P(C, D, A, B, 6, 15, 0xA3014314);
196 P(B, C, D, A, 13, 21, 0x4E0811A1);
197 P(A, B, C, D, 4, 6, 0xF7537E82);
198 P(D, A, B, C, 11, 10, 0xBD3AF235);
199 P(C, D, A, B, 2, 15, 0x2AD7D2BB);
200 P(B, C, D, A, 9, 21, 0xEB86D391);
202 #undef F
204 ctx->state[0] += A;
205 ctx->state[1] += B;
206 ctx->state[2] += C;
207 ctx->state[3] += D;
211 * MD5 process buffer
213 void md5_update(md5_context * ctx, const unsigned char *input, int ilen)
215 int fill;
216 unsigned long left;
218 if (ilen <= 0)
219 return;
221 left = ctx->total[0] & 0x3F;
222 fill = 64 - left;
224 ctx->total[0] += ilen;
225 ctx->total[0] &= 0xFFFFFFFF;
227 if (ctx->total[0] < (unsigned long)ilen)
228 ctx->total[1]++;
230 if (left && ilen >= fill) {
231 memcpy((void *)(ctx->buffer + left), (const void *)input, fill);
232 md5_process(ctx, ctx->buffer);
233 input += fill;
234 ilen -= fill;
235 left = 0;
238 while (ilen >= 64) {
239 md5_process(ctx, input);
240 input += 64;
241 ilen -= 64;
244 if (ilen > 0) {
245 memcpy((void *)(ctx->buffer + left), (const void *)input, ilen);
249 static const unsigned char md5_padding[64] = {
250 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
251 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
252 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
253 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
257 * MD5 final digest
259 void md5_finish(md5_context * ctx, unsigned char output[16])
261 unsigned long last, padn;
262 unsigned long high, low;
263 unsigned char msglen[8];
265 high = (ctx->total[0] >> 29)
266 | (ctx->total[1] << 3);
267 low = (ctx->total[0] << 3);
269 PUT_ULONG_LE(low, msglen, 0);
270 PUT_ULONG_LE(high, msglen, 4);
272 last = ctx->total[0] & 0x3F;
273 padn = (last < 56) ? (56 - last) : (120 - last);
275 md5_update(ctx, md5_padding, padn);
276 md5_update(ctx, msglen, 8);
278 PUT_ULONG_LE(ctx->state[0], output, 0);
279 PUT_ULONG_LE(ctx->state[1], output, 4);
280 PUT_ULONG_LE(ctx->state[2], output, 8);
281 PUT_ULONG_LE(ctx->state[3], output, 12);
285 * output = MD5( input buffer )
287 void md5(const unsigned char *input, int ilen, unsigned char output[16])
289 md5_context ctx;
291 md5_starts(&ctx);
292 md5_update(&ctx, input, ilen);
293 md5_finish(&ctx, output);
295 memset(&ctx, 0, sizeof(md5_context));
299 * output = MD5( file contents )
301 int md5_file(const char *path, unsigned char output[16])
303 FILE *f;
304 size_t n;
305 md5_context ctx;
306 unsigned char buf[1024];
308 if ((f = fopen(path, "rb")) == NULL)
309 return (1);
311 md5_starts(&ctx);
313 while ((n = fread(buf, 1, sizeof(buf), f)) > 0)
314 md5_update(&ctx, buf, (int)n);
316 md5_finish(&ctx, output);
318 memset(&ctx, 0, sizeof(md5_context));
320 if (ferror(f) != 0) {
321 fclose(f);
322 return (2);
325 fclose(f);
326 return (0);
330 * MD5 HMAC context setup
332 void md5_hmac_starts(md5_context * ctx, const unsigned char *key, int keylen)
334 int i;
335 unsigned char sum[16];
337 if (keylen > 64) {
338 md5(key, keylen, sum);
339 keylen = 16;
340 key = sum;
343 memset(ctx->ipad, 0x36, 64);
344 memset(ctx->opad, 0x5C, 64);
346 for (i = 0; i < keylen; i++) {
347 ctx->ipad[i] = (unsigned char)(ctx->ipad[i] ^ key[i]);
348 ctx->opad[i] = (unsigned char)(ctx->opad[i] ^ key[i]);
351 md5_starts(ctx);
352 md5_update(ctx, ctx->ipad, 64);
354 memset(sum, 0, sizeof(sum));
358 * MD5 HMAC process buffer
360 void md5_hmac_update(md5_context * ctx, const unsigned char *input, int ilen)
362 md5_update(ctx, input, ilen);
366 * MD5 HMAC final digest
368 void md5_hmac_finish(md5_context * ctx, unsigned char output[16])
370 unsigned char tmpbuf[16];
372 md5_finish(ctx, tmpbuf);
373 md5_starts(ctx);
374 md5_update(ctx, ctx->opad, 64);
375 md5_update(ctx, tmpbuf, 16);
376 md5_finish(ctx, output);
378 memset(tmpbuf, 0, sizeof(tmpbuf));
382 * output = HMAC-MD5( hmac key, input buffer )
384 void md5_hmac(const unsigned char *key, int keylen,
385 const unsigned char *input, int ilen,
386 unsigned char output[16])
388 md5_context ctx;
390 md5_hmac_starts(&ctx, key, keylen);
391 md5_hmac_update(&ctx, input, ilen);
392 md5_hmac_finish(&ctx, output);
394 memset(&ctx, 0, sizeof(md5_context));
397 #if defined(TROPICSSL_SELF_TEST)
399 * RFC 1321 test vectors
401 static unsigned char md5_test_buf[7][81] = {
402 {""},
403 {"a"},
404 {"abc"},
405 {"message digest"},
406 {"abcdefghijklmnopqrstuvwxyz"},
407 {"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"},
409 "12345678901234567890123456789012345678901234567890123456789012"
410 "345678901234567890"}
413 static const int md5_test_buflen[7] = {
414 0, 1, 3, 14, 26, 62, 80
417 static const unsigned char md5_test_sum[7][16] = {
419 0xD4, 0x1D, 0x8C, 0xD9, 0x8F, 0x00, 0xB2, 0x04,
420 0xE9, 0x80, 0x09, 0x98, 0xEC, 0xF8, 0x42, 0x7E},
422 0x0C, 0xC1, 0x75, 0xB9, 0xC0, 0xF1, 0xB6, 0xA8,
423 0x31, 0xC3, 0x99, 0xE2, 0x69, 0x77, 0x26, 0x61},
425 0x90, 0x01, 0x50, 0x98, 0x3C, 0xD2, 0x4F, 0xB0,
426 0xD6, 0x96, 0x3F, 0x7D, 0x28, 0xE1, 0x7F, 0x72},
428 0xF9, 0x6B, 0x69, 0x7D, 0x7C, 0xB7, 0x93, 0x8D,
429 0x52, 0x5A, 0x2F, 0x31, 0xAA, 0xF1, 0x61, 0xD0},
431 0xC3, 0xFC, 0xD3, 0xD7, 0x61, 0x92, 0xE4, 0x00,
432 0x7D, 0xFB, 0x49, 0x6C, 0xCA, 0x67, 0xE1, 0x3B},
434 0xD1, 0x74, 0xAB, 0x98, 0xD2, 0x77, 0xD9, 0xF5,
435 0xA5, 0x61, 0x1C, 0x2C, 0x9F, 0x41, 0x9D, 0x9F},
437 0x57, 0xED, 0xF4, 0xA2, 0x2B, 0xE3, 0xC9, 0x55,
438 0xAC, 0x49, 0xDA, 0x2E, 0x21, 0x07, 0xB6, 0x7A}
442 * RFC 2202 test vectors
444 static unsigned char md5_hmac_test_key[7][26] = {
445 {"\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B"},
446 {"Jefe"},
447 {"\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"},
449 "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10"
450 "\x11\x12\x13\x14\x15\x16\x17\x18\x19"},
451 {"\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C"},
452 {""}, /* 0xAA 80 times */
453 {""}
456 static const int md5_hmac_test_keylen[7] = {
457 16, 4, 16, 25, 16, 80, 80
460 static unsigned char md5_hmac_test_buf[7][74] = {
461 {"Hi There"},
462 {"what do ya want for nothing?"},
464 "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
465 "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
466 "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
467 "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
468 "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"},
470 "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
471 "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
472 "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
473 "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
474 "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"},
475 {"Test With Truncation"},
476 {"Test Using Larger Than Block-Size Key - Hash Key First"},
478 "Test Using Larger Than Block-Size Key and Larger"
479 " Than One Block-Size Data"}
482 static const int md5_hmac_test_buflen[7] = {
483 8, 28, 50, 50, 20, 54, 73
486 static const unsigned char md5_hmac_test_sum[7][16] = {
488 0x92, 0x94, 0x72, 0x7A, 0x36, 0x38, 0xBB, 0x1C,
489 0x13, 0xF4, 0x8E, 0xF8, 0x15, 0x8B, 0xFC, 0x9D},
491 0x75, 0x0C, 0x78, 0x3E, 0x6A, 0xB0, 0xB5, 0x03,
492 0xEA, 0xA8, 0x6E, 0x31, 0x0A, 0x5D, 0xB7, 0x38},
494 0x56, 0xBE, 0x34, 0x52, 0x1D, 0x14, 0x4C, 0x88,
495 0xDB, 0xB8, 0xC7, 0x33, 0xF0, 0xE8, 0xB3, 0xF6},
497 0x69, 0x7E, 0xAF, 0x0A, 0xCA, 0x3A, 0x3A, 0xEA,
498 0x3A, 0x75, 0x16, 0x47, 0x46, 0xFF, 0xAA, 0x79},
500 0x56, 0x46, 0x1E, 0xF2, 0x34, 0x2E, 0xDC, 0x00,
501 0xF9, 0xBA, 0xB9, 0x95},
503 0x6B, 0x1A, 0xB7, 0xFE, 0x4B, 0xD7, 0xBF, 0x8F,
504 0x0B, 0x62, 0xE6, 0xCE, 0x61, 0xB9, 0xD0, 0xCD},
506 0x6F, 0x63, 0x0F, 0xAD, 0x67, 0xCD, 0xA0, 0xEE,
507 0x1F, 0xB1, 0xF5, 0x62, 0xDB, 0x3A, 0xA5, 0x3E}
511 * Checkup routine
513 int md5_self_test(int verbose)
515 int i, buflen;
516 unsigned char buf[1024];
517 unsigned char md5sum[16];
518 md5_context ctx;
520 for (i = 0; i < 7; i++) {
521 if (verbose != 0)
522 printf(" MD5 test #%d: ", i + 1);
524 md5(md5_test_buf[i], md5_test_buflen[i], md5sum);
526 if (memcmp(md5sum, md5_test_sum[i], 16) != 0) {
527 if (verbose != 0)
528 printf("failed\n");
530 return (1);
533 if (verbose != 0)
534 printf("passed\n");
537 if (verbose != 0)
538 printf("\n");
540 for (i = 0; i < 7; i++) {
541 if (verbose != 0)
542 printf(" HMAC-MD5 test #%d: ", i + 1);
544 if (i == 5 || i == 6) {
545 memset(buf, '\xAA', buflen = 80);
546 md5_hmac_starts(&ctx, buf, buflen);
547 } else
548 md5_hmac_starts(&ctx, md5_hmac_test_key[i],
549 md5_hmac_test_keylen[i]);
551 md5_hmac_update(&ctx, md5_hmac_test_buf[i],
552 md5_hmac_test_buflen[i]);
554 md5_hmac_finish(&ctx, md5sum);
556 buflen = (i == 4) ? 12 : 16;
558 if (memcmp(md5sum, md5_hmac_test_sum[i], buflen) != 0) {
559 if (verbose != 0)
560 printf("failed\n");
562 return (1);
565 if (verbose != 0)
566 printf("passed\n");
569 if (verbose != 0)
570 printf("\n");
572 return (0);
575 #endif
577 #endif