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>
11 * MD5 Message Digest Algorithm (RFC1321).
13 * Derived from cryptoapi implementation, originally based on the
14 * public domain implementation written by Colin Plumb in 1993.
16 * Copyright (c) Cryptoapi developers.
17 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
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)
24 #define MD5_DIGEST_SIZE 16
25 #define MD5_HMAC_BLOCK_SIZE 64
26 #define MD5_BLOCK_WORDS 16
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))
33 #define MD5STEP(f, w, x, y, z, in, s) \
34 (w += f(x, y, z) + in, w = (w<<s | w>>(32-s)) + x)
37 u32 hash[MD5_HASH_WORDS];
38 u32 block[MD5_BLOCK_WORDS];
42 static void md5_transform(u32 *hash, u32 const *in)
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);
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);
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);
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);
125 /* XXX: this stuff can be optimized */
126 static inline void le32_to_cpu_array(u32 *buf, unsigned int words)
134 static inline void cpu_to_le32_array(u32 *buf, unsigned int words)
142 static inline void md5_transform_helper(struct md5_ctx *ctx)
144 le32_to_cpu_array(ctx->block, sizeof(ctx->block) / sizeof(u32));
145 md5_transform(ctx->hash, ctx->block);
148 static void md5_init(struct md5_ctx *mctx)
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;
157 static void md5_update(struct md5_ctx *mctx, const u8 *data, unsigned int len)
159 const u32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
161 mctx->byte_count += len;
164 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
169 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
172 md5_transform_helper(mctx);
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);
183 memcpy(mctx->block, data, len);
186 static void md5_final(struct md5_ctx *mctx)
188 const unsigned int offset = mctx->byte_count & 0x3f;
189 char *p = (char *)mctx->block + offset;
190 int padding = 56 - (offset + 1);
194 memset(p, 0x00, padding + sizeof (u64));
195 md5_transform_helper(mctx);
196 p = (char *)mctx->block;
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));
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])
215 for (i = 0; i < MD5_HASH_WORDS; i++) {
216 if ((a[i] & mask[i]) != (b[i] & mask[i]))
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]);
232 static void inc_pattern(u8 *pattern, unsigned int len)
236 for (i = 0; i < len; i++) {
238 if (pattern[i] <= 'Z')
244 int main(int argc, char *argv[])
246 struct at_pool *atp = at_get_pool(&argc, argv, NULL);
247 struct md5_search *md5s;
250 err(1, "Not a worker thread?");
252 /* Tell parent we're ready. */
253 at_tell_parent(atp, INITIAL_POINTER);
254 while ((md5s = at_read_parent(atp)) != NULL) {
256 md5s->success = false;
258 for (i = 0; i < md5s->num_tries; i++) {
262 md5_update(&ctx, md5s->pattern, md5s->num_bytes);
265 if (bits_match(ctx.hash, md5s->md5, md5s->mask)) {
266 md5s->success = true;
269 inc_pattern(md5s->pattern, md5s->num_bytes);
271 at_tell_parent(atp, md5s);