]> git.zerfleddert.de Git - proxmark3-svn/blame - armsrc/mifarecmd.c
Merge pull request #524 from Proxmark/iceman1001-patch-1
[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
7c7327e7 697 // send a dummy response in order to trigger the cards authentication failure timeout\r
698 uint8_t dummy_answer[8] = {0};\r
699 ReaderTransmit(dummy_answer, 8, NULL);\r
7906cb41 700\r
de77d4ac 701 num_nonces++;\r
702 if (num_nonces % 2) {\r
703 memcpy(buf+i, receivedAnswer, 4);\r
704 nt_par_enc = par_enc[0] & 0xf0;\r
705 } else {\r
706 nt_par_enc |= par_enc[0] >> 4;\r
707 memcpy(buf+i+4, receivedAnswer, 4);\r
708 memcpy(buf+i+8, &nt_par_enc, 1);\r
709 i += 9;\r
710 }\r
711\r
de77d4ac 712 }\r
713\r
714 LED_C_OFF();\r
7906cb41 715\r
de77d4ac 716 crypto1_destroy(pcs);\r
7906cb41 717\r
de77d4ac 718 LED_B_ON();\r
719 cmd_send(CMD_ACK, isOK, cuid, num_nonces, buf, sizeof(buf));\r
720 LED_B_OFF();\r
721\r
722 if (MF_DBGLEVEL >= 3) DbpString("AcquireEncryptedNonces finished");\r
723\r
724 if (field_off) {\r
725 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
726 LEDsoff();\r
727 }\r
728}\r
729\r
730\r
8556b852 731//-----------------------------------------------------------------------------\r
7906cb41
F
732// MIFARE nested authentication.\r
733//\r
8556b852 734//-----------------------------------------------------------------------------\r
9492e0b0 735void MifareNested(uint32_t arg0, uint32_t arg1, uint32_t calibrate, uint8_t *datain)\r
8556b852
M
736{\r
737 // params\r
9492e0b0 738 uint8_t blockNo = arg0 & 0xff;\r
739 uint8_t keyType = (arg0 >> 8) & 0xff;\r
740 uint8_t targetBlockNo = arg1 & 0xff;\r
741 uint8_t targetKeyType = (arg1 >> 8) & 0xff;\r
8556b852
M
742 uint64_t ui64Key = 0;\r
743\r
744 ui64Key = bytes_to_num(datain, 6);\r
7906cb41 745\r
8556b852 746 // variables\r
9492e0b0 747 uint16_t rtr, i, j, len;\r
748 uint16_t davg;\r
749 static uint16_t dmin, dmax;\r
1c611bbd 750 uint8_t uid[10];\r
6a1f2d82 751 uint32_t cuid, nt1, nt2, nttmp, nttest, ks1;\r
752 uint8_t par[1];\r
9492e0b0 753 uint32_t target_nt[2], target_ks[2];\r
7906cb41 754\r
8556b852 755 uint8_t par_array[4];\r
9492e0b0 756 uint16_t ncount = 0;\r
8556b852
M
757 struct Crypto1State mpcs = {0, 0};\r
758 struct Crypto1State *pcs;\r
759 pcs = &mpcs;\r
f71f4deb 760 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
8556b852 761\r
9492e0b0 762 uint32_t auth1_time, auth2_time;\r
763 static uint16_t delta_time;\r
764\r
09ffd16e 765 LED_A_ON();\r
766 LED_C_OFF();\r
767 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
768\r
f71f4deb 769 // free eventually allocated BigBuf memory\r
770 BigBuf_free();\r
09ffd16e 771\r
5330f532 772 if (calibrate) clear_trace();\r
773 set_tracing(true);\r
7906cb41 774\r
9492e0b0 775 // statistics on nonce distance\r
dc8ba239 776 int16_t isOK = 0;\r
777 #define NESTED_MAX_TRIES 12\r
778 uint16_t unsuccessfull_tries = 0;\r
9492e0b0 779 if (calibrate) { // for first call only. Otherwise reuse previous calibration\r
780 LED_B_ON();\r
3fe4ff4f 781 WDT_HIT();\r
8556b852 782\r
9492e0b0 783 davg = dmax = 0;\r
784 dmin = 2000;\r
785 delta_time = 0;\r
7906cb41 786\r
9492e0b0 787 for (rtr = 0; rtr < 17; rtr++) {\r
8556b852 788\r
dc8ba239 789 // Test if the action was cancelled\r
790 if(BUTTON_PRESS()) {\r
791 isOK = -2;\r
792 break;\r
793 }\r
794\r
9492e0b0 795 // prepare next select. No need to power down the card.\r
796 if(mifare_classic_halt(pcs, cuid)) {\r
797 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Halt error");\r
798 rtr--;\r
799 continue;\r
800 }\r
801\r
c04a4b60 802 if(!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {\r
9492e0b0 803 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Can't select card");\r
804 rtr--;\r
805 continue;\r
806 };\r
807\r
808 auth1_time = 0;\r
809 if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, &auth1_time)) {\r
810 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth1 error");\r
811 rtr--;\r
812 continue;\r
813 };\r
814\r
815 if (delta_time) {\r
816 auth2_time = auth1_time + delta_time;\r
817 } else {\r
818 auth2_time = 0;\r
819 }\r
820 if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_NESTED, &nt2, &auth2_time)) {\r
821 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth2 error");\r
822 rtr--;\r
823 continue;\r
824 };\r
825\r
b19bd5d6 826 nttmp = prng_successor(nt1, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160\r
827 for (i = 101; i < 1200; i++) {\r
9492e0b0 828 nttmp = prng_successor(nttmp, 1);\r
829 if (nttmp == nt2) break;\r
830 }\r
831\r
832 if (i != 1200) {\r
833 if (rtr != 0) {\r
834 davg += i;\r
835 dmin = MIN(dmin, i);\r
836 dmax = MAX(dmax, i);\r
837 }\r
838 else {\r
839 delta_time = auth2_time - auth1_time + 32; // allow some slack for proper timing\r
840 }\r
841 if (MF_DBGLEVEL >= 3) Dbprintf("Nested: calibrating... ntdist=%d", i);\r
dc8ba239 842 } else {\r
843 unsuccessfull_tries++;\r
844 if (unsuccessfull_tries > NESTED_MAX_TRIES) { // card isn't vulnerable to nested attack (random numbers are not predictable)\r
845 isOK = -3;\r
846 }\r
9492e0b0 847 }\r
8556b852 848 }\r
8556b852 849\r
9492e0b0 850 davg = (davg + (rtr - 1)/2) / (rtr - 1);\r
7906cb41 851\r
dc8ba239 852 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 853\r
9492e0b0 854 dmin = davg - 2;\r
855 dmax = davg + 2;\r
7906cb41 856\r
9492e0b0 857 LED_B_OFF();\r
7906cb41 858\r
9492e0b0 859 }\r
7906cb41
F
860 // -------------------------------------------------------------------------------------------------\r
861\r
8556b852
M
862 LED_C_ON();\r
863\r
864 // get crypted nonces for target sector\r
dc8ba239 865 for(i=0; i < 2 && !isOK; i++) { // look for exactly two different nonces\r
8556b852 866\r
9492e0b0 867 target_nt[i] = 0;\r
868 while(target_nt[i] == 0) { // continue until we have an unambiguous nonce\r
7906cb41 869\r
9492e0b0 870 // prepare next select. No need to power down the card.\r
871 if(mifare_classic_halt(pcs, cuid)) {\r
872 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Halt error");\r
873 continue;\r
874 }\r
8556b852 875\r
c04a4b60 876 if(!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {\r
9492e0b0 877 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Can't select card");\r
878 continue;\r
879 };\r
7906cb41 880\r
9492e0b0 881 auth1_time = 0;\r
882 if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, &auth1_time)) {\r
883 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth1 error");\r
884 continue;\r
885 };\r
8556b852 886\r
9492e0b0 887 // nested authentication\r
888 auth2_time = auth1_time + delta_time;\r
e35031d2 889 len = mifare_sendcmd_short(pcs, AUTH_NESTED, 0x60 + (targetKeyType & 0x01), targetBlockNo, receivedAnswer, par, &auth2_time);\r
9492e0b0 890 if (len != 4) {\r
891 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth2 error len=%d", len);\r
892 continue;\r
893 };\r
7906cb41
F
894\r
895 nt2 = bytes_to_num(receivedAnswer, 4);\r
6a1f2d82 896 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i+1, nt1, nt2, par[0]);\r
7906cb41 897\r
9492e0b0 898 // Parity validity check\r
899 for (j = 0; j < 4; j++) {\r
1f065e1d 900 par_array[j] = (oddparity8(receivedAnswer[j]) != ((par[0] >> (7-j)) & 0x01));\r
9492e0b0 901 }\r
7906cb41 902\r
9492e0b0 903 ncount = 0;\r
904 nttest = prng_successor(nt1, dmin - 1);\r
905 for (j = dmin; j < dmax + 1; j++) {\r
906 nttest = prng_successor(nttest, 1);\r
907 ks1 = nt2 ^ nttest;\r
908\r
909 if (valid_nonce(nttest, nt2, ks1, par_array)){\r
910 if (ncount > 0) { // we are only interested in disambiguous nonces, try again\r
911 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i+1, j);\r
912 target_nt[i] = 0;\r
913 break;\r
914 }\r
915 target_nt[i] = nttest;\r
916 target_ks[i] = ks1;\r
917 ncount++;\r
918 if (i == 1 && target_nt[1] == target_nt[0]) { // we need two different nonces\r
919 target_nt[i] = 0;\r
920 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j);\r
8556b852
M
921 break;\r
922 }\r
9492e0b0 923 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i+1, j);\r
8556b852 924 }\r
8556b852 925 }\r
9492e0b0 926 if (target_nt[i] == 0 && j == dmax+1 && MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i+1);\r
8556b852
M
927 }\r
928 }\r
929\r
930 LED_C_OFF();\r
7906cb41 931\r
8556b852
M
932 // ----------------------------- crypto1 destroy\r
933 crypto1_destroy(pcs);\r
7906cb41 934\r
9492e0b0 935 byte_t buf[4 + 4 * 4];\r
936 memcpy(buf, &cuid, 4);\r
937 memcpy(buf+4, &target_nt[0], 4);\r
938 memcpy(buf+8, &target_ks[0], 4);\r
939 memcpy(buf+12, &target_nt[1], 4);\r
940 memcpy(buf+16, &target_ks[1], 4);\r
7906cb41 941\r
8556b852 942 LED_B_ON();\r
dc8ba239 943 cmd_send(CMD_ACK, isOK, 0, targetBlockNo + (targetKeyType * 0x100), buf, sizeof(buf));\r
6e82300d 944 LED_B_OFF();\r
8556b852 945\r
9492e0b0 946 if (MF_DBGLEVEL >= 3) DbpString("NESTED FINISHED");\r
8556b852 947\r
8556b852
M
948 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
949 LEDsoff();\r
8556b852
M
950}\r
951\r
952//-----------------------------------------------------------------------------\r
7906cb41
F
953// MIFARE check keys. key count up to 85.\r
954//\r
8556b852 955//-----------------------------------------------------------------------------\r
275d9e61 956void MifareChkKeys(uint16_t arg0, uint16_t arg1, uint8_t arg2, uint8_t *datain)\r
8556b852 957{\r
5330f532 958 uint8_t blockNo = arg0 & 0xff;\r
959 uint8_t keyType = (arg0 >> 8) & 0xff;\r
275d9e61
OM
960 bool clearTrace = arg1 & 0x01;\r
961 bool multisectorCheck = arg1 & 0x02;\r
962 uint8_t set14aTimeout = (arg1 >> 8) & 0xff;\r
8556b852 963 uint8_t keyCount = arg2;\r
7906cb41 964\r
8556b852 965 // clear debug level\r
7906cb41 966 int OLD_MF_DBGLEVEL = MF_DBGLEVEL;\r
8556b852 967 MF_DBGLEVEL = MF_DBG_NONE;\r
7906cb41 968\r
8556b852
M
969 LED_A_ON();\r
970 LED_B_OFF();\r
971 LED_C_OFF();\r
09ffd16e 972 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
973\r
5330f532 974 if (clearTrace) clear_trace();\r
de77d4ac 975 set_tracing(true);\r
8556b852 976\r
275d9e61
OM
977 if (set14aTimeout){\r
978 iso14a_set_timeout(set14aTimeout * 10); // timeout: ms = x/106 35-minimum, 50-OK 106-recommended 500-safe\r
979 }\r
980 \r
981 if (multisectorCheck) {\r
982 TKeyIndex keyIndex = {{0}};\r
983 uint8_t sectorCnt = blockNo;\r
984 int res = MifareMultisectorChk(datain, keyCount, sectorCnt, keyType, OLD_MF_DBGLEVEL, &keyIndex);\r
de77d4ac 985\r
275d9e61
OM
986 LED_B_ON();\r
987 if (res >= 0) {\r
988 cmd_send(CMD_ACK, 1, 0, 0, keyIndex, 80);\r
989 } else {\r
990 cmd_send(CMD_ACK, 0, 0, 0, NULL, 0);\r
9492e0b0 991 }\r
275d9e61
OM
992 LED_B_OFF();\r
993 } else { \r
994 int res = MifareChkBlockKeys(datain, keyCount, blockNo, keyType, OLD_MF_DBGLEVEL);\r
995 \r
996 LED_B_ON();\r
997 if (res > 0) {\r
998 cmd_send(CMD_ACK, 1, 0, 0, datain + (res - 1) * 6, 6);\r
999 } else {\r
1000 cmd_send(CMD_ACK, 0, 0, 0, NULL, 0);\r
de77d4ac 1001 }\r
275d9e61 1002 LED_B_OFF();\r
8556b852 1003 }\r
7906cb41 1004\r
8556b852
M
1005 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1006 LEDsoff();\r
1007\r
1008 // restore debug level\r
7906cb41 1009 MF_DBGLEVEL = OLD_MF_DBGLEVEL;\r
8556b852
M
1010}\r
1011\r
1012//-----------------------------------------------------------------------------\r
1013// MIFARE commands set debug level\r
7906cb41 1014//\r
8556b852
M
1015//-----------------------------------------------------------------------------\r
1016void MifareSetDbgLvl(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
1017 MF_DBGLEVEL = arg0;\r
1018 Dbprintf("Debug level: %d", MF_DBGLEVEL);\r
1019}\r
1020\r
1021//-----------------------------------------------------------------------------\r
1022// Work with emulator memory\r
7906cb41 1023//\r
09ffd16e 1024// Note: we call FpgaDownloadAndGo(FPGA_BITSTREAM_HF) here although FPGA is not\r
1025// involved in dealing with emulator memory. But if it is called later, it might\r
1026// destroy the Emulator Memory.\r
8556b852 1027//-----------------------------------------------------------------------------\r
09ffd16e 1028\r
8556b852 1029void MifareEMemClr(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
09ffd16e 1030 FpgaDownloadAndGo(FPGA_BITSTREAM_HF);\r
8556b852
M
1031 emlClearMem();\r
1032}\r
1033\r
1034void MifareEMemSet(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
09ffd16e 1035 FpgaDownloadAndGo(FPGA_BITSTREAM_HF);\r
8556b852
M
1036 emlSetMem(datain, arg0, arg1); // data, block num, blocks count\r
1037}\r
1038\r
1039void MifareEMemGet(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
09ffd16e 1040 FpgaDownloadAndGo(FPGA_BITSTREAM_HF);\r
3fe4ff4f 1041 byte_t buf[USB_CMD_DATA_SIZE];\r
baeaf579 1042 emlGetMem(buf, arg0, arg1); // data, block num, blocks count (max 4)\r
8556b852
M
1043\r
1044 LED_B_ON();\r
3fe4ff4f 1045 cmd_send(CMD_ACK,arg0,arg1,0,buf,USB_CMD_DATA_SIZE);\r
8556b852
M
1046 LED_B_OFF();\r
1047}\r
1048\r
1049//-----------------------------------------------------------------------------\r
1050// Load a card into the emulator memory\r
7906cb41 1051//\r
8556b852
M
1052//-----------------------------------------------------------------------------\r
1053void MifareECardLoad(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
baeaf579 1054 uint8_t numSectors = arg0;\r
8556b852
M
1055 uint8_t keyType = arg1;\r
1056 uint64_t ui64Key = 0;\r
1057 uint32_t cuid;\r
1058 struct Crypto1State mpcs = {0, 0};\r
1059 struct Crypto1State *pcs;\r
1060 pcs = &mpcs;\r
1061\r
1062 // variables\r
1063 byte_t dataoutbuf[16];\r
ab8b654e 1064 byte_t dataoutbuf2[16];\r
1c611bbd 1065 uint8_t uid[10];\r
8556b852 1066\r
8556b852
M
1067 LED_A_ON();\r
1068 LED_B_OFF();\r
1069 LED_C_OFF();\r
09ffd16e 1070 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
7906cb41 1071\r
09ffd16e 1072 clear_trace();\r
1073 set_tracing(false);\r
7906cb41 1074\r
baeaf579 1075 bool isOK = true;\r
1076\r
c04a4b60 1077 if(!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {\r
baeaf579 1078 isOK = false;\r
1079 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
1080 }\r
7906cb41 1081\r
baeaf579 1082 for (uint8_t sectorNo = 0; isOK && sectorNo < numSectors; sectorNo++) {\r
1083 ui64Key = emlGetKey(sectorNo, keyType);\r
1084 if (sectorNo == 0){\r
1085 if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_FIRST)) {\r
1086 isOK = false;\r
1087 if (MF_DBGLEVEL >= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo);\r
8556b852 1088 break;\r
baeaf579 1089 }\r
1090 } else {\r
1091 if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_NESTED)) {\r
1092 isOK = false;\r
1093 if (MF_DBGLEVEL >= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo);\r
8556b852 1094 break;\r
baeaf579 1095 }\r
1096 }\r
7906cb41 1097\r
baeaf579 1098 for (uint8_t blockNo = 0; isOK && blockNo < NumBlocksPerSector(sectorNo); blockNo++) {\r
1099 if(isOK && mifare_classic_readblock(pcs, cuid, FirstBlockOfSector(sectorNo) + blockNo, dataoutbuf)) {\r
1100 isOK = false;\r
1101 if (MF_DBGLEVEL >= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo, blockNo);\r
ab8b654e
M
1102 break;\r
1103 };\r
baeaf579 1104 if (isOK) {\r
1105 if (blockNo < NumBlocksPerSector(sectorNo) - 1) {\r
1106 emlSetMem(dataoutbuf, FirstBlockOfSector(sectorNo) + blockNo, 1);\r
1107 } else { // sector trailer, keep the keys, set only the AC\r
1108 emlGetMem(dataoutbuf2, FirstBlockOfSector(sectorNo) + blockNo, 1);\r
1109 memcpy(&dataoutbuf2[6], &dataoutbuf[6], 4);\r
1110 emlSetMem(dataoutbuf2, FirstBlockOfSector(sectorNo) + blockNo, 1);\r
1111 }\r
1112 }\r
8556b852
M
1113 }\r
1114\r
baeaf579 1115 }\r
1116\r
1117 if(mifare_classic_halt(pcs, cuid)) {\r
1118 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
1119 };\r
8556b852
M
1120\r
1121 // ----------------------------- crypto1 destroy\r
1122 crypto1_destroy(pcs);\r
ab8b654e
M
1123\r
1124 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1125 LEDsoff();\r
7906cb41 1126\r
8556b852
M
1127 if (MF_DBGLEVEL >= 2) DbpString("EMUL FILL SECTORS FINISHED");\r
1128\r
8556b852
M
1129}\r
1130\r
0675f200
M
1131\r
1132//-----------------------------------------------------------------------------\r
1133// Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)\r
7906cb41 1134//\r
0675f200 1135//-----------------------------------------------------------------------------\r
3a05a1e7
OM
1136\r
1137static bool isBlockTrailer(int blockN) {\r
1138 if (blockN >= 0 && blockN < 128) {\r
1139 return ((blockN & 0x03) == 0x03);\r
1140 }\r
1141 if (blockN >= 128 && blockN <= 256) {\r
1142 return ((blockN & 0x0F) == 0x0F);\r
1143 }\r
1144 return FALSE;\r
1145}\r
1146\r
1147void MifareCWipe(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
1148 // var\r
1149 byte_t isOK = 0;\r
1150 uint32_t numBlocks = arg0;\r
1151 // cmdParams:\r
1152 // bit 0 - wipe gen1a\r
1153 // bit 1 - fill card with default data\r
1154 // bit 2 - gen1a = 0, gen1b = 1\r
1155 uint8_t cmdParams = arg1;\r
1156 bool needWipe = cmdParams & 0x01;\r
1157 bool needFill = cmdParams & 0x02;\r
1158 bool gen1b = cmdParams & 0x04;\r
1159 \r
1160 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
1161 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
1162 \r
1163 uint8_t block0[16] = {0x01, 0x02, 0x03, 0x04, 0x04, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBE, 0xAF};\r
1164 uint8_t block1[16] = {0x00};\r
1165 uint8_t blockK[16] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x08, 0x77, 0x8F, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};\r
1166 uint8_t d_block[18] = {0x00};\r
1167 \r
1168 // card commands\r
1169 uint8_t wupC1[] = { 0x40 };\r
1170 uint8_t wupC2[] = { 0x43 };\r
1171 uint8_t wipeC[] = { 0x41 };\r
1172 \r
1173 // iso14443 setup\r
1174 LED_A_ON();\r
1175 LED_B_OFF();\r
1176 LED_C_OFF();\r
1177 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
1178\r
1179 // tracing\r
1180 clear_trace();\r
1181 set_tracing(true);\r
1182 \r
1183 while (true){\r
1184 // wipe\r
1185 if (needWipe){\r
1186 ReaderTransmitBitsPar(wupC1,7,0, NULL);\r
1187 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r
1188 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");\r
1189 break;\r
1190 };\r
1191\r
1192 ReaderTransmit(wipeC, sizeof(wipeC), NULL);\r
1193 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r
1194 if (MF_DBGLEVEL >= 1) Dbprintf("wipeC error");\r
1195 break;\r
1196 };\r
1197\r
1198 if(mifare_classic_halt(NULL, 0)) {\r
1199 if (MF_DBGLEVEL > 2) Dbprintf("Halt error");\r
1200 };\r
1201 };\r
1202 \r
1203 // put default data\r
1204 if (needFill){\r
1205 // select commands\r
1206 ReaderTransmitBitsPar(wupC1, 7, 0, NULL);\r
1207\r
1208 // 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
1209 if (!gen1b) { \r
1210\r
1211 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r
1212 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");\r
1213 break;\r
1214 };\r
1215\r
1216 ReaderTransmit(wupC2, sizeof(wupC2), NULL);\r
1217 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r
1218 if (MF_DBGLEVEL >= 1) Dbprintf("wupC2 error");\r
1219 break;\r
1220 };\r
1221 }\r
1222\r
1223 // send blocks command\r
1224 for (int blockNo = 0; blockNo < numBlocks; blockNo++) {\r
1225 if ((mifare_sendcmd_short(NULL, 0, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL) != 1) || (receivedAnswer[0] != 0x0a)) {\r
1226 if (MF_DBGLEVEL >= 1) Dbprintf("write block send command error");\r
1227 break;\r
1228 };\r
1229 \r
1230 // check type of block and add crc\r
1231 if (!isBlockTrailer(blockNo)){\r
1232 memcpy(d_block, block1, 16);\r
1233 } else {\r
1234 memcpy(d_block, blockK, 16);\r
1235 }\r
1236 if (blockNo == 0) {\r
1237 memcpy(d_block, block0, 16);\r
1238 }\r
1239 AppendCrc14443a(d_block, 16);\r
1240\r
1241 // send write command\r
1242 ReaderTransmit(d_block, sizeof(d_block), NULL);\r
1243 if ((ReaderReceive(receivedAnswer, receivedAnswerPar) != 1) || (receivedAnswer[0] != 0x0a)) {\r
1244 if (MF_DBGLEVEL >= 1) Dbprintf("write block send data error");\r
1245 break;\r
1246 };\r
1247 }\r
1248 \r
1249 // halt\r
1250 // do no issue halt command for gen1b \r
1251 if (!gen1b) {\r
1252 if (mifare_classic_halt(NULL, 0)) {\r
1253 if (MF_DBGLEVEL > 2) Dbprintf("Halt error");\r
1254 break;\r
1255 }\r
1256 }\r
1257 }\r
1258 break;\r
1259 } \r
1260\r
1261 // send USB response\r
1262 LED_B_ON();\r
1263 cmd_send(CMD_ACK,isOK,0,0,NULL,0);\r
1264 LED_B_OFF();\r
1265 \r
1266 // reset fpga\r
1267 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1268 LEDsoff();\r
1269 \r
1270 return;\r
1271}\r
1272\r
0675f200 1273void MifareCSetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
7906cb41 1274\r
0675f200
M
1275 // params\r
1276 uint8_t needWipe = arg0;\r
208a0166
M
1277 // bit 0 - need get UID\r
1278 // bit 1 - need wupC\r
1279 // bit 2 - need HALT after sequence\r
1280 // bit 3 - need init FPGA and field before sequence\r
1281 // bit 4 - need reset FPGA and LED\r
7906cb41 1282 // bit 6 - gen1b backdoor type\r
208a0166 1283 uint8_t workFlags = arg1;\r
0675f200 1284 uint8_t blockNo = arg2;\r
7906cb41 1285\r
0675f200 1286 // card commands\r
7906cb41
F
1287 uint8_t wupC1[] = { 0x40 };\r
1288 uint8_t wupC2[] = { 0x43 };\r
1289 uint8_t wipeC[] = { 0x41 };\r
1290\r
0675f200
M
1291 // variables\r
1292 byte_t isOK = 0;\r
3fe4ff4f 1293 uint8_t uid[10] = {0x00};\r
1294 uint8_t d_block[18] = {0x00};\r
0675f200 1295 uint32_t cuid;\r
7906cb41 1296\r
f71f4deb 1297 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
1298 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
6a1f2d82 1299\r
3fe4ff4f 1300 // reset FPGA and LED\r
208a0166 1301 if (workFlags & 0x08) {\r
208a0166
M
1302 LED_A_ON();\r
1303 LED_B_OFF();\r
1304 LED_C_OFF();\r
09ffd16e 1305 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
7906cb41 1306\r
3000dc4e 1307 clear_trace();\r
de77d4ac 1308 set_tracing(true);\r
208a0166 1309 }\r
0675f200
M
1310\r
1311 while (true) {\r
3fe4ff4f 1312\r
0675f200 1313 // get UID from chip\r
208a0166 1314 if (workFlags & 0x01) {\r
c04a4b60 1315 if(!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {\r
0675f200 1316 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
3a05a1e7
OM
1317 // 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
1318 //break;\r
7906cb41 1319 };\r
0675f200 1320\r
7906cb41
F
1321 if(mifare_classic_halt(NULL, cuid)) {\r
1322 if (MF_DBGLEVEL > 2) Dbprintf("Halt error");\r
1323 // Continue, some magic tags misbehavies and send an answer to it.\r
3a05a1e7 1324 // break;\r
7906cb41 1325 };\r
0675f200 1326 };\r
7906cb41 1327\r
0675f200 1328 // reset chip\r
7906cb41
F
1329 // Wipe command don't work with gen1b\r
1330 if (needWipe && !(workFlags & 0x40)){\r
6a1f2d82 1331 ReaderTransmitBitsPar(wupC1,7,0, NULL);\r
1332 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r
0675f200
M
1333 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");\r
1334 break;\r
1335 };\r
1336\r
9492e0b0 1337 ReaderTransmit(wipeC, sizeof(wipeC), NULL);\r
6a1f2d82 1338 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r
0675f200
M
1339 if (MF_DBGLEVEL >= 1) Dbprintf("wipeC error");\r
1340 break;\r
1341 };\r
1342\r
3a05a1e7 1343 if(mifare_classic_halt(NULL, 0)) {\r
7906cb41
F
1344 if (MF_DBGLEVEL > 2) Dbprintf("Halt error");\r
1345 // Continue, some magic tags misbehavies and send an answer to it.\r
1346 // break;\r
0675f200 1347 };\r
7906cb41 1348 };\r
0675f200 1349\r
545a1f38 1350 // write block\r
208a0166 1351 if (workFlags & 0x02) {\r
6a1f2d82 1352 ReaderTransmitBitsPar(wupC1,7,0, NULL);\r
0675f200 1353\r
7906cb41
F
1354 // 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
1355 if (!(workFlags & 0x40)) {\r
1356\r
1357 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r
1358 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");\r
1359 break;\r
1360 };\r
1361\r
1362 ReaderTransmit(wupC2, sizeof(wupC2), NULL);\r
1363 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r
1364 if (MF_DBGLEVEL >= 1) Dbprintf("wupC2 error");\r
1365 break;\r
1366 };\r
1367 }\r
208a0166 1368 }\r
0675f200 1369\r
6a1f2d82 1370 if ((mifare_sendcmd_short(NULL, 0, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL) != 1) || (receivedAnswer[0] != 0x0a)) {\r
0675f200
M
1371 if (MF_DBGLEVEL >= 1) Dbprintf("write block send command error");\r
1372 break;\r
1373 };\r
7906cb41 1374\r
0675f200
M
1375 memcpy(d_block, datain, 16);\r
1376 AppendCrc14443a(d_block, 16);\r
7906cb41 1377\r
9492e0b0 1378 ReaderTransmit(d_block, sizeof(d_block), NULL);\r
6a1f2d82 1379 if ((ReaderReceive(receivedAnswer, receivedAnswerPar) != 1) || (receivedAnswer[0] != 0x0a)) {\r
0675f200
M
1380 if (MF_DBGLEVEL >= 1) Dbprintf("write block send data error");\r
1381 break;\r
7906cb41
F
1382 };\r
1383\r
208a0166 1384 if (workFlags & 0x04) {\r
7906cb41
F
1385 // do no issue halt command for gen1b magic tag (#db# halt error. response len: 1)\r
1386 if (!(workFlags & 0x40)) {\r
3a05a1e7 1387 if (mifare_classic_halt(NULL, 0)) {\r
7906cb41
F
1388 if (MF_DBGLEVEL > 2) Dbprintf("Halt error");\r
1389 // Continue, some magic tags misbehavies and send an answer to it.\r
1390 // break;\r
1391 }\r
1392 }\r
208a0166 1393 }\r
7906cb41 1394\r
0675f200
M
1395 isOK = 1;\r
1396 break;\r
1397 }\r
7906cb41 1398\r
0675f200 1399 LED_B_ON();\r
baeaf579 1400 cmd_send(CMD_ACK,isOK,0,0,uid,4);\r
0675f200
M
1401 LED_B_OFF();\r
1402\r
545a1f38 1403 if ((workFlags & 0x10) || (!isOK)) {\r
208a0166
M
1404 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1405 LEDsoff();\r
1406 }\r
0675f200 1407}\r
545a1f38 1408\r
baeaf579 1409\r
545a1f38 1410void MifareCGetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
7906cb41
F
1411\r
1412 // params\r
545a1f38
M
1413 // bit 1 - need wupC\r
1414 // bit 2 - need HALT after sequence\r
1415 // bit 3 - need init FPGA and field before sequence\r
1416 // bit 4 - need reset FPGA and LED\r
c89274cc 1417 // bit 5 - need to set datain instead of issuing USB reply (called via ARM for StandAloneMode14a)\r
7906cb41 1418 // bit 6 - gen1b backdoor type\r
545a1f38
M
1419 uint8_t workFlags = arg0;\r
1420 uint8_t blockNo = arg2;\r
7906cb41 1421\r
545a1f38 1422 // card commands\r
7906cb41
F
1423 uint8_t wupC1[] = { 0x40 };\r
1424 uint8_t wupC2[] = { 0x43 };\r
1425\r
545a1f38
M
1426 // variables\r
1427 byte_t isOK = 0;\r
3fe4ff4f 1428 uint8_t data[18] = {0x00};\r
545a1f38 1429 uint32_t cuid = 0;\r
7906cb41 1430\r
f71f4deb 1431 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
1432 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
7906cb41 1433\r
545a1f38 1434 if (workFlags & 0x08) {\r
545a1f38
M
1435 LED_A_ON();\r
1436 LED_B_OFF();\r
1437 LED_C_OFF();\r
09ffd16e 1438 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
1439\r
3000dc4e 1440 clear_trace();\r
de77d4ac 1441 set_tracing(true);\r
545a1f38
M
1442 }\r
1443\r
1444 while (true) {\r
1445 if (workFlags & 0x02) {\r
7bc95e2e 1446 ReaderTransmitBitsPar(wupC1,7,0, NULL);\r
6a1f2d82 1447 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r
545a1f38
M
1448 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");\r
1449 break;\r
7906cb41
F
1450 };\r
1451 // do no issue for gen1b magic tag\r
1452 if (!(workFlags & 0x40)) {\r
9492e0b0 1453 ReaderTransmit(wupC2, sizeof(wupC2), NULL);\r
6a1f2d82 1454 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r
545a1f38
M
1455 if (MF_DBGLEVEL >= 1) Dbprintf("wupC2 error");\r
1456 break;\r
1457 };\r
1458 }\r
7906cb41 1459 }\r
545a1f38
M
1460\r
1461 // read block\r
6a1f2d82 1462 if ((mifare_sendcmd_short(NULL, 0, 0x30, blockNo, receivedAnswer, receivedAnswerPar, NULL) != 18)) {\r
545a1f38
M
1463 if (MF_DBGLEVEL >= 1) Dbprintf("read block send command error");\r
1464 break;\r
1465 };\r
1466 memcpy(data, receivedAnswer, 18);\r
7906cb41 1467\r
545a1f38 1468 if (workFlags & 0x04) {\r
7906cb41
F
1469 // do no issue halt command for gen1b magic tag (#db# halt error. response len: 1)\r
1470 if (!(workFlags & 0x40)) {\r
1471 if (mifare_classic_halt(NULL, cuid)) {\r
1472 if (MF_DBGLEVEL > 1) Dbprintf("Halt error");\r
1473 // Continue, some magic tags misbehavies and send an answer to it.\r
1474 // break;\r
1475 }\r
1476 }\r
545a1f38 1477 }\r
7906cb41 1478\r
545a1f38
M
1479 isOK = 1;\r
1480 break;\r
1481 }\r
7906cb41 1482\r
545a1f38 1483 LED_B_ON();\r
c89274cc
CY
1484 if (workFlags & 0x20) {\r
1485 if (isOK)\r
1486 memcpy(datain, data, 18);\r
1487 }\r
1488 else\r
1489 cmd_send(CMD_ACK,isOK,0,0,data,18);\r
545a1f38
M
1490 LED_B_OFF();\r
1491\r
1492 if ((workFlags & 0x10) || (!isOK)) {\r
545a1f38
M
1493 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1494 LEDsoff();\r
1495 }\r
1496}\r
1497\r
3fe4ff4f 1498void MifareCIdent(){\r
7906cb41 1499\r
3fe4ff4f 1500 // card commands\r
7906cb41
F
1501 uint8_t wupC1[] = { 0x40 };\r
1502 uint8_t wupC2[] = { 0x43 };\r
1503\r
3fe4ff4f 1504 // variables\r
7906cb41
F
1505 byte_t isOK = 0;\r
1506\r
f71f4deb 1507 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
1508 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
8bdb6043
OM
1509 \r
1510 LED_A_ON();\r
1511 LED_B_OFF();\r
1512 LED_C_OFF();\r
8bdb6043
OM
1513 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
1514\r
1515 clear_trace();\r
1516 set_tracing(true); \r
3fe4ff4f 1517\r
1518 ReaderTransmitBitsPar(wupC1,7,0, NULL);\r
7906cb41
F
1519 if(ReaderReceive(receivedAnswer, receivedAnswerPar) && (receivedAnswer[0] == 0x0a)) {\r
1520 isOK = 2;\r
3fe4ff4f 1521\r
7906cb41
F
1522 ReaderTransmit(wupC2, sizeof(wupC2), NULL);\r
1523 if(ReaderReceive(receivedAnswer, receivedAnswerPar) && (receivedAnswer[0] == 0x0a)) {\r
1524 isOK = 1;\r
1525 };\r
3fe4ff4f 1526 };\r
1527\r
7906cb41
F
1528 // From iceman1001: removed the if, since some magic tags misbehavies and send an answer to it.\r
1529 mifare_classic_halt(NULL, 0);\r
8bdb6043
OM
1530 \r
1531 LED_B_ON();\r
3fe4ff4f 1532 cmd_send(CMD_ACK,isOK,0,0,0,0);\r
8bdb6043
OM
1533 LED_B_OFF();\r
1534\r
1535 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1536 LEDsoff(); \r
3fe4ff4f 1537}\r
1538\r
09ffd16e 1539//\r
3fe4ff4f 1540// DESFIRE\r
1541//\r
a631936e 1542\r
1543void Mifare_DES_Auth1(uint8_t arg0, uint8_t *datain){\r
1544\r
1545 byte_t dataout[11] = {0x00};\r
1546 uint8_t uid[10] = {0x00};\r
1547 uint32_t cuid;\r
7906cb41 1548\r
a631936e 1549 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
09ffd16e 1550 clear_trace();\r
a631936e 1551\r
c04a4b60 1552 int len = iso14443a_select_card(uid, NULL, &cuid, true, 0, true);\r
a631936e 1553 if(!len) {\r
8258f409 1554 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card");\r
1555 OnError(1);\r
a631936e 1556 return;\r
1557 };\r
1558\r
1559 if(mifare_desfire_des_auth1(cuid, dataout)){\r
8258f409 1560 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Authentication part1: Fail.");\r
1561 OnError(4);\r
a631936e 1562 return;\r
1563 }\r
1564\r
1565 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) DbpString("AUTH 1 FINISHED");\r
a631936e 1566 cmd_send(CMD_ACK,1,cuid,0,dataout, sizeof(dataout));\r
1567}\r
1568\r
1569void Mifare_DES_Auth2(uint32_t arg0, uint8_t *datain){\r
1570\r
1571 uint32_t cuid = arg0;\r
1572 uint8_t key[16] = {0x00};\r
1573 byte_t isOK = 0;\r
1574 byte_t dataout[12] = {0x00};\r
7906cb41 1575\r
a631936e 1576 memcpy(key, datain, 16);\r
7906cb41 1577\r
a631936e 1578 isOK = mifare_desfire_des_auth2(cuid, key, dataout);\r
7906cb41 1579\r
a631936e 1580 if( isOK) {\r
7906cb41 1581 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) Dbprintf("Authentication part2: Failed");\r
4973f23d 1582 OnError(4);\r
a631936e 1583 return;\r
1584 }\r
1585\r
4973f23d 1586 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) DbpString("AUTH 2 FINISHED");\r
a631936e 1587\r
1588 cmd_send(CMD_ACK, isOK, 0, 0, dataout, sizeof(dataout));\r
1589 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1590 LEDsoff();\r
3000dc4e 1591}\r
f168b263 1592\r
1593void OnSuccess(){\r
1594 pcb_blocknum = 0;\r
1595 ReaderTransmit(deselect_cmd, 3 , NULL);\r
1596 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1597 LEDsoff();\r
1598}\r
1599\r
1600void OnError(uint8_t reason){\r
1601 pcb_blocknum = 0;\r
1602 ReaderTransmit(deselect_cmd, 3 , NULL);\r
1603 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1604 cmd_send(CMD_ACK,0,reason,0,0,0);\r
1605 LEDsoff();\r
1606}\r
Impressum, Datenschutz