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