Mercurial > dropbear
comparison libtomcrypt/src/mac/f9/f9_file.c @ 1511:5916af64acd4 fuzz
merge from main
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sat, 17 Feb 2018 19:29:51 +0800 |
parents | 6dba84798cd5 |
children | e9dba7abd939 |
comparison
equal
deleted
inserted
replaced
1457:32f990cc96b1 | 1511:5916af64acd4 |
---|---|
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 f9_file.c | 12 @file f9_file.c |
15 f9 support, process a file, Tom St Denis | 13 f9 support, process a file, Tom St Denis |
16 */ | 14 */ |
17 | 15 |
18 #ifdef LTC_F9_MODE | 16 #ifdef LTC_F9_MODE |
20 /** | 18 /** |
21 f9 a file | 19 f9 a file |
22 @param cipher The index of the cipher desired | 20 @param cipher The index of the cipher desired |
23 @param key The secret key | 21 @param key The secret key |
24 @param keylen The length of the secret key (octets) | 22 @param keylen The length of the secret key (octets) |
25 @param filename The name of the file you wish to f9 | 23 @param fname The name of the file you wish to f9 |
26 @param out [out] Where the authentication tag is to be stored | 24 @param out [out] Where the authentication tag is to be stored |
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 f9_file(int cipher, | 28 int f9_file(int cipher, |
31 const unsigned char *key, unsigned long keylen, | 29 const unsigned char *key, unsigned long keylen, |
32 const char *filename, | 30 const char *fname, |
33 unsigned char *out, unsigned long *outlen) | 31 unsigned char *out, unsigned long *outlen) |
34 { | 32 { |
35 #ifdef LTC_NO_FILE | 33 #ifdef LTC_NO_FILE |
36 return CRYPT_NOP; | 34 return CRYPT_NOP; |
37 #else | 35 #else |
38 int err, x; | 36 size_t x; |
37 int err; | |
39 f9_state f9; | 38 f9_state f9; |
40 FILE *in; | 39 FILE *in; |
41 unsigned char buf[512]; | 40 unsigned char *buf; |
42 | 41 |
43 LTC_ARGCHK(key != NULL); | 42 LTC_ARGCHK(key != NULL); |
44 LTC_ARGCHK(filename != NULL); | 43 LTC_ARGCHK(fname != NULL); |
45 LTC_ARGCHK(out != NULL); | 44 LTC_ARGCHK(out != NULL); |
46 LTC_ARGCHK(outlen != NULL); | 45 LTC_ARGCHK(outlen != NULL); |
47 | 46 |
48 in = fopen(filename, "rb"); | 47 if ((buf = XMALLOC(LTC_FILE_READ_BUFSIZE)) == NULL) { |
49 if (in == NULL) { | 48 return CRYPT_MEM; |
50 return CRYPT_FILE_NOTFOUND; | |
51 } | 49 } |
52 | 50 |
53 if ((err = f9_init(&f9, cipher, key, keylen)) != CRYPT_OK) { | 51 if ((err = f9_init(&f9, cipher, key, keylen)) != CRYPT_OK) { |
54 fclose(in); | 52 goto LBL_ERR; |
55 return err; | 53 } |
54 | |
55 in = fopen(fname, "rb"); | |
56 if (in == NULL) { | |
57 err = CRYPT_FILE_NOTFOUND; | |
58 goto LBL_ERR; | |
56 } | 59 } |
57 | 60 |
58 do { | 61 do { |
59 x = fread(buf, 1, sizeof(buf), in); | 62 x = fread(buf, 1, LTC_FILE_READ_BUFSIZE, in); |
60 if ((err = f9_process(&f9, buf, x)) != CRYPT_OK) { | 63 if ((err = f9_process(&f9, buf, (unsigned long)x)) != CRYPT_OK) { |
61 fclose(in); | 64 fclose(in); |
62 return err; | 65 goto LBL_CLEANBUF; |
63 } | 66 } |
64 } while (x == sizeof(buf)); | 67 } while (x == LTC_FILE_READ_BUFSIZE); |
65 fclose(in); | |
66 | 68 |
67 if ((err = f9_done(&f9, out, outlen)) != CRYPT_OK) { | 69 if (fclose(in) != 0) { |
68 return err; | 70 err = CRYPT_ERROR; |
71 goto LBL_CLEANBUF; | |
69 } | 72 } |
70 | 73 |
74 err = f9_done(&f9, out, outlen); | |
75 | |
76 LBL_CLEANBUF: | |
77 zeromem(buf, LTC_FILE_READ_BUFSIZE); | |
78 LBL_ERR: | |
71 #ifdef LTC_CLEAN_STACK | 79 #ifdef LTC_CLEAN_STACK |
72 zeromem(buf, sizeof(buf)); | 80 zeromem(&f9, sizeof(f9_state)); |
73 #endif | 81 #endif |
74 | 82 XFREE(buf); |
75 return CRYPT_OK; | 83 return err; |
76 #endif | 84 #endif |
77 } | 85 } |
78 | 86 |
79 #endif | 87 #endif |
80 | 88 |
81 /* $Source$ */ | 89 /* ref: $Format:%D$ */ |
82 /* $Revision$ */ | 90 /* git commit: $Format:%H$ */ |
83 /* $Date$ */ | 91 /* commit time: $Format:%ai$ */ |