Merge pull request #260 from gm4tr1x/m13100_osx
[hashcat.git] / OpenCL / m13100_a0.cl
1 /**
2  * Authors......: Jens Steube <jens.steube@gmail.com>
3  *                Fist0urs <eddy.maaalou@gmail.com>
4  *                Gabriele Gristina <matrix@hashcat.net>
5  *
6  * License.....: MIT
7  */
8
9 #define _KRB5TGS_
10
11 #include "include/constants.h"
12 #include "include/kernel_vendor.h"
13
14 #define DGST_R0 0
15 #define DGST_R1 1
16 #define DGST_R2 2
17 #define DGST_R3 3
18
19 #include "include/kernel_functions.c"
20 #include "OpenCL/types_ocl.c"
21 #include "OpenCL/common.c"
22 #include "include/rp_kernel.h"
23 #include "OpenCL/rp.c"
24
25 typedef struct
26 {
27   u8 S[256];
28
29   u32 wtf_its_faster;
30
31 } RC4_KEY;
32
33 static void swap (__local RC4_KEY *rc4_key, const u8 i, const u8 j)
34 {
35   u8 tmp;
36
37   tmp           = rc4_key->S[i];
38   rc4_key->S[i] = rc4_key->S[j];
39   rc4_key->S[j] = tmp;
40 }
41
42 static void rc4_init_16 (__local RC4_KEY *rc4_key, const u32 data[4])
43 {
44   u32 v = 0x03020100;
45   u32 a = 0x04040404;
46
47   __local u32 *ptr = (__local u32 *) rc4_key->S;
48
49   #pragma unroll
50   for (u32 i = 0; i < 64; i++)
51   {
52     *ptr++ = v; v += a;
53   }
54
55   u32 j = 0;
56
57   for (u32 i = 0; i < 16; i++)
58   {
59     u32 idx = i * 16;
60
61     u32 v;
62
63     v = data[0];
64
65     j += rc4_key->S[idx] + (v >>  0); swap (rc4_key, idx, j); idx++;
66     j += rc4_key->S[idx] + (v >>  8); swap (rc4_key, idx, j); idx++;
67     j += rc4_key->S[idx] + (v >> 16); swap (rc4_key, idx, j); idx++;
68     j += rc4_key->S[idx] + (v >> 24); swap (rc4_key, idx, j); idx++;
69
70     v = data[1];
71
72     j += rc4_key->S[idx] + (v >>  0); swap (rc4_key, idx, j); idx++;
73     j += rc4_key->S[idx] + (v >>  8); swap (rc4_key, idx, j); idx++;
74     j += rc4_key->S[idx] + (v >> 16); swap (rc4_key, idx, j); idx++;
75     j += rc4_key->S[idx] + (v >> 24); swap (rc4_key, idx, j); idx++;
76
77     v = data[2];
78
79     j += rc4_key->S[idx] + (v >>  0); swap (rc4_key, idx, j); idx++;
80     j += rc4_key->S[idx] + (v >>  8); swap (rc4_key, idx, j); idx++;
81     j += rc4_key->S[idx] + (v >> 16); swap (rc4_key, idx, j); idx++;
82     j += rc4_key->S[idx] + (v >> 24); swap (rc4_key, idx, j); idx++;
83
84     v = data[3];
85
86     j += rc4_key->S[idx] + (v >>  0); swap (rc4_key, idx, j); idx++;
87     j += rc4_key->S[idx] + (v >>  8); swap (rc4_key, idx, j); idx++;
88     j += rc4_key->S[idx] + (v >> 16); swap (rc4_key, idx, j); idx++;
89     j += rc4_key->S[idx] + (v >> 24); swap (rc4_key, idx, j); idx++;
90   }
91 }
92
93 static u8 rc4_next_16 (__local RC4_KEY *rc4_key, u8 i, u8 j, __global u32 *in, u32 out[4])
94 {
95   #pragma unroll
96   for (u32 k = 0; k < 4; k++)
97   {
98     u32 xor4 = 0;
99
100     u8 idx;
101
102     i += 1;
103     j += rc4_key->S[i];
104
105     swap (rc4_key, i, j);
106
107     idx = rc4_key->S[i] + rc4_key->S[j];
108
109     xor4 |= rc4_key->S[idx] <<  0;
110
111     i += 1;
112     j += rc4_key->S[i];
113
114     swap (rc4_key, i, j);
115
116     idx = rc4_key->S[i] + rc4_key->S[j];
117
118     xor4 |= rc4_key->S[idx] <<  8;
119
120     i += 1;
121     j += rc4_key->S[i];
122
123     swap (rc4_key, i, j);
124
125     idx = rc4_key->S[i] + rc4_key->S[j];
126
127     xor4 |= rc4_key->S[idx] << 16;
128
129     i += 1;
130     j += rc4_key->S[i];
131
132     swap (rc4_key, i, j);
133
134     idx = rc4_key->S[i] + rc4_key->S[j];
135
136     xor4 |= rc4_key->S[idx] << 24;
137
138     out[k] = in[k] ^ xor4;
139   }
140
141   return j;
142 }
143
144 static void md4_transform (const u32 w0[4], const u32 w1[4], const u32 w2[4], const u32 w3[4], u32 digest[4])
145 {
146   u32 a = digest[0];
147   u32 b = digest[1];
148   u32 c = digest[2];
149   u32 d = digest[3];
150
151   MD4_STEP (MD4_Fo, a, b, c, d, w0[0], MD4C00, MD4S00);
152   MD4_STEP (MD4_Fo, d, a, b, c, w0[1], MD4C00, MD4S01);
153   MD4_STEP (MD4_Fo, c, d, a, b, w0[2], MD4C00, MD4S02);
154   MD4_STEP (MD4_Fo, b, c, d, a, w0[3], MD4C00, MD4S03);
155   MD4_STEP (MD4_Fo, a, b, c, d, w1[0], MD4C00, MD4S00);
156   MD4_STEP (MD4_Fo, d, a, b, c, w1[1], MD4C00, MD4S01);
157   MD4_STEP (MD4_Fo, c, d, a, b, w1[2], MD4C00, MD4S02);
158   MD4_STEP (MD4_Fo, b, c, d, a, w1[3], MD4C00, MD4S03);
159   MD4_STEP (MD4_Fo, a, b, c, d, w2[0], MD4C00, MD4S00);
160   MD4_STEP (MD4_Fo, d, a, b, c, w2[1], MD4C00, MD4S01);
161   MD4_STEP (MD4_Fo, c, d, a, b, w2[2], MD4C00, MD4S02);
162   MD4_STEP (MD4_Fo, b, c, d, a, w2[3], MD4C00, MD4S03);
163   MD4_STEP (MD4_Fo, a, b, c, d, w3[0], MD4C00, MD4S00);
164   MD4_STEP (MD4_Fo, d, a, b, c, w3[1], MD4C00, MD4S01);
165   MD4_STEP (MD4_Fo, c, d, a, b, w3[2], MD4C00, MD4S02);
166   MD4_STEP (MD4_Fo, b, c, d, a, w3[3], MD4C00, MD4S03);
167
168   MD4_STEP (MD4_Go, a, b, c, d, w0[0], MD4C01, MD4S10);
169   MD4_STEP (MD4_Go, d, a, b, c, w1[0], MD4C01, MD4S11);
170   MD4_STEP (MD4_Go, c, d, a, b, w2[0], MD4C01, MD4S12);
171   MD4_STEP (MD4_Go, b, c, d, a, w3[0], MD4C01, MD4S13);
172   MD4_STEP (MD4_Go, a, b, c, d, w0[1], MD4C01, MD4S10);
173   MD4_STEP (MD4_Go, d, a, b, c, w1[1], MD4C01, MD4S11);
174   MD4_STEP (MD4_Go, c, d, a, b, w2[1], MD4C01, MD4S12);
175   MD4_STEP (MD4_Go, b, c, d, a, w3[1], MD4C01, MD4S13);
176   MD4_STEP (MD4_Go, a, b, c, d, w0[2], MD4C01, MD4S10);
177   MD4_STEP (MD4_Go, d, a, b, c, w1[2], MD4C01, MD4S11);
178   MD4_STEP (MD4_Go, c, d, a, b, w2[2], MD4C01, MD4S12);
179   MD4_STEP (MD4_Go, b, c, d, a, w3[2], MD4C01, MD4S13);
180   MD4_STEP (MD4_Go, a, b, c, d, w0[3], MD4C01, MD4S10);
181   MD4_STEP (MD4_Go, d, a, b, c, w1[3], MD4C01, MD4S11);
182   MD4_STEP (MD4_Go, c, d, a, b, w2[3], MD4C01, MD4S12);
183   MD4_STEP (MD4_Go, b, c, d, a, w3[3], MD4C01, MD4S13);
184
185   MD4_STEP (MD4_H , a, b, c, d, w0[0], MD4C02, MD4S20);
186   MD4_STEP (MD4_H , d, a, b, c, w2[0], MD4C02, MD4S21);
187   MD4_STEP (MD4_H , c, d, a, b, w1[0], MD4C02, MD4S22);
188   MD4_STEP (MD4_H , b, c, d, a, w3[0], MD4C02, MD4S23);
189   MD4_STEP (MD4_H , a, b, c, d, w0[2], MD4C02, MD4S20);
190   MD4_STEP (MD4_H , d, a, b, c, w2[2], MD4C02, MD4S21);
191   MD4_STEP (MD4_H , c, d, a, b, w1[2], MD4C02, MD4S22);
192   MD4_STEP (MD4_H , b, c, d, a, w3[2], MD4C02, MD4S23);
193   MD4_STEP (MD4_H , a, b, c, d, w0[1], MD4C02, MD4S20);
194   MD4_STEP (MD4_H , d, a, b, c, w2[1], MD4C02, MD4S21);
195   MD4_STEP (MD4_H , c, d, a, b, w1[1], MD4C02, MD4S22);
196   MD4_STEP (MD4_H , b, c, d, a, w3[1], MD4C02, MD4S23);
197   MD4_STEP (MD4_H , a, b, c, d, w0[3], MD4C02, MD4S20);
198   MD4_STEP (MD4_H , d, a, b, c, w2[3], MD4C02, MD4S21);
199   MD4_STEP (MD4_H , c, d, a, b, w1[3], MD4C02, MD4S22);
200   MD4_STEP (MD4_H , b, c, d, a, w3[3], MD4C02, MD4S23);
201
202   digest[0] += a;
203   digest[1] += b;
204   digest[2] += c;
205   digest[3] += d;
206 }
207
208 static void md5_transform (const u32 w0[4], const u32 w1[4], const u32 w2[4], const u32 w3[4], u32 digest[4])
209 {
210   u32 a = digest[0];
211   u32 b = digest[1];
212   u32 c = digest[2];
213   u32 d = digest[3];
214
215   u32 w0_t = w0[0];
216   u32 w1_t = w0[1];
217   u32 w2_t = w0[2];
218   u32 w3_t = w0[3];
219   u32 w4_t = w1[0];
220   u32 w5_t = w1[1];
221   u32 w6_t = w1[2];
222   u32 w7_t = w1[3];
223   u32 w8_t = w2[0];
224   u32 w9_t = w2[1];
225   u32 wa_t = w2[2];
226   u32 wb_t = w2[3];
227   u32 wc_t = w3[0];
228   u32 wd_t = w3[1];
229   u32 we_t = w3[2];
230   u32 wf_t = w3[3];
231
232   MD5_STEP (MD5_Fo, a, b, c, d, w0_t, MD5C00, MD5S00);
233   MD5_STEP (MD5_Fo, d, a, b, c, w1_t, MD5C01, MD5S01);
234   MD5_STEP (MD5_Fo, c, d, a, b, w2_t, MD5C02, MD5S02);
235   MD5_STEP (MD5_Fo, b, c, d, a, w3_t, MD5C03, MD5S03);
236   MD5_STEP (MD5_Fo, a, b, c, d, w4_t, MD5C04, MD5S00);
237   MD5_STEP (MD5_Fo, d, a, b, c, w5_t, MD5C05, MD5S01);
238   MD5_STEP (MD5_Fo, c, d, a, b, w6_t, MD5C06, MD5S02);
239   MD5_STEP (MD5_Fo, b, c, d, a, w7_t, MD5C07, MD5S03);
240   MD5_STEP (MD5_Fo, a, b, c, d, w8_t, MD5C08, MD5S00);
241   MD5_STEP (MD5_Fo, d, a, b, c, w9_t, MD5C09, MD5S01);
242   MD5_STEP (MD5_Fo, c, d, a, b, wa_t, MD5C0a, MD5S02);
243   MD5_STEP (MD5_Fo, b, c, d, a, wb_t, MD5C0b, MD5S03);
244   MD5_STEP (MD5_Fo, a, b, c, d, wc_t, MD5C0c, MD5S00);
245   MD5_STEP (MD5_Fo, d, a, b, c, wd_t, MD5C0d, MD5S01);
246   MD5_STEP (MD5_Fo, c, d, a, b, we_t, MD5C0e, MD5S02);
247   MD5_STEP (MD5_Fo, b, c, d, a, wf_t, MD5C0f, MD5S03);
248
249   MD5_STEP (MD5_Go, a, b, c, d, w1_t, MD5C10, MD5S10);
250   MD5_STEP (MD5_Go, d, a, b, c, w6_t, MD5C11, MD5S11);
251   MD5_STEP (MD5_Go, c, d, a, b, wb_t, MD5C12, MD5S12);
252   MD5_STEP (MD5_Go, b, c, d, a, w0_t, MD5C13, MD5S13);
253   MD5_STEP (MD5_Go, a, b, c, d, w5_t, MD5C14, MD5S10);
254   MD5_STEP (MD5_Go, d, a, b, c, wa_t, MD5C15, MD5S11);
255   MD5_STEP (MD5_Go, c, d, a, b, wf_t, MD5C16, MD5S12);
256   MD5_STEP (MD5_Go, b, c, d, a, w4_t, MD5C17, MD5S13);
257   MD5_STEP (MD5_Go, a, b, c, d, w9_t, MD5C18, MD5S10);
258   MD5_STEP (MD5_Go, d, a, b, c, we_t, MD5C19, MD5S11);
259   MD5_STEP (MD5_Go, c, d, a, b, w3_t, MD5C1a, MD5S12);
260   MD5_STEP (MD5_Go, b, c, d, a, w8_t, MD5C1b, MD5S13);
261   MD5_STEP (MD5_Go, a, b, c, d, wd_t, MD5C1c, MD5S10);
262   MD5_STEP (MD5_Go, d, a, b, c, w2_t, MD5C1d, MD5S11);
263   MD5_STEP (MD5_Go, c, d, a, b, w7_t, MD5C1e, MD5S12);
264   MD5_STEP (MD5_Go, b, c, d, a, wc_t, MD5C1f, MD5S13);
265
266   MD5_STEP (MD5_H , a, b, c, d, w5_t, MD5C20, MD5S20);
267   MD5_STEP (MD5_H , d, a, b, c, w8_t, MD5C21, MD5S21);
268   MD5_STEP (MD5_H , c, d, a, b, wb_t, MD5C22, MD5S22);
269   MD5_STEP (MD5_H , b, c, d, a, we_t, MD5C23, MD5S23);
270   MD5_STEP (MD5_H , a, b, c, d, w1_t, MD5C24, MD5S20);
271   MD5_STEP (MD5_H , d, a, b, c, w4_t, MD5C25, MD5S21);
272   MD5_STEP (MD5_H , c, d, a, b, w7_t, MD5C26, MD5S22);
273   MD5_STEP (MD5_H , b, c, d, a, wa_t, MD5C27, MD5S23);
274   MD5_STEP (MD5_H , a, b, c, d, wd_t, MD5C28, MD5S20);
275   MD5_STEP (MD5_H , d, a, b, c, w0_t, MD5C29, MD5S21);
276   MD5_STEP (MD5_H , c, d, a, b, w3_t, MD5C2a, MD5S22);
277   MD5_STEP (MD5_H , b, c, d, a, w6_t, MD5C2b, MD5S23);
278   MD5_STEP (MD5_H , a, b, c, d, w9_t, MD5C2c, MD5S20);
279   MD5_STEP (MD5_H , d, a, b, c, wc_t, MD5C2d, MD5S21);
280   MD5_STEP (MD5_H , c, d, a, b, wf_t, MD5C2e, MD5S22);
281   MD5_STEP (MD5_H , b, c, d, a, w2_t, MD5C2f, MD5S23);
282
283   MD5_STEP (MD5_I , a, b, c, d, w0_t, MD5C30, MD5S30);
284   MD5_STEP (MD5_I , d, a, b, c, w7_t, MD5C31, MD5S31);
285   MD5_STEP (MD5_I , c, d, a, b, we_t, MD5C32, MD5S32);
286   MD5_STEP (MD5_I , b, c, d, a, w5_t, MD5C33, MD5S33);
287   MD5_STEP (MD5_I , a, b, c, d, wc_t, MD5C34, MD5S30);
288   MD5_STEP (MD5_I , d, a, b, c, w3_t, MD5C35, MD5S31);
289   MD5_STEP (MD5_I , c, d, a, b, wa_t, MD5C36, MD5S32);
290   MD5_STEP (MD5_I , b, c, d, a, w1_t, MD5C37, MD5S33);
291   MD5_STEP (MD5_I , a, b, c, d, w8_t, MD5C38, MD5S30);
292   MD5_STEP (MD5_I , d, a, b, c, wf_t, MD5C39, MD5S31);
293   MD5_STEP (MD5_I , c, d, a, b, w6_t, MD5C3a, MD5S32);
294   MD5_STEP (MD5_I , b, c, d, a, wd_t, MD5C3b, MD5S33);
295   MD5_STEP (MD5_I , a, b, c, d, w4_t, MD5C3c, MD5S30);
296   MD5_STEP (MD5_I , d, a, b, c, wb_t, MD5C3d, MD5S31);
297   MD5_STEP (MD5_I , c, d, a, b, w2_t, MD5C3e, MD5S32);
298   MD5_STEP (MD5_I , b, c, d, a, w9_t, MD5C3f, MD5S33);
299
300   digest[0] += a;
301   digest[1] += b;
302   digest[2] += c;
303   digest[3] += d;
304 }
305
306 static void hmac_md5_pad (u32 w0[4], u32 w1[4], u32 w2[4], u32 w3[4], u32 ipad[4], u32 opad[4])
307 {
308   w0[0] = w0[0] ^ 0x36363636;
309   w0[1] = w0[1] ^ 0x36363636;
310   w0[2] = w0[2] ^ 0x36363636;
311   w0[3] = w0[3] ^ 0x36363636;
312   w1[0] = w1[0] ^ 0x36363636;
313   w1[1] = w1[1] ^ 0x36363636;
314   w1[2] = w1[2] ^ 0x36363636;
315   w1[3] = w1[3] ^ 0x36363636;
316   w2[0] = w2[0] ^ 0x36363636;
317   w2[1] = w2[1] ^ 0x36363636;
318   w2[2] = w2[2] ^ 0x36363636;
319   w2[3] = w2[3] ^ 0x36363636;
320   w3[0] = w3[0] ^ 0x36363636;
321   w3[1] = w3[1] ^ 0x36363636;
322   w3[2] = w3[2] ^ 0x36363636;
323   w3[3] = w3[3] ^ 0x36363636;
324
325   ipad[0] = MD5M_A;
326   ipad[1] = MD5M_B;
327   ipad[2] = MD5M_C;
328   ipad[3] = MD5M_D;
329
330   md5_transform (w0, w1, w2, w3, ipad);
331
332   w0[0] = w0[0] ^ 0x6a6a6a6a;
333   w0[1] = w0[1] ^ 0x6a6a6a6a;
334   w0[2] = w0[2] ^ 0x6a6a6a6a;
335   w0[3] = w0[3] ^ 0x6a6a6a6a;
336   w1[0] = w1[0] ^ 0x6a6a6a6a;
337   w1[1] = w1[1] ^ 0x6a6a6a6a;
338   w1[2] = w1[2] ^ 0x6a6a6a6a;
339   w1[3] = w1[3] ^ 0x6a6a6a6a;
340   w2[0] = w2[0] ^ 0x6a6a6a6a;
341   w2[1] = w2[1] ^ 0x6a6a6a6a;
342   w2[2] = w2[2] ^ 0x6a6a6a6a;
343   w2[3] = w2[3] ^ 0x6a6a6a6a;
344   w3[0] = w3[0] ^ 0x6a6a6a6a;
345   w3[1] = w3[1] ^ 0x6a6a6a6a;
346   w3[2] = w3[2] ^ 0x6a6a6a6a;
347   w3[3] = w3[3] ^ 0x6a6a6a6a;
348
349   opad[0] = MD5M_A;
350   opad[1] = MD5M_B;
351   opad[2] = MD5M_C;
352   opad[3] = MD5M_D;
353
354   md5_transform (w0, w1, w2, w3, opad);
355 }
356
357 static void hmac_md5_run (u32 w0[4], u32 w1[4], u32 w2[4], u32 w3[4], u32 ipad[4], u32 opad[4], u32 digest[4])
358 {
359   digest[0] = ipad[0];
360   digest[1] = ipad[1];
361   digest[2] = ipad[2];
362   digest[3] = ipad[3];
363
364   md5_transform (w0, w1, w2, w3, digest);
365
366   w0[0] = digest[0];
367   w0[1] = digest[1];
368   w0[2] = digest[2];
369   w0[3] = digest[3];
370   w1[0] = 0x80;
371   w1[1] = 0;
372   w1[2] = 0;
373   w1[3] = 0;
374   w2[0] = 0;
375   w2[1] = 0;
376   w2[2] = 0;
377   w2[3] = 0;
378   w3[0] = 0;
379   w3[1] = 0;
380   w3[2] = (64 + 16) * 8;
381   w3[3] = 0;
382
383   digest[0] = opad[0];
384   digest[1] = opad[1];
385   digest[2] = opad[2];
386   digest[3] = opad[3];
387
388   md5_transform (w0, w1, w2, w3, digest);
389 }
390
391 static int decrypt_and_check (__local RC4_KEY *rc4_key, u32 data[4], __global u32 *edata2, const u32 edata2_len, const u32 K2[4], const u32 checksum[4])
392 {
393   rc4_init_16 (rc4_key, data);
394
395   u32 out0[4];
396   u32 out1[4];
397
398   u8 i = 0;
399   u8 j = 0;
400
401   /*
402     8 first bytes are nonce, then ASN1 structs (DER encoding: type-length-data)
403
404     if length >= 128 bytes:
405         length is on 2 bytes and type is \x63\x82 (encode_krb5_enc_tkt_part) and data is an ASN1 sequence \x30\x82
406     else:
407         length is on 1 byte and type is \x63\x81 and data is an ASN1 sequence \x30\x81
408
409     next headers follow the same ASN1 "type-length-data" scheme
410   */
411
412   j = rc4_next_16 (rc4_key, i, j, edata2 + 0, out0); i += 16;
413
414   if (((out0[2] & 0xff00ffff) != 0x30008163) && ((out0[2] & 0x0000ffff) != 0x00008263)) return 0;
415
416   j = rc4_next_16 (rc4_key, i, j, edata2 + 4, out1); i += 16;
417
418   if (((out1[0] & 0x00ffffff) != 0x00000503) && (out1[0] != 0x050307A0)) return 0;
419
420   rc4_init_16 (rc4_key, data);
421
422   i = 0;
423   j = 0;
424
425   // init hmac
426
427   u32 w0[4];
428   u32 w1[4];
429   u32 w2[4];
430   u32 w3[4];
431
432   w0[0] = K2[0];
433   w0[1] = K2[1];
434   w0[2] = K2[2];
435   w0[3] = K2[3];
436   w1[0] = 0;
437   w1[1] = 0;
438   w1[2] = 0;
439   w1[3] = 0;
440   w2[0] = 0;
441   w2[1] = 0;
442   w2[2] = 0;
443   w2[3] = 0;
444   w3[0] = 0;
445   w3[1] = 0;
446   w3[2] = 0;
447   w3[3] = 0;
448
449   u32 ipad[4];
450   u32 opad[4];
451
452   hmac_md5_pad (w0, w1, w2, w3, ipad, opad);
453
454   int edata2_left;
455
456   for (edata2_left = edata2_len; edata2_left >= 64; edata2_left -= 64)
457   {
458     j = rc4_next_16 (rc4_key, i, j, edata2, w0); i += 16; edata2 += 4;
459     j = rc4_next_16 (rc4_key, i, j, edata2, w1); i += 16; edata2 += 4;
460     j = rc4_next_16 (rc4_key, i, j, edata2, w2); i += 16; edata2 += 4;
461     j = rc4_next_16 (rc4_key, i, j, edata2, w3); i += 16; edata2 += 4;
462
463     md5_transform (w0, w1, w2, w3, ipad);
464   }
465
466   w0[0] = 0;
467   w0[1] = 0;
468   w0[2] = 0;
469   w0[3] = 0;
470   w1[0] = 0;
471   w1[1] = 0;
472   w1[2] = 0;
473   w1[3] = 0;
474   w2[0] = 0;
475   w2[1] = 0;
476   w2[2] = 0;
477   w2[3] = 0;
478   w3[0] = 0;
479   w3[1] = 0;
480   w3[2] = 0;
481   w3[3] = 0;
482
483   if (edata2_left < 16)
484   {
485     j = rc4_next_16 (rc4_key, i, j, edata2, w0); i += 16; edata2 += 4;
486
487     truncate_block  (w0, edata2_left & 0xf);
488     append_0x80_1x4 (w0, edata2_left & 0xf);
489
490     w3[2] = (64 + edata2_len) * 8;
491     w3[3] = 0;
492
493     md5_transform (w0, w1, w2, w3, ipad);
494   }
495   else if (edata2_left < 32)
496   {
497     j = rc4_next_16 (rc4_key, i, j, edata2, w0); i += 16; edata2 += 4;
498     j = rc4_next_16 (rc4_key, i, j, edata2, w1); i += 16; edata2 += 4;
499
500     truncate_block  (w1, edata2_left & 0xf);
501     append_0x80_1x4 (w1, edata2_left & 0xf);
502
503     w3[2] = (64 + edata2_len) * 8;
504     w3[3] = 0;
505
506     md5_transform (w0, w1, w2, w3, ipad);
507   }
508   else if (edata2_left < 48)
509   {
510     j = rc4_next_16 (rc4_key, i, j, edata2, w0); i += 16; edata2 += 4;
511     j = rc4_next_16 (rc4_key, i, j, edata2, w1); i += 16; edata2 += 4;
512     j = rc4_next_16 (rc4_key, i, j, edata2, w2); i += 16; edata2 += 4;
513
514     truncate_block  (w2, edata2_left & 0xf);
515     append_0x80_1x4 (w2, edata2_left & 0xf);
516
517     w3[2] = (64 + edata2_len) * 8;
518     w3[3] = 0;
519
520     md5_transform (w0, w1, w2, w3, ipad);
521   }
522   else
523   {
524     j = rc4_next_16 (rc4_key, i, j, edata2, w0); i += 16; edata2 += 4;
525     j = rc4_next_16 (rc4_key, i, j, edata2, w1); i += 16; edata2 += 4;
526     j = rc4_next_16 (rc4_key, i, j, edata2, w2); i += 16; edata2 += 4;
527     j = rc4_next_16 (rc4_key, i, j, edata2, w3); i += 16; edata2 += 4;
528
529     truncate_block  (w3, edata2_left & 0xf);
530     append_0x80_1x4 (w3, edata2_left & 0xf);
531
532     if (edata2_left < 56)
533     {
534       w3[2] = (64 + edata2_len) * 8;
535       w3[3] = 0;
536
537       md5_transform (w0, w1, w2, w3, ipad);
538     }
539     else
540     {
541       md5_transform (w0, w1, w2, w3, ipad);
542
543       w0[0] = 0;
544       w0[1] = 0;
545       w0[2] = 0;
546       w0[3] = 0;
547       w1[0] = 0;
548       w1[1] = 0;
549       w1[2] = 0;
550       w1[3] = 0;
551       w2[0] = 0;
552       w2[1] = 0;
553       w2[2] = 0;
554       w2[3] = 0;
555       w3[0] = 0;
556       w3[1] = 0;
557       w3[2] = (64 + edata2_len) * 8;
558       w3[3] = 0;
559
560       md5_transform (w0, w1, w2, w3, ipad);
561     }
562   }
563
564   w0[0] = ipad[0];
565   w0[1] = ipad[1];
566   w0[2] = ipad[2];
567   w0[3] = ipad[3];
568   w1[0] = 0x80;
569   w1[1] = 0;
570   w1[2] = 0;
571   w1[3] = 0;
572   w2[0] = 0;
573   w2[1] = 0;
574   w2[2] = 0;
575   w2[3] = 0;
576   w3[0] = 0;
577   w3[1] = 0;
578   w3[2] = (64 + 16) * 8;
579   w3[3] = 0;
580
581   md5_transform (w0, w1, w2, w3, opad);
582
583   if (checksum[0] != opad[0]) return 0;
584   if (checksum[1] != opad[1]) return 0;
585   if (checksum[2] != opad[2]) return 0;
586   if (checksum[3] != opad[3]) return 0;
587
588   return 1;
589 }
590
591 static void kerb_prepare (const u32 w0[4], const u32 w1[4], const u32 pw_len, const u32 checksum[4], u32 digest[4], u32 K2[4])
592 {
593   /**
594    * pads
595    */
596
597   u32 w0_t[4];
598   u32 w1_t[4];
599   u32 w2_t[4];
600   u32 w3_t[4];
601
602   w0_t[0] = w0[0];
603   w0_t[1] = w0[1];
604   w0_t[2] = w0[2];
605   w0_t[3] = w0[3];
606   w1_t[0] = w1[0];
607   w1_t[1] = w1[1];
608   w1_t[2] = w1[2];
609   w1_t[3] = w1[3];
610   w2_t[0] = 0;
611   w2_t[1] = 0;
612   w2_t[2] = 0;
613   w2_t[3] = 0;
614   w3_t[0] = 0;
615   w3_t[1] = 0;
616   w3_t[2] = 0;
617   w3_t[3] = 0;
618
619   // K=MD4(Little_indian(UNICODE(pwd))
620
621   append_0x80_2x4 (w0_t, w1_t, pw_len);
622
623   make_unicode (w1_t, w2_t, w3_t);
624   make_unicode (w0_t, w0_t, w1_t);
625
626   w3_t[2] = pw_len * 8 * 2;
627   w3_t[3] = 0;
628
629   digest[0] = MD4M_A;
630   digest[1] = MD4M_B;
631   digest[2] = MD4M_C;
632   digest[3] = MD4M_D;
633
634   md4_transform (w0_t, w1_t, w2_t, w3_t, digest);
635
636   // K1=MD5_HMAC(K,1); with 2 encoded as little indian on 4 bytes (02000000 in hexa);
637
638   w0_t[0] = digest[0];
639   w0_t[1] = digest[1];
640   w0_t[2] = digest[2];
641   w0_t[3] = digest[3];
642   w1_t[0] = 0;
643   w1_t[1] = 0;
644   w1_t[2] = 0;
645   w1_t[3] = 0;
646   w2_t[0] = 0;
647   w2_t[1] = 0;
648   w2_t[2] = 0;
649   w2_t[3] = 0;
650   w3_t[0] = 0;
651   w3_t[1] = 0;
652   w3_t[2] = 0;
653   w3_t[3] = 0;
654
655   u32 ipad[4];
656   u32 opad[4];
657
658   hmac_md5_pad (w0_t, w1_t, w2_t, w3_t, ipad, opad);
659
660   w0_t[0] = 2;
661   w0_t[1] = 0x80;
662   w0_t[2] = 0;
663   w0_t[3] = 0;
664   w1_t[0] = 0;
665   w1_t[1] = 0;
666   w1_t[2] = 0;
667   w1_t[3] = 0;
668   w2_t[0] = 0;
669   w2_t[1] = 0;
670   w2_t[2] = 0;
671   w2_t[3] = 0;
672   w3_t[0] = 0;
673   w3_t[1] = 0;
674   w3_t[2] = (64 + 4) * 8;
675   w3_t[3] = 0;
676
677   hmac_md5_run (w0_t, w1_t, w2_t, w3_t, ipad, opad, digest);
678
679   // K2 = K1;
680
681   K2[0] = digest[0];
682   K2[1] = digest[1];
683   K2[2] = digest[2];
684   K2[3] = digest[3];
685
686   // K3=MD5_HMAC(K1,checksum);
687
688   w0_t[0] = digest[0];
689   w0_t[1] = digest[1];
690   w0_t[2] = digest[2];
691   w0_t[3] = digest[3];
692   w1_t[0] = 0;
693   w1_t[1] = 0;
694   w1_t[2] = 0;
695   w1_t[3] = 0;
696   w2_t[0] = 0;
697   w2_t[1] = 0;
698   w2_t[2] = 0;
699   w2_t[3] = 0;
700   w3_t[0] = 0;
701   w3_t[1] = 0;
702   w3_t[2] = 0;
703   w3_t[3] = 0;
704
705   hmac_md5_pad (w0_t, w1_t, w2_t, w3_t, ipad, opad);
706
707   w0_t[0] = checksum[0];
708   w0_t[1] = checksum[1];
709   w0_t[2] = checksum[2];
710   w0_t[3] = checksum[3];
711   w1_t[0] = 0x80;
712   w1_t[1] = 0;
713   w1_t[2] = 0;
714   w1_t[3] = 0;
715   w2_t[0] = 0;
716   w2_t[1] = 0;
717   w2_t[2] = 0;
718   w2_t[3] = 0;
719   w3_t[0] = 0;
720   w3_t[1] = 0;
721   w3_t[2] = (64 + 16) * 8;
722   w3_t[3] = 0;
723
724   hmac_md5_run (w0_t, w1_t, w2_t, w3_t, ipad, opad, digest);
725 }
726
727 static void m13100 (__local RC4_KEY *rc4_keys, u32 w0[4], u32 w1[4], u32 w2[4], u32 w3[4], const u32 pw_len, __global pw_t *pws, __global kernel_rule_t *rules_buf, __global comb_t *combs_buf, __global bf_t *bfs_buf, __global void *tmps, __global void *hooks, __global u32 *bitmaps_buf_s1_a, __global u32 *bitmaps_buf_s1_b, __global u32 *bitmaps_buf_s1_c, __global u32 *bitmaps_buf_s1_d, __global u32 *bitmaps_buf_s2_a, __global u32 *bitmaps_buf_s2_b, __global u32 *bitmaps_buf_s2_c, __global u32 *bitmaps_buf_s2_d, __global plain_t *plains_buf, __global digest_t *digests_buf, __global u32 *hashes_shown, __global salt_t *salt_bufs, __global krb5tgs_t *krb5tgs_bufs, __global u32 *d_return_buf, __global u32 *d_scryptV_buf, const u32 bitmap_mask, const u32 bitmap_shift1, const u32 bitmap_shift2, const u32 salt_pos, const u32 loop_pos, const u32 loop_cnt, const u32 il_cnt, const u32 digests_cnt, const u32 digests_offset)
728 {
729   /**
730    * modifier
731    */
732
733   const u32 gid = get_global_id (0);
734   const u32 lid = get_local_id (0);
735
736   /**
737    * salt
738    */
739
740   u32 checksum[4];
741
742   checksum[0] = krb5tgs_bufs[salt_pos].checksum[0];
743   checksum[1] = krb5tgs_bufs[salt_pos].checksum[1];
744   checksum[2] = krb5tgs_bufs[salt_pos].checksum[2];
745   checksum[3] = krb5tgs_bufs[salt_pos].checksum[3];
746
747   /**
748    * loop
749    */
750
751   u32 w0l = w0[0];
752
753   for (u32 il_pos = 0; il_pos < il_cnt; il_pos++)
754   {
755     const u32 w0r = bfs_buf[il_pos].i;
756
757     w0[0] = w0l | w0r;
758
759     u32 digest[4];
760
761     u32 K2[4];
762
763     kerb_prepare (w0, w1, pw_len, checksum, digest, K2);
764
765     u32 tmp[4];
766
767     tmp[0] = digest[0];
768     tmp[1] = digest[1];
769     tmp[2] = digest[2];
770     tmp[3] = digest[3];
771
772     if (decrypt_and_check (&rc4_keys[lid], tmp, krb5tgs_bufs[salt_pos].edata2, krb5tgs_bufs[salt_pos].edata2_len, K2, checksum) == 1)
773     {
774       mark_hash (plains_buf, hashes_shown, digests_offset, gid, il_pos);
775
776       d_return_buf[lid] = 1;
777     }
778   }
779 }
780
781 __kernel void m13100_m04 (__global pw_t *pws, __global kernel_rule_t *rules_buf, __global comb_t *combs_buf, __global bf_t *bfs_buf, __global void *tmps, __global void *hooks, __global u32 *bitmaps_buf_s1_a, __global u32 *bitmaps_buf_s1_b, __global u32 *bitmaps_buf_s1_c, __global u32 *bitmaps_buf_s1_d, __global u32 *bitmaps_buf_s2_a, __global u32 *bitmaps_buf_s2_b, __global u32 *bitmaps_buf_s2_c, __global u32 *bitmaps_buf_s2_d, __global plain_t *plains_buf, __global digest_t *digests_buf, __global u32 *hashes_shown, __global salt_t *salt_bufs, __global krb5tgs_t *krb5tgs_bufs, __global u32 *d_return_buf, __global u32 *d_scryptV_buf, const u32 bitmap_mask, const u32 bitmap_shift1, const u32 bitmap_shift2, const u32 salt_pos, const u32 loop_pos, const u32 loop_cnt, const u32 il_cnt, const u32 digests_cnt, const u32 digests_offset, const u32 combs_mode, const u32 gid_max)
782 {
783   /**
784    * modifier
785    */
786
787   __local RC4_KEY rc4_keys[64];
788
789   const u32 lid = get_local_id (0);
790   const u32 gid = get_global_id (0);
791
792   if (gid >= gid_max) return;
793
794   /**
795    * base
796    */
797
798   u32 pw_buf0[4];
799
800   pw_buf0[0] = pws[gid].i[ 0];
801   pw_buf0[1] = pws[gid].i[ 1];
802   pw_buf0[2] = pws[gid].i[ 2];
803   pw_buf0[3] = pws[gid].i[ 3];
804
805   u32 pw_buf1[4];
806
807   pw_buf1[0] = pws[gid].i[ 4];
808   pw_buf1[1] = pws[gid].i[ 5];
809   pw_buf1[2] = pws[gid].i[ 6];
810   pw_buf1[3] = pws[gid].i[ 7];
811
812   const u32 pw_len = pws[gid].pw_len;
813
814   /**
815    * salt
816    */
817
818   u32 checksum[4];
819
820   checksum[0] = krb5tgs_bufs[salt_pos].checksum[0];
821   checksum[1] = krb5tgs_bufs[salt_pos].checksum[1];
822   checksum[2] = krb5tgs_bufs[salt_pos].checksum[2];
823   checksum[3] = krb5tgs_bufs[salt_pos].checksum[3];
824
825   /**
826    * loop
827    */
828
829   for (u32 il_pos = 0; il_pos < il_cnt; il_pos++)
830   {
831     u32 w0[4];
832
833     w0[0] = pw_buf0[0];
834     w0[1] = pw_buf0[1];
835     w0[2] = pw_buf0[2];
836     w0[3] = pw_buf0[3];
837
838     u32 w1[4];
839
840     w1[0] = pw_buf1[0];
841     w1[1] = pw_buf1[1];
842     w1[2] = pw_buf1[2];
843     w1[3] = pw_buf1[3];
844
845     u32 w2[4];
846
847     w2[0] = 0;
848     w2[1] = 0;
849     w2[2] = 0;
850     w2[3] = 0;
851
852     u32 w3[4];
853
854     w3[0] = 0;
855     w3[1] = 0;
856     w3[2] = 0;
857     w3[3] = 0;
858
859     const u32 out_len = apply_rules (rules_buf[il_pos].cmds, w0, w1, pw_len);
860
861     /**
862      * kerberos
863      */
864
865     u32 digest[4];
866
867     u32 K2[4];
868
869     kerb_prepare (w0, w1, pw_len, checksum, digest, K2);
870
871     u32 tmp[4];
872
873     tmp[0] = digest[0];
874     tmp[1] = digest[1];
875     tmp[2] = digest[2];
876     tmp[3] = digest[3];
877
878     if (decrypt_and_check (&rc4_keys[lid], tmp, krb5tgs_bufs[salt_pos].edata2, krb5tgs_bufs[salt_pos].edata2_len, K2, checksum) == 1)
879     {
880       mark_hash (plains_buf, hashes_shown, digests_offset, gid, il_pos);
881
882       d_return_buf[lid] = 1;
883     }
884   }
885 }
886
887 __kernel void m13100_m08 (__global pw_t *pws, __global kernel_rule_t *rules_buf, __global comb_t *combs_buf, __global bf_t *bfs_buf, __global void *tmps, __global void *hooks, __global u32 *bitmaps_buf_s1_a, __global u32 *bitmaps_buf_s1_b, __global u32 *bitmaps_buf_s1_c, __global u32 *bitmaps_buf_s1_d, __global u32 *bitmaps_buf_s2_a, __global u32 *bitmaps_buf_s2_b, __global u32 *bitmaps_buf_s2_c, __global u32 *bitmaps_buf_s2_d, __global plain_t *plains_buf, __global digest_t *digests_buf, __global u32 *hashes_shown, __global salt_t *salt_bufs, __global krb5tgs_t *krb5tgs_bufs, __global u32 *d_return_buf, __global u32 *d_scryptV_buf, const u32 bitmap_mask, const u32 bitmap_shift1, const u32 bitmap_shift2, const u32 salt_pos, const u32 loop_pos, const u32 loop_cnt, const u32 il_cnt, const u32 digests_cnt, const u32 digests_offset, const u32 combs_mode, const u32 gid_max)
888 {
889 }
890
891 __kernel void m13100_m16 (__global pw_t *pws, __global kernel_rule_t *rules_buf, __global comb_t *combs_buf, __global bf_t *bfs_buf, __global void *tmps, __global void *hooks, __global u32 *bitmaps_buf_s1_a, __global u32 *bitmaps_buf_s1_b, __global u32 *bitmaps_buf_s1_c, __global u32 *bitmaps_buf_s1_d, __global u32 *bitmaps_buf_s2_a, __global u32 *bitmaps_buf_s2_b, __global u32 *bitmaps_buf_s2_c, __global u32 *bitmaps_buf_s2_d, __global plain_t *plains_buf, __global digest_t *digests_buf, __global u32 *hashes_shown, __global salt_t *salt_bufs, __global krb5tgs_t *krb5tgs_bufs, __global u32 *d_return_buf, __global u32 *d_scryptV_buf, const u32 bitmap_mask, const u32 bitmap_shift1, const u32 bitmap_shift2, const u32 salt_pos, const u32 loop_pos, const u32 loop_cnt, const u32 il_cnt, const u32 digests_cnt, const u32 digests_offset, const u32 combs_mode, const u32 gid_max)
892 {
893 }
894
895 __kernel void m13100_s04 (__global pw_t *pws, __global kernel_rule_t *rules_buf, __global comb_t *combs_buf, __global bf_t *bfs_buf, __global void *tmps, __global void *hooks, __global u32 *bitmaps_buf_s1_a, __global u32 *bitmaps_buf_s1_b, __global u32 *bitmaps_buf_s1_c, __global u32 *bitmaps_buf_s1_d, __global u32 *bitmaps_buf_s2_a, __global u32 *bitmaps_buf_s2_b, __global u32 *bitmaps_buf_s2_c, __global u32 *bitmaps_buf_s2_d, __global plain_t *plains_buf, __global digest_t *digests_buf, __global u32 *hashes_shown, __global salt_t *salt_bufs, __global krb5tgs_t *krb5tgs_bufs, __global u32 *d_return_buf, __global u32 *d_scryptV_buf, const u32 bitmap_mask, const u32 bitmap_shift1, const u32 bitmap_shift2, const u32 salt_pos, const u32 loop_pos, const u32 loop_cnt, const u32 il_cnt, const u32 digests_cnt, const u32 digests_offset, const u32 combs_mode, const u32 gid_max)
896 {
897   /**
898    * modifier
899    */
900
901   __local RC4_KEY rc4_keys[64];
902
903   const u32 lid = get_local_id (0);
904   const u32 gid = get_global_id (0);
905
906   if (gid >= gid_max) return;
907
908   /**
909    * base
910    */
911
912   u32 pw_buf0[4];
913
914   pw_buf0[0] = pws[gid].i[ 0];
915   pw_buf0[1] = pws[gid].i[ 1];
916   pw_buf0[2] = pws[gid].i[ 2];
917   pw_buf0[3] = pws[gid].i[ 3];
918
919   u32 pw_buf1[4];
920
921   pw_buf1[0] = pws[gid].i[ 4];
922   pw_buf1[1] = pws[gid].i[ 5];
923   pw_buf1[2] = pws[gid].i[ 6];
924   pw_buf1[3] = pws[gid].i[ 7];
925
926   const u32 pw_len = pws[gid].pw_len;
927
928   /**
929    * salt
930    */
931
932   u32 checksum[4];
933
934   checksum[0] = krb5tgs_bufs[salt_pos].checksum[0];
935   checksum[1] = krb5tgs_bufs[salt_pos].checksum[1];
936   checksum[2] = krb5tgs_bufs[salt_pos].checksum[2];
937   checksum[3] = krb5tgs_bufs[salt_pos].checksum[3];
938
939   /**
940    * loop
941    */
942
943   for (u32 il_pos = 0; il_pos < il_cnt; il_pos++)
944   {
945     u32 w0[4];
946
947     w0[0] = pw_buf0[0];
948     w0[1] = pw_buf0[1];
949     w0[2] = pw_buf0[2];
950     w0[3] = pw_buf0[3];
951
952     u32 w1[4];
953
954     w1[0] = pw_buf1[0];
955     w1[1] = pw_buf1[1];
956     w1[2] = pw_buf1[2];
957     w1[3] = pw_buf1[3];
958
959     u32 w2[4];
960
961     w2[0] = 0;
962     w2[1] = 0;
963     w2[2] = 0;
964     w2[3] = 0;
965
966     u32 w3[4];
967
968     w3[0] = 0;
969     w3[1] = 0;
970     w3[2] = 0;
971     w3[3] = 0;
972
973     const u32 out_len = apply_rules (rules_buf[il_pos].cmds, w0, w1, pw_len);
974
975     /**
976      * kerberos
977      */
978
979     u32 digest[4];
980
981     u32 K2[4];
982
983     kerb_prepare (w0, w1, pw_len, checksum, digest, K2);
984
985     u32 tmp[4];
986
987     tmp[0] = digest[0];
988     tmp[1] = digest[1];
989     tmp[2] = digest[2];
990     tmp[3] = digest[3];
991
992     if (decrypt_and_check (&rc4_keys[lid], tmp, krb5tgs_bufs[salt_pos].edata2, krb5tgs_bufs[salt_pos].edata2_len, K2, checksum) == 1)
993     {
994       mark_hash (plains_buf, hashes_shown, digests_offset, gid, il_pos);
995
996       d_return_buf[lid] = 1;
997     }
998   }
999 }
1000
1001 __kernel void m13100_s08 (__global pw_t *pws, __global kernel_rule_t *rules_buf, __global comb_t *combs_buf, __global bf_t *bfs_buf, __global void *tmps, __global void *hooks, __global u32 *bitmaps_buf_s1_a, __global u32 *bitmaps_buf_s1_b, __global u32 *bitmaps_buf_s1_c, __global u32 *bitmaps_buf_s1_d, __global u32 *bitmaps_buf_s2_a, __global u32 *bitmaps_buf_s2_b, __global u32 *bitmaps_buf_s2_c, __global u32 *bitmaps_buf_s2_d, __global plain_t *plains_buf, __global digest_t *digests_buf, __global u32 *hashes_shown, __global salt_t *salt_bufs, __global krb5tgs_t *krb5tgs_bufs, __global u32 *d_return_buf, __global u32 *d_scryptV_buf, const u32 bitmap_mask, const u32 bitmap_shift1, const u32 bitmap_shift2, const u32 salt_pos, const u32 loop_pos, const u32 loop_cnt, const u32 il_cnt, const u32 digests_cnt, const u32 digests_offset, const u32 combs_mode, const u32 gid_max)
1002 {
1003 }
1004
1005 __kernel void m13100_s16 (__global pw_t *pws, __global kernel_rule_t *rules_buf, __global comb_t *combs_buf, __global bf_t *bfs_buf, __global void *tmps, __global void *hooks, __global u32 *bitmaps_buf_s1_a, __global u32 *bitmaps_buf_s1_b, __global u32 *bitmaps_buf_s1_c, __global u32 *bitmaps_buf_s1_d, __global u32 *bitmaps_buf_s2_a, __global u32 *bitmaps_buf_s2_b, __global u32 *bitmaps_buf_s2_c, __global u32 *bitmaps_buf_s2_d, __global plain_t *plains_buf, __global digest_t *digests_buf, __global u32 *hashes_shown, __global salt_t *salt_bufs, __global krb5tgs_t *krb5tgs_bufs, __global u32 *d_return_buf, __global u32 *d_scryptV_buf, const u32 bitmap_mask, const u32 bitmap_shift1, const u32 bitmap_shift2, const u32 salt_pos, const u32 loop_pos, const u32 loop_cnt, const u32 il_cnt, const u32 digests_cnt, const u32 digests_offset, const u32 combs_mode, const u32 gid_max)
1006 {
1007 }