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 response in order to trigger the cards authentication failure timeout
698 uint8_t dummy_answer
[8] = {0};
699 ReaderTransmit(dummy_answer
, 8, NULL
);
702 if (num_nonces
% 2) {
703 memcpy(buf
+i
, receivedAnswer
, 4);
704 nt_par_enc
= par_enc
[0] & 0xf0;
706 nt_par_enc
|= par_enc
[0] >> 4;
707 memcpy(buf
+i
+4, receivedAnswer
, 4);
708 memcpy(buf
+i
+8, &nt_par_enc
, 1);
716 crypto1_destroy(pcs
);
719 cmd_send(CMD_ACK
, isOK
, cuid
, num_nonces
, buf
, sizeof(buf
));
722 if (MF_DBGLEVEL
>= 3) DbpString("AcquireEncryptedNonces finished");
725 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
731 //-----------------------------------------------------------------------------
732 // MIFARE nested authentication.
734 //-----------------------------------------------------------------------------
735 void MifareNested(uint32_t arg0
, uint32_t arg1
, uint32_t calibrate
, uint8_t *datain
)
738 uint8_t blockNo
= arg0
& 0xff;
739 uint8_t keyType
= (arg0
>> 8) & 0xff;
740 uint8_t targetBlockNo
= arg1
& 0xff;
741 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
742 uint64_t ui64Key
= 0;
744 ui64Key
= bytes_to_num(datain
, 6);
747 uint16_t rtr
, i
, j
, len
;
749 static uint16_t dmin
, dmax
;
751 uint32_t cuid
, nt1
, nt2
, nttmp
, nttest
, ks1
;
753 uint32_t target_nt
[2], target_ks
[2];
755 uint8_t par_array
[4];
757 struct Crypto1State mpcs
= {0, 0};
758 struct Crypto1State
*pcs
;
760 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
762 uint32_t auth1_time
, auth2_time
;
763 static uint16_t delta_time
;
767 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
769 // free eventually allocated BigBuf memory
772 if (calibrate
) clear_trace();
775 // statistics on nonce distance
777 #define NESTED_MAX_TRIES 12
778 uint16_t unsuccessfull_tries
= 0;
779 if (calibrate
) { // for first call only. Otherwise reuse previous calibration
787 for (rtr
= 0; rtr
< 17; rtr
++) {
789 // Test if the action was cancelled
795 // prepare next select. No need to power down the card.
796 if(mifare_classic_halt(pcs
, cuid
)) {
797 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
802 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
803 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
809 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
810 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
816 auth2_time
= auth1_time
+ delta_time
;
820 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_NESTED
, &nt2
, &auth2_time
)) {
821 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error");
826 nttmp
= prng_successor(nt1
, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160
827 for (i
= 101; i
< 1200; i
++) {
828 nttmp
= prng_successor(nttmp
, 1);
829 if (nttmp
== nt2
) break;
839 delta_time
= auth2_time
- auth1_time
+ 32; // allow some slack for proper timing
841 if (MF_DBGLEVEL
>= 3) Dbprintf("Nested: calibrating... ntdist=%d", i
);
843 unsuccessfull_tries
++;
844 if (unsuccessfull_tries
> NESTED_MAX_TRIES
) { // card isn't vulnerable to nested attack (random numbers are not predictable)
850 davg
= (davg
+ (rtr
- 1)/2) / (rtr
- 1);
852 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
);
860 // -------------------------------------------------------------------------------------------------
864 // get crypted nonces for target sector
865 for(i
=0; i
< 2 && !isOK
; i
++) { // look for exactly two different nonces
868 while(target_nt
[i
] == 0) { // continue until we have an unambiguous nonce
870 // prepare next select. No need to power down the card.
871 if(mifare_classic_halt(pcs
, cuid
)) {
872 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
876 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
877 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
882 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
883 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
887 // nested authentication
888 auth2_time
= auth1_time
+ delta_time
;
889 len
= mifare_sendcmd_short(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType
& 0x01), targetBlockNo
, receivedAnswer
, par
, &auth2_time
);
891 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error len=%d", len
);
895 nt2
= bytes_to_num(receivedAnswer
, 4);
896 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i
+1, nt1
, nt2
, par
[0]);
898 // Parity validity check
899 for (j
= 0; j
< 4; j
++) {
900 par_array
[j
] = (oddparity8(receivedAnswer
[j
]) != ((par
[0] >> (7-j
)) & 0x01));
904 nttest
= prng_successor(nt1
, dmin
- 1);
905 for (j
= dmin
; j
< dmax
+ 1; j
++) {
906 nttest
= prng_successor(nttest
, 1);
909 if (valid_nonce(nttest
, nt2
, ks1
, par_array
)){
910 if (ncount
> 0) { // we are only interested in disambiguous nonces, try again
911 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i
+1, j
);
915 target_nt
[i
] = nttest
;
918 if (i
== 1 && target_nt
[1] == target_nt
[0]) { // we need two different nonces
920 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j
);
923 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i
+1, j
);
926 if (target_nt
[i
] == 0 && j
== dmax
+1 && MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i
+1);
932 // ----------------------------- crypto1 destroy
933 crypto1_destroy(pcs
);
935 byte_t buf
[4 + 4 * 4];
936 memcpy(buf
, &cuid
, 4);
937 memcpy(buf
+4, &target_nt
[0], 4);
938 memcpy(buf
+8, &target_ks
[0], 4);
939 memcpy(buf
+12, &target_nt
[1], 4);
940 memcpy(buf
+16, &target_ks
[1], 4);
943 cmd_send(CMD_ACK
, isOK
, 0, targetBlockNo
+ (targetKeyType
* 0x100), buf
, sizeof(buf
));
946 if (MF_DBGLEVEL
>= 3) DbpString("NESTED FINISHED");
948 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
952 //-----------------------------------------------------------------------------
953 // MIFARE check keys. key count up to 85.
955 //-----------------------------------------------------------------------------
956 void MifareChkKeys(uint16_t arg0
, uint16_t arg1
, uint8_t arg2
, uint8_t *datain
)
958 uint8_t blockNo
= arg0
& 0xff;
959 uint8_t keyType
= (arg0
>> 8) & 0xff;
960 bool clearTrace
= arg1
& 0x01;
961 bool multisectorCheck
= arg1
& 0x02;
962 uint8_t set14aTimeout
= (arg1
>> 8) & 0xff;
963 uint8_t keyCount
= arg2
;
966 int OLD_MF_DBGLEVEL
= MF_DBGLEVEL
;
967 MF_DBGLEVEL
= MF_DBG_NONE
;
972 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
974 if (clearTrace
) clear_trace();
978 iso14a_set_timeout(set14aTimeout
* 10); // timeout: ms = x/106 35-minimum, 50-OK 106-recommended 500-safe
981 if (multisectorCheck
) {
982 TKeyIndex keyIndex
= {{0}};
983 uint8_t sectorCnt
= blockNo
;
984 int res
= MifareMultisectorChk(datain
, keyCount
, sectorCnt
, keyType
, OLD_MF_DBGLEVEL
, &keyIndex
);
988 cmd_send(CMD_ACK
, 1, 0, 0, keyIndex
, 80);
990 cmd_send(CMD_ACK
, 0, 0, 0, NULL
, 0);
994 int res
= MifareChkBlockKeys(datain
, keyCount
, blockNo
, keyType
, OLD_MF_DBGLEVEL
);
998 cmd_send(CMD_ACK
, 1, 0, 0, datain
+ (res
- 1) * 6, 6);
1000 cmd_send(CMD_ACK
, 0, 0, 0, NULL
, 0);
1005 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1008 // restore debug level
1009 MF_DBGLEVEL
= OLD_MF_DBGLEVEL
;
1012 //-----------------------------------------------------------------------------
1013 // MIFARE commands set debug level
1015 //-----------------------------------------------------------------------------
1016 void MifareSetDbgLvl(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1018 Dbprintf("Debug level: %d", MF_DBGLEVEL
);
1021 //-----------------------------------------------------------------------------
1022 // Work with emulator memory
1024 // Note: we call FpgaDownloadAndGo(FPGA_BITSTREAM_HF) here although FPGA is not
1025 // involved in dealing with emulator memory. But if it is called later, it might
1026 // destroy the Emulator Memory.
1027 //-----------------------------------------------------------------------------
1029 void MifareEMemClr(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1030 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1034 void MifareEMemSet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1035 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1036 emlSetMem(datain
, arg0
, arg1
); // data, block num, blocks count
1039 void MifareEMemGet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1040 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1041 byte_t buf
[USB_CMD_DATA_SIZE
];
1042 emlGetMem(buf
, arg0
, arg1
); // data, block num, blocks count (max 4)
1045 cmd_send(CMD_ACK
,arg0
,arg1
,0,buf
,USB_CMD_DATA_SIZE
);
1049 //-----------------------------------------------------------------------------
1050 // Load a card into the emulator memory
1052 //-----------------------------------------------------------------------------
1053 void MifareECardLoad(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1054 uint8_t numSectors
= arg0
;
1055 uint8_t keyType
= arg1
;
1056 uint64_t ui64Key
= 0;
1058 struct Crypto1State mpcs
= {0, 0};
1059 struct Crypto1State
*pcs
;
1063 byte_t dataoutbuf
[16];
1064 byte_t dataoutbuf2
[16];
1070 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1077 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
1079 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1082 for (uint8_t sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
1083 ui64Key
= emlGetKey(sectorNo
, keyType
);
1085 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
1087 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo
);
1091 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_NESTED
)) {
1093 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo
);
1098 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
1099 if(isOK
&& mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
)) {
1101 if (MF_DBGLEVEL
>= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo
, blockNo
);
1105 if (blockNo
< NumBlocksPerSector(sectorNo
) - 1) {
1106 emlSetMem(dataoutbuf
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
1107 } else { // sector trailer, keep the keys, set only the AC
1108 emlGetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
1109 memcpy(&dataoutbuf2
[6], &dataoutbuf
[6], 4);
1110 emlSetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
1117 if(mifare_classic_halt(pcs
, cuid
)) {
1118 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1121 // ----------------------------- crypto1 destroy
1122 crypto1_destroy(pcs
);
1124 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1127 if (MF_DBGLEVEL
>= 2) DbpString("EMUL FILL SECTORS FINISHED");
1132 //-----------------------------------------------------------------------------
1133 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
1135 //-----------------------------------------------------------------------------
1137 static bool isBlockTrailer(int blockN
) {
1138 if (blockN
>= 0 && blockN
< 128) {
1139 return ((blockN
& 0x03) == 0x03);
1141 if (blockN
>= 128 && blockN
<= 256) {
1142 return ((blockN
& 0x0F) == 0x0F);
1147 void MifareCWipe(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1150 uint32_t numBlocks
= arg0
;
1152 // bit 0 - wipe gen1a
1153 // bit 1 - fill card with default data
1154 // bit 2 - gen1a = 0, gen1b = 1
1155 uint8_t cmdParams
= arg1
;
1156 bool needWipe
= cmdParams
& 0x01;
1157 bool needFill
= cmdParams
& 0x02;
1158 bool gen1b
= cmdParams
& 0x04;
1160 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1161 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1163 uint8_t block0
[16] = {0x01, 0x02, 0x03, 0x04, 0x04, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBE, 0xAF};
1164 uint8_t block1
[16] = {0x00};
1165 uint8_t blockK
[16] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x08, 0x77, 0x8F, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1166 uint8_t d_block
[18] = {0x00};
1169 uint8_t wupC1
[] = { 0x40 };
1170 uint8_t wupC2
[] = { 0x43 };
1171 uint8_t wipeC
[] = { 0x41 };
1177 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1186 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1187 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1188 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1192 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
1193 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1194 if (MF_DBGLEVEL
>= 1) Dbprintf("wipeC error");
1198 if(mifare_classic_halt(NULL
, 0)) {
1199 if (MF_DBGLEVEL
> 2) Dbprintf("Halt error");
1206 ReaderTransmitBitsPar(wupC1
, 7, 0, NULL
);
1208 // 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)
1211 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1212 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1216 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1217 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1218 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1223 // send blocks command
1224 for (int blockNo
= 0; blockNo
< numBlocks
; blockNo
++) {
1225 if ((mifare_sendcmd_short(NULL
, 0, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1226 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send command error");
1230 // check type of block and add crc
1231 if (!isBlockTrailer(blockNo
)){
1232 memcpy(d_block
, block1
, 16);
1234 memcpy(d_block
, blockK
, 16);
1237 memcpy(d_block
, block0
, 16);
1239 AppendCrc14443a(d_block
, 16);
1241 // send write command
1242 ReaderTransmit(d_block
, sizeof(d_block
), NULL
);
1243 if ((ReaderReceive(receivedAnswer
, receivedAnswerPar
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1244 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send data error");
1250 // do no issue halt command for gen1b
1252 if (mifare_classic_halt(NULL
, 0)) {
1253 if (MF_DBGLEVEL
> 2) Dbprintf("Halt error");
1261 // send USB response
1263 cmd_send(CMD_ACK
,isOK
,0,0,NULL
,0);
1267 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1273 void MifareCSetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1276 uint8_t needWipe
= arg0
;
1277 // bit 0 - need get UID
1278 // bit 1 - need wupC
1279 // bit 2 - need HALT after sequence
1280 // bit 3 - need init FPGA and field before sequence
1281 // bit 4 - need reset FPGA and LED
1282 // bit 6 - gen1b backdoor type
1283 uint8_t workFlags
= arg1
;
1284 uint8_t blockNo
= arg2
;
1287 uint8_t wupC1
[] = { 0x40 };
1288 uint8_t wupC2
[] = { 0x43 };
1289 uint8_t wipeC
[] = { 0x41 };
1293 uint8_t uid
[10] = {0x00};
1294 uint8_t d_block
[18] = {0x00};
1297 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1298 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1300 // reset FPGA and LED
1301 if (workFlags
& 0x08) {
1305 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1313 // get UID from chip
1314 if (workFlags
& 0x01) {
1315 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
1316 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1317 // 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.
1321 if(mifare_classic_halt(NULL
, cuid
)) {
1322 if (MF_DBGLEVEL
> 2) Dbprintf("Halt error");
1323 // Continue, some magic tags misbehavies and send an answer to it.
1329 // Wipe command don't work with gen1b
1330 if (needWipe
&& !(workFlags
& 0x40)){
1331 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1332 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1333 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1337 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
1338 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1339 if (MF_DBGLEVEL
>= 1) Dbprintf("wipeC error");
1343 if(mifare_classic_halt(NULL
, 0)) {
1344 if (MF_DBGLEVEL
> 2) Dbprintf("Halt error");
1345 // Continue, some magic tags misbehavies and send an answer to it.
1351 if (workFlags
& 0x02) {
1352 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1354 // 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)
1355 if (!(workFlags
& 0x40)) {
1357 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1358 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1362 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1363 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1364 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1370 if ((mifare_sendcmd_short(NULL
, 0, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1371 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send command error");
1375 memcpy(d_block
, datain
, 16);
1376 AppendCrc14443a(d_block
, 16);
1378 ReaderTransmit(d_block
, sizeof(d_block
), NULL
);
1379 if ((ReaderReceive(receivedAnswer
, receivedAnswerPar
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1380 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send data error");
1384 if (workFlags
& 0x04) {
1385 // do no issue halt command for gen1b magic tag (#db# halt error. response len: 1)
1386 if (!(workFlags
& 0x40)) {
1387 if (mifare_classic_halt(NULL
, 0)) {
1388 if (MF_DBGLEVEL
> 2) Dbprintf("Halt error");
1389 // Continue, some magic tags misbehavies and send an answer to it.
1400 cmd_send(CMD_ACK
,isOK
,0,0,uid
,4);
1403 if ((workFlags
& 0x10) || (!isOK
)) {
1404 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1410 void MifareCGetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1413 // bit 1 - need wupC
1414 // bit 2 - need HALT after sequence
1415 // bit 3 - need init FPGA and field before sequence
1416 // bit 4 - need reset FPGA and LED
1417 // bit 5 - need to set datain instead of issuing USB reply (called via ARM for StandAloneMode14a)
1418 // bit 6 - gen1b backdoor type
1419 uint8_t workFlags
= arg0
;
1420 uint8_t blockNo
= arg2
;
1423 uint8_t wupC1
[] = { 0x40 };
1424 uint8_t wupC2
[] = { 0x43 };
1428 uint8_t data
[18] = {0x00};
1431 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1432 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1434 if (workFlags
& 0x08) {
1438 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1445 if (workFlags
& 0x02) {
1446 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1447 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1448 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1451 // do no issue for gen1b magic tag
1452 if (!(workFlags
& 0x40)) {
1453 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1454 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1455 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1462 if ((mifare_sendcmd_short(NULL
, 0, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 18)) {
1463 if (MF_DBGLEVEL
>= 1) Dbprintf("read block send command error");
1466 memcpy(data
, receivedAnswer
, 18);
1468 if (workFlags
& 0x04) {
1469 // do no issue halt command for gen1b magic tag (#db# halt error. response len: 1)
1470 if (!(workFlags
& 0x40)) {
1471 if (mifare_classic_halt(NULL
, cuid
)) {
1472 if (MF_DBGLEVEL
> 1) Dbprintf("Halt error");
1473 // Continue, some magic tags misbehavies and send an answer to it.
1484 if (workFlags
& 0x20) {
1486 memcpy(datain
, data
, 18);
1489 cmd_send(CMD_ACK
,isOK
,0,0,data
,18);
1492 if ((workFlags
& 0x10) || (!isOK
)) {
1493 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1498 void MifareCIdent(){
1501 uint8_t wupC1
[] = { 0x40 };
1502 uint8_t wupC2
[] = { 0x43 };
1507 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1508 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1513 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1518 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1519 if(ReaderReceive(receivedAnswer
, receivedAnswerPar
) && (receivedAnswer
[0] == 0x0a)) {
1522 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1523 if(ReaderReceive(receivedAnswer
, receivedAnswerPar
) && (receivedAnswer
[0] == 0x0a)) {
1528 // From iceman1001: removed the if, since some magic tags misbehavies and send an answer to it.
1529 mifare_classic_halt(NULL
, 0);
1532 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
1535 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1543 void Mifare_DES_Auth1(uint8_t arg0
, uint8_t *datain
){
1545 byte_t dataout
[11] = {0x00};
1546 uint8_t uid
[10] = {0x00};
1549 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1552 int len
= iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true);
1554 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
1559 if(mifare_desfire_des_auth1(cuid
, dataout
)){
1560 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication part1: Fail.");
1565 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 1 FINISHED");
1566 cmd_send(CMD_ACK
,1,cuid
,0,dataout
, sizeof(dataout
));
1569 void Mifare_DES_Auth2(uint32_t arg0
, uint8_t *datain
){
1571 uint32_t cuid
= arg0
;
1572 uint8_t key
[16] = {0x00};
1574 byte_t dataout
[12] = {0x00};
1576 memcpy(key
, datain
, 16);
1578 isOK
= mifare_desfire_des_auth2(cuid
, key
, dataout
);
1581 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) Dbprintf("Authentication part2: Failed");
1586 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 2 FINISHED");
1588 cmd_send(CMD_ACK
, isOK
, 0, 0, dataout
, sizeof(dataout
));
1589 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1595 ReaderTransmit(deselect_cmd
, 3 , NULL
);
1596 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1600 void OnError(uint8_t reason
){
1602 ReaderTransmit(deselect_cmd
, 3 , NULL
);
1603 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1604 cmd_send(CMD_ACK
,0,reason
,0,0,0);