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