chg: 'hf mf mifare' - (deviceside) reset cycles when negative or too large

This commit is contained in:
iceman1001 2018-02-05 16:47:10 +01:00
parent c72548562e
commit fca1c9b7cf
2 changed files with 12 additions and 6 deletions

View file

@ -27,10 +27,8 @@ all clean: %: client/% bootrom/% armsrc/% recovery/% mfkey/% nonce2key/%
mfkey/%: FORCE
$(MAKE) -C tools/mfkey $(patsubst mfkey/%,%,$@)
nonce2key/%: FORCE
$(MAKE) -C tools/nonce2key $(patsubst nonce2key/%,%,$@)
bootrom/%: FORCE
$(MAKE) -C bootrom $(patsubst bootrom/%,%,$@)
armsrc/%: FORCE

View file

@ -2344,7 +2344,7 @@ void ReaderMifare(bool first_try, uint8_t block, uint8_t keytype ) {
// static variables here, is re-used in the next call
static uint32_t nt_attacked = 0;
static uint32_t sync_time = 0;
static uint32_t sync_cycles = 0;
static int32_t sync_cycles = 0;
static uint8_t par_low = 0;
static uint8_t mf_nr_ar3 = 0;
@ -2455,15 +2455,23 @@ void ReaderMifare(bool first_try, uint8_t block, uint8_t keytype ) {
sync_cycles = (sync_cycles - nt_distance)/elapsed_prng_sequences;
if (sync_cycles <= 0)
sync_cycles += PRNG_SEQUENCE_LENGTH;
// no negative sync_cycles
if (sync_cycles <= 0) sync_cycles += PRNG_SEQUENCE_LENGTH;
// reset sync_cycles
if (sync_cycles > PRNG_SEQUENCE_LENGTH * 2 ) {
sync_cycles = PRNG_SEQUENCE_LENGTH;
sync_time = GetCountSspClk() & 0xfffffff8;
}
if (MF_DBGLEVEL >= 4)
Dbprintf("calibrating in cycle %d. nt_distance=%d, elapsed_prng_sequences=%d, new sync_cycles: %d\n", i, nt_distance, elapsed_prng_sequences, sync_cycles);
LED_B_OFF();
continue;
}
}
LED_B_OFF();
if ( (nt != nt_attacked) && nt_attacked) { // we somehow lost sync. Try to catch up again...
@ -2503,7 +2511,7 @@ void ReaderMifare(bool first_try, uint8_t block, uint8_t keytype ) {
if (received_nack) {
catch_up_cycles = 8; // the PRNG is delayed by 8 cycles due to the NAC (4Bits = 0x05 encrypted) transfer
if (nt_diff == 0 && first_try)
if (nt_diff == 0)
par_low = par[0] & 0xE0; // there is no need to check all parities for other nt_diff. Parity Bits for mf_nr_ar[0..2] won't change
par_list[nt_diff] = reflect8(par[0]);