Mercurial > dropbear
comparison libtomcrypt/src/headers/tomcrypt_hash.h @ 1478:3a933956437e coverity
update coverity
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Fri, 09 Feb 2018 23:49:22 +0800 |
parents | 6dba84798cd5 |
children |
comparison
equal
deleted
inserted
replaced
1439:8d24733026c5 | 1478:3a933956437e |
---|---|
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 | |
1 /* ---- HASH FUNCTIONS ---- */ | 10 /* ---- HASH FUNCTIONS ---- */ |
11 #ifdef LTC_SHA3 | |
12 struct sha3_state { | |
13 ulong64 saved; /* the portion of the input message that we didn't consume yet */ | |
14 ulong64 s[25]; | |
15 unsigned char sb[25 * 8]; /* used for storing `ulong64 s[25]` as little-endian bytes */ | |
16 unsigned short byte_index; /* 0..7--the next byte after the set one (starts from 0; 0--none are buffered) */ | |
17 unsigned short word_index; /* 0..24--the next word to integrate input (starts from 0) */ | |
18 unsigned short capacity_words; /* the double size of the hash output in words (e.g. 16 for Keccak 512) */ | |
19 unsigned short xof_flag; | |
20 }; | |
21 #endif | |
22 | |
2 #ifdef LTC_SHA512 | 23 #ifdef LTC_SHA512 |
3 struct sha512_state { | 24 struct sha512_state { |
4 ulong64 length, state[8]; | 25 ulong64 length, state[8]; |
5 unsigned long curlen; | 26 unsigned long curlen; |
6 unsigned char buf[128]; | 27 unsigned char buf[128]; |
100 unsigned char state[MAXBLOCKSIZE], buf[MAXBLOCKSIZE]; | 121 unsigned char state[MAXBLOCKSIZE], buf[MAXBLOCKSIZE]; |
101 ulong32 curlen; | 122 ulong32 curlen; |
102 }; | 123 }; |
103 #endif | 124 #endif |
104 | 125 |
126 #ifdef LTC_BLAKE2S | |
127 struct blake2s_state { | |
128 ulong32 h[8]; | |
129 ulong32 t[2]; | |
130 ulong32 f[2]; | |
131 unsigned char buf[64]; | |
132 unsigned long curlen; | |
133 unsigned long outlen; | |
134 unsigned char last_node; | |
135 }; | |
136 #endif | |
137 | |
138 #ifdef LTC_BLAKE2B | |
139 struct blake2b_state { | |
140 ulong64 h[8]; | |
141 ulong64 t[2]; | |
142 ulong64 f[2]; | |
143 unsigned char buf[128]; | |
144 unsigned long curlen; | |
145 unsigned long outlen; | |
146 unsigned char last_node; | |
147 }; | |
148 #endif | |
149 | |
105 typedef union Hash_state { | 150 typedef union Hash_state { |
106 char dummy[1]; | 151 char dummy[1]; |
107 #ifdef LTC_CHC_HASH | 152 #ifdef LTC_CHC_HASH |
108 struct chc_state chc; | 153 struct chc_state chc; |
109 #endif | 154 #endif |
110 #ifdef LTC_WHIRLPOOL | 155 #ifdef LTC_WHIRLPOOL |
111 struct whirlpool_state whirlpool; | 156 struct whirlpool_state whirlpool; |
112 #endif | 157 #endif |
158 #ifdef LTC_SHA3 | |
159 struct sha3_state sha3; | |
160 #endif | |
113 #ifdef LTC_SHA512 | 161 #ifdef LTC_SHA512 |
114 struct sha512_state sha512; | 162 struct sha512_state sha512; |
115 #endif | 163 #endif |
116 #ifdef LTC_SHA256 | 164 #ifdef LTC_SHA256 |
117 struct sha256_state sha256; | 165 struct sha256_state sha256; |
141 struct rmd256_state rmd256; | 189 struct rmd256_state rmd256; |
142 #endif | 190 #endif |
143 #ifdef LTC_RIPEMD320 | 191 #ifdef LTC_RIPEMD320 |
144 struct rmd320_state rmd320; | 192 struct rmd320_state rmd320; |
145 #endif | 193 #endif |
194 #ifdef LTC_BLAKE2S | |
195 struct blake2s_state blake2s; | |
196 #endif | |
197 #ifdef LTC_BLAKE2B | |
198 struct blake2b_state blake2b; | |
199 #endif | |
200 | |
146 void *data; | 201 void *data; |
147 } hash_state; | 202 } hash_state; |
148 | 203 |
149 /** hash descriptor */ | 204 /** hash descriptor */ |
150 extern struct ltc_hash_descriptor { | 205 extern struct ltc_hash_descriptor { |
151 /** name of hash */ | 206 /** name of hash */ |
152 char *name; | 207 const char *name; |
153 /** internal ID */ | 208 /** internal ID */ |
154 unsigned char ID; | 209 unsigned char ID; |
155 /** Size of digest in octets */ | 210 /** Size of digest in octets */ |
156 unsigned long hashsize; | 211 unsigned long hashsize; |
157 /** Input block size in octets */ | 212 /** Input block size in octets */ |
164 /** Init a hash state | 219 /** Init a hash state |
165 @param hash The hash to initialize | 220 @param hash The hash to initialize |
166 @return CRYPT_OK if successful | 221 @return CRYPT_OK if successful |
167 */ | 222 */ |
168 int (*init)(hash_state *hash); | 223 int (*init)(hash_state *hash); |
169 /** Process a block of data | 224 /** Process a block of data |
170 @param hash The hash state | 225 @param hash The hash state |
171 @param in The data to hash | 226 @param in The data to hash |
172 @param inlen The length of the data (octets) | 227 @param inlen The length of the data (octets) |
173 @return CRYPT_OK if successful | 228 @return CRYPT_OK if successful |
174 */ | 229 */ |
184 */ | 239 */ |
185 int (*test)(void); | 240 int (*test)(void); |
186 | 241 |
187 /* accelerated hmac callback: if you need to-do multiple packets just use the generic hmac_memory and provide a hash callback */ | 242 /* accelerated hmac callback: if you need to-do multiple packets just use the generic hmac_memory and provide a hash callback */ |
188 int (*hmac_block)(const unsigned char *key, unsigned long keylen, | 243 int (*hmac_block)(const unsigned char *key, unsigned long keylen, |
189 const unsigned char *in, unsigned long inlen, | 244 const unsigned char *in, unsigned long inlen, |
190 unsigned char *out, unsigned long *outlen); | 245 unsigned char *out, unsigned long *outlen); |
191 | 246 |
192 } hash_descriptor[]; | 247 } hash_descriptor[]; |
193 | 248 |
194 #ifdef LTC_CHC_HASH | 249 #ifdef LTC_CHC_HASH |
206 int whirlpool_done(hash_state * md, unsigned char *hash); | 261 int whirlpool_done(hash_state * md, unsigned char *hash); |
207 int whirlpool_test(void); | 262 int whirlpool_test(void); |
208 extern const struct ltc_hash_descriptor whirlpool_desc; | 263 extern const struct ltc_hash_descriptor whirlpool_desc; |
209 #endif | 264 #endif |
210 | 265 |
266 #ifdef LTC_SHA3 | |
267 int sha3_512_init(hash_state * md); | |
268 int sha3_512_test(void); | |
269 extern const struct ltc_hash_descriptor sha3_512_desc; | |
270 int sha3_384_init(hash_state * md); | |
271 int sha3_384_test(void); | |
272 extern const struct ltc_hash_descriptor sha3_384_desc; | |
273 int sha3_256_init(hash_state * md); | |
274 int sha3_256_test(void); | |
275 extern const struct ltc_hash_descriptor sha3_256_desc; | |
276 int sha3_224_init(hash_state * md); | |
277 int sha3_224_test(void); | |
278 extern const struct ltc_hash_descriptor sha3_224_desc; | |
279 /* process + done are the same for all variants */ | |
280 int sha3_process(hash_state * md, const unsigned char *in, unsigned long inlen); | |
281 int sha3_done(hash_state *md, unsigned char *hash); | |
282 /* SHAKE128 + SHAKE256 */ | |
283 int sha3_shake_init(hash_state *md, int num); | |
284 #define sha3_shake_process(a,b,c) sha3_process(a,b,c) | |
285 int sha3_shake_done(hash_state *md, unsigned char *out, unsigned long outlen); | |
286 int sha3_shake_test(void); | |
287 int sha3_shake_memory(int num, const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen); | |
288 #endif | |
289 | |
211 #ifdef LTC_SHA512 | 290 #ifdef LTC_SHA512 |
212 int sha512_init(hash_state * md); | 291 int sha512_init(hash_state * md); |
213 int sha512_process(hash_state * md, const unsigned char *in, unsigned long inlen); | 292 int sha512_process(hash_state * md, const unsigned char *in, unsigned long inlen); |
214 int sha512_done(hash_state * md, unsigned char *hash); | 293 int sha512_done(hash_state * md, unsigned char *hash); |
215 int sha512_test(void); | 294 int sha512_test(void); |
225 int sha384_done(hash_state * md, unsigned char *hash); | 304 int sha384_done(hash_state * md, unsigned char *hash); |
226 int sha384_test(void); | 305 int sha384_test(void); |
227 extern const struct ltc_hash_descriptor sha384_desc; | 306 extern const struct ltc_hash_descriptor sha384_desc; |
228 #endif | 307 #endif |
229 | 308 |
309 #ifdef LTC_SHA512_256 | |
310 #ifndef LTC_SHA512 | |
311 #error LTC_SHA512 is required for LTC_SHA512_256 | |
312 #endif | |
313 int sha512_256_init(hash_state * md); | |
314 #define sha512_256_process sha512_process | |
315 int sha512_256_done(hash_state * md, unsigned char *hash); | |
316 int sha512_256_test(void); | |
317 extern const struct ltc_hash_descriptor sha512_256_desc; | |
318 #endif | |
319 | |
320 #ifdef LTC_SHA512_224 | |
321 #ifndef LTC_SHA512 | |
322 #error LTC_SHA512 is required for LTC_SHA512_224 | |
323 #endif | |
324 int sha512_224_init(hash_state * md); | |
325 #define sha512_224_process sha512_process | |
326 int sha512_224_done(hash_state * md, unsigned char *hash); | |
327 int sha512_224_test(void); | |
328 extern const struct ltc_hash_descriptor sha512_224_desc; | |
329 #endif | |
330 | |
230 #ifdef LTC_SHA256 | 331 #ifdef LTC_SHA256 |
231 int sha256_init(hash_state * md); | 332 int sha256_init(hash_state * md); |
232 int sha256_process(hash_state * md, const unsigned char *in, unsigned long inlen); | 333 int sha256_process(hash_state * md, const unsigned char *in, unsigned long inlen); |
233 int sha256_done(hash_state * md, unsigned char *hash); | 334 int sha256_done(hash_state * md, unsigned char *hash); |
234 int sha256_test(void); | 335 int sha256_test(void); |
252 int sha1_done(hash_state * md, unsigned char *hash); | 353 int sha1_done(hash_state * md, unsigned char *hash); |
253 int sha1_test(void); | 354 int sha1_test(void); |
254 extern const struct ltc_hash_descriptor sha1_desc; | 355 extern const struct ltc_hash_descriptor sha1_desc; |
255 #endif | 356 #endif |
256 | 357 |
358 #ifdef LTC_BLAKE2S | |
359 extern const struct ltc_hash_descriptor blake2s_256_desc; | |
360 int blake2s_256_init(hash_state * md); | |
361 int blake2s_256_test(void); | |
362 | |
363 extern const struct ltc_hash_descriptor blake2s_224_desc; | |
364 int blake2s_224_init(hash_state * md); | |
365 int blake2s_224_test(void); | |
366 | |
367 extern const struct ltc_hash_descriptor blake2s_160_desc; | |
368 int blake2s_160_init(hash_state * md); | |
369 int blake2s_160_test(void); | |
370 | |
371 extern const struct ltc_hash_descriptor blake2s_128_desc; | |
372 int blake2s_128_init(hash_state * md); | |
373 int blake2s_128_test(void); | |
374 | |
375 int blake2s_init(hash_state * md, unsigned long outlen, const unsigned char *key, unsigned long keylen); | |
376 int blake2s_process(hash_state * md, const unsigned char *in, unsigned long inlen); | |
377 int blake2s_done(hash_state * md, unsigned char *hash); | |
378 #endif | |
379 | |
380 #ifdef LTC_BLAKE2B | |
381 extern const struct ltc_hash_descriptor blake2b_512_desc; | |
382 int blake2b_512_init(hash_state * md); | |
383 int blake2b_512_test(void); | |
384 | |
385 extern const struct ltc_hash_descriptor blake2b_384_desc; | |
386 int blake2b_384_init(hash_state * md); | |
387 int blake2b_384_test(void); | |
388 | |
389 extern const struct ltc_hash_descriptor blake2b_256_desc; | |
390 int blake2b_256_init(hash_state * md); | |
391 int blake2b_256_test(void); | |
392 | |
393 extern const struct ltc_hash_descriptor blake2b_160_desc; | |
394 int blake2b_160_init(hash_state * md); | |
395 int blake2b_160_test(void); | |
396 | |
397 int blake2b_init(hash_state * md, unsigned long outlen, const unsigned char *key, unsigned long keylen); | |
398 int blake2b_process(hash_state * md, const unsigned char *in, unsigned long inlen); | |
399 int blake2b_done(hash_state * md, unsigned char *hash); | |
400 #endif | |
401 | |
257 #ifdef LTC_MD5 | 402 #ifdef LTC_MD5 |
258 int md5_init(hash_state * md); | 403 int md5_init(hash_state * md); |
259 int md5_process(hash_state * md, const unsigned char *in, unsigned long inlen); | 404 int md5_process(hash_state * md, const unsigned char *in, unsigned long inlen); |
260 int md5_done(hash_state * md, unsigned char *hash); | 405 int md5_done(hash_state * md, unsigned char *hash); |
261 int md5_test(void); | 406 int md5_test(void); |
323 int find_hash_id(unsigned char ID); | 468 int find_hash_id(unsigned char ID); |
324 int find_hash_oid(const unsigned long *ID, unsigned long IDlen); | 469 int find_hash_oid(const unsigned long *ID, unsigned long IDlen); |
325 int find_hash_any(const char *name, int digestlen); | 470 int find_hash_any(const char *name, int digestlen); |
326 int register_hash(const struct ltc_hash_descriptor *hash); | 471 int register_hash(const struct ltc_hash_descriptor *hash); |
327 int unregister_hash(const struct ltc_hash_descriptor *hash); | 472 int unregister_hash(const struct ltc_hash_descriptor *hash); |
473 int register_all_hashes(void); | |
328 int hash_is_valid(int idx); | 474 int hash_is_valid(int idx); |
329 | 475 |
330 LTC_MUTEX_PROTO(ltc_hash_mutex) | 476 LTC_MUTEX_PROTO(ltc_hash_mutex) |
331 | 477 |
332 int hash_memory(int hash, | 478 int hash_memory(int hash, |
333 const unsigned char *in, unsigned long inlen, | 479 const unsigned char *in, unsigned long inlen, |
334 unsigned char *out, unsigned long *outlen); | 480 unsigned char *out, unsigned long *outlen); |
335 int hash_memory_multi(int hash, unsigned char *out, unsigned long *outlen, | 481 int hash_memory_multi(int hash, unsigned char *out, unsigned long *outlen, |
336 const unsigned char *in, unsigned long inlen, ...); | 482 const unsigned char *in, unsigned long inlen, ...); |
483 | |
484 #ifndef LTC_NO_FILE | |
337 int hash_filehandle(int hash, FILE *in, unsigned char *out, unsigned long *outlen); | 485 int hash_filehandle(int hash, FILE *in, unsigned char *out, unsigned long *outlen); |
338 int hash_file(int hash, const char *fname, unsigned char *out, unsigned long *outlen); | 486 int hash_file(int hash, const char *fname, unsigned char *out, unsigned long *outlen); |
487 #endif | |
339 | 488 |
340 /* a simple macro for making hash "process" functions */ | 489 /* a simple macro for making hash "process" functions */ |
341 #define HASH_PROCESS(func_name, compress_name, state_var, block_size) \ | 490 #define HASH_PROCESS(func_name, compress_name, state_var, block_size) \ |
342 int func_name (hash_state * md, const unsigned char *in, unsigned long inlen) \ | 491 int func_name (hash_state * md, const unsigned char *in, unsigned long inlen) \ |
343 { \ | 492 { \ |
346 LTC_ARGCHK(md != NULL); \ | 495 LTC_ARGCHK(md != NULL); \ |
347 LTC_ARGCHK(in != NULL); \ | 496 LTC_ARGCHK(in != NULL); \ |
348 if (md-> state_var .curlen > sizeof(md-> state_var .buf)) { \ | 497 if (md-> state_var .curlen > sizeof(md-> state_var .buf)) { \ |
349 return CRYPT_INVALID_ARG; \ | 498 return CRYPT_INVALID_ARG; \ |
350 } \ | 499 } \ |
500 if ((md-> state_var .length + inlen) < md-> state_var .length) { \ | |
501 return CRYPT_HASH_OVERFLOW; \ | |
502 } \ | |
351 while (inlen > 0) { \ | 503 while (inlen > 0) { \ |
352 if (md-> state_var .curlen == 0 && inlen >= block_size) { \ | 504 if (md-> state_var .curlen == 0 && inlen >= block_size) { \ |
353 if ((err = compress_name (md, (unsigned char *)in)) != CRYPT_OK) { \ | 505 if ((err = compress_name (md, (unsigned char *)in)) != CRYPT_OK) { \ |
354 return err; \ | 506 return err; \ |
355 } \ | 507 } \ |
356 md-> state_var .length += block_size * 8; \ | 508 md-> state_var .length += block_size * 8; \ |
357 in += block_size; \ | 509 in += block_size; \ |
358 inlen -= block_size; \ | 510 inlen -= block_size; \ |
359 } else { \ | 511 } else { \ |
360 n = MIN(inlen, (block_size - md-> state_var .curlen)); \ | 512 n = MIN(inlen, (block_size - md-> state_var .curlen)); \ |
361 memcpy(md-> state_var .buf + md-> state_var.curlen, in, (size_t)n); \ | 513 XMEMCPY(md-> state_var .buf + md-> state_var.curlen, in, (size_t)n); \ |
362 md-> state_var .curlen += n; \ | 514 md-> state_var .curlen += n; \ |
363 in += n; \ | 515 in += n; \ |
364 inlen -= n; \ | 516 inlen -= n; \ |
365 if (md-> state_var .curlen == block_size) { \ | 517 if (md-> state_var .curlen == block_size) { \ |
366 if ((err = compress_name (md, md-> state_var .buf)) != CRYPT_OK) { \ | 518 if ((err = compress_name (md, md-> state_var .buf)) != CRYPT_OK) { \ |
372 } \ | 524 } \ |
373 } \ | 525 } \ |
374 return CRYPT_OK; \ | 526 return CRYPT_OK; \ |
375 } | 527 } |
376 | 528 |
377 /* $Source$ */ | 529 /* ref: $Format:%D$ */ |
378 /* $Revision$ */ | 530 /* git commit: $Format:%H$ */ |
379 /* $Date$ */ | 531 /* commit time: $Format:%ai$ */ |