style
[RRG-proxmark3.git] / client / src / mifare / desfirecore.h
blob929bd019766c3cd6b622bc9b345e1e43abf41051
1 //-----------------------------------------------------------------------------
2 // Borrowed initially from https://github.com/nfc-tools/libfreefare
3 // Copyright (C) 2010, Romain Tartiere.
4 // Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
5 //
6 // This program is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // See LICENSE.txt for the text of the license.
17 //-----------------------------------------------------------------------------
18 // High frequency Desfire core functions
19 //-----------------------------------------------------------------------------
21 #ifndef __DESFIRECORE_H
22 #define __DESFIRECORE_H
24 #include "common.h"
25 #include "cliparser.h"
26 #include "mifare/desfirecrypto.h"
28 #define DESFIRE_TX_FRAME_MAX_LEN 54
29 #define DESFIRE_BUFFER_SIZE 65538
31 enum DesfireISOSelectControlEnum {
32 ISSMFDFEF = 0x00,
33 ISSChildDF = 0x01,
34 ISSEFByFileID = 0x02,
35 ISSParentDF = 0x03,
36 ISSDFName = 0x04
38 typedef enum DesfireISOSelectControlEnum DesfireISOSelectControl;
40 enum DesfireISOSelectWayEnum {
41 ISW6bAID,
42 ISWMF,
43 ISWIsoID,
44 ISWDFName
46 typedef enum DesfireISOSelectWayEnum DesfireISOSelectWay;
48 typedef struct {
49 const uint8_t id;
50 const char *text;
51 const uint8_t cmd;
52 const uint8_t len;
53 const uint8_t createlen;
54 const bool mayHaveISOfid;
55 } DesfireCreateFileCommands_t;
57 typedef struct {
58 // all
59 uint8_t fileType;
60 uint8_t fileOption;
61 uint8_t fileCommMode;
62 DesfireCommunicationMode commMode;
63 bool additionalAccessRightsEn;
64 uint16_t rawAccessRights;
65 uint8_t rAccess;
66 uint8_t wAccess;
67 uint8_t rwAccess;
68 uint8_t chAccess;
70 // data
71 uint32_t fileSize;
73 //value
74 uint32_t lowerLimit;
75 uint32_t upperLimit;
76 uint32_t value;
77 uint8_t limitedCredit;
79 // record
80 uint32_t recordSize;
81 uint32_t maxRecordCount;
82 uint32_t curRecordCount;
84 //mac
85 uint8_t keyType;
86 uint8_t key[16];
87 uint8_t keyVersion;
89 // additional rights
90 uint8_t additionalAccessRightsLength;
91 uint16_t additionalAccessRights[16];
93 } FileSettings_t;
95 typedef struct {
96 uint8_t fileNum;
97 uint16_t fileISONum;
98 FileSettings_t fileSettings;
99 } FileListElm_t;
101 typedef FileListElm_t FileList_t[32];
103 typedef struct {
104 bool checked;
105 bool auth;
106 bool authISO;
107 bool authAES;
108 bool authEV2;
109 bool authISONative;
110 bool authLRP;
111 } AuthCommandsChk_t;
113 typedef struct {
114 uint32_t appNum;
115 uint16_t appISONum;
116 char appDFName[16];
117 AuthCommandsChk_t authCmdCheck;
119 uint8_t keySettings;
120 uint8_t numKeysRaw;
121 bool isoFileIDEnabled; // from numKeysRaw
122 uint8_t numberOfKeys; // from numKeysRaw
123 DesfireCryptoAlgorithm keyType; // from numKeysRaw
125 uint8_t keyVersions[16];
127 bool filesReaded;
128 size_t filesCount;
129 bool isoPresent;
130 FileList_t fileList;
131 } AppListElm_t;
132 typedef AppListElm_t AppListS[64];
134 typedef struct {
135 size_t appCount;
136 uint32_t freemem;
137 AuthCommandsChk_t authCmdCheck;
139 uint8_t keySettings;
140 uint8_t numKeysRaw;
141 uint8_t numberOfKeys; // from numKeysRaw
143 uint8_t keyVersion0;
144 } PICCInfo_t;
146 typedef enum {
147 RFTAuto,
148 RFTData,
149 RFTValue,
150 RFTRecord,
151 RFTMAC,
152 } DesfireReadOpFileType;
154 extern const CLIParserOption DesfireAlgoOpts[];
155 extern const CLIParserOption DesfireKDFAlgoOpts[];
156 extern const CLIParserOption DesfireCommunicationModeOpts[];
157 extern const CLIParserOption DesfireCommandSetOpts[];
158 extern const CLIParserOption DesfireSecureChannelOpts[];
159 extern const CLIParserOption DesfireFileAccessModeOpts[];
160 extern const CLIParserOption DesfireValueFileOperOpts[];
161 extern const CLIParserOption DesfireReadFileTypeOpts[];
163 const char *DesfireGetErrorString(int res, uint16_t *sw);
164 uint32_t DesfireAIDByteToUint(const uint8_t *data);
165 void DesfireAIDUintToByte(uint32_t aid, uint8_t *data);
167 void DesfirePrintContext(DesfireContext_t *ctx);
169 int DesfireExchange(DesfireContext_t *ctx, uint8_t cmd, uint8_t *data, size_t datalen, uint8_t *respcode, uint8_t *resp, size_t *resplen);
170 int DesfireExchangeEx(bool activate_field, DesfireContext_t *ctx, uint8_t cmd, uint8_t *data, size_t datalen, uint8_t *respcode, uint8_t *resp, size_t *resplen, bool enable_chaining, size_t splitbysize);
172 int DesfireReadSignature(DesfireContext_t *dctx, uint8_t sid, uint8_t *resp, size_t *resplen);
174 int DesfireAnticollision(bool verbose);
175 int DesfireSelectAID(DesfireContext_t *ctx, uint8_t *aid1, uint8_t *aid2);
176 int DesfireSelectAIDHex(DesfireContext_t *ctx, uint32_t aid1, bool select_two, uint32_t aid2);
177 int DesfireSelectAIDHexNoFieldOn(DesfireContext_t *ctx, uint32_t aid);
178 void DesfirePrintAIDFunctions(uint32_t appid);
179 void DesfirePrintMADAID(uint32_t appid, bool verbose);
181 int DesfireGetCardUID(DesfireContext_t *ctx);
183 const char *DesfireSelectWayToStr(DesfireISOSelectWay way);
184 char *DesfireWayIDStr(DesfireISOSelectWay way, uint32_t id);
185 bool DesfireMFSelected(DesfireISOSelectWay way, uint32_t id);
186 int DesfireSelectEx(DesfireContext_t *ctx, bool fieldon, DesfireISOSelectWay way, uint32_t id, const char *dfname);
187 int DesfireSelect(DesfireContext_t *ctx, DesfireISOSelectWay way, uint32_t id, char *dfname);
189 const char *DesfireAuthErrorToStr(int error);
190 int DesfireSelectAndAuthenticate(DesfireContext_t *dctx, DesfireSecureChannel secureChannel, uint32_t aid, bool verbose);
191 int DesfireSelectAndAuthenticateEx(DesfireContext_t *dctx, DesfireSecureChannel secureChannel, uint32_t aid, bool noauth, bool verbose);
192 int DesfireSelectAndAuthenticateW(DesfireContext_t *dctx, DesfireSecureChannel secureChannel, DesfireISOSelectWay way, uint32_t id, bool selectfile, uint16_t isofileid, bool noauth, bool verbose);
193 int DesfireSelectAndAuthenticateAppW(DesfireContext_t *dctx, DesfireSecureChannel secureChannel, DesfireISOSelectWay way, uint32_t id, bool noauth, bool verbose);
194 int DesfireSelectAndAuthenticateISO(DesfireContext_t *dctx, DesfireSecureChannel secureChannel, bool useaid, uint32_t aid, uint16_t isoappid, bool selectfile, uint16_t isofileid, bool noauth, bool verbose);
195 int DesfireAuthenticate(DesfireContext_t *dctx, DesfireSecureChannel secureChannel, bool verbose);
197 bool DesfireCheckAuthCmd(DesfireISOSelectWay way, uint32_t appID, uint8_t keyNum, uint8_t authcmd, bool checklrp);
198 void DesfireCheckAuthCommands(DesfireISOSelectWay way, uint32_t appID, char *dfname, uint8_t keyNum, AuthCommandsChk_t *authCmdCheck);
199 void DesfireCheckAuthCommandsPrint(AuthCommandsChk_t *authCmdCheck);
201 int DesfireFormatPICC(DesfireContext_t *dctx);
202 int DesfireGetFreeMem(DesfireContext_t *dctx, uint32_t *freemem);
203 int DesfireGetUID(DesfireContext_t *dctx, uint8_t *resp, size_t *resplen);
204 int DesfireGetAIDList(DesfireContext_t *dctx, uint8_t *resp, size_t *resplen);
205 int DesfireGetDFList(DesfireContext_t *dctx, uint8_t *resp, size_t *resplen);
206 int DesfireFillPICCInfo(DesfireContext_t *dctx, PICCInfo_t *PICCInfo, bool deepmode);
207 int DesfireFillAppList(DesfireContext_t *dctx, PICCInfo_t *PICCInfo, AppListS appList, bool deepmode, bool readFiles, bool fillAppSettings);
208 void DesfirePrintPICCInfo(DesfireContext_t *dctx, PICCInfo_t *PICCInfo);
209 void DesfirePrintAppList(DesfireContext_t *dctx, PICCInfo_t *PICCInfo, AppListS appList);
211 int DesfireCreateApplication(DesfireContext_t *dctx, uint8_t *appdata, size_t appdatalen);
212 int DesfireDeleteApplication(DesfireContext_t *dctx, uint32_t aid);
214 int DesfireGetKeyVersion(DesfireContext_t *dctx, uint8_t *data, size_t len, uint8_t *resp, size_t *resplen);
215 int DesfireGetKeySettings(DesfireContext_t *dctx, uint8_t *resp, size_t *resplen);
216 int DesfireChangeKeySettings(DesfireContext_t *dctx, uint8_t *data, size_t len);
217 void PrintKeySettings(uint8_t keysettings, uint8_t numkeys, bool applevel, bool print2ndbyte);
219 int DesfireChangeKeyCmd(DesfireContext_t *dctx, uint8_t *data, size_t datalen, uint8_t *resp, size_t *resplen);
220 int DesfireChangeKey(DesfireContext_t *dctx, bool change_master_key, uint8_t newkeynum, DesfireCryptoAlgorithm newkeytype, uint32_t newkeyver, uint8_t *newkey, DesfireCryptoAlgorithm oldkeytype, uint8_t *oldkey, bool verbose);
222 int DesfireSetConfigurationCmd(DesfireContext_t *dctx, uint8_t *data, size_t len, uint8_t *resp, size_t *resplen);
223 int DesfireSetConfiguration(DesfireContext_t *dctx, uint8_t paramid, uint8_t *param, size_t paramlen);
225 int DesfireFillFileList(DesfireContext_t *dctx, FileList_t FileList, size_t *filescount, bool *isopresent);
226 int DesfireGetFileIDList(DesfireContext_t *dctx, uint8_t *resp, size_t *resplen);
227 int DesfireGetFileISOIDList(DesfireContext_t *dctx, uint8_t *resp, size_t *resplen);
229 void DesfireFillFileSettings(uint8_t *data, size_t datalen, FileSettings_t *fsettings);
230 void DesfirePrintFileSettingsOneLine(FileSettings_t *fsettings);
231 void DesfirePrintFileSettingsTable(bool printheader, uint8_t id, bool isoidavail, uint16_t isoid, FileSettings_t *fsettings);
232 void DesfirePrintFileSettingsExtended(FileSettings_t *fsettings);
233 int DesfireGetFileSettings(DesfireContext_t *dctx, uint8_t fileid, uint8_t *resp, size_t *resplen);
234 int DesfireFileSettingsStruct(DesfireContext_t *dctx, uint8_t fileid, FileSettings_t *fsettings);
235 int DesfireChangeFileSettings(DesfireContext_t *dctx, uint8_t *data, size_t datalen);
237 const DesfireCreateFileCommands_t *GetDesfireFileCmdRec(uint8_t type);
238 const char *GetDesfireAccessRightStr(uint8_t right);
239 const char *GetDesfireAccessRightShortStr(uint8_t right);
240 void DesfireEncodeFileAcessMode(uint8_t *mode, uint8_t r, uint8_t w, uint8_t rw, uint8_t ch);
241 void DesfireDecodeFileAcessMode(const uint8_t *mode, uint8_t *r, uint8_t *w, uint8_t *rw, uint8_t *ch);
242 void DesfirePrintAccessRight(uint8_t *data);
243 void DesfirePrintFileSettings(uint8_t *data, size_t len);
244 void DesfirePrintSetFileSettings(uint8_t *data, size_t len);
245 void DesfirePrintCreateFileSettings(uint8_t filetype, uint8_t *data, size_t len);
247 const char *GetDesfireFileType(uint8_t type);
248 int DesfireCreateFile(DesfireContext_t *dctx, uint8_t ftype, uint8_t *fdata, size_t fdatalen, bool checklen);
249 int DesfireDeleteFile(DesfireContext_t *dctx, uint8_t fnum);
250 int DesfireCommitReaderID(DesfireContext_t *dctx, uint8_t *readerid, size_t readeridlen, uint8_t *resp, size_t *resplen);
251 int DesfireCommitTransactionEx(DesfireContext_t *dctx, bool enable_options, uint8_t options, uint8_t *resp, size_t *resplen);
252 int DesfireCommitTransaction(DesfireContext_t *dctx, bool enable_options, uint8_t options);
253 int DesfireAbortTransaction(DesfireContext_t *dctx);
255 int DesfireValueFileOperations(DesfireContext_t *dctx, uint8_t fid, uint8_t operation, uint32_t *value);
256 int DesfireClearRecordFile(DesfireContext_t *dctx, uint8_t fnum);
258 int DesfireReadFile(DesfireContext_t *dctx, uint8_t fnum, uint32_t offset, uint32_t len, uint8_t *resp, size_t *resplen);
259 int DesfireWriteFile(DesfireContext_t *dctx, uint8_t fnum, uint32_t offset, uint32_t len, uint8_t *data);
260 int DesfireReadRecords(DesfireContext_t *dctx, uint8_t fnum, uint32_t recnum, uint32_t reccount, uint8_t *resp, size_t *resplen);
261 int DesfireWriteRecord(DesfireContext_t *dctx, uint8_t fnum, uint32_t offset, uint32_t len, uint8_t *data);
262 int DesfireUpdateRecord(DesfireContext_t *dctx, uint8_t fnum, uint32_t recnum, uint32_t offset, uint32_t len, uint8_t *data);
264 int DesfireISOSelectDF(DesfireContext_t *dctx, char *dfname, uint8_t *resp, size_t *resplen);
265 int DesfireISOSelect(DesfireContext_t *dctx, DesfireISOSelectControl cntr, uint8_t *data, uint8_t datalen, uint8_t *resp, size_t *resplen);
266 int DesfireISOSelectFile(DesfireContext_t *dctx, char *appdfname, uint16_t appid, uint16_t fileid);
267 int DesfireISOSelectEx(DesfireContext_t *dctx, bool fieldon, DesfireISOSelectControl cntr, uint8_t *data, uint8_t datalen, uint8_t *resp, size_t *resplen);
268 int DesfireISOGetChallenge(DesfireContext_t *dctx, DesfireCryptoAlgorithm keytype, uint8_t *resp, size_t *resplen);
269 int DesfireISOExternalAuth(DesfireContext_t *dctx, bool app_level, uint8_t keynum, DesfireCryptoAlgorithm keytype, uint8_t *data);
270 int DesfireISOInternalAuth(DesfireContext_t *dctx, bool app_level, uint8_t keynum, DesfireCryptoAlgorithm keytype, uint8_t *data, uint8_t *resp, size_t *resplen);
272 int DesfireISOReadBinary(DesfireContext_t *dctx, bool use_file_id, uint8_t fileid, uint16_t offset, uint8_t length, uint8_t *resp, size_t *resplen);
273 int DesfireISOUpdateBinary(DesfireContext_t *dctx, bool use_file_id, uint8_t fileid, uint16_t offset, uint8_t *data, size_t datalen);
274 int DesfireISOReadRecords(DesfireContext_t *dctx, uint8_t recordnum, bool read_all_records, uint8_t fileid, uint8_t length, uint8_t *resp, size_t *resplen);
275 int DesfireISOAppendRecord(DesfireContext_t *dctx, uint8_t fileid, uint8_t *data, size_t datalen);
277 #endif // __DESFIRECORE_H