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_READ_DATA (0xBD)
21#define MF_DESFIRE_CMD_GET_VALUE (0x6C)
22#define MF_DESFIRE_CMD_READ_RECORDS (0xBB)
23
24#define MF_DESFIRE_MAX_KEYS (14)
25#define MF_DESFIRE_MAX_FILES (32)
26
27#define MF_DESFIRE_UID_SIZE (7)
28#define MF_DESFIRE_BATCH_SIZE (5)
29#define MF_DESFIRE_APP_ID_SIZE (3)
30#define MF_DESFIRE_VALUE_SIZE (4)
31
32typedef enum {
33 MfDesfireTypeMF3ICD40,
34 MfDesfireTypeEV1,
35 MfDesfireTypeEV2,
36 MfDesfireTypeEV2XL,
37 MfDesfireTypeEV3,
38
39 MfDesfireTypeUnknown,
40 MfDesfireTypeNum,
41} MfDesfireType;
42
43typedef enum {
44 MfDesfireSize2k,
45 MfDesfireSize4k,
46 MfDesfireSize8k,
47 MfDesfireSize16k,
48 MfDesfireSize32k,
49
50 MfDesfireSizeUnknown,
51 MfDesfireSizeNum,
52} MfDesfireSize;
53
54typedef struct {
55 uint8_t hw_vendor;
56 uint8_t hw_type;
57 uint8_t hw_subtype;
58 uint8_t hw_major;
59 uint8_t hw_minor;
60 uint8_t hw_storage;
61 uint8_t hw_proto;
62
63 uint8_t sw_vendor;
64 uint8_t sw_type;
65 uint8_t sw_subtype;
66 uint8_t sw_major;
67 uint8_t sw_minor;
68 uint8_t sw_storage;
69 uint8_t sw_proto;
70
71 uint8_t uid[MF_DESFIRE_UID_SIZE];
72 uint8_t batch[MF_DESFIRE_BATCH_SIZE];
73 uint8_t prod_week;
74 uint8_t prod_year;
76
77typedef struct {
78 uint32_t bytes_free;
79 bool is_present;
80} MfDesfireFreeMemory; // EV1+ only
81
82typedef struct {
83 bool is_master_key_changeable;
84 bool is_free_directory_list;
85 bool is_free_create_delete;
86 bool is_config_changeable;
87 uint8_t change_key_id;
88 uint8_t max_keys;
89 uint8_t flags;
91
92typedef uint8_t MfDesfireKeyVersion;
93
94typedef enum {
95 MfDesfireFileTypeStandard = 0,
96 MfDesfireFileTypeBackup = 1,
97 MfDesfireFileTypeValue = 2,
98 MfDesfireFileTypeLinearRecord = 3,
99 MfDesfireFileTypeCyclicRecord = 4,
100 MfDesfireFileTypeTransactionMac = 5,
101} MfDesfireFileType;
102
103typedef enum {
104 MfDesfireFileCommunicationSettingsPlaintext = 0,
105 MfDesfireFileCommunicationSettingsAuthenticated = 1,
106 MfDesfireFileCommunicationSettingsEnciphered = 3,
107} MfDesfireFileCommunicationSettings;
108
109typedef uint8_t MfDesfireFileId;
110typedef uint16_t MfDesfireFileAccessRights;
111
112typedef struct {
113 MfDesfireFileType type;
114 MfDesfireFileCommunicationSettings comm;
115 MfDesfireFileAccessRights access_rights[MF_DESFIRE_MAX_KEYS];
116 uint8_t access_rights_len;
117 union {
118 struct {
119 uint32_t size;
120 } data;
121 struct {
122 uint32_t lo_limit;
123 uint32_t hi_limit;
124 uint32_t limited_credit_value;
125 bool limited_credit_enabled;
126 } value;
127 struct {
128 uint32_t size;
129 uint32_t max;
130 uint32_t cur;
131 } record;
132 struct {
133 uint8_t key_option;
134 uint8_t key_version;
135 uint32_t counter_limit;
136 } transaction_mac;
137 };
139
140typedef struct {
141 SimpleArray* data;
143
144typedef struct {
145 uint8_t data[MF_DESFIRE_APP_ID_SIZE];
147
148typedef struct MfDesfireApplication {
149 MfDesfireKeySettings key_settings;
150 SimpleArray* key_versions;
151 SimpleArray* file_ids;
152 SimpleArray* file_settings;
153 SimpleArray* file_data;
155
156typedef enum {
157 MfDesfireErrorNone,
158 MfDesfireErrorNotPresent,
159 MfDesfireErrorProtocol,
160 MfDesfireErrorTimeout,
161 MfDesfireErrorAuthentication,
162 MfDesfireErrorCommandNotSupported,
163} MfDesfireError;
164
165typedef struct {
166 Iso14443_4aData* iso14443_4a_data;
167 MfDesfireVersion version;
168 MfDesfireFreeMemory free_memory;
169 MfDesfireKeySettings master_key_settings;
170 SimpleArray* master_key_versions;
171 SimpleArray* application_ids;
172 SimpleArray* applications;
173 FuriString* device_name;
175
176extern const NfcDeviceBase nfc_device_mf_desfire;
177
178// Virtual methods
179
180MfDesfireData* mf_desfire_alloc(void);
181
182void mf_desfire_free(MfDesfireData* data);
183
184void mf_desfire_reset(MfDesfireData* data);
185
186void mf_desfire_copy(MfDesfireData* data, const MfDesfireData* other);
187
188bool mf_desfire_verify(MfDesfireData* data, const FuriString* device_type);
189
190bool mf_desfire_load(MfDesfireData* data, FlipperFormat* ff, uint32_t version);
191
192bool mf_desfire_save(const MfDesfireData* data, FlipperFormat* ff);
193
194bool mf_desfire_is_equal(const MfDesfireData* data, const MfDesfireData* other);
195
196const char* mf_desfire_get_device_name(const MfDesfireData* data, NfcDeviceNameType name_type);
197
198const uint8_t* mf_desfire_get_uid(const MfDesfireData* data, size_t* uid_len);
199
200bool mf_desfire_set_uid(MfDesfireData* data, const uint8_t* uid, size_t uid_len);
201
202Iso14443_4aData* mf_desfire_get_base_data(const MfDesfireData* data);
203
204// Getters and tests
205
207 mf_desfire_get_application(const MfDesfireData* data, const MfDesfireApplicationId* app_id);
208
210 mf_desfire_get_file_settings(const MfDesfireApplication* data, const MfDesfireFileId* file_id);
211
213 mf_desfire_get_file_data(const MfDesfireApplication* data, const MfDesfireFileId* file_id);
214
215#ifdef __cplusplus
216}
217#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:148
Definition mf_desfire.h:144
Definition mf_desfire.h:165
Definition mf_desfire.h:140
Definition mf_desfire.h:112
Definition mf_desfire.h:77
Definition mf_desfire.h:82
Definition mf_desfire.h:54
Generic NFC device interface.
Definition nfc_device_base_i.h:142
Definition simple_array.c:5