33
33
#include "parity.h"
34
34
#include "hardnested/hardnested_bruteforce.h"
35
35
#include "hardnested/hardnested_bitarray_core.h"
36
+ #include "zlib.h"
36
37
37
38
#define NUM_CHECK_BITFLIPS_THREADS (num_CPUs())
38
39
#define NUM_REDUCTION_WORKING_THREADS (num_CPUs())
39
40
40
41
#define IGNORE_BITFLIP_THRESHOLD 0.99 // ignore bitflip arrays which have nearly only valid states
41
42
42
43
#define STATE_FILES_DIRECTORY "hardnested/tables/"
43
- #define STATE_FILE_TEMPLATE "bitflip_%d_%03" PRIx16 "_states.bin"
44
+ #define STATE_FILE_TEMPLATE "bitflip_%d_%03" PRIx16 "_states.bin.z "
44
45
45
46
#define DEBUG_KEY_ELIMINATION
46
47
// #define DEBUG_REDUCTION
@@ -240,12 +241,48 @@ static int compare_count_bitflip_bitarrays(const void *b1, const void *b2)
240
241
}
241
242
242
243
244
+ static voidpf inflate_malloc (voidpf opaque , uInt items , uInt size )
245
+ {
246
+ return malloc (items * size );
247
+ }
248
+
249
+
250
+ static void inflate_free (voidpf opaque , voidpf address )
251
+ {
252
+ free (address );
253
+ }
254
+
255
+ #define OUTPUT_BUFFER_LEN 80
256
+ #define INPUT_BUFFER_LEN 80
257
+
258
+ //----------------------------------------------------------------------------
259
+ // Initialize decompression of the respective (HF or LF) FPGA stream
260
+ //----------------------------------------------------------------------------
261
+ static void init_inflate (z_streamp compressed_stream , uint8_t * input_buffer , uint32_t insize , uint8_t * output_buffer , uint32_t outsize )
262
+ {
263
+
264
+ // initialize z_stream structure for inflate:
265
+ compressed_stream -> next_in = input_buffer ;
266
+ compressed_stream -> avail_in = insize ;
267
+ compressed_stream -> next_out = output_buffer ;
268
+ compressed_stream -> avail_out = outsize ;
269
+ compressed_stream -> zalloc = & inflate_malloc ;
270
+ compressed_stream -> zfree = & inflate_free ;
271
+
272
+ inflateInit2 (compressed_stream , 0 );
273
+
274
+ }
275
+
276
+
243
277
static void init_bitflip_bitarrays (void )
244
278
{
245
279
#if defined (DEBUG_REDUCTION )
246
280
uint8_t line = 0 ;
247
281
#endif
248
282
283
+
284
+ z_stream compressed_stream ;
285
+
249
286
char state_files_path [strlen (get_my_executable_directory ()) + strlen (STATE_FILES_DIRECTORY ) + strlen (STATE_FILE_TEMPLATE ) + 1 ];
250
287
char state_file_name [strlen (STATE_FILE_TEMPLATE )+ 1 ];
251
288
@@ -262,22 +299,31 @@ static void init_bitflip_bitarrays(void)
262
299
if (statesfile == NULL ) {
263
300
continue ;
264
301
} else {
265
- uint32_t * bitset = (uint32_t * )malloc_bitarray (sizeof (uint32_t ) * (1 <<19 ));
266
- if (bitset == NULL ) {
267
- printf ("Out of memory error in init_bitflip_statelists(). Aborting...\n" );
268
- fclose (statesfile );
269
- exit (4 );
270
- }
271
- size_t bytesread = fread (bitset , 1 , sizeof (uint32_t ) * (1 <<19 ), statesfile );
272
- if (bytesread != sizeof (uint32_t ) * (1 <<19 )) {
273
- printf ("File read error with %s. Aborting..." , state_file_name );
302
+ fseek (statesfile , 0 , SEEK_END );
303
+ uint32_t filesize = (uint32_t )ftell (statesfile );
304
+ rewind (statesfile );
305
+ uint8_t input_buffer [filesize ];
306
+ size_t bytesread = fread (input_buffer , 1 , filesize , statesfile );
307
+ if (bytesread != filesize ) {
308
+ printf ("File read error with %s. Aborting...\n" , state_file_name );
274
309
fclose (statesfile );
275
- free_bitarray ( bitset );
310
+ inflateEnd ( & compressed_stream );
276
311
exit (5 );
277
312
}
278
313
fclose (statesfile );
279
- uint32_t count = count_states (bitset );
314
+ uint32_t count = 0 ;
315
+ init_inflate (& compressed_stream , input_buffer , filesize , (uint8_t * )& count , sizeof (count ));
316
+ inflate (& compressed_stream , Z_SYNC_FLUSH );
280
317
if ((float )count /(1 <<24 ) < IGNORE_BITFLIP_THRESHOLD ) {
318
+ uint32_t * bitset = (uint32_t * )malloc_bitarray (sizeof (uint32_t ) * (1 <<19 ));
319
+ if (bitset == NULL ) {
320
+ printf ("Out of memory error in init_bitflip_statelists(). Aborting...\n" );
321
+ inflateEnd (& compressed_stream );
322
+ exit (4 );
323
+ }
324
+ compressed_stream .next_out = (uint8_t * )bitset ;
325
+ compressed_stream .avail_out = sizeof (uint32_t ) * (1 <<19 );
326
+ inflate (& compressed_stream , Z_SYNC_FLUSH );
281
327
effective_bitflip [odd_even ][num_effective_bitflips [odd_even ]++ ] = bitflip ;
282
328
bitflip_bitarrays [odd_even ][bitflip ] = bitset ;
283
329
count_bitflip_bitarrays [odd_even ][bitflip ] = count ;
@@ -289,9 +335,8 @@ static void init_bitflip_bitarrays(void)
289
335
line = 0 ;
290
336
}
291
337
#endif
292
- } else {
293
- free_bitarray (bitset );
294
338
}
339
+ inflateEnd (& compressed_stream );
295
340
}
296
341
}
297
342
effective_bitflip [odd_even ][num_effective_bitflips [odd_even ]] = 0x400 ; // EndOfList marker
@@ -2549,6 +2594,7 @@ int mfnestedhard(uint8_t blockNo, uint8_t keyType, uint8_t *key, uint8_t trgBloc
2549
2594
best_first_bytes [0 ] = best_first_byte_smallest_bitarray ;
2550
2595
pre_XOR_nonces ();
2551
2596
prepare_bf_test_nonces (nonces , best_first_bytes [0 ]);
2597
+ hardnested_print_progress (num_acquired_nonces , "Starting brute force..." , expected_brute_force1 , 0 );
2552
2598
key_found = brute_force ();
2553
2599
free (candidates -> states [ODD_STATE ]);
2554
2600
free (candidates -> states [EVEN_STATE ]);
@@ -2568,6 +2614,7 @@ int mfnestedhard(uint8_t blockNo, uint8_t keyType, uint8_t *key, uint8_t trgBloc
2568
2614
// printf("Estimated remaining states: %" PRIu64 " (2^%1.1f)\n", nonces[best_first_bytes[0]].sum_a8_guess[j].num_states, log(nonces[best_first_bytes[0]].sum_a8_guess[j].num_states)/log(2.0));
2569
2615
generate_candidates (first_byte_Sum , nonces [best_first_bytes [0 ]].sum_a8_guess [j ].sum_a8_idx );
2570
2616
// printf("Time for generating key candidates list: %1.0f sec (%1.1f sec CPU)\n", difftime(time(NULL), start_time), (float)(msclock() - start_clock)/1000.0);
2617
+ hardnested_print_progress (num_acquired_nonces , "Starting brute force..." , expected_brute_force , 0 );
2571
2618
key_found = brute_force ();
2572
2619
free_statelist_cache ();
2573
2620
free_candidates_memory (candidates );
@@ -2608,6 +2655,12 @@ int mfnestedhard(uint8_t blockNo, uint8_t keyType, uint8_t *key, uint8_t trgBloc
2608
2655
2609
2656
if (nonce_file_read ) { // use pre-acquired data from file nonces.bin
2610
2657
if (read_nonce_file () != 0 ) {
2658
+ free_bitflip_bitarrays ();
2659
+ free_nonces_memory ();
2660
+ free_bitarray (all_bitflips_bitarray [ODD_STATE ]);
2661
+ free_bitarray (all_bitflips_bitarray [EVEN_STATE ]);
2662
+ free_sum_bitarrays ();
2663
+ free_part_sum_bitarrays ();
2611
2664
return 3 ;
2612
2665
}
2613
2666
hardnested_stage = CHECK_1ST_BYTES | CHECK_2ND_BYTES ;
@@ -2617,6 +2670,12 @@ int mfnestedhard(uint8_t blockNo, uint8_t keyType, uint8_t *key, uint8_t trgBloc
2617
2670
} else { // acquire nonces.
2618
2671
uint16_t is_OK = acquire_nonces (blockNo , keyType , key , trgBlockNo , trgKeyType , nonce_file_write , slow );
2619
2672
if (is_OK != 0 ) {
2673
+ free_bitflip_bitarrays ();
2674
+ free_nonces_memory ();
2675
+ free_bitarray (all_bitflips_bitarray [ODD_STATE ]);
2676
+ free_bitarray (all_bitflips_bitarray [EVEN_STATE ]);
2677
+ free_sum_bitarrays ();
2678
+ free_part_sum_bitarrays ();
2620
2679
return is_OK ;
2621
2680
}
2622
2681
}
@@ -2646,10 +2705,11 @@ int mfnestedhard(uint8_t blockNo, uint8_t keyType, uint8_t *key, uint8_t trgBloc
2646
2705
for (statelist_t * sl = candidates ; sl != NULL ; sl = sl -> next ) {
2647
2706
maximum_states += (uint64_t )sl -> len [ODD_STATE ] * sl -> len [EVEN_STATE ];
2648
2707
}
2649
- printf ("Number of remaining possible keys: %" PRIu64 " (2^%1.1f)\n" , maximum_states , log (maximum_states )/log (2.0 ));
2708
+ // printf("Number of remaining possible keys: %" PRIu64 " (2^%1.1f)\n", maximum_states, log(maximum_states)/log(2.0));
2650
2709
best_first_bytes [0 ] = best_first_byte_smallest_bitarray ;
2651
2710
pre_XOR_nonces ();
2652
2711
prepare_bf_test_nonces (nonces , best_first_bytes [0 ]);
2712
+ hardnested_print_progress (num_acquired_nonces , "Starting brute force..." , expected_brute_force1 , 0 );
2653
2713
key_found = brute_force ();
2654
2714
free (candidates -> states [ODD_STATE ]);
2655
2715
free (candidates -> states [EVEN_STATE ]);
@@ -2669,6 +2729,7 @@ int mfnestedhard(uint8_t blockNo, uint8_t keyType, uint8_t *key, uint8_t trgBloc
2669
2729
// printf("Estimated remaining states: %" PRIu64 " (2^%1.1f)\n", nonces[best_first_bytes[0]].sum_a8_guess[j].num_states, log(nonces[best_first_bytes[0]].sum_a8_guess[j].num_states)/log(2.0));
2670
2730
generate_candidates (first_byte_Sum , nonces [best_first_bytes [0 ]].sum_a8_guess [j ].sum_a8_idx );
2671
2731
// printf("Time for generating key candidates list: %1.0f sec (%1.1f sec CPU)\n", difftime(time(NULL), start_time), (float)(msclock() - start_clock)/1000.0);
2732
+ hardnested_print_progress (num_acquired_nonces , "Starting brute force..." , expected_brute_force , 0 );
2672
2733
key_found = brute_force ();
2673
2734
free_statelist_cache ();
2674
2735
free_candidates_memory (candidates );
0 commit comments