Debian Codegraph @

src/MD5.c

raw symbols SCIP
1 /* ===================================================================
2 *
3 * Copyright (c) 2018, Helder Eijs <[email protected]>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 * ===================================================================
30 */
31
32 #include <stdio.h>
33 #include "common.h"
34 #include "endianess.h"
35
36 FAKE_INIT(MD5)
37
38 /**
39 * MD5 as defined in RFC1321
40 */
41
42 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
43 #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
44 #define H(x, y, z) ((x) ^ (y) ^ (z))
45 #define I(x, y, z) ((y) ^ ((x) | (~z)))
46
47 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
48
49 #define FF(a, b, c, d, x, s, ac) { \
50 (a) += F ((b), (c), (d)) + (x) + (uint32_t)(ac); \
51 (a) = ROTATE_LEFT ((a), (s)); \
52 (a) += (b); \
53 }
54
55 #define GG(a, b, c, d, x, s, ac) { \
56 (a) += G ((b), (c), (d)) + (x) + (uint32_t)(ac); \
57 (a) = ROTATE_LEFT ((a), (s)); \
58 (a) += (b); \
59 }
60
61 #define HH(a, b, c, d, x, s, ac) { \
62 (a) += H ((b), (c), (d)) + (x) + (uint32_t)(ac); \
63 (a) = ROTATE_LEFT ((a), (s)); \
64 (a) += (b); \
65 }
66
67 #define II(a, b, c, d, x, s, ac) { \
68 (a) += I ((b), (c), (d)) + (x) + (uint32_t)(ac); \
69 (a) = ROTATE_LEFT ((a), (s)); \
70 (a) += (b); \
71 }
72
73 #define S11 7
74 #define S12 12
75 #define S13 17
76 #define S14 22
77 #define S21 5
78 #define S22 9
79 #define S23 14
80 #define S24 20
81 #define S31 4
82 #define S32 11
83 #define S33 16
84 #define S34 23
85 #define S41 6
86 #define S42 10
87 #define S43 15
88 #define S44 21
89
90 #define BLOCK_SIZE 64
91
92 #define DIGEST_SIZE (128/8)
93
94 typedef struct t_hash_state {
95 uint32_t h[4];
96 uint8_t buf[BLOCK_SIZE]; /** 64 bytes == 512 bits == sixteen 32-bit words **/
97 unsigned curlen; /** Useful message bytes in buf[] (leftmost) **/
98 uint64_t totbits; /** Total message length in bits **/
99 } hash_state;
100
101 static int add_bits(hash_state *hs, unsigned bits)
102 {
103 /** Maximum message length for MD5 is 2**64 bits **/
104 hs->totbits += bits;
105 return (hs->totbits < bits) ? ERR_MAX_DATA : 0;
106 }
107
108 static void md5_compress(hash_state * hs)
109 {
110 uint32_t a, b, c, d;
111 uint32_t x[16];
112 int i;
113
114 /** Words flow in in little-endian mode **/
115 for (i=0; i<16; i++) {
116 x[i] = LOAD_U32_LITTLE(&hs->buf[i*4]);
117 }
118
119 a = hs->h[0];
120 b = hs->h[1];
121 c = hs->h[2];
122 d = hs->h[3];
123
124 /* Round 1 */
125 FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
126 FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
127 FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
128 FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
129 FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
130 FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
131 FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
132 FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
133 FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
134 FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
135 FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
136 FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
137 FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
138 FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
139 FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
140 FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
141
142 /* Round 2 */
143 GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
144 GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
145 GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
146 GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
147 GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
148 GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
149 GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
150 GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
151 GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
152 GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
153 GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
154 GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
155 GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
156 GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
157 GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
158 GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
159
160 /* Round 3 */
161 HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
162 HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
163 HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
164 HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
165 HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
166 HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
167 HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
168 HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
169 HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
170 HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
171 HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
172 HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
173 HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
174 HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
175 HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
176 HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
177
178 /* Round 4 */
179 II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
180 II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
181 II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
182 II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
183 II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
184 II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
185 II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
186 II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
187 II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
188 II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
189 II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
190 II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
191 II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
192 II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
193 II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
194 II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
195
196 /** compute new intermediate hash **/
197 hs->h[0] += a;
198 hs->h[1] += b;
199 hs->h[2] += c;
200 hs->h[3] += d;
201 }
202
203 EXPORT_SYM int MD5_init(hash_state **mdState)
204 {
205 hash_state *hs;
206
207 if (NULL == mdState) {
208 return ERR_NULL;
209 }
210
211 *mdState = hs = (hash_state*) calloc(1, sizeof(hash_state));
212 if (NULL == hs)
213 return ERR_MEMORY;
214
215 hs->curlen = 0;
216 hs->totbits = 0;
217
218 /** Initial intermediate hash value **/
219 hs->h[0] = 0x67452301;
220 hs->h[1] = 0xefcdab89;
221 hs->h[2] = 0x98badcfe;
222 hs->h[3] = 0x10325476;
223
224 return 0;
225 }
226
227 EXPORT_SYM int MD5_destroy (hash_state *mdState)
228 {
229 free(mdState);
230 return 0;
231 }
232
233 EXPORT_SYM int MD5_update(hash_state *hs, const uint8_t *buf, size_t len)
234 {
235 if (NULL == hs || NULL == buf) {
236 return ERR_NULL;
237 }
238
239 assert(hs->curlen < BLOCK_SIZE);
240
241 while (len>0) {
242 unsigned left, btc;
243
244 left = BLOCK_SIZE - hs->curlen;
245 btc = (unsigned)MIN(left, len);
246 memcpy(&hs->buf[hs->curlen], buf, btc);
247 buf += btc;
248 hs->curlen += btc;
249 len -= btc;
250
251 if (hs->curlen == BLOCK_SIZE) {
252 md5_compress(hs);
253 hs->curlen = 0;
254 if (add_bits(hs, BLOCK_SIZE*8)) {
255 return ERR_MAX_DATA;
256 }
257 }
258 }
259
260 return 0;
261 }
262
263 static int md5_finalize(hash_state *hs, uint8_t *hash /** [DIGEST_SIZE] **/)
264 {
265 unsigned left, i;
266
267 assert(hs->curlen < BLOCK_SIZE);
268
269 /* remaining length of the message */
270 if (add_bits(hs, hs->curlen*8)) {
271 return ERR_MAX_DATA;
272 }
273
274 /* append the '1' bit */
275 /* buf[] is guaranteed to have at least 1 byte free */
276 hs->buf[hs->curlen++] = 0x80;
277
278 /** if there are less then 64 bits lef, just pad with zeroes and compress **/
279 left = BLOCK_SIZE - hs->curlen;
280 if (left < 8) {
281 memset(&hs->buf[hs->curlen], 0, left);
282 md5_compress(hs);
283 hs->curlen = 0;
284 }
285
286 /**
287 * pad with zeroes and close the block with the bit length
288 * encoded as 64-bit integer little endian.
289 **/
290 left = BLOCK_SIZE - hs->curlen;
291 memset(&hs->buf[hs->curlen], 0, left);
292 STORE_U64_LITTLE(&hs->buf[BLOCK_SIZE-8], hs->totbits);
293
294 /** compress one last time **/
295 md5_compress(hs);
296
297 /** create final hash **/
298 for (i=0; i<4; i++) {
299 STORE_U32_LITTLE(&hash[i*4], hs->h[i]);
300 }
301
302 return 0;
303 }
304
305 EXPORT_SYM int MD5_digest(const hash_state *mdState, uint8_t digest[DIGEST_SIZE])
306 {
307 hash_state temp;
308
309 if (NULL == mdState) {
310 return ERR_NULL;
311 }
312
313 temp = *mdState;
314 md5_finalize(&temp, digest);
315 return 0;
316 }
317
318 EXPORT_SYM int MD5_copy(const hash_state *src, hash_state *dst)
319 {
320 if (NULL == src || NULL == dst) {
321 return ERR_NULL;
322 }
323
324 *dst = *src;
325 return 0;
326 }
327
328 /**
329 * This is a specialized function to efficiently perform the inner loop of PBKDF2-HMAC.
330 *
331 * - inner, the hash after the inner padded secret has been absorbed
332 * - outer, the hash after the outer padded secret has been absorbed
333 * - first_hmac, the output of the first HMAC iteration (with salt and counter)
334 * - result, the XOR of the HMACs from all iterations
335 * - iterations, the total number of PBKDF2 iterations (>0)
336 *
337 * This function does not change the state of either hash.
338 */
339 EXPORT_SYM int MD5_pbkdf2_hmac_assist(const hash_state *inner, const hash_state *outer,
340 const uint8_t first_hmac[DIGEST_SIZE],
341 uint8_t result[DIGEST_SIZE],
342 size_t iterations)
343 {
344 hash_state inner_temp, outer_temp;
345 size_t i;
346 uint8_t last_hmac[DIGEST_SIZE];
347
348 if (NULL == inner || NULL == outer || NULL == first_hmac || NULL == result) {
349 return ERR_NULL;
350 }
351
352 if (iterations == 0) {
353 return ERR_NR_ROUNDS;
354 }
355
356 memcpy(result, first_hmac, DIGEST_SIZE);
357 memcpy(last_hmac, first_hmac, DIGEST_SIZE);
358
359 for (i=1; i<iterations; i++) {
360 int j;
361
362 inner_temp = *inner;
363 outer_temp = *outer;
364
365 MD5_update(&inner_temp, last_hmac, DIGEST_SIZE);
366 md5_finalize(&inner_temp, last_hmac);
367
368 /** last_hmac is now the intermediate digest **/
369
370 MD5_update(&outer_temp, last_hmac, DIGEST_SIZE);
371 md5_finalize(&outer_temp, last_hmac);
372
373 for (j=0; j<DIGEST_SIZE; j++) {
374 result[j] ^= last_hmac[j];
375 }
376 }
377
378 return 0;
379 }
380
381 #ifdef MAIN
382 int main(void)
383 {
384 hash_state *hs;
385 const uint8_t tv[] = "The quick brown fox jumps over the lazy dog";
386 uint8_t result[DIGEST_SIZE];
387 int i;
388
389 MD5_init(&hs);
390 MD5_update(hs, tv, sizeof tv - 1);
391 MD5_digest(hs, result);
392 MD5_destroy(hs);
393
394 for (i=0; i<sizeof result; i++) {
395 printf("%02X", result[i]);
396 }
397 printf("\n");
398
399 MD5_init(&hs);
400 MD5_digest(hs, result);
401 MD5_destroy(hs);
402
403 for (i=0; i<sizeof result; i++) {
404 printf("%02X", result[i]);
405 }
406 printf("\n");
407
408 MD5_init(&hs);
409 for (i=0; i<10000000; i++) {
410 MD5_update(hs, tv, sizeof tv - 1);
411 }
412 MD5_destroy(hs);
413
414 printf("\n");
415 }
416 #endif