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