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