MD5.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * This code is derived from (original license follows):
  3. *
  4. * This is an OpenSSL-compatible implementation of the RSA Data Security, Inc.
  5. * MD5 Message-Digest Algorithm (RFC 1321).
  6. *
  7. * Homepage:
  8. * http://openwall.info/wiki/people/solar/software/public-domain-source-code/md5
  9. *
  10. * Author:
  11. * Alexander Peslyak, better known as Solar Designer <solar at openwall.com>
  12. *
  13. * This software was written by Alexander Peslyak in 2001. No copyright is
  14. * claimed, and the software is hereby placed in the public domain.
  15. * In case this attempt to disclaim copyright and place the software in the
  16. * public domain is deemed null and void, then the software is
  17. * Copyright (c) 2001 Alexander Peslyak and it is hereby released to the
  18. * general public under the following terms:
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted.
  22. *
  23. * There's ABSOLUTELY NO WARRANTY, express or implied.
  24. *
  25. * (This is a heavily cut-down "BSD license".)
  26. *
  27. * This differs from Colin Plumb's older public domain implementation in that
  28. * no exactly 32-bit integer data type is required (any 32-bit or wider
  29. * unsigned integer data type will do), there's no compile-time endianness
  30. * configuration, and the function prototypes match OpenSSL's. No code from
  31. * Colin Plumb's implementation has been reused; this comment merely compares
  32. * the properties of the two independent implementations.
  33. *
  34. * The primary goals of this implementation are portability and ease of use.
  35. * It is meant to be fast, but not as fast as possible. Some known
  36. * optimizations are not included to reduce source code size and avoid
  37. * compile-time configuration.
  38. */
  39. #include "llvm/ADT/ArrayRef.h"
  40. #include "llvm/Support/Format.h"
  41. #include "llvm/Support/MD5.h"
  42. #include "llvm/Support/raw_ostream.h"
  43. #include <cstring>
  44. // The basic MD5 functions.
  45. // F and G are optimized compared to their RFC 1321 definitions for
  46. // architectures that lack an AND-NOT instruction, just like in Colin Plumb's
  47. // implementation.
  48. #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
  49. #define G(x, y, z) ((y) ^ ((z) & ((x) ^ (y))))
  50. #define H(x, y, z) ((x) ^ (y) ^ (z))
  51. #define I(x, y, z) ((y) ^ ((x) | ~(z)))
  52. // The MD5 transformation for all four rounds.
  53. #define STEP(f, a, b, c, d, x, t, s) \
  54. (a) += f((b), (c), (d)) + (x) + (t); \
  55. (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \
  56. (a) += (b);
  57. // SET reads 4 input bytes in little-endian byte order and stores them
  58. // in a properly aligned word in host byte order.
  59. #define SET(n) \
  60. (block[(n)] = \
  61. (MD5_u32plus) ptr[(n) * 4] | ((MD5_u32plus) ptr[(n) * 4 + 1] << 8) | \
  62. ((MD5_u32plus) ptr[(n) * 4 + 2] << 16) | \
  63. ((MD5_u32plus) ptr[(n) * 4 + 3] << 24))
  64. #define GET(n) (block[(n)])
  65. namespace llvm {
  66. /// \brief This processes one or more 64-byte data blocks, but does NOT update
  67. ///the bit counters. There are no alignment requirements.
  68. const uint8_t *MD5::body(ArrayRef<uint8_t> Data) {
  69. const uint8_t *ptr;
  70. MD5_u32plus a, b, c, d;
  71. MD5_u32plus saved_a, saved_b, saved_c, saved_d;
  72. unsigned long Size = Data.size();
  73. ptr = Data.data();
  74. a = this->a;
  75. b = this->b;
  76. c = this->c;
  77. d = this->d;
  78. do {
  79. saved_a = a;
  80. saved_b = b;
  81. saved_c = c;
  82. saved_d = d;
  83. // Round 1
  84. STEP(F, a, b, c, d, SET(0), 0xd76aa478, 7)
  85. STEP(F, d, a, b, c, SET(1), 0xe8c7b756, 12)
  86. STEP(F, c, d, a, b, SET(2), 0x242070db, 17)
  87. STEP(F, b, c, d, a, SET(3), 0xc1bdceee, 22)
  88. STEP(F, a, b, c, d, SET(4), 0xf57c0faf, 7)
  89. STEP(F, d, a, b, c, SET(5), 0x4787c62a, 12)
  90. STEP(F, c, d, a, b, SET(6), 0xa8304613, 17)
  91. STEP(F, b, c, d, a, SET(7), 0xfd469501, 22)
  92. STEP(F, a, b, c, d, SET(8), 0x698098d8, 7)
  93. STEP(F, d, a, b, c, SET(9), 0x8b44f7af, 12)
  94. STEP(F, c, d, a, b, SET(10), 0xffff5bb1, 17)
  95. STEP(F, b, c, d, a, SET(11), 0x895cd7be, 22)
  96. STEP(F, a, b, c, d, SET(12), 0x6b901122, 7)
  97. STEP(F, d, a, b, c, SET(13), 0xfd987193, 12)
  98. STEP(F, c, d, a, b, SET(14), 0xa679438e, 17)
  99. STEP(F, b, c, d, a, SET(15), 0x49b40821, 22)
  100. // Round 2
  101. STEP(G, a, b, c, d, GET(1), 0xf61e2562, 5)
  102. STEP(G, d, a, b, c, GET(6), 0xc040b340, 9)
  103. STEP(G, c, d, a, b, GET(11), 0x265e5a51, 14)
  104. STEP(G, b, c, d, a, GET(0), 0xe9b6c7aa, 20)
  105. STEP(G, a, b, c, d, GET(5), 0xd62f105d, 5)
  106. STEP(G, d, a, b, c, GET(10), 0x02441453, 9)
  107. STEP(G, c, d, a, b, GET(15), 0xd8a1e681, 14)
  108. STEP(G, b, c, d, a, GET(4), 0xe7d3fbc8, 20)
  109. STEP(G, a, b, c, d, GET(9), 0x21e1cde6, 5)
  110. STEP(G, d, a, b, c, GET(14), 0xc33707d6, 9)
  111. STEP(G, c, d, a, b, GET(3), 0xf4d50d87, 14)
  112. STEP(G, b, c, d, a, GET(8), 0x455a14ed, 20)
  113. STEP(G, a, b, c, d, GET(13), 0xa9e3e905, 5)
  114. STEP(G, d, a, b, c, GET(2), 0xfcefa3f8, 9)
  115. STEP(G, c, d, a, b, GET(7), 0x676f02d9, 14)
  116. STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20)
  117. // Round 3
  118. STEP(H, a, b, c, d, GET(5), 0xfffa3942, 4)
  119. STEP(H, d, a, b, c, GET(8), 0x8771f681, 11)
  120. STEP(H, c, d, a, b, GET(11), 0x6d9d6122, 16)
  121. STEP(H, b, c, d, a, GET(14), 0xfde5380c, 23)
  122. STEP(H, a, b, c, d, GET(1), 0xa4beea44, 4)
  123. STEP(H, d, a, b, c, GET(4), 0x4bdecfa9, 11)
  124. STEP(H, c, d, a, b, GET(7), 0xf6bb4b60, 16)
  125. STEP(H, b, c, d, a, GET(10), 0xbebfbc70, 23)
  126. STEP(H, a, b, c, d, GET(13), 0x289b7ec6, 4)
  127. STEP(H, d, a, b, c, GET(0), 0xeaa127fa, 11)
  128. STEP(H, c, d, a, b, GET(3), 0xd4ef3085, 16)
  129. STEP(H, b, c, d, a, GET(6), 0x04881d05, 23)
  130. STEP(H, a, b, c, d, GET(9), 0xd9d4d039, 4)
  131. STEP(H, d, a, b, c, GET(12), 0xe6db99e5, 11)
  132. STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16)
  133. STEP(H, b, c, d, a, GET(2), 0xc4ac5665, 23)
  134. // Round 4
  135. STEP(I, a, b, c, d, GET(0), 0xf4292244, 6)
  136. STEP(I, d, a, b, c, GET(7), 0x432aff97, 10)
  137. STEP(I, c, d, a, b, GET(14), 0xab9423a7, 15)
  138. STEP(I, b, c, d, a, GET(5), 0xfc93a039, 21)
  139. STEP(I, a, b, c, d, GET(12), 0x655b59c3, 6)
  140. STEP(I, d, a, b, c, GET(3), 0x8f0ccc92, 10)
  141. STEP(I, c, d, a, b, GET(10), 0xffeff47d, 15)
  142. STEP(I, b, c, d, a, GET(1), 0x85845dd1, 21)
  143. STEP(I, a, b, c, d, GET(8), 0x6fa87e4f, 6)
  144. STEP(I, d, a, b, c, GET(15), 0xfe2ce6e0, 10)
  145. STEP(I, c, d, a, b, GET(6), 0xa3014314, 15)
  146. STEP(I, b, c, d, a, GET(13), 0x4e0811a1, 21)
  147. STEP(I, a, b, c, d, GET(4), 0xf7537e82, 6)
  148. STEP(I, d, a, b, c, GET(11), 0xbd3af235, 10)
  149. STEP(I, c, d, a, b, GET(2), 0x2ad7d2bb, 15)
  150. STEP(I, b, c, d, a, GET(9), 0xeb86d391, 21)
  151. a += saved_a;
  152. b += saved_b;
  153. c += saved_c;
  154. d += saved_d;
  155. ptr += 64;
  156. } while (Size -= 64);
  157. this->a = a;
  158. this->b = b;
  159. this->c = c;
  160. this->d = d;
  161. return ptr;
  162. }
  163. MD5::MD5()
  164. : a(0x67452301), b(0xefcdab89), c(0x98badcfe), d(0x10325476), hi(0), lo(0) {
  165. }
  166. /// Incrementally add the bytes in \p Data to the hash.
  167. void MD5::update(ArrayRef<uint8_t> Data) {
  168. MD5_u32plus saved_lo;
  169. unsigned long used, free;
  170. const uint8_t *Ptr = Data.data();
  171. unsigned long Size = Data.size();
  172. saved_lo = lo;
  173. if ((lo = (saved_lo + Size) & 0x1fffffff) < saved_lo)
  174. hi++;
  175. hi += Size >> 29;
  176. used = saved_lo & 0x3f;
  177. if (used) {
  178. free = 64 - used;
  179. if (Size < free) {
  180. memcpy(&buffer[used], Ptr, Size);
  181. return;
  182. }
  183. memcpy(&buffer[used], Ptr, free);
  184. Ptr = Ptr + free;
  185. Size -= free;
  186. body(makeArrayRef(buffer, 64));
  187. }
  188. if (Size >= 64) {
  189. Ptr = body(makeArrayRef(Ptr, Size & ~(unsigned long) 0x3f));
  190. Size &= 0x3f;
  191. }
  192. memcpy(buffer, Ptr, Size);
  193. }
  194. /// Add the bytes in the StringRef \p Str to the hash.
  195. // Note that this isn't a string and so this won't include any trailing NULL
  196. // bytes.
  197. void MD5::update(StringRef Str) {
  198. ArrayRef<uint8_t> SVal((const uint8_t *)Str.data(), Str.size());
  199. update(SVal);
  200. }
  201. /// \brief Finish the hash and place the resulting hash into \p result.
  202. /// \param result is assumed to be a minimum of 16-bytes in size.
  203. void MD5::final(MD5Result &Result) {
  204. unsigned long used, free;
  205. used = lo & 0x3f;
  206. buffer[used++] = 0x80;
  207. free = 64 - used;
  208. if (free < 8) {
  209. memset(&buffer[used], 0, free);
  210. body(makeArrayRef(buffer, 64));
  211. used = 0;
  212. free = 64;
  213. }
  214. memset(&buffer[used], 0, free - 8);
  215. lo <<= 3;
  216. buffer[56] = lo;
  217. buffer[57] = lo >> 8;
  218. buffer[58] = lo >> 16;
  219. buffer[59] = lo >> 24;
  220. buffer[60] = hi;
  221. buffer[61] = hi >> 8;
  222. buffer[62] = hi >> 16;
  223. buffer[63] = hi >> 24;
  224. body(makeArrayRef(buffer, 64));
  225. Result[0] = a;
  226. Result[1] = a >> 8;
  227. Result[2] = a >> 16;
  228. Result[3] = a >> 24;
  229. Result[4] = b;
  230. Result[5] = b >> 8;
  231. Result[6] = b >> 16;
  232. Result[7] = b >> 24;
  233. Result[8] = c;
  234. Result[9] = c >> 8;
  235. Result[10] = c >> 16;
  236. Result[11] = c >> 24;
  237. Result[12] = d;
  238. Result[13] = d >> 8;
  239. Result[14] = d >> 16;
  240. Result[15] = d >> 24;
  241. }
  242. void MD5::stringifyResult(MD5Result &Result, SmallString<32> &Str) {
  243. raw_svector_ostream Res(Str);
  244. for (int i = 0; i < 16; ++i)
  245. Res << format("%.2x", Result[i]);
  246. }
  247. }