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