Path: blob/master/libs/tomcrypt/src/mac/hmac/hmac_memory.c
8686 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_memory.c12HMAC support, process a block of memory, Tom St Denis/Dobes Vandermeer13*/1415#ifdef LTC_HMAC1617/**18HMAC a block of memory to produce the authentication tag19@param hash The index of the hash to use20@param key The secret key21@param keylen The length of the secret key (octets)22@param in The data to HMAC23@param inlen The length of the data to HMAC (octets)24@param out [out] Destination of the authentication tag25@param outlen [in/out] Max size and resulting size of authentication tag26@return CRYPT_OK if successful27*/28int hmac_memory(int hash,29const unsigned char *key, unsigned long keylen,30const unsigned char *in, unsigned long inlen,31unsigned char *out, unsigned long *outlen)32{33hmac_state *hmac;34int err;3536LTC_ARGCHK(key != NULL);37LTC_ARGCHK(in != NULL);38LTC_ARGCHK(out != NULL);39LTC_ARGCHK(outlen != NULL);4041/* make sure hash descriptor is valid */42if ((err = hash_is_valid(hash)) != CRYPT_OK) {43return err;44}4546/* is there a descriptor? */47if (hash_descriptor[hash].hmac_block != NULL) {48return hash_descriptor[hash].hmac_block(key, keylen, in, inlen, out, outlen);49}5051/* nope, so call the hmac functions */52/* allocate ram for hmac state */53hmac = XMALLOC(sizeof(hmac_state));54if (hmac == NULL) {55return CRYPT_MEM;56}5758if ((err = hmac_init(hmac, hash, key, keylen)) != CRYPT_OK) {59goto LBL_ERR;60}6162if ((err = hmac_process(hmac, in, inlen)) != CRYPT_OK) {63goto LBL_ERR;64}6566if ((err = hmac_done(hmac, out, outlen)) != CRYPT_OK) {67goto LBL_ERR;68}6970err = CRYPT_OK;71LBL_ERR:72#ifdef LTC_CLEAN_STACK73zeromem(hmac, sizeof(hmac_state));74#endif7576XFREE(hmac);77return err;78}7980#endif818283