Skip to content

Commit fca1c9b

Browse files
committed
chg: 'hf mf mifare' - (deviceside) reset cycles when negative or too large
1 parent c725485 commit fca1c9b

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

Makefile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,8 @@ all clean: %: client/% bootrom/% armsrc/% recovery/% mfkey/% nonce2key/%
2727

2828
mfkey/%: FORCE
2929
$(MAKE) -C tools/mfkey $(patsubst mfkey/%,%,$@)
30-
3130
nonce2key/%: FORCE
3231
$(MAKE) -C tools/nonce2key $(patsubst nonce2key/%,%,$@)
33-
3432
bootrom/%: FORCE
3533
$(MAKE) -C bootrom $(patsubst bootrom/%,%,$@)
3634
armsrc/%: FORCE

armsrc/iso14443a.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2344,7 +2344,7 @@ void ReaderMifare(bool first_try, uint8_t block, uint8_t keytype ) {
23442344
// static variables here, is re-used in the next call
23452345
static uint32_t nt_attacked = 0;
23462346
static uint32_t sync_time = 0;
2347-
static uint32_t sync_cycles = 0;
2347+
static int32_t sync_cycles = 0;
23482348
static uint8_t par_low = 0;
23492349
static uint8_t mf_nr_ar3 = 0;
23502350

@@ -2455,15 +2455,23 @@ void ReaderMifare(bool first_try, uint8_t block, uint8_t keytype ) {
24552455

24562456
sync_cycles = (sync_cycles - nt_distance)/elapsed_prng_sequences;
24572457

2458-
if (sync_cycles <= 0)
2459-
sync_cycles += PRNG_SEQUENCE_LENGTH;
2458+
// no negative sync_cycles
2459+
if (sync_cycles <= 0) sync_cycles += PRNG_SEQUENCE_LENGTH;
2460+
2461+
// reset sync_cycles
2462+
if (sync_cycles > PRNG_SEQUENCE_LENGTH * 2 ) {
2463+
sync_cycles = PRNG_SEQUENCE_LENGTH;
2464+
sync_time = GetCountSspClk() & 0xfffffff8;
2465+
}
24602466

24612467
if (MF_DBGLEVEL >= 4)
24622468
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);
24632469

2470+
LED_B_OFF();
24642471
continue;
24652472
}
24662473
}
2474+
LED_B_OFF();
24672475

24682476
if ( (nt != nt_attacked) && nt_attacked) { // we somehow lost sync. Try to catch up again...
24692477

@@ -2503,7 +2511,7 @@ void ReaderMifare(bool first_try, uint8_t block, uint8_t keytype ) {
25032511
if (received_nack) {
25042512
catch_up_cycles = 8; // the PRNG is delayed by 8 cycles due to the NAC (4Bits = 0x05 encrypted) transfer
25052513

2506-
if (nt_diff == 0 && first_try)
2514+
if (nt_diff == 0)
25072515
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
25082516

25092517
par_list[nt_diff] = reflect8(par[0]);

0 commit comments

Comments
 (0)