Add hashcat command lines
[pwdhash.git] / md5-cl.c
1 #include <stdint.h>
2 #include <stdio.h>
3 #include <sys/types.h>
4 #include <string.h>
5
6 #include "md5-cl.h"
7
8 const u8 domain[] = "flypig.co.uk";
9 const u32x domain_len = 12;
10
11
12 #define MD5_STEP(f,a,b,c,d,x,K,s) \
13 { \
14 a += K; \
15 a += x; \
16 a += f (b, c, d); \
17 a = rotl32 (a, s); \
18 a += b; \
19 }
20
21 #define MD5_F(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
22 #define MD5_G(x,y,z) ((y) ^ ((z) & ((x) ^ (y))))
23 #define MD5_H(x,y,z) ((x) ^ (y) ^ (z))
24 #define MD5_I(x,y,z) ((y) ^ ((x) | ~(z)))
25 #define MD5_Fo(x,y,z) (MD5_F((x), (y), (z)))
26 #define MD5_Go(x,y,z) (MD5_G((x), (y), (z)))
27
28 #define MD5S00 7u
29 #define MD5S01 12u
30 #define MD5S02 17u
31 #define MD5S03 22u
32 #define MD5S10 5u
33 #define MD5S11 9u
34 #define MD5S12 14u
35 #define MD5S13 20u
36 #define MD5S20 4u
37 #define MD5S21 11u
38 #define MD5S22 16u
39 #define MD5S23 23u
40 #define MD5S30 6u
41 #define MD5S31 10u
42 #define MD5S32 15u
43 #define MD5S33 21u
44
45 #define MD5C00 0xd76aa478u
46 #define MD5C01 0xe8c7b756u
47 #define MD5C02 0x242070dbu
48 #define MD5C03 0xc1bdceeeu
49 #define MD5C04 0xf57c0fafu
50 #define MD5C05 0x4787c62au
51 #define MD5C06 0xa8304613u
52 #define MD5C07 0xfd469501u
53 #define MD5C08 0x698098d8u
54 #define MD5C09 0x8b44f7afu
55 #define MD5C0a 0xffff5bb1u
56 #define MD5C0b 0x895cd7beu
57 #define MD5C0c 0x6b901122u
58 #define MD5C0d 0xfd987193u
59 #define MD5C0e 0xa679438eu
60 #define MD5C0f 0x49b40821u
61 #define MD5C10 0xf61e2562u
62 #define MD5C11 0xc040b340u
63 #define MD5C12 0x265e5a51u
64 #define MD5C13 0xe9b6c7aau
65 #define MD5C14 0xd62f105du
66 #define MD5C15 0x02441453u
67 #define MD5C16 0xd8a1e681u
68 #define MD5C17 0xe7d3fbc8u
69 #define MD5C18 0x21e1cde6u
70 #define MD5C19 0xc33707d6u
71 #define MD5C1a 0xf4d50d87u
72 #define MD5C1b 0x455a14edu
73 #define MD5C1c 0xa9e3e905u
74 #define MD5C1d 0xfcefa3f8u
75 #define MD5C1e 0x676f02d9u
76 #define MD5C1f 0x8d2a4c8au
77 #define MD5C20 0xfffa3942u
78 #define MD5C21 0x8771f681u
79 #define MD5C22 0x6d9d6122u
80 #define MD5C23 0xfde5380cu
81 #define MD5C24 0xa4beea44u
82 #define MD5C25 0x4bdecfa9u
83 #define MD5C26 0xf6bb4b60u
84 #define MD5C27 0xbebfbc70u
85 #define MD5C28 0x289b7ec6u
86 #define MD5C29 0xeaa127fau
87 #define MD5C2a 0xd4ef3085u
88 #define MD5C2b 0x04881d05u
89 #define MD5C2c 0xd9d4d039u
90 #define MD5C2d 0xe6db99e5u
91 #define MD5C2e 0x1fa27cf8u
92 #define MD5C2f 0xc4ac5665u
93 #define MD5C30 0xf4292244u
94 #define MD5C31 0x432aff97u
95 #define MD5C32 0xab9423a7u
96 #define MD5C33 0xfc93a039u
97 #define MD5C34 0x655b59c3u
98 #define MD5C35 0x8f0ccc92u
99 #define MD5C36 0xffeff47du
100 #define MD5C37 0x85845dd1u
101 #define MD5C38 0x6fa87e4fu
102 #define MD5C39 0xfe2ce6e0u
103 #define MD5C3a 0xa3014314u
104 #define MD5C3b 0x4e0811a1u
105 #define MD5C3c 0xf7537e82u
106 #define MD5C3d 0xbd3af235u
107 #define MD5C3e 0x2ad7d2bbu
108 #define MD5C3f 0xeb86d391u
109
110 u32x rotl32 (const u32x a, const u32 n)
111 {
112 u32x result;
113
114 result = a << n;
115 result |= a >> (32 - n);
116
117 return result;
118 // return rotate (a, n);
119 }
120
121 void md5_transform_cl (const u32x w0[4], const u32x w1[4], const u32x w2[4], const u32x w3[4], u32x digest[4])
122 {
123 u32x a = digest[0];
124 u32x b = digest[1];
125 u32x c = digest[2];
126 u32x d = digest[3];
127
128 u32x w0_t = w0[0];
129 u32x w1_t = w0[1];
130 u32x w2_t = w0[2];
131 u32x w3_t = w0[3];
132 u32x w4_t = w1[0];
133 u32x w5_t = w1[1];
134 u32x w6_t = w1[2];
135 u32x w7_t = w1[3];
136 u32x w8_t = w2[0];
137 u32x w9_t = w2[1];
138 u32x wa_t = w2[2];
139 u32x wb_t = w2[3];
140 u32x wc_t = w3[0];
141 u32x wd_t = w3[1];
142 u32x we_t = w3[2];
143 u32x wf_t = w3[3];
144
145 MD5_STEP (MD5_Fo, a, b, c, d, w0_t, MD5C00, MD5S00);
146 MD5_STEP (MD5_Fo, d, a, b, c, w1_t, MD5C01, MD5S01);
147 MD5_STEP (MD5_Fo, c, d, a, b, w2_t, MD5C02, MD5S02);
148 MD5_STEP (MD5_Fo, b, c, d, a, w3_t, MD5C03, MD5S03);
149 MD5_STEP (MD5_Fo, a, b, c, d, w4_t, MD5C04, MD5S00);
150 MD5_STEP (MD5_Fo, d, a, b, c, w5_t, MD5C05, MD5S01);
151 MD5_STEP (MD5_Fo, c, d, a, b, w6_t, MD5C06, MD5S02);
152 MD5_STEP (MD5_Fo, b, c, d, a, w7_t, MD5C07, MD5S03);
153 MD5_STEP (MD5_Fo, a, b, c, d, w8_t, MD5C08, MD5S00);
154 MD5_STEP (MD5_Fo, d, a, b, c, w9_t, MD5C09, MD5S01);
155 MD5_STEP (MD5_Fo, c, d, a, b, wa_t, MD5C0a, MD5S02);
156 MD5_STEP (MD5_Fo, b, c, d, a, wb_t, MD5C0b, MD5S03);
157 MD5_STEP (MD5_Fo, a, b, c, d, wc_t, MD5C0c, MD5S00);
158 MD5_STEP (MD5_Fo, d, a, b, c, wd_t, MD5C0d, MD5S01);
159 MD5_STEP (MD5_Fo, c, d, a, b, we_t, MD5C0e, MD5S02);
160 MD5_STEP (MD5_Fo, b, c, d, a, wf_t, MD5C0f, MD5S03);
161
162 MD5_STEP (MD5_Go, a, b, c, d, w1_t, MD5C10, MD5S10);
163 MD5_STEP (MD5_Go, d, a, b, c, w6_t, MD5C11, MD5S11);
164 MD5_STEP (MD5_Go, c, d, a, b, wb_t, MD5C12, MD5S12);
165 MD5_STEP (MD5_Go, b, c, d, a, w0_t, MD5C13, MD5S13);
166 MD5_STEP (MD5_Go, a, b, c, d, w5_t, MD5C14, MD5S10);
167 MD5_STEP (MD5_Go, d, a, b, c, wa_t, MD5C15, MD5S11);
168 MD5_STEP (MD5_Go, c, d, a, b, wf_t, MD5C16, MD5S12);
169 MD5_STEP (MD5_Go, b, c, d, a, w4_t, MD5C17, MD5S13);
170 MD5_STEP (MD5_Go, a, b, c, d, w9_t, MD5C18, MD5S10);
171 MD5_STEP (MD5_Go, d, a, b, c, we_t, MD5C19, MD5S11);
172 MD5_STEP (MD5_Go, c, d, a, b, w3_t, MD5C1a, MD5S12);
173 MD5_STEP (MD5_Go, b, c, d, a, w8_t, MD5C1b, MD5S13);
174 MD5_STEP (MD5_Go, a, b, c, d, wd_t, MD5C1c, MD5S10);
175 MD5_STEP (MD5_Go, d, a, b, c, w2_t, MD5C1d, MD5S11);
176 MD5_STEP (MD5_Go, c, d, a, b, w7_t, MD5C1e, MD5S12);
177 MD5_STEP (MD5_Go, b, c, d, a, wc_t, MD5C1f, MD5S13);
178
179 MD5_STEP (MD5_H , a, b, c, d, w5_t, MD5C20, MD5S20);
180 MD5_STEP (MD5_H , d, a, b, c, w8_t, MD5C21, MD5S21);
181 MD5_STEP (MD5_H , c, d, a, b, wb_t, MD5C22, MD5S22);
182 MD5_STEP (MD5_H , b, c, d, a, we_t, MD5C23, MD5S23);
183 MD5_STEP (MD5_H , a, b, c, d, w1_t, MD5C24, MD5S20);
184 MD5_STEP (MD5_H , d, a, b, c, w4_t, MD5C25, MD5S21);
185 MD5_STEP (MD5_H , c, d, a, b, w7_t, MD5C26, MD5S22);
186 MD5_STEP (MD5_H , b, c, d, a, wa_t, MD5C27, MD5S23);
187 MD5_STEP (MD5_H , a, b, c, d, wd_t, MD5C28, MD5S20);
188 MD5_STEP (MD5_H , d, a, b, c, w0_t, MD5C29, MD5S21);
189 MD5_STEP (MD5_H , c, d, a, b, w3_t, MD5C2a, MD5S22);
190 MD5_STEP (MD5_H , b, c, d, a, w6_t, MD5C2b, MD5S23);
191 MD5_STEP (MD5_H , a, b, c, d, w9_t, MD5C2c, MD5S20);
192 MD5_STEP (MD5_H , d, a, b, c, wc_t, MD5C2d, MD5S21);
193 MD5_STEP (MD5_H , c, d, a, b, wf_t, MD5C2e, MD5S22);
194 MD5_STEP (MD5_H , b, c, d, a, w2_t, MD5C2f, MD5S23);
195
196 MD5_STEP (MD5_I , a, b, c, d, w0_t, MD5C30, MD5S30);
197 MD5_STEP (MD5_I , d, a, b, c, w7_t, MD5C31, MD5S31);
198 MD5_STEP (MD5_I , c, d, a, b, we_t, MD5C32, MD5S32);
199 MD5_STEP (MD5_I , b, c, d, a, w5_t, MD5C33, MD5S33);
200 MD5_STEP (MD5_I , a, b, c, d, wc_t, MD5C34, MD5S30);
201 MD5_STEP (MD5_I , d, a, b, c, w3_t, MD5C35, MD5S31);
202 MD5_STEP (MD5_I , c, d, a, b, wa_t, MD5C36, MD5S32);
203 MD5_STEP (MD5_I , b, c, d, a, w1_t, MD5C37, MD5S33);
204 MD5_STEP (MD5_I , a, b, c, d, w8_t, MD5C38, MD5S30);
205 MD5_STEP (MD5_I , d, a, b, c, wf_t, MD5C39, MD5S31);
206 MD5_STEP (MD5_I , c, d, a, b, w6_t, MD5C3a, MD5S32);
207 MD5_STEP (MD5_I , b, c, d, a, wd_t, MD5C3b, MD5S33);
208 MD5_STEP (MD5_I , a, b, c, d, w4_t, MD5C3c, MD5S30);
209 MD5_STEP (MD5_I , d, a, b, c, wb_t, MD5C3d, MD5S31);
210 MD5_STEP (MD5_I , c, d, a, b, w2_t, MD5C3e, MD5S32);
211 MD5_STEP (MD5_I , b, c, d, a, w9_t, MD5C3f, MD5S33);
212
213 digest[0] += a;
214 digest[1] += b;
215 digest[2] += c;
216 digest[3] += d;
217 }
218
219 void append_0x80_2x4_S (u32 w0[4], u32 w1[4], const u32 offset)
220 {
221 switch (offset)
222 {
223 case 0:
224 w0[0] = 0x80;
225 break;
226
227 case 1:
228 w0[0] = w0[0] | 0x8000;
229 break;
230
231 case 2:
232 w0[0] = w0[0] | 0x800000;
233 break;
234
235 case 3:
236 w0[0] = w0[0] | 0x80000000;
237 break;
238
239 case 4:
240 w0[1] = 0x80;
241 break;
242
243 case 5:
244 w0[1] = w0[1] | 0x8000;
245 break;
246
247 case 6:
248 w0[1] = w0[1] | 0x800000;
249 break;
250
251 case 7:
252 w0[1] = w0[1] | 0x80000000;
253 break;
254
255 case 8:
256 w0[2] = 0x80;
257 break;
258
259 case 9:
260 w0[2] = w0[2] | 0x8000;
261 break;
262
263 case 10:
264 w0[2] = w0[2] | 0x800000;
265 break;
266
267 case 11:
268 w0[2] = w0[2] | 0x80000000;
269 break;
270
271 case 12:
272 w0[3] = 0x80;
273 break;
274
275 case 13:
276 w0[3] = w0[3] | 0x8000;
277 break;
278
279 case 14:
280 w0[3] = w0[3] | 0x800000;
281 break;
282
283 case 15:
284 w0[3] = w0[3] | 0x80000000;
285 break;
286
287 case 16:
288 w1[0] = 0x80;
289 break;
290
291 case 17:
292 w1[0] = w1[0] | 0x8000;
293 break;
294
295 case 18:
296 w1[0] = w1[0] | 0x800000;
297 break;
298
299 case 19:
300 w1[0] = w1[0] | 0x80000000;
301 break;
302
303 case 20:
304 w1[1] = 0x80;
305 break;
306
307 case 21:
308 w1[1] = w1[1] | 0x8000;
309 break;
310
311 case 22:
312 w1[1] = w1[1] | 0x800000;
313 break;
314
315 case 23:
316 w1[1] = w1[1] | 0x80000000;
317 break;
318
319 case 24:
320 w1[2] = 0x80;
321 break;
322
323 case 25:
324 w1[2] = w1[2] | 0x8000;
325 break;
326
327 case 26:
328 w1[2] = w1[2] | 0x800000;
329 break;
330
331 case 27:
332 w1[2] = w1[2] | 0x80000000;
333 break;
334
335 case 28:
336 w1[3] = 0x80;
337 break;
338
339 case 29:
340 w1[3] = w1[3] | 0x8000;
341 break;
342
343 case 30:
344 w1[3] = w1[3] | 0x800000;
345 break;
346
347 case 31:
348 w1[3] = w1[3] | 0x80000000;
349 break;
350 }
351 }
352
353 void append_0x80_2x4_VV (u32x w0[4], u32x w1[4], const u32x offset)
354 {
355 append_0x80_2x4_S (w0, w1, offset);
356 }
357
358
359 // HMAC MD5 ///////////////////////////////////////////////////////
360
361 void hmac_md5_pad_cl (u32x w0[4], u32x w1[4], u32x w2[4], u32x w3[4], u32x ipad[4], u32x opad[4])
362 {
363 w0[0] = w0[0] ^ 0x36363636;
364 w0[1] = w0[1] ^ 0x36363636;
365 w0[2] = w0[2] ^ 0x36363636;
366 w0[3] = w0[3] ^ 0x36363636;
367 w1[0] = w1[0] ^ 0x36363636;
368 w1[1] = w1[1] ^ 0x36363636;
369 w1[2] = w1[2] ^ 0x36363636;
370 w1[3] = w1[3] ^ 0x36363636;
371 w2[0] = w2[0] ^ 0x36363636;
372 w2[1] = w2[1] ^ 0x36363636;
373 w2[2] = w2[2] ^ 0x36363636;
374 w2[3] = w2[3] ^ 0x36363636;
375 w3[0] = w3[0] ^ 0x36363636;
376 w3[1] = w3[1] ^ 0x36363636;
377 w3[2] = w3[2] ^ 0x36363636;
378 w3[3] = w3[3] ^ 0x36363636;
379
380 ipad[0] = MD5M_A;
381 ipad[1] = MD5M_B;
382 ipad[2] = MD5M_C;
383 ipad[3] = MD5M_D;
384
385 md5_transform_cl (w0, w1, w2, w3, ipad);
386
387 w0[0] = w0[0] ^ 0x6a6a6a6a;
388 w0[1] = w0[1] ^ 0x6a6a6a6a;
389 w0[2] = w0[2] ^ 0x6a6a6a6a;
390 w0[3] = w0[3] ^ 0x6a6a6a6a;
391 w1[0] = w1[0] ^ 0x6a6a6a6a;
392 w1[1] = w1[1] ^ 0x6a6a6a6a;
393 w1[2] = w1[2] ^ 0x6a6a6a6a;
394 w1[3] = w1[3] ^ 0x6a6a6a6a;
395 w2[0] = w2[0] ^ 0x6a6a6a6a;
396 w2[1] = w2[1] ^ 0x6a6a6a6a;
397 w2[2] = w2[2] ^ 0x6a6a6a6a;
398 w2[3] = w2[3] ^ 0x6a6a6a6a;
399 w3[0] = w3[0] ^ 0x6a6a6a6a;
400 w3[1] = w3[1] ^ 0x6a6a6a6a;
401 w3[2] = w3[2] ^ 0x6a6a6a6a;
402 w3[3] = w3[3] ^ 0x6a6a6a6a;
403
404 opad[0] = MD5M_A;
405 opad[1] = MD5M_B;
406 opad[2] = MD5M_C;
407 opad[3] = MD5M_D;
408
409 md5_transform_cl (w0, w1, w2, w3, opad);
410 }
411
412 void hmac_md5_run_cl (u32x w0[4], u32x w1[4], u32x w2[4], u32x w3[4], u32x ipad[4], u32x opad[4], u32x digest[4])
413 {
414 digest[0] = ipad[0];
415 digest[1] = ipad[1];
416 digest[2] = ipad[2];
417 digest[3] = ipad[3];
418
419 md5_transform_cl (w0, w1, w2, w3, digest);
420
421 w0[0] = digest[0];
422 w0[1] = digest[1];
423 w0[2] = digest[2];
424 w0[3] = digest[3];
425 w1[0] = 0x80;
426 w1[1] = 0;
427 w1[2] = 0;
428 w1[3] = 0;
429 w2[0] = 0;
430 w2[1] = 0;
431 w2[2] = 0;
432 w2[3] = 0;
433 w3[0] = 0;
434 w3[1] = 0;
435 w3[2] = (64 + 16) * 8;
436 w3[3] = 0;
437
438 digest[0] = opad[0];
439 digest[1] = opad[1];
440 digest[2] = opad[2];
441 digest[3] = opad[3];
442
443 md5_transform_cl (w0, w1, w2, w3, digest);
444 }
445
446 void md5hmac_none(u8 const * const inData, const u32x pw_len, u8 outDigest[16])
447 {
448 u32 pos;
449 u32 boundary;
450
451 boundary = pw_len;
452 if (boundary > 16) {
453 boundary = 16;
454 }
455
456 for (pos = 0; pos < boundary; pos++) {
457 outDigest[pos] = inData[pos];
458 }
459 for (pos = boundary; pos < 16; pos++) {
460 outDigest[pos] = 0;
461 }
462 }
463
464 void md5hmac_domain(u8 const * const in_key, const u32x key_len, u8 out_digest[16])
465 {
466 u32 pos;
467
468 // data
469
470 u32x data_buf[16];
471
472 for (pos = 0; pos < domain_len; pos++) {
473 ((u8 *)data_buf)[pos] = domain[pos];
474 }
475 for (pos = domain_len; pos < 64; pos++) {
476 ((u8 *)data_buf)[pos] = 0;
477 }
478
479 // key
480
481 u32x key_buf[16];
482
483 for (pos = 0; pos < key_len; pos++) {
484 ((u8 *)key_buf)[pos] = in_key[pos];
485 }
486 for (pos = key_len; pos < 64; pos++) {
487 ((u8 *)key_buf)[pos] = 0;
488 }
489
490 // pads
491
492 u32x ipad[4];
493 u32x opad[4];
494
495 hmac_md5_pad_cl (key_buf, key_buf + 4, key_buf + 8, key_buf + 12, ipad, opad);
496
497 // loop
498
499 append_0x80_2x4_VV (data_buf, data_buf + 4, domain_len);
500
501 data_buf[14] = (64 + domain_len) * 8;
502
503 hmac_md5_run_cl (data_buf, data_buf + 4, data_buf + 8, data_buf + 12, ipad, opad, (u32x *)out_digest);
504 }
505
506 void md5hmac_cl(u8 * inKey, u32 key_len, u8 * inData, u32 pw_len, u8 outDigest[DIGEST_SIZE])
507 {
508 u32 pos;
509
510 /**
511 * data
512 */
513
514 u32 data_buf[16];
515
516 for (pos = 0; pos < pw_len; pos++) {
517 ((u8 *)data_buf)[pos] = inData[pos];
518 }
519 for (pos = pw_len; pos < 64; pos++) {
520 ((u8 *)data_buf)[pos] = 0;
521 }
522
523 /**
524 * key
525 */
526
527 u32 key_buf[16];
528
529 for (pos = 0; pos < key_len; pos++) {
530 ((u8 *)key_buf)[pos] = inKey[pos];
531 }
532 for (pos = key_len; pos < 64; pos++) {
533 ((u8 *)key_buf)[pos] = 0;
534 }
535
536 /**
537 * pads
538 */
539
540 u32x ipad[4];
541 u32x opad[4];
542
543 hmac_md5_pad_cl (key_buf, key_buf + 4, key_buf + 8, key_buf + 12, ipad, opad);
544
545 /**
546 * loop
547 */
548
549 append_0x80_2x4_VV (data_buf, data_buf + 4, pw_len);
550
551 data_buf[14] = (64 + pw_len) * 8;
552
553 hmac_md5_run_cl (data_buf, data_buf + 4, data_buf + 8, data_buf + 12, ipad, opad, (u32x *)outDigest);
554 }
555
556
557
558