xref: /freebsd/crypto/libecc/src/examples/hash/md4.h (revision f0865ec9906d5a18fa2a3b61381f22ce16e606ad)
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 __MD4_H__
12 
13 /* Include libec for useful types and macros */
14 #include <libecc/libec.h>
15 
16 /****************************************************/
17 /*
18  * 32-bit integer manipulation macros
19  */
20 #ifndef GET_UINT32_BE
21 #define GET_UINT32_BE(n, b, i)			  	\
22 do {						    	\
23 	(n) =     ( ((u32) (b)[(i)    ]) << 24 )   	\
24 		| ( ((u32) (b)[(i) + 1]) << 16 )	\
25 		| ( ((u32) (b)[(i) + 2]) <<  8 )	\
26 		| ( ((u32) (b)[(i) + 3])       );       \
27 } while( 0 )
28 #endif
29 #ifndef GET_UINT32_LE
30 #define GET_UINT32_LE(n, b, i)			 	\
31 do {						    	\
32 	(n) =     ( ((u32) (b)[(i) + 3]) << 24 )   	\
33 		| ( ((u32) (b)[(i) + 2]) << 16 )	\
34 		| ( ((u32) (b)[(i) + 1]) <<  8 )	\
35 		| ( ((u32) (b)[(i)    ])       );       \
36 } while( 0 )
37 #endif
38 
39 
40 #ifndef PUT_UINT32_BE
41 #define PUT_UINT32_BE(n, b, i)		  	\
42 do {					    	\
43 	(b)[(i)    ] = (u8) ( (n) >> 24 );      \
44 	(b)[(i) + 1] = (u8) ( (n) >> 16 );      \
45 	(b)[(i) + 2] = (u8) ( (n) >>  8 );      \
46 	(b)[(i) + 3] = (u8) ( (n)       );      \
47 } while( 0 )
48 #endif
49 
50 #ifndef PUT_UINT32_LE
51 #define PUT_UINT32_LE(n, b, i)		  	\
52 do {					    	\
53 	(b)[(i) + 3] = (u8) ( (n) >> 24 );      \
54 	(b)[(i) + 2] = (u8) ( (n) >> 16 );      \
55 	(b)[(i) + 1] = (u8) ( (n) >>  8 );      \
56 	(b)[(i)    ] = (u8) ( (n)       );      \
57 } while( 0 )
58 #endif
59 
60 /*
61  * 64-bit integer manipulation macros
62  */
63 #ifndef PUT_UINT64_BE
64 #define PUT_UINT64_BE(n,b,i)            \
65 do {                                    \
66     (b)[(i)    ] = (u8) ( (n) >> 56 );  \
67     (b)[(i) + 1] = (u8) ( (n) >> 48 );  \
68     (b)[(i) + 2] = (u8) ( (n) >> 40 );  \
69     (b)[(i) + 3] = (u8) ( (n) >> 32 );  \
70     (b)[(i) + 4] = (u8) ( (n) >> 24 );  \
71     (b)[(i) + 5] = (u8) ( (n) >> 16 );  \
72     (b)[(i) + 6] = (u8) ( (n) >>  8 );  \
73     (b)[(i) + 7] = (u8) ( (n)       );  \
74 } while( 0 )
75 #endif /* PUT_UINT64_BE */
76 
77 #ifndef PUT_UINT64_LE
78 #define PUT_UINT64_LE(n,b,i)            \
79 do {                                    \
80     (b)[(i) + 7] = (u8) ( (n) >> 56 );  \
81     (b)[(i) + 6] = (u8) ( (n) >> 48 );  \
82     (b)[(i) + 5] = (u8) ( (n) >> 40 );  \
83     (b)[(i) + 4] = (u8) ( (n) >> 32 );  \
84     (b)[(i) + 3] = (u8) ( (n) >> 24 );  \
85     (b)[(i) + 2] = (u8) ( (n) >> 16 );  \
86     (b)[(i) + 1] = (u8) ( (n) >>  8 );  \
87     (b)[(i)    ] = (u8) ( (n)       );  \
88 } while( 0 )
89 #endif /* PUT_UINT64_LE */
90 
91 #define MD4_STATE_SIZE   4
92 #define MD4_BLOCK_SIZE   64
93 #define MD4_DIGEST_SIZE  16
94 #define MD4_DIGEST_SIZE_BITS  128
95 
96 #define MD4_HASH_MAGIC ((word_t)(0x4423955132399122ULL))
97 #define MD4_HASH_CHECK_INITIALIZED(A, ret, err) \
98 	MUST_HAVE((((void *)(A)) != NULL) && ((A)->magic == MD4_HASH_MAGIC), ret, err)
99 
100 #define ROTL_MD4(x, n)      ((((u32)(x)) << (n)) | (((u32)(x)) >> (32-(n))))
101 
102 typedef struct {
103 	/* Number of bytes processed */
104 	u64 md4_total;
105 	/* Internal state */
106 	u32 md4_state[MD4_STATE_SIZE];
107 	/* Internal buffer to handle updates in a block */
108 	u8 md4_buffer[MD4_BLOCK_SIZE];
109 	/* Initialization magic value */
110 	word_t magic;
111 } md4_context;
112 
113 /* Init hash function. Returns 0 on success, -1 on error. */
114 ATTRIBUTE_WARN_UNUSED_RET int md4_init(md4_context *ctx);
115 
116 ATTRIBUTE_WARN_UNUSED_RET int md4_update(md4_context *ctx, const u8 *input, u32 ilen);
117 
118 /* Finalize. Returns 0 on success, -1 on error.*/
119 ATTRIBUTE_WARN_UNUSED_RET int md4_final(md4_context *ctx, u8 output[MD4_DIGEST_SIZE]);
120 
121 /*
122  * Scattered version performing init/update/finalize on a vector of buffers
123  * 'inputs' with the length of each buffer passed via 'ilens'. The function
124  * loops on pointers in 'inputs' until it finds a NULL pointer. The function
125  * returns 0 on success, -1 on error.
126  */
127 ATTRIBUTE_WARN_UNUSED_RET int md4_scattered(const u8 **inputs, const u32 *ilens,
128 		      u8 output[MD4_DIGEST_SIZE]);
129 
130 /*
131  * Single call version performing init/update/final on given input.
132  * Returns 0 on success, -1 on error.
133  */
134 ATTRIBUTE_WARN_UNUSED_RET int md4(const u8 *input, u32 ilen, u8 output[MD4_DIGEST_SIZE]);
135 
136 #endif /* __MD4_H__ */
137