Skip to content

Commit

Permalink
Removing default testing nonce B from auth commands ; Running make style
Browse files Browse the repository at this point in the history
  • Loading branch information
maxieds committed Feb 11, 2022
1 parent ba3c736 commit e22b286
Show file tree
Hide file tree
Showing 17 changed files with 182 additions and 182 deletions.
58 changes: 29 additions & 29 deletions Firmware/Chameleon-Mini/Application/CryptoTDEA.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ void EncryptDESBuffer(uint16_t Count, const void *Plaintext, void *Ciphertext, c
if (IVIn == NULL) {
memset(IV, 0x00, CRYPTO_DES_BLOCK_SIZE);
} else {
memcpy(IV, IVIn, CRYPTO_DES_BLOCK_SIZE);
memcpy(IV, IVIn, CRYPTO_DES_BLOCK_SIZE);
}
while (blockIndex < numBlocks) {
if (blockIndex == 0) {
memcpy(inputBlock, &Plaintext[0], CRYPTO_DES_BLOCK_SIZE);
CryptoMemoryXOR(IV, inputBlock, CRYPTO_DES_BLOCK_SIZE);
} else {
CryptoMemoryXOR(IV, inputBlock, CRYPTO_DES_BLOCK_SIZE);
} else {
memcpy(inputBlock, &Ciphertext[(blockIndex - 1) * CRYPTO_DES_BLOCK_SIZE], CRYPTO_DES_BLOCK_SIZE);
CryptoMemoryXOR(&Plaintext[blockIndex * CRYPTO_DES_BLOCK_SIZE], inputBlock, CRYPTO_DES_BLOCK_SIZE);
}
CryptoSpec.cryptFunc(inputBlock, ctBuf, Keys);
ctBuf += CryptoSpec.blockSize;
blockIndex++;
CryptoMemoryXOR(&Plaintext[blockIndex * CRYPTO_DES_BLOCK_SIZE], inputBlock, CRYPTO_DES_BLOCK_SIZE);
}
CryptoSpec.cryptFunc(inputBlock, ctBuf, Keys);
ctBuf += CryptoSpec.blockSize;
blockIndex++;
}
}

