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 struct {
33 uint8_t hw_vendor;
34 uint8_t hw_type;
35 uint8_t hw_subtype;
36 uint8_t hw_major;
37 uint8_t hw_minor;
38 uint8_t hw_storage;
39 uint8_t hw_proto;
40
41 uint8_t sw_vendor;
42 uint8_t sw_type;
43 uint8_t sw_subtype;
44 uint8_t sw_major;
45 uint8_t sw_minor;
46 uint8_t sw_storage;
47 uint8_t sw_proto;
48
49 uint8_t uid[MF_DESFIRE_UID_SIZE];
50 uint8_t batch[MF_DESFIRE_BATCH_SIZE];
51 uint8_t prod_week;
52 uint8_t prod_year;
54
55typedef struct {
56 uint32_t bytes_free;
57 bool is_present;
58} MfDesfireFreeMemory; // EV1+ only
59
60typedef struct {
61 bool is_master_key_changeable;
62 bool is_free_directory_list;
63 bool is_free_create_delete;
64 bool is_config_changeable;
65 uint8_t change_key_id;
66 uint8_t max_keys;
67 uint8_t flags;
69
70typedef uint8_t MfDesfireKeyVersion;
71
72typedef enum {
73 MfDesfireFileTypeStandard = 0,
74 MfDesfireFileTypeBackup = 1,
75 MfDesfireFileTypeValue = 2,
76 MfDesfireFileTypeLinearRecord = 3,
77 MfDesfireFileTypeCyclicRecord = 4,
78} MfDesfireFileType;
79
80typedef enum {
81 MfDesfireFileCommunicationSettingsPlaintext = 0,
82 MfDesfireFileCommunicationSettingsAuthenticated = 1,
83 MfDesfireFileCommunicationSettingsEnciphered = 3,
84} MfDesfireFileCommunicationSettings;
85
86typedef uint8_t MfDesfireFileId;
87typedef uint16_t MfDesfireFileAccessRights;
88
89typedef struct {
90 MfDesfireFileType type;
91 MfDesfireFileCommunicationSettings comm;
92 MfDesfireFileAccessRights access_rights[MF_DESFIRE_MAX_KEYS];
93 uint8_t access_rights_len;
94 union {
95 struct {
96 uint32_t size;
97 } data;
98 struct {
99 uint32_t lo_limit;
100 uint32_t hi_limit;
101 uint32_t limited_credit_value;
102 bool limited_credit_enabled;
103 } value;
104 struct {
105 uint32_t size;
106 uint32_t max;
107 uint32_t cur;
108 } record;
109 };
111
112typedef struct {
113 SimpleArray* data;
115
116typedef struct {
117 uint8_t data[MF_DESFIRE_APP_ID_SIZE];
119
120typedef struct MfDesfireApplication {
121 MfDesfireKeySettings key_settings;
122 SimpleArray* key_versions;
123 SimpleArray* file_ids;
124 SimpleArray* file_settings;
125 SimpleArray* file_data;
127
128typedef enum {
129 MfDesfireErrorNone,
130 MfDesfireErrorNotPresent,
131 MfDesfireErrorProtocol,
132 MfDesfireErrorTimeout,
133 MfDesfireErrorAuthentication,
134} MfDesfireError;
135
136typedef struct {
137 Iso14443_4aData* iso14443_4a_data;
138 MfDesfireVersion version;
139 MfDesfireFreeMemory free_memory;
140 MfDesfireKeySettings master_key_settings;
141 SimpleArray* master_key_versions;
142 SimpleArray* application_ids;
143 SimpleArray* applications;
145
146extern const NfcDeviceBase nfc_device_mf_desfire;
147
148// Virtual methods
149
150MfDesfireData* mf_desfire_alloc(void);
151
152void mf_desfire_free(MfDesfireData* data);
153
154void mf_desfire_reset(MfDesfireData* data);
155
156void mf_desfire_copy(MfDesfireData* data, const MfDesfireData* other);
157
158bool mf_desfire_verify(MfDesfireData* data, const FuriString* device_type);
159
160bool mf_desfire_load(MfDesfireData* data, FlipperFormat* ff, uint32_t version);
161
162bool mf_desfire_save(const MfDesfireData* data, FlipperFormat* ff);
163
164bool mf_desfire_is_equal(const MfDesfireData* data, const MfDesfireData* other);
165
166const char* mf_desfire_get_device_name(const MfDesfireData* data, NfcDeviceNameType name_type);
167
168const uint8_t* mf_desfire_get_uid(const MfDesfireData* data, size_t* uid_len);
169
170bool mf_desfire_set_uid(MfDesfireData* data, const uint8_t* uid, size_t uid_len);
171
172Iso14443_4aData* mf_desfire_get_base_data(const MfDesfireData* data);
173
174// Getters and tests
175
177 mf_desfire_get_application(const MfDesfireData* data, const MfDesfireApplicationId* app_id);
178
180 mf_desfire_get_file_settings(const MfDesfireApplication* data, const MfDesfireFileId* file_id);
181
183 mf_desfire_get_file_data(const MfDesfireApplication* data, const MfDesfireFileId* file_id);
184
185#ifdef __cplusplus
186}
187#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:12
Definition string.c:4
Definition iso14443_4a.h:42
Definition mf_desfire.h:120
Definition mf_desfire.h:116
Definition mf_desfire.h:136
Definition mf_desfire.h:112
Definition mf_desfire.h:89
Definition mf_desfire.h:55
Definition mf_desfire.h:60
Definition mf_desfire.h:32
Generic NFC device interface.
Definition nfc_device_base_i.h:142
Definition simple_array.c:5