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