1 | /* |
---|
2 | * zzuf - general purpose fuzzer |
---|
3 | * Copyright (c) 2002-2010 Sam Hocevar <sam@hocevar.net> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * This program is free software. It comes without any warranty, to |
---|
7 | * the extent permitted by applicable law. You can redistribute it |
---|
8 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
9 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
10 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
11 | */ |
---|
12 | |
---|
13 | /* |
---|
14 | * md5.c: MD5 computation. Written and put into the public domain |
---|
15 | * by Colin Plumb in 1993. |
---|
16 | */ |
---|
17 | |
---|
18 | #include "config.h" |
---|
19 | |
---|
20 | #if defined HAVE_STDINT_H |
---|
21 | # include <stdint.h> |
---|
22 | #elif defined HAVE_INTTYPES_H |
---|
23 | # include <inttypes.h> |
---|
24 | #endif |
---|
25 | #if defined HAVE_ENDIAN_H |
---|
26 | # include <endian.h> |
---|
27 | #endif |
---|
28 | #include <string.h> |
---|
29 | #include <stdlib.h> |
---|
30 | |
---|
31 | #include "md5.h" |
---|
32 | |
---|
33 | struct md5 |
---|
34 | { |
---|
35 | uint32_t buf[4]; |
---|
36 | uint32_t bits[2]; |
---|
37 | uint32_t in[64]; |
---|
38 | }; |
---|
39 | |
---|
40 | static void swapwords(uint32_t *buf, unsigned words); |
---|
41 | static void transform(uint32_t buf[4], uint32_t in[16]); |
---|
42 | |
---|
43 | struct md5 *_zz_md5_init(void) |
---|
44 | { |
---|
45 | struct md5 *ctx = malloc(sizeof(struct md5)); |
---|
46 | |
---|
47 | ctx->buf[0] = 0x67452301; |
---|
48 | ctx->buf[1] = 0xefcdab89; |
---|
49 | ctx->buf[2] = 0x98badcfe; |
---|
50 | ctx->buf[3] = 0x10325476; |
---|
51 | |
---|
52 | ctx->bits[0] = 0; |
---|
53 | ctx->bits[1] = 0; |
---|
54 | |
---|
55 | return ctx; |
---|
56 | } |
---|
57 | |
---|
58 | void _zz_md5_add(struct md5 *ctx, uint8_t *buf, unsigned len) |
---|
59 | { |
---|
60 | uint32_t t; |
---|
61 | |
---|
62 | t = ctx->bits[0]; |
---|
63 | if((ctx->bits[0] = t + ((uint32_t)len << 3)) < t) |
---|
64 | ctx->bits[1]++; |
---|
65 | ctx->bits[1] += len >> 29; |
---|
66 | |
---|
67 | t = (t >> 3) & 0x3f; |
---|
68 | |
---|
69 | if(t) |
---|
70 | { |
---|
71 | uint8_t *p = (uint8_t *)ctx->in + t; |
---|
72 | |
---|
73 | t = 64 - t; |
---|
74 | if(len < t) |
---|
75 | { |
---|
76 | memcpy(p, buf, len); |
---|
77 | return; |
---|
78 | } |
---|
79 | memcpy(p, buf, t); |
---|
80 | swapwords(ctx->in, 64 / 4); |
---|
81 | transform(ctx->buf, ctx->in); |
---|
82 | buf += t; |
---|
83 | len -= t; |
---|
84 | } |
---|
85 | |
---|
86 | while(len >= 64) |
---|
87 | { |
---|
88 | memcpy(ctx->in, buf, 64); |
---|
89 | swapwords(ctx->in, 64 / 4); |
---|
90 | transform(ctx->buf, ctx->in); |
---|
91 | buf += 64; |
---|
92 | len -= 64; |
---|
93 | } |
---|
94 | |
---|
95 | memcpy(ctx->in, buf, len); |
---|
96 | } |
---|
97 | |
---|
98 | void _zz_md5_fini(uint8_t *digest, struct md5 *ctx) |
---|
99 | { |
---|
100 | unsigned count; |
---|
101 | uint8_t *p; |
---|
102 | |
---|
103 | count = (ctx->bits[0] >> 3) & 0x3F; |
---|
104 | p = (uint8_t *)ctx->in + count; |
---|
105 | *p++ = 0x80; |
---|
106 | |
---|
107 | count = 64 - 1 - count; |
---|
108 | if(count < 8) |
---|
109 | { |
---|
110 | memset(p, 0, count); |
---|
111 | swapwords(ctx->in, 64 / 4); |
---|
112 | transform(ctx->buf, ctx->in); |
---|
113 | memset(ctx->in, 0, 56); |
---|
114 | } |
---|
115 | else |
---|
116 | memset(p, 0, count - 8); |
---|
117 | |
---|
118 | swapwords(ctx->in, 56 / 4); |
---|
119 | memcpy(ctx->in + 56 / 4, ctx->bits, 8); |
---|
120 | transform(ctx->buf, ctx->in); |
---|
121 | swapwords(ctx->buf, 16 / 4); |
---|
122 | memcpy(digest, ctx->buf, 16); |
---|
123 | free(ctx); |
---|
124 | } |
---|
125 | |
---|
126 | static void swapwords(uint32_t *buf, unsigned words) |
---|
127 | { |
---|
128 | /* XXX: no need to swap words on little endian machines */ |
---|
129 | #if defined HAVE_ENDIAN_H |
---|
130 | if(__BYTE_ORDER == __LITTLE_ENDIAN) |
---|
131 | return; |
---|
132 | #else |
---|
133 | /* This is compile-time optimised with at least -O1 or -Os */ |
---|
134 | uint32_t const tmp = 0x12345678; |
---|
135 | if(*(uint8_t const *)&tmp == 0x78) |
---|
136 | return; |
---|
137 | #endif |
---|
138 | |
---|
139 | while(words > 0) |
---|
140 | { |
---|
141 | uint8_t *b = (uint8_t *)buf; |
---|
142 | *buf++ = (uint32_t)((unsigned) b[3] << 8 | b[2]) << 16 | |
---|
143 | ((unsigned) b[1] << 8 | b[0]); |
---|
144 | words--; |
---|
145 | } |
---|
146 | } |
---|
147 | |
---|
148 | /* #define F1(x, y, z) (x & y | ~x & z) */ |
---|
149 | #define F1(x, y, z) (z ^ (x & (y ^ z))) |
---|
150 | #define F2(x, y, z) F1(z, x, y) |
---|
151 | #define F3(x, y, z) (x ^ y ^ z) |
---|
152 | #define F4(x, y, z) (y ^ (x | ~z)) |
---|
153 | |
---|
154 | #define MD5STEP(f, w, x, y, z, data, s) \ |
---|
155 | (w += f(x, y, z) + data, w = w << s | w >> (32 - s), w += x) |
---|
156 | |
---|
157 | static void transform(uint32_t buf[4], uint32_t in[16]) |
---|
158 | { |
---|
159 | uint32_t a = buf[0]; |
---|
160 | uint32_t b = buf[1]; |
---|
161 | uint32_t c = buf[2]; |
---|
162 | uint32_t d = buf[3]; |
---|
163 | |
---|
164 | MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7); |
---|
165 | MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12); |
---|
166 | MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17); |
---|
167 | MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22); |
---|
168 | MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7); |
---|
169 | MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12); |
---|
170 | MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17); |
---|
171 | MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22); |
---|
172 | MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7); |
---|
173 | MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12); |
---|
174 | MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17); |
---|
175 | MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22); |
---|
176 | MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7); |
---|
177 | MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12); |
---|
178 | MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17); |
---|
179 | MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22); |
---|
180 | |
---|
181 | MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5); |
---|
182 | MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9); |
---|
183 | MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14); |
---|
184 | MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20); |
---|
185 | MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5); |
---|
186 | MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9); |
---|
187 | MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14); |
---|
188 | MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20); |
---|
189 | MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5); |
---|
190 | MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9); |
---|
191 | MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14); |
---|
192 | MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20); |
---|
193 | MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5); |
---|
194 | MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9); |
---|
195 | MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14); |
---|
196 | MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20); |
---|
197 | |
---|
198 | MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4); |
---|
199 | MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11); |
---|
200 | MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16); |
---|
201 | MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23); |
---|
202 | MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4); |
---|
203 | MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11); |
---|
204 | MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16); |
---|
205 | MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23); |
---|
206 | MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4); |
---|
207 | MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11); |
---|
208 | MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16); |
---|
209 | MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23); |
---|
210 | MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4); |
---|
211 | MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11); |
---|
212 | MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16); |
---|
213 | MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23); |
---|
214 | |
---|
215 | MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6); |
---|
216 | MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10); |
---|
217 | MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15); |
---|
218 | MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21); |
---|
219 | MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6); |
---|
220 | MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10); |
---|
221 | MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15); |
---|
222 | MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21); |
---|
223 | MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6); |
---|
224 | MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10); |
---|
225 | MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15); |
---|
226 | MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21); |
---|
227 | MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6); |
---|
228 | MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10); |
---|
229 | MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15); |
---|
230 | MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21); |
---|
231 | |
---|
232 | buf[0] += a; |
---|
233 | buf[1] += b; |
---|
234 | buf[2] += c; |
---|
235 | buf[3] += d; |
---|
236 | } |
---|
237 | |
---|