1 /*------------------------------------------------------------------------
2  * Vulkan Conformance Tests
3  * ------------------------
4  *
5  * Copyright (c) 2023 The Khronos Group Inc.
6  * Copyright (c) 2023 The SQLite Project.
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *	  http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  *//*!
21  * \file
22  * \brief Utilities for calculating MD5 checksums.
23  *
24  * This file was modified from Chromium,
25  * https://chromium.googlesource.com/chromium/src/base/+/7ef85b701132474f71e6369f081a2fb84582ee88/md5.cc
26  *
27  * This code implements the MD5 message-digest algorithm.
28  * The algorithm is due to Ron Rivest.  This code was
29  * written by Colin Plumb in 1993, no copyright is claimed.
30  * This code is in the public domain; do with it what you wish.
31  *
32  * Equivalent code is available from RSA Data Security, Inc.
33  * This code has been tested against that, and is equivalent,
34  * except that you don't need to include two pages of legalese
35  * with every copy.
36  *
37  * To compute the message digest of a chunk of bytes, declare an
38  * MD5Context structure, pass it to MD5Init, call MD5Update as
39  * needed on buffers full of bytes, and then call MD5Final, which
40  * will fill a supplied 16-byte array with the digest.
41  * --------------------------------------------------------------------*/
42 #include "vkMd5Sum.hpp"
43 #include <deMemory.h>
44 
45 namespace vk
46 {
47 
48 struct Context
49 {
50 	deUint32 buf[4];
51 	deUint32 bits[2];
52 	deUint8	 in[64];
53 };
54 
55 /*
56  * Note: this code is harmless on little-endian machines.
57  */
byteReverse(deUint8* buf, unsigned longs)58 void byteReverse(deUint8* buf, unsigned longs)
59 {
60 	deUint32 t;
61 	do
62 	{
63 		t				= (deUint32)((unsigned)buf[3] << 8 | buf[2]) << 16 | ((unsigned)buf[1] << 8 | buf[0]);
64 		*(deUint32*)buf = t;
65 		buf += 4;
66 	}
67 	while (--longs);
68 }
69 
70 /* The four core functions - F1 is optimized somewhat */
71 /* #define F1(x, y, z) (x & y | ~x & z) */
72 #define F1(x, y, z) (z ^ (x & (y ^ z)))
73 #define F2(x, y, z) F1(z, x, y)
74 #define F3(x, y, z) (x ^ y ^ z)
75 #define F4(x, y, z) (y ^ (x | ~z))
76 /* This is the central step in the MD5 algorithm. */
77 #define MD5STEP(f, w, x, y, z, data, s) (w += f(x, y, z) + data, w = w << s | w >> (32 - s), w += x)
78 /*
79  * The core of the MD5 algorithm, this alters an existing MD5 hash to
80  * reflect the addition of 16 longwords of new data.  MD5Update blocks
81  * the data and converts bytes into longwords for this routine.
82  */
MD5Transform(deUint32 buf[4], const deUint32 in[16])83 void MD5Transform(deUint32 buf[4], const deUint32 in[16])
84 {
85 	deUint32 a, b, c, d;
86 	a = buf[0];
87 	b = buf[1];
88 	c = buf[2];
89 	d = buf[3];
90 	MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
91 	MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
92 	MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
93 	MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
94 	MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
95 	MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
96 	MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
97 	MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
98 	MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
99 	MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
100 	MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
101 	MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
102 	MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
103 	MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
104 	MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
105 	MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
106 	MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
107 	MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
108 	MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
109 	MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
110 	MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
111 	MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
112 	MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
113 	MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
114 	MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
115 	MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
116 	MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
117 	MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
118 	MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
119 	MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
120 	MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
121 	MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
122 	MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
123 	MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
124 	MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
125 	MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
126 	MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
127 	MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
128 	MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
129 	MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
130 	MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
131 	MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
132 	MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
133 	MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
134 	MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
135 	MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
136 	MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
137 	MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
138 	MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
139 	MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
140 	MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
141 	MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
142 	MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
143 	MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
144 	MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
145 	MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
146 	MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
147 	MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
148 	MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
149 	MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
150 	MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
151 	MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
152 	MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
153 	MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
154 	buf[0] += a;
155 	buf[1] += b;
156 	buf[2] += c;
157 	buf[3] += d;
158 }
159 
160 /*
161  * Start MD5 accumulation.  Set bit count to 0 and buffer to mysterious
162  * initialization constants.
163  */
MD5Init(MD5Context* context)164 void MD5Init(MD5Context* context)
165 {
166 	struct Context* ctx = (struct Context*)context;
167 	ctx->buf[0]			= 0x67452301;
168 	ctx->buf[1]			= 0xefcdab89;
169 	ctx->buf[2]			= 0x98badcfe;
170 	ctx->buf[3]			= 0x10325476;
171 	ctx->bits[0]		= 0;
172 	ctx->bits[1]		= 0;
173 }
174 
175 /*
176  * Update context to reflect the concatenation of another buffer full
177  * of bytes.
178  */
MD5Update(MD5Context* context, const deUint8* data, std::size_t len)179 void MD5Update(MD5Context* context, const deUint8* data, std::size_t len)
180 {
181 	const deUint8*	inbuf = data;
182 	struct Context* ctx	  = (struct Context*)context;
183 	const deUint8*	buf	  = (const deUint8*)inbuf;
184 	deUint32		t;
185 	/* Update bitcount */
186 	t = ctx->bits[0];
187 	if ((ctx->bits[0] = t + ((deUint32)len << 3)) < t)
188 		ctx->bits[1]++;	 /* Carry from low to high */
189 	ctx->bits[1] += static_cast<deUint32>(len >> 29);
190 	t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
191 	/* Handle any leading odd-sized chunks */
192 	if (t)
193 	{
194 		deUint8* p = (deUint8*)ctx->in + t;
195 		t		   = 64 - t;
196 		if (len < t)
197 		{
198 			deMemcpy(p, buf, len);
199 			return;
200 		}
201 		deMemcpy(p, buf, t);
202 		byteReverse(ctx->in, 16);
203 		MD5Transform(ctx->buf, (deUint32*)ctx->in);
204 		buf += t;
205 		len -= t;
206 	}
207 	/* Process data in 64-byte chunks */
208 	while (len >= 64)
209 	{
210 		deMemcpy(ctx->in, buf, 64);
211 		byteReverse(ctx->in, 16);
212 		MD5Transform(ctx->buf, (deUint32*)ctx->in);
213 		buf += 64;
214 		len -= 64;
215 	}
216 	/* Handle any remaining bytes of data. */
217 	deMemcpy(ctx->in, buf, len);
218 }
219 
220 /*
221  * Final wrapup - pad to 64-byte boundary with the bit pattern
222  * 1 0* (64-bit count of bits processed, MSB-first)
223  */
MD5Final(MD5Digest* digest, MD5Context* context)224 void MD5Final(MD5Digest* digest, MD5Context* context)
225 {
226 	struct Context* ctx = (struct Context*)context;
227 	unsigned		count;
228 	deUint8*		p;
229 	/* Compute number of bytes mod 64 */
230 	count = (ctx->bits[0] >> 3) & 0x3F;
231 	/* Set the first char of padding to 0x80.  This is safe since there is
232 	   always at least one byte free */
233 	p	 = ctx->in + count;
234 	*p++ = 0x80;
235 	/* Bytes of padding needed to make 64 bytes */
236 	count = 64 - 1 - count;
237 	/* Pad out to 56 mod 64 */
238 	if (count < 8)
239 	{
240 		/* Two lots of padding:  Pad the first block to 64 bytes */
241 		deMemset(p, 0, count);
242 		byteReverse(ctx->in, 16);
243 		MD5Transform(ctx->buf, (deUint32*)ctx->in);
244 		/* Now fill the next block with 56 bytes */
245 		deMemset(ctx->in, 0, 56);
246 	}
247 	else
248 	{
249 		/* Pad block to 56 bytes */
250 		deMemset(p, 0, count - 8);
251 	}
252 	byteReverse(ctx->in, 14);
253 	/* Append length in bits and transform */
254 	((deUint32*)ctx->in)[14] = ctx->bits[0];
255 	((deUint32*)ctx->in)[15] = ctx->bits[1];
256 	MD5Transform(ctx->buf, (deUint32*)ctx->in);
257 	byteReverse((deUint8*)ctx->buf, 4);
258 	deMemcpy(digest->a, ctx->buf, 16);
259 	deMemset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */
260 }
261 
MD5DigestToBase16(const MD5Digest& digest)262 std::string MD5DigestToBase16(const MD5Digest& digest)
263 {
264 	static char const zEncode[] = "0123456789abcdef";
265 	std::string		  ret;
266 	ret.resize(32);
267 	int j = 0;
268 	for (int i = 0; i < 16; i++)
269 	{
270 		int a	 = digest.a[i];
271 		ret[j++] = zEncode[(a >> 4) & 0xf];
272 		ret[j++] = zEncode[a & 0xf];
273 	}
274 	return ret;
275 }
276 
MD5Sum(const void* data, std::size_t length, MD5Digest* digest)277 void MD5Sum(const void* data, std::size_t length, MD5Digest* digest)
278 {
279 	MD5Context ctx;
280 	MD5Init(&ctx);
281 	MD5Update(&ctx, reinterpret_cast<const deUint8*>(data), length);
282 	MD5Final(digest, &ctx);
283 }
284 
MD5SumBase16(const void* data, std::size_t length)285 std::string MD5SumBase16(const void* data, std::size_t length)
286 {
287 	MD5Digest digest;
288 	MD5Sum(data, length, &digest);
289 	return MD5DigestToBase16(digest);
290 }
291 
292 } // namespace vkt
293