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