/src/pycryptodome/src/MD5.c
Line | Count | Source |
1 | | /* =================================================================== |
2 | | * |
3 | | * Copyright (c) 2018, Helder Eijs <helderijs@gmail.com> |
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 | 5.50M | #define F(x, y, z) (((x) & (y)) | ((~x) & (z))) |
43 | 5.50M | #define G(x, y, z) (((x) & (z)) | ((y) & (~z))) |
44 | 5.50M | #define H(x, y, z) ((x) ^ (y) ^ (z)) |
45 | 5.50M | #define I(x, y, z) ((y) ^ ((x) | (~z))) |
46 | | |
47 | 22.0M | #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n)))) |
48 | | |
49 | 5.50M | #define FF(a, b, c, d, x, s, ac) { \ |
50 | 5.50M | (a) += F ((b), (c), (d)) + (x) + (uint32_t)(ac); \ |
51 | 5.50M | (a) = ROTATE_LEFT ((a), (s)); \ |
52 | 5.50M | (a) += (b); \ |
53 | 5.50M | } |
54 | | |
55 | 5.50M | #define GG(a, b, c, d, x, s, ac) { \ |
56 | 5.50M | (a) += G ((b), (c), (d)) + (x) + (uint32_t)(ac); \ |
57 | 5.50M | (a) = ROTATE_LEFT ((a), (s)); \ |
58 | 5.50M | (a) += (b); \ |
59 | 5.50M | } |
60 | | |
61 | 5.50M | #define HH(a, b, c, d, x, s, ac) { \ |
62 | 5.50M | (a) += H ((b), (c), (d)) + (x) + (uint32_t)(ac); \ |
63 | 5.50M | (a) = ROTATE_LEFT ((a), (s)); \ |
64 | 5.50M | (a) += (b); \ |
65 | 5.50M | } |
66 | | |
67 | 5.50M | #define II(a, b, c, d, x, s, ac) { \ |
68 | 5.50M | (a) += I ((b), (c), (d)) + (x) + (uint32_t)(ac); \ |
69 | 5.50M | (a) = ROTATE_LEFT ((a), (s)); \ |
70 | 5.50M | (a) += (b); \ |
71 | 5.50M | } |
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 | 1.03M | #define BLOCK_SIZE 64 |
91 | | |
92 | 0 | #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 | 344k | { |
103 | | /** Maximum message length for MD5 is 2**64 bits **/ |
104 | 344k | hs->totbits += bits; |
105 | 344k | return (hs->totbits < bits) ? ERR_MAX_DATA : 0; |
106 | 344k | } |
107 | | |
108 | | static void md5_compress(hash_state * hs) |
109 | 344k | { |
110 | 344k | uint32_t a, b, c, d; |
111 | 344k | uint32_t x[16]; |
112 | 344k | int i; |
113 | | |
114 | | /** Words flow in in little-endian mode **/ |
115 | 5.85M | for (i=0; i<16; i++) { |
116 | 5.50M | x[i] = LOAD_U32_LITTLE(&hs->buf[i*4]); |
117 | 5.50M | } |
118 | | |
119 | 344k | a = hs->h[0]; |
120 | 344k | b = hs->h[1]; |
121 | 344k | c = hs->h[2]; |
122 | 344k | d = hs->h[3]; |
123 | | |
124 | | /* Round 1 */ |
125 | 344k | FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */ |
126 | 344k | FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */ |
127 | 344k | FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */ |
128 | 344k | FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */ |
129 | 344k | FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */ |
130 | 344k | FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */ |
131 | 344k | FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */ |
132 | 344k | FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */ |
133 | 344k | FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */ |
134 | 344k | FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */ |
135 | 344k | FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */ |
136 | 344k | FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */ |
137 | 344k | FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */ |
138 | 344k | FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */ |
139 | 344k | FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */ |
140 | 344k | FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */ |
141 | | |
142 | | /* Round 2 */ |
143 | 344k | GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */ |
144 | 344k | GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */ |
145 | 344k | GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */ |
146 | 344k | GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */ |
147 | 344k | GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */ |
148 | 344k | GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */ |
149 | 344k | GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */ |
150 | 344k | GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */ |
151 | 344k | GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */ |
152 | 344k | GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */ |
153 | 344k | GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */ |
154 | 344k | GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */ |
155 | 344k | GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */ |
156 | 344k | GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */ |
157 | 344k | GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */ |
158 | 344k | GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */ |
159 | | |
160 | | /* Round 3 */ |
161 | 344k | HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */ |
162 | 344k | HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */ |
163 | 344k | HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */ |
164 | 344k | HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */ |
165 | 344k | HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */ |
166 | 344k | HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */ |
167 | 344k | HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */ |
168 | 344k | HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */ |
169 | 344k | HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */ |
170 | 344k | HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */ |
171 | 344k | HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */ |
172 | 344k | HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */ |
173 | 344k | HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */ |
174 | 344k | HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */ |
175 | 344k | HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */ |
176 | 344k | HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */ |
177 | | |
178 | | /* Round 4 */ |
179 | 344k | II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */ |
180 | 344k | II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */ |
181 | 344k | II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */ |
182 | 344k | II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */ |
183 | 344k | II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */ |
184 | 344k | II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */ |
185 | 344k | II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */ |
186 | 344k | II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */ |
187 | 344k | II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */ |
188 | 344k | II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */ |
189 | 344k | II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */ |
190 | 344k | II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */ |
191 | 344k | II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */ |
192 | 344k | II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */ |
193 | 344k | II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */ |
194 | 344k | II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */ |
195 | | |
196 | | /** compute new intermediate hash **/ |
197 | 344k | hs->h[0] += a; |
198 | 344k | hs->h[1] += b; |
199 | 344k | hs->h[2] += c; |
200 | 344k | hs->h[3] += d; |
201 | 344k | } |
202 | | |
203 | | EXPORT_SYM int MD5_init(hash_state **mdState) |
204 | 289 | { |
205 | 289 | hash_state *hs; |
206 | | |
207 | 289 | if (NULL == mdState) { |
208 | 0 | return ERR_NULL; |
209 | 0 | } |
210 | | |
211 | 289 | *mdState = hs = (hash_state*) calloc(1, sizeof(hash_state)); |
212 | 289 | if (NULL == hs) |
213 | 0 | return ERR_MEMORY; |
214 | | |
215 | 289 | hs->curlen = 0; |
216 | 289 | hs->totbits = 0; |
217 | | |
218 | | /** Initial intermediate hash value **/ |
219 | 289 | hs->h[0] = 0x67452301; |
220 | 289 | hs->h[1] = 0xefcdab89; |
221 | 289 | hs->h[2] = 0x98badcfe; |
222 | 289 | hs->h[3] = 0x10325476; |
223 | | |
224 | 289 | return 0; |
225 | 289 | } |
226 | | |
227 | | EXPORT_SYM int MD5_destroy (hash_state *mdState) |
228 | 289 | { |
229 | 289 | free(mdState); |
230 | 289 | return 0; |
231 | 289 | } |
232 | | |
233 | | EXPORT_SYM int MD5_update(hash_state *hs, const uint8_t *buf, size_t len) |
234 | 1.19k | { |
235 | 1.19k | if (NULL == hs || NULL == buf) { |
236 | 139 | return ERR_NULL; |
237 | 139 | } |
238 | | |
239 | 1.19k | assert(hs->curlen < BLOCK_SIZE); |
240 | | |
241 | 346k | while (len>0) { |
242 | 345k | unsigned left, btc; |
243 | | |
244 | 345k | left = BLOCK_SIZE - hs->curlen; |
245 | 345k | btc = (unsigned)MIN(left, len); |
246 | 345k | memcpy(&hs->buf[hs->curlen], buf, btc); |
247 | 345k | buf += btc; |
248 | 345k | hs->curlen += btc; |
249 | 345k | len -= btc; |
250 | | |
251 | 345k | if (hs->curlen == BLOCK_SIZE) { |
252 | 344k | md5_compress(hs); |
253 | 344k | hs->curlen = 0; |
254 | 344k | if (add_bits(hs, BLOCK_SIZE*8)) { |
255 | 0 | return ERR_MAX_DATA; |
256 | 0 | } |
257 | 344k | } |
258 | 345k | } |
259 | | |
260 | 1.05k | return 0; |
261 | 1.05k | } |
262 | | |
263 | | static int md5_finalize(hash_state *hs, uint8_t *hash /** [DIGEST_SIZE] **/) |
264 | 150 | { |
265 | 150 | unsigned left, i; |
266 | | |
267 | 150 | assert(hs->curlen < BLOCK_SIZE); |
268 | | |
269 | | /* remaining length of the message */ |
270 | 150 | if (add_bits(hs, hs->curlen*8)) { |
271 | 0 | return ERR_MAX_DATA; |
272 | 0 | } |
273 | | |
274 | | /* append the '1' bit */ |
275 | | /* buf[] is guaranteed to have at least 1 byte free */ |
276 | 150 | hs->buf[hs->curlen++] = 0x80; |
277 | | |
278 | | /** if there are less then 64 bits lef, just pad with zeroes and compress **/ |
279 | 150 | left = BLOCK_SIZE - hs->curlen; |
280 | 150 | if (left < 8) { |
281 | 55 | memset(&hs->buf[hs->curlen], 0, left); |
282 | 55 | md5_compress(hs); |
283 | 55 | hs->curlen = 0; |
284 | 55 | } |
285 | | |
286 | | /** |
287 | | * pad with zeroes and close the block with the bit length |
288 | | * encoded as 64-bit integer little endian. |
289 | | **/ |
290 | 150 | left = BLOCK_SIZE - hs->curlen; |
291 | 150 | memset(&hs->buf[hs->curlen], 0, left); |
292 | 150 | STORE_U64_LITTLE(&hs->buf[BLOCK_SIZE-8], hs->totbits); |
293 | | |
294 | | /** compress one last time **/ |
295 | 150 | md5_compress(hs); |
296 | | |
297 | | /** create final hash **/ |
298 | 750 | for (i=0; i<4; i++) { |
299 | 600 | STORE_U32_LITTLE(&hash[i*4], hs->h[i]); |
300 | 600 | } |
301 | | |
302 | 150 | return 0; |
303 | 150 | } |
304 | | |
305 | | EXPORT_SYM int MD5_digest(const hash_state *mdState, uint8_t digest[DIGEST_SIZE]) |
306 | 150 | { |
307 | 150 | hash_state temp; |
308 | | |
309 | 150 | if (NULL == mdState) { |
310 | 0 | return ERR_NULL; |
311 | 0 | } |
312 | | |
313 | 150 | temp = *mdState; |
314 | 150 | md5_finalize(&temp, digest); |
315 | 150 | return 0; |
316 | 150 | } |
317 | | |
318 | | EXPORT_SYM int MD5_copy(const hash_state *src, hash_state *dst) |
319 | 0 | { |
320 | 0 | if (NULL == src || NULL == dst) { |
321 | 0 | return ERR_NULL; |
322 | 0 | } |
323 | | |
324 | 0 | *dst = *src; |
325 | 0 | return 0; |
326 | 0 | } |
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 | 0 | { |
344 | 0 | hash_state inner_temp, outer_temp; |
345 | 0 | size_t i; |
346 | 0 | uint8_t last_hmac[DIGEST_SIZE]; |
347 | |
|
348 | 0 | if (NULL == inner || NULL == outer || NULL == first_hmac || NULL == result) { |
349 | 0 | return ERR_NULL; |
350 | 0 | } |
351 | | |
352 | 0 | if (iterations == 0) { |
353 | 0 | return ERR_NR_ROUNDS; |
354 | 0 | } |
355 | | |
356 | 0 | memcpy(result, first_hmac, DIGEST_SIZE); |
357 | 0 | memcpy(last_hmac, first_hmac, DIGEST_SIZE); |
358 | |
|
359 | 0 | for (i=1; i<iterations; i++) { |
360 | 0 | int j; |
361 | |
|
362 | 0 | inner_temp = *inner; |
363 | 0 | outer_temp = *outer; |
364 | |
|
365 | 0 | MD5_update(&inner_temp, last_hmac, DIGEST_SIZE); |
366 | 0 | md5_finalize(&inner_temp, last_hmac); |
367 | | |
368 | | /** last_hmac is now the intermediate digest **/ |
369 | |
|
370 | 0 | MD5_update(&outer_temp, last_hmac, DIGEST_SIZE); |
371 | 0 | md5_finalize(&outer_temp, last_hmac); |
372 | |
|
373 | 0 | for (j=0; j<DIGEST_SIZE; j++) { |
374 | 0 | result[j] ^= last_hmac[j]; |
375 | 0 | } |
376 | 0 | } |
377 | |
|
378 | 0 | return 0; |
379 | 0 | } |
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 |