ReactOS 0.4.17-dev-934-g091855f
checksum.c
Go to the documentation of this file.
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/*
37 * DXBC uses a variation of the MD5 algorithm, which only changes the way
38 * the message is padded in the final step.
39 */
40
42
43#define DXBC_CHECKSUM_BLOCK_SIZE 64
44
45STATIC_ASSERT(sizeof(unsigned int) == 4);
46
47struct md5_ctx
48{
49 unsigned int i[2];
50 unsigned int buf[4];
51 unsigned char in[DXBC_CHECKSUM_BLOCK_SIZE];
52 unsigned char digest[16];
53};
54
55/* The four core functions - F1 is optimized somewhat */
56
57/* #define F1(x, y, z) (x & y | ~x & z) */
58#define F1(x, y, z) (z ^ (x & (y ^ z)))
59#define F2(x, y, z) F1(z, x, y)
60#define F3(x, y, z) (x ^ y ^ z)
61#define F4(x, y, z) (y ^ (x | ~z))
62
63/* This is the central step in the MD5 algorithm. */
64#define MD5STEP(f, w, x, y, z, data, s) \
65 (w += f(x, y, z) + data, w = w << s | w >> (32 - s), w += x)
66
67/*
68 * The core of the MD5 algorithm, this alters an existing MD5 hash to
69 * reflect the addition of 16 longwords of new data. md5_update blocks
70 * the data and converts bytes into longwords for this routine.
71 */
72static void md5_transform(unsigned int buf[4], const unsigned int in[16])
73{
74 unsigned int a, b, c, d;
75
76 a = buf[0];
77 b = buf[1];
78 c = buf[2];
79 d = buf[3];
80
81 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
82 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
83 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
84 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
85 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
86 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
87 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
88 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
89 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
90 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
91 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
92 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
93 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
94 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
95 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
96 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
97
98 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
99 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
100 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
101 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
102 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
103 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
104 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
105 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
106 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
107 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
108 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
109 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
110 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
111 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
112 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
113 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
114
115 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
116 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
117 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
118 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
119 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
120 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
121 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
122 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
123 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
124 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
125 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
126 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
127 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
128 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
129 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
130 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
131
132 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
133 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
134 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
135 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
136 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
137 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
138 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
139 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
140 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
141 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
142 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
143 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
144 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
145 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
146 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
147 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
148
149 buf[0] += a;
150 buf[1] += b;
151 buf[2] += c;
152 buf[3] += d;
153}
154
155/*
156 * Note: this code is harmless on little-endian machines.
157 */
158static void byte_reverse(unsigned char *buf, unsigned longs)
159{
160 unsigned int t;
161
162 do
163 {
165 *(unsigned int *)buf = t;
166 buf += 4;
167 } while (--longs);
168}
169
170/*
171 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
172 * initialization constants.
173 */
174static void md5_init(struct md5_ctx *ctx)
175{
176 ctx->buf[0] = 0x67452301;
177 ctx->buf[1] = 0xefcdab89;
178 ctx->buf[2] = 0x98badcfe;
179 ctx->buf[3] = 0x10325476;
180
181 ctx->i[0] = ctx->i[1] = 0;
182}
183
184/*
185 * Update context to reflect the concatenation of another buffer full
186 * of bytes.
187 */
188static void md5_update(struct md5_ctx *ctx, const unsigned char *buf, unsigned int len)
189{
190 unsigned int t;
191
192 /* Update bitcount */
193 t = ctx->i[0];
194
195 if ((ctx->i[0] = t + (len << 3)) < t)
196 ctx->i[1]++; /* Carry from low to high */
197
198 ctx->i[1] += len >> 29;
199 t = (t >> 3) & 0x3f;
200
201 /* Handle any leading odd-sized chunks */
202 if (t)
203 {
204 unsigned char *p = (unsigned char *)ctx->in + t;
206
207 if (len < t)
208 {
209 memcpy(p, buf, len);
210 return;
211 }
212
213 memcpy(p, buf, t);
214 byte_reverse(ctx->in, 16);
215
216 md5_transform(ctx->buf, (unsigned int *)ctx->in);
217
218 buf += t;
219 len -= t;
220 }
221
222 /* Process data in 64-byte chunks */
224 {
226 byte_reverse(ctx->in, 16);
227
228 md5_transform(ctx->buf, (unsigned int *)ctx->in);
229
232 }
233
234 /* Handle any remaining bytes of data. */
235 memcpy(ctx->in, buf, len);
236}
237
238static void md5_final(struct md5_ctx *ctx, enum vkd3d_md5_variant variant)
239{
240 unsigned int padding;
241 unsigned int count;
242 unsigned char *p;
243
244 /* Compute number of bytes mod 64 */
245 count = (ctx->i[0] >> 3) & 0x3F;
246
247 /* Set the first char of padding to 0x80. This is safe since there is
248 always at least one byte free */
249 p = ctx->in + count;
250 *p++ = 0x80;
251 ++count;
252
253 /* Bytes of padding needed to make 64 bytes */
255
256 /* Pad out to 56 mod 64 */
257 if (padding < 8)
258 {
259 /* Two lots of padding: Pad the first block to 64 bytes */
260 memset(p, 0, padding);
261 byte_reverse(ctx->in, 16);
262 md5_transform(ctx->buf, (unsigned int *)ctx->in);
263
264 /* Now fill the next block */
266 }
267 else if (variant == VKD3D_MD5_DXBC)
268 {
269 /* Make place for bitcount at the beginning of the block */
270 memmove(&ctx->in[4], ctx->in, count);
271
272 /* Pad block to 60 bytes */
273 memset(p + 4, 0, padding - 4);
274 }
275 else
276 {
277 /* Pad block to 56 bytes */
278 memset(p, 0, padding - 8);
279 }
280
281 /* Append length in bits and transform */
282 if (variant == VKD3D_MD5_DXBC)
283 {
284 unsigned int length;
285
286 length = ctx->i[0];
287 memcpy(&ctx->in[0], &length, sizeof(length));
288 byte_reverse(&ctx->in[4], 14);
289 length = ctx->i[0] >> 2 | 0x1;
290 memcpy(&ctx->in[DXBC_CHECKSUM_BLOCK_SIZE - 4], &length, sizeof(length));
291 }
292 else
293 {
294 byte_reverse(ctx->in, 14);
295
296 ((unsigned int *)ctx->in)[14] = ctx->i[0];
297 ((unsigned int *)ctx->in)[15] = ctx->i[1];
298 }
299
300 md5_transform(ctx->buf, (unsigned int *)ctx->in);
301 byte_reverse((unsigned char *)ctx->buf, 4);
302 memcpy(ctx->digest, ctx->buf, 16);
303}
304
305void vkd3d_compute_md5(const void *data, size_t size, uint32_t checksum[4], enum vkd3d_md5_variant variant)
306{
307 const uint8_t *ptr = data;
308 struct md5_ctx ctx;
309
310 md5_init(&ctx);
312 md5_final(&ctx, variant);
313
314 memcpy(checksum, ctx.digest, sizeof(ctx.digest));
315}
#define md5_update
Definition: compat-1.3.h:2043
#define md5_init
Definition: compat-1.3.h:2039
UINT32 uint32_t
Definition: types.h:75
static cab_ULONG checksum(const cab_UBYTE *data, cab_UWORD bytes, cab_ULONG csum)
Definition: fdi.c:353
unsigned char uint8_t
Definition: stdint.h:33
GLuint GLuint GLsizei count
Definition: gl.h:1545
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
GLdouble GLdouble t
Definition: gl.h:2047
GLsizeiptr size
Definition: glext.h:5919
const GLubyte * c
Definition: glext.h:8905
GLboolean GLboolean GLboolean b
Definition: glext.h:6204
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
GLuint in
Definition: glext.h:9616
GLuint GLsizei GLsizei * length
Definition: glext.h:6040
GLfloat GLfloat p
Definition: glext.h:8902
GLenum GLsizei len
Definition: glext.h:6722
GLboolean GLboolean GLboolean GLboolean a
Definition: glext.h:6204
#define d
Definition: ke_i.h:81
#define a
Definition: ke_i.h:78
#define c
Definition: ke_i.h:80
#define b
Definition: ke_i.h:79
if(dx< 0)
Definition: linetemp.h:194
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
#define memmove(s1, s2, n)
Definition: mkisofs.h:881
static PVOID ptr
Definition: dispmode.c:27
static const DWORD padding[]
Definition: mciwnd.c:88
#define memset(x, y, z)
Definition: compat.h:39
#define MD5STEP(f, w, x, y, z, data, s)
Definition: checksum.c:64
void vkd3d_compute_md5(const void *data, size_t size, uint32_t checksum[4], enum vkd3d_md5_variant variant)
Definition: checksum.c:305
static void md5_transform(unsigned int buf[4], const unsigned int in[16])
Definition: checksum.c:72
#define F1(x, y, z)
Definition: checksum.c:58
#define F4(x, y, z)
Definition: checksum.c:61
#define DXBC_CHECKSUM_BLOCK_SIZE
Definition: checksum.c:43
#define F3(x, y, z)
Definition: checksum.c:60
static void md5_final(struct md5_ctx *ctx, enum vkd3d_md5_variant variant)
Definition: checksum.c:238
#define F2(x, y, z)
Definition: checksum.c:59
static void byte_reverse(unsigned char *buf, unsigned longs)
Definition: checksum.c:158
Definition: msi.c:4007
unsigned char digest[16]
Definition: msi.c:4011
unsigned int i[2]
Definition: msi.c:4008
static uint16_t vkd3d_make_u16(uint8_t low, uint8_t high)
Definition: vkd3d_common.h:376
static uint32_t vkd3d_make_u32(uint16_t low, uint16_t high)
Definition: vkd3d_common.h:381
#define STATIC_ASSERT(e)
Definition: vkd3d_common.h:47
@ VKD3D_MD5_DXBC