Chameleon-Mini
DESFireFile.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  * DESFireFile.h :
24  * Maxie D. Schmidt (github.com/maxieds)
25  */
26 
27 #ifndef __DESFIRE_EFFILE_H__
28 #define __DESFIRE_EFFILE_H__
29 
30 #include "DESFireFirmwareSettings.h"
31 #include "DESFireInstructions.h"
32 
33 #define EFFILE_DATA_CHUNK_SIZE (4)
34 #define EFFILE_DATA_CHUNK_ALIGNAT __attribute__((align(EFFILE_DATA_CHUNK_SIZE)))
35 
36 // TODO: See type 4 tags on CC and NTAG AIDs and FIDs
37 #define ISO7816_4_CURRENT_EF_FILE_ID 0x0000
38 #define ISO7816_4_CURRENT_DF_FILE_ID 0x3FFF
39 #define ISO7816_4_MASTER_FILE_ID 0x3F00
40 
41 /* File types */
42 
43 // NOTE: See pages 48-49 of the datasheet for more details:
44 #define DESFIRE_FILE_STANDARD_DATA 0
45 #define DESFIRE_FILE_BACKUP_DATA 1
46 #define DESFIRE_FILE_VALUE_DATA 2
47 #define DESFIRE_FILE_LINEAR_RECORDS 3
48 #define DESFIRE_FILE_CIRCULAR_RECORDS 4
49 
50 #define DESFIRE_FILE_NOFILE_INDEX (0xff)
51 
52 /* Data about an application's file is currently kept in this structure.
53  * The location of these structures is defined by the file index.
54  */
55 typedef struct DESFIRE_FIRMWARE_PACKING {
56  uint8_t FileType;
57  uint8_t FileNumber;
58  uint16_t FileSize;
59  uint16_t FileDataAddress; /* FRAM address of the storage of the data for the file */
60  union DESFIRE_FIRMWARE_ALIGNAT {
61  struct DESFIRE_FIRMWARE_ALIGNAT {
62  uint16_t FileSize;
63  } StandardFile;
64  struct DESFIRE_FIRMWARE_ALIGNAT {
65  uint16_t FileSize;
66  uint8_t BlockCount;
67  } BackupFile;
68  struct DESFIRE_FIRMWARE_ALIGNAT {
69  int32_t CleanValue;
70  int32_t DirtyValue;
71  int32_t LowerLimit;
72  int32_t UpperLimit;
73  uint8_t LimitedCreditEnabled;
74  int32_t PreviousDebit;
75  } ValueFile;
76  struct DESFIRE_FIRMWARE_ALIGNAT {
77  uint16_t BlockCount;
78  uint16_t RecordPointer;
79  //uint8_t ClearPending; // USED ???
80  uint8_t RecordSize[3];
81  uint8_t CurrentNumRecords[3];
82  uint8_t MaxRecordCount[3];
83  } RecordFile;
84  };
85 } DESFireFileTypeSettings;
86 
87 uint16_t GetFileSizeFromFileType(DESFireFileTypeSettings *File);
88 
89 typedef struct DESFIRE_FIRMWARE_PACKING {
90  BYTE Num;
91  DESFireFileTypeSettings File;
92 } SelectedFileCacheType;
93 
94 /*
95  * File management: creation, deletion, and misc routines
96  */
97 
98 uint16_t GetFileDataAreaBlockId(uint8_t FileIndex);
99 uint8_t ReadFileControlBlockIntoCacheStructure(uint8_t FileNum, SelectedFileCacheType *FileCache);
100 uint8_t ReadFileControlBlock(uint8_t FileNum, DESFireFileTypeSettings *File);
101 uint8_t WriteFileControlBlock(uint8_t FileNum, DESFireFileTypeSettings *File);
102 
103 /* Creation and deletion */
104 uint8_t CreateFileHeaderData(uint8_t FileNum, uint8_t CommSettings,
105  uint16_t AccessRights, DESFireFileTypeSettings *File);
106 uint8_t CreateStandardFile(uint8_t FileNum, uint8_t CommSettings, uint16_t AccessRights, uint16_t FileSize);
107 uint8_t CreateBackupFile(uint8_t FileNum, uint8_t CommSettings, uint16_t AccessRights, uint16_t FileSize);
108 uint8_t CreateValueFile(uint8_t FileNum, uint8_t CommSettings, uint16_t AccessRights,
109  int32_t LowerLimit, int32_t UpperLimit, int32_t Value, bool LimitedCreditEnabled);
110 uint8_t CreateRecordFile(uint8_t FileType, uint8_t FileNum, uint8_t CommSettings, uint16_t AccessRights,
111  uint8_t *RecordSize, uint8_t *MaxRecordSize);
112 uint8_t DeleteFile(uint8_t FileIndex);
113 
114 /* Transactions */
115 
116 /* File transfers */
117 TransferStatus ReadDataFileTransfer(uint8_t *Buffer);
118 uint8_t WriteDataFileTransfer(uint8_t *Buffer, uint8_t ByteCount);
119 uint8_t ReadDataFileSetup(uint8_t FileIndex, uint8_t CommSettings, uint16_t Offset, uint16_t Length);
120 uint8_t WriteDataFileSetup(uint8_t FileIndex, uint8_t FileType, uint8_t CommSettings, uint16_t Offset, uint16_t Length);
121 uint16_t ReadDataFileIterator(uint8_t *Buffer);
122 uint8_t WriteDataFileInternal(uint8_t *Buffer, uint16_t ByteCount);
123 uint16_t WriteDataFileIterator(uint8_t *Buffer, uint16_t ByteCount);
124 
125 /* Special values for access key IDs */
126 #define DESFIRE_ACCESS_FREE 0xE
127 #define DESFIRE_ACCESS_DENY 0xF
128 
129 /* Validation routines */
130 #define VALIDATE_ACCESS_READWRITE ((uint16_t) (0x000f))
131 #define VALIDATE_ACCESS_WRITE ((uint16_t) (0x000f << 4))
132 #define VALIDATE_ACCESS_READ ((uint16_t) (0x000f << 8))
133 #define VALIDATE_ACCESS_CHANGE ((uint16_t) (0x000f << 12))
134 
135 #define VALIDATED_ACCESS_DENIED 0
136 #define VALIDATED_ACCESS_GRANTED 1
137 #define VALIDATED_ACCESS_GRANTED_PLAINTEXT 2
138 
139 /*
140  * [READ (4 bits) | WRITE | READ-WRITE | CHANGE]
141  * Stored in little endian format from memory:
142  */
143 #define GetReadPermissions(AccessRights) \
144  (BYTE) (AccessRights & 0x000f)
145 #define GetWritePermissions(AccessRights) \
146  (BYTE) (((0x00f0 & AccessRights) >> 4) & 0x000f)
147 #define GetReadWritePermissions(AccessRights) \
148  (BYTE) (((0x0f00 & AccessRights) >> 8) & 0x000f)
149 #define GetChangePermissions(AccessRights) \
150  (BYTE) (((0xf000 & AccessRights) >> 12) & 0x000f)
151 
152 const char *GetFileAccessPermissionsDesc(uint16_t fileAccessRights);
153 
154 /*
155  * There are also command/instruction-wise
156  * citations given from file's access permissions. This data is taken
157  * from the table on page 21 of the NXP application note:
158  * https://www.nxp.com/docs/en/application-note/AN12343.pdf
159  */
160 uint8_t CreateFileCommonValidation(uint8_t FileNum);
161 uint8_t ValidateAuthentication(uint16_t AccessRights, uint16_t CheckMask);
162 
163 #endif