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