37 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
44 #if (MD5_SUPPORT == ENABLED)
47 #define F(x, y, z) (((x) & (y)) | (~(x) & (z)))
48 #define G(x, y, z) (((x) & (z)) | ((y) & ~(z)))
49 #define H(x, y, z) ((x) ^ (y) ^ (z))
50 #define I(x, y, z) ((y) ^ ((x) | ~(z)))
52 #define FF(a, b, c, d, x, s, k) a += F(b, c, d) + (x) + (k), a = ROL32(a, s) + (b)
53 #define GG(a, b, c, d, x, s, k) a += G(b, c, d) + (x) + (k), a = ROL32(a, s) + (b)
54 #define HH(a, b, c, d, x, s, k) a += H(b, c, d) + (x) + (k), a = ROL32(a, s) + (b)
55 #define II(a, b, c, d, x, s, k) a += I(b, c, d) + (x) + (k), a = ROL32(a, s) + (b)
58 static const uint8_t padding[64] =
60 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
61 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
62 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
63 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
67 static const uint32_t k[64] =
69 0xD76AA478, 0xE8C7B756, 0x242070DB, 0xC1BDCEEE, 0xF57C0FAF, 0x4787C62A, 0xA8304613, 0xFD469501,
70 0x698098D8, 0x8B44F7AF, 0xFFFF5BB1, 0x895CD7BE, 0x6B901122, 0xFD987193, 0xA679438E, 0x49B40821,
71 0xF61E2562, 0xC040B340, 0x265E5A51, 0xE9B6C7AA, 0xD62F105D, 0x02441453, 0xD8A1E681, 0xE7D3FBC8,
72 0x21E1CDE6, 0xC33707D6, 0xF4D50D87, 0x455A14ED, 0xA9E3E905, 0xFCEFA3F8, 0x676F02D9, 0x8D2A4C8A,
73 0xFFFA3942, 0x8771F681, 0x6D9D6122, 0xFDE5380C, 0xA4BEEA44, 0x4BDECFA9, 0xF6BB4B60, 0xBEBFBC70,
74 0x289B7EC6, 0xEAA127FA, 0xD4EF3085, 0x04881D05, 0xD9D4D039, 0xE6DB99E5, 0x1FA27CF8, 0xC4AC5665,
75 0xF4292244, 0x432AFF97, 0xAB9423A7, 0xFC93A039, 0x655B59C3, 0x8F0CCC92, 0xFFEFF47D, 0x85845DD1,
76 0x6FA87E4F, 0xFE2CE6E0, 0xA3014314, 0x4E0811A1, 0xF7537E82, 0xBD3AF235, 0x2AD7D2BB, 0xEB86D391
80 const uint8_t
MD5_OID[8] = {0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x02, 0x05};
111 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
124 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
139 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
157 context->
h[0] = 0x67452301;
158 context->
h[1] = 0xEFCDAB89;
159 context->
h[2] = 0x98BADCFE;
160 context->
h[3] = 0x10325476;
198 if(context->
size == 64)
225 if(context->
size < 56)
227 paddingSize = 56 - context->
size;
231 paddingSize = 64 + 56 - context->
size;
235 md5Update(context, padding, paddingSize);
238 context->
x[14] =
htole32((uint32_t) totalSize);
239 context->
x[15] =
htole32((uint32_t) (totalSize >> 32));
245 for(i = 0; i < 4; i++)
269 for(i = 0; i < 4; i++)
278 for(i = 0; i < 4; i++)
295 uint32_t
a = context->
h[0];
296 uint32_t
b = context->
h[1];
297 uint32_t
c = context->
h[2];
298 uint32_t d = context->
h[3];
301 uint32_t *
x = context->
x;
304 for(i = 0; i < 16; i++)
310 FF(
a,
b,
c, d,
x[0], 7, k[0]);
311 FF(d,
a,
b,
c,
x[1], 12, k[1]);
312 FF(
c, d,
a,
b,
x[2], 17, k[2]);
313 FF(
b,
c, d,
a,
x[3], 22, k[3]);
314 FF(
a,
b,
c, d,
x[4], 7, k[4]);
315 FF(d,
a,
b,
c,
x[5], 12, k[5]);
316 FF(
c, d,
a,
b,
x[6], 17, k[6]);
317 FF(
b,
c, d,
a,
x[7], 22, k[7]);
318 FF(
a,
b,
c, d,
x[8], 7, k[8]);
319 FF(d,
a,
b,
c,
x[9], 12, k[9]);
320 FF(
c, d,
a,
b,
x[10], 17, k[10]);
321 FF(
b,
c, d,
a,
x[11], 22, k[11]);
322 FF(
a,
b,
c, d,
x[12], 7, k[12]);
323 FF(d,
a,
b,
c,
x[13], 12, k[13]);
324 FF(
c, d,
a,
b,
x[14], 17, k[14]);
325 FF(
b,
c, d,
a,
x[15], 22, k[15]);
328 GG(
a,
b,
c, d,
x[1], 5, k[16]);
329 GG(d,
a,
b,
c,
x[6], 9, k[17]);
330 GG(
c, d,
a,
b,
x[11], 14, k[18]);
331 GG(
b,
c, d,
a,
x[0], 20, k[19]);
332 GG(
a,
b,
c, d,
x[5], 5, k[20]);
333 GG(d,
a,
b,
c,
x[10], 9, k[21]);
334 GG(
c, d,
a,
b,
x[15], 14, k[22]);
335 GG(
b,
c, d,
a,
x[4], 20, k[23]);
336 GG(
a,
b,
c, d,
x[9], 5, k[24]);
337 GG(d,
a,
b,
c,
x[14], 9, k[25]);
338 GG(
c, d,
a,
b,
x[3], 14, k[26]);
339 GG(
b,
c, d,
a,
x[8], 20, k[27]);
340 GG(
a,
b,
c, d,
x[13], 5, k[28]);
341 GG(d,
a,
b,
c,
x[2], 9, k[29]);
342 GG(
c, d,
a,
b,
x[7], 14, k[30]);
343 GG(
b,
c, d,
a,
x[12], 20, k[31]);
346 HH(
a,
b,
c, d,
x[5], 4, k[32]);
347 HH(d,
a,
b,
c,
x[8], 11, k[33]);
348 HH(
c, d,
a,
b,
x[11], 16, k[34]);
349 HH(
b,
c, d,
a,
x[14], 23, k[35]);
350 HH(
a,
b,
c, d,
x[1], 4, k[36]);
351 HH(d,
a,
b,
c,
x[4], 11, k[37]);
352 HH(
c, d,
a,
b,
x[7], 16, k[38]);
353 HH(
b,
c, d,
a,
x[10], 23, k[39]);
354 HH(
a,
b,
c, d,
x[13], 4, k[40]);
355 HH(d,
a,
b,
c,
x[0], 11, k[41]);
356 HH(
c, d,
a,
b,
x[3], 16, k[42]);
357 HH(
b,
c, d,
a,
x[6], 23, k[43]);
358 HH(
a,
b,
c, d,
x[9], 4, k[44]);
359 HH(d,
a,
b,
c,
x[12], 11, k[45]);
360 HH(
c, d,
a,
b,
x[15], 16, k[46]);
361 HH(
b,
c, d,
a,
x[2], 23, k[47]);
364 II(
a,
b,
c, d,
x[0], 6, k[48]);
365 II(d,
a,
b,
c,
x[7], 10, k[49]);
366 II(
c, d,
a,
b,
x[14], 15, k[50]);
367 II(
b,
c, d,
a,
x[5], 21, k[51]);
368 II(
a,
b,
c, d,
x[12], 6, k[52]);
369 II(d,
a,
b,
c,
x[3], 10, k[53]);
370 II(
c, d,
a,
b,
x[10], 15, k[54]);
371 II(
b,
c, d,
a,
x[1], 21, k[55]);
372 II(
a,
b,
c, d,
x[8], 6, k[56]);
373 II(d,
a,
b,
c,
x[15], 10, k[57]);
374 II(
c, d,
a,
b,
x[6], 15, k[58]);
375 II(
b,
c, d,
a,
x[13], 21, k[59]);
376 II(
a,
b,
c, d,
x[4], 6, k[60]);
377 II(d,
a,
b,
c,
x[11], 10, k[61]);
378 II(
c, d,
a,
b,
x[2], 15, k[62]);
379 II(
b,
c, d,
a,
x[9], 21, k[63]);