Files
tomcrypt/src/mac/hmac/hmac_init.c
T

113 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.
*
2005-04-17 11:37:13 +00:00
* Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.org
2004-05-12 20:42:16 +00:00
*/
2004-12-30 23:55:53 +00:00
#include "tomcrypt.h"
2004-05-12 20:42:16 +00:00
2004-12-30 23:55:53 +00:00
/**
@file hmac_init.c
HMAC support, initialize state, 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
/**
Initialize an HMAC context.
@param hmac The HMAC state
@param hash The index of the hash you want to use
@param key The secret key
@param keylen The length of the secret key (octets)
@return CRYPT_OK if successful
*/
2004-05-12 20:42:16 +00:00
int hmac_init(hmac_state *hmac, int hash, const unsigned char *key, unsigned long keylen)
{
2004-06-20 02:41:49 +00:00
unsigned char *buf;
2004-05-12 20:42:16 +00:00
unsigned long hashsize;
unsigned long i, z;
int err;
2004-12-30 23:55:53 +00:00
LTC_ARGCHK(hmac != NULL);
LTC_ARGCHK(key != NULL);
2004-05-12 20:42:16 +00:00
2004-06-20 02:41:49 +00:00
/* valid hash? */
2004-05-12 20:42:16 +00:00
if ((err = hash_is_valid(hash)) != CRYPT_OK) {
return err;
}
2004-06-20 02:41:49 +00:00
hmac->hash = hash;
hashsize = hash_descriptor[hash].hashsize;
2004-05-12 20:42:16 +00:00
/* valid key length? */
if (keylen == 0) {
return CRYPT_INVALID_KEYSIZE;
}
2004-06-20 02:41:49 +00:00
/* allocate ram for buf */
buf = XMALLOC(HMAC_BLOCKSIZE);
if (buf == NULL) {
return CRYPT_MEM;
}
2004-05-12 20:42:16 +00:00
2004-07-23 15:40:22 +00:00
/* allocate memory for key */
hmac->key = XMALLOC(HMAC_BLOCKSIZE);
if (hmac->key == NULL) {
XFREE(buf);
return CRYPT_MEM;
}
2004-08-06 16:42:41 +00:00
/* (1) make sure we have a large enough key */
2004-05-12 20:42:16 +00:00
if(keylen > HMAC_BLOCKSIZE) {
2004-08-06 16:42:41 +00:00
z = HMAC_BLOCKSIZE;
2004-05-12 20:42:16 +00:00
if ((err = hash_memory(hash, key, keylen, hmac->key, &z)) != CRYPT_OK) {
2004-12-30 23:55:53 +00:00
goto LBL_ERR;
2004-05-12 20:42:16 +00:00
}
if(hashsize < HMAC_BLOCKSIZE) {
zeromem((hmac->key) + hashsize, (size_t)(HMAC_BLOCKSIZE - hashsize));
}
keylen = hashsize;
} else {
2004-06-20 02:41:49 +00:00
XMEMCPY(hmac->key, key, (size_t)keylen);
2004-05-12 20:42:16 +00:00
if(keylen < HMAC_BLOCKSIZE) {
zeromem((hmac->key) + keylen, (size_t)(HMAC_BLOCKSIZE - keylen));
}
}
2004-08-06 16:42:41 +00:00
/* Create the initial vector for step (3) */
2004-05-12 20:42:16 +00:00
for(i=0; i < HMAC_BLOCKSIZE; i++) {
buf[i] = hmac->key[i] ^ 0x36;
}
2004-08-06 16:42:41 +00:00
/* Pre-pend that to the hash data */
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-08-06 16:42:41 +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-08-06 16:42:41 +00:00
}
goto done;
2004-12-30 23:55:53 +00:00
LBL_ERR:
2004-08-06 16:42:41 +00:00
/* free the key since we failed */
XFREE(hmac->key);
done:
2004-12-30 23:55:53 +00:00
#ifdef LTC_CLEAN_STACK
2004-06-20 02:41:49 +00:00
zeromem(buf, HMAC_BLOCKSIZE);
#endif
XFREE(buf);
return err;
2004-05-12 20:42:16 +00:00
}
#endif
2005-06-09 00:08:13 +00:00
/* $Source$ */
/* $Revision$ */
/* $Date$ */