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