1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
13 HMAC support, process a file, Tom St Denis/Dobes Vandermeer
20 @param hash The index of the hash you wish to use
21 @param fname The name of the file you wish to HMAC
22 @param key The secret key
23 @param keylen The length of the secret key
24 @param out [out] The HMAC authentication tag
25 @param outlen [in/out] The max size and resulting size of the authentication tag
26 @return CRYPT_OK if successful, CRYPT_NOP if file support has been disabled
28 int hmac_file(int hash, const char *fname,
29 const unsigned char *key, unsigned long keylen,
30 unsigned char *out, unsigned long *outlen)
33 LTC_UNUSED_PARAM(hash);
34 LTC_UNUSED_PARAM(fname);
35 LTC_UNUSED_PARAM(key);
36 LTC_UNUSED_PARAM(keylen);
37 LTC_UNUSED_PARAM(out);
38 LTC_UNUSED_PARAM(outlen);
47 LTC_ARGCHK(fname != NULL);
48 LTC_ARGCHK(key != NULL);
49 LTC_ARGCHK(out != NULL);
50 LTC_ARGCHK(outlen != NULL);
52 if ((buf = XMALLOC(LTC_FILE_READ_BUFSIZE)) == NULL) {
56 if ((err = hash_is_valid(hash)) != CRYPT_OK) {
60 if ((err = hmac_init(&hmac, hash, key, keylen)) != CRYPT_OK) {
64 in = fopen(fname, "rb");
66 err = CRYPT_FILE_NOTFOUND;
71 x = fread(buf, 1, LTC_FILE_READ_BUFSIZE, in);
72 if ((err = hmac_process(&hmac, buf, (unsigned long)x)) != CRYPT_OK) {
73 fclose(in); /* we don't trap this error since we're already returning an error! */
76 } while (x == LTC_FILE_READ_BUFSIZE);
78 if (fclose(in) != 0) {
83 err = hmac_done(&hmac, out, outlen);
86 zeromem(buf, LTC_FILE_READ_BUFSIZE);
88 #ifdef LTC_CLEAN_STACK
89 zeromem(&hmac, sizeof(hmac_state));
98 /* ref: $Format:%D$ */
99 /* git commit: $Format:%H$ */
100 /* commit time: $Format:%ai$ */