Path: blob/master/libs/tomcrypt/src/mac/hmac/hmac_done.c
5972 views
/* LibTomCrypt, modular cryptographic library -- Tom St Denis1*2* LibTomCrypt is a library that provides various cryptographic3* algorithms in a highly modular and flexible manner.4*5* The library is free for all purposes without any express6* guarantee it works.7*/8#include "tomcrypt.h"910/**11@file hmac_done.c12HMAC support, terminate stream, Tom St Denis/Dobes Vandermeer13*/1415#ifdef LTC_HMAC1617#define LTC_HMAC_BLOCKSIZE hash_descriptor[hash].blocksize1819/**20Terminate an HMAC session21@param hmac The HMAC state22@param out [out] The destination of the HMAC authentication tag23@param outlen [in/out] The max size and resulting size of the HMAC authentication tag24@return CRYPT_OK if successful25*/26int hmac_done(hmac_state *hmac, unsigned char *out, unsigned long *outlen)27{28unsigned char *buf, *isha;29unsigned long hashsize, i;30int hash, err;3132LTC_ARGCHK(hmac != NULL);33LTC_ARGCHK(out != NULL);3435/* test hash */36hash = hmac->hash;37if((err = hash_is_valid(hash)) != CRYPT_OK) {38return err;39}4041/* get the hash message digest size */42hashsize = hash_descriptor[hash].hashsize;4344/* allocate buffers */45buf = XMALLOC(LTC_HMAC_BLOCKSIZE);46isha = XMALLOC(hashsize);47if (buf == NULL || isha == NULL) {48if (buf != NULL) {49XFREE(buf);50}51if (isha != NULL) {52XFREE(isha);53}54return CRYPT_MEM;55}5657/* Get the hash of the first HMAC vector plus the data */58if ((err = hash_descriptor[hash].done(&hmac->md, isha)) != CRYPT_OK) {59goto LBL_ERR;60}6162/* Create the second HMAC vector vector for step (3) */63for(i=0; i < LTC_HMAC_BLOCKSIZE; i++) {64buf[i] = hmac->key[i] ^ 0x5C;65}6667/* Now calculate the "outer" hash for step (5), (6), and (7) */68if ((err = hash_descriptor[hash].init(&hmac->md)) != CRYPT_OK) {69goto LBL_ERR;70}71if ((err = hash_descriptor[hash].process(&hmac->md, buf, LTC_HMAC_BLOCKSIZE)) != CRYPT_OK) {72goto LBL_ERR;73}74if ((err = hash_descriptor[hash].process(&hmac->md, isha, hashsize)) != CRYPT_OK) {75goto LBL_ERR;76}77if ((err = hash_descriptor[hash].done(&hmac->md, buf)) != CRYPT_OK) {78goto LBL_ERR;79}8081/* copy to output */82for (i = 0; i < hashsize && i < *outlen; i++) {83out[i] = buf[i];84}85*outlen = i;8687err = CRYPT_OK;88LBL_ERR:89XFREE(hmac->key);90#ifdef LTC_CLEAN_STACK91zeromem(isha, hashsize);92zeromem(buf, hashsize);93zeromem(hmac, sizeof(*hmac));94#endif9596XFREE(isha);97XFREE(buf);9899return err;100}101102#endif103104105