Loading...
Searching...
No Matches
mf_classic_poller_i.h
1#pragma once
2
3#include "mf_classic_poller.h"
4#include <lib/nfc/protocols/iso14443_3a/iso14443_3a_poller_i.h>
5#include <bit_lib/bit_lib.h>
6#include <nfc/helpers/iso14443_crc.h>
7#include <nfc/helpers/crypto1.h>
8#include <stream/stream.h>
9#include <stream/buffered_file_stream.h>
10#include <toolbox/keys_dict.h>
11#include <helpers/nfc_util.h>
12
13#ifdef __cplusplus
14extern "C" {
15#endif
16
17#define MF_CLASSIC_FWT_FC (60000)
18#define NFC_FOLDER EXT_PATH("nfc")
19#define NFC_ASSETS_FOLDER EXT_PATH("nfc/assets")
20#define MF_CLASSIC_NESTED_ANALYZE_NT_COUNT (5)
21#define MF_CLASSIC_NESTED_NT_HARD_MINIMUM (3)
22#define MF_CLASSIC_NESTED_RETRY_MAXIMUM (60)
23#define MF_CLASSIC_NESTED_HARD_RETRY_MAXIMUM (3)
24#define MF_CLASSIC_NESTED_CALIBRATION_COUNT (21)
25#define MF_CLASSIC_NESTED_LOGS_FILE_NAME ".nested.log"
26#define MF_CLASSIC_NESTED_SYSTEM_DICT_FILE_NAME "mf_classic_dict_nested.nfc"
27#define MF_CLASSIC_NESTED_USER_DICT_FILE_NAME "mf_classic_dict_user_nested.nfc"
28#define MF_CLASSIC_NESTED_LOGS_FILE_PATH (NFC_FOLDER "/" MF_CLASSIC_NESTED_LOGS_FILE_NAME)
29#define MF_CLASSIC_NESTED_SYSTEM_DICT_PATH \
30 (NFC_ASSETS_FOLDER "/" MF_CLASSIC_NESTED_SYSTEM_DICT_FILE_NAME)
31#define MF_CLASSIC_NESTED_USER_DICT_PATH \
32 (NFC_ASSETS_FOLDER "/" MF_CLASSIC_NESTED_USER_DICT_FILE_NAME)
33#define SET_PACKED_BIT(arr, bit) ((arr)[(bit) / 8] |= (1 << ((bit) % 8)))
34#define GET_PACKED_BIT(arr, bit) ((arr)[(bit) / 8] & (1 << ((bit) % 8)))
35
36extern const MfClassicKey auth1_backdoor_key;
37extern const MfClassicKey auth2_backdoor_key;
38extern const MfClassicKey auth3_backdoor_key;
39extern const uint16_t valid_sums[19];
40
41typedef enum {
42 MfClassicAuthStateIdle,
43 MfClassicAuthStatePassed,
44} MfClassicAuthState;
45
46typedef enum {
47 MfClassicCardStateDetected,
48 MfClassicCardStateLost,
49} MfClassicCardState;
50
51typedef struct {
52 MfClassicKey key;
53 MfClassicBackdoor type;
55
56extern const MfClassicBackdoorKeyPair mf_classic_backdoor_keys[];
57extern const size_t mf_classic_backdoor_keys_count;
58
59typedef struct {
60 uint32_t cuid; // Card UID
61 uint8_t key_idx; // Key index
62 uint32_t nt; // Nonce
63 uint32_t nt_enc; // Encrypted nonce
64 uint8_t par; // Parity
65 uint16_t dist; // Distance
67
68typedef struct {
70 size_t count;
72
73typedef enum {
74 MfClassicPollerStateDetectType,
75 MfClassicPollerStateStart,
76
77 // Write states
78 MfClassicPollerStateRequestSectorTrailer,
79 MfClassicPollerStateCheckWriteConditions,
80 MfClassicPollerStateReadBlock,
81 MfClassicPollerStateWriteBlock,
82 MfClassicPollerStateWriteValueBlock,
83
84 // Read states
85 MfClassicPollerStateRequestReadSector,
86 MfClassicPollerStateReadSectorBlocks,
87
88 // Dict attack states
89 MfClassicPollerStateNextSector,
90 MfClassicPollerStateAnalyzeBackdoor,
91 MfClassicPollerStateBackdoorReadSector,
92 MfClassicPollerStateRequestKey,
93 MfClassicPollerStateReadSector,
94 MfClassicPollerStateAuthKeyA,
95 MfClassicPollerStateAuthKeyB,
96 MfClassicPollerStateKeyReuseStart,
97 MfClassicPollerStateKeyReuseStartNoOffset,
98 MfClassicPollerStateKeyReuseAuthKeyA,
99 MfClassicPollerStateKeyReuseAuthKeyB,
100 MfClassicPollerStateKeyReuseReadSector,
101 MfClassicPollerStateSuccess,
102 MfClassicPollerStateFail,
103
104 // Enhanced dictionary attack states
105 MfClassicPollerStateNestedAnalyzePRNG,
106 MfClassicPollerStateNestedCalibrate,
107 MfClassicPollerStateNestedCollectNt,
108 MfClassicPollerStateNestedController,
109 MfClassicPollerStateNestedCollectNtEnc,
110 MfClassicPollerStateNestedDictAttack,
111 MfClassicPollerStateNestedLog,
112
113 MfClassicPollerStateNum,
114} MfClassicPollerState;
115
116typedef struct {
117 uint8_t current_sector;
119 uint16_t current_block;
120 bool is_value_block;
121 MfClassicKeyType key_type_read;
122 MfClassicKeyType key_type_write;
123 bool need_halt_before_write;
124 MfClassicBlock tag_block;
126
127typedef struct {
128 uint8_t current_sector;
129 MfClassicKey current_key;
130 MfClassicKeyType current_key_type;
131 bool auth_passed;
132 uint16_t current_block;
133 uint8_t reuse_key_sector;
134 MfClassicBackdoor backdoor;
135 // Enhanced dictionary attack and nested nonce collection
136 bool enhanced_dict;
137 MfClassicNestedPhase nested_phase;
138 MfClassicKey nested_known_key;
139 MfClassicKeyType nested_known_key_type;
140 bool current_key_checked;
141 uint8_t nested_known_key_sector;
142 uint16_t nested_target_key;
143 MfClassicNestedNonceArray nested_nonce;
144 MfClassicPrngType prng_type;
145 bool static_encrypted;
146 uint32_t static_encrypted_nonce;
147 bool calibrated;
148 uint16_t d_min;
149 uint16_t d_max;
150 uint8_t attempt_count;
151 KeysDict* mf_classic_system_dict;
152 KeysDict* mf_classic_user_dict;
153 // Hardnested
154 uint8_t nt_enc_msb
155 [32]; // Bit-packed array to track which unique most significant bytes have been seen (256 bits = 32 bytes)
156 uint16_t msb_par_sum; // Sum of parity bits for each unique most significant byte
157 uint16_t msb_count; // Number of unique most significant bytes seen
159
160typedef struct {
161 uint8_t current_sector;
162 uint16_t current_block;
163 MfClassicKeyType key_type;
164 MfClassicKey key;
165 bool auth_passed;
167
168typedef union {
170 MfClassicPollerDictAttackContext dict_attack_ctx;
172
174
176 Iso14443_3aPoller* iso14443_3a_poller;
177
178 MfClassicPollerState state;
179 MfClassicAuthState auth_state;
180 MfClassicCardState card_state;
181
182 MfClassicType current_type_check;
183 uint8_t sectors_total;
185
186 Crypto1* crypto;
187 BitBuffer* tx_plain_buffer;
188 BitBuffer* tx_encrypted_buffer;
189 BitBuffer* rx_plain_buffer;
190 BitBuffer* rx_encrypted_buffer;
191 MfClassicData* data;
192
193 NfcGenericEvent general_event;
194 MfClassicPollerEvent mfc_event;
195 MfClassicPollerEventData mfc_event_data;
196 NfcGenericCallback callback;
197 void* context;
198};
199
200typedef struct {
201 uint8_t block;
202 MfClassicKeyType key_type;
203 MfClassicNt nt;
205
206typedef struct {
207 uint8_t block_num;
208 MfClassicKey key;
209 MfClassicKeyType key_type;
210 MfClassicBlock block;
212
213typedef struct {
214 uint8_t block_num;
215 MfClassicKey key;
216 MfClassicKeyType key_type;
217 MfClassicBlock block;
219
220typedef struct {
221 uint8_t block_num;
222 MfClassicKey key;
223 MfClassicKeyType key_type;
224 int32_t value;
226
227typedef struct {
228 uint8_t block_num;
229 MfClassicKey key;
230 MfClassicKeyType key_type;
231 MfClassicValueCommand value_cmd;
232 int32_t data;
233 int32_t new_value;
235
236typedef struct {
238 uint8_t current_sector;
240
241typedef union {
242 MfClassicCollectNtContext collect_nt_context;
243 MfClassicAuthContext auth_context;
244 MfClassicReadBlockContext read_block_context;
245 MfClassicWriteBlockContext write_block_context;
246 MfClassicReadValueContext read_value_context;
247 MfClassicChangeValueContext change_value_context;
248 MfClassicReadContext read_context;
250
251MfClassicError mf_classic_process_error(Iso14443_3aError error);
252
253MfClassicPoller* mf_classic_poller_alloc(Iso14443_3aPoller* iso14443_3a_poller);
254
255void mf_classic_poller_free(MfClassicPoller* instance);
256
257#ifdef __cplusplus
258}
259#endif
NfcCommand(* NfcGenericCallback)(NfcGenericEvent event, void *context)
Generic Nfc event callback type.
Definition nfc_generic_event.h:75
Definition bit_buffer.c:7
Definition crypto1.h:10
Definition iso14443_3a_poller_i.h:42
Definition keys_dict.c:11
Definition mf_classic.h:111
Definition mf_classic_poller_i.h:51
Definition mf_classic.h:78
Definition mf_classic_poller_i.h:227
Definition mf_classic_poller_i.h:200
Definition mf_classic.h:137
Definition mf_classic.h:130
Definition mf_classic.h:87
Definition mf_classic_poller_i.h:68
Definition mf_classic_poller_i.h:59
Definition mf_classic.h:95
Definition mf_classic_poller_i.h:127
MfClassic poller event.
Definition mf_classic_poller.h:200
Definition mf_classic_poller_i.h:175
Definition mf_classic_poller_i.h:160
Definition mf_classic_poller_i.h:116
Definition mf_classic_poller_i.h:206
Definition mf_classic_poller_i.h:236
Definition mf_classic_poller_i.h:220
Definition mf_classic_poller_i.h:213
Generic Nfc event type.
Definition nfc_generic_event.h:58
Definition mf_classic_poller_i.h:241
MfClassic poller event data.
Definition mf_classic_poller.h:182
Definition mf_classic_poller_i.h:168
Definition mf_classic.h:121