1 // SPDX-License-Identifier: BSD-2-Clause
2 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 *
4 * LibTomCrypt is a library that provides various cryptographic
5 * algorithms in a highly modular and flexible manner.
6 *
7 * The library is free for all purposes without any express
8 * guarantee it works.
9 */
10 #include "tomcrypt_private.h"
11
12 /**
13 @file hmac_file.c
14 HMAC support, process a file, Tom St Denis/Dobes Vandermeer
15 */
16
17 #ifdef LTC_HMAC
18
19 /**
20 HMAC a file
21 @param hash The index of the hash you wish to use
22 @param fname The name of the file you wish to HMAC
23 @param key The secret key
24 @param keylen The length of the secret key
25 @param out [out] The HMAC authentication tag
26 @param outlen [in/out] The max size and resulting size of the authentication tag
27 @return CRYPT_OK if successful, CRYPT_NOP if file support has been disabled
28 */
hmac_file(int hash,const char * fname,const unsigned char * key,unsigned long keylen,unsigned char * out,unsigned long * outlen)29 int hmac_file(int hash, const char *fname,
30 const unsigned char *key, unsigned long keylen,
31 unsigned char *out, unsigned long *outlen)
32 {
33 #ifdef LTC_NO_FILE
34 LTC_UNUSED_PARAM(hash);
35 LTC_UNUSED_PARAM(fname);
36 LTC_UNUSED_PARAM(key);
37 LTC_UNUSED_PARAM(keylen);
38 LTC_UNUSED_PARAM(out);
39 LTC_UNUSED_PARAM(outlen);
40 return CRYPT_NOP;
41 #else
42 hmac_state hmac;
43 FILE *in;
44 unsigned char *buf;
45 size_t x;
46 int err;
47
48 LTC_ARGCHK(fname != NULL);
49 LTC_ARGCHK(key != NULL);
50 LTC_ARGCHK(out != NULL);
51 LTC_ARGCHK(outlen != NULL);
52
53 if ((buf = XMALLOC(LTC_FILE_READ_BUFSIZE)) == NULL) {
54 return CRYPT_MEM;
55 }
56
57 if ((err = hash_is_valid(hash)) != CRYPT_OK) {
58 goto LBL_ERR;
59 }
60
61 if ((err = hmac_init(&hmac, hash, key, keylen)) != CRYPT_OK) {
62 goto LBL_ERR;
63 }
64
65 in = fopen(fname, "rb");
66 if (in == NULL) {
67 err = CRYPT_FILE_NOTFOUND;
68 goto LBL_ERR;
69 }
70
71 do {
72 x = fread(buf, 1, LTC_FILE_READ_BUFSIZE, in);
73 if ((err = hmac_process(&hmac, buf, (unsigned long)x)) != CRYPT_OK) {
74 fclose(in); /* we don't trap this error since we're already returning an error! */
75 goto LBL_CLEANBUF;
76 }
77 } while (x == LTC_FILE_READ_BUFSIZE);
78
79 if (fclose(in) != 0) {
80 err = CRYPT_ERROR;
81 goto LBL_CLEANBUF;
82 }
83
84 err = hmac_done(&hmac, out, outlen);
85
86 LBL_CLEANBUF:
87 zeromem(buf, LTC_FILE_READ_BUFSIZE);
88 LBL_ERR:
89 #ifdef LTC_CLEAN_STACK
90 zeromem(&hmac, sizeof(hmac_state));
91 #endif
92 XFREE(buf);
93 return err;
94 #endif
95 }
96
97 #endif
98
99 /* ref: $Format:%D$ */
100 /* git commit: $Format:%H$ */
101 /* commit time: $Format:%ai$ */
102