]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - client/cmdhfmfhard.c
ADD; Some 7816 error codes
[proxmark3-svn] / client / cmdhfmfhard.c
index 4427978ae467dff6f74eb8a0ae5d82c63dda58df..eac783ff3903a6adcebf5fe88ec7348c234f9694 100644 (file)
@@ -88,7 +88,7 @@ typedef struct noncelist {
 } noncelist_t;
 
 
-static uint32_t cuid;
+static uint32_t cuid = 0;
 static noncelist_t nonces[256];
 static uint8_t best_first_bytes[256];
 static uint16_t first_byte_Sum = 0;
@@ -286,20 +286,18 @@ static double p_hypergeometric(uint16_t N, uint16_t K, uint16_t n, uint16_t k)
 static float sum_probability(uint16_t K, uint16_t n, uint16_t k)
 {
        const uint16_t N = 256;
-       
-       
 
-               if (k > K || p_K[K] == 0.0) return 0.0;
+       if (k > K || p_K[K] == 0.0) return 0.0;
 
-               double p_T_is_k_when_S_is_K = p_hypergeometric(N, K, n, k);
-               double p_S_is_K = p_K[K];
-               double p_T_is_k = 0;
-               for (uint16_t i = 0; i <= 256; i++) {
-                       if (p_K[i] != 0.0) {
-                               p_T_is_k += p_K[i] * p_hypergeometric(N, i, n, k);
-                       }
+       double p_T_is_k_when_S_is_K = p_hypergeometric(N, K, n, k);
+       double p_S_is_K = p_K[K];
+       double p_T_is_k = 0;
+       for (uint16_t i = 0; i <= 256; i++) {
+               if (p_K[i] != 0.0) {
+                       p_T_is_k += p_K[i] * p_hypergeometric(N, i, n, k);
                }
-               return(p_T_is_k_when_S_is_K * p_S_is_K / p_T_is_k);
+       }
+       return(p_T_is_k_when_S_is_K * p_S_is_K / p_T_is_k);
 }
 
                
@@ -514,7 +512,7 @@ static void sort_best_first_bytes(void)
        // determine how many are above the CONFIDENCE_THRESHOLD
        uint16_t num_good_nonces = 0;
        for (uint16_t i = 0; i < 256; i++) {
-               if (nonces[best_first_bytes[i]].Sum8_prob > CONFIDENCE_THRESHOLD) {
+               if (nonces[best_first_bytes[i]].Sum8_prob >= CONFIDENCE_THRESHOLD) {
                        ++num_good_nonces;
                }
        }
@@ -603,7 +601,7 @@ static uint16_t estimate_second_byte_sum(void)
 
        uint16_t num_good_nonces = 0;
        for (uint16_t i = 0; i < 256; i++) {
-               if (nonces[best_first_bytes[i]].Sum8_prob > CONFIDENCE_THRESHOLD) {
+               if (nonces[best_first_bytes[i]].Sum8_prob >= CONFIDENCE_THRESHOLD) {
                        ++num_good_nonces;
                }
        }
@@ -628,7 +626,8 @@ static int read_nonce_file(void)
        }
 
        PrintAndLog("Reading nonces from file nonces.bin...");
-       if (fread(read_buf, 1, 6, fnonces) == 0) {
+       size_t bytes_read = fread(read_buf, 1, 6, fnonces);
+       if ( bytes_read == 0) {
                PrintAndLog("File reading error.");
                fclose(fnonces);
                return 1;
@@ -687,7 +686,8 @@ static void Check_for_FilterFlipProperties(void)
 
 static void simulate_MFplus_RNG(uint32_t test_cuid, uint64_t test_key, uint32_t *nt_enc, uint8_t *par_enc)
 {
-       struct Crypto1State sim_cs;
+       struct Crypto1State sim_cs = {0, 0};
+//     sim_cs.odd = sim_cs.even = 0;
 
        // init cryptostate with key:
        for(int8_t i = 47; i > 0; i -= 2) {
@@ -751,11 +751,13 @@ static void simulate_acquire_nonces()
 
        } while (num_good_first_bytes < GOOD_BYTES_REQUIRED);
        
+       time1 = clock() - time1;
+       if ( time1 > 0 ) {
        PrintAndLog("Acquired a total of %d nonces in %1.1f seconds (%0.0f nonces/minute)", 
                total_num_nonces, 
-               ((float)clock()-time1)/CLOCKS_PER_SEC, 
-               total_num_nonces*60.0*CLOCKS_PER_SEC/((float)clock()-time1));
-
+               ((float)time1)/CLOCKS_PER_SEC, 
+               total_num_nonces * 60.0 * CLOCKS_PER_SEC/(float)time1);
+       }
        fprintf(fstats, "%d;%d;%d;%1.2f;", total_num_nonces, total_added_nonces, num_good_first_bytes, CONFIDENCE_THRESHOLD);
                
 }
@@ -858,8 +860,14 @@ static int acquire_nonces(uint8_t blockNo, uint8_t keyType, uint8_t *key, uint8_
                }
 
                if (!initialize) {
-                       if (!WaitForResponseTimeout(CMD_ACK, &resp, 3000)) return 1;
-                       if (resp.arg[0]) return resp.arg[0];  // error during nested_hard
+                       if (!WaitForResponseTimeout(CMD_ACK, &resp, 3000)) {
+                               fclose(fnonces);
+                               return 1;
+                       }
+                       if (resp.arg[0]) {
+                               fclose(fnonces);
+                               return resp.arg[0];  // error during nested_hard
+                       }
                }
 
                initialize = false;
@@ -871,11 +879,14 @@ static int acquire_nonces(uint8_t blockNo, uint8_t keyType, uint8_t *key, uint8_
                fclose(fnonces);
        }
        
+       time1 = clock() - time1;
+       if ( time1 > 0 ) {
        PrintAndLog("Acquired a total of %d nonces in %1.1f seconds (%0.0f nonces/minute)", 
                total_num_nonces, 
-               ((float)clock()-time1)/CLOCKS_PER_SEC, 
-               total_num_nonces*60.0*CLOCKS_PER_SEC/((float)clock()-time1));
-       
+               ((float)time1)/CLOCKS_PER_SEC, 
+               total_num_nonces * 60.0 * CLOCKS_PER_SEC/(float)time1
+               );
+       }
        return 0;
 }
 
@@ -1294,7 +1305,7 @@ static void generate_candidates(uint16_t sum_a0, uint16_t sum_a8)
                        }
                }
        }
