Home | History | Annotate | Download | only in hmac
      1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
      2  *
      3  * LibTomCrypt is a library that provides various cryptographic
      4  * algorithms in a highly modular and flexible manner.
      5  *
      6  * The library is free for all purposes without any express
      7  * guarantee it works.
      8  *
      9  * Tom St Denis, tomstdenis (at) gmail.com, http://libtomcrypt.com
     10  */
     11 #include "tomcrypt.h"
     12 
     13 /**
     14   @file hmac_memory.c
     15   HMAC support, process a block of memory, Tom St Denis/Dobes Vandermeer
     16 */
     17 
     18 #ifdef LTC_HMAC
     19 
     20 /**
     21    HMAC a block of memory to produce the authentication tag
     22    @param hash      The index of the hash to use
     23    @param key       The secret key
     24    @param keylen    The length of the secret key (octets)
     25    @param in        The data to HMAC
     26    @param inlen     The length of the data to HMAC (octets)
     27    @param out       [out] Destination of the authentication tag
     28    @param outlen    [in/out] Max size and resulting size of authentication tag
     29    @return CRYPT_OK if successful
     30 */
     31 int hmac_memory(int hash,
     32                 const unsigned char *key,  unsigned long keylen,
     33                 const unsigned char *in,   unsigned long inlen,
     34                       unsigned char *out,  unsigned long *outlen)
     35 {
     36     hmac_state *hmac;
     37     int         err;
     38 
     39     LTC_ARGCHK(key    != NULL);
     40     LTC_ARGCHK(in     != NULL);
     41     LTC_ARGCHK(out    != NULL);
     42     LTC_ARGCHK(outlen != NULL);
     43 
     44     /* make sure hash descriptor is valid */
     45     if ((err = hash_is_valid(hash)) != CRYPT_OK) {
     46        return err;
     47     }
     48 
     49     /* is there a descriptor? */
     50     if (hash_descriptor[hash].hmac_block != NULL) {
     51         return hash_descriptor[hash].hmac_block(key, keylen, in, inlen, out, outlen);
     52     }
     53 
     54     /* nope, so call the hmac functions */
     55     /* allocate ram for hmac state */
     56     hmac = XMALLOC(sizeof(hmac_state));
     57     if (hmac == NULL) {
     58        return CRYPT_MEM;
     59     }
     60 
     61     if ((err = hmac_init(hmac, hash, key, keylen)) != CRYPT_OK) {
     62        goto LBL_ERR;
     63     }
     64 
     65     if ((err = hmac_process(hmac, in, inlen)) != CRYPT_OK) {
     66        goto LBL_ERR;
     67     }
     68 
     69     if ((err = hmac_done(hmac, out, outlen)) != CRYPT_OK) {
     70        goto LBL_ERR;
     71     }
     72 
     73    err = CRYPT_OK;
     74 LBL_ERR:
     75 #ifdef LTC_CLEAN_STACK
     76    zeromem(hmac, sizeof(hmac_state));
     77 #endif
     78 
     79    XFREE(hmac);
     80    return err;
     81 }
     82 
     83 #endif
     84 
     85 
     86 /* $Source: /cvs/libtom/libtomcrypt/src/mac/hmac/hmac_memory.c,v $ */
     87 /* $Revision: 1.6 $ */
     88 /* $Date: 2006/11/03 00:39:49 $ */
     89