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