xref: /freebsd/crypto/libecc/src/examples/hash/md5.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 "md5.h"
12*f0865ec9SKyle Evans 
13*f0865ec9SKyle Evans /* All the inner MD-5 operations */
14*f0865ec9SKyle Evans static const u32 K_MD5[64] = {
15*f0865ec9SKyle Evans 	0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
16*f0865ec9SKyle Evans 	0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
17*f0865ec9SKyle Evans 	0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
18*f0865ec9SKyle Evans 	0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
19*f0865ec9SKyle Evans 	0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
20*f0865ec9SKyle Evans 	0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
21*f0865ec9SKyle Evans 	0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
22*f0865ec9SKyle Evans 	0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
23*f0865ec9SKyle Evans };
24*f0865ec9SKyle Evans 
25*f0865ec9SKyle Evans static const u8 R_MD5[64] = {
26*f0865ec9SKyle Evans 	7, 12, 17, 22,  7, 12, 17, 22,  7, 12, 17, 22,  7, 12, 17, 22,
27*f0865ec9SKyle Evans 	5,  9, 14, 20,  5,  9, 14, 20,  5,  9, 14, 20,  5,  9, 14, 20,
28*f0865ec9SKyle Evans 	4, 11, 16, 23,  4, 11, 16, 23,  4, 11, 16, 23,  4, 11, 16, 23,
29*f0865ec9SKyle Evans 	6, 10, 15, 21,  6, 10, 15, 21,  6, 10, 15, 21,  6, 10, 15, 21
30*f0865ec9SKyle Evans };
31*f0865ec9SKyle Evans 
32*f0865ec9SKyle Evans #define F_MD5(x, y, z)   (((x) & (y)) | ((~(x)) & (z)))
33*f0865ec9SKyle Evans #define G_MD5(x, y, z)   (((x) & (z)) | ((y) & (~(z))))
34*f0865ec9SKyle Evans #define H_MD5(x, y, z)   ((x) ^ (y) ^ (z))
35*f0865ec9SKyle Evans #define I_MD5(x, y, z)   ((y) ^ ((x) | ((~z))))
36*f0865ec9SKyle Evans 
37*f0865ec9SKyle Evans /* SHA-2 core processing. Returns 0 on success, -1 on error. */
md5_process(md5_context * ctx,const u8 data[MD5_BLOCK_SIZE])38*f0865ec9SKyle Evans ATTRIBUTE_WARN_UNUSED_RET static inline int md5_process(md5_context *ctx,
39*f0865ec9SKyle Evans 			   const u8 data[MD5_BLOCK_SIZE])
40*f0865ec9SKyle Evans {
41*f0865ec9SKyle Evans 	u32 A, B, C, D, tmp;
42*f0865ec9SKyle Evans 	u32 W[16];
43*f0865ec9SKyle Evans 	int ret;
44*f0865ec9SKyle Evans 	unsigned int i;
45*f0865ec9SKyle Evans 
46*f0865ec9SKyle Evans 	MUST_HAVE((data != NULL), ret, err);
47*f0865ec9SKyle Evans 	MD5_HASH_CHECK_INITIALIZED(ctx, ret, err);
48*f0865ec9SKyle Evans 
49*f0865ec9SKyle Evans 	/* Init our inner variables */
50*f0865ec9SKyle Evans 	A = ctx->md5_state[0];
51*f0865ec9SKyle Evans 	B = ctx->md5_state[1];
52*f0865ec9SKyle Evans 	C = ctx->md5_state[2];
53*f0865ec9SKyle Evans 	D = ctx->md5_state[3];
54*f0865ec9SKyle Evans 
55*f0865ec9SKyle Evans 	/* Load data */
56*f0865ec9SKyle Evans 	for (i = 0; i < 16; i++) {
57*f0865ec9SKyle Evans 		GET_UINT32_LE(W[i], data, (4 * i));
58*f0865ec9SKyle Evans 	}
59*f0865ec9SKyle Evans 	for (i = 0; i < 64; i++) {
60*f0865ec9SKyle Evans 		u32 f, g;
61*f0865ec9SKyle Evans 		if(i <= 15){
62*f0865ec9SKyle Evans 			f = F_MD5(B, C, D);
63*f0865ec9SKyle Evans 			g = i;
64*f0865ec9SKyle Evans 		}
65*f0865ec9SKyle Evans 		else if((i >= 16) && (i <= 31)){
66*f0865ec9SKyle Evans 			f = G_MD5(B, C, D);
67*f0865ec9SKyle Evans 			g = (((5 * i) + 1) % 16);
68*f0865ec9SKyle Evans 		}
69*f0865ec9SKyle Evans 		else if((i >= 32) && (i <= 47)){
70*f0865ec9SKyle Evans 			f = H_MD5(B, C, D);
71*f0865ec9SKyle Evans 			g = (((3 * i) + 5) % 16);
72*f0865ec9SKyle Evans 		}
73*f0865ec9SKyle Evans 		else{
74*f0865ec9SKyle Evans 			f = I_MD5(B, C, D);
75*f0865ec9SKyle Evans 			g = ((7 * i) % 16);
76*f0865ec9SKyle Evans 		}
77*f0865ec9SKyle Evans 		tmp = D;
78*f0865ec9SKyle Evans 		D = C;
79*f0865ec9SKyle Evans 		C = B;
80*f0865ec9SKyle Evans 		B += ROTL_MD5((A + f + K_MD5[i] + W[g]), R_MD5[i]);
81*f0865ec9SKyle Evans 		A = tmp;
82*f0865ec9SKyle Evans 	}
83*f0865ec9SKyle Evans 
84*f0865ec9SKyle Evans 	/* Update state */
85*f0865ec9SKyle Evans 	ctx->md5_state[0] += A;
86*f0865ec9SKyle Evans 	ctx->md5_state[1] += B;
87*f0865ec9SKyle Evans 	ctx->md5_state[2] += C;
88*f0865ec9SKyle Evans 	ctx->md5_state[3] += D;
89*f0865ec9SKyle Evans 
90*f0865ec9SKyle Evans 	ret = 0;
91*f0865ec9SKyle Evans 
92*f0865ec9SKyle Evans err:
93*f0865ec9SKyle Evans 	return ret;
94*f0865ec9SKyle Evans }
95*f0865ec9SKyle Evans 
96*f0865ec9SKyle Evans /* Init hash function. Returns 0 on success, -1 on error. */
md5_init(md5_context * ctx)97*f0865ec9SKyle Evans ATTRIBUTE_WARN_UNUSED_RET int md5_init(md5_context *ctx)
98*f0865ec9SKyle Evans {
99*f0865ec9SKyle Evans 	int ret;
100*f0865ec9SKyle Evans 
101*f0865ec9SKyle Evans 	MUST_HAVE((ctx != NULL), ret, err);
102*f0865ec9SKyle Evans 
103*f0865ec9SKyle Evans         /* Sanity check on size */
104*f0865ec9SKyle Evans 	MUST_HAVE((MD5_DIGEST_SIZE <= MAX_DIGEST_SIZE), ret, err);
105*f0865ec9SKyle Evans 
106*f0865ec9SKyle Evans 	ctx->md5_total = 0;
107*f0865ec9SKyle Evans 	ctx->md5_state[0] = 0x67452301;
108*f0865ec9SKyle Evans 	ctx->md5_state[1] = 0xEFCDAB89;
109*f0865ec9SKyle Evans 	ctx->md5_state[2] = 0x98BADCFE;
110*f0865ec9SKyle Evans 	ctx->md5_state[3] = 0x10325476;
111*f0865ec9SKyle Evans 
112*f0865ec9SKyle Evans 	/* Tell that we are initialized */
113*f0865ec9SKyle Evans 	ctx->magic = MD5_HASH_MAGIC;
114*f0865ec9SKyle Evans 
115*f0865ec9SKyle Evans 	ret = 0;
116*f0865ec9SKyle Evans 
117*f0865ec9SKyle Evans err:
118*f0865ec9SKyle Evans 	return ret;
119*f0865ec9SKyle Evans }
120*f0865ec9SKyle Evans 
md5_update(md5_context * ctx,const u8 * input,u32 ilen)121*f0865ec9SKyle Evans ATTRIBUTE_WARN_UNUSED_RET int md5_update(md5_context *ctx, const u8 *input, u32 ilen)
122*f0865ec9SKyle Evans {
123*f0865ec9SKyle Evans 	const u8 *data_ptr = input;
124*f0865ec9SKyle Evans 	u32 remain_ilen = ilen;
125*f0865ec9SKyle Evans 	u16 fill;
126*f0865ec9SKyle Evans 	u8 left;
127*f0865ec9SKyle Evans 	int ret;
128*f0865ec9SKyle Evans 
129*f0865ec9SKyle Evans 	MUST_HAVE((input != NULL) || (ilen == 0), ret, err);
130*f0865ec9SKyle Evans 	MD5_HASH_CHECK_INITIALIZED(ctx, ret, err);
131*f0865ec9SKyle Evans 
132*f0865ec9SKyle Evans 	/* Nothing to process, return */
133*f0865ec9SKyle Evans 	if (ilen == 0) {
134*f0865ec9SKyle Evans 		ret = 0;
135*f0865ec9SKyle Evans 		goto err;
136*f0865ec9SKyle Evans 	}
137*f0865ec9SKyle Evans 
138*f0865ec9SKyle Evans 	/* Get what's left in our local buffer */
139*f0865ec9SKyle Evans 	left = (ctx->md5_total & 0x3F);
140*f0865ec9SKyle Evans 	fill = (u16)(MD5_BLOCK_SIZE - left);
141*f0865ec9SKyle Evans 
142*f0865ec9SKyle Evans 	ctx->md5_total += ilen;
143*f0865ec9SKyle Evans 
144*f0865ec9SKyle Evans 	if ((left > 0) && (remain_ilen >= fill)) {
145*f0865ec9SKyle Evans 		/* Copy data at the end of the buffer */
146*f0865ec9SKyle Evans 		ret = local_memcpy(ctx->md5_buffer + left, data_ptr, fill); EG(ret, err);
147*f0865ec9SKyle Evans 		ret = md5_process(ctx, ctx->md5_buffer); EG(ret, err);
148*f0865ec9SKyle Evans 		data_ptr += fill;
149*f0865ec9SKyle Evans 		remain_ilen -= fill;
150*f0865ec9SKyle Evans 		left = 0;
151*f0865ec9SKyle Evans 	}
152*f0865ec9SKyle Evans 
153*f0865ec9SKyle Evans 	while (remain_ilen >= MD5_BLOCK_SIZE) {
154*f0865ec9SKyle Evans 		ret = md5_process(ctx, data_ptr); EG(ret, err);
155*f0865ec9SKyle Evans 		data_ptr += MD5_BLOCK_SIZE;
156*f0865ec9SKyle Evans 		remain_ilen -= MD5_BLOCK_SIZE;
157*f0865ec9SKyle Evans 	}
158*f0865ec9SKyle Evans 
159*f0865ec9SKyle Evans 	if (remain_ilen > 0) {
160*f0865ec9SKyle Evans 		ret = local_memcpy(ctx->md5_buffer + left, data_ptr, remain_ilen); EG(ret, err);
161*f0865ec9SKyle Evans 	}
162*f0865ec9SKyle Evans 
163*f0865ec9SKyle Evans 	ret = 0;
164*f0865ec9SKyle Evans 
165*f0865ec9SKyle Evans err:
166*f0865ec9SKyle Evans 	return ret;
167*f0865ec9SKyle Evans }
168*f0865ec9SKyle Evans 
169*f0865ec9SKyle Evans /* Finalize. Returns 0 on success, -1 on error.*/
md5_final(md5_context * ctx,u8 output[MD5_DIGEST_SIZE])170*f0865ec9SKyle Evans ATTRIBUTE_WARN_UNUSED_RET int md5_final(md5_context *ctx, u8 output[MD5_DIGEST_SIZE])
171*f0865ec9SKyle Evans {
172*f0865ec9SKyle Evans 	unsigned int block_present = 0;
173*f0865ec9SKyle Evans 	u8 last_padded_block[2 * MD5_BLOCK_SIZE];
174*f0865ec9SKyle Evans 	int ret;
175*f0865ec9SKyle Evans 
176*f0865ec9SKyle Evans 	MUST_HAVE((output != NULL), ret, err);
177*f0865ec9SKyle Evans 	MD5_HASH_CHECK_INITIALIZED(ctx, ret, err);
178*f0865ec9SKyle Evans 
179*f0865ec9SKyle Evans 	/* Fill in our last block with zeroes */
180*f0865ec9SKyle Evans 	ret = local_memset(last_padded_block, 0, sizeof(last_padded_block)); EG(ret, err);
181*f0865ec9SKyle Evans 
182*f0865ec9SKyle Evans 	/* This is our final step, so we proceed with the padding */
183*f0865ec9SKyle Evans 	block_present = ctx->md5_total % MD5_BLOCK_SIZE;
184*f0865ec9SKyle Evans 	if (block_present != 0) {
185*f0865ec9SKyle Evans 		/* Copy what's left in our temporary context buffer */
186*f0865ec9SKyle Evans 		ret = local_memcpy(last_padded_block, ctx->md5_buffer,
187*f0865ec9SKyle Evans 			     block_present); EG(ret, err);
188*f0865ec9SKyle Evans 	}
189*f0865ec9SKyle Evans 
190*f0865ec9SKyle Evans 	/* Put the 0x80 byte, beginning of padding  */
191*f0865ec9SKyle Evans 	last_padded_block[block_present] = 0x80;
192*f0865ec9SKyle Evans 
193*f0865ec9SKyle Evans 	/* Handle possible additional block */
194*f0865ec9SKyle Evans 	if (block_present > (MD5_BLOCK_SIZE - 1 - sizeof(u64))) {
195*f0865ec9SKyle Evans 		/* We need an additional block */
196*f0865ec9SKyle Evans 		PUT_UINT64_LE(8 * ctx->md5_total, last_padded_block,
197*f0865ec9SKyle Evans 			      (2 * MD5_BLOCK_SIZE) - sizeof(u64));
198*f0865ec9SKyle Evans 		ret = md5_process(ctx, last_padded_block); EG(ret, err);
199*f0865ec9SKyle Evans 		ret = md5_process(ctx, last_padded_block + MD5_BLOCK_SIZE); EG(ret, err);
200*f0865ec9SKyle Evans 	} else {
201*f0865ec9SKyle Evans 		/* We do not need an additional block */
202*f0865ec9SKyle Evans 		PUT_UINT64_LE(8 * ctx->md5_total, last_padded_block,
203*f0865ec9SKyle Evans 			      MD5_BLOCK_SIZE - sizeof(u64));
204*f0865ec9SKyle Evans 		ret = md5_process(ctx, last_padded_block); EG(ret, err);
205*f0865ec9SKyle Evans 	}
206*f0865ec9SKyle Evans 
207*f0865ec9SKyle Evans 	/* Output the hash result */
208*f0865ec9SKyle Evans 	PUT_UINT32_LE(ctx->md5_state[0], output, 0);
209*f0865ec9SKyle Evans 	PUT_UINT32_LE(ctx->md5_state[1], output, 4);
210*f0865ec9SKyle Evans 	PUT_UINT32_LE(ctx->md5_state[2], output, 8);
211*f0865ec9SKyle Evans 	PUT_UINT32_LE(ctx->md5_state[3], output, 12);
212*f0865ec9SKyle Evans 
213*f0865ec9SKyle Evans 	/* Tell that we are uninitialized */
214*f0865ec9SKyle Evans 	ctx->magic = WORD(0);
215*f0865ec9SKyle Evans 
216*f0865ec9SKyle Evans 	ret = 0;
217*f0865ec9SKyle Evans 
218*f0865ec9SKyle Evans err:
219*f0865ec9SKyle Evans 	return ret;
220*f0865ec9SKyle Evans }
221*f0865ec9SKyle Evans 
222*f0865ec9SKyle Evans 
223*f0865ec9SKyle Evans /*
224*f0865ec9SKyle Evans  * Scattered version performing init/update/finalize on a vector of buffers
225*f0865ec9SKyle Evans  * 'inputs' with the length of each buffer passed via 'ilens'. The function
226*f0865ec9SKyle Evans  * loops on pointers in 'inputs' until it finds a NULL pointer. The function
227*f0865ec9SKyle Evans  * returns 0 on success, -1 on error.
228*f0865ec9SKyle Evans  */
md5_scattered(const u8 ** inputs,const u32 * ilens,u8 output[MD5_DIGEST_SIZE])229*f0865ec9SKyle Evans ATTRIBUTE_WARN_UNUSED_RET int md5_scattered(const u8 **inputs, const u32 *ilens,
230*f0865ec9SKyle Evans 		      u8 output[MD5_DIGEST_SIZE])
231*f0865ec9SKyle Evans {
232*f0865ec9SKyle Evans 	md5_context ctx;
233*f0865ec9SKyle Evans 	int ret, pos = 0;
234*f0865ec9SKyle Evans 
235*f0865ec9SKyle Evans 	MUST_HAVE((inputs != NULL) && (ilens != NULL) && (output != NULL), ret, err);
236*f0865ec9SKyle Evans 
237*f0865ec9SKyle Evans 	ret = md5_init(&ctx); EG(ret, err);
238*f0865ec9SKyle Evans 
239*f0865ec9SKyle Evans 	while (inputs[pos] != NULL) {
240*f0865ec9SKyle Evans 		ret = md5_update(&ctx, inputs[pos], ilens[pos]); EG(ret, err);
241*f0865ec9SKyle Evans 		pos += 1;
242*f0865ec9SKyle Evans 	}
243*f0865ec9SKyle Evans 
244*f0865ec9SKyle Evans 	ret = md5_final(&ctx, output);
245*f0865ec9SKyle Evans 
246*f0865ec9SKyle Evans err:
247*f0865ec9SKyle Evans 	return ret;
248*f0865ec9SKyle Evans }
249*f0865ec9SKyle Evans 
250*f0865ec9SKyle Evans /*
251*f0865ec9SKyle Evans  * Single call version performing init/update/final on given input.
252*f0865ec9SKyle Evans  * Returns 0 on success, -1 on error.
253*f0865ec9SKyle Evans  */
md5(const u8 * input,u32 ilen,u8 output[MD5_DIGEST_SIZE])254*f0865ec9SKyle Evans ATTRIBUTE_WARN_UNUSED_RET int md5(const u8 *input, u32 ilen, u8 output[MD5_DIGEST_SIZE])
255*f0865ec9SKyle Evans {
256*f0865ec9SKyle Evans 	md5_context ctx;
257*f0865ec9SKyle Evans 	int ret;
258*f0865ec9SKyle Evans 
259*f0865ec9SKyle Evans 	ret = md5_init(&ctx); EG(ret, err);
260*f0865ec9SKyle Evans 	ret = md5_update(&ctx, input, ilen); EG(ret, err);
261*f0865ec9SKyle Evans 	ret = md5_final(&ctx, output);
262*f0865ec9SKyle Evans 
263*f0865ec9SKyle Evans err:
264*f0865ec9SKyle Evans 	return ret;
265*f0865ec9SKyle Evans }
266