Skip to content

Commit

Permalink
Restore point for many incremental updates, bug fixes and documentati…
Browse files Browse the repository at this point in the history
…on changes
  • Loading branch information
maxieds committed Jul 20, 2022
1 parent 126189a commit 5cd6773
Show file tree
Hide file tree
Showing 38 changed files with 340 additions and 662 deletions.
16 changes: 10 additions & 6 deletions Doc/DESFireSupportReadme.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ We can modify the remaining tag header information emulated by the tag as follow
DF_SETHDR=ATS xxxxxxxxxx
DF_SETHDR=ATQA xxxx
DF_SETHDR=ManuID xx
DF_SETHDR=HardwareVersion mmMM
DF_SETHDR=SoftwareVersion mmMM
DF_SETHDR=BatchNumber xxxxxxxxxx
DF_SETHDR=ProductionDate WWYY
DF_SETHDR=Type xx
DF_SETHDR=Subtype xx
DF_SETHDR=HwProtoType xx
DF_SETHDR=HwVers mmMM
DF_SETHDR=SwProtoType xx
DF_SETHDR=SwVers mmMM
DF_SETHDR=BatchNo xxxxxxxxxx
DF_SETHDR=ProdDate WWYY
```

##### Examples:
Expand Down Expand Up @@ -246,8 +250,8 @@ DF_ENCMODE=AES:CBC

| Instruction | Cmd Byte | Description | Testing Status | Implementation Notes |
| :--- | :----: | :----: | :----: | :-- |
| 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. |
| CMD_AUTHENTICATE_ISO | 0x1A | ISO / 3DES auth | :question: | This implementation is too slow! Need hardware support for 3DES crypto? |
| CMD_AUTHENTICATE | 0x0A | Authenticate legacy | :ballot_box_with_check: | |
| CMD_AUTHENTICATE_ISO | 0x1A | ISO / 3DES auth | :ballot_box_with_check: | |
| CMD_AUTHENTICATE_AES | 0xAA | Standard AES auth | :ballot_box_with_check: | |
| CMD_AUTHENTICATE_EV2_FIRST | 0x71 | Newer spec auth variant | :x: | |
| CMD_AUTHENTICATE_EV2_NONFIRST | 0x77 | Newer spec auth variant | :x: | See page 32 of AN12343.pdf |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,6 @@ void WriteFileSettings(uint8_t AppSlot, uint8_t FileIndex, DESFireFileTypeSettin
}
SIZET fileTypeSettingsBlockId = GetAppProperty(DESFIRE_APP_FILES_PTR_BLOCK_ID, AppSlot);
SIZET fileTypeSettingsAddresses[DESFIRE_MAX_FILES];
// TODO: ???
ReadBlockBytes(fileTypeSettingsAddresses, fileTypeSettingsBlockId, 2 * DESFIRE_MAX_FILES);
WriteBlockBytes(FileSettings, fileTypeSettingsAddresses[FileIndex], sizeof(DESFireFileTypeSettings));
memcpy(&(SelectedFile.File), FileSettings, sizeof(DESFireFileTypeSettings));
Expand All @@ -494,10 +493,11 @@ void WriteFileSettings(uint8_t AppSlot, uint8_t FileIndex, DESFireFileTypeSettin
uint8_t LookupAppSlot(const DESFireAidType Aid) {
uint8_t Slot;
for (Slot = 0; Slot < DESFIRE_MAX_SLOTS; ++Slot) {
if (!memcmp(AppDir.AppIds[Slot], Aid, DESFIRE_AID_SIZE))
break;
if (!memcmp(AppDir.AppIds[Slot], Aid, DESFIRE_AID_SIZE)) {
return Slot;
}
}
return Slot;
return DESFIRE_MAX_SLOTS;
}

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

Expand All @@ -603,7 +603,7 @@ uint16_t CreateApp(const DESFireAidType Aid, uint8_t KeyCount, uint8_t KeySettin
} else {
BYTE keySettings[DESFIRE_MAX_KEYS];
memset(keySettings, KeySettings, DESFIRE_MAX_KEYS);
keySettings[0] = KeySettings; // NEEDED ???
keySettings[0] = KeySettings;
WriteBlockBytes(keySettings, appCacheData.KeySettings, DESFIRE_MAX_KEYS);
}
appCacheData.FileNumbersArrayMap = AllocateBlocks(APP_CACHE_FILE_NUMBERS_HASHMAP_BLOCK_SIZE);
Expand Down Expand Up @@ -710,8 +710,7 @@ uint16_t DeleteApp(const DESFireAidType Aid) {
}

void GetApplicationIdsSetup(void) {
/* Skip the PICC application */
TransferState.GetApplicationIds.NextIndex = 1;
TransferState.GetApplicationIds.NextIndex = 0;
}

TransferStatus GetApplicationIdsTransfer(uint8_t *Buffer) {
Expand All @@ -724,7 +723,7 @@ TransferStatus GetApplicationIdsTransfer(uint8_t *Buffer) {
continue;
}
/* If it won't fit -- remember and return */
if (Status.BytesProcessed >= TERMINAL_BUFFER_SIZE) {
if (Status.BytesProcessed >= TERMINAL_BUFFER_SIZE - 20) {
TransferState.GetApplicationIds.NextIndex = EntryIndex;
Status.IsComplete = false;
return Status;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,27 +77,51 @@ CommandStatusIdType CommandDESFireSetHeaderProperty(char *OutParam, const char *
} else {
Picc.ManufacturerID = propSpecBytes[0];
}
} else if (!strcasecmp_P(hdrPropSpecStr, PSTR("HardwareVersion"))) {
} else if (!strcasecmp_P(hdrPropSpecStr, PSTR("Type"))) {
if (dataByteCount != 1) {
StatusError = 1;
} else {
Picc.TagType = propSpecBytes[0];
}
} else if (!strcasecmp_P(hdrPropSpecStr, PSTR("Subtype"))) {
if (dataByteCount != 1) {
StatusError = 1;
} else {
Picc.TagSubtype = propSpecBytes[0];
}
} else if (!strcasecmp_P(hdrPropSpecStr, PSTR("HWProtoType"))) {
if (dataByteCount != 1) {
StatusError = 1;
} else {
Picc.HwProtocolType = propSpecBytes[0];
}
} else if (!strcasecmp_P(hdrPropSpecStr, PSTR("HWVers"))) {
if (dataByteCount != 2) {
StatusError = 1;
} else {
Picc.HwVersionMajor = propSpecBytes[0];
Picc.HwVersionMinor = propSpecBytes[1];
}
} else if (!strcasecmp_P(hdrPropSpecStr, PSTR("SoftwareVersion"))) {
} else if (!strcasecmp_P(hdrPropSpecStr, PSTR("SwProtoType"))) {
if (dataByteCount != 1) {
StatusError = 1;
} else {
Picc.SwProtocolType = propSpecBytes[0];
}
} else if (!strcasecmp_P(hdrPropSpecStr, PSTR("SwVers"))) {
if (dataByteCount != 2) {
StatusError = 1;
} else {
Picc.SwVersionMajor = propSpecBytes[0];
Picc.SwVersionMinor = propSpecBytes[1];
}
} else if (!strcasecmp_P(hdrPropSpecStr, PSTR("BatchNumber"))) {
} else if (!strcasecmp_P(hdrPropSpecStr, PSTR("BatchNo"))) {
if (dataByteCount != 5) {
StatusError = 1;
} else {
memcpy(Picc.BatchNumber, propSpecBytes, 5);
}
} else if (!strcasecmp_P(hdrPropSpecStr, PSTR("ProductionDate"))) {
} else if (!strcasecmp_P(hdrPropSpecStr, PSTR("ProdDate"))) {
if (dataByteCount != 2) {
StatusError = 1;
} else {
Expand Down
6 changes: 2 additions & 4 deletions Firmware/Chameleon-Mini/Application/DESFire/DESFireCrypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,13 @@ uint8_t ActiveCommMode = DESFIRE_DEFAULT_COMMS_STANDARD;
void InvalidateAuthState(BYTE keepPICCAuthData) {
if (!keepPICCAuthData) {
AuthenticatedWithPICCMasterKey = false;
memset(&SessionKey[0], 0x00, CRYPTO_MAX_BLOCK_SIZE);
memset(&SessionIV[0], 0x00, CRYPTO_MAX_BLOCK_SIZE);
}
Authenticated = false;
AuthenticatedWithKey = DESFIRE_NOT_AUTHENTICATED;
Iso7816FileSelected = false;
CryptoAuthMethod = CRYPTO_TYPE_ANY;
ActiveCommMode = DESFIRE_DEFAULT_COMMS_STANDARD;
DesfireCommMode = ActiveCommMode;
memset(&SessionKey[0], 0x00, CRYPTO_MAX_BLOCK_SIZE);
memset(&SessionIV[0], 0x00, CRYPTO_MAX_BLOCK_SIZE);
}

bool IsAuthenticated(void) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ This notice must be retained at the top of all source files where indicated.
#include "DESFireUtils.h"

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

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

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

#define ISO14443_R_BLOCK_SIZE 1 /* Bytes */
#define ISO14443_R_BLOCK_SIZE 1

#define ISO14443_PCB_S_DESELECT (ISO14443_PCB_S_BLOCK_STATIC)
#define ISO14443_PCB_S_DESELECT_V2 0xCA
Expand Down Expand Up @@ -104,7 +103,7 @@ extern uint8_t ISO14443ALastIncomingDataFrame[MAX_DATA_FRAME_XFER_SIZE];
extern uint16_t ISO14443ALastIncomingDataFrameBits;
uint16_t ISO14443AStoreLastDataFrameAndReturn(const uint8_t *Buffer, uint16_t BufferBitCount);

#define MAX_STATE_RETRY_COUNT (0xFF)
#define MAX_STATE_RETRY_COUNT (0x14)
extern uint8_t StateRetryCount;
bool CheckStateRetryCount(bool resetByDefault);
bool CheckStateRetryCountWithLogging(bool resetByDefault, bool performLogging);
Expand All @@ -128,6 +127,7 @@ uint16_t ISO14443AUpdateCRCA(const uint8_t *Buffer, uint16_t ByteCount, uint16_t
fullReturnBits = ASBITS(ByteCount) + ISO14443A_CRC_FRAME_SIZE; \
fullReturnBits; \
})

#define GetAndSetNoResponseCRCA(Buffer) ({ \
uint16_t fullReturnBits = 0; \
ISO14443AUpdateCRCA(Buffer, 0, ISO14443A_CRCA_INIT); \
Expand Down
Loading

0 comments on commit 5cd6773

Please sign in to comment.