3
|
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis |
|
2 * |
|
3 * LibTomCrypt is a library that provides various cryptographic |
|
4 * algorithms in a highly modular and flexible manner. |
|
5 * |
|
6 * The library is free for all purposes without any express |
|
7 * guarantee it works. |
|
8 * |
|
9 * Tom St Denis, [email protected], http://libtomcrypt.org |
|
10 */ |
|
11 #include "mycrypt.h" |
|
12 |
|
13 int hash_filehandle(int hash, FILE *in, unsigned char *dst, unsigned long *outlen) |
|
14 { |
|
15 #ifdef NO_FILE |
|
16 return CRYPT_NOP; |
|
17 #else |
|
18 hash_state md; |
|
19 unsigned char buf[512]; |
|
20 size_t x; |
|
21 int err; |
|
22 |
|
23 _ARGCHK(dst != NULL); |
|
24 _ARGCHK(outlen != NULL); |
|
25 _ARGCHK(in != NULL); |
|
26 |
|
27 if ((err = hash_is_valid(hash)) != CRYPT_OK) { |
|
28 return err; |
|
29 } |
|
30 |
|
31 if (*outlen < hash_descriptor[hash].hashsize) { |
|
32 return CRYPT_BUFFER_OVERFLOW; |
|
33 } |
143
|
34 if ((err = hash_descriptor[hash].init(&md)) != CRYPT_OK) { |
|
35 return err; |
|
36 } |
3
|
37 |
143
|
38 *outlen = hash_descriptor[hash].hashsize; |
3
|
39 do { |
|
40 x = fread(buf, 1, sizeof(buf), in); |
143
|
41 if ((err = hash_descriptor[hash].process(&md, buf, x)) != CRYPT_OK) { |
|
42 return err; |
|
43 } |
3
|
44 } while (x == sizeof(buf)); |
143
|
45 err = hash_descriptor[hash].done(&md, dst); |
3
|
46 |
|
47 #ifdef CLEAN_STACK |
|
48 zeromem(buf, sizeof(buf)); |
|
49 #endif |
143
|
50 return err; |
3
|
51 #endif |
|
52 } |
|
53 |