comparison libtomcrypt/src/hashes/md5.c @ 1471:6dba84798cd5

Update to libtomcrypt 1.18.1, merged with Dropbear changes
author Matt Johnston <matt@ucc.asn.au>
date Fri, 09 Feb 2018 21:44:05 +0800
parents f849a5ca2efc
children
comparison
equal deleted inserted replaced
1470:8bba51a55704 1471:6dba84798cd5
3 * LibTomCrypt is a library that provides various cryptographic 3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner. 4 * algorithms in a highly modular and flexible manner.
5 * 5 *
6 * The library is free for all purposes without any express 6 * The library is free for all purposes without any express
7 * guarantee it works. 7 * guarantee it works.
8 *
9 * Tom St Denis, [email protected], http://libtom.org
10 */ 8 */
11 #include "tomcrypt.h" 9 #include "tomcrypt.h"
12 10
13 11
14 /** 12 /**
15 @file md5.c 13 @file md5.c
16 LTC_MD5 hash function by Tom St Denis 14 LTC_MD5 hash function by Tom St Denis
17 */ 15 */
18 16
19 #ifdef LTC_MD5 17 #ifdef LTC_MD5
20 18
21 const struct ltc_hash_descriptor md5_desc = 19 const struct ltc_hash_descriptor md5_desc =
93 91
94 #define II(a,b,c,d,M,s,t) \ 92 #define II(a,b,c,d,M,s,t) \
95 a = (a + I(b,c,d) + M + t); a = ROLc(a, s) + b; 93 a = (a + I(b,c,d) + M + t); a = ROLc(a, s) + b;
96 94
97 95
98 #endif 96 #endif
99 97
100 #ifdef LTC_CLEAN_STACK 98 #ifdef LTC_CLEAN_STACK
101 static int _md5_compress(hash_state *md, unsigned char *buf) 99 static int _md5_compress(hash_state *md, unsigned char *buf)
102 #else 100 #else
103 static int md5_compress(hash_state *md, unsigned char *buf) 101 static int md5_compress(hash_state *md, unsigned char *buf)
110 108
111 /* copy the state into 512-bits into W[0..15] */ 109 /* copy the state into 512-bits into W[0..15] */
112 for (i = 0; i < 16; i++) { 110 for (i = 0; i < 16; i++) {
113 LOAD32L(W[i], buf + (4*i)); 111 LOAD32L(W[i], buf + (4*i));
114 } 112 }
115 113
116 /* copy state */ 114 /* copy state */
117 a = md->md5.state[0]; 115 a = md->md5.state[0];
118 b = md->md5.state[1]; 116 b = md->md5.state[1];
119 c = md->md5.state[2]; 117 c = md->md5.state[2];
120 d = md->md5.state[3]; 118 d = md->md5.state[3];
307 } 305 }
308 306
309 /** 307 /**
310 Self-test the hash 308 Self-test the hash
311 @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled 309 @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled
312 */ 310 */
313 int md5_test(void) 311 int md5_test(void)
314 { 312 {
315 #ifndef LTC_TEST 313 #ifndef LTC_TEST
316 return CRYPT_NOP; 314 return CRYPT_NOP;
317 #else 315 #else
318 static const struct { 316 static const struct {
319 char *msg; 317 const char *msg;
320 unsigned char hash[16]; 318 unsigned char hash[16];
321 } tests[] = { 319 } tests[] = {
322 { "", 320 { "",
323 { 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04, 321 { 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04,
324 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e } }, 322 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e } },
325 { "a", 323 { "a",
326 {0x0c, 0xc1, 0x75, 0xb9, 0xc0, 0xf1, 0xb6, 0xa8, 324 {0x0c, 0xc1, 0x75, 0xb9, 0xc0, 0xf1, 0xb6, 0xa8,
327 0x31, 0xc3, 0x99, 0xe2, 0x69, 0x77, 0x26, 0x61 } }, 325 0x31, 0xc3, 0x99, 0xe2, 0x69, 0x77, 0x26, 0x61 } },
328 { "abc", 326 { "abc",
329 { 0x90, 0x01, 0x50, 0x98, 0x3c, 0xd2, 0x4f, 0xb0, 327 { 0x90, 0x01, 0x50, 0x98, 0x3c, 0xd2, 0x4f, 0xb0,
330 0xd6, 0x96, 0x3f, 0x7d, 0x28, 0xe1, 0x7f, 0x72 } }, 328 0xd6, 0x96, 0x3f, 0x7d, 0x28, 0xe1, 0x7f, 0x72 } },
331 { "message digest", 329 { "message digest",
332 { 0xf9, 0x6b, 0x69, 0x7d, 0x7c, 0xb7, 0x93, 0x8d, 330 { 0xf9, 0x6b, 0x69, 0x7d, 0x7c, 0xb7, 0x93, 0x8d,
333 0x52, 0x5a, 0x2f, 0x31, 0xaa, 0xf1, 0x61, 0xd0 } }, 331 0x52, 0x5a, 0x2f, 0x31, 0xaa, 0xf1, 0x61, 0xd0 } },
334 { "abcdefghijklmnopqrstuvwxyz", 332 { "abcdefghijklmnopqrstuvwxyz",
335 { 0xc3, 0xfc, 0xd3, 0xd7, 0x61, 0x92, 0xe4, 0x00, 333 { 0xc3, 0xfc, 0xd3, 0xd7, 0x61, 0x92, 0xe4, 0x00,
336 0x7d, 0xfb, 0x49, 0x6c, 0xca, 0x67, 0xe1, 0x3b } }, 334 0x7d, 0xfb, 0x49, 0x6c, 0xca, 0x67, 0xe1, 0x3b } },
337 { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 335 { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
338 { 0xd1, 0x74, 0xab, 0x98, 0xd2, 0x77, 0xd9, 0xf5, 336 { 0xd1, 0x74, 0xab, 0x98, 0xd2, 0x77, 0xd9, 0xf5,
339 0xa5, 0x61, 0x1c, 0x2c, 0x9f, 0x41, 0x9d, 0x9f } }, 337 0xa5, 0x61, 0x1c, 0x2c, 0x9f, 0x41, 0x9d, 0x9f } },
340 { "12345678901234567890123456789012345678901234567890123456789012345678901234567890", 338 { "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
341 { 0x57, 0xed, 0xf4, 0xa2, 0x2b, 0xe3, 0xc9, 0x55, 339 { 0x57, 0xed, 0xf4, 0xa2, 0x2b, 0xe3, 0xc9, 0x55,
342 0xac, 0x49, 0xda, 0x2e, 0x21, 0x07, 0xb6, 0x7a } }, 340 0xac, 0x49, 0xda, 0x2e, 0x21, 0x07, 0xb6, 0x7a } },
343 { NULL, { 0 } } 341 { NULL, { 0 } }
344 }; 342 };
345 343
346 int i; 344 int i;
347 unsigned char tmp[16]; 345 unsigned char tmp[16];
349 347
350 for (i = 0; tests[i].msg != NULL; i++) { 348 for (i = 0; tests[i].msg != NULL; i++) {
351 md5_init(&md); 349 md5_init(&md);
352 md5_process(&md, (unsigned char *)tests[i].msg, (unsigned long)strlen(tests[i].msg)); 350 md5_process(&md, (unsigned char *)tests[i].msg, (unsigned long)strlen(tests[i].msg));
353 md5_done(&md, tmp); 351 md5_done(&md, tmp);
354 if (XMEMCMP(tmp, tests[i].hash, 16) != 0) { 352 if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "MD5", i)) {
355 return CRYPT_FAIL_TESTVECTOR; 353 return CRYPT_FAIL_TESTVECTOR;
356 } 354 }
357 } 355 }
358 return CRYPT_OK; 356 return CRYPT_OK;
359 #endif 357 #endif
361 359
362 #endif 360 #endif
363 361
364 362
365 363
366 /* $Source$ */ 364 /* ref: $Format:%D$ */
367 /* $Revision$ */ 365 /* git commit: $Format:%H$ */
368 /* $Date$ */ 366 /* commit time: $Format:%ai$ */