From 4d4dd323548b2d3b2e9e66c791bd05f419c2de71 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 15 Sep 2026 21:58:57 +0200 Subject: [PATCH] hf mfdes sim: SetConfiguration The simulation answers SetConfiguration. The option byte travels in the clear and the data behind it is enciphered with a CRC32, the same shape ChangeKey uses, and card level master key authentication is required (M134034 9.4.9). Option 0x00, the configuration byte, is kept in the card image header. Both bits it defines are one way on a card -- the spec says "cannot be reset" of each -- so they are only ever set, never cleared, and a reader that sends a zero byte afterwards gets an OK and no change, which is what the silicon does. bit 0 disables FormatPICC, and that command now refuses from then on. bit 1 switches anticollision to a random id: a 4 byte id whose first byte is the 0x08 random tag and whose other three are the random number, with a single cascade level (M134034 6.5). The real UID is then only reachable through GetCardUID, which is the reason that command exists. A card draws the number at RF reset; the nearest thing here is the start of a simulation, since that is when the anticollision answers are built, so it is stable across activations within one run and different across runs -- measured 08 01 9A C9 four times in a row, then 08 01 BB 96 and 08 01 DC 26 and 08 01 FC 8E on three restarts. Simulating that needed the 4 byte case adding to the UID length the simulation accepts, which previously took only 7 and 10 and refused to start otherwise. Option 0x02 replaces the ATS. It lands in the image, so it takes effect the next time the simulation starts rather than mid-run, because the ATS is one of the answers precompiled before the reader is listened to. Option 0x01, the default key new applications are created with, is refused with 91 9E rather than accepted and ignored. Storing it needs two fields the card image does not have, and adding them moves every table in it. A reader that sets a default key and then finds new applications keyed with zeros is worse off than one told the option is not there. Co-Authored-By: Claude Opus 5 (1M context) --- CHANGELOG.md | 1 + armsrc/desfiresim.c | 120 +++++++++++++++++++++++++++++++++++++++++-- include/desfire_em.h | 8 ++- 3 files changed, 124 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index de633cb01..cccce2e18 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] +- Added `hf mfdes sim` - SetConfiguration is answered, so a reader can disable FormatPICC, switch the card to a random id, or give it a user defined ATS (@iceman1001) - Added `hf mfdes sim` - ReadSignature is answered for a card image of a generation that has one, and refused the way a genuine EV1 refuses it (@iceman1001) - Added `hf mfdes sim` - GetKeyVersion is answered, so a reader can read the version of any key without knowing the key itself (@iceman1001) - Added `hf mfdes sim` - ChangeKeySettings is answered, enciphered and master key only, and clearing the configuration changeable bit is one way as it is on a card (@iceman1001) diff --git a/armsrc/desfiresim.c b/armsrc/desfiresim.c index f65fb5192..026d80fc8 100644 --- a/armsrc/desfiresim.c +++ b/armsrc/desfiresim.c @@ -787,7 +787,8 @@ static uint8_t desfire_sim_hdrlen(uint8_t cmd) { case MFDES_GET_VALUE: case MFDES_CHANGE_FILE_SETTINGS: case MFDES_CHANGE_KEY: - return 1; // file number, or key number, only + case MFDES_CHANGE_CONFIGURATION: + return 1; // file, key or option number only default: return 0; } @@ -826,6 +827,7 @@ static bool desfire_sim_cmd_unwraps_own(uint8_t cmd) { return (desfire_sim_cmd_is_write(cmd) || cmd == MFDES_CHANGE_FILE_SETTINGS || cmd == MFDES_CHANGE_KEY_SETTINGS || + cmd == MFDES_CHANGE_CONFIGURATION || cmd == MFDES_CHANGE_KEY); } @@ -1906,6 +1908,77 @@ static uint16_t desfire_sim_command(desfire_sim_state_t *st, uint8_t cmd, const return desfire_sim_maced(st, out, MFDES_S_OPERATION_OK, buf, sizeof(buf)); } + case MFDES_CHANGE_CONFIGURATION: { + + // "Master key authentication on card level needs to be performed + // prior to the SetConfiguration command" -- M134034 9.4.9 + if (st->authenticated == false || st->selected != 0 || st->auth_keyno != 0) { + return desfire_sim_status(out, MFDES_E_AUTHENTICATION_ERROR); + } + + if (inlen < 1) { + return desfire_sim_status(out, MFDES_E_LENGTH); + } + + // the option byte travels in the clear, the data behind it does not + uint8_t buf[DESFIRE_SIM_WRITE_MAX] = {0}; + if (inlen > sizeof(buf)) { + return desfire_sim_status(out, MFDES_E_LENGTH); + } + + memcpy(buf, in, inlen); + uint16_t len = inlen; + + if (desfire_sim_unwrap(st, cmd, DESFIRE_SIM_COMM_FULL, buf, &len) == false) { + desfire_sim_auth_clear(st); + return desfire_sim_status(out, MFDES_E_INTEGRITY_ERROR); + } + + if (len < 2) { + return desfire_sim_status(out, MFDES_E_LENGTH); + } + + desfire_em_hdr_t *w = (desfire_em_hdr_t *)hdr; + + switch (buf[0]) { + + case 0x00: { + // the configuration byte. Both bits it defines are one way + // on a card -- "cannot be reset" -- so they are only ever + // set here, never cleared. + w->flags |= (buf[1] & (DESFIRE_EM_PICC_NO_FORMAT | DESFIRE_EM_PICC_RANDOM_UID)); + break; + } + + case 0x02: { + // the user defined ATS, TL first and without the CRC the + // card appends. The spec checks its length and nothing + // else, so neither does this. + uint16_t n = len - 1; + if (n == 0 || n > sizeof(w->ats)) { + return desfire_sim_status(out, MFDES_E_LENGTH); + } + + memset(w->ats, 0, sizeof(w->ats)); + memcpy(w->ats, buf + 1, n); + w->atslen = n; + break; + } + + case 0x01: + // the default key new applications are created with. Storing + // it needs two fields the card image does not have, and adding + // them moves every table in it, so this is refused rather than + // silently ignored -- a reader that sets a default key and then + // finds new applications keyed with zeros is worse off than one + // told the option is not there. + default: + return desfire_sim_status(out, MFDES_E_PARAMETER_ERROR); + } + + return desfire_sim_maced(st, out, MFDES_S_OPERATION_OK, NULL, 0); + } + case MFDES_READSIG: { // one byte selects which signature, and EV1 only has the one @@ -2693,6 +2766,11 @@ static uint16_t desfire_sim_command(desfire_sim_state_t *st, uint8_t cmd, const return desfire_sim_status(out, MFDES_E_AUTHENTICATION_ERROR); } + // SetConfiguration can switch this command off for good + if (hdr->flags & DESFIRE_EM_PICC_NO_FORMAT) { + return desfire_sim_status(out, MFDES_E_PERMISSION_DENIED); + } + desfire_sim_format(st); return desfire_sim_maced(st, out, MFDES_S_OPERATION_OK, NULL, 0); } @@ -2785,6 +2863,9 @@ static bool desfire_sim_init(void) { return true; } +// The three random id bytes, drawn when a simulation starts. +static uint8_t s_sim_randomid[3]; + static void desfire_sim_identity(uint8_t *uid, uint8_t *uidlen, uint8_t *atqa, uint8_t *sak, uint8_t *ats, uint8_t *atslen) { if (s_ready == false) { @@ -2793,17 +2874,37 @@ static void desfire_sim_identity(uint8_t *uid, uint8_t *uidlen, uint8_t *atqa, u const desfire_em_hdr_t *hdr = s_st.hdr; - if (uid && hdr->uidlen <= sizeof(hdr->uid)) { - memcpy(uid, hdr->uid, hdr->uidlen); + // With random ID switched on, anticollision shows a 4 byte id whose first + // byte is the 0x08 random tag and whose other three are the random number, + // and a single cascade level is all that is used (M134034 6.5). The real + // UID is then only reachable through GetCardUID, which is what that command + // is for. A card draws the number at RF reset; the nearest thing here is + // the start of a simulation, since that is when the answers are built. + bool randomid = (hdr->flags & DESFIRE_EM_PICC_RANDOM_UID) != 0; + + if (uid) { + if (randomid) { + uid[0] = 0x08; + uid[1] = s_sim_randomid[0]; + uid[2] = s_sim_randomid[1]; + uid[3] = s_sim_randomid[2]; + } else if (hdr->uidlen <= sizeof(hdr->uid)) { + memcpy(uid, hdr->uid, hdr->uidlen); + } } if (uidlen) { - *uidlen = hdr->uidlen; + *uidlen = randomid ? 4 : hdr->uidlen; } if (atqa) { atqa[0] = hdr->atqa[0]; atqa[1] = hdr->atqa[1]; + + // the UID size bits of ATQA byte 0 have to follow the id actually shown + if (randomid) { + atqa[0] &= ~0xC0; + } } if (sak) { @@ -2961,6 +3062,13 @@ void SimulateDesfireTag(void) { // keep emulator memory, that is where eload put the card image BigBuf_free_keep_EM(); + // A card draws its random id at RF reset; a simulation draws it here, which + // is the moment the anticollision answers are built. + uint32_t seed = GetTickCount(); + s_sim_randomid[0] = (seed >> 16) & 0xFF; + s_sim_randomid[1] = (seed >> 8) & 0xFF; + s_sim_randomid[2] = seed & 0xFF; + if (desfire_sim_init() == false) { Dbprintf("No DESFire card image in emulator memory"); Dbprintf("Load one with " _YELLOW_("`hf mfdes eload -f `")); @@ -2978,6 +3086,10 @@ void SimulateDesfireTag(void) { uint16_t flags = FLAG_ATS_IN_DATA; switch (uidlen) { + case 4: + // a random id is 4 bytes and uses a single cascade level + flags |= FLAG_4B_UID_IN_DATA; + break; case 7: flags |= FLAG_7B_UID_IN_DATA; break; diff --git a/include/desfire_em.h b/include/desfire_em.h index 64d3be604..821ad6a17 100644 --- a/include/desfire_em.h +++ b/include/desfire_em.h @@ -65,6 +65,12 @@ // 64. Rounding the same way keeps GetFreeMem card-shaped. Note the rounding is // invisible to a reader otherwise -- GetFileSettings reports the size the file // was created with, also measured. +// PICC configuration, set by SetConfiguration option 0x00 and kept in hdr.flags. +// Both are one way on a card -- "cannot be reset" -- and the bit positions are +// the config byte's own, so the byte is stored as it arrives. +#define DESFIRE_EM_PICC_NO_FORMAT (1 << 0) // FormatPICC refused from here on +#define DESFIRE_EM_PICC_RANDOM_UID (1 << 1) // anticollision shows a random id + #define DESFIRE_EM_GRANULE 32 #define DESFIRE_EM_ROUNDUP(x) (((x) + (DESFIRE_EM_GRANULE - 1)) & ~(DESFIRE_EM_GRANULE - 1)) @@ -101,7 +107,7 @@ typedef struct { uint32_t magic; // DESFIRE_EM_MAGIC uint8_t layout; // DESFIRE_EM_LAYOUT_VERSION uint8_t generation; // desfire_em_gen_t - uint8_t flags; // RFU + uint8_t flags; // desfire_em_picc_flags_t uint8_t rfu0; // card identity, straight from the dump