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