tomcrypt/src/mac/hmac/hmac_done.c

110 lines
2.7 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.
*
2006-04-06 19:48:32 +00:00
* Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.com
2004-05-12 20:42:16 +00:00
*/
2004-12-30 23:55:53 +00:00
#include "tomcrypt.h"
/**
@file hmac_done.c
HMAC support, terminate stream, Tom St Denis/Dobes Vandermeer
2004-05-12 20:42:16 +00:00
*/
#ifdef HMAC
#define HMAC_BLOCKSIZE hash_descriptor[hash].blocksize
2004-12-30 23:55:53 +00: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
@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 */
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;
}
2004-08-06 16:42:41 +00: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
}
2004-08-06 16:42:41 +00:00
/* Create the second HMAC vector vector for step (3) */
2004-05-12 20:42:16 +00:00
for(i=0; i < HMAC_BLOCKSIZE; i++) {
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
}
2004-06-20 02:41:49 +00:00
if ((err = hash_descriptor[hash].process(&hmac->md, buf, 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$ */