3
|
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis |
|
2 * |
|
3 * LibTomCrypt is a library that provides various cryptographic |
|
4 * algorithms in a highly modular and flexible manner. |
|
5 * |
|
6 * The library is free for all purposes without any express |
|
7 * guarantee it works. |
|
8 * |
|
9 * Tom St Denis, [email protected], http://libtomcrypt.org |
|
10 */ |
|
11 |
|
12 /* SHA1 code by Tom St Denis */ |
|
13 #include "mycrypt.h" |
|
14 |
|
15 #ifdef SHA1 |
|
16 |
|
17 const struct _hash_descriptor sha1_desc = |
|
18 { |
|
19 "sha1", |
|
20 2, |
|
21 20, |
|
22 64, |
15
|
23 |
|
24 /* DER identifier */ |
|
25 { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x0E, |
|
26 0x03, 0x02, 0x1A, 0x05, 0x00, 0x04, 0x14 }, |
|
27 15, |
|
28 |
3
|
29 &sha1_init, |
|
30 &sha1_process, |
|
31 &sha1_done, |
|
32 &sha1_test |
|
33 }; |
|
34 |
|
35 #define F0(x,y,z) (z ^ (x & (y ^ z))) |
|
36 #define F1(x,y,z) (x ^ y ^ z) |
|
37 #define F2(x,y,z) ((x & y) | (z & (x | y))) |
|
38 #define F3(x,y,z) (x ^ y ^ z) |
|
39 |
|
40 #ifdef CLEAN_STACK |
|
41 static void _sha1_compress(hash_state *md, unsigned char *buf) |
|
42 #else |
|
43 static void sha1_compress(hash_state *md, unsigned char *buf) |
|
44 #endif |
|
45 { |
|
46 ulong32 a,b,c,d,e,W[80],i; |
15
|
47 #ifdef SMALL_CODE |
|
48 ulong32 t; |
|
49 #endif |
3
|
50 |
|
51 /* copy the state into 512-bits into W[0..15] */ |
|
52 for (i = 0; i < 16; i++) { |
|
53 LOAD32H(W[i], buf + (4*i)); |
|
54 } |
|
55 |
|
56 /* copy state */ |
|
57 a = md->sha1.state[0]; |
|
58 b = md->sha1.state[1]; |
|
59 c = md->sha1.state[2]; |
|
60 d = md->sha1.state[3]; |
|
61 e = md->sha1.state[4]; |
|
62 |
|
63 /* expand it */ |
|
64 for (i = 16; i < 80; i++) { |
|
65 W[i] = ROL(W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16], 1); |
|
66 } |
|
67 |
|
68 /* compress */ |
|
69 /* round one */ |
|
70 #define FF0(a,b,c,d,e,i) e = (ROL(a, 5) + F0(b,c,d) + e + W[i] + 0x5a827999UL); b = ROL(b, 30); |
|
71 #define FF1(a,b,c,d,e,i) e = (ROL(a, 5) + F1(b,c,d) + e + W[i] + 0x6ed9eba1UL); b = ROL(b, 30); |
|
72 #define FF2(a,b,c,d,e,i) e = (ROL(a, 5) + F2(b,c,d) + e + W[i] + 0x8f1bbcdcUL); b = ROL(b, 30); |
|
73 #define FF3(a,b,c,d,e,i) e = (ROL(a, 5) + F3(b,c,d) + e + W[i] + 0xca62c1d6UL); b = ROL(b, 30); |
|
74 |
15
|
75 #ifdef SMALL_CODE |
|
76 |
|
77 for (i = 0; i < 20; ) { |
|
78 FF0(a,b,c,d,e,i++); t = e; e = d; d = c; c = b; b = a; a = t; |
|
79 } |
|
80 |
|
81 for (; i < 40; ) { |
|
82 FF1(a,b,c,d,e,i++); t = e; e = d; d = c; c = b; b = a; a = t; |
|
83 } |
|
84 |
|
85 for (; i < 60; ) { |
|
86 FF2(a,b,c,d,e,i++); t = e; e = d; d = c; c = b; b = a; a = t; |
|
87 } |
|
88 |
|
89 for (; i < 80; ) { |
|
90 FF3(a,b,c,d,e,i++); t = e; e = d; d = c; c = b; b = a; a = t; |
|
91 } |
|
92 |
|
93 #else |
|
94 |
3
|
95 for (i = 0; i < 20; ) { |
|
96 FF0(a,b,c,d,e,i++); |
|
97 FF0(e,a,b,c,d,i++); |
|
98 FF0(d,e,a,b,c,i++); |
|
99 FF0(c,d,e,a,b,i++); |
|
100 FF0(b,c,d,e,a,i++); |
|
101 } |
|
102 |
|
103 /* round two */ |
|
104 for (; i < 40; ) { |
|
105 FF1(a,b,c,d,e,i++); |
|
106 FF1(e,a,b,c,d,i++); |
|
107 FF1(d,e,a,b,c,i++); |
|
108 FF1(c,d,e,a,b,i++); |
|
109 FF1(b,c,d,e,a,i++); |
|
110 } |
|
111 |
|
112 /* round three */ |
|
113 for (; i < 60; ) { |
|
114 FF2(a,b,c,d,e,i++); |
|
115 FF2(e,a,b,c,d,i++); |
|
116 FF2(d,e,a,b,c,i++); |
|
117 FF2(c,d,e,a,b,i++); |
|
118 FF2(b,c,d,e,a,i++); |
|
119 } |
|
120 |
|
121 /* round four */ |
|
122 for (; i < 80; ) { |
|
123 FF3(a,b,c,d,e,i++); |
|
124 FF3(e,a,b,c,d,i++); |
|
125 FF3(d,e,a,b,c,i++); |
|
126 FF3(c,d,e,a,b,i++); |
|
127 FF3(b,c,d,e,a,i++); |
|
128 } |
15
|
129 #endif |
3
|
130 |
|
131 #undef FF0 |
|
132 #undef FF1 |
|
133 #undef FF2 |
|
134 #undef FF3 |
|
135 |
|
136 /* store */ |
|
137 md->sha1.state[0] = md->sha1.state[0] + a; |
|
138 md->sha1.state[1] = md->sha1.state[1] + b; |
|
139 md->sha1.state[2] = md->sha1.state[2] + c; |
|
140 md->sha1.state[3] = md->sha1.state[3] + d; |
|
141 md->sha1.state[4] = md->sha1.state[4] + e; |
|
142 } |
|
143 |
|
144 #ifdef CLEAN_STACK |
|
145 static void sha1_compress(hash_state *md, unsigned char *buf) |
|
146 { |
|
147 _sha1_compress(md, buf); |
|
148 burn_stack(sizeof(ulong32) * 87); |
|
149 } |
|
150 #endif |
|
151 |
|
152 void sha1_init(hash_state * md) |
|
153 { |
|
154 _ARGCHK(md != NULL); |
|
155 md->sha1.state[0] = 0x67452301UL; |
|
156 md->sha1.state[1] = 0xefcdab89UL; |
|
157 md->sha1.state[2] = 0x98badcfeUL; |
|
158 md->sha1.state[3] = 0x10325476UL; |
|
159 md->sha1.state[4] = 0xc3d2e1f0UL; |
|
160 md->sha1.curlen = 0; |
|
161 md->sha1.length = 0; |
|
162 } |
|
163 |
|
164 HASH_PROCESS(sha1_process, sha1_compress, sha1, 64) |
|
165 |
|
166 int sha1_done(hash_state * md, unsigned char *hash) |
|
167 { |
|
168 int i; |
|
169 |
|
170 _ARGCHK(md != NULL); |
|
171 _ARGCHK(hash != NULL); |
|
172 |
|
173 if (md->sha1.curlen >= sizeof(md->sha1.buf)) { |
|
174 return CRYPT_INVALID_ARG; |
|
175 } |
|
176 |
|
177 /* increase the length of the message */ |
|
178 md->sha1.length += md->sha1.curlen * 8; |
|
179 |
|
180 /* append the '1' bit */ |
|
181 md->sha1.buf[md->sha1.curlen++] = (unsigned char)0x80; |
|
182 |
|
183 /* if the length is currently above 56 bytes we append zeros |
|
184 * then compress. Then we can fall back to padding zeros and length |
|
185 * encoding like normal. |
|
186 */ |
|
187 if (md->sha1.curlen > 56) { |
|
188 while (md->sha1.curlen < 64) { |
|
189 md->sha1.buf[md->sha1.curlen++] = (unsigned char)0; |
|
190 } |
|
191 sha1_compress(md, md->sha1.buf); |
|
192 md->sha1.curlen = 0; |
|
193 } |
|
194 |
|
195 /* pad upto 56 bytes of zeroes */ |
|
196 while (md->sha1.curlen < 56) { |
|
197 md->sha1.buf[md->sha1.curlen++] = (unsigned char)0; |
|
198 } |
|
199 |
|
200 /* store length */ |
|
201 STORE64H(md->sha1.length, md->sha1.buf+56); |
|
202 sha1_compress(md, md->sha1.buf); |
|
203 |
|
204 /* copy output */ |
|
205 for (i = 0; i < 5; i++) { |
|
206 STORE32H(md->sha1.state[i], hash+(4*i)); |
|
207 } |
|
208 #ifdef CLEAN_STACK |
|
209 zeromem(md, sizeof(hash_state)); |
|
210 #endif |
|
211 return CRYPT_OK; |
|
212 } |
|
213 |
|
214 int sha1_test(void) |
|
215 { |
|
216 #ifndef LTC_TEST |
|
217 return CRYPT_NOP; |
|
218 #else |
|
219 static const struct { |
|
220 char *msg; |
|
221 unsigned char hash[20]; |
|
222 } tests[] = { |
|
223 { "abc", |
|
224 { 0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a, |
|
225 0xba, 0x3e, 0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c, |
|
226 0x9c, 0xd0, 0xd8, 0x9d } |
|
227 }, |
|
228 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", |
|
229 { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, |
|
230 0xBA, 0xAE, 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, |
|
231 0xE5, 0x46, 0x70, 0xF1 } |
|
232 } |
|
233 }; |
|
234 |
|
235 int i; |
|
236 unsigned char tmp[20]; |
|
237 hash_state md; |
|
238 |
|
239 for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) { |
|
240 sha1_init(&md); |
|
241 sha1_process(&md, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg)); |
|
242 sha1_done(&md, tmp); |
|
243 if (memcmp(tmp, tests[i].hash, 20) != 0) { |
|
244 return CRYPT_FAIL_TESTVECTOR; |
|
245 } |
|
246 } |
|
247 return CRYPT_OK; |
|
248 #endif |
|
249 } |
|
250 |
|
251 #endif |
|
252 |
|
253 |