1/*
2** This code taken from the SQLite test library. Originally found on
3** the internet. The original header comment follows this comment.
4** The code is largerly unchanged, but there have been some modifications.
5*/
6/*
7 * This code implements the MD5 message-digest algorithm.
8 * The algorithm is due to Ron Rivest. This code was
9 * written by Colin Plumb in 1993, no copyright is claimed.
10 * This code is in the public domain; do with it what you wish.
11 *
12 * Equivalent code is available from RSA Data Security, Inc.
13 * This code has been tested against that, and is equivalent,
14 * except that you don't need to include two pages of legalese
15 * with every copy.
16 *
17 * To compute the message digest of a chunk of bytes, declare an
18 * MD5Context structure, pass it to MD5Init, call MD5Update as
19 * needed on buffers full of bytes, and then call MD5Final, which
20 * will fill a supplied 16-byte array with the digest.
21 */
22#include "duckdb/common/crypto/md5.hpp"
23
24namespace duckdb {
25
26/*
27 * Note: this code is harmless on little-endian machines.
28 */
29static void ByteReverse(unsigned char *buf, unsigned longs) {
30 uint32_t t;
31 do {
32 t = (uint32_t)((unsigned)buf[3] << 8 | buf[2]) << 16 | ((unsigned)buf[1] << 8 | buf[0]);
33 *reinterpret_cast<uint32_t *>(buf) = t;
34 buf += 4;
35 } while (--longs);
36}
37/* The four core functions - F1 is optimized somewhat */
38
39/* #define F1(x, y, z) (x & y | ~x & z) */
40#define F1(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
41#define F2(x, y, z) F1(z, x, y)
42#define F3(x, y, z) ((x) ^ (y) ^ (z))
43#define F4(x, y, z) ((y) ^ ((x) | ~(z)))
44
45/* This is the central step in the MD5 algorithm. */
46#define MD5STEP(f, w, x, y, z, data, s) ((w) += f(x, y, z) + (data), (w) = (w) << (s) | (w) >> (32 - (s)), (w) += (x))
47
48/*
49 * The core of the MD5 algorithm, this alters an existing MD5 hash to
50 * reflect the addition of 16 longwords of new data. MD5Update blocks
51 * the data and converts bytes into longwords for this routine.
52 */
53static void MD5Transform(uint32_t buf[4], const uint32_t in[16]) {
54 uint32_t a, b, c, d;
55
56 a = buf[0];
57 b = buf[1];
58 c = buf[2];
59 d = buf[3];
60
61 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
62 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
63 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
64 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
65 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
66 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
67 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
68 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
69 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
70 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
71 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
72 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
73 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
74 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
75 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
76 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
77
78 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
79 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
80 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
81 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
82 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
83 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
84 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
85 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
86 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
87 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
88 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
89 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
90 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
91 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
92 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
93 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
94
95 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
96 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
97 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
98 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
99 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
100 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
101 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
102 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
103 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
104 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
105 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
106 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
107 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
108 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
109 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
110 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
111
112 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
113 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
114 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
115 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
116 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
117 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
118 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
119 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
120 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
121 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
122 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
123 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
124 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
125 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
126 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
127 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
128
129 buf[0] += a;
130 buf[1] += b;
131 buf[2] += c;
132 buf[3] += d;
133}
134
135/*
136 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
137 * initialization constants.
138 */
139MD5Context::MD5Context() {
140 buf[0] = 0x67452301;
141 buf[1] = 0xefcdab89;
142 buf[2] = 0x98badcfe;
143 buf[3] = 0x10325476;
144 bits[0] = 0;
145 bits[1] = 0;
146}
147
148/*
149 * Update context to reflect the concatenation of another buffer full
150 * of bytes.
151 */
152void MD5Context::MD5Update(const_data_ptr_t input, idx_t len) {
153 uint32_t t;
154
155 /* Update bitcount */
156
157 t = bits[0];
158 if ((bits[0] = t + ((uint32_t)len << 3)) < t) {
159 bits[1]++; /* Carry from low to high */
160 }
161 bits[1] += len >> 29;
162
163 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
164
165 /* Handle any leading odd-sized chunks */
166
167 if (t) {
168 unsigned char *p = (unsigned char *)in + t;
169
170 t = 64 - t;
171 if (len < t) {
172 memcpy(dest: p, src: input, n: len);
173 return;
174 }
175 memcpy(dest: p, src: input, n: t);
176 ByteReverse(buf: in, longs: 16);
177 MD5Transform(buf, in: reinterpret_cast<uint32_t *>(in));
178 input += t;
179 len -= t;
180 }
181
182 /* Process data in 64-byte chunks */
183
184 while (len >= 64) {
185 memcpy(dest: in, src: input, n: 64);
186 ByteReverse(buf: in, longs: 16);
187 MD5Transform(buf, in: reinterpret_cast<uint32_t *>(in));
188 input += 64;
189 len -= 64;
190 }
191
192 /* Handle any remaining bytes of data. */
193 memcpy(dest: in, src: input, n: len);
194}
195
196/*
197 * Final wrapup - pad to 64-byte boundary with the bit pattern
198 * 1 0* (64-bit count of bits processed, MSB-first)
199 */
200void MD5Context::Finish(data_ptr_t out_digest) {
201 unsigned count;
202 unsigned char *p;
203
204 /* Compute number of bytes mod 64 */
205 count = (bits[0] >> 3) & 0x3F;
206
207 /* Set the first char of padding to 0x80. This is safe since there is
208 always at least one byte free */
209 p = in + count;
210 *p++ = 0x80;
211
212 /* Bytes of padding needed to make 64 bytes */
213 count = 64 - 1 - count;
214
215 /* Pad out to 56 mod 64 */
216 if (count < 8) {
217 /* Two lots of padding: Pad the first block to 64 bytes */
218 memset(s: p, c: 0, n: count);
219 ByteReverse(buf: in, longs: 16);
220 MD5Transform(buf, in: reinterpret_cast<uint32_t *>(in));
221
222 /* Now fill the next block with 56 bytes */
223 memset(s: in, c: 0, n: 56);
224 } else {
225 /* Pad block to 56 bytes */
226 memset(s: p, c: 0, n: count - 8);
227 }
228 ByteReverse(buf: in, longs: 14);
229
230 /* Append length in bits and transform */
231 (reinterpret_cast<uint32_t *>(in))[14] = bits[0];
232 (reinterpret_cast<uint32_t *>(in))[15] = bits[1];
233
234 MD5Transform(buf, in: reinterpret_cast<uint32_t *>(in));
235 ByteReverse(buf: reinterpret_cast<unsigned char *>(buf), longs: 4);
236 memcpy(dest: out_digest, src: buf, n: 16);
237}
238
239void MD5Context::DigestToBase16(const_data_ptr_t digest, char *zbuf) {
240 static char const HEX_CODES[] = "0123456789abcdef";
241 int i, j;
242
243 for (j = i = 0; i < 16; i++) {
244 int a = digest[i];
245 zbuf[j++] = HEX_CODES[(a >> 4) & 0xf];
246 zbuf[j++] = HEX_CODES[a & 0xf];
247 }
248}
249
250void MD5Context::FinishHex(char *out_digest) {
251 data_t digest[MD5_HASH_LENGTH_BINARY];
252 Finish(out_digest: digest);
253 DigestToBase16(digest, zbuf: out_digest);
254}
255
256string MD5Context::FinishHex() {
257 char digest[MD5_HASH_LENGTH_TEXT];
258 FinishHex(out_digest: digest);
259 return string(digest, MD5_HASH_LENGTH_TEXT);
260}
261
262void MD5Context::Add(const char *data) {
263 MD5Update(input: const_data_ptr_cast(src: data), len: strlen(s: data));
264}
265
266} // namespace duckdb
267