1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
4 /* ---- HASH FUNCTIONS ---- */
5 #if defined(LTC_SHA3) || defined(LTC_KECCAK)
7 ulong64 saved
; /* the portion of the input message that we didn't consume yet */
9 unsigned char sb
[25 * 8]; /* used for storing `ulong64 s[25]` as little-endian bytes */
10 unsigned short byte_index
; /* 0..7--the next byte after the set one (starts from 0; 0--none are buffered) */
11 unsigned short word_index
; /* 0..24--the next word to integrate input (starts from 0) */
12 unsigned short capacity_words
; /* the double size of the hash output in words (e.g. 16 for Keccak 512) */
13 unsigned short xof_flag
;
19 ulong64 length
, state
[8];
21 unsigned char buf
[128];
28 ulong32 state
[8], curlen
;
29 unsigned char buf
[64];
36 ulong32 state
[5], curlen
;
37 unsigned char buf
[64];
44 ulong32 state
[4], curlen
;
45 unsigned char buf
[64];
52 ulong32 state
[4], curlen
;
53 unsigned char buf
[64];
59 ulong64 state
[3], length
;
61 unsigned char buf
[64];
67 unsigned char chksum
[16], X
[48], buf
[16];
75 unsigned char buf
[64];
76 ulong32 curlen
, state
[4];
83 unsigned char buf
[64];
84 ulong32 curlen
, state
[5];
91 unsigned char buf
[64];
92 ulong32 curlen
, state
[8];
99 unsigned char buf
[64];
100 ulong32 curlen
, state
[10];
105 struct whirlpool_state
{
106 ulong64 length
, state
[8];
107 unsigned char buf
[64];
115 unsigned char state
[MAXBLOCKSIZE
], buf
[MAXBLOCKSIZE
];
121 struct blake2s_state
{
125 unsigned char buf
[64];
126 unsigned long curlen
;
127 unsigned long outlen
;
128 unsigned char last_node
;
133 struct blake2b_state
{
137 unsigned char buf
[128];
138 unsigned long curlen
;
139 unsigned long outlen
;
140 unsigned char last_node
;
144 typedef union Hash_state
{
147 struct chc_state chc
;
150 struct whirlpool_state whirlpool
;
152 #if defined(LTC_SHA3) || defined(LTC_KECCAK)
153 struct sha3_state sha3
;
156 struct sha512_state sha512
;
159 struct sha256_state sha256
;
162 struct sha1_state sha1
;
165 struct md5_state md5
;
168 struct md4_state md4
;
171 struct md2_state md2
;
174 struct tiger_state tiger
;
177 struct rmd128_state rmd128
;
180 struct rmd160_state rmd160
;
183 struct rmd256_state rmd256
;
186 struct rmd320_state rmd320
;
189 struct blake2s_state blake2s
;
192 struct blake2b_state blake2b
;
198 /** hash descriptor */
199 extern struct ltc_hash_descriptor
{
204 /** Size of digest in octets */
205 unsigned long hashsize
;
206 /** Input block size in octets */
207 unsigned long blocksize
;
209 unsigned long OID
[16];
210 /** Length of DER encoding */
211 unsigned long OIDlen
;
213 /** Init a hash state
214 @param hash The hash to initialize
215 @return CRYPT_OK if successful
217 int (*init
)(hash_state
*hash
);
218 /** Process a block of data
219 @param hash The hash state
220 @param in The data to hash
221 @param inlen The length of the data (octets)
222 @return CRYPT_OK if successful
224 int (*process
)(hash_state
*hash
, const unsigned char *in
, unsigned long inlen
);
225 /** Produce the digest and store it
226 @param hash The hash state
227 @param out [out] The destination of the digest
228 @return CRYPT_OK if successful
230 int (*done
)(hash_state
*hash
, unsigned char *out
);
232 @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled
236 /* accelerated hmac callback: if you need to-do multiple packets just use the generic hmac_memory and provide a hash callback */
237 int (*hmac_block
)(const unsigned char *key
, unsigned long keylen
,
238 const unsigned char *in
, unsigned long inlen
,
239 unsigned char *out
, unsigned long *outlen
);
244 int chc_register(int cipher
);
245 int chc_init(hash_state
* md
);
246 int chc_process(hash_state
* md
, const unsigned char *in
, unsigned long inlen
);
247 int chc_done(hash_state
* md
, unsigned char *out
);
249 extern const struct ltc_hash_descriptor chc_desc
;
253 int whirlpool_init(hash_state
* md
);
254 int whirlpool_process(hash_state
* md
, const unsigned char *in
, unsigned long inlen
);
255 int whirlpool_done(hash_state
* md
, unsigned char *out
);
256 int whirlpool_test(void);
257 extern const struct ltc_hash_descriptor whirlpool_desc
;
260 #if defined(LTC_SHA3) || defined(LTC_KECCAK)
261 /* sha3_NNN_init are shared by SHA3 and KECCAK */
262 int sha3_512_init(hash_state
* md
);
263 int sha3_384_init(hash_state
* md
);
264 int sha3_256_init(hash_state
* md
);
265 int sha3_224_init(hash_state
* md
);
266 /* sha3_process is the same for all variants of SHA3 + KECCAK */
267 int sha3_process(hash_state
* md
, const unsigned char *in
, unsigned long inlen
);
271 int sha3_512_test(void);
272 extern const struct ltc_hash_descriptor sha3_512_desc
;
273 int sha3_384_test(void);
274 extern const struct ltc_hash_descriptor sha3_384_desc
;
275 int sha3_256_test(void);
276 extern const struct ltc_hash_descriptor sha3_256_desc
;
277 int sha3_224_test(void);
278 extern const struct ltc_hash_descriptor sha3_224_desc
;
279 int sha3_done(hash_state
*md
, unsigned char *out
);
280 /* SHAKE128 + SHAKE256 */
281 int sha3_shake_init(hash_state
*md
, int num
);
282 #define sha3_shake_process(a,b,c) sha3_process(a,b,c)
283 int sha3_shake_done(hash_state
*md
, unsigned char *out
, unsigned long outlen
);
284 int sha3_shake_test(void);
285 int sha3_shake_memory(int num
, const unsigned char *in
, unsigned long inlen
, unsigned char *out
, const unsigned long *outlen
);
289 #define keccak_512_init(a) sha3_512_init(a)
290 #define keccak_384_init(a) sha3_384_init(a)
291 #define keccak_256_init(a) sha3_256_init(a)
292 #define keccak_224_init(a) sha3_224_init(a)
293 #define keccak_process(a,b,c) sha3_process(a,b,c)
294 extern const struct ltc_hash_descriptor keccak_512_desc
;
295 int keccak_512_test(void);
296 extern const struct ltc_hash_descriptor keccak_384_desc
;
297 int keccak_384_test(void);
298 extern const struct ltc_hash_descriptor keccak_256_desc
;
299 int keccak_256_test(void);
300 extern const struct ltc_hash_descriptor keccak_224_desc
;
301 int keccak_224_test(void);
302 int keccak_done(hash_state
*md
, unsigned char *out
);
306 int sha512_init(hash_state
* md
);
307 int sha512_process(hash_state
* md
, const unsigned char *in
, unsigned long inlen
);
308 int sha512_done(hash_state
* md
, unsigned char *out
);
309 int sha512_test(void);
310 extern const struct ltc_hash_descriptor sha512_desc
;
315 #error LTC_SHA512 is required for LTC_SHA384
317 int sha384_init(hash_state
* md
);
318 #define sha384_process sha512_process
319 int sha384_done(hash_state
* md
, unsigned char *out
);
320 int sha384_test(void);
321 extern const struct ltc_hash_descriptor sha384_desc
;
324 #ifdef LTC_SHA512_256
326 #error LTC_SHA512 is required for LTC_SHA512_256
328 int sha512_256_init(hash_state
* md
);
329 #define sha512_256_process sha512_process
330 int sha512_256_done(hash_state
* md
, unsigned char *out
);
331 int sha512_256_test(void);
332 extern const struct ltc_hash_descriptor sha512_256_desc
;
335 #ifdef LTC_SHA512_224
337 #error LTC_SHA512 is required for LTC_SHA512_224
339 int sha512_224_init(hash_state
* md
);
340 #define sha512_224_process sha512_process
341 int sha512_224_done(hash_state
* md
, unsigned char *out
);
342 int sha512_224_test(void);
343 extern const struct ltc_hash_descriptor sha512_224_desc
;
347 int sha256_init(hash_state
* md
);
348 int sha256_process(hash_state
* md
, const unsigned char *in
, unsigned long inlen
);
349 int sha256_done(hash_state
* md
, unsigned char *out
);
350 int sha256_test(void);
351 extern const struct ltc_hash_descriptor sha256_desc
;
355 #error LTC_SHA256 is required for LTC_SHA224
357 int sha224_init(hash_state
* md
);
358 #define sha224_process sha256_process
359 int sha224_done(hash_state
* md
, unsigned char *out
);
360 int sha224_test(void);
361 extern const struct ltc_hash_descriptor sha224_desc
;
366 int sha1_init(hash_state
* md
);
367 int sha1_process(hash_state
* md
, const unsigned char *in
, unsigned long inlen
);
368 int sha1_done(hash_state
* md
, unsigned char *out
);
370 extern const struct ltc_hash_descriptor sha1_desc
;
374 extern const struct ltc_hash_descriptor blake2s_256_desc
;
375 int blake2s_256_init(hash_state
* md
);
376 int blake2s_256_test(void);
378 extern const struct ltc_hash_descriptor blake2s_224_desc
;
379 int blake2s_224_init(hash_state
* md
);
380 int blake2s_224_test(void);
382 extern const struct ltc_hash_descriptor blake2s_160_desc
;
383 int blake2s_160_init(hash_state
* md
);
384 int blake2s_160_test(void);
386 extern const struct ltc_hash_descriptor blake2s_128_desc
;
387 int blake2s_128_init(hash_state
* md
);
388 int blake2s_128_test(void);
390 int blake2s_init(hash_state
* md
, unsigned long outlen
, const unsigned char *key
, unsigned long keylen
);
391 int blake2s_process(hash_state
* md
, const unsigned char *in
, unsigned long inlen
);
392 int blake2s_done(hash_state
* md
, unsigned char *out
);
396 extern const struct ltc_hash_descriptor blake2b_512_desc
;
397 int blake2b_512_init(hash_state
* md
);
398 int blake2b_512_test(void);
400 extern const struct ltc_hash_descriptor blake2b_384_desc
;
401 int blake2b_384_init(hash_state
* md
);
402 int blake2b_384_test(void);
404 extern const struct ltc_hash_descriptor blake2b_256_desc
;
405 int blake2b_256_init(hash_state
* md
);
406 int blake2b_256_test(void);
408 extern const struct ltc_hash_descriptor blake2b_160_desc
;
409 int blake2b_160_init(hash_state
* md
);
410 int blake2b_160_test(void);
412 int blake2b_init(hash_state
* md
, unsigned long outlen
, const unsigned char *key
, unsigned long keylen
);
413 int blake2b_process(hash_state
* md
, const unsigned char *in
, unsigned long inlen
);
414 int blake2b_done(hash_state
* md
, unsigned char *out
);
418 int md5_init(hash_state
* md
);
419 int md5_process(hash_state
* md
, const unsigned char *in
, unsigned long inlen
);
420 int md5_done(hash_state
* md
, unsigned char *out
);
422 extern const struct ltc_hash_descriptor md5_desc
;
426 int md4_init(hash_state
* md
);
427 int md4_process(hash_state
* md
, const unsigned char *in
, unsigned long inlen
);
428 int md4_done(hash_state
* md
, unsigned char *out
);
430 extern const struct ltc_hash_descriptor md4_desc
;
434 int md2_init(hash_state
* md
);
435 int md2_process(hash_state
* md
, const unsigned char *in
, unsigned long inlen
);
436 int md2_done(hash_state
* md
, unsigned char *out
);
438 extern const struct ltc_hash_descriptor md2_desc
;
442 int tiger_init(hash_state
* md
);
443 int tiger_process(hash_state
* md
, const unsigned char *in
, unsigned long inlen
);
444 int tiger_done(hash_state
* md
, unsigned char *out
);
445 int tiger_test(void);
446 extern const struct ltc_hash_descriptor tiger_desc
;
450 int rmd128_init(hash_state
* md
);
451 int rmd128_process(hash_state
* md
, const unsigned char *in
, unsigned long inlen
);
452 int rmd128_done(hash_state
* md
, unsigned char *out
);
453 int rmd128_test(void);
454 extern const struct ltc_hash_descriptor rmd128_desc
;
458 int rmd160_init(hash_state
* md
);
459 int rmd160_process(hash_state
* md
, const unsigned char *in
, unsigned long inlen
);
460 int rmd160_done(hash_state
* md
, unsigned char *out
);
461 int rmd160_test(void);
462 extern const struct ltc_hash_descriptor rmd160_desc
;
466 int rmd256_init(hash_state
* md
);
467 int rmd256_process(hash_state
* md
, const unsigned char *in
, unsigned long inlen
);
468 int rmd256_done(hash_state
* md
, unsigned char *out
);
469 int rmd256_test(void);
470 extern const struct ltc_hash_descriptor rmd256_desc
;
474 int rmd320_init(hash_state
* md
);
475 int rmd320_process(hash_state
* md
, const unsigned char *in
, unsigned long inlen
);
476 int rmd320_done(hash_state
* md
, unsigned char *out
);
477 int rmd320_test(void);
478 extern const struct ltc_hash_descriptor rmd320_desc
;
482 int find_hash(const char *name
);
483 int find_hash_id(unsigned char ID
);
484 int find_hash_oid(const unsigned long *ID
, unsigned long IDlen
);
485 int find_hash_any(const char *name
, int digestlen
);
486 int register_hash(const struct ltc_hash_descriptor
*hash
);
487 int unregister_hash(const struct ltc_hash_descriptor
*hash
);
488 int register_all_hashes(void);
489 int hash_is_valid(int idx
);
491 LTC_MUTEX_PROTO(ltc_hash_mutex
)
493 int hash_memory(int hash
,
494 const unsigned char *in
, unsigned long inlen
,
495 unsigned char *out
, unsigned long *outlen
);
496 int hash_memory_multi(int hash
, unsigned char *out
, unsigned long *outlen
,
497 const unsigned char *in
, unsigned long inlen
, ...);
500 int hash_filehandle(int hash
, FILE *in
, unsigned char *out
, unsigned long *outlen
);
501 int hash_file(int hash
, const char *fname
, unsigned char *out
, unsigned long *outlen
);