1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
10 /* ---- HASH FUNCTIONS ---- */
13 ulong64 saved; /* the portion of the input message that we didn't consume yet */
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;
25 ulong64 length, state[8];
27 unsigned char buf[128];
34 ulong32 state[8], curlen;
35 unsigned char buf[64];
42 ulong32 state[5], curlen;
43 unsigned char buf[64];
50 ulong32 state[4], curlen;
51 unsigned char buf[64];
58 ulong32 state[4], curlen;
59 unsigned char buf[64];
65 ulong64 state[3], length;
67 unsigned char buf[64];
73 unsigned char chksum[16], X[48], buf[16];
81 unsigned char buf[64];
82 ulong32 curlen, state[4];
89 unsigned char buf[64];
90 ulong32 curlen, state[5];
97 unsigned char buf[64];
98 ulong32 curlen, state[8];
103 struct rmd320_state {
105 unsigned char buf[64];
106 ulong32 curlen, state[10];
111 struct whirlpool_state {
112 ulong64 length, state[8];
113 unsigned char buf[64];
121 unsigned char state[MAXBLOCKSIZE], buf[MAXBLOCKSIZE];
127 struct blake2s_state {
131 unsigned char buf[64];
132 unsigned long curlen;
133 unsigned long outlen;
134 unsigned char last_node;
139 struct blake2b_state {
143 unsigned char buf[128];
144 unsigned long curlen;
145 unsigned long outlen;
146 unsigned char last_node;
150 typedef union Hash_state {
153 struct chc_state chc;
156 struct whirlpool_state whirlpool;
159 struct sha3_state sha3;
162 struct sha512_state sha512;
165 struct sha256_state sha256;
168 struct sha1_state sha1;
171 struct md5_state md5;
174 struct md4_state md4;
177 struct md2_state md2;
180 struct tiger_state tiger;
183 struct rmd128_state rmd128;
186 struct rmd160_state rmd160;
189 struct rmd256_state rmd256;
192 struct rmd320_state rmd320;
195 struct blake2s_state blake2s;
198 struct blake2b_state blake2b;
204 /** hash descriptor */
205 extern struct ltc_hash_descriptor {
210 /** Size of digest in octets */
211 unsigned long hashsize;
212 /** Input block size in octets */
213 unsigned long blocksize;
215 unsigned long OID[16];
216 /** Length of DER encoding */
217 unsigned long OIDlen;
219 /** Init a hash state
220 @param hash The hash to initialize
221 @return CRYPT_OK if successful
223 int (*init)(hash_state *hash);
224 /** Process a block of data
225 @param hash The hash state
226 @param in The data to hash
227 @param inlen The length of the data (octets)
228 @return CRYPT_OK if successful
230 int (*process)(hash_state *hash, const unsigned char *in, unsigned long inlen);
231 /** Produce the digest and store it
232 @param hash The hash state
233 @param out [out] The destination of the digest
234 @return CRYPT_OK if successful
236 int (*done)(hash_state *hash, unsigned char *out);
238 @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled
242 /* accelerated hmac callback: if you need to-do multiple packets just use the generic hmac_memory and provide a hash callback */
243 int (*hmac_block)(const unsigned char *key, unsigned long keylen,
244 const unsigned char *in, unsigned long inlen,
245 unsigned char *out, unsigned long *outlen);
250 int chc_register(int cipher);
251 int chc_init(hash_state * md);
252 int chc_process(hash_state * md, const unsigned char *in, unsigned long inlen);
253 int chc_done(hash_state * md, unsigned char *hash);
255 extern const struct ltc_hash_descriptor chc_desc;
259 int whirlpool_init(hash_state * md);
260 int whirlpool_process(hash_state * md, const unsigned char *in, unsigned long inlen);
261 int whirlpool_done(hash_state * md, unsigned char *hash);
262 int whirlpool_test(void);
263 extern const struct ltc_hash_descriptor whirlpool_desc;
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);
291 int sha512_init(hash_state * md);
292 int sha512_process(hash_state * md, const unsigned char *in, unsigned long inlen);
293 int sha512_done(hash_state * md, unsigned char *hash);
294 int sha512_test(void);
295 extern const struct ltc_hash_descriptor sha512_desc;
300 #error LTC_SHA512 is required for LTC_SHA384
302 int sha384_init(hash_state * md);
303 #define sha384_process sha512_process
304 int sha384_done(hash_state * md, unsigned char *hash);
305 int sha384_test(void);
306 extern const struct ltc_hash_descriptor sha384_desc;
309 #ifdef LTC_SHA512_256
311 #error LTC_SHA512 is required for LTC_SHA512_256
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;
320 #ifdef LTC_SHA512_224
322 #error LTC_SHA512 is required for LTC_SHA512_224
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;
332 int sha256_init(hash_state * md);
333 int sha256_process(hash_state * md, const unsigned char *in, unsigned long inlen);
334 int sha256_done(hash_state * md, unsigned char *hash);
335 int sha256_test(void);
336 extern const struct ltc_hash_descriptor sha256_desc;
340 #error LTC_SHA256 is required for LTC_SHA224
342 int sha224_init(hash_state * md);
343 #define sha224_process sha256_process
344 int sha224_done(hash_state * md, unsigned char *hash);
345 int sha224_test(void);
346 extern const struct ltc_hash_descriptor sha224_desc;
351 int sha1_init(hash_state * md);
352 int sha1_process(hash_state * md, const unsigned char *in, unsigned long inlen);
353 int sha1_done(hash_state * md, unsigned char *hash);
355 extern const struct ltc_hash_descriptor sha1_desc;
359 extern const struct ltc_hash_descriptor blake2s_256_desc;
360 int blake2s_256_init(hash_state * md);
361 int blake2s_256_test(void);
363 extern const struct ltc_hash_descriptor blake2s_224_desc;
364 int blake2s_224_init(hash_state * md);
365 int blake2s_224_test(void);
367 extern const struct ltc_hash_descriptor blake2s_160_desc;
368 int blake2s_160_init(hash_state * md);
369 int blake2s_160_test(void);
371 extern const struct ltc_hash_descriptor blake2s_128_desc;
372 int blake2s_128_init(hash_state * md);
373 int blake2s_128_test(void);
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);
381 extern const struct ltc_hash_descriptor blake2b_512_desc;
382 int blake2b_512_init(hash_state * md);
383 int blake2b_512_test(void);
385 extern const struct ltc_hash_descriptor blake2b_384_desc;
386 int blake2b_384_init(hash_state * md);
387 int blake2b_384_test(void);
389 extern const struct ltc_hash_descriptor blake2b_256_desc;
390 int blake2b_256_init(hash_state * md);
391 int blake2b_256_test(void);
393 extern const struct ltc_hash_descriptor blake2b_160_desc;
394 int blake2b_160_init(hash_state * md);
395 int blake2b_160_test(void);
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);
403 int md5_init(hash_state * md);
404 int md5_process(hash_state * md, const unsigned char *in, unsigned long inlen);
405 int md5_done(hash_state * md, unsigned char *hash);
407 extern const struct ltc_hash_descriptor md5_desc;
411 int md4_init(hash_state * md);
412 int md4_process(hash_state * md, const unsigned char *in, unsigned long inlen);
413 int md4_done(hash_state * md, unsigned char *hash);
415 extern const struct ltc_hash_descriptor md4_desc;
419 int md2_init(hash_state * md);
420 int md2_process(hash_state * md, const unsigned char *in, unsigned long inlen);
421 int md2_done(hash_state * md, unsigned char *hash);
423 extern const struct ltc_hash_descriptor md2_desc;
427 int tiger_init(hash_state * md);
428 int tiger_process(hash_state * md, const unsigned char *in, unsigned long inlen);
429 int tiger_done(hash_state * md, unsigned char *hash);
430 int tiger_test(void);
431 extern const struct ltc_hash_descriptor tiger_desc;
435 int rmd128_init(hash_state * md);
436 int rmd128_process(hash_state * md, const unsigned char *in, unsigned long inlen);
437 int rmd128_done(hash_state * md, unsigned char *hash);
438 int rmd128_test(void);
439 extern const struct ltc_hash_descriptor rmd128_desc;
443 int rmd160_init(hash_state * md);
444 int rmd160_process(hash_state * md, const unsigned char *in, unsigned long inlen);
445 int rmd160_done(hash_state * md, unsigned char *hash);
446 int rmd160_test(void);
447 extern const struct ltc_hash_descriptor rmd160_desc;
451 int rmd256_init(hash_state * md);
452 int rmd256_process(hash_state * md, const unsigned char *in, unsigned long inlen);
453 int rmd256_done(hash_state * md, unsigned char *hash);
454 int rmd256_test(void);
455 extern const struct ltc_hash_descriptor rmd256_desc;
459 int rmd320_init(hash_state * md);
460 int rmd320_process(hash_state * md, const unsigned char *in, unsigned long inlen);
461 int rmd320_done(hash_state * md, unsigned char *hash);
462 int rmd320_test(void);
463 extern const struct ltc_hash_descriptor rmd320_desc;
467 int find_hash(const char *name);
468 int find_hash_id(unsigned char ID);
469 int find_hash_oid(const unsigned long *ID, unsigned long IDlen);
470 int find_hash_any(const char *name, int digestlen);
471 int register_hash(const struct ltc_hash_descriptor *hash);
472 int unregister_hash(const struct ltc_hash_descriptor *hash);
473 int register_all_hashes(void);
474 int hash_is_valid(int idx);
476 LTC_MUTEX_PROTO(ltc_hash_mutex)
478 int hash_memory(int hash,
479 const unsigned char *in, unsigned long inlen,
480 unsigned char *out, unsigned long *outlen);
481 int hash_memory_multi(int hash, unsigned char *out, unsigned long *outlen,
482 const unsigned char *in, unsigned long inlen, ...);
485 int hash_filehandle(int hash, FILE *in, unsigned char *out, unsigned long *outlen);
486 int hash_file(int hash, const char *fname, unsigned char *out, unsigned long *outlen);
489 /* a simple macro for making hash "process" functions */
490 #define HASH_PROCESS(func_name, compress_name, state_var, block_size) \
491 int func_name (hash_state * md, const unsigned char *in, unsigned long inlen) \
495 LTC_ARGCHK(md != NULL); \
496 LTC_ARGCHK(in != NULL); \
497 if (md-> state_var .curlen > sizeof(md-> state_var .buf)) { \
498 return CRYPT_INVALID_ARG; \
500 if ((md-> state_var .length + inlen) < md-> state_var .length) { \
501 return CRYPT_HASH_OVERFLOW; \
503 while (inlen > 0) { \
504 if (md-> state_var .curlen == 0 && inlen >= block_size) { \
505 if ((err = compress_name (md, (unsigned char *)in)) != CRYPT_OK) { \
508 md-> state_var .length += block_size * 8; \
510 inlen -= block_size; \
512 n = MIN(inlen, (block_size - md-> state_var .curlen)); \
513 XMEMCPY(md-> state_var .buf + md-> state_var.curlen, in, (size_t)n); \
514 md-> state_var .curlen += n; \
517 if (md-> state_var .curlen == block_size) { \
518 if ((err = compress_name (md, md-> state_var .buf)) != CRYPT_OK) { \
521 md-> state_var .length += 8*block_size; \
522 md-> state_var .curlen = 0; \
529 /* ref: $Format:%D$ */
530 /* git commit: $Format:%H$ */
531 /* commit time: $Format:%ai$ */