Skip to content

Commit

Permalink
Adding in preliminary AES transfer functions -- Trying to save space …
Browse files Browse the repository at this point in the history
…for more where it can be pruned -- III -- cf. emsec#313
  • Loading branch information
maxieds committed Feb 12, 2022
1 parent a0cb74c commit de798fb
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 405 deletions.
17 changes: 0 additions & 17 deletions Doc/DESFireSupportReadme.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,23 +204,6 @@ DF_SETHDR=ATS 0675f7b102
```
Note that the UID for the tag can be set using separate Chameleon terminal commands.

#### DF_PPRINT_PICC -- Visualize tag contents

This lets users pretty print the tag layout in several different ways, and with
a couple of options for verbosity. This helps with visualizing the landscape that
we are programming. The syntax include:
```bash
DF_PPRINT_PICC FullImage
DF_PPRINT_PICC HeaderData
```

#### DF_FWINFO -- Print firmware revision information

Self explanatory and similar to the familiar ``VERSION`` command. Syntax:
```bash
DF_FWINFO
```

#### DF_LOGMODE -- Sets the depth of (LIVE) logging messages printed at runtime

Syntax -- not guaranteeing that all of these are meaningful or distinct just yet:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,50 +114,6 @@ CommandStatusIdType CommandDESFireSetHeaderProperty(char *OutParam, const char *
}
#endif /* DISABLE_PERMISSIVE_DESFIRE_SETTINGS */

#if 0
CommandStatusIdType CommandDESFireLayoutPPrint(char *OutParam, const char *InParams) {
if (!IsDESFireConfiguration()) {
ExitOnInvalidConfigurationError(OutParam);
}
char pprintListSpecStr[32];
BYTE StatusError = 0x00;
if (!sscanf_P(InParams, PSTR("%31s"), pprintListSpecStr)) {
StatusError = 0x01;
} else {
pprintListSpecStr[31] = '\0';
if (!strcasecmp_P(pprintListSpecStr, PSTR("FullImage"))) {
PrettyPrintPICCImageData((BYTE *) OutParam, TERMINAL_BUFFER_SIZE, 0x01);
} else if (!strcasecmp_P(pprintListSpecStr, PSTR("HeaderData"))) {
PrettyPrintPICCHeaderData((BYTE *) OutParam, TERMINAL_BUFFER_SIZE, 0x01);
} else {
StatusError = 0x01;
}
}
if (StatusError) {
snprintf_P(OutParam, TERMINAL_BUFFER_SIZE,
PSTR("%s <FullImage|HeaderData>"),
DFCOMMAND_LAYOUT_PPRINT);
return COMMAND_ERR_INVALID_USAGE_ID;
}
return COMMAND_INFO_OK_WITH_TEXT_ID;
}

CommandStatusIdType CommandDESFireFirmwareInfo(char *OutParam) {
if (!IsDESFireConfiguration()) {
ExitOnInvalidConfigurationError(OutParam);
}
snprintf_P(OutParam, TERMINAL_BUFFER_SIZE,
PSTR("Chameleon-Mini DESFire enabled firmware built on %s "
"based on %s from \r\n"
"https://github.com/maxieds/ChameleonMini.\r\n"
"Revision: %s\r\n"),
DESFIRE_FIRMWARE_BUILD_TIMESTAMP,
DESFIRE_FIRMWARE_GIT_COMMIT_ID,
DESFIRE_FIRMWARE_REVISION);
return COMMAND_INFO_OK_WITH_TEXT_ID;
}
#endif

CommandStatusIdType CommandDESFireGetLoggingMode(char *OutParam) {
if (!IsDESFireConfiguration()) {
ExitOnInvalidConfigurationError(OutParam);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,6 @@ CommandStatusIdType CommandDESFireGetHeaderProperty(char *OutParam);
CommandStatusIdType CommandDESFireSetHeaderProperty(char *OutMessage, const char *InParams);
#endif

//#define DFCOMMAND_LAYOUT_PPRINT "DF_PPRINT_PICC"
//CommandStatusIdType CommandDESFireLayoutPPrint(char *OutParam, const char *InParams);

//#define DFCOMMAND_FIRMWARE_INFO "DF_FWINFO"
//CommandStatusIdType CommandDESFireFirmwareInfo(char *OutParam);

#define DFCOMMAND_LOGGING_MODE "DF_LOGMODE"
CommandStatusIdType CommandDESFireGetLoggingMode(char *OutParam);
CommandStatusIdType CommandDESFireSetLoggingMode(char *OutMessage, const char *InParams);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,6 @@ This notice must be retained at the top of all source files where indicated.
.SetFunc = CommandDESFireSetHeaderProperty,
.GetFunc = CommandDESFireGetHeaderProperty
}, {
// .Command = DFCOMMAND_LAYOUT_PPRINT,
// .ExecFunc = NO_FUNCTION,
// .ExecParamFunc = CommandDESFireLayoutPPrint,
// .SetFunc = NO_FUNCTION,
// .GetFunc = NO_FUNCTION
//}, {
// .Command = DFCOMMAND_FIRMWARE_INFO,
// .ExecFunc = CommandDESFireFirmwareInfo,
// .ExecParamFunc = NO_FUNCTION,
// .SetFunc = NO_FUNCTION,
// .GetFunc = NO_FUNCTION
//}, {
.Command = DFCOMMAND_LOGGING_MODE,
.ExecFunc = NO_FUNCTION,
.ExecParamFunc = NO_FUNCTION,
Expand Down
62 changes: 62 additions & 0 deletions Firmware/Chameleon-Mini/Application/DESFire/DESFireInstructions.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,68 @@ This notice must be retained at the top of all source files where indicated.

DesfireSavedCommandStateType DesfireCommandState = { 0 };

/* Helper and batch process functions */
static uint16_t ExitWithStatus(uint8_t *Buffer, uint8_t StatusCode, uint16_t DefaultReturnValue);
uint16_t CmdNotImplemented(uint8_t *Buffer, uint16_t ByteCount);

/* General commands */
static uint16_t EV0CmdFormatPicc(uint8_t *Buffer, uint16_t ByteCount);
static uint16_t DesfireCmdGetCardUID(uint8_t *Buffer, uint16_t ByteCount);
static uint16_t DesfireCmdSetConfiguration(uint8_t *Buffer, uint16_t ByteCount); // ?? Docs ??
static uint16_t DesfireCmdFreeMemory(uint8_t *Buffer, uint16_t ByteCount); // returns free memory on the tag

/* Key management commands */
static uint16_t EV0CmdChangeKey(uint8_t *Buffer, uint16_t ByteCount);
static uint16_t EV0CmdGetKeySettings(uint8_t *Buffer, uint16_t ByteCount);
static uint16_t EV0CmdChangeKeySettings(uint8_t *Buffer, uint16_t ByteCount);
static uint16_t DesfireCmdGetKeyVersion(uint8_t *Buffer, uint16_t ByteCount);

/* Application management commands */
static uint16_t EV0CmdGetApplicationIds1(uint8_t *Buffer, uint16_t ByteCount);
static uint16_t EV0CmdCreateApplication(uint8_t *Buffer, uint16_t ByteCount);
static uint16_t EV0CmdDeleteApplication(uint8_t *Buffer, uint16_t ByteCount);
static uint16_t EV0CmdSelectApplication(uint8_t *Buffer, uint16_t ByteCount);
static uint16_t DesfireCmdGetDFNames(uint8_t *Buffer, uint16_t ByteCount);

/* File management commands */
static uint16_t EV0CmdCreateStandardDataFile(uint8_t *Buffer, uint16_t ByteCount);
static uint16_t EV0CmdCreateBackupDataFile(uint8_t *Buffer, uint16_t ByteCount);
static uint16_t EV0CmdCreateValueFile(uint8_t *Buffer, uint16_t ByteCount);
static uint16_t EV0CmdCreateLinearRecordFile(uint8_t *Buffer, uint16_t ByteCount);
static uint16_t EV0CmdCreateCyclicRecordFile(uint8_t *Buffer, uint16_t ByteCount);
static uint16_t EV0CmdDeleteFile(uint8_t *Buffer, uint16_t ByteCount);
static uint16_t EV0CmdGetFileIds(uint8_t *Buffer, uint16_t ByteCount);
static uint16_t EV0CmdGetFileSettings(uint8_t *Buffer, uint16_t ByteCount);
static uint16_t EV0CmdChangeFileSettings(uint8_t *Buffer, uint16_t ByteCount);

/* Data manipulation commands */
// NOTE: Page 57: Read file functions:
static uint16_t EV0CmdReadData(uint8_t *Buffer, uint16_t ByteCount);
static uint16_t EV0CmdWriteData(uint8_t *Buffer, uint16_t ByteCount);
static uint16_t EV0CmdGetValue(uint8_t *Buffer, uint16_t ByteCount);
static uint16_t EV0CmdCredit(uint8_t *Buffer, uint16_t ByteCount);
static uint16_t EV0CmdDebit(uint8_t *Buffer, uint16_t ByteCount);
static uint16_t EV0CmdLimitedCredit(uint8_t *Buffer, uint16_t ByteCount);
static uint16_t EV0CmdReadRecords(uint8_t *Buffer, uint16_t ByteCount);
static uint16_t EV0CmdWriteRecord(uint8_t *Buffer, uint16_t ByteCount);
static uint16_t EV0CmdClearRecords(uint8_t *Buffer, uint16_t ByteCount);

/* Transaction handling commands */
static uint16_t EV0CmdCommitTransaction(uint8_t *Buffer, uint16_t ByteCount);
static uint16_t EV0CmdAbortTransaction(uint8_t *Buffer, uint16_t ByteCount);

/* ISO7816 command handling */
static uint16_t ISO7816CmdSelect(uint8_t *Buffer, uint16_t ByteCount);
static uint16_t ISO7816CmdSelectEF(uint8_t *Buffer, uint16_t ByteCount);
static uint16_t ISO7816CmdSelectDF(uint8_t *Buffer, uint16_t ByteCount);
static uint16_t ISO7816CmdGetChallenge(uint8_t *Buffer, uint16_t ByteCount);
static uint16_t ISO7816CmdExternalAuthenticate(uint8_t *Buffer, uint16_t ByteCount);
static uint16_t ISO7816CmdInternalAuthenticate(uint8_t *Buffer, uint16_t ByteCount);
static uint16_t ISO7816CmdReadBinary(uint8_t *Buffer, uint16_t ByteCount);
static uint16_t ISO7816CmdUpdateBinary(uint8_t *Buffer, uint16_t ByteCount);
static uint16_t ISO7816CmdReadRecords(uint8_t *Buffer, uint16_t ByteCount);
static uint16_t ISO7816CmdAppendRecord(uint8_t *Buffer, uint16_t ByteCount);

/* NOTE: The order of the structures in this buffer MUST be kept in
* ascending sorted order by the INS code. This property of the
* array has to be maintained as new commands and functions are
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,81 +122,21 @@ extern const __flash DESFireCommand DESFireCommandSet[];

/* Helper and batch process functions */
uint16_t CallInstructionHandler(uint8_t *Buffer, uint16_t ByteCount);
uint16_t ExitWithStatus(uint8_t *Buffer, uint8_t StatusCode, uint16_t DefaultReturnValue);
uint16_t CmdNotImplemented(uint8_t *Buffer, uint16_t ByteCount);

/*
* The following section implements:
* DESFire EV0 / D40 specific commands
*/

/* General commands */
uint16_t EV0CmdGetVersion1(uint8_t *Buffer, uint16_t ByteCount);
uint16_t EV0CmdGetVersion2(uint8_t *Buffer, uint16_t ByteCount);
uint16_t EV0CmdGetVersion3(uint8_t *Buffer, uint16_t ByteCount);
uint16_t EV0CmdFormatPicc(uint8_t *Buffer, uint16_t ByteCount);
uint16_t DesfireCmdGetCardUID(uint8_t *Buffer, uint16_t ByteCount);
uint16_t DesfireCmdSetConfiguration(uint8_t *Buffer, uint16_t ByteCount); // ?? Docs ??
uint16_t DesfireCmdFreeMemory(uint8_t *Buffer, uint16_t ByteCount); // returns free memory on the tag

/* Key management commands */
uint16_t EV0CmdChangeKey(uint8_t *Buffer, uint16_t ByteCount);
uint16_t EV0CmdGetKeySettings(uint8_t *Buffer, uint16_t ByteCount);
uint16_t EV0CmdChangeKeySettings(uint8_t *Buffer, uint16_t ByteCount);
uint16_t DesfireCmdGetKeyVersion(uint8_t *Buffer, uint16_t ByteCount);

/* Application management commands */
uint16_t EV0CmdGetApplicationIds1(uint8_t *Buffer, uint16_t ByteCount);
uint16_t EV0CmdCreateApplication(uint8_t *Buffer, uint16_t ByteCount);
uint16_t EV0CmdDeleteApplication(uint8_t *Buffer, uint16_t ByteCount);
uint16_t EV0CmdSelectApplication(uint8_t *Buffer, uint16_t ByteCount);
uint16_t DesfireCmdGetDFNames(uint8_t *Buffer, uint16_t ByteCount);

/* File management commands */
uint16_t EV0CmdCreateStandardDataFile(uint8_t *Buffer, uint16_t ByteCount);
uint16_t EV0CmdCreateBackupDataFile(uint8_t *Buffer, uint16_t ByteCount);
uint16_t EV0CmdCreateValueFile(uint8_t *Buffer, uint16_t ByteCount);
uint16_t EV0CmdCreateLinearRecordFile(uint8_t *Buffer, uint16_t ByteCount);
uint16_t EV0CmdCreateCyclicRecordFile(uint8_t *Buffer, uint16_t ByteCount);
uint16_t EV0CmdDeleteFile(uint8_t *Buffer, uint16_t ByteCount);
uint16_t EV0CmdGetFileIds(uint8_t *Buffer, uint16_t ByteCount);
uint16_t EV0CmdGetFileSettings(uint8_t *Buffer, uint16_t ByteCount);
uint16_t EV0CmdChangeFileSettings(uint8_t *Buffer, uint16_t ByteCount);

/* Data manipulation commands */
// NOTE: Page 57: Read file functions:
uint16_t EV0CmdReadData(uint8_t *Buffer, uint16_t ByteCount);
uint16_t EV0CmdWriteData(uint8_t *Buffer, uint16_t ByteCount);
uint16_t EV0CmdGetValue(uint8_t *Buffer, uint16_t ByteCount);
uint16_t EV0CmdCredit(uint8_t *Buffer, uint16_t ByteCount);
uint16_t EV0CmdDebit(uint8_t *Buffer, uint16_t ByteCount);
uint16_t EV0CmdLimitedCredit(uint8_t *Buffer, uint16_t ByteCount);
uint16_t EV0CmdReadRecords(uint8_t *Buffer, uint16_t ByteCount);
uint16_t EV0CmdWriteRecord(uint8_t *Buffer, uint16_t ByteCount);
uint16_t EV0CmdClearRecords(uint8_t *Buffer, uint16_t ByteCount);

/* Transaction handling commands */
uint16_t EV0CmdCommitTransaction(uint8_t *Buffer, uint16_t ByteCount);
uint16_t EV0CmdAbortTransaction(uint8_t *Buffer, uint16_t ByteCount);

/* EV1/EV2 supported commands */
uint16_t EV0CmdAuthenticateLegacy1(uint8_t *Buffer, uint16_t ByteCount);
uint16_t EV0CmdAuthenticateLegacy2(uint8_t *Buffer, uint16_t ByteCount);

uint16_t DesfireCmdAuthenticate3KTDEA1(uint8_t *Buffer, uint16_t ByteCount);
uint16_t DesfireCmdAuthenticate3KTDEA2(uint8_t *Buffer, uint16_t ByteCount);
uint16_t DesfireCmdAuthenticateAES1(uint8_t *Buffer, uint16_t ByteCount);
uint16_t DesfireCmdAuthenticateAES2(uint8_t *Buffer, uint16_t ByteCount);

/* ISO7816 command handling */
uint16_t ISO7816CmdSelect(uint8_t *Buffer, uint16_t ByteCount);
uint16_t ISO7816CmdSelectEF(uint8_t *Buffer, uint16_t ByteCount);
uint16_t ISO7816CmdSelectDF(uint8_t *Buffer, uint16_t ByteCount);
uint16_t ISO7816CmdGetChallenge(uint8_t *Buffer, uint16_t ByteCount);
uint16_t ISO7816CmdExternalAuthenticate(uint8_t *Buffer, uint16_t ByteCount);
uint16_t ISO7816CmdInternalAuthenticate(uint8_t *Buffer, uint16_t ByteCount);
uint16_t ISO7816CmdReadBinary(uint8_t *Buffer, uint16_t ByteCount);
uint16_t ISO7816CmdUpdateBinary(uint8_t *Buffer, uint16_t ByteCount);
uint16_t ISO7816CmdReadRecords(uint8_t *Buffer, uint16_t ByteCount);
uint16_t ISO7816CmdAppendRecord(uint8_t *Buffer, uint16_t ByteCount);

#endif
Loading

0 comments on commit de798fb

Please sign in to comment.