hf mfdes sim: authentication, secure messaging, reads, writes and transactions

The DESFire simulation now plays the EV1 protocol rather than just the
activation and a handful of unauthenticated queries. A reader authenticates,
enumerates, reads and writes files, and commits or aborts transactions against
the card image `hf mfdes eload` put in emulator memory.

Authentication covers 0x0A, 0x1A and 0xAA with DES, 2TDEA, 3K3DES and AES keys
taken from the image. A key the image only holds a version for still refuses to
authenticate rather than authenticating with zeros.

Secure messaging follows the file's communication mode, with the rule that the
mode only applies when a key right matching the authenticated key granted the
operation -- access granted by the free-access right runs plain whatever the
file settings say. Responses are MACed or enciphered accordingly, and the
session CMAC is taken over every command and response in order so the two sides'
IVs stay together even where neither puts a MAC on the wire.

Reads cover ReadData, ReadRecords, GetValue and GetCardUID. Writes cover
WriteData, WriteRecord, UpdateRecord, Credit, Debit and LimitedCredit, plus
CommitTransaction, AbortTransaction and ClearRecordFile. Backup data, value and
record files write into their shadow region and only move across on commit, so
an abort really does discard.

Three fixes were needed to interoperate with the client, and all three share a
shape worth naming: the authentication handshake still succeeded, because it
runs on the original key, and only the traffic afterwards was wrong.

1. A DES or 2TDEA key is stored as 16 bytes and the key itself decides which
   cipher the PICC uses -- if the second half equals the first it is a single
   DES key, and that governs session key generation too (M134034 8.1). The
   all-zero default key is the common case. Deriving a 2K3DES session key from
   it left the card MACing under a key the reader did not have. Confirmed by
   decrypting a captured GetCardUID response offline: under the session key the
   reader derives it yields the UID and a valid CRC32, under the other it does
   not.

2. Session keys are built here rather than through Desfire_session_key_new(),
   whose 3K3DES branch clears the low bit of the first eight bytes. Those bits
   are key version, which a session key does not have, and the reader keeps them.

3. The reader's 0xAF continuation is not a command, it continues one. Giving it
   its own CMAC restarted the running calculation halfway through a chained
   answer, so every chained response longer than one frame carried a wrong MAC
   while single-frame answers verified fine.

The sample card in traces/mifare is rekeyed from all zeros to the sequence
01 02 .. 10, extended to .. 18 for 3TDEA. An all-zero key has matching halves,
so it exercises only the degenerate path of fix 1 above and hides the bug;
distinct halves surface a wrong derivation on the first MACed frame.

tools/desfire_sim_test.sh loads an image, simulates it, drives the second
Proxmark3 at it and reports. It stops the simulation the way the client does, a
newline on its stdin, through a fifo held open for the run rather than a fixed
timer -- a run that outlives its timer leaves the device simulating. A command
counts as passing only if it prints something that says it worked: card errors,
client side argument rejections and MAC or CRC complaints all fail, because
several of those print a plausible result line as well and matching on the
result alone reports passes that never happened.

Tested on two RDV4s, one simulating and one reading: activation, info,
application and file enumeration, all three key types, enciphered GetCardUID,
plain and MACed reads up to 256 bytes over chained frames, and writes verified
by reading the image back out with `hf mfdes esave`.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
iceman1001andClaude Opus 5 committed 2026-09-15 20:40:32 +02:00
1 parent da3400ff04
commit 4dc412e403
5 files changed
+1751 -244

No files matched your search

