66 static const char *
const test[7*2] = {
67 "",
"d41d8cd98f00b204e9800998ecf8427e",
68 "a",
"0cc175b9c0f1b6a831c399e269772661",
69 "abc",
"900150983cd24fb0d6963f7d28e17f72",
70 "message digest",
"f96b697d7cb7938d525a2f31aaf161d0",
71 "abcdefghijklmnopqrstuvwxyz",
"c3fcd3d76192e4007dfb496cca67e13b",
72 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
73 "d174ab98d277d9f5a5611c2c9f419d9f",
74 "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
"57edf4a22be3c955ac49da2e2107b67a" 78 for (i = 0; i < 7*2; i += 2) {
81 char hex_output[16*2 + 1];
86 MD5_Final(digest, &state);
87 printf(
"MD5 (\"%s\") = ", test[i]);
88 for (di = 0; di < 16; ++di)
89 sprintf(hex_output + di * 2,
"%02x", digest[di]);
91 if (strcmp(hex_output, test[i + 1]))
92 printf(
"**** ERROR, should be: %s\n", test[i + 1]);
102 #ifdef COMPUTE_T_VALUES 108 for (i = 1; i <= 64; ++i) {
109 unsigned long v = (
unsigned long)(4294967296.0 * fabs(sin((
double)i)));
116 printf(
"#define T%d /* 0x%08lx */ (T_MASK ^ 0x%08lx)\n", i,
117 v, (
unsigned long)(
unsigned int)(~v));
119 printf(
"#define T%d 0x%08lx\n", i, v);
131 #define T_MASK ((uint32_t)~0) 132 #define T1 (T_MASK ^ 0x28955b87) 133 #define T2 (T_MASK ^ 0x173848a9) 134 #define T3 0x242070db 135 #define T4 (T_MASK ^ 0x3e423111) 136 #define T5 (T_MASK ^ 0x0a83f050) 137 #define T6 0x4787c62a 138 #define T7 (T_MASK ^ 0x57cfb9ec) 139 #define T8 (T_MASK ^ 0x02b96afe) 140 #define T9 0x698098d8 141 #define T10 (T_MASK ^ 0x74bb0850) 142 #define T11 (T_MASK ^ 0x0000a44e) 143 #define T12 (T_MASK ^ 0x76a32841) 144 #define T13 0x6b901122 145 #define T14 (T_MASK ^ 0x02678e6c) 146 #define T15 (T_MASK ^ 0x5986bc71) 147 #define T16 0x49b40821 148 #define T17 (T_MASK ^ 0x09e1da9d) 149 #define T18 (T_MASK ^ 0x3fbf4cbf) 150 #define T19 0x265e5a51 151 #define T20 (T_MASK ^ 0x16493855) 152 #define T21 (T_MASK ^ 0x29d0efa2) 153 #define T22 0x02441453 154 #define T23 (T_MASK ^ 0x275e197e) 155 #define T24 (T_MASK ^ 0x182c0437) 156 #define T25 0x21e1cde6 157 #define T26 (T_MASK ^ 0x3cc8f829) 158 #define T27 (T_MASK ^ 0x0b2af278) 159 #define T28 0x455a14ed 160 #define T29 (T_MASK ^ 0x561c16fa) 161 #define T30 (T_MASK ^ 0x03105c07) 162 #define T31 0x676f02d9 163 #define T32 (T_MASK ^ 0x72d5b375) 164 #define T33 (T_MASK ^ 0x0005c6bd) 165 #define T34 (T_MASK ^ 0x788e097e) 166 #define T35 0x6d9d6122 167 #define T36 (T_MASK ^ 0x021ac7f3) 168 #define T37 (T_MASK ^ 0x5b4115bb) 169 #define T38 0x4bdecfa9 170 #define T39 (T_MASK ^ 0x0944b49f) 171 #define T40 (T_MASK ^ 0x4140438f) 172 #define T41 0x289b7ec6 173 #define T42 (T_MASK ^ 0x155ed805) 174 #define T43 (T_MASK ^ 0x2b10cf7a) 175 #define T44 0x04881d05 176 #define T45 (T_MASK ^ 0x262b2fc6) 177 #define T46 (T_MASK ^ 0x1924661a) 178 #define T47 0x1fa27cf8 179 #define T48 (T_MASK ^ 0x3b53a99a) 180 #define T49 (T_MASK ^ 0x0bd6ddbb) 181 #define T50 0x432aff97 182 #define T51 (T_MASK ^ 0x546bdc58) 183 #define T52 (T_MASK ^ 0x036c5fc6) 184 #define T53 0x655b59c3 185 #define T54 (T_MASK ^ 0x70f3336d) 186 #define T55 (T_MASK ^ 0x00100b82) 187 #define T56 (T_MASK ^ 0x7a7ba22e) 188 #define T57 0x6fa87e4f 189 #define T58 (T_MASK ^ 0x01d3191f) 190 #define T59 (T_MASK ^ 0x5cfebceb) 191 #define T60 0x4e0811a1 192 #define T61 (T_MASK ^ 0x08ac817d) 193 #define T62 (T_MASK ^ 0x42c50dca) 194 #define T63 0x2ad7d2bb 195 #define T64 (T_MASK ^ 0x14792c6e) 206 #ifdef WORDS_BIGENDIAN 216 for (i = 0; i < 16; ++i, xp += 4)
217 X[i] = xp[0] + (xp[1] << 8) + (xp[2] << 16) + (xp[3] << 24);
228 if (!((data - (
const uint8_t *)0) & 3)) {
233 memcpy(xbuf, data, 64);
238 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) 243 #define F(x, y, z) (((x) & (y)) | (~(x) & (z))) 244 #define SET(a, b, c, d, k, s, Ti)\ 245 t = a + F(b,c,d) + X[k] + Ti;\ 246 a = ROTATE_LEFT(t, s) + b 248 SET(a, b, c, d, 0, 7,
T1);
249 SET(d, a, b, c, 1, 12,
T2);
250 SET(c, d, a, b, 2, 17,
T3);
251 SET(b, c, d, a, 3, 22,
T4);
252 SET(a, b, c, d, 4, 7,
T5);
253 SET(d, a, b, c, 5, 12,
T6);
254 SET(c, d, a, b, 6, 17,
T7);
255 SET(b, c, d, a, 7, 22,
T8);
256 SET(a, b, c, d, 8, 7,
T9);
257 SET(d, a, b, c, 9, 12,
T10);
258 SET(c, d, a, b, 10, 17,
T11);
259 SET(b, c, d, a, 11, 22,
T12);
260 SET(a, b, c, d, 12, 7,
T13);
261 SET(d, a, b, c, 13, 12,
T14);
262 SET(c, d, a, b, 14, 17,
T15);
263 SET(b, c, d, a, 15, 22,
T16);
269 #define G(x, y, z) (((x) & (z)) | ((y) & ~(z))) 270 #define SET(a, b, c, d, k, s, Ti)\ 271 t = a + G(b,c,d) + X[k] + Ti;\ 272 a = ROTATE_LEFT(t, s) + b 274 SET(a, b, c, d, 1, 5,
T17);
275 SET(d, a, b, c, 6, 9,
T18);
276 SET(c, d, a, b, 11, 14,
T19);
277 SET(b, c, d, a, 0, 20,
T20);
278 SET(a, b, c, d, 5, 5,
T21);
279 SET(d, a, b, c, 10, 9,
T22);
280 SET(c, d, a, b, 15, 14,
T23);
281 SET(b, c, d, a, 4, 20,
T24);
282 SET(a, b, c, d, 9, 5,
T25);
283 SET(d, a, b, c, 14, 9,
T26);
284 SET(c, d, a, b, 3, 14,
T27);
285 SET(b, c, d, a, 8, 20,
T28);
286 SET(a, b, c, d, 13, 5,
T29);
287 SET(d, a, b, c, 2, 9,
T30);
288 SET(c, d, a, b, 7, 14,
T31);
289 SET(b, c, d, a, 12, 20,
T32);
295 #define H(x, y, z) ((x) ^ (y) ^ (z)) 296 #define SET(a, b, c, d, k, s, Ti)\ 297 t = a + H(b,c,d) + X[k] + Ti;\ 298 a = ROTATE_LEFT(t, s) + b 300 SET(a, b, c, d, 5, 4,
T33);
301 SET(d, a, b, c, 8, 11,
T34);
302 SET(c, d, a, b, 11, 16,
T35);
303 SET(b, c, d, a, 14, 23,
T36);
304 SET(a, b, c, d, 1, 4,
T37);
305 SET(d, a, b, c, 4, 11,
T38);
306 SET(c, d, a, b, 7, 16,
T39);
307 SET(b, c, d, a, 10, 23,
T40);
308 SET(a, b, c, d, 13, 4,
T41);
309 SET(d, a, b, c, 0, 11,
T42);
310 SET(c, d, a, b, 3, 16,
T43);
311 SET(b, c, d, a, 6, 23,
T44);
312 SET(a, b, c, d, 9, 4,
T45);
313 SET(d, a, b, c, 12, 11,
T46);
314 SET(c, d, a, b, 15, 16,
T47);
315 SET(b, c, d, a, 2, 23,
T48);
321 #define I(x, y, z) ((y) ^ ((x) | ~(z))) 322 #define SET(a, b, c, d, k, s, Ti)\ 323 t = a + I(b,c,d) + X[k] + Ti;\ 324 a = ROTATE_LEFT(t, s) + b 326 SET(a, b, c, d, 0, 6,
T49);
327 SET(d, a, b, c, 7, 10,
T50);
328 SET(c, d, a, b, 14, 15,
T51);
329 SET(b, c, d, a, 5, 21,
T52);
330 SET(a, b, c, d, 12, 6,
T53);
331 SET(d, a, b, c, 3, 10,
T54);
332 SET(c, d, a, b, 10, 15,
T55);
333 SET(b, c, d, a, 1, 21,
T56);
334 SET(a, b, c, d, 8, 6,
T57);
335 SET(d, a, b, c, 15, 10,
T58);
336 SET(c, d, a, b, 6, 15,
T59);
337 SET(b, c, d, a, 13, 21,
T60);
338 SET(a, b, c, d, 4, 6,
T61);
339 SET(d, a, b, c, 11, 10,
T62);
340 SET(c, d, a, b, 2, 15,
T63);
341 SET(b, c, d, a, 9, 21,
T64);
357 pms->
state[0] = 0x67452301;
360 pms->
state[3] = 0x10325476;
368 size_t left = nbytes;
369 size_t offset = (pms->
count[0] >> 3) & 63;
376 pms->
count[1] += nbytes >> 29;
377 pms->
count[0] += nbits;
378 if (pms->
count[0] < nbits)
383 size_t copy = (offset + nbytes > 64 ? 64 - offset : nbytes);
385 memcpy(pms->
buffer + offset, p, copy);
386 if (offset + copy < 64)
390 md5_process(pms, pms->
buffer);
394 for (; left >= 64; p += 64, left -= 64)
399 memcpy(pms->
buffer, p, left);
405 static const uint8_t pad[64] = {
406 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
407 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
408 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
409 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
415 for (i = 0; i < 8; ++i)
416 data[i] = (
uint8_t)(pms->
count[i >> 2] >> ((i & 3) << 3));
421 for (i = 0; i < 16; ++i)
422 digest[i] = (
uint8_t)(pms->
state[i >> 2] >> ((i & 3) << 3));
#define SET(a, b, c, d, k, s, Ti)
size_t strlen(const char *)
int MD5_Finish(MD5_CTX *pms, uint8_t *digest)
void MD5_Update(MD5_CTX *pms, const uint8_t *data, size_t nbytes)
int MD5_Init(MD5_CTX *pms)
int main(int argc, char **argv)