From 7772aa92bb363da251ca9f2a7a1920167a725a8c Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Thu, 12 Nov 2026 12:51:56 +0700 Subject: [PATCH] ExchangeRAW14a: keep the block number in step, recover lost frames `hf mfp chk` could report a near empty key table on a card whose keys are all there, and DESFire in the native command set could fail an authentication with the right key. Two ISO/IEC 14443-4 problems, both in `ExchangeRAW14a()`, which MIFARE Plus and the DESFire native command set both sit on. The block number was toggled where the I-block is built, not once the PICC had answered it. 7.5.3 toggles on a completed exchange, so every exchange that did not complete left the client one step ahead of the card, and from there every following command carried a block number the card read as a retransmission. Nothing but a reselect recovered it, so a single lost frame spoiled the rest of the sequence. That is what turned a frame loss of a few percent into a 43% authentication failure rate on a link that drops frames. The second one is that a lost answer ended the exchange. 7.5.6 calls that a transmission error and gives the PCD R(NAK) to ask the PICC for its last block again; an R(ACK) carrying the other block number back means the PICC never got ours, so the block goes out once more. `iso14_apdu()` got this in eab887680 and the raw path had nothing, which is why the same card behaves so differently through the two transports. Bounded at two tries. A silent device is left alone: waiting again there only stacks up another full timeout. Measured on a PM5 whose HF link drops frames, same card and position throughout. 30 authentications with the correct AES key via `hf mfdes auth -c native`: 13 failed before, 6 with the block number fixed, 0 with both. `hf mfdes auth -c niso` was already at 0 and stays there. `hf mfp chk -s 0 -e 31` found 0, 1 and 1 of 15 AES keys on three runs before and 15 on five runs after - it takes longer because it now finishes the search instead of abandoning it. Behaviour change: commands on these paths retry a lost frame, so one that used to fail fast can now take up to two extra exchanges. A wrong key is still a wrong key - verified that all-FF reports AUTHENTICATION_ERROR and not the no-answer code - and a channel the card does not implement still fails on the first frame, so neither is mistaken for something to retry. Not addressed here, both known and separate: - `hf mfp chk` abandons every remaining sector on one PM3_ERFTRANS and its caller drops that return, so the key table comes out short with a success exit code. Same conflation as #3787. - the 14443a reader transmit in armsrc/iso14443a.c hands the frame to the FPGA and returns without waiting for it to go out, unlike iso14443b.c and iso15693.c. Adding the wait measurably cuts the frame loss but wants a regression pass of its own. Co-Authored-By: Claude Opus 5 --- CHANGELOG.md | 1 + client/src/cmdhf14a.c | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c443f4b7..756c0ba9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file. This project uses the changelog in accordance with [keepchangelog](http://keepachangelog.com/). Please use this to write notable changes, which is not the same as git commit log... ## [unreleased][unreleased] +- Fixed `hf mfp`, `hf mfdes` native command set - the ISO 14443-4 block number no longer goes out of step with the card when a frame is lost, and a lost frame is asked for again instead of failing the command; on a lossy link `hf mfp chk` found 0-1 of 15 AES keys before, now 15 (@iceman1001) - Fixed `hf mf brute` - options read at their own positions (`--1k` ran a Mini), `--emu` reports keys and dumps not transferred to emulator memory (@matrix) - Fixed `hf mf darkside` - with more candidates than fit one check, every candidate is tested and none is read past the list (@matrix) - Fixed `hf mf nested` - tests every key candidate, follows cards whose nonce distance varies, single sector retries (@matrix) diff --git a/client/src/cmdhf14a.c b/client/src/cmdhf14a.c index b599214db..451d3a1c6 100644 --- a/client/src/cmdhf14a.c +++ b/client/src/cmdhf14a.c @@ -1315,6 +1315,9 @@ int CmdHF14ASniff(const char *Cmd) { return PM3_SUCCESS; } +// ISO/IEC 14443-4 7.5.6 leaves the retransmission count to the PCD +#define ISO14A_MAX_RETRANS 2 + int ExchangeRAW14a(uint8_t *datain, int datainlen, bool activateField, bool leaveSignalON, uint8_t *dataout, int maxdataoutlen, int *dataoutlen, bool silentMode) { uint16_t cmdc = 0; @@ -1333,11 +1336,19 @@ int ExchangeRAW14a(uint8_t *datain, int datainlen, bool activateField, bool leav cmdc |= ISO14A_NO_DISCONNECT; } + // ISO/IEC 14443-4 7.5.3: the block number toggles once the PICC has answered + // the block, not when it is built. Toggling here left it out of step with the + // card for every exchange that did not complete, and nothing but a reselect + // put it back uint8_t data[PM3_CMD_DATA_SIZE] = { 0x0a | gs_frames_num, 0x00 }; - gs_frames_num ^= 1; int min = MIN((int)(g_conn.max_cmd_data_size - 2), (datainlen & 0x1FF)); memcpy(&data[2], datain, min); + + // R(NAK) for this block, same CID as the I-block above + uint8_t nak[2] = { (uint8_t)(0xBA | gs_frames_num), 0x00 }; + uint8_t attempt = 0; + SendIso14aReader(ISO14A_RAW | ISO14A_APPEND_CRC | cmdc, data, min + 2); uint8_t *recv; @@ -1349,12 +1360,27 @@ resend: int iLen = rlen_1245; if (iLen == 0) { + // ISO/IEC 14443-4 7.5.6: a lost answer is a transmission error, not a + // dead exchange. R(NAK) asks the PICC for its last block again + if (attempt++ < ISO14A_MAX_RETRANS) { + SendIso14aReader(ISO14A_RAW | ISO14A_APPEND_CRC | ISO14A_NO_DISCONNECT | cmdc, nak, sizeof(nak)); + goto resend; + } if (silentMode == false) { PrintAndLogEx(ERR, "No card response"); } return PM3_ECARDEXCHANGE; } + // R(ACK) carrying the other block number: the PICC never got our block, + // so it goes out once more + if ((recv[0] & 0xF6) == 0xA2 && (recv[0] & 0x01) != (data[0] & 0x01)) { + if (attempt++ < ISO14A_MAX_RETRANS) { + SendIso14aReader(ISO14A_RAW | ISO14A_APPEND_CRC | cmdc, data, min + 2); + goto resend; + } + } + *dataoutlen = iLen - 2; if (*dataoutlen < 0) { *dataoutlen = 0; @@ -1391,6 +1417,9 @@ resend: return PM3_ELENGTH; } + // the PICC answered this block, so the exchange is complete + gs_frames_num ^= 1; + memcpy(dataout, &recv[2], *dataoutlen); // CRC Check @@ -1402,6 +1431,8 @@ resend: } } else { + // the device itself went quiet, not the card. Asking again would only + // stack up another full wait if (silentMode == false) { PrintAndLogEx(ERR, "Reply timeout."); }