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"
20 #include "proxmark3.h"
22 #include "crapto1/crapto1.h"
23 #include "iso14443a.h"
25 #include "mifareutil.h"
27 #include "protocols.h"
31 #include "fpgaloader.h"
33 #define HARDNESTED_AUTHENTICATION_TIMEOUT 848 // card times out 1ms after wrong authentication (according to NXP documentation)
34 #define HARDNESTED_PRE_AUTHENTICATION_LEADTIME 400 // some (non standard) cards need a pause after select before they are ready for first authentication
37 // the block number for the ISO14443-4 PCB
38 static uint8_t pcb_blocknum = 0;
39 // Deselect card by sending a s-block. the crc is precalced for speed
40 static uint8_t deselect_cmd[] = {0xc2,0xe0,0xb4};
42 static void OnSuccess(){
44 ReaderTransmit(deselect_cmd, 3 , NULL);
45 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
50 static void OnError(uint8_t reason
){
52 // ReaderTransmit(deselect_cmd, 3 , NULL);
53 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
55 cmd_send(CMD_ACK
,0,reason
,0,0,0);
59 //-----------------------------------------------------------------------------
60 // Select, Authenticate, Read a MIFARE tag.
62 //-----------------------------------------------------------------------------
63 void MifareReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
67 uint8_t blockNo
= arg0
;
68 uint8_t keyType
= arg1
;
70 ui64Key
= bytes_to_num(datain
, 6);
73 byte_t dataoutbuf
[16];
76 struct Crypto1State mpcs
= {0, 0};
77 struct Crypto1State
*pcs
;
80 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
85 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
86 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
90 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
91 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
95 if(mifare_classic_readblock(pcs
, cuid
, blockNo
, dataoutbuf
)) {
96 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block error");
100 if(mifare_classic_halt(pcs
, cuid
)) {
101 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
109 // ----------------------------- crypto1 destroy
110 crypto1_destroy(pcs
);
112 if (MF_DBGLEVEL
>= 2) DbpString("READ BLOCK FINISHED");
115 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16);
118 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
122 void MifareUC_Auth(uint8_t arg0
, uint8_t *keybytes
){
125 bool turnOffField
= (arg0
== 1);
127 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
129 if (!iso14443a_select_card(NULL
, NULL
, NULL
, true, 0, true)) {
130 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
135 if (!mifare_ultra_auth(keybytes
)){
136 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication failed");
142 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
146 cmd_send(CMD_ACK
,1,0,0,0,0);
151 // Arg1 = UsePwd bool
152 // datain = PWD bytes,
153 void MifareUReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
)
157 uint8_t blockNo
= arg0
;
158 byte_t dataout
[16] = {0x00};
159 bool useKey
= (arg1
== 1); //UL_C
160 bool usePwd
= (arg1
== 2); //UL_EV1/NTAG
162 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
164 int len
= iso14443a_select_card(NULL
, NULL
, NULL
, true, 0, true);
166 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%02X)",len
);
171 // UL-C authentication
173 uint8_t key
[16] = {0x00};
174 memcpy(key
, datain
, sizeof(key
) );
176 if ( !mifare_ultra_auth(key
) ) {
182 // UL-EV1 / NTAG authentication
184 uint8_t pwd
[4] = {0x00};
185 memcpy(pwd
, datain
, 4);
186 uint8_t pack
[4] = {0,0,0,0};
187 if (!mifare_ul_ev1_auth(pwd
, pack
)) {
193 if (mifare_ultra_readblock(blockNo
, dataout
)) {
194 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Read block error");
199 if (mifare_ultra_halt()) {
200 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Halt error");
205 cmd_send(CMD_ACK
,1,0,0,dataout
,16);
206 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
211 //-----------------------------------------------------------------------------
212 // Select, Authenticate, Read a MIFARE tag.
213 // read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)
214 //-----------------------------------------------------------------------------
215 void MifareReadSector(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
218 uint8_t sectorNo
= arg0
;
219 uint8_t keyType
= arg1
;
220 uint64_t ui64Key
= 0;
221 ui64Key
= bytes_to_num(datain
, 6);
225 byte_t dataoutbuf
[16 * 16];
228 struct Crypto1State mpcs
= {0, 0};
229 struct Crypto1State
*pcs
;
232 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
241 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
243 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
247 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
249 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
252 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
253 if(mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
+ 16 * blockNo
)) {
255 if (MF_DBGLEVEL
>= 1) Dbprintf("Read sector %2d block %2d error", sectorNo
, blockNo
);
260 if(mifare_classic_halt(pcs
, cuid
)) {
261 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
264 // ----------------------------- crypto1 destroy
265 crypto1_destroy(pcs
);
267 if (MF_DBGLEVEL
>= 2) DbpString("READ SECTOR FINISHED");
270 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16*NumBlocksPerSector(sectorNo
));
274 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
278 // arg0 = blockNo (start)
279 // arg1 = Pages (number of blocks)
281 // datain = KEY bytes
282 void MifareUReadCard(uint8_t arg0
, uint16_t arg1
, uint8_t arg2
, uint8_t *datain
)
285 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
287 // free eventually allocated BigBuf memory
291 uint8_t blockNo
= arg0
;
292 uint16_t blocks
= arg1
;
293 bool useKey
= (arg2
== 1); //UL_C
294 bool usePwd
= (arg2
== 2); //UL_EV1/NTAG
295 uint32_t countblocks
= 0;
296 uint8_t *dataout
= BigBuf_malloc(CARD_MEMORY_SIZE
);
297 if (dataout
== NULL
){
298 Dbprintf("out of memory");
303 int len
= iso14443a_select_card(NULL
, NULL
, NULL
, true, 0, true);
305 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%d)",len
);
310 // UL-C authentication
312 uint8_t key
[16] = {0x00};
313 memcpy(key
, datain
, sizeof(key
) );
315 if ( !mifare_ultra_auth(key
) ) {
321 // UL-EV1 / NTAG authentication
323 uint8_t pwd
[4] = {0x00};
324 memcpy(pwd
, datain
, sizeof(pwd
));
325 uint8_t pack
[4] = {0,0,0,0};
327 if (!mifare_ul_ev1_auth(pwd
, pack
)){
333 for (int i
= 0; i
< blocks
; i
++){
334 if ((i
*4) + 4 >= CARD_MEMORY_SIZE
) {
335 Dbprintf("Data exceeds buffer!!");
339 len
= mifare_ultra_readblock(blockNo
+ i
, dataout
+ 4 * i
);
342 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Read block %d error",i
);
343 // if no blocks read - error out
348 //stop at last successful read block and return what we got
356 len
= mifare_ultra_halt();
358 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Halt error");
363 if (MF_DBGLEVEL
>= MF_DBG_DEBUG
) Dbprintf("Blocks read %d", countblocks
);
365 cmd_send(CMD_ACK
, 1, countblocks
*4, BigBuf_max_traceLen(), 0, 0);
367 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
373 //-----------------------------------------------------------------------------
374 // Select, Authenticate, Write a MIFARE tag.
376 //-----------------------------------------------------------------------------
377 void MifareWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
380 uint8_t blockNo
= arg0
;
381 uint8_t keyType
= arg1
;
382 uint64_t ui64Key
= 0;
383 byte_t blockdata
[16];
385 ui64Key
= bytes_to_num(datain
, 6);
386 memcpy(blockdata
, datain
+ 10, 16);
392 struct Crypto1State mpcs
= {0, 0};
393 struct Crypto1State
*pcs
;
396 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
405 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
406 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
410 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
411 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
415 if(mifare_classic_writeblock(pcs
, cuid
, blockNo
, blockdata
)) {
416 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
420 if(mifare_classic_halt(pcs
, cuid
)) {
421 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
429 // ----------------------------- crypto1 destroy
430 crypto1_destroy(pcs
);
432 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
435 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
440 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
444 /* // Command not needed but left for future testing
445 void MifareUWriteBlockCompat(uint8_t arg0, uint8_t *datain)
447 uint8_t blockNo = arg0;
448 byte_t blockdata[16] = {0x00};
450 memcpy(blockdata, datain, 16);
452 uint8_t uid[10] = {0x00};
454 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
457 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
459 if(!iso14443a_select_card(uid, NULL, NULL, true, 0)) {
460 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
465 if(mifare_ultra_writeblock_compat(blockNo, blockdata)) {
466 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
470 if(mifare_ultra_halt()) {
471 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
476 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");
478 cmd_send(CMD_ACK,1,0,0,0,0);
479 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
484 // Arg0 : Block to write to.
485 // Arg1 : 0 = use no authentication.
486 // 1 = use 0x1A authentication.
487 // 2 = use 0x1B authentication.
488 // datain : 4 first bytes is data to be written.
489 // : 4/16 next bytes is authentication key.
490 void MifareUWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
)
492 uint8_t blockNo
= arg0
;
493 bool useKey
= (arg1
== 1); //UL_C
494 bool usePwd
= (arg1
== 2); //UL_EV1/NTAG
495 byte_t blockdata
[4] = {0x00};
497 memcpy(blockdata
, datain
,4);
501 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
505 if(!iso14443a_select_card(NULL
, NULL
, NULL
, true, 0, true)) {
506 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
511 // UL-C authentication
513 uint8_t key
[16] = {0x00};
514 memcpy(key
, datain
+4, sizeof(key
) );
516 if ( !mifare_ultra_auth(key
) ) {
522 // UL-EV1 / NTAG authentication
524 uint8_t pwd
[4] = {0x00};
525 memcpy(pwd
, datain
+4, 4);
526 uint8_t pack
[4] = {0,0,0,0};
527 if (!mifare_ul_ev1_auth(pwd
, pack
)) {
533 if(mifare_ultra_writeblock(blockNo
, blockdata
)) {
534 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
539 if(mifare_ultra_halt()) {
540 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
545 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
547 cmd_send(CMD_ACK
,1,0,0,0,0);
548 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
552 void MifareUSetPwd(uint8_t arg0
, uint8_t *datain
){
554 uint8_t pwd
[16] = {0x00};
555 byte_t blockdata
[4] = {0x00};
557 memcpy(pwd
, datain
, 16);
559 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
560 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
564 if(!iso14443a_select_card(NULL
, NULL
, NULL
, true, 0, true)) {
565 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
570 blockdata
[0] = pwd
[7];
571 blockdata
[1] = pwd
[6];
572 blockdata
[2] = pwd
[5];
573 blockdata
[3] = pwd
[4];
574 if(mifare_ultra_writeblock( 44, blockdata
)) {
575 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
580 blockdata
[0] = pwd
[3];
581 blockdata
[1] = pwd
[2];
582 blockdata
[2] = pwd
[1];
583 blockdata
[3] = pwd
[0];
584 if(mifare_ultra_writeblock( 45, blockdata
)) {
585 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
590 blockdata
[0] = pwd
[15];
591 blockdata
[1] = pwd
[14];
592 blockdata
[2] = pwd
[13];
593 blockdata
[3] = pwd
[12];
594 if(mifare_ultra_writeblock( 46, blockdata
)) {
595 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
600 blockdata
[0] = pwd
[11];
601 blockdata
[1] = pwd
[10];
602 blockdata
[2] = pwd
[9];
603 blockdata
[3] = pwd
[8];
604 if(mifare_ultra_writeblock( 47, blockdata
)) {
605 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
610 if(mifare_ultra_halt()) {
611 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
616 cmd_send(CMD_ACK
,1,0,0,0,0);
617 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
621 // Return 1 if the nonce is invalid else return 0
622 int valid_nonce(uint32_t Nt
, uint32_t NtEnc
, uint32_t Ks1
, uint8_t *parity
) {
623 return ((oddparity8((Nt
>> 24) & 0xFF) == ((parity
[0]) ^ oddparity8((NtEnc
>> 24) & 0xFF) ^ BIT(Ks1
,16))) & \
624 (oddparity8((Nt
>> 16) & 0xFF) == ((parity
[1]) ^ oddparity8((NtEnc
>> 16) & 0xFF) ^ BIT(Ks1
,8))) & \
625 (oddparity8((Nt
>> 8) & 0xFF) == ((parity
[2]) ^ oddparity8((NtEnc
>> 8) & 0xFF) ^ BIT(Ks1
,0)))) ? 1 : 0;
629 //-----------------------------------------------------------------------------
630 // acquire encrypted nonces in order to perform the attack described in
631 // Carlo Meijer, Roel Verdult, "Ciphertext-only Cryptanalysis on Hardened
632 // Mifare Classic Cards" in Proceedings of the 22nd ACM SIGSAC Conference on
633 // Computer and Communications Security, 2015
634 //-----------------------------------------------------------------------------
635 void MifareAcquireEncryptedNonces(uint32_t arg0
, uint32_t arg1
, uint32_t flags
, uint8_t *datain
)
637 uint64_t ui64Key
= 0;
640 uint8_t cascade_levels
= 0;
641 struct Crypto1State mpcs
= {0, 0};
642 struct Crypto1State
*pcs
;
644 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
647 uint8_t nt_par_enc
= 0;
648 uint8_t buf
[USB_CMD_DATA_SIZE
];
651 uint8_t blockNo
= arg0
& 0xff;
652 uint8_t keyType
= (arg0
>> 8) & 0xff;
653 uint8_t targetBlockNo
= arg1
& 0xff;
654 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
655 ui64Key
= bytes_to_num(datain
, 6);
656 bool initialize
= flags
& 0x0001;
657 bool slow
= flags
& 0x0002;
658 bool field_off
= flags
& 0x0004;
664 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
671 uint16_t num_nonces
= 0;
672 bool have_uid
= false;
673 for (uint16_t i
= 0; i
<= USB_CMD_DATA_SIZE
- 9; ) {
675 // Test if the action was cancelled
682 if (!have_uid
) { // need a full select cycle to get the uid first
683 iso14a_card_select_t card_info
;
684 if(!iso14443a_select_card(uid
, &card_info
, &cuid
, true, 0, true)) {
685 if (MF_DBGLEVEL
>= 1) Dbprintf("AcquireNonces: Can't select card (ALL)");
688 switch (card_info
.uidlen
) {
689 case 4 : cascade_levels
= 1; break;
690 case 7 : cascade_levels
= 2; break;
691 case 10: cascade_levels
= 3; break;
695 } else { // no need for anticollision. We can directly select the card
696 if(!iso14443a_select_card(uid
, NULL
, NULL
, false, cascade_levels
, true)) {
697 if (MF_DBGLEVEL
>= 1) Dbprintf("AcquireNonces: Can't select card (UID)");
703 timeout
= GetCountSspClk() + HARDNESTED_PRE_AUTHENTICATION_LEADTIME
;
704 while(GetCountSspClk() < timeout
);
708 if (mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, NULL
)) {
709 if (MF_DBGLEVEL
>= 1) Dbprintf("AcquireNonces: Auth1 error");
713 // nested authentication
714 uint16_t len
= mifare_sendcmd_short(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType
& 0x01), targetBlockNo
, receivedAnswer
, par_enc
, NULL
);
716 if (MF_DBGLEVEL
>= 1) Dbprintf("AcquireNonces: Auth2 error len=%d", len
);
720 // send an incomplete dummy response in order to trigger the card's authentication failure timeout
721 uint8_t dummy_answer
[1] = {0};
722 ReaderTransmit(dummy_answer
, 1, NULL
);
724 timeout
= GetCountSspClk() + HARDNESTED_AUTHENTICATION_TIMEOUT
;
727 if (num_nonces
% 2) {
728 memcpy(buf
+i
, receivedAnswer
, 4);
729 nt_par_enc
= par_enc
[0] & 0xf0;
731 nt_par_enc
|= par_enc
[0] >> 4;
732 memcpy(buf
+i
+4, receivedAnswer
, 4);
733 memcpy(buf
+i
+8, &nt_par_enc
, 1);
737 // wait for the card to become ready again
738 while(GetCountSspClk() < timeout
);
744 crypto1_destroy(pcs
);
747 cmd_send(CMD_ACK
, isOK
, cuid
, num_nonces
, buf
, sizeof(buf
));
750 if (MF_DBGLEVEL
>= 3) DbpString("AcquireEncryptedNonces finished");
753 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
759 //-----------------------------------------------------------------------------
760 // MIFARE nested authentication.
762 //-----------------------------------------------------------------------------
763 void MifareNested(uint32_t arg0
, uint32_t arg1
, uint32_t calibrate
, uint8_t *datain
)
766 uint8_t blockNo
= arg0
& 0xff;
767 uint8_t keyType
= (arg0
>> 8) & 0xff;
768 uint8_t targetBlockNo
= arg1
& 0xff;
769 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
770 uint64_t ui64Key
= 0;
772 ui64Key
= bytes_to_num(datain
, 6);
775 uint16_t rtr
, i
, j
, len
;
777 static uint16_t dmin
, dmax
;
779 uint32_t cuid
, nt1
, nt2
, nttmp
, nttest
, ks1
;
781 uint32_t target_nt
[2], target_ks
[2];
782 uint8_t target_nt_duplicate_count
= 0;
784 uint8_t par_array
[4];
786 struct Crypto1State mpcs
= {0, 0};
787 struct Crypto1State
*pcs
;
789 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
791 uint32_t auth1_time
, auth2_time
;
792 static uint16_t delta_time
;
796 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
798 // free eventually allocated BigBuf memory
801 if (calibrate
) clear_trace();
804 // statistics on nonce distance
806 #define NESTED_MAX_TRIES 12
807 uint16_t unsuccessfull_tries
= 0;
808 if (calibrate
) { // for first call only. Otherwise reuse previous calibration
816 for (rtr
= 0; rtr
< 17; rtr
++) {
818 // prepare next select. No need to power down the card.
819 if(mifare_classic_halt(pcs
, cuid
)) {
820 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
825 // Test if the action was cancelled
831 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
832 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
838 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
839 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
845 auth2_time
= auth1_time
+ delta_time
;
849 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_NESTED
, &nt2
, &auth2_time
)) {
850 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error");
855 nttmp
= prng_successor(nt1
, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160
856 for (i
= 101; i
< 1200; i
++) {
857 nttmp
= prng_successor(nttmp
, 1);
858 if (nttmp
== nt2
) break;
868 delta_time
= auth2_time
- auth1_time
+ 32; // allow some slack for proper timing
870 if (MF_DBGLEVEL
>= 3) Dbprintf("Nested: calibrating... ntdist=%d", i
);
872 unsuccessfull_tries
++;
873 if (unsuccessfull_tries
> NESTED_MAX_TRIES
) { // card isn't vulnerable to nested attack (random numbers are not predictable)
879 davg
= (davg
+ (rtr
- 1)/2) / (rtr
- 1);
881 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
);
889 // -------------------------------------------------------------------------------------------------
893 // get crypted nonces for target sector
894 for(i
=0; i
< 2 && !isOK
; i
++) { // look for exactly two different nonces
897 while(target_nt
[i
] == 0 && !isOK
) { // continue until we have an unambiguous nonce
899 // prepare next select. No need to power down the card.
900 if(mifare_classic_halt(pcs
, cuid
)) {
901 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
905 // break out of the loop on button press
911 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
912 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
917 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
918 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
922 // nested authentication
923 auth2_time
= auth1_time
+ delta_time
;
924 len
= mifare_sendcmd_short(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType
& 0x01), targetBlockNo
, receivedAnswer
, par
, &auth2_time
);
926 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error len=%d", len
);
930 nt2
= bytes_to_num(receivedAnswer
, 4);
931 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i
+1, nt1
, nt2
, par
[0]);
933 // Parity validity check
934 for (j
= 0; j
< 4; j
++) {
935 par_array
[j
] = (oddparity8(receivedAnswer
[j
]) != ((par
[0] >> (7-j
)) & 0x01));
939 nttest
= prng_successor(nt1
, dmin
- 1);
940 for (j
= dmin
; j
< dmax
+ 1; j
++) {
941 nttest
= prng_successor(nttest
, 1);
944 if (valid_nonce(nttest
, nt2
, ks1
, par_array
)){
945 if (ncount
> 0) { // we are only interested in disambiguous nonces, try again
946 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i
+1, j
);
950 target_nt
[i
] = nttest
;
953 if (i
== 1 && target_nt
[1] == target_nt
[0]) { // we need two different nonces
954 if( ++target_nt_duplicate_count
>= NESTED_MAX_TRIES
) { // unable to get a 2nd nonce after NESTED_MAX_TRIES tries, probably a fixed nonce
955 if (MF_DBGLEVEL
>= 2) Dbprintf("Nonce#2: cannot get nonce that != nonce#1, continuing anyway with single nonce! ntdist=%d", j
);
960 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j
);
963 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i
+1, j
);
966 if (target_nt
[i
] == 0 && j
== dmax
+1 && MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i
+1);
972 // ----------------------------- crypto1 destroy
973 crypto1_destroy(pcs
);
975 byte_t buf
[4 + 4 * 4];
976 memcpy(buf
, &cuid
, 4);
977 memcpy(buf
+4, &target_nt
[0], 4);
978 memcpy(buf
+8, &target_ks
[0], 4);
979 memcpy(buf
+12, &target_nt
[1], 4);
980 memcpy(buf
+16, &target_ks
[1], 4);
983 cmd_send(CMD_ACK
, isOK
, 0, targetBlockNo
+ (targetKeyType
* 0x100), buf
, sizeof(buf
));
986 if (MF_DBGLEVEL
>= 3) DbpString("NESTED FINISHED");
988 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
992 //-----------------------------------------------------------------------------
993 // MIFARE check keys. key count up to 85.
995 //-----------------------------------------------------------------------------
996 void MifareChkKeys(uint16_t arg0
, uint16_t arg1
, uint8_t arg2
, uint8_t *datain
)
998 uint8_t blockNo
= arg0
& 0xff;
999 uint8_t keyType
= (arg0
>> 8) & 0xff;
1000 bool clearTrace
= arg1
& 0x01;
1001 bool multisectorCheck
= arg1
& 0x02;
1002 uint8_t set14aTimeout
= (arg1
>> 8) & 0xff;
1003 uint8_t keyCount
= arg2
;
1007 // clear debug level
1008 int OLD_MF_DBGLEVEL
= MF_DBGLEVEL
;
1009 MF_DBGLEVEL
= MF_DBG_NONE
;
1011 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1019 iso14a_set_timeout(set14aTimeout
* 10); // timeout: ms = x/106 35-minimum, 50-OK 106-recommended 500-safe
1022 if (multisectorCheck
) {
1023 TKeyIndex keyIndex
= {{0}};
1024 uint8_t sectorCnt
= blockNo
;
1025 int res
= MifareMultisectorChk(datain
, keyCount
, sectorCnt
, keyType
, OLD_MF_DBGLEVEL
, &keyIndex
);
1028 cmd_send(CMD_ACK
, 1, res
, 0, keyIndex
, 80);
1030 cmd_send(CMD_ACK
, 0, res
, 0, NULL
, 0);
1033 int res
= MifareChkBlockKeys(datain
, keyCount
, blockNo
, keyType
, OLD_MF_DBGLEVEL
);
1036 cmd_send(CMD_ACK
, 1, res
, 0, datain
+ (res
- 1) * 6, 6);
1038 cmd_send(CMD_ACK
, 0, res
, 0, NULL
, 0);
1042 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1045 // restore debug level
1046 MF_DBGLEVEL
= OLD_MF_DBGLEVEL
;
1052 //-----------------------------------------------------------------------------
1053 // MIFARE Personalize UID. Only for Mifare Classic EV1 7Byte UID
1054 //-----------------------------------------------------------------------------
1055 void MifarePersonalizeUID(uint8_t keyType
, uint8_t perso_option
, uint8_t *data
) {
1059 struct Crypto1State mpcs
= {0, 0};
1060 struct Crypto1State
*pcs
;
1066 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1070 if (!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
1071 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1075 uint8_t block_number
= 0;
1076 uint64_t key
= bytes_to_num(data
, 6);
1077 if (mifare_classic_auth(pcs
, cuid
, block_number
, keyType
, key
, AUTH_FIRST
)) {
1078 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
1082 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1083 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1084 int len
= mifare_sendcmd_short(pcs
, true, MIFARE_EV1_PERSONAL_UID
, perso_option
, receivedAnswer
, receivedAnswerPar
, NULL
);
1085 if (len
!= 1 || receivedAnswer
[0] != CARD_ACK
) {
1086 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
1093 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1096 crypto1_destroy(pcs
);
1098 if (MF_DBGLEVEL
>= 2) DbpString("PERSONALIZE UID FINISHED");
1100 cmd_send(CMD_ACK
, isOK
, 0, 0, NULL
, 0);
1105 //-----------------------------------------------------------------------------
1106 // MIFARE commands set debug level
1108 //-----------------------------------------------------------------------------
1109 void MifareSetDbgLvl(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1111 Dbprintf("Debug level: %d", MF_DBGLEVEL
);
1114 //-----------------------------------------------------------------------------
1115 // Work with emulator memory
1117 // Note: we call FpgaDownloadAndGo(FPGA_BITSTREAM_HF) here although FPGA is not
1118 // involved in dealing with emulator memory. But if it is called later, it might
1119 // destroy the Emulator Memory.
1120 //-----------------------------------------------------------------------------
1122 void MifareEMemClr(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1123 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1127 void MifareEMemSet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1128 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1129 emlSetMem(datain
, arg0
, arg1
); // data, block num, blocks count
1132 void MifareEMemGet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1133 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1134 byte_t buf
[USB_CMD_DATA_SIZE
];
1135 emlGetMem(buf
, arg0
, arg1
); // data, block num, blocks count (max 4)
1138 cmd_send(CMD_ACK
,arg0
,arg1
,0,buf
,USB_CMD_DATA_SIZE
);
1142 //-----------------------------------------------------------------------------
1143 // Load a card into the emulator memory
1145 //-----------------------------------------------------------------------------
1146 void MifareECardLoad(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1147 uint8_t numSectors
= arg0
;
1148 uint8_t keyType
= arg1
;
1149 uint64_t ui64Key
= 0;
1151 struct Crypto1State mpcs
= {0, 0};
1152 struct Crypto1State
*pcs
;
1156 byte_t dataoutbuf
[16];
1157 byte_t dataoutbuf2
[16];
1163 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1170 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
1172 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1175 for (uint8_t sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
1176 ui64Key
= emlGetKey(sectorNo
, keyType
);
1178 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
1180 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo
);
1184 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_NESTED
)) {
1186 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo
);
1191 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
1192 if(isOK
&& mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
)) {
1194 if (MF_DBGLEVEL
>= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo
, blockNo
);
1198 if (blockNo
< NumBlocksPerSector(sectorNo
) - 1) {
1199 emlSetMem(dataoutbuf
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
1200 } else { // sector trailer, keep the keys, set only the AC
1201 emlGetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
1202 memcpy(&dataoutbuf2
[6], &dataoutbuf
[6], 4);
1203 emlSetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
1210 if(mifare_classic_halt(pcs
, cuid
)) {
1211 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1214 // ----------------------------- crypto1 destroy
1215 crypto1_destroy(pcs
);
1217 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1220 if (MF_DBGLEVEL
>= 2) DbpString("EMUL FILL SECTORS FINISHED");
1225 //-----------------------------------------------------------------------------
1226 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
1228 //-----------------------------------------------------------------------------
1230 static bool isBlockTrailer(int blockN
) {
1231 if (blockN
>= 0 && blockN
< 128) {
1232 return ((blockN
& 0x03) == 0x03);
1234 if (blockN
>= 128 && blockN
<= 256) {
1235 return ((blockN
& 0x0F) == 0x0F);
1240 void MifareCWipe(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1243 uint32_t numBlocks
= arg0
;
1245 // bit 0 - wipe gen1a
1246 // bit 1 - fill card with default data
1247 // bit 2 - gen1a = 0, gen1b = 1
1248 uint8_t cmdParams
= arg1
;
1249 bool needWipe
= cmdParams
& 0x01;
1250 bool needFill
= cmdParams
& 0x02;
1251 bool gen1b
= cmdParams
& 0x04;
1253 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1254 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1256 uint8_t block0
[16] = {0x01, 0x02, 0x03, 0x04, 0x04, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBE, 0xAF};
1257 uint8_t block1
[16] = {0x00};
1258 uint8_t blockK
[16] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x08, 0x77, 0x8F, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1259 uint8_t d_block
[18] = {0x00};
1262 uint8_t wupC1
[] = { 0x40 };
1263 uint8_t wupC2
[] = { 0x43 };
1264 uint8_t wipeC
[] = { 0x41 };
1270 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1279 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1280 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != CARD_ACK
)) {
1281 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1285 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
1286 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != CARD_ACK
)) {
1287 if (MF_DBGLEVEL
>= 1) Dbprintf("wipeC error");
1291 if(mifare_classic_halt(NULL
, 0)) {
1292 if (MF_DBGLEVEL
> 2) Dbprintf("Halt error");
1299 ReaderTransmitBitsPar(wupC1
, 7, 0, NULL
);
1301 // gen1b magic tag : do no issue wupC2 and don't expect CARD_ACK response after SELECT_UID (after getting UID from chip in 'hf mf csetuid' command)
1304 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != CARD_ACK
)) {
1305 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1309 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1310 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != CARD_ACK
)) {
1311 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1316 // send blocks command
1317 for (int blockNo
= 0; blockNo
< numBlocks
; blockNo
++) {
1318 if ((mifare_sendcmd_short(NULL
, 0, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 1) || (receivedAnswer
[0] != CARD_ACK
)) {
1319 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send command error");
1323 // check type of block and add crc
1324 if (!isBlockTrailer(blockNo
)){
1325 memcpy(d_block
, block1
, 16);
1327 memcpy(d_block
, blockK
, 16);
1330 memcpy(d_block
, block0
, 16);
1332 AppendCrc14443a(d_block
, 16);
1334 // send write command
1335 ReaderTransmit(d_block
, sizeof(d_block
), NULL
);
1336 if ((ReaderReceive(receivedAnswer
, receivedAnswerPar
) != 1) || (receivedAnswer
[0] != CARD_ACK
)) {
1337 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send data error");
1343 // do no issue halt command for gen1b
1345 if (mifare_classic_halt(NULL
, 0)) {
1346 if (MF_DBGLEVEL
> 2) Dbprintf("Halt error");
1354 // send USB response
1356 cmd_send(CMD_ACK
,isOK
,0,0,NULL
,0);
1360 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1366 void MifareCSetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1369 uint8_t needWipe
= arg0
;
1370 // bit 0 - need get UID
1371 // bit 1 - need wupC
1372 // bit 2 - need HALT after sequence
1373 // bit 3 - need init FPGA and field before sequence
1374 // bit 4 - need reset FPGA and LED
1375 // bit 6 - gen1b backdoor type
1376 uint8_t workFlags
= arg1
;
1377 uint8_t blockNo
= arg2
;
1380 uint8_t wupC1
[] = { 0x40 };
1381 uint8_t wupC2
[] = { 0x43 };
1382 uint8_t wipeC
[] = { 0x41 };
1386 uint8_t uid
[10] = {0x00};
1387 uint8_t d_block
[18] = {0x00};
1390 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1391 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1393 // reset FPGA and LED
1394 if (workFlags
& 0x08) {
1398 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1406 // get UID from chip
1407 if (workFlags
& 0x01) {
1408 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
1409 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1410 // 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.
1414 if(mifare_classic_halt(NULL
, cuid
)) {
1415 if (MF_DBGLEVEL
> 2) Dbprintf("Halt error");
1416 // Continue, some magic tags misbehavies and send an answer to it.
1422 // Wipe command don't work with gen1b
1423 if (needWipe
&& !(workFlags
& 0x40)){
1424 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1425 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != CARD_ACK
)) {
1426 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1430 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
1431 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != CARD_ACK
)) {
1432 if (MF_DBGLEVEL
>= 1) Dbprintf("wipeC error");
1436 if(mifare_classic_halt(NULL
, 0)) {
1437 if (MF_DBGLEVEL
> 2) Dbprintf("Halt error");
1438 // Continue, some magic tags misbehavies and send an answer to it.
1444 if (workFlags
& 0x02) {
1445 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1447 // gen1b magic tag : do no issue wupC2 and don't expect CARD_ACK response after SELECT_UID (after getting UID from chip in 'hf mf csetuid' command)
1448 if (!(workFlags
& 0x40)) {
1450 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != CARD_ACK
)) {
1451 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1455 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1456 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != CARD_ACK
)) {
1457 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1463 if ((mifare_sendcmd_short(NULL
, 0, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 1) || (receivedAnswer
[0] != CARD_ACK
)) {
1464 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send command error");
1468 memcpy(d_block
, datain
, 16);
1469 AppendCrc14443a(d_block
, 16);
1471 ReaderTransmit(d_block
, sizeof(d_block
), NULL
);
1472 if ((ReaderReceive(receivedAnswer
, receivedAnswerPar
) != 1) || (receivedAnswer
[0] != CARD_ACK
)) {
1473 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send data error");
1477 if (workFlags
& 0x04) {
1478 // do no issue halt command for gen1b magic tag (#db# halt error. response len: 1)
1479 if (!(workFlags
& 0x40)) {
1480 if (mifare_classic_halt(NULL
, 0)) {
1481 if (MF_DBGLEVEL
> 2) Dbprintf("Halt error");
1482 // Continue, some magic tags misbehavies and send an answer to it.
1493 cmd_send(CMD_ACK
,isOK
,0,0,uid
,4);
1496 if ((workFlags
& 0x10) || (!isOK
)) {
1497 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1503 void MifareCGetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1506 // bit 1 - need wupC
1507 // bit 2 - need HALT after sequence
1508 // bit 3 - need init FPGA and field before sequence
1509 // bit 4 - need reset FPGA and LED
1510 // bit 5 - need to set datain instead of issuing USB reply (called via ARM for StandAloneMode14a)
1511 // bit 6 - gen1b backdoor type
1512 uint8_t workFlags
= arg0
;
1513 uint8_t blockNo
= arg2
;
1516 uint8_t wupC1
[] = { 0x40 };
1517 uint8_t wupC2
[] = { 0x43 };
1521 uint8_t data
[18] = {0x00};
1524 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1525 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1527 if (workFlags
& 0x08) {
1531 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1538 if (workFlags
& 0x02) {
1539 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1540 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != CARD_ACK
)) {
1541 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1544 // do no issue for gen1b magic tag
1545 if (!(workFlags
& 0x40)) {
1546 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1547 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != CARD_ACK
)) {
1548 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1555 if ((mifare_sendcmd_short(NULL
, 0, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 18)) {
1556 if (MF_DBGLEVEL
>= 1) Dbprintf("read block send command error");
1559 memcpy(data
, receivedAnswer
, 18);
1561 if (workFlags
& 0x04) {
1562 // do no issue halt command for gen1b magic tag (#db# halt error. response len: 1)
1563 if (!(workFlags
& 0x40)) {
1564 if (mifare_classic_halt(NULL
, cuid
)) {
1565 if (MF_DBGLEVEL
> 1) Dbprintf("Halt error");
1566 // Continue, some magic tags misbehavies and send an answer to it.
1577 if (workFlags
& 0x20) {
1579 memcpy(datain
, data
, 18);
1582 cmd_send(CMD_ACK
,isOK
,0,0,data
,18);
1585 if ((workFlags
& 0x10) || (!isOK
)) {
1586 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1591 void MifareCIdent(){
1594 uint8_t wupC1
[] = { 0x40 };
1595 uint8_t wupC2
[] = { 0x43 };
1600 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1601 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1606 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1611 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1612 if(ReaderReceive(receivedAnswer
, receivedAnswerPar
) && (receivedAnswer
[0] == CARD_ACK
)) {
1615 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1616 if(ReaderReceive(receivedAnswer
, receivedAnswerPar
) && (receivedAnswer
[0] == CARD_ACK
)) {
1621 // From iceman1001: removed the if, since some magic tags misbehavies and send an answer to it.
1622 mifare_classic_halt(NULL
, 0);
1625 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
1628 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1636 void Mifare_DES_Auth1(uint8_t arg0
, uint8_t *datain
){
1638 byte_t dataout
[11] = {0x00};
1639 uint8_t uid
[10] = {0x00};
1642 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1645 int len
= iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true);
1647 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
1652 if(mifare_desfire_des_auth1(cuid
, dataout
)){
1653 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication part1: Fail.");
1658 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 1 FINISHED");
1659 cmd_send(CMD_ACK
,1,cuid
,0,dataout
, sizeof(dataout
));
1662 void Mifare_DES_Auth2(uint32_t arg0
, uint8_t *datain
){
1664 uint32_t cuid
= arg0
;
1665 uint8_t key
[16] = {0x00};
1667 byte_t dataout
[12] = {0x00};
1669 memcpy(key
, datain
, 16);
1671 isOK
= mifare_desfire_des_auth2(cuid
, key
, dataout
);
1674 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) Dbprintf("Authentication part2: Failed");
1679 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 2 FINISHED");
1681 cmd_send(CMD_ACK
, isOK
, 0, 0, dataout
, sizeof(dataout
));
1682 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);