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