hf mfdes sim: FormatPICC, and stop esave dropping the ATS

FormatPICC is answered now. It releases every application and every file and
hands the memory back, which is what makes it different from DeleteFile and
DeleteApplication: those leave a tombstone because a real card does not give the
memory back either, and this is the one command that reclaims. Here that means
rebuilding the tables around the surviving PICC entry and putting both frontiers
back where an empty card has them.

The PICC master key, its settings and the card identity are explicitly untouched
(M134034 9.4.6), so what is left is the same card with nothing on it. The same
section says the command always requires a preceding authentication with the
PICC master key, and unlike create and delete that is not relaxed by the free
create/delete key setting, so the check is on the session rather than on the
application's key settings. Measured against the simulation: 3 applications and
6400 bytes free become 0 applications and 8000 bytes free, and a bare
`90 FC 00 00 00` with no session is refused with 91 AE.

Separately, `hf mfdes esave` wrote the ATS as zeros. desfire_em_unpack() bounded
the copy with

    MIN(hdr->atslen, (uint8_t)sizeof(dump->card_info.ats))

and card_info.ats is 256 bytes, so the cast wrapped to 0 and the memcpy copied
nothing -- while ats_len was still set from the image, so the saved file carried
a correct length and eight zero bytes. Both copies here are now bounded by the
image's own array, which is the smaller of the two and the one a corrupt length
could run past.

This was invisible in simulation because SimulateIso14443aInit() falls back to
the tag type's built-in DESFire ATS when the image supplies none, and for this
card the two are identical. A reader saw a correct ATS from a card image that no
longer had one, and only a load-save round trip showed it.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
iceman1001andClaude Opus 5 committed 2026-09-15 20:49:01 +02:00
1 parent 7e0ce399f0
commit 480934bfd5
3 files changed
+75 -4

No files matched your search

+2
View File
@@ -3,6 +3,8 @@ 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]
- Added `hf mfdes sim` - FormatPICC is answered, releasing every application and file and handing the memory back, with the PICC master key and its settings kept (@iceman1001)
- Fixed `hf mfdes esave` - the ATS was saved as zeros, a `sizeof()` of the 256 byte destination cast to uint8_t wrapped to 0 and the copy was skipped while its length was still reported (@iceman1001)
- Added `hf mfdes sim` - DESFire EV1 simulation now answers authentication, secure messaging, file reads, writes and transactions from the card image in emulator memory (@iceman1001)
- Fixed `hf mfdes sim` - a 2TDEA key whose two halves match is handled as a single DES key, session key generation included, so the session key matches what a genuine PICC derives (@iceman1001)
- Fixed `hf mfdes sim` - the CMAC of a chained answer is taken over the whole transfer instead of being restarted on every 0xAF continuation (@iceman1001)
+65
View File
@@ -1135,6 +1135,57 @@ static uint16_t desfire_sim_dfname_record(const desfire_sim_state_t *st, uint8_t
return 0;
}
// FormatPICC: every application and every file goes, and the memory they held
// comes back. The PICC master key and its settings are explicitly untouched
// (M134034 9.4.6), and so is the card identity, so what is left is the same
// card with nothing on it.
//
// Everywhere else a delete only sets a tombstone, because a real card does not
// hand the memory back either. This is the one command that reclaims, and here
// that means rebuilding the tables around the surviving PICC entry and putting
// both frontiers back where an empty card has them.
static void desfire_sim_format(desfire_sim_state_t *st) {
uint8_t *base = desfire_sim_wbase();
desfire_em_hdr_t *hdr = (desfire_em_hdr_t *)base;
uint16_t oldkeys = hdr->keycount;
// With no applications but the PICC and no files at all, the key table
// starts directly behind the one application entry.
uint16_t newoff = hdr->app_off + sizeof(desfire_em_app_t);
desfire_em_key_t *dst = (desfire_em_key_t *)(base + newoff);
// Only the PICC's own keys survive, compacted to the front of the new
// table. Each one moves to an address at or below the one it came from --
// the table start only ever moves down and the write index never runs ahead
// of the read index -- so copying forward cannot clobber a key not yet read.
uint8_t keepcount = 0;
for (uint16_t i = 0; i < oldkeys; i++) {
if (st->keys[i].app != 0) {
continue;
}
memmove(&dst[keepcount], &st->keys[i], sizeof(desfire_em_key_t));
keepcount++;
}
hdr->appcount = 1;
hdr->filecount = 0;
hdr->keycount = keepcount;
hdr->file_off = newoff; // empty, so the key table starts here too
hdr->key_off = newoff;
hdr->tables_end = newoff + (keepcount * sizeof(desfire_em_key_t));
hdr->data_start = hdr->size;
hdr->reserved = 0;
// the tables moved, so the parsed view of them has to be taken again
desfire_sim_load(st);
}
// Act on a write once every frame of it has arrived. `buf`/`len` are the plain
// parameters, secure messaging already stripped.
static uint16_t desfire_sim_write_apply(desfire_sim_state_t *st, uint8_t cmd,
@@ -1726,6 +1777,20 @@ static uint16_t desfire_sim_command(desfire_sim_state_t *st, uint8_t cmd, const
case MFDES_LIMITED_CREDIT:
return desfire_sim_write_gather(st, cmd, in, inlen, out);
case MFDES_FORMAT_PICC: {
// "This command always requires a preceding authentication with the
// PICC master key" -- M134034 9.4.6. Unlike create and delete it is
// not relaxed by the free create/delete key setting, so this checks
// the session rather than the application's key settings.
if (st->authenticated == false || st->selected != 0 || st->auth_keyno != 0) {
return desfire_sim_status(out, MFDES_E_AUTHENTICATION_ERROR);
}
desfire_sim_format(st);
return desfire_sim_maced(st, out, MFDES_S_OPERATION_OK, NULL, 0);
}
case MFDES_COMMIT_TRANSACTION: {
if (st->selected == 0) {
+8 -4
View File
@@ -455,12 +455,16 @@ int desfire_em_unpack(const uint8_t *img, size_t imglen, desfire_dump_t *dump) {
memset(dump, 0, sizeof(desfire_dump_t));
dump->card_info.uidlen = hdr->uidlen;
memcpy(dump->card_info.uid, hdr->uid, MIN(hdr->uidlen, (uint8_t)sizeof(dump->card_info.uid)));
// Bound each copy by the image's own array, which is the smaller of the two
// and the one a corrupt length could run past. Not by the destination cast
// to uint8_t: card_info.ats is 256 bytes, that cast wrapped to 0, and the
// ATS was silently dropped while its length was still reported as 8.
dump->card_info.uidlen = MIN(hdr->uidlen, sizeof(hdr->uid));
memcpy(dump->card_info.uid, hdr->uid, dump->card_info.uidlen);
memcpy(dump->card_info.atqa, hdr->atqa, sizeof(hdr->atqa));
dump->card_info.sak = hdr->sak;
dump->card_info.ats_len = hdr->atslen;
memcpy(dump->card_info.ats, hdr->ats, MIN(hdr->atslen, (uint8_t)sizeof(dump->card_info.ats)));
dump->card_info.ats_len = MIN(hdr->atslen, sizeof(hdr->ats));
memcpy(dump->card_info.ats, hdr->ats, dump->card_info.ats_len);
dump->versionhwlen = hdr->versionhwlen;
memcpy(dump->versionhw, hdr->versionhw, sizeof(dump->versionhw));