DESFire: fix value file operations and improve MAC mode compatibility

- Add auto-detection fallback for MAC mode in value operations
  When MAC mode fails with length errors, automatically retry with
  plain mode for better compatibility across different card types

- Fix MAC transmission behavior for value operations
  Remove CREDIT, LIMITED_CREDIT, and DEBIT from EV1D40TransmitMAC
  array to match real card behavior and prevent authentication issues

- Change default algorithm from DES to 2TDEA
  Real DESFire cards seem to use 2TDEA by default, improving
  out-of-the-box compatibility with factory cards

- Update help text for value commands to follow client patterns
  Standardize "Crypt algo (deft: 2TDEA)" format for consistency

- Add online test suite for DESFire value operations
  New pm3_online_tests.sh script validates value file creation,
  credit/debit operations in both plain and MAC modes with real cards
This commit is contained in:
Mistial Developer committed 2025-07-19 08:39:47 -04:00
1 parent ae8b71197d
commit e1598cd620
4 files changed
+165 -6

No files matched your search

+3 -3
View File
@@ -463,7 +463,7 @@ static void swap24(uint8_t *data) {
// default parameters
static uint8_t defaultKeyNum = 0;
static DesfireCryptoAlgorithm defaultAlgoId = T_DES;
static DesfireCryptoAlgorithm defaultAlgoId = T_3DES; // Real DESFire cards seem to use 2TDEA by default
static uint8_t defaultKey[DESFIRE_MAX_KEY_SIZE] = {0};
static int defaultKdfAlgo = MFDES_KDF_ALGO_NONE;
static int defaultKdfInputLen = 0;
@@ -4034,7 +4034,7 @@ static int CmdHF14ADesCreateValueFile(const char *Cmd) {
arg_lit0("a", "apdu", "Show APDU requests and responses"),
arg_lit0("v", "verbose", "Verbose output"),
arg_int0("n", "keyno", "<dec>", "Key number"),
arg_str0("t", "algo", "<DES|2TDEA|3TDEA|AES>", "Crypt algo"),
arg_str0("t", "algo", "<DES|2TDEA|3TDEA|AES>", "Crypt algo (deft: 2TDEA)"),
arg_str0("k", "key", "<hex>", "Key for authenticate (HEX 8(DES), 16(2TDEA or AES) or 24(3TDEA) bytes)"),
arg_str0(NULL, "kdf", "<none|AN10922|gallagher>", "Key Derivation Function (KDF)"),
arg_str0("i", "kdfi", "<hex>", "KDF input (1-31 hex bytes)"),
@@ -4474,7 +4474,7 @@ static int CmdHF14ADesValueOperations(const char *Cmd) {
arg_lit0("a", "apdu", "Show APDU requests and responses"),
arg_lit0("v", "verbose", "Verbose output"),
arg_int0("n", "keyno", "<dec>", "Key number"),
arg_str0("t", "algo", "<DES|2TDEA|3TDEA|AES>", "Crypt algo"),
arg_str0("t", "algo", "<DES|2TDEA|3TDEA|AES>", "Crypt algo (deft: 2TDEA)"),
arg_str0("k", "key", "<hex>", "Key for authenticate (HEX 8(DES), 16(2TDEA or AES) or 24(3TDEA) bytes)"),
arg_str0(NULL, "kdf", "<none|AN10922|gallagher>", "Key Derivation Function (KDF)"),
arg_str0("i", "kdfi", "<hex>", "KDF input (1-31 hex bytes)"),
+14
View File
@@ -2240,6 +2240,20 @@ int DesfireValueFileOperations(DesfireContext_t *dctx, uint8_t fid, uint8_t oper
int res = DesfireCommand(dctx, operation, data, datalen, resp, &resplen, -1);
// Auto-detection fallback: if MAC mode fails with length error, retry with plain mode
if ((res == 0x7E || res == -20) && dctx->commMode == DCMMACed) {
PrintAndLogEx(INFO, "MAC mode failed with length error, retrying with plain mode");
DesfireCommunicationMode original_mode = dctx->commMode;
dctx->commMode = DCMPlain;
memset(resp, 0, sizeof(resp));
resplen = 0;
res = DesfireCommand(dctx, operation, data, datalen, resp, &resplen, -1);
// Restore original mode for future commands
dctx->commMode = original_mode;
}
if (resplen == 4 && value) {
*value = MemLeToUint4byte(resp);
}
-3
View File
@@ -219,9 +219,6 @@ static uint8_t DesfireGetCmdHeaderLen(uint8_t cmd) {
static const uint8_t EV1D40TransmitMAC[] = {
MFDES_WRITE_DATA,
MFDES_CREDIT,
MFDES_LIMITED_CREDIT,
MFDES_DEBIT,
MFDES_WRITE_RECORD,
MFDES_UPDATE_RECORD,
MFDES_COMMIT_READER_ID,