1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
13 HMAC support, terminate stream, Tom St Denis/Dobes Vandermeer
18 #define LTC_HMAC_BLOCKSIZE hash_descriptor[hash].blocksize
21 Terminate an HMAC session
22 @param hmac The HMAC state
23 @param out [out] The destination of the HMAC authentication tag
24 @param outlen [in/out] The max size and resulting size of the HMAC authentication tag
25 @return CRYPT_OK if successful
27 int hmac_done(hmac_state *hmac, unsigned char *out, unsigned long *outlen)
29 unsigned char *buf, *isha;
30 unsigned long hashsize, i;
33 LTC_ARGCHK(hmac != NULL);
34 LTC_ARGCHK(out != NULL);
38 if((err = hash_is_valid(hash)) != CRYPT_OK) {
42 /* get the hash message digest size */
43 hashsize = hash_descriptor[hash].hashsize;
45 /* allocate buffers */
46 buf = XMALLOC(LTC_HMAC_BLOCKSIZE);
47 isha = XMALLOC(hashsize);
48 if (buf == NULL || isha == NULL) {
58 /* Get the hash of the first HMAC vector plus the data */
59 if ((err = hash_descriptor[hash].done(&hmac->md, isha)) != CRYPT_OK) {
63 /* Create the second HMAC vector vector for step (3) */
64 for(i=0; i < LTC_HMAC_BLOCKSIZE; i++) {
65 buf[i] = hmac->key[i] ^ 0x5C;
68 /* Now calculate the "outer" hash for step (5), (6), and (7) */
69 if ((err = hash_descriptor[hash].init(&hmac->md)) != CRYPT_OK) {
72 if ((err = hash_descriptor[hash].process(&hmac->md, buf, LTC_HMAC_BLOCKSIZE)) != CRYPT_OK) {
75 if ((err = hash_descriptor[hash].process(&hmac->md, isha, hashsize)) != CRYPT_OK) {
78 if ((err = hash_descriptor[hash].done(&hmac->md, buf)) != CRYPT_OK) {
83 for (i = 0; i < hashsize && i < *outlen; i++) {
91 #ifdef LTC_CLEAN_STACK
92 zeromem(isha, hashsize);
93 zeromem(buf, hashsize);
94 zeromem(hmac, sizeof(*hmac));
105 /* ref: $Format:%D$ */
106 /* git commit: $Format:%H$ */
107 /* commit time: $Format:%ai$ */