1 /*
2  * Copyright (C) 2001 Nikos Mavroyanopoulos
3  * Copyright (C) 2004 Hans Leidekker
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19 
20 /*
21  * This code implements the MD5 message-digest algorithm.
22  * It is based on code in the public domain written by Colin
23  * Plumb in 1993. The algorithm is due to Ron Rivest.
24  *
25  * Equivalent code is available from RSA Data Security, Inc.
26  * This code has been tested against that, and is equivalent,
27  * except that you don't need to include two pages of legalese
28  * with every copy.
29  *
30  * To compute the message digest of a chunk of bytes, declare an
31  * md5_ctx structure, pass it to md5_init, call md5_update as
32  * needed on buffers full of bytes, and then call md5_final, which
33  * will fill a supplied 16-byte array with the digest.
34  */
35 
36 #include "vkd3d_shader_private.h"
37 
38 #define DXBC_CHECKSUM_BLOCK_SIZE 64
39 
40 STATIC_ASSERT(sizeof(unsigned int) == 4);
41 
42 struct md5_ctx
43 {
44     unsigned int i[2];
45     unsigned int buf[4];
46     unsigned char in[DXBC_CHECKSUM_BLOCK_SIZE];
47     unsigned char digest[16];
48 };
49 
50 /* The four core functions - F1 is optimized somewhat */
51 
52 /* #define F1(x, y, z) (x & y | ~x & z) */
53 #define F1(x, y, z) (z ^ (x & (y ^ z)))
54 #define F2(x, y, z) F1(z, x, y)
55 #define F3(x, y, z) (x ^ y ^ z)
56 #define F4(x, y, z) (y ^ (x | ~z))
57 
58 /* This is the central step in the MD5 algorithm. */
59 #define MD5STEP(f, w, x, y, z, data, s) \
60         (w += f(x, y, z) + data,  w = w << s | w >> (32 - s),  w += x)
61 
62 /*
63  * The core of the MD5 algorithm, this alters an existing MD5 hash to
64  * reflect the addition of 16 longwords of new data. md5_update blocks
65  * the data and converts bytes into longwords for this routine.
66  */
md5_transform(unsigned int buf[4],const unsigned int in[16])67 static void md5_transform(unsigned int buf[4], const unsigned int in[16])
68 {
69     unsigned int a, b, c, d;
70 
71     a = buf[0];
72     b = buf[1];
73     c = buf[2];
74     d = buf[3];
75 
76     MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
77     MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
78     MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
79     MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
80     MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
81     MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
82     MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
83     MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
84     MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
85     MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
86     MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
87     MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
88     MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
89     MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
90     MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
91     MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
92 
93     MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
94     MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
95     MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
96     MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
97     MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
98     MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
99     MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
100     MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
101     MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
102     MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
103     MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
104     MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
105     MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
106     MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
107     MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
108     MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
109 
110     MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
111     MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
112     MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
113     MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
114     MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
115     MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
116     MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
117     MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
118     MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
119     MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
120     MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
121     MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
122     MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
123     MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
124     MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
125     MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
126 
127     MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
128     MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
129     MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
130     MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
131     MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
132     MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
133     MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
134     MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
135     MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
136     MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
137     MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
138     MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
139     MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
140     MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
141     MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
142     MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
143 
144     buf[0] += a;
145     buf[1] += b;
146     buf[2] += c;
147     buf[3] += d;
148 }
149 
150 /*
151  * Note: this code is harmless on little-endian machines.
152  */
byte_reverse(unsigned char * buf,unsigned longs)153 static void byte_reverse(unsigned char *buf, unsigned longs)
154 {
155     unsigned int t;
156 
157     do
158     {
159         t = ((unsigned)buf[3] << 8 | buf[2]) << 16 |
160             ((unsigned)buf[1] << 8 | buf[0]);
161         *(unsigned int *)buf = t;
162         buf += 4;
163     } while (--longs);
164 }
165 
166 /*
167  * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
168  * initialization constants.
169  */
md5_init(struct md5_ctx * ctx)170 static void md5_init(struct md5_ctx *ctx)
171 {
172     ctx->buf[0] = 0x67452301;
173     ctx->buf[1] = 0xefcdab89;
174     ctx->buf[2] = 0x98badcfe;
175     ctx->buf[3] = 0x10325476;
176 
177     ctx->i[0] = ctx->i[1] = 0;
178 }
179 
180 /*
181  * Update context to reflect the concatenation of another buffer full
182  * of bytes.
183  */
md5_update(struct md5_ctx * ctx,const unsigned char * buf,unsigned int len)184 static void md5_update(struct md5_ctx *ctx, const unsigned char *buf, unsigned int len)
185 {
186     unsigned int t;
187 
188     /* Update bitcount */
189     t = ctx->i[0];
190 
191     if ((ctx->i[0] = t + (len << 3)) < t)
192         ctx->i[1]++;        /* Carry from low to high */
193 
194     ctx->i[1] += len >> 29;
195     t = (t >> 3) & 0x3f;
196 
197     /* Handle any leading odd-sized chunks */
198     if (t)
199     {
200         unsigned char *p = (unsigned char *)ctx->in + t;
201         t = DXBC_CHECKSUM_BLOCK_SIZE - t;
202 
203         if (len < t)
204         {
205             memcpy(p, buf, len);
206             return;
207         }
208 
209         memcpy(p, buf, t);
210         byte_reverse(ctx->in, 16);
211 
212         md5_transform(ctx->buf, (unsigned int *)ctx->in);
213 
214         buf += t;
215         len -= t;
216     }
217 
218     /* Process data in 64-byte chunks */
219     while (len >= DXBC_CHECKSUM_BLOCK_SIZE)
220     {
221         memcpy(ctx->in, buf, DXBC_CHECKSUM_BLOCK_SIZE);
222         byte_reverse(ctx->in, 16);
223 
224         md5_transform(ctx->buf, (unsigned int *)ctx->in);
225 
226         buf += DXBC_CHECKSUM_BLOCK_SIZE;
227         len -= DXBC_CHECKSUM_BLOCK_SIZE;
228     }
229 
230     /* Handle any remaining bytes of data. */
231     memcpy(ctx->in, buf, len);
232 }
233 
dxbc_checksum_final(struct md5_ctx * ctx)234 static void dxbc_checksum_final(struct md5_ctx *ctx)
235 {
236     unsigned int padding;
237     unsigned int length;
238     unsigned int count;
239     unsigned char *p;
240 
241     /* Compute number of bytes mod 64 */
242     count = (ctx->i[0] >> 3) & 0x3F;
243 
244     /* Set the first char of padding to 0x80.  This is safe since there is
245        always at least one byte free */
246     p = ctx->in + count;
247     *p++ = 0x80;
248     ++count;
249 
250     /* Bytes of padding needed to make 64 bytes */
251     padding = DXBC_CHECKSUM_BLOCK_SIZE - count;
252 
253     /* Pad out to 56 mod 64 */
254     if (padding < 8)
255     {
256         /* Two lots of padding:  Pad the first block to 64 bytes */
257         memset(p, 0, padding);
258         byte_reverse(ctx->in, 16);
259         md5_transform(ctx->buf, (unsigned int *)ctx->in);
260 
261         /* Now fill the next block */
262         memset(ctx->in, 0, DXBC_CHECKSUM_BLOCK_SIZE);
263     }
264     else
265     {
266         /* Make place for bitcount at the beginning of the block */
267         memmove(&ctx->in[4], ctx->in, count);
268 
269         /* Pad block to 60 bytes */
270         memset(p + 4, 0, padding - 4);
271     }
272 
273     /* Append length in bits and transform */
274     length = ctx->i[0];
275     memcpy(&ctx->in[0], &length, sizeof(length));
276     byte_reverse(&ctx->in[4], 14);
277     length = ctx->i[0] >> 2 | 0x1;
278     memcpy(&ctx->in[DXBC_CHECKSUM_BLOCK_SIZE - 4], &length, sizeof(length));
279 
280     md5_transform(ctx->buf, (unsigned int *)ctx->in);
281     byte_reverse((unsigned char *)ctx->buf, 4);
282     memcpy(ctx->digest, ctx->buf, 16);
283 }
284 
285 #define DXBC_CHECKSUM_SKIP_BYTE_COUNT 20
286 
vkd3d_compute_dxbc_checksum(const void * dxbc,size_t size,uint32_t checksum[4])287 void vkd3d_compute_dxbc_checksum(const void *dxbc, size_t size, uint32_t checksum[4])
288 {
289     const uint8_t *ptr = dxbc;
290     struct md5_ctx ctx;
291 
292     assert(size > DXBC_CHECKSUM_SKIP_BYTE_COUNT);
293     ptr += DXBC_CHECKSUM_SKIP_BYTE_COUNT;
294     size -= DXBC_CHECKSUM_SKIP_BYTE_COUNT;
295 
296     md5_init(&ctx);
297     md5_update(&ctx, ptr, size);
298     dxbc_checksum_final(&ctx);
299 
300     memcpy(checksum, ctx.digest, sizeof(ctx.digest));
301 }
302