-       printf("Number of possible keys with Sum(a0) = %d: %lld (2^%1.1f)\n", sum_a0, maximum_states, log(maximum_states)/log(2.0));
+       printf("Number of possible keys with Sum(a0) = %d: %"PRIu64" (2^%1.1f)\n", sum_a0, maximum_states, log(maximum_states)/log(2.0));
        
        init_statelist_cache();
        
@@ -1343,7 +1354,7 @@ static void generate_candidates(uint16_t sum_a0, uint16_t sum_a8)
        for (statelist_t *sl = candidates; sl != NULL; sl = sl->next) {
                maximum_states += (uint64_t)sl->len[ODD_STATE] * sl->len[EVEN_STATE];
        }
-       printf("Number of remaining possible keys: %lld (2^%1.1f)\n", maximum_states, log(maximum_states)/log(2.0));
+       printf("Number of remaining possible keys: %"PRIu64" (2^%1.1f)\n", maximum_states, log(maximum_states)/log(2.0));
        if (write_stats) {
                if (maximum_states != 0) {
                        fprintf(fstats, "%1.1f;", log(maximum_states)/log(2.0));
@@ -1429,47 +1440,48 @@ int mfnestedhard(uint8_t blockNo, uint8_t keyType, uint8_t *key, uint8_t trgBloc
                fclose(fstats);
        } else {
                init_nonce_memory();
-       if (nonce_file_read) {          // use pre-acquired data from file nonces.bin
-               if (read_nonce_file() != 0) {
-                       return 3;
-               }
-               Check_for_FilterFlipProperties();
-               num_good_first_bytes = MIN(estimate_second_byte_sum(), GOOD_BYTES_REQUIRED);
-       } else {                                        // acquire nonces.
-               uint16_t is_OK = acquire_nonces(blockNo, keyType, key, trgBlockNo, trgKeyType, nonce_file_write, slow);
-               if (is_OK != 0) {
-                       return is_OK;
+               if (nonce_file_read) {          // use pre-acquired data from file nonces.bin
+                       if (read_nonce_file() != 0) {
+                               return 3;
+                       }
+                       Check_for_FilterFlipProperties();
+                       num_good_first_bytes = MIN(estimate_second_byte_sum(), GOOD_BYTES_REQUIRED);
+               } else {                                        // acquire nonces.
+                       uint16_t is_OK = acquire_nonces(blockNo, keyType, key, trgBlockNo, trgKeyType, nonce_file_write, slow);
+                       if (is_OK != 0) {
+                               return is_OK;
+                       }
                }
-       }
 
-       Tests();
-
-       PrintAndLog("");
-       PrintAndLog("Sum(a0) = %d", first_byte_Sum);
-       // PrintAndLog("Best 10 first bytes: %02x, %02x, %02x, %02x, %02x, %02x, %02x, %02x, %02x, %02x",
-               // best_first_bytes[0],
-               // best_first_bytes[1],
-               // best_first_bytes[2],
-               // best_first_bytes[3],
-               // best_first_bytes[4],
-               // best_first_bytes[5],
-               // best_first_bytes[6],
-               // best_first_bytes[7],
-               // best_first_bytes[8],
-               // best_first_bytes[9]  );
-       PrintAndLog("Number of first bytes with confidence > %2.1f%%: %d", CONFIDENCE_THRESHOLD*100.0, num_good_first_bytes);
-
-       time_t start_time = clock();
-       generate_candidates(first_byte_Sum, nonces[best_first_bytes[0]].Sum8_guess);
-       PrintAndLog("Time for generating key candidates list: %1.0f seconds", (float)(clock() - start_time)/CLOCKS_PER_SEC);
+               Tests();
+
+               PrintAndLog("");
+               PrintAndLog("Sum(a0) = %d", first_byte_Sum);
+               // PrintAndLog("Best 10 first bytes: %02x, %02x, %02x, %02x, %02x, %02x, %02x, %02x, %02x, %02x",
+                       // best_first_bytes[0],
+                       // best_first_bytes[1],
+                       // best_first_bytes[2],
+                       // best_first_bytes[3],
+                       // best_first_bytes[4],
+                       // best_first_bytes[5],
+                       // best_first_bytes[6],
+                       // best_first_bytes[7],
+                       // best_first_bytes[8],
+                       // best_first_bytes[9]  );
+               PrintAndLog("Number of first bytes with confidence > %2.1f%%: %d", CONFIDENCE_THRESHOLD*100.0, num_good_first_bytes);
+
+               clock_t time1 = clock();
+               generate_candidates(first_byte_Sum, nonces[best_first_bytes[0]].Sum8_guess);
+               time1 = clock() - time1;
+               if ( time1 > 0 )
+                       PrintAndLog("Time for generating key candidates list: %1.0f seconds", ((float)time1)/CLOCKS_PER_SEC);
        
-       brute_force();
+               brute_force();
                free_nonces_memory();
                free_statelist_cache();
                free_candidates_memory(candidates);
                candidates = NULL;
-       }
-       
+       }       
        return 0;
 }
 
Impressum, Datenschutz