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