Loading...
Searching...
No Matches
mf_classic.h
1#pragma once
2
3#include <lib/nfc/protocols/iso14443_3a/iso14443_3a.h>
4
5#ifdef __cplusplus
6extern "C" {
7#endif
8
9#define MF_CLASSIC_CMD_AUTH_KEY_A (0x60U)
10#define MF_CLASSIC_CMD_AUTH_KEY_B (0x61U)
11#define MF_CLASSIC_CMD_BACKDOOR_AUTH_KEY_A (0x64U)
12#define MF_CLASSIC_CMD_BACKDOOR_AUTH_KEY_B (0x65U)
13#define MF_CLASSIC_CMD_READ_BLOCK (0x30U)
14#define MF_CLASSIC_CMD_WRITE_BLOCK (0xA0U)
15#define MF_CLASSIC_CMD_VALUE_DEC (0xC0U)
16#define MF_CLASSIC_CMD_VALUE_INC (0xC1U)
17#define MF_CLASSIC_CMD_VALUE_RESTORE (0xC2U)
18#define MF_CLASSIC_CMD_VALUE_TRANSFER (0xB0U)
19
20#define MF_CLASSIC_CMD_HALT_MSB (0x50)
21#define MF_CLASSIC_CMD_HALT_LSB (0x00)
22#define MF_CLASSIC_CMD_ACK (0x0A)
23#define MF_CLASSIC_CMD_NACK (0x00)
24#define MF_CLASSIC_CMD_NACK_TRANSFER_INVALID (0x04)
25#define MF_CLASSIC_CMD_NACK_TRANSFER_CRC_ERROR (0x01)
26
27#define MF_CLASSIC_TOTAL_SECTORS_MAX (40)
28#define MF_CLASSIC_TOTAL_BLOCKS_MAX (256)
29#define MF_CLASSIC_READ_MASK_SIZE (MF_CLASSIC_TOTAL_BLOCKS_MAX / 32)
30#define MF_CLASSIC_BLOCK_SIZE (16)
31#define MF_CLASSIC_KEY_SIZE (6)
32#define MF_CLASSIC_ACCESS_BYTES_SIZE (4)
33
34#define MF_CLASSIC_NT_SIZE (4)
35#define MF_CLASSIC_NR_SIZE (4)
36#define MF_CLASSIC_AR_SIZE (4)
37#define MF_CLASSIC_AT_SIZE (4)
38
39typedef enum {
40 MfClassicErrorNone,
41 MfClassicErrorNotPresent,
42 MfClassicErrorProtocol,
43 MfClassicErrorAuth,
44 MfClassicErrorPartialRead,
45 MfClassicErrorTimeout,
46} MfClassicError;
47
48typedef enum {
49 MfClassicTypeMini,
50 MfClassicType1k,
51 MfClassicType4k,
52
53 MfClassicTypeNum,
54} MfClassicType;
55
56typedef enum {
57 MfClassicActionDataRead,
58 MfClassicActionDataWrite,
59 MfClassicActionDataInc,
60 MfClassicActionDataDec,
61
62 MfClassicActionKeyARead,
63 MfClassicActionKeyAWrite,
64 MfClassicActionKeyBRead,
65 MfClassicActionKeyBWrite,
66 MfClassicActionACRead,
67 MfClassicActionACWrite,
68} MfClassicAction;
69
70typedef enum {
71 MfClassicValueCommandIncrement,
72 MfClassicValueCommandDecrement,
73 MfClassicValueCommandRestore,
74
75 MfClassicValueCommandInvalid,
76} MfClassicValueCommand;
77
78typedef struct {
79 uint8_t data[MF_CLASSIC_BLOCK_SIZE];
81
82typedef enum {
83 MfClassicKeyTypeA,
84 MfClassicKeyTypeB,
85} MfClassicKeyType;
86
87typedef struct {
88 uint8_t data[MF_CLASSIC_KEY_SIZE];
90
91typedef struct {
92 uint8_t data[MF_CLASSIC_ACCESS_BYTES_SIZE];
94
95typedef struct {
96 uint8_t data[MF_CLASSIC_NT_SIZE];
98
99typedef struct {
100 uint8_t data[MF_CLASSIC_AT_SIZE];
102
103typedef struct {
104 uint8_t data[MF_CLASSIC_NR_SIZE];
106
107typedef struct {
108 uint8_t data[MF_CLASSIC_AR_SIZE];
110
111typedef struct {
112 uint8_t block_num;
113 MfClassicKey key;
114 MfClassicKeyType key_type;
115 MfClassicNt nt;
116 MfClassicNr nr;
117 MfClassicAr ar;
118 MfClassicAt at;
120
121typedef union {
122 MfClassicBlock block;
123 struct {
124 MfClassicKey key_a;
125 MfClassicAccessBits access_bits;
126 MfClassicKey key_b;
127 };
129
130typedef struct {
131 uint64_t key_a_mask;
132 MfClassicKey key_a[MF_CLASSIC_TOTAL_SECTORS_MAX];
133 uint64_t key_b_mask;
134 MfClassicKey key_b[MF_CLASSIC_TOTAL_SECTORS_MAX];
136
137typedef struct {
138 Iso14443_3aData* iso14443_3a_data;
139 MfClassicType type;
140 uint32_t block_read_mask[MF_CLASSIC_READ_MASK_SIZE];
141 uint64_t key_a_mask;
142 uint64_t key_b_mask;
143 MfClassicBlock block[MF_CLASSIC_TOTAL_BLOCKS_MAX];
145
146extern const NfcDeviceBase nfc_device_mf_classic;
147
148MfClassicData* mf_classic_alloc(void);
149
150void mf_classic_free(MfClassicData* data);
151
152void mf_classic_reset(MfClassicData* data);
153
154void mf_classic_copy(MfClassicData* data, const MfClassicData* other);
155
156bool mf_classic_verify(MfClassicData* data, const FuriString* device_type);
157
158bool mf_classic_load(MfClassicData* data, FlipperFormat* ff, uint32_t version);
159
160bool mf_classic_save(const MfClassicData* data, FlipperFormat* ff);
161
162bool mf_classic_is_equal(const MfClassicData* data, const MfClassicData* other);
163
164const char* mf_classic_get_device_name(const MfClassicData* data, NfcDeviceNameType name_type);
165
166const uint8_t* mf_classic_get_uid(const MfClassicData* data, size_t* uid_len);
167
168bool mf_classic_set_uid(MfClassicData* data, const uint8_t* uid, size_t uid_len);
169
170Iso14443_3aData* mf_classic_get_base_data(const MfClassicData* data);
171
172uint8_t mf_classic_get_total_sectors_num(MfClassicType type);
173
174uint16_t mf_classic_get_total_block_num(MfClassicType type);
175
176uint8_t mf_classic_get_first_block_num_of_sector(uint8_t sector);
177
178uint8_t mf_classic_get_blocks_num_in_sector(uint8_t sector);
179
180uint8_t mf_classic_get_sector_trailer_num_by_sector(uint8_t sector);
181
182uint8_t mf_classic_get_sector_trailer_num_by_block(uint8_t block);
183
185 mf_classic_get_sector_trailer_by_sector(const MfClassicData* data, uint8_t sector_num);
186
187bool mf_classic_is_sector_trailer(uint8_t block);
188
189void mf_classic_set_sector_trailer_read(
190 MfClassicData* data,
191 uint8_t block_num,
192 MfClassicSectorTrailer* sec_tr);
193
194uint8_t mf_classic_get_sector_by_block(uint8_t block);
195
196bool mf_classic_block_to_value(const MfClassicBlock* block, int32_t* value, uint8_t* addr);
197
198void mf_classic_value_to_block(int32_t value, uint8_t addr, MfClassicBlock* block);
199
200bool mf_classic_is_key_found(
201 const MfClassicData* data,
202 uint8_t sector_num,
203 MfClassicKeyType key_type);
204
205void mf_classic_set_key_found(
206 MfClassicData* data,
207 uint8_t sector_num,
208 MfClassicKeyType key_type,
209 uint64_t key);
210
211void mf_classic_set_key_not_found(
212 MfClassicData* data,
213 uint8_t sector_num,
214 MfClassicKeyType key_type);
215
217 mf_classic_get_key(const MfClassicData* data, uint8_t sector_num, MfClassicKeyType key_type);
218
219bool mf_classic_is_block_read(const MfClassicData* data, uint8_t block_num);
220
221void mf_classic_set_block_read(MfClassicData* data, uint8_t block_num, MfClassicBlock* block_data);
222
223bool mf_classic_is_sector_read(const MfClassicData* data, uint8_t sector_num);
224
225void mf_classic_get_read_sectors_and_keys(
226 const MfClassicData* data,
227 uint8_t* sectors_read,
228 uint8_t* keys_found);
229
230bool mf_classic_is_card_read(const MfClassicData* data);
231
232bool mf_classic_is_value_block(MfClassicSectorTrailer* sec_tr, uint8_t block_num);
233
234bool mf_classic_is_allowed_access_data_block(
236 uint8_t block_num,
237 MfClassicKeyType key_type,
238 MfClassicAction action);
239
240bool mf_classic_is_allowed_access(
241 MfClassicData* data,
242 uint8_t block_num,
243 MfClassicKeyType key_type,
244 MfClassicAction action);
245
246#ifdef __cplusplus
247}
248#endif
NfcDeviceNameType
Verbosity level of the displayed NFC device name.
Definition nfc_device_base.h:14
Definition flipper_format.c:12
Definition string.c:4
Definition iso14443_3a.h:58
Definition mf_classic.h:91
Definition mf_classic.h:107
Definition mf_classic.h:99
Definition mf_classic.h:111
Definition mf_classic.h:78
Definition mf_classic.h:137
Definition mf_classic.h:130
Definition mf_classic.h:87
Definition mf_classic.h:103
Definition mf_classic.h:95
Generic NFC device interface.
Definition nfc_device_base_i.h:142
Definition mf_classic.h:121