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