]> git.zerfleddert.de Git - proxmark3-svn/blame - armsrc/mifarecmd.c
Update CHANGELOG.md
[proxmark3-svn] / armsrc / mifarecmd.c
CommitLineData
8556b852 1//-----------------------------------------------------------------------------\r
b62a5a84 2// Merlok - June 2011, 2012\r
8556b852
M
3// Gerhard de Koning Gans - May 2008\r
4// Hagen Fritsch - June 2010\r
3fe4ff4f 5// Midnitesnake - Dec 2013\r
6// Andy Davies - Apr 2014\r
88e20c9f 7// Iceman - May 2014,2015,2016\r
8556b852
M
8//\r
9// This code is licensed to you under the terms of the GNU GPL, version 2 or,\r
10// at your option, any later version. See the LICENSE.txt file for the text of\r
11// the license.\r
12//-----------------------------------------------------------------------------\r
13// Routines to support ISO 14443 type A.\r
14//-----------------------------------------------------------------------------\r
15\r
16#include "mifarecmd.h"\r
a631936e 17\r
8556b852 18//-----------------------------------------------------------------------------\r
baeaf579 19// Select, Authenticate, Read a MIFARE tag. \r
8556b852
M
20// read block\r
21//-----------------------------------------------------------------------------\r
22void MifareReadBlock(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)\r
23{\r
24 // params\r
25 uint8_t blockNo = arg0;\r
26 uint8_t keyType = arg1;\r
27 uint64_t ui64Key = 0;\r
28 ui64Key = bytes_to_num(datain, 6);\r
29 \r
30 // variables\r
31 byte_t isOK = 0;\r
d798d31c 32 byte_t dataoutbuf[16] = {0x00};\r
33 uint8_t uid[10] = {0x00};\r
34 uint32_t cuid = 0;\r
8556b852
M
35 struct Crypto1State mpcs = {0, 0};\r
36 struct Crypto1State *pcs;\r
37 pcs = &mpcs;\r
38\r
7bc95e2e 39 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
8556b852 40\r
99cf19d9 41 clear_trace();\r
810f5379 42 set_tracing(true);\r
99cf19d9 43\r
8556b852
M
44 LED_A_ON();\r
45 LED_B_OFF();\r
46 LED_C_OFF();\r
47\r
48 while (true) {\r
c188b1b9 49 if(!iso14443a_select_card(uid, NULL, &cuid, true, 0)) {\r
baeaf579 50 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
8556b852
M
51 break;\r
52 };\r
53\r
54 if(mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) {\r
baeaf579 55 if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");\r
8556b852
M
56 break;\r
57 };\r
58 \r
59 if(mifare_classic_readblock(pcs, cuid, blockNo, dataoutbuf)) {\r
baeaf579 60 if (MF_DBGLEVEL >= 1) Dbprintf("Read block error");\r
8556b852
M
61 break;\r
62 };\r
63\r
64 if(mifare_classic_halt(pcs, cuid)) {\r
baeaf579 65 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
8556b852
M
66 break;\r
67 };\r
68 \r
69 isOK = 1;\r
70 break;\r
71 }\r
72 \r
8556b852
M
73 crypto1_destroy(pcs);\r
74 \r
75 if (MF_DBGLEVEL >= 2) DbpString("READ BLOCK FINISHED");\r
76\r
8556b852 77 LED_B_ON();\r
baeaf579 78 cmd_send(CMD_ACK,isOK,0,0,dataoutbuf,16);\r
8556b852
M
79 LED_B_OFF();\r
80\r
a631936e 81 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
82 LEDsoff();\r
83}\r
84\r
4d2e4eea 85void MifareUC_Auth(uint8_t arg0, uint8_t *keybytes){\r
a631936e 86\r
4d2e4eea 87 bool turnOffField = (arg0 == 1);\r
a631936e 88\r
aa60d156 89 LED_A_ON(); LED_B_OFF(); LED_C_OFF();\r
99cf19d9 90\r
a631936e 91 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
92\r
99cf19d9 93 clear_trace();\r
810f5379 94 set_tracing(true);\r
99cf19d9 95\r
c188b1b9 96 if(!iso14443a_select_card(NULL, NULL, NULL, true, 0)) {\r
aa60d156 97 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card");\r
0ec548dc 98 OnError(0);\r
a631936e 99 return;\r
100 };\r
101 \r
e7e95088 102 if(!mifare_ultra_auth(keybytes)){\r
4d2e4eea 103 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Authentication failed");\r
0ec548dc 104 OnError(1);\r
a631936e 105 return;\r
106 }\r
107\r
4d2e4eea 108 if (turnOffField) {\r
109 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
110 LEDsoff();\r
cceabb79 111 }\r
9d87eb66 112 cmd_send(CMD_ACK,1,0,0,0,0);\r
7cc204bf 113}\r
114\r
8d53ea14 115// Arg0 = BlockNo,\r
116// Arg1 = UsePwd bool\r
75377d29 117// datain = PWD bytes,\r
0ec548dc 118void MifareUReadBlock(uint8_t arg0, uint8_t arg1, uint8_t *datain)\r
7cc204bf 119{\r
7cc204bf 120 uint8_t blockNo = arg0;\r
787b5bd8 121 byte_t dataout[16] = {0x00};\r
4d2e4eea 122 bool useKey = (arg1 == 1); //UL_C\r
123 bool usePwd = (arg1 == 2); //UL_EV1/NTAG\r
8d53ea14 124\r
75377d29 125 LEDsoff();\r
126 LED_A_ON();\r
787b5bd8 127 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
f168b263 128\r
99cf19d9 129 clear_trace();\r
810f5379 130 set_tracing(true);\r
99cf19d9 131\r
c188b1b9 132 int len = iso14443a_select_card(NULL, NULL, NULL, true, 0);\r
787b5bd8 133 if(!len) {\r
aa60d156 134 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card (RC:%02X)",len);\r
0ec548dc 135 OnError(1);\r
787b5bd8 136 return;\r
aa60d156 137 }\r
f168b263 138\r
4d2e4eea 139 // UL-C authentication\r
140 if ( useKey ) {\r
8258f409 141 uint8_t key[16] = {0x00};\r
4d2e4eea 142 memcpy(key, datain, sizeof(key) );\r
143\r
e7e95088 144 if ( !mifare_ultra_auth(key) ) {\r
aa60d156 145 OnError(1);\r
f168b263 146 return;\r
0ec548dc 147 }\r
4d2e4eea 148 }\r
f168b263 149\r
4d2e4eea 150 // UL-EV1 / NTAG authentication\r
8258f409 151 if ( usePwd ) {\r
4d2e4eea 152 uint8_t pwd[4] = {0x00};\r
153 memcpy(pwd, datain, 4);\r
154 uint8_t pack[4] = {0,0,0,0};\r
e7e95088 155 if (!mifare_ul_ev1_auth(pwd, pack)) {\r
0ec548dc 156 OnError(1);\r
f168b263 157 return;\r
062b7cb9 158 }\r
8258f409 159 } \r
f168b263 160\r
aa60d156 161 if( mifare_ultra_readblock(blockNo, dataout) ) {\r
162 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Read block error");\r
0ec548dc 163 OnError(2);\r
787b5bd8 164 return;\r
aa60d156 165 }\r
8258f409 166\r
aa60d156 167 if( mifare_ultra_halt() ) {\r
168 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Halt error");\r
0ec548dc 169 OnError(3);\r
787b5bd8 170 return;\r
aa60d156 171 }\r
8258f409 172\r
787b5bd8 173 cmd_send(CMD_ACK,1,0,0,dataout,16);\r
7cc204bf 174 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
175 LEDsoff();\r
176}\r
e7e95088 177\r
7cc204bf 178//-----------------------------------------------------------------------------\r
baeaf579 179// Select, Authenticate, Read a MIFARE tag. \r
180// read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)\r
8556b852
M
181//-----------------------------------------------------------------------------\r
182void MifareReadSector(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)\r
183{\r
184 // params\r
185 uint8_t sectorNo = arg0;\r
186 uint8_t keyType = arg1;\r
187 uint64_t ui64Key = 0;\r
188 ui64Key = bytes_to_num(datain, 6);\r
189 \r
190 // variables\r
3fe4ff4f 191 byte_t isOK = 0;\r
baeaf579 192 byte_t dataoutbuf[16 * 16];\r
d798d31c 193 uint8_t uid[10] = {0x00};\r
194 uint32_t cuid = 0;\r
8556b852
M
195 struct Crypto1State mpcs = {0, 0};\r
196 struct Crypto1State *pcs;\r
197 pcs = &mpcs;\r
198\r
7bc95e2e 199 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
8556b852 200\r
99cf19d9 201 clear_trace();\r
810f5379 202 set_tracing(true);\r
203 \r
8556b852
M
204 LED_A_ON();\r
205 LED_B_OFF();\r
206 LED_C_OFF();\r
207\r
baeaf579 208 isOK = 1;\r
c188b1b9 209 if(!iso14443a_select_card(uid, NULL, &cuid, true, 0)) {\r
baeaf579 210 isOK = 0;\r
8556b852 211 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
baeaf579 212 }\r
baeaf579 213 \r
e7e95088 214 \r
baeaf579 215 if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_FIRST)) {\r
216 isOK = 0;\r
8556b852 217 if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");\r
baeaf579 218 }\r
219 \r
220 for (uint8_t blockNo = 0; isOK && blockNo < NumBlocksPerSector(sectorNo); blockNo++) {\r
221 if(mifare_classic_readblock(pcs, cuid, FirstBlockOfSector(sectorNo) + blockNo, dataoutbuf + 16 * blockNo)) {\r
222 isOK = 0;\r
223 if (MF_DBGLEVEL >= 1) Dbprintf("Read sector %2d block %2d error", sectorNo, blockNo);\r
8556b852 224 break;\r
baeaf579 225 }\r
226 }\r
8556b852 227 \r
baeaf579 228 if(mifare_classic_halt(pcs, cuid)) {\r
8556b852 229 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
8556b852 230 }\r
baeaf579 231\r
8556b852
M
232 if (MF_DBGLEVEL >= 2) DbpString("READ SECTOR FINISHED");\r
233\r
88e20c9f 234 crypto1_destroy(pcs);\r
235\r
8556b852 236 LED_B_ON();\r
baeaf579 237 cmd_send(CMD_ACK,isOK,0,0,dataoutbuf,16*NumBlocksPerSector(sectorNo));\r
6e82300d 238 LED_B_OFF();\r
8556b852 239\r
8556b852
M
240 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
241 LEDsoff();\r
88e20c9f 242 set_tracing(FALSE);\r
7cc204bf 243}\r
244\r
8d53ea14 245// arg0 = blockNo (start)\r
246// arg1 = Pages (number of blocks)\r
247// arg2 = useKey\r
248// datain = KEY bytes\r
249void MifareUReadCard(uint8_t arg0, uint16_t arg1, uint8_t arg2, uint8_t *datain)\r
7cc204bf 250{\r
99cf19d9 251 LEDsoff();\r
252 LED_A_ON();\r
253 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
254\r
0a0d9a58 255 // free eventually allocated BigBuf memory\r
aaa1a9a2 256 BigBuf_free(); BigBuf_Clear_ext(false);\r
0a0d9a58 257 clear_trace();\r
810f5379 258 set_tracing(true);\r
259 \r
0ec548dc 260 // params\r
8d53ea14 261 uint8_t blockNo = arg0;\r
262 uint16_t blocks = arg1;\r
4d2e4eea 263 bool useKey = (arg2 == 1); //UL_C\r
264 bool usePwd = (arg2 == 2); //UL_EV1/NTAG\r
e7e95088 265 uint32_t countblocks = 0;\r
0a0d9a58 266 uint8_t *dataout = BigBuf_malloc(CARD_MEMORY_SIZE);\r
267 if (dataout == NULL){\r
268 Dbprintf("out of memory");\r
269 OnError(1);\r
270 return;\r
271 }\r
7cc204bf 272\r
c188b1b9 273 int len = iso14443a_select_card(NULL, NULL, NULL, true, 0);\r
787b5bd8 274 if (!len) {\r
aa60d156 275 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card (RC:%d)",len);\r
0ec548dc 276 OnError(1);\r
787b5bd8 277 return;\r
278 }\r
75377d29 279\r
8258f409 280 // UL-C authentication\r
75377d29 281 if ( useKey ) {\r
282 uint8_t key[16] = {0x00};\r
8258f409 283 memcpy(key, datain, sizeof(key) );\r
75377d29 284\r
9d87eb66 285 if ( !mifare_ultra_auth(key) ) {\r
75377d29 286 OnError(1);\r
287 return;\r
288 }\r
75377d29 289 }\r
290\r
8258f409 291 // UL-EV1 / NTAG authentication\r
292 if (usePwd) {\r
293 uint8_t pwd[4] = {0x00};\r
294 memcpy(pwd, datain, sizeof(pwd));\r
cceabb79 295 uint8_t pack[4] = {0,0,0,0};\r
296\r
9d87eb66 297 if (!mifare_ul_ev1_auth(pwd, pack)){\r
cceabb79 298 OnError(1);\r
cceabb79 299 return; \r
300 }\r
301 }\r
302\r
75377d29 303 for (int i = 0; i < blocks; i++){\r
95aeb706 304 if ((i*4) + 4 >= CARD_MEMORY_SIZE) {\r
9d87eb66 305 Dbprintf("Data exceeds buffer!!");\r
306 break;\r
307 }\r
8d53ea14 308\r
e7e95088 309 len = mifare_ultra_readblock(blockNo + i, dataout + 4 * i);\r
787b5bd8 310 \r
311 if (len) {\r
aa60d156 312 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Read block %d error",i);\r
e7e95088 313 // if no blocks read - error out\r
314 if (i==0){\r
9d87eb66 315 OnError(2);\r
787b5bd8 316 return;\r
e7e95088 317 } else {\r
318 //stop at last successful read block and return what we got\r
319 break;\r
320 }\r
787b5bd8 321 } else {\r
4d2e4eea 322 countblocks++;\r
787b5bd8 323 }\r
324 }\r
75377d29 325\r
4d2e4eea 326 len = mifare_ultra_halt();\r
327 if (len) {\r
aa60d156 328 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Halt error");\r
0ec548dc 329 OnError(3);\r
787b5bd8 330 return;\r
331 }\r
7cc204bf 332\r
4d2e4eea 333 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) Dbprintf("Blocks read %d", countblocks);\r
7cc204bf 334\r
e7e95088 335 countblocks *= 4;\r
95aeb706 336\r
0ce03d9a 337 cmd_send(CMD_ACK, 1, countblocks, BigBuf_max_traceLen(), 0, 0);\r
31d1caa5
MHS
338 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
339 LEDsoff();\r
46cd801c 340 BigBuf_free();\r
88e20c9f 341 set_tracing(FALSE);\r
7cc204bf 342}\r
343\r
7cc204bf 344//-----------------------------------------------------------------------------\r
baeaf579 345// Select, Authenticate, Write a MIFARE tag. \r
7cc204bf 346// read block\r
8556b852
M
347//-----------------------------------------------------------------------------\r
348void MifareWriteBlock(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)\r
349{\r
350 // params\r
351 uint8_t blockNo = arg0;\r
352 uint8_t keyType = arg1;\r
353 uint64_t ui64Key = 0;\r
d798d31c 354 byte_t blockdata[16] = {0x00};\r
8556b852
M
355\r
356 ui64Key = bytes_to_num(datain, 6);\r
357 memcpy(blockdata, datain + 10, 16);\r
358 \r
359 // variables\r
360 byte_t isOK = 0;\r
d798d31c 361 uint8_t uid[10] = {0x00};\r
362 uint32_t cuid = 0;\r
8556b852
M
363 struct Crypto1State mpcs = {0, 0};\r
364 struct Crypto1State *pcs;\r
365 pcs = &mpcs;\r
366\r
7bc95e2e 367 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
8556b852 368\r
99cf19d9 369 clear_trace();\r
810f5379 370 set_tracing(true);\r
371 \r
8556b852
M
372 LED_A_ON();\r
373 LED_B_OFF();\r
374 LED_C_OFF();\r
375\r
376 while (true) {\r
f8850434 377 if(!iso14443a_select_card(uid, NULL, &cuid, true, 0)) {\r
8556b852
M
378 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
379 break;\r
380 };\r
381\r
382 if(mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) {\r
383 if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");\r
384 break;\r
385 };\r
386 \r
387 if(mifare_classic_writeblock(pcs, cuid, blockNo, blockdata)) {\r
388 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
389 break;\r
390 };\r
391\r
392 if(mifare_classic_halt(pcs, cuid)) {\r
393 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
394 break;\r
395 };\r
396 \r
397 isOK = 1;\r
398 break;\r
399 }\r
400 \r
8556b852
M
401 crypto1_destroy(pcs);\r
402 \r
403 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");\r
404\r
9492e0b0 405 cmd_send(CMD_ACK,isOK,0,0,0,0);\r
8556b852 406\r
8556b852
M
407 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
408 LEDsoff();\r
88e20c9f 409 set_tracing(FALSE);\r
7cc204bf 410}\r
411\r
95aeb706 412/* // Command not needed but left for future testing \r
413void MifareUWriteBlockCompat(uint8_t arg0, uint8_t *datain)\r
7cc204bf 414{\r
f168b263 415 uint8_t blockNo = arg0;\r
787b5bd8 416 byte_t blockdata[16] = {0x00};\r
7cc204bf 417\r
f168b263 418 memcpy(blockdata, datain, 16);\r
419\r
787b5bd8 420 uint8_t uid[10] = {0x00};\r
7cc204bf 421\r
aa60d156 422 LED_A_ON(); LED_B_OFF(); LED_C_OFF();\r
7cc204bf 423\r
aa60d156 424 clear_trace();\r
810f5379 425 set_tracing(true);\r
aa60d156 426 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
7cc204bf 427\r
c188b1b9 428 if(!iso14443a_select_card(uid, NULL, NULL, true, 0)) {\r
aa60d156 429 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
430 OnError(0);\r
431 return;\r
432 };\r
433\r
95aeb706 434 if(mifare_ultra_writeblock_compat(blockNo, blockdata)) {\r
aa60d156 435 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
436 OnError(0);\r
437 return; };\r
438\r
439 if(mifare_ultra_halt()) {\r
440 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
441 OnError(0);\r
442 return;\r
443 };\r
f168b263 444\r
aa60d156 445 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");\r
7cc204bf 446\r
aa60d156 447 cmd_send(CMD_ACK,1,0,0,0,0);\r
448 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
449 LEDsoff();\r
7cc204bf 450}\r
95aeb706 451*/\r
7cc204bf 452\r
fff69a1e 453// Arg0 : Block to write to.\r
454// Arg1 : 0 = use no authentication.\r
455// 1 = use 0x1A authentication.\r
456// 2 = use 0x1B authentication.\r
457// datain : 4 first bytes is data to be written.\r
458// : 4/16 next bytes is authentication key.\r
95aeb706 459void MifareUWriteBlock(uint8_t arg0, uint8_t arg1, uint8_t *datain)\r
7cc204bf 460{\r
baeaf579 461 uint8_t blockNo = arg0;\r
fff69a1e 462 bool useKey = (arg1 == 1); //UL_C\r
463 bool usePwd = (arg1 == 2); //UL_EV1/NTAG\r
787b5bd8 464 byte_t blockdata[4] = {0x00};\r
7cc204bf 465\r
8258f409 466 memcpy(blockdata, datain,4);\r
aa60d156 467 \r
8d53ea14 468 LEDsoff();\r
469 LED_A_ON();\r
aa60d156 470 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
471\r
99cf19d9 472 clear_trace();\r
810f5379 473 set_tracing(true);\r
474 \r
c188b1b9 475 if(!iso14443a_select_card(NULL, NULL, NULL, true, 0)) {\r
aa60d156 476 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
477 OnError(0);\r
478 return;\r
479 };\r
7cc204bf 480\r
fff69a1e 481 // UL-C authentication\r
482 if ( useKey ) {\r
483 uint8_t key[16] = {0x00}; \r
484 memcpy(key, datain+4, sizeof(key) );\r
485\r
486 if ( !mifare_ultra_auth(key) ) {\r
487 OnError(1);\r
488 return; \r
489 }\r
490 }\r
491 \r
492 // UL-EV1 / NTAG authentication\r
493 if (usePwd) { \r
494 uint8_t pwd[4] = {0x00};\r
495 memcpy(pwd, datain+4, 4);\r
496 uint8_t pack[4] = {0,0,0,0};\r
497 if (!mifare_ul_ev1_auth(pwd, pack)) {\r
498 OnError(1);\r
499 return; \r
500 }\r
501 }\r
502 \r
95aeb706 503 if(mifare_ultra_writeblock(blockNo, blockdata)) {\r
aa60d156 504 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
505 OnError(0);\r
506 return;\r
507 };\r
508\r
509 if(mifare_ultra_halt()) {\r
510 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
511 OnError(0);\r
512 return;\r
513 };\r
514\r
515 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");\r
516\r
517 cmd_send(CMD_ACK,1,0,0,0,0);\r
518 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
519 LEDsoff();\r
88e20c9f 520 set_tracing(FALSE);\r
aa60d156 521}\r
522\r
523void MifareUSetPwd(uint8_t arg0, uint8_t *datain){\r
524 \r
525 uint8_t pwd[16] = {0x00};\r
526 byte_t blockdata[4] = {0x00};\r
527 \r
528 memcpy(pwd, datain, 16);\r
529 \r
530 LED_A_ON(); LED_B_OFF(); LED_C_OFF();\r
baeaf579 531 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
7cc204bf 532\r
99cf19d9 533 clear_trace();\r
810f5379 534 set_tracing(true);\r
535 \r
c188b1b9 536 if(!iso14443a_select_card(NULL, NULL, NULL, true, 0)) {\r
aa60d156 537 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
538 OnError(0);\r
539 return;\r
540 };\r
7cc204bf 541\r
aa60d156 542 blockdata[0] = pwd[7];\r
543 blockdata[1] = pwd[6];\r
544 blockdata[2] = pwd[5];\r
545 blockdata[3] = pwd[4];\r
95aeb706 546 if(mifare_ultra_writeblock( 44, blockdata)) {\r
aa60d156 547 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
548 OnError(44);\r
549 return;\r
550 };\r
7cc204bf 551\r
aa60d156 552 blockdata[0] = pwd[3];\r
553 blockdata[1] = pwd[2];\r
554 blockdata[2] = pwd[1];\r
555 blockdata[3] = pwd[0];\r
95aeb706 556 if(mifare_ultra_writeblock( 45, blockdata)) {\r
aa60d156 557 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
558 OnError(45);\r
559 return;\r
560 };\r
7cc204bf 561\r
aa60d156 562 blockdata[0] = pwd[15];\r
563 blockdata[1] = pwd[14];\r
564 blockdata[2] = pwd[13];\r
565 blockdata[3] = pwd[12];\r
95aeb706 566 if(mifare_ultra_writeblock( 46, blockdata)) {\r
aa60d156 567 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
568 OnError(46);\r
569 return;\r
570 };\r
7cc204bf 571\r
aa60d156 572 blockdata[0] = pwd[11];\r
573 blockdata[1] = pwd[10];\r
574 blockdata[2] = pwd[9];\r
575 blockdata[3] = pwd[8];\r
95aeb706 576 if(mifare_ultra_writeblock( 47, blockdata)) {\r
aa60d156 577 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
578 OnError(47);\r
579 return;\r
580 }; \r
7cc204bf 581\r
aa60d156 582 if(mifare_ultra_halt()) {\r
583 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
584 OnError(0);\r
585 return;\r
586 };\r
7cc204bf 587\r
aa60d156 588 cmd_send(CMD_ACK,1,0,0,0,0);\r
baeaf579 589 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
590 LEDsoff();\r
88e20c9f 591 set_tracing(FALSE);\r
7cc204bf 592}\r
593\r
594// Return 1 if the nonce is invalid else return 0\r
6a1f2d82 595int valid_nonce(uint32_t Nt, uint32_t NtEnc, uint32_t Ks1, uint8_t *parity) {\r
f8ada309 596 return ((oddparity8((Nt >> 24) & 0xFF) == ((parity[0]) ^ oddparity8((NtEnc >> 24) & 0xFF) ^ BIT(Ks1,16))) & \\r
597 (oddparity8((Nt >> 16) & 0xFF) == ((parity[1]) ^ oddparity8((NtEnc >> 16) & 0xFF) ^ BIT(Ks1,8))) & \\r
598 (oddparity8((Nt >> 8) & 0xFF) == ((parity[2]) ^ oddparity8((NtEnc >> 8) & 0xFF) ^ BIT(Ks1,0)))) ? 1 : 0;\r
8556b852
M
599}\r
600\r
9492e0b0 601\r
c188b1b9 602//-----------------------------------------------------------------------------\r
603// acquire encrypted nonces in order to perform the attack described in\r
604// Carlo Meijer, Roel Verdult, "Ciphertext-only Cryptanalysis on Hardened\r
605// Mifare Classic Cards" in Proceedings of the 22nd ACM SIGSAC Conference on \r
606// Computer and Communications Security, 2015\r
607//-----------------------------------------------------------------------------\r
8d61d9b1 608#define AUTHENTICATION_TIMEOUT 848 //848 // card times out 1ms after wrong authentication (according to NXP documentation)\r
86d597a5 609#define PRE_AUTHENTICATION_LEADTIME 400 // some (non standard) cards need a pause after select before they are ready for first authentication \r
610\r
c188b1b9 611void MifareAcquireEncryptedNonces(uint32_t arg0, uint32_t arg1, uint32_t flags, uint8_t *datain)\r
612{\r
613 uint64_t ui64Key = 0;\r
d798d31c 614 uint8_t uid[10] = {0x00};\r
4a71da5a 615 uint32_t cuid = 0;\r
c188b1b9 616 uint8_t cascade_levels = 0;\r
617 struct Crypto1State mpcs = {0, 0};\r
618 struct Crypto1State *pcs;\r
619 pcs = &mpcs;\r
d798d31c 620 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE] = {0x00};\r
c188b1b9 621 int16_t isOK = 0;\r
d798d31c 622 uint8_t par_enc[1] = {0x00};\r
c188b1b9 623 uint8_t nt_par_enc = 0;\r
d798d31c 624 uint8_t buf[USB_CMD_DATA_SIZE] = {0x00};\r
625 uint32_t timeout = 0;\r
c188b1b9 626 \r
627 uint8_t blockNo = arg0 & 0xff;\r
628 uint8_t keyType = (arg0 >> 8) & 0xff;\r
629 uint8_t targetBlockNo = arg1 & 0xff;\r
630 uint8_t targetKeyType = (arg1 >> 8) & 0xff;\r
631 ui64Key = bytes_to_num(datain, 6);\r
632 bool initialize = flags & 0x0001;\r
633 bool slow = flags & 0x0002;\r
634 bool field_off = flags & 0x0004;\r
635 \r
c188b1b9 636 LED_A_ON();\r
637 LED_C_OFF();\r
638\r
77f3f9ff 639 BigBuf_free(); BigBuf_Clear_ext(false); \r
640 clear_trace();\r
641 set_tracing(FALSE);\r
642 \r
c188b1b9 643 if (initialize) {\r
644 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
c188b1b9 645 }\r
77f3f9ff 646 \r
c188b1b9 647 LED_C_ON();\r
648 \r
77f3f9ff 649 uint8_t dummy_answer = 0; \r
c188b1b9 650 uint16_t num_nonces = 0;\r
651 bool have_uid = false;\r
652 for (uint16_t i = 0; i <= USB_CMD_DATA_SIZE - 9; ) {\r
653\r
654 // Test if the action was cancelled\r
655 if(BUTTON_PRESS()) {\r
656 isOK = 2;\r
657 field_off = true;\r
658 break;\r
659 }\r
660\r
661 if (!have_uid) { // need a full select cycle to get the uid first\r
662 iso14a_card_select_t card_info; \r
663 if(!iso14443a_select_card(uid, &card_info, &cuid, true, 0)) {\r
664 if (MF_DBGLEVEL >= 1) Dbprintf("AcquireNonces: Can't select card (ALL)");\r
665 continue;\r
666 }\r
667 switch (card_info.uidlen) {\r
668 case 4 : cascade_levels = 1; break;\r
669 case 7 : cascade_levels = 2; break;\r
670 case 10: cascade_levels = 3; break;\r
671 default: break;\r
672 }\r
673 have_uid = true; \r
674 } else { // no need for anticollision. We can directly select the card\r
675 if(!iso14443a_select_card(uid, NULL, NULL, false, cascade_levels)) {\r
676 if (MF_DBGLEVEL >= 1) Dbprintf("AcquireNonces: Can't select card (UID)");\r
677 continue;\r
678 }\r
679 }\r
680 \r
681 if (slow) {\r
682 timeout = GetCountSspClk() + PRE_AUTHENTICATION_LEADTIME;\r
683 while(GetCountSspClk() < timeout);\r
684 }\r
685\r
686 uint32_t nt1;\r
687 if (mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, NULL)) {\r
688 if (MF_DBGLEVEL >= 1) Dbprintf("AcquireNonces: Auth1 error");\r
689 continue;\r
690 }\r
691\r
692 // nested authentication\r
693 uint16_t len = mifare_sendcmd_short(pcs, AUTH_NESTED, 0x60 + (targetKeyType & 0x01), targetBlockNo, receivedAnswer, par_enc, NULL);\r
694 if (len != 4) {\r
695 if (MF_DBGLEVEL >= 1) Dbprintf("AcquireNonces: Auth2 error len=%d", len);\r
696 continue;\r
697 }\r
698 \r
699 // send a dummy byte as reader response in order to trigger the cards authentication timeout\r
c188b1b9 700 ReaderTransmit(&dummy_answer, 1, NULL);\r
701 timeout = GetCountSspClk() + AUTHENTICATION_TIMEOUT;\r
702 \r
703 num_nonces++;\r
704 if (num_nonces % 2) {\r
705 memcpy(buf+i, receivedAnswer, 4);\r
706 nt_par_enc = par_enc[0] & 0xf0;\r
707 } else {\r
0b53530a 708 nt_par_enc |= par_enc[0] >> 4;\r
c188b1b9 709 memcpy(buf+i+4, receivedAnswer, 4);\r
710 memcpy(buf+i+8, &nt_par_enc, 1);\r
711 i += 9;\r
712 }\r
c188b1b9 713 // wait for the card to become ready again\r
68410a48 714 while(GetCountSspClk() < timeout); \r
c188b1b9 715 }\r
716\r
717 LED_C_OFF();\r
0b53530a 718 crypto1_destroy(pcs); \r
c188b1b9 719 LED_B_ON();\r
720 cmd_send(CMD_ACK, isOK, cuid, num_nonces, buf, sizeof(buf));\r
721 LED_B_OFF();\r
722\r
723 if (MF_DBGLEVEL >= 3) DbpString("AcquireEncryptedNonces finished");\r
724\r
725 if (field_off) {\r
726 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
727 LEDsoff();\r
77f3f9ff 728 set_tracing(FALSE);\r
c188b1b9 729 }\r
730}\r
731\r
732\r
8556b852
M
733//-----------------------------------------------------------------------------\r
734// MIFARE nested authentication. \r
735// \r
736//-----------------------------------------------------------------------------\r
9492e0b0 737void MifareNested(uint32_t arg0, uint32_t arg1, uint32_t calibrate, uint8_t *datain)\r
8556b852
M
738{\r
739 // params\r
9492e0b0 740 uint8_t blockNo = arg0 & 0xff;\r
741 uint8_t keyType = (arg0 >> 8) & 0xff;\r
742 uint8_t targetBlockNo = arg1 & 0xff;\r
743 uint8_t targetKeyType = (arg1 >> 8) & 0xff;\r
8556b852
M
744 uint64_t ui64Key = 0;\r
745\r
746 ui64Key = bytes_to_num(datain, 6);\r
747 \r
748 // variables\r
9492e0b0 749 uint16_t rtr, i, j, len;\r
d798d31c 750 uint16_t davg = 0;\r
9492e0b0 751 static uint16_t dmin, dmax;\r
d798d31c 752 uint8_t uid[10] = {0x00};\r
5ebcb867 753 uint32_t cuid = 0, nt1, nt2, nttmp, nttest, ks1;\r
d798d31c 754 uint8_t par[1] = {0x00};\r
755 uint32_t target_nt[2] = {0x00}, target_ks[2] = {0x00};\r
9492e0b0 756 \r
d798d31c 757 uint8_t par_array[4] = {0x00};\r
9492e0b0 758 uint16_t ncount = 0;\r
8556b852
M
759 struct Crypto1State mpcs = {0, 0};\r
760 struct Crypto1State *pcs;\r
761 pcs = &mpcs;\r
5ebcb867 762 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE] = {0x00};\r
8556b852 763\r
9492e0b0 764 uint32_t auth1_time, auth2_time;\r
7504dc50 765 static uint16_t delta_time = 0;\r
9492e0b0 766\r
99cf19d9 767 LED_A_ON();\r
768 LED_C_OFF();\r
769 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
770\r
f71f4deb 771 // free eventually allocated BigBuf memory\r
aaa1a9a2 772 BigBuf_free(); BigBuf_Clear_ext(false);\r
773 \r
b10a759f 774 if (calibrate) clear_trace();\r
775 set_tracing(true);\r
8556b852 776\r
9492e0b0 777 // statistics on nonce distance\r
c830303d 778 int16_t isOK = 0;\r
779 #define NESTED_MAX_TRIES 12\r
780 uint16_t unsuccessfull_tries = 0;\r
9492e0b0 781 if (calibrate) { // for first call only. Otherwise reuse previous calibration\r
782 LED_B_ON();\r
3fe4ff4f 783 WDT_HIT();\r
8556b852 784\r
9492e0b0 785 davg = dmax = 0;\r
786 dmin = 2000;\r
787 delta_time = 0;\r
8556b852 788 \r
9492e0b0 789 for (rtr = 0; rtr < 17; rtr++) {\r
8556b852 790\r
c830303d 791 // Test if the action was cancelled\r
792 if(BUTTON_PRESS()) {\r
793 isOK = -2;\r
794 break;\r
795 }\r
796\r
9492e0b0 797 // prepare next select. No need to power down the card.\r
798 if(mifare_classic_halt(pcs, cuid)) {\r
799 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Halt error");\r
800 rtr--;\r
801 continue;\r
802 }\r
803\r
c188b1b9 804 if(!iso14443a_select_card(uid, NULL, &cuid, true, 0)) {\r
9492e0b0 805 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Can't select card");\r
806 rtr--;\r
807 continue;\r
808 };\r
809\r
810 auth1_time = 0;\r
811 if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, &auth1_time)) {\r
812 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth1 error");\r
813 rtr--;\r
814 continue;\r
815 };\r
7504dc50 816 auth2_time = (delta_time) ? auth1_time + delta_time : 0;\r
9492e0b0 817\r
9492e0b0 818 if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_NESTED, &nt2, &auth2_time)) {\r
819 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth2 error");\r
820 rtr--;\r
821 continue;\r
822 };\r
823\r
b19bd5d6 824 nttmp = prng_successor(nt1, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160\r
825 for (i = 101; i < 1200; i++) {\r
2abdfa49 826 nttmp = prng_successor(nttmp, 1);\r
9492e0b0 827 if (nttmp == nt2) break;\r
828 }\r
829\r
830 if (i != 1200) {\r
831 if (rtr != 0) {\r
832 davg += i;\r
833 dmin = MIN(dmin, i);\r
834 dmax = MAX(dmax, i);\r
835 }\r
836 else {\r
837 delta_time = auth2_time - auth1_time + 32; // allow some slack for proper timing\r
838 }\r
839 if (MF_DBGLEVEL >= 3) Dbprintf("Nested: calibrating... ntdist=%d", i);\r
c830303d 840 } else {\r
841 unsuccessfull_tries++;\r
842 if (unsuccessfull_tries > NESTED_MAX_TRIES) { // card isn't vulnerable to nested attack (random numbers are not predictable)\r
843 isOK = -3;\r
844 }\r
9492e0b0 845 }\r
8556b852 846 }\r
8556b852 847\r
9492e0b0 848 davg = (davg + (rtr - 1)/2) / (rtr - 1);\r
849 \r
c830303d 850 if (MF_DBGLEVEL >= 3) Dbprintf("rtr=%d isOK=%d min=%d max=%d avg=%d, delta_time=%d", rtr, isOK, dmin, dmax, davg, delta_time);\r
8556b852 851\r
9492e0b0 852 dmin = davg - 2;\r
853 dmax = davg + 2;\r
854 \r
855 LED_B_OFF();\r
9492e0b0 856 }\r
8556b852
M
857// ------------------------------------------------------------------------------------------------- \r
858 \r
859 LED_C_ON();\r
860\r
861 // get crypted nonces for target sector\r
c830303d 862 for(i=0; i < 2 && !isOK; i++) { // look for exactly two different nonces\r
8556b852 863\r
9492e0b0 864 target_nt[i] = 0;\r
865 while(target_nt[i] == 0) { // continue until we have an unambiguous nonce\r
8556b852 866 \r
9492e0b0 867 // prepare next select. No need to power down the card.\r
868 if(mifare_classic_halt(pcs, cuid)) {\r
869 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Halt error");\r
870 continue;\r
871 }\r
8556b852 872\r
c188b1b9 873 if(!iso14443a_select_card(uid, NULL, &cuid, true, 0)) {\r
9492e0b0 874 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Can't select card");\r
875 continue;\r
876 };\r
8556b852 877 \r
9492e0b0 878 auth1_time = 0;\r
879 if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, &auth1_time)) {\r
880 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth1 error");\r
881 continue;\r
882 };\r
8556b852 883\r
9492e0b0 884 // nested authentication\r
885 auth2_time = auth1_time + delta_time;\r
7504dc50 886\r
2d2f7d19 887 len = mifare_sendcmd_short(pcs, AUTH_NESTED, 0x60 + (targetKeyType & 0x01), targetBlockNo, receivedAnswer, par, &auth2_time);\r
9492e0b0 888 if (len != 4) {\r
889 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth2 error len=%d", len);\r
890 continue;\r
891 };\r
8556b852 892 \r
9492e0b0 893 nt2 = bytes_to_num(receivedAnswer, 4); \r
6a1f2d82 894 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i+1, nt1, nt2, par[0]);\r
8556b852 895 \r
9492e0b0 896 // Parity validity check\r
7504dc50 897// for (j = 0; j < 4; j++) {\r
898// par_array[j] = (oddparity8(receivedAnswer[j]) != ((par[0] >> (7-j)) & 0x01));\r
899// }\r
900 par_array[0] = (oddparity8(receivedAnswer[0]) != ((par[0] >> (7-0)) & 0x01));\r
901 par_array[1] = (oddparity8(receivedAnswer[1]) != ((par[0] >> (7-1)) & 0x01));\r
902 par_array[2] = (oddparity8(receivedAnswer[2]) != ((par[0] >> (7-2)) & 0x01));\r
903 par_array[3] = (oddparity8(receivedAnswer[3]) != ((par[0] >> (7-3)) & 0x01));\r
9492e0b0 904 \r
905 ncount = 0;\r
906 nttest = prng_successor(nt1, dmin - 1);\r
907 for (j = dmin; j < dmax + 1; j++) {\r
2abdfa49 908 nttest = prng_successor(nttest, 1);\r
9492e0b0 909 ks1 = nt2 ^ nttest;\r
910\r
911 if (valid_nonce(nttest, nt2, ks1, par_array)){\r
912 if (ncount > 0) { // we are only interested in disambiguous nonces, try again\r
913 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i+1, j);\r
914 target_nt[i] = 0;\r
915 break;\r
916 }\r
917 target_nt[i] = nttest;\r
918 target_ks[i] = ks1;\r
919 ncount++;\r
920 if (i == 1 && target_nt[1] == target_nt[0]) { // we need two different nonces\r
921 target_nt[i] = 0;\r
922 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j);\r
8556b852
M
923 break;\r
924 }\r
9492e0b0 925 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i+1, j);\r
8556b852 926 }\r
8556b852 927 }\r
9492e0b0 928 if (target_nt[i] == 0 && j == dmax+1 && MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i+1);\r
8556b852
M
929 }\r
930 }\r
931\r
932 LED_C_OFF();\r
933 \r
8556b852
M
934 crypto1_destroy(pcs);\r
935 \r
7504dc50 936 byte_t buf[4 + 4 * 4] = {0};\r
82885445 937 memcpy(buf, &cuid, 4);\r
9492e0b0 938 memcpy(buf+4, &target_nt[0], 4);\r
939 memcpy(buf+8, &target_ks[0], 4);\r
940 memcpy(buf+12, &target_nt[1], 4);\r
941 memcpy(buf+16, &target_ks[1], 4);\r
8556b852
M
942 \r
943 LED_B_ON();\r
c830303d 944 cmd_send(CMD_ACK, isOK, 0, targetBlockNo + (targetKeyType * 0x100), buf, sizeof(buf));\r
6e82300d 945 LED_B_OFF();\r
8556b852 946\r
9492e0b0 947 if (MF_DBGLEVEL >= 3) DbpString("NESTED FINISHED");\r
8556b852 948\r
8556b852
M
949 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
950 LEDsoff();\r
5ee53a0e 951 set_tracing(FALSE);\r
8556b852
M
952}\r
953\r
954//-----------------------------------------------------------------------------\r
9492e0b0 955// MIFARE check keys. key count up to 85. \r
8556b852
M
956// \r
957//-----------------------------------------------------------------------------\r
86d597a5 958void MifareChkKeys(uint16_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain) {\r
57778a46 959#define STD_SEARCH 1\r
960#define EXT_SEARCH 2\r
961\r
962 uint8_t blockNo = arg0 & 0xFF;\r
963 uint8_t keyType = (arg0 >> 8) & 0xFF;\r
57e1e31d 964 //uint8_t searchType = (arg1 >> 8 ) & 0xFF;\r
57778a46 965 bool clearTrace = arg1 & 0xFF;\r
8556b852
M
966 uint8_t keyCount = arg2;\r
967 uint64_t ui64Key = 0;\r
968 \r
86d597a5 969 bool have_uid = FALSE;\r
970 uint8_t cascade_levels = 0;\r
971 uint32_t timeout = 0;\r
972 \r
8556b852
M
973 int i;\r
974 byte_t isOK = 0;\r
d798d31c 975 uint8_t uid[10] = {0x00};\r
976 uint32_t cuid = 0;\r
8556b852
M
977 struct Crypto1State mpcs = {0, 0};\r
978 struct Crypto1State *pcs;\r
979 pcs = &mpcs;\r
980 \r
2abdfa49 981 // save old debuglevel, and tempory turn off dbg printing. speedissues.\r
8556b852
M
982 int OLD_MF_DBGLEVEL = MF_DBGLEVEL; \r
983 MF_DBGLEVEL = MF_DBG_NONE;\r
984 \r
7504dc50 985 LEDsoff();\r
8556b852 986 LED_A_ON();\r
7504dc50 987 \r
99cf19d9 988 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
989\r
810f5379 990 if (clearTrace) \r
991 clear_trace();\r
992 \r
99cf19d9 993 set_tracing(TRUE);\r
2abdfa49 994 \r
65c2d21d 995 for (i = 0; i < keyCount; ++i) {\r
8556b852 996\r
86d597a5 997 //mifare_classic_halt(pcs, cuid);\r
8556b852 998\r
86d597a5 999 // this part is from Piwi's faster nonce collecting part in Hardnested.\r
1000 if (!have_uid) { // need a full select cycle to get the uid first\r
1001 iso14a_card_select_t card_info; \r
1002 if(!iso14443a_select_card(uid, &card_info, &cuid, true, 0)) {\r
1003 if (MF_DBGLEVEL >= 1) Dbprintf("ChkKeys: Can't select card (ALL)");\r
1004 break;\r
1005 }\r
1006 switch (card_info.uidlen) {\r
1007 case 4 : cascade_levels = 1; break;\r
1008 case 7 : cascade_levels = 2; break;\r
1009 case 10: cascade_levels = 3; break;\r
1010 default: break;\r
1011 }\r
1012 have_uid = TRUE; \r
1013 } else { // no need for anticollision. We can directly select the card\r
1014 if(!iso14443a_select_card(uid, NULL, NULL, false, cascade_levels)) {\r
1015 if (MF_DBGLEVEL >= 1) Dbprintf("ChkKeys: Can't select card (UID)");\r
1016 continue;\r
1017 }\r
1018 }\r
1019 \r
8556b852 1020 ui64Key = bytes_to_num(datain + i * 6, 6);\r
2abdfa49 1021 \r
86d597a5 1022 if (mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) {\r
1023\r
1024 uint8_t dummy_answer = 0;\r
1025 ReaderTransmit(&dummy_answer, 1, NULL);\r
1026 timeout = GetCountSspClk() + AUTHENTICATION_TIMEOUT;\r
1027 \r
1028 // wait for the card to become ready again\r
1029 while(GetCountSspClk() < timeout);\r
1030 \r
8556b852 1031 continue;\r
86d597a5 1032 }\r
57778a46 1033 \r
1034 // found a key.\r
1035 //\r
1036 //if ( searchType == EXT_SEARCH) {\r
1037 \r
1038 //}\r
1039 //else {\r
1040 isOK = 1;\r
1041 break;\r
1042 //}\r
8556b852 1043 }\r
8556b852 1044 \r
8556b852 1045 LED_B_ON();\r
2abdfa49 1046 cmd_send(CMD_ACK, isOK, 0, 0, datain + i * 6, 6);\r
838c15a6 1047\r
2abdfa49 1048 // restore debug level\r
1049 MF_DBGLEVEL = OLD_MF_DBGLEVEL; \r
1050 \r
8556b852
M
1051 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1052 LEDsoff();\r
5ee53a0e 1053 set_tracing(FALSE);\r
838c15a6 1054 crypto1_destroy(pcs);\r
8556b852
M
1055}\r
1056\r
1057//-----------------------------------------------------------------------------\r
1058// MIFARE commands set debug level\r
1059// \r
1060//-----------------------------------------------------------------------------\r
1061void MifareSetDbgLvl(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
1062 MF_DBGLEVEL = arg0;\r
1063 Dbprintf("Debug level: %d", MF_DBGLEVEL);\r
1064}\r
1065\r
1066//-----------------------------------------------------------------------------\r
1067// Work with emulator memory\r
1068// \r
99cf19d9 1069// Note: we call FpgaDownloadAndGo(FPGA_BITSTREAM_HF) here although FPGA is not\r
1070// involved in dealing with emulator memory. But if it is called later, it might\r
1071// destroy the Emulator Memory.\r
8556b852 1072//-----------------------------------------------------------------------------\r
99cf19d9 1073\r
8556b852 1074void MifareEMemClr(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
99cf19d9 1075 FpgaDownloadAndGo(FPGA_BITSTREAM_HF);\r
8556b852
M
1076 emlClearMem();\r
1077}\r
1078\r
1079void MifareEMemSet(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
99cf19d9 1080 FpgaDownloadAndGo(FPGA_BITSTREAM_HF);\r
9332b857 1081 if (arg2==0) arg2 = 16; // backwards compat... default bytewidth\r
0de8e387 1082 emlSetMem_xt(datain, arg0, arg1, arg2); // data, block num, blocks count, block byte width\r
8556b852
M
1083}\r
1084\r
1085void MifareEMemGet(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
99cf19d9 1086 FpgaDownloadAndGo(FPGA_BITSTREAM_HF);\r
d798d31c 1087 byte_t buf[USB_CMD_DATA_SIZE] = {0x00};\r
baeaf579 1088 emlGetMem(buf, arg0, arg1); // data, block num, blocks count (max 4)\r
8556b852
M
1089\r
1090 LED_B_ON();\r
3fe4ff4f 1091 cmd_send(CMD_ACK,arg0,arg1,0,buf,USB_CMD_DATA_SIZE);\r
8556b852
M
1092 LED_B_OFF();\r
1093}\r
1094\r
1095//-----------------------------------------------------------------------------\r
1096// Load a card into the emulator memory\r
1097// \r
1098//-----------------------------------------------------------------------------\r
1099void MifareECardLoad(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
baeaf579 1100 uint8_t numSectors = arg0;\r
8556b852
M
1101 uint8_t keyType = arg1;\r
1102 uint64_t ui64Key = 0;\r
d798d31c 1103 uint32_t cuid = 0;\r
8556b852
M
1104 struct Crypto1State mpcs = {0, 0};\r
1105 struct Crypto1State *pcs;\r
1106 pcs = &mpcs;\r
1107\r
1108 // variables\r
d798d31c 1109 byte_t dataoutbuf[16] = {0x00};\r
1110 byte_t dataoutbuf2[16] = {0x00};\r
1111 uint8_t uid[10] = {0x00};\r
8556b852 1112\r
8556b852
M
1113 LED_A_ON();\r
1114 LED_B_OFF();\r
1115 LED_C_OFF();\r
99cf19d9 1116 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
1117 \r
1118 clear_trace();\r
5ee53a0e 1119 set_tracing(TRUE);\r
8556b852 1120 \r
baeaf579 1121 bool isOK = true;\r
1122\r
c188b1b9 1123 if(!iso14443a_select_card(uid, NULL, &cuid, true, 0)) {\r
baeaf579 1124 isOK = false;\r
1125 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
1126 }\r
8556b852 1127 \r
baeaf579 1128 for (uint8_t sectorNo = 0; isOK && sectorNo < numSectors; sectorNo++) {\r
1129 ui64Key = emlGetKey(sectorNo, keyType);\r
1130 if (sectorNo == 0){\r
1131 if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_FIRST)) {\r
1132 isOK = false;\r
1133 if (MF_DBGLEVEL >= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo);\r
8556b852 1134 break;\r
baeaf579 1135 }\r
1136 } else {\r
1137 if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_NESTED)) {\r
1138 isOK = false;\r
1139 if (MF_DBGLEVEL >= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo);\r
8556b852 1140 break;\r
baeaf579 1141 }\r
1142 }\r
1143 \r
1144 for (uint8_t blockNo = 0; isOK && blockNo < NumBlocksPerSector(sectorNo); blockNo++) {\r
1145 if(isOK && mifare_classic_readblock(pcs, cuid, FirstBlockOfSector(sectorNo) + blockNo, dataoutbuf)) {\r
1146 isOK = false;\r
1147 if (MF_DBGLEVEL >= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo, blockNo);\r
ab8b654e 1148 break;\r
d798d31c 1149 }\r
baeaf579 1150 if (isOK) {\r
1151 if (blockNo < NumBlocksPerSector(sectorNo) - 1) {\r
1152 emlSetMem(dataoutbuf, FirstBlockOfSector(sectorNo) + blockNo, 1);\r
1153 } else { // sector trailer, keep the keys, set only the AC\r
1154 emlGetMem(dataoutbuf2, FirstBlockOfSector(sectorNo) + blockNo, 1);\r
1155 memcpy(&dataoutbuf2[6], &dataoutbuf[6], 4);\r
1156 emlSetMem(dataoutbuf2, FirstBlockOfSector(sectorNo) + blockNo, 1);\r
1157 }\r
1158 }\r
8556b852
M
1159 }\r
1160\r
baeaf579 1161 }\r
1162\r
d798d31c 1163 if(mifare_classic_halt(pcs, cuid))\r
1164 if (MF_DBGLEVEL >= 1)\r
1165 Dbprintf("Halt error");\r
8556b852
M
1166\r
1167 // ----------------------------- crypto1 destroy\r
1168 crypto1_destroy(pcs);\r
ab8b654e
M
1169\r
1170 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1171 LEDsoff();\r
8556b852
M
1172 \r
1173 if (MF_DBGLEVEL >= 2) DbpString("EMUL FILL SECTORS FINISHED");\r
1174\r
5ee53a0e 1175 set_tracing(FALSE);\r
8556b852
M
1176}\r
1177\r
0675f200
M
1178\r
1179//-----------------------------------------------------------------------------\r
1180// Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)\r
1181// \r
c2731f37 1182// PARAMS - workFlags\r
1183// bit 0 - need get UID\r
1184// bit 1 - need wupC\r
1185// bit 2 - need HALT after sequence\r
1186// bit 3 - need turn on FPGA before sequence\r
1187// bit 4 - need turn off FPGA\r
1188// bit 5 - need to set datain instead of issuing USB reply (called via ARM for StandAloneMode14a)\r
1189// bit 6 - wipe tag.\r
0675f200 1190//-----------------------------------------------------------------------------\r
c2731f37 1191// magic uid card generation 1 commands\r
1192uint8_t wupC1[] = { MIFARE_MAGICWUPC1 }; \r
1193uint8_t wupC2[] = { MIFARE_MAGICWUPC2 }; \r
1194uint8_t wipeC[] = { MIFARE_MAGICWIPEC }; \r
1195 \r
1196void MifareCSetBlock(uint32_t arg0, uint32_t arg1, uint8_t *datain){\r
0675f200 1197 \r
c2731f37 1198 // params\r
1199 uint8_t workFlags = arg0;\r
1200 uint8_t blockNo = arg1;\r
0675f200 1201 \r
0675f200 1202 // variables\r
6a18e46f 1203 bool isOK = false; //assume we will get an error\r
1204 uint8_t errormsg = 0x00;\r
3fe4ff4f 1205 uint8_t uid[10] = {0x00};\r
c2731f37 1206 uint8_t data[18] = {0x00};\r
d798d31c 1207 uint32_t cuid = 0;\r
0675f200 1208 \r
6a18e46f 1209 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE] = {0x00};\r
1210 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE] = {0x00};\r
6a1f2d82 1211\r
c2731f37 1212 if (workFlags & MAGIC_INIT) {\r
208a0166
M
1213 LED_A_ON();\r
1214 LED_B_OFF();\r
99cf19d9 1215 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
3000dc4e
MHS
1216 clear_trace();\r
1217 set_tracing(TRUE);\r
208a0166 1218 }\r
0675f200 1219\r
6a18e46f 1220 //loop doesn't loop just breaks out if error\r
1221 while (true) {\r
1222 // read UID and return to client with write\r
1223 if (workFlags & MAGIC_UID) {\r
1224 if(!iso14443a_select_card(uid, NULL, &cuid, true, 0)) {\r
1225 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card");\r
1226 errormsg = MAGIC_UID;\r
6a18e46f 1227 }\r
d2094433 1228 mifare_classic_halt_ex(NULL);\r
1229 break;\r
9332b857 1230 }\r
0675f200 1231 \r
6a18e46f 1232 // wipe tag, fill it with zeros\r
1233 if (workFlags & MAGIC_WIPE){\r
2abdfa49 1234 ReaderTransmitBitsPar(wupC1, 7, NULL, NULL);\r
6a18e46f 1235 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r
1236 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("wupC1 error");\r
1237 errormsg = MAGIC_WIPE;\r
1238 break;\r
1239 }\r
0675f200 1240\r
6a18e46f 1241 ReaderTransmit(wipeC, sizeof(wipeC), NULL);\r
1242 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r
1243 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("wipeC error");\r
1244 errormsg = MAGIC_WIPE;\r
1245 break;\r
1246 }\r
0675f200 1247\r
d2094433 1248 mifare_classic_halt_ex(NULL);\r
6a18e46f 1249 } \r
0675f200 1250\r
6a18e46f 1251 // write block\r
1252 if (workFlags & MAGIC_WUPC) {\r
2abdfa49 1253 ReaderTransmitBitsPar(wupC1, 7, NULL, NULL);\r
6a18e46f 1254 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r
1255 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("wupC1 error");\r
1256 errormsg = MAGIC_WUPC;\r
1257 break;\r
1258 }\r
1259\r
1260 ReaderTransmit(wupC2, sizeof(wupC2), NULL);\r
1261 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r
1262 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("wupC2 error");\r
1263 errormsg = MAGIC_WUPC;\r
1264 break;\r
1265 }\r
9332b857 1266 }\r
c2731f37 1267\r
6a18e46f 1268 if ((mifare_sendcmd_short(NULL, 0, ISO14443A_CMD_WRITEBLOCK, blockNo, receivedAnswer, receivedAnswerPar, NULL) != 1) || (receivedAnswer[0] != 0x0a)) {\r
1269 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("write block send command error");\r
1270 errormsg = 4;\r
1271 break;\r
1272 }\r
0675f200 1273 \r
6a18e46f 1274 memcpy(data, datain, 16);\r
1275 AppendCrc14443a(data, 16);\r
0675f200 1276 \r
6a18e46f 1277 ReaderTransmit(data, sizeof(data), NULL);\r
1278 if ((ReaderReceive(receivedAnswer, receivedAnswerPar) != 1) || (receivedAnswer[0] != 0x0a)) {\r
1279 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("write block send data error");\r
1280 errormsg = 0;\r
1281 break;\r
1282 } \r
0675f200 1283 \r
6a18e46f 1284 if (workFlags & MAGIC_OFF) \r
d2094433 1285 mifare_classic_halt_ex(NULL);\r
6a18e46f 1286 \r
1287 isOK = true;\r
1288 break;\r
1289\r
1290 } // end while \r
1291\r
1292 if (isOK )\r
1293 cmd_send(CMD_ACK,1,0,0,uid,sizeof(uid));\r
1294 else\r
1295 OnErrorMagic(errormsg);\r
0675f200 1296\r
c2731f37 1297 if (workFlags & MAGIC_OFF)\r
1298 OnSuccessMagic();\r
0675f200 1299}\r
545a1f38 1300\r
c2731f37 1301void MifareCGetBlock(uint32_t arg0, uint32_t arg1, uint8_t *datain){\r
1302 \r
545a1f38 1303 uint8_t workFlags = arg0;\r
c2731f37 1304 uint8_t blockNo = arg1;\r
6a18e46f 1305 uint8_t errormsg = 0x00;\r
1306 bool isOK = false; //assume we will get an error\r
1307 \r
545a1f38 1308 // variables\r
c2731f37 1309 uint8_t data[MAX_MIFARE_FRAME_SIZE];\r
6a18e46f 1310 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE] = {0x00};\r
1311 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE] = {0x00};\r
545a1f38 1312 \r
c2731f37 1313 memset(data, 0x00, sizeof(data));\r
1314 \r
1315 if (workFlags & MAGIC_INIT) {\r
545a1f38
M
1316 LED_A_ON();\r
1317 LED_B_OFF();\r
c2731f37 1318 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN); \r
3000dc4e
MHS
1319 clear_trace();\r
1320 set_tracing(TRUE);\r
545a1f38
M
1321 }\r
1322\r
6a18e46f 1323 //loop doesn't loop just breaks out if error or done\r
1324 while (true) {\r
1325 if (workFlags & MAGIC_WUPC) {\r
2abdfa49 1326 ReaderTransmitBitsPar(wupC1, 7, NULL, NULL);\r
6a18e46f 1327 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r
1328 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("wupC1 error");\r
1329 errormsg = MAGIC_WUPC;\r
1330 break;\r
1331 }\r
545a1f38 1332\r
6a18e46f 1333 ReaderTransmit(wupC2, sizeof(wupC2), NULL);\r
1334 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r
1335 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("wupC2 error");\r
1336 errormsg = MAGIC_WUPC;\r
1337 break;\r
1338 }\r
9332b857 1339 }\r
c2731f37 1340\r
6a18e46f 1341 // read block \r
1342 if ((mifare_sendcmd_short(NULL, 0, ISO14443A_CMD_READBLOCK, blockNo, receivedAnswer, receivedAnswerPar, NULL) != 18)) {\r
1343 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("read block send command error");\r
1344 errormsg = 0;\r
1345 break;\r
1346 }\r
1347 \r
1348 memcpy(data, receivedAnswer, sizeof(data));\r
1349 \r
1350 // send HALT\r
c7962313 1351 if (workFlags & MAGIC_HALT)\r
6a18e46f 1352 mifare_classic_halt_ex(NULL);\r
c7962313 1353\r
6a18e46f 1354 isOK = true;\r
1355 break;\r
9332b857 1356 }\r
c2731f37 1357 // if MAGIC_DATAIN, the data stays on device side.\r
6a18e46f 1358 if (workFlags & MAGIC_DATAIN) {\r
1359 if (isOK)\r
1360 memcpy(datain, data, sizeof(data));\r
1361 } else {\r
1362 if (isOK) \r
1363 cmd_send(CMD_ACK,1,0,0,data,sizeof(data)); \r
1364 else \r
1365 OnErrorMagic(errormsg); \r
1366 }\r
c2731f37 1367 \r
c2731f37 1368 if (workFlags & MAGIC_OFF)\r
1369 OnSuccessMagic();\r
545a1f38
M
1370}\r
1371\r
3fe4ff4f 1372void MifareCIdent(){\r
8db18d2f 1373 #define GEN_1A 1\r
1374 #define GEN_1B 2\r
f24edfec 1375 #define GEN_2 4\r
3fe4ff4f 1376 // variables\r
8db18d2f 1377 uint8_t isGen = 0;\r
f24edfec 1378 uint8_t rec[1] = {0x00};\r
1379 uint8_t recpar[1] = {0x00};\r
1380 \r
1381 // Generation 1 test\r
2abdfa49 1382 ReaderTransmitBitsPar(wupC1, 7, NULL, NULL);\r
f24edfec 1383 if(!ReaderReceive(rec, recpar) || (rec[0] != 0x0a)) {\r
1384 goto TEST2;\r
1385 };\r
1386 isGen = GEN_1B;\r
8db18d2f 1387 \r
3fe4ff4f 1388 ReaderTransmit(wupC2, sizeof(wupC2), NULL);\r
f24edfec 1389 if(!ReaderReceive(rec, recpar) || (rec[0] != 0x0a)) {\r
8db18d2f 1390 goto OUT;\r
f24edfec 1391 }; \r
8db18d2f 1392 isGen = GEN_1A;\r
f24edfec 1393 goto OUT;\r
3fe4ff4f 1394\r
f24edfec 1395TEST2:;\r
1396/*\r
1397 // Generation 2 test\r
1398 struct Crypto1State mpcs = {0, 0};\r
1399 struct Crypto1State *pcs = &mpcs;\r
1400\r
1401 // halt previous.\r
1402 mifare_classic_halt(NULL, 0);\r
1403 \r
1404 //select\r
1405 if (!iso14443a_select_card(NULL, NULL, NULL, true, 0)) {\r
1406 goto OUT;\r
1407 };\r
1408 \r
1409 // MIFARE_CLASSIC_WRITEBLOCK 0xA0\r
1410 // ACK 0x0a\r
1411 uint16_t len = mifare_sendcmd_short(pcs, 1, 0xA0, 0, rec, recpar, NULL);\r
1412 if ((len != 1) || (rec[0] != 0x0A)) { \r
1413 isGen = GEN_2; \r
1414 };\r
1415 */\r
1416OUT:;\r
acd7ccdb 1417 // removed the if, since some magic tags misbehavies and send an answer to it.\r
1418 mifare_classic_halt(NULL, 0);\r
8db18d2f 1419 cmd_send(CMD_ACK,isGen, 0, 0, 0, 0);\r
3fe4ff4f 1420}\r
1421\r
c2731f37 1422void OnSuccessMagic(){\r
add0504d 1423 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1424 LEDsoff();\r
c2731f37 1425 set_tracing(FALSE); \r
1426}\r
1427void OnErrorMagic(uint8_t reason){\r
1428 // ACK, ISOK, reason,0,0,0\r
1429 cmd_send(CMD_ACK,0,reason,0,0,0);\r
1430 OnSuccessMagic();\r
1431}\r
add0504d 1432//\r
3fe4ff4f 1433// DESFIRE\r
1434//\r
a631936e 1435void Mifare_DES_Auth1(uint8_t arg0, uint8_t *datain){\r
4a71da5a 1436 byte_t dataout[12] = {0x00};\r
a631936e 1437 uint8_t uid[10] = {0x00};\r
d798d31c 1438 uint32_t cuid = 0;\r
a631936e 1439 \r
a631936e 1440 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
99cf19d9 1441 clear_trace();\r
810f5379 1442 set_tracing(true);\r
a631936e 1443\r
c188b1b9 1444 int len = iso14443a_select_card(uid, NULL, &cuid, true, 0);\r
a631936e 1445 if(!len) {\r
aa60d156 1446 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card");\r
0ec548dc 1447 OnError(1);\r
a631936e 1448 return;\r
1449 };\r
1450\r
1451 if(mifare_desfire_des_auth1(cuid, dataout)){\r
aa60d156 1452 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Authentication part1: Fail.");\r
0ec548dc 1453 OnError(4);\r
a631936e 1454 return;\r
1455 }\r
1456\r
1457 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) DbpString("AUTH 1 FINISHED");\r
88e20c9f 1458 cmd_send(CMD_ACK, 1, cuid, 0, dataout, sizeof(dataout));\r
a631936e 1459}\r
1460\r
1461void Mifare_DES_Auth2(uint32_t arg0, uint8_t *datain){\r
a631936e 1462 uint32_t cuid = arg0;\r
1463 uint8_t key[16] = {0x00};\r
a631936e 1464 byte_t dataout[12] = {0x00};\r
aa60d156 1465 byte_t isOK = 0;\r
a631936e 1466 \r
1467 memcpy(key, datain, 16);\r
1468 \r
1469 isOK = mifare_desfire_des_auth2(cuid, key, dataout);\r
1470 \r
1471 if( isOK) {\r
aa60d156 1472 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) Dbprintf("Authentication part2: Failed"); \r
0ec548dc 1473 OnError(4);\r
a631936e 1474 return;\r
1475 }\r
1476\r
aa60d156 1477 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) DbpString("AUTH 2 FINISHED");\r
a631936e 1478\r
1479 cmd_send(CMD_ACK, isOK, 0, 0, dataout, sizeof(dataout));\r
1480 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1481 LEDsoff();\r
88e20c9f 1482 set_tracing(FALSE);\r
ce432659 1483}
Impressum, Datenschutz