desfire simulation to support native and iso7816 wrapping

This commit is contained in:
iceman1001 committed 2026-09-14 22:19:38 +02:00
1 parent 59cd76905f
commit 06ebe87cae
2 files changed
+44

No files matched your search

+39
View File
@@ -413,6 +413,45 @@ uint16_t desfire_sim_apdu(const uint8_t *in, uint16_t inlen, uint8_t *out) {
return 0;
}
// Two framings carry the same commands and a reader may use either.
//
// native <cmd> <data...> -> <status> <data...>
// ISO 7816 90 <cmd> 00 00 Lc <data> Le -> <data...> 91 <status>
//
// The proxmark client defaults to the wrapped form, so a simulation that
// only understands the native one answers ILLEGAL_COMMAND_CODE to
// everything -- it reads the 0x90 class byte as the command.
if (in[0] == DESFIRE_SIM_ISO7816_CLA && inlen >= 5) {
uint8_t cmd = in[1];
// 5 bytes is CLA INS P1 P2 Le, no data. Longer means in[4] is Lc and
// the data follows, with Le after it.
uint8_t lc = 0;
const uint8_t *data = NULL;
if (inlen > 5) {
lc = in[4];
data = in + 5;
if ((uint16_t)5 + lc > inlen) {
lc = inlen - 5; // truncated frame, use what arrived
}
}
uint8_t native[DESFIRE_SIM_MAX_RESP] = {0};
uint16_t n = desfire_sim_command(&s_st, cmd, data, lc, native);
if (n == 0) {
return 0;
}
// status leads in the native answer and trails in the wrapped one
if (n > 1) {
memcpy(out, native + 1, n - 1);
}
out[n - 1] = DESFIRE_SIM_ISO7816_SW1;
out[n] = native[0];
return n + 1;
}
return desfire_sim_command(&s_st, in[0], in + 1, inlen - 1, out);
}
+5
View File
@@ -25,6 +25,11 @@
#include "common.h"
// ISO 7816 wrapping of the DESFire command set: class byte on the way in,
// first status byte on the way back.
#define DESFIRE_SIM_ISO7816_CLA 0x90
#define DESFIRE_SIM_ISO7816_SW1 0x91
// Largest answer built before the 14443-4 prologue and CRC are added.
// GetApplicationIDs on a full PICC is 28 * 3 bytes plus a status byte.
#define DESFIRE_SIM_MAX_RESP 128