Mercurial > dropbear
comparison src/headers/tomcrypt_hash.h @ 191:1c15b283127b libtomcrypt-orig
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Fri, 06 May 2005 13:23:02 +0000 |
parents | |
children | 39d5d58461d6 |
comparison
equal
deleted
inserted
replaced
143:5d99163f7e32 | 191:1c15b283127b |
---|---|
1 /* ---- HASH FUNCTIONS ---- */ | |
2 #ifdef SHA512 | |
3 struct sha512_state { | |
4 ulong64 length, state[8]; | |
5 unsigned long curlen; | |
6 unsigned char buf[128]; | |
7 }; | |
8 #endif | |
9 | |
10 #ifdef SHA256 | |
11 struct sha256_state { | |
12 ulong64 length; | |
13 ulong32 state[8], curlen; | |
14 unsigned char buf[64]; | |
15 }; | |
16 #endif | |
17 | |
18 #ifdef SHA1 | |
19 struct sha1_state { | |
20 ulong64 length; | |
21 ulong32 state[5], curlen; | |
22 unsigned char buf[64]; | |
23 }; | |
24 #endif | |
25 | |
26 #ifdef MD5 | |
27 struct md5_state { | |
28 ulong64 length; | |
29 ulong32 state[4], curlen; | |
30 unsigned char buf[64]; | |
31 }; | |
32 #endif | |
33 | |
34 #ifdef MD4 | |
35 struct md4_state { | |
36 ulong64 length; | |
37 ulong32 state[4], curlen; | |
38 unsigned char buf[64]; | |
39 }; | |
40 #endif | |
41 | |
42 #ifdef TIGER | |
43 struct tiger_state { | |
44 ulong64 state[3], length; | |
45 unsigned long curlen; | |
46 unsigned char buf[64]; | |
47 }; | |
48 #endif | |
49 | |
50 #ifdef MD2 | |
51 struct md2_state { | |
52 unsigned char chksum[16], X[48], buf[16]; | |
53 unsigned long curlen; | |
54 }; | |
55 #endif | |
56 | |
57 #ifdef RIPEMD128 | |
58 struct rmd128_state { | |
59 ulong64 length; | |
60 unsigned char buf[64]; | |
61 ulong32 curlen, state[4]; | |
62 }; | |
63 #endif | |
64 | |
65 #ifdef RIPEMD160 | |
66 struct rmd160_state { | |
67 ulong64 length; | |
68 unsigned char buf[64]; | |
69 ulong32 curlen, state[5]; | |
70 }; | |
71 #endif | |
72 | |
73 #ifdef WHIRLPOOL | |
74 struct whirlpool_state { | |
75 ulong64 length, state[8]; | |
76 unsigned char buf[64]; | |
77 ulong32 curlen; | |
78 }; | |
79 #endif | |
80 | |
81 #ifdef CHC_HASH | |
82 struct chc_state { | |
83 ulong64 length; | |
84 unsigned char state[MAXBLOCKSIZE], buf[MAXBLOCKSIZE]; | |
85 ulong32 curlen; | |
86 }; | |
87 #endif | |
88 | |
89 typedef union Hash_state { | |
90 #ifdef CHC_HASH | |
91 struct chc_state chc; | |
92 #endif | |
93 #ifdef WHIRLPOOL | |
94 struct whirlpool_state whirlpool; | |
95 #endif | |
96 #ifdef SHA512 | |
97 struct sha512_state sha512; | |
98 #endif | |
99 #ifdef SHA256 | |
100 struct sha256_state sha256; | |
101 #endif | |
102 #ifdef SHA1 | |
103 struct sha1_state sha1; | |
104 #endif | |
105 #ifdef MD5 | |
106 struct md5_state md5; | |
107 #endif | |
108 #ifdef MD4 | |
109 struct md4_state md4; | |
110 #endif | |
111 #ifdef MD2 | |
112 struct md2_state md2; | |
113 #endif | |
114 #ifdef TIGER | |
115 struct tiger_state tiger; | |
116 #endif | |
117 #ifdef RIPEMD128 | |
118 struct rmd128_state rmd128; | |
119 #endif | |
120 #ifdef RIPEMD160 | |
121 struct rmd160_state rmd160; | |
122 #endif | |
123 void *data; | |
124 } hash_state; | |
125 | |
126 extern struct ltc_hash_descriptor { | |
127 /** name of hash */ | |
128 char *name; | |
129 /** internal ID */ | |
130 unsigned char ID; | |
131 /** Size of digest in octets */ | |
132 unsigned long hashsize; | |
133 /** Input block size in octets */ | |
134 unsigned long blocksize; | |
135 /** ASN.1 DER identifier */ | |
136 unsigned char DER[64]; | |
137 /** Length of DER encoding */ | |
138 unsigned long DERlen; | |
139 /** Init a hash state | |
140 @param hash The hash to initialize | |
141 @return CRYPT_OK if successful | |
142 */ | |
143 int (*init)(hash_state *hash); | |
144 /** Process a block of data | |
145 @param hash The hash state | |
146 @param in The data to hash | |
147 @param inlen The length of the data (octets) | |
148 @return CRYPT_OK if successful | |
149 */ | |
150 int (*process)(hash_state *hash, const unsigned char *in, unsigned long inlen); | |
151 /** Produce the digest and store it | |
152 @param hash The hash state | |
153 @param out [out] The destination of the digest | |
154 @return CRYPT_OK if successful | |
155 */ | |
156 int (*done)(hash_state *hash, unsigned char *out); | |
157 /** Self-test | |
158 @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled | |
159 */ | |
160 int (*test)(void); | |
161 } hash_descriptor[]; | |
162 | |
163 #ifdef CHC_HASH | |
164 int chc_register(int cipher); | |
165 int chc_init(hash_state * md); | |
166 int chc_process(hash_state * md, const unsigned char *in, unsigned long inlen); | |
167 int chc_done(hash_state * md, unsigned char *hash); | |
168 int chc_test(void); | |
169 extern const struct ltc_hash_descriptor chc_desc; | |
170 #endif | |
171 | |
172 #ifdef WHIRLPOOL | |
173 int whirlpool_init(hash_state * md); | |
174 int whirlpool_process(hash_state * md, const unsigned char *in, unsigned long inlen); | |
175 int whirlpool_done(hash_state * md, unsigned char *hash); | |
176 int whirlpool_test(void); | |
177 extern const struct ltc_hash_descriptor whirlpool_desc; | |
178 #endif | |
179 | |
180 #ifdef SHA512 | |
181 int sha512_init(hash_state * md); | |
182 int sha512_process(hash_state * md, const unsigned char *in, unsigned long inlen); | |
183 int sha512_done(hash_state * md, unsigned char *hash); | |
184 int sha512_test(void); | |
185 extern const struct ltc_hash_descriptor sha512_desc; | |
186 #endif | |
187 | |
188 #ifdef SHA384 | |
189 #ifndef SHA512 | |
190 #error SHA512 is required for SHA384 | |
191 #endif | |
192 int sha384_init(hash_state * md); | |
193 #define sha384_process sha512_process | |
194 int sha384_done(hash_state * md, unsigned char *hash); | |
195 int sha384_test(void); | |
196 extern const struct ltc_hash_descriptor sha384_desc; | |
197 #endif | |
198 | |
199 #ifdef SHA256 | |
200 int sha256_init(hash_state * md); | |
201 int sha256_process(hash_state * md, const unsigned char *in, unsigned long inlen); | |
202 int sha256_done(hash_state * md, unsigned char *hash); | |
203 int sha256_test(void); | |
204 extern const struct ltc_hash_descriptor sha256_desc; | |
205 | |
206 #ifdef SHA224 | |
207 #ifndef SHA256 | |
208 #error SHA256 is required for SHA224 | |
209 #endif | |
210 int sha224_init(hash_state * md); | |
211 #define sha224_process sha256_process | |
212 int sha224_done(hash_state * md, unsigned char *hash); | |
213 int sha224_test(void); | |
214 extern const struct ltc_hash_descriptor sha224_desc; | |
215 #endif | |
216 #endif | |
217 | |
218 #ifdef SHA1 | |
219 int sha1_init(hash_state * md); | |
220 int sha1_process(hash_state * md, const unsigned char *in, unsigned long inlen); | |
221 int sha1_done(hash_state * md, unsigned char *hash); | |
222 int sha1_test(void); | |
223 extern const struct ltc_hash_descriptor sha1_desc; | |
224 #endif | |
225 | |
226 #ifdef MD5 | |
227 int md5_init(hash_state * md); | |
228 int md5_process(hash_state * md, const unsigned char *in, unsigned long inlen); | |
229 int md5_done(hash_state * md, unsigned char *hash); | |
230 int md5_test(void); | |
231 extern const struct ltc_hash_descriptor md5_desc; | |
232 #endif | |
233 | |
234 #ifdef MD4 | |
235 int md4_init(hash_state * md); | |
236 int md4_process(hash_state * md, const unsigned char *in, unsigned long inlen); | |
237 int md4_done(hash_state * md, unsigned char *hash); | |
238 int md4_test(void); | |
239 extern const struct ltc_hash_descriptor md4_desc; | |
240 #endif | |
241 | |
242 #ifdef MD2 | |
243 int md2_init(hash_state * md); | |
244 int md2_process(hash_state * md, const unsigned char *in, unsigned long inlen); | |
245 int md2_done(hash_state * md, unsigned char *hash); | |
246 int md2_test(void); | |
247 extern const struct ltc_hash_descriptor md2_desc; | |
248 #endif | |
249 | |
250 #ifdef TIGER | |
251 int tiger_init(hash_state * md); | |
252 int tiger_process(hash_state * md, const unsigned char *in, unsigned long inlen); | |
253 int tiger_done(hash_state * md, unsigned char *hash); | |
254 int tiger_test(void); | |
255 extern const struct ltc_hash_descriptor tiger_desc; | |
256 #endif | |
257 | |
258 #ifdef RIPEMD128 | |
259 int rmd128_init(hash_state * md); | |
260 int rmd128_process(hash_state * md, const unsigned char *in, unsigned long inlen); | |
261 int rmd128_done(hash_state * md, unsigned char *hash); | |
262 int rmd128_test(void); | |
263 extern const struct ltc_hash_descriptor rmd128_desc; | |
264 #endif | |
265 | |
266 #ifdef RIPEMD160 | |
267 int rmd160_init(hash_state * md); | |
268 int rmd160_process(hash_state * md, const unsigned char *in, unsigned long inlen); | |
269 int rmd160_done(hash_state * md, unsigned char *hash); | |
270 int rmd160_test(void); | |
271 extern const struct ltc_hash_descriptor rmd160_desc; | |
272 #endif | |
273 | |
274 int find_hash(const char *name); | |
275 int find_hash_id(unsigned char ID); | |
276 int find_hash_any(const char *name, int digestlen); | |
277 int register_hash(const struct ltc_hash_descriptor *hash); | |
278 int unregister_hash(const struct ltc_hash_descriptor *hash); | |
279 int hash_is_valid(int idx); | |
280 | |
281 int hash_memory(int hash, | |
282 const unsigned char *in, unsigned long inlen, | |
283 unsigned char *out, unsigned long *outlen); | |
284 int hash_memory_multi(int hash, unsigned char *out, unsigned long *outlen, | |
285 const unsigned char *in, unsigned long inlen, ...); | |
286 int hash_filehandle(int hash, FILE *in, unsigned char *out, unsigned long *outlen); | |
287 int hash_file(int hash, const char *fname, unsigned char *out, unsigned long *outlen); | |
288 | |
289 /* a simple macro for making hash "process" functions */ | |
290 #define HASH_PROCESS(func_name, compress_name, state_var, block_size) \ | |
291 int func_name (hash_state * md, const unsigned char *in, unsigned long inlen) \ | |
292 { \ | |
293 unsigned long n; \ | |
294 int err; \ | |
295 LTC_ARGCHK(md != NULL); \ | |
296 LTC_ARGCHK(in != NULL); \ | |
297 if (md-> state_var .curlen > sizeof(md-> state_var .buf)) { \ | |
298 return CRYPT_INVALID_ARG; \ | |
299 } \ | |
300 while (inlen > 0) { \ | |
301 if (md-> state_var .curlen == 0 && inlen >= block_size) { \ | |
302 if ((err = compress_name (md, (unsigned char *)in)) != CRYPT_OK) { \ | |
303 return err; \ | |
304 } \ | |
305 md-> state_var .length += block_size * 8; \ | |
306 in += block_size; \ | |
307 inlen -= block_size; \ | |
308 } else { \ | |
309 n = MIN(inlen, (block_size - md-> state_var .curlen)); \ | |
310 memcpy(md-> state_var .buf + md-> state_var.curlen, in, (size_t)n); \ | |
311 md-> state_var .curlen += n; \ | |
312 in += n; \ | |
313 inlen -= n; \ | |
314 if (md-> state_var .curlen == block_size) { \ | |
315 if ((err = compress_name (md, md-> state_var .buf)) != CRYPT_OK) {\ | |
316 return err; \ | |
317 } \ | |
318 md-> state_var .length += 8*block_size; \ | |
319 md-> state_var .curlen = 0; \ | |
320 } \ | |
321 } \ | |
322 } \ | |
323 return CRYPT_OK; \ | |
324 } |