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 MfClassicKeyType requested_key_type; // Key type requested from app (for CUID mode)
132 bool auth_passed;
133 uint16_t current_block;
134 uint8_t reuse_key_sector;
135 MfClassicBackdoor backdoor;
136 MfClassicPollerMode mode; // Current attack mode
137 // Enhanced dictionary attack and nested nonce collection
138 bool enhanced_dict;
139 MfClassicNestedPhase nested_phase;
140 MfClassicKey nested_known_key;
141 MfClassicKeyType nested_known_key_type;
142 bool current_key_checked;
143 uint8_t nested_known_key_sector;
144 uint16_t nested_target_key;
145 MfClassicNestedNonceArray nested_nonce;
146 MfClassicPrngType prng_type;
147 bool static_encrypted;
148 uint32_t static_encrypted_nonce;
149 bool calibrated;
150 uint16_t d_min;
151 uint16_t d_max;
152 uint8_t attempt_count;
153 KeysDict* mf_classic_system_dict;
154 KeysDict* mf_classic_user_dict;
155 // Hardnested
156 uint8_t nt_enc_msb
157 [32]; // Bit-packed array to track which unique most significant bytes have been seen (256 bits = 32 bytes)
158 uint16_t msb_par_sum; // Sum of parity bits for each unique most significant byte
159 uint16_t msb_count; // Number of unique most significant bytes seen
161
162typedef struct {
163 uint8_t current_sector;
164 uint16_t current_block;
165 MfClassicKeyType key_type;
166 MfClassicKey key;
167 bool auth_passed;
169
170typedef union {
172 MfClassicPollerDictAttackContext dict_attack_ctx;
174
176
178 Iso14443_3aPoller* iso14443_3a_poller;
179
180 MfClassicPollerState state;
181 MfClassicAuthState auth_state;
182 MfClassicCardState card_state;
183
184 MfClassicType current_type_check;
185 uint8_t sectors_total;
186 MfClassicPollerMode mode;
188
189 Crypto1* crypto;
190 BitBuffer* tx_plain_buffer;
191 BitBuffer* tx_encrypted_buffer;
192 BitBuffer* rx_plain_buffer;
193 BitBuffer* rx_encrypted_buffer;
194 MfClassicData* data;
195
196 NfcGenericEvent general_event;
197 MfClassicPollerEvent mfc_event;
198 MfClassicPollerEventData mfc_event_data;
199 NfcGenericCallback callback;
200 void* context;
201};
202
203typedef struct {
204 uint8_t block;
205 MfClassicKeyType key_type;
206 MfClassicNt nt;
208
209typedef struct {
210 uint8_t block_num;
211 MfClassicKey key;
212 MfClassicKeyType key_type;
213 MfClassicBlock block;
215
216typedef struct {
217 uint8_t block_num;
218 MfClassicKey key;
219 MfClassicKeyType key_type;
220 MfClassicBlock block;
222
223typedef struct {
224 uint8_t block_num;
225 MfClassicKey key;
226 MfClassicKeyType key_type;
227 int32_t value;
229
230typedef struct {
231 uint8_t block_num;
232 MfClassicKey key;
233 MfClassicKeyType key_type;
234 MfClassicValueCommand value_cmd;
235 int32_t data;
236 int32_t new_value;
238
239typedef struct {
241 uint8_t current_sector;
243
244typedef union {
245 MfClassicCollectNtContext collect_nt_context;
246 MfClassicAuthContext auth_context;
247 MfClassicReadBlockContext read_block_context;
248 MfClassicWriteBlockContext write_block_context;
249 MfClassicReadValueContext read_value_context;
250 MfClassicChangeValueContext change_value_context;
251 MfClassicReadContext read_context;
253
254MfClassicError mf_classic_process_error(Iso14443_3aError error);
255
256MfClassicPoller* mf_classic_poller_alloc(Iso14443_3aPoller* iso14443_3a_poller);
257
258void mf_classic_poller_free(MfClassicPoller* instance);
259
260#ifdef __cplusplus
261}
262#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:230
Definition mf_classic_poller_i.h:203
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:202
Definition mf_classic_poller_i.h:177
Definition mf_classic_poller_i.h:162
Definition mf_classic_poller_i.h:116
Definition mf_classic_poller_i.h:209
Definition mf_classic_poller_i.h:239
Definition mf_classic_poller_i.h:223
Definition mf_classic_poller_i.h:216
Generic Nfc event type.
Definition nfc_generic_event.h:58
Definition mf_classic_poller_i.h:244
MfClassic poller event data.
Definition mf_classic_poller.h:184
Definition mf_classic_poller_i.h:170
Definition mf_classic.h:121