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