+14 -4
View File
@@ -152,16 +152,26 @@ batch `B9 0C 17 49 70`, week 27 / 2017, hardware `04010101001A05`, software
### Keys
Every key on this card is the default all-zero one. The PICC master key is
2TDEA `00000000000000000000000000000000`; each application uses its own
algorithm, and all of its keys are that algorithm's all-zero key.
Every key on this card is the counting sequence `01 02 03 .. 10`, extended to
`.. 18` for the 24 byte 3TDEA keys. The PICC master key is 2TDEA
`0102030405060708090A0B0C0D0E0F10`; each application uses its own algorithm,
and all of its keys are that algorithm's length of the same sequence.
Deliberately not the all-zero key, and not a key whose two halves match. A
16 byte DES/2TDEA key string whose second half equals the first is handled as a
single DES key by the PICC -- during authentication *and* session key
generation (M134034 8.1) -- and the all-zero key is the common case of that. A
card image keyed with it exercises only the degenerate path: an implementation
that derives the wrong session key still authenticates, and only fails later on
the first MACed or enciphered frame. This sequence has distinct halves, so a
wrong derivation shows up immediately.
The `mfdes v2` key files carry **application** keys only -- that format has no
place for the PICC master key, because `hf mfdes chk` only walks the application
list and takes the PICC key from `-k`. Hand it in explicitly:
```
hf mfdes dump -n 0 -t 2TDEA -k 00000000000000000000000000000000 --keys traces/mifare/hf-mfdes-ev1-8k-test-keys.json
hf mfdes dump -n 0 -t 2TDEA -k 0102030405060708090A0B0C0D0E0F10 --keys traces/mifare/hf-mfdes-ev1-8k-test-keys.json
```
Note that every key is also stored inside the dump itself, under each
+49 -29
View File
@@ -1,34 +1,54 @@
{
"Created": "proxmark3",
"FileType": "mfdes v2",
"Card": {
"UID": "04268512A25680",
"SAK": "20",
"ATQA": "4403",
"ATS": "06757781028002F0"
},
"Applications": {
"010203": {
"AES": {
"0": { "Key": "00000000000000000000000000000000" },
"1": { "Key": "00000000000000000000000000000000" },
"2": { "Key": "00000000000000000000000000000000" }
}
"Created": "proxmark3",
"FileType": "mfdes v2",
"Card": {
"UID": "04268512A25680",
"SAK": "20",
"ATQA": "4403",
"ATS": "06757781028002F0"
},
"Applications": {
"010203": {
"AES": {
"0": {
"Key": "0102030405060708090A0B0C0D0E0F10"
},
"112233": {
"2TDEA": {
"0": { "Key": "00000000000000000000000000000000" },
"1": { "Key": "00000000000000000000000000000000" }
}
"1": {
"Key": "0102030405060708090A0B0C0D0E0F10"
},
"AABBCC": {
"3TDEA": {
"0": { "Key": "000000000000000000000000000000000000000000000000" },
"1": { "Key": "000000000000000000000000000000000000000000000000" },
"2": { "Key": "000000000000000000000000000000000000000000000000" },
"3": { "Key": "000000000000000000000000000000000000000000000000" },
"4": { "Key": "000000000000000000000000000000000000000000000000" }
}
"2": {
"Key": "0102030405060708090A0B0C0D0E0F10"
}
}
},
"112233": {
"2TDEA": {
"0": {
"Key": "0102030405060708090A0B0C0D0E0F10"
},
"1": {
"Key": "0102030405060708090A0B0C0D0E0F10"
}
}
},
"AABBCC": {
"3TDEA": {
"0": {
"Key": "0102030405060708090A0B0C0D0E0F101112131415161718"
},
"1": {
"Key": "0102030405060708090A0B0C0D0E0F101112131415161718"
},
"2": {
"Key": "0102030405060708090A0B0C0D0E0F101112131415161718"
},
"3": {
"Key": "0102030405060708090A0B0C0D0E0F101112131415161718"
},
"4": {
"Key": "0102030405060708090A0B0C0D0E0F101112131415161718"
}
}
}
}
}
}
+175 -168
View File
@@ -1,172 +1,179 @@
{
"Created": "proxmark3",
"FileType": "mfdes v1",
"Version": 1,
"Card": {
"UID": "04268512A25680",
"ATQA": "4403",
"SAK": "20",
"ATS": "06757781028002F0",
"VersionHW": "04010101001A05",
"VersionSW": "04010101041A05",
"VersionProd": "04268512A25680B90C1749702717",
"FreeMem": 6400
},
"Applications": {
"000000": {
"KeySettings": "0F",
"NumKeysRaw": "01",
"NumKeys": 1,
"KeyType": "2tdea",
"Authenticated": true,
"Keys": {
"0": {
"Version": "00",
"Key": "00000000000000000000000000000000"
}
}
},
"010203": {
"ISOFileID": "1234",
"DFName": "7465737431",
"KeySettings": "0B",
"NumKeysRaw": "83",
"NumKeys": 3,
"KeyType": "aes",
"Authenticated": true,
"Keys": {
"0": {
"Version": "00",
"Key": "00000000000000000000000000000000"
},
"1": {
"Version": "00"
},
"2": {
"Version": "00"
}
},
"Files": {
"01": {
"Type": "standard",
"TypeRaw": "00",
"CommMode": "plain",
"AccessRights": "EEEE",
"ISOFileID": "0001",
"FileSize": 256,
"Read": true,
"Data": "50726F786D61726B3320444553466972652045563120384B20746573742063617264000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
},
"02": {
"Type": "backup",
"TypeRaw": "01",
"CommMode": "mac",
"AccessRights": "1200",
"ISOFileID": "0002",
"FileSize": 128,
"Read": true,
"Data": "6261636B75702066696C6520636F6E74656E7400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
}
}
},
"112233": {
"ISOFileID": "2345",
"DFName": "7465737432",
"KeySettings": "0B",
"NumKeysRaw": "02",
"NumKeys": 2,
"KeyType": "2tdea",
"Authenticated": true,
"Keys": {
"0": {
"Version": "00",
"Key": "00000000000000000000000000000000"
},
"1": {
"Version": "00"
}
},
"Files": {
"00": {
"Type": "value",
"TypeRaw": "02",
"CommMode": "plain",
"AccessRights": "EE00",
"LowerLimit": 0,
"UpperLimit": 10000,
"LimitedCredit": "00",
"Value": 1000,
"Read": true
},
"01": {
"Type": "linear_record",
"TypeRaw": "03",
"CommMode": "plain",
"AccessRights": "EE00",
"ISOFileID": "0011",
"RecordSize": 16,
"MaxRecords": 8,
"CurRecords": 2,
"Read": true,
"Records": {
"0": "6C696E65617220726563202330310000",
"1": "6C696E65617220726563202330320000"
}
}
}
},
"AABBCC": {
"ISOFileID": "3456",
"DFName": "7465737433",
"KeySettings": "0B",
"NumKeysRaw": "45",
"NumKeys": 5,
"KeyType": "3tdea",
"Authenticated": true,
"Keys": {
"0": {
"Version": "00",
"Key": "000000000000000000000000000000000000000000000000"
},
"1": {
"Version": "00"
},
"2": {
"Version": "00"
},
"3": {
"Version": "00"
},
"4": {
"Version": "00"
}
},
"Files": {
"05": {
"Type": "cyclic_record",
"TypeRaw": "04",
"CommMode": "plain",
"AccessRights": "EE00",
"ISOFileID": "0055",
"RecordSize": 24,
"MaxRecords": 4,
"CurRecords": 1,
"Read": true,
"Records": {
"0": "6379636C6963207265636F72642023310000000000000000"
}
},
"03": {
"Type": "standard",
"TypeRaw": "00",
"CommMode": "plain",
"AccessRights": "EEEE",
"ISOFileID": "0033",
"FileSize": 64,
"Read": true,
"Data": "667265652072656164207374642066696C6500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
}
}
"Created": "proxmark3",
"FileType": "mfdes v1",
"Version": 1,
"Card": {
"UID": "04268512A25680",
"ATQA": "4403",
"SAK": "20",
"ATS": "06757781028002F0",
"VersionHW": "04010101001A05",
"VersionSW": "04010101041A05",
"VersionProd": "04268512A25680B90C1749702717",
"FreeMem": 6400
},
"Applications": {
"000000": {
"KeySettings": "0F",
"NumKeysRaw": "01",
"NumKeys": 1,
"KeyType": "2tdea",
"Authenticated": true,
"Keys": {
"0": {
"Version": "00",
"Key": "0102030405060708090A0B0C0D0E0F10"
}
}
},
"010203": {
"ISOFileID": "1234",
"DFName": "7465737431",
"KeySettings": "0B",
"NumKeysRaw": "83",
"NumKeys": 3,
"KeyType": "aes",
"Authenticated": true,
"Keys": {
"0": {
"Version": "00",
"Key": "0102030405060708090A0B0C0D0E0F10"
},
"1": {
"Version": "00",
"Key": "0102030405060708090A0B0C0D0E0F10"
},
"2": {
"Version": "00",
"Key": "0102030405060708090A0B0C0D0E0F10"
}
},
"Files": {
"01": {
"Type": "standard",
"TypeRaw": "00",
"CommMode": "plain",
"AccessRights": "EEEE",
"ISOFileID": "0001",
"FileSize": 256,
"Read": true,
"Data": "50726F786D61726B3320444553466972652045563120384B20746573742063617264000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
},
"02": {
"Type": "backup",
"TypeRaw": "01",
"CommMode": "mac",
"AccessRights": "1200",
"ISOFileID": "0002",
"FileSize": 128,
"Read": true,
"Data": "6261636B75702066696C6520636F6E74656E7400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
}
}
},
"112233": {
"ISOFileID": "2345",
"DFName": "7465737432",
"KeySettings": "0B",
"NumKeysRaw": "02",
"NumKeys": 2,
"KeyType": "2tdea",
"Authenticated": true,
"Keys": {
"0": {
"Version": "00",
"Key": "0102030405060708090A0B0C0D0E0F10"
},
"1": {
"Version": "00",
"Key": "0102030405060708090A0B0C0D0E0F10"
}
},
"Files": {
"00": {
"Type": "value",
"TypeRaw": "02",
"CommMode": "plain",
"AccessRights": "EE00",
"LowerLimit": 0,
"UpperLimit": 10000,
"LimitedCredit": "00",
"Value": 1000,
"Read": true
},
"01": {
"Type": "linear_record",
"TypeRaw": "03",
"CommMode": "plain",
"AccessRights": "EE00",
"ISOFileID": "0011",
"RecordSize": 16,
"MaxRecords": 8,
"CurRecords": 2,
"Read": true,
"Records": {
"0": "6C696E65617220726563202330310000",
"1": "6C696E65617220726563202330320000"
}
}
}
},
"AABBCC": {
"ISOFileID": "3456",
"DFName": "7465737433",
"KeySettings": "0B",
"NumKeysRaw": "45",
"NumKeys": 5,
"KeyType": "3tdea",
"Authenticated": true,
"Keys": {
"0": {
"Version": "00",
"Key": "0102030405060708090A0B0C0D0E0F101112131415161718"
},
"1": {
"Version": "00",
"Key": "0102030405060708090A0B0C0D0E0F101112131415161718"
},
"2": {
"Version": "00",
"Key": "0102030405060708090A0B0C0D0E0F101112131415161718"
},
"3": {
"Version": "00",
"Key": "0102030405060708090A0B0C0D0E0F101112131415161718"
},
"4": {
"Version": "00",
"Key": "0102030405060708090A0B0C0D0E0F101112131415161718"
}
},
"Files": {
"05": {
"Type": "cyclic_record",
"TypeRaw": "04",
"CommMode": "plain",
"AccessRights": "EE00",
"ISOFileID": "0055",
"RecordSize": 24,
"MaxRecords": 4,
"CurRecords": 1,
"Read": true,
"Records": {
"0": "6379636C6963207265636F72642023310000000000000000"
}
},
"03": {
"Type": "standard",
"TypeRaw": "00",
"CommMode": "plain",
"AccessRights": "EEEE",
"ISOFileID": "0033",
"FileSize": 64,
"Read": true,
"Data": "667265652072656164207374642066696C6500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
}
}
}
}
}