-
Notifications
You must be signed in to change notification settings - Fork 4
/
socracked.c
1664 lines (1535 loc) · 55.8 KB
/
socracked.c
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
/* SoCracked
Attacks up to eight rounds of the Lattice/SoDark algorithm as specified in
MIL-STD-188-141 and recovers all candidate keys in time proportional to
2^9 for two rounds, 2^16 for three rounds, 2^33 for four rounds,
2^49 for five rounds, 2^46 for six rounds, 2^46 for seven rounds, and
2^45 for eight rounds. Attacks on more than six rounds require certain
properties of the plaintext-ciphertext-tuple tweaks. In particular, this
means that successful attacks require a couple of thousand tuples that
differ in tweak byte five only. The program also performs known-plaintext
brute force attacks on up to sixteen rounds of the cipher.
Copyright (C) 2016-2018, 2020 Marcus Dansarie <marcus@dansarie.se>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#define _GNU_SOURCE
#include <assert.h>
#include <curses.h>
#include <inttypes.h>
#include <limits.h>
#include <math.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include "sodark.h"
#include "socracked.h"
#ifdef WITH_CUDA
#include "socracked_cuda.h"
const bool CUDA_ENABLED = true;
#else
const bool CUDA_ENABLED = false;
#endif
pthread_mutex_t g_next_lock;
pthread_mutex_t g_write_lock;
pthread_mutex_t g_threadcount_lock;
bool g_prof = false;
bool g_mutexes_initialized = false;
bool g_exit = false; /* Indicates that the worker threads should shut down. */
uint32_t g_num_cpu_threads; /* Number of worker threads. */
uint32_t g_num_threads;
int g_num_cuda_devices = 0; /* Number of CUDA devices. */
uint32_t g_cuda_count = 0; /* Indicates which CUDA device to use. */
double *g_thread_speeds; /* Individual thread cracking speeds in calls to get_next
per second. */
struct timeval *g_last_get_next_calls; /* Used for calculation of thread speeds. */
uint64_t g_keysfound = 0; /* Number of keys found so far. */
uint64_t g_last_key_found = (uint64_t)-1; /* Last key found. */
uint32_t g_threadcount = 0; /* Number of working threads. */
uint32_t g_next = 0; /* Next work unit. */
uint32_t g_next_pair = 0; /* Next pair. (Used when cracking 6, 7, and 8 rounds. */
pairs_t g_pairs = {NULL, 0, 0, 0}; /* Candidate pairs. Holds pairs for get_next_678. */
FILE *g_outfp = NULL; /* Pointer to output file. */
#define STATUS_BUF_LEN (51)
char g_status[STATUS_BUF_LEN] = "";
pair_t g_current_pair = {{0, 0, 0}, {0, 0, 0}, {0}, 0};
/* Updates the current cracking speed in the g_thread_speeds array. Called
whenever a thread fetches a new work unit. */
static void update_thread_speed(uint32_t threadid) {
struct timeval time_now;
gettimeofday(&time_now, NULL);
if (g_last_get_next_calls[threadid].tv_sec > 0) {
double elapsed = time_now.tv_sec - g_last_get_next_calls[threadid].tv_sec
+ (time_now.tv_usec - g_last_get_next_calls[threadid].tv_usec) / 1E6;
g_thread_speeds[threadid] = 1.0 / elapsed;
}
g_last_get_next_calls[threadid] = time_now;
}
/* Gets the status line in the user interface. */
void set_status(const char *status) {
pthread_mutex_lock(&g_write_lock);
memset(g_status, ' ', STATUS_BUF_LEN - 1);
for (int i = 0; i < strlen(status) && i < STATUS_BUF_LEN - 1; i++) {
g_status[i] = status[i];
}
g_status[STATUS_BUF_LEN - 1] = '\0';
pthread_mutex_unlock(&g_write_lock);
}
/* Returns the current user interface status line. */
static void get_status(char *status) {
pthread_mutex_lock(&g_write_lock);
strncpy(status, g_status, STATUS_BUF_LEN);
status[STATUS_BUF_LEN - 1] = '\0';
pthread_mutex_unlock(&g_write_lock);
}
/* Returns the next work unit, i.e. the next value of two key bytes.
Returns false to indicate that there are no more work units available and
that the thread should stop. */
static bool get_next(uint32_t threadid, uint32_t *next) {
update_thread_speed(threadid);
pthread_mutex_lock(&g_next_lock);
if (g_next >= 0x10000) {
set_status("Done. Waiting for threads.");
pthread_mutex_unlock(&g_next_lock);
return false;
}
*next = g_next;
g_next += 1;
pthread_mutex_unlock(&g_next_lock);
return true;
}
/* Returns the next work unit when cracking 6, 7, or 8 rounds. Returns false to indicate that there
are no more work units available and that the thread should stop. */
bool get_next_678(uint32_t threadid, uint32_t *k12, pair_t **pair) {
update_thread_speed(threadid);
pthread_mutex_lock(&g_next_lock);
if (g_next_pair >= g_pairs.num_pairs) {
set_status("Done. Waiting for threads.");
pthread_mutex_unlock(&g_next_lock);
return false;
}
*k12 = g_next;
*pair = &g_pairs.pairs[g_next_pair];
g_next += 1;
if (!g_prof && g_next == 0x10000) {
g_next = 0;
g_next_pair += 1;
}
if (g_prof && g_next > 0xc228) {
g_next_pair = g_pairs.num_pairs;
}
g_current_pair = **pair;
pthread_mutex_unlock(&g_next_lock);
return true;
}
/* Tests a candidate key against all known tuples. Returns true if the key is
a match for all of them. */
bool test_key(uint32_t rounds, uint64_t key, tuple_t *tuples, uint32_t num_tuples) {
for (uint32_t i = 0; i < num_tuples; i++) {
if (encrypt_sodark_3(rounds, tuples[i].pt, key, tuples[i].tw) != tuples[i].ct) {
return false;
}
}
return true;
}
/* Called by a cracking thread to write a found key to file and to update the
number of keys found.*/
void found_key(uint64_t key) {
pthread_mutex_lock(&g_write_lock);
fprintf(g_outfp, "%014" PRIx64 "\n", key);
fflush(g_outfp);
g_keysfound += 1;
g_last_key_found = key;
pthread_mutex_unlock(&g_write_lock);
}
/* Returns the number of keys found so far. */
static uint64_t get_keys_found() {
pthread_mutex_lock(&g_write_lock);
uint64_t keysfound = g_keysfound;
pthread_mutex_unlock(&g_write_lock);
return keysfound;
}
/* Returns the last key found. */
static uint64_t get_last_key_found() {
pthread_mutex_lock(&g_write_lock);
uint64_t last_key = g_last_key_found;
pthread_mutex_unlock(&g_write_lock);
return last_key;
}
/* Cracks two rounds. */
void crack2(tuple_t *tuples, uint32_t num_tuples) {
assert(num_tuples > 1);
const uint8_t tw11 = (tuples[0].tw >> 56) & 0xff;
const uint8_t tw12 = (tuples[0].tw >> 48) & 0xff;
const uint8_t tw13 = (tuples[0].tw >> 40) & 0xff;
const uint8_t tw14 = (tuples[0].tw >> 32) & 0xff;
const uint8_t tw15 = (tuples[0].tw >> 24) & 0xff;
const uint8_t tw16 = (tuples[0].tw >> 16) & 0xff;
const uint8_t tw21 = (tuples[1].tw >> 56) & 0xff;
const uint8_t tw22 = (tuples[1].tw >> 48) & 0xff;
const uint8_t tw23 = (tuples[1].tw >> 40) & 0xff;
const uint8_t tw24 = (tuples[1].tw >> 32) & 0xff;
const uint8_t tw25 = (tuples[1].tw >> 24) & 0xff;
const uint8_t tw26 = (tuples[1].tw >> 16) & 0xff;
const uint8_t b1 = ((tuples[0].pt >> 8) & 0xff) ^ tw13;
const uint8_t a1 = (((tuples[0].pt >> 16) ^ (tuples[0].pt >> 8)) & 0xff) ^ tw11;
const uint8_t c1 = ((tuples[0].pt ^ (tuples[0].pt >> 8)) & 0xff) ^ tw12;
const uint8_t b2 = ((tuples[1].pt >> 8) & 0xff) ^ tw23;
const uint8_t a2 = (((tuples[1].pt >> 16) ^ (tuples[1].pt >> 8)) & 0xff) ^ tw21;
const uint8_t c2 = ((tuples[1].pt ^ (tuples[1].pt >> 8)) & 0xff) ^ tw22;
const uint8_t app1 = (tuples[0].ct >> 16) & 0xff;
const uint8_t app2 = (tuples[1].ct >> 16) & 0xff;
const uint8_t cpp1 = tuples[0].ct & 0xff;
const uint8_t cpp2 = tuples[1].ct & 0xff;
const uint8_t bpp1 = g_sbox_dec[(tuples[0].ct >> 8) & 0xff] ^ app1 ^ cpp1 ^ tw16;
const uint8_t bpp2 = g_sbox_dec[(tuples[1].ct >> 8) & 0xff] ^ app2 ^ cpp2 ^ tw26;
const uint8_t sapp1 = g_sbox_dec[app1] ^ tw14;
const uint8_t sapp2 = g_sbox_dec[app2] ^ tw24;
const uint8_t scpp1 = g_sbox_dec[cpp1] ^ tw15;
const uint8_t scpp2 = g_sbox_dec[cpp2] ^ tw25;
const uint8_t da = sapp1 ^ sapp2 ^ bpp1 ^ bpp2;
const uint8_t dc = scpp1 ^ scpp2 ^ bpp1 ^ bpp2;
uint8_t k1[256];
uint8_t k2[256];
uint16_t k1p = 0;
uint16_t k2p = 0;
for (uint16_t k = 0; k < 256; k++) {
if ((g_sbox_enc[a1 ^ k] ^ g_sbox_enc[a2 ^ k]) == da) {
k1[k1p++] = k;
}
if ((g_sbox_enc[c1 ^ k] ^ g_sbox_enc[c2 ^ k]) == dc) {
k2[k2p++] = k;
}
}
for (uint16_t i = 0; i < k1p; i++) {
const uint8_t ap1 = g_sbox_enc[a1 ^ k1[i]];
const uint8_t ap2 = g_sbox_enc[a2 ^ k1[i]];
for (uint16_t k = 0; k < k2p; k++) {
const uint8_t cp1 = g_sbox_enc[c1 ^ k2[k]];
const uint8_t cp2 = g_sbox_enc[c2 ^ k2[k]];
for (uint16_t k3 = 0; k3 < 256; k3++) {
const uint8_t bp1 = g_sbox_enc[b1 ^ ap1 ^ cp1 ^ k3];
const uint8_t bp2 = g_sbox_enc[b2 ^ ap2 ^ cp2 ^ k3];
const uint8_t k41 = bp1 ^ ap1 ^ sapp1;
const uint8_t k42 = bp2 ^ ap2 ^ sapp2;
const uint8_t k51 = bp1 ^ cp1 ^ scpp1;
const uint8_t k52 = bp2 ^ cp2 ^ scpp2;
const uint8_t k6 = bp1 ^ bpp1;
if (k41 == k42 && k51 == k52) {
uint64_t key = (uint64_t)k1[i] << 48 | (uint64_t)k2[k] << 40 | (uint64_t)k3 << 32
| (uint64_t)k41 << 24 | (uint64_t)k51 << 16 | (uint64_t)k6 << 8;
if (test_key(2, key, tuples, num_tuples)) {
found_key(key);
}
}
}
}
}
uint64_t keysfound = get_keys_found();
if (keysfound == 0) {
printf("No keys found.\n");
} else if (keysfound == 1) {
printf("1 key found.\n");
} else {
printf("%" PRIu64 " keys found.\n", keysfound);
}
}
/* Cracks three rounds. */
void crack3(tuple_t *tuples, uint32_t num_tuples) {
assert(num_tuples > 1);
const uint8_t tw11 = (tuples[0].tw >> 56) & 0xff;
const uint8_t tw12 = (tuples[0].tw >> 48) & 0xff;
const uint8_t tw13 = (tuples[0].tw >> 40) & 0xff;
const uint8_t tw14 = (tuples[0].tw >> 32) & 0xff;
const uint8_t tw15 = (tuples[0].tw >> 24) & 0xff;
const uint8_t tw16 = (tuples[0].tw >> 16) & 0xff;
const uint8_t tw17 = (tuples[0].tw >> 8) & 0xff;
const uint8_t tw18 = tuples[0].tw & 0xff;
const uint8_t tw21 = (tuples[1].tw >> 56) & 0xff;
const uint8_t tw22 = (tuples[1].tw >> 48) & 0xff;
const uint8_t tw23 = (tuples[1].tw >> 40) & 0xff;
const uint8_t tw24 = (tuples[1].tw >> 32) & 0xff;
const uint8_t tw25 = (tuples[1].tw >> 24) & 0xff;
const uint8_t tw26 = (tuples[1].tw >> 16) & 0xff;
const uint8_t tw27 = (tuples[1].tw >> 8) & 0xff;
const uint8_t tw28 = tuples[1].tw & 0xff;
const uint8_t b1 = ((tuples[0].pt >> 8) & 0xff) ^ tw13;
const uint8_t a1 = (((tuples[0].pt >> 16) ^ (tuples[0].pt >> 8)) & 0xff) ^ tw11;
const uint8_t c1 = ((tuples[0].pt ^ (tuples[0].pt >> 8)) & 0xff) ^ tw12;
const uint8_t b2 = ((tuples[1].pt >> 8) & 0xff) ^ tw23;
const uint8_t a2 = (((tuples[1].pt >> 16) ^ (tuples[1].pt >> 8)) & 0xff) ^ tw21;
const uint8_t c2 = ((tuples[1].pt ^ (tuples[1].pt >> 8)) & 0xff) ^ tw22;
const uint8_t bppp1 = ((g_sbox_dec[(tuples[0].ct >> 8) & 0xff] ^ tuples[0].ct
^ (tuples[0].ct >> 16)) & 0xff) ^ tw11;
const uint8_t appp1 = g_sbox_dec[(tuples[0].ct >> 16) & 0xff] ^ tw17;
const uint8_t cppp1 = g_sbox_dec[tuples[0].ct & 0xff] ^ tw18;
const uint8_t bppp2 = ((g_sbox_dec[(tuples[1].ct >> 8) & 0xff] ^ tuples[1].ct
^ (tuples[1].ct >> 16)) & 0xff) ^ tw21;
const uint8_t appp2 = g_sbox_dec[(tuples[1].ct >> 16) & 0xff] ^ tw27;
const uint8_t cppp2 = g_sbox_dec[tuples[1].ct & 0xff] ^ tw28;
const uint8_t dbpp = bppp1 ^ bppp2;
const uint8_t dapp = appp1 ^ appp2 ^ dbpp;
const uint8_t dcpp = cppp1 ^ cppp2 ^ dbpp;
const uint8_t dacpp = dapp ^ dcpp;
const uint8_t dtw4 = tw14 ^ tw24;
const uint8_t dtw5 = tw15 ^ tw25;
const uint8_t dtw6 = tw16 ^ tw26;
for (uint16_t k2 = 0; k2 < 256; k2++) {
const uint8_t bpp1 = bppp1 ^ k2;
const uint8_t bpp2 = bppp2 ^ k2;
const uint8_t sbpp1 = g_sbox_dec[bpp1];
const uint8_t sbpp2 = g_sbox_dec[bpp2];
const uint8_t dfbpp = sbpp1 ^ sbpp2 ^ dtw6;
const uint8_t cp1 = g_sbox_enc[c1 ^ k2];
const uint8_t cp2 = g_sbox_enc[c2 ^ k2];
const uint8_t dcp = cp1 ^ cp2;
for (uint16_t k1 = 0; k1 < 256; k1++) {
const uint8_t ap1 = g_sbox_enc[a1 ^ k1];
const uint8_t ap2 = g_sbox_enc[a2 ^ k1];
const uint8_t cpp1 = cppp1 ^ k1 ^ bpp1;
const uint8_t cpp2 = cppp2 ^ k1 ^ bpp2;
const uint8_t scpp1 = g_sbox_dec[cpp1];
const uint8_t scpp2 = g_sbox_dec[cpp2];
const uint8_t dbp = dcp ^ scpp1 ^ scpp2 ^ dtw5;
if (dfbpp == (dacpp ^ dbp)) {
const uint8_t dap = g_sbox_enc[a1 ^ k1] ^ g_sbox_enc[a2 ^ k1] ^ dtw4;
for (uint16_t k7 = 0; k7 < 256; k7++) {
const uint8_t app1 = appp1 ^ bpp1 ^ k7;
const uint8_t app2 = appp2 ^ bpp2 ^ k7;
const uint8_t sapp1 = g_sbox_dec[app1];
const uint8_t sapp2 = g_sbox_dec[app2];
if ((sapp1 ^ sapp2 ^ dap) == dbp) {
for (uint16_t k3 = 0; k3 < 256; k3++) {
const uint8_t bp1 = g_sbox_enc[ap1 ^ cp1 ^ b1 ^ k3];
const uint8_t bp2 = g_sbox_enc[ap2 ^ cp2 ^ b2 ^ k3];
const uint8_t k41 = sapp1 ^ ap1 ^ bp1 ^ tw14;
const uint8_t k42 = sapp2 ^ ap2 ^ bp2 ^ tw24;
const uint8_t k5 = scpp1 ^ cp1 ^ bp1 ^ tw15;
const uint8_t k6 = sbpp1 ^ app1 ^ cpp1 ^ bp1 ^ tw16;
if (k41 == k42) {
uint64_t key = (uint64_t)k1 << 48 | (uint64_t)k2 << 40 | (uint64_t)k3 << 32
| (uint64_t)k41 << 24 | (uint64_t)k5 << 16 | (uint64_t)k6 << 8 | k7;
if (test_key(3, key, tuples, num_tuples)) {
found_key(key);
}
}
}
}
}
}
}
}
uint64_t keysfound = get_keys_found();
if (keysfound == 0) {
printf("No keys found.\n");
} else if (keysfound == 1) {
printf("1 key found.\n");
} else {
printf("%" PRIu64 " keys found.\n", keysfound);
}
}
/* Cracking thread function for 4 rounds. */
void *crack4(void *p) {
worker_param_t params = *((worker_param_t*)p);
tuple_t *tuples = params.tuples;
uint32_t num_tuples = params.num_tuples;
assert(num_tuples > 1);
assert(params.nrounds == 4);
pthread_mutex_lock(&g_threadcount_lock);
uint32_t threadid = g_threadcount++;
pthread_mutex_unlock(&g_threadcount_lock);
struct delta **lists = NULL;
struct delta *items = NULL;
if (g_exit) {
goto exit4;
}
/* Precalculate tweaks. */
const uint32_t r1tw1 = tuples[0].tw >> 40;
const uint32_t r1tw2 = tuples[1].tw >> 40;
const uint32_t r4tw1 = (tuples[0].tw >> 32) & 0xffffff;
const uint32_t r4tw2 = (tuples[1].tw >> 32) & 0xffffff;
const uint8_t tw11 = (tuples[0].tw >> 56) & 0xff;
const uint8_t tw14 = (tuples[0].tw >> 32) & 0xff;
const uint8_t tw15 = (tuples[0].tw >> 24) & 0xff;
const uint8_t tw16 = (tuples[0].tw >> 16) & 0xff;
const uint8_t tw17 = (tuples[0].tw >> 8) & 0xff;
const uint8_t tw18 = tuples[0].tw & 0xff;
const uint8_t tw21 = (tuples[1].tw >> 56) & 0xff;
const uint8_t tw24 = (tuples[1].tw >> 32) & 0xff;
const uint8_t tw25 = (tuples[1].tw >> 24) & 0xff;
const uint8_t tw26 = (tuples[1].tw >> 16) & 0xff;
const uint8_t tw27 = (tuples[1].tw >> 8) & 0xff;
const uint8_t tw28 = tuples[1].tw & 0xff;
struct delta {
uint8_t k5;
uint8_t app1;
uint8_t app2;
uint8_t bpp1;
uint8_t bpp2;
uint8_t cpp1;
uint8_t cpp2;
struct delta *next;
struct delta *last;
};
lists = (struct delta**)malloc(0x10000 * sizeof(struct delta*));
items = (struct delta*)malloc(0x10000 * sizeof(struct delta));
if (lists == NULL || items == NULL) {
fprintf(stderr, "Error: malloc returned null in thread %" PRIu32 ".\n", threadid);
if (lists != NULL) {
free(lists);
}
if (items != NULL) {
free(items);
}
pthread_mutex_lock(&g_threadcount_lock);
g_threadcount -= 1;
pthread_mutex_unlock(&g_threadcount_lock);
return NULL;
}
uint32_t k23;
while (get_next(threadid, &k23)) {
if (g_exit) {
goto exit4;
}
const uint8_t k2 = k23 >> 8;
const uint8_t k3 = k23 & 0xff;
memset(lists, 0, 0x10000 * sizeof(struct delta*));
for (uint32_t k45 = 0; k45 < 0x10000; k45++) {
const uint8_t k4 = k45 >> 8;
const uint8_t k5 = k45 & 0xff;
const uint32_t k345 = ((uint32_t)k3 << 16) | k45;
const uint32_t r31 = dec_one_round_3(tuples[0].ct, k345 ^ r4tw1);
const uint32_t r32 = dec_one_round_3(tuples[1].ct, k345 ^ r4tw2);
const uint8_t r31a = (r31 >> 16) & 0xff;
const uint8_t r31b = (r31 >> 8) & 0xff;
const uint8_t r31c = r31 & 0xff;
const uint8_t bpp1 = g_sbox_dec[r31b] ^ r31a ^ r31c ^ k2 ^ tw11;
const uint8_t r32a = (r32 >> 16) & 0xff;
const uint8_t r32b = (r32 >> 8) & 0xff;
const uint8_t r32c = r32 & 0xff;
const uint8_t bpp2 = g_sbox_dec[r32b] ^ r32a ^ r32c ^ k2 ^ tw21;
const uint8_t app1 = g_sbox_dec[r31a] ^ bpp1 ^ tw17;
const uint8_t app2 = g_sbox_dec[r32a] ^ bpp2 ^ tw27;
const uint8_t cpp1 = g_sbox_dec[r31c] ^ bpp1 ^ tw18;
const uint8_t cpp2 = g_sbox_dec[r32c] ^ bpp2 ^ tw28;
const uint16_t addr = k4 * 256 + (app1 ^ app2);
if (lists[addr] == NULL) {
lists[addr] = &(items[k45]);
lists[addr]->k5 = k5;
lists[addr]->next = NULL;
lists[addr]->last = lists[addr];
lists[addr]->app1 = app1;
lists[addr]->app2 = app2;
lists[addr]->bpp1 = bpp1;
lists[addr]->bpp2 = bpp2;
lists[addr]->cpp1 = cpp1;
lists[addr]->cpp2 = cpp2;
} else {
lists[addr]->last->next = &(items[k45]);
lists[addr]->last = &(items[k45]);
lists[addr]->last->k5 = k5;
lists[addr]->last->next = NULL;
lists[addr]->last->app1 = app1;
lists[addr]->last->app2 = app2;
lists[addr]->last->bpp1 = bpp1;
lists[addr]->last->bpp2 = bpp2;
lists[addr]->last->cpp1 = cpp1;
lists[addr]->last->cpp2 = cpp2;
}
}
for (uint16_t k1 = 0; k1 < 256; k1++) {
const uint32_t k123 = ((uint32_t)k1 << 16) | ((uint32_t)k2 << 8) | k3;
const uint32_t r11 = enc_one_round_3(tuples[0].pt, k123 ^ r1tw1);
const uint32_t r12 = enc_one_round_3(tuples[1].pt, k123 ^ r1tw2);
const uint8_t r11a = r11 >> 16;
const uint8_t r11b = (r11 >> 8) & 0xff;
const uint8_t r12a = r12 >> 16;
const uint8_t r12b = (r12 >> 8) & 0xff;
for (uint16_t k4 = 0; k4 < 256; k4++) {
const uint8_t app1 = g_sbox_enc[r11a ^ r11b ^ k4 ^ tw14];
const uint8_t app2 = g_sbox_enc[r12a ^ r12b ^ k4 ^ tw24];
struct delta *next = lists[k4 * 256 + (app1 ^ app2)];
while (next != NULL) {
const uint8_t r11c = r11 & 0xff;
const uint8_t r12c = r12 & 0xff;
const uint8_t cpp1 = g_sbox_enc[r11b ^ r11c ^ next->k5 ^ tw15];
const uint8_t cpp2 = g_sbox_enc[r12b ^ r12c ^ next->k5 ^ tw25];
const uint8_t k11 = cpp1 ^ next->cpp1;
const uint8_t k12 = cpp2 ^ next->cpp2;
const uint8_t k61 = r11b ^ app1 ^ cpp1 ^ tw16 ^ g_sbox_dec[next->bpp1];
const uint8_t k62 = r12b ^ app2 ^ cpp2 ^ tw26 ^ g_sbox_dec[next->bpp2];
const uint8_t k71 = app1 ^ next->app1;
const uint8_t k72 = app2 ^ next->app2;
if (k11 == k12 && k61 == k62 && k71 == k72) {
const uint64_t key = ((uint64_t)k123 << 32) | ((uint64_t)k4) << 24
| ((uint64_t)(next->k5)) << 16 | ((uint64_t)k61) << 8 | k71;
if (test_key(4, key, tuples, num_tuples)) {
found_key(key);
}
}
next = next->next;
}
}
}
}
exit4:
free(lists);
free(items);
pthread_mutex_lock(&g_threadcount_lock);
g_threadcount -= 1;
pthread_mutex_unlock(&g_threadcount_lock);
return NULL;
}
/* Cracking thread function for 5 rounds. */
void *crack5(void *p) {
worker_param_t params = *((worker_param_t*)p);
tuple_t *tuples = params.tuples;
uint32_t num_tuples = params.num_tuples;
assert(num_tuples > 1);
assert(params.nrounds == 5);
pthread_mutex_lock(&g_threadcount_lock);
uint32_t threadid = g_threadcount++;
pthread_mutex_unlock(&g_threadcount_lock);
if (g_exit) {
goto exit5;
}
/* Precalculate tweaks. */
const uint32_t r1tw1 = tuples[0].tw >> 40;
const uint32_t r1tw2 = tuples[1].tw >> 40;
const uint32_t r2tw1 = (tuples[0].tw >> 16) & 0xffffff;
const uint32_t r2tw2 = (tuples[1].tw >> 16) & 0xffffff;
const uint32_t r4tw1 = (tuples[0].tw >> 32) & 0xffffff;
const uint32_t r4tw2 = (tuples[1].tw >> 32) & 0xffffff;
const uint32_t r5tw1 = (tuples[0].tw >> 8) & 0xffffff;
const uint32_t r5tw2 = (tuples[1].tw >> 8) & 0xffffff;
struct delta {
uint8_t k2;
uint32_t delta;
struct delta *next;
struct delta *last;
};
struct delta items[0x100];
struct delta *lists[0x100];
uint32_t k13;
while (get_next(threadid, &k13)) {
if (g_prof && k13 != 0xc24a) {
goto exit5;
}
const uint8_t k1 = k13 >> 8;
const uint8_t k3 = k13 & 0xff;
for (uint32_t k456 = 0; k456 < 0x1000000; k456++) {
const uint64_t pkey = ((uint64_t)k1 << 48) | ((uint64_t)k3 << 32) | ((uint64_t)k456 << 8);
uint32_t k345 = ((uint32_t)k3 << 16) | (k456 >> 8);
memset(lists, 0, 0x100 * sizeof(struct delta*));
for (uint16_t k2 = 0; k2 < 0x100; k2++) {
uint32_t k123 = ((uint32_t)k1 << 16) | (k2 << 8) | k3;
uint32_t v1 = enc_one_round_3(enc_one_round_3(tuples[0].pt, k123 ^ r1tw1), k456 ^ r2tw1);
uint32_t v2 = enc_one_round_3(enc_one_round_3(tuples[1].pt, k123 ^ r1tw2), k456 ^ r2tw2);
uint32_t delta = v1 ^ v2;
uint8_t addr = delta & 0xff;
if (lists[addr] == NULL) {
lists[addr] = &(items[k2]);
lists[addr]->k2 = k2;
lists[addr]->delta = delta;
lists[addr]->next = NULL;
lists[addr]->last = lists[addr];
} else {
lists[addr]->last->next = &(items[k2]);
lists[addr]->last = &(items[k2]);
lists[addr]->last->k2 = k2;
lists[addr]->last->delta = delta;
lists[addr]->last->next = NULL;
}
}
for (uint16_t k7 = 0; k7 < 0x100; k7++) {
if (g_exit) {
goto exit5;
}
uint32_t k671 = ((k456 & 0xff) << 16) | (k7 << 8) | k1;
uint32_t v1 = dec_one_round_3(dec_one_round_3(tuples[0].ct, k671 ^ r5tw1), k345 ^ r4tw1);
uint32_t v2 = dec_one_round_3(dec_one_round_3(tuples[1].ct, k671 ^ r5tw2), k345 ^ r4tw2);
uint32_t db = g_sbox_dec[(v1 >> 8) & 0xff];
db ^= g_sbox_dec[(v2 >> 8) & 0xff];
db ^= v1;
db ^= v2;
db ^= v1 >> 16;
db ^= v2 >> 16;
db &= 0xff;
uint32_t da = g_sbox_dec[v1 >> 16];
da ^= g_sbox_dec[v2 >> 16];
da ^= db;
uint32_t dc = g_sbox_dec[v1 & 0xff];
dc ^= g_sbox_dec[v2 & 0xff];
dc ^= db;
uint32_t delta = (da << 16) | (db << 8) | dc;
uint8_t addr = delta & 0xff;
struct delta *next = lists[addr];
while (next != NULL) {
if (next->delta != delta) {
next = next->next;
continue;
}
const uint64_t key = pkey | k7 | ((uint64_t)next->k2 << 40);
if (test_key(5, key, tuples, num_tuples)) {
found_key(key);
}
next = next->next;
}
}
}
}
exit5:
pthread_mutex_lock(&g_threadcount_lock);
g_threadcount -= 1;
pthread_mutex_unlock(&g_threadcount_lock);
return NULL;
}
/* Cracking thread function for 6, 7, and 8 rounds. */
void *crack678(void *p) {
worker_param_t params = *((worker_param_t*)p);
assert(params.num_tuples > 1);
assert(params.nrounds > 5 && params.nrounds < 9);
pthread_mutex_lock(&g_threadcount_lock);
uint32_t threadid = g_threadcount++;
pthread_mutex_unlock(&g_threadcount_lock);
if (g_exit) {
pthread_mutex_lock(&g_threadcount_lock);
g_threadcount -= 1;
pthread_mutex_unlock(&g_threadcount_lock);
return NULL;
}
uint32_t k12;
pair_t *pair;
while (get_next_678(threadid, &k12, &pair)) {
int k1 = k12 >> 8;
int k2 = k12 & 0xff;
/* Plaintexts. */
const uint32_t a01 = (pair->t1.pt >> 16) & 0xff;
const uint32_t a02 = (pair->t2.pt >> 16) & 0xff;
const uint32_t b01 = (pair->t1.pt >> 8) & 0xff;
const uint32_t b02 = (pair->t2.pt >> 8) & 0xff;
const uint32_t c01 = pair->t1.pt & 0xff;
const uint32_t c02 = pair->t2.pt & 0xff;
/* Tweaks. */
const uint32_t t11 = (pair->t1.tw >> 56) & 0xff;
const uint32_t t12 = (pair->t2.tw >> 56) & 0xff;
const uint32_t t21 = (pair->t1.tw >> 48) & 0xff;
const uint32_t t22 = (pair->t2.tw >> 48) & 0xff;
const uint32_t t31 = (pair->t1.tw >> 40) & 0xff;
const uint32_t t32 = (pair->t2.tw >> 40) & 0xff;
const uint32_t t41 = (pair->t1.tw >> 32) & 0xff;
const uint32_t t42 = (pair->t2.tw >> 32) & 0xff;
const uint32_t t51 = (pair->t1.tw >> 24) & 0xff;
const uint32_t t52 = (pair->t2.tw >> 24) & 0xff;
const uint32_t t61 = (pair->t1.tw >> 16) & 0xff;
const uint32_t t62 = (pair->t2.tw >> 16) & 0xff;
const uint32_t t81 = pair->t1.tw & 0xff;
const uint32_t t82 = pair->t2.tw & 0xff;
const uint32_t a11 = g_sbox_enc[a01 ^ b01 ^ k1 ^ t11];
const uint32_t a12 = g_sbox_enc[a02 ^ b02 ^ k1 ^ t12];
const uint32_t c11 = g_sbox_enc[c01 ^ b01 ^ k2 ^ t21];
const uint32_t c12 = g_sbox_enc[c02 ^ b02 ^ k2 ^ t22];
for (int k3p = 0; k3p < pair->num_k3; k3p++) {
int k3 = pair->k3[k3p];
const uint32_t b11 = g_sbox_enc[a11 ^ b01 ^ c11 ^ k3 ^ t31];
const uint32_t b12 = g_sbox_enc[a12 ^ b02 ^ c12 ^ k3 ^ t32];
for (int k4 = 0; k4 < 0x100; k4++) {
if (g_exit) {
goto exit678;
}
const uint32_t a21 = g_sbox_enc[a11 ^ b11 ^ k4 ^ t41];
const uint32_t a22 = g_sbox_enc[a12 ^ b12 ^ k4 ^ t42];
for (int k5 = 0; k5 < 0x100; k5++) {
const uint32_t c21 = g_sbox_enc[c11 ^ b11 ^ k5 ^ t51];
const uint32_t c22 = g_sbox_enc[c12 ^ b12 ^ k5 ^ t52];
for (int k6 = 0; k6 < 0x100; k6++) {
const uint32_t b21 = g_sbox_enc[a21 ^ b11 ^ c21 ^ k6 ^ t61];
const uint32_t b22 = g_sbox_enc[a22 ^ b12 ^ c22 ^ k6 ^ t62];
const uint32_t c31 = g_sbox_enc[c21 ^ b21 ^ k1 ^ t81];
const uint32_t c32 = g_sbox_enc[c22 ^ b22 ^ k1 ^ t82];
if ((c31 ^ c32) == (t51 ^ t52)) {
uint64_t pkey = ((uint64_t)k1 << 48) | ((uint64_t)k2 << 40) | ((uint64_t)k3 << 32)
| ((uint64_t)k4 << 24) | ((uint64_t)k5 << 16) | ((uint64_t)k6 << 8);
for (int k7 = 0; k7 < 0x100; k7++) {
uint64_t fullkey = pkey | k7;
if (test_key(params.nrounds, fullkey, params.tuples, params.num_tuples)) {
found_key(fullkey);
}
}
}
}
}
}
}
}
exit678:
pthread_mutex_lock(&g_threadcount_lock);
g_threadcount -= 1;
pthread_mutex_unlock(&g_threadcount_lock);
return NULL;
}
#ifdef WITH_CUDA
void *cuda_crack(void *p, bool brute) {
worker_param_t params = *((worker_param_t*)p);
assert(params.num_tuples > 1);
assert(params.nrounds > 5 && params.nrounds <= 16);
pthread_mutex_lock(&g_threadcount_lock);
uint32_t threadid = g_threadcount++;
uint32_t cuda_device = g_cuda_count++ % g_num_cuda_devices;
pthread_mutex_unlock(&g_threadcount_lock);
if (g_exit) {
pthread_mutex_lock(&g_threadcount_lock);
g_threadcount -= 1;
pthread_mutex_unlock(&g_threadcount_lock);
return NULL;
}
if (brute) {
cuda_brute(params, threadid, cuda_device, params.nrounds);
} else {
cuda_fast(params, threadid, cuda_device);
}
pthread_mutex_lock(&g_threadcount_lock);
g_threadcount -= 1;
pthread_mutex_unlock(&g_threadcount_lock);
return NULL;
}
void *cuda_crack_brute(void *p) {
return cuda_crack(p, true);
}
void *cuda_crack_fast(void *p) {
return cuda_crack(p, false);
}
#endif /* WITH_CUDA */
/* Initializes a list of tuple-pairs. */
static bool init_pairs(pairs_t *pairs) {
assert(pairs != NULL);
pairs->allocsize = pairs->allocstep = 100;
pairs->num_pairs = 0;
pairs->pairs = malloc(sizeof(pair_t) * pairs->allocsize);
if (pairs->pairs == NULL) {
pairs->allocsize = 0;
endwin();
fprintf(stderr, "Memory allocation error on line %d.\n", __LINE__);
return false;
}
return true;
}
/* Frees a list of tuple-pairs. */
static void free_pairs(pairs_t *pairs) {
assert(pairs != NULL);
free(pairs->pairs);
pairs->pairs = NULL;
pairs->allocsize = 0;
pairs->num_pairs = 0;
}
/* Adds a pair to a list of tuple-pairs. Returns true on success and false on failure. */
static bool add_pair(pairs_t *pairs, pair_t p) {
assert(pairs != NULL);
assert(pairs->allocsize > 0);
assert(pairs->allocstep > 0);
assert(pairs->num_pairs >= 0);
assert(pairs->num_pairs < pairs->allocsize);
pairs->pairs[pairs->num_pairs] = p;
pairs->num_pairs += 1;
if (pairs->num_pairs == pairs->allocsize) {
pairs->allocsize += pairs->allocstep;
pairs->pairs = realloc(pairs->pairs, sizeof(pair_t) * pairs->allocsize);
if (pairs->pairs == NULL) {
endwin();
fprintf(stderr, "Memory allocation error on line %d.\n", __LINE__);
return false;
}
}
return true;
}
bool init_tuples(worker_param_t *worker_params) {
worker_params->allocstep = 1000;
worker_params->allocsize = worker_params->allocstep;
worker_params->num_tuples = 0;
worker_params->tuples = malloc(sizeof(tuple_t) * worker_params->allocsize);
if (worker_params->tuples == NULL) {
endwin();
fprintf(stderr, "Memory allocation error on line %d.\n", __LINE__);
return true;
}
return false;
}
void free_tuples(worker_param_t *worker_params) {
free(worker_params->tuples);
worker_params->tuples = NULL;
worker_params->allocsize = 0;
worker_params->num_tuples = 0;
}
bool add_tuple(worker_param_t *worker_params, tuple_t tuple) {
worker_params->tuples[worker_params->num_tuples].pt = tuple.pt;
worker_params->tuples[worker_params->num_tuples].ct = tuple.ct;
worker_params->tuples[worker_params->num_tuples].tw = tuple.tw;
worker_params->num_tuples += 1;
if (worker_params->num_tuples == worker_params->allocsize) {
worker_params->allocsize += worker_params->allocstep;
worker_params->tuples = realloc(worker_params->tuples,
sizeof(tuple_t) * worker_params->allocsize);
if (worker_params->tuples == NULL) {
endwin();
fprintf(stderr, "Memory allocation error on line %d.\n", __LINE__);
return true;
}
}
return false;
}
void get_elapsed_time(struct timeval start_time, struct timeval time_now,
int *days, int *hours, int *minutes, int *seconds) {
int elapsed = time_now.tv_sec - start_time.tv_sec;
*days = elapsed / 86400;
elapsed -= *days * 86400;
*hours = elapsed / 3600;
elapsed -= *hours * 3600;
*minutes = elapsed / 60;
elapsed -= *minutes * 60;
*seconds = elapsed;
}
#define PRINT_BACKGROUND_STRING(y, x, maxy, str) \
if (y < (maxy - 2)) {\
mvprintw(y, x, str);\
} else {\
refresh();\
return;\
}
/* Draws the static background to the screen. */
static void draw_background(WINDOW *screen) {
assert(screen != NULL);
int maxx = 0;
int maxy = 0;
(void)maxx; /* Silence unused warning. */
getmaxyx(screen, maxy, maxx);
clear();
bkgd(COLOR_PAIR(1));
border(0, 0, 0, 0, 0, 0, 0, 0);
attrset(COLOR_PAIR(1));
if (CUDA_ENABLED) {
PRINT_BACKGROUND_STRING(1, 1, maxy, "SoCracked v. " SOCRACKED_VERSION " (CUDA)");
} else {
PRINT_BACKGROUND_STRING(1, 1, maxy, "SoCracked v. " SOCRACKED_VERSION);
}
PRINT_BACKGROUND_STRING(3, 1, maxy, "Start time:");
PRINT_BACKGROUND_STRING(4, 1, maxy, "Elapsed time:");
PRINT_BACKGROUND_STRING(5, 1, maxy, "Estimated finish:");
PRINT_BACKGROUND_STRING(6, 1, maxy, "Success probability:");
PRINT_BACKGROUND_STRING(8, 1, maxy, "Rounds:");
PRINT_BACKGROUND_STRING(9, 1, maxy, "Tuple 1:");
PRINT_BACKGROUND_STRING(10, 1, maxy, "Tuple 2:");
PRINT_BACKGROUND_STRING(11, 1, maxy, "Keys found:");
PRINT_BACKGROUND_STRING(12, 1, maxy, "Last key found:");
PRINT_BACKGROUND_STRING(14, 1, maxy, "Pairs:");
PRINT_BACKGROUND_STRING(14, 22, maxy, "[");
PRINT_BACKGROUND_STRING(14, 77, maxy, "]");
PRINT_BACKGROUND_STRING(15, 1, maxy, "Keys:");
PRINT_BACKGROUND_STRING(15, 22, maxy, "[");
PRINT_BACKGROUND_STRING(15, 77, maxy, "]");
PRINT_BACKGROUND_STRING(17, 1, maxy, "Status:");
PRINT_BACKGROUND_STRING(18, 1, maxy, "CPU threads:");
if (CUDA_ENABLED) {
PRINT_BACKGROUND_STRING(19, 1, maxy, "CUDA devices:");
}
refresh();
}
#define RETURN_IF_MAXY(maxy, y) if (y > (maxy - 3)) { refresh(); return; }
/* Draws all dynamic content on the screen.
start_time UTC time that the program was started.
psuccess Calculated probability of success, as a percentage (0.0 <= p <= 100.0).
Values outside this range result in no probability being printed.
rounds Number of rounds cracked.
screen Window reference.
qonce True if Q has already been pressed once. */
static void draw_foreground(struct timeval start_time, double psuccess, uint32_t rounds,
WINDOW *screen, bool qonce) {
assert(screen != NULL);
attrset(COLOR_PAIR(1));
int maxx = 0;
int maxy = 0;
(void)maxx; /* Silence unused warning. */
getmaxyx(screen, maxy, maxx);
if (qonce) {
mvprintw(maxy - 2, 1, "Press Q to quit. ");
} else {
mvprintw(maxy - 2, 1, "Press Q twice to quit.");
}
RETURN_IF_MAXY(maxy, 3);
attrset(COLOR_PAIR(2));
/* Start time */
struct tm *stime = gmtime(&start_time.tv_sec);
mvprintw(3, 22, "%04d-%02d-%02d %02d:%02d:%02d UTC", 1900 + stime->tm_year, stime->tm_mon + 1,
stime->tm_mday, stime->tm_hour, stime->tm_min, stime->tm_sec);
RETURN_IF_MAXY(maxy, 4);
/* Elapsed time */
struct timeval time_now;
gettimeofday(&time_now, NULL);
int days, hours, minutes, seconds;
get_elapsed_time(start_time, time_now, &days, &hours, &minutes, &seconds);
mvprintw(4, 22, "%dd %02d:%02d:%02d", days, hours, minutes, seconds);
RETURN_IF_MAXY(maxy, 5);
/* Finish time */
if (g_thread_speeds != NULL) {
double tspeed = 0.0;
bool all_valid = true;
time_t speed_update_time = 0;
for (int i = 0; i < g_num_threads; i++) {
if (g_thread_speeds[i] <= 0.0) {
all_valid = false;
break;
}
tspeed += g_thread_speeds[i];
if (g_last_get_next_calls[i].tv_sec > speed_update_time) {
speed_update_time = g_last_get_next_calls[i].tv_sec;
}
}
if (all_valid) {
time_t finish_time = speed_update_time + (0x10000 - g_next) / tspeed;
if (rounds > 5) {
pthread_mutex_lock(&g_next_lock);
finish_time += (0x10000 * (g_pairs.num_pairs - g_next_pair - 1)) / tspeed;
pthread_mutex_unlock(&g_next_lock);
}
stime = gmtime(&finish_time);
if (stime != NULL) {
mvprintw(5, 22, "%04d-%02d-%02d %02d:%02d:%02d UTC", 1900 + stime->tm_year,
stime->tm_mon + 1, stime->tm_mday, stime->tm_hour, stime->tm_min, stime->tm_sec);
}
}
}
RETURN_IF_MAXY(maxy, 6);
if (psuccess >= 0.0 && psuccess <= 100.0) {
mvprintw(6, 22, "%.1f%%", psuccess);
}
RETURN_IF_MAXY(maxy, 8);
mvprintw(8, 22, "%d ", rounds);
RETURN_IF_MAXY(maxy, 9);
pthread_mutex_lock(&g_next_lock);
mvprintw(9, 22, "%06" PRIx32 " %06" PRIx32 " %016" PRIx64,
g_current_pair.t1.pt, g_current_pair.t1.ct, g_current_pair.t1.tw); /* Tuple 1 */
if (10 > (maxy - 3)) {
pthread_mutex_unlock(&g_next_lock);
refresh();
return;