Loading...
Searching...
No Matches
mf_desfire.h
1#pragma once
2
3#include <lib/nfc/protocols/iso14443_4a/iso14443_4a.h>
4
6
7#ifdef __cplusplus
8extern "C" {
9#endif
10
11#define MF_DESFIRE_CMD_GET_VERSION (0x60)
12#define MF_DESFIRE_CMD_GET_FREE_MEMORY (0x6E)
13#define MF_DESFIRE_CMD_GET_KEY_SETTINGS (0x45)
14#define MF_DESFIRE_CMD_GET_KEY_VERSION (0x64)
15#define MF_DESFIRE_CMD_GET_APPLICATION_IDS (0x6A)
16#define MF_DESFIRE_CMD_SELECT_APPLICATION (0x5A)
17#define MF_DESFIRE_CMD_GET_FILE_IDS (0x6F)
18#define MF_DESFIRE_CMD_GET_FILE_SETTINGS (0xF5)
19
20#define MF_DESFIRE_CMD_CREATE_APPLICATION (0xCA)
21#define MF_DESFIRE_CMD_CREATE_STD_DATA_FILE (0xCD)
22#define MF_DESFIRE_CMD_CREATE_BACKUP_DATA_FILE (0xCB)
23#define MF_DESFIRE_CMD_CREATE_VALUE_FILE (0xCC)
24#define MF_DESFIRE_CMD_CREATE_LINEAR_RECORD_FILE (0xC1)
25#define MF_DESFIRE_CMD_CREATE_CYCLIC_RECORD_FILE (0xC0)
26
27#define MF_DESFIRE_CMD_READ_DATA (0xBD)
28#define MF_DESFIRE_CMD_GET_VALUE (0x6C)
29#define MF_DESFIRE_CMD_READ_RECORDS (0xBB)
30
31#define MF_DESFIRE_MAX_KEYS (14)
32#define MF_DESFIRE_MAX_FILES (32)
33
34#define MF_DESFIRE_UID_SIZE (7)
35#define MF_DESFIRE_BATCH_SIZE (5)
36#define MF_DESFIRE_APP_ID_SIZE (3)
37#define MF_DESFIRE_VALUE_SIZE (4)
38
39typedef enum {
40 MfDesfireTypeMF3ICD40,
41 MfDesfireTypeEV1,
42 MfDesfireTypeEV2,
43 MfDesfireTypeEV2XL,
44 MfDesfireTypeEV3,
45
46 MfDesfireTypeUnknown,
47 MfDesfireTypeNum,
48} MfDesfireType;
49
50typedef enum {
51 MfDesfireSize2k,
52 MfDesfireSize4k,
53 MfDesfireSize8k,
54 MfDesfireSize16k,
55 MfDesfireSize32k,
56
57 MfDesfireSizeUnknown,
58 MfDesfireSizeNum,
59} MfDesfireSize;
60
61typedef struct {
62 uint8_t hw_vendor;
63 uint8_t hw_type;
64 uint8_t hw_subtype;
65 uint8_t hw_major;
66 uint8_t hw_minor;
67 uint8_t hw_storage;
68 uint8_t hw_proto;
69
70 uint8_t sw_vendor;
71 uint8_t sw_type;
72 uint8_t sw_subtype;
73 uint8_t sw_major;
74 uint8_t sw_minor;
75 uint8_t sw_storage;
76 uint8_t sw_proto;
77
78 uint8_t uid[MF_DESFIRE_UID_SIZE];
79 uint8_t batch[MF_DESFIRE_BATCH_SIZE];
80 uint8_t prod_week;
81 uint8_t prod_year;
83
84typedef struct {
85 uint32_t bytes_free;
86 bool is_present;
87} MfDesfireFreeMemory; // EV1+ only
88
89typedef struct {
90 bool is_master_key_changeable;
91 bool is_free_directory_list;
92 bool is_free_create_delete;
93 bool is_config_changeable;
94 uint8_t change_key_id;
95 uint8_t max_keys;
96 uint8_t flags;
98
99typedef uint8_t MfDesfireKeyVersion;
100
101typedef enum {
102 MfDesfireFileTypeStandard = 0,
103 MfDesfireFileTypeBackup = 1,
104 MfDesfireFileTypeValue = 2,
105 MfDesfireFileTypeLinearRecord = 3,
106 MfDesfireFileTypeCyclicRecord = 4,
107 MfDesfireFileTypeTransactionMac = 5,
108} MfDesfireFileType;
109
110typedef enum {
111 MfDesfireFileCommunicationSettingsPlaintext = 0,
112 MfDesfireFileCommunicationSettingsAuthenticated = 1,
113 MfDesfireFileCommunicationSettingsEnciphered = 3,
114} MfDesfireFileCommunicationSettings;
115
116typedef uint8_t MfDesfireFileId;
117typedef uint16_t MfDesfireFileAccessRights;
118
119typedef struct {
120 MfDesfireFileType type;
121 MfDesfireFileCommunicationSettings comm;
122 MfDesfireFileAccessRights access_rights[MF_DESFIRE_MAX_KEYS];
123 uint8_t access_rights_len;
124 union {
125 struct {
126 uint32_t size;
127 } data;
128 struct {
129 uint32_t lo_limit;
130 uint32_t hi_limit;
131 uint32_t limited_credit_value;
132 bool limited_credit_enabled;
133 } value;
134 struct {
135 uint32_t size;
136 uint32_t max;
137 uint32_t cur;
138 } record;
139 struct {
140 uint8_t key_option;
141 uint8_t key_version;
142 uint32_t counter_limit;
143 } transaction_mac;
144 };
146
147typedef struct {
148 SimpleArray* data;
150
151typedef struct {
152 uint8_t data[MF_DESFIRE_APP_ID_SIZE];
154
155typedef struct MfDesfireApplication {
156 MfDesfireKeySettings key_settings;
157 SimpleArray* key_versions;
158 SimpleArray* file_ids;
159 SimpleArray* file_settings;
160 SimpleArray* file_data;
162
163typedef enum {
164 MfDesfireErrorNone,
165 MfDesfireErrorNotPresent,
166 MfDesfireErrorProtocol,
167 MfDesfireErrorTimeout,
168 MfDesfireErrorAuthentication,
169 MfDesfireErrorCommandNotSupported,
170} MfDesfireError;
171
172typedef struct {
173 Iso14443_4aData* iso14443_4a_data;
174 MfDesfireVersion version;
175 MfDesfireFreeMemory free_memory;
176 MfDesfireKeySettings master_key_settings;
177 SimpleArray* master_key_versions;
178 SimpleArray* application_ids;
179 SimpleArray* applications;
180 FuriString* device_name;
182
183extern const NfcDeviceBase nfc_device_mf_desfire;
184
185// Virtual methods
186
187MfDesfireData* mf_desfire_alloc(void);
188
189void mf_desfire_free(MfDesfireData* data);
190
191void mf_desfire_reset(MfDesfireData* data);
192
193void mf_desfire_copy(MfDesfireData* data, const MfDesfireData* other);
194
195bool mf_desfire_verify(MfDesfireData* data, const FuriString* device_type);
196
197bool mf_desfire_load(MfDesfireData* data, FlipperFormat* ff, uint32_t version);
198
199bool mf_desfire_save(const MfDesfireData* data, FlipperFormat* ff);
200
201bool mf_desfire_is_equal(const MfDesfireData* data, const MfDesfireData* other);
202
203const char* mf_desfire_get_device_name(const MfDesfireData* data, NfcDeviceNameType name_type);
204
205const uint8_t* mf_desfire_get_uid(const MfDesfireData* data, size_t* uid_len);
206
207bool mf_desfire_set_uid(MfDesfireData* data, const uint8_t* uid, size_t uid_len);
208
209Iso14443_4aData* mf_desfire_get_base_data(const MfDesfireData* data);
210
211// Getters and tests
212
214 mf_desfire_get_application(const MfDesfireData* data, const MfDesfireApplicationId* app_id);
215
217 mf_desfire_get_file_settings(const MfDesfireApplication* data, const MfDesfireFileId* file_id);
218
220 mf_desfire_get_file_data(const MfDesfireApplication* data, const MfDesfireFileId* file_id);
221
222#ifdef __cplusplus
223}
224#endif
NfcDeviceNameType
Verbosity level of the displayed NFC device name.
Definition nfc_device_base.h:14
This file provides a simple (non-type safe) array for elements with (optional) in-place construction ...
Definition flipper_format.c:17
Definition string.c:4
Definition iso14443_4a.h:42
Definition mf_desfire.h:155
Definition mf_desfire.h:151
Definition mf_desfire.h:172
Definition mf_desfire.h:147
Definition mf_desfire.h:119
Definition mf_desfire.h:84
Definition mf_desfire.h:89
Definition mf_desfire.h:61
Generic NFC device interface.
Definition nfc_device_base_i.h:142
Definition simple_array.c:5