diff --git a/CHANGELOG.md b/CHANGELOG.md index 99a9c5243..2297dca8d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/armsrc/desfiresim.c b/armsrc/desfiresim.c index 4d2ac0215..bc2792f78 100644 --- a/armsrc/desfiresim.c +++ b/armsrc/desfiresim.c @@ -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) { diff --git a/client/src/mifare/desfireem.c b/client/src/mifare/desfireem.c index 4449318bc..7c0964408 100644 --- a/client/src/mifare/desfireem.c +++ b/client/src/mifare/desfireem.c @@ -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));