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_init(hmac_state *hmac, int hash, const unsigned char *key, unsigned long keylen) |
|
37 { |
143
|
38 unsigned char *buf; |
3
|
39 unsigned long hashsize; |
|
40 unsigned long i, z; |
|
41 int err; |
|
42 |
|
43 _ARGCHK(hmac != NULL); |
143
|
44 _ARGCHK(key != NULL); |
3
|
45 |
143
|
46 /* valid hash? */ |
3
|
47 if ((err = hash_is_valid(hash)) != CRYPT_OK) { |
|
48 return err; |
|
49 } |
143
|
50 hmac->hash = hash; |
|
51 hashsize = hash_descriptor[hash].hashsize; |
3
|
52 |
|
53 /* valid key length? */ |
|
54 if (keylen == 0) { |
|
55 return CRYPT_INVALID_KEYSIZE; |
|
56 } |
|
57 |
143
|
58 /* allocate ram for buf */ |
|
59 buf = XMALLOC(HMAC_BLOCKSIZE); |
|
60 if (buf == NULL) { |
|
61 return CRYPT_MEM; |
|
62 } |
3
|
63 |
143
|
64 /* allocate memory for key */ |
|
65 hmac->key = XMALLOC(HMAC_BLOCKSIZE); |
|
66 if (hmac->key == NULL) { |
|
67 XFREE(buf); |
|
68 return CRYPT_MEM; |
|
69 } |
|
70 |
|
71 /* (1) make sure we have a large enough key */ |
3
|
72 if(keylen > HMAC_BLOCKSIZE) { |
143
|
73 z = HMAC_BLOCKSIZE; |
3
|
74 if ((err = hash_memory(hash, key, keylen, hmac->key, &z)) != CRYPT_OK) { |
143
|
75 goto __ERR; |
3
|
76 } |
|
77 if(hashsize < HMAC_BLOCKSIZE) { |
|
78 zeromem((hmac->key) + hashsize, (size_t)(HMAC_BLOCKSIZE - hashsize)); |
|
79 } |
|
80 keylen = hashsize; |
|
81 } else { |
143
|
82 XMEMCPY(hmac->key, key, (size_t)keylen); |
3
|
83 if(keylen < HMAC_BLOCKSIZE) { |
|
84 zeromem((hmac->key) + keylen, (size_t)(HMAC_BLOCKSIZE - keylen)); |
|
85 } |
|
86 } |
|
87 |
143
|
88 /* Create the initial vector for step (3) */ |
3
|
89 for(i=0; i < HMAC_BLOCKSIZE; i++) { |
|
90 buf[i] = hmac->key[i] ^ 0x36; |
|
91 } |
|
92 |
143
|
93 /* Pre-pend that to the hash data */ |
|
94 if ((err = hash_descriptor[hash].init(&hmac->md)) != CRYPT_OK) { |
|
95 goto __ERR; |
|
96 } |
3
|
97 |
143
|
98 if ((err = hash_descriptor[hash].process(&hmac->md, buf, HMAC_BLOCKSIZE)) != CRYPT_OK) { |
|
99 goto __ERR; |
|
100 } |
|
101 goto done; |
|
102 __ERR: |
|
103 /* free the key since we failed */ |
|
104 XFREE(hmac->key); |
|
105 done: |
|
106 #ifdef CLEAN_STACK |
|
107 zeromem(buf, HMAC_BLOCKSIZE); |
|
108 #endif |
|
109 |
|
110 XFREE(buf); |
|
111 return err; |
3
|
112 } |
|
113 |
|
114 #endif |