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