# HG changeset patch # User Matt Johnston # Date 1425192364 -28800 # Node ID a2bfd4374878e4aa3720ec5bd0ce8a6c3818c57b # Parent 4d7b4c5526c51eed411ec68fe40a73fabd6b2c6e Avoid malloc in hmac diff -r 4d7b4c5526c5 -r a2bfd4374878 libtomcrypt/src/mac/hmac/hmac_done.c --- a/libtomcrypt/src/mac/hmac/hmac_done.c Sun Mar 01 00:57:21 2015 +0800 +++ b/libtomcrypt/src/mac/hmac/hmac_done.c Sun Mar 01 14:46:04 2015 +0800 @@ -28,7 +28,7 @@ */ int hmac_done(hmac_state *hmac, unsigned char *out, unsigned long *outlen) { - unsigned char *buf, *isha; + unsigned char buf[MAXBLOCKSIZE], isha[MAXBLOCKSIZE]; unsigned long hashsize, i; int hash, err; @@ -44,19 +44,6 @@ /* get the hash message digest size */ hashsize = hash_descriptor[hash].hashsize; - /* allocate buffers */ - buf = XMALLOC(HMAC_BLOCKSIZE); - isha = XMALLOC(hashsize); - if (buf == NULL || isha == NULL) { - if (buf != NULL) { - XFREE(buf); - } - if (isha != NULL) { - XFREE(isha); - } - return CRYPT_MEM; - } - /* Get the hash of the first HMAC vector plus the data */ if ((err = hash_descriptor[hash].done(&hmac->md, isha)) != CRYPT_OK) { goto LBL_ERR; @@ -96,9 +83,6 @@ zeromem(hmac, sizeof(*hmac)); #endif - XFREE(isha); - XFREE(buf); - return err; } diff -r 4d7b4c5526c5 -r a2bfd4374878 libtomcrypt/src/mac/hmac/hmac_init.c --- a/libtomcrypt/src/mac/hmac/hmac_init.c Sun Mar 01 00:57:21 2015 +0800 +++ b/libtomcrypt/src/mac/hmac/hmac_init.c Sun Mar 01 14:46:04 2015 +0800 @@ -29,7 +29,7 @@ */ int hmac_init(hmac_state *hmac, int hash, const unsigned char *key, unsigned long keylen) { - unsigned char *buf; + unsigned char buf[MAXBLOCKSIZE]; unsigned long hashsize; unsigned long i, z; int err; @@ -49,16 +49,9 @@ return CRYPT_INVALID_KEYSIZE; } - /* allocate ram for buf */ - buf = XMALLOC(HMAC_BLOCKSIZE); - if (buf == NULL) { - return CRYPT_MEM; - } - /* allocate memory for key */ hmac->key = XMALLOC(HMAC_BLOCKSIZE); if (hmac->key == NULL) { - XFREE(buf); return CRYPT_MEM; } @@ -101,7 +94,6 @@ zeromem(buf, HMAC_BLOCKSIZE); #endif - XFREE(buf); return err; }