Expand All @@ -49,18 +49,18 @@ void DecryptDESBuffer(uint16_t Count, void *Plaintext, const void *Ciphertext, c
if (IVIn == NULL) {
memset(IV, 0x00, CRYPTO_DES_BLOCK_SIZE);
} else {
memcpy(IV, IVIn, CRYPTO_DES_BLOCK_SIZE);
memcpy(IV, IVIn, CRYPTO_DES_BLOCK_SIZE);
}
while (blockIndex < numBlocks) {
CryptoSpec.cryptFunc(inputBlock, Ciphertext + blockIndex * CRYPTO_DES_BLOCK_SIZE, Keys);
CryptoSpec.cryptFunc(inputBlock, Ciphertext + blockIndex * CRYPTO_DES_BLOCK_SIZE, Keys);
if (blockIndex == 0) {
memcpy(Plaintext, inputBlock, CRYPTO_DES_BLOCK_SIZE);
CryptoMemoryXOR(IV, Plaintext, CRYPTO_DES_BLOCK_SIZE);
} else {
CryptoMemoryXOR(IV, Plaintext, CRYPTO_DES_BLOCK_SIZE);
} else {
memcpy(Plaintext + blockIndex * CRYPTO_DES_BLOCK_SIZE, inputBlock, CRYPTO_DES_BLOCK_SIZE);
CryptoMemoryXOR(&Ciphertext[(blockIndex - 1) * CRYPTO_DES_BLOCK_SIZE],
Plaintext + blockIndex * CRYPTO_DES_BLOCK_SIZE, CRYPTO_DES_BLOCK_SIZE);
}
CryptoMemoryXOR(&Ciphertext[(blockIndex - 1) * CRYPTO_DES_BLOCK_SIZE],
Plaintext + blockIndex * CRYPTO_DES_BLOCK_SIZE, CRYPTO_DES_BLOCK_SIZE);
}
blockIndex++;
}
}
Expand All @@ -78,19 +78,19 @@ void Encrypt3DESBuffer(uint16_t Count, const void *Plaintext, void *Ciphertext,
if (IVIn == NULL) {
memset(IV, 0x00, CRYPTO_3KTDEA_BLOCK_SIZE);
} else {
memcpy(IV, IVIn, CRYPTO_3KTDEA_BLOCK_SIZE);
memcpy(IV, IVIn, CRYPTO_3KTDEA_BLOCK_SIZE);
}
while (blockIndex < numBlocks) {
if (blockIndex == 0) {
memcpy(inputBlock, &Plaintext[0], CRYPTO_3KTDEA_BLOCK_SIZE);
CryptoMemoryXOR(IV, inputBlock, CRYPTO_3KTDEA_BLOCK_SIZE);
} else {
CryptoMemoryXOR(IV, inputBlock, CRYPTO_3KTDEA_BLOCK_SIZE);
} else {
memcpy(inputBlock, &Ciphertext[(blockIndex - 1) * CRYPTO_3KTDEA_BLOCK_SIZE], CRYPTO_3KTDEA_BLOCK_SIZE);
CryptoMemoryXOR(&Plaintext[blockIndex * CRYPTO_3KTDEA_BLOCK_SIZE], inputBlock, CRYPTO_3KTDEA_BLOCK_SIZE);
}
CryptoSpec.cryptFunc(inputBlock, ctBuf, Keys);
CryptoMemoryXOR(&Plaintext[blockIndex * CRYPTO_3KTDEA_BLOCK_SIZE], inputBlock, CRYPTO_3KTDEA_BLOCK_SIZE);
}
CryptoSpec.cryptFunc(inputBlock, ctBuf, Keys);
ctBuf += CryptoSpec.blockSize;
blockIndex++;
blockIndex++;
}
}

Expand All @@ -106,18 +106,18 @@ void Decrypt3DESBuffer(uint16_t Count, void *Plaintext, const void *Ciphertext,
if (IVIn == NULL) {
memset(IV, 0x00, CRYPTO_3KTDEA_BLOCK_SIZE);
} else {
memcpy(IV, IVIn, CRYPTO_3KTDEA_BLOCK_SIZE);
memcpy(IV, IVIn, CRYPTO_3KTDEA_BLOCK_SIZE);
}
while (blockIndex < numBlocks) {
CryptoSpec.cryptFunc(inputBlock, Ciphertext + blockIndex * CRYPTO_3KTDEA_BLOCK_SIZE, Keys);
CryptoSpec.cryptFunc(inputBlock, Ciphertext + blockIndex * CRYPTO_3KTDEA_BLOCK_SIZE, Keys);
if (blockIndex == 0) {
memcpy(Plaintext, inputBlock, CRYPTO_3KTDEA_BLOCK_SIZE);
CryptoMemoryXOR(IV, Plaintext, CRYPTO_3KTDEA_BLOCK_SIZE);
} else {
CryptoMemoryXOR(IV, Plaintext, CRYPTO_3KTDEA_BLOCK_SIZE);
} else {
memcpy(Plaintext + blockIndex * CRYPTO_3KTDEA_BLOCK_SIZE, inputBlock, CRYPTO_3KTDEA_BLOCK_SIZE);
CryptoMemoryXOR(&Ciphertext[(blockIndex - 1) * CRYPTO_3KTDEA_BLOCK_SIZE],
Plaintext + blockIndex * CRYPTO_3KTDEA_BLOCK_SIZE, CRYPTO_3KTDEA_BLOCK_SIZE);
}
CryptoMemoryXOR(&Ciphertext[(blockIndex - 1) * CRYPTO_3KTDEA_BLOCK_SIZE],
Plaintext + blockIndex * CRYPTO_3KTDEA_BLOCK_SIZE, CRYPTO_3KTDEA_BLOCK_SIZE);
}
blockIndex++;
}
}
12 changes: 6 additions & 6 deletions Firmware/Chameleon-Mini/Application/CryptoTDEA.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/*
* CryptoDES.h
*
* Created on: 18.10.2016
* Author: dev_zzo
*/
/*
* CryptoDES.h
*
* Created on: 18.10.2016
* Author: dev_zzo
*/

#ifndef CRYPTODES_H_
#define CRYPTODES_H_
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ void SetAppProperty(DesfireCardLayout propId, BYTE AppSlot, SIZET Value) {
bool KeyIdValid(uint8_t AppSlot, uint8_t KeyId) {
if (KeyId >= DESFIRE_MAX_KEYS || KeyId >= ReadMaxKeyCount(AppSlot)) {
const char *debugMsg = PSTR("INVKEY-KeyId(%02x)-RdMax(%02x)");
DEBUG_PRINT_P(debugMsg, KeyId, ReadMaxKeyCount(AppSlot));
DEBUG_PRINT_P(debugMsg, KeyId, ReadMaxKeyCount(AppSlot));
return false;
}
return true;
Expand Down Expand Up @@ -585,8 +585,8 @@ uint16_t CreateApp(const DESFireAidType Aid, uint8_t KeyCount, uint8_t KeySettin
/* Allocate storage for the application structure itself */
AppDir.AppCacheStructBlockOffset[Slot] = AllocateBlocks(SELECTED_APP_CACHE_TYPE_BLOCK_SIZE);
if (AppDir.AppCacheStructBlockOffset[Slot] == 0) {
const char *debugMsg = PSTR("X - alloc blks, slot = %d");
DEBUG_PRINT_P(debugMsg, Slot);
const char *debugMsg = PSTR("X - alloc blks, slot = %d");
DEBUG_PRINT_P(debugMsg, Slot);
return STATUS_OUT_OF_EEPROM_ERROR;
}
/* Allocate storage for the application components */
Expand Down Expand Up @@ -628,8 +628,8 @@ uint16_t CreateApp(const DESFireAidType Aid, uint8_t KeyCount, uint8_t KeySettin
} else {
SIZET fileAccessRightsData[DESFIRE_MAX_FILES];
for (int fidx = 0; fidx < DESFIRE_MAX_FILES; fidx++) {
fileAccessRightsData[fidx] = 0x000f;
}
fileAccessRightsData[fidx] = 0x000f;
}
WriteBlockBytes(fileAccessRightsData, appCacheData.FileAccessRights, sizeof(SIZET) * DESFIRE_MAX_FILES);
}
appCacheData.KeyVersionsArray = AllocateBlocks(APP_CACHE_KEY_VERSIONS_ARRAY_BLOCK_SIZE);
Expand Down Expand Up @@ -669,8 +669,8 @@ uint16_t CreateApp(const DESFireAidType Aid, uint8_t KeyCount, uint8_t KeySettin
}
BYTE cryptoBlankKeyData[CRYPTO_MAX_KEY_SIZE];
memset(cryptoBlankKeyData, 0x00, CRYPTO_MAX_KEY_SIZE);
WriteBlockBytes(cryptoBlankKeyData, keyAddresses[0], CRYPTO_MAX_KEY_SIZE);
WriteBlockBytes(keyAddresses, appCacheData.KeyAddress, sizeof(SIZET) * DESFIRE_MAX_KEYS);
WriteBlockBytes(cryptoBlankKeyData, keyAddresses[0], CRYPTO_MAX_KEY_SIZE);
WriteBlockBytes(keyAddresses, appCacheData.KeyAddress, sizeof(SIZET) * DESFIRE_MAX_KEYS);
}
SIZET appCacheDataBlockId = AppDir.AppCacheStructBlockOffset[Slot];
WriteBlockBytes(&appCacheData, appCacheDataBlockId, sizeof(SelectedAppCacheType));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ This notice must be retained at the top of all source files where indicated.

#ifdef MEMORY_LIMITED_TESTING
#define DESFIRE_MAX_APPS (3)
#else
#else
#ifdef DESFIRE_CUSTOM_MAX_APPS
#define DESFIRE_MAX_APPS (DESFIRE_CUSTOM_MAX_APPS)
#else
Expand All @@ -49,7 +49,7 @@ This notice must be retained at the top of all source files where indicated.

#ifdef MEMORY_LIMITED_TESTING
#define DESFIRE_MAX_FILES (4)
#else
#else
#ifdef DESFIRE_CUSTOM_MAX_FILES
#define DESFIRE_MAX_FILES (DESFIRE_CUSTOM_MAX_FILES)
#else
Expand All @@ -59,7 +59,7 @@ This notice must be retained at the top of all source files where indicated.

#ifdef MEMORY_LIMITED_TESTING
#define DESFIRE_MAX_KEYS (2)
#else
#else
#ifdef DESFIRE_CUSTOM_MAX_KEYS
#define DESFIRE_MAX_KEYS (DESFIRE_CUSTOM_MAX_KEYS)
#else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,13 @@ CommandStatusIdType CommandDESFireSetTestingMode(char *OutParam, const char *InP
CommandStatusIdType CommandDESFireGetCommMode(char *OutParam) {
if (!IsDESFireConfiguration()) {
ExitOnInvalidConfigurationError(OutParam);
} else if(DesfireCommMode == DESFIRE_COMMS_PLAINTEXT) {
} else if (DesfireCommMode == DESFIRE_COMMS_PLAINTEXT) {
snprintf_P(OutParam, TERMINAL_BUFFER_SIZE, PSTR("Plaintext"));
} else if(DesfireCommMode == DESFIRE_COMMS_PLAINTEXT_MAC) {
} else if (DesfireCommMode == DESFIRE_COMMS_PLAINTEXT_MAC) {
snprintf_P(OutParam, TERMINAL_BUFFER_SIZE, PSTR("Plaintext/MAC"));
} else if(DesfireCommMode == DESFIRE_COMMS_CIPHERTEXT_DES) {
} else if (DesfireCommMode == DESFIRE_COMMS_CIPHERTEXT_DES) {
snprintf_P(OutParam, TERMINAL_BUFFER_SIZE, PSTR("Enciphered/DES"));
} else if(DesfireCommMode == DESFIRE_COMMS_CIPHERTEXT_AES128) {
} else if (DesfireCommMode == DESFIRE_COMMS_CIPHERTEXT_AES128) {
snprintf_P(OutParam, TERMINAL_BUFFER_SIZE, PSTR("Enciphered/AES128"));
} else {
snprintf_P(OutParam, TERMINAL_BUFFER_SIZE, PSTR("Unknown"));
Expand Down
50 changes: 25 additions & 25 deletions Firmware/Chameleon-Mini/Application/DESFire/DESFireCrypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,32 +122,32 @@ const char *GetCommSettingsDesc(uint8_t cryptoType) {

/* Code is adapted from @github/andrade/nfcjlib */
bool generateSessionKey(uint8_t *sessionKey, uint8_t *rndA, uint8_t *rndB, uint16_t cryptoType) {
switch(cryptoType) {
case CRYPTO_TYPE_DES:
switch (cryptoType) {
case CRYPTO_TYPE_DES:
memcpy(sessionKey, rndA, 4);
memcpy(sessionKey + 4, rndB, 4);
break;
case CRYPTO_TYPE_2KTDEA:
memcpy(sessionKey, rndA, 4);
memcpy(sessionKey + 4, rndB, 4);
memcpy(sessionKey + 8, rndA + 4, 4);
memcpy(sessionKey + 12, rndB + 4, 4);
break;
case CRYPTO_TYPE_3K3DES:
memcpy(sessionKey, rndA, 4);
memcpy(sessionKey + 4, rndB, 4);
memcpy(sessionKey + 8, rndA + 6, 4);
memcpy(sessionKey + 12, rndB + 6, 4);
memcpy(sessionKey + 16, rndA + 12, 4);
memcpy(sessionKey + 20, rndB + 12, 4);
break;
case CRYPTO_TYPE_AES128:
memcpy(sessionKey, rndA, 4);
memcpy(sessionKey + 4, rndB, 4);
break;
case CRYPTO_TYPE_2KTDEA:
memcpy(sessionKey, rndA, 4);
memcpy(sessionKey + 4, rndB, 4);
memcpy(sessionKey + 8, rndA + 4, 4);
memcpy(sessionKey + 12, rndB + 4, 4);
break;
case CRYPTO_TYPE_3K3DES:
memcpy(sessionKey, rndA, 4);
memcpy(sessionKey + 4, rndB, 4);
memcpy(sessionKey + 8, rndA + 6, 4);
memcpy(sessionKey + 12, rndB + 6, 4);
memcpy(sessionKey + 16, rndA + 12, 4);
memcpy(sessionKey + 20, rndB + 12, 4);
break;
case CRYPTO_TYPE_AES128:
memcpy(sessionKey, rndA, 4);
memcpy(sessionKey + 4, rndB, 4);
memcpy(sessionKey + 8, rndA + 12, 4);
memcpy(sessionKey + 12, rndB + 12, 4);
break;
default:
memcpy(sessionKey + 4, rndB, 4);
memcpy(sessionKey + 8, rndA + 12, 4);
memcpy(sessionKey + 12, rndB + 12, 4);
break;
default:
return false;
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ extern BYTE DesfireCommMode;

/* Key sizes, block sizes (in bytes): */
#define CRYPTO_AES_KEY_SIZE (16)
#define CRYPTO_MAX_KEY_SIZE (24)
#define CRYPTO_MAX_KEY_SIZE (24)
#define CRYPTO_MAX_BLOCK_SIZE (16)
#define DESFIRE_AES_IV_SIZE (CRYPTO_AES_BLOCK_SIZE)
#define CRYPTO_CHALLENGE_RESPONSE_BYTES (16)
Expand Down
46 changes: 23 additions & 23 deletions Firmware/Chameleon-Mini/Application/DESFire/DESFireInstructions.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ This notice must be retained at the top of all source files where indicated.

DesfireSavedCommandStateType DesfireCommandState = { 0 };

/* 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
* added to keep CallInstructionHandler(uint8_t*, uint16_t)
* operating correctly. The instruction handler performs a
* binary search on the array to save time locating the correct
* C function to call to execute the command -- and this speedup
* helps keep timing issues at bay. DO NOT just append new command
* handlers to the end of the array, or insert them haphazardly in
/* 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
* added to keep CallInstructionHandler(uint8_t*, uint16_t)
* operating correctly. The instruction handler performs a
* binary search on the array to save time locating the correct
* C function to call to execute the command -- and this speedup
* helps keep timing issues at bay. DO NOT just append new command
* handlers to the end of the array, or insert them haphazardly in
* the middle !!!
*/
const __flash DESFireCommand DESFireCommandSet[] = {
Expand Down Expand Up @@ -309,19 +309,19 @@ uint16_t CallInstructionHandler(uint8_t *Buffer, uint16_t ByteCount) {
uint16_t curInsLower = 0, curInsUpper = sizeof(DESFireCommandSet) / sizeof(DESFireCommand) - 1;
uint16_t curInsIndex;
DESFireCommand dfCmd;
while(curInsUpper >= curInsLower) {
while (curInsUpper >= curInsLower) {
curInsIndex = curInsLower + (curInsUpper + 1 - curInsLower) / 2;
memcpy_P(&dfCmd, insLookupTableBuf + curInsIndex * sizeof(DESFireCommand), sizeof(DESFireCommand));
if (dfCmd.insCode == insCode) {
if (dfCmd.insFunc == NULL) {
return CmdNotImplemented(Buffer, ByteCount);
}
return dfCmd.insFunc(Buffer, ByteCount);
} else if (dfCmd.insCode < insCode) {
curInsLower = curInsIndex + 1;
} else {
curInsUpper = curInsIndex - 1;
}
if (dfCmd.insFunc == NULL) {
return CmdNotImplemented(Buffer, ByteCount);
}
return dfCmd.insFunc(Buffer, ByteCount);
} else if (dfCmd.insCode < insCode) {
curInsLower = curInsIndex + 1;
} else {
curInsUpper = curInsIndex - 1;
}
}
return ISO14443A_APP_NO_RESPONSE;
}
Expand Down Expand Up @@ -507,7 +507,7 @@ uint16_t EV0CmdAuthenticateLegacy2(uint8_t *Buffer, uint16_t ByteCount) {

/* Set status for the next incoming command on error */
DesfireState = DESFIRE_IDLE;

/* Validate command length */
if (ByteCount != 2 * CRYPTO_CHALLENGE_RESPONSE_BYTES + 1) {
Buffer[0] = STATUS_LENGTH_ERROR;
Expand All @@ -534,7 +534,7 @@ uint16_t EV0CmdAuthenticateLegacy2(uint8_t *Buffer, uint16_t ByteCount) {
/* Check that the returned RndB matches what we sent in the previous round */
if (memcmp(DesfireCommandState.RndB, challengeRndB, CRYPTO_CHALLENGE_RESPONSE_BYTES)) {
Buffer[0] = STATUS_AUTHENTICATION_ERROR;
return DESFIRE_STATUS_RESPONSE_SIZE;
return DESFIRE_STATUS_RESPONSE_SIZE;
}

/* Authenticated successfully */
Expand Down Expand Up @@ -1791,7 +1791,7 @@ uint16_t DesfireCmdAuthenticate3KTDEA2(uint8_t *Buffer, uint16_t ByteCount) {
/* Check that the returned RndB matches what we sent in the previous round */
if (memcmp(DesfireCommandState.RndB, challengeRndB, CRYPTO_CHALLENGE_RESPONSE_BYTES)) {
LogEntry(LOG_ERR_DESFIRE_GENERIC_ERROR, (const void *) challengeRndB, CRYPTO_CHALLENGE_RESPONSE_BYTES);
Buffer[0] = STATUS_AUTHENTICATION_ERROR;
Buffer[0] = STATUS_AUTHENTICATION_ERROR;
return DESFIRE_STATUS_RESPONSE_SIZE;
}

Expand All @@ -1805,7 +1805,7 @@ uint16_t DesfireCmdAuthenticate3KTDEA2(uint8_t *Buffer, uint16_t ByteCount) {
RotateArrayLeft(challengeRndA, challengeRndAB, CRYPTO_CHALLENGE_RESPONSE_BYTES);
Encrypt3DESBuffer(CRYPTO_CHALLENGE_RESPONSE_BYTES, challengeRndAB,
&Buffer[1], NULL, Key);

generateSessionKey(SessionKey, challengeRndA, challengeRndB, CRYPTO_TYPE_3K3DES);

/* Return the status on success */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ This notice must be retained at the top of all source files where indicated.
#define PICC_FORMAT_BYTE (0x00)
#define PICC_EMPTY_BYTE (0x00)

typedef struct DESFIRE_FIRMWARE_PACKING DESFIRE_FIRMWARE_ALIGNAT{
typedef struct DESFIRE_FIRMWARE_PACKING DESFIRE_FIRMWARE_ALIGNAT {
/* Static data: does not change during the PICC's lifetime.
* We will add Chameleon Mini terminal commands to enable
* resetting this data so tags can be emulated authentically.
Expand Down
Loading

0 comments on commit e22b286

Please sign in to comment.