Loading...
Searching...
No Matches
mf_ultralight.h
1#pragma once
2
3#include <lib/nfc/protocols/iso14443_3a/iso14443_3a.h>
4#include <mbedtls/include/mbedtls/des.h>
5
6#ifdef __cplusplus
7extern "C" {
8#endif
9
10#define MF_ULTRALIGHT_TEARING_FLAG_DEFAULT (0xBD)
11
12#define MF_ULTRALIGHT_CMD_GET_VERSION (0x60)
13#define MF_ULTRALIGHT_CMD_READ_PAGE (0x30)
14#define MF_ULTRALIGHT_CMD_FAST_READ (0x3A)
15#define MF_ULTRALIGHT_CMD_SECTOR_SELECT (0xC2)
16#define MF_ULTRALIGHT_CMD_COMP_WRITE (0xA0)
17#define MF_ULTRALIGHT_CMD_WRITE_PAGE (0xA2)
18#define MF_ULTRALIGHT_CMD_FAST_WRITE (0xA6)
19#define MF_ULTRALIGHT_CMD_READ_SIG (0x3C)
20#define MF_ULTRALIGHT_CMD_READ_CNT (0x39)
21#define MF_ULTRALIGHT_CMD_INCR_CNT (0xA5)
22#define MF_ULTRALIGHT_CMD_CHECK_TEARING (0x3E)
23#define MF_ULTRALIGHT_CMD_PWD_AUTH (0x1B)
24#define MF_ULTRALIGHT_CMD_AUTH (0x1A)
25#define MF_ULTRALIGHT_CMD_VCSL (0x4B)
26
27#define MF_ULTRALIGHT_CMD_ACK (0x0A)
28#define MF_ULTRALIGHT_CMD_NACK (0x00)
29#define MF_ULTRALIGHT_CMD_AUTH_NAK (0x04)
30
31#define MF_ULTRALIGHT_MAX_CNTR_VAL (0x00FFFFFF)
32#define MF_ULTRALIGHT_MAX_PAGE_NUM (510)
33#define MF_ULTRALIGHT_PAGE_SIZE (4U)
34#define MF_ULTRALIGHT_SIGNATURE_SIZE (32)
35#define MF_ULTRALIGHT_COUNTER_SIZE (3)
36#define MF_ULTRALIGHT_COUNTER_NUM (3)
37#define MF_ULTRALIGHT_TEARING_FLAG_SIZE (1)
38#define MF_ULTRALIGHT_TEARING_FLAG_NUM (3)
39#define MF_ULTRALIGHT_AUTH_PASSWORD_SIZE (4)
40#define MF_ULTRALIGHT_AUTH_PACK_SIZE (2)
41
42#define MF_ULTRALIGHT_C_AUTH_RESPONSE_SIZE (9)
43#define MF_ULTRALIGHT_C_AUTH_DES_KEY_SIZE (16)
44#define MF_ULTRALIGHT_C_AUTH_DATA_SIZE (MF_ULTRALIGHT_C_AUTH_DES_KEY_SIZE)
45#define MF_ULTRALIGHT_C_AUTH_IV_BLOCK_SIZE (8)
46#define MF_ULTRALIGHT_C_AUTH_RND_BLOCK_SIZE (8)
47#define MF_ULTRALIGHT_C_AUTH_RND_A_BLOCK_OFFSET (0)
48#define MF_ULTRALIGHT_C_AUTH_RND_B_BLOCK_OFFSET (8)
49#define MF_ULTRALIGHT_C_ENCRYPTED_PACK_SIZE (MF_ULTRALIGHT_C_AUTH_DATA_SIZE + 1)
50
51typedef enum {
52 MfUltralightErrorNone,
53 MfUltralightErrorNotPresent,
54 MfUltralightErrorProtocol,
55 MfUltralightErrorAuth,
56 MfUltralightErrorTimeout,
57} MfUltralightError;
58
59typedef enum {
60 MfUltralightTypeOrigin,
61 MfUltralightTypeNTAG203,
62 MfUltralightTypeMfulC,
63 MfUltralightTypeUL11,
64 MfUltralightTypeUL21,
65 MfUltralightTypeNTAG213,
66 MfUltralightTypeNTAG215,
67 MfUltralightTypeNTAG216,
68 MfUltralightTypeNTAGI2C1K,
69 MfUltralightTypeNTAGI2C2K,
70 MfUltralightTypeNTAGI2CPlus1K,
71 MfUltralightTypeNTAGI2CPlus2K,
72
73 MfUltralightTypeNum,
74} MfUltralightType;
75
76typedef enum {
77 MfUltralightFeatureSupportReadVersion = (1U << 0),
78 MfUltralightFeatureSupportReadSignature = (1U << 1),
79 MfUltralightFeatureSupportReadCounter = (1U << 2),
80 MfUltralightFeatureSupportCheckTearingFlag = (1U << 3),
81 MfUltralightFeatureSupportFastRead = (1U << 4),
82 MfUltralightFeatureSupportIncCounter = (1U << 5),
83 MfUltralightFeatureSupportFastWrite = (1U << 6),
84 MfUltralightFeatureSupportCompatibleWrite = (1U << 7),
85 MfUltralightFeatureSupportPasswordAuth = (1U << 8),
86 MfUltralightFeatureSupportVcsl = (1U << 9),
87 MfUltralightFeatureSupportSectorSelect = (1U << 10),
88 MfUltralightFeatureSupportSingleCounter = (1U << 11),
89 MfUltralightFeatureSupportAsciiMirror = (1U << 12),
90 MfUltralightFeatureSupportCounterInMemory = (1U << 13),
91 MfUltralightFeatureSupportDynamicLock = (1U << 14),
92 MfUltralightFeatureSupportAuthenticate = (1U << 15),
93} MfUltralightFeatureSupport;
94
95typedef struct {
96 uint8_t data[MF_ULTRALIGHT_PAGE_SIZE];
98
99typedef struct {
100 MfUltralightPage page[4];
102
103typedef struct {
104 uint8_t header;
105 uint8_t vendor_id;
106 uint8_t prod_type;
107 uint8_t prod_subtype;
108 uint8_t prod_ver_major;
109 uint8_t prod_ver_minor;
110 uint8_t storage_size;
111 uint8_t protocol_type;
113
114typedef struct {
115 uint8_t data[MF_ULTRALIGHT_SIGNATURE_SIZE];
117
118typedef union {
119 uint32_t counter;
120 uint8_t data[MF_ULTRALIGHT_COUNTER_SIZE];
122
123typedef struct {
124 uint8_t data;
126
127typedef struct {
128 uint8_t data[MF_ULTRALIGHT_AUTH_PASSWORD_SIZE];
130
131typedef struct {
132 uint8_t data[MF_ULTRALIGHT_C_AUTH_DES_KEY_SIZE];
134
135typedef struct {
136 uint8_t data[MF_ULTRALIGHT_AUTH_PACK_SIZE];
138
139typedef enum {
140 MfUltralightMirrorNone,
141 MfUltralightMirrorUid,
142 MfUltralightMirrorCounter,
143 MfUltralightMirrorUidCounter,
144} MfUltralightMirrorConf;
145
146typedef struct FURI_PACKED {
147 union {
148 uint8_t value;
149 struct {
150 uint8_t rfui1 : 2;
151 bool strg_mod_en : 1;
152 bool rfui2 : 1;
153 uint8_t mirror_byte : 2;
154 MfUltralightMirrorConf mirror_conf : 2;
155 };
156 } mirror;
157 uint8_t rfui1;
158 uint8_t mirror_page;
159 uint8_t auth0;
160 union {
161 uint8_t value;
162 struct {
163 uint8_t authlim : 3;
164 bool nfc_cnt_pwd_prot : 1;
165 bool nfc_cnt_en : 1;
166 bool nfc_dis_sec1 : 1; // NTAG I2C Plus only
167 bool cfglck : 1;
168 bool prot : 1;
169 };
170 } access;
171 uint8_t vctid;
172 uint8_t rfui2[2];
175 uint8_t rfui3[2];
177
178typedef struct {
179 Iso14443_3aData* iso14443_3a_data;
180 MfUltralightType type;
181 MfUltralightVersion version;
182 MfUltralightSignature signature;
183 MfUltralightCounter counter[MF_ULTRALIGHT_COUNTER_NUM];
184 MfUltralightTearingFlag tearing_flag[MF_ULTRALIGHT_TEARING_FLAG_NUM];
185 MfUltralightPage page[MF_ULTRALIGHT_MAX_PAGE_NUM];
186 uint16_t pages_read;
187 uint16_t pages_total;
188 uint32_t auth_attempts;
190
191extern const NfcDeviceBase nfc_device_mf_ultralight;
192
193MfUltralightData* mf_ultralight_alloc(void);
194
195void mf_ultralight_free(MfUltralightData* data);
196
197void mf_ultralight_reset(MfUltralightData* data);
198
199void mf_ultralight_copy(MfUltralightData* data, const MfUltralightData* other);
200
201bool mf_ultralight_verify(MfUltralightData* data, const FuriString* device_type);
202
203bool mf_ultralight_load(MfUltralightData* data, FlipperFormat* ff, uint32_t version);
204
205bool mf_ultralight_save(const MfUltralightData* data, FlipperFormat* ff);
206
207bool mf_ultralight_is_equal(const MfUltralightData* data, const MfUltralightData* other);
208
209const char*
210 mf_ultralight_get_device_name(const MfUltralightData* data, NfcDeviceNameType name_type);
211
212const uint8_t* mf_ultralight_get_uid(const MfUltralightData* data, size_t* uid_len);
213
214bool mf_ultralight_set_uid(MfUltralightData* data, const uint8_t* uid, size_t uid_len);
215
216Iso14443_3aData* mf_ultralight_get_base_data(const MfUltralightData* data);
217
218MfUltralightType mf_ultralight_get_type_by_version(MfUltralightVersion* version);
219
220uint16_t mf_ultralight_get_pages_total(MfUltralightType type);
221
222uint32_t mf_ultralight_get_feature_support_set(MfUltralightType type);
223
224uint16_t mf_ultralight_get_config_page_num(MfUltralightType type);
225
226uint8_t mf_ultralight_get_write_end_page(MfUltralightType type);
227
228uint8_t mf_ultralight_get_pwd_page_num(MfUltralightType type);
229
230bool mf_ultralight_is_page_pwd_or_pack(MfUltralightType type, uint16_t page_num);
231
232bool mf_ultralight_support_feature(const uint32_t feature_set, const uint32_t features_to_check);
233
234bool mf_ultralight_get_config_page(const MfUltralightData* data, MfUltralightConfigPages** config);
235
236bool mf_ultralight_is_all_data_read(const MfUltralightData* data);
237
238bool mf_ultralight_detect_protocol(const Iso14443_3aData* iso14443_3a_data);
239
240bool mf_ultralight_is_counter_configured(const MfUltralightData* data);
241
242void mf_ultralight_3des_shift_data(uint8_t* const arr);
243
244bool mf_ultralight_3des_key_valid(const MfUltralightData* data);
245
246const uint8_t* mf_ultralight_3des_get_key(const MfUltralightData* data);
247
248void mf_ultralight_3des_encrypt(
249 mbedtls_des3_context* ctx,
250 const uint8_t* ck,
251 const uint8_t* iv,
252 const uint8_t* input,
253 const uint8_t length,
254 uint8_t* out);
255
256void mf_ultralight_3des_decrypt(
257 mbedtls_des3_context* ctx,
258 const uint8_t* ck,
259 const uint8_t* iv,
260 const uint8_t* input,
261 const uint8_t length,
262 uint8_t* out);
263
264#ifdef __cplusplus
265}
266#endif
NfcDeviceNameType
Verbosity level of the displayed NFC device name.
Definition nfc_device_base.h:14
Definition hid_profile.c:33
Definition flipper_format.c:12
Definition string.c:4
Definition iso14443_3a.h:58
Definition mf_ultralight.h:135
Definition mf_ultralight.h:127
Definition mf_ultralight.h:131
Definition mf_ultralight.h:178
Definition mf_ultralight.h:95
Definition mf_ultralight.h:99
Definition mf_ultralight.h:114
Definition mf_ultralight.h:123
Definition mf_ultralight.h:103
Generic NFC device interface.
Definition nfc_device_base_i.h:142
Definition mf_ultralight.h:118