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 /* Submited by Dobes Vandermeer ([email protected]) */ |
|
12 |
|
13 #include "mycrypt.h" |
|
14 |
|
15 /* |
|
16 (1) append zeros to the end of K to create a B byte string |
|
17 (e.g., if K is of length 20 bytes and B=64, then K will be |
|
18 appended with 44 zero bytes 0x00) |
|
19 (2) XOR (bitwise exclusive-OR) the B byte string computed in step |
|
20 (1) with ipad (ipad = the byte 0x36 repeated B times) |
|
21 (3) append the stream of data 'text' to the B byte string resulting |
|
22 from step (2) |
|
23 (4) apply H to the stream generated in step (3) |
|
24 (5) XOR (bitwise exclusive-OR) the B byte string computed in |
|
25 step (1) with opad (opad = the byte 0x5C repeated B times.) |
|
26 (6) append the H result from step (4) to the B byte string |
|
27 resulting from step (5) |
|
28 (7) apply H to the stream generated in step (6) and output |
|
29 the result |
|
30 */ |
|
31 |
|
32 #ifdef HMAC |
|
33 |
|
34 #define HMAC_BLOCKSIZE hash_descriptor[hash].blocksize |
|
35 |
|
36 int hmac_done(hmac_state *hmac, unsigned char *hashOut, unsigned long *outlen) |
|
37 { |
143
|
38 unsigned char *buf, *isha; |
3
|
39 unsigned long hashsize, i; |
|
40 int hash, err; |
|
41 |
143
|
42 _ARGCHK(hmac != NULL); |
3
|
43 _ARGCHK(hashOut != NULL); |
|
44 |
143
|
45 /* test hash */ |
3
|
46 hash = hmac->hash; |
|
47 if((err = hash_is_valid(hash)) != CRYPT_OK) { |
|
48 return err; |
|
49 } |
|
50 |
|
51 /* get the hash message digest size */ |
|
52 hashsize = hash_descriptor[hash].hashsize; |
|
53 |
143
|
54 /* allocate buffers */ |
|
55 buf = XMALLOC(HMAC_BLOCKSIZE); |
|
56 isha = XMALLOC(hashsize); |
|
57 if (buf == NULL || isha == NULL) { |
|
58 if (buf != NULL) { |
|
59 XFREE(buf); |
|
60 } |
|
61 if (isha != NULL) { |
|
62 XFREE(isha); |
|
63 } |
|
64 return CRYPT_MEM; |
3
|
65 } |
|
66 |
143
|
67 /* Get the hash of the first HMAC vector plus the data */ |
|
68 if ((err = hash_descriptor[hash].done(&hmac->md, isha)) != CRYPT_OK) { |
|
69 goto __ERR; |
|
70 } |
|
71 |
|
72 /* Create the second HMAC vector vector for step (3) */ |
3
|
73 for(i=0; i < HMAC_BLOCKSIZE; i++) { |
|
74 buf[i] = hmac->key[i] ^ 0x5C; |
|
75 } |
|
76 |
143
|
77 /* Now calculate the "outer" hash for step (5), (6), and (7) */ |
|
78 if ((err = hash_descriptor[hash].init(&hmac->md)) != CRYPT_OK) { |
|
79 goto __ERR; |
|
80 } |
|
81 if ((err = hash_descriptor[hash].process(&hmac->md, buf, HMAC_BLOCKSIZE)) != CRYPT_OK) { |
|
82 goto __ERR; |
|
83 } |
|
84 if ((err = hash_descriptor[hash].process(&hmac->md, isha, hashsize)) != CRYPT_OK) { |
|
85 goto __ERR; |
|
86 } |
|
87 if ((err = hash_descriptor[hash].done(&hmac->md, buf)) != CRYPT_OK) { |
|
88 goto __ERR; |
|
89 } |
3
|
90 |
143
|
91 /* copy to output */ |
3
|
92 for (i = 0; i < hashsize && i < *outlen; i++) { |
|
93 hashOut[i] = buf[i]; |
|
94 } |
|
95 *outlen = i; |
|
96 |
143
|
97 err = CRYPT_OK; |
|
98 __ERR: |
|
99 XFREE(hmac->key); |
3
|
100 #ifdef CLEAN_STACK |
143
|
101 zeromem(isha, hashsize); |
|
102 zeromem(buf, hashsize); |
3
|
103 zeromem(hmac, sizeof(*hmac)); |
|
104 #endif |
143
|
105 |
|
106 XFREE(isha); |
|
107 XFREE(buf); |
|
108 |
|
109 return err; |
3
|
110 } |
|
111 |
|
112 #endif |