xref: /freebsd/crypto/libecc/src/examples/hash/md2.h (revision f0865ec9906d5a18fa2a3b61381f22ce16e606ad)
1*f0865ec9SKyle Evans /*
2*f0865ec9SKyle Evans  *  Copyright (C) 2021 - This file is part of libecc project
3*f0865ec9SKyle Evans  *
4*f0865ec9SKyle Evans  *  Authors:
5*f0865ec9SKyle Evans  *      Ryad BENADJILA <ryadbenadjila@gmail.com>
6*f0865ec9SKyle Evans  *      Arnaud EBALARD <arnaud.ebalard@ssi.gouv.fr>
7*f0865ec9SKyle Evans  *
8*f0865ec9SKyle Evans  *  This software is licensed under a dual BSD and GPL v2 license.
9*f0865ec9SKyle Evans  *  See LICENSE file at the root folder of the project.
10*f0865ec9SKyle Evans  */
11*f0865ec9SKyle Evans #ifndef __MD2_H__
12*f0865ec9SKyle Evans #define __MD2_H__
13*f0865ec9SKyle Evans 
14*f0865ec9SKyle Evans /* Include libec for useful types and macros */
15*f0865ec9SKyle Evans #include <libecc/libec.h>
16*f0865ec9SKyle Evans 
17*f0865ec9SKyle Evans #define MD2_STATE_SIZE   16
18*f0865ec9SKyle Evans #define MD2_BLOCK_SIZE   16
19*f0865ec9SKyle Evans #define MD2_DIGEST_SIZE  16
20*f0865ec9SKyle Evans #define MD2_DIGEST_SIZE_BITS  128
21*f0865ec9SKyle Evans 
22*f0865ec9SKyle Evans #define MD2_HASH_MAGIC ((word_t)(0x8432927137264770ULL))
23*f0865ec9SKyle Evans #define MD2_HASH_CHECK_INITIALIZED(A, ret, err) \
24*f0865ec9SKyle Evans 	MUST_HAVE((((void *)(A)) != NULL) && ((A)->magic == MD2_HASH_MAGIC), ret, err)
25*f0865ec9SKyle Evans 
26*f0865ec9SKyle Evans typedef struct {
27*f0865ec9SKyle Evans 	/* Number of bytes processed */
28*f0865ec9SKyle Evans 	u64 md2_total;
29*f0865ec9SKyle Evans 	/* Internal state */
30*f0865ec9SKyle Evans 	u8 md2_state[MD2_STATE_SIZE];
31*f0865ec9SKyle Evans 	/* Internal buffer to handle updates in a block */
32*f0865ec9SKyle Evans 	u8 md2_buffer[MD2_BLOCK_SIZE];
33*f0865ec9SKyle Evans 	/* Internal buffer to hold the checksum */
34*f0865ec9SKyle Evans 	u8 md2_checksum[MD2_BLOCK_SIZE];
35*f0865ec9SKyle Evans 	/* Initialization magic value */
36*f0865ec9SKyle Evans 	word_t magic;
37*f0865ec9SKyle Evans } md2_context;
38*f0865ec9SKyle Evans 
39*f0865ec9SKyle Evans 
40*f0865ec9SKyle Evans /* Init hash function. Returns 0 on success, -1 on error. */
41*f0865ec9SKyle Evans ATTRIBUTE_WARN_UNUSED_RET int md2_init(md2_context *ctx);
42*f0865ec9SKyle Evans 
43*f0865ec9SKyle Evans ATTRIBUTE_WARN_UNUSED_RET int md2_update(md2_context *ctx, const u8 *input, u32 ilen);
44*f0865ec9SKyle Evans 
45*f0865ec9SKyle Evans /* Finalize. Returns 0 on success, -1 on error.*/
46*f0865ec9SKyle Evans ATTRIBUTE_WARN_UNUSED_RET int md2_final(md2_context *ctx, u8 output[MD2_DIGEST_SIZE]);
47*f0865ec9SKyle Evans 
48*f0865ec9SKyle Evans /*
49*f0865ec9SKyle Evans  * Scattered version performing init/update/finalize on a vector of buffers
50*f0865ec9SKyle Evans  * 'inputs' with the length of each buffer passed via 'ilens'. The function
51*f0865ec9SKyle Evans  * loops on pointers in 'inputs' until it finds a NULL pointer. The function
52*f0865ec9SKyle Evans  * returns 0 on success, -1 on error.
53*f0865ec9SKyle Evans  */
54*f0865ec9SKyle Evans ATTRIBUTE_WARN_UNUSED_RET int md2_scattered(const u8 **inputs, const u32 *ilens,
55*f0865ec9SKyle Evans 		      u8 output[MD2_DIGEST_SIZE]);
56*f0865ec9SKyle Evans 
57*f0865ec9SKyle Evans /*
58*f0865ec9SKyle Evans  * Single call version performing init/update/final on given input.
59*f0865ec9SKyle Evans  * Returns 0 on success, -1 on error.
60*f0865ec9SKyle Evans  */
61*f0865ec9SKyle Evans ATTRIBUTE_WARN_UNUSED_RET int md2(const u8 *input, u32 ilen, u8 output[MD2_DIGEST_SIZE]);
62*f0865ec9SKyle Evans 
63*f0865ec9SKyle Evans #endif /* __MD2_H__ */
64