tsan_md5.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. //===-- tsan_md5.cpp ------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file is a part of ThreadSanitizer (TSan), a race detector.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "tsan_defs.h"
  13. namespace __tsan {
  14. #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
  15. #define G(x, y, z) ((y) ^ ((z) & ((x) ^ (y))))
  16. #define H(x, y, z) ((x) ^ (y) ^ (z))
  17. #define I(x, y, z) ((y) ^ ((x) | ~(z)))
  18. #define STEP(f, a, b, c, d, x, t, s) \
  19. (a) += f((b), (c), (d)) + (x) + (t); \
  20. (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \
  21. (a) += (b);
  22. #define SET(n) \
  23. (*(const MD5_u32plus *)&ptr[(n) * 4])
  24. #define GET(n) \
  25. SET(n)
  26. typedef unsigned int MD5_u32plus;
  27. typedef unsigned long ulong_t;
  28. typedef struct {
  29. MD5_u32plus lo, hi;
  30. MD5_u32plus a, b, c, d;
  31. unsigned char buffer[64];
  32. MD5_u32plus block[16];
  33. } MD5_CTX;
  34. static const void *body(MD5_CTX *ctx, const void *data, ulong_t size) {
  35. const unsigned char *ptr = (const unsigned char *)data;
  36. MD5_u32plus a, b, c, d;
  37. MD5_u32plus saved_a, saved_b, saved_c, saved_d;
  38. a = ctx->a;
  39. b = ctx->b;
  40. c = ctx->c;
  41. d = ctx->d;
  42. do {
  43. saved_a = a;
  44. saved_b = b;
  45. saved_c = c;
  46. saved_d = d;
  47. STEP(F, a, b, c, d, SET(0), 0xd76aa478, 7)
  48. STEP(F, d, a, b, c, SET(1), 0xe8c7b756, 12)
  49. STEP(F, c, d, a, b, SET(2), 0x242070db, 17)
  50. STEP(F, b, c, d, a, SET(3), 0xc1bdceee, 22)
  51. STEP(F, a, b, c, d, SET(4), 0xf57c0faf, 7)
  52. STEP(F, d, a, b, c, SET(5), 0x4787c62a, 12)
  53. STEP(F, c, d, a, b, SET(6), 0xa8304613, 17)
  54. STEP(F, b, c, d, a, SET(7), 0xfd469501, 22)
  55. STEP(F, a, b, c, d, SET(8), 0x698098d8, 7)
  56. STEP(F, d, a, b, c, SET(9), 0x8b44f7af, 12)
  57. STEP(F, c, d, a, b, SET(10), 0xffff5bb1, 17)
  58. STEP(F, b, c, d, a, SET(11), 0x895cd7be, 22)
  59. STEP(F, a, b, c, d, SET(12), 0x6b901122, 7)
  60. STEP(F, d, a, b, c, SET(13), 0xfd987193, 12)
  61. STEP(F, c, d, a, b, SET(14), 0xa679438e, 17)
  62. STEP(F, b, c, d, a, SET(15), 0x49b40821, 22)
  63. STEP(G, a, b, c, d, GET(1), 0xf61e2562, 5)
  64. STEP(G, d, a, b, c, GET(6), 0xc040b340, 9)
  65. STEP(G, c, d, a, b, GET(11), 0x265e5a51, 14)
  66. STEP(G, b, c, d, a, GET(0), 0xe9b6c7aa, 20)
  67. STEP(G, a, b, c, d, GET(5), 0xd62f105d, 5)
  68. STEP(G, d, a, b, c, GET(10), 0x02441453, 9)
  69. STEP(G, c, d, a, b, GET(15), 0xd8a1e681, 14)
  70. STEP(G, b, c, d, a, GET(4), 0xe7d3fbc8, 20)
  71. STEP(G, a, b, c, d, GET(9), 0x21e1cde6, 5)
  72. STEP(G, d, a, b, c, GET(14), 0xc33707d6, 9)
  73. STEP(G, c, d, a, b, GET(3), 0xf4d50d87, 14)
  74. STEP(G, b, c, d, a, GET(8), 0x455a14ed, 20)
  75. STEP(G, a, b, c, d, GET(13), 0xa9e3e905, 5)
  76. STEP(G, d, a, b, c, GET(2), 0xfcefa3f8, 9)
  77. STEP(G, c, d, a, b, GET(7), 0x676f02d9, 14)
  78. STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20)
  79. STEP(H, a, b, c, d, GET(5), 0xfffa3942, 4)
  80. STEP(H, d, a, b, c, GET(8), 0x8771f681, 11)
  81. STEP(H, c, d, a, b, GET(11), 0x6d9d6122, 16)
  82. STEP(H, b, c, d, a, GET(14), 0xfde5380c, 23)
  83. STEP(H, a, b, c, d, GET(1), 0xa4beea44, 4)
  84. STEP(H, d, a, b, c, GET(4), 0x4bdecfa9, 11)
  85. STEP(H, c, d, a, b, GET(7), 0xf6bb4b60, 16)
  86. STEP(H, b, c, d, a, GET(10), 0xbebfbc70, 23)
  87. STEP(H, a, b, c, d, GET(13), 0x289b7ec6, 4)
  88. STEP(H, d, a, b, c, GET(0), 0xeaa127fa, 11)
  89. STEP(H, c, d, a, b, GET(3), 0xd4ef3085, 16)
  90. STEP(H, b, c, d, a, GET(6), 0x04881d05, 23)
  91. STEP(H, a, b, c, d, GET(9), 0xd9d4d039, 4)
  92. STEP(H, d, a, b, c, GET(12), 0xe6db99e5, 11)
  93. STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16)
  94. STEP(H, b, c, d, a, GET(2), 0xc4ac5665, 23)
  95. STEP(I, a, b, c, d, GET(0), 0xf4292244, 6)
  96. STEP(I, d, a, b, c, GET(7), 0x432aff97, 10)
  97. STEP(I, c, d, a, b, GET(14), 0xab9423a7, 15)
  98. STEP(I, b, c, d, a, GET(5), 0xfc93a039, 21)
  99. STEP(I, a, b, c, d, GET(12), 0x655b59c3, 6)
  100. STEP(I, d, a, b, c, GET(3), 0x8f0ccc92, 10)
  101. STEP(I, c, d, a, b, GET(10), 0xffeff47d, 15)
  102. STEP(I, b, c, d, a, GET(1), 0x85845dd1, 21)
  103. STEP(I, a, b, c, d, GET(8), 0x6fa87e4f, 6)
  104. STEP(I, d, a, b, c, GET(15), 0xfe2ce6e0, 10)
  105. STEP(I, c, d, a, b, GET(6), 0xa3014314, 15)
  106. STEP(I, b, c, d, a, GET(13), 0x4e0811a1, 21)
  107. STEP(I, a, b, c, d, GET(4), 0xf7537e82, 6)
  108. STEP(I, d, a, b, c, GET(11), 0xbd3af235, 10)
  109. STEP(I, c, d, a, b, GET(2), 0x2ad7d2bb, 15)
  110. STEP(I, b, c, d, a, GET(9), 0xeb86d391, 21)
  111. a += saved_a;
  112. b += saved_b;
  113. c += saved_c;
  114. d += saved_d;
  115. ptr += 64;
  116. } while (size -= 64);
  117. ctx->a = a;
  118. ctx->b = b;
  119. ctx->c = c;
  120. ctx->d = d;
  121. return ptr;
  122. }
  123. #undef F
  124. #undef G
  125. #undef H
  126. #undef I
  127. #undef STEP
  128. #undef SET
  129. #undef GET
  130. void MD5_Init(MD5_CTX *ctx) {
  131. ctx->a = 0x67452301;
  132. ctx->b = 0xefcdab89;
  133. ctx->c = 0x98badcfe;
  134. ctx->d = 0x10325476;
  135. ctx->lo = 0;
  136. ctx->hi = 0;
  137. }
  138. void MD5_Update(MD5_CTX *ctx, const void *data, ulong_t size) {
  139. MD5_u32plus saved_lo;
  140. ulong_t used, free;
  141. saved_lo = ctx->lo;
  142. if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo)
  143. ctx->hi++;
  144. ctx->hi += size >> 29;
  145. used = saved_lo & 0x3f;
  146. if (used) {
  147. free = 64 - used;
  148. if (size < free) {
  149. internal_memcpy(&ctx->buffer[used], data, size);
  150. return;
  151. }
  152. internal_memcpy(&ctx->buffer[used], data, free);
  153. data = (const unsigned char *)data + free;
  154. size -= free;
  155. body(ctx, ctx->buffer, 64);
  156. }
  157. if (size >= 64) {
  158. data = body(ctx, data, size & ~(ulong_t)0x3f);
  159. size &= 0x3f;
  160. }
  161. internal_memcpy(ctx->buffer, data, size);
  162. }
  163. void MD5_Final(unsigned char *result, MD5_CTX *ctx) {
  164. ulong_t used, free;
  165. used = ctx->lo & 0x3f;
  166. ctx->buffer[used++] = 0x80;
  167. free = 64 - used;
  168. if (free < 8) {
  169. internal_memset(&ctx->buffer[used], 0, free);
  170. body(ctx, ctx->buffer, 64);
  171. used = 0;
  172. free = 64;
  173. }
  174. internal_memset(&ctx->buffer[used], 0, free - 8);
  175. ctx->lo <<= 3;
  176. ctx->buffer[56] = ctx->lo;
  177. ctx->buffer[57] = ctx->lo >> 8;
  178. ctx->buffer[58] = ctx->lo >> 16;
  179. ctx->buffer[59] = ctx->lo >> 24;
  180. ctx->buffer[60] = ctx->hi;
  181. ctx->buffer[61] = ctx->hi >> 8;
  182. ctx->buffer[62] = ctx->hi >> 16;
  183. ctx->buffer[63] = ctx->hi >> 24;
  184. body(ctx, ctx->buffer, 64);
  185. result[0] = ctx->a;
  186. result[1] = ctx->a >> 8;
  187. result[2] = ctx->a >> 16;
  188. result[3] = ctx->a >> 24;
  189. result[4] = ctx->b;
  190. result[5] = ctx->b >> 8;
  191. result[6] = ctx->b >> 16;
  192. result[7] = ctx->b >> 24;
  193. result[8] = ctx->c;
  194. result[9] = ctx->c >> 8;
  195. result[10] = ctx->c >> 16;
  196. result[11] = ctx->c >> 24;
  197. result[12] = ctx->d;
  198. result[13] = ctx->d >> 8;
  199. result[14] = ctx->d >> 16;
  200. result[15] = ctx->d >> 24;
  201. internal_memset(ctx, 0, sizeof(*ctx));
  202. }
  203. MD5Hash md5_hash(const void *data, uptr size) {
  204. MD5Hash res;
  205. MD5_CTX ctx;
  206. MD5_Init(&ctx);
  207. MD5_Update(&ctx, data, size);
  208. MD5_Final((unsigned char*)&res.hash[0], &ctx);
  209. return res;
  210. }
  211. } // namespace __tsan