Chameleon-Mini
DESFirePICCControl.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  * DESFirePICCControl.h :
24  * Maxie D. Schmidt (github.com/maxieds)
25  */
26 
27 #ifndef __DESFIRE_PICC_CONTROL_H__
28 #define __DESFIRE_PICC_CONTROL_H__
29 
30 #include "../../Configuration.h"
31 
32 #include "DESFireFirmwareSettings.h"
33 #include "DESFirePICCHeaderLayout.h"
34 #include "DESFireInstructions.h"
35 #include "DESFireApplicationDirectory.h"
36 #include "DESFireFile.h"
37 #include "DESFireCrypto.h"
38 
39 /*
40  * Internal state variables:
41  */
42 
43 /* Cached data: flush to FRAM or relevant EEPROM addresses if changed */
44 extern DESFirePICCInfoType Picc;
45 extern DESFireAppDirType AppDir;
46 
47 /* Cached app data */
48 extern SelectedAppCacheType SelectedApp;
49 extern SelectedFileCacheType SelectedFile;
50 
51 typedef void (*TransferSourceFuncType)(BYTE *Buffer, BYTE Count);
52 typedef void (*TransferSinkFuncType)(BYTE *Buffer, BYTE Count);
53 typedef void (*TransferChecksumUpdateFuncType)(const BYTE *Buffer, BYTE Count);
54 typedef BYTE(*TransferChecksumFinalFuncType)(BYTE *Buffer);
55 typedef BYTE(*TransferEncryptFuncType)(BYTE *Buffer, BYTE Count);
56 typedef TransferStatus(*PiccToPcdTransferFilterFuncType)(BYTE *Buffer);
57 typedef BYTE(*PcdToPiccTransferFilterFuncType)(BYTE *Buffer, BYTE Count);
58 
59 /* Stored transfer state for all transfers */
60 typedef union DESFIRE_FIRMWARE_PACKING {
61  struct DESFIRE_FIRMWARE_ALIGNAT {
62  BYTE NextIndex;
63  } GetApplicationIds;
64  BYTE BlockBuffer[CRYPTO_MAX_BLOCK_SIZE];
65  struct DESFIRE_FIRMWARE_ALIGNAT {
66  TransferChecksumUpdateFuncType UpdateFunc;
67  TransferChecksumFinalFuncType FinalFunc;
68  BYTE AvailablePlaintext;
69  struct DESFIRE_FIRMWARE_ALIGNAT {
70  union DESFIRE_FIRMWARE_ALIGNAT {
71  CryptoAESCBCFuncType AESFunc;
72  CryptoTDEACBCFuncType TDEAFunc;
73  } CryptoChecksumFunc;
74  union {
75  SIZET CRCA;
76  UINT CRC32;
77  BYTE CMAC[DESFIRE_CMAC_LENGTH];
78  };
79  } MACData;
80  } Checksums;
81  struct DESFIRE_FIRMWARE_ALIGNAT {
82  SIZET BytesLeft;
83  struct DESFIRE_FIRMWARE_ALIGNAT {
84  TransferSourceFuncType Func;
85  SIZET Pointer; /* in FRAM */
86  } Source;
87  struct DESFIRE_FIRMWARE_ALIGNAT {
88  BOOL FirstPaddingBitSet;
89  TransferEncryptFuncType Func;
90  BYTE AvailablePlaintext;
91  } Encryption;
92  } ReadData;
93  struct DESFIRE_FIRMWARE_ALIGNAT {
94  SIZET BytesLeft;
95  struct DESFIRE_FIRMWARE_ALIGNAT {
96  TransferSinkFuncType Func;
97  SIZET Pointer; /* in FRAM */
98  } Sink;
99  struct DESFIRE_FIRMWARE_ALIGNAT {
100  TransferEncryptFuncType Func;
101  BYTE AvailablePlaintext;
102  } Encryption;
103  } WriteData;
104 } TransferStateType;
105 extern TransferStateType TransferState;
106 
107 /* Transfer routines */
108 void SyncronizePICCInfo(void);
109 TransferStatus PiccToPcdTransfer(uint8_t *Buffer);
110 uint8_t PcdToPiccTransfer(uint8_t *Buffer, uint8_t Count);
111 
112 /* Setup routines */
113 uint8_t ReadDataFilterSetup(uint8_t CommSettings);
114 uint8_t WriteDataFilterSetup(uint8_t CommSettings);
115 
116 /* PICC management */
117 void InitialisePiccBackendEV0(uint8_t StorageSize);
118 void InitialisePiccBackendEV1(uint8_t StorageSize);
119 void ResetPiccBackend(void);
120 bool IsEmulatingEV1(void);
121 void GetPiccHardwareVersionInfo(uint8_t *Buffer);
122 void GetPiccSoftwareVersionInfo(uint8_t *Buffer);
123 void GetPiccManufactureInfo(uint8_t *Buffer);
124 uint8_t GetPiccKeySettings(void);
125 void FormatPicc(void);
126 void CreatePiccApp(void);
127 void FactoryFormatPiccEV0(void);
128 void FactoryFormatPiccEV1(uint8_t StorageSize);
129 void GetPiccUid(ConfigurationUidType Uid);
130 void SetPiccUid(ConfigurationUidType Uid);
131 
132 #endif