Skip to content

Commit a02f214

Browse files
committed
Working AES128 auth and ISO auth ; Still need to debug the legacy auth
1 parent b451e12 commit a02f214

22 files changed

+146
-271
lines changed

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

+13-22
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ BYTE AMKAllKeysFrozen(void) {
121121
}
122122

123123
SIZET GetAppProperty(DesfireCardLayout propId, BYTE AppSlot) {
124-
if (AppSlot >= DESFIRE_MAX_SLOTS) {
124+
if (AppSlot >= AppDir.FirstFreeSlot || AppSlot >= DESFIRE_MAX_SLOTS) {
125125
return 0x00;
126126
}
127127
SelectedAppCacheType appCache;
@@ -157,7 +157,7 @@ SIZET GetAppProperty(DesfireCardLayout propId, BYTE AppSlot) {
157157
}
158158

159159
void SetAppProperty(DesfireCardLayout propId, BYTE AppSlot, SIZET Value) {
160-
if (AppSlot >= DESFIRE_MAX_SLOTS) {
160+
if (AppSlot >= AppDir.FirstFreeSlot || AppSlot >= DESFIRE_MAX_SLOTS) {
161161
return;
162162
}
163163
SelectedAppCacheType appCache;
@@ -308,16 +308,6 @@ void ReadAppKey(uint8_t AppSlot, uint8_t KeyId, uint8_t *Key, SIZET KeySize) {
308308
SIZET keyStorageArray[DESFIRE_MAX_KEYS];
309309
ReadBlockBytes(keyStorageArray, keyStorageArrayBlockId, 2 * DESFIRE_MAX_KEYS);
310310
ReadBlockBytes(Key, keyStorageArray[KeyId], KeySize);
311-
/*if (KeySize > DESFIRE_EEPROM_BLOCK_SIZE) {
312-
ReadBlockBytes(Key, keyStorageArray[KeyId], DESFIRE_EEPROM_BLOCK_SIZE);
313-
uint8_t fullBlock[DESFIRE_EEPROM_BLOCK_SIZE];
314-
ReadBlockBytes(fullBlock, keyStorageArray[KeyId] + 1, DESFIRE_EEPROM_BLOCK_SIZE);
315-
memcpy(Key + DESFIRE_EEPROM_BLOCK_SIZE, fullBlock, KeySize - DESFIRE_EEPROM_BLOCK_SIZE);
316-
} else {
317-
uint8_t fullBlock[DESFIRE_EEPROM_BLOCK_SIZE];
318-
ReadBlockBytes(fullBlock, keyStorageArray[KeyId], DESFIRE_EEPROM_BLOCK_SIZE);
319-
memcpy(Key, fullBlock, KeySize);
320-
}*/
321311
}
322312

323313
void WriteAppKey(uint8_t AppSlot, uint8_t KeyId, const uint8_t *Key, SIZET KeySize) {
@@ -329,7 +319,6 @@ void WriteAppKey(uint8_t AppSlot, uint8_t KeyId, const uint8_t *Key, SIZET KeySi
329319
SIZET keyStorageArrayBlockId = ReadKeyStorageAddress(AppSlot);
330320
SIZET keyStorageArray[DESFIRE_MAX_KEYS];
331321
ReadBlockBytes(keyStorageArray, keyStorageArrayBlockId, 2 * DESFIRE_MAX_KEYS);
332-
// TODO:
333322
WriteBlockBytes(Key, keyStorageArray[KeyId], KeySize);
334323
}
335324

@@ -519,8 +508,10 @@ void SelectAppBySlot(uint8_t AppSlot) {
519508
if (appCacheSelectedBlockId == 0) {
520509
return;
521510
}
522-
SIZET prevAppCacheSelectedBlockId = AppDir.AppCacheStructBlockOffset[SelectedApp.Slot];
523-
WriteBlockBytes(&SelectedApp, prevAppCacheSelectedBlockId, sizeof(SelectedAppCacheType));
511+
if (SelectedApp.Slot != (uint8_t) -1) {
512+
SIZET prevAppCacheSelectedBlockId = AppDir.AppCacheStructBlockOffset[SelectedApp.Slot];
513+
WriteBlockBytes(&SelectedApp, prevAppCacheSelectedBlockId, sizeof(SelectedAppCacheType));
514+
}
524515
ReadBlockBytes(&SelectedApp, appCacheSelectedBlockId, sizeof(SelectedAppCacheType));
525516
SelectedApp.Slot = AppSlot;
526517
SynchronizeAppDir();
@@ -605,7 +596,7 @@ uint16_t CreateApp(const DESFireAidType Aid, uint8_t KeyCount, uint8_t KeySettin
605596
appCacheData.KeyCount = 1; // Master Key
606597
appCacheData.MaxKeyCount = KeyCount;
607598
appCacheData.FileCount = 0;
608-
appCacheData.CryptoCommStandard = DESFIRE_DEFAULT_COMMS_STANDARD;
599+
appCacheData.CryptoCommStandard = DesfireCommMode;
609600
appCacheData.KeySettings = AllocateBlocks(APP_CACHE_KEY_SETTINGS_ARRAY_BLOCK_SIZE);
610601
if (appCacheData.KeySettings == 0) {
611602
return STATUS_OUT_OF_EEPROM_ERROR;
@@ -636,7 +627,9 @@ uint16_t CreateApp(const DESFireAidType Aid, uint8_t KeyCount, uint8_t KeySettin
636627
return STATUS_OUT_OF_EEPROM_ERROR;
637628
} else {
638629
SIZET fileAccessRightsData[DESFIRE_MAX_FILES];
639-
memset(fileAccessRightsData, 0x0f, sizeof(SIZET) * DESFIRE_MAX_FILES);
630+
for (int fidx = 0; fidx < DESFIRE_MAX_FILES; fidx++) {
631+
fileAccessRightsData[fidx] = 0x000f;
632+
}
640633
WriteBlockBytes(fileAccessRightsData, appCacheData.FileAccessRights, sizeof(SIZET) * DESFIRE_MAX_FILES);
641634
}
642635
appCacheData.KeyVersionsArray = AllocateBlocks(APP_CACHE_KEY_VERSIONS_ARRAY_BLOCK_SIZE);
@@ -651,9 +644,9 @@ uint16_t CreateApp(const DESFireAidType Aid, uint8_t KeyCount, uint8_t KeySettin
651644
if (appCacheData.KeyTypesArray == 0) {
652645
return STATUS_OUT_OF_EEPROM_ERROR;
653646
} else {
654-
BYTE keyTypesData[APP_CACHE_KEY_TYPES_ARRAY_BLOCK_SIZE * DESFIRE_EEPROM_BLOCK_SIZE];
655-
memset(keyTypesData, 0x00, APP_CACHE_KEY_TYPES_ARRAY_BLOCK_SIZE * DESFIRE_EEPROM_BLOCK_SIZE);
656-
WriteBlockBytes(keyTypesData, appCacheData.KeyTypesArray, APP_CACHE_KEY_TYPES_ARRAY_BLOCK_SIZE * DESFIRE_EEPROM_BLOCK_SIZE);
647+
BYTE keyTypesData[APP_CACHE_KEY_TYPES_ARRAY_BLOCK_SIZE * DESFIRE_BLOCK_SIZE];
648+
memset(keyTypesData, 0x00, APP_CACHE_KEY_TYPES_ARRAY_BLOCK_SIZE * DESFIRE_BLOCK_SIZE);
649+
WriteBlockBytes(keyTypesData, appCacheData.KeyTypesArray, APP_CACHE_KEY_TYPES_ARRAY_BLOCK_SIZE * DESFIRE_BLOCK_SIZE);
657650
}
658651
appCacheData.FilesAddress = AllocateBlocks(APP_CACHE_FILE_BLOCKIDS_ARRAY_BLOCK_SIZE);
659652
if (appCacheData.FilesAddress == 0) {
@@ -676,8 +669,6 @@ uint16_t CreateApp(const DESFireAidType Aid, uint8_t KeyCount, uint8_t KeySettin
676669
}
677670
BYTE cryptoBlankKeyData[CRYPTO_MAX_KEY_SIZE];
678671
memset(cryptoBlankKeyData, 0x00, CRYPTO_MAX_KEY_SIZE);
679-
//WriteBlockBytes(cryptoBlankKeyData, keyAddresses[0], DESFIRE_EEPROM_BLOCK_SIZE);
680-
//WriteBlockBytes(cryptoBlankKeyData, keyAddresses[0] + 1, CRYPTO_MAX_KEY_SIZE - DESFIRE_EEPROM_BLOCK_SIZE);
681672
WriteBlockBytes(cryptoBlankKeyData, keyAddresses[0], CRYPTO_MAX_KEY_SIZE);
682673
WriteBlockBytes(keyAddresses, appCacheData.KeyAddress, sizeof(SIZET) * DESFIRE_MAX_KEYS);
683674
}

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

+12-22
Original file line numberDiff line numberDiff line change
@@ -35,46 +35,36 @@ This notice must be retained at the top of all source files where indicated.
3535
#define DESFIRE_MAX_FILES_EV0 16
3636
#define DESFIRE_MAX_FILES_EV1 32
3737

38-
#if defined(MEMORY_LIMITED_TESTING) && !defined(DESFIRE_CUSTOM_MAX_APPS)
38+
#ifdef MEMORY_LIMITED_TESTING
3939
#define DESFIRE_MAX_APPS (3)
40-
#elif defined(DESFIRE_CUSTOM_MAX_APPS)
40+
#else
41+
#ifdef DESFIRE_CUSTOM_MAX_APPS
4142
#define DESFIRE_MAX_APPS (DESFIRE_CUSTOM_MAX_APPS)
4243
#else
4344
#define DESFIRE_MAX_APPS (28)
4445
#endif
46+
#endif
4547

4648
#define DESFIRE_MAX_SLOTS (DESFIRE_MAX_APPS + 1)
4749

48-
#if defined(MEMORY_LIMITED_TESTING) && !defined(DESFIRE_CUSTOM_MAX_FILES)
50+
#ifdef MEMORY_LIMITED_TESTING
4951
#define DESFIRE_MAX_FILES (4)
50-
#elif defined(DESFIRE_CUSTOM_MAX_FILES)
52+
#else
53+
#ifdef DESFIRE_CUSTOM_MAX_FILES
5154
#define DESFIRE_MAX_FILES (DESFIRE_CUSTOM_MAX_FILES)
5255
#else
53-
#define DESFIRE_MAX_FILES (DESFIRE_MAX_FILES_EV1)
56+
#define DESFIRE_MAX_FILES (DESFIRE_MAX_FILES_EV0)
57+
#endif
5458
#endif
5559

56-
#if defined(MEMORY_LIMITED_TESTING) && !defined(DESFIRE_CUSTOM_MAX_KEYS)
60+
#ifdef MEMORY_LIMITED_TESTING
5761
#define DESFIRE_MAX_KEYS (2)
58-
#elif defined(DESFIRE_CUSTOM_MAX_KEYS)
62+
#else
63+
#ifdef DESFIRE_CUSTOM_MAX_KEYS
5964
#define DESFIRE_MAX_KEYS (DESFIRE_CUSTOM_MAX_KEYS)
6065
#else
6166
#define DESFIRE_MAX_KEYS (14)
6267
#endif
63-
64-
#ifdef DESFIRE_USE_FACTORY_SIZES
65-
#undef DESFIRE_CUSTOM_MAX_APPS
66-
#define DESFIRE_CUSTOM_MAX_APPS (28)
67-
#undef DESFIRE_CUSTOM_MAX_KEYS
68-
#define DESFIRE_CUSTOM_MAX_KEYS (14)
69-
#undef DESFIRE_CUSTOM_MAX_FILES
70-
#define DESFIRE_CUSTOM_MAX_FILES (DESFIRE_MAX_FILES_EV1)
71-
#elif defined(DESFIRE_MAXIMIZE_SIZES_FOR_STORAGE)
72-
#undef DESFIRE_CUSTOM_MAX_APPS
73-
#define DESFIRE_CUSTOM_MAX_APPS (DESFIRE_EEPROM_BLOCK_SIZE - 1)
74-
#undef DESFIRE_CUSTOM_MAX_KEYS
75-
#define DESFIRE_CUSTOM_MAX_KEYS (DESFIRE_EEPROM_BLOCK_SIZE)
76-
#undef DESFIRE_CUSTOM_MAX_FILES
77-
#define DESFIRE_CUSTOM_MAX_FILES (DESFIRE_EEPROM_BLOCK_SIZE)
7868
#endif
7969

8070
/* Mifare DESFire EV1 Application crypto operations */

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,20 @@ extern BYTE DesfireCommMode;
5353
#define CRYPTO_TYPE_3K3DES (0x1A)
5454
#define CRYPTO_TYPE_AES128 (0x4A)
5555

56-
#define CryptoTypeDES(ct) \
56+
#define CryptoTypeDES(ct) \
5757
((ct == CRYPTO_TYPE_DES) || (ct == CRYPTO_TYPE_ANY))
5858
#define CryptoType2KTDEA(ct) \
5959
((ct == CRYPTO_TYPE_2KTDEA) || (ct == CRYPTO_TYPE_ANY))
6060
#define CryptoType3KTDEA(ct) \
6161
((ct == CRYPTO_TYPE_3K3DES) || (ct == CRYPTO_TYPE_ANY))
62-
#define CryptoTypeAES(ct) \
62+
#define CryptoTypeAES(ct) \
6363
((ct == CRYPTO_TYPE_AES128) || (ct == CRYPTO_TYPE_ANY))
6464

6565
/* Key sizes, block sizes (in bytes): */
6666
#define CRYPTO_AES_KEY_SIZE (16)
67-
#define CRYPTO_MAX_KEY_SIZE (24) // (32) // Make it a multiple of the EEPROM_BLOCK_SIZE
67+
#define CRYPTO_MAX_KEY_SIZE (24)
6868
#define CRYPTO_MAX_BLOCK_SIZE (16)
6969
#define DESFIRE_AES_IV_SIZE (CRYPTO_AES_BLOCK_SIZE)
70-
#define DESFIRE_SESSION_KEY_SIZE (CRYPTO_3KTDEA_KEY_SIZE)
7170
#define CRYPTO_CHALLENGE_RESPONSE_BYTES (16)
7271

7372
typedef BYTE CryptoKeyBufferType[CRYPTO_MAX_KEY_SIZE];

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ uint16_t GetFileSizeFromFileType(DESFireFileTypeSettings *File) {
4848
case DESFIRE_FILE_VALUE_DATA:
4949
return sizeof(int32_t); // 4
5050
case DESFIRE_FILE_LINEAR_RECORDS:
51-
return (File->RecordFile.BlockCount) * DESFIRE_EEPROM_BLOCK_SIZE;
51+
return (File->RecordFile.BlockCount) * DESFIRE_BLOCK_SIZE;
5252
case DESFIRE_FILE_CIRCULAR_RECORDS:
5353
default:
5454
break;

0 commit comments

Comments
 (0)