comparison libtomcrypt/src/mac/xcbc/xcbc_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 xcbc_file.c 12 @file xcbc_file.c
15 XCBC support, process a file, Tom St Denis 13 XCBC support, process a file, Tom St Denis
16 */ 14 */
17 15
18 #ifdef LTC_XCBC 16 #ifdef LTC_XCBC
25 @param filename The name of the file you wish to XCBC 23 @param filename The name of the file you wish to XCBC
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 xcbc_file(int cipher, 28 int xcbc_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 xcbc_state xcbc; 38 xcbc_state xcbc;
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(filename != 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 = xcbc_init(&xcbc, cipher, key, keylen)) != CRYPT_OK) { 51 if ((err = xcbc_init(&xcbc, cipher, key, keylen)) != CRYPT_OK) {
54 fclose(in); 52 goto LBL_ERR;
55 return err; 53 }
54
55 in = fopen(filename, "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 = xcbc_process(&xcbc, buf, x)) != CRYPT_OK) { 63 if ((err = xcbc_process(&xcbc, 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 = xcbc_done(&xcbc, 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 = xcbc_done(&xcbc, 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(&xcbc, sizeof(xcbc_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$ */