xref: /freebsd/crypto/libecc/src/examples/hash/mdc2.h (revision dd21556857e8d40f66bf5ad54754d9d52669ebf7)
1 /*
2  *  Copyright (C) 2021 - This file is part of libecc project
3  *
4  *  Authors:
5  *      Ryad BENADJILA <ryadbenadjila@gmail.com>
6  *      Arnaud EBALARD <arnaud.ebalard@ssi.gouv.fr>
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 #ifndef __MDC2_H__
12 #define __MDC2_H__
13 
14 /* Include libec for useful types and macros */
15 #include <libecc/libec.h>
16 
17 #define MDC2_STATE_SIZE   16
18 #define MDC2_BLOCK_SIZE   8
19 #define MDC2_DIGEST_SIZE  16
20 #define MDC2_DIGEST_SIZE_BITS  128
21 
22 #define MDC2_HASH_MAGIC ((word_t)(0x8296527183648310ULL))
23 #define MDC2_HASH_CHECK_INITIALIZED(A, ret, err) \
24 	MUST_HAVE((((void *)(A)) != NULL) && ((A)->magic == MDC2_HASH_MAGIC), ret, err)
25 
26 /* Padding types as described in the informative appendix of
27  * ISO-IEC-10118-2-1994
28  */
29 typedef enum {
30 	ISOIEC10118_TYPE1 = 0,
31 	ISOIEC10118_TYPE2 = 1,
32 } padding_type;
33 
34 typedef struct {
35 	/* Number of bytes processed */
36 	u64 mdc2_total;
37 	/* Internal state */
38 	u8 mdc2_state[MDC2_STATE_SIZE];
39 	/* Internal buffer to handle updates in a block */
40 	u8 mdc2_buffer[MDC2_BLOCK_SIZE];
41 	/* Initialization magic value */
42 	word_t magic;
43 	/* Padding type, as per ISO-IEC-10118-2-1994 */
44 	padding_type padding;
45 } mdc2_context;
46 
47 ATTRIBUTE_WARN_UNUSED_RET int mdc2_set_padding_type(mdc2_context *ctx,
48 							padding_type p);
49 
50 /* Init hash function. Returns 0 on success, -1 on error. */
51 ATTRIBUTE_WARN_UNUSED_RET int mdc2_init(mdc2_context *ctx);
52 
53 ATTRIBUTE_WARN_UNUSED_RET int mdc2_update(mdc2_context *ctx, const u8 *input, u32 ilen);
54 
55 /* Finalize. Returns 0 on success, -1 on error.*/
56 ATTRIBUTE_WARN_UNUSED_RET int mdc2_final(mdc2_context *ctx, u8 output[MDC2_DIGEST_SIZE]);
57 
58 /*
59  * Scattered version performing init/update/finalize on a vector of buffers
60  * 'inputs' with the length of each buffer passed via 'ilens'. The function
61  * loops on pointers in 'inputs' until it finds a NULL pointer. The function
62  * returns 0 on success, -1 on error.
63  */
64 ATTRIBUTE_WARN_UNUSED_RET int mdc2_scattered(const u8 **inputs, const u32 *ilens,
65 		      u8 output[MDC2_DIGEST_SIZE], padding_type p);
66 
67 /*
68  * Scattered version performing init/update/finalize on a vector of buffers
69  * 'inputs' with the length of each buffer passed via 'ilens'. The function
70  * loops on pointers in 'inputs' until it finds a NULL pointer. The function
71  * returns 0 on success, -1 on error.
72  */
73 ATTRIBUTE_WARN_UNUSED_RET int mdc2_scattered_padding1(const u8 **inputs, const u32 *ilens,
74 		      u8 output[MDC2_DIGEST_SIZE]);
75 
76 /*
77  * Scattered version performing init/update/finalize on a vector of buffers
78  * 'inputs' with the length of each buffer passed via 'ilens'. The function
79  * loops on pointers in 'inputs' until it finds a NULL pointer. The function
80  * returns 0 on success, -1 on error.
81  */
82 ATTRIBUTE_WARN_UNUSED_RET int mdc2_scattered_padding2(const u8 **inputs, const u32 *ilens,
83 		      u8 output[MDC2_DIGEST_SIZE]);
84 /*
85  * Single call version performing init/update/final on given input.
86  * Returns 0 on success, -1 on error.
87  */
88 ATTRIBUTE_WARN_UNUSED_RET int mdc2(const u8 *input, u32 ilen, u8 output[MDC2_DIGEST_SIZE], padding_type p);
89 
90 /*
91  * Single call version performing init/update/final on given input.
92  * Returns 0 on success, -1 on error.
93  */
94 ATTRIBUTE_WARN_UNUSED_RET int mdc2_padding1(const u8 *input, u32 ilen, u8 output[MDC2_DIGEST_SIZE]);
95 
96 /*
97  * Single call version performing init/update/final on given input.
98  * Returns 0 on success, -1 on error.
99  */
100 ATTRIBUTE_WARN_UNUSED_RET int mdc2_padding2(const u8 *input, u32 ilen, u8 output[MDC2_DIGEST_SIZE]);
101 
102 #endif /* __MDC2_H__ */
103