comparison libtomcrypt/src/mac/hmac/hmac_init.c @ 1439:8d24733026c5 coverity

merge
author Matt Johnston <matt@ucc.asn.au>
date Sat, 24 Jun 2017 23:33:16 +0800
parents f849a5ca2efc
children 6dba84798cd5
comparison
equal deleted inserted replaced
1400:238a439670f5 1439:8d24733026c5
4 * algorithms in a highly modular and flexible manner. 4 * algorithms in a highly modular and flexible manner.
5 * 5 *
6 * The library is free for all purposes without any express 6 * The library is free for all purposes without any express
7 * guarantee it works. 7 * guarantee it works.
8 * 8 *
9 * Tom St Denis, [email protected], http://libtomcrypt.com 9 * Tom St Denis, [email protected], http://libtom.org
10 */ 10 */
11 #include "tomcrypt.h" 11 #include "tomcrypt.h"
12 12
13 /** 13 /**
14 @file hmac_init.c 14 @file hmac_init.c
15 HMAC support, initialize state, Tom St Denis/Dobes Vandermeer 15 LTC_HMAC support, initialize state, Tom St Denis/Dobes Vandermeer
16 */ 16 */
17 17
18 #ifdef LTC_HMAC 18 #ifdef LTC_HMAC
19 19
20 #define HMAC_BLOCKSIZE hash_descriptor[hash].blocksize 20 #define LTC_HMAC_BLOCKSIZE hash_descriptor[hash].blocksize
21 21
22 /** 22 /**
23 Initialize an HMAC context. 23 Initialize an LTC_HMAC context.
24 @param hmac The HMAC state 24 @param hmac The LTC_HMAC state
25 @param hash The index of the hash you want to use 25 @param hash The index of the hash you want to use
26 @param key The secret key 26 @param key The secret key
27 @param keylen The length of the secret key (octets) 27 @param keylen The length of the secret key (octets)
28 @return CRYPT_OK if successful 28 @return CRYPT_OK if successful
29 */ 29 */
48 if (keylen == 0) { 48 if (keylen == 0) {
49 return CRYPT_INVALID_KEYSIZE; 49 return CRYPT_INVALID_KEYSIZE;
50 } 50 }
51 51
52 /* allocate memory for key */ 52 /* allocate memory for key */
53 hmac->key = XMALLOC(HMAC_BLOCKSIZE); 53 hmac->key = XMALLOC(LTC_HMAC_BLOCKSIZE);
54 if (hmac->key == NULL) { 54 if (hmac->key == NULL) {
55 return CRYPT_MEM; 55 return CRYPT_MEM;
56 } 56 }
57 57
58 /* (1) make sure we have a large enough key */ 58 /* (1) make sure we have a large enough key */
59 if(keylen > HMAC_BLOCKSIZE) { 59 if(keylen > LTC_HMAC_BLOCKSIZE) {
60 z = HMAC_BLOCKSIZE; 60 z = LTC_HMAC_BLOCKSIZE;
61 if ((err = hash_memory(hash, key, keylen, hmac->key, &z)) != CRYPT_OK) { 61 if ((err = hash_memory(hash, key, keylen, hmac->key, &z)) != CRYPT_OK) {
62 goto LBL_ERR; 62 goto LBL_ERR;
63 } 63 }
64 if(hashsize < HMAC_BLOCKSIZE) { 64 if(hashsize < LTC_HMAC_BLOCKSIZE) {
65 zeromem((hmac->key) + hashsize, (size_t)(HMAC_BLOCKSIZE - hashsize)); 65 zeromem((hmac->key) + hashsize, (size_t)(LTC_HMAC_BLOCKSIZE - hashsize));
66 } 66 }
67 keylen = hashsize; 67 keylen = hashsize;
68 } else { 68 } else {
69 XMEMCPY(hmac->key, key, (size_t)keylen); 69 XMEMCPY(hmac->key, key, (size_t)keylen);
70 if(keylen < HMAC_BLOCKSIZE) { 70 if(keylen < LTC_HMAC_BLOCKSIZE) {
71 zeromem((hmac->key) + keylen, (size_t)(HMAC_BLOCKSIZE - keylen)); 71 zeromem((hmac->key) + keylen, (size_t)(LTC_HMAC_BLOCKSIZE - keylen));
72 } 72 }
73 } 73 }
74 74
75 /* Create the initial vector for step (3) */ 75 /* Create the initial vector for step (3) */
76 for(i=0; i < HMAC_BLOCKSIZE; i++) { 76 for(i=0; i < LTC_HMAC_BLOCKSIZE; i++) {
77 buf[i] = hmac->key[i] ^ 0x36; 77 buf[i] = hmac->key[i] ^ 0x36;
78 } 78 }
79 79
80 /* Pre-pend that to the hash data */ 80 /* Pre-pend that to the hash data */
81 if ((err = hash_descriptor[hash].init(&hmac->md)) != CRYPT_OK) { 81 if ((err = hash_descriptor[hash].init(&hmac->md)) != CRYPT_OK) {
82 goto LBL_ERR; 82 goto LBL_ERR;
83 } 83 }
84 84
85 if ((err = hash_descriptor[hash].process(&hmac->md, buf, HMAC_BLOCKSIZE)) != CRYPT_OK) { 85 if ((err = hash_descriptor[hash].process(&hmac->md, buf, LTC_HMAC_BLOCKSIZE)) != CRYPT_OK) {
86 goto LBL_ERR; 86 goto LBL_ERR;
87 } 87 }
88 goto done; 88 goto done;
89 LBL_ERR: 89 LBL_ERR:
90 /* free the key since we failed */ 90 /* free the key since we failed */
91 XFREE(hmac->key); 91 XFREE(hmac->key);
92 done: 92 done:
93 #ifdef LTC_CLEAN_STACK 93 #ifdef LTC_CLEAN_STACK
94 zeromem(buf, HMAC_BLOCKSIZE); 94 zeromem(buf, LTC_HMAC_BLOCKSIZE);
95 #endif 95 #endif
96 96
97 return err; 97 return err;
98 } 98 }
99 99
100 #endif 100 #endif
101 101
102 /* $Source: /cvs/libtom/libtomcrypt/src/mac/hmac/hmac_init.c,v $ */ 102 /* $Source$ */
103 /* $Revision: 1.5 $ */ 103 /* $Revision$ */
104 /* $Date: 2006/11/03 00:39:49 $ */ 104 /* $Date$ */