]> CyberLeo.Net >> Repos - SourceForge/eyefi-config.git/blob - sha1.c
Added dependencies on eyefi-config.h
[SourceForge/eyefi-config.git] / sha1.c
1 /*
2  * SHA1 hash implementation and interface functions
3  * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "eyefi-config.h"
16
17 #include <string.h>
18 #include <unistd.h>
19
20 #define SHA1_MAC_LEN 20
21 void sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac);
22
23 #define MD5_MAC_LEN 16
24
25 /**
26  * hmac_sha1_vector - HMAC-SHA1 over data vector (RFC 2104)
27  * @key: Key for HMAC operations
28  * @key_len: Length of the key in bytes
29  * @num_elem: Number of elements in the data vector
30  * @addr: Pointers to the data areas
31  * @len: Lengths of the data blocks
32  * @mac: Buffer for the hash (20 bytes)
33  */
34 void hmac_sha1_vector(const u8 *key, size_t key_len, size_t num_elem,
35                       const u8 *addr[], const size_t *len, u8 *mac)
36 {
37         unsigned char k_pad[64]; /* padding - key XORd with ipad/opad */
38         unsigned char tk[20];
39         const u8 *_addr[6];
40         size_t _len[6], i;
41
42         if (num_elem > 5) {
43                 /*
44                  * Fixed limit on the number of fragments to avoid having to
45                  * allocate memory (which could fail).
46                  */
47                 return;
48         }
49
50         /* if key is longer than 64 bytes reset it to key = SHA1(key) */
51         if (key_len > 64) {
52                 sha1_vector(1, &key, &key_len, tk);
53                 key = tk;
54                 key_len = 20;
55         }
56
57         /* the HMAC_SHA1 transform looks like:
58          *
59          * SHA1(K XOR opad, SHA1(K XOR ipad, text))
60          *
61          * where K is an n byte key
62          * ipad is the byte 0x36 repeated 64 times
63          * opad is the byte 0x5c repeated 64 times
64          * and text is the data being protected */
65
66         /* start out by storing key in ipad */
67         os_memset(k_pad, 0, sizeof(k_pad));
68         os_memcpy(k_pad, key, key_len);
69         /* XOR key with ipad values */
70         for (i = 0; i < 64; i++)
71                 k_pad[i] ^= 0x36;
72
73         /* perform inner SHA1 */
74         _addr[0] = k_pad;
75         _len[0] = 64;
76         for (i = 0; i < num_elem; i++) {
77                 _addr[i + 1] = addr[i];
78                 _len[i + 1] = len[i];
79         }
80         sha1_vector(1 + num_elem, _addr, _len, mac);
81
82         os_memset(k_pad, 0, sizeof(k_pad));
83         os_memcpy(k_pad, key, key_len);
84         /* XOR key with opad values */
85         for (i = 0; i < 64; i++)
86                 k_pad[i] ^= 0x5c;
87
88         /* perform outer SHA1 */
89         _addr[0] = k_pad;
90         _len[0] = 64;
91         _addr[1] = mac;
92         _len[1] = SHA1_MAC_LEN;
93         sha1_vector(2, _addr, _len, mac);
94 }
95
96
97 /**
98  * hmac_sha1 - HMAC-SHA1 over data buffer (RFC 2104)
99  * @key: Key for HMAC operations
100  * @key_len: Length of the key in bytes
101  * @data: Pointers to the data area
102  * @data_len: Length of the data area
103  * @mac: Buffer for the hash (20 bytes)
104  */
105 void hmac_sha1(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
106                u8 *mac)
107 {
108         hmac_sha1_vector(key, key_len, 1, &data, &data_len, mac);
109 }
110
111
112 /**
113  * sha1_prf - SHA1-based Pseudo-Random Function (PRF) (IEEE 802.11i, 8.5.1.1)
114  * @key: Key for PRF
115  * @key_len: Length of the key in bytes
116  * @label: A unique label for each purpose of the PRF
117  * @data: Extra data to bind into the key
118  * @data_len: Length of the data
119  * @buf: Buffer for the generated pseudo-random key
120  * @buf_len: Number of bytes of key to generate
121  *
122  * This function is used to derive new, cryptographically separate keys from a
123  * given key (e.g., PMK in IEEE 802.11i).
124  */
125 void sha1_prf(const u8 *key, size_t key_len, const char *label,
126               const u8 *data, size_t data_len, u8 *buf, size_t buf_len)
127 {
128         u8 zero = 0, counter = 0;
129         size_t pos, plen;
130         u8 hash[SHA1_MAC_LEN];
131         size_t label_len = os_strlen(label);
132         const unsigned char *addr[4];
133         size_t len[4];
134
135         addr[0] = (u8 *) label;
136         len[0] = label_len;
137         addr[1] = &zero;
138         len[1] = 1;
139         addr[2] = data;
140         len[2] = data_len;
141         addr[3] = &counter;
142         len[3] = 1;
143
144         pos = 0;
145         while (pos < buf_len) {
146                 plen = buf_len - pos;
147                 if (plen >= SHA1_MAC_LEN) {
148                         hmac_sha1_vector(key, key_len, 4, addr, len,
149                                          &buf[pos]);
150                         pos += SHA1_MAC_LEN;
151                 } else {
152                         hmac_sha1_vector(key, key_len, 4, addr, len,
153                                          hash);
154                         os_memcpy(&buf[pos], hash, plen);
155                         break;
156                 }
157                 counter++;
158         }
159 }
160
161
162 /**
163  * sha1_t_prf - EAP-FAST Pseudo-Random Function (T-PRF)
164  * @key: Key for PRF
165  * @key_len: Length of the key in bytes
166  * @label: A unique label for each purpose of the PRF
167  * @seed: Seed value to bind into the key
168  * @seed_len: Length of the seed
169  * @buf: Buffer for the generated pseudo-random key
170  * @buf_len: Number of bytes of key to generate
171  *
172  * This function is used to derive new, cryptographically separate keys from a
173  * given key for EAP-FAST. T-PRF is defined in
174  * draft-cam-winget-eap-fast-02.txt, Appendix B.
175  */
176 void sha1_t_prf(const u8 *key, size_t key_len, const char *label,
177                 const u8 *seed, size_t seed_len, u8 *buf, size_t buf_len)
178 {
179         unsigned char counter = 0;
180         size_t pos, plen;
181         u8 hash[SHA1_MAC_LEN];
182         size_t label_len = os_strlen(label);
183         u8 output_len[2];
184         const unsigned char *addr[5];
185         size_t len[5];
186
187         addr[0] = hash;
188         len[0] = 0;
189         addr[1] = (unsigned char *) label;
190         len[1] = label_len + 1;
191         addr[2] = seed;
192         len[2] = seed_len;
193         addr[3] = output_len;
194         len[3] = 2;
195         addr[4] = &counter;
196         len[4] = 1;
197
198         output_len[0] = (buf_len >> 8) & 0xff;
199         output_len[1] = buf_len & 0xff;
200         pos = 0;
201         while (pos < buf_len) {
202                 counter++;
203                 plen = buf_len - pos;
204                 hmac_sha1_vector(key, key_len, 5, addr, len, hash);
205                 if (plen >= SHA1_MAC_LEN) {
206                         os_memcpy(&buf[pos], hash, SHA1_MAC_LEN);
207                         pos += SHA1_MAC_LEN;
208                 } else {
209                         os_memcpy(&buf[pos], hash, plen);
210                         break;
211                 }
212                 len[0] = SHA1_MAC_LEN;
213         }
214 }
215
216
217 /**
218  * tls_prf - Pseudo-Random Function for TLS (TLS-PRF, RFC 2246)
219  * @secret: Key for PRF
220  * @secret_len: Length of the key in bytes
221  * @label: A unique label for each purpose of the PRF
222  * @seed: Seed value to bind into the key
223  * @seed_len: Length of the seed
224  * @out: Buffer for the generated pseudo-random key
225  * @outlen: Number of bytes of key to generate
226  * Returns: 0 on success, -1 on failure.
227  *
228  * This function is used to derive new, cryptographically separate keys from a
229  * given key in TLS. This PRF is defined in RFC 2246, Chapter 5.
230  */
231 int tls_prf(const u8 *secret, size_t secret_len, const char *label,
232             const u8 *seed, size_t seed_len, u8 *out, size_t outlen)
233 {
234         size_t L_S1, L_S2, i;
235         const u8 *S1, *S2;
236         u8 A_MD5[MD5_MAC_LEN], A_SHA1[SHA1_MAC_LEN];
237         u8 P_MD5[MD5_MAC_LEN], P_SHA1[SHA1_MAC_LEN];
238         int MD5_pos, SHA1_pos;
239         const u8 *MD5_addr[3];
240         size_t MD5_len[3];
241         const unsigned char *SHA1_addr[3];
242         size_t SHA1_len[3];
243
244         if (secret_len & 1)
245                 return -1;
246
247         MD5_addr[0] = A_MD5;
248         MD5_len[0] = MD5_MAC_LEN;
249         MD5_addr[1] = (unsigned char *) label;
250         MD5_len[1] = os_strlen(label);
251         MD5_addr[2] = seed;
252         MD5_len[2] = seed_len;
253
254         SHA1_addr[0] = A_SHA1;
255         SHA1_len[0] = SHA1_MAC_LEN;
256         SHA1_addr[1] = (unsigned char *) label;
257         SHA1_len[1] = os_strlen(label);
258         SHA1_addr[2] = seed;
259         SHA1_len[2] = seed_len;
260
261         /* RFC 2246, Chapter 5
262          * A(0) = seed, A(i) = HMAC(secret, A(i-1))
263          * P_hash = HMAC(secret, A(1) + seed) + HMAC(secret, A(2) + seed) + ..
264          * PRF = P_MD5(S1, label + seed) XOR P_SHA-1(S2, label + seed)
265          */
266
267         L_S1 = L_S2 = (secret_len + 1) / 2;
268         S1 = secret;
269         S2 = secret + L_S1;
270
271         hmac_md5_vector(S1, L_S1, 2, &MD5_addr[1], &MD5_len[1], A_MD5);
272         hmac_sha1_vector(S2, L_S2, 2, &SHA1_addr[1], &SHA1_len[1], A_SHA1);
273
274         MD5_pos = MD5_MAC_LEN;
275         SHA1_pos = SHA1_MAC_LEN;
276         for (i = 0; i < outlen; i++) {
277                 if (MD5_pos == MD5_MAC_LEN) {
278                         hmac_md5_vector(S1, L_S1, 3, MD5_addr, MD5_len, P_MD5);
279                         MD5_pos = 0;
280                         hmac_md5(S1, L_S1, A_MD5, MD5_MAC_LEN, A_MD5);
281                 }
282                 if (SHA1_pos == SHA1_MAC_LEN) {
283                         hmac_sha1_vector(S2, L_S2, 3, SHA1_addr, SHA1_len,
284                                          P_SHA1);
285                         SHA1_pos = 0;
286                         hmac_sha1(S2, L_S2, A_SHA1, SHA1_MAC_LEN, A_SHA1);
287                 }
288
289                 out[i] = P_MD5[MD5_pos] ^ P_SHA1[SHA1_pos];
290
291                 MD5_pos++;
292                 SHA1_pos++;
293         }
294
295         return 0;
296 }
297
298
299 static void pbkdf2_sha1_f(const char *passphrase, const char *ssid,
300                           size_t ssid_len, int iterations, unsigned int count,
301                           u8 *digest)
302 {
303         unsigned char tmp[SHA1_MAC_LEN], tmp2[SHA1_MAC_LEN];
304         int i, j;
305         unsigned char count_buf[4];
306         const u8 *addr[2];
307         size_t len[2];
308         size_t passphrase_len = os_strlen(passphrase);
309
310         addr[0] = (u8 *) ssid;
311         len[0] = ssid_len;
312         addr[1] = count_buf;
313         len[1] = 4;
314
315         /* F(P, S, c, i) = U1 xor U2 xor ... Uc
316          * U1 = PRF(P, S || i)
317          * U2 = PRF(P, U1)
318          * Uc = PRF(P, Uc-1)
319          */
320
321         count_buf[0] = (count >> 24) & 0xff;
322         count_buf[1] = (count >> 16) & 0xff;
323         count_buf[2] = (count >> 8) & 0xff;
324         count_buf[3] = count & 0xff;
325         hmac_sha1_vector((u8 *) passphrase, passphrase_len, 2, addr, len, tmp);
326         os_memcpy(digest, tmp, SHA1_MAC_LEN);
327
328         for (i = 1; i < iterations; i++) {
329                 hmac_sha1((u8 *) passphrase, passphrase_len, tmp, SHA1_MAC_LEN,
330                           tmp2);
331                 os_memcpy(tmp, tmp2, SHA1_MAC_LEN);
332                 for (j = 0; j < SHA1_MAC_LEN; j++)
333                         digest[j] ^= tmp2[j];
334         }
335 }
336
337
338 /**
339  * pbkdf2_sha1 - SHA1-based key derivation function (PBKDF2) for IEEE 802.11i
340  * @passphrase: ASCII passphrase
341  * @ssid: SSID
342  * @ssid_len: SSID length in bytes
343  * @interations: Number of iterations to run
344  * @buf: Buffer for the generated key
345  * @buflen: Length of the buffer in bytes
346  *
347  * This function is used to derive PSK for WPA-PSK. For this protocol,
348  * iterations is set to 4096 and buflen to 32. This function is described in
349  * IEEE Std 802.11-2004, Clause H.4. The main construction is from PKCS#5 v2.0.
350  */
351 void pbkdf2_sha1(const char *passphrase, const char *ssid, size_t ssid_len,
352                  int iterations, u8 *buf, size_t buflen)
353 {
354         unsigned int count = 0;
355         unsigned char *pos = buf;
356         size_t left = buflen, plen;
357         unsigned char digest[SHA1_MAC_LEN];
358
359         while (left > 0) {
360                 count++;
361                 pbkdf2_sha1_f(passphrase, ssid, ssid_len, iterations, count,
362                               digest);
363                 plen = left > SHA1_MAC_LEN ? SHA1_MAC_LEN : left;
364                 os_memcpy(pos, digest, plen);
365                 pos += plen;
366                 left -= plen;
367         }
368 }
369
370
371 struct SHA1Context {
372         u32 state[5];
373         u32 count[2];
374         unsigned char buffer[64];
375 };
376
377 typedef struct SHA1Context SHA1_CTX;
378
379 #ifndef CONFIG_CRYPTO_INTERNAL
380 static void SHA1Init(struct SHA1Context *context);
381 static void SHA1Update(struct SHA1Context *context, const void *data, u32 len);
382 static void SHA1Final(unsigned char digest[20], struct SHA1Context *context);
383 #endif /* CONFIG_CRYPTO_INTERNAL */
384 static void SHA1Transform(u32 state[5], const unsigned char buffer[64]);
385
386 /**
387  * sha1_vector - SHA-1 hash for data vector
388  * @num_elem: Number of elements in the data vector
389  * @addr: Pointers to the data areas
390  * @len: Lengths of the data blocks
391  * @mac: Buffer for the hash
392  */
393 void sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len,
394                  u8 *mac)
395 {
396         SHA1_CTX ctx;
397         size_t i;
398
399         SHA1Init(&ctx);
400         for (i = 0; i < num_elem; i++)
401                 SHA1Update(&ctx, addr[i], len[i]);
402         SHA1Final(mac, &ctx);
403 }
404
405
406 int fips186_2_prf(const u8 *seed, size_t seed_len, u8 *x, size_t xlen)
407 {
408         u8 xkey[64];
409         u32 t[5], _t[5];
410         int i, j, m, k;
411         u8 *xpos = x;
412         u32 carry;
413
414         if (seed_len > sizeof(xkey))
415                 seed_len = sizeof(xkey);
416
417         /* FIPS 186-2 + change notice 1 */
418
419         os_memcpy(xkey, seed, seed_len);
420         os_memset(xkey + seed_len, 0, 64 - seed_len);
421         t[0] = 0x67452301;
422         t[1] = 0xEFCDAB89;
423         t[2] = 0x98BADCFE;
424         t[3] = 0x10325476;
425         t[4] = 0xC3D2E1F0;
426
427         m = xlen / 40;
428         for (j = 0; j < m; j++) {
429                 /* XSEED_j = 0 */
430                 for (i = 0; i < 2; i++) {
431                         /* XVAL = (XKEY + XSEED_j) mod 2^b */
432
433                         /* w_i = G(t, XVAL) */
434                         os_memcpy(_t, t, 20);
435                         SHA1Transform(_t, xkey);
436                         _t[0] = host_to_be32(_t[0]);
437                         _t[1] = host_to_be32(_t[1]);
438                         _t[2] = host_to_be32(_t[2]);
439                         _t[3] = host_to_be32(_t[3]);
440                         _t[4] = host_to_be32(_t[4]);
441                         os_memcpy(xpos, _t, 20);
442
443                         /* XKEY = (1 + XKEY + w_i) mod 2^b */
444                         carry = 1;
445                         for (k = 19; k >= 0; k--) {
446                                 carry += xkey[k] + xpos[k];
447                                 xkey[k] = carry & 0xff;
448                                 carry >>= 8;
449                         }
450
451                         xpos += SHA1_MAC_LEN;
452                 }
453                 /* x_j = w_0|w_1 */
454         }
455
456         return 0;
457 }
458
459
460 /* ===== start - public domain SHA1 implementation ===== */
461
462 /*
463 SHA-1 in C
464 By Steve Reid <sreid@sea-to-sky.net>
465 100% Public Domain
466
467 -----------------
468 Modified 7/98 
469 By James H. Brown <jbrown@burgoyne.com>
470 Still 100% Public Domain
471
472 Corrected a problem which generated improper hash values on 16 bit machines
473 Routine SHA1Update changed from
474         void SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned int
475 len)
476 to
477         void SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned
478 long len)
479
480 The 'len' parameter was declared an int which works fine on 32 bit machines.
481 However, on 16 bit machines an int is too small for the shifts being done
482 against
483 it.  This caused the hash function to generate incorrect values if len was
484 greater than 8191 (8K - 1) due to the 'len << 3' on line 3 of SHA1Update().
485
486 Since the file IO in main() reads 16K at a time, any file 8K or larger would
487 be guaranteed to generate the wrong hash (e.g. Test Vector #3, a million
488 "a"s).
489
490 I also changed the declaration of variables i & j in SHA1Update to 
491 unsigned long from unsigned int for the same reason.
492
493 These changes should make no difference to any 32 bit implementations since
494 an
495 int and a long are the same size in those environments.
496
497 --
498 I also corrected a few compiler warnings generated by Borland C.
499 1. Added #include <process.h> for exit() prototype
500 2. Removed unused variable 'j' in SHA1Final
501 3. Changed exit(0) to return(0) at end of main.
502
503 ALL changes I made can be located by searching for comments containing 'JHB'
504 -----------------
505 Modified 8/98
506 By Steve Reid <sreid@sea-to-sky.net>
507 Still 100% public domain
508
509 1- Removed #include <process.h> and used return() instead of exit()
510 2- Fixed overwriting of finalcount in SHA1Final() (discovered by Chris Hall)
511 3- Changed email address from steve@edmweb.com to sreid@sea-to-sky.net
512
513 -----------------
514 Modified 4/01
515 By Saul Kravitz <Saul.Kravitz@celera.com>
516 Still 100% PD
517 Modified to run on Compaq Alpha hardware.  
518
519 -----------------
520 Modified 4/01
521 By Jouni Malinen <j@w1.fi>
522 Minor changes to match the coding style used in Dynamics.
523
524 Modified September 24, 2004
525 By Jouni Malinen <j@w1.fi>
526 Fixed alignment issue in SHA1Transform when SHA1HANDSOFF is defined.
527
528 */
529
530 /*
531 Test Vectors (from FIPS PUB 180-1)
532 "abc"
533   A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
534 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
535   84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
536 A million repetitions of "a"
537   34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
538 */
539
540 #define SHA1HANDSOFF
541
542 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
543
544 /* blk0() and blk() perform the initial expand. */
545 /* I got the idea of expanding during the round function from SSLeay */
546 #ifndef WORDS_BIGENDIAN
547 #define blk0(i) (block->l[i] = (rol(block->l[i], 24) & 0xFF00FF00) | \
548         (rol(block->l[i], 8) & 0x00FF00FF))
549 #else
550 #define blk0(i) block->l[i]
551 #endif
552 #define blk(i) (block->l[i & 15] = rol(block->l[(i + 13) & 15] ^ \
553         block->l[(i + 8) & 15] ^ block->l[(i + 2) & 15] ^ block->l[i & 15], 1))
554
555 /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
556 #define R0(v,w,x,y,z,i) \
557         z += ((w & (x ^ y)) ^ y) + blk0(i) + 0x5A827999 + rol(v, 5); \
558         w = rol(w, 30);
559 #define R1(v,w,x,y,z,i) \
560         z += ((w & (x ^ y)) ^ y) + blk(i) + 0x5A827999 + rol(v, 5); \
561         w = rol(w, 30);
562 #define R2(v,w,x,y,z,i) \
563         z += (w ^ x ^ y) + blk(i) + 0x6ED9EBA1 + rol(v, 5); w = rol(w, 30);
564 #define R3(v,w,x,y,z,i) \
565         z += (((w | x) & y) | (w & x)) + blk(i) + 0x8F1BBCDC + rol(v, 5); \
566         w = rol(w, 30);
567 #define R4(v,w,x,y,z,i) \
568         z += (w ^ x ^ y) + blk(i) + 0xCA62C1D6 + rol(v, 5); \
569         w=rol(w, 30);
570
571
572 #ifdef VERBOSE  /* SAK */
573 void SHAPrintContext(SHA1_CTX *context, char *msg)
574 {
575         printf("%s (%d,%d) %x %x %x %x %x\n",
576                msg,
577                context->count[0], context->count[1], 
578                context->state[0],
579                context->state[1],
580                context->state[2],
581                context->state[3],
582                context->state[4]);
583 }
584 #endif
585
586 /* Hash a single 512-bit block. This is the core of the algorithm. */
587
588 static void SHA1Transform(u32 state[5], const unsigned char buffer[64])
589 {
590         u32 a, b, c, d, e;
591         typedef union {
592                 unsigned char c[64];
593                 u32 l[16];
594         } CHAR64LONG16;
595         CHAR64LONG16* block;
596 #ifdef SHA1HANDSOFF
597         u32 workspace[16];
598         block = (CHAR64LONG16 *) workspace;
599         os_memcpy(block, buffer, 64);
600 #else
601         block = (CHAR64LONG16 *) buffer;
602 #endif
603         /* Copy context->state[] to working vars */
604         a = state[0];
605         b = state[1];
606         c = state[2];
607         d = state[3];
608         e = state[4];
609         /* 4 rounds of 20 operations each. Loop unrolled. */
610         R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3);
611         R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7);
612         R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11);
613         R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15);
614         R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
615         R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
616         R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
617         R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
618         R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
619         R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
620         R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
621         R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
622         R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
623         R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
624         R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
625         R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
626         R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
627         R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
628         R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
629         R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
630         /* Add the working vars back into context.state[] */
631         state[0] += a;
632         state[1] += b;
633         state[2] += c;
634         state[3] += d;
635         state[4] += e;
636         /* Wipe variables */
637         a = b = c = d = e = 0;
638 #ifdef SHA1HANDSOFF
639         os_memset(block, 0, 64);
640 #endif
641 }
642
643
644 /* SHA1Init - Initialize new context */
645
646 void SHA1Init(SHA1_CTX* context)
647 {
648         /* SHA1 initialization constants */
649         context->state[0] = 0x67452301;
650         context->state[1] = 0xEFCDAB89;
651         context->state[2] = 0x98BADCFE;
652         context->state[3] = 0x10325476;
653         context->state[4] = 0xC3D2E1F0;
654         context->count[0] = context->count[1] = 0;
655 }
656
657
658 /* Run your data through this. */
659
660 void SHA1Update(SHA1_CTX* context, const void *_data, u32 len)
661 {
662         u32 i, j;
663         const unsigned char *data = _data;
664
665 #ifdef VERBOSE
666         SHAPrintContext(context, "before");
667 #endif
668         j = (context->count[0] >> 3) & 63;
669         if ((context->count[0] += len << 3) < (len << 3))
670                 context->count[1]++;
671         context->count[1] += (len >> 29);
672         if ((j + len) > 63) {
673                 os_memcpy(&context->buffer[j], data, (i = 64-j));
674                 SHA1Transform(context->state, context->buffer);
675                 for ( ; i + 63 < len; i += 64) {
676                         SHA1Transform(context->state, &data[i]);
677                 }
678                 j = 0;
679         }
680         else i = 0;
681         os_memcpy(&context->buffer[j], &data[i], len - i);
682 #ifdef VERBOSE
683         SHAPrintContext(context, "after ");
684 #endif
685 }
686
687
688 /* Add padding and return the message digest. */
689
690 void SHA1Final(unsigned char digest[20], SHA1_CTX* context)
691 {
692         u32 i;
693         unsigned char finalcount[8];
694
695         for (i = 0; i < 8; i++) {
696                 finalcount[i] = (unsigned char)
697                         ((context->count[(i >= 4 ? 0 : 1)] >>
698                           ((3-(i & 3)) * 8) ) & 255);  /* Endian independent */
699         }
700         SHA1Update(context, (unsigned char *) "\200", 1);
701         while ((context->count[0] & 504) != 448) {
702                 SHA1Update(context, (unsigned char *) "\0", 1);
703         }
704         SHA1Update(context, finalcount, 8);  /* Should cause a SHA1Transform()
705                                               */
706         for (i = 0; i < 20; i++) {
707                 digest[i] = (unsigned char)
708                         ((context->state[i >> 2] >> ((3 - (i & 3)) * 8)) &
709                          255);
710         }
711         /* Wipe variables */
712         i = 0;
713         os_memset(context->buffer, 0, 64);
714         os_memset(context->state, 0, 20);
715         os_memset(context->count, 0, 8);
716         os_memset(finalcount, 0, 8);
717 }
718
719 /* ===== end - public domain SHA1 implementation ===== */