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