comparison libtomcrypt/src/mac/hmac/hmac_init.c @ 1056:a2bfd4374878 nocircbuffer

Avoid malloc in hmac
author Matt Johnston <matt@ucc.asn.au>
date Sun, 01 Mar 2015 14:46:04 +0800
parents 0cbe8f6dbf9e
children f849a5ca2efc
comparison
equal deleted inserted replaced
1055:4d7b4c5526c5 1056:a2bfd4374878
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 */
30 int hmac_init(hmac_state *hmac, int hash, const unsigned char *key, unsigned long keylen) 30 int hmac_init(hmac_state *hmac, int hash, const unsigned char *key, unsigned long keylen)
31 { 31 {
32 unsigned char *buf; 32 unsigned char buf[MAXBLOCKSIZE];
33 unsigned long hashsize; 33 unsigned long hashsize;
34 unsigned long i, z; 34 unsigned long i, z;
35 int err; 35 int err;
36 36
37 LTC_ARGCHK(hmac != NULL); 37 LTC_ARGCHK(hmac != NULL);
47 /* valid key length? */ 47 /* valid key length? */
48 if (keylen == 0) { 48 if (keylen == 0) {
49 return CRYPT_INVALID_KEYSIZE; 49 return CRYPT_INVALID_KEYSIZE;
50 } 50 }
51 51
52 /* allocate ram for buf */
53 buf = XMALLOC(HMAC_BLOCKSIZE);
54 if (buf == NULL) {
55 return CRYPT_MEM;
56 }
57
58 /* allocate memory for key */ 52 /* allocate memory for key */
59 hmac->key = XMALLOC(HMAC_BLOCKSIZE); 53 hmac->key = XMALLOC(HMAC_BLOCKSIZE);
60 if (hmac->key == NULL) { 54 if (hmac->key == NULL) {
61 XFREE(buf);
62 return CRYPT_MEM; 55 return CRYPT_MEM;
63 } 56 }
64 57
65 /* (1) make sure we have a large enough key */ 58 /* (1) make sure we have a large enough key */
66 if(keylen > HMAC_BLOCKSIZE) { 59 if(keylen > HMAC_BLOCKSIZE) {
99 done: 92 done:
100 #ifdef LTC_CLEAN_STACK 93 #ifdef LTC_CLEAN_STACK
101 zeromem(buf, HMAC_BLOCKSIZE); 94 zeromem(buf, HMAC_BLOCKSIZE);
102 #endif 95 #endif
103 96
104 XFREE(buf);
105 return err; 97 return err;
106 } 98 }
107 99
108 #endif 100 #endif
109 101