]> git.ozlabs.org Git - ccan/blob - ccan/antithread/examples/md5_worker.c
Add antithread. Not finished, but useful as example of module whose
[ccan] / ccan / antithread / examples / md5_worker.c
1 /* Worker thread: tries to find data with given MD5. */
2 #include "ccan/antithread/antithread.h"
3 #include "md5_finder.h"
4 #include <netinet/in.h>
5 #include <err.h>
6 #include <string.h>
7
8 /* 
9  * Cryptographic API.
10  *
11  * MD5 Message Digest Algorithm (RFC1321).
12  *
13  * Derived from cryptoapi implementation, originally based on the
14  * public domain implementation written by Colin Plumb in 1993.
15  *
16  * Copyright (c) Cryptoapi developers.
17  * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
18  * 
19  * This program is free software; you can redistribute it and/or modify it
20  * under the terms of the GNU General Public License as published by the Free
21  * Software Foundation; either version 2 of the License, or (at your option) 
22  * any later version.
23  */
24 #define MD5_DIGEST_SIZE         16
25 #define MD5_HMAC_BLOCK_SIZE     64
26 #define MD5_BLOCK_WORDS         16
27
28 #define F1(x, y, z)     (z ^ (x & (y ^ z)))
29 #define F2(x, y, z)     F1(z, x, y)
30 #define F3(x, y, z)     (x ^ y ^ z)
31 #define F4(x, y, z)     (y ^ (x | ~z))
32
33 #define MD5STEP(f, w, x, y, z, in, s) \
34         (w += f(x, y, z) + in, w = (w<<s | w>>(32-s)) + x)
35
36 struct md5_ctx {
37         u32 hash[MD5_HASH_WORDS];
38         u32 block[MD5_BLOCK_WORDS];
39         u64 byte_count;
40 };
41
42 static void md5_transform(u32 *hash, u32 const *in)
43 {
44         u32 a, b, c, d;
45
46         a = hash[0];
47         b = hash[1];
48         c = hash[2];
49         d = hash[3];
50
51         MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
52         MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
53         MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
54         MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
55         MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
56         MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
57         MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
58         MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
59         MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
60         MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
61         MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
62         MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
63         MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
64         MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
65         MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
66         MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
67
68         MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
69         MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
70         MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
71         MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
72         MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
73         MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
74         MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
75         MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
76         MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
77         MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
78         MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
79         MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
80         MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
81         MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
82         MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
83         MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
84
85         MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
86         MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
87         MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
88         MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
89         MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
90         MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
91         MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
92         MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
93         MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
94         MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
95         MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
96         MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
97         MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
98         MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
99         MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
100         MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
101
102         MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
103         MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
104         MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
105         MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
106         MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
107         MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
108         MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
109         MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
110         MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
111         MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
112         MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
113         MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
114         MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
115         MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
116         MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
117         MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
118
119         hash[0] += a;
120         hash[1] += b;
121         hash[2] += c;
122         hash[3] += d;
123 }
124
125 /* XXX: this stuff can be optimized */
126 static inline void le32_to_cpu_array(u32 *buf, unsigned int words)
127 {
128         while (words--) {
129                 *buf = ntohl(*buf);
130                 buf++;
131         }
132 }
133
134 static inline void cpu_to_le32_array(u32 *buf, unsigned int words)
135 {
136         while (words--) {
137                 *buf = htonl(*buf);
138                 buf++;
139         }
140 }
141
142 static inline void md5_transform_helper(struct md5_ctx *ctx)
143 {
144         le32_to_cpu_array(ctx->block, sizeof(ctx->block) / sizeof(u32));
145         md5_transform(ctx->hash, ctx->block);
146 }
147
148 static void md5_init(struct md5_ctx *mctx)
149 {
150         mctx->hash[0] = 0x67452301;
151         mctx->hash[1] = 0xefcdab89;
152         mctx->hash[2] = 0x98badcfe;
153         mctx->hash[3] = 0x10325476;
154         mctx->byte_count = 0;
155 }
156
157 static void md5_update(struct md5_ctx *mctx, const u8 *data, unsigned int len)
158 {
159         const u32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
160
161         mctx->byte_count += len;
162
163         if (avail > len) {
164                 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
165                        data, len);
166                 return;
167         }
168
169         memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
170                data, avail);
171
172         md5_transform_helper(mctx);
173         data += avail;
174         len -= avail;
175
176         while (len >= sizeof(mctx->block)) {
177                 memcpy(mctx->block, data, sizeof(mctx->block));
178                 md5_transform_helper(mctx);
179                 data += sizeof(mctx->block);
180                 len -= sizeof(mctx->block);
181         }
182
183         memcpy(mctx->block, data, len);
184 }
185
186 static void md5_final(struct md5_ctx *mctx)
187 {
188         const unsigned int offset = mctx->byte_count & 0x3f;
189         char *p = (char *)mctx->block + offset;
190         int padding = 56 - (offset + 1);
191
192         *p++ = 0x80;
193         if (padding < 0) {
194                 memset(p, 0x00, padding + sizeof (u64));
195                 md5_transform_helper(mctx);
196                 p = (char *)mctx->block;
197                 padding = 56;
198         }
199
200         memset(p, 0, padding);
201         mctx->block[14] = mctx->byte_count << 3;
202         mctx->block[15] = mctx->byte_count >> 29;
203         le32_to_cpu_array(mctx->block, (sizeof(mctx->block) -
204                           sizeof(u64)) / sizeof(u32));
205         md5_transform(mctx->hash, mctx->block);
206         cpu_to_le32_array(mctx->hash, sizeof(mctx->hash) / sizeof(u32));
207 }
208
209 static bool bits_match(const u32 a[MD5_HASH_WORDS],
210                        const u32 b[MD5_HASH_WORDS],
211                        const u32 mask[MD5_HASH_WORDS])
212 {
213         unsigned int i;
214
215         for (i = 0; i < MD5_HASH_WORDS; i++) {
216                 if ((a[i] & mask[i]) != (b[i] & mask[i]))
217                         return false;
218         }
219
220 #if 0
221         printf("mask = %08x%08x%08x%08x\n"
222                "a = %08x%08x%08x%08x\n"
223                "b = %08x%08x%08x%08x\n",
224                mask[0], mask[1], mask[2], mask[3],
225                a[0], a[1], a[2], a[3],
226                b[0], b[1], b[2], b[3]);
227 #endif
228
229         return true;
230 }
231
232 static void inc_pattern(u8 *pattern, unsigned int len)
233 {
234         unsigned int i;
235
236         for (i = 0; i < len; i++) {
237                 pattern[i]++;
238                 if (pattern[i] <= 'Z')
239                         break;
240                 pattern[i] = 'A';
241         }
242 }
243
244 int main(int argc, char *argv[])
245 {
246         struct at_pool *atp = at_get_pool(&argc, argv, NULL);
247         struct md5_search *md5s;
248
249         if (!atp)
250                 err(1, "Not a worker thread?");
251
252         /* Tell parent we're ready. */
253         at_tell_parent(atp, INITIAL_POINTER);
254         while ((md5s = at_read_parent(atp)) != NULL) {
255                 unsigned int i;
256                 md5s->success = false;
257
258                 for (i = 0; i < md5s->num_tries; i++) {
259                         struct md5_ctx ctx;     
260
261                         md5_init(&ctx);
262                         md5_update(&ctx, md5s->pattern, md5s->num_bytes);
263                         md5_final(&ctx);
264
265                         if (bits_match(ctx.hash, md5s->md5, md5s->mask)) {
266                                 md5s->success = true;
267                                 break;
268                         }
269                         inc_pattern(md5s->pattern, md5s->num_bytes);
270                 }
271                 at_tell_parent(atp, md5s);
272         }
273         return 0;
274 }