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