MD5.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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/Support/MD5.h"
  40. #include "llvm/ADT/ArrayRef.h"
  41. #include "llvm/ADT/SmallString.h"
  42. #include "llvm/ADT/StringExtras.h"
  43. #include "llvm/ADT/StringRef.h"
  44. #include "llvm/Support/Endian.h"
  45. #include <array>
  46. #include <cstdint>
  47. #include <cstring>
  48. // The basic MD5 functions.
  49. // F and G are optimized compared to their RFC 1321 definitions for
  50. // architectures that lack an AND-NOT instruction, just like in Colin Plumb's
  51. // implementation.
  52. #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
  53. #define G(x, y, z) ((y) ^ ((z) & ((x) ^ (y))))
  54. #define H(x, y, z) ((x) ^ (y) ^ (z))
  55. #define I(x, y, z) ((y) ^ ((x) | ~(z)))
  56. // The MD5 transformation for all four rounds.
  57. #define STEP(f, a, b, c, d, x, t, s) \
  58. (a) += f((b), (c), (d)) + (x) + (t); \
  59. (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \
  60. (a) += (b);
  61. // SET reads 4 input bytes in little-endian byte order and stores them
  62. // in a properly aligned word in host byte order.
  63. #define SET(n) \
  64. (InternalState.block[(n)] = (MD5_u32plus)ptr[(n)*4] | \
  65. ((MD5_u32plus)ptr[(n)*4 + 1] << 8) | \
  66. ((MD5_u32plus)ptr[(n)*4 + 2] << 16) | \
  67. ((MD5_u32plus)ptr[(n)*4 + 3] << 24))
  68. #define GET(n) (InternalState.block[(n)])
  69. using namespace llvm;
  70. /// This processes one or more 64-byte data blocks, but does NOT update
  71. ///the bit counters. There are no alignment requirements.
  72. const uint8_t *MD5::body(ArrayRef<uint8_t> Data) {
  73. const uint8_t *ptr;
  74. MD5_u32plus a, b, c, d;
  75. MD5_u32plus saved_a, saved_b, saved_c, saved_d;
  76. unsigned long Size = Data.size();
  77. ptr = Data.data();
  78. a = InternalState.a;
  79. b = InternalState.b;
  80. c = InternalState.c;
  81. d = InternalState.d;
  82. do {
  83. saved_a = a;
  84. saved_b = b;
  85. saved_c = c;
  86. saved_d = d;
  87. // Round 1
  88. STEP(F, a, b, c, d, SET(0), 0xd76aa478, 7)
  89. STEP(F, d, a, b, c, SET(1), 0xe8c7b756, 12)
  90. STEP(F, c, d, a, b, SET(2), 0x242070db, 17)
  91. STEP(F, b, c, d, a, SET(3), 0xc1bdceee, 22)
  92. STEP(F, a, b, c, d, SET(4), 0xf57c0faf, 7)
  93. STEP(F, d, a, b, c, SET(5), 0x4787c62a, 12)
  94. STEP(F, c, d, a, b, SET(6), 0xa8304613, 17)
  95. STEP(F, b, c, d, a, SET(7), 0xfd469501, 22)
  96. STEP(F, a, b, c, d, SET(8), 0x698098d8, 7)
  97. STEP(F, d, a, b, c, SET(9), 0x8b44f7af, 12)
  98. STEP(F, c, d, a, b, SET(10), 0xffff5bb1, 17)
  99. STEP(F, b, c, d, a, SET(11), 0x895cd7be, 22)
  100. STEP(F, a, b, c, d, SET(12), 0x6b901122, 7)
  101. STEP(F, d, a, b, c, SET(13), 0xfd987193, 12)
  102. STEP(F, c, d, a, b, SET(14), 0xa679438e, 17)
  103. STEP(F, b, c, d, a, SET(15), 0x49b40821, 22)
  104. // Round 2
  105. STEP(G, a, b, c, d, GET(1), 0xf61e2562, 5)
  106. STEP(G, d, a, b, c, GET(6), 0xc040b340, 9)
  107. STEP(G, c, d, a, b, GET(11), 0x265e5a51, 14)
  108. STEP(G, b, c, d, a, GET(0), 0xe9b6c7aa, 20)
  109. STEP(G, a, b, c, d, GET(5), 0xd62f105d, 5)
  110. STEP(G, d, a, b, c, GET(10), 0x02441453, 9)
  111. STEP(G, c, d, a, b, GET(15), 0xd8a1e681, 14)
  112. STEP(G, b, c, d, a, GET(4), 0xe7d3fbc8, 20)
  113. STEP(G, a, b, c, d, GET(9), 0x21e1cde6, 5)
  114. STEP(G, d, a, b, c, GET(14), 0xc33707d6, 9)
  115. STEP(G, c, d, a, b, GET(3), 0xf4d50d87, 14)
  116. STEP(G, b, c, d, a, GET(8), 0x455a14ed, 20)
  117. STEP(G, a, b, c, d, GET(13), 0xa9e3e905, 5)
  118. STEP(G, d, a, b, c, GET(2), 0xfcefa3f8, 9)
  119. STEP(G, c, d, a, b, GET(7), 0x676f02d9, 14)
  120. STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20)
  121. // Round 3
  122. STEP(H, a, b, c, d, GET(5), 0xfffa3942, 4)
  123. STEP(H, d, a, b, c, GET(8), 0x8771f681, 11)
  124. STEP(H, c, d, a, b, GET(11), 0x6d9d6122, 16)
  125. STEP(H, b, c, d, a, GET(14), 0xfde5380c, 23)
  126. STEP(H, a, b, c, d, GET(1), 0xa4beea44, 4)
  127. STEP(H, d, a, b, c, GET(4), 0x4bdecfa9, 11)
  128. STEP(H, c, d, a, b, GET(7), 0xf6bb4b60, 16)
  129. STEP(H, b, c, d, a, GET(10), 0xbebfbc70, 23)
  130. STEP(H, a, b, c, d, GET(13), 0x289b7ec6, 4)
  131. STEP(H, d, a, b, c, GET(0), 0xeaa127fa, 11)
  132. STEP(H, c, d, a, b, GET(3), 0xd4ef3085, 16)
  133. STEP(H, b, c, d, a, GET(6), 0x04881d05, 23)
  134. STEP(H, a, b, c, d, GET(9), 0xd9d4d039, 4)
  135. STEP(H, d, a, b, c, GET(12), 0xe6db99e5, 11)
  136. STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16)
  137. STEP(H, b, c, d, a, GET(2), 0xc4ac5665, 23)
  138. // Round 4
  139. STEP(I, a, b, c, d, GET(0), 0xf4292244, 6)
  140. STEP(I, d, a, b, c, GET(7), 0x432aff97, 10)
  141. STEP(I, c, d, a, b, GET(14), 0xab9423a7, 15)
  142. STEP(I, b, c, d, a, GET(5), 0xfc93a039, 21)
  143. STEP(I, a, b, c, d, GET(12), 0x655b59c3, 6)
  144. STEP(I, d, a, b, c, GET(3), 0x8f0ccc92, 10)
  145. STEP(I, c, d, a, b, GET(10), 0xffeff47d, 15)
  146. STEP(I, b, c, d, a, GET(1), 0x85845dd1, 21)
  147. STEP(I, a, b, c, d, GET(8), 0x6fa87e4f, 6)
  148. STEP(I, d, a, b, c, GET(15), 0xfe2ce6e0, 10)
  149. STEP(I, c, d, a, b, GET(6), 0xa3014314, 15)
  150. STEP(I, b, c, d, a, GET(13), 0x4e0811a1, 21)
  151. STEP(I, a, b, c, d, GET(4), 0xf7537e82, 6)
  152. STEP(I, d, a, b, c, GET(11), 0xbd3af235, 10)
  153. STEP(I, c, d, a, b, GET(2), 0x2ad7d2bb, 15)
  154. STEP(I, b, c, d, a, GET(9), 0xeb86d391, 21)
  155. a += saved_a;
  156. b += saved_b;
  157. c += saved_c;
  158. d += saved_d;
  159. ptr += 64;
  160. } while (Size -= 64);
  161. InternalState.a = a;
  162. InternalState.b = b;
  163. InternalState.c = c;
  164. InternalState.d = d;
  165. return ptr;
  166. }
  167. MD5::MD5() = default;
  168. /// Incrementally add the bytes in \p Data to the hash.
  169. void MD5::update(ArrayRef<uint8_t> Data) {
  170. MD5_u32plus saved_lo;
  171. unsigned long used, free;
  172. const uint8_t *Ptr = Data.data();
  173. unsigned long Size = Data.size();
  174. saved_lo = InternalState.lo;
  175. if ((InternalState.lo = (saved_lo + Size) & 0x1fffffff) < saved_lo)
  176. InternalState.hi++;
  177. InternalState.hi += Size >> 29;
  178. used = saved_lo & 0x3f;
  179. if (used) {
  180. free = 64 - used;
  181. if (Size < free) {
  182. memcpy(&InternalState.buffer[used], Ptr, Size);
  183. return;
  184. }
  185. memcpy(&InternalState.buffer[used], Ptr, free);
  186. Ptr = Ptr + free;
  187. Size -= free;
  188. body(ArrayRef(InternalState.buffer, 64));
  189. }
  190. if (Size >= 64) {
  191. Ptr = body(ArrayRef(Ptr, Size & ~(unsigned long)0x3f));
  192. Size &= 0x3f;
  193. }
  194. memcpy(InternalState.buffer, Ptr, Size);
  195. }
  196. /// Add the bytes in the StringRef \p Str to the hash.
  197. // Note that this isn't a string and so this won't include any trailing NULL
  198. // bytes.
  199. void MD5::update(StringRef Str) {
  200. ArrayRef<uint8_t> SVal((const uint8_t *)Str.data(), Str.size());
  201. update(SVal);
  202. }
  203. /// Finish the hash and place the resulting hash into \p result.
  204. /// \param Result is assumed to be a minimum of 16-bytes in size.
  205. void MD5::final(MD5Result &Result) {
  206. unsigned long used, free;
  207. used = InternalState.lo & 0x3f;
  208. InternalState.buffer[used++] = 0x80;
  209. free = 64 - used;
  210. if (free < 8) {
  211. memset(&InternalState.buffer[used], 0, free);
  212. body(ArrayRef(InternalState.buffer, 64));
  213. used = 0;
  214. free = 64;
  215. }
  216. memset(&InternalState.buffer[used], 0, free - 8);
  217. InternalState.lo <<= 3;
  218. support::endian::write32le(&InternalState.buffer[56], InternalState.lo);
  219. support::endian::write32le(&InternalState.buffer[60], InternalState.hi);
  220. body(ArrayRef(InternalState.buffer, 64));
  221. support::endian::write32le(&Result[0], InternalState.a);
  222. support::endian::write32le(&Result[4], InternalState.b);
  223. support::endian::write32le(&Result[8], InternalState.c);
  224. support::endian::write32le(&Result[12], InternalState.d);
  225. }
  226. MD5::MD5Result MD5::final() {
  227. MD5Result Result;
  228. final(Result);
  229. return Result;
  230. }
  231. MD5::MD5Result MD5::result() {
  232. auto StateToRestore = InternalState;
  233. auto Hash = final();
  234. // Restore the state
  235. InternalState = StateToRestore;
  236. return Hash;
  237. }
  238. SmallString<32> MD5::MD5Result::digest() const {
  239. SmallString<32> Str;
  240. toHex(*this, /*LowerCase*/ true, Str);
  241. return Str;
  242. }
  243. void MD5::stringifyResult(MD5Result &Result, SmallVectorImpl<char> &Str) {
  244. toHex(Result, /*LowerCase*/ true, Str);
  245. }
  246. MD5::MD5Result MD5::hash(ArrayRef<uint8_t> Data) {
  247. MD5 Hash;
  248. Hash.update(Data);
  249. MD5::MD5Result Res;
  250. Hash.final(Res);
  251. return Res;
  252. }