Revert "Revert "and added files""
[bcm963xx.git] / userapps / opensource / sshd / libtomcrypt / hash.c
1 #include "mycrypt.h"
2
3 int hash_memory(int hash, const unsigned char *data, unsigned long len, unsigned char *dst, unsigned long *outlen)
4 {
5     hash_state md;
6     int err;
7
8     _ARGCHK(data != NULL);
9     _ARGCHK(dst != NULL);
10     _ARGCHK(outlen != NULL);
11
12     if ((err = hash_is_valid(hash)) != CRYPT_OK) {
13         return err;
14     }
15
16     if (*outlen < hash_descriptor[hash].hashsize) {
17        return CRYPT_BUFFER_OVERFLOW;
18     }
19     *outlen = hash_descriptor[hash].hashsize;
20
21     hash_descriptor[hash].init(&md);
22     hash_descriptor[hash].process(&md, data, len);
23     hash_descriptor[hash].done(&md, dst);
24     return CRYPT_OK;
25 }
26
27 int hash_filehandle(int hash, FILE *in, unsigned char *dst, unsigned long *outlen)
28 {
29 #ifdef NO_FILE
30     return CRYPT_ERROR;
31 #else
32     hash_state md;
33     unsigned char buf[512];
34     size_t x;
35     int err;
36
37     _ARGCHK(dst != NULL);
38     _ARGCHK(outlen != NULL);
39     _ARGCHK(in != NULL);
40
41     if ((err = hash_is_valid(hash)) != CRYPT_OK) {
42         return err;
43     }
44
45     if (*outlen < hash_descriptor[hash].hashsize) {
46        return CRYPT_BUFFER_OVERFLOW;
47     }
48     *outlen = hash_descriptor[hash].hashsize;
49
50     hash_descriptor[hash].init(&md);
51     do {
52         x = fread(buf, 1, sizeof(buf), in);
53         hash_descriptor[hash].process(&md, buf, x);
54     } while (x == sizeof(buf));
55     hash_descriptor[hash].done(&md, dst);
56
57 #ifdef CLEAN_STACK
58     zeromem(buf, sizeof(buf));
59 #endif
60     return CRYPT_OK;
61 #endif
62 }
63
64 int hash_file(int hash, const char *fname, unsigned char *dst, unsigned long *outlen)
65 {
66 #ifdef NO_FILE
67     return CRYPT_ERROR;
68 #else
69     FILE *in;
70     int err;
71     _ARGCHK(fname != NULL);
72     _ARGCHK(dst != NULL);
73     _ARGCHK(outlen != NULL);
74
75     if ((err = hash_is_valid(hash)) != CRYPT_OK) {
76         return err;
77     }
78
79     in = fopen(fname, "rb");
80     if (in == NULL) { 
81        return CRYPT_INVALID_ARG;
82     }
83
84     if ((err = hash_filehandle(hash, in, dst, outlen)) != CRYPT_OK) {
85        (void)fclose(in);
86        return err;
87     }
88     (void)fclose(in);
89
90     return CRYPT_OK;
91 #endif
92 }
93