md5.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /* md5.c - an implementation of the MD5 algorithm, based on RFC 1321.
  2. *
  3. * Copyright: 2007-2012 Aleksey Kravchenko <rhash.admin@gmail.com>
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  14. * or FITNESS FOR A PARTICULAR PURPOSE. Use this program at your own risk!
  15. */
  16. #include <string.h>
  17. #include "byte_order.h"
  18. #include "md5.h"
  19. /**
  20. * Initialize context before calculaing hash.
  21. *
  22. * @param ctx context to initialize
  23. */
  24. void rhash_md5_init(md5_ctx *ctx)
  25. {
  26. ctx->length = 0;
  27. /* initialize state */
  28. ctx->hash[0] = 0x67452301;
  29. ctx->hash[1] = 0xefcdab89;
  30. ctx->hash[2] = 0x98badcfe;
  31. ctx->hash[3] = 0x10325476;
  32. }
  33. /* First, define four auxiliary functions that each take as input
  34. * three 32-bit words and returns a 32-bit word.*/
  35. /* F(x,y,z) = ((y XOR z) AND x) XOR z - is faster then original version */
  36. #define MD5_F(x, y, z) ((((y) ^ (z)) & (x)) ^ (z))
  37. #define MD5_G(x, y, z) (((x) & (z)) | ((y) & (~z)))
  38. #define MD5_H(x, y, z) ((x) ^ (y) ^ (z))
  39. #define MD5_I(x, y, z) ((y) ^ ((x) | (~z)))
  40. /* transformations for rounds 1, 2, 3, and 4. */
  41. #define MD5_ROUND1(a, b, c, d, x, s, ac) { \
  42. (a) += MD5_F((b), (c), (d)) + (x) + (ac); \
  43. (a) = ROTL32((a), (s)); \
  44. (a) += (b); \
  45. }
  46. #define MD5_ROUND2(a, b, c, d, x, s, ac) { \
  47. (a) += MD5_G((b), (c), (d)) + (x) + (ac); \
  48. (a) = ROTL32((a), (s)); \
  49. (a) += (b); \
  50. }
  51. #define MD5_ROUND3(a, b, c, d, x, s, ac) { \
  52. (a) += MD5_H((b), (c), (d)) + (x) + (ac); \
  53. (a) = ROTL32((a), (s)); \
  54. (a) += (b); \
  55. }
  56. #define MD5_ROUND4(a, b, c, d, x, s, ac) { \
  57. (a) += MD5_I((b), (c), (d)) + (x) + (ac); \
  58. (a) = ROTL32((a), (s)); \
  59. (a) += (b); \
  60. }
  61. /**
  62. * The core transformation. Process a 512-bit block.
  63. * The function has been taken from RFC 1321 with little changes.
  64. *
  65. * @param state algorithm state
  66. * @param x the message block to process
  67. */
  68. static void rhash_md5_process_block(unsigned state[4], const unsigned* x)
  69. {
  70. register unsigned a, b, c, d;
  71. a = state[0];
  72. b = state[1];
  73. c = state[2];
  74. d = state[3];
  75. MD5_ROUND1(a, b, c, d, x[ 0], 7, 0xd76aa478);
  76. MD5_ROUND1(d, a, b, c, x[ 1], 12, 0xe8c7b756);
  77. MD5_ROUND1(c, d, a, b, x[ 2], 17, 0x242070db);
  78. MD5_ROUND1(b, c, d, a, x[ 3], 22, 0xc1bdceee);
  79. MD5_ROUND1(a, b, c, d, x[ 4], 7, 0xf57c0faf);
  80. MD5_ROUND1(d, a, b, c, x[ 5], 12, 0x4787c62a);
  81. MD5_ROUND1(c, d, a, b, x[ 6], 17, 0xa8304613);
  82. MD5_ROUND1(b, c, d, a, x[ 7], 22, 0xfd469501);
  83. MD5_ROUND1(a, b, c, d, x[ 8], 7, 0x698098d8);
  84. MD5_ROUND1(d, a, b, c, x[ 9], 12, 0x8b44f7af);
  85. MD5_ROUND1(c, d, a, b, x[10], 17, 0xffff5bb1);
  86. MD5_ROUND1(b, c, d, a, x[11], 22, 0x895cd7be);
  87. MD5_ROUND1(a, b, c, d, x[12], 7, 0x6b901122);
  88. MD5_ROUND1(d, a, b, c, x[13], 12, 0xfd987193);
  89. MD5_ROUND1(c, d, a, b, x[14], 17, 0xa679438e);
  90. MD5_ROUND1(b, c, d, a, x[15], 22, 0x49b40821);
  91. MD5_ROUND2(a, b, c, d, x[ 1], 5, 0xf61e2562);
  92. MD5_ROUND2(d, a, b, c, x[ 6], 9, 0xc040b340);
  93. MD5_ROUND2(c, d, a, b, x[11], 14, 0x265e5a51);
  94. MD5_ROUND2(b, c, d, a, x[ 0], 20, 0xe9b6c7aa);
  95. MD5_ROUND2(a, b, c, d, x[ 5], 5, 0xd62f105d);
  96. MD5_ROUND2(d, a, b, c, x[10], 9, 0x2441453);
  97. MD5_ROUND2(c, d, a, b, x[15], 14, 0xd8a1e681);
  98. MD5_ROUND2(b, c, d, a, x[ 4], 20, 0xe7d3fbc8);
  99. MD5_ROUND2(a, b, c, d, x[ 9], 5, 0x21e1cde6);
  100. MD5_ROUND2(d, a, b, c, x[14], 9, 0xc33707d6);
  101. MD5_ROUND2(c, d, a, b, x[ 3], 14, 0xf4d50d87);
  102. MD5_ROUND2(b, c, d, a, x[ 8], 20, 0x455a14ed);
  103. MD5_ROUND2(a, b, c, d, x[13], 5, 0xa9e3e905);
  104. MD5_ROUND2(d, a, b, c, x[ 2], 9, 0xfcefa3f8);
  105. MD5_ROUND2(c, d, a, b, x[ 7], 14, 0x676f02d9);
  106. MD5_ROUND2(b, c, d, a, x[12], 20, 0x8d2a4c8a);
  107. MD5_ROUND3(a, b, c, d, x[ 5], 4, 0xfffa3942);
  108. MD5_ROUND3(d, a, b, c, x[ 8], 11, 0x8771f681);
  109. MD5_ROUND3(c, d, a, b, x[11], 16, 0x6d9d6122);
  110. MD5_ROUND3(b, c, d, a, x[14], 23, 0xfde5380c);
  111. MD5_ROUND3(a, b, c, d, x[ 1], 4, 0xa4beea44);
  112. MD5_ROUND3(d, a, b, c, x[ 4], 11, 0x4bdecfa9);
  113. MD5_ROUND3(c, d, a, b, x[ 7], 16, 0xf6bb4b60);
  114. MD5_ROUND3(b, c, d, a, x[10], 23, 0xbebfbc70);
  115. MD5_ROUND3(a, b, c, d, x[13], 4, 0x289b7ec6);
  116. MD5_ROUND3(d, a, b, c, x[ 0], 11, 0xeaa127fa);
  117. MD5_ROUND3(c, d, a, b, x[ 3], 16, 0xd4ef3085);
  118. MD5_ROUND3(b, c, d, a, x[ 6], 23, 0x4881d05);
  119. MD5_ROUND3(a, b, c, d, x[ 9], 4, 0xd9d4d039);
  120. MD5_ROUND3(d, a, b, c, x[12], 11, 0xe6db99e5);
  121. MD5_ROUND3(c, d, a, b, x[15], 16, 0x1fa27cf8);
  122. MD5_ROUND3(b, c, d, a, x[ 2], 23, 0xc4ac5665);
  123. MD5_ROUND4(a, b, c, d, x[ 0], 6, 0xf4292244);
  124. MD5_ROUND4(d, a, b, c, x[ 7], 10, 0x432aff97);
  125. MD5_ROUND4(c, d, a, b, x[14], 15, 0xab9423a7);
  126. MD5_ROUND4(b, c, d, a, x[ 5], 21, 0xfc93a039);
  127. MD5_ROUND4(a, b, c, d, x[12], 6, 0x655b59c3);
  128. MD5_ROUND4(d, a, b, c, x[ 3], 10, 0x8f0ccc92);
  129. MD5_ROUND4(c, d, a, b, x[10], 15, 0xffeff47d);
  130. MD5_ROUND4(b, c, d, a, x[ 1], 21, 0x85845dd1);
  131. MD5_ROUND4(a, b, c, d, x[ 8], 6, 0x6fa87e4f);
  132. MD5_ROUND4(d, a, b, c, x[15], 10, 0xfe2ce6e0);
  133. MD5_ROUND4(c, d, a, b, x[ 6], 15, 0xa3014314);
  134. MD5_ROUND4(b, c, d, a, x[13], 21, 0x4e0811a1);
  135. MD5_ROUND4(a, b, c, d, x[ 4], 6, 0xf7537e82);
  136. MD5_ROUND4(d, a, b, c, x[11], 10, 0xbd3af235);
  137. MD5_ROUND4(c, d, a, b, x[ 2], 15, 0x2ad7d2bb);
  138. MD5_ROUND4(b, c, d, a, x[ 9], 21, 0xeb86d391);
  139. state[0] += a;
  140. state[1] += b;
  141. state[2] += c;
  142. state[3] += d;
  143. }
  144. /**
  145. * Calculate message hash.
  146. * Can be called repeatedly with chunks of the message to be hashed.
  147. *
  148. * @param ctx the algorithm context containing current hashing state
  149. * @param msg message chunk
  150. * @param size length of the message chunk
  151. */
  152. void rhash_md5_update(md5_ctx *ctx, const unsigned char* msg, size_t size)
  153. {
  154. unsigned index = (unsigned)ctx->length & 63;
  155. ctx->length += size;
  156. /* fill partial block */
  157. if (index) {
  158. unsigned left = md5_block_size - index;
  159. le32_copy((char*)ctx->message, index, msg, (size < left ? size : left));
  160. if (size < left) return;
  161. /* process partial block */
  162. rhash_md5_process_block(ctx->hash, ctx->message);
  163. msg += left;
  164. size -= left;
  165. }
  166. while (size >= md5_block_size) {
  167. unsigned* aligned_message_block;
  168. if (IS_LITTLE_ENDIAN && IS_ALIGNED_32(msg)) {
  169. /* the most common case is processing a 32-bit aligned message
  170. on a little-endian CPU without copying it */
  171. aligned_message_block = (unsigned*)msg;
  172. } else {
  173. le32_copy(ctx->message, 0, msg, md5_block_size);
  174. aligned_message_block = ctx->message;
  175. }
  176. rhash_md5_process_block(ctx->hash, aligned_message_block);
  177. msg += md5_block_size;
  178. size -= md5_block_size;
  179. }
  180. if (size) {
  181. /* save leftovers */
  182. le32_copy(ctx->message, 0, msg, size);
  183. }
  184. }
  185. /**
  186. * Store calculated hash into the given array.
  187. *
  188. * @param ctx the algorithm context containing current hashing state
  189. * @param result calculated hash in binary form
  190. */
  191. void rhash_md5_final(md5_ctx *ctx, unsigned char* result)
  192. {
  193. unsigned index = ((unsigned)ctx->length & 63) >> 2;
  194. unsigned shift = ((unsigned)ctx->length & 3) * 8;
  195. /* pad message and run for last block */
  196. /* append the byte 0x80 to the message */
  197. ctx->message[index] &= ~(0xFFFFFFFFu << shift);
  198. ctx->message[index++] ^= 0x80u << shift;
  199. /* if no room left in the message to store 64-bit message length */
  200. if (index > 14) {
  201. /* then fill the rest with zeros and process it */
  202. while (index < 16) {
  203. ctx->message[index++] = 0;
  204. }
  205. rhash_md5_process_block(ctx->hash, ctx->message);
  206. index = 0;
  207. }
  208. while (index < 14) {
  209. ctx->message[index++] = 0;
  210. }
  211. ctx->message[14] = (unsigned)(ctx->length << 3);
  212. ctx->message[15] = (unsigned)(ctx->length >> 29);
  213. rhash_md5_process_block(ctx->hash, ctx->message);
  214. if (result) le32_copy(result, 0, &ctx->hash, 16);
  215. }