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
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
12 //-----------------------------------------------------------------------------
13 // Routines to support ISO 14443 type A.
14 //-----------------------------------------------------------------------------
16 #include "mifarecmd.h"
23 // the block number for the ISO14443-4 PCB
24 static uint8_t pcb_blocknum
= 0;
25 // Deselect card by sending a s-block. the crc is precalced for speed
26 static uint8_t deselect_cmd
[] = {0xc2,0xe0,0xb4};
28 //-----------------------------------------------------------------------------
29 // Select, Authenticate, Read a MIFARE tag.
31 //-----------------------------------------------------------------------------
32 void MifareReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
35 uint8_t blockNo
= arg0
;
36 uint8_t keyType
= arg1
;
38 ui64Key
= bytes_to_num(datain
, 6);
42 byte_t dataoutbuf
[16];
45 struct Crypto1State mpcs
= {0, 0};
46 struct Crypto1State
*pcs
;
49 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
58 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
59 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
63 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
64 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
68 if(mifare_classic_readblock(pcs
, cuid
, blockNo
, dataoutbuf
)) {
69 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block error");
73 if(mifare_classic_halt(pcs
, cuid
)) {
74 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
82 // ----------------------------- crypto1 destroy
85 if (MF_DBGLEVEL
>= 2) DbpString("READ BLOCK FINISHED");
88 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16);
91 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
95 void MifareUC_Auth(uint8_t arg0
, uint8_t *keybytes
){
97 bool turnOffField
= (arg0
== 1);
99 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
101 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
105 if(!iso14443a_select_card(NULL
, NULL
, NULL
, true, 0, true)) {
106 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
111 if(!mifare_ultra_auth(keybytes
)){
112 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication failed");
118 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
121 cmd_send(CMD_ACK
,1,0,0,0,0);
125 // Arg1 = UsePwd bool
126 // datain = PWD bytes,
127 void MifareUReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
)
129 uint8_t blockNo
= arg0
;
130 byte_t dataout
[16] = {0x00};
131 bool useKey
= (arg1
== 1); //UL_C
132 bool usePwd
= (arg1
== 2); //UL_EV1/NTAG
136 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
140 int len
= iso14443a_select_card(NULL
, NULL
, NULL
, true, 0, true);
142 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%02X)",len
);
147 // UL-C authentication
149 uint8_t key
[16] = {0x00};
150 memcpy(key
, datain
, sizeof(key
) );
152 if ( !mifare_ultra_auth(key
) ) {
158 // UL-EV1 / NTAG authentication
160 uint8_t pwd
[4] = {0x00};
161 memcpy(pwd
, datain
, 4);
162 uint8_t pack
[4] = {0,0,0,0};
163 if (!mifare_ul_ev1_auth(pwd
, pack
)) {
169 if( mifare_ultra_readblock(blockNo
, dataout
) ) {
170 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Read block error");
175 if( mifare_ultra_halt() ) {
176 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Halt error");
181 cmd_send(CMD_ACK
,1,0,0,dataout
,16);
182 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
186 //-----------------------------------------------------------------------------
187 // Select, Authenticate, Read a MIFARE tag.
188 // read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)
189 //-----------------------------------------------------------------------------
190 void MifareReadSector(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
193 uint8_t sectorNo
= arg0
;
194 uint8_t keyType
= arg1
;
195 uint64_t ui64Key
= 0;
196 ui64Key
= bytes_to_num(datain
, 6);
200 byte_t dataoutbuf
[16 * 16];
203 struct Crypto1State mpcs
= {0, 0};
204 struct Crypto1State
*pcs
;
207 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
216 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
218 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
222 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
224 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
227 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
228 if(mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
+ 16 * blockNo
)) {
230 if (MF_DBGLEVEL
>= 1) Dbprintf("Read sector %2d block %2d error", sectorNo
, blockNo
);
235 if(mifare_classic_halt(pcs
, cuid
)) {
236 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
239 // ----------------------------- crypto1 destroy
240 crypto1_destroy(pcs
);
242 if (MF_DBGLEVEL
>= 2) DbpString("READ SECTOR FINISHED");
245 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16*NumBlocksPerSector(sectorNo
));
249 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
253 // arg0 = blockNo (start)
254 // arg1 = Pages (number of blocks)
256 // datain = KEY bytes
257 void MifareUReadCard(uint8_t arg0
, uint16_t arg1
, uint8_t arg2
, uint8_t *datain
)
261 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
263 // free eventually allocated BigBuf memory
268 uint8_t blockNo
= arg0
;
269 uint16_t blocks
= arg1
;
270 bool useKey
= (arg2
== 1); //UL_C
271 bool usePwd
= (arg2
== 2); //UL_EV1/NTAG
272 uint32_t countblocks
= 0;
273 uint8_t *dataout
= BigBuf_malloc(CARD_MEMORY_SIZE
);
274 if (dataout
== NULL
){
275 Dbprintf("out of memory");
280 int len
= iso14443a_select_card(NULL
, NULL
, NULL
, true, 0, true);
282 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%d)",len
);
287 // UL-C authentication
289 uint8_t key
[16] = {0x00};
290 memcpy(key
, datain
, sizeof(key
) );
292 if ( !mifare_ultra_auth(key
) ) {
298 // UL-EV1 / NTAG authentication
300 uint8_t pwd
[4] = {0x00};
301 memcpy(pwd
, datain
, sizeof(pwd
));
302 uint8_t pack
[4] = {0,0,0,0};
304 if (!mifare_ul_ev1_auth(pwd
, pack
)){
310 for (int i
= 0; i
< blocks
; i
++){
311 if ((i
*4) + 4 >= CARD_MEMORY_SIZE
) {
312 Dbprintf("Data exceeds buffer!!");
316 len
= mifare_ultra_readblock(blockNo
+ i
, dataout
+ 4 * i
);
319 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Read block %d error",i
);
320 // if no blocks read - error out
325 //stop at last successful read block and return what we got
333 len
= mifare_ultra_halt();
335 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Halt error");
340 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) Dbprintf("Blocks read %d", countblocks
);
344 cmd_send(CMD_ACK
, 1, countblocks
, BigBuf_max_traceLen(), 0, 0);
345 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
350 //-----------------------------------------------------------------------------
351 // Select, Authenticate, Write a MIFARE tag.
353 //-----------------------------------------------------------------------------
354 void MifareWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
357 uint8_t blockNo
= arg0
;
358 uint8_t keyType
= arg1
;
359 uint64_t ui64Key
= 0;
360 byte_t blockdata
[16];
362 ui64Key
= bytes_to_num(datain
, 6);
363 memcpy(blockdata
, datain
+ 10, 16);
369 struct Crypto1State mpcs
= {0, 0};
370 struct Crypto1State
*pcs
;
373 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
382 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
383 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
387 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
388 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
392 if(mifare_classic_writeblock(pcs
, cuid
, blockNo
, blockdata
)) {
393 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
397 if(mifare_classic_halt(pcs
, cuid
)) {
398 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
406 // ----------------------------- crypto1 destroy
407 crypto1_destroy(pcs
);
409 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
412 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
417 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
421 /* // Command not needed but left for future testing
422 void MifareUWriteBlockCompat(uint8_t arg0, uint8_t *datain)
424 uint8_t blockNo = arg0;
425 byte_t blockdata[16] = {0x00};
427 memcpy(blockdata, datain, 16);
429 uint8_t uid[10] = {0x00};
431 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
434 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
436 if(!iso14443a_select_card(uid, NULL, NULL, true, 0)) {
437 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
442 if(mifare_ultra_writeblock_compat(blockNo, blockdata)) {
443 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
447 if(mifare_ultra_halt()) {
448 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
453 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");
455 cmd_send(CMD_ACK,1,0,0,0,0);
456 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
461 // Arg0 : Block to write to.
462 // Arg1 : 0 = use no authentication.
463 // 1 = use 0x1A authentication.
464 // 2 = use 0x1B authentication.
465 // datain : 4 first bytes is data to be written.
466 // : 4/16 next bytes is authentication key.
467 void MifareUWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
)
469 uint8_t blockNo
= arg0
;
470 bool useKey
= (arg1
== 1); //UL_C
471 bool usePwd
= (arg1
== 2); //UL_EV1/NTAG
472 byte_t blockdata
[4] = {0x00};
474 memcpy(blockdata
, datain
,4);
478 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
482 if(!iso14443a_select_card(NULL
, NULL
, NULL
, true, 0, true)) {
483 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
488 // UL-C authentication
490 uint8_t key
[16] = {0x00};
491 memcpy(key
, datain
+4, sizeof(key
) );
493 if ( !mifare_ultra_auth(key
) ) {
499 // UL-EV1 / NTAG authentication
501 uint8_t pwd
[4] = {0x00};
502 memcpy(pwd
, datain
+4, 4);
503 uint8_t pack
[4] = {0,0,0,0};
504 if (!mifare_ul_ev1_auth(pwd
, pack
)) {
510 if(mifare_ultra_writeblock(blockNo
, blockdata
)) {
511 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
516 if(mifare_ultra_halt()) {
517 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
522 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
524 cmd_send(CMD_ACK
,1,0,0,0,0);
525 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
529 void MifareUSetPwd(uint8_t arg0
, uint8_t *datain
){
531 uint8_t pwd
[16] = {0x00};
532 byte_t blockdata
[4] = {0x00};
534 memcpy(pwd
, datain
, 16);
536 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
537 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
541 if(!iso14443a_select_card(NULL
, NULL
, NULL
, true, 0, true)) {
542 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
547 blockdata
[0] = pwd
[7];
548 blockdata
[1] = pwd
[6];
549 blockdata
[2] = pwd
[5];
550 blockdata
[3] = pwd
[4];
551 if(mifare_ultra_writeblock( 44, blockdata
)) {
552 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
557 blockdata
[0] = pwd
[3];
558 blockdata
[1] = pwd
[2];
559 blockdata
[2] = pwd
[1];
560 blockdata
[3] = pwd
[0];
561 if(mifare_ultra_writeblock( 45, blockdata
)) {
562 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
567 blockdata
[0] = pwd
[15];
568 blockdata
[1] = pwd
[14];
569 blockdata
[2] = pwd
[13];
570 blockdata
[3] = pwd
[12];
571 if(mifare_ultra_writeblock( 46, blockdata
)) {
572 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
577 blockdata
[0] = pwd
[11];
578 blockdata
[1] = pwd
[10];
579 blockdata
[2] = pwd
[9];
580 blockdata
[3] = pwd
[8];
581 if(mifare_ultra_writeblock( 47, blockdata
)) {
582 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
587 if(mifare_ultra_halt()) {
588 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
593 cmd_send(CMD_ACK
,1,0,0,0,0);
594 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
598 // Return 1 if the nonce is invalid else return 0
599 int valid_nonce(uint32_t Nt
, uint32_t NtEnc
, uint32_t Ks1
, uint8_t *parity
) {
600 return ((oddparity8((Nt
>> 24) & 0xFF) == ((parity
[0]) ^ oddparity8((NtEnc
>> 24) & 0xFF) ^ BIT(Ks1
,16))) & \
601 (oddparity8((Nt
>> 16) & 0xFF) == ((parity
[1]) ^ oddparity8((NtEnc
>> 16) & 0xFF) ^ BIT(Ks1
,8))) & \
602 (oddparity8((Nt
>> 8) & 0xFF) == ((parity
[2]) ^ oddparity8((NtEnc
>> 8) & 0xFF) ^ BIT(Ks1
,0)))) ? 1 : 0;
606 //-----------------------------------------------------------------------------
607 // acquire encrypted nonces in order to perform the attack described in
608 // Carlo Meijer, Roel Verdult, "Ciphertext-only Cryptanalysis on Hardened
609 // Mifare Classic Cards" in Proceedings of the 22nd ACM SIGSAC Conference on
610 // Computer and Communications Security, 2015
611 //-----------------------------------------------------------------------------
612 void MifareAcquireEncryptedNonces(uint32_t arg0
, uint32_t arg1
, uint32_t flags
, uint8_t *datain
)
614 uint64_t ui64Key
= 0;
617 uint8_t cascade_levels
= 0;
618 struct Crypto1State mpcs
= {0, 0};
619 struct Crypto1State
*pcs
;
621 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
624 uint8_t nt_par_enc
= 0;
625 uint8_t buf
[USB_CMD_DATA_SIZE
];
628 uint8_t blockNo
= arg0
& 0xff;
629 uint8_t keyType
= (arg0
>> 8) & 0xff;
630 uint8_t targetBlockNo
= arg1
& 0xff;
631 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
632 ui64Key
= bytes_to_num(datain
, 6);
633 bool initialize
= flags
& 0x0001;
634 bool slow
= flags
& 0x0002;
635 bool field_off
= flags
& 0x0004;
641 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
648 uint16_t num_nonces
= 0;
649 bool have_uid
= false;
650 for (uint16_t i
= 0; i
<= USB_CMD_DATA_SIZE
- 9; ) {
652 // Test if the action was cancelled
659 if (!have_uid
) { // need a full select cycle to get the uid first
660 iso14a_card_select_t card_info
;
661 if(!iso14443a_select_card(uid
, &card_info
, &cuid
, true, 0, true)) {
662 if (MF_DBGLEVEL
>= 1) Dbprintf("AcquireNonces: Can't select card (ALL)");
665 switch (card_info
.uidlen
) {
666 case 4 : cascade_levels
= 1; break;
667 case 7 : cascade_levels
= 2; break;
668 case 10: cascade_levels
= 3; break;
672 } else { // no need for anticollision. We can directly select the card
673 if(!iso14443a_select_card(uid
, NULL
, NULL
, false, cascade_levels
, true)) {
674 if (MF_DBGLEVEL
>= 1) Dbprintf("AcquireNonces: Can't select card (UID)");
680 timeout
= GetCountSspClk() + PRE_AUTHENTICATION_LEADTIME
;
681 while(GetCountSspClk() < timeout
);
685 if (mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, NULL
)) {
686 if (MF_DBGLEVEL
>= 1) Dbprintf("AcquireNonces: Auth1 error");
690 // nested authentication
691 uint16_t len
= mifare_sendcmd_short(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType
& 0x01), targetBlockNo
, receivedAnswer
, par_enc
, NULL
);
693 if (MF_DBGLEVEL
>= 1) Dbprintf("AcquireNonces: Auth2 error len=%d", len
);
697 // send a dummy byte as reader response in order to trigger the cards authentication timeout
698 uint8_t dummy_answer
= 0;
699 ReaderTransmit(&dummy_answer
, 1, NULL
);
700 timeout
= GetCountSspClk() + AUTHENTICATION_TIMEOUT
;
703 if (num_nonces
% 2) {
704 memcpy(buf
+i
, receivedAnswer
, 4);
705 nt_par_enc
= par_enc
[0] & 0xf0;
707 nt_par_enc
|= par_enc
[0] >> 4;
708 memcpy(buf
+i
+4, receivedAnswer
, 4);
709 memcpy(buf
+i
+8, &nt_par_enc
, 1);
713 // wait for the card to become ready again
714 while(GetCountSspClk() < timeout
);
720 crypto1_destroy(pcs
);
723 cmd_send(CMD_ACK
, isOK
, cuid
, num_nonces
, buf
, sizeof(buf
));
726 if (MF_DBGLEVEL
>= 3) DbpString("AcquireEncryptedNonces finished");
729 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
735 //-----------------------------------------------------------------------------
736 // MIFARE nested authentication.
738 //-----------------------------------------------------------------------------
739 void MifareNested(uint32_t arg0
, uint32_t arg1
, uint32_t calibrate
, uint8_t *datain
)
742 uint8_t blockNo
= arg0
& 0xff;
743 uint8_t keyType
= (arg0
>> 8) & 0xff;
744 uint8_t targetBlockNo
= arg1
& 0xff;
745 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
746 uint64_t ui64Key
= 0;
748 ui64Key
= bytes_to_num(datain
, 6);
751 uint16_t rtr
, i
, j
, len
;
753 static uint16_t dmin
, dmax
;
755 uint32_t cuid
, nt1
, nt2
, nttmp
, nttest
, ks1
;
757 uint32_t target_nt
[2], target_ks
[2];
759 uint8_t par_array
[4];
761 struct Crypto1State mpcs
= {0, 0};
762 struct Crypto1State
*pcs
;
764 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
766 uint32_t auth1_time
, auth2_time
;
767 static uint16_t delta_time
;
771 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
773 // free eventually allocated BigBuf memory
776 if (calibrate
) clear_trace();
779 // statistics on nonce distance
781 #define NESTED_MAX_TRIES 12
782 uint16_t unsuccessfull_tries
= 0;
783 if (calibrate
) { // for first call only. Otherwise reuse previous calibration
791 for (rtr
= 0; rtr
< 17; rtr
++) {
793 // Test if the action was cancelled
799 // prepare next select. No need to power down the card.
800 if(mifare_classic_halt(pcs
, cuid
)) {
801 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
806 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
807 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
813 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
814 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
820 auth2_time
= auth1_time
+ delta_time
;
824 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_NESTED
, &nt2
, &auth2_time
)) {
825 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error");
830 nttmp
= prng_successor(nt1
, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160
831 for (i
= 101; i
< 1200; i
++) {
832 nttmp
= prng_successor(nttmp
, 1);
833 if (nttmp
== nt2
) break;
843 delta_time
= auth2_time
- auth1_time
+ 32; // allow some slack for proper timing
845 if (MF_DBGLEVEL
>= 3) Dbprintf("Nested: calibrating... ntdist=%d", i
);
847 unsuccessfull_tries
++;
848 if (unsuccessfull_tries
> NESTED_MAX_TRIES
) { // card isn't vulnerable to nested attack (random numbers are not predictable)
854 davg
= (davg
+ (rtr
- 1)/2) / (rtr
- 1);
856 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
);
864 // -------------------------------------------------------------------------------------------------
868 // get crypted nonces for target sector
869 for(i
=0; i
< 2 && !isOK
; i
++) { // look for exactly two different nonces
872 while(target_nt
[i
] == 0) { // continue until we have an unambiguous nonce
874 // prepare next select. No need to power down the card.
875 if(mifare_classic_halt(pcs
, cuid
)) {
876 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
880 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
881 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
886 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
887 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
891 // nested authentication
892 auth2_time
= auth1_time
+ delta_time
;
893 len
= mifare_sendcmd_short(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType
& 0x01), targetBlockNo
, receivedAnswer
, par
, &auth2_time
);
895 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error len=%d", len
);
899 nt2
= bytes_to_num(receivedAnswer
, 4);
900 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i
+1, nt1
, nt2
, par
[0]);
902 // Parity validity check
903 for (j
= 0; j
< 4; j
++) {
904 par_array
[j
] = (oddparity8(receivedAnswer
[j
]) != ((par
[0] >> (7-j
)) & 0x01));
908 nttest
= prng_successor(nt1
, dmin
- 1);
909 for (j
= dmin
; j
< dmax
+ 1; j
++) {
910 nttest
= prng_successor(nttest
, 1);
913 if (valid_nonce(nttest
, nt2
, ks1
, par_array
)){
914 if (ncount
> 0) { // we are only interested in disambiguous nonces, try again
915 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i
+1, j
);
919 target_nt
[i
] = nttest
;
922 if (i
== 1 && target_nt
[1] == target_nt
[0]) { // we need two different nonces
924 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j
);
927 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i
+1, j
);
930 if (target_nt
[i
] == 0 && j
== dmax
+1 && MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i
+1);
936 // ----------------------------- crypto1 destroy
937 crypto1_destroy(pcs
);
939 byte_t buf
[4 + 4 * 4];
940 memcpy(buf
, &cuid
, 4);
941 memcpy(buf
+4, &target_nt
[0], 4);
942 memcpy(buf
+8, &target_ks
[0], 4);
943 memcpy(buf
+12, &target_nt
[1], 4);
944 memcpy(buf
+16, &target_ks
[1], 4);
947 cmd_send(CMD_ACK
, isOK
, 0, targetBlockNo
+ (targetKeyType
* 0x100), buf
, sizeof(buf
));
950 if (MF_DBGLEVEL
>= 3) DbpString("NESTED FINISHED");
952 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
956 //-----------------------------------------------------------------------------
957 // MIFARE check keys. key count up to 85.
959 //-----------------------------------------------------------------------------
960 void MifareChkKeys(uint16_t arg0
, uint16_t arg1
, uint8_t arg2
, uint8_t *datain
)
962 uint8_t blockNo
= arg0
& 0xff;
963 uint8_t keyType
= (arg0
>> 8) & 0xff;
964 bool clearTrace
= arg1
& 0x01;
965 bool multisectorCheck
= arg1
& 0x02;
966 uint8_t set14aTimeout
= (arg1
>> 8) & 0xff;
967 uint8_t keyCount
= arg2
;
970 int OLD_MF_DBGLEVEL
= MF_DBGLEVEL
;
971 MF_DBGLEVEL
= MF_DBG_NONE
;
976 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
978 if (clearTrace
) clear_trace();
982 iso14a_set_timeout(set14aTimeout
* 10); // timeout: ms = x/106 35-minimum, 50-OK 106-recommended 500-safe
985 if (multisectorCheck
) {
986 TKeyIndex keyIndex
= {{0}};
987 uint8_t sectorCnt
= blockNo
;
988 int res
= MifareMultisectorChk(datain
, keyCount
, sectorCnt
, keyType
, OLD_MF_DBGLEVEL
, &keyIndex
);
992 cmd_send(CMD_ACK
, 1, 0, 0, keyIndex
, 80);
994 cmd_send(CMD_ACK
, 0, 0, 0, NULL
, 0);
998 int res
= MifareChkBlockKeys(datain
, keyCount
, blockNo
, keyType
, OLD_MF_DBGLEVEL
);
1002 cmd_send(CMD_ACK
, 1, 0, 0, datain
+ (res
- 1) * 6, 6);
1004 cmd_send(CMD_ACK
, 0, 0, 0, NULL
, 0);
1009 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1012 // restore debug level
1013 MF_DBGLEVEL
= OLD_MF_DBGLEVEL
;
1016 //-----------------------------------------------------------------------------
1017 // MIFARE commands set debug level
1019 //-----------------------------------------------------------------------------
1020 void MifareSetDbgLvl(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1022 Dbprintf("Debug level: %d", MF_DBGLEVEL
);
1025 //-----------------------------------------------------------------------------
1026 // Work with emulator memory
1028 // Note: we call FpgaDownloadAndGo(FPGA_BITSTREAM_HF) here although FPGA is not
1029 // involved in dealing with emulator memory. But if it is called later, it might
1030 // destroy the Emulator Memory.
1031 //-----------------------------------------------------------------------------
1033 void MifareEMemClr(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1034 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1038 void MifareEMemSet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1039 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1040 emlSetMem(datain
, arg0
, arg1
); // data, block num, blocks count
1043 void MifareEMemGet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1044 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1045 byte_t buf
[USB_CMD_DATA_SIZE
];
1046 emlGetMem(buf
, arg0
, arg1
); // data, block num, blocks count (max 4)
1049 cmd_send(CMD_ACK
,arg0
,arg1
,0,buf
,USB_CMD_DATA_SIZE
);
1053 //-----------------------------------------------------------------------------
1054 // Load a card into the emulator memory
1056 //-----------------------------------------------------------------------------
1057 void MifareECardLoad(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1058 uint8_t numSectors
= arg0
;
1059 uint8_t keyType
= arg1
;
1060 uint64_t ui64Key
= 0;
1062 struct Crypto1State mpcs
= {0, 0};
1063 struct Crypto1State
*pcs
;
1067 byte_t dataoutbuf
[16];
1068 byte_t dataoutbuf2
[16];
1074 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1081 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
1083 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1086 for (uint8_t sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
1087 ui64Key
= emlGetKey(sectorNo
, keyType
);
1089 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
1091 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo
);
1095 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_NESTED
)) {
1097 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo
);
1102 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
1103 if(isOK
&& mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
)) {
1105 if (MF_DBGLEVEL
>= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo
, blockNo
);
1109 if (blockNo
< NumBlocksPerSector(sectorNo
) - 1) {
1110 emlSetMem(dataoutbuf
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
1111 } else { // sector trailer, keep the keys, set only the AC
1112 emlGetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
1113 memcpy(&dataoutbuf2
[6], &dataoutbuf
[6], 4);
1114 emlSetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
1121 if(mifare_classic_halt(pcs
, cuid
)) {
1122 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1125 // ----------------------------- crypto1 destroy
1126 crypto1_destroy(pcs
);
1128 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1131 if (MF_DBGLEVEL
>= 2) DbpString("EMUL FILL SECTORS FINISHED");
1136 //-----------------------------------------------------------------------------
1137 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
1139 //-----------------------------------------------------------------------------
1141 static bool isBlockTrailer(int blockN
) {
1142 if (blockN
>= 0 && blockN
< 128) {
1143 return ((blockN
& 0x03) == 0x03);
1145 if (blockN
>= 128 && blockN
<= 256) {
1146 return ((blockN
& 0x0F) == 0x0F);
1151 void MifareCWipe(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1154 uint32_t numBlocks
= arg0
;
1156 // bit 0 - wipe gen1a
1157 // bit 1 - fill card with default data
1158 // bit 2 - gen1a = 0, gen1b = 1
1159 uint8_t cmdParams
= arg1
;
1160 bool needWipe
= cmdParams
& 0x01;
1161 bool needFill
= cmdParams
& 0x02;
1162 bool gen1b
= cmdParams
& 0x04;
1164 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1165 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1167 uint8_t block0
[16] = {0x01, 0x02, 0x03, 0x04, 0x04, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBE, 0xAF};
1168 uint8_t block1
[16] = {0x00};
1169 uint8_t blockK
[16] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x08, 0x77, 0x8F, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1170 uint8_t d_block
[18] = {0x00};
1173 uint8_t wupC1
[] = { 0x40 };
1174 uint8_t wupC2
[] = { 0x43 };
1175 uint8_t wipeC
[] = { 0x41 };
1181 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1190 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1191 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1192 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1196 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
1197 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1198 if (MF_DBGLEVEL
>= 1) Dbprintf("wipeC error");
1202 if(mifare_classic_halt(NULL
, 0)) {
1203 if (MF_DBGLEVEL
> 2) Dbprintf("Halt error");
1210 ReaderTransmitBitsPar(wupC1
, 7, 0, NULL
);
1212 // gen1b magic tag : do no issue wupC2 and don't expect 0x0a response after SELECT_UID (after getting UID from chip in 'hf mf csetuid' command)
1215 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1216 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1220 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1221 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1222 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1227 // send blocks command
1228 for (int blockNo
= 0; blockNo
< numBlocks
; blockNo
++) {
1229 if ((mifare_sendcmd_short(NULL
, 0, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1230 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send command error");
1234 // check type of block and add crc
1235 if (!isBlockTrailer(blockNo
)){
1236 memcpy(d_block
, block1
, 16);
1238 memcpy(d_block
, blockK
, 16);
1241 memcpy(d_block
, block0
, 16);
1243 AppendCrc14443a(d_block
, 16);
1245 // send write command
1246 ReaderTransmit(d_block
, sizeof(d_block
), NULL
);
1247 if ((ReaderReceive(receivedAnswer
, receivedAnswerPar
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1248 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send data error");
1254 // do no issue halt command for gen1b
1256 if (mifare_classic_halt(NULL
, 0)) {
1257 if (MF_DBGLEVEL
> 2) Dbprintf("Halt error");
1265 // send USB response
1267 cmd_send(CMD_ACK
,isOK
,0,0,NULL
,0);
1271 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1277 void MifareCSetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1280 uint8_t needWipe
= arg0
;
1281 // bit 0 - need get UID
1282 // bit 1 - need wupC
1283 // bit 2 - need HALT after sequence
1284 // bit 3 - need init FPGA and field before sequence
1285 // bit 4 - need reset FPGA and LED
1286 // bit 6 - gen1b backdoor type
1287 uint8_t workFlags
= arg1
;
1288 uint8_t blockNo
= arg2
;
1291 uint8_t wupC1
[] = { 0x40 };
1292 uint8_t wupC2
[] = { 0x43 };
1293 uint8_t wipeC
[] = { 0x41 };
1297 uint8_t uid
[10] = {0x00};
1298 uint8_t d_block
[18] = {0x00};
1301 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1302 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1304 // reset FPGA and LED
1305 if (workFlags
& 0x08) {
1309 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1317 // get UID from chip
1318 if (workFlags
& 0x01) {
1319 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
1320 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1321 // 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.
1325 if(mifare_classic_halt(NULL
, cuid
)) {
1326 if (MF_DBGLEVEL
> 2) Dbprintf("Halt error");
1327 // Continue, some magic tags misbehavies and send an answer to it.
1333 // Wipe command don't work with gen1b
1334 if (needWipe
&& !(workFlags
& 0x40)){
1335 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1336 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1337 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1341 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
1342 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1343 if (MF_DBGLEVEL
>= 1) Dbprintf("wipeC error");
1347 if(mifare_classic_halt(NULL
, 0)) {
1348 if (MF_DBGLEVEL
> 2) Dbprintf("Halt error");
1349 // Continue, some magic tags misbehavies and send an answer to it.
1355 if (workFlags
& 0x02) {
1356 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1358 // gen1b magic tag : do no issue wupC2 and don't expect 0x0a response after SELECT_UID (after getting UID from chip in 'hf mf csetuid' command)
1359 if (!(workFlags
& 0x40)) {
1361 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1362 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1366 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1367 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1368 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1374 if ((mifare_sendcmd_short(NULL
, 0, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1375 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send command error");
1379 memcpy(d_block
, datain
, 16);
1380 AppendCrc14443a(d_block
, 16);
1382 ReaderTransmit(d_block
, sizeof(d_block
), NULL
);
1383 if ((ReaderReceive(receivedAnswer
, receivedAnswerPar
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1384 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send data error");
1388 if (workFlags
& 0x04) {
1389 // do no issue halt command for gen1b magic tag (#db# halt error. response len: 1)
1390 if (!(workFlags
& 0x40)) {
1391 if (mifare_classic_halt(NULL
, 0)) {
1392 if (MF_DBGLEVEL
> 2) Dbprintf("Halt error");
1393 // Continue, some magic tags misbehavies and send an answer to it.
1404 cmd_send(CMD_ACK
,isOK
,0,0,uid
,4);
1407 if ((workFlags
& 0x10) || (!isOK
)) {
1408 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1414 void MifareCGetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1417 // bit 1 - need wupC
1418 // bit 2 - need HALT after sequence
1419 // bit 3 - need init FPGA and field before sequence
1420 // bit 4 - need reset FPGA and LED
1421 // bit 5 - need to set datain instead of issuing USB reply (called via ARM for StandAloneMode14a)
1422 // bit 6 - gen1b backdoor type
1423 uint8_t workFlags
= arg0
;
1424 uint8_t blockNo
= arg2
;
1427 uint8_t wupC1
[] = { 0x40 };
1428 uint8_t wupC2
[] = { 0x43 };
1432 uint8_t data
[18] = {0x00};
1435 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1436 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1438 if (workFlags
& 0x08) {
1442 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1449 if (workFlags
& 0x02) {
1450 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1451 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1452 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1455 // do no issue for gen1b magic tag
1456 if (!(workFlags
& 0x40)) {
1457 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1458 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1459 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1466 if ((mifare_sendcmd_short(NULL
, 0, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 18)) {
1467 if (MF_DBGLEVEL
>= 1) Dbprintf("read block send command error");
1470 memcpy(data
, receivedAnswer
, 18);
1472 if (workFlags
& 0x04) {
1473 // do no issue halt command for gen1b magic tag (#db# halt error. response len: 1)
1474 if (!(workFlags
& 0x40)) {
1475 if (mifare_classic_halt(NULL
, cuid
)) {
1476 if (MF_DBGLEVEL
> 1) Dbprintf("Halt error");
1477 // Continue, some magic tags misbehavies and send an answer to it.
1488 if (workFlags
& 0x20) {
1490 memcpy(datain
, data
, 18);
1493 cmd_send(CMD_ACK
,isOK
,0,0,data
,18);
1496 if ((workFlags
& 0x10) || (!isOK
)) {
1497 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1502 void MifareCIdent(){
1505 uint8_t wupC1
[] = { 0x40 };
1506 uint8_t wupC2
[] = { 0x43 };
1511 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1512 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1517 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1522 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1523 if(ReaderReceive(receivedAnswer
, receivedAnswerPar
) && (receivedAnswer
[0] == 0x0a)) {
1526 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1527 if(ReaderReceive(receivedAnswer
, receivedAnswerPar
) && (receivedAnswer
[0] == 0x0a)) {
1532 // From iceman1001: removed the if, since some magic tags misbehavies and send an answer to it.
1533 mifare_classic_halt(NULL
, 0);
1536 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
1539 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1547 void Mifare_DES_Auth1(uint8_t arg0
, uint8_t *datain
){
1549 byte_t dataout
[11] = {0x00};
1550 uint8_t uid
[10] = {0x00};
1553 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1556 int len
= iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true);
1558 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
1563 if(mifare_desfire_des_auth1(cuid
, dataout
)){
1564 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication part1: Fail.");
1569 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 1 FINISHED");
1570 cmd_send(CMD_ACK
,1,cuid
,0,dataout
, sizeof(dataout
));
1573 void Mifare_DES_Auth2(uint32_t arg0
, uint8_t *datain
){
1575 uint32_t cuid
= arg0
;
1576 uint8_t key
[16] = {0x00};
1578 byte_t dataout
[12] = {0x00};
1580 memcpy(key
, datain
, 16);
1582 isOK
= mifare_desfire_des_auth2(cuid
, key
, dataout
);
1585 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) Dbprintf("Authentication part2: Failed");
1590 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 2 FINISHED");
1592 cmd_send(CMD_ACK
, isOK
, 0, 0, dataout
, sizeof(dataout
));
1593 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1599 ReaderTransmit(deselect_cmd
, 3 , NULL
);
1600 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1604 void OnError(uint8_t reason
){
1606 ReaderTransmit(deselect_cmd
, 3 , NULL
);
1607 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1608 cmd_send(CMD_ACK
,0,reason
,0,0,0);