tomcrypt/src/mac/hmac/hmac_done.c

108 lines
2.6 KiB
C
Raw Normal View History

2004-05-12 20:42:16 +00:00
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
*
* LibTomCrypt is a library that provides various cryptographic
* algorithms in a highly modular and flexible manner.
*
* The library is free for all purposes without any express
* guarantee it works.
*/
2004-12-30 23:55:53 +00:00
#include "tomcrypt.h"
/**
@file hmac_done.c
2014-01-03 15:16:59 +01:00
HMAC support, terminate stream, Tom St Denis/Dobes Vandermeer
2004-05-12 20:42:16 +00:00
*/
2006-11-17 14:21:24 +00:00
#ifdef LTC_HMAC
2004-05-12 20:42:16 +00:00
2007-07-20 17:48:02 +00:00
#define LTC_HMAC_BLOCKSIZE hash_descriptor[hash].blocksize
2004-05-12 20:42:16 +00:00
2004-12-30 23:55:53 +00:00
/**
2014-01-03 15:16:59 +01:00
Terminate an HMAC session
@param hmac The HMAC state
@param out [out] The destination of the HMAC authentication tag
@param outlen [in/out] The max size and resulting size of the HMAC authentication tag
2004-12-30 23:55:53 +00:00
@return CRYPT_OK if successful
*/
int hmac_done(hmac_state *hmac, unsigned char *out, unsigned long *outlen)
2004-05-12 20:42:16 +00:00
{
2004-06-20 02:41:49 +00:00
unsigned char *buf, *isha;
2004-05-12 20:42:16 +00:00
unsigned long hashsize, i;
int hash, err;
2004-12-30 23:55:53 +00:00
LTC_ARGCHK(hmac != NULL);
LTC_ARGCHK(out != NULL);
2004-05-12 20:42:16 +00:00
2004-06-20 02:41:49 +00:00
/* test hash */
2004-05-12 20:42:16 +00:00
hash = hmac->hash;
if((err = hash_is_valid(hash)) != CRYPT_OK) {
return err;
}
/* get the hash message digest size */
hashsize = hash_descriptor[hash].hashsize;
2004-06-20 02:41:49 +00:00
/* allocate buffers */
2007-07-20 17:48:02 +00:00
buf = XMALLOC(LTC_HMAC_BLOCKSIZE);
2004-06-20 02:41:49 +00:00
isha = XMALLOC(hashsize);
2014-01-03 15:16:59 +01:00
if (buf == NULL || isha == NULL) {
2004-06-20 02:41:49 +00:00
if (buf != NULL) {
XFREE(buf);
2014-01-03 15:16:59 +01:00
}
2004-06-20 02:41:49 +00:00
if (isha != NULL) {
XFREE(isha);
2014-01-03 15:16:59 +01:00
}
2004-06-20 02:41:49 +00:00
return CRYPT_MEM;
}
2014-01-03 15:16:59 +01:00
/* Get the hash of the first HMAC vector plus the data */
2004-05-12 20:42:16 +00:00
if ((err = hash_descriptor[hash].done(&hmac->md, isha)) != CRYPT_OK) {
2004-12-30 23:55:53 +00:00
goto LBL_ERR;
2004-05-12 20:42:16 +00:00
}
2014-01-03 15:16:59 +01:00
/* Create the second HMAC vector vector for step (3) */
2007-07-20 17:48:02 +00:00
for(i=0; i < LTC_HMAC_BLOCKSIZE; i++) {
2004-05-12 20:42:16 +00:00
buf[i] = hmac->key[i] ^ 0x5C;
}
2004-08-06 16:42:41 +00:00
/* Now calculate the "outer" hash for step (5), (6), and (7) */
2004-10-30 03:00:26 +00:00
if ((err = hash_descriptor[hash].init(&hmac->md)) != CRYPT_OK) {
2004-12-30 23:55:53 +00:00
goto LBL_ERR;
2004-10-30 03:00:26 +00:00
}
2007-07-20 17:48:02 +00:00
if ((err = hash_descriptor[hash].process(&hmac->md, buf, LTC_HMAC_BLOCKSIZE)) != CRYPT_OK) {
2004-12-30 23:55:53 +00:00
goto LBL_ERR;
2004-06-20 02:41:49 +00:00
}
if ((err = hash_descriptor[hash].process(&hmac->md, isha, hashsize)) != CRYPT_OK) {
2004-12-30 23:55:53 +00:00
goto LBL_ERR;
2004-06-20 02:41:49 +00:00
}
if ((err = hash_descriptor[hash].done(&hmac->md, buf)) != CRYPT_OK) {
2004-12-30 23:55:53 +00:00
goto LBL_ERR;
2004-06-20 02:41:49 +00:00
}
2004-05-12 20:42:16 +00:00
2004-08-06 16:42:41 +00:00
/* copy to output */
2004-05-12 20:42:16 +00:00
for (i = 0; i < hashsize && i < *outlen; i++) {
2004-12-30 23:55:53 +00:00
out[i] = buf[i];
2004-05-12 20:42:16 +00:00
}
*outlen = i;
2004-06-20 02:41:49 +00:00
err = CRYPT_OK;
2004-12-30 23:55:53 +00:00
LBL_ERR:
2004-07-23 15:40:22 +00:00
XFREE(hmac->key);
2004-12-30 23:55:53 +00:00
#ifdef LTC_CLEAN_STACK
2004-06-20 02:41:49 +00:00
zeromem(isha, hashsize);
zeromem(buf, hashsize);
2004-05-12 20:42:16 +00:00
zeromem(hmac, sizeof(*hmac));
#endif
2004-06-20 02:41:49 +00:00
XFREE(isha);
XFREE(buf);
return err;
2004-05-12 20:42:16 +00:00
}
#endif
2005-06-09 00:08:13 +00:00
/* $Source$ */
/* $Revision$ */
/* $Date$ */