Skip to content

Commit 5cd6773

Browse files
committed
Restore point for many incremental updates, bug fixes and documentation changes
1 parent 126189a commit 5cd6773

38 files changed

+340
-662
lines changed

Doc/DESFireSupportReadme.md

+10-6
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,14 @@ We can modify the remaining tag header information emulated by the tag as follow
2323
DF_SETHDR=ATS xxxxxxxxxx
2424
DF_SETHDR=ATQA xxxx
2525
DF_SETHDR=ManuID xx
26-
DF_SETHDR=HardwareVersion mmMM
27-
DF_SETHDR=SoftwareVersion mmMM
28-
DF_SETHDR=BatchNumber xxxxxxxxxx
29-
DF_SETHDR=ProductionDate WWYY
26+
DF_SETHDR=Type xx
27+
DF_SETHDR=Subtype xx
28+
DF_SETHDR=HwProtoType xx
29+
DF_SETHDR=HwVers mmMM
30+
DF_SETHDR=SwProtoType xx
31+
DF_SETHDR=SwVers mmMM
32+
DF_SETHDR=BatchNo xxxxxxxxxx
33+
DF_SETHDR=ProdDate WWYY
3034
```
3135

3236
##### Examples:
@@ -246,8 +250,8 @@ DF_ENCMODE=AES:CBC
246250

247251
| Instruction | Cmd Byte | Description | Testing Status | Implementation Notes |
248252
| :--- | :----: | :----: | :----: | :-- |
249-
| CMD_AUTHENTICATE | 0x0A | Authenticate legacy | :ballot_box_with_check: | Works with the ``-DDESFIRE_QUICK_DES_CRYPTO`` Makefile ``SETTINGS`` compiler flag set to enable "quicker" DES crypto. |
250-
| CMD_AUTHENTICATE_ISO | 0x1A | ISO / 3DES auth | :question: | This implementation is too slow! Need hardware support for 3DES crypto? |
253+
| CMD_AUTHENTICATE | 0x0A | Authenticate legacy | :ballot_box_with_check: | |
254+
| CMD_AUTHENTICATE_ISO | 0x1A | ISO / 3DES auth | :ballot_box_with_check: | |
251255
| CMD_AUTHENTICATE_AES | 0xAA | Standard AES auth | :ballot_box_with_check: | |
252256
| CMD_AUTHENTICATE_EV2_FIRST | 0x71 | Newer spec auth variant | :x: | |
253257
| CMD_AUTHENTICATE_EV2_NONFIRST | 0x77 | Newer spec auth variant | :x: | See page 32 of AN12343.pdf |

Firmware/Chameleon-Mini/Application/DESFire/DESFireApplicationDirectory.c

+8-9
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,6 @@ void WriteFileSettings(uint8_t AppSlot, uint8_t FileIndex, DESFireFileTypeSettin
481481
}
482482
SIZET fileTypeSettingsBlockId = GetAppProperty(DESFIRE_APP_FILES_PTR_BLOCK_ID, AppSlot);
483483
SIZET fileTypeSettingsAddresses[DESFIRE_MAX_FILES];
484-
// TODO: ???
485484
ReadBlockBytes(fileTypeSettingsAddresses, fileTypeSettingsBlockId, 2 * DESFIRE_MAX_FILES);
486485
WriteBlockBytes(FileSettings, fileTypeSettingsAddresses[FileIndex], sizeof(DESFireFileTypeSettings));
487486
memcpy(&(SelectedFile.File), FileSettings, sizeof(DESFireFileTypeSettings));
@@ -494,10 +493,11 @@ void WriteFileSettings(uint8_t AppSlot, uint8_t FileIndex, DESFireFileTypeSettin
494493
uint8_t LookupAppSlot(const DESFireAidType Aid) {
495494
uint8_t Slot;
496495
for (Slot = 0; Slot < DESFIRE_MAX_SLOTS; ++Slot) {
497-
if (!memcmp(AppDir.AppIds[Slot], Aid, DESFIRE_AID_SIZE))
498-
break;
496+
if (!memcmp(AppDir.AppIds[Slot], Aid, DESFIRE_AID_SIZE)) {
497+
return Slot;
498+
}
499499
}
500-
return Slot;
500+
return DESFIRE_MAX_SLOTS;
501501
}
502502

503503
void SelectAppBySlot(uint8_t AppSlot) {
@@ -578,7 +578,7 @@ uint16_t CreateApp(const DESFireAidType Aid, uint8_t KeyCount, uint8_t KeySettin
578578
}
579579
/* Update the next free slot */
580580
for (FreeSlot = 1; FreeSlot < DESFIRE_MAX_SLOTS; ++FreeSlot) {
581-
if ((AppDir.AppIds[FreeSlot][0] | AppDir.AppIds[FreeSlot][1] | AppDir.AppIds[FreeSlot][2]) == 0)
581+
if (FreeSlot != Slot && (AppDir.AppIds[FreeSlot][0] | AppDir.AppIds[FreeSlot][1] | AppDir.AppIds[FreeSlot][2]) == 0)
582582
break;
583583
}
584584

@@ -603,7 +603,7 @@ uint16_t CreateApp(const DESFireAidType Aid, uint8_t KeyCount, uint8_t KeySettin
603603
} else {
604604
BYTE keySettings[DESFIRE_MAX_KEYS];
605605
memset(keySettings, KeySettings, DESFIRE_MAX_KEYS);
606-
keySettings[0] = KeySettings; // NEEDED ???
606+
keySettings[0] = KeySettings;
607607
WriteBlockBytes(keySettings, appCacheData.KeySettings, DESFIRE_MAX_KEYS);
608608
}
609609
appCacheData.FileNumbersArrayMap = AllocateBlocks(APP_CACHE_FILE_NUMBERS_HASHMAP_BLOCK_SIZE);
@@ -710,8 +710,7 @@ uint16_t DeleteApp(const DESFireAidType Aid) {
710710
}
711711

712712
void GetApplicationIdsSetup(void) {
713-
/* Skip the PICC application */
714-
TransferState.GetApplicationIds.NextIndex = 1;
713+
TransferState.GetApplicationIds.NextIndex = 0;
715714
}
716715

717716
TransferStatus GetApplicationIdsTransfer(uint8_t *Buffer) {
@@ -724,7 +723,7 @@ TransferStatus GetApplicationIdsTransfer(uint8_t *Buffer) {
724723
continue;
725724
}
726725
/* If it won't fit -- remember and return */
727-
if (Status.BytesProcessed >= TERMINAL_BUFFER_SIZE) {
726+
if (Status.BytesProcessed >= TERMINAL_BUFFER_SIZE - 20) {
728727
TransferState.GetApplicationIds.NextIndex = EntryIndex;
729728
Status.IsComplete = false;
730729
return Status;

Firmware/Chameleon-Mini/Application/DESFire/DESFireChameleonTerminal.c

+28-4
Original file line numberDiff line numberDiff line change
@@ -77,27 +77,51 @@ CommandStatusIdType CommandDESFireSetHeaderProperty(char *OutParam, const char *
7777
} else {
7878
Picc.ManufacturerID = propSpecBytes[0];
7979
}
80-
} else if (!strcasecmp_P(hdrPropSpecStr, PSTR("HardwareVersion"))) {
80+
} else if (!strcasecmp_P(hdrPropSpecStr, PSTR("Type"))) {
81+
if (dataByteCount != 1) {
82+
StatusError = 1;
83+
} else {
84+
Picc.TagType = propSpecBytes[0];
85+
}
86+
} else if (!strcasecmp_P(hdrPropSpecStr, PSTR("Subtype"))) {
87+
if (dataByteCount != 1) {
88+
StatusError = 1;
89+
} else {
90+
Picc.TagSubtype = propSpecBytes[0];
91+
}
92+
} else if (!strcasecmp_P(hdrPropSpecStr, PSTR("HWProtoType"))) {
93+
if (dataByteCount != 1) {
94+
StatusError = 1;
95+
} else {
96+
Picc.HwProtocolType = propSpecBytes[0];
97+
}
98+
} else if (!strcasecmp_P(hdrPropSpecStr, PSTR("HWVers"))) {
8199
if (dataByteCount != 2) {
82100
StatusError = 1;
83101
} else {
84102
Picc.HwVersionMajor = propSpecBytes[0];
85103
Picc.HwVersionMinor = propSpecBytes[1];
86104
}
87-
} else if (!strcasecmp_P(hdrPropSpecStr, PSTR("SoftwareVersion"))) {
105+
} else if (!strcasecmp_P(hdrPropSpecStr, PSTR("SwProtoType"))) {
106+
if (dataByteCount != 1) {
107+
StatusError = 1;
108+
} else {
109+
Picc.SwProtocolType = propSpecBytes[0];
110+
}
111+
} else if (!strcasecmp_P(hdrPropSpecStr, PSTR("SwVers"))) {
88112
if (dataByteCount != 2) {
89113
StatusError = 1;
90114
} else {
91115
Picc.SwVersionMajor = propSpecBytes[0];
92116
Picc.SwVersionMinor = propSpecBytes[1];
93117
}
94-
} else if (!strcasecmp_P(hdrPropSpecStr, PSTR("BatchNumber"))) {
118+
} else if (!strcasecmp_P(hdrPropSpecStr, PSTR("BatchNo"))) {
95119
if (dataByteCount != 5) {
96120
StatusError = 1;
97121
} else {
98122
memcpy(Picc.BatchNumber, propSpecBytes, 5);
99123
}
100-
} else if (!strcasecmp_P(hdrPropSpecStr, PSTR("ProductionDate"))) {
124+
} else if (!strcasecmp_P(hdrPropSpecStr, PSTR("ProdDate"))) {
101125
if (dataByteCount != 2) {
102126
StatusError = 1;
103127
} else {

Firmware/Chameleon-Mini/Application/DESFire/DESFireCrypto.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,13 @@ uint8_t ActiveCommMode = DESFIRE_DEFAULT_COMMS_STANDARD;
5555
void InvalidateAuthState(BYTE keepPICCAuthData) {
5656
if (!keepPICCAuthData) {
5757
AuthenticatedWithPICCMasterKey = false;
58+
memset(&SessionKey[0], 0x00, CRYPTO_MAX_BLOCK_SIZE);
59+
memset(&SessionIV[0], 0x00, CRYPTO_MAX_BLOCK_SIZE);
5860
}
5961
Authenticated = false;
6062
AuthenticatedWithKey = DESFIRE_NOT_AUTHENTICATED;
6163
Iso7816FileSelected = false;
6264
CryptoAuthMethod = CRYPTO_TYPE_ANY;
63-
ActiveCommMode = DESFIRE_DEFAULT_COMMS_STANDARD;
64-
DesfireCommMode = ActiveCommMode;
65-
memset(&SessionKey[0], 0x00, CRYPTO_MAX_BLOCK_SIZE);
66-
memset(&SessionIV[0], 0x00, CRYPTO_MAX_BLOCK_SIZE);
6765
}
6866

6967
bool IsAuthenticated(void) {

Firmware/Chameleon-Mini/Application/DESFire/DESFireISO14443Support.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ This notice must be retained at the top of all source files where indicated.
3131
#include "DESFireUtils.h"
3232

3333
#include "../ISO14443-3A.h"
34-
#include "../../Codec/ISO14443-2A.h"
3534

3635
/* General structure of a ISO 14443-4 block:
3736
* PCB (protocol control byte)
@@ -42,7 +41,7 @@ This notice must be retained at the top of all source files where indicated.
4241
*/
4342

4443
#define ISO14443A_CMD_RATS 0xE0
45-
#define ISO14443A_RATS_FRAME_SIZE ASBITS(6) /* Bit */
44+
#define ISO14443A_RATS_FRAME_SIZE ASBITS(6)
4645
#define ISO14443A_CMD_RNAK 0xB2
4746
#define ISO14443A_CRC_FRAME_SIZE ASBITS(ISO14443A_CRCA_SIZE)
4847
#define ISO14443A_CMD_DESELECT 0xC2
@@ -68,7 +67,7 @@ This notice must be retained at the top of all source files where indicated.
6867
#define ISO14443_PCB_R_BLOCK_ACK 0x00
6968
#define ISO14443_PCB_R_BLOCK_NAK 0x10
7069

71-
#define ISO14443_R_BLOCK_SIZE 1 /* Bytes */
70+
#define ISO14443_R_BLOCK_SIZE 1
7271

7372
#define ISO14443_PCB_S_DESELECT (ISO14443_PCB_S_BLOCK_STATIC)
7473
#define ISO14443_PCB_S_DESELECT_V2 0xCA
@@ -104,7 +103,7 @@ extern uint8_t ISO14443ALastIncomingDataFrame[MAX_DATA_FRAME_XFER_SIZE];
104103
extern uint16_t ISO14443ALastIncomingDataFrameBits;
105104
uint16_t ISO14443AStoreLastDataFrameAndReturn(const uint8_t *Buffer, uint16_t BufferBitCount);
106105

107-
#define MAX_STATE_RETRY_COUNT (0xFF)
106+
#define MAX_STATE_RETRY_COUNT (0x14)
108107
extern uint8_t StateRetryCount;
109108
bool CheckStateRetryCount(bool resetByDefault);
110109
bool CheckStateRetryCountWithLogging(bool resetByDefault, bool performLogging);
@@ -128,6 +127,7 @@ uint16_t ISO14443AUpdateCRCA(const uint8_t *Buffer, uint16_t ByteCount, uint16_t
128127
fullReturnBits = ASBITS(ByteCount) + ISO14443A_CRC_FRAME_SIZE; \
129128
fullReturnBits; \
130129
})
130+
131131
#define GetAndSetNoResponseCRCA(Buffer) ({ \
132132
uint16_t fullReturnBits = 0; \
133133
ISO14443AUpdateCRCA(Buffer, 0, ISO14443A_CRCA_INIT); \

0 commit comments

Comments
 (0)