comparison libtomcrypt/src/mac/hmac/hmac_file.c @ 1471:6dba84798cd5

Update to libtomcrypt 1.18.1, merged with Dropbear changes
author Matt Johnston <matt@ucc.asn.au>
date Fri, 09 Feb 2018 21:44:05 +0800
parents f849a5ca2efc
children e9dba7abd939
comparison
equal deleted inserted replaced
1470:8bba51a55704 1471:6dba84798cd5
3 * LibTomCrypt is a library that provides various cryptographic 3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner. 4 * algorithms in a highly modular and flexible manner.
5 * 5 *
6 * The library is free for all purposes without any express 6 * The library is free for all purposes without any express
7 * guarantee it works. 7 * guarantee it works.
8 *
9 * Tom St Denis, [email protected], http://libtom.org
10 */ 8 */
11 #include "tomcrypt.h" 9 #include "tomcrypt.h"
12 10
13 /** 11 /**
14 @file hmac_file.c 12 @file hmac_file.c
15 LTC_HMAC support, process a file, Tom St Denis/Dobes Vandermeer 13 HMAC support, process a file, Tom St Denis/Dobes Vandermeer
16 */ 14 */
17 15
18 #ifdef LTC_HMAC 16 #ifdef LTC_HMAC
19 17
20 /** 18 /**
21 LTC_HMAC a file 19 HMAC a file
22 @param hash The index of the hash you wish to use 20 @param hash The index of the hash you wish to use
23 @param fname The name of the file you wish to LTC_HMAC 21 @param fname The name of the file you wish to HMAC
24 @param key The secret key 22 @param key The secret key
25 @param keylen The length of the secret key 23 @param keylen The length of the secret key
26 @param out [out] The LTC_HMAC authentication tag 24 @param out [out] The HMAC authentication tag
27 @param outlen [in/out] The max size and resulting size of the authentication tag 25 @param outlen [in/out] The max size and resulting size of the authentication tag
28 @return CRYPT_OK if successful, CRYPT_NOP if file support has been disabled 26 @return CRYPT_OK if successful, CRYPT_NOP if file support has been disabled
29 */ 27 */
30 int hmac_file(int hash, const char *fname, 28 int hmac_file(int hash, const char *fname,
31 const unsigned char *key, unsigned long keylen, 29 const unsigned char *key, unsigned long keylen,
32 unsigned char *out, unsigned long *outlen) 30 unsigned char *out, unsigned long *outlen)
33 { 31 {
34 #ifdef LTC_NO_FILE 32 #ifdef LTC_NO_FILE
35 (void)hash; (void)fname; (void)key; (void)keylen; (void)out; (void)outlen; 33 (void)hash; (void)fname; (void)key; (void)keylen; (void)out; (void)outlen;
36 return CRYPT_NOP; 34 return CRYPT_NOP;
37 #else 35 #else
38 hmac_state hmac; 36 hmac_state hmac;
39 FILE *in; 37 FILE *in;
40 unsigned char buf[512]; 38 unsigned char *buf;
41 size_t x; 39 size_t x;
42 int err; 40 int err;
43 41
44 LTC_ARGCHK(fname != NULL); 42 LTC_ARGCHK(fname != NULL);
45 LTC_ARGCHK(key != NULL); 43 LTC_ARGCHK(key != NULL);
46 LTC_ARGCHK(out != NULL); 44 LTC_ARGCHK(out != NULL);
47 LTC_ARGCHK(outlen != NULL); 45 LTC_ARGCHK(outlen != NULL);
48 46
49 if((err = hash_is_valid(hash)) != CRYPT_OK) { 47 if ((buf = XMALLOC(LTC_FILE_READ_BUFSIZE)) == NULL) {
50 return err; 48 return CRYPT_MEM;
49 }
50
51 if ((err = hash_is_valid(hash)) != CRYPT_OK) {
52 goto LBL_ERR;
51 } 53 }
52 54
53 if ((err = hmac_init(&hmac, hash, key, keylen)) != CRYPT_OK) { 55 if ((err = hmac_init(&hmac, hash, key, keylen)) != CRYPT_OK) {
54 return err; 56 goto LBL_ERR;
55 } 57 }
56 58
57 in = fopen(fname, "rb"); 59 in = fopen(fname, "rb");
58 if (in == NULL) { 60 if (in == NULL) {
59 return CRYPT_FILE_NOTFOUND; 61 err = CRYPT_FILE_NOTFOUND;
62 goto LBL_ERR;
60 } 63 }
61 64
62 /* process the file contents */
63 do { 65 do {
64 x = fread(buf, 1, sizeof(buf), in); 66 x = fread(buf, 1, LTC_FILE_READ_BUFSIZE, in);
65 if ((err = hmac_process(&hmac, buf, (unsigned long)x)) != CRYPT_OK) { 67 if ((err = hmac_process(&hmac, buf, (unsigned long)x)) != CRYPT_OK) {
66 /* we don't trap this error since we're already returning an error! */ 68 fclose(in); /* we don't trap this error since we're already returning an error! */
67 fclose(in); 69 goto LBL_CLEANBUF;
68 return err;
69 } 70 }
70 } while (x == sizeof(buf)); 71 } while (x == LTC_FILE_READ_BUFSIZE);
71 72
72 if (fclose(in) != 0) { 73 if (fclose(in) != 0) {
73 return CRYPT_ERROR; 74 err = CRYPT_ERROR;
75 goto LBL_CLEANBUF;
74 } 76 }
75 77
76 /* get final hmac */ 78 err = hmac_done(&hmac, out, outlen);
77 if ((err = hmac_done(&hmac, out, outlen)) != CRYPT_OK) {
78 return err;
79 }
80 79
80 LBL_CLEANBUF:
81 zeromem(buf, LTC_FILE_READ_BUFSIZE);
82 LBL_ERR:
81 #ifdef LTC_CLEAN_STACK 83 #ifdef LTC_CLEAN_STACK
82 /* clear memory */ 84 zeromem(&hmac, sizeof(hmac_state));
83 zeromem(buf, sizeof(buf)); 85 #endif
84 #endif 86 XFREE(buf);
85 return CRYPT_OK; 87 return err;
86 #endif 88 #endif
87 } 89 }
88 90
89 #endif 91 #endif
90 92
91 93 /* ref: $Format:%D$ */
92 /* $Source$ */ 94 /* git commit: $Format:%H$ */
93 /* $Revision$ */ 95 /* commit time: $Format:%ai$ */
94 /* $Date$ */