hf mf hardnested: fix the nonce reply length, 9 bytes per pair not 4 per nonce

Thanks @TheArchitect0880 for pointing it out and suggested a first fix.

MifareAcquireEncryptedNonces packs two 4 byte encrypted nonces plus one
byte holding both their encrypted parity nibbles into every entry, but
the reply declared num_nonces * 4 bytes. The client walks that buffer 9
bytes at a time, so on RDV4 it read 612 bytes out of a 544 byte payload
and handed roughly 15 of every 136 nonces to add_nonce() from stale
packet buffer content. Those fake nonces went into the .bin nonce file
too.

Count pairs instead of bytes. num_nonces now reports whole pairs only,
so a button abort or a static nonce bailout part way through a pair
drops the dangling nonce rather than shipping a half built entry whose
parity nibble was never filled in.

MFC_NONCE_PAIR_SIZE and MFC_MAX_NONCE_PAIRS document the layout next to
mf_nonces_resp_t so the 9 vs 4 confusion cannot come back, and the
client now refuses a reply too short for the nonce count it carries.

Both acquisition functions collect straight into the reply buffer rather
than a second PM3_CMD_DATA_SIZE stack array, which halves the stack used
per call. MifareAcquireNonces also returned isOK = 2 on button press,
which is not a PM3_* status; that is PM3_EOPABORTED now.

Co-Authored-By: Claude Opus 5 (1M context)
This commit is contained in:
iceman1001andClaude Opus 5 (1M context) committed 2026-09-12 10:44:00 +02:00
1 parent 5ca15e4587
commit 6341f40b4c
4 files changed
+53 -24

No files matched your search

+12 -3
View File
@@ -1733,9 +1733,18 @@ static int acquire_nonces(uint8_t blockNo, uint8_t keyType, uint8_t *key, uint8_
if (initialize == false) {
const mf_nonces_resp_t *nresp = (const mf_nonces_resp_t *)resp.data.asBytes;
uint16_t num_sampled_nonces = nresp->num_nonces;
uint16_t num_sampled_nonces = nresp->num_nonces & ~1; // nonces come in pairs
const uint8_t *bufp = nresp->nonces;
if (resp.length < sizeof(mf_nonces_resp_t) + ((num_sampled_nonces / 2) * MFC_NONCE_PAIR_SIZE)) {
PrintAndLogEx(FAILED, "Truncated nonce reply, got %u bytes for %u nonces", resp.length, num_sampled_nonces);
if (nonce_file_write) {
fclose(fnonces);
}
DropField();
return PM3_ESOFT;
}
for (uint16_t i = 0; i < num_sampled_nonces; i += 2) {
uint32_t nt_enc1 = bytes_to_num(bufp, 4);
uint32_t nt_enc2 = bytes_to_num(bufp + 4, 4);
@@ -1760,10 +1769,10 @@ static int acquire_nonces(uint8_t blockNo, uint8_t keyType, uint8_t *key, uint8_
num_acquired_nonces += add_res;
if (nonce_file_write) {
fwrite(bufp, 1, 9, fnonces);
fwrite(bufp, 1, MFC_NONCE_PAIR_SIZE, fnonces);
fflush(fnonces);
}
bufp += 9;
bufp += MFC_NONCE_PAIR_SIZE;
}
//total_num_nonces += num_sampled_nonces;