Skip to content

Commit

Permalink
2.2.0 - Remove a lot of useless code from HardNestedSolver
Browse files Browse the repository at this point in the history
  • Loading branch information
AloneLiberty committed May 1, 2023
1 parent ce6dfa8 commit 32f58df
Show file tree
Hide file tree
Showing 31 changed files with 63 additions and 8,277 deletions.
69 changes: 2 additions & 67 deletions HardNestedSolver/cmdhfmfhard.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
#include "hardnested/hardnested_bf_core.h"
#include "hardnested/hardnested_bitarray_core.h"
#include "pm3/ui.h"
#include "pm3/fileutils.h"
#include "pm3/commonutil.h"
#include "pm3/util_posix.h"
#include "hardnested/tables.h"
Expand Down Expand Up @@ -984,71 +983,6 @@ static void estimate_sum_a8(void) {
}
}

static int read_nonce_file(char *filename) {

if (filename == NULL) {
PrintAndLogEx(WARNING, "Filename is NULL");
return PM3_EINVARG;
}
FILE *fnonces = NULL;
char progress_text[80] = "";
uint8_t read_buf[9];

num_acquired_nonces = 0;
if ((fnonces = fopen(filename, "rb")) == NULL) {
PrintAndLogEx(WARNING, "Could not open file "
_YELLOW_("%s"), filename);
return PM3_EFILE;
}

snprintf(progress_text, 80, "Reading nonces from file "
_YELLOW_("%s"), filename);
hardnested_print_progress(0, progress_text, (float) (1LL << 47), 0);
size_t bytes_read = fread(read_buf, 1, 6, fnonces);
if (bytes_read != 6) {
PrintAndLogEx(ERR, "File reading error.");
fclose(fnonces);
return PM3_EFILE;
}
cuid = bytes_to_num(read_buf, 4);
uint8_t trgBlockNo = bytes_to_num(read_buf + 4, 1);
uint8_t trgKeyType = bytes_to_num(read_buf + 5, 1);

bytes_read = fread(read_buf, 1, 9, fnonces);
while (bytes_read == 9) {
uint32_t nt_enc1 = bytes_to_num(read_buf, 4);
uint32_t nt_enc2 = bytes_to_num(read_buf + 4, 4);
uint8_t par_enc = bytes_to_num(read_buf + 8, 1);
add_nonce(nt_enc1, par_enc >> 4);
add_nonce(nt_enc2, par_enc & 0x0f);
num_acquired_nonces += 2;
bytes_read = fread(read_buf, 1, 9, fnonces);
}
fclose(fnonces);

char progress_string[80];
snprintf(progress_string, sizeof(progress_string), "Read %u nonces from file. cuid = %08x", num_acquired_nonces,
cuid);
hardnested_print_progress(num_acquired_nonces, progress_string, (float) (1LL << 47), 0);
snprintf(progress_string, sizeof(progress_string), "Target Block=%d, Keytype=%c", trgBlockNo,
trgKeyType == 0 ? 'A' : 'B');
hardnested_print_progress(num_acquired_nonces, progress_string, (float) (1LL << 47), 0);

bool got_match = false;
for (uint8_t i = 0; i < NUM_SUMS; i++) {
if (first_byte_Sum == sums[i]) {
first_byte_Sum = i;
got_match = true;
break;
}
}
if (got_match == false) {
PrintAndLogEx(FAILED, "No match for the First_Byte_Sum (%u), is the card a genuine MFC Ev1? ", first_byte_Sum);
return PM3_ESOFT;
}
return 0;
}

