Skip to content

Commit 01eb92d

Browse files
authored
Mifare Classic emulation fixes (#1566)
* Add fix for field * More small bugfixes * Clean up
1 parent ca23d0c commit 01eb92d

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

firmware/targets/f7/furi_hal/furi_hal_nfc.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,13 +341,13 @@ void furi_hal_nfc_listen_start(FuriHalNfcDevData* nfc_data) {
341341
if(nfc_data->uid_len == 4) {
342342
pt_memory[12] = nfc_data->sak & ~FURI_HAL_NFC_UID_INCOMPLETE;
343343
} else {
344-
pt_memory[12] = nfc_data->sak | FURI_HAL_NFC_UID_INCOMPLETE;
344+
pt_memory[12] = FURI_HAL_NFC_UID_INCOMPLETE;
345345
}
346346
pt_memory[13] = nfc_data->sak & ~FURI_HAL_NFC_UID_INCOMPLETE;
347347
pt_memory[14] = nfc_data->sak & ~FURI_HAL_NFC_UID_INCOMPLETE;
348348

349349
st25r3916WritePTMem(pt_memory, sizeof(pt_memory));
350-
// Go to sence
350+
// Go to sense
351351
st25r3916ExecuteCommand(ST25R3916_CMD_GOTO_SENSE);
352352
}
353353

@@ -507,6 +507,9 @@ static bool furi_hal_nfc_transparent_tx_rx(FuriHalNfcTxRxContext* tx_rx, uint16_
507507
uint8_t rxe = 0;
508508
uint32_t start = DWT->CYCCNT;
509509
while(true) {
510+
if(!rfalIsExtFieldOn()) {
511+
return false;
512+
}
510513
if(furi_hal_gpio_read(&gpio_nfc_irq_rfid_pull) == true) {
511514
st25r3916ReadRegister(ST25R3916_REG_IRQ_MAIN, &rxe);
512515
if(rxe & (1 << 4)) {

lib/nfc/protocols/mifare_classic.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,7 @@ bool mf_classic_emulator(MfClassicEmulator* emulator, FuriHalNfcTxRxContext* tx_
771771
// Read command
772772
while(!command_processed) {
773773
if(!is_encrypted) {
774+
crypto1_reset(&emulator->crypto);
774775
memcpy(plain_data, tx_rx->rx_data, tx_rx->rx_bits / 8);
775776
} else {
776777
if(!furi_hal_nfc_tx_rx(tx_rx, 300)) {
@@ -803,7 +804,7 @@ bool mf_classic_emulator(MfClassicEmulator* emulator, FuriHalNfcTxRxContext* tx_
803804
access_key = MfClassicKeyB;
804805
}
805806

806-
uint32_t nonce = prng_successor(DWT->CYCCNT, 32);
807+
uint32_t nonce = prng_successor(DWT->CYCCNT, 32) ^ 0xAA;
807808
uint8_t nt[4];
808809
uint8_t nt_keystream[4];
809810
nfc_util_num2bytes(nonce, 4, nt);
@@ -858,7 +859,7 @@ bool mf_classic_emulator(MfClassicEmulator* emulator, FuriHalNfcTxRxContext* tx_
858859
uint32_t cardRr = ar ^ crypto1_word(&emulator->crypto, 0, 0);
859860
if(cardRr != prng_successor(nonce, 64)) {
860861
FURI_LOG_T(TAG, "Wrong AUTH! %08X != %08X", cardRr, prng_successor(nonce, 64));
861-
// Don't send NACK, as tag don't send it
862+
// Don't send NACK, as the tag doesn't send it
862863
command_processed = true;
863864
break;
864865
}
@@ -897,7 +898,18 @@ bool mf_classic_emulator(MfClassicEmulator* emulator, FuriHalNfcTxRxContext* tx_
897898
} else {
898899
if(!mf_classic_is_allowed_access(
899900
emulator, block, access_key, MfClassicActionDataRead)) {
900-
memset(block_data, 0, 16);
901+
// Send NACK
902+
uint8_t nack = 0x04;
903+
if(is_encrypted) {
904+
mf_crypto1_encrypt(
905+
&emulator->crypto, NULL, &nack, 4, tx_rx->tx_data, tx_rx->tx_parity);
906+
} else {
907+
tx_rx->tx_data[0] = nack;
908+
}
909+
tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
910+
tx_rx->tx_bits = 4;
911+
furi_hal_nfc_tx_rx(tx_rx, 300);
912+
break;
901913
}
902914
}
903915
nfca_append_crc16(block_data, 16);

0 commit comments

Comments
 (0)