-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathops_SIMD.cpp
More file actions
executable file
·1513 lines (1265 loc) · 33 KB
/
ops_SIMD.cpp
File metadata and controls
executable file
·1513 lines (1265 loc) · 33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This software may be modified, redistributed, and used for any purpose,
// so long as its origin is acknowledged.
//#define DEMO
//#define MAX_THREADS 4
// -- Dennis Yurichev <dennis@conus.info>
#define VERSION "0.3"
#ifdef _WIN32
#include <windows.h>
#elif linux
#include <pthread.h>
#include <unistd.h>
#include <strings.h>
#define _snprintf snprintf
#define _stricmp strcasecmp
#define _strnicmp strncasecmp
#define _strdup strdup
#else
#error "something wrong"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <memory.h>
#include <time.h>
#include <assert.h>
#include <signal.h>
//#include <emmintrin.h>
#include <immintrin.h>
#include <dvec.h>
#ifdef _DEBUG
#include <crtdbg.h>
#endif
#include <list>
#include <string>
#ifdef USE_AVX
typedef __m256 SIMD;
#ifdef _WIN32
#define EXE_NAME "ops_avx.exe"
#endif
#ifdef linux
#define EXE_NAME "ops_avx"
#endif
#endif
#ifdef USE_SSE2
typedef __m128i SIMD;
#ifdef _WIN32
#define EXE_NAME "ops_sse2.exe"
#endif
#ifdef linux
#define EXE_NAME "ops_sse2"
#endif
#endif
#define BITS_IN_SIMD (sizeof(SIMD)*8)
typedef unsigned int uint;
typedef unsigned short ushort;
typedef unsigned char uchar;
char * def_first_symbol_charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char * def_charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789#$_";
#ifdef USE_SSE2
#include "deseval_SSE.h"
#endif
#ifdef USE_AVX
#include "deseval_AVX.h"
#endif
#define ORA_BUF_SIZE 80
#ifdef _WIN32
CRITICAL_SECTION cs;
#endif
#ifdef linux
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
#endif
bool global_stop=false;
void tlock()
{
#ifdef _WIN32
EnterCriticalSection(&cs);
#endif
#ifdef linux
assert (pthread_mutex_lock( &mutex1 )==0);
#endif
};
void tunlock()
{
#ifdef _WIN32
LeaveCriticalSection(&cs);
#endif
#ifdef linux
pthread_mutex_unlock( &mutex1 );
#endif
};
static time_t start;
struct hash
{
int hash[64];
char *uname;
char *comment;
bool processed;
bool current;
bool solved;
char *password;
struct hash *next;
};
struct t
{
SIMD DES_CBC_cache[10][64];
int DES_last_block_cached;
#ifndef DEMO
char *uname;
int uname_len;
#endif
int pass_len; // password len without first char!
int lowest_block_modified;
uchar pass_first_char;
__int64 encrypted;
struct hash* hashes;
char *charset;
int charset_len;
bool stop;
// arrays, len=pass_len
uchar *begin_at_password;
uchar *last_password_crunched;
};
struct t2info
{
__int64 passwords_total;
int threads_total;
int first_char_total;
struct hash* hashes;
};
char * seconds_to_readable (int s)
{
int seconds;
int minutes;
int hours;
int days;
char buf[16];
char *outbuf;
outbuf=(char*)malloc (128);
outbuf[0]=0;
seconds=s;
minutes=hours=days=0;
while (seconds>=60)
{
seconds-=60;
minutes++;
};
while (minutes>=60)
{
minutes-=60;
hours++;
};
while (hours>=24)
{
hours-=24;
days++;
};
*outbuf=0;
if (days)
{
_snprintf (buf, 16, "%dd", days);
strcpy (outbuf+strlen (outbuf), buf);
};
if (hours)
{
_snprintf (buf, 16, "%dh", hours);
strcpy (outbuf+strlen (outbuf), buf);
};
if (minutes)
{
_snprintf (buf, 16, "%dm", minutes);
strcpy (outbuf+strlen (outbuf), buf);
};
if (seconds)
{
_snprintf (buf, 16, "%ds", seconds);
strcpy (outbuf+strlen (outbuf), buf);
}
if (strlen (buf)==0)
strcpy (outbuf, "?");
return outbuf;
};
#ifdef USE_AVX
__m256 __inline get_mask256()
{
return _mm256_castsi256_ps(_mm256_set1_epi8(0xFF));
};
#endif
int get_bit_in_SIMD (SIMD n, int idx)
{
assert (idx<BITS_IN_SIMD);
assert (idx!=-1);
uchar tmp=*(((uchar*)&n)+(idx>>3));
return (tmp>>(idx&7))&1;
};
uchar get_byte_in_block_rv (SIMD *p, int j, int idx) // p is array [10][64]
{
int j_l=j&7;
int j_h=(j-j_l)/8;
uchar rt=0;
assert (j<ORA_BUF_SIZE);
assert (idx<BITS_IN_SIMD);
for (int i=0; i<8; i++) // bits in each byte of b
{
SIMD *p2=&p[i + (7-j_l)*8 + j_h*64];
if (get_bit_in_SIMD (*p2, idx)==1)
rt|=1<<i;
};
return rt;
};
#ifdef USE_AVX
#define deseval_SIMD deseval_AVX
#define SIMD_xor _mm256_xor_ps
#endif
#ifdef USE_SSE2
#define deseval_SIMD deseval_SSE
#define SIMD_xor _mm_xor_si128
#endif
void DES_CBC (struct t *th, SIMD blks[10][64], int blks_t, SIMD k[56], SIMD *lastblock, bool cache, int lowest_blk_last_modified)
{
int beg;
if (cache && th->DES_last_block_cached != -1 && th->DES_last_block_cached >= lowest_blk_last_modified)
{
beg=lowest_blk_last_modified;
}
else
beg=0;
for (int b=beg; b<blks_t; b++)
{
if (b==0)
{
if (cache)
{
deseval_SIMD (&blks[b][0], &th->DES_CBC_cache[0][0], k); // encrypt 256 blocks per once
th->DES_last_block_cached=0;
}
else
{
deseval_SIMD (&blks[b][0], lastblock, k); // encrypt 256 blocks per once
};
}
else
{
SIMD tmp_p[64];
for (int i=0; i<64; i++)
{
if (cache)
tmp_p[i]=SIMD_xor (blks[b][i], th->DES_CBC_cache[b-1][i]);
else
tmp_p[i]=SIMD_xor (blks[b][i], lastblock[i]);
};
if (cache)
{
deseval_SIMD (tmp_p, &th->DES_CBC_cache[b][0], k); // encrypt 256 blocks per once
th->DES_last_block_cached=b;
}
else
deseval_SIMD (tmp_p, lastblock, k); // encrypt 256 blocks per once
};
if ((b+1 == blks_t) && cache==true)
memcpy (lastblock, &th->DES_CBC_cache[b][0], 64*sizeof(SIMD));
};
};
#ifdef USE_SSE2
#include "key0..F.h"
#endif
#ifdef USE_AVX
__m256 key0123456789ABCDEF_AVX[56];
void key0123456789ABCDEF_AVX_init()
{
key0123456789ABCDEF_AVX[0]=get_mask256();
key0123456789ABCDEF_AVX[1]=get_mask256();
key0123456789ABCDEF_AVX[2]=get_mask256();
key0123456789ABCDEF_AVX[3]=_mm256_setzero_ps();
key0123456789ABCDEF_AVX[4]=get_mask256();
key0123456789ABCDEF_AVX[5]=get_mask256();
key0123456789ABCDEF_AVX[6]=get_mask256();
key0123456789ABCDEF_AVX[7]=_mm256_setzero_ps();
key0123456789ABCDEF_AVX[8]=get_mask256();
key0123456789ABCDEF_AVX[9]=get_mask256();
key0123456789ABCDEF_AVX[10]=_mm256_setzero_ps();
key0123456789ABCDEF_AVX[11]=_mm256_setzero_ps();
key0123456789ABCDEF_AVX[12]=get_mask256();
key0123456789ABCDEF_AVX[13]=get_mask256();
key0123456789ABCDEF_AVX[14]=get_mask256();
key0123456789ABCDEF_AVX[15]=_mm256_setzero_ps();
key0123456789ABCDEF_AVX[16]=get_mask256();
key0123456789ABCDEF_AVX[17]=_mm256_setzero_ps();
key0123456789ABCDEF_AVX[18]=get_mask256();
key0123456789ABCDEF_AVX[19]=_mm256_setzero_ps();
key0123456789ABCDEF_AVX[20]=get_mask256();
key0123456789ABCDEF_AVX[21]=_mm256_setzero_ps();
key0123456789ABCDEF_AVX[22]=_mm256_setzero_ps();
key0123456789ABCDEF_AVX[23]=get_mask256();
key0123456789ABCDEF_AVX[24]=_mm256_setzero_ps();
key0123456789ABCDEF_AVX[25]=_mm256_setzero_ps();
key0123456789ABCDEF_AVX[26]=_mm256_setzero_ps();
key0123456789ABCDEF_AVX[27]=get_mask256();
key0123456789ABCDEF_AVX[28]=get_mask256();
key0123456789ABCDEF_AVX[29]=get_mask256();
key0123456789ABCDEF_AVX[30]=_mm256_setzero_ps();
key0123456789ABCDEF_AVX[31]=_mm256_setzero_ps();
key0123456789ABCDEF_AVX[32]=get_mask256();
key0123456789ABCDEF_AVX[33]=get_mask256();
key0123456789ABCDEF_AVX[34]=_mm256_setzero_ps();
key0123456789ABCDEF_AVX[35]=_mm256_setzero_ps();
key0123456789ABCDEF_AVX[36]=get_mask256();
key0123456789ABCDEF_AVX[37]=_mm256_setzero_ps();
key0123456789ABCDEF_AVX[38]=_mm256_setzero_ps();
key0123456789ABCDEF_AVX[39]=_mm256_setzero_ps();
key0123456789ABCDEF_AVX[40]=get_mask256();
key0123456789ABCDEF_AVX[41]=_mm256_setzero_ps();
key0123456789ABCDEF_AVX[42]=get_mask256();
key0123456789ABCDEF_AVX[43]=_mm256_setzero_ps();
key0123456789ABCDEF_AVX[44]=_mm256_setzero_ps();
key0123456789ABCDEF_AVX[45]=_mm256_setzero_ps();
key0123456789ABCDEF_AVX[46]=get_mask256();
key0123456789ABCDEF_AVX[47]=_mm256_setzero_ps();
key0123456789ABCDEF_AVX[48]=_mm256_setzero_ps();
key0123456789ABCDEF_AVX[49]=_mm256_setzero_ps();
key0123456789ABCDEF_AVX[50]=_mm256_setzero_ps();
key0123456789ABCDEF_AVX[51]=_mm256_setzero_ps();
key0123456789ABCDEF_AVX[52]=_mm256_setzero_ps();
key0123456789ABCDEF_AVX[53]=_mm256_setzero_ps();
key0123456789ABCDEF_AVX[54]=_mm256_setzero_ps();
key0123456789ABCDEF_AVX[55]=_mm256_setzero_ps();
};
#endif
bool search_for_hash (uchar *c, struct hash* hashes, struct hash* & found_hash, int *found_idx)
{
int SIMD_val_len=sizeof(SIMD);
for (int idx_h=0; idx_h<SIMD_val_len; idx_h++)
for (int idx_l=0; idx_l<8; idx_l++)
{
struct hash* i=hashes;
while (i!=NULL)
{
if (i->current==true && i->solved==false)
{
bool eq=true;
for (int b=0; (b < 64) && (eq==true); b++)
{
uchar* ptr=(uchar*)&c[b*SIMD_val_len];
if (((ptr[idx_h]>>idx_l)&1) != i->hash[b])
eq=false;
};
if (eq==true)
{
*found_idx=(idx_h*8 + idx_l);
found_hash=i;
return true;
};
};
i=i->next;
};
};
return false;
};
bool is_there_still_unsolved_hashes_among_current (struct hash* hashes)
{
struct hash* i=hashes;
assert (hashes!=NULL);
while (i!=NULL)
{
if (i->current==true)
if (i->solved==false)
return true;
i=i->next;
};
return false;
};
bool __inline calc_next_password(uchar *pw, int pass_len, int charset_len, int & lowest_pos)
{
for (int pos=pass_len-1; pos>=0; pos--)
{
pw[pos]++;
if (pw[pos]==charset_len)
pw[pos]=0;
else
{
if (lowest_pos>pos) lowest_pos=pos;
return true;
};
};
return false;
};
// megabuf: 256 bytes of first byte, 256 bytes of 2nd byte ... 256 bytes of 80th byte
void __inline update_high_and_low_watermarks (int & megabuf_changes_low, int & megabuf_changes_high, int megabuf_ch_idx)
{
if (megabuf_changes_low==-1) megabuf_changes_low=megabuf_ch_idx;
if (megabuf_changes_high==-1) megabuf_changes_high=megabuf_ch_idx;
if (megabuf_ch_idx>megabuf_changes_high) megabuf_changes_high=megabuf_ch_idx;
if (megabuf_ch_idx<megabuf_changes_low) megabuf_changes_low=megabuf_ch_idx;
};
int prepare_next_passwords_to_megabuf(struct t *th, int N, uchar *megabuf,
bool & megabuf_contain_uname_and_first_char_of_password,
int & megabuf_changes_low, int & megabuf_changes_high, int & lowest_blk_last_modified)
{
int megabuf_idx, megabuf_ch_idx;
int lowest_pos=th->pass_len-1;
int prev_lowest_pos=0;
if (megabuf_contain_uname_and_first_char_of_password==false)
{
for (int i=0; i<N; i++)
{
#ifdef DEMO
for (int j=0; j<3; j++)
#else
for (int j=0; j<th->uname_len; j++)
#endif
{
megabuf_ch_idx=(j*2 + 1);
assert (megabuf_ch_idx<ORA_BUF_SIZE);
megabuf_idx=megabuf_ch_idx*N + i;
#ifdef DEMO
megabuf[megabuf_idx]="SYS"[j];
#else
megabuf[megabuf_idx]=th->uname[j];
#endif
update_high_and_low_watermarks (megabuf_changes_low, megabuf_changes_high, megabuf_ch_idx);
};
megabuf_ch_idx=(
#ifdef DEMO
3
#else
th->uname_len
#endif
*2 + 1);
assert (megabuf_ch_idx<ORA_BUF_SIZE);
megabuf_idx=megabuf_ch_idx*N + i;
megabuf[megabuf_idx]=th->pass_first_char;
update_high_and_low_watermarks (megabuf_changes_low, megabuf_changes_high, megabuf_ch_idx);
};
megabuf_contain_uname_and_first_char_of_password=true;
lowest_blk_last_modified=0;
};
int megabuf_ch_idx_part1=
#ifdef DEMO
3
#else
th->uname_len
#endif
*2 + 1;
for (int i=0; i<N; i++)
{
megabuf_ch_idx=megabuf_ch_idx_part1 + (prev_lowest_pos+1)*2;
for (int j=prev_lowest_pos; j<th->pass_len; j++)
{
//megabuf_ch_idx=megabuf_ch_idx_part1 + (j+1)*2;
assert (megabuf_ch_idx<ORA_BUF_SIZE);
megabuf_idx=megabuf_ch_idx*N + i;
uchar *a1=&megabuf[megabuf_idx];
char *a2=&th->charset[th->last_password_crunched[j]];
if (*a1 != *a2)
{
*a1=*a2;
update_high_and_low_watermarks (megabuf_changes_low, megabuf_changes_high, megabuf_ch_idx);
int megabuf_ch_idx_8=megabuf_ch_idx/8;
if (lowest_blk_last_modified > megabuf_ch_idx_8) lowest_blk_last_modified = megabuf_ch_idx_8;
};
megabuf_ch_idx+=2;
};
// set next password
if (calc_next_password(th->last_password_crunched, th->pass_len, th->charset_len, lowest_pos)==false)
{
// no more passwords
return i;
};
};
prev_lowest_pos=lowest_pos;
lowest_pos=th->pass_len-1;
return N;
};
void make_N_oracle_hashes(struct t *th, SIMD blocks[10][64], int des_blocks, SIMD lastblock[64],
int pass_len, int lowest_blk_last_modified)
{
SIMD tmp_key[56];
if (pass_len==0)
DES_CBC (th, blocks, des_blocks,
#ifdef USE_AVX
key0123456789ABCDEF_AVX,
#endif
#ifdef USE_SSE2
key0123456789ABCDEF_SSE,
#endif
lastblock, false, lowest_blk_last_modified);
DES_CBC (th, blocks, des_blocks,
#ifdef USE_AVX
key0123456789ABCDEF_AVX,
#endif
#ifdef USE_SSE2
key0123456789ABCDEF_SSE,
#endif
lastblock, true, lowest_blk_last_modified);
for (int i=0; i<8; i++)
memcpy (&tmp_key[i*7], &lastblock[i*8+1], 7*sizeof (SIMD));
DES_CBC (th, blocks, des_blocks, tmp_key, lastblock, false, lowest_blk_last_modified);
};
// one DES block: [bit0:32 bytes][bit1:32 bytes][bit2:32 bytes]...[bit63:32 bytes]
void prepare_640_values (uchar *megabuf, uchar *SIMD_blocks, int megabuf_changes_low, int megabuf_changes_high)
{
for (int DES_blk_n=0; DES_blk_n<10; DES_blk_n++) // DES blk number
for (int DES_blk_pos=0; DES_blk_pos<64; DES_blk_pos++) // DES blk position
{
// seek pos in megabuf
// each DES block in megabuf takes 64 bit or 8 bytes
int megabuf_changes_byte_pos = DES_blk_n*8 + (DES_blk_pos>>3);
assert (megabuf_changes_byte_pos<ORA_BUF_SIZE);
int megabuf_byte_pos = megabuf_changes_byte_pos*BITS_IN_SIMD;
if (megabuf_changes_byte_pos<=megabuf_changes_high && megabuf_changes_byte_pos>=megabuf_changes_low)
{
int megabuf_bit_pos = DES_blk_pos&7;
int idx = DES_blk_n*64 + (63-DES_blk_pos);
ushort *w=(ushort*)(SIMD_blocks + idx*sizeof(SIMD));
for (int outbyte=0; outbyte<sizeof(SIMD)/2; outbyte++)
{
__m128i tt=*(__m128i *)&megabuf[megabuf_byte_pos];
if (megabuf_bit_pos)
for (int q=0; q<megabuf_bit_pos; q++)
tt=_mm_slli_epi16 (tt, 1);
*w=_mm_movemask_epi8 (tt);
megabuf_byte_pos+=16;
w++;
};
};
};
};
#ifdef _WIN32
#define THREAD_RESULT DWORD WINAPI
#define THREAD1_ARG struct t *th
#endif
#ifdef linux
#define THREAD_RESULT void*
#define THREAD1_ARG void* arg
#endif
THREAD_RESULT thread1 (THREAD1_ARG)
{
#ifdef linux
struct t *th=(struct t *)arg;
#endif
SIMD SIMD_blocks[10][64];
SIMD SIMD_lastblock[64];
memcpy (th->last_password_crunched, th->begin_at_password, th->pass_len);
#define MEGABUF_SIZE (ORA_BUF_SIZE*BITS_IN_SIMD)
uchar *megabuf;
#ifdef _WIN32
megabuf=(uchar*)_aligned_malloc (MEGABUF_SIZE, sizeof(SIMD));
#endif
#ifdef linux
posix_memalign ((void**)&megabuf, sizeof(SIMD), MEGABUF_SIZE);
#endif
memset (megabuf, 0, MEGABUF_SIZE);
assert (th->charset!=NULL);
bool megabuf_contain_uname_and_first_char_of_password=false;
th->DES_last_block_cached=-1;
while (1)
{
int lowest_blk_last_modified=9;
// ïîäãîòîâèòü 128/256 íîâûõ ïàðîëåé è ñäåëàòü èç íèõ N áóôåðîâ ïî 80 áàéò
// int prepare_next_passwords_to_megabuf(struct t *th, int N, uchar *megabuf, uchar *uname, int uname_len)
int megabuf_changes_low=-1;
int megabuf_changes_high=-1;
int passwords_generated=0;
passwords_generated=prepare_next_passwords_to_megabuf (th, BITS_IN_SIMD, megabuf, megabuf_contain_uname_and_first_char_of_password,
megabuf_changes_low, megabuf_changes_high, lowest_blk_last_modified);
//printf ("passwords_generated=%d\n", passwords_generated);
//printf ("passwords:\n");
//dump_list_of_strings (passwords);
// ñäåëàòü èç íèõ N áóôåðîâ ïî 80 áàéò
//fill_megabuf (megabuf, N, (uchar*)th->uname, th->uname_len, passwords);
// ãîòîâèì 640 m128 èëè m256
// __m128i blks[10][64]
prepare_640_values (megabuf, (uchar*)SIMD_blocks, megabuf_changes_low, megabuf_changes_high);
int bytes_to_encrypt=(
#ifdef DEMO
3
#else
th->uname_len
#endif
+ 1 + th->pass_len)*2;
int des_blocks=bytes_to_encrypt>>3;
if ((bytes_to_encrypt&7)!=0)
des_blocks++;
// ïðîâîðà÷èâàåì
make_N_oracle_hashes(th, SIMD_blocks, des_blocks, SIMD_lastblock, th->pass_len, lowest_blk_last_modified);
// èùåì
int found_idx;
struct hash *found_hash;
bool rr=false;
rr=search_for_hash ((uchar*)SIMD_lastblock, th->hashes, found_hash, &found_idx);
if (rr)
{
// store password
tlock();
found_hash->solved=true;
found_hash->password=(char*)malloc (th->pass_len+2);
found_hash->password[0]=th->pass_first_char;
found_hash->password[th->pass_len+1]=0;
for (int i=1; i<th->pass_len+2; i++)
{
found_hash->password[i]=megabuf[(1 + i*2+
#ifdef DEMO
3
#else
th->uname_len
#endif
*2)*BITS_IN_SIMD + found_idx];
};
tunlock();
};
th->encrypted+=passwords_generated+1;
//assert (th->progress<=1);
if (is_there_still_unsolved_hashes_among_current (th->hashes)==false || passwords_generated!=BITS_IN_SIMD || th->stop || global_stop)
{
#ifdef _WIN32
_aligned_free (megabuf);
return 0;
#endif
#ifdef linux
free (megabuf);
pthread_exit(NULL);
#endif
};
};
};
bool SSE2_supported()
{
int b[4];
__cpuid(b,1);
if (b[3] & (1<<26)) // EDX, bit 26
return true;
return false;
};
extern "C" int isAvxSupported();
bool t2enable;
struct t *th;
#ifdef _WIN32
HANDLE *THDL;
DWORD *TID;
#endif
#ifdef linux
pthread_t *threads;
#endif
#ifdef _WIN32
#define THREAD2_ARG struct t2info *t2
#endif
#ifdef linux
#define THREAD2_ARG void* arg
#endif
THREAD_RESULT thread2 (THREAD2_ARG)
{
#ifdef linux
struct t2info *t2=(struct t2info *)arg;
#endif
while (t2enable)
{
char *s;
int t;
int percents;
double sec_per_;
tlock();
__int64 encrypted_total=0;
for (int i=0; i<t2->threads_total; i++)
{
//printf ("th[%d].encrypted = %lld\n", i, th[i].encrypted);
//printf ("th[%d].pass_first_char=%c\n", i, th[i].pass_first_char);
encrypted_total = encrypted_total + th[i].encrypted;
};
tunlock();
//printf ("encrypted_total = %lld\n", encrypted_total);
//printf ("t2->passwords_total = %lld\n", t2->passwords_total);
//assert (encrypted_total <= (t2->passwords_total));
double q=(double)encrypted_total / (double)t2->passwords_total;
percents=(int)(q*100);
printf ("overall progress=%3d%%", percents);
t=time(NULL) - start; // time gone
if (percents)
{
sec_per_=t/q;
s=seconds_to_readable ( sec_per_ * (1-q) );
printf (" / time remaining: %s ", s);
free (s);
}
printf ("\r");
#ifdef _WIN32
Sleep (1000);
#endif
#ifdef linux
sleep (1);
#endif
};
printf ("\n");
#ifdef _WIN32
return 0;
#endif
#ifdef linux
pthread_exit(NULL);
#endif
};
void alloc_all(int threads_total)
{
#ifdef _WIN32
BOOL b=InitializeCriticalSectionAndSpinCount(&cs, 0x80000400);
assert (b==TRUE);
#endif
#ifdef _WIN32
THDL=(HANDLE*)malloc (threads_total*sizeof(HANDLE));
TID=(DWORD*)malloc (threads_total*sizeof(DWORD));
#endif
#ifdef linux
threads=(pthread_t*)malloc (threads_total*sizeof (pthread_t));
#endif
#ifdef _WIN32
th=(struct t*)_aligned_malloc (threads_total*sizeof(struct t), 0x20);
#endif
#ifdef linux
posix_memalign ((void**)&th, 0x10, threads_total*sizeof(struct t));
#endif
};
void free_all(int threads_total)
{
#ifdef _WIN32
_aligned_free (th);
free (TID);
free (THDL);
DeleteCriticalSection(&cs);
#endif
#ifdef linux
free (th);
free (threads);
#endif
};
#ifdef DEMO
void check(struct hash* hashes, int pass_len, char *first_symbol_charset, char *charset, int threads_total)
#else
void check(struct hash* hashes, char *uname, int pass_len, char *first_symbol_charset, char *charset, int threads_total)
#endif
{
time_t elapsed;
#ifdef _WIN32
HANDLE T2HDL;
DWORD T2ID;
#endif
#ifdef linux
int rc;
pthread_t T2;
#endif
struct t2info t2;
int charset_len=strlen (charset);
int first_symbol_charset_len=strlen (first_symbol_charset);
t2.passwords_total=1;
for (int i=0; i<pass_len-1; i++)
t2.passwords_total=t2.passwords_total * charset_len;
t2.passwords_total=t2.passwords_total * first_symbol_charset_len;
//printf ("t2.passwords_total = %I64d\n", t2.passwords_total);
if (global_stop) return;
{
struct hash* i=hashes;
int t=0;
while (i!=NULL)
{
if (i->current==true && i->solved==false)
t++;
i=i->next;
};
#ifdef DEMO
printf ("username=%s: %d unsolved hash(es) left\n", "SYS", t);
#else
printf ("username=%s: %d unsolved hash(es) left\n", uname, t);
#endif
};
assert (first_symbol_charset!=NULL);
assert (charset!=NULL);
alloc_all(threads_total);
start=time(NULL);
#ifndef DEMO
assert (uname!=NULL);
printf ("Checking %d-symbol passwords for username %s\n", pass_len, uname);
#else
printf ("Checking %d-symbol passwords for username %s\n", pass_len, "SYS");
#endif
// prepare N threads
for (int i=0; i<threads_total; i++)
{
#ifndef DEMO
th[i].uname=uname;
th[i].uname_len=strlen(uname);
#endif
th[i].pass_len=pass_len-1;
th[i].encrypted=0;
th[i].hashes=hashes;
th[i].charset=charset;
th[i].charset_len=strlen(charset);
th[i].stop=false;
th[i].begin_at_password=(uchar*)malloc (pass_len-1);
th[i].last_password_crunched=(uchar*)malloc (pass_len-1);
};
// start T2
t2enable=true;
t2.threads_total=threads_total;
t2.first_char_total=first_symbol_charset_len;
t2.hashes=hashes;
#ifdef _WIN32
T2HDL=CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)thread2, (PVOID)&t2, 0, &T2ID);
#endif
#ifdef linux
rc = pthread_create(&T2, NULL, thread2, (void *)&t2);
assert (rc==0);
#endif
for (int passchar=0; passchar<first_symbol_charset_len;)
{
// start N threads
for (int tt=0; (tt<threads_total) && (passchar<first_symbol_charset_len); tt++)
{
// modify first password char in each thread
th[tt].pass_first_char=first_symbol_charset[passchar];
memset (th[tt].begin_at_password, 0, pass_len-1);
// start thread
#ifdef _WIN32
THDL[tt]=CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)thread1, (PVOID)&th[tt], 0, &TID[tt]);
BOOL b=SetThreadPriority (THDL[tt], THREAD_PRIORITY_BELOW_NORMAL);
assert (b==TRUE);
#endif
#ifdef linux
rc = pthread_create(&threads[tt], NULL, thread1, (void *)&th[tt]);
assert (rc==0);
#endif
passchar++;
};
// wait for them
#ifdef _WIN32
WaitForMultipleObjects(threads_total, THDL, TRUE, INFINITE);
#endif
#ifdef linux
for (int i=0; i<threads_total; i++)
pthread_join(threads[i], NULL);
#endif
};
// stop T2