xref: /freebsd/crypto/libecc/src/examples/hash/md2.c (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 #include "md2.h"
12*f0865ec9SKyle Evans 
13*f0865ec9SKyle Evans /* All the inner MD-2 operations */
14*f0865ec9SKyle Evans static const u8 PI_SUBST[256] = {
15*f0865ec9SKyle Evans 	41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240, 6,
16*f0865ec9SKyle Evans 	19, 98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188,
17*f0865ec9SKyle Evans 	76, 130, 202, 30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24,
18*f0865ec9SKyle Evans 	138, 23, 229, 18, 190, 78, 196, 214, 218, 158, 222, 73, 160, 251,
19*f0865ec9SKyle Evans 	245, 142, 187, 47, 238, 122, 169, 104, 121, 145, 21, 178, 7, 63,
20*f0865ec9SKyle Evans 	148, 194, 16, 137, 11, 34, 95, 33, 128, 127, 93, 154, 90, 144, 50,
21*f0865ec9SKyle Evans 	39, 53, 62, 204, 231, 191, 247, 151, 3, 255, 25, 48, 179, 72, 165,
22*f0865ec9SKyle Evans 	181, 209, 215, 94, 146, 42, 172, 86, 170, 198, 79, 184, 56, 210,
23*f0865ec9SKyle Evans 	150, 164, 125, 182, 118, 252, 107, 226, 156, 116, 4, 241, 69, 157,
24*f0865ec9SKyle Evans 	112, 89, 100, 113, 135, 32, 134, 91, 207, 101, 230, 45, 168, 2, 27,
25*f0865ec9SKyle Evans 	96, 37, 173, 174, 176, 185, 246, 28, 70, 97, 105, 52, 64, 126, 15,
26*f0865ec9SKyle Evans 	85, 71, 163, 35, 221, 81, 175, 58, 195, 92, 249, 206, 186, 197,
27*f0865ec9SKyle Evans 	234, 38, 44, 83, 13, 110, 133, 40, 132, 9, 211, 223, 205, 244, 65,
28*f0865ec9SKyle Evans 	129, 77, 82, 106, 220, 55, 200, 108, 193, 171, 250, 36, 225, 123,
29*f0865ec9SKyle Evans 	8, 12, 189, 177, 74, 120, 136, 149, 139, 227, 99, 232, 109, 233,
30*f0865ec9SKyle Evans 	203, 213, 254, 59, 0, 29, 57, 242, 239, 183, 14, 102, 88, 208, 228,
31*f0865ec9SKyle Evans 	166, 119, 114, 248, 235, 117, 75, 10, 49, 68, 80, 180, 143, 237,
32*f0865ec9SKyle Evans 	31, 26, 219, 153, 141, 51, 159, 17, 131, 20
33*f0865ec9SKyle Evans };
34*f0865ec9SKyle Evans 
35*f0865ec9SKyle Evans /* MD-2 core processing. Returns 0 on success, -1 on error. */
md2_process(md2_context * ctx,const u8 data[MD2_BLOCK_SIZE])36*f0865ec9SKyle Evans ATTRIBUTE_WARN_UNUSED_RET static inline int md2_process(md2_context *ctx,
37*f0865ec9SKyle Evans 			   const u8 data[MD2_BLOCK_SIZE])
38*f0865ec9SKyle Evans {
39*f0865ec9SKyle Evans 	int ret;
40*f0865ec9SKyle Evans 	unsigned int i, j, t;
41*f0865ec9SKyle Evans 	u8 x[3 * MD2_BLOCK_SIZE];
42*f0865ec9SKyle Evans 
43*f0865ec9SKyle Evans 	MUST_HAVE((data != NULL), ret, err);
44*f0865ec9SKyle Evans 	MD2_HASH_CHECK_INITIALIZED(ctx, ret, err);
45*f0865ec9SKyle Evans 
46*f0865ec9SKyle Evans 	/* Form state, block, state ^ block */
47*f0865ec9SKyle Evans 	ret = local_memcpy(&x[0], ctx->md2_state, MD2_BLOCK_SIZE); EG(ret, err);
48*f0865ec9SKyle Evans 	ret = local_memcpy(&x[MD2_BLOCK_SIZE], data, MD2_BLOCK_SIZE); EG(ret, err);
49*f0865ec9SKyle Evans 	for(i = 0; i < MD2_BLOCK_SIZE; i++){
50*f0865ec9SKyle Evans 		x[(2 * MD2_BLOCK_SIZE) + i] = (ctx->md2_state[i] ^ data[i]);
51*f0865ec9SKyle Evans 	}
52*f0865ec9SKyle Evans 	/* Encrypt the block during 18 rounds */
53*f0865ec9SKyle Evans 	t = 0;
54*f0865ec9SKyle Evans 	for(i = 0; i < 18; i++){
55*f0865ec9SKyle Evans 		for (j = 0; j < (3 * MD2_BLOCK_SIZE); j++){
56*f0865ec9SKyle Evans 			x[j] ^= PI_SUBST[t];
57*f0865ec9SKyle Evans 			t = x[j];
58*f0865ec9SKyle Evans 		}
59*f0865ec9SKyle Evans 		t = (t + i) & 0xff;
60*f0865ec9SKyle Evans 	}
61*f0865ec9SKyle Evans 	/* Save the new state */
62*f0865ec9SKyle Evans 	ret = local_memcpy(ctx->md2_state, &x[0], MD2_BLOCK_SIZE); EG(ret, err);
63*f0865ec9SKyle Evans 	/* Update the checksum */
64*f0865ec9SKyle Evans 	t = ctx->md2_checksum[MD2_BLOCK_SIZE - 1];
65*f0865ec9SKyle Evans 	for(i = 0; i < MD2_BLOCK_SIZE; i++){
66*f0865ec9SKyle Evans 		ctx->md2_checksum[i] ^= PI_SUBST[data[i] ^ t];
67*f0865ec9SKyle Evans 		t = ctx->md2_checksum[i];
68*f0865ec9SKyle Evans 	}
69*f0865ec9SKyle Evans 
70*f0865ec9SKyle Evans 	/* Zeroize x as not needed anymore */
71*f0865ec9SKyle Evans 	ret = local_memset(x, 0, sizeof(x)); EG(ret, err);
72*f0865ec9SKyle Evans 
73*f0865ec9SKyle Evans 	ret = 0;
74*f0865ec9SKyle Evans 
75*f0865ec9SKyle Evans err:
76*f0865ec9SKyle Evans 	return ret;
77*f0865ec9SKyle Evans }
78*f0865ec9SKyle Evans 
79*f0865ec9SKyle Evans /* Init hash function. Returns 0 on success, -1 on error. */
md2_init(md2_context * ctx)80*f0865ec9SKyle Evans ATTRIBUTE_WARN_UNUSED_RET int md2_init(md2_context *ctx)
81*f0865ec9SKyle Evans {
82*f0865ec9SKyle Evans 	int ret;
83*f0865ec9SKyle Evans 
84*f0865ec9SKyle Evans 	MUST_HAVE((ctx != NULL), ret, err);
85*f0865ec9SKyle Evans 
86*f0865ec9SKyle Evans         /* Sanity check on size */
87*f0865ec9SKyle Evans 	MUST_HAVE((MD2_DIGEST_SIZE <= MAX_DIGEST_SIZE), ret, err);
88*f0865ec9SKyle Evans 
89*f0865ec9SKyle Evans 	ctx->md2_total = 0;
90*f0865ec9SKyle Evans 	/* Zeroize the state */
91*f0865ec9SKyle Evans 	ret = local_memset(ctx->md2_state, 0, sizeof(ctx->md2_state)); EG(ret, err);
92*f0865ec9SKyle Evans 	/* Zeroize the checksum */
93*f0865ec9SKyle Evans 	ret = local_memset(ctx->md2_checksum, 0, sizeof(ctx->md2_checksum)); EG(ret, err);
94*f0865ec9SKyle Evans 
95*f0865ec9SKyle Evans 	/* Tell that we are initialized */
96*f0865ec9SKyle Evans 	ctx->magic = MD2_HASH_MAGIC;
97*f0865ec9SKyle Evans 
98*f0865ec9SKyle Evans 	ret = 0;
99*f0865ec9SKyle Evans 
100*f0865ec9SKyle Evans err:
101*f0865ec9SKyle Evans 	return ret;
102*f0865ec9SKyle Evans }
103*f0865ec9SKyle Evans 
md2_update(md2_context * ctx,const u8 * input,u32 ilen)104*f0865ec9SKyle Evans ATTRIBUTE_WARN_UNUSED_RET int md2_update(md2_context *ctx, const u8 *input, u32 ilen)
105*f0865ec9SKyle Evans {
106*f0865ec9SKyle Evans 	const u8 *data_ptr = input;
107*f0865ec9SKyle Evans 	u32 remain_ilen = ilen;
108*f0865ec9SKyle Evans 	u16 fill;
109*f0865ec9SKyle Evans 	u8 left;
110*f0865ec9SKyle Evans 	int ret;
111*f0865ec9SKyle Evans 
112*f0865ec9SKyle Evans 	MUST_HAVE((input != NULL) || (ilen == 0), ret, err);
113*f0865ec9SKyle Evans 	MD2_HASH_CHECK_INITIALIZED(ctx, ret, err);
114*f0865ec9SKyle Evans 
115*f0865ec9SKyle Evans 	/* Nothing to process, return */
116*f0865ec9SKyle Evans 	if (ilen == 0) {
117*f0865ec9SKyle Evans 		ret = 0;
118*f0865ec9SKyle Evans 		goto err;
119*f0865ec9SKyle Evans 	}
120*f0865ec9SKyle Evans 
121*f0865ec9SKyle Evans 	/* Get what's left in our local buffer */
122*f0865ec9SKyle Evans 	left = (ctx->md2_total & 0xF);
123*f0865ec9SKyle Evans 	fill = (u16)(MD2_BLOCK_SIZE - left);
124*f0865ec9SKyle Evans 
125*f0865ec9SKyle Evans 	ctx->md2_total += ilen;
126*f0865ec9SKyle Evans 
127*f0865ec9SKyle Evans 	if ((left > 0) && (remain_ilen >= fill)) {
128*f0865ec9SKyle Evans 		/* Copy data at the end of the buffer */
129*f0865ec9SKyle Evans 		ret = local_memcpy(ctx->md2_buffer + left, data_ptr, fill); EG(ret, err);
130*f0865ec9SKyle Evans 		ret = md2_process(ctx, ctx->md2_buffer); EG(ret, err);
131*f0865ec9SKyle Evans 		data_ptr += fill;
132*f0865ec9SKyle Evans 		remain_ilen -= fill;
133*f0865ec9SKyle Evans 		left = 0;
134*f0865ec9SKyle Evans 	}
135*f0865ec9SKyle Evans 
136*f0865ec9SKyle Evans 	while (remain_ilen >= MD2_BLOCK_SIZE) {
137*f0865ec9SKyle Evans 		ret = md2_process(ctx, data_ptr); EG(ret, err);
138*f0865ec9SKyle Evans 		data_ptr += MD2_BLOCK_SIZE;
139*f0865ec9SKyle Evans 		remain_ilen -= MD2_BLOCK_SIZE;
140*f0865ec9SKyle Evans 	}
141*f0865ec9SKyle Evans 
142*f0865ec9SKyle Evans 	if (remain_ilen > 0) {
143*f0865ec9SKyle Evans 		ret = local_memcpy(ctx->md2_buffer + left, data_ptr, remain_ilen); EG(ret, err);
144*f0865ec9SKyle Evans 	}
145*f0865ec9SKyle Evans 
146*f0865ec9SKyle Evans 	ret = 0;
147*f0865ec9SKyle Evans 
148*f0865ec9SKyle Evans err:
149*f0865ec9SKyle Evans 	return ret;
150*f0865ec9SKyle Evans }
151*f0865ec9SKyle Evans 
152*f0865ec9SKyle Evans /* Finalize. Returns 0 on success, -1 on error.*/
md2_final(md2_context * ctx,u8 output[MD2_DIGEST_SIZE])153*f0865ec9SKyle Evans ATTRIBUTE_WARN_UNUSED_RET int md2_final(md2_context *ctx, u8 output[MD2_DIGEST_SIZE])
154*f0865ec9SKyle Evans {
155*f0865ec9SKyle Evans 	int ret;
156*f0865ec9SKyle Evans 	unsigned int i;
157*f0865ec9SKyle Evans 	u8 pad_byte;
158*f0865ec9SKyle Evans 
159*f0865ec9SKyle Evans 	MUST_HAVE((output != NULL), ret, err);
160*f0865ec9SKyle Evans 	MD2_HASH_CHECK_INITIALIZED(ctx, ret, err);
161*f0865ec9SKyle Evans 
162*f0865ec9SKyle Evans 	/* This is our final step, so we proceed with the padding */
163*f0865ec9SKyle Evans 	pad_byte = (u8)(MD2_BLOCK_SIZE - (ctx->md2_total % MD2_BLOCK_SIZE));
164*f0865ec9SKyle Evans 	for(i = (ctx->md2_total % MD2_BLOCK_SIZE); i < MD2_BLOCK_SIZE; i++){
165*f0865ec9SKyle Evans 		ctx->md2_buffer[i] = pad_byte;
166*f0865ec9SKyle Evans 	}
167*f0865ec9SKyle Evans 	/* And process the block */
168*f0865ec9SKyle Evans 	ret = md2_process(ctx, ctx->md2_buffer); EG(ret, err);
169*f0865ec9SKyle Evans 
170*f0865ec9SKyle Evans 	/* Also process the checksum */
171*f0865ec9SKyle Evans 	ret = md2_process(ctx, ctx->md2_checksum); EG(ret, err);
172*f0865ec9SKyle Evans 
173*f0865ec9SKyle Evans 	/* Output the hash result */
174*f0865ec9SKyle Evans 	ret = local_memcpy(output, ctx->md2_state, MD2_DIGEST_SIZE); EG(ret, err);
175*f0865ec9SKyle Evans 
176*f0865ec9SKyle Evans 	/* Tell that we are uninitialized */
177*f0865ec9SKyle Evans 	ctx->magic = WORD(0);
178*f0865ec9SKyle Evans 
179*f0865ec9SKyle Evans 	ret = 0;
180*f0865ec9SKyle Evans 
181*f0865ec9SKyle Evans err:
182*f0865ec9SKyle Evans 	return ret;
183*f0865ec9SKyle Evans }
184*f0865ec9SKyle Evans 
185*f0865ec9SKyle Evans 
186*f0865ec9SKyle Evans /*
187*f0865ec9SKyle Evans  * Scattered version performing init/update/finalize on a vector of buffers
188*f0865ec9SKyle Evans  * 'inputs' with the length of each buffer passed via 'ilens'. The function
189*f0865ec9SKyle Evans  * loops on pointers in 'inputs' until it finds a NULL pointer. The function
190*f0865ec9SKyle Evans  * returns 0 on success, -1 on error.
191*f0865ec9SKyle Evans  */
md2_scattered(const u8 ** inputs,const u32 * ilens,u8 output[MD2_DIGEST_SIZE])192*f0865ec9SKyle Evans ATTRIBUTE_WARN_UNUSED_RET int md2_scattered(const u8 **inputs, const u32 *ilens,
193*f0865ec9SKyle Evans 		      u8 output[MD2_DIGEST_SIZE])
194*f0865ec9SKyle Evans {
195*f0865ec9SKyle Evans 	md2_context ctx;
196*f0865ec9SKyle Evans 	int ret, pos = 0;
197*f0865ec9SKyle Evans 
198*f0865ec9SKyle Evans 	MUST_HAVE((inputs != NULL) && (ilens != NULL) && (output != NULL), ret, err);
199*f0865ec9SKyle Evans 
200*f0865ec9SKyle Evans 	ret = md2_init(&ctx); EG(ret, err);
201*f0865ec9SKyle Evans 
202*f0865ec9SKyle Evans 	while (inputs[pos] != NULL) {
203*f0865ec9SKyle Evans 		ret = md2_update(&ctx, inputs[pos], ilens[pos]); EG(ret, err);
204*f0865ec9SKyle Evans 		pos += 1;
205*f0865ec9SKyle Evans 	}
206*f0865ec9SKyle Evans 
207*f0865ec9SKyle Evans 	ret = md2_final(&ctx, output);
208*f0865ec9SKyle Evans 
209*f0865ec9SKyle Evans err:
210*f0865ec9SKyle Evans 	return ret;
211*f0865ec9SKyle Evans }
212*f0865ec9SKyle Evans 
213*f0865ec9SKyle Evans /*
214*f0865ec9SKyle Evans  * Single call version performing init/update/final on given input.
215*f0865ec9SKyle Evans  * Returns 0 on success, -1 on error.
216*f0865ec9SKyle Evans  */
md2(const u8 * input,u32 ilen,u8 output[MD2_DIGEST_SIZE])217*f0865ec9SKyle Evans ATTRIBUTE_WARN_UNUSED_RET int md2(const u8 *input, u32 ilen, u8 output[MD2_DIGEST_SIZE])
218*f0865ec9SKyle Evans {
219*f0865ec9SKyle Evans 	md2_context ctx;
220*f0865ec9SKyle Evans 	int ret;
221*f0865ec9SKyle Evans 
222*f0865ec9SKyle Evans 	ret = md2_init(&ctx); EG(ret, err);
223*f0865ec9SKyle Evans 	ret = md2_update(&ctx, input, ilen); EG(ret, err);
224*f0865ec9SKyle Evans 	ret = md2_final(&ctx, output);
225*f0865ec9SKyle Evans 
226*f0865ec9SKyle Evans err:
227*f0865ec9SKyle Evans 	return ret;
228*f0865ec9SKyle Evans }
229