xref: /freebsd/crypto/libecc/include/libecc/hash/ripemd160.h (revision f0865ec9906d5a18fa2a3b61381f22ce16e606ad)
1 /*
2  *  Copyright (C) 2021 - This file is part of libecc project
3  *
4  *  Authors:
5  *      Arnaud EBALARD <arnaud.ebalard@ssi.gouv.fr>
6  *      Ryad BENADJILA <ryadbenadjila@gmail.com>
7  *
8  *  This software is licensed under a dual BSD and GPL v2 license.
9  *  See LICENSE file at the root folder of the project.
10  */
11 #include <libecc/lib_ecc_config.h>
12 #ifdef WITH_HASH_RIPEMD160
13 
14 #ifndef __RIPEMD160_H__
15 #define __RIPEMD160_H__
16 
17 #include <libecc/words/words.h>
18 #include <libecc/utils/utils.h>
19 
20 #define RIPEMD160_STATE_SIZE   5
21 #define RIPEMD160_BLOCK_SIZE   64
22 #define RIPEMD160_DIGEST_SIZE  20
23 #define RIPEMD160_DIGEST_SIZE_BITS  160
24 
25 /* Compute max hash digest and block sizes */
26 #ifndef MAX_DIGEST_SIZE
27 #define MAX_DIGEST_SIZE	0
28 #endif
29 #if (MAX_DIGEST_SIZE < RIPEMD160_DIGEST_SIZE)
30 #undef MAX_DIGEST_SIZE
31 #define MAX_DIGEST_SIZE RIPEMD160_DIGEST_SIZE
32 #endif
33 
34 #ifndef MAX_DIGEST_SIZE_BITS
35 #define MAX_DIGEST_SIZE_BITS	0
36 #endif
37 #if (MAX_DIGEST_SIZE_BITS < RIPEMD160_DIGEST_SIZE_BITS)
38 #undef MAX_DIGEST_SIZE_BITS
39 #define MAX_DIGEST_SIZE_BITS RIPEMD160_DIGEST_SIZE_BITS
40 #endif
41 
42 #ifndef MAX_BLOCK_SIZE
43 #define MAX_BLOCK_SIZE  0
44 #endif
45 #if (MAX_BLOCK_SIZE < RIPEMD160_BLOCK_SIZE)
46 #undef MAX_BLOCK_SIZE
47 #define MAX_BLOCK_SIZE RIPEMD160_BLOCK_SIZE
48 #endif
49 
50 #define RIPEMD160_HASH_MAGIC ((word_t)(0x7392018463926719ULL))
51 #define RIPEMD160_HASH_CHECK_INITIALIZED(A, ret, err) \
52 	MUST_HAVE((((void *)(A)) != NULL) && ((A)->magic == RIPEMD160_HASH_MAGIC), ret, err)
53 
54 typedef struct {
55 	/* Number of bytes processed */
56 	u64 ripemd160_total;
57 	/* Internal state */
58 	u32 ripemd160_state[RIPEMD160_STATE_SIZE];
59 	/* Internal buffer to handle updates in a block */
60 	u8 ripemd160_buffer[RIPEMD160_BLOCK_SIZE];
61 	/* Initialization magic value */
62 	word_t magic;
63 } ripemd160_context;
64 
65 ATTRIBUTE_WARN_UNUSED_RET int ripemd160_init(ripemd160_context *ctx);
66 ATTRIBUTE_WARN_UNUSED_RET int ripemd160_update(ripemd160_context *ctx, const u8 *input, u32 ilen);
67 ATTRIBUTE_WARN_UNUSED_RET int ripemd160_final(ripemd160_context *ctx, u8 output[RIPEMD160_DIGEST_SIZE]);
68 ATTRIBUTE_WARN_UNUSED_RET int ripemd160_scattered(const u8 **inputs, const u32 *ilens,
69 		     u8 output[RIPEMD160_DIGEST_SIZE]);
70 ATTRIBUTE_WARN_UNUSED_RET int ripemd160(const u8 *input, u32 ilen, u8 output[RIPEMD160_DIGEST_SIZE]);
71 
72 #endif /* __RIPEMD160_H__ */
73 #endif /* WITH_HASH_RIPEMD160 */
74