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_memory(int hash, const unsigned char *data, unsigned long len, unsigned char *dst, unsigned long *outlen) |
|
14 { |
143
|
15 hash_state *md; |
3
|
16 int err; |
|
17 |
|
18 _ARGCHK(data != NULL); |
|
19 _ARGCHK(dst != NULL); |
|
20 _ARGCHK(outlen != NULL); |
|
21 |
|
22 if ((err = hash_is_valid(hash)) != CRYPT_OK) { |
|
23 return err; |
|
24 } |
|
25 |
|
26 if (*outlen < hash_descriptor[hash].hashsize) { |
|
27 return CRYPT_BUFFER_OVERFLOW; |
|
28 } |
143
|
29 |
|
30 md = XMALLOC(sizeof(hash_state)); |
|
31 if (md == NULL) { |
|
32 return CRYPT_MEM; |
|
33 } |
3
|
34 |
143
|
35 if ((err = hash_descriptor[hash].init(md)) != CRYPT_OK) { |
|
36 goto __ERR; |
|
37 } |
|
38 if ((err = hash_descriptor[hash].process(md, data, len)) != CRYPT_OK) { |
|
39 goto __ERR; |
|
40 } |
|
41 err = hash_descriptor[hash].done(md, dst); |
|
42 *outlen = hash_descriptor[hash].hashsize; |
|
43 __ERR: |
|
44 #ifdef CLEAN_STACK |
|
45 zeromem(md, sizeof(hash_state)); |
|
46 #endif |
|
47 XFREE(md); |
|
48 |
|
49 return err; |
3
|
50 } |