]>
Commit | Line | Data |
---|---|---|
1 | //-----------------------------------------------------------------------------\r | |
2 | // Merlok, May 2011, 2012\r | |
3 | // Many authors, whom made it possible\r | |
4 | //\r | |
5 | // This code is licensed to you under the terms of the GNU GPL, version 2 or,\r | |
6 | // at your option, any later version. See the LICENSE.txt file for the text of\r | |
7 | // the license.\r | |
8 | //-----------------------------------------------------------------------------\r | |
9 | // Work with mifare cards.\r | |
10 | //-----------------------------------------------------------------------------\r | |
11 | \r | |
12 | #include "mifareutil.h"\r | |
13 | \r | |
14 | #include <string.h>\r | |
15 | #include <stdbool.h>\r | |
16 | \r | |
17 | #include "proxmark3.h"\r | |
18 | #include "apps.h"\r | |
19 | #include "util.h"\r | |
20 | #include "parity.h"\r | |
21 | #include "iso14443crc.h"\r | |
22 | #include "iso14443a.h"\r | |
23 | #include "crapto1/crapto1.h"\r | |
24 | #include "polarssl/des.h"\r | |
25 | \r | |
26 | int MF_DBGLEVEL = MF_DBG_ALL;\r | |
27 | \r | |
28 | // crypto1 helpers\r | |
29 | void mf_crypto1_decryptEx(struct Crypto1State *pcs, uint8_t *data_in, int len, uint8_t *data_out){\r | |
30 | uint8_t bt = 0;\r | |
31 | int i;\r | |
32 | \r | |
33 | if (len != 1) {\r | |
34 | for (i = 0; i < len; i++)\r | |
35 | data_out[i] = crypto1_byte(pcs, 0x00, 0) ^ data_in[i];\r | |
36 | } else {\r | |
37 | bt = 0;\r | |
38 | for (i = 0; i < 4; i++)\r | |
39 | bt |= (crypto1_bit(pcs, 0, 0) ^ BIT(data_in[0], i)) << i;\r | |
40 | \r | |
41 | data_out[0] = bt;\r | |
42 | }\r | |
43 | return;\r | |
44 | }\r | |
45 | \r | |
46 | void mf_crypto1_decrypt(struct Crypto1State *pcs, uint8_t *data, int len){\r | |
47 | mf_crypto1_decryptEx(pcs, data, len, data);\r | |
48 | }\r | |
49 | \r | |
50 | void mf_crypto1_encrypt(struct Crypto1State *pcs, uint8_t *data, uint16_t len, uint8_t *par) {\r | |
51 | uint8_t bt = 0;\r | |
52 | int i;\r | |
53 | par[0] = 0;\r | |
54 | \r | |
55 | for (i = 0; i < len; i++) {\r | |
56 | bt = data[i];\r | |
57 | data[i] = crypto1_byte(pcs, 0x00, 0) ^ data[i];\r | |
58 | if((i&0x0007) == 0) \r | |
59 | par[i>>3] = 0;\r | |
60 | par[i>>3] |= (((filter(pcs->odd) ^ oddparity8(bt)) & 0x01)<<(7-(i&0x0007)));\r | |
61 | } \r | |
62 | return;\r | |
63 | }\r | |
64 | \r | |
65 | uint8_t mf_crypto1_encrypt4bit(struct Crypto1State *pcs, uint8_t data) {\r | |
66 | uint8_t bt = 0;\r | |
67 | int i;\r | |
68 | \r | |
69 | for (i = 0; i < 4; i++)\r | |
70 | bt |= (crypto1_bit(pcs, 0, 0) ^ BIT(data, i)) << i;\r | |
71 | \r | |
72 | return bt;\r | |
73 | }\r | |
74 | \r | |
75 | // send X byte basic commands\r | |
76 | int mifare_sendcmd(uint8_t cmd, uint8_t* data, uint8_t data_size, uint8_t* answer, uint8_t *answer_parity, uint32_t *timing)\r | |
77 | {\r | |
78 | uint8_t dcmd[data_size+3];\r | |
79 | dcmd[0] = cmd;\r | |
80 | memcpy(dcmd+1,data,data_size);\r | |
81 | AppendCrc14443a(dcmd, data_size+1);\r | |
82 | ReaderTransmit(dcmd, sizeof(dcmd), timing);\r | |
83 | int len = ReaderReceive(answer, answer_parity);\r | |
84 | if(!len) {\r | |
85 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("%02X Cmd failed. Card timeout.", cmd);\r | |
86 | len = ReaderReceive(answer,answer_parity);\r | |
87 | //return 0;\r | |
88 | }\r | |
89 | return len;\r | |
90 | }\r | |
91 | \r | |
92 | // send 2 byte commands\r | |
93 | int mifare_sendcmd_short(struct Crypto1State *pcs, uint8_t crypted, uint8_t cmd, uint8_t data, uint8_t *answer, uint8_t *answer_parity, uint32_t *timing)\r | |
94 | {\r | |
95 | uint8_t dcmd[4], ecmd[4];\r | |
96 | uint16_t pos, res;\r | |
97 | uint8_t par[1]; // 1 Byte parity is enough here\r | |
98 | dcmd[0] = cmd;\r | |
99 | dcmd[1] = data;\r | |
100 | AppendCrc14443a(dcmd, 2);\r | |
101 | \r | |
102 | memcpy(ecmd, dcmd, sizeof(dcmd));\r | |
103 | \r | |
104 | if (crypted) {\r | |
105 | par[0] = 0;\r | |
106 | for (pos = 0; pos < 4; pos++)\r | |
107 | {\r | |
108 | ecmd[pos] = crypto1_byte(pcs, 0x00, 0) ^ dcmd[pos];\r | |
109 | par[0] |= (((filter(pcs->odd) ^ oddparity8(dcmd[pos])) & 0x01) << (7-pos));\r | |
110 | } \r | |
111 | \r | |
112 | ReaderTransmitPar(ecmd, sizeof(ecmd), par, timing);\r | |
113 | \r | |
114 | } else {\r | |
115 | ReaderTransmit(dcmd, sizeof(dcmd), timing);\r | |
116 | }\r | |
117 | \r | |
118 | int len = ReaderReceive(answer, par);\r | |
119 | \r | |
120 | if (answer_parity) *answer_parity = par[0];\r | |
121 | \r | |
122 | if (crypted == CRYPT_ALL) {\r | |
123 | if (len == 1) {\r | |
124 | res = 0;\r | |
125 | for (pos = 0; pos < 4; pos++)\r | |
126 | res |= (crypto1_bit(pcs, 0, 0) ^ BIT(answer[0], pos)) << pos;\r | |
127 | \r | |
128 | answer[0] = res;\r | |
129 | \r | |
130 | } else {\r | |
131 | for (pos = 0; pos < len; pos++)\r | |
132 | {\r | |
133 | answer[pos] = crypto1_byte(pcs, 0x00, 0) ^ answer[pos];\r | |
134 | }\r | |
135 | }\r | |
136 | }\r | |
137 | \r | |
138 | return len;\r | |
139 | }\r | |
140 | \r | |
141 | // mifare classic commands\r | |
142 | int mifare_classic_auth(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t keyType, uint64_t ui64Key, uint8_t isNested) \r | |
143 | {\r | |
144 | return mifare_classic_authex(pcs, uid, blockNo, keyType, ui64Key, isNested, NULL, NULL);\r | |
145 | }\r | |
146 | \r | |
147 | int mifare_classic_authex(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t keyType, uint64_t ui64Key, uint8_t isNested, uint32_t *ntptr, uint32_t *timing) \r | |
148 | {\r | |
149 | // variables\r | |
150 | int len; \r | |
151 | uint32_t pos;\r | |
152 | uint8_t tmp4[4];\r | |
153 | uint8_t par[1] = {0x00};\r | |
154 | byte_t nr[4];\r | |
155 | uint32_t nt, ntpp; // Supplied tag nonce\r | |
156 | \r | |
157 | uint8_t mf_nr_ar[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };\r | |
158 | uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r | |
159 | uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r | |
160 | \r | |
161 | // Transmit MIFARE_CLASSIC_AUTH\r | |
162 | len = mifare_sendcmd_short(pcs, isNested, 0x60 + (keyType & 0x01), blockNo, receivedAnswer, receivedAnswerPar, timing);\r | |
163 | if (MF_DBGLEVEL >= 4) Dbprintf("rand tag nonce len: %x", len); \r | |
164 | if (len != 4) return 1;\r | |
165 | \r | |
166 | // "random" reader nonce:\r | |
167 | nr[0] = 0x55;\r | |
168 | nr[1] = 0x41;\r | |
169 | nr[2] = 0x49;\r | |
170 | nr[3] = 0x92; \r | |
171 | \r | |
172 | // Save the tag nonce (nt)\r | |
173 | nt = bytes_to_num(receivedAnswer, 4);\r | |
174 | \r | |
175 | // ----------------------------- crypto1 create\r | |
176 | if (isNested)\r | |
177 | crypto1_destroy(pcs);\r | |
178 | \r | |
179 | // Init cipher with key\r | |
180 | crypto1_create(pcs, ui64Key);\r | |
181 | \r | |
182 | if (isNested == AUTH_NESTED) {\r | |
183 | // decrypt nt with help of new key \r | |
184 | nt = crypto1_word(pcs, nt ^ uid, 1) ^ nt;\r | |
185 | } else {\r | |
186 | // Load (plain) uid^nt into the cipher\r | |
187 | crypto1_word(pcs, nt ^ uid, 0);\r | |
188 | }\r | |
189 | \r | |
190 | // some statistic\r | |
191 | if (!ntptr && (MF_DBGLEVEL >= 3))\r | |
192 | Dbprintf("auth uid: %08x nt: %08x", uid, nt); \r | |
193 | \r | |
194 | // save Nt\r | |
195 | if (ntptr)\r | |
196 | *ntptr = nt;\r | |
197 | \r | |
198 | // Generate (encrypted) nr+parity by loading it into the cipher (Nr)\r | |
199 | par[0] = 0;\r | |
200 | for (pos = 0; pos < 4; pos++)\r | |
201 | {\r | |
202 | mf_nr_ar[pos] = crypto1_byte(pcs, nr[pos], 0) ^ nr[pos];\r | |
203 | par[0] |= (((filter(pcs->odd) ^ oddparity8(nr[pos])) & 0x01) << (7-pos));\r | |
204 | } \r | |
205 | \r | |
206 | // Skip 32 bits in pseudo random generator\r | |
207 | nt = prng_successor(nt,32);\r | |
208 | \r | |
209 | // ar+parity\r | |
210 | for (pos = 4; pos < 8; pos++)\r | |
211 | {\r | |
212 | nt = prng_successor(nt,8);\r | |
213 | mf_nr_ar[pos] = crypto1_byte(pcs,0x00,0) ^ (nt & 0xff);\r | |
214 | par[0] |= (((filter(pcs->odd) ^ oddparity8(nt)) & 0x01) << (7-pos));\r | |
215 | } \r | |
216 | \r | |
217 | // Transmit reader nonce and reader answer\r | |
218 | ReaderTransmitPar(mf_nr_ar, sizeof(mf_nr_ar), par, NULL);\r | |
219 | \r | |
220 | // Receive 4 byte tag answer\r | |
221 | len = ReaderReceive(receivedAnswer, receivedAnswerPar);\r | |
222 | if (!len)\r | |
223 | {\r | |
224 | if (MF_DBGLEVEL >= 1) Dbprintf("Authentication failed. Card timeout.");\r | |
225 | return 2;\r | |
226 | }\r | |
227 | \r | |
228 | memcpy(tmp4, receivedAnswer, 4);\r | |
229 | ntpp = prng_successor(nt, 32) ^ crypto1_word(pcs, 0,0);\r | |
230 | \r | |
231 | if (ntpp != bytes_to_num(tmp4, 4)) {\r | |
232 | if (MF_DBGLEVEL >= 1) Dbprintf("Authentication failed. Error card response.");\r | |
233 | return 3;\r | |
234 | }\r | |
235 | \r | |
236 | return 0;\r | |
237 | }\r | |
238 | \r | |
239 | int mifare_classic_readblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t *blockData) \r | |
240 | {\r | |
241 | // variables\r | |
242 | int len; \r | |
243 | uint8_t bt[2];\r | |
244 | \r | |
245 | uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r | |
246 | uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r | |
247 | \r | |
248 | // command MIFARE_CLASSIC_READBLOCK\r | |
249 | len = mifare_sendcmd_short(pcs, 1, 0x30, blockNo, receivedAnswer, receivedAnswerPar, NULL);\r | |
250 | if (len == 1) {\r | |
251 | if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]); \r | |
252 | return 1;\r | |
253 | }\r | |
254 | if (len != 18) {\r | |
255 | if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: card timeout. len: %x", len); \r | |
256 | return 2;\r | |
257 | }\r | |
258 | \r | |
259 | memcpy(bt, receivedAnswer + 16, 2);\r | |
260 | AppendCrc14443a(receivedAnswer, 16);\r | |
261 | if (bt[0] != receivedAnswer[16] || bt[1] != receivedAnswer[17]) {\r | |
262 | if (MF_DBGLEVEL >= 1) Dbprintf("Cmd CRC response error."); \r | |
263 | return 3;\r | |
264 | }\r | |
265 | \r | |
266 | memcpy(blockData, receivedAnswer, 16);\r | |
267 | return 0;\r | |
268 | }\r | |
269 | \r | |
270 | // mifare ultralight commands\r | |
271 | int mifare_ul_ev1_auth(uint8_t *keybytes, uint8_t *pack){\r | |
272 | \r | |
273 | uint16_t len;\r | |
274 | uint8_t resp[4];\r | |
275 | uint8_t respPar[1];\r | |
276 | uint8_t key[4] = {0x00};\r | |
277 | memcpy(key, keybytes, 4);\r | |
278 | \r | |
279 | if (MF_DBGLEVEL >= MF_DBG_EXTENDED)\r | |
280 | Dbprintf("EV1 Auth : %02x%02x%02x%02x", key[0], key[1], key[2], key[3]);\r | |
281 | len = mifare_sendcmd(0x1B, key, sizeof(key), resp, respPar, NULL);\r | |
282 | //len = mifare_sendcmd_short_mfuev1auth(NULL, 0, 0x1B, key, resp, respPar, NULL);\r | |
283 | if (len != 4) {\r | |
284 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x %u", resp[0], len);\r | |
285 | return 0;\r | |
286 | }\r | |
287 | \r | |
288 | if (MF_DBGLEVEL >= MF_DBG_EXTENDED)\r | |
289 | Dbprintf("Auth Resp: %02x%02x%02x%02x", resp[0],resp[1],resp[2],resp[3]);\r | |
290 | \r | |
291 | memcpy(pack, resp, 4);\r | |
292 | return 1;\r | |
293 | }\r | |
294 | \r | |
295 | int mifare_ultra_auth(uint8_t *keybytes){\r | |
296 | \r | |
297 | /// 3des2k\r | |
298 | \r | |
299 | des3_context ctx = { 0x00 };\r | |
300 | uint8_t random_a[8] = {1,1,1,1,1,1,1,1};\r | |
301 | uint8_t random_b[8] = {0x00};\r | |
302 | uint8_t enc_random_b[8] = {0x00};\r | |
303 | uint8_t rnd_ab[16] = {0x00};\r | |
304 | uint8_t IV[8] = {0x00};\r | |
305 | uint8_t key[16] = {0x00};\r | |
306 | memcpy(key, keybytes, 16);\r | |
307 | \r | |
308 | uint16_t len;\r | |
309 | uint8_t resp[19] = {0x00};\r | |
310 | uint8_t respPar[3] = {0,0,0};\r | |
311 | \r | |
312 | // REQUEST AUTHENTICATION\r | |
313 | len = mifare_sendcmd_short(NULL, 1, 0x1A, 0x00, resp, respPar ,NULL);\r | |
314 | if (len != 11) {\r | |
315 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", resp[0]);\r | |
316 | return 0;\r | |
317 | }\r | |
318 | \r | |
319 | // tag nonce.\r | |
320 | memcpy(enc_random_b,resp+1,8);\r | |
321 | \r | |
322 | // decrypt nonce.\r | |
323 | // tdes_2key_dec(random_b, enc_random_b, sizeof(random_b), key, IV );\r | |
324 | des3_set2key_dec(&ctx, key);\r | |
325 | des3_crypt_cbc(&ctx // des3_context\r | |
326 | , DES_DECRYPT // int mode\r | |
327 | , sizeof(random_b) // length\r | |
328 | , IV // iv[8]\r | |
329 | , enc_random_b // input\r | |
330 | , random_b // output\r | |
331 | );\r | |
332 | \r | |
333 | rol(random_b,8);\r | |
334 | memcpy(rnd_ab ,random_a,8);\r | |
335 | memcpy(rnd_ab+8,random_b,8);\r | |
336 | \r | |
337 | if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {\r | |
338 | Dbprintf("enc_B: %02x %02x %02x %02x %02x %02x %02x %02x",\r | |
339 | enc_random_b[0],enc_random_b[1],enc_random_b[2],enc_random_b[3],enc_random_b[4],enc_random_b[5],enc_random_b[6],enc_random_b[7]);\r | |
340 | \r | |
341 | Dbprintf(" B: %02x %02x %02x %02x %02x %02x %02x %02x",\r | |
342 | random_b[0],random_b[1],random_b[2],random_b[3],random_b[4],random_b[5],random_b[6],random_b[7]);\r | |
343 | \r | |
344 | Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",\r | |
345 | rnd_ab[0],rnd_ab[1],rnd_ab[2],rnd_ab[3],rnd_ab[4],rnd_ab[5],rnd_ab[6],rnd_ab[7]);\r | |
346 | \r | |
347 | Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",\r | |
348 | rnd_ab[8],rnd_ab[9],rnd_ab[10],rnd_ab[11],rnd_ab[12],rnd_ab[13],rnd_ab[14],rnd_ab[15] );\r | |
349 | }\r | |
350 | \r | |
351 | // encrypt out, in, length, key, iv\r | |
352 | //tdes_2key_enc(rnd_ab, rnd_ab, sizeof(rnd_ab), key, enc_random_b);\r | |
353 | des3_set2key_enc(&ctx, key);\r | |
354 | des3_crypt_cbc(&ctx // des3_context\r | |
355 | , DES_ENCRYPT // int mode\r | |
356 | , sizeof(rnd_ab) // length\r | |
357 | , enc_random_b // iv[8]\r | |
358 | , rnd_ab // input\r | |
359 | , rnd_ab // output\r | |
360 | );\r | |
361 | \r | |
362 | //len = mifare_sendcmd_short_mfucauth(NULL, 1, 0xAF, rnd_ab, resp, respPar, NULL);\r | |
363 | len = mifare_sendcmd(0xAF, rnd_ab, sizeof(rnd_ab), resp, respPar, NULL);\r | |
364 | if (len != 11) {\r | |
365 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", resp[0]);\r | |
366 | return 0;\r | |
367 | }\r | |
368 | \r | |
369 | uint8_t enc_resp[8] = { 0,0,0,0,0,0,0,0 };\r | |
370 | uint8_t resp_random_a[8] = { 0,0,0,0,0,0,0,0 };\r | |
371 | memcpy(enc_resp, resp+1, 8);\r | |
372 | \r | |
373 | // decrypt out, in, length, key, iv \r | |
374 | // tdes_2key_dec(resp_random_a, enc_resp, 8, key, enc_random_b);\r | |
375 | des3_set2key_dec(&ctx, key);\r | |
376 | des3_crypt_cbc(&ctx // des3_context\r | |
377 | , DES_DECRYPT // int mode\r | |
378 | , 8 // length\r | |
379 | , enc_random_b // iv[8]\r | |
380 | , enc_resp // input\r | |
381 | , resp_random_a // output\r | |
382 | );\r | |
383 | if ( memcmp(resp_random_a, random_a, 8) != 0 ) {\r | |
384 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("failed authentication");\r | |
385 | return 0;\r | |
386 | }\r | |
387 | \r | |
388 | if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {\r | |
389 | Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x", \r | |
390 | rnd_ab[0],rnd_ab[1],rnd_ab[2],rnd_ab[3],\r | |
391 | rnd_ab[4],rnd_ab[5],rnd_ab[6],rnd_ab[7]);\r | |
392 | \r | |
393 | Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x",\r | |
394 | rnd_ab[8],rnd_ab[9],rnd_ab[10],rnd_ab[11],\r | |
395 | rnd_ab[12],rnd_ab[13],rnd_ab[14],rnd_ab[15]);\r | |
396 | \r | |
397 | Dbprintf("a: %02x %02x %02x %02x %02x %02x %02x %02x",\r | |
398 | random_a[0],random_a[1],random_a[2],random_a[3],\r | |
399 | random_a[4],random_a[5],random_a[6],random_a[7]);\r | |
400 | \r | |
401 | Dbprintf("b: %02x %02x %02x %02x %02x %02x %02x %02x",\r | |
402 | resp_random_a[0],resp_random_a[1],resp_random_a[2],resp_random_a[3],\r | |
403 | resp_random_a[4],resp_random_a[5],resp_random_a[6],resp_random_a[7]);\r | |
404 | }\r | |
405 | return 1;\r | |
406 | }\r | |
407 | \r | |
408 | \r | |
409 | #define MFU_MAX_RETRIES 5\r | |
410 | int mifare_ultra_readblock(uint8_t blockNo, uint8_t *blockData)\r | |
411 | {\r | |
412 | uint16_t len;\r | |
413 | uint8_t bt[2];\r | |
414 | uint8_t receivedAnswer[MAX_FRAME_SIZE];\r | |
415 | uint8_t receivedAnswerPar[MAX_PARITY_SIZE];\r | |
416 | uint8_t retries;\r | |
417 | int result = 0;\r | |
418 | \r | |
419 | for (retries = 0; retries < MFU_MAX_RETRIES; retries++) {\r | |
420 | len = mifare_sendcmd_short(NULL, 1, 0x30, blockNo, receivedAnswer, receivedAnswerPar, NULL);\r | |
421 | if (len == 1) {\r | |
422 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);\r | |
423 | result = 1;\r | |
424 | continue;\r | |
425 | }\r | |
426 | if (len != 18) {\r | |
427 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: card timeout. len: %x", len);\r | |
428 | result = 2;\r | |
429 | continue;\r | |
430 | }\r | |
431 | \r | |
432 | memcpy(bt, receivedAnswer + 16, 2);\r | |
433 | AppendCrc14443a(receivedAnswer, 16);\r | |
434 | if (bt[0] != receivedAnswer[16] || bt[1] != receivedAnswer[17]) {\r | |
435 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd CRC response error.");\r | |
436 | result = 3;\r | |
437 | continue;\r | |
438 | }\r | |
439 | \r | |
440 | // No errors encountered; don't retry\r | |
441 | result = 0;\r | |
442 | break;\r | |
443 | }\r | |
444 | \r | |
445 | if (result != 0) {\r | |
446 | Dbprintf("Cmd Error: too many retries; read failed");\r | |
447 | return result;\r | |
448 | }\r | |
449 | \r | |
450 | memcpy(blockData, receivedAnswer, 14);\r | |
451 | return 0;\r | |
452 | }\r | |
453 | \r | |
454 | int mifare_classic_writeblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t *blockData) \r | |
455 | {\r | |
456 | // variables\r | |
457 | uint16_t len, i; \r | |
458 | uint32_t pos;\r | |
459 | uint8_t par[3] = {0}; // enough for 18 Bytes to send\r | |
460 | byte_t res;\r | |
461 | \r | |
462 | uint8_t d_block[18], d_block_enc[18];\r | |
463 | uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r | |
464 | uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r | |
465 | \r | |
466 | // command MIFARE_CLASSIC_WRITEBLOCK\r | |
467 | len = mifare_sendcmd_short(pcs, 1, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL);\r | |
468 | \r | |
469 | if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK\r | |
470 | if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]); \r | |
471 | return 1;\r | |
472 | }\r | |
473 | \r | |
474 | memcpy(d_block, blockData, 16);\r | |
475 | AppendCrc14443a(d_block, 16);\r | |
476 | \r | |
477 | // crypto\r | |
478 | for (pos = 0; pos < 18; pos++)\r | |
479 | {\r | |
480 | d_block_enc[pos] = crypto1_byte(pcs, 0x00, 0) ^ d_block[pos];\r | |
481 | par[pos>>3] |= (((filter(pcs->odd) ^ oddparity8(d_block[pos])) & 0x01) << (7 - (pos&0x0007)));\r | |
482 | } \r | |
483 | \r | |
484 | ReaderTransmitPar(d_block_enc, sizeof(d_block_enc), par, NULL);\r | |
485 | \r | |
486 | // Receive the response\r | |
487 | len = ReaderReceive(receivedAnswer, receivedAnswerPar); \r | |
488 | \r | |
489 | res = 0;\r | |
490 | for (i = 0; i < 4; i++)\r | |
491 | res |= (crypto1_bit(pcs, 0, 0) ^ BIT(receivedAnswer[0], i)) << i;\r | |
492 | \r | |
493 | if ((len != 1) || (res != 0x0A)) {\r | |
494 | if (MF_DBGLEVEL >= 1) Dbprintf("Cmd send data2 Error: %02x", res); \r | |
495 | return 2;\r | |
496 | }\r | |
497 | \r | |
498 | return 0;\r | |
499 | }\r | |
500 | \r | |
501 | /* // command not needed, but left for future testing\r | |
502 | int mifare_ultra_writeblock_compat(uint8_t blockNo, uint8_t *blockData) \r | |
503 | {\r | |
504 | uint16_t len;\r | |
505 | uint8_t par[3] = {0}; // enough for 18 parity bits\r | |
506 | uint8_t d_block[18] = {0x00};\r | |
507 | uint8_t receivedAnswer[MAX_FRAME_SIZE];\r | |
508 | uint8_t receivedAnswerPar[MAX_PARITY_SIZE];\r | |
509 | \r | |
510 | len = mifare_sendcmd_short(NULL, true, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL);\r | |
511 | \r | |
512 | if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK\r | |
513 | if (MF_DBGLEVEL >= MF_DBG_ERROR)\r | |
514 | Dbprintf("Cmd Addr Error: %02x", receivedAnswer[0]);\r | |
515 | return 1;\r | |
516 | }\r | |
517 | \r | |
518 | memcpy(d_block, blockData, 16);\r | |
519 | AppendCrc14443a(d_block, 16);\r | |
520 | \r | |
521 | ReaderTransmitPar(d_block, sizeof(d_block), par, NULL);\r | |
522 | \r | |
523 | len = ReaderReceive(receivedAnswer, receivedAnswerPar);\r | |
524 | \r | |
525 | if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK\r | |
526 | if (MF_DBGLEVEL >= MF_DBG_ERROR)\r | |
527 | Dbprintf("Cmd Data Error: %02x %d", receivedAnswer[0],len);\r | |
528 | return 2;\r | |
529 | }\r | |
530 | return 0;\r | |
531 | }\r | |
532 | */\r | |
533 | \r | |
534 | int mifare_ultra_writeblock(uint8_t blockNo, uint8_t *blockData)\r | |
535 | {\r | |
536 | uint16_t len;\r | |
537 | uint8_t d_block[5] = {0x00};\r | |
538 | uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r | |
539 | uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r | |
540 | \r | |
541 | // command MIFARE_CLASSIC_WRITEBLOCK\r | |
542 | d_block[0]= blockNo;\r | |
543 | memcpy(d_block+1,blockData,4);\r | |
544 | //AppendCrc14443a(d_block, 6);\r | |
545 | \r | |
546 | len = mifare_sendcmd(0xA2, d_block, sizeof(d_block), receivedAnswer, receivedAnswerPar, NULL);\r | |
547 | \r | |
548 | if (receivedAnswer[0] != 0x0A) { // 0x0a - ACK\r | |
549 | if (MF_DBGLEVEL >= MF_DBG_ERROR)\r | |
550 | Dbprintf("Cmd Send Error: %02x %d", receivedAnswer[0],len);\r | |
551 | return 1;\r | |
552 | }\r | |
553 | return 0;\r | |
554 | }\r | |
555 | \r | |
556 | int mifare_classic_halt(struct Crypto1State *pcs, uint32_t uid) \r | |
557 | {\r | |
558 | uint16_t len; \r | |
559 | uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r | |
560 | uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r | |
561 | \r | |
562 | len = mifare_sendcmd_short(pcs, pcs == NULL ? false:true, 0x50, 0x00, receivedAnswer, receivedAnswerPar, NULL);\r | |
563 | if (len != 0) {\r | |
564 | if (MF_DBGLEVEL >= MF_DBG_ERROR)\r | |
565 | Dbprintf("halt error. response len: %x", len); \r | |
566 | return 1;\r | |
567 | }\r | |
568 | \r | |
569 | return 0;\r | |
570 | }\r | |
571 | \r | |
572 | int mifare_ultra_halt()\r | |
573 | {\r | |
574 | uint16_t len;\r | |
575 | uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r | |
576 | uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r | |
577 | \r | |
578 | len = mifare_sendcmd_short(NULL, true, 0x50, 0x00, receivedAnswer, receivedAnswerPar, NULL);\r | |
579 | if (len != 0) {\r | |
580 | if (MF_DBGLEVEL >= MF_DBG_ERROR)\r | |
581 | Dbprintf("halt error. response len: %x", len);\r | |
582 | return 1;\r | |
583 | }\r | |
584 | return 0;\r | |
585 | }\r | |
586 | \r | |
587 | \r | |
588 | // Mifare Memory Structure: up to 32 Sectors with 4 blocks each (1k and 2k cards),\r | |
589 | // plus evtl. 8 sectors with 16 blocks each (4k cards)\r | |
590 | uint8_t NumBlocksPerSector(uint8_t sectorNo) \r | |
591 | {\r | |
592 | if (sectorNo < 32) \r | |
593 | return 4;\r | |
594 | else\r | |
595 | return 16;\r | |
596 | }\r | |
597 | \r | |
598 | uint8_t FirstBlockOfSector(uint8_t sectorNo) \r | |
599 | {\r | |
600 | if (sectorNo < 32)\r | |
601 | return sectorNo * 4;\r | |
602 | else\r | |
603 | return 32*4 + (sectorNo - 32) * 16;\r | |
604 | \r | |
605 | }\r | |
606 | \r | |
607 | uint8_t SectorTrailer(uint8_t blockNo)\r | |
608 | {\r | |
609 | if (blockNo < 32*4) {\r | |
610 | return (blockNo | 0x03);\r | |
611 | } else {\r | |
612 | return (blockNo | 0x0f);\r | |
613 | }\r | |
614 | }\r | |
615 | \r | |
616 | bool IsSectorTrailer(uint8_t blockNo)\r | |
617 | {\r | |
618 | return (blockNo == SectorTrailer(blockNo));\r | |
619 | }\r | |
620 | \r | |
621 | // work with emulator memory\r | |
622 | void emlSetMem(uint8_t *data, int blockNum, int blocksCount) {\r | |
623 | uint8_t* emCARD = BigBuf_get_EM_addr();\r | |
624 | memcpy(emCARD + blockNum * 16, data, blocksCount * 16);\r | |
625 | }\r | |
626 | \r | |
627 | void emlGetMem(uint8_t *data, int blockNum, int blocksCount) {\r | |
628 | uint8_t* emCARD = BigBuf_get_EM_addr();\r | |
629 | memcpy(data, emCARD + blockNum * 16, blocksCount * 16);\r | |
630 | }\r | |
631 | \r | |
632 | void emlGetMemBt(uint8_t *data, int bytePtr, int byteCount) {\r | |
633 | uint8_t* emCARD = BigBuf_get_EM_addr();\r | |
634 | memcpy(data, emCARD + bytePtr, byteCount);\r | |
635 | }\r | |
636 | \r | |
637 | int emlCheckValBl(int blockNum) {\r | |
638 | uint8_t* emCARD = BigBuf_get_EM_addr();\r | |
639 | uint8_t* data = emCARD + blockNum * 16;\r | |
640 | \r | |
641 | if ((data[0] != (data[4] ^ 0xff)) || (data[0] != data[8]) ||\r | |
642 | (data[1] != (data[5] ^ 0xff)) || (data[1] != data[9]) ||\r | |
643 | (data[2] != (data[6] ^ 0xff)) || (data[2] != data[10]) ||\r | |
644 | (data[3] != (data[7] ^ 0xff)) || (data[3] != data[11]) ||\r | |
645 | (data[12] != (data[13] ^ 0xff)) || (data[12] != data[14]) ||\r | |
646 | (data[12] != (data[15] ^ 0xff))\r | |
647 | ) \r | |
648 | return 1;\r | |
649 | return 0;\r | |
650 | }\r | |
651 | \r | |
652 | int emlGetValBl(uint32_t *blReg, uint8_t *blBlock, int blockNum) {\r | |
653 | uint8_t* emCARD = BigBuf_get_EM_addr();\r | |
654 | uint8_t* data = emCARD + blockNum * 16;\r | |
655 | \r | |
656 | if (emlCheckValBl(blockNum)) {\r | |
657 | return 1;\r | |
658 | }\r | |
659 | \r | |
660 | memcpy(blReg, data, 4);\r | |
661 | *blBlock = data[12];\r | |
662 | return 0;\r | |
663 | }\r | |
664 | \r | |
665 | int emlSetValBl(uint32_t blReg, uint8_t blBlock, int blockNum) {\r | |
666 | uint8_t* emCARD = BigBuf_get_EM_addr();\r | |
667 | uint8_t* data = emCARD + blockNum * 16;\r | |
668 | \r | |
669 | memcpy(data + 0, &blReg, 4);\r | |
670 | memcpy(data + 8, &blReg, 4);\r | |
671 | blReg = blReg ^ 0xffffffff;\r | |
672 | memcpy(data + 4, &blReg, 4);\r | |
673 | \r | |
674 | data[12] = blBlock;\r | |
675 | data[13] = blBlock ^ 0xff;\r | |
676 | data[14] = blBlock;\r | |
677 | data[15] = blBlock ^ 0xff;\r | |
678 | \r | |
679 | return 0;\r | |
680 | }\r | |
681 | \r | |
682 | uint64_t emlGetKey(int sectorNum, int keyType) {\r | |
683 | uint8_t key[6];\r | |
684 | uint8_t* emCARD = BigBuf_get_EM_addr();\r | |
685 | \r | |
686 | memcpy(key, emCARD + 16 * (FirstBlockOfSector(sectorNum) + NumBlocksPerSector(sectorNum) - 1) + keyType * 10, 6);\r | |
687 | return bytes_to_num(key, 6);\r | |
688 | }\r | |
689 | \r | |
690 | void emlClearMem(void) {\r | |
691 | int b;\r | |
692 | \r | |
693 | const uint8_t trailer[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x80, 0x69, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};\r | |
694 | const uint8_t uid[] = {0xe6, 0x84, 0x87, 0xf3, 0x16, 0x88, 0x04, 0x00, 0x46, 0x8e, 0x45, 0x55, 0x4d, 0x70, 0x41, 0x04};\r | |
695 | uint8_t* emCARD = BigBuf_get_EM_addr();\r | |
696 | \r | |
697 | memset(emCARD, 0, CARD_MEMORY_SIZE);\r | |
698 | \r | |
699 | // fill sectors trailer data\r | |
700 | for(b = 3; b < 256; b<127?(b+=4):(b+=16)) {\r | |
701 | emlSetMem((uint8_t *)trailer, b , 1);\r | |
702 | } \r | |
703 | \r | |
704 | // uid\r | |
705 | emlSetMem((uint8_t *)uid, 0, 1);\r | |
706 | return;\r | |
707 | }\r | |
708 | \r | |
709 | \r | |
710 | // Mifare desfire commands\r | |
711 | int mifare_sendcmd_special(struct Crypto1State *pcs, uint8_t crypted, uint8_t cmd, uint8_t* data, uint8_t* answer, uint8_t *answer_parity, uint32_t *timing)\r | |
712 | {\r | |
713 | uint8_t dcmd[5] = {0x00};\r | |
714 | dcmd[0] = cmd;\r | |
715 | memcpy(dcmd+1,data,2);\r | |
716 | AppendCrc14443a(dcmd, 3);\r | |
717 | \r | |
718 | ReaderTransmit(dcmd, sizeof(dcmd), NULL);\r | |
719 | int len = ReaderReceive(answer, answer_parity);\r | |
720 | if(!len) {\r | |
721 | if (MF_DBGLEVEL >= MF_DBG_ERROR) \r | |
722 | Dbprintf("Authentication failed. Card timeout.");\r | |
723 | return 1;\r | |
724 | }\r | |
725 | return len;\r | |
726 | }\r | |
727 | \r | |
728 | int mifare_sendcmd_special2(struct Crypto1State *pcs, uint8_t crypted, uint8_t cmd, uint8_t* data, uint8_t* answer,uint8_t *answer_parity, uint32_t *timing)\r | |
729 | {\r | |
730 | uint8_t dcmd[20] = {0x00};\r | |
731 | dcmd[0] = cmd;\r | |
732 | memcpy(dcmd+1,data,17);\r | |
733 | AppendCrc14443a(dcmd, 18);\r | |
734 | \r | |
735 | ReaderTransmit(dcmd, sizeof(dcmd), NULL);\r | |
736 | int len = ReaderReceive(answer, answer_parity);\r | |
737 | if(!len){\r | |
738 | if (MF_DBGLEVEL >= MF_DBG_ERROR)\r | |
739 | Dbprintf("Authentication failed. Card timeout.");\r | |
740 | return 1;\r | |
741 | }\r | |
742 | return len;\r | |
743 | }\r | |
744 | \r | |
745 | int mifare_desfire_des_auth1(uint32_t uid, uint8_t *blockData){\r | |
746 | \r | |
747 | int len;\r | |
748 | // load key, keynumber\r | |
749 | uint8_t data[2]={0x0a, 0x00};\r | |
750 | uint8_t receivedAnswer[MAX_FRAME_SIZE];\r | |
751 | uint8_t receivedAnswerPar[MAX_PARITY_SIZE];\r | |
752 | \r | |
753 | len = mifare_sendcmd_special(NULL, 1, 0x02, data, receivedAnswer,receivedAnswerPar,NULL);\r | |
754 | if (len == 1) {\r | |
755 | if (MF_DBGLEVEL >= MF_DBG_ERROR)\r | |
756 | Dbprintf("Cmd Error: %02x", receivedAnswer[0]);\r | |
757 | return 1;\r | |
758 | }\r | |
759 | \r | |
760 | if (len == 12) {\r | |
761 | if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {\r | |
762 | Dbprintf("Auth1 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",\r | |
763 | receivedAnswer[0],receivedAnswer[1],receivedAnswer[2],receivedAnswer[3],receivedAnswer[4],\r | |
764 | receivedAnswer[5],receivedAnswer[6],receivedAnswer[7],receivedAnswer[8],receivedAnswer[9],\r | |
765 | receivedAnswer[10],receivedAnswer[11]);\r | |
766 | }\r | |
767 | memcpy(blockData, receivedAnswer, 12);\r | |
768 | return 0;\r | |
769 | }\r | |
770 | return 1;\r | |
771 | }\r | |
772 | \r | |
773 | int mifare_desfire_des_auth2(uint32_t uid, uint8_t *key, uint8_t *blockData){\r | |
774 | \r | |
775 | int len;\r | |
776 | uint8_t data[17] = {0x00};\r | |
777 | data[0] = 0xAF;\r | |
778 | memcpy(data+1,key,16);\r | |
779 | \r | |
780 | uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r | |
781 | uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r | |
782 | \r | |
783 | len = mifare_sendcmd_special2(NULL, 1, 0x03, data, receivedAnswer, receivedAnswerPar ,NULL);\r | |
784 | \r | |
785 | if ((receivedAnswer[0] == 0x03) && (receivedAnswer[1] == 0xae)) {\r | |
786 | if (MF_DBGLEVEL >= MF_DBG_ERROR)\r | |
787 | Dbprintf("Auth Error: %02x %02x", receivedAnswer[0], receivedAnswer[1]);\r | |
788 | return 1;\r | |
789 | }\r | |
790 | \r | |
791 | if (len == 12){\r | |
792 | if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {\r | |
793 | Dbprintf("Auth2 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",\r | |
794 | receivedAnswer[0],receivedAnswer[1],receivedAnswer[2],receivedAnswer[3],receivedAnswer[4],\r | |
795 | receivedAnswer[5],receivedAnswer[6],receivedAnswer[7],receivedAnswer[8],receivedAnswer[9],\r | |
796 | receivedAnswer[10],receivedAnswer[11]);\r | |
797 | }\r | |
798 | memcpy(blockData, receivedAnswer, 12);\r | |
799 | return 0;\r | |
800 | }\r | |
801 | return 1;\r | |
802 | }\r | |
803 | \r | |
804 | //-----------------------------------------------------------------------------\r | |
805 | // MIFARE check keys\r | |
806 | //\r | |
807 | //-----------------------------------------------------------------------------\r | |
808 | // one key check\r | |
809 | int MifareChkBlockKey(uint8_t *uid, uint32_t *cuid, uint8_t *cascade_levels, uint64_t ui64Key, uint8_t blockNo, uint8_t keyType, uint8_t debugLevel) {\r | |
810 | \r | |
811 | struct Crypto1State mpcs = {0, 0};\r | |
812 | struct Crypto1State *pcs;\r | |
813 | pcs = &mpcs;\r | |
814 | \r | |
815 | // Iceman: use piwi's faster nonce collecting part in hardnested.\r | |
816 | if (*cascade_levels == 0) { // need a full select cycle to get the uid first\r | |
817 | iso14a_card_select_t card_info;\r | |
818 | if(!iso14443a_select_card(uid, &card_info, cuid, true, 0, true)) {\r | |
819 | if (debugLevel >= 1) Dbprintf("ChkKeys: Can't select card");\r | |
820 | return 1;\r | |
821 | }\r | |
822 | switch (card_info.uidlen) {\r | |
823 | case 4 : *cascade_levels = 1; break;\r | |
824 | case 7 : *cascade_levels = 2; break;\r | |
825 | case 10: *cascade_levels = 3; break;\r | |
826 | default: break;\r | |
827 | }\r | |
828 | } else { // no need for anticollision. We can directly select the card\r | |
829 | if(!iso14443a_select_card(uid, NULL, NULL, false, *cascade_levels, true)) {\r | |
830 | if (debugLevel >= 1) Dbprintf("ChkKeys: Can't select card (UID) lvl=%d", *cascade_levels);\r | |
831 | return 1;\r | |
832 | }\r | |
833 | }\r | |
834 | \r | |
835 | if(mifare_classic_auth(pcs, *cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) {\r | |
836 | // SpinDelayUs(AUTHENTICATION_TIMEOUT); // it not needs because mifare_classic_auth have timeout from iso14a_set_timeout()\r | |
837 | return 2;\r | |
838 | } else {\r | |
839 | /* // let it be here. it like halt command, but maybe it will work in some strange cases\r | |
840 | uint8_t dummy_answer = 0;\r | |
841 | ReaderTransmit(&dummy_answer, 1, NULL);\r | |
842 | int timeout = GetCountSspClk() + AUTHENTICATION_TIMEOUT; \r | |
843 | // wait for the card to become ready again\r | |
844 | while(GetCountSspClk() < timeout) {};\r | |
845 | */\r | |
846 | // it needs after success authentication\r | |
847 | mifare_classic_halt(pcs, *cuid);\r | |
848 | }\r | |
849 | \r | |
850 | return 0;\r | |
851 | }\r | |
852 | \r | |
853 | // multi key check\r | |
854 | int MifareChkBlockKeys(uint8_t *keys, uint8_t keyCount, uint8_t blockNo, uint8_t keyType, uint8_t debugLevel) {\r | |
855 | uint8_t uid[10];\r | |
856 | uint32_t cuid = 0;\r | |
857 | uint8_t cascade_levels = 0;\r | |
858 | uint64_t ui64Key = 0;\r | |
859 | \r | |
860 | int retryCount = 0;\r | |
861 | for (uint8_t i = 0; i < keyCount; i++) {\r | |
862 | \r | |
863 | // Allow button press / usb cmd to interrupt device\r | |
864 | if (BUTTON_PRESS() && !usb_poll_validate_length()) { \r | |
865 | Dbprintf("ChkKeys: Cancel operation. Exit...");\r | |
866 | return -2;\r | |
867 | }\r | |
868 | \r | |
869 | ui64Key = bytes_to_num(keys + i * 6, 6);\r | |
870 | int res = MifareChkBlockKey(uid, &cuid, &cascade_levels, ui64Key, blockNo, keyType, debugLevel);\r | |
871 | \r | |
872 | // can't select\r | |
873 | if (res == 1) {\r | |
874 | retryCount++;\r | |
875 | if (retryCount >= 5) {\r | |
876 | Dbprintf("ChkKeys: block=%d key=%d. Can't select. Exit...", blockNo, keyType);\r | |
877 | return -1;\r | |
878 | }\r | |
879 | --i; // try the same key once again\r | |
880 | \r | |
881 | SpinDelay(20);\r | |
882 | // Dbprintf("ChkKeys: block=%d key=%d. Try the same key once again...", blockNo, keyType);\r | |
883 | continue;\r | |
884 | }\r | |
885 | \r | |
886 | // can't authenticate\r | |
887 | if (res == 2) {\r | |
888 | retryCount = 0;\r | |
889 | continue; // can't auth. wrong key.\r | |
890 | }\r | |
891 | \r | |
892 | return i + 1;\r | |
893 | }\r | |
894 | \r | |
895 | return 0;\r | |
896 | }\r | |
897 | \r | |
898 | // multisector multikey check\r | |
899 | int MifareMultisectorChk(uint8_t *keys, uint8_t keyCount, uint8_t SectorCount, uint8_t keyType, uint8_t debugLevel, TKeyIndex *keyIndex) {\r | |
900 | int res = 0;\r | |
901 | \r | |
902 | // int clk = GetCountSspClk();\r | |
903 | \r | |
904 | for(int sc = 0; sc < SectorCount; sc++){\r | |
905 | WDT_HIT();\r | |
906 | \r | |
907 | int keyAB = keyType;\r | |
908 | do {\r | |
909 | res = MifareChkBlockKeys(keys, keyCount, FirstBlockOfSector(sc), keyAB & 0x01, debugLevel);\r | |
910 | if (res < 0){\r | |
911 | return res;\r | |
912 | }\r | |
913 | if (res > 0){\r | |
914 | (*keyIndex)[keyAB & 0x01][sc] = res;\r | |
915 | }\r | |
916 | } while(--keyAB > 0);\r | |
917 | }\r | |
918 | \r | |
919 | // Dbprintf("%d %d", GetCountSspClk() - clk, (GetCountSspClk() - clk)/(SectorCount*keyCount*(keyType==2?2:1)));\r | |
920 | \r | |
921 | return 0;\r | |
922 | }\r | |
923 | \r | |
924 | \r |