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.
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
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
{
38 typedef enum DesfireISOSelectControlEnum DesfireISOSelectControl
;
40 enum DesfireISOSelectWayEnum
{
46 typedef enum DesfireISOSelectWayEnum DesfireISOSelectWay
;
53 const uint8_t createlen
;
54 const bool mayHaveISOfid
;
55 } DesfireCreateFileCommands_t
;
62 DesfireCommunicationMode commMode
;
63 bool additionalAccessRightsEn
;
64 uint16_t rawAccessRights
;
77 uint8_t limitedCredit
;
81 uint32_t maxRecordCount
;
82 uint32_t curRecordCount
;
90 uint8_t additionalAccessRightsLength
;
91 uint16_t additionalAccessRights
[16];
98 FileSettings_t fileSettings
;
101 typedef FileListElm_t FileList_t
[32];
117 AuthCommandsChk_t authCmdCheck
;
121 bool isoFileIDEnabled
; // from numKeysRaw
122 uint8_t numberOfKeys
; // from numKeysRaw
123 DesfireCryptoAlgorithm keyType
; // from numKeysRaw
125 uint8_t keyVersions
[16];
132 typedef AppListElm_t AppListS
[64];
137 AuthCommandsChk_t authCmdCheck
;
141 uint8_t numberOfKeys
; // from numKeysRaw
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