/src/dropbear/libtomcrypt/src/mac/hmac/hmac_init.c
Line | Count | Source (jump to first uncovered line) |
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 | | #include "tomcrypt.h" |
10 | | |
11 | | /** |
12 | | @file hmac_init.c |
13 | | HMAC support, initialize state, Tom St Denis/Dobes Vandermeer |
14 | | */ |
15 | | |
16 | | #ifdef LTC_HMAC |
17 | | |
18 | 610k | #define LTC_HMAC_BLOCKSIZE hash_descriptor[hash].blocksize |
19 | | |
20 | | /** |
21 | | Initialize an HMAC context. |
22 | | @param hmac The HMAC state |
23 | | @param hash The index of the hash you want to use |
24 | | @param key The secret key |
25 | | @param keylen The length of the secret key (octets) |
26 | | @return CRYPT_OK if successful |
27 | | */ |
28 | | int hmac_init(hmac_state *hmac, int hash, const unsigned char *key, unsigned long keylen) |
29 | 8.71k | { |
30 | 8.71k | unsigned char buf[MAXBLOCKSIZE]; |
31 | 8.71k | unsigned long hashsize; |
32 | 8.71k | unsigned long i, z; |
33 | 8.71k | int err; |
34 | | |
35 | 8.71k | LTC_ARGCHK(hmac != NULL); |
36 | 8.71k | LTC_ARGCHK(key != NULL); |
37 | | |
38 | | /* valid hash? */ |
39 | 8.71k | if ((err = hash_is_valid(hash)) != CRYPT_OK) { |
40 | 0 | return err; |
41 | 0 | } |
42 | 8.71k | hmac->hash = hash; |
43 | 8.71k | hashsize = hash_descriptor[hash].hashsize; |
44 | | |
45 | | /* valid key length? */ |
46 | 8.71k | if (keylen == 0) { |
47 | 0 | return CRYPT_INVALID_KEYSIZE; |
48 | 0 | } |
49 | | |
50 | | /* allocate memory for key */ |
51 | 8.71k | hmac->key = XMALLOC(LTC_HMAC_BLOCKSIZE); |
52 | 8.71k | if (hmac->key == NULL) { |
53 | 0 | return CRYPT_MEM; |
54 | 0 | } |
55 | | |
56 | | /* (1) make sure we have a large enough key */ |
57 | 8.71k | if(keylen > LTC_HMAC_BLOCKSIZE) { |
58 | 0 | z = LTC_HMAC_BLOCKSIZE; |
59 | 0 | if ((err = hash_memory(hash, key, keylen, hmac->key, &z)) != CRYPT_OK) { |
60 | 0 | goto LBL_ERR; |
61 | 0 | } |
62 | 0 | keylen = hashsize; |
63 | 8.71k | } else { |
64 | 8.71k | XMEMCPY(hmac->key, key, (size_t)keylen); |
65 | 8.71k | } |
66 | | |
67 | 8.71k | if(keylen < LTC_HMAC_BLOCKSIZE) { |
68 | 8.71k | zeromem((hmac->key) + keylen, (size_t)(LTC_HMAC_BLOCKSIZE - keylen)); |
69 | 8.71k | } |
70 | | |
71 | | /* Create the initialization vector for step (3) */ |
72 | 566k | for(i=0; i < LTC_HMAC_BLOCKSIZE; i++) { |
73 | 557k | buf[i] = hmac->key[i] ^ 0x36; |
74 | 557k | } |
75 | | |
76 | | /* Pre-pend that to the hash data */ |
77 | 8.71k | if ((err = hash_descriptor[hash].init(&hmac->md)) != CRYPT_OK) { |
78 | 0 | goto LBL_ERR; |
79 | 0 | } |
80 | | |
81 | 8.71k | if ((err = hash_descriptor[hash].process(&hmac->md, buf, LTC_HMAC_BLOCKSIZE)) != CRYPT_OK) { |
82 | 0 | goto LBL_ERR; |
83 | 0 | } |
84 | 8.71k | goto done; |
85 | 8.71k | LBL_ERR: |
86 | | /* free the key since we failed */ |
87 | 0 | XFREE(hmac->key); |
88 | 8.71k | done: |
89 | | #ifdef LTC_CLEAN_STACK |
90 | | zeromem(buf, LTC_HMAC_BLOCKSIZE); |
91 | | #endif |
92 | | |
93 | 8.71k | return err; |
94 | 0 | } |
95 | | |
96 | | #endif |
97 | | |
98 | | /* ref: $Format:%D$ */ |
99 | | /* git commit: $Format:%H$ */ |
100 | | /* commit time: $Format:%ai$ */ |