comparison libtomcrypt/src/hashes/rmd160.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 @file rmd160.c 12 @file rmd160.c
15 RMD160 hash function 13 RMD160 hash function
16 */ 14 */
17 15
18 /* Implementation of LTC_RIPEMD-160 based on the source by Antoon Bosselaers, ESAT-COSIC 16 /* Implementation of LTC_RIPEMD-160 based on the source by Antoon Bosselaers, ESAT-COSIC
19 * 17 *
20 * This source has been radically overhauled to be portable and work within 18 * This source has been radically overhauled to be portable and work within
21 * the LibTomCrypt API by Tom St Denis 19 * the LibTomCrypt API by Tom St Denis
40 &rmd160_test, 38 &rmd160_test,
41 NULL 39 NULL
42 }; 40 };
43 41
44 /* the five basic functions F(), G() and H() */ 42 /* the five basic functions F(), G() and H() */
45 #define F(x, y, z) ((x) ^ (y) ^ (z)) 43 #define F(x, y, z) ((x) ^ (y) ^ (z))
46 #define G(x, y, z) (((x) & (y)) | (~(x) & (z))) 44 #define G(x, y, z) (((x) & (y)) | (~(x) & (z)))
47 #define H(x, y, z) (((x) | ~(y)) ^ (z)) 45 #define H(x, y, z) (((x) | ~(y)) ^ (z))
48 #define I(x, y, z) (((x) & (z)) | ((y) & ~(z))) 46 #define I(x, y, z) (((x) & (z)) | ((y) & ~(z)))
49 #define J(x, y, z) ((x) ^ ((y) | ~(z))) 47 #define J(x, y, z) ((x) ^ ((y) | ~(z)))
50 48
51 /* the ten basic operations FF() through III() */ 49 /* the ten basic operations FF() through III() */
52 #define FF(a, b, c, d, e, x, s) \ 50 #define FF(a, b, c, d, e, x, s) \
53 (a) += F((b), (c), (d)) + (x);\ 51 (a) += F((b), (c), (d)) + (x);\
54 (a) = ROLc((a), (s)) + (e);\ 52 (a) = ROLc((a), (s)) + (e);\
55 (c) = ROLc((c), 10); 53 (c) = ROLc((c), 10);
136 FF(ee, aa, bb, cc, dd, X[11], 15); 134 FF(ee, aa, bb, cc, dd, X[11], 15);
137 FF(dd, ee, aa, bb, cc, X[12], 6); 135 FF(dd, ee, aa, bb, cc, X[12], 6);
138 FF(cc, dd, ee, aa, bb, X[13], 7); 136 FF(cc, dd, ee, aa, bb, X[13], 7);
139 FF(bb, cc, dd, ee, aa, X[14], 9); 137 FF(bb, cc, dd, ee, aa, X[14], 9);
140 FF(aa, bb, cc, dd, ee, X[15], 8); 138 FF(aa, bb, cc, dd, ee, X[15], 8);
141 139
142 /* round 2 */ 140 /* round 2 */
143 GG(ee, aa, bb, cc, dd, X[ 7], 7); 141 GG(ee, aa, bb, cc, dd, X[ 7], 7);
144 GG(dd, ee, aa, bb, cc, X[ 4], 6); 142 GG(dd, ee, aa, bb, cc, X[ 4], 6);
145 GG(cc, dd, ee, aa, bb, X[13], 8); 143 GG(cc, dd, ee, aa, bb, X[13], 8);
146 GG(bb, cc, dd, ee, aa, X[ 1], 13); 144 GG(bb, cc, dd, ee, aa, X[ 1], 13);
228 JJJ(ccc, ddd, eee, aaa, bbb, X[10], 14); 226 JJJ(ccc, ddd, eee, aaa, bbb, X[10], 14);
229 JJJ(bbb, ccc, ddd, eee, aaa, X[ 3], 12); 227 JJJ(bbb, ccc, ddd, eee, aaa, X[ 3], 12);
230 JJJ(aaa, bbb, ccc, ddd, eee, X[12], 6); 228 JJJ(aaa, bbb, ccc, ddd, eee, X[12], 6);
231 229
232 /* parallel round 2 */ 230 /* parallel round 2 */
233 III(eee, aaa, bbb, ccc, ddd, X[ 6], 9); 231 III(eee, aaa, bbb, ccc, ddd, X[ 6], 9);
234 III(ddd, eee, aaa, bbb, ccc, X[11], 13); 232 III(ddd, eee, aaa, bbb, ccc, X[11], 13);
235 III(ccc, ddd, eee, aaa, bbb, X[ 3], 15); 233 III(ccc, ddd, eee, aaa, bbb, X[ 3], 15);
236 III(bbb, ccc, ddd, eee, aaa, X[ 7], 7); 234 III(bbb, ccc, ddd, eee, aaa, X[ 7], 7);
237 III(aaa, bbb, ccc, ddd, eee, X[ 0], 12); 235 III(aaa, bbb, ccc, ddd, eee, X[ 0], 12);
238 III(eee, aaa, bbb, ccc, ddd, X[13], 8); 236 III(eee, aaa, bbb, ccc, ddd, X[13], 8);
263 HHH(bbb, ccc, ddd, eee, aaa, X[10], 13); 261 HHH(bbb, ccc, ddd, eee, aaa, X[10], 13);
264 HHH(aaa, bbb, ccc, ddd, eee, X[ 0], 13); 262 HHH(aaa, bbb, ccc, ddd, eee, X[ 0], 13);
265 HHH(eee, aaa, bbb, ccc, ddd, X[ 4], 7); 263 HHH(eee, aaa, bbb, ccc, ddd, X[ 4], 7);
266 HHH(ddd, eee, aaa, bbb, ccc, X[13], 5); 264 HHH(ddd, eee, aaa, bbb, ccc, X[13], 5);
267 265
268 /* parallel round 4 */ 266 /* parallel round 4 */
269 GGG(ccc, ddd, eee, aaa, bbb, X[ 8], 15); 267 GGG(ccc, ddd, eee, aaa, bbb, X[ 8], 15);
270 GGG(bbb, ccc, ddd, eee, aaa, X[ 6], 5); 268 GGG(bbb, ccc, ddd, eee, aaa, X[ 6], 5);
271 GGG(aaa, bbb, ccc, ddd, eee, X[ 4], 8); 269 GGG(aaa, bbb, ccc, ddd, eee, X[ 4], 8);
272 GGG(eee, aaa, bbb, ccc, ddd, X[ 1], 11); 270 GGG(eee, aaa, bbb, ccc, ddd, X[ 1], 11);
273 GGG(ddd, eee, aaa, bbb, ccc, X[ 3], 14); 271 GGG(ddd, eee, aaa, bbb, ccc, X[ 3], 14);
405 } 403 }
406 404
407 /** 405 /**
408 Self-test the hash 406 Self-test the hash
409 @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled 407 @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled
410 */ 408 */
411 int rmd160_test(void) 409 int rmd160_test(void)
412 { 410 {
413 #ifndef LTC_TEST 411 #ifndef LTC_TEST
414 return CRYPT_NOP; 412 return CRYPT_NOP;
415 #else 413 #else
416 static const struct { 414 static const struct {
417 char *msg; 415 const char *msg;
418 unsigned char md[20]; 416 unsigned char hash[20];
419 } tests[] = { 417 } tests[] = {
420 { "", 418 { "",
421 { 0x9c, 0x11, 0x85, 0xa5, 0xc5, 0xe9, 0xfc, 0x54, 0x61, 0x28, 419 { 0x9c, 0x11, 0x85, 0xa5, 0xc5, 0xe9, 0xfc, 0x54, 0x61, 0x28,
422 0x08, 0x97, 0x7e, 0xe8, 0xf5, 0x48, 0xb2, 0x25, 0x8d, 0x31 } 420 0x08, 0x97, 0x7e, 0xe8, 0xf5, 0x48, 0xb2, 0x25, 0x8d, 0x31 }
423 }, 421 },
440 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 438 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
441 { 0x12, 0xa0, 0x53, 0x38, 0x4a, 0x9c, 0x0c, 0x88, 0xe4, 0x05, 439 { 0x12, 0xa0, 0x53, 0x38, 0x4a, 0x9c, 0x0c, 0x88, 0xe4, 0x05,
442 0xa0, 0x6c, 0x27, 0xdc, 0xf4, 0x9a, 0xda, 0x62, 0xeb, 0x2b } 440 0xa0, 0x6c, 0x27, 0xdc, 0xf4, 0x9a, 0xda, 0x62, 0xeb, 0x2b }
443 } 441 }
444 }; 442 };
445 int x; 443
446 unsigned char buf[20]; 444 int i;
445 unsigned char tmp[20];
447 hash_state md; 446 hash_state md;
448 447
449 for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) { 448 for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
450 rmd160_init(&md); 449 rmd160_init(&md);
451 rmd160_process(&md, (unsigned char *)tests[x].msg, strlen(tests[x].msg)); 450 rmd160_process(&md, (unsigned char *)tests[i].msg, strlen(tests[i].msg));
452 rmd160_done(&md, buf); 451 rmd160_done(&md, tmp);
453 if (XMEMCMP(buf, tests[x].md, 20) != 0) { 452 if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "RIPEMD160", i)) {
454 #if 0
455 printf("Failed test %d\n", x);
456 #endif
457 return CRYPT_FAIL_TESTVECTOR; 453 return CRYPT_FAIL_TESTVECTOR;
458 } 454 }
459 } 455 }
460 return CRYPT_OK; 456 return CRYPT_OK;
461 #endif 457 #endif
462 } 458 }
463 459
464 #endif 460 #endif
465 461
466 462
467 /* $Source$ */ 463 /* ref: $Format:%D$ */
468 /* $Revision$ */ 464 /* git commit: $Format:%H$ */
469 /* $Date$ */ 465 /* commit time: $Format:%ai$ */