1*8411e6adSJerome Forissier /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2*8411e6adSJerome Forissier /* SPDX-License-Identifier: Unlicense */
35a913ee7SJerome Forissier #include "tomcrypt_private.h"
4b0104773SPascal Brand
5b0104773SPascal Brand /**
6b0104773SPascal Brand @file sha1.c
7b0104773SPascal Brand LTC_SHA1 code by Tom St Denis
8b0104773SPascal Brand */
9b0104773SPascal Brand
10b0104773SPascal Brand
11b0104773SPascal Brand #ifdef LTC_SHA1
12b0104773SPascal Brand
13b0104773SPascal Brand const struct ltc_hash_descriptor sha1_desc =
14b0104773SPascal Brand {
15b0104773SPascal Brand "sha1",
16b0104773SPascal Brand 2,
17b0104773SPascal Brand 20,
18b0104773SPascal Brand 64,
19b0104773SPascal Brand
20b0104773SPascal Brand /* OID */
21b0104773SPascal Brand { 1, 3, 14, 3, 2, 26, },
22b0104773SPascal Brand 6,
23b0104773SPascal Brand
24b0104773SPascal Brand &sha1_init,
25b0104773SPascal Brand &sha1_process,
26b0104773SPascal Brand &sha1_done,
27b0104773SPascal Brand &sha1_test,
28b0104773SPascal Brand NULL
29b0104773SPascal Brand };
30b0104773SPascal Brand
31b0104773SPascal Brand #define F0(x,y,z) (z ^ (x & (y ^ z)))
32b0104773SPascal Brand #define F1(x,y,z) (x ^ y ^ z)
33b0104773SPascal Brand #define F2(x,y,z) ((x & y) | (z & (x | y)))
34b0104773SPascal Brand #define F3(x,y,z) (x ^ y ^ z)
35b0104773SPascal Brand
36b0104773SPascal Brand #ifdef LTC_CLEAN_STACK
ss_sha1_compress(hash_state * md,const unsigned char * buf)37*8411e6adSJerome Forissier static int ss_sha1_compress(hash_state *md, const unsigned char *buf)
38b0104773SPascal Brand #else
39*8411e6adSJerome Forissier static int s_sha1_compress(hash_state *md, const unsigned char *buf)
40b0104773SPascal Brand #endif
41b0104773SPascal Brand {
42b0104773SPascal Brand ulong32 a,b,c,d,e,W[80],i;
43b0104773SPascal Brand #ifdef LTC_SMALL_CODE
44b0104773SPascal Brand ulong32 t;
45b0104773SPascal Brand #endif
46b0104773SPascal Brand
47b0104773SPascal Brand /* copy the state into 512-bits into W[0..15] */
48b0104773SPascal Brand for (i = 0; i < 16; i++) {
49b0104773SPascal Brand LOAD32H(W[i], buf + (4*i));
50b0104773SPascal Brand }
51b0104773SPascal Brand
52b0104773SPascal Brand /* copy state */
53b0104773SPascal Brand a = md->sha1.state[0];
54b0104773SPascal Brand b = md->sha1.state[1];
55b0104773SPascal Brand c = md->sha1.state[2];
56b0104773SPascal Brand d = md->sha1.state[3];
57b0104773SPascal Brand e = md->sha1.state[4];
58b0104773SPascal Brand
59b0104773SPascal Brand /* expand it */
60b0104773SPascal Brand for (i = 16; i < 80; i++) {
61b0104773SPascal Brand W[i] = ROL(W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16], 1);
62b0104773SPascal Brand }
63b0104773SPascal Brand
64b0104773SPascal Brand /* compress */
65b0104773SPascal Brand /* round one */
66b0104773SPascal Brand #define FF0(a,b,c,d,e,i) e = (ROLc(a, 5) + F0(b,c,d) + e + W[i] + 0x5a827999UL); b = ROLc(b, 30);
67b0104773SPascal Brand #define FF1(a,b,c,d,e,i) e = (ROLc(a, 5) + F1(b,c,d) + e + W[i] + 0x6ed9eba1UL); b = ROLc(b, 30);
68b0104773SPascal Brand #define FF2(a,b,c,d,e,i) e = (ROLc(a, 5) + F2(b,c,d) + e + W[i] + 0x8f1bbcdcUL); b = ROLc(b, 30);
69b0104773SPascal Brand #define FF3(a,b,c,d,e,i) e = (ROLc(a, 5) + F3(b,c,d) + e + W[i] + 0xca62c1d6UL); b = ROLc(b, 30);
70b0104773SPascal Brand
71b0104773SPascal Brand #ifdef LTC_SMALL_CODE
72b0104773SPascal Brand
73b0104773SPascal Brand for (i = 0; i < 20; ) {
74b0104773SPascal Brand FF0(a,b,c,d,e,i++); t = e; e = d; d = c; c = b; b = a; a = t;
75b0104773SPascal Brand }
76b0104773SPascal Brand
77b0104773SPascal Brand for (; i < 40; ) {
78b0104773SPascal Brand FF1(a,b,c,d,e,i++); t = e; e = d; d = c; c = b; b = a; a = t;
79b0104773SPascal Brand }
80b0104773SPascal Brand
81b0104773SPascal Brand for (; i < 60; ) {
82b0104773SPascal Brand FF2(a,b,c,d,e,i++); t = e; e = d; d = c; c = b; b = a; a = t;
83b0104773SPascal Brand }
84b0104773SPascal Brand
85b0104773SPascal Brand for (; i < 80; ) {
86b0104773SPascal Brand FF3(a,b,c,d,e,i++); t = e; e = d; d = c; c = b; b = a; a = t;
87b0104773SPascal Brand }
88b0104773SPascal Brand
89b0104773SPascal Brand #else
90b0104773SPascal Brand
91b0104773SPascal Brand for (i = 0; i < 20; ) {
92b0104773SPascal Brand FF0(a,b,c,d,e,i++);
93b0104773SPascal Brand FF0(e,a,b,c,d,i++);
94b0104773SPascal Brand FF0(d,e,a,b,c,i++);
95b0104773SPascal Brand FF0(c,d,e,a,b,i++);
96b0104773SPascal Brand FF0(b,c,d,e,a,i++);
97b0104773SPascal Brand }
98b0104773SPascal Brand
99b0104773SPascal Brand /* round two */
100b0104773SPascal Brand for (; i < 40; ) {
101b0104773SPascal Brand FF1(a,b,c,d,e,i++);
102b0104773SPascal Brand FF1(e,a,b,c,d,i++);
103b0104773SPascal Brand FF1(d,e,a,b,c,i++);
104b0104773SPascal Brand FF1(c,d,e,a,b,i++);
105b0104773SPascal Brand FF1(b,c,d,e,a,i++);
106b0104773SPascal Brand }
107b0104773SPascal Brand
108b0104773SPascal Brand /* round three */
109b0104773SPascal Brand for (; i < 60; ) {
110b0104773SPascal Brand FF2(a,b,c,d,e,i++);
111b0104773SPascal Brand FF2(e,a,b,c,d,i++);
112b0104773SPascal Brand FF2(d,e,a,b,c,i++);
113b0104773SPascal Brand FF2(c,d,e,a,b,i++);
114b0104773SPascal Brand FF2(b,c,d,e,a,i++);
115b0104773SPascal Brand }
116b0104773SPascal Brand
117b0104773SPascal Brand /* round four */
118b0104773SPascal Brand for (; i < 80; ) {
119b0104773SPascal Brand FF3(a,b,c,d,e,i++);
120b0104773SPascal Brand FF3(e,a,b,c,d,i++);
121b0104773SPascal Brand FF3(d,e,a,b,c,i++);
122b0104773SPascal Brand FF3(c,d,e,a,b,i++);
123b0104773SPascal Brand FF3(b,c,d,e,a,i++);
124b0104773SPascal Brand }
125b0104773SPascal Brand #endif
126b0104773SPascal Brand
127b0104773SPascal Brand #undef FF0
128b0104773SPascal Brand #undef FF1
129b0104773SPascal Brand #undef FF2
130b0104773SPascal Brand #undef FF3
131b0104773SPascal Brand
132b0104773SPascal Brand /* store */
133b0104773SPascal Brand md->sha1.state[0] = md->sha1.state[0] + a;
134b0104773SPascal Brand md->sha1.state[1] = md->sha1.state[1] + b;
135b0104773SPascal Brand md->sha1.state[2] = md->sha1.state[2] + c;
136b0104773SPascal Brand md->sha1.state[3] = md->sha1.state[3] + d;
137b0104773SPascal Brand md->sha1.state[4] = md->sha1.state[4] + e;
138b0104773SPascal Brand
139b0104773SPascal Brand return CRYPT_OK;
140b0104773SPascal Brand }
141b0104773SPascal Brand
142b0104773SPascal Brand #ifdef LTC_CLEAN_STACK
s_sha1_compress(hash_state * md,const unsigned char * buf)143*8411e6adSJerome Forissier static int s_sha1_compress(hash_state *md, const unsigned char *buf)
144b0104773SPascal Brand {
145b0104773SPascal Brand int err;
146*8411e6adSJerome Forissier err = ss_sha1_compress(md, buf);
147b0104773SPascal Brand burn_stack(sizeof(ulong32) * 87);
148b0104773SPascal Brand return err;
149b0104773SPascal Brand }
150b0104773SPascal Brand #endif
151b0104773SPascal Brand
152b0104773SPascal Brand /**
153b0104773SPascal Brand Initialize the hash state
154b0104773SPascal Brand @param md The hash state you wish to initialize
155b0104773SPascal Brand @return CRYPT_OK if successful
156b0104773SPascal Brand */
sha1_init(hash_state * md)157b0104773SPascal Brand int sha1_init(hash_state * md)
158b0104773SPascal Brand {
159b0104773SPascal Brand LTC_ARGCHK(md != NULL);
160b0104773SPascal Brand md->sha1.state[0] = 0x67452301UL;
161b0104773SPascal Brand md->sha1.state[1] = 0xefcdab89UL;
162b0104773SPascal Brand md->sha1.state[2] = 0x98badcfeUL;
163b0104773SPascal Brand md->sha1.state[3] = 0x10325476UL;
164b0104773SPascal Brand md->sha1.state[4] = 0xc3d2e1f0UL;
165b0104773SPascal Brand md->sha1.curlen = 0;
166b0104773SPascal Brand md->sha1.length = 0;
167b0104773SPascal Brand return CRYPT_OK;
168b0104773SPascal Brand }
169b0104773SPascal Brand
170b0104773SPascal Brand /**
171b0104773SPascal Brand Process a block of memory though the hash
172b0104773SPascal Brand @param md The hash state
173b0104773SPascal Brand @param in The data to hash
174b0104773SPascal Brand @param inlen The length of the data (octets)
175b0104773SPascal Brand @return CRYPT_OK if successful
176b0104773SPascal Brand */
177*8411e6adSJerome Forissier HASH_PROCESS(sha1_process, s_sha1_compress, sha1, 64)
178b0104773SPascal Brand
179b0104773SPascal Brand /**
180b0104773SPascal Brand Terminate the hash to get the digest
181b0104773SPascal Brand @param md The hash state
182b0104773SPascal Brand @param out [out] The destination of the hash (20 bytes)
183b0104773SPascal Brand @return CRYPT_OK if successful
184b0104773SPascal Brand */
sha1_done(hash_state * md,unsigned char * out)185b0104773SPascal Brand int sha1_done(hash_state * md, unsigned char *out)
186b0104773SPascal Brand {
187b0104773SPascal Brand int i;
188b0104773SPascal Brand
189b0104773SPascal Brand LTC_ARGCHK(md != NULL);
190b0104773SPascal Brand LTC_ARGCHK(out != NULL);
191b0104773SPascal Brand
192b0104773SPascal Brand if (md->sha1.curlen >= sizeof(md->sha1.buf)) {
193b0104773SPascal Brand return CRYPT_INVALID_ARG;
194b0104773SPascal Brand }
195b0104773SPascal Brand
196b0104773SPascal Brand /* increase the length of the message */
197b0104773SPascal Brand md->sha1.length += md->sha1.curlen * 8;
198b0104773SPascal Brand
199b0104773SPascal Brand /* append the '1' bit */
200b0104773SPascal Brand md->sha1.buf[md->sha1.curlen++] = (unsigned char)0x80;
201b0104773SPascal Brand
202b0104773SPascal Brand /* if the length is currently above 56 bytes we append zeros
203b0104773SPascal Brand * then compress. Then we can fall back to padding zeros and length
204b0104773SPascal Brand * encoding like normal.
205b0104773SPascal Brand */
206b0104773SPascal Brand if (md->sha1.curlen > 56) {
207b0104773SPascal Brand while (md->sha1.curlen < 64) {
208b0104773SPascal Brand md->sha1.buf[md->sha1.curlen++] = (unsigned char)0;
209b0104773SPascal Brand }
210*8411e6adSJerome Forissier s_sha1_compress(md, md->sha1.buf);
211b0104773SPascal Brand md->sha1.curlen = 0;
212b0104773SPascal Brand }
213b0104773SPascal Brand
214b0104773SPascal Brand /* pad upto 56 bytes of zeroes */
215b0104773SPascal Brand while (md->sha1.curlen < 56) {
216b0104773SPascal Brand md->sha1.buf[md->sha1.curlen++] = (unsigned char)0;
217b0104773SPascal Brand }
218b0104773SPascal Brand
219b0104773SPascal Brand /* store length */
220b0104773SPascal Brand STORE64H(md->sha1.length, md->sha1.buf+56);
221*8411e6adSJerome Forissier s_sha1_compress(md, md->sha1.buf);
222b0104773SPascal Brand
223b0104773SPascal Brand /* copy output */
224b0104773SPascal Brand for (i = 0; i < 5; i++) {
225b0104773SPascal Brand STORE32H(md->sha1.state[i], out+(4*i));
226b0104773SPascal Brand }
227b0104773SPascal Brand #ifdef LTC_CLEAN_STACK
228b0104773SPascal Brand zeromem(md, sizeof(hash_state));
229b0104773SPascal Brand #endif
230b0104773SPascal Brand return CRYPT_OK;
231b0104773SPascal Brand }
232b0104773SPascal Brand
233b0104773SPascal Brand /**
234b0104773SPascal Brand Self-test the hash
235b0104773SPascal Brand @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled
236b0104773SPascal Brand */
sha1_test(void)237b0104773SPascal Brand int sha1_test(void)
238b0104773SPascal Brand {
239b0104773SPascal Brand #ifndef LTC_TEST
240b0104773SPascal Brand return CRYPT_NOP;
241b0104773SPascal Brand #else
242b0104773SPascal Brand static const struct {
243b0104773SPascal Brand const char *msg;
244b0104773SPascal Brand unsigned char hash[20];
245b0104773SPascal Brand } tests[] = {
246b0104773SPascal Brand { "abc",
247b0104773SPascal Brand { 0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a,
248b0104773SPascal Brand 0xba, 0x3e, 0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c,
249b0104773SPascal Brand 0x9c, 0xd0, 0xd8, 0x9d }
250b0104773SPascal Brand },
251b0104773SPascal Brand { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
252b0104773SPascal Brand { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E,
253b0104773SPascal Brand 0xBA, 0xAE, 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5,
254b0104773SPascal Brand 0xE5, 0x46, 0x70, 0xF1 }
255b0104773SPascal Brand }
256b0104773SPascal Brand };
257b0104773SPascal Brand
258b0104773SPascal Brand int i;
259b0104773SPascal Brand unsigned char tmp[20];
260b0104773SPascal Brand hash_state md;
261b0104773SPascal Brand
262b0104773SPascal Brand for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) {
263b0104773SPascal Brand sha1_init(&md);
264*8411e6adSJerome Forissier sha1_process(&md, (unsigned char*)tests[i].msg, (unsigned long)XSTRLEN(tests[i].msg));
265b0104773SPascal Brand sha1_done(&md, tmp);
2665a913ee7SJerome Forissier if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "SHA1", i)) {
267b0104773SPascal Brand return CRYPT_FAIL_TESTVECTOR;
268b0104773SPascal Brand }
269b0104773SPascal Brand }
270b0104773SPascal Brand return CRYPT_OK;
271b0104773SPascal Brand #endif
272b0104773SPascal Brand }
273b0104773SPascal Brand
274b0104773SPascal Brand #endif
275b0104773SPascal Brand
276b0104773SPascal Brand
277