md5.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * This is an OpenSSL-compatible implementation of the RSA Data Security,
  3. * Inc. MD5 Message-Digest Algorithm (RFC 1321).
  4. *
  5. * Written by Solar Designer <solar at openwall.com> in 2001, and placed
  6. * in the public domain. There's absolutely no warranty.
  7. *
  8. * This differs from Colin Plumb's older public domain implementation in
  9. * that no 32-bit integer data type is required, there's no compile-time
  10. * endianness configuration, and the function prototypes match OpenSSL's.
  11. * The primary goals are portability and ease of use.
  12. *
  13. * This implementation is meant to be fast, but not as fast as possible.
  14. * Some known optimizations are not included to reduce source code size
  15. * and avoid compile-time configuration.
  16. */
  17. #include <string.h>
  18. #include "md5.h"
  19. /*
  20. * The basic MD5 functions.
  21. *
  22. * F and G are optimized compared to their RFC 1321 definitions for
  23. * architectures that lack an AND-NOT instruction, just like in Colin Plumb's
  24. * implementation.
  25. */
  26. #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
  27. #define G(x, y, z) ((y) ^ ((z) & ((x) ^ (y))))
  28. #define H(x, y, z) ((x) ^ (y) ^ (z))
  29. #define I(x, y, z) ((y) ^ ((x) | ~(z)))
  30. /*
  31. * The MD5 transformation for all four rounds.
  32. */
  33. #define STEP(f, a, b, c, d, x, t, s) \
  34. (a) += f((b), (c), (d)) + (x) + (t); \
  35. (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \
  36. (a) += (b);
  37. /*
  38. * SET reads 4 input bytes in little-endian byte order and stores them
  39. * in a properly aligned word in host byte order.
  40. *
  41. * The check for little-endian architectures that tolerate unaligned
  42. * memory accesses is just an optimization. Nothing will break if it
  43. * doesn't work.
  44. */
  45. #if defined(__i386__) || defined(__x86_64__) || defined(__vax__)
  46. # define SET(n) \
  47. (*(uint32_t *)&ptr[(n) * 4])
  48. # define GET(n) \
  49. SET(n)
  50. #else
  51. # define SET(n) \
  52. (ctx->block[(n)] = \
  53. (uint32_t)ptr[(n) * 4] | \
  54. ((uint32_t)ptr[(n) * 4 + 1] << 8) | \
  55. ((uint32_t)ptr[(n) * 4 + 2] << 16) | \
  56. ((uint32_t)ptr[(n) * 4 + 3] << 24))
  57. # define GET(n) \
  58. (ctx->block[(n)])
  59. #endif
  60. void make_digest(char *md5str, const unsigned char *digest)
  61. {
  62. make_digest_ex(md5str, digest, 16);
  63. }
  64. void make_digest_ex(char *md5str, const unsigned char *digest, int len)
  65. {
  66. static const char hexits[17] = "0123456789abcdef";
  67. int i;
  68. for (i = 0; i < len; i++) {
  69. md5str[i * 2] = hexits[digest[i] >> 4];
  70. md5str[(i * 2) + 1] = hexits[digest[i] & 0x0F];
  71. }
  72. md5str[len * 2] = '\0';
  73. }
  74. /*
  75. * This processes one or more 64-byte data blocks, but does NOT update
  76. * the bit counters. There are no alignment requirements.
  77. */
  78. static const void *body(MD5_CTX *ctx, const void *data, size_t size)
  79. {
  80. const unsigned char *ptr;
  81. uint32_t a, b, c, d;
  82. uint32_t saved_a, saved_b, saved_c, saved_d;
  83. ptr = data;
  84. a = ctx->a;
  85. b = ctx->b;
  86. c = ctx->c;
  87. d = ctx->d;
  88. do {
  89. saved_a = a;
  90. saved_b = b;
  91. saved_c = c;
  92. saved_d = d;
  93. /* Round 1 */
  94. STEP(F, a, b, c, d, SET(0), 0xd76aa478, 7)
  95. STEP(F, d, a, b, c, SET(1), 0xe8c7b756, 12)
  96. STEP(F, c, d, a, b, SET(2), 0x242070db, 17)
  97. STEP(F, b, c, d, a, SET(3), 0xc1bdceee, 22)
  98. STEP(F, a, b, c, d, SET(4), 0xf57c0faf, 7)
  99. STEP(F, d, a, b, c, SET(5), 0x4787c62a, 12)
  100. STEP(F, c, d, a, b, SET(6), 0xa8304613, 17)
  101. STEP(F, b, c, d, a, SET(7), 0xfd469501, 22)
  102. STEP(F, a, b, c, d, SET(8), 0x698098d8, 7)
  103. STEP(F, d, a, b, c, SET(9), 0x8b44f7af, 12)
  104. STEP(F, c, d, a, b, SET(10), 0xffff5bb1, 17)
  105. STEP(F, b, c, d, a, SET(11), 0x895cd7be, 22)
  106. STEP(F, a, b, c, d, SET(12), 0x6b901122, 7)
  107. STEP(F, d, a, b, c, SET(13), 0xfd987193, 12)
  108. STEP(F, c, d, a, b, SET(14), 0xa679438e, 17)
  109. STEP(F, b, c, d, a, SET(15), 0x49b40821, 22)
  110. /* Round 2 */
  111. STEP(G, a, b, c, d, GET(1), 0xf61e2562, 5)
  112. STEP(G, d, a, b, c, GET(6), 0xc040b340, 9)
  113. STEP(G, c, d, a, b, GET(11), 0x265e5a51, 14)
  114. STEP(G, b, c, d, a, GET(0), 0xe9b6c7aa, 20)
  115. STEP(G, a, b, c, d, GET(5), 0xd62f105d, 5)
  116. STEP(G, d, a, b, c, GET(10), 0x02441453, 9)
  117. STEP(G, c, d, a, b, GET(15), 0xd8a1e681, 14)
  118. STEP(G, b, c, d, a, GET(4), 0xe7d3fbc8, 20)
  119. STEP(G, a, b, c, d, GET(9), 0x21e1cde6, 5)
  120. STEP(G, d, a, b, c, GET(14), 0xc33707d6, 9)
  121. STEP(G, c, d, a, b, GET(3), 0xf4d50d87, 14)
  122. STEP(G, b, c, d, a, GET(8), 0x455a14ed, 20)
  123. STEP(G, a, b, c, d, GET(13), 0xa9e3e905, 5)
  124. STEP(G, d, a, b, c, GET(2), 0xfcefa3f8, 9)
  125. STEP(G, c, d, a, b, GET(7), 0x676f02d9, 14)
  126. STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20)
  127. /* Round 3 */
  128. STEP(H, a, b, c, d, GET(5), 0xfffa3942, 4)
  129. STEP(H, d, a, b, c, GET(8), 0x8771f681, 11)
  130. STEP(H, c, d, a, b, GET(11), 0x6d9d6122, 16)
  131. STEP(H, b, c, d, a, GET(14), 0xfde5380c, 23)
  132. STEP(H, a, b, c, d, GET(1), 0xa4beea44, 4)
  133. STEP(H, d, a, b, c, GET(4), 0x4bdecfa9, 11)
  134. STEP(H, c, d, a, b, GET(7), 0xf6bb4b60, 16)
  135. STEP(H, b, c, d, a, GET(10), 0xbebfbc70, 23)
  136. STEP(H, a, b, c, d, GET(13), 0x289b7ec6, 4)
  137. STEP(H, d, a, b, c, GET(0), 0xeaa127fa, 11)
  138. STEP(H, c, d, a, b, GET(3), 0xd4ef3085, 16)
  139. STEP(H, b, c, d, a, GET(6), 0x04881d05, 23)
  140. STEP(H, a, b, c, d, GET(9), 0xd9d4d039, 4)
  141. STEP(H, d, a, b, c, GET(12), 0xe6db99e5, 11)
  142. STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16)
  143. STEP(H, b, c, d, a, GET(2), 0xc4ac5665, 23)
  144. /* Round 4 */
  145. STEP(I, a, b, c, d, GET(0), 0xf4292244, 6)
  146. STEP(I, d, a, b, c, GET(7), 0x432aff97, 10)
  147. STEP(I, c, d, a, b, GET(14), 0xab9423a7, 15)
  148. STEP(I, b, c, d, a, GET(5), 0xfc93a039, 21)
  149. STEP(I, a, b, c, d, GET(12), 0x655b59c3, 6)
  150. STEP(I, d, a, b, c, GET(3), 0x8f0ccc92, 10)
  151. STEP(I, c, d, a, b, GET(10), 0xffeff47d, 15)
  152. STEP(I, b, c, d, a, GET(1), 0x85845dd1, 21)
  153. STEP(I, a, b, c, d, GET(8), 0x6fa87e4f, 6)
  154. STEP(I, d, a, b, c, GET(15), 0xfe2ce6e0, 10)
  155. STEP(I, c, d, a, b, GET(6), 0xa3014314, 15)
  156. STEP(I, b, c, d, a, GET(13), 0x4e0811a1, 21)
  157. STEP(I, a, b, c, d, GET(4), 0xf7537e82, 6)
  158. STEP(I, d, a, b, c, GET(11), 0xbd3af235, 10)
  159. STEP(I, c, d, a, b, GET(2), 0x2ad7d2bb, 15)
  160. STEP(I, b, c, d, a, GET(9), 0xeb86d391, 21)
  161. a += saved_a;
  162. b += saved_b;
  163. c += saved_c;
  164. d += saved_d;
  165. ptr += 64;
  166. } while (size -= 64);
  167. ctx->a = a;
  168. ctx->b = b;
  169. ctx->c = c;
  170. ctx->d = d;
  171. return ptr;
  172. }
  173. void MD5Init(MD5_CTX *ctx)
  174. {
  175. ctx->a = 0x67452301;
  176. ctx->b = 0xefcdab89;
  177. ctx->c = 0x98badcfe;
  178. ctx->d = 0x10325476;
  179. ctx->lo = 0;
  180. ctx->hi = 0;
  181. }
  182. void MD5Update(MD5_CTX *ctx, const void *data, size_t size)
  183. {
  184. uint32_t saved_lo;
  185. uint32_t used, free;
  186. saved_lo = ctx->lo;
  187. if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo) {
  188. ctx->hi++;
  189. }
  190. ctx->hi += size >> 29;
  191. used = saved_lo & 0x3f;
  192. if (used) {
  193. free = 64 - used;
  194. if (size < free) {
  195. memcpy(&ctx->buffer[used], data, size);
  196. return;
  197. }
  198. memcpy(&ctx->buffer[used], data, free);
  199. data = (unsigned char *)data + free;
  200. size -= free;
  201. body(ctx, ctx->buffer, 64);
  202. }
  203. if (size >= 64) {
  204. data = body(ctx, data, size & ~(size_t)0x3f);
  205. size &= 0x3f;
  206. }
  207. memcpy(ctx->buffer, data, size);
  208. }
  209. void MD5Final(unsigned char *result, MD5_CTX *ctx)
  210. {
  211. uint32_t used, free;
  212. used = ctx->lo & 0x3f;
  213. ctx->buffer[used++] = 0x80;
  214. free = 64 - used;
  215. if (free < 8) {
  216. memset(&ctx->buffer[used], 0, free);
  217. body(ctx, ctx->buffer, 64);
  218. used = 0;
  219. free = 64;
  220. }
  221. memset(&ctx->buffer[used], 0, free - 8);
  222. ctx->lo <<= 3;
  223. ctx->buffer[56] = ctx->lo;
  224. ctx->buffer[57] = ctx->lo >> 8;
  225. ctx->buffer[58] = ctx->lo >> 16;
  226. ctx->buffer[59] = ctx->lo >> 24;
  227. ctx->buffer[60] = ctx->hi;
  228. ctx->buffer[61] = ctx->hi >> 8;
  229. ctx->buffer[62] = ctx->hi >> 16;
  230. ctx->buffer[63] = ctx->hi >> 24;
  231. body(ctx, ctx->buffer, 64);
  232. result[0] = ctx->a;
  233. result[1] = ctx->a >> 8;
  234. result[2] = ctx->a >> 16;
  235. result[3] = ctx->a >> 24;
  236. result[4] = ctx->b;
  237. result[5] = ctx->b >> 8;
  238. result[6] = ctx->b >> 16;
  239. result[7] = ctx->b >> 24;
  240. result[8] = ctx->c;
  241. result[9] = ctx->c >> 8;
  242. result[10] = ctx->c >> 16;
  243. result[11] = ctx->c >> 24;
  244. result[12] = ctx->d;
  245. result[13] = ctx->d >> 8;
  246. result[14] = ctx->d >> 16;
  247. result[15] = ctx->d >> 24;
  248. memset((void*)ctx, 0, sizeof(*ctx));
  249. }
  250. void md5(void *data, size_t data_size, void *hashstr)
  251. {
  252. MD5_CTX context;
  253. unsigned char digest[16];
  254. MD5Init(&context);
  255. MD5Update(&context, data, data_size);
  256. MD5Final(digest, &context);
  257. make_digest_ex(hashstr, digest, 16);
  258. }
  259. /*
  260. * Local variables:
  261. * tab-width: 4
  262. * c-basic-offset: 4
  263. * End:
  264. * vim600: sw=4 ts=4 fdm=marker
  265. * vim<600: sw=4 ts=4
  266. */