Chameleon-Mini
DESFirePICCHeaderLayout.h
1 /*
2 The DESFire stack portion of this firmware source
3 is free software written by Maxie Dion Schmidt (@maxieds):
4 You can redistribute it and/or modify
5 it under the terms of this license.
6 
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 
11 The complete source distribution of
12 this firmware is available at the following link:
13 https://github.com/maxieds/ChameleonMiniFirmwareDESFireStack.
14 
15 Based in part on the original DESFire code created by
16 @dev-zzo (GitHub handle) [Dmitry Janushkevich] available at
17 https://github.com/dev-zzo/ChameleonMini/tree/desfire.
18 
19 This notice must be retained at the top of all source files where indicated.
20 */
21 
22 /*
23  * DESFirePICCHeaderLayout.h
24  * Maxie D. Schmidt (github.com/maxieds)
25  */
26 
27 #ifndef __DESFIRE_PICC_HDRLAYOUT_H__
28 #define __DESFIRE_PICC_HDRLAYOUT_H__
29 
30 #include "DESFireFirmwareSettings.h"
31 #include "DESFireISO14443Support.h"
32 
33 #define DESFIRE_PICC_APP_SLOT 0x00
34 #define DESFIRE_MASTER_KEY_ID 0x00
35 
36 #define DESFIRE_NATIVE_CLA 0x90
37 #define DESFIRE_ISO7816_CLA 0x00
38 
39 /* Storage allocation constants */
40 #define DESFIRE_EEPROM_BLOCK_SIZE 16 //32 /* Bytes */
41 #define DESFIRE_BYTES_TO_BLOCKS(x) \
42  ( ((x) + DESFIRE_EEPROM_BLOCK_SIZE - 1) / DESFIRE_EEPROM_BLOCK_SIZE )
43 
44 #define DESFIRE_UID_SIZE ISO14443A_UID_SIZE_DOUBLE
45 
46 #define DESFIRE_MAX_PAYLOAD_SIZE 55 // 64 /* Bytes */
47 
48 /*
49  * Definitions pertaining to on-card data
50  */
51 
52 /* Anticollision parameters */
53 #define ATQA_VALUE 0x0344
54 #define SAK_CL1_VALUE (ISO14443A_SAK_INCOMPLETE)
55 #define SAK_CL2_VALUE (ISO14443A_SAK_COMPLETE_COMPLIANT)
56 #define SAK_UID_NOT_FINISHED 0x04
57 
58 #define STATUS_FRAME_SIZE (1 * 8) /* Bits */
59 
60 #define DESFIRE_EV0_ATS_TL_BYTE 0x06 /* TL: ATS length, 6 bytes */
61 #define DESFIRE_EV0_ATS_T0_BYTE 0x75 /* T0: TA, TB, TC present; max accepted frame is 64 bytes */
62 #define DESFIRE_EV0_ATS_TA_BYTE 0x00 /* TA: Only the lowest bit rate is supported (normal is 0x77) */
63 #define DESFIRE_EV0_ATS_TB_BYTE 0x81 /* TB: taken from the DESFire spec */
64 #define DESFIRE_EV0_ATS_TC_BYTE 0x02 /* TC: taken from the DESFire spec */
65 
66 #define GET_LE16(p) (*((uint16_t*)&(p)[0]))
67 #define GET_LE24(p) (*((__uint24*)&(p)[0]))
68 #define GET_LE32(p) (*((uint32_t*)&(p)[0]))
69 
70 /* Defines for GetVersion */
71 #define ID_PHILIPS_NXP 0x04
72 #define DESFIRE_MANUFACTURER_ID ID_PHILIPS_NXP
73 
74 /* These do not change */
75 #define DESFIRE_TYPE 0x01
76 #define DESFIRE_SUBTYPE 0x01
77 #define DESFIRE_HW_PROTOCOL_TYPE 0x05
78 #define DESFIRE_SW_PROTOCOL_TYPE 0x05
79 
81 /* DESFire EV0 versions */
82 #define DESFIRE_HW_MAJOR_EV0 0x00
83 #define DESFIRE_HW_MINOR_EV0 0x01
84 #define DESFIRE_SW_MAJOR_EV0 0x00
85 #define DESFIRE_SW_MINOR_EV0 0x01
86 
87 #define IsPiccEV0(picc) \
88  (picc.HwVersionMajor == DESFIRE_HW_MAJOR_EV0 && picc.SwVersionMajor == DESFIRE_SW_MAJOR_EV0)
89 
90 /* DESFire EV1 versions */
91 #define DESFIRE_HW_MAJOR_EV1 0x01
92 #define DESFIRE_HW_MINOR_EV1 0x01
93 #define DESFIRE_SW_MAJOR_EV1 0x01
94 #define DESFIRE_SW_MINOR_EV1 0x01
95 
96 /* DESFire EV2 versions */
97 #define DESFIRE_HW_MAJOR_EV2 0x12
98 #define DESFIRE_HW_MINOR_EV2 0x01
99 #define DESFIRE_SW_MAJOR_EV2 0x12
100 #define DESFIRE_SW_MINOR_EV2 0x01
101 
102 /* Other HW product types for DESFire tags: See page 7 of
103  * https://www.nxp.com/docs/en/application-note/AN12343.pdf
104  */
105 // typedef enum DESFIRE_FIRMWARE_ENUM_PACKING {
106 // NATIVEIC_PHYS_CARD = 0x01,
107 // LIGHT_NATIVEIC_PHYS_CARD = 0x08,
108 // MICROCONTROLLER_PHYS_CARDI = 0x81,
109 // MICROCONTROLLER_PHYS_CARDII = 0x83,
110 // JAVACARD_SECURE_ELEMENT_PHYS_CARD = 0x91,
111 // HCE_MIFARE_2GO = 0xa1,
112 // } DESFireHWProductCodes;
113 //
114 // const BYTE DefaultDESFireATS[] = {
115 // 0x06, 0x75, 0x77, 0x81, 0x02, 0x80
116 // };
117 // const BYTE DefaultJCOPDESFireATS[] = {
118 // 0x06, 0x75, 0xf7, 0xb1, 0x02, 0x80
119 // };
120 //
121 // const BYTE DEFAULT_SELECT_DESFIRE_AID[] = {
122 // 0xd2, 0x76, 0x00, 0x00, 0x85, 0x01, 0x00
123 // };
124 // const BYTE DEFAULT_SELECT_ISO7816_AID[] = {
125 // 0xa0, 0x00, 0x00, 0x00, 0x03, 0x96
126 // };
127 //
128 // const BYTE VERSION1[] = {
129 // 0x04, 0x01, 0x01, 0x01, 0x00, 0x1a, 0x05
130 // };
131 // const BYTE VERSION2[] = {
132 // 0x04, 0x01, 0x01, 0x01, 0x03, 0x1a, 0x05
133 // };
134 // const BYTE VERSION3[] = {
135 // // Expected Response: 00 04 91 3a 29 93 26 80 00 00 00 00 00 39 08 91 00
136 // 0x04, (BYTE) 0x91, 0x3a, 0x29, (BYTE) 0x93,
137 // 0x26, (BYTE) 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x39, 0x08
138 // };
139 
140 #define DESFIRE_STORAGE_SIZE_2K 0x16
141 #define DESFIRE_STORAGE_SIZE_4K 0x18
142 #define DESFIRE_STORAGE_SIZE_8K 0x1A
143 
144 /*
145  * Defines the global PICC configuration.
146  * This is located in the very first block on the card.
147  */
148 #define PICC_FORMAT_BYTE (0xff)
149 #define PICC_EMPTY_BYTE (0x00)
150 
151 typedef struct DESFIRE_FIRMWARE_PACKING {
152  /* Static data: does not change during the PICC's lifetime.
153  * We will add Chameleon Mini terminal commands to enable
154  * resetting this data so tags can be emulated authentically.
155  * This structure is stored verbatim (using memcpy) at the
156  * start of the FRAM setting space for the configuration.
157  */
158  uint8_t Uid[DESFIRE_UID_SIZE] DESFIRE_FIRMWARE_ALIGNAT;
159  uint8_t StorageSize;
160  uint8_t HwVersionMajor;
161  uint8_t HwVersionMinor;
162  uint8_t SwVersionMajor;
163  uint8_t SwVersionMinor;
164  uint8_t BatchNumber[5] DESFIRE_FIRMWARE_ALIGNAT;
165  uint8_t ProductionWeek;
166  uint8_t ProductionYear;
167  uint8_t ATSBytes[5];
168  /* Dynamic data: changes during the PICC's lifetime */
169  uint16_t FirstFreeBlock;
170  uint8_t TransactionStarted; // USED ???
171  uint8_t Spare[9] DESFIRE_FIRMWARE_ALIGNAT; // USED ???
172 } DESFirePICCInfoType;
173 
174 typedef struct DESFIRE_FIRMWARE_PACKING {
175  BYTE Slot;
176  BYTE KeyCount;
177  BYTE MaxKeyCount;
178  BYTE FileCount;
179  BYTE CryptoCommStandard;
180  SIZET KeySettings; /* Block offset in FRAM */
181  SIZET FileNumbersArrayMap; /* Block offset in FRAM */
182  SIZET FileCommSettings; /* Block offset in FRAM */
183  SIZET FileAccessRights; /* Block offset in FRAM */
184  SIZET FilesAddress; /* Block offset in FRAM */
185  SIZET KeyVersionsArray; /* Block offset in FRAM */
186  SIZET KeyTypesArray; /* Block offset in FRAM */
187  SIZET KeyAddress; /* Block offset in FRAM */
188  UINT DirtyFlags; // USED ???
189 } SelectedAppCacheType;
190 
191 extern BYTE SELECTED_APP_CACHE_TYPE_BLOCK_SIZE;
192 extern BYTE APP_CACHE_KEY_SETTINGS_ARRAY_BLOCK_SIZE;
193 extern BYTE APP_CACHE_FILE_NUMBERS_HASHMAP_BLOCK_SIZE;
194 extern BYTE APP_CACHE_FILE_COMM_SETTINGS_ARRAY_BLOCK_SIZE;
195 extern BYTE APP_CACHE_FILE_ACCESS_RIGHTS_ARRAY_BLOCK_SIZE;
196 extern BYTE APP_CACHE_KEY_VERSIONS_ARRAY_BLOCK_SIZE;
197 extern BYTE APP_CACHE_KEY_TYPES_ARRAY_BLOCK_SIZE;
198 extern BYTE APP_CACHE_KEY_BLOCKIDS_ARRAY_BLOCK_SIZE;
199 extern BYTE APP_CACHE_FILE_BLOCKIDS_ARRAY_BLOCK_SIZE;
200 extern BYTE APP_CACHE_MAX_KEY_BLOCK_SIZE;
201 
202 extern SIZET DESFIRE_PICC_INFO_BLOCK_ID;
203 extern SIZET DESFIRE_APP_DIR_BLOCK_ID;
204 extern SIZET DESFIRE_APP_CACHE_DATA_ARRAY_BLOCK_ID;
205 extern SIZET DESFIRE_INITIAL_FIRST_FREE_BLOCK_ID;
206 extern SIZET DESFIRE_FIRST_FREE_BLOCK_ID;
207 extern SIZET CardCapacityBlocks;
208 
209 typedef enum DESFIRE_FIRMWARE_ENUM_PACKING {
210  /* AppData keeping track how many keys each app has */
211  DESFIRE_APP_KEY_COUNT,
212  DESFIRE_APP_MAX_KEY_COUNT,
213  /* AppData active file count */
214  DESFIRE_APP_FILE_COUNT,
215  /* AppData keep track of default crypto comm standard */
216  DESFIRE_APP_CRYPTO_COMM_STANDARD,
217  /* AppData keeping track of apps key settings */
218  DESFIRE_APP_KEY_SETTINGS_BLOCK_ID,
219  /* AppData hash-like unsorted array mapping file indices to their labeled numbers */
220  DESFIRE_APP_FILE_NUMBER_ARRAY_MAP_BLOCK_ID,
221  /* AppData communication settings (crypto transfer protocols) per file */
222  DESFIRE_APP_FILE_COMM_SETTINGS_BLOCK_ID,
223  /* AppData file access rights */
224  DESFIRE_APP_FILE_ACCESS_RIGHTS_BLOCK_ID,
225  /* AppData keep track of newer EVx revisions key versioning schemes */
226  DESFIRE_APP_KEY_VERSIONS_ARRAY_BLOCK_ID,
227  /* AppData keep track of the key types (and hence, byte sizes) by crypto method */
228  DESFIRE_APP_KEY_TYPES_ARRAY_BLOCK_ID,
229  /* AppData keeping track of apps file index blocks */
230  DESFIRE_APP_FILES_PTR_BLOCK_ID,
231  /* AppData keeping track of apps key locations */
232  DESFIRE_APP_KEYS_PTR_BLOCK_ID,
233 } DesfireCardLayout;
234 
235 SIZET PrettyPrintPICCHeaderData(BYTE *outputBuffer, SIZET maxLength, BYTE verbose);
236 SIZET PrettyPrintFileContentsData(BYTE *outputBuffer, SIZET maxLength, BYTE fileNumber);
237 SIZET PrettyPrintPICCFile(SelectedAppCacheType *appData, uint8_t fileIndex,
238  BYTE *outputBuffer, SIZET maxLength, BYTE verbose);
239 SIZET PrettyPrintPICCFilesFull(SelectedAppCacheType *appData, BYTE *outputBuffer, SIZET maxLength, BYTE verbose);
240 SIZET PrettyPrintPICCKey(SelectedAppCacheType *appData, uint8_t keyIndex,
241  BYTE *outputBuffer, SIZET maxLength, BYTE verbose);
242 SIZET PrettyPrintPICCKeysFull(SelectedAppCacheType *appData, BYTE *outputBuffer, SIZET maxLength, BYTE verbose);
243 SIZET PrettyPrintPICCAppDir(uint8_t appIndex,
244  BYTE *outputBuffer, SIZET maxLength, BYTE verbose);
245 SIZET PrettyPrintPICCAppDirsFull(BYTE *outputBuffer, SIZET maxLength, BYTE verbose);
246 SIZET PrettyPrintPICCImageData(BYTE *outputBuffer, SIZET maxLength, BYTE verbose);
247 
248 #endif