]> git.zerfleddert.de Git - proxmark3-svn/blame_incremental - armsrc/mifarecmd.c
fix WaitForResponse (without timeout)
[proxmark3-svn] / armsrc / mifarecmd.c
... / ...
CommitLineData
1//-----------------------------------------------------------------------------\r
2// Merlok - June 2011, 2012\r
3// Gerhard de Koning Gans - May 2008\r
4// Hagen Fritsch - June 2010\r
5// Midnitesnake - Dec 2013\r
6// Andy Davies - Apr 2014\r
7// Iceman - May 2014\r
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\r
18#include <stdint.h>\r
19\r
20#include "proxmark3.h"\r
21#include "usb_cdc.h"\r
22#include "crapto1/crapto1.h"\r
23#include "iso14443a.h"\r
24#include "BigBuf.h"\r
25#include "mifareutil.h"\r
26#include "apps.h"\r
27#include "protocols.h"\r
28#include "util.h"\r
29#include "parity.h"\r
30#include "crc.h"\r
31#include "fpgaloader.h"\r
32\r
33#define HARDNESTED_AUTHENTICATION_TIMEOUT 848 // card times out 1ms after wrong authentication (according to NXP documentation)\r
34#define HARDNESTED_PRE_AUTHENTICATION_LEADTIME 400 // some (non standard) cards need a pause after select before they are ready for first authentication\r
35\r
36/*\r
37// the block number for the ISO14443-4 PCB\r
38static uint8_t pcb_blocknum = 0;\r
39// Deselect card by sending a s-block. the crc is precalced for speed\r
40static uint8_t deselect_cmd[] = {0xc2,0xe0,0xb4};\r
41\r
42static void OnSuccess(){\r
43 pcb_blocknum = 0;\r
44 ReaderTransmit(deselect_cmd, 3 , NULL);\r
45 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
46 LEDsoff();\r
47}\r
48*/\r
49\r
50static void OnError(uint8_t reason){\r
51 // pcb_blocknum = 0;\r
52 // ReaderTransmit(deselect_cmd, 3 , NULL);\r
53 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
54 LED_D_OFF();\r
55 cmd_send(CMD_ACK,0,reason,0,0,0);\r
56 LED_A_OFF();\r
57}\r
58\r
59//-----------------------------------------------------------------------------\r
60// Select, Authenticate, Read a MIFARE tag.\r
61// read block\r
62//-----------------------------------------------------------------------------\r
63void MifareReadBlock(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)\r
64{\r
65 LED_A_ON();\r
66\r
67 uint8_t blockNo = arg0;\r
68 uint8_t keyType = arg1;\r
69 uint64_t ui64Key = 0;\r
70 ui64Key = bytes_to_num(datain, 6);\r
71\r
72 byte_t isOK = 0;\r
73 byte_t dataoutbuf[16];\r
74 uint8_t uid[10];\r
75 uint32_t cuid;\r
76 struct Crypto1State mpcs = {0, 0};\r
77 struct Crypto1State *pcs;\r
78 pcs = &mpcs;\r
79\r
80 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
81\r
82 clear_trace();\r
83\r
84 while (true) {\r
85 if(!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {\r
86 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
87 break;\r
88 };\r
89\r
90 if(mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, NULL)) {\r
91 if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");\r
92 break;\r
93 };\r
94\r
95 if(mifare_classic_readblock(pcs, cuid, blockNo, dataoutbuf)) {\r
96 if (MF_DBGLEVEL >= 1) Dbprintf("Read block error");\r
97 break;\r
98 };\r
99\r
100 if(mifare_classic_halt(pcs, cuid)) {\r
101 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
102 break;\r
103 };\r
104\r
105 isOK = 1;\r
106 break;\r
107 }\r
108\r
109 // ----------------------------- crypto1 destroy\r
110 crypto1_destroy(pcs);\r
111\r
112 if (MF_DBGLEVEL >= 2) DbpString("READ BLOCK FINISHED");\r
113\r
114 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
115\r
116 LED_B_ON();\r
117 cmd_send(CMD_ACK,isOK,0,0,dataoutbuf,16);\r
118 LED_B_OFF();\r
119\r
120 LEDsoff();\r
121}\r
122\r
123void MifareUC_Auth(uint8_t arg0, uint8_t *keybytes){\r
124\r
125 LED_A_ON();\r
126 bool turnOffField = (arg0 == 1);\r
127\r
128 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
129\r
130 if (!iso14443a_select_card(NULL, NULL, NULL, true, 0, true)) {\r
131 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card");\r
132 OnError(0);\r
133 return;\r
134 };\r
135\r
136 if (!mifare_ultra_auth(keybytes)){\r
137 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Authentication failed");\r
138 OnError(1);\r
139 return;\r
140 }\r
141\r
142 if (turnOffField) {\r
143 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
144 LED_D_OFF();\r
145 }\r
146\r
147 cmd_send(CMD_ACK,1,0,0,0,0);\r
148 LED_A_OFF();\r
149}\r
150\r
151// Arg0 = BlockNo,\r
152// Arg1 = UsePwd bool\r
153// datain = PWD bytes,\r
154void MifareUReadBlock(uint8_t arg0, uint8_t arg1, uint8_t *datain)\r
155{\r
156 LED_A_ON();\r
157\r
158 uint8_t blockNo = arg0;\r
159 byte_t dataout[16] = {0x00};\r
160 bool useKey = (arg1 == 1); //UL_C\r
161 bool usePwd = (arg1 == 2); //UL_EV1/NTAG\r
162\r
163 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
164\r
165 int len = iso14443a_select_card(NULL, NULL, NULL, true, 0, true);\r
166 if(!len) {\r
167 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card (RC:%02X)",len);\r
168 OnError(1);\r
169 return;\r
170 }\r
171\r
172 // UL-C authentication\r
173 if (useKey) {\r
174 uint8_t key[16] = {0x00};\r
175 memcpy(key, datain, sizeof(key) );\r
176\r
177 if ( !mifare_ultra_auth(key) ) {\r
178 OnError(1);\r
179 return;\r
180 }\r
181 }\r
182\r
183 // UL-EV1 / NTAG authentication\r
184 if (usePwd) {\r
185 uint8_t pwd[4] = {0x00};\r
186 memcpy(pwd, datain, 4);\r
187 uint8_t pack[4] = {0,0,0,0};\r
188 if (!mifare_ul_ev1_auth(pwd, pack)) {\r
189 OnError(1);\r
190 return;\r
191 }\r
192 }\r
193\r
194 if (mifare_ultra_readblock(blockNo, dataout)) {\r
195 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Read block error");\r
196 OnError(2);\r
197 return;\r
198 }\r
199\r
200 if (mifare_ultra_halt()) {\r
201 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Halt error");\r
202 OnError(3);\r
203 return;\r
204 }\r
205\r
206 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
207 LED_D_OFF();\r
208\r
209 cmd_send(CMD_ACK,1,0,0,dataout,16);\r
210 LED_A_OFF();\r
211}\r
212\r
213//-----------------------------------------------------------------------------\r
214// Select, Authenticate, Read a MIFARE tag.\r
215// read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)\r
216//-----------------------------------------------------------------------------\r
217void MifareReadSector(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)\r
218{\r
219 // params\r
220 uint8_t sectorNo = arg0;\r
221 uint8_t keyType = arg1;\r
222 uint64_t ui64Key = 0;\r
223 ui64Key = bytes_to_num(datain, 6);\r
224\r
225 // variables\r
226 byte_t isOK = 0;\r
227 byte_t dataoutbuf[16 * 16];\r
228 uint8_t uid[10];\r
229 uint32_t cuid;\r
230 struct Crypto1State mpcs = {0, 0};\r
231 struct Crypto1State *pcs;\r
232 pcs = &mpcs;\r
233\r
234 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
235\r
236 clear_trace();\r
237\r
238 LED_A_ON();\r
239 LED_B_OFF();\r
240 LED_C_OFF();\r
241\r
242 isOK = 1;\r
243 if(!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {\r
244 isOK = 0;\r
245 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
246 }\r
247\r
248\r
249 if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_FIRST, NULL)) {\r
250 isOK = 0;\r
251 if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");\r
252 }\r
253\r
254 for (uint8_t blockNo = 0; isOK && blockNo < NumBlocksPerSector(sectorNo); blockNo++) {\r
255 if(mifare_classic_readblock(pcs, cuid, FirstBlockOfSector(sectorNo) + blockNo, dataoutbuf + 16 * blockNo)) {\r
256 isOK = 0;\r
257 if (MF_DBGLEVEL >= 1) Dbprintf("Read sector %2d block %2d error", sectorNo, blockNo);\r
258 break;\r
259 }\r
260 }\r
261\r
262 if(mifare_classic_halt(pcs, cuid)) {\r
263 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
264 }\r
265\r
266 // ----------------------------- crypto1 destroy\r
267 crypto1_destroy(pcs);\r
268\r
269 if (MF_DBGLEVEL >= 2) DbpString("READ SECTOR FINISHED");\r
270\r
271 // Thats it...\r
272 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
273\r
274 LED_B_ON();\r
275 cmd_send(CMD_ACK,isOK,0,0,dataoutbuf,16*NumBlocksPerSector(sectorNo));\r
276 LED_B_OFF();\r
277\r
278 LEDsoff();\r
279}\r
280\r
281// arg0 = blockNo (start)\r
282// arg1 = Pages (number of blocks)\r
283// arg2 = useKey\r
284// datain = KEY bytes\r
285void MifareUReadCard(uint8_t arg0, uint16_t arg1, uint8_t arg2, uint8_t *datain)\r
286{\r
287 LED_A_ON();\r
288 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
289\r
290 // free eventually allocated BigBuf memory\r
291 BigBuf_free();\r
292\r
293 // params\r
294 uint8_t blockNo = arg0;\r
295 uint16_t blocks = arg1;\r
296 bool useKey = (arg2 == 1); //UL_C\r
297 bool usePwd = (arg2 == 2); //UL_EV1/NTAG\r
298 uint32_t countblocks = 0;\r
299 uint8_t *dataout = BigBuf_malloc(CARD_MEMORY_SIZE);\r
300 if (dataout == NULL){\r
301 Dbprintf("out of memory");\r
302 OnError(1);\r
303 return;\r
304 }\r
305\r
306 int len = iso14443a_select_card(NULL, NULL, NULL, true, 0, true);\r
307 if (!len) {\r
308 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card (RC:%d)",len);\r
309 OnError(1);\r
310 return;\r
311 }\r
312\r
313 // UL-C authentication\r
314 if (useKey) {\r
315 uint8_t key[16] = {0x00};\r
316 memcpy(key, datain, sizeof(key) );\r
317\r
318 if ( !mifare_ultra_auth(key) ) {\r
319 OnError(1);\r
320 return;\r
321 }\r
322 }\r
323\r
324 // UL-EV1 / NTAG authentication\r
325 if (usePwd) {\r
326 uint8_t pwd[4] = {0x00};\r
327 memcpy(pwd, datain, sizeof(pwd));\r
328 uint8_t pack[4] = {0,0,0,0};\r
329\r
330 if (!mifare_ul_ev1_auth(pwd, pack)){\r
331 OnError(1);\r
332 return;\r
333 }\r
334 }\r
335\r
336 for (int i = 0; i < blocks; i++){\r
337 if ((i*4) + 4 >= CARD_MEMORY_SIZE) {\r
338 Dbprintf("Data exceeds buffer!!");\r
339 break;\r
340 }\r
341\r
342 len = mifare_ultra_readblock(blockNo + i, dataout + 4 * i);\r
343\r
344 if (len) {\r
345 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Read block %d error",i);\r
346 // if no blocks read - error out\r
347 if (i==0){\r
348 OnError(2);\r
349 return;\r
350 } else {\r
351 //stop at last successful read block and return what we got\r
352 break;\r
353 }\r
354 } else {\r
355 countblocks++;\r
356 }\r
357 }\r
358\r
359 len = mifare_ultra_halt();\r
360 if (len) {\r
361 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Halt error");\r
362 OnError(3);\r
363 return;\r
364 }\r
365\r
366 if (MF_DBGLEVEL >= MF_DBG_DEBUG) Dbprintf("Blocks read %d", countblocks);\r
367\r
368 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
369 LED_D_OFF();\r
370\r
371 cmd_send(CMD_ACK, 1, countblocks*4, BigBuf_max_traceLen(), 0, 0);\r
372\r
373 BigBuf_free();\r
374 LED_A_OFF();\r
375}\r
376\r
377//-----------------------------------------------------------------------------\r
378// Select, Authenticate, Write a MIFARE tag.\r
379// read block\r
380//-----------------------------------------------------------------------------\r
381void MifareWriteBlock(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)\r
382{\r
383 // params\r
384 uint8_t blockNo = arg0;\r
385 uint8_t keyType = arg1;\r
386 uint64_t ui64Key = 0;\r
387 byte_t blockdata[16];\r
388\r
389 ui64Key = bytes_to_num(datain, 6);\r
390 memcpy(blockdata, datain + 10, 16);\r
391\r
392 // variables\r
393 byte_t isOK = 0;\r
394 uint8_t uid[10];\r
395 uint32_t cuid;\r
396 struct Crypto1State mpcs = {0, 0};\r
397 struct Crypto1State *pcs;\r
398 pcs = &mpcs;\r
399\r
400 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
401\r
402 clear_trace();\r
403\r
404 LED_A_ON();\r
405 LED_B_OFF();\r
406 LED_C_OFF();\r
407\r
408 while (true) {\r
409 if(!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {\r
410 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
411 break;\r
412 };\r
413\r
414 if(mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, NULL)) {\r
415 if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");\r
416 break;\r
417 };\r
418\r
419 if(mifare_classic_writeblock(pcs, cuid, blockNo, blockdata)) {\r
420 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
421 break;\r
422 };\r
423\r
424 if(mifare_classic_halt(pcs, cuid)) {\r
425 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
426 break;\r
427 };\r
428\r
429 isOK = 1;\r
430 break;\r
431 }\r
432\r
433 // ----------------------------- crypto1 destroy\r
434 crypto1_destroy(pcs);\r
435\r
436 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");\r
437\r
438 // Thats it...\r
439 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
440\r
441 LED_B_ON();\r
442 cmd_send(CMD_ACK,isOK,0,0,0,0);\r
443 LED_B_OFF();\r
444\r
445\r
446 LEDsoff();\r
447}\r
448\r
449/* // Command not needed but left for future testing\r
450void MifareUWriteBlockCompat(uint8_t arg0, uint8_t *datain)\r
451{\r
452 uint8_t blockNo = arg0;\r
453 byte_t blockdata[16] = {0x00};\r
454\r
455 memcpy(blockdata, datain, 16);\r
456\r
457 uint8_t uid[10] = {0x00};\r
458\r
459 LED_A_ON(); LED_B_OFF(); LED_C_OFF();\r
460\r
461 clear_trace();\r
462 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
463\r
464 if(!iso14443a_select_card(uid, NULL, NULL, true, 0)) {\r
465 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
466 OnError(0);\r
467 return;\r
468 };\r
469\r
470 if(mifare_ultra_writeblock_compat(blockNo, blockdata)) {\r
471 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
472 OnError(0);\r
473 return; };\r
474\r
475 if(mifare_ultra_halt()) {\r
476 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
477 OnError(0);\r
478 return;\r
479 };\r
480\r
481 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");\r
482\r
483 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
484\r
485 cmd_send(CMD_ACK,1,0,0,0,0);\r
486 LEDsoff();\r
487}\r
488*/\r
489\r
490// Arg0 : Block to write to.\r
491// Arg1 : 0 = use no authentication.\r
492// 1 = use 0x1A authentication.\r
493// 2 = use 0x1B authentication.\r
494// datain : 4 first bytes is data to be written.\r
495// : 4/16 next bytes is authentication key.\r
496void MifareUWriteBlock(uint8_t arg0, uint8_t arg1, uint8_t *datain)\r
497{\r
498 uint8_t blockNo = arg0;\r
499 bool useKey = (arg1 == 1); //UL_C\r
500 bool usePwd = (arg1 == 2); //UL_EV1/NTAG\r
501 byte_t blockdata[4] = {0x00};\r
502\r
503 memcpy(blockdata, datain,4);\r
504\r
505 LEDsoff();\r
506 LED_A_ON();\r
507 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
508\r
509 clear_trace();\r
510\r
511 if(!iso14443a_select_card(NULL, NULL, NULL, true, 0, true)) {\r
512 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
513 OnError(0);\r
514 return;\r
515 };\r
516\r
517 // UL-C authentication\r
518 if ( useKey ) {\r
519 uint8_t key[16] = {0x00};\r
520 memcpy(key, datain+4, sizeof(key) );\r
521\r
522 if ( !mifare_ultra_auth(key) ) {\r
523 OnError(1);\r
524 return;\r
525 }\r
526 }\r
527\r
528 // UL-EV1 / NTAG authentication\r
529 if (usePwd) {\r
530 uint8_t pwd[4] = {0x00};\r
531 memcpy(pwd, datain+4, 4);\r
532 uint8_t pack[4] = {0,0,0,0};\r
533 if (!mifare_ul_ev1_auth(pwd, pack)) {\r
534 OnError(1);\r
535 return;\r
536 }\r
537 }\r
538\r
539 if(mifare_ultra_writeblock(blockNo, blockdata)) {\r
540 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
541 OnError(0);\r
542 return;\r
543 };\r
544\r
545 if(mifare_ultra_halt()) {\r
546 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
547 OnError(0);\r
548 return;\r
549 };\r
550\r
551 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");\r
552\r
553 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
554\r
555 cmd_send(CMD_ACK,1,0,0,0,0);\r
556 LEDsoff();\r
557}\r
558\r
559void MifareUSetPwd(uint8_t arg0, uint8_t *datain){\r
560\r
561 uint8_t pwd[16] = {0x00};\r
562 byte_t blockdata[4] = {0x00};\r
563\r
564 memcpy(pwd, datain, 16);\r
565\r
566 LED_A_ON(); LED_B_OFF(); LED_C_OFF();\r
567 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
568\r
569 clear_trace();\r
570\r
571 if(!iso14443a_select_card(NULL, NULL, NULL, true, 0, true)) {\r
572 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
573 OnError(0);\r
574 return;\r
575 };\r
576\r
577 blockdata[0] = pwd[7];\r
578 blockdata[1] = pwd[6];\r
579 blockdata[2] = pwd[5];\r
580 blockdata[3] = pwd[4];\r
581 if(mifare_ultra_writeblock( 44, blockdata)) {\r
582 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
583 OnError(44);\r
584 return;\r
585 };\r
586\r
587 blockdata[0] = pwd[3];\r
588 blockdata[1] = pwd[2];\r
589 blockdata[2] = pwd[1];\r
590 blockdata[3] = pwd[0];\r
591 if(mifare_ultra_writeblock( 45, blockdata)) {\r
592 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
593 OnError(45);\r
594 return;\r
595 };\r
596\r
597 blockdata[0] = pwd[15];\r
598 blockdata[1] = pwd[14];\r
599 blockdata[2] = pwd[13];\r
600 blockdata[3] = pwd[12];\r
601 if(mifare_ultra_writeblock( 46, blockdata)) {\r
602 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
603 OnError(46);\r
604 return;\r
605 };\r
606\r
607 blockdata[0] = pwd[11];\r
608 blockdata[1] = pwd[10];\r
609 blockdata[2] = pwd[9];\r
610 blockdata[3] = pwd[8];\r
611 if(mifare_ultra_writeblock( 47, blockdata)) {\r
612 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
613 OnError(47);\r
614 return;\r
615 };\r
616\r
617 if(mifare_ultra_halt()) {\r
618 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
619 OnError(0);\r
620 return;\r
621 };\r
622\r
623 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
624\r
625 cmd_send(CMD_ACK,1,0,0,0,0);\r
626 LEDsoff();\r
627}\r
628\r
629// Return 1 if the nonce is invalid else return 0\r
630int valid_nonce(uint32_t Nt, uint32_t NtEnc, uint32_t Ks1, uint8_t *parity) {\r
631 return ((oddparity8((Nt >> 24) & 0xFF) == ((parity[0]) ^ oddparity8((NtEnc >> 24) & 0xFF) ^ BIT(Ks1,16))) & \\r
632 (oddparity8((Nt >> 16) & 0xFF) == ((parity[1]) ^ oddparity8((NtEnc >> 16) & 0xFF) ^ BIT(Ks1,8))) & \\r
633 (oddparity8((Nt >> 8) & 0xFF) == ((parity[2]) ^ oddparity8((NtEnc >> 8) & 0xFF) ^ BIT(Ks1,0)))) ? 1 : 0;\r
634}\r
635\r
636\r
637//-----------------------------------------------------------------------------\r
638// acquire encrypted nonces in order to perform the attack described in\r
639// Carlo Meijer, Roel Verdult, "Ciphertext-only Cryptanalysis on Hardened\r
640// Mifare Classic Cards" in Proceedings of the 22nd ACM SIGSAC Conference on\r
641// Computer and Communications Security, 2015\r
642//-----------------------------------------------------------------------------\r
643void MifareAcquireEncryptedNonces(uint32_t arg0, uint32_t arg1, uint32_t flags, uint8_t *datain)\r
644{\r
645 uint64_t ui64Key = 0;\r
646 uint8_t uid[10];\r
647 uint32_t cuid;\r
648 uint8_t cascade_levels = 0;\r
649 struct Crypto1State mpcs = {0, 0};\r
650 struct Crypto1State *pcs;\r
651 pcs = &mpcs;\r
652 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
653 int16_t isOK = 0;\r
654 uint8_t par_enc[1];\r
655 uint8_t nt_par_enc = 0;\r
656 uint8_t buf[USB_CMD_DATA_SIZE];\r
657 uint32_t timeout;\r
658\r
659 uint8_t blockNo = arg0 & 0xff;\r
660 uint8_t keyType = (arg0 >> 8) & 0xff;\r
661 uint8_t targetBlockNo = arg1 & 0xff;\r
662 uint8_t targetKeyType = (arg1 >> 8) & 0xff;\r
663 ui64Key = bytes_to_num(datain, 6);\r
664 bool initialize = flags & 0x0001;\r
665 bool slow = flags & 0x0002;\r
666 bool field_off = flags & 0x0004;\r
667\r
668 LED_A_ON();\r
669 LED_C_OFF();\r
670\r
671 if (initialize) {\r
672 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
673 clear_trace();\r
674 set_tracing(true);\r
675 }\r
676\r
677 LED_C_ON();\r
678\r
679 uint16_t num_nonces = 0;\r
680 bool have_uid = false;\r
681 for (uint16_t i = 0; i <= USB_CMD_DATA_SIZE - 9; ) {\r
682\r
683 // Test if the action was cancelled\r
684 if(BUTTON_PRESS()) {\r
685 isOK = 2;\r
686 field_off = true;\r
687 break;\r
688 }\r
689\r
690 if (!have_uid) { // need a full select cycle to get the uid first\r
691 iso14a_card_select_t card_info;\r
692 if(!iso14443a_select_card(uid, &card_info, &cuid, true, 0, true)) {\r
693 if (MF_DBGLEVEL >= 1) Dbprintf("AcquireNonces: Can't select card (ALL)");\r
694 continue;\r
695 }\r
696 switch (card_info.uidlen) {\r
697 case 4 : cascade_levels = 1; break;\r
698 case 7 : cascade_levels = 2; break;\r
699 case 10: cascade_levels = 3; break;\r
700 default: break;\r
701 }\r
702 have_uid = true;\r
703 } else { // no need for anticollision. We can directly select the card\r
704 if(!iso14443a_select_card(uid, NULL, NULL, false, cascade_levels, true)) {\r
705 if (MF_DBGLEVEL >= 1) Dbprintf("AcquireNonces: Can't select card (UID)");\r
706 continue;\r
707 }\r
708 }\r
709\r
710 if (slow) {\r
711 timeout = GetCountSspClk() + HARDNESTED_PRE_AUTHENTICATION_LEADTIME;\r
712 while(GetCountSspClk() < timeout);\r
713 }\r
714\r
715 uint32_t nt1;\r
716 if (mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, NULL, NULL)) {\r
717 if (MF_DBGLEVEL >= 1) Dbprintf("AcquireNonces: Auth1 error");\r
718 continue;\r
719 }\r
720\r
721 // nested authentication\r
722 uint16_t len = mifare_sendcmd_short(pcs, AUTH_NESTED, 0x60 + (targetKeyType & 0x01), targetBlockNo, receivedAnswer, par_enc, NULL);\r
723 if (len != 4) {\r
724 if (MF_DBGLEVEL >= 1) Dbprintf("AcquireNonces: Auth2 error len=%d", len);\r
725 continue;\r
726 }\r
727\r
728 // send an incomplete dummy response in order to trigger the card's authentication failure timeout\r
729 uint8_t dummy_answer[1] = {0};\r
730 ReaderTransmit(dummy_answer, 1, NULL);\r
731\r
732 timeout = GetCountSspClk() + HARDNESTED_AUTHENTICATION_TIMEOUT;\r
733\r
734 num_nonces++;\r
735 if (num_nonces % 2) {\r
736 memcpy(buf+i, receivedAnswer, 4);\r
737 nt_par_enc = par_enc[0] & 0xf0;\r
738 } else {\r
739 nt_par_enc |= par_enc[0] >> 4;\r
740 memcpy(buf+i+4, receivedAnswer, 4);\r
741 memcpy(buf+i+8, &nt_par_enc, 1);\r
742 i += 9;\r
743 }\r
744\r
745 // wait for the card to become ready again\r
746 while(GetCountSspClk() < timeout);\r
747\r
748 }\r
749\r
750 LED_C_OFF();\r
751\r
752 crypto1_destroy(pcs);\r
753\r
754 if (field_off) {\r
755 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
756 LEDsoff();\r
757 }\r
758\r
759 LED_B_ON();\r
760 cmd_send(CMD_ACK, isOK, cuid, num_nonces, buf, sizeof(buf));\r
761 LED_B_OFF();\r
762\r
763 if (MF_DBGLEVEL >= 3) DbpString("AcquireEncryptedNonces finished");\r
764\r
765}\r
766\r
767\r
768//-----------------------------------------------------------------------------\r
769// MIFARE nested authentication.\r
770//\r
771//-----------------------------------------------------------------------------\r
772void MifareNested(uint32_t arg0, uint32_t arg1, uint32_t calibrate, uint8_t *datain) {\r
773\r
774 uint8_t blockNo = arg0 & 0xff;\r
775 uint8_t keyType = (arg0 >> 8) & 0xff;\r
776 uint8_t targetBlockNo = arg1 & 0xff;\r
777 uint8_t targetKeyType = (arg1 >> 8) & 0xff;\r
778 uint64_t ui64Key = 0;\r
779\r
780 ui64Key = bytes_to_num(datain, 6);\r
781\r
782 uint16_t rtr, i, j, len;\r
783 uint16_t davg;\r
784 static uint16_t dmin, dmax;\r
785 uint8_t uid[10];\r
786 uint32_t cuid, nt1, nt2, nttmp, nttest, ks1;\r
787 uint8_t par[1];\r
788 uint32_t target_nt[2], target_ks[2];\r
789 uint8_t target_nt_duplicate_count = 0;\r
790\r
791 uint8_t par_array[4];\r
792 uint16_t ncount = 0;\r
793 struct Crypto1State mpcs = {0, 0};\r
794 struct Crypto1State *pcs;\r
795 pcs = &mpcs;\r
796 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
797\r
798 uint32_t auth1_time, auth2_time, authentication_timeout = 0;\r
799 static uint16_t delta_time;\r
800\r
801 LED_A_ON();\r
802 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
803\r
804 // free eventually allocated BigBuf memory\r
805 BigBuf_free();\r
806\r
807 if (calibrate) clear_trace();\r
808 set_tracing(true);\r
809\r
810 // statistics on nonce distance\r
811 int16_t isOK = 0;\r
812 #define NESTED_MAX_TRIES 12\r
813 uint16_t unsuccessfull_tries = 0;\r
814 if (calibrate) { // for first call only. Otherwise reuse previous calibration\r
815 LED_B_ON();\r
816 WDT_HIT();\r
817\r
818 davg = dmax = 0;\r
819 dmin = 2000;\r
820 delta_time = 0;\r
821\r
822 for (rtr = 0; rtr < 17; rtr++) {\r
823\r
824 // prepare next select. No need to power down the card.\r
825 if (mifare_classic_halt(pcs, cuid)) {\r
826 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Halt error");\r
827 rtr--;\r
828 continue;\r
829 }\r
830\r
831 // Test if the action was cancelled\r
832 if (BUTTON_PRESS()) {\r
833 isOK = -2;\r
834 break;\r
835 }\r
836\r
837 if (!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {\r
838 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Can't select card");\r
839 rtr--;\r
840 continue;\r
841 };\r
842\r
843 auth1_time = 0;\r
844 if (mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, &auth1_time, NULL)) {\r
845 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth1 error");\r
846 rtr--;\r
847 continue;\r
848 };\r
849\r
850 if (delta_time) {\r
851 auth2_time = auth1_time + delta_time;\r
852 } else {\r
853 auth2_time = 0;\r
854 }\r
855 if (mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_NESTED, &nt2, &auth2_time, NULL)) {\r
856 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth2 error");\r
857 rtr--;\r
858 continue;\r
859 };\r
860\r
861 nttmp = prng_successor(nt1, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160\r
862 for (i = 101; i < 1200; i++) {\r
863 nttmp = prng_successor(nttmp, 1);\r
864 if (nttmp == nt2) break;\r
865 }\r
866\r
867 if (i != 1200) {\r
868 if (rtr != 0) {\r
869 davg += i;\r
870 dmin = MIN(dmin, i);\r
871 dmax = MAX(dmax, i);\r
872 }\r
873 else {\r
874 delta_time = auth2_time - auth1_time + 32; // allow some slack for proper timing\r
875 }\r
876 if (MF_DBGLEVEL >= 3) Dbprintf("Nested: calibrating... ntdist=%d", i);\r
877 } else {\r
878 unsuccessfull_tries++;\r
879 if (unsuccessfull_tries > NESTED_MAX_TRIES) { // card isn't vulnerable to nested attack (random numbers are not predictable)\r
880 isOK = -3;\r
881 }\r
882 }\r
883 }\r
884\r
885 davg = (davg + (rtr - 1)/2) / (rtr - 1);\r
886\r
887 if (MF_DBGLEVEL >= 3) Dbprintf("rtr=%d isOK=%d min=%d max=%d avg=%d, delta_time=%d", rtr, isOK, dmin, dmax, davg, delta_time);\r
888\r
889 dmin = davg - 2;\r
890 dmax = davg + 2;\r
891\r
892 LED_B_OFF();\r
893\r
894 }\r
895 // -------------------------------------------------------------------------------------------------\r
896\r
897 LED_C_ON();\r
898\r
899 // get crypted nonces for target sector\r
900 for (i=0; i < 2 && !isOK; i++) { // look for exactly two different nonces\r
901\r
902 target_nt[i] = 0;\r
903 while (target_nt[i] == 0 && !isOK) { // continue until we have an unambiguous nonce\r
904\r
905 // prepare next select. No need to power down the card.\r
906 if(mifare_classic_halt(pcs, cuid)) {\r
907 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Halt error");\r
908 continue;\r
909 }\r
910\r
911 // break out of the loop on button press\r
912 if (BUTTON_PRESS()) {\r
913 isOK = -2;\r
914 break;\r
915 }\r
916\r
917 if (!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {\r
918 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Can't select card");\r
919 continue;\r
920 }\r
921\r
922 auth1_time = 0;\r
923 authentication_timeout = 0;\r
924 if (mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, &auth1_time, &authentication_timeout)) {\r
925 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth1 error");\r
926 continue;\r
927 }\r
928\r
929 // nested authentication\r
930 auth2_time = auth1_time + delta_time;\r
931 len = mifare_sendcmd_short(pcs, AUTH_NESTED, 0x60 + (targetKeyType & 0x01), targetBlockNo, receivedAnswer, par, &auth2_time);\r
932 if (len != 4) {\r
933 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth2 error len=%d", len);\r
934 continue;\r
935 }\r
936\r
937 nt2 = bytes_to_num(receivedAnswer, 4);\r
938 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i+1, nt1, nt2, par[0]);\r
939\r
940 // Parity validity check\r
941 for (j = 0; j < 4; j++) {\r
942 par_array[j] = (oddparity8(receivedAnswer[j]) != ((par[0] >> (7-j)) & 0x01));\r
943 }\r
944\r
945 ncount = 0;\r
946 nttest = prng_successor(nt1, dmin - 1);\r
947 for (j = dmin; j < dmax + 1; j++) {\r
948 nttest = prng_successor(nttest, 1);\r
949 ks1 = nt2 ^ nttest;\r
950\r
951 if (valid_nonce(nttest, nt2, ks1, par_array)){\r
952 if (ncount > 0) { // we are only interested in disambiguous nonces, try again\r
953 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i+1, j);\r
954 target_nt[i] = 0;\r
955 break;\r
956 }\r
957 target_nt[i] = nttest;\r
958 target_ks[i] = ks1;\r
959 ncount++;\r
960 if (i == 1 && target_nt[1] == target_nt[0]) { // we need two different nonces\r
961 if( ++target_nt_duplicate_count >= NESTED_MAX_TRIES ) { // unable to get a 2nd nonce after NESTED_MAX_TRIES tries, probably a fixed nonce\r
962 if (MF_DBGLEVEL >= 2) Dbprintf("Nonce#2: cannot get nonce that != nonce#1, continuing anyway with single nonce! ntdist=%d", j);\r
963 break;\r
964 }\r
965\r
966 target_nt[1] = 0;\r
967 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j);\r
968 break;\r
969 }\r
970 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i+1, j);\r
971 }\r
972 }\r
973 if (target_nt[i] == 0 && j == dmax+1 && MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i+1);\r
974 }\r
975 }\r
976\r
977 LED_C_OFF();\r
978\r
979 // ----------------------------- crypto1 destroy\r
980 crypto1_destroy(pcs);\r
981\r
982 uint8_t buf[4 + 4 * 4 + 4];\r
983 memcpy(buf, &cuid, 4);\r
984 memcpy(buf+4, &target_nt[0], 4);\r
985 memcpy(buf+8, &target_ks[0], 4);\r
986 memcpy(buf+12, &target_nt[1], 4);\r
987 memcpy(buf+16, &target_ks[1], 4);\r
988 memcpy(buf+20, &authentication_timeout, 4);\r
989\r
990 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
991\r
992 if (MF_DBGLEVEL >= 3) DbpString("NESTED FINISHED");\r
993\r
994 LED_B_ON();\r
995 cmd_send(CMD_ACK, isOK, 0, targetBlockNo + (targetKeyType * 0x100), buf, sizeof(buf));\r
996 LED_B_OFF();\r
997\r
998 LEDsoff();\r
999}\r
1000\r
1001\r
1002//-----------------------------------------------------------------------------\r
1003// MIFARE check keys. key count up to 85.\r
1004//\r
1005//-----------------------------------------------------------------------------\r
1006void MifareChkKeys(uint16_t arg0, uint32_t arg1, uint8_t arg2, uint8_t *datain) {\r
1007\r
1008 uint8_t blockNo = arg0 & 0xff;\r
1009 uint8_t keyType = arg0 >> 8;\r
1010 bool clearTrace = arg1 & 0x01;\r
1011 bool multisectorCheck = arg1 & 0x02;\r
1012 bool init = arg1 & 0x04;\r
1013 bool drop_field = arg1 & 0x08;\r
1014 uint32_t auth_timeout = arg1 >> 16;\r
1015 uint8_t keyCount = arg2;\r
1016\r
1017 LED_A_ON();\r
1018\r
1019 if (init) {\r
1020 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
1021 }\r
1022 \r
1023 if (clearTrace) {\r
1024 clear_trace();\r
1025 }\r
1026 set_tracing(true);\r
1027\r
1028 // clear debug level. We are expecting lots of authentication failures...\r
1029 int OLD_MF_DBGLEVEL = MF_DBGLEVEL;\r
1030 MF_DBGLEVEL = MF_DBG_NONE;\r
1031\r
1032 int res = 0;\r
1033 if (multisectorCheck) {\r
1034 TKeyIndex keyIndex = {{0}};\r
1035 uint8_t sectorCnt = blockNo;\r
1036 res = MifareMultisectorChk(datain, keyCount, sectorCnt, keyType, &auth_timeout, OLD_MF_DBGLEVEL, &keyIndex);\r
1037 if (res >= 0) {\r
1038 cmd_send(CMD_ACK, 1, res, 0, keyIndex, 80);\r
1039 } else {\r
1040 cmd_send(CMD_ACK, 0, res, 0, NULL, 0);\r
1041 }\r
1042 } else { \r
1043 res = MifareChkBlockKeys(datain, keyCount, blockNo, keyType, &auth_timeout, OLD_MF_DBGLEVEL);\r
1044 if (res > 0) {\r
1045 cmd_send(CMD_ACK, 1, res, 0, datain + (res - 1) * 6, 6);\r
1046 } else {\r
1047 cmd_send(CMD_ACK, 0, res, 0, NULL, 0);\r
1048 }\r
1049 }\r
1050\r
1051 if (drop_field || res != 0) {\r
1052 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1053 LED_D_OFF();\r
1054 }\r
1055\r
1056 // restore debug level\r
1057 MF_DBGLEVEL = OLD_MF_DBGLEVEL;\r
1058\r
1059 LED_A_OFF();\r
1060}\r
1061\r
1062\r
1063//-----------------------------------------------------------------------------\r
1064// MIFARE Personalize UID. Only for Mifare Classic EV1 7Byte UID\r
1065//-----------------------------------------------------------------------------\r
1066void MifarePersonalizeUID(uint8_t keyType, uint8_t perso_option, uint8_t *data) {\r
1067\r
1068 uint8_t uid[10];\r
1069 uint32_t cuid;\r
1070 struct Crypto1State mpcs = {0, 0};\r
1071 struct Crypto1State *pcs;\r
1072 pcs = &mpcs;\r
1073\r
1074 LED_A_ON();\r
1075 clear_trace();\r
1076\r
1077 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
1078\r
1079 bool isOK = false;\r
1080 while (true) {\r
1081 if (!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {\r
1082 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
1083 break;\r
1084 }\r
1085\r
1086 uint8_t block_number = 0;\r
1087 uint64_t key = bytes_to_num(data, 6);\r
1088 if (mifare_classic_auth(pcs, cuid, block_number, keyType, key, AUTH_FIRST, NULL)) {\r
1089 if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");\r
1090 break;\r
1091 }\r
1092\r
1093 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
1094 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
1095 int len = mifare_sendcmd_short(pcs, true, MIFARE_EV1_PERSONAL_UID, perso_option, receivedAnswer, receivedAnswerPar, NULL);\r
1096 if (len != 1 || receivedAnswer[0] != CARD_ACK) {\r
1097 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);\r
1098 break;;\r
1099 }\r
1100 isOK = true;\r
1101 break;\r
1102 }\r
1103\r
1104 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1105 LED_D_OFF();\r
1106\r
1107 crypto1_destroy(pcs);\r
1108\r
1109 if (MF_DBGLEVEL >= 2) DbpString("PERSONALIZE UID FINISHED");\r
1110\r
1111 cmd_send(CMD_ACK, isOK, 0, 0, NULL, 0);\r
1112\r
1113 LED_A_OFF();\r
1114}\r
1115\r
1116//-----------------------------------------------------------------------------\r
1117// MIFARE commands set debug level\r
1118//\r
1119//-----------------------------------------------------------------------------\r
1120void MifareSetDbgLvl(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
1121 MF_DBGLEVEL = arg0;\r
1122 Dbprintf("Debug level: %d", MF_DBGLEVEL);\r
1123}\r
1124\r
1125//-----------------------------------------------------------------------------\r
1126// Work with emulator memory\r
1127//\r
1128// Note: we call FpgaDownloadAndGo(FPGA_BITSTREAM_HF) here although FPGA is not\r
1129// involved in dealing with emulator memory. But if it is called later, it might\r
1130// destroy the Emulator Memory.\r
1131//-----------------------------------------------------------------------------\r
1132\r
1133void MifareEMemClr(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
1134 FpgaDownloadAndGo(FPGA_BITSTREAM_HF);\r
1135 emlClearMem();\r
1136}\r
1137\r
1138void MifareEMemSet(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
1139 FpgaDownloadAndGo(FPGA_BITSTREAM_HF);\r
1140 emlSetMem(datain, arg0, arg1); // data, block num, blocks count\r
1141}\r
1142\r
1143void MifareEMemGet(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
1144 FpgaDownloadAndGo(FPGA_BITSTREAM_HF);\r
1145 byte_t buf[USB_CMD_DATA_SIZE];\r
1146 emlGetMem(buf, arg0, arg1); // data, block num, blocks count (max 4)\r
1147\r
1148 LED_B_ON();\r
1149 cmd_send(CMD_ACK,arg0,arg1,0,buf,USB_CMD_DATA_SIZE);\r
1150 LED_B_OFF();\r
1151}\r
1152\r
1153//-----------------------------------------------------------------------------\r
1154// Load a card into the emulator memory\r
1155//\r
1156//-----------------------------------------------------------------------------\r
1157void MifareECardLoad(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
1158 uint8_t numSectors = arg0;\r
1159 uint8_t keyType = arg1;\r
1160 uint64_t ui64Key = 0;\r
1161 uint32_t cuid;\r
1162 struct Crypto1State mpcs = {0, 0};\r
1163 struct Crypto1State *pcs;\r
1164 pcs = &mpcs;\r
1165\r
1166 // variables\r
1167 byte_t dataoutbuf[16];\r
1168 byte_t dataoutbuf2[16];\r
1169 uint8_t uid[10];\r
1170\r
1171 LED_A_ON();\r
1172 LED_B_OFF();\r
1173 LED_C_OFF();\r
1174 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
1175\r
1176 clear_trace();\r
1177 set_tracing(false);\r
1178\r
1179 bool isOK = true;\r
1180\r
1181 if(!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {\r
1182 isOK = false;\r
1183 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
1184 }\r
1185\r
1186 for (uint8_t sectorNo = 0; isOK && sectorNo < numSectors; sectorNo++) {\r
1187 ui64Key = emlGetKey(sectorNo, keyType);\r
1188 if (sectorNo == 0){\r
1189 if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_FIRST, NULL)) {\r
1190 isOK = false;\r
1191 if (MF_DBGLEVEL >= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo);\r
1192 break;\r
1193 }\r
1194 } else {\r
1195 if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_NESTED, NULL)) {\r
1196 isOK = false;\r
1197 if (MF_DBGLEVEL >= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo);\r
1198 break;\r
1199 }\r
1200 }\r
1201\r
1202 for (uint8_t blockNo = 0; isOK && blockNo < NumBlocksPerSector(sectorNo); blockNo++) {\r
1203 if(isOK && mifare_classic_readblock(pcs, cuid, FirstBlockOfSector(sectorNo) + blockNo, dataoutbuf)) {\r
1204 isOK = false;\r
1205 if (MF_DBGLEVEL >= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo, blockNo);\r
1206 break;\r
1207 };\r
1208 if (isOK) {\r
1209 if (blockNo < NumBlocksPerSector(sectorNo) - 1) {\r
1210 emlSetMem(dataoutbuf, FirstBlockOfSector(sectorNo) + blockNo, 1);\r
1211 } else { // sector trailer, keep the keys, set only the AC\r
1212 emlGetMem(dataoutbuf2, FirstBlockOfSector(sectorNo) + blockNo, 1);\r
1213 memcpy(&dataoutbuf2[6], &dataoutbuf[6], 4);\r
1214 emlSetMem(dataoutbuf2, FirstBlockOfSector(sectorNo) + blockNo, 1);\r
1215 }\r
1216 }\r
1217 }\r
1218\r
1219 }\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 // ----------------------------- crypto1 destroy\r
1226 crypto1_destroy(pcs);\r
1227\r
1228 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1229 LEDsoff();\r
1230\r
1231 if (MF_DBGLEVEL >= 2) DbpString("EMUL FILL SECTORS FINISHED");\r
1232\r
1233}\r
1234\r
1235\r
1236//-----------------------------------------------------------------------------\r
1237// Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)\r
1238//\r
1239//-----------------------------------------------------------------------------\r
1240\r
1241static bool isBlockTrailer(int blockN) {\r
1242 if (blockN >= 0 && blockN < 128) {\r
1243 return ((blockN & 0x03) == 0x03);\r
1244 }\r
1245 if (blockN >= 128 && blockN <= 256) {\r
1246 return ((blockN & 0x0F) == 0x0F);\r
1247 }\r
1248 return false;\r
1249}\r
1250\r
1251void MifareCWipe(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
1252 // var\r
1253 byte_t isOK = 0;\r
1254 uint32_t numBlocks = arg0;\r
1255 // cmdParams:\r
1256 // bit 0 - wipe gen1a\r
1257 // bit 1 - fill card with default data\r
1258 // bit 2 - gen1a = 0, gen1b = 1\r
1259 uint8_t cmdParams = arg1;\r
1260 bool needWipe = cmdParams & 0x01;\r
1261 bool needFill = cmdParams & 0x02;\r
1262 bool gen1b = cmdParams & 0x04;\r
1263\r
1264 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
1265 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
1266\r
1267 uint8_t block0[16] = {0x01, 0x02, 0x03, 0x04, 0x04, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBE, 0xAF};\r
1268 uint8_t block1[16] = {0x00};\r
1269 uint8_t blockK[16] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x08, 0x77, 0x8F, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};\r
1270 uint8_t d_block[18] = {0x00};\r
1271\r
1272 // card commands\r
1273 uint8_t wupC1[] = { 0x40 };\r
1274 uint8_t wupC2[] = { 0x43 };\r
1275 uint8_t wipeC[] = { 0x41 };\r
1276\r
1277 // iso14443 setup\r
1278 LED_A_ON();\r
1279 LED_B_OFF();\r
1280 LED_C_OFF();\r
1281 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
1282\r
1283 // tracing\r
1284 clear_trace();\r
1285 set_tracing(true);\r
1286\r
1287 while (true){\r
1288 // wipe\r
1289 if (needWipe){\r
1290 ReaderTransmitBitsPar(wupC1,7,0, NULL);\r
1291 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != CARD_ACK)) {\r
1292 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");\r
1293 break;\r
1294 };\r
1295\r
1296 ReaderTransmit(wipeC, sizeof(wipeC), NULL);\r
1297 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != CARD_ACK)) {\r
1298 if (MF_DBGLEVEL >= 1) Dbprintf("wipeC error");\r
1299 break;\r
1300 };\r
1301\r
1302 if(mifare_classic_halt(NULL, 0)) {\r
1303 if (MF_DBGLEVEL > 2) Dbprintf("Halt error");\r
1304 };\r
1305 };\r
1306\r
1307 // put default data\r
1308 if (needFill){\r
1309 // select commands\r
1310 ReaderTransmitBitsPar(wupC1, 7, 0, NULL);\r
1311\r
1312 // gen1b magic tag : do no issue wupC2 and don't expect CARD_ACK response after SELECT_UID (after getting UID from chip in 'hf mf csetuid' command)\r
1313 if (!gen1b) {\r
1314\r
1315 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != CARD_ACK)) {\r
1316 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");\r
1317 break;\r
1318 };\r
1319\r
1320 ReaderTransmit(wupC2, sizeof(wupC2), NULL);\r
1321 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != CARD_ACK)) {\r
1322 if (MF_DBGLEVEL >= 1) Dbprintf("wupC2 error");\r
1323 break;\r
1324 };\r
1325 }\r
1326\r
1327 // send blocks command\r
1328 for (int blockNo = 0; blockNo < numBlocks; blockNo++) {\r
1329 if ((mifare_sendcmd_short(NULL, 0, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL) != 1) || (receivedAnswer[0] != CARD_ACK)) {\r
1330 if (MF_DBGLEVEL >= 1) Dbprintf("write block send command error");\r
1331 break;\r
1332 };\r
1333\r
1334 // check type of block and add crc\r
1335 if (!isBlockTrailer(blockNo)){\r
1336 memcpy(d_block, block1, 16);\r
1337 } else {\r
1338 memcpy(d_block, blockK, 16);\r
1339 }\r
1340 if (blockNo == 0) {\r
1341 memcpy(d_block, block0, 16);\r
1342 }\r
1343 AppendCrc14443a(d_block, 16);\r
1344\r
1345 // send write command\r
1346 ReaderTransmit(d_block, sizeof(d_block), NULL);\r
1347 if ((ReaderReceive(receivedAnswer, receivedAnswerPar) != 1) || (receivedAnswer[0] != CARD_ACK)) {\r
1348 if (MF_DBGLEVEL >= 1) Dbprintf("write block send data error");\r
1349 break;\r
1350 };\r
1351 }\r
1352\r
1353 // halt\r
1354 // do no issue halt command for gen1b\r
1355 if (!gen1b) {\r
1356 if (mifare_classic_halt(NULL, 0)) {\r
1357 if (MF_DBGLEVEL > 2) Dbprintf("Halt error");\r
1358 break;\r
1359 }\r
1360 }\r
1361 }\r
1362 break;\r
1363 }\r
1364\r
1365 // reset fpga\r
1366 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1367\r
1368 // send USB response\r
1369 LED_B_ON();\r
1370 cmd_send(CMD_ACK,isOK,0,0,NULL,0);\r
1371 LED_B_OFF();\r
1372\r
1373 LEDsoff();\r
1374\r
1375 return;\r
1376}\r
1377\r
1378void MifareCSetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
1379\r
1380 // params\r
1381 uint8_t needWipe = arg0;\r
1382 // bit 0 - need get UID\r
1383 // bit 1 - need wupC\r
1384 // bit 2 - need HALT after sequence\r
1385 // bit 3 - need init FPGA and field before sequence\r
1386 // bit 4 - need reset FPGA and LED\r
1387 // bit 6 - gen1b backdoor type\r
1388 uint8_t workFlags = arg1;\r
1389 uint8_t blockNo = arg2;\r
1390\r
1391 // card commands\r
1392 uint8_t wupC1[] = { 0x40 };\r
1393 uint8_t wupC2[] = { 0x43 };\r
1394 uint8_t wipeC[] = { 0x41 };\r
1395\r
1396 // variables\r
1397 byte_t isOK = 0;\r
1398 uint8_t uid[10] = {0x00};\r
1399 uint8_t d_block[18] = {0x00};\r
1400 uint32_t cuid;\r
1401\r
1402 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
1403 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
1404\r
1405 // reset FPGA and LED\r
1406 if (workFlags & 0x08) {\r
1407 LED_A_ON();\r
1408 LED_B_OFF();\r
1409 LED_C_OFF();\r
1410 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
1411\r
1412 clear_trace();\r
1413 set_tracing(true);\r
1414 }\r
1415\r
1416 while (true) {\r
1417\r
1418 // get UID from chip\r
1419 if (workFlags & 0x01) {\r
1420 if(!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {\r
1421 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
1422 // Continue, if we set wrong UID or wrong UID checksum or some ATQA or SAK we will can't select card. But we need to write block 0 to make card work.\r
1423 //break;\r
1424 };\r
1425\r
1426 if(mifare_classic_halt(NULL, cuid)) {\r
1427 if (MF_DBGLEVEL > 2) Dbprintf("Halt error");\r
1428 // Continue, some magic tags misbehavies and send an answer to it.\r
1429 // break;\r
1430 };\r
1431 };\r
1432\r
1433 // reset chip\r
1434 // Wipe command don't work with gen1b\r
1435 if (needWipe && !(workFlags & 0x40)){\r
1436 ReaderTransmitBitsPar(wupC1,7,0, NULL);\r
1437 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != CARD_ACK)) {\r
1438 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");\r
1439 break;\r
1440 };\r
1441\r
1442 ReaderTransmit(wipeC, sizeof(wipeC), NULL);\r
1443 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != CARD_ACK)) {\r
1444 if (MF_DBGLEVEL >= 1) Dbprintf("wipeC error");\r
1445 break;\r
1446 };\r
1447\r
1448 if(mifare_classic_halt(NULL, 0)) {\r
1449 if (MF_DBGLEVEL > 2) Dbprintf("Halt error");\r
1450 // Continue, some magic tags misbehavies and send an answer to it.\r
1451 // break;\r
1452 };\r
1453 };\r
1454\r
1455 // write block\r
1456 if (workFlags & 0x02) {\r
1457 ReaderTransmitBitsPar(wupC1,7,0, NULL);\r
1458\r
1459 // gen1b magic tag : do no issue wupC2 and don't expect CARD_ACK response after SELECT_UID (after getting UID from chip in 'hf mf csetuid' command)\r
1460 if (!(workFlags & 0x40)) {\r
1461\r
1462 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != CARD_ACK)) {\r
1463 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");\r
1464 break;\r
1465 };\r
1466\r
1467 ReaderTransmit(wupC2, sizeof(wupC2), NULL);\r
1468 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != CARD_ACK)) {\r
1469 if (MF_DBGLEVEL >= 1) Dbprintf("wupC2 error");\r
1470 break;\r
1471 };\r
1472 }\r
1473 }\r
1474\r
1475 if ((mifare_sendcmd_short(NULL, 0, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL) != 1) || (receivedAnswer[0] != CARD_ACK)) {\r
1476 if (MF_DBGLEVEL >= 1) Dbprintf("write block send command error");\r
1477 break;\r
1478 };\r
1479\r
1480 memcpy(d_block, datain, 16);\r
1481 AppendCrc14443a(d_block, 16);\r
1482\r
1483 ReaderTransmit(d_block, sizeof(d_block), NULL);\r
1484 if ((ReaderReceive(receivedAnswer, receivedAnswerPar) != 1) || (receivedAnswer[0] != CARD_ACK)) {\r
1485 if (MF_DBGLEVEL >= 1) Dbprintf("write block send data error");\r
1486 break;\r
1487 };\r
1488\r
1489 if (workFlags & 0x04) {\r
1490 // do no issue halt command for gen1b magic tag (#db# halt error. response len: 1)\r
1491 if (!(workFlags & 0x40)) {\r
1492 if (mifare_classic_halt(NULL, 0)) {\r
1493 if (MF_DBGLEVEL > 2) Dbprintf("Halt error");\r
1494 // Continue, some magic tags misbehavies and send an answer to it.\r
1495 // break;\r
1496 }\r
1497 }\r
1498 }\r
1499\r
1500 isOK = 1;\r
1501 break;\r
1502 }\r
1503\r
1504 if ((workFlags & 0x10) || (!isOK)) {\r
1505 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1506 }\r
1507\r
1508 LED_B_ON();\r
1509 cmd_send(CMD_ACK,isOK,0,0,uid,4);\r
1510 LED_B_OFF();\r
1511\r
1512 LEDsoff();\r
1513}\r
1514\r
1515\r
1516void MifareCGetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
1517\r
1518 // params\r
1519 // bit 1 - need wupC\r
1520 // bit 2 - need HALT after sequence\r
1521 // bit 3 - need init FPGA and field before sequence\r
1522 // bit 4 - need reset FPGA and LED\r
1523 // bit 5 - need to set datain instead of issuing USB reply (called via ARM for StandAloneMode14a)\r
1524 // bit 6 - gen1b backdoor type\r
1525 uint8_t workFlags = arg0;\r
1526 uint8_t blockNo = arg2;\r
1527\r
1528 // card commands\r
1529 uint8_t wupC1[] = { 0x40 };\r
1530 uint8_t wupC2[] = { 0x43 };\r
1531\r
1532 // variables\r
1533 byte_t isOK = 0;\r
1534 uint8_t data[18] = {0x00};\r
1535 uint32_t cuid = 0;\r
1536\r
1537 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
1538 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
1539\r
1540 if (workFlags & 0x08) {\r
1541 LED_A_ON();\r
1542 LED_B_OFF();\r
1543 LED_C_OFF();\r
1544 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
1545\r
1546 clear_trace();\r
1547 set_tracing(true);\r
1548 }\r
1549\r
1550 while (true) {\r
1551 if (workFlags & 0x02) {\r
1552 ReaderTransmitBitsPar(wupC1,7,0, NULL);\r
1553 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != CARD_ACK)) {\r
1554 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");\r
1555 break;\r
1556 };\r
1557 // do no issue for gen1b magic tag\r
1558 if (!(workFlags & 0x40)) {\r
1559 ReaderTransmit(wupC2, sizeof(wupC2), NULL);\r
1560 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != CARD_ACK)) {\r
1561 if (MF_DBGLEVEL >= 1) Dbprintf("wupC2 error");\r
1562 break;\r
1563 };\r
1564 }\r
1565 }\r
1566\r
1567 // read block\r
1568 if ((mifare_sendcmd_short(NULL, 0, 0x30, blockNo, receivedAnswer, receivedAnswerPar, NULL) != 18)) {\r
1569 if (MF_DBGLEVEL >= 1) Dbprintf("read block send command error");\r
1570 break;\r
1571 };\r
1572 memcpy(data, receivedAnswer, 18);\r
1573\r
1574 if (workFlags & 0x04) {\r
1575 // do no issue halt command for gen1b magic tag (#db# halt error. response len: 1)\r
1576 if (!(workFlags & 0x40)) {\r
1577 if (mifare_classic_halt(NULL, cuid)) {\r
1578 if (MF_DBGLEVEL > 1) Dbprintf("Halt error");\r
1579 // Continue, some magic tags misbehavies and send an answer to it.\r
1580 // break;\r
1581 }\r
1582 }\r
1583 }\r
1584\r
1585 isOK = 1;\r
1586 break;\r
1587 }\r
1588\r
1589 if ((workFlags & 0x10) || (!isOK)) {\r
1590 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1591 }\r
1592\r
1593 LED_B_ON();\r
1594 if (workFlags & 0x20) {\r
1595 if (isOK)\r
1596 memcpy(datain, data, 18);\r
1597 }\r
1598 else\r
1599 cmd_send(CMD_ACK,isOK,0,0,data,18);\r
1600 LED_B_OFF();\r
1601\r
1602 LEDsoff();\r
1603}\r
1604\r
1605void MifareCIdent(){\r
1606\r
1607 // card commands\r
1608 uint8_t wupC1[] = { 0x40 };\r
1609 uint8_t wupC2[] = { 0x43 };\r
1610\r
1611 // variables\r
1612 byte_t isOK = 0;\r
1613\r
1614 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
1615 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
1616\r
1617 LED_A_ON();\r
1618 LED_B_OFF();\r
1619 LED_C_OFF();\r
1620 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
1621\r
1622 clear_trace();\r
1623 set_tracing(true);\r
1624\r
1625 ReaderTransmitBitsPar(wupC1,7,0, NULL);\r
1626 if(ReaderReceive(receivedAnswer, receivedAnswerPar) && (receivedAnswer[0] == CARD_ACK)) {\r
1627 isOK = 2;\r
1628\r
1629 ReaderTransmit(wupC2, sizeof(wupC2), NULL);\r
1630 if(ReaderReceive(receivedAnswer, receivedAnswerPar) && (receivedAnswer[0] == CARD_ACK)) {\r
1631 isOK = 1;\r
1632 };\r
1633 };\r
1634\r
1635 // From iceman1001: removed the if, since some magic tags misbehavies and send an answer to it.\r
1636 mifare_classic_halt(NULL, 0);\r
1637\r
1638 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1639\r
1640 LED_B_ON();\r
1641 cmd_send(CMD_ACK,isOK,0,0,0,0);\r
1642 LED_B_OFF();\r
1643\r
1644 LEDsoff();\r
1645}\r
1646\r
1647//\r
1648// DESFIRE\r
1649//\r
1650\r
1651void Mifare_DES_Auth1(uint8_t arg0, uint8_t *datain){\r
1652\r
1653 byte_t dataout[11] = {0x00};\r
1654 uint8_t uid[10] = {0x00};\r
1655 uint32_t cuid;\r
1656\r
1657 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
1658 clear_trace();\r
1659\r
1660 int len = iso14443a_select_card(uid, NULL, &cuid, true, 0, true);\r
1661 if(!len) {\r
1662 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card");\r
1663 OnError(1);\r
1664 return;\r
1665 };\r
1666\r
1667 if(mifare_desfire_des_auth1(cuid, dataout)){\r
1668 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Authentication part1: Fail.");\r
1669 OnError(4);\r
1670 return;\r
1671 }\r
1672\r
1673 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) DbpString("AUTH 1 FINISHED");\r
1674\r
1675 cmd_send(CMD_ACK, 1, cuid, 0, dataout, sizeof(dataout));\r
1676}\r
1677\r
1678void Mifare_DES_Auth2(uint32_t arg0, uint8_t *datain){\r
1679\r
1680 uint32_t cuid = arg0;\r
1681 uint8_t key[16] = {0x00};\r
1682 byte_t isOK = 0;\r
1683 byte_t dataout[12] = {0x00};\r
1684\r
1685 memcpy(key, datain, 16);\r
1686\r
1687 isOK = mifare_desfire_des_auth2(cuid, key, dataout);\r
1688\r
1689 if (isOK) {\r
1690 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) Dbprintf("Authentication part2: Failed");\r
1691 OnError(4);\r
1692 return;\r
1693 }\r
1694\r
1695 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1696\r
1697 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) DbpString("AUTH 2 FINISHED");\r
1698\r
1699 cmd_send(CMD_ACK, isOK, 0, 0, dataout, sizeof(dataout));\r
1700 LEDsoff();\r
1701}\r
1702\r
Impressum, Datenschutz