comparison libtomcrypt/src/hashes/tiger.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 9
12 #include "tomcrypt.h" 10 #include "tomcrypt.h"
13 11
14 /** 12 /**
556 CONST64(0xC83223F1720AEF96) /* 1022 */, CONST64(0xC3A0396F7363A51F) /* 1023 */}; 554 CONST64(0xC83223F1720AEF96) /* 1022 */, CONST64(0xC3A0396F7363A51F) /* 1023 */};
557 555
558 #ifdef _MSC_VER 556 #ifdef _MSC_VER
559 #define INLINE __inline 557 #define INLINE __inline
560 #else 558 #else
561 #define INLINE 559 #define INLINE
562 #endif 560 #endif
563 561
564 /* one round of the hash function */ 562 /* one round of the hash function */
565 INLINE static void tiger_round(ulong64 *a, ulong64 *b, ulong64 *c, ulong64 x, int mul) 563 INLINE static void tiger_round(ulong64 *a, ulong64 *b, ulong64 *c, ulong64 x, int mul)
566 { 564 {
567 ulong64 tmp; 565 ulong64 tmp;
568 tmp = (*c ^= x); 566 tmp = (*c ^= x);
569 *a -= t1[byte(tmp, 0)] ^ t2[byte(tmp, 2)] ^ t3[byte(tmp, 4)] ^ t4[byte(tmp, 6)]; 567 *a -= t1[byte(tmp, 0)] ^ t2[byte(tmp, 2)] ^ t3[byte(tmp, 4)] ^ t4[byte(tmp, 6)];
570 tmp = (*b += t4[byte(tmp, 1)] ^ t3[byte(tmp, 3)] ^ t2[byte(tmp,5)] ^ t1[byte(tmp,7)]); 568 tmp = (*b += t4[byte(tmp, 1)] ^ t3[byte(tmp, 3)] ^ t2[byte(tmp,5)] ^ t1[byte(tmp,7)]);
571 switch (mul) { 569 switch (mul) {
572 case 5: *b = (tmp << 2) + tmp; break; 570 case 5: *b = (tmp << 2) + tmp; break;
573 case 7: *b = (tmp << 3) - tmp; break; 571 case 7: *b = (tmp << 3) - tmp; break;
574 case 9: *b = (tmp << 3) + tmp; break; 572 case 9: *b = (tmp << 3) + tmp; break;
575 } 573 }
576 } 574 }
577 575
578 /* one complete pass */ 576 /* one complete pass */
579 static void pass(ulong64 *a, ulong64 *b, ulong64 *c, ulong64 *x, int mul) 577 static void pass(ulong64 *a, ulong64 *b, ulong64 *c, ulong64 *x, int mul)
580 { 578 {
581 tiger_round(a,b,c,x[0],mul); 579 tiger_round(a,b,c,x[0],mul);
582 tiger_round(b,c,a,x[1],mul); 580 tiger_round(b,c,a,x[1],mul);
583 tiger_round(c,a,b,x[2],mul); 581 tiger_round(c,a,b,x[2],mul);
584 tiger_round(a,b,c,x[3],mul); 582 tiger_round(a,b,c,x[3],mul);
585 tiger_round(b,c,a,x[4],mul); 583 tiger_round(b,c,a,x[4],mul);
586 tiger_round(c,a,b,x[5],mul); 584 tiger_round(c,a,b,x[5],mul);
587 tiger_round(a,b,c,x[6],mul); 585 tiger_round(a,b,c,x[6],mul);
588 tiger_round(b,c,a,x[7],mul); 586 tiger_round(b,c,a,x[7],mul);
589 } 587 }
590 588
591 /* The key mixing schedule */ 589 /* The key mixing schedule */
592 static void key_schedule(ulong64 *x) 590 static void key_schedule(ulong64 *x)
593 { 591 {
594 x[0] -= x[7] ^ CONST64(0xA5A5A5A5A5A5A5A5); 592 x[0] -= x[7] ^ CONST64(0xA5A5A5A5A5A5A5A5);
595 x[1] ^= x[0]; 593 x[1] ^= x[0];
596 x[2] += x[1]; 594 x[2] += x[1];
597 x[3] -= x[2] ^ ((~x[1])<<19); 595 x[3] -= x[2] ^ ((~x[1])<<19);
598 x[4] ^= x[3]; 596 x[4] ^= x[3];
599 x[5] += x[4]; 597 x[5] += x[4];
600 x[6] -= x[5] ^ ((~x[4])>>23); 598 x[6] -= x[5] ^ ((~x[4])>>23);
601 x[7] ^= x[6]; 599 x[7] ^= x[6];
602 x[0] += x[7]; 600 x[0] += x[7];
603 x[1] -= x[0] ^ ((~x[7])<<19); 601 x[1] -= x[0] ^ ((~x[7])<<19);
604 x[2] ^= x[1]; 602 x[2] ^= x[1];
605 x[3] += x[2]; 603 x[3] += x[2];
606 x[4] -= x[3] ^ ((~x[2])>>23); 604 x[4] -= x[3] ^ ((~x[2])>>23);
607 x[5] ^= x[4]; 605 x[5] ^= x[4];
608 x[6] += x[5]; 606 x[6] += x[5];
609 x[7] -= x[6] ^ CONST64(0x0123456789ABCDEF); 607 x[7] -= x[6] ^ CONST64(0x0123456789ABCDEF);
610 } 608 }
611 609
612 #ifdef LTC_CLEAN_STACK 610 #ifdef LTC_CLEAN_STACK
613 static int _tiger_compress(hash_state *md, unsigned char *buf) 611 static int _tiger_compress(hash_state *md, unsigned char *buf)
614 #else 612 #else
615 static int tiger_compress(hash_state *md, unsigned char *buf) 613 static int tiger_compress(hash_state *md, unsigned char *buf)
707 md->tiger.curlen = 0; 705 md->tiger.curlen = 0;
708 } 706 }
709 707
710 /* pad upto 56 bytes of zeroes */ 708 /* pad upto 56 bytes of zeroes */
711 while (md->tiger.curlen < 56) { 709 while (md->tiger.curlen < 56) {
712 md->tiger.buf[md->tiger.curlen++] = (unsigned char)0; 710 md->tiger.buf[md->tiger.curlen++] = (unsigned char)0;
713 } 711 }
714 712
715 /* store length */ 713 /* store length */
716 STORE64L(md->tiger.length, md->tiger.buf+56); 714 STORE64L(md->tiger.length, md->tiger.buf+56);
717 tiger_compress(md, md->tiger.buf); 715 tiger_compress(md, md->tiger.buf);
728 } 726 }
729 727
730 /** 728 /**
731 Self-test the hash 729 Self-test the hash
732 @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled 730 @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled
733 */ 731 */
734 int tiger_test(void) 732 int tiger_test(void)
735 { 733 {
736 #ifndef LTC_TEST 734 #ifndef LTC_TEST
737 return CRYPT_NOP; 735 return CRYPT_NOP;
738 #else 736 #else
739 static const struct { 737 static const struct {
740 char *msg; 738 const char *msg;
741 unsigned char hash[24]; 739 unsigned char hash[24];
742 } tests[] = { 740 } tests[] = {
743 { "", 741 { "",
744 { 0x32, 0x93, 0xac, 0x63, 0x0c, 0x13, 0xf0, 0x24, 742 { 0x32, 0x93, 0xac, 0x63, 0x0c, 0x13, 0xf0, 0x24,
745 0x5f, 0x92, 0xbb, 0xb1, 0x76, 0x6e, 0x16, 0x16, 743 0x5f, 0x92, 0xbb, 0xb1, 0x76, 0x6e, 0x16, 0x16,
773 771
774 for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) { 772 for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) {
775 tiger_init(&md); 773 tiger_init(&md);
776 tiger_process(&md, (unsigned char *)tests[i].msg, (unsigned long)strlen(tests[i].msg)); 774 tiger_process(&md, (unsigned char *)tests[i].msg, (unsigned long)strlen(tests[i].msg));
777 tiger_done(&md, tmp); 775 tiger_done(&md, tmp);
778 if (XMEMCMP(tmp, tests[i].hash, 24) != 0) { 776 if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "TIGER", i)) {
779 return CRYPT_FAIL_TESTVECTOR; 777 return CRYPT_FAIL_TESTVECTOR;
780 } 778 }
781 } 779 }
782 return CRYPT_OK; 780 return CRYPT_OK;
783 #endif 781 #endif
807 */ 805 */
808 806
809 807
810 808
811 809
812 /* $Source$ */ 810 /* ref: $Format:%D$ */
813 /* $Revision$ */ 811 /* git commit: $Format:%H$ */
814 /* $Date$ */ 812 /* commit time: $Format:%ai$ */