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