md5.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdint.h>
  5. /*
  6. * this file comes from https://github.com/pod32g/MD5/blob/master/md5.c
  7. */
  8. // Constants are the integer part of the sines of integers (in radians) * 2^32.
  9. const uint32_t k[64] = {
  10. 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee ,
  11. 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501 ,
  12. 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be ,
  13. 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821 ,
  14. 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa ,
  15. 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8 ,
  16. 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed ,
  17. 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a ,
  18. 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c ,
  19. 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70 ,
  20. 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05 ,
  21. 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665 ,
  22. 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039 ,
  23. 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1 ,
  24. 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1 ,
  25. 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391 };
  26. // r specifies the per-round shift amounts
  27. const uint32_t r[] = {7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
  28. 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
  29. 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
  30. 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21};
  31. // leftrotate function definition
  32. #define LEFTROTATE(x, c) (((x) << (c)) | ((x) >> (32 - (c))))
  33. void to_bytes(uint32_t val, uint8_t *bytes)
  34. {
  35. bytes[0] = (uint8_t) val;
  36. bytes[1] = (uint8_t) (val >> 8);
  37. bytes[2] = (uint8_t) (val >> 16);
  38. bytes[3] = (uint8_t) (val >> 24);
  39. }
  40. uint32_t to_int32(const uint8_t *bytes)
  41. {
  42. return (uint32_t) bytes[0]
  43. | ((uint32_t) bytes[1] << 8)
  44. | ((uint32_t) bytes[2] << 16)
  45. | ((uint32_t) bytes[3] << 24);
  46. }
  47. void md5(const uint8_t *initial_msg, size_t initial_len, uint8_t *digest) {
  48. // These vars will contain the hash
  49. uint32_t h0, h1, h2, h3;
  50. // Message (to prepare)
  51. uint8_t *msg = NULL;
  52. size_t new_len, offset;
  53. uint32_t w[16];
  54. uint32_t a, b, c, d, i, f, g, temp;
  55. // Initialize variables - simple count in nibbles:
  56. h0 = 0x67452301;
  57. h1 = 0xefcdab89;
  58. h2 = 0x98badcfe;
  59. h3 = 0x10325476;
  60. //Pre-processing:
  61. //append "1" bit to message
  62. //append "0" bits until message length in bits ≡ 448 (mod 512)
  63. //append length mod (2^64) to message
  64. for (new_len = initial_len + 1; new_len % (512/8) != 448/8; new_len++)
  65. ;
  66. uint8_t buf[new_len + 8];
  67. msg = buf;//(uint8_t*)malloc(new_len + 8);
  68. memcpy(msg, initial_msg, initial_len);
  69. msg[initial_len] = 0x80; // append the "1" bit; most significant bit is "first"
  70. for (offset = initial_len + 1; offset < new_len; offset++)
  71. msg[offset] = 0; // append "0" bits
  72. // append the len in bits at the end of the buffer.
  73. to_bytes(initial_len*8, msg + new_len);
  74. // initial_len>>29 == initial_len*8>>32, but avoids overflow.
  75. to_bytes(initial_len>>29, msg + new_len + 4);
  76. // Process the message in successive 512-bit chunks:
  77. //for each 512-bit chunk of message:
  78. for(offset=0; offset<new_len; offset += (512/8)) {
  79. // break chunk into sixteen 32-bit words w[j], 0 ≤ j ≤ 15
  80. for (i = 0; i < 16; i++)
  81. w[i] = to_int32(msg + offset + i*4);
  82. // Initialize hash value for this chunk:
  83. a = h0;
  84. b = h1;
  85. c = h2;
  86. d = h3;
  87. // Main loop:
  88. for(i = 0; i<64; i++) {
  89. if (i < 16) {
  90. f = (b & c) | ((~b) & d);
  91. g = i;
  92. } else if (i < 32) {
  93. f = (d & b) | ((~d) & c);
  94. g = (5*i + 1) % 16;
  95. } else if (i < 48) {
  96. f = b ^ c ^ d;
  97. g = (3*i + 5) % 16;
  98. } else {
  99. f = c ^ (b | (~d));
  100. g = (7*i) % 16;
  101. }
  102. temp = d;
  103. d = c;
  104. c = b;
  105. b = b + LEFTROTATE((a + f + k[i] + w[g]), r[i]);
  106. a = temp;
  107. }
  108. // Add this chunk's hash to result so far:
  109. h0 += a;
  110. h1 += b;
  111. h2 += c;
  112. h3 += d;
  113. }
  114. // cleanup
  115. //free(msg);
  116. //var char digest[16] := h0 append h1 append h2 append h3 //(Output is in little-endian)
  117. to_bytes(h0, digest);
  118. to_bytes(h1, digest + 4);
  119. to_bytes(h2, digest + 8);
  120. to_bytes(h3, digest + 12);
  121. }
  122. /*
  123. int main(int argc, char **argv) {
  124. char *msg;
  125. size_t len;
  126. int i;
  127. uint8_t result[16];
  128. if (argc < 2) {
  129. printf("usage: %s 'string'\n", argv[0]);
  130. return 1;
  131. }
  132. msg = argv[1];
  133. len = strlen(msg);
  134. // benchmark
  135. for (i = 0; i < 1000000; i++) {
  136. md5((uint8_t*)msg, len, result);
  137. }
  138. // display result
  139. for (i = 0; i < 16; i++)
  140. printf("%2.2x", result[i]);
  141. puts("");
  142. return 0;
  143. }
  144. */