static noncelistentry_t *SearchFor2ndByte(uint8_t b1, uint8_t b2) {
noncelistentry_t *p = nonces[b1].first;
while (p != NULL) {
Expand Down Expand Up @@ -1262,6 +1196,7 @@ static int simulate_acquire_nonces(uint32_t uid, char* path) {
num_acquired_nonces += add_nonce(nt_enc, par_enc);
total_num_nonces++;
}

if (num_acquired_nonces % 256 == 0) {
hardnested_print_progress(num_acquired_nonces, "Loading nonces from file", brute_force_depth, 0);
}
Expand All @@ -1283,7 +1218,7 @@ static int simulate_acquire_nonces(uint32_t uid, char* path) {
if (got_match == false) {
PrintAndLogEx(FAILED, "No match for the First_Byte_Sum (%u), is the card a genuine MFC Ev1? ",
first_byte_Sum);
return PM3_ESOFT;
return -1;
}

hardnested_stage |= CHECK_2ND_BYTES;
Expand Down
39 changes: 1 addition & 38 deletions HardNestedSolver/hardnested/hardnested_bruteforce.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,12 @@ THE SOFTWARE.
#include <string.h>
#include <stdlib.h>

//#include "../pm3/common.h"
//#include "../cmdhfmfhard.h"
#include "hardnested_bf_core.h"
#include "../pm3/ui.h"
//#include "../pm3/util.h"
#include "../pm3/util_posix.h"
#include "../crapto1.h"
#include "../parity.h"
#include "../cmdhfmfhard.h"
//#include "../pm3/fileutils.h"
//#include "../pm3/pm3_cmd.h"
#include "hardnested_benchmark_data.h"

#define NUM_BRUTE_FORCE_THREADS (num_CPUs())
Expand All @@ -77,8 +72,7 @@ THE SOFTWARE.
#endif
#define DEFAULT_BRUTE_FORCE_RATE (120000000.0) // if benchmark doesn't succeed
#define TEST_BENCH_SIZE (6000) // number of odd and even states for brute force benchmark
#define TEST_BENCH_FILENAME "hardnested_bf_bench_data.bin"
//#define WRITE_BENCH_FILE

#ifdef _MSC_VER
#include <Windows.h>
#include <share.h>
Expand Down Expand Up @@ -309,37 +303,6 @@ void prepare_bf_test_nonces(noncelist_t *nonces, uint8_t best_first_byte) {
}
}


#if defined (WRITE_BENCH_FILE)
static void write_benchfile(statelist_t *candidates) {

PrintAndLogEx(NORMAL, "Writing brute force benchmark data in " RESOURCES_SUBDIR " subdirectory...");
FILE *benchfile = fopen(RESOURCES_SUBDIR TEST_BENCH_FILENAME, "wb");
if (benchfile == NULL) {
PrintAndLogEx(ERR, "Can't write " RESOURCES_SUBDIR TEST_BENCH_FILENAME", abort!");
return;
}
fwrite(&nonces_to_bruteforce, 1, sizeof(nonces_to_bruteforce), benchfile);
for (uint32_t i = 0; i < nonces_to_bruteforce; i++) {
fwrite(&(bf_test_nonce[i]), 1, sizeof(bf_test_nonce[i]), benchfile);
fwrite(&(bf_test_nonce_par[i]), 1, sizeof(bf_test_nonce_par[i]), benchfile);
}
uint32_t num_states = MIN(candidates->len[EVEN_STATE], TEST_BENCH_SIZE);
fwrite(&num_states, 1, sizeof(num_states), benchfile);
for (uint32_t i = 0; i < num_states; i++) {
fwrite(&(candidates->states[EVEN_STATE][i]), 1, sizeof(uint32_t), benchfile);
}
num_states = MIN(candidates->len[ODD_STATE], TEST_BENCH_SIZE);
fwrite(&num_states, 1, sizeof(num_states), benchfile);
for (uint32_t i = 0; i < num_states; i++) {
fwrite(&(candidates->states[ODD_STATE][i]), 1, sizeof(uint32_t), benchfile);
}
fclose(benchfile);
PrintAndLogEx(NORMAL, "Done");
}
#endif


bool brute_force_bs(float *bf_rate, statelist_t *candidates, uint32_t cuid, uint32_t num_acquired_nonces, uint64_t maximum_states, noncelist_t *nonces, uint8_t *best_first_bytes, uint64_t *found_key) {
#if defined (WRITE_BENCH_FILE)
write_benchfile(candidates);
Expand Down
Loading

0 comments on commit 32f58df

Please sign in to comment.