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