comparison libtomcrypt/src/hashes/helper/hash_filehandle.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 1ff2a1034c52
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
11 #ifndef LTC_NO_FILE
13 /** 12 /**
14 @file hash_filehandle.c 13 @file hash_filehandle.c
15 Hash open files, Tom St Denis 14 Hash open files, Tom St Denis
16 */ 15 */
17 16
23 @param outlen [in/out] The max size and resulting size of the digest 22 @param outlen [in/out] The max size and resulting size of the digest
24 @result CRYPT_OK if successful 23 @result CRYPT_OK if successful
25 */ 24 */
26 int hash_filehandle(int hash, FILE *in, unsigned char *out, unsigned long *outlen) 25 int hash_filehandle(int hash, FILE *in, unsigned char *out, unsigned long *outlen)
27 { 26 {
28 #ifdef LTC_NO_FILE
29 (void)hash; (void)in; (void)out; (void)outlen;
30 return CRYPT_NOP;
31 #else
32 hash_state md; 27 hash_state md;
33 unsigned char buf[512]; 28 unsigned char *buf;
34 size_t x; 29 size_t x;
35 int err; 30 int err;
36 31
37 LTC_ARGCHK(out != NULL); 32 LTC_ARGCHK(out != NULL);
38 LTC_ARGCHK(outlen != NULL); 33 LTC_ARGCHK(outlen != NULL);
39 LTC_ARGCHK(in != NULL); 34 LTC_ARGCHK(in != NULL);
40 35
36 if ((buf = XMALLOC(LTC_FILE_READ_BUFSIZE)) == NULL) {
37 return CRYPT_MEM;
38 }
39
41 if ((err = hash_is_valid(hash)) != CRYPT_OK) { 40 if ((err = hash_is_valid(hash)) != CRYPT_OK) {
42 return err; 41 goto LBL_ERR;
43 } 42 }
44 43
45 if (*outlen < hash_descriptor[hash].hashsize) { 44 if (*outlen < hash_descriptor[hash].hashsize) {
46 *outlen = hash_descriptor[hash].hashsize; 45 *outlen = hash_descriptor[hash].hashsize;
47 return CRYPT_BUFFER_OVERFLOW; 46 err = CRYPT_BUFFER_OVERFLOW;
47 goto LBL_ERR;
48 } 48 }
49 if ((err = hash_descriptor[hash].init(&md)) != CRYPT_OK) { 49 if ((err = hash_descriptor[hash].init(&md)) != CRYPT_OK) {
50 return err; 50 goto LBL_ERR;
51 } 51 }
52 52
53 do {
54 x = fread(buf, 1, LTC_FILE_READ_BUFSIZE, in);
55 if ((err = hash_descriptor[hash].process(&md, buf, (unsigned long)x)) != CRYPT_OK) {
56 goto LBL_CLEANBUF;
57 }
58 } while (x == LTC_FILE_READ_BUFSIZE);
59 if ((err = hash_descriptor[hash].done(&md, out)) == CRYPT_OK) {
53 *outlen = hash_descriptor[hash].hashsize; 60 *outlen = hash_descriptor[hash].hashsize;
54 do {
55 x = fread(buf, 1, sizeof(buf), in);
56 if ((err = hash_descriptor[hash].process(&md, buf, x)) != CRYPT_OK) {
57 return err;
58 } 61 }
59 } while (x == sizeof(buf));
60 err = hash_descriptor[hash].done(&md, out);
61 62
62 #ifdef LTC_CLEAN_STACK 63 LBL_CLEANBUF:
63 zeromem(buf, sizeof(buf)); 64 zeromem(buf, LTC_FILE_READ_BUFSIZE);
64 #endif 65 LBL_ERR:
66 XFREE(buf);
65 return err; 67 return err;
66 #endif
67 } 68 }
69 #endif /* #ifndef LTC_NO_FILE */
68 70
69 71
70 /* $Source$ */ 72 /* ref: $Format:%D$ */
71 /* $Revision$ */ 73 /* git commit: $Format:%H$ */
72 /* $Date$ */ 74 /* commit time: $Format:%ai$ */