tsan_md5.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. //===-- tsan_md5.cc -------------------------------------------------------===//
  2. //
  3. // This file is distributed under the University of Illinois Open Source
  4. // License. See LICENSE.TXT for details.
  5. //
  6. //===----------------------------------------------------------------------===//
  7. //
  8. // This file is a part of ThreadSanitizer (TSan), a race detector.
  9. //
  10. //===----------------------------------------------------------------------===//
  11. #include "tsan_defs.h"
  12. namespace __tsan {
  13. #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
  14. #define G(x, y, z) ((y) ^ ((z) & ((x) ^ (y))))
  15. #define H(x, y, z) ((x) ^ (y) ^ (z))
  16. #define I(x, y, z) ((y) ^ ((x) | ~(z)))
  17. #define STEP(f, a, b, c, d, x, t, s) \
  18. (a) += f((b), (c), (d)) + (x) + (t); \
  19. (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \
  20. (a) += (b);
  21. #define SET(n) \
  22. (*(MD5_u32plus *)&ptr[(n) * 4])
  23. #define GET(n) \
  24. SET(n)
  25. typedef unsigned int MD5_u32plus;
  26. typedef unsigned long ulong_t; // NOLINT
  27. typedef struct {
  28. MD5_u32plus lo, hi;
  29. MD5_u32plus a, b, c, d;
  30. unsigned char buffer[64];
  31. MD5_u32plus block[16];
  32. } MD5_CTX;
  33. static void *body(MD5_CTX *ctx, void *data, ulong_t size) {
  34. unsigned char *ptr;
  35. MD5_u32plus a, b, c, d;
  36. MD5_u32plus saved_a, saved_b, saved_c, saved_d;
  37. ptr = (unsigned char*)data;
  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. void MD5_Init(MD5_CTX *ctx) {
  124. ctx->a = 0x67452301;
  125. ctx->b = 0xefcdab89;
  126. ctx->c = 0x98badcfe;
  127. ctx->d = 0x10325476;
  128. ctx->lo = 0;
  129. ctx->hi = 0;
  130. }
  131. void MD5_Update(MD5_CTX *ctx, void *data, ulong_t size) {
  132. MD5_u32plus saved_lo;
  133. ulong_t used, free;
  134. saved_lo = ctx->lo;
  135. if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo)
  136. ctx->hi++;
  137. ctx->hi += size >> 29;
  138. used = saved_lo & 0x3f;
  139. if (used) {
  140. free = 64 - used;
  141. if (size < free) {
  142. internal_memcpy(&ctx->buffer[used], data, size);
  143. return;
  144. }
  145. internal_memcpy(&ctx->buffer[used], data, free);
  146. data = (unsigned char *)data + free;
  147. size -= free;
  148. body(ctx, ctx->buffer, 64);
  149. }
  150. if (size >= 64) {
  151. data = body(ctx, data, size & ~(ulong_t)0x3f);
  152. size &= 0x3f;
  153. }
  154. internal_memcpy(ctx->buffer, data, size);
  155. }
  156. void MD5_Final(unsigned char *result, MD5_CTX *ctx) {
  157. ulong_t used, free;
  158. used = ctx->lo & 0x3f;
  159. ctx->buffer[used++] = 0x80;
  160. free = 64 - used;
  161. if (free < 8) {
  162. internal_memset(&ctx->buffer[used], 0, free);
  163. body(ctx, ctx->buffer, 64);
  164. used = 0;
  165. free = 64;
  166. }
  167. internal_memset(&ctx->buffer[used], 0, free - 8);
  168. ctx->lo <<= 3;
  169. ctx->buffer[56] = ctx->lo;
  170. ctx->buffer[57] = ctx->lo >> 8;
  171. ctx->buffer[58] = ctx->lo >> 16;
  172. ctx->buffer[59] = ctx->lo >> 24;
  173. ctx->buffer[60] = ctx->hi;
  174. ctx->buffer[61] = ctx->hi >> 8;
  175. ctx->buffer[62] = ctx->hi >> 16;
  176. ctx->buffer[63] = ctx->hi >> 24;
  177. body(ctx, ctx->buffer, 64);
  178. result[0] = ctx->a;
  179. result[1] = ctx->a >> 8;
  180. result[2] = ctx->a >> 16;
  181. result[3] = ctx->a >> 24;
  182. result[4] = ctx->b;
  183. result[5] = ctx->b >> 8;
  184. result[6] = ctx->b >> 16;
  185. result[7] = ctx->b >> 24;
  186. result[8] = ctx->c;
  187. result[9] = ctx->c >> 8;
  188. result[10] = ctx->c >> 16;
  189. result[11] = ctx->c >> 24;
  190. result[12] = ctx->d;
  191. result[13] = ctx->d >> 8;
  192. result[14] = ctx->d >> 16;
  193. result[15] = ctx->d >> 24;
  194. internal_memset(ctx, 0, sizeof(*ctx));
  195. }
  196. MD5Hash md5_hash(const void *data, uptr size) {
  197. MD5Hash res;
  198. MD5_CTX ctx;
  199. MD5_Init(&ctx);
  200. MD5_Update(&ctx, (void*)data, size);
  201. MD5_Final((unsigned char*)&res.hash[0], &ctx);
  202. return res;
  203. }
  204. } // namespace __tsan