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