md5.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /* Src: https://github.com/Zunawe/md5-c, commit: f3529b6
  2. * License: Unlicense */
  3. /*
  4. * Derived from the RSA Data Security, Inc. MD5 Message-Digest Algorithm
  5. * and modified slightly to be functionally identical but condensed into control structures.
  6. */
  7. #include "md5.h"
  8. /*
  9. * Constants defined by the MD5 algorithm
  10. */
  11. #define A 0x67452301
  12. #define B 0xefcdab89
  13. #define C 0x98badcfe
  14. #define D 0x10325476
  15. static uint32_t S[] = {7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
  16. 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
  17. 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
  18. 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21};
  19. static uint32_t K[] = {0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
  20. 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
  21. 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
  22. 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
  23. 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
  24. 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
  25. 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
  26. 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
  27. 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
  28. 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
  29. 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05,
  30. 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
  31. 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
  32. 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
  33. 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
  34. 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391};
  35. /*
  36. * Padding used to make the size (in bits) of the input congruent to 448 mod 512
  37. */
  38. static uint8_t PADDING[] = {0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  39. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  40. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  41. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  42. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  43. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  44. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  45. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  46. /*
  47. * Bit-manipulation functions defined by the MD5 algorithm
  48. */
  49. #define F(X, Y, Z) ((X & Y) | (~X & Z))
  50. #define G(X, Y, Z) ((X & Z) | (Y & ~Z))
  51. #define H(X, Y, Z) (X ^ Y ^ Z)
  52. #define I(X, Y, Z) (Y ^ (X | ~Z))
  53. /*
  54. * Rotates a 32-bit word left by n bits
  55. */
  56. uint32_t rotateLeft(uint32_t x, uint32_t n){
  57. return (x << n) | (x >> (32 - n));
  58. }
  59. /*
  60. * Initialize a context
  61. */
  62. void md5Init(MD5Context *ctx){
  63. ctx->size = (uint64_t)0;
  64. ctx->buffer[0] = (uint32_t)A;
  65. ctx->buffer[1] = (uint32_t)B;
  66. ctx->buffer[2] = (uint32_t)C;
  67. ctx->buffer[3] = (uint32_t)D;
  68. }
  69. /*
  70. * Add some amount of input to the context
  71. *
  72. * If the input fills out a block of 512 bits, apply the algorithm (md5Step)
  73. * and save the result in the buffer. Also updates the overall size.
  74. */
  75. void md5Update(MD5Context *ctx, uint8_t *input_buffer, size_t input_len){
  76. uint32_t input[16];
  77. unsigned int offset = ctx->size % 64;
  78. ctx->size += (uint64_t)input_len;
  79. // Copy each byte in input_buffer into the next space in our context input
  80. for(unsigned int i = 0; i < input_len; ++i){
  81. ctx->input[offset++] = (uint8_t)*(input_buffer + i);
  82. // If we've filled our context input, copy it into our local array input
  83. // then reset the offset to 0 and fill in a new buffer.
  84. // Every time we fill out a chunk, we run it through the algorithm
  85. // to enable some back and forth between cpu and i/o
  86. if(offset % 64 == 0){
  87. for(unsigned int j = 0; j < 16; ++j){
  88. // Convert to little-endian
  89. // The local variable `input` our 512-bit chunk separated into 32-bit words
  90. // we can use in calculations
  91. input[j] = (uint32_t)(ctx->input[(j * 4) + 3]) << 24 |
  92. (uint32_t)(ctx->input[(j * 4) + 2]) << 16 |
  93. (uint32_t)(ctx->input[(j * 4) + 1]) << 8 |
  94. (uint32_t)(ctx->input[(j * 4)]);
  95. }
  96. md5Step(ctx->buffer, input);
  97. offset = 0;
  98. }
  99. }
  100. }
  101. /*
  102. * Pad the current input to get to 448 bytes, append the size in bits to the very end,
  103. * and save the result of the final iteration into digest.
  104. */
  105. void md5Finalize(MD5Context *ctx){
  106. uint32_t input[16];
  107. unsigned int offset = ctx->size % 64;
  108. unsigned int padding_length = offset < 56 ? 56 - offset : (56 + 64) - offset;
  109. // Fill in the padding and undo the changes to size that resulted from the update
  110. md5Update(ctx, PADDING, padding_length);
  111. ctx->size -= (uint64_t)padding_length;
  112. // Do a final update (internal to this function)
  113. // Last two 32-bit words are the two halves of the size (converted from bytes to bits)
  114. for(unsigned int j = 0; j < 14; ++j){
  115. input[j] = (uint32_t)(ctx->input[(j * 4) + 3]) << 24 |
  116. (uint32_t)(ctx->input[(j * 4) + 2]) << 16 |
  117. (uint32_t)(ctx->input[(j * 4) + 1]) << 8 |
  118. (uint32_t)(ctx->input[(j * 4)]);
  119. }
  120. input[14] = (uint32_t)(ctx->size * 8);
  121. input[15] = (uint32_t)((ctx->size * 8) >> 32);
  122. md5Step(ctx->buffer, input);
  123. // Move the result into digest (convert from little-endian)
  124. for(unsigned int i = 0; i < 4; ++i){
  125. ctx->digest[(i * 4) + 0] = (uint8_t)((ctx->buffer[i] & 0x000000FF));
  126. ctx->digest[(i * 4) + 1] = (uint8_t)((ctx->buffer[i] & 0x0000FF00) >> 8);
  127. ctx->digest[(i * 4) + 2] = (uint8_t)((ctx->buffer[i] & 0x00FF0000) >> 16);
  128. ctx->digest[(i * 4) + 3] = (uint8_t)((ctx->buffer[i] & 0xFF000000) >> 24);
  129. }
  130. }
  131. /*
  132. * Step on 512 bits of input with the main MD5 algorithm.
  133. */
  134. void md5Step(uint32_t *buffer, uint32_t *input){
  135. uint32_t AA = buffer[0];
  136. uint32_t BB = buffer[1];
  137. uint32_t CC = buffer[2];
  138. uint32_t DD = buffer[3];
  139. uint32_t E;
  140. unsigned int j;
  141. for(unsigned int i = 0; i < 64; ++i){
  142. switch(i / 16){
  143. case 0:
  144. E = F(BB, CC, DD);
  145. j = i;
  146. break;
  147. case 1:
  148. E = G(BB, CC, DD);
  149. j = ((i * 5) + 1) % 16;
  150. break;
  151. case 2:
  152. E = H(BB, CC, DD);
  153. j = ((i * 3) + 5) % 16;
  154. break;
  155. default:
  156. E = I(BB, CC, DD);
  157. j = (i * 7) % 16;
  158. break;
  159. }
  160. uint32_t temp = DD;
  161. DD = CC;
  162. CC = BB;
  163. BB = BB + rotateLeft(AA + E + K[i] + input[j], S[i]);
  164. AA = temp;
  165. }
  166. buffer[0] += AA;
  167. buffer[1] += BB;
  168. buffer[2] += CC;
  169. buffer[3] += DD;
  170. }
  171. /*
  172. * Functions that run the algorithm on the provided input and put the digest into result.
  173. * result should be able to store 16 bytes.
  174. */
  175. void md5String(char *input, uint8_t *result){
  176. MD5Context ctx;
  177. md5Init(&ctx);
  178. md5Update(&ctx, (uint8_t *)input, strlen(input));
  179. md5Finalize(&ctx);
  180. memcpy(result, ctx.digest, 16);
  181. }
  182. void md5File(FILE *file, uint8_t *result){
  183. void *input_buffer = malloc(1024);
  184. size_t input_size = 0;
  185. MD5Context ctx;
  186. md5Init(&ctx);
  187. while((input_size = fread(input_buffer, 1, 1024, file)) > 0){
  188. md5Update(&ctx, (uint8_t *)input_buffer, input_size);
  189. }
  190. md5Finalize(&ctx);
  191. free(input_buffer);
  192. memcpy(result, ctx.digest, 16);
  193. }