Skip to content

Commit

Permalink
Working AES128 auth and ISO auth ; Still need to debug the legacy auth
Browse files Browse the repository at this point in the history
  • Loading branch information
maxieds committed Feb 5, 2022
1 parent b451e12 commit a02f214
Show file tree
Hide file tree
Showing 22 changed files with 146 additions and 271 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ BYTE AMKAllKeysFrozen(void) {
}

SIZET GetAppProperty(DesfireCardLayout propId, BYTE AppSlot) {
if (AppSlot >= DESFIRE_MAX_SLOTS) {
if (AppSlot >= AppDir.FirstFreeSlot || AppSlot >= DESFIRE_MAX_SLOTS) {
return 0x00;
}
SelectedAppCacheType appCache;
Expand Down Expand Up @@ -157,7 +157,7 @@ SIZET GetAppProperty(DesfireCardLayout propId, BYTE AppSlot) {
}

void SetAppProperty(DesfireCardLayout propId, BYTE AppSlot, SIZET Value) {
if (AppSlot >= DESFIRE_MAX_SLOTS) {
if (AppSlot >= AppDir.FirstFreeSlot || AppSlot >= DESFIRE_MAX_SLOTS) {
return;
}
SelectedAppCacheType appCache;
Expand Down Expand Up @@ -308,16 +308,6 @@ void ReadAppKey(uint8_t AppSlot, uint8_t KeyId, uint8_t *Key, SIZET KeySize) {
SIZET keyStorageArray[DESFIRE_MAX_KEYS];
ReadBlockBytes(keyStorageArray, keyStorageArrayBlockId, 2 * DESFIRE_MAX_KEYS);
ReadBlockBytes(Key, keyStorageArray[KeyId], KeySize);
/*if (KeySize > DESFIRE_EEPROM_BLOCK_SIZE) {
ReadBlockBytes(Key, keyStorageArray[KeyId], DESFIRE_EEPROM_BLOCK_SIZE);
uint8_t fullBlock[DESFIRE_EEPROM_BLOCK_SIZE];
ReadBlockBytes(fullBlock, keyStorageArray[KeyId] + 1, DESFIRE_EEPROM_BLOCK_SIZE);
memcpy(Key + DESFIRE_EEPROM_BLOCK_SIZE, fullBlock, KeySize - DESFIRE_EEPROM_BLOCK_SIZE);
} else {
uint8_t fullBlock[DESFIRE_EEPROM_BLOCK_SIZE];
ReadBlockBytes(fullBlock, keyStorageArray[KeyId], DESFIRE_EEPROM_BLOCK_SIZE);
memcpy(Key, fullBlock, KeySize);
}*/
}

void WriteAppKey(uint8_t AppSlot, uint8_t KeyId, const uint8_t *Key, SIZET KeySize) {
Expand All @@ -329,7 +319,6 @@ void WriteAppKey(uint8_t AppSlot, uint8_t KeyId, const uint8_t *Key, SIZET KeySi
SIZET keyStorageArrayBlockId = ReadKeyStorageAddress(AppSlot);
SIZET keyStorageArray[DESFIRE_MAX_KEYS];
ReadBlockBytes(keyStorageArray, keyStorageArrayBlockId, 2 * DESFIRE_MAX_KEYS);
// TODO:
WriteBlockBytes(Key, keyStorageArray[KeyId], KeySize);
}

Expand Down Expand Up @@ -519,8 +508,10 @@ void SelectAppBySlot(uint8_t AppSlot) {
if (appCacheSelectedBlockId == 0) {
return;
}
SIZET prevAppCacheSelectedBlockId = AppDir.AppCacheStructBlockOffset[SelectedApp.Slot];
WriteBlockBytes(&SelectedApp, prevAppCacheSelectedBlockId, sizeof(SelectedAppCacheType));
if (SelectedApp.Slot != (uint8_t) -1) {
SIZET prevAppCacheSelectedBlockId = AppDir.AppCacheStructBlockOffset[SelectedApp.Slot];
WriteBlockBytes(&SelectedApp, prevAppCacheSelectedBlockId, sizeof(SelectedAppCacheType));
}
ReadBlockBytes(&SelectedApp, appCacheSelectedBlockId, sizeof(SelectedAppCacheType));
SelectedApp.Slot = AppSlot;
SynchronizeAppDir();
Expand Down Expand Up @@ -605,7 +596,7 @@ uint16_t CreateApp(const DESFireAidType Aid, uint8_t KeyCount, uint8_t KeySettin
appCacheData.KeyCount = 1; // Master Key
appCacheData.MaxKeyCount = KeyCount;
appCacheData.FileCount = 0;
appCacheData.CryptoCommStandard = DESFIRE_DEFAULT_COMMS_STANDARD;
appCacheData.CryptoCommStandard = DesfireCommMode;
appCacheData.KeySettings = AllocateBlocks(APP_CACHE_KEY_SETTINGS_ARRAY_BLOCK_SIZE);
if (appCacheData.KeySettings == 0) {
return STATUS_OUT_OF_EEPROM_ERROR;
Expand Down Expand Up @@ -636,7 +627,9 @@ uint16_t CreateApp(const DESFireAidType Aid, uint8_t KeyCount, uint8_t KeySettin
return STATUS_OUT_OF_EEPROM_ERROR;
} else {
SIZET fileAccessRightsData[DESFIRE_MAX_FILES];
memset(fileAccessRightsData, 0x0f, sizeof(SIZET) * DESFIRE_MAX_FILES);
for (int fidx = 0; fidx < DESFIRE_MAX_FILES; fidx++) {
fileAccessRightsData[fidx] = 0x000f;
}
WriteBlockBytes(fileAccessRightsData, appCacheData.FileAccessRights, sizeof(SIZET) * DESFIRE_MAX_FILES);
}
appCacheData.KeyVersionsArray = AllocateBlocks(APP_CACHE_KEY_VERSIONS_ARRAY_BLOCK_SIZE);
Expand All @@ -651,9 +644,9 @@ uint16_t CreateApp(const DESFireAidType Aid, uint8_t KeyCount, uint8_t KeySettin
if (appCacheData.KeyTypesArray == 0) {
return STATUS_OUT_OF_EEPROM_ERROR;
} else {
BYTE keyTypesData[APP_CACHE_KEY_TYPES_ARRAY_BLOCK_SIZE * DESFIRE_EEPROM_BLOCK_SIZE];
memset(keyTypesData, 0x00, APP_CACHE_KEY_TYPES_ARRAY_BLOCK_SIZE * DESFIRE_EEPROM_BLOCK_SIZE);
WriteBlockBytes(keyTypesData, appCacheData.KeyTypesArray, APP_CACHE_KEY_TYPES_ARRAY_BLOCK_SIZE * DESFIRE_EEPROM_BLOCK_SIZE);
BYTE keyTypesData[APP_CACHE_KEY_TYPES_ARRAY_BLOCK_SIZE * DESFIRE_BLOCK_SIZE];
memset(keyTypesData, 0x00, APP_CACHE_KEY_TYPES_ARRAY_BLOCK_SIZE * DESFIRE_BLOCK_SIZE);
WriteBlockBytes(keyTypesData, appCacheData.KeyTypesArray, APP_CACHE_KEY_TYPES_ARRAY_BLOCK_SIZE * DESFIRE_BLOCK_SIZE);
}
appCacheData.FilesAddress = AllocateBlocks(APP_CACHE_FILE_BLOCKIDS_ARRAY_BLOCK_SIZE);
if (appCacheData.FilesAddress == 0) {
Expand All @@ -676,8 +669,6 @@ 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], DESFIRE_EEPROM_BLOCK_SIZE);
//WriteBlockBytes(cryptoBlankKeyData, keyAddresses[0] + 1, CRYPTO_MAX_KEY_SIZE - DESFIRE_EEPROM_BLOCK_SIZE);
WriteBlockBytes(cryptoBlankKeyData, keyAddresses[0], CRYPTO_MAX_KEY_SIZE);
WriteBlockBytes(keyAddresses, appCacheData.KeyAddress, sizeof(SIZET) * DESFIRE_MAX_KEYS);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,46 +35,36 @@ This notice must be retained at the top of all source files where indicated.
#define DESFIRE_MAX_FILES_EV0 16
#define DESFIRE_MAX_FILES_EV1 32

#if defined(MEMORY_LIMITED_TESTING) && !defined(DESFIRE_CUSTOM_MAX_APPS)
#ifdef MEMORY_LIMITED_TESTING
#define DESFIRE_MAX_APPS (3)
#elif defined(DESFIRE_CUSTOM_MAX_APPS)
#else
#ifdef DESFIRE_CUSTOM_MAX_APPS
#define DESFIRE_MAX_APPS (DESFIRE_CUSTOM_MAX_APPS)
#else
#define DESFIRE_MAX_APPS (28)
#endif
#endif

#define DESFIRE_MAX_SLOTS (DESFIRE_MAX_APPS + 1)

#if defined(MEMORY_LIMITED_TESTING) && !defined(DESFIRE_CUSTOM_MAX_FILES)
#ifdef MEMORY_LIMITED_TESTING
#define DESFIRE_MAX_FILES (4)
#elif defined(DESFIRE_CUSTOM_MAX_FILES)
#else
#ifdef DESFIRE_CUSTOM_MAX_FILES
#define DESFIRE_MAX_FILES (DESFIRE_CUSTOM_MAX_FILES)
#else
#define DESFIRE_MAX_FILES (DESFIRE_MAX_FILES_EV1)
#define DESFIRE_MAX_FILES (DESFIRE_MAX_FILES_EV0)
#endif
#endif

#if defined(MEMORY_LIMITED_TESTING) && !defined(DESFIRE_CUSTOM_MAX_KEYS)
#ifdef MEMORY_LIMITED_TESTING
#define DESFIRE_MAX_KEYS (2)
#elif defined(DESFIRE_CUSTOM_MAX_KEYS)
#else
#ifdef DESFIRE_CUSTOM_MAX_KEYS
#define DESFIRE_MAX_KEYS (DESFIRE_CUSTOM_MAX_KEYS)
#else
#define DESFIRE_MAX_KEYS (14)
#endif

#ifdef DESFIRE_USE_FACTORY_SIZES
#undef DESFIRE_CUSTOM_MAX_APPS
#define DESFIRE_CUSTOM_MAX_APPS (28)
#undef DESFIRE_CUSTOM_MAX_KEYS
#define DESFIRE_CUSTOM_MAX_KEYS (14)
#undef DESFIRE_CUSTOM_MAX_FILES
#define DESFIRE_CUSTOM_MAX_FILES (DESFIRE_MAX_FILES_EV1)
#elif defined(DESFIRE_MAXIMIZE_SIZES_FOR_STORAGE)
#undef DESFIRE_CUSTOM_MAX_APPS
#define DESFIRE_CUSTOM_MAX_APPS (DESFIRE_EEPROM_BLOCK_SIZE - 1)
#undef DESFIRE_CUSTOM_MAX_KEYS
#define DESFIRE_CUSTOM_MAX_KEYS (DESFIRE_EEPROM_BLOCK_SIZE)
#undef DESFIRE_CUSTOM_MAX_FILES
#define DESFIRE_CUSTOM_MAX_FILES (DESFIRE_EEPROM_BLOCK_SIZE)
#endif

/* Mifare DESFire EV1 Application crypto operations */
Expand Down
7 changes: 3 additions & 4 deletions Firmware/Chameleon-Mini/Application/DESFire/DESFireCrypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,20 @@ extern BYTE DesfireCommMode;
#define CRYPTO_TYPE_3K3DES (0x1A)
#define CRYPTO_TYPE_AES128 (0x4A)

#define CryptoTypeDES(ct) \
#define CryptoTypeDES(ct) \
((ct == CRYPTO_TYPE_DES) || (ct == CRYPTO_TYPE_ANY))
#define CryptoType2KTDEA(ct) \
((ct == CRYPTO_TYPE_2KTDEA) || (ct == CRYPTO_TYPE_ANY))
#define CryptoType3KTDEA(ct) \
((ct == CRYPTO_TYPE_3K3DES) || (ct == CRYPTO_TYPE_ANY))
#define CryptoTypeAES(ct) \
#define CryptoTypeAES(ct) \
((ct == CRYPTO_TYPE_AES128) || (ct == CRYPTO_TYPE_ANY))

/* Key sizes, block sizes (in bytes): */
#define CRYPTO_AES_KEY_SIZE (16)
#define CRYPTO_MAX_KEY_SIZE (24) // (32) // Make it a multiple of the EEPROM_BLOCK_SIZE
#define CRYPTO_MAX_KEY_SIZE (24)
#define CRYPTO_MAX_BLOCK_SIZE (16)
#define DESFIRE_AES_IV_SIZE (CRYPTO_AES_BLOCK_SIZE)
#define DESFIRE_SESSION_KEY_SIZE (CRYPTO_3KTDEA_KEY_SIZE)
#define CRYPTO_CHALLENGE_RESPONSE_BYTES (16)

typedef BYTE CryptoKeyBufferType[CRYPTO_MAX_KEY_SIZE];
Expand Down
2 changes: 1 addition & 1 deletion Firmware/Chameleon-Mini/Application/DESFire/DESFireFile.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ uint16_t GetFileSizeFromFileType(DESFireFileTypeSettings *File) {
case DESFIRE_FILE_VALUE_DATA:
return sizeof(int32_t); // 4
case DESFIRE_FILE_LINEAR_RECORDS:
return (File->RecordFile.BlockCount) * DESFIRE_EEPROM_BLOCK_SIZE;
return (File->RecordFile.BlockCount) * DESFIRE_BLOCK_SIZE;
case DESFIRE_FILE_CIRCULAR_RECORDS:
default:
break;
Expand Down
Loading

0 comments on commit a02f214

Please sign in to comment.