replace swap32 and swap64 with faster versions for NV
[hashcat.git] / OpenCL / types_ocl.c
1 /**
2 * Author......: Jens Steube <jens.steube@gmail.com>
3 * License.....: MIT
4 */
5
6 typedef uchar u8;
7 typedef ushort u16;
8 typedef uint u32;
9 typedef ulong u64;
10
11 #ifdef IS_AMD
12 static inline u32 swap32 (const u32 v)
13 {
14 return (as_uint (as_uchar4 (v).s3210));
15 }
16
17 static inline u64 swap64 (const u64 v)
18 {
19 return (as_ulong (as_uchar8 (v).s76543210));
20 }
21
22 #endif
23
24 #ifdef IS_NV
25 static inline u32 swap32 (const u32 v)
26 {
27 u32 r;
28
29 asm ("prmt.b32 %0, %1, 0, 0x0123;" : "=r"(r) : "r"(v));
30
31 return r;
32 }
33
34 static inline u64 swap64 (const u64 v)
35 {
36 u32 il;
37 u32 ir;
38
39 asm ("mov.b64 {%0, %1}, %2;" : "=r"(il), "=r"(ir) : "l"(v));
40
41 u32 tl;
42 u32 tr;
43
44 asm ("prmt.b32 %0, %1, 0, 0x0123;" : "=r"(tl) : "r"(il));
45 asm ("prmt.b32 %0, %1, 0, 0x0123;" : "=r"(tr) : "r"(ir));
46
47 u64 r;
48
49 asm ("mov.b64 %0, {%1, %2};" : "=l"(r) : "r"(tr), "r"(tl));
50
51 return r;
52 }
53
54 #endif
55
56 #ifdef IS_AMD
57 static inline u32 __bfe (const u32 a, const u32 b, const u32 c)
58 {
59 return amd_bfe (a, b, c);
60 }
61 #endif
62
63 #ifdef IS_NV
64 static inline u32 __byte_perm (const u32 a, const u32 b, const u32 c)
65 {
66 u32 r;
67
68 asm ("prmt.b32 %0, %1, %2, %3;" : "=r"(r) : "r"(a), "r"(b), "r"(c));
69
70 return r;
71 }
72
73 static inline u32 __bfe (const u32 a, const u32 b, const u32 c)
74 {
75 u32 r;
76
77 asm ("bfe.u32 %0, %1, %2, %3;" : "=r"(r) : "r"(a), "r"(b), "r"(c));
78
79 return r;
80 }
81
82 #if CUDA_ARCH >= 350
83
84 static inline u32 amd_bytealign (const u32 a, const u32 b, const u32 c)
85 {
86 u32 r;
87
88 asm ("shf.r.wrap.b32 %0, %1, %2, %3;" : "=r"(r) : "r"(b), "r"(a), "r"((c & 3) * 8));
89
90 return r;
91 }
92
93 #else
94
95 static inline u32 amd_bytealign (const u32 a, const u32 b, const u32 c)
96 {
97 return __byte_perm (b, a, (0x76543210 >> ((c & 3) * 4)) & 0xffff);
98 }
99
100 #endif
101
102 static inline u32 lut3_2d (const u32 a, const u32 b, const u32 c)
103 {
104 u32 r;
105
106 asm ("lop3.b32 %0, %1, %2, %3, 0x2d;" : "=r" (r) : "r" (a), "r" (b), "r" (c));
107
108 return r;
109 }
110
111 static inline u32 lut3_39 (const u32 a, const u32 b, const u32 c)
112 {
113 u32 r;
114
115 asm ("lop3.b32 %0, %1, %2, %3, 0x39;" : "=r" (r) : "r" (a), "r" (b), "r" (c));
116
117 return r;
118 }
119
120 static inline u32 lut3_59 (const u32 a, const u32 b, const u32 c)
121 {
122 u32 r;
123
124 asm ("lop3.b32 %0, %1, %2, %3, 0x59;" : "=r" (r) : "r" (a), "r" (b), "r" (c));
125
126 return r;
127 }
128
129 static inline u32 lut3_96 (const u32 a, const u32 b, const u32 c)
130 {
131 u32 r;
132
133 asm ("lop3.b32 %0, %1, %2, %3, 0x96;" : "=r" (r) : "r" (a), "r" (b), "r" (c));
134
135 return r;
136 }
137
138 static inline u32 lut3_e4 (const u32 a, const u32 b, const u32 c)
139 {
140 u32 r;
141
142 asm ("lop3.b32 %0, %1, %2, %3, 0xe4;" : "=r" (r) : "r" (a), "r" (b), "r" (c));
143
144 return r;
145 }
146
147 static inline u32 lut3_e8 (const u32 a, const u32 b, const u32 c)
148 {
149 u32 r;
150
151 asm ("lop3.b32 %0, %1, %2, %3, 0xe8;" : "=r" (r) : "r" (a), "r" (b), "r" (c));
152
153 return r;
154 }
155
156 static inline u32 lut3_ca (const u32 a, const u32 b, const u32 c)
157 {
158 u32 r;
159
160 asm ("lop3.b32 %0, %1, %2, %3, 0xca;" : "=r" (r) : "r" (a), "r" (b), "r" (c));
161
162 return r;
163 }
164
165 #endif
166
167 #define allx(r) r
168
169 /*
170 static u32 allx (const u32 r)
171 {
172 return r;
173 }
174 */
175
176 static inline u32 l32_from_64 (u64 a)
177 {
178 const u32 r = (uint) (a);
179
180 return r;
181 }
182
183 static inline u32 h32_from_64 (u64 a)
184 {
185 a >>= 32;
186
187 const u32 r = (uint) (a);
188
189 return r;
190 }
191
192 static inline u64 hl32_to_64 (const u32 a, const u32 b)
193 {
194 return as_ulong ((uint2) (b, a));
195 }
196
197 #ifdef IS_AMD
198
199 static inline u32 rotr32 (const u32 a, const u32 n)
200 {
201 return rotate (a, 32 - n);
202 }
203
204 static inline u32 rotl32 (const u32 a, const u32 n)
205 {
206 return rotate (a, n);
207 }
208
209 static inline u64 rotr64 (const u64 a, const u32 n)
210 {
211 uint2 a2 = as_uint2 (a);
212
213 uint2 t;
214
215 t.s0 = (n >= 32) ? amd_bitalign (a2.s0, a2.s1, n - 32)
216 : amd_bitalign (a2.s1, a2.s0, n);
217 t.s1 = (n >= 32) ? amd_bitalign (a2.s1, a2.s0, n - 32)
218 : amd_bitalign (a2.s0, a2.s1, n);
219
220 return as_ulong (t);
221 }
222
223 static inline u64 rotl64 (const u64 a, const u32 n)
224 {
225 return rotr64 (a, 64 - n);
226 }
227
228 #endif
229
230 #ifdef IS_NV
231
232 #if CUDA_ARCH >= 350
233
234 static inline u32 rotr32 (const u32 a, const u32 n)
235 {
236 u32 r;
237
238 asm ("shf.r.wrap.b32 %0, %1, %2, %3;" : "=r"(r) : "r"(a), "r"(a), "r"(n));
239
240 return r;
241 }
242
243 static inline u32 rotl32 (const u32 a, const u32 n)
244 {
245 return rotr32 (a, 32 - n);
246 }
247
248 static inline u64 rotr64 (const u64 a, const u32 n)
249 {
250 u32 il;
251 u32 ir;
252
253 asm ("mov.b64 {%0, %1}, %2;" : "=r"(il), "=r"(ir) : "l"(a));
254
255 u32 tl;
256 u32 tr;
257
258 if (n >= 32)
259 {
260 asm ("shf.r.wrap.b32 %0, %1, %2, %3;" : "=r"(tl) : "r"(ir), "r"(il), "r"(n - 32));
261 asm ("shf.r.wrap.b32 %0, %1, %2, %3;" : "=r"(tr) : "r"(il), "r"(ir), "r"(n - 32));
262 }
263 else
264 {
265 asm ("shf.r.wrap.b32 %0, %1, %2, %3;" : "=r"(tl) : "r"(il), "r"(ir), "r"(n));
266 asm ("shf.r.wrap.b32 %0, %1, %2, %3;" : "=r"(tr) : "r"(ir), "r"(il), "r"(n));
267 }
268
269 u64 r;
270
271 asm ("mov.b64 %0, {%1, %2};" : "=l"(r) : "r"(tl), "r"(tr));
272
273 return r;
274 }
275
276 static inline u64 rotl64 (const u64 a, const u32 n)
277 {
278 return rotr64 (a, 64 - n);
279 }
280
281 #else
282
283 static inline u32 rotr32 (const u32 a, const u32 n)
284 {
285 return rotate (a, 32 - n);
286 }
287
288 static inline u32 rotl32 (const u32 a, const u32 n)
289 {
290 return rotate (a, n);
291 }
292
293 static inline u64 rotr64 (const u64 a, const u64 n)
294 {
295 return rotate (a, 64 - n);
296 }
297
298 static inline u64 rotl64 (const u64 a, const u64 n)
299 {
300 return rotate (a, n);
301 }
302
303 #endif
304 #endif
305
306 typedef struct
307 {
308 #if defined _DES_
309 u32 digest_buf[4];
310 #elif defined _MD4_
311 u32 digest_buf[4];
312 #elif defined _MD5_
313 u32 digest_buf[4];
314 #elif defined _MD5H_
315 u32 digest_buf[4];
316 #elif defined _SHA1_
317 u32 digest_buf[5];
318 #elif defined _BCRYPT_
319 u32 digest_buf[6];
320 #elif defined _SHA256_
321 u32 digest_buf[8];
322 #elif defined _SHA384_
323 u32 digest_buf[16];
324 #elif defined _SHA512_
325 u32 digest_buf[16];
326 #elif defined _KECCAK_
327 u32 digest_buf[50];
328 #elif defined _RIPEMD160_
329 u32 digest_buf[5];
330 #elif defined _WHIRLPOOL_
331 u32 digest_buf[16];
332 #elif defined _GOST_
333 u32 digest_buf[8];
334 #elif defined _GOST2012_256_
335 u32 digest_buf[8];
336 #elif defined _GOST2012_512_
337 u32 digest_buf[16];
338 #elif defined _SAPB_
339 u32 digest_buf[4];
340 #elif defined _SAPG_
341 u32 digest_buf[5];
342 #elif defined _MYSQL323_
343 u32 digest_buf[4];
344 #elif defined _LOTUS5_
345 u32 digest_buf[4];
346 #elif defined _LOTUS6_
347 u32 digest_buf[4];
348 #elif defined _SCRYPT_
349 u32 digest_buf[8];
350 #elif defined _LOTUS8_
351 u32 digest_buf[4];
352 #elif defined _OFFICE2007_
353 u32 digest_buf[4];
354 #elif defined _OFFICE2010_
355 u32 digest_buf[4];
356 #elif defined _OFFICE2013_
357 u32 digest_buf[4];
358 #elif defined _OLDOFFICE01_
359 u32 digest_buf[4];
360 #elif defined _OLDOFFICE34_
361 u32 digest_buf[4];
362 #elif defined _SIPHASH_
363 u32 digest_buf[4];
364 #elif defined _PBKDF2_MD5_
365 u32 digest_buf[32];
366 #elif defined _PBKDF2_SHA1_
367 u32 digest_buf[32];
368 #elif defined _PBKDF2_SHA256_
369 u32 digest_buf[32];
370 #elif defined _PBKDF2_SHA512_
371 u32 digest_buf[32];
372 #elif defined _PDF17L8_
373 u32 digest_buf[8];
374 #elif defined _CRC32_
375 u32 digest_buf[4];
376 #elif defined _SEVEN_ZIP_
377 u32 digest_buf[4];
378 #elif defined _ANDROIDFDE_
379 u32 digest_buf[4];
380 #elif defined _DCC2_
381 u32 digest_buf[4];
382 #elif defined _WPA_
383 u32 digest_buf[4];
384 #elif defined _MD5_SHA1_
385 u32 digest_buf[4];
386 #elif defined _SHA1_MD5_
387 u32 digest_buf[5];
388 #elif defined _NETNTLMV2_
389 u32 digest_buf[4];
390 #elif defined _KRB5PA_
391 u32 digest_buf[4];
392 #elif defined _CLOUDKEY_
393 u32 digest_buf[8];
394 #elif defined _SCRYPT_
395 u32 digest_buf[4];
396 #elif defined _PSAFE2_
397 u32 digest_buf[5];
398 #elif defined _LOTUS8_
399 u32 digest_buf[4];
400 #elif defined _RAR3_
401 u32 digest_buf[4];
402 #elif defined _SHA256_SHA1_
403 u32 digest_buf[8];
404 #elif defined _MS_DRSR_
405 u32 digest_buf[8];
406 #endif
407
408 } digest_t;
409
410 typedef struct
411 {
412 u32 salt_buf[16];
413 u32 salt_buf_pc[8];
414
415 u32 salt_len;
416 u32 salt_iter;
417 u32 salt_sign[2];
418
419 u32 keccak_mdlen;
420 u32 truecrypt_mdlen;
421
422 u32 digests_cnt;
423 u32 digests_done;
424
425 u32 digests_offset;
426
427 u32 scrypt_N;
428 u32 scrypt_r;
429 u32 scrypt_p;
430 u32 scrypt_tmto;
431 u32 scrypt_phy;
432
433 } salt_t;
434
435 typedef struct
436 {
437 int V;
438 int R;
439 int P;
440
441 int enc_md;
442
443 u32 id_buf[8];
444 u32 u_buf[32];
445 u32 o_buf[32];
446
447 int id_len;
448 int o_len;
449 int u_len;
450
451 u32 rc4key[2];
452 u32 rc4data[2];
453
454 } pdf_t;
455
456 typedef struct
457 {
458 u32 pke[25];
459 u32 eapol[64];
460 int eapol_size;
461 int keyver;
462
463 } wpa_t;
464
465 typedef struct
466 {
467 u32 cry_master_buf[64];
468 u32 ckey_buf[64];
469 u32 public_key_buf[64];
470
471 u32 cry_master_len;
472 u32 ckey_len;
473 u32 public_key_len;
474
475 } bitcoin_wallet_t;
476
477 typedef struct
478 {
479 u32 salt_buf[30];
480 u32 salt_len;
481
482 u32 esalt_buf[38];
483 u32 esalt_len;
484
485 } sip_t;
486
487 typedef struct
488 {
489 u32 data[384];
490
491 } androidfde_t;
492
493 typedef struct
494 {
495 u32 nr_buf[16];
496 u32 nr_len;
497
498 u32 msg_buf[128];
499 u32 msg_len;
500
501 } ikepsk_t;
502
503 typedef struct
504 {
505 u32 user_len;
506 u32 domain_len;
507 u32 srvchall_len;
508 u32 clichall_len;
509
510 u32 userdomain_buf[64];
511 u32 chall_buf[256];
512
513 } netntlm_t;
514
515 typedef struct
516 {
517 u32 user[16];
518 u32 realm[16];
519 u32 salt[32];
520 u32 timestamp[16];
521 u32 checksum[4];
522
523 } krb5pa_t;
524
525 typedef struct
526 {
527 u32 salt_buf[16];
528 u32 data_buf[112];
529 u32 keyfile_buf[16];
530
531 } tc_t;
532
533 typedef struct
534 {
535 u32 salt_buf[16];
536
537 } pbkdf2_md5_t;
538
539 typedef struct
540 {
541 u32 salt_buf[16];
542
543 } pbkdf2_sha1_t;
544
545 typedef struct
546 {
547 u32 salt_buf[16];
548
549 } pbkdf2_sha256_t;
550
551 typedef struct
552 {
553 u32 salt_buf[32];
554
555 } pbkdf2_sha512_t;
556
557 typedef struct
558 {
559 u32 salt_buf[128];
560 u32 salt_len;
561
562 } rakp_t;
563
564 typedef struct
565 {
566 u32 data_len;
567 u32 data_buf[512];
568
569 } cloudkey_t;
570
571 typedef struct
572 {
573 u32 encryptedVerifier[4];
574 u32 encryptedVerifierHash[5];
575
576 u32 keySize;
577
578 } office2007_t;
579
580 typedef struct
581 {
582 u32 encryptedVerifier[4];
583 u32 encryptedVerifierHash[8];
584
585 } office2010_t;
586
587 typedef struct
588 {
589 u32 encryptedVerifier[4];
590 u32 encryptedVerifierHash[8];
591
592 } office2013_t;
593
594 typedef struct
595 {
596 u32 version;
597 u32 encryptedVerifier[4];
598 u32 encryptedVerifierHash[4];
599 u32 rc4key[2];
600
601 } oldoffice01_t;
602
603 typedef struct
604 {
605 u32 version;
606 u32 encryptedVerifier[4];
607 u32 encryptedVerifierHash[5];
608 u32 rc4key[2];
609
610 } oldoffice34_t;
611
612 typedef struct
613 {
614 u32 digest[4];
615 u32 out[4];
616
617 } pdf14_tmp_t;
618
619 typedef struct
620 {
621 union
622 {
623 u32 dgst32[16];
624 u64 dgst64[8];
625 };
626
627 u32 dgst_len;
628 u32 W_len;
629
630 } pdf17l8_tmp_t;
631
632 typedef struct
633 {
634 u32 digest_buf[4];
635
636 } phpass_tmp_t;
637
638 typedef struct
639 {
640 u32 digest_buf[4];
641
642 } md5crypt_tmp_t;
643
644 typedef struct
645 {
646 u32 alt_result[8];
647
648 u32 p_bytes[4];
649 u32 s_bytes[4];
650
651 } sha256crypt_tmp_t;
652
653 typedef struct
654 {
655 u64 l_alt_result[8];
656
657 u64 l_p_bytes[2];
658 u64 l_s_bytes[2];
659
660 } sha512crypt_tmp_t;
661
662 typedef struct
663 {
664 u32 ipad[5];
665 u32 opad[5];
666
667 u32 dgst[10];
668 u32 out[10];
669
670 } wpa_tmp_t;
671
672 typedef struct
673 {
674 u64 dgst[8];
675
676 } bitcoin_wallet_tmp_t;
677
678 typedef struct
679 {
680 u32 ipad[5];
681 u32 opad[5];
682
683 u32 dgst[5];
684 u32 out[4];
685
686 } dcc2_tmp_t;
687
688 typedef struct
689 {
690 u32 E[18];
691
692 u32 P[18];
693
694 u32 S0[256];
695 u32 S1[256];
696 u32 S2[256];
697 u32 S3[256];
698
699 } bcrypt_tmp_t;
700
701 typedef struct
702 {
703 u32 digest[2];
704
705 u32 P[18];
706
707 u32 S0[256];
708 u32 S1[256];
709 u32 S2[256];
710 u32 S3[256];
711
712 } pwsafe2_tmp_t;
713
714 typedef struct
715 {
716 u32 digest_buf[8];
717
718 } pwsafe3_tmp_t;
719
720 typedef struct
721 {
722 u32 digest_buf[5];
723
724 } androidpin_tmp_t;
725
726 typedef struct
727 {
728 u32 ipad[5];
729 u32 opad[5];
730
731 u32 dgst[10];
732 u32 out[10];
733
734 } androidfde_tmp_t;
735
736 typedef struct
737 {
738 u32 ipad[16];
739 u32 opad[16];
740
741 u32 dgst[64];
742 u32 out[64];
743
744 } tc_tmp_t;
745
746 typedef struct
747 {
748 u64 ipad[8];
749 u64 opad[8];
750
751 u64 dgst[32];
752 u64 out[32];
753
754 } tc64_tmp_t;
755
756 typedef struct
757 {
758 u32 ipad[4];
759 u32 opad[4];
760
761 u32 dgst[32];
762 u32 out[32];
763
764 } pbkdf2_md5_tmp_t;
765
766 typedef struct
767 {
768 u32 ipad[5];
769 u32 opad[5];
770
771 u32 dgst[32];
772 u32 out[32];
773
774 } pbkdf2_sha1_tmp_t;
775
776 typedef struct
777 {
778 u32 ipad[8];
779 u32 opad[8];
780
781 u32 dgst[32];
782 u32 out[32];
783
784 } pbkdf2_sha256_tmp_t;
785
786 typedef struct
787 {
788 u64 ipad[8];
789 u64 opad[8];
790
791 u64 dgst[16];
792 u64 out[16];
793
794 } pbkdf2_sha512_tmp_t;
795
796 typedef struct
797 {
798 u64 out[8];
799
800 } ecryptfs_tmp_t;
801
802 typedef struct
803 {
804 u64 ipad[8];
805 u64 opad[8];
806
807 u64 dgst[16];
808 u64 out[16];
809
810 } oraclet_tmp_t;
811
812 typedef struct
813 {
814 u32 ipad[5];
815 u32 opad[5];
816
817 u32 dgst[5];
818 u32 out[5];
819
820 } agilekey_tmp_t;
821
822 typedef struct
823 {
824 u32 ipad[5];
825 u32 opad[5];
826
827 u32 dgst1[5];
828 u32 out1[5];
829
830 u32 dgst2[5];
831 u32 out2[5];
832
833 } mywallet_tmp_t;
834
835 typedef struct
836 {
837 u32 ipad[5];
838 u32 opad[5];
839
840 u32 dgst[5];
841 u32 out[5];
842
843 } sha1aix_tmp_t;
844
845 typedef struct
846 {
847 u32 ipad[8];
848 u32 opad[8];
849
850 u32 dgst[8];
851 u32 out[8];
852
853 } sha256aix_tmp_t;
854
855 typedef struct
856 {
857 u64 ipad[8];
858 u64 opad[8];
859
860 u64 dgst[8];
861 u64 out[8];
862
863 } sha512aix_tmp_t;
864
865 typedef struct
866 {
867 u32 ipad[8];
868 u32 opad[8];
869
870 u32 dgst[8];
871 u32 out[8];
872
873 } lastpass_tmp_t;
874
875 typedef struct
876 {
877 u64 digest_buf[8];
878
879 } drupal7_tmp_t;
880
881 typedef struct
882 {
883 u32 ipad[5];
884 u32 opad[5];
885
886 u32 dgst[5];
887 u32 out[5];
888
889 } lotus8_tmp_t;
890
891 typedef struct
892 {
893 u32 out[5];
894
895 } office2007_tmp_t;
896
897 typedef struct
898 {
899 u32 out[5];
900
901 } office2010_tmp_t;
902
903 typedef struct
904 {
905 u64 out[8];
906
907 } office2013_tmp_t;
908
909 typedef struct
910 {
911 u32 digest_buf[5];
912
913 } saph_sha1_tmp_t;
914
915 typedef struct
916 {
917 u32 block[16];
918
919 u32 dgst[8];
920
921 u32 block_len;
922 u32 final_len;
923
924 } seven_zip_tmp_t;
925
926 typedef struct
927 {
928 u32 Kc[16];
929 u32 Kd[16];
930
931 u32 iv[2];
932
933 } bsdicrypt_tmp_t;
934
935 typedef struct
936 {
937 u32 dgst[17][5];
938
939 } rar3_tmp_t;
940
941 typedef struct
942 {
943 u32 user[16];
944
945 } cram_md5_t;
946
947 typedef struct
948 {
949 u32 iv_buf[4];
950 u32 iv_len;
951
952 u32 salt_buf[4];
953 u32 salt_len;
954
955 u32 crc;
956
957 u32 data_buf[96];
958 u32 data_len;
959
960 u32 unpack_size;
961
962 } seven_zip_t;
963
964 typedef struct
965 {
966 u32 key;
967 u64 val;
968
969 } hcstat_table_t;
970
971 typedef struct
972 {
973 u32 cs_buf[0x100];
974 u32 cs_len;
975
976 } cs_t;
977
978 typedef struct
979 {
980 u32 cmds[15];
981
982 } gpu_rule_t;
983
984 /*
985 typedef struct
986 {
987 u32 plain_buf[16];
988 u32 plailen;
989
990 } plain_t;
991 */
992
993 typedef struct
994 {
995 u32 gidvid;
996 u32 il_pos;
997
998 } plain_t;
999
1000 typedef struct
1001 {
1002 u32 i[64];
1003
1004 u32 pw_len;
1005
1006 u32 alignment_placeholder_1;
1007 u32 alignment_placeholder_2;
1008 u32 alignment_placeholder_3;
1009
1010 } pw_t;
1011
1012 typedef struct
1013 {
1014 u32 i;
1015
1016 } bf_t;
1017
1018 typedef struct
1019 {
1020 u32 i[8];
1021
1022 u32 pw_len;
1023
1024 } comb_t;
1025
1026 typedef struct
1027 {
1028 u32 b[32];
1029
1030 } bs_word_t;
1031
1032 typedef struct
1033 {
1034 uint4 P[64];
1035
1036 } scrypt_tmp_t;