3
|
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, [email protected], http://libtomcrypt.org |
|
10 */ |
|
11 /* Submited by Dobes Vandermeer ([email protected]) */ |
|
12 |
|
13 #include "mycrypt.h" |
|
14 |
|
15 #ifdef HMAC |
|
16 |
|
17 /* hmac_file added by Tom St Denis */ |
|
18 int hmac_file(int hash, const char *fname, |
|
19 const unsigned char *key, unsigned long keylen, |
|
20 unsigned char *dst, unsigned long *dstlen) |
|
21 { |
|
22 #ifdef NO_FILE |
|
23 return CRYPT_NOP; |
|
24 #else |
|
25 hmac_state hmac; |
|
26 FILE *in; |
|
27 unsigned char buf[512]; |
|
28 size_t x; |
|
29 int err; |
|
30 |
|
31 _ARGCHK(fname != NULL); |
|
32 _ARGCHK(key != NULL); |
|
33 _ARGCHK(dst != NULL); |
|
34 _ARGCHK(dstlen != NULL); |
|
35 |
|
36 if((err = hash_is_valid(hash)) != CRYPT_OK) { |
|
37 return err; |
|
38 } |
|
39 |
|
40 if ((err = hmac_init(&hmac, hash, key, keylen)) != CRYPT_OK) { |
|
41 return err; |
|
42 } |
|
43 |
|
44 in = fopen(fname, "rb"); |
|
45 if (in == NULL) { |
|
46 return CRYPT_FILE_NOTFOUND; |
|
47 } |
|
48 |
|
49 /* process the file contents */ |
|
50 do { |
|
51 x = fread(buf, 1, sizeof(buf), in); |
|
52 if ((err = hmac_process(&hmac, buf, (unsigned long)x)) != CRYPT_OK) { |
|
53 /* we don't trap this error since we're already returning an error! */ |
|
54 fclose(in); |
|
55 return err; |
|
56 } |
|
57 } while (x == sizeof(buf)); |
|
58 |
|
59 if (fclose(in) != 0) { |
|
60 return CRYPT_ERROR; |
|
61 } |
|
62 |
|
63 /* get final hmac */ |
|
64 if ((err = hmac_done(&hmac, dst, dstlen)) != CRYPT_OK) { |
|
65 return err; |
|
66 } |
|
67 |
|
68 #ifdef CLEAN_STACK |
|
69 /* clear memory */ |
|
70 zeromem(buf, sizeof(buf)); |
|
71 #endif |
|
72 return CRYPT_OK; |
|
73 #endif |
|
74 } |
|
75 |
|
76 #endif |
|
77 |