Mercurial > dropbear
comparison libtomcrypt/src/mac/hmac/hmac_done.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 |
---|---|
26 @param outlen [in/out] The max size and resulting size of the HMAC authentication tag | 26 @param outlen [in/out] The max size and resulting size of the HMAC authentication tag |
27 @return CRYPT_OK if successful | 27 @return CRYPT_OK if successful |
28 */ | 28 */ |
29 int hmac_done(hmac_state *hmac, unsigned char *out, unsigned long *outlen) | 29 int hmac_done(hmac_state *hmac, unsigned char *out, unsigned long *outlen) |
30 { | 30 { |
31 unsigned char *buf, *isha; | 31 unsigned char buf[MAXBLOCKSIZE], isha[MAXBLOCKSIZE]; |
32 unsigned long hashsize, i; | 32 unsigned long hashsize, i; |
33 int hash, err; | 33 int hash, err; |
34 | 34 |
35 LTC_ARGCHK(hmac != NULL); | 35 LTC_ARGCHK(hmac != NULL); |
36 LTC_ARGCHK(out != NULL); | 36 LTC_ARGCHK(out != NULL); |
41 return err; | 41 return err; |
42 } | 42 } |
43 | 43 |
44 /* get the hash message digest size */ | 44 /* get the hash message digest size */ |
45 hashsize = hash_descriptor[hash].hashsize; | 45 hashsize = hash_descriptor[hash].hashsize; |
46 | |
47 /* allocate buffers */ | |
48 buf = XMALLOC(HMAC_BLOCKSIZE); | |
49 isha = XMALLOC(hashsize); | |
50 if (buf == NULL || isha == NULL) { | |
51 if (buf != NULL) { | |
52 XFREE(buf); | |
53 } | |
54 if (isha != NULL) { | |
55 XFREE(isha); | |
56 } | |
57 return CRYPT_MEM; | |
58 } | |
59 | 46 |
60 /* Get the hash of the first HMAC vector plus the data */ | 47 /* Get the hash of the first HMAC vector plus the data */ |
61 if ((err = hash_descriptor[hash].done(&hmac->md, isha)) != CRYPT_OK) { | 48 if ((err = hash_descriptor[hash].done(&hmac->md, isha)) != CRYPT_OK) { |
62 goto LBL_ERR; | 49 goto LBL_ERR; |
63 } | 50 } |
94 zeromem(isha, hashsize); | 81 zeromem(isha, hashsize); |
95 zeromem(buf, hashsize); | 82 zeromem(buf, hashsize); |
96 zeromem(hmac, sizeof(*hmac)); | 83 zeromem(hmac, sizeof(*hmac)); |
97 #endif | 84 #endif |
98 | 85 |
99 XFREE(isha); | |
100 XFREE(buf); | |
101 | |
102 return err; | 86 return err; |
103 } | 87 } |
104 | 88 |
105 #endif | 89 #endif |
106 | 90 |