]> cvs.zerfleddert.de Git - proxmark3-svn/blame - armsrc/mifareutil.c
reenable intermediate "Waiting for a response from the proxmark..." message
[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
9492e0b0 116 ReaderTransmitPar(ecmd, sizeof(ecmd), par, timing);\r
20f9a2a1 117 } else {\r
9492e0b0 118 ReaderTransmit(dcmd, sizeof(dcmd), timing);\r
20f9a2a1
M
119 }\r
120\r
6a1f2d82 121 int len = ReaderReceive(answer, par);\r
a8561e35 122\r
6a1f2d82 123 if (answer_parity) *answer_parity = par[0];\r
a8561e35 124\r
4abe4f58 125 if (crypted == CRYPT_ALL) {\r
20f9a2a1
M
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
a8561e35 130\r
20f9a2a1 131 answer[0] = res;\r
a8561e35 132\r
20f9a2a1
M
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
a8561e35 140\r
20f9a2a1
M
141 return len;\r
142}\r
143\r
a749b1e5 144\r
787b5bd8 145// mifare classic commands\r
a749b1e5 146int 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
f89c7050
M
149}\r
150\r
a749b1e5 151\r
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, uint32_t *auth_timeout) {\r
153\r
a8561e35 154 int len;\r
20f9a2a1 155 uint32_t pos;\r
3fe4ff4f 156 uint8_t par[1] = {0x00};\r
6a1f2d82 157 byte_t nr[4];\r
20f9a2a1 158 uint32_t nt, ntpp; // Supplied tag nonce\r
a8561e35 159\r
20f9a2a1 160 uint8_t mf_nr_ar[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };\r
f71f4deb 161 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
162 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
a8561e35 163\r
4abe4f58 164 // Transmit MIFARE_CLASSIC_AUTH\r
0b4efbde 165 len = mifare_sendcmd_short(pcs, isNested, keyType & 0x01 ? MIFARE_AUTH_KEYB : MIFARE_AUTH_KEYA, blockNo, receivedAnswer, receivedAnswerPar, timing);\r
a8561e35 166 if (MF_DBGLEVEL >= 4) Dbprintf("rand tag nonce len: %x", len);\r
4abe4f58 167 if (len != 4) return 1;\r
a8561e35 168\r
6a1f2d82 169 // "random" reader nonce:\r
170 nr[0] = 0x55;\r
171 nr[1] = 0x41;\r
172 nr[2] = 0x49;\r
a8561e35 173 nr[3] = 0x92;\r
174\r
20f9a2a1
M
175 // Save the tag nonce (nt)\r
176 nt = bytes_to_num(receivedAnswer, 4);\r
20f9a2a1
M
177\r
178 // ----------------------------- crypto1 create\r
4abe4f58
M
179 if (isNested)\r
180 crypto1_destroy(pcs);\r
181\r
182 // Init cipher with key\r
20f9a2a1
M
183 crypto1_create(pcs, ui64Key);\r
184\r
4abe4f58 185 if (isNested == AUTH_NESTED) {\r
a8561e35 186 // decrypt nt with help of new key\r
4abe4f58
M
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
f397b5cc 194 if (!ntptr && (MF_DBGLEVEL >= 3))\r
a8561e35 195 Dbprintf("auth uid: %08x nt: %08x", uid, nt);\r
196\r
f89c7050
M
197 // save Nt\r
198 if (ntptr)\r
199 *ntptr = nt;\r
20f9a2a1 200\r
4abe4f58 201 // Generate (encrypted) nr+parity by loading it into the cipher (Nr)\r
6a1f2d82 202 par[0] = 0;\r
a749b1e5 203 for (pos = 0; pos < 4; pos++) {\r
6a1f2d82 204 mf_nr_ar[pos] = crypto1_byte(pcs, nr[pos], 0) ^ nr[pos];\r
1f065e1d 205 par[0] |= (((filter(pcs->odd) ^ oddparity8(nr[pos])) & 0x01) << (7-pos));\r
a8561e35 206 }\r
207\r
4abe4f58
M
208 // Skip 32 bits in pseudo random generator\r
209 nt = prng_successor(nt,32);\r
20f9a2a1
M
210\r
211 // ar+parity\r
a749b1e5 212 for (pos = 4; pos < 8; pos++) {\r
20f9a2a1 213 nt = prng_successor(nt,8);\r
4abe4f58 214 mf_nr_ar[pos] = crypto1_byte(pcs,0x00,0) ^ (nt & 0xff);\r
1f065e1d 215 par[0] |= (((filter(pcs->odd) ^ oddparity8(nt)) & 0x01) << (7-pos));\r
a8561e35 216 }\r
217\r
4abe4f58 218 // Transmit reader nonce and reader answer\r
9492e0b0 219 ReaderTransmitPar(mf_nr_ar, sizeof(mf_nr_ar), par, NULL);\r
20f9a2a1 220\r
6a1f2d82 221 // Receive 4 byte tag answer\r
a749b1e5 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
6a1f2d82 227 len = ReaderReceive(receivedAnswer, receivedAnswerPar);\r
a749b1e5 228 iso14a_set_timeout(save_timeout); // restore standard timeout\r
229 if (!len) {\r
a8561e35 230 if (MF_DBGLEVEL >= 1) Dbprintf("Authentication failed. Card timeout.");\r
20f9a2a1 231 return 2;\r
4abe4f58 232 }\r
a749b1e5 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
a8561e35 236\r
a749b1e5 237 ntpp = prng_successor(nt, 32) ^ crypto1_word(pcs, 0, 0);\r
a8561e35 238\r
a749b1e5 239 if (ntpp != bytes_to_num(receivedAnswer, 4)) {\r
a8561e35 240 if (MF_DBGLEVEL >= 1) Dbprintf("Authentication failed. Error card response.");\r
20f9a2a1
M
241 return 3;\r
242 }\r
243\r
244 return 0;\r
245}\r
246\r
a749b1e5 247\r
248int mifare_classic_readblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t *blockData) {\r
20f9a2a1 249 // variables\r
a8561e35 250 int len;\r
251 uint8_t bt[2];\r
252\r
f71f4deb 253 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
254 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
a8561e35 255\r
4abe4f58 256 // command MIFARE_CLASSIC_READBLOCK\r
0b4efbde 257 len = mifare_sendcmd_short(pcs, 1, MIFARE_CMD_READBLOCK, blockNo, receivedAnswer, receivedAnswerPar, NULL);\r
20f9a2a1 258 if (len == 1) {\r
a8561e35 259 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);\r
20f9a2a1
M
260 return 1;\r
261 }\r
262 if (len != 18) {\r
a8561e35 263 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: card timeout. len: %x", len);\r
20f9a2a1
M
264 return 2;\r
265 }\r
266\r
267 memcpy(bt, receivedAnswer + 16, 2);\r
4abe4f58 268 AppendCrc14443a(receivedAnswer, 16);\r
20f9a2a1 269 if (bt[0] != receivedAnswer[16] || bt[1] != receivedAnswer[17]) {\r
a8561e35 270 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd CRC response error.");\r
20f9a2a1
M
271 return 3;\r
272 }\r
a8561e35 273\r
20f9a2a1 274 memcpy(blockData, receivedAnswer, 16);\r
f168b263 275 return 0;\r
276}\r
277\r
a631936e 278// mifare ultralight commands\r
8258f409 279int mifare_ul_ev1_auth(uint8_t *keybytes, uint8_t *pack){\r
cceabb79 280\r
281 uint16_t len;\r
8258f409 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
e35031d2 287 if (MF_DBGLEVEL >= MF_DBG_EXTENDED)\r
a8561e35 288 Dbprintf("EV1 Auth : %02x%02x%02x%02x", key[0], key[1], key[2], key[3]);\r
0b4efbde 289 len = mifare_sendcmd(MIFARE_ULEV1_AUTH, key, sizeof(key), resp, respPar, NULL);\r
4973f23d 290 //len = mifare_sendcmd_short_mfuev1auth(NULL, 0, 0x1B, key, resp, respPar, NULL);\r
cceabb79 291 if (len != 4) {\r
8258f409 292 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x %u", resp[0], len);\r
9d87eb66 293 return 0;\r
cceabb79 294 }\r
8258f409 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
9d87eb66 300 return 1;\r
cceabb79 301}\r
302\r
8258f409 303int mifare_ultra_auth(uint8_t *keybytes){\r
304\r
305 /// 3des2k\r
306\r
aa0b1c43 307 mbedtls_des3_context ctx = { {0} };\r
8258f409 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
a631936e 315\r
316 uint16_t len;\r
8258f409 317 uint8_t resp[19] = {0x00};\r
318 uint8_t respPar[3] = {0,0,0};\r
319\r
320 // REQUEST AUTHENTICATION\r
0b4efbde 321 len = mifare_sendcmd_short(NULL, 1, MIFARE_ULC_AUTH_1, 0x00, resp, respPar ,NULL);\r
f168b263 322 if (len != 11) {\r
8258f409 323 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", resp[0]);\r
9d87eb66 324 return 0;\r
a631936e 325 }\r
a631936e 326\r
8258f409 327 // tag nonce.\r
328 memcpy(enc_random_b,resp+1,8);\r
329\r
330 // decrypt nonce.\r
930763e8 331 // tdes_2key_dec(random_b, enc_random_b, sizeof(random_b), key, IV );\r
e0991f6a 332 mbedtls_des3_set2key_dec(&ctx, key);\r
a8561e35 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
930763e8 339 );\r
340\r
8258f409 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
a631936e 345 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {\r
8258f409 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
a631936e 348\r
8258f409 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
a631936e 351\r
8258f409 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
930763e8 360 //tdes_2key_enc(rnd_ab, rnd_ab, sizeof(rnd_ab), key, enc_random_b);\r
e0991f6a 361 mbedtls_des3_set2key_enc(&ctx, key);\r
a8561e35 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
930763e8 368 );\r
369\r
4973f23d 370 //len = mifare_sendcmd_short_mfucauth(NULL, 1, 0xAF, rnd_ab, resp, respPar, NULL);\r
0b4efbde 371 len = mifare_sendcmd(MIFARE_ULC_AUTH_2, rnd_ab, sizeof(rnd_ab), resp, respPar, NULL);\r
f168b263 372 if (len != 11) {\r
8258f409 373 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", resp[0]);\r
9d87eb66 374 return 0;\r
a631936e 375 }\r
8258f409 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
a8561e35 381 // decrypt out, in, length, key, iv\r
930763e8 382 // tdes_2key_dec(resp_random_a, enc_resp, 8, key, enc_random_b);\r
e0991f6a 383 mbedtls_des3_set2key_dec(&ctx, key);\r
a8561e35 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
930763e8 390 );\r
8258f409 391 if ( memcmp(resp_random_a, random_a, 8) != 0 ) {\r
392 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("failed authentication");\r
9d87eb66 393 return 0;\r
8258f409 394 }\r
395\r
a631936e 396 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {\r
a8561e35 397 Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x",\r
8258f409 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
a631936e 412 }\r
9d87eb66 413 return 1;\r
a631936e 414}\r
415\r
b24930c7
JC
416\r
417#define MFU_MAX_RETRIES 5\r
f168b263 418int mifare_ultra_readblock(uint8_t blockNo, uint8_t *blockData)\r
419{\r
420 uint16_t len;\r
a8561e35 421 uint8_t bt[2];\r
f168b263 422 uint8_t receivedAnswer[MAX_FRAME_SIZE];\r
423 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];\r
b24930c7
JC
424 uint8_t retries;\r
425 int result = 0;\r
4973f23d 426\r
b24930c7 427 for (retries = 0; retries < MFU_MAX_RETRIES; retries++) {\r
0b4efbde 428 len = mifare_sendcmd_short(NULL, 1, MIFARE_CMD_READBLOCK, blockNo, receivedAnswer, receivedAnswerPar, NULL);\r
b24930c7
JC
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
f168b263 451 }\r
b24930c7
JC
452\r
453 if (result != 0) {\r
454 Dbprintf("Cmd Error: too many retries; read failed");\r
455 return result;\r
f168b263 456 }\r
b24930c7 457\r
b8dd1ef6 458 memcpy(blockData, receivedAnswer, 16);\r
f168b263 459 return 0;\r
460}\r
461\r
a8561e35 462int mifare_classic_writeblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t *blockData)\r
f168b263 463{\r
464 // variables\r
a8561e35 465 uint16_t len, i;\r
20f9a2a1 466 uint32_t pos;\r
a8561e35 467 uint8_t par[3] = {0}; // enough for 18 Bytes to send\r
4abe4f58 468 byte_t res;\r
a8561e35 469\r
20f9a2a1 470 uint8_t d_block[18], d_block_enc[18];\r
f71f4deb 471 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
472 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
a8561e35 473\r
4abe4f58 474 // command MIFARE_CLASSIC_WRITEBLOCK\r
0b4efbde 475 len = mifare_sendcmd_short(pcs, 1, MIFARE_CMD_WRITEBLOCK, blockNo, receivedAnswer, receivedAnswerPar, NULL);\r
20f9a2a1
M
476\r
477 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK\r
a8561e35 478 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);\r
20f9a2a1
M
479 return 1;\r
480 }\r
a8561e35 481\r
20f9a2a1
M
482 memcpy(d_block, blockData, 16);\r
483 AppendCrc14443a(d_block, 16);\r
a8561e35 484\r
20f9a2a1 485 // crypto\r
4abe4f58
M
486 for (pos = 0; pos < 18; pos++)\r
487 {\r
488 d_block_enc[pos] = crypto1_byte(pcs, 0x00, 0) ^ d_block[pos];\r
1f065e1d 489 par[pos>>3] |= (((filter(pcs->odd) ^ oddparity8(d_block[pos])) & 0x01) << (7 - (pos&0x0007)));\r
a8561e35 490 }\r
20f9a2a1 491\r
9492e0b0 492 ReaderTransmitPar(d_block_enc, sizeof(d_block_enc), par, NULL);\r
20f9a2a1 493\r
4abe4f58 494 // Receive the response\r
a8561e35 495 len = ReaderReceive(receivedAnswer, receivedAnswerPar);\r
20f9a2a1
M
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
a8561e35 502 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd send data2 Error: %02x", res);\r
20f9a2a1
M
503 return 2;\r
504 }\r
a8561e35 505\r
f168b263 506 return 0;\r
507}\r
508\r
4973f23d 509/* // command not needed, but left for future testing\r
a8561e35 510int mifare_ultra_writeblock_compat(uint8_t blockNo, uint8_t *blockData)\r
f168b263 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
0b4efbde 518 len = mifare_sendcmd_short(NULL, true, MIFARE_CMD_WRITEBLOCK, blockNo, receivedAnswer, receivedAnswerPar, NULL);\r
f168b263 519\r
520 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK\r
787b5bd8 521 if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
f168b263 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
787b5bd8 534 if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
f168b263 535 Dbprintf("Cmd Data Error: %02x %d", receivedAnswer[0],len);\r
536 return 2;\r
537 }\r
538 return 0;\r
539}\r
4973f23d 540*/\r
f168b263 541\r
4973f23d 542int mifare_ultra_writeblock(uint8_t blockNo, uint8_t *blockData)\r
f168b263 543{\r
544 uint16_t len;\r
4973f23d 545 uint8_t d_block[5] = {0x00};\r
f71f4deb 546 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
547 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
f168b263 548\r
549 // command MIFARE_CLASSIC_WRITEBLOCK\r
550 d_block[0]= blockNo;\r
551 memcpy(d_block+1,blockData,4);\r
4973f23d 552 //AppendCrc14443a(d_block, 6);\r
f168b263 553\r
4973f23d 554 len = mifare_sendcmd(0xA2, d_block, sizeof(d_block), receivedAnswer, receivedAnswerPar, NULL);\r
f168b263 555\r
556 if (receivedAnswer[0] != 0x0A) { // 0x0a - ACK\r
787b5bd8 557 if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
f168b263 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
a8561e35 564int mifare_classic_halt(struct Crypto1State *pcs, uint32_t uid)\r
f168b263 565{\r
a8561e35 566 uint16_t len;\r
f71f4deb 567 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
568 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
20f9a2a1 569\r
0b4efbde 570 len = mifare_sendcmd_short(pcs, pcs == NULL ? false:true, ISO14443A_CMD_HALT, 0x00, receivedAnswer, receivedAnswerPar, NULL);\r
4abe4f58 571 if (len != 0) {\r
787b5bd8 572 if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
a8561e35 573 Dbprintf("halt error. response len: %x", len);\r
20f9a2a1
M
574 return 1;\r
575 }\r
576\r
f168b263 577 return 0;\r
578}\r
579\r
580int mifare_ultra_halt()\r
581{\r
582 uint16_t len;\r
f71f4deb 583 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
584 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
a8561e35 585\r
0b4efbde 586 len = mifare_sendcmd_short(NULL, true, ISO14443A_CMD_HALT, 0x00, receivedAnswer, receivedAnswerPar, NULL);\r
f168b263 587 if (len != 0) {\r
787b5bd8 588 if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
f168b263 589 Dbprintf("halt error. response len: %x", len);\r
590 return 1;\r
591 }\r
592 return 0;\r
593}\r
594\r
baeaf579 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
a8561e35 598uint8_t NumBlocksPerSector(uint8_t sectorNo)\r
baeaf579 599{\r
a8561e35 600 if (sectorNo < 32)\r
baeaf579 601 return 4;\r
602 else\r
603 return 16;\r
604}\r
605\r
a8561e35 606uint8_t FirstBlockOfSector(uint8_t sectorNo)\r
baeaf579 607{\r
608 if (sectorNo < 32)\r
609 return sectorNo * 4;\r
610 else\r
611 return 32*4 + (sectorNo - 32) * 16;\r
a8561e35 612\r
baeaf579 613}\r
614\r
b35e04a7 615uint8_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
624bool IsSectorTrailer(uint8_t blockNo)\r
625{\r
626 return (blockNo == SectorTrailer(blockNo));\r
627}\r
baeaf579 628\r
f168b263 629// work with emulator memory\r
630void emlSetMem(uint8_t *data, int blockNum, int blocksCount) {\r
631 uint8_t* emCARD = BigBuf_get_EM_addr();\r
8f51ddb0
M
632 memcpy(emCARD + blockNum * 16, data, blocksCount * 16);\r
633}\r
634\r
635void emlGetMem(uint8_t *data, int blockNum, int blocksCount) {\r
f71f4deb 636 uint8_t* emCARD = BigBuf_get_EM_addr();\r
8f51ddb0
M
637 memcpy(data, emCARD + blockNum * 16, blocksCount * 16);\r
638}\r
639\r
640void emlGetMemBt(uint8_t *data, int bytePtr, int byteCount) {\r
f71f4deb 641 uint8_t* emCARD = BigBuf_get_EM_addr();\r
8f51ddb0
M
642 memcpy(data, emCARD + bytePtr, byteCount);\r
643}\r
644\r
0014cb46 645int emlCheckValBl(int blockNum) {\r
f71f4deb 646 uint8_t* emCARD = BigBuf_get_EM_addr();\r
0014cb46
M
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
a8561e35 655 )\r
0014cb46
M
656 return 1;\r
657 return 0;\r
658}\r
659\r
660int emlGetValBl(uint32_t *blReg, uint8_t *blBlock, int blockNum) {\r
f71f4deb 661 uint8_t* emCARD = BigBuf_get_EM_addr();\r
0014cb46 662 uint8_t* data = emCARD + blockNum * 16;\r
a8561e35 663\r
0014cb46
M
664 if (emlCheckValBl(blockNum)) {\r
665 return 1;\r
666 }\r
a8561e35 667\r
0014cb46
M
668 memcpy(blReg, data, 4);\r
669 *blBlock = data[12];\r
0014cb46
M
670 return 0;\r
671}\r
672\r
673int emlSetValBl(uint32_t blReg, uint8_t blBlock, int blockNum) {\r
f71f4deb 674 uint8_t* emCARD = BigBuf_get_EM_addr();\r
0014cb46 675 uint8_t* data = emCARD + blockNum * 16;\r
a8561e35 676\r
0014cb46
M
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
a8561e35 681\r
0014cb46
M
682 data[12] = blBlock;\r
683 data[13] = blBlock ^ 0xff;\r
684 data[14] = blBlock;\r
685 data[15] = blBlock ^ 0xff;\r
a8561e35 686\r
0014cb46
M
687 return 0;\r
688}\r
689\r
8556b852
M
690uint64_t emlGetKey(int sectorNum, int keyType) {\r
691 uint8_t key[6];\r
f71f4deb 692 uint8_t* emCARD = BigBuf_get_EM_addr();\r
a8561e35 693\r
baeaf579 694 memcpy(key, emCARD + 16 * (FirstBlockOfSector(sectorNum) + NumBlocksPerSector(sectorNum) - 1) + keyType * 10, 6);\r
8556b852
M
695 return bytes_to_num(key, 6);\r
696}\r
697\r
8f51ddb0 698void emlClearMem(void) {\r
aea4d766 699 int b;\r
a8561e35 700\r
8f51ddb0 701 const uint8_t trailer[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x80, 0x69, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};\r
8f51ddb0 702 const uint8_t uid[] = {0xe6, 0x84, 0x87, 0xf3, 0x16, 0x88, 0x04, 0x00, 0x46, 0x8e, 0x45, 0x55, 0x4d, 0x70, 0x41, 0x04};\r
f71f4deb 703 uint8_t* emCARD = BigBuf_get_EM_addr();\r
a8561e35 704\r
6a1f2d82 705 memset(emCARD, 0, CARD_MEMORY_SIZE);\r
a8561e35 706\r
aea4d766 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
a8561e35 710 }\r
8f51ddb0
M
711\r
712 // uid\r
713 emlSetMem((uint8_t *)uid, 0, 1);\r
714 return;\r
715}\r
c8b6da22 716\r
717\r
718// Mifare desfire commands\r
719int 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
a8561e35 721 uint8_t dcmd[5] = {0x00};\r
722 dcmd[0] = cmd;\r
723 memcpy(dcmd+1,data,2);\r
c8b6da22 724 AppendCrc14443a(dcmd, 3);\r
a8561e35 725\r
c8b6da22 726 ReaderTransmit(dcmd, sizeof(dcmd), NULL);\r
727 int len = ReaderReceive(answer, answer_parity);\r
728 if(!len) {\r
a8561e35 729 if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
c8b6da22 730 Dbprintf("Authentication failed. Card timeout.");\r
731 return 1;\r
a8561e35 732 }\r
c8b6da22 733 return len;\r
734}\r
735\r
736int 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
a8561e35 738 uint8_t dcmd[20] = {0x00};\r
739 dcmd[0] = cmd;\r
740 memcpy(dcmd+1,data,17);\r
c8b6da22 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
a8561e35 746 if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
c8b6da22 747 Dbprintf("Authentication failed. Card timeout.");\r
748 return 1;\r
a8561e35 749 }\r
c8b6da22 750 return len;\r
751}\r
752\r
753int 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
29250969 758 uint8_t receivedAnswer[MAX_FRAME_SIZE];\r
759 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];\r
a8561e35 760\r
c8b6da22 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
a8561e35 767\r
c8b6da22 768 if (len == 12) {\r
a8561e35 769 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {\r
c8b6da22 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
a8561e35 776 return 0;\r
c8b6da22 777 }\r
778 return 1;\r
779}\r
780\r
781int 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
a8561e35 787\r
f71f4deb 788 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
789 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
a8561e35 790\r
c8b6da22 791 len = mifare_sendcmd_special2(NULL, 1, 0x03, data, receivedAnswer, receivedAnswerPar ,NULL);\r
a8561e35 792\r
c8b6da22 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
a8561e35 798\r
c8b6da22 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
f168b263 810}\r
275d9e61
OM
811\r
812//-----------------------------------------------------------------------------\r
813// MIFARE check keys\r
814//\r
815//-----------------------------------------------------------------------------\r
816// one key check\r
a749b1e5 817int 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
275d9e61
OM
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
a749b1e5 826 if (!iso14443a_select_card(uid, &card_info, cuid, true, 0, true)) {\r
a8561e35 827 if (debugLevel >= 1) Dbprintf("ChkKeys: Can't select card");\r
275d9e61
OM
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
a749b1e5 837 if (!iso14443a_select_card(uid, NULL, NULL, false, *cascade_levels, true)) {\r
a8561e35 838 if (debugLevel >= 1) Dbprintf("ChkKeys: Can't select card (UID) lvl=%d", *cascade_levels);\r
275d9e61
OM
839 return 1;\r
840 }\r
841 }\r
a8561e35 842\r
a749b1e5 843 if (mifare_classic_auth(pcs, *cuid, blockNo, keyType, ui64Key, AUTH_FIRST, auth_timeout)) { // authentication failed\r
275d9e61
OM
844 return 2;\r
845 } else {\r
275d9e61
OM
846 mifare_classic_halt(pcs, *cuid);\r
847 }\r
a8561e35 848\r
275d9e61
OM
849 return 0;\r
850}\r
851\r
a749b1e5 852\r
275d9e61 853// multi key check\r
a749b1e5 854int MifareChkBlockKeys(uint8_t *keys, uint8_t keyCount, uint8_t blockNo, uint8_t keyType, uint32_t *auth_timeout, uint8_t debugLevel) {\r
855\r
275d9e61
OM
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
275d9e61 864 ui64Key = bytes_to_num(keys + i * 6, 6);\r
a749b1e5 865 int res = MifareChkBlockKey(uid, &cuid, &cascade_levels, ui64Key, blockNo, keyType, auth_timeout, debugLevel);\r
a8561e35 866\r
275d9e61
OM
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
a8561e35 877// Dbprintf("ChkKeys: block=%d key=%d. Try the same key once again...", blockNo, keyType);\r
275d9e61
OM
878 continue;\r
879 }\r
a8561e35 880\r
275d9e61
OM
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
a749b1e5 887 // successful authentication\r
275d9e61
OM
888 return i + 1;\r
889 }\r
a8561e35 890\r
a749b1e5 891 if (BUTTON_PRESS()) {\r
892 return -2;\r
893 }\r
894\r
275d9e61
OM
895 return 0;\r
896}\r
897\r
a749b1e5 898\r
275d9e61 899// multisector multikey check\r
a749b1e5 900int MifareMultisectorChk(uint8_t *keys, uint8_t keyCount, uint8_t SectorCount, uint8_t keyType, uint32_t *auth_timeout, uint8_t debugLevel, TKeyIndex *keyIndex) {\r
275d9e61 901 int res = 0;\r
a8561e35 902\r
903// int clk = GetCountSspClk();\r
275d9e61
OM
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
a749b1e5 910 res = MifareChkBlockKeys(keys, keyCount, FirstBlockOfSector(sc), keyAB & 0x01, auth_timeout, debugLevel);\r
911 if (res < 0) {\r
275d9e61
OM
912 return res;\r
913 }\r
a749b1e5 914 if (res > 0) {\r
275d9e61
OM
915 (*keyIndex)[keyAB & 0x01][sc] = res;\r
916 }\r
917 } while(--keyAB > 0);\r
918 }\r
a8561e35 919\r
920// Dbprintf("%d %d", GetCountSspClk() - clk, (GetCountSspClk() - clk)/(SectorCount*keyCount*(keyType==2?2:1)));\r
921\r
a749b1e5 922 return 1;\r
275d9e61
OM
923}\r
924\r
925\r
Impressum, Datenschutz