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