Logo Search packages:      
Sourcecode: libtomcrypt version File versions  Download package

int hmac_file ( int  hash,
const char *  fname,
const unsigned char *  key,
unsigned long  keylen,
unsigned char *  out,
unsigned long *  outlen 

LTC_HMAC a file

hashThe index of the hash you wish to use
fnameThe name of the file you wish to LTC_HMAC
keyThe secret key
keylenThe length of the secret key
out[out] The LTC_HMAC authentication tag
outlen[in/out] The max size and resulting size of the authentication tag
CRYPT_OK if successful, CRYPT_NOP if file support has been disabled

Definition at line 30 of file hmac_file.c.

References hmac_done(), hmac_init(), hmac_process(), and zeromem().

#ifdef LTC_NO_FILE
    return CRYPT_NOP;
   hmac_state hmac;
   FILE *in;
   unsigned char buf[512];
   size_t x;
   int err;

   LTC_ARGCHK(fname  != NULL);
   LTC_ARGCHK(key    != NULL);
   LTC_ARGCHK(out    != NULL);
   LTC_ARGCHK(outlen != NULL);
   if((err = hash_is_valid(hash)) != CRYPT_OK) {
       return err;

   if ((err = hmac_init(&hmac, hash, key, keylen)) != CRYPT_OK) {
       return err;

   in = fopen(fname, "rb");
   if (in == NULL) {

   /* process the file contents */
   do {
      x = fread(buf, 1, sizeof(buf), in);
      if ((err = hmac_process(&hmac, buf, (unsigned long)x)) != CRYPT_OK) {
         /* we don't trap this error since we're already returning an error! */
         return err;
   } while (x == sizeof(buf));

   if (fclose(in) != 0) {
      return CRYPT_ERROR;

   /* get final hmac */
   if ((err = hmac_done(&hmac, out, outlen)) != CRYPT_OK) {
      return err;

   /* clear memory */
   zeromem(buf, sizeof(buf));
   return CRYPT_OK;

Here is the call graph for this function:

Generated by  Doxygen 1.6.0   Back to index