comparison libtomcrypt/src/hashes/sha1.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 sha1.c 12 @file sha1.c
15 LTC_SHA1 code by Tom St Denis 13 LTC_SHA1 code by Tom St Denis
16 */ 14 */
17 15
18 16
19 #ifdef LTC_SHA1 17 #ifdef LTC_SHA1
20 18
64 d = md->sha1.state[3]; 62 d = md->sha1.state[3];
65 e = md->sha1.state[4]; 63 e = md->sha1.state[4];
66 64
67 /* expand it */ 65 /* expand it */
68 for (i = 16; i < 80; i++) { 66 for (i = 16; i < 80; i++) {
69 W[i] = ROL(W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16], 1); 67 W[i] = ROL(W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16], 1);
70 } 68 }
71 69
72 /* compress */ 70 /* compress */
73 /* round one */ 71 /* round one */
74 #define FF0(a,b,c,d,e,i) e = (ROLc(a, 5) + F0(b,c,d) + e + W[i] + 0x5a827999UL); b = ROLc(b, 30); 72 #define FF0(a,b,c,d,e,i) e = (ROLc(a, 5) + F0(b,c,d) + e + W[i] + 0x5a827999UL); b = ROLc(b, 30);
75 #define FF1(a,b,c,d,e,i) e = (ROLc(a, 5) + F1(b,c,d) + e + W[i] + 0x6ed9eba1UL); b = ROLc(b, 30); 73 #define FF1(a,b,c,d,e,i) e = (ROLc(a, 5) + F1(b,c,d) + e + W[i] + 0x6ed9eba1UL); b = ROLc(b, 30);
76 #define FF2(a,b,c,d,e,i) e = (ROLc(a, 5) + F2(b,c,d) + e + W[i] + 0x8f1bbcdcUL); b = ROLc(b, 30); 74 #define FF2(a,b,c,d,e,i) e = (ROLc(a, 5) + F2(b,c,d) + e + W[i] + 0x8f1bbcdcUL); b = ROLc(b, 30);
77 #define FF3(a,b,c,d,e,i) e = (ROLc(a, 5) + F3(b,c,d) + e + W[i] + 0xca62c1d6UL); b = ROLc(b, 30); 75 #define FF3(a,b,c,d,e,i) e = (ROLc(a, 5) + F3(b,c,d) + e + W[i] + 0xca62c1d6UL); b = ROLc(b, 30);
78 76
79 #ifdef LTC_SMALL_CODE 77 #ifdef LTC_SMALL_CODE
80 78
81 for (i = 0; i < 20; ) { 79 for (i = 0; i < 20; ) {
82 FF0(a,b,c,d,e,i++); t = e; e = d; d = c; c = b; b = a; a = t; 80 FF0(a,b,c,d,e,i++); t = e; e = d; d = c; c = b; b = a; a = t;
83 } 81 }
84 82
85 for (; i < 40; ) { 83 for (; i < 40; ) {
103 FF0(c,d,e,a,b,i++); 101 FF0(c,d,e,a,b,i++);
104 FF0(b,c,d,e,a,i++); 102 FF0(b,c,d,e,a,i++);
105 } 103 }
106 104
107 /* round two */ 105 /* round two */
108 for (; i < 40; ) { 106 for (; i < 40; ) {
109 FF1(a,b,c,d,e,i++); 107 FF1(a,b,c,d,e,i++);
110 FF1(e,a,b,c,d,i++); 108 FF1(e,a,b,c,d,i++);
111 FF1(d,e,a,b,c,i++); 109 FF1(d,e,a,b,c,i++);
112 FF1(c,d,e,a,b,i++); 110 FF1(c,d,e,a,b,i++);
113 FF1(b,c,d,e,a,i++); 111 FF1(b,c,d,e,a,i++);
114 } 112 }
115 113
116 /* round three */ 114 /* round three */
117 for (; i < 60; ) { 115 for (; i < 60; ) {
118 FF2(a,b,c,d,e,i++); 116 FF2(a,b,c,d,e,i++);
119 FF2(e,a,b,c,d,i++); 117 FF2(e,a,b,c,d,i++);
120 FF2(d,e,a,b,c,i++); 118 FF2(d,e,a,b,c,i++);
121 FF2(c,d,e,a,b,i++); 119 FF2(c,d,e,a,b,i++);
122 FF2(b,c,d,e,a,i++); 120 FF2(b,c,d,e,a,i++);
123 } 121 }
124 122
125 /* round four */ 123 /* round four */
126 for (; i < 80; ) { 124 for (; i < 80; ) {
127 FF3(a,b,c,d,e,i++); 125 FF3(a,b,c,d,e,i++);
128 FF3(e,a,b,c,d,i++); 126 FF3(e,a,b,c,d,i++);
129 FF3(d,e,a,b,c,i++); 127 FF3(d,e,a,b,c,i++);
130 FF3(c,d,e,a,b,i++); 128 FF3(c,d,e,a,b,i++);
131 FF3(b,c,d,e,a,i++); 129 FF3(b,c,d,e,a,i++);
239 } 237 }
240 238
241 /** 239 /**
242 Self-test the hash 240 Self-test the hash
243 @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled 241 @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled
244 */ 242 */
245 int sha1_test(void) 243 int sha1_test(void)
246 { 244 {
247 #ifndef LTC_TEST 245 #ifndef LTC_TEST
248 return CRYPT_NOP; 246 return CRYPT_NOP;
249 #else 247 #else
250 static const struct { 248 static const struct {
251 char *msg; 249 const char *msg;
252 unsigned char hash[20]; 250 unsigned char hash[20];
253 } tests[] = { 251 } tests[] = {
254 { "abc", 252 { "abc",
255 { 0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a, 253 { 0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a,
256 0xba, 0x3e, 0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c, 254 0xba, 0x3e, 0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c,
269 267
270 for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) { 268 for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) {
271 sha1_init(&md); 269 sha1_init(&md);
272 sha1_process(&md, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg)); 270 sha1_process(&md, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg));
273 sha1_done(&md, tmp); 271 sha1_done(&md, tmp);
274 if (XMEMCMP(tmp, tests[i].hash, 20) != 0) { 272 if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "SHA1", i)) {
275 return CRYPT_FAIL_TESTVECTOR; 273 return CRYPT_FAIL_TESTVECTOR;
276 } 274 }
277 } 275 }
278 return CRYPT_OK; 276 return CRYPT_OK;
279 #endif 277 #endif
281 279
282 #endif 280 #endif
283 281
284 282
285 283
286 /* $Source$ */ 284 /* ref: $Format:%D$ */
287 /* $Revision$ */ 285 /* git commit: $Format:%H$ */
288 /* $Date$ */ 286 /* commit time: $Format:%ai$ */