Mercurial > dropbear
comparison libtomcrypt/src/mac/pmac/pmac_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 pmac_file.c | 12 @file pmac_file.c |
15 PMAC implementation, process a file, by Tom St Denis | 13 PMAC implementation, process a file, by Tom St Denis |
16 */ | 14 */ |
17 | 15 |
18 #ifdef LTC_PMAC | 16 #ifdef LTC_PMAC |
19 | 17 |
20 /** | 18 /** |
21 PMAC a file | 19 PMAC 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 to send through PMAC | 23 @param filename The name of the file to send through PMAC |
26 @param out [out] Destination for the authentication tag | 24 @param out [out] Destination for the authentication tag |
27 @param outlen [in/out] Max size and resulting size of the authentication tag | 25 @param outlen [in/out] 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 pmac_file(int cipher, | 28 int pmac_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 *filename, |
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 pmac_state pmac; | 38 pmac_state pmac; |
40 FILE *in; | 39 FILE *in; |
41 unsigned char buf[512]; | 40 unsigned char *buf; |
42 | 41 |
43 | 42 |
44 LTC_ARGCHK(key != NULL); | 43 LTC_ARGCHK(key != NULL); |
45 LTC_ARGCHK(filename != NULL); | 44 LTC_ARGCHK(filename != NULL); |
46 LTC_ARGCHK(out != NULL); | 45 LTC_ARGCHK(out != NULL); |
47 LTC_ARGCHK(outlen != NULL); | 46 LTC_ARGCHK(outlen != NULL); |
48 | 47 |
49 in = fopen(filename, "rb"); | 48 if ((buf = XMALLOC(LTC_FILE_READ_BUFSIZE)) == NULL) { |
50 if (in == NULL) { | 49 return CRYPT_MEM; |
51 return CRYPT_FILE_NOTFOUND; | |
52 } | 50 } |
53 | 51 |
54 if ((err = pmac_init(&pmac, cipher, key, keylen)) != CRYPT_OK) { | 52 if ((err = pmac_init(&pmac, cipher, key, keylen)) != CRYPT_OK) { |
55 fclose(in); | 53 goto LBL_ERR; |
56 return err; | 54 } |
55 | |
56 in = fopen(filename, "rb"); | |
57 if (in == NULL) { | |
58 err = CRYPT_FILE_NOTFOUND; | |
59 goto LBL_ERR; | |
57 } | 60 } |
58 | 61 |
59 do { | 62 do { |
60 x = fread(buf, 1, sizeof(buf), in); | 63 x = fread(buf, 1, LTC_FILE_READ_BUFSIZE, in); |
61 if ((err = pmac_process(&pmac, buf, x)) != CRYPT_OK) { | 64 if ((err = pmac_process(&pmac, buf, (unsigned long)x)) != CRYPT_OK) { |
62 fclose(in); | 65 fclose(in); |
63 return err; | 66 goto LBL_CLEANBUF; |
64 } | 67 } |
65 } while (x == sizeof(buf)); | 68 } while (x == LTC_FILE_READ_BUFSIZE); |
66 fclose(in); | |
67 | 69 |
68 if ((err = pmac_done(&pmac, out, outlen)) != CRYPT_OK) { | 70 if (fclose(in) != 0) { |
69 return err; | 71 err = CRYPT_ERROR; |
72 goto LBL_CLEANBUF; | |
70 } | 73 } |
71 | 74 |
75 err = pmac_done(&pmac, out, outlen); | |
76 | |
77 LBL_CLEANBUF: | |
78 zeromem(buf, LTC_FILE_READ_BUFSIZE); | |
79 LBL_ERR: | |
72 #ifdef LTC_CLEAN_STACK | 80 #ifdef LTC_CLEAN_STACK |
73 zeromem(buf, sizeof(buf)); | 81 zeromem(&pmac, sizeof(pmac_state)); |
74 #endif | 82 #endif |
75 | 83 XFREE(buf); |
76 return CRYPT_OK; | 84 return err; |
77 #endif | 85 #endif |
78 } | 86 } |
79 | 87 |
80 #endif | 88 #endif |
81 | 89 |
82 /* $Source$ */ | 90 /* ref: $Format:%D$ */ |
83 /* $Revision$ */ | 91 /* git commit: $Format:%H$ */ |
84 /* $Date$ */ | 92 /* commit time: $Format:%ai$ */ |