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