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
, NULL
)) {
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");
114 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
117 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16);
123 void MifareUC_Auth(uint8_t arg0
, uint8_t *keybytes
){
126 bool turnOffField
= (arg0
== 1);
128 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
130 if (!iso14443a_select_card(NULL
, NULL
, NULL
, true, 0, true)) {
131 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
136 if (!mifare_ultra_auth(keybytes
)){
137 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication failed");
143 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
147 cmd_send(CMD_ACK
,1,0,0,0,0);
152 // Arg1 = UsePwd bool
153 // datain = PWD bytes,
154 void MifareUReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
)
158 uint8_t blockNo
= arg0
;
159 byte_t dataout
[16] = {0x00};
160 bool useKey
= (arg1
== 1); //UL_C
161 bool usePwd
= (arg1
== 2); //UL_EV1/NTAG
163 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
165 int len
= iso14443a_select_card(NULL
, NULL
, NULL
, true, 0, true);
167 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%02X)",len
);
172 // UL-C authentication
174 uint8_t key
[16] = {0x00};
175 memcpy(key
, datain
, sizeof(key
) );
177 if ( !mifare_ultra_auth(key
) ) {
183 // UL-EV1 / NTAG authentication
185 uint8_t pwd
[4] = {0x00};
186 memcpy(pwd
, datain
, 4);
187 uint8_t pack
[4] = {0,0,0,0};
188 if (!mifare_ul_ev1_auth(pwd
, pack
)) {
194 if (mifare_ultra_readblock(blockNo
, dataout
)) {
195 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Read block error");
200 if (mifare_ultra_halt()) {
201 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Halt error");
206 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
209 cmd_send(CMD_ACK
,1,0,0,dataout
,16);
213 //-----------------------------------------------------------------------------
214 // Select, Authenticate, Read a MIFARE tag.
215 // read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)
216 //-----------------------------------------------------------------------------
217 void MifareReadSector(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
220 uint8_t sectorNo
= arg0
;
221 uint8_t keyType
= arg1
;
222 uint64_t ui64Key
= 0;
223 ui64Key
= bytes_to_num(datain
, 6);
227 byte_t dataoutbuf
[16 * 16];
230 struct Crypto1State mpcs
= {0, 0};
231 struct Crypto1State
*pcs
;
234 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
243 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
245 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
249 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
, NULL
)) {
251 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
254 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
255 if(mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
+ 16 * blockNo
)) {
257 if (MF_DBGLEVEL
>= 1) Dbprintf("Read sector %2d block %2d error", sectorNo
, blockNo
);
262 if(mifare_classic_halt(pcs
, cuid
)) {
263 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
266 // ----------------------------- crypto1 destroy
267 crypto1_destroy(pcs
);
269 if (MF_DBGLEVEL
>= 2) DbpString("READ SECTOR FINISHED");
272 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
275 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16*NumBlocksPerSector(sectorNo
));
281 // arg0 = blockNo (start)
282 // arg1 = Pages (number of blocks)
284 // datain = KEY bytes
285 void MifareUReadCard(uint8_t arg0
, uint16_t arg1
, uint8_t arg2
, uint8_t *datain
)
288 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
290 // free eventually allocated BigBuf memory
294 uint8_t blockNo
= arg0
;
295 uint16_t blocks
= arg1
;
296 bool useKey
= (arg2
== 1); //UL_C
297 bool usePwd
= (arg2
== 2); //UL_EV1/NTAG
298 uint32_t countblocks
= 0;
299 uint8_t *dataout
= BigBuf_malloc(CARD_MEMORY_SIZE
);
300 if (dataout
== NULL
){
301 Dbprintf("out of memory");
306 int len
= iso14443a_select_card(NULL
, NULL
, NULL
, true, 0, true);
308 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%d)",len
);
313 // UL-C authentication
315 uint8_t key
[16] = {0x00};
316 memcpy(key
, datain
, sizeof(key
) );
318 if ( !mifare_ultra_auth(key
) ) {
324 // UL-EV1 / NTAG authentication
326 uint8_t pwd
[4] = {0x00};
327 memcpy(pwd
, datain
, sizeof(pwd
));
328 uint8_t pack
[4] = {0,0,0,0};
330 if (!mifare_ul_ev1_auth(pwd
, pack
)){
336 for (int i
= 0; i
< blocks
; i
++){
337 if ((i
*4) + 4 >= CARD_MEMORY_SIZE
) {
338 Dbprintf("Data exceeds buffer!!");
342 len
= mifare_ultra_readblock(blockNo
+ i
, dataout
+ 4 * i
);
345 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Read block %d error",i
);
346 // if no blocks read - error out
351 //stop at last successful read block and return what we got
359 len
= mifare_ultra_halt();
361 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Halt error");
366 if (MF_DBGLEVEL
>= MF_DBG_DEBUG
) Dbprintf("Blocks read %d", countblocks
);
368 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
371 cmd_send(CMD_ACK
, 1, countblocks
*4, BigBuf_max_traceLen(), 0, 0);
377 //-----------------------------------------------------------------------------
378 // Select, Authenticate, Write a MIFARE tag.
380 //-----------------------------------------------------------------------------
381 void MifareWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
384 uint8_t blockNo
= arg0
;
385 uint8_t keyType
= arg1
;
386 uint64_t ui64Key
= 0;
387 byte_t blockdata
[16];
389 ui64Key
= bytes_to_num(datain
, 6);
390 memcpy(blockdata
, datain
+ 10, 16);
396 struct Crypto1State mpcs
= {0, 0};
397 struct Crypto1State
*pcs
;
400 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
409 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
410 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
414 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, NULL
)) {
415 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
419 if(mifare_classic_writeblock(pcs
, cuid
, blockNo
, blockdata
)) {
420 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
424 if(mifare_classic_halt(pcs
, cuid
)) {
425 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
433 // ----------------------------- crypto1 destroy
434 crypto1_destroy(pcs
);
436 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
439 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
442 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
449 /* // Command not needed but left for future testing
450 void MifareUWriteBlockCompat(uint8_t arg0, uint8_t *datain)
452 uint8_t blockNo = arg0;
453 byte_t blockdata[16] = {0x00};
455 memcpy(blockdata, datain, 16);
457 uint8_t uid[10] = {0x00};
459 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
462 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
464 if(!iso14443a_select_card(uid, NULL, NULL, true, 0)) {
465 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
470 if(mifare_ultra_writeblock_compat(blockNo, blockdata)) {
471 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
475 if(mifare_ultra_halt()) {
476 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
481 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");
483 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
485 cmd_send(CMD_ACK,1,0,0,0,0);
490 // Arg0 : Block to write to.
491 // Arg1 : 0 = use no authentication.
492 // 1 = use 0x1A authentication.
493 // 2 = use 0x1B authentication.
494 // datain : 4 first bytes is data to be written.
495 // : 4/16 next bytes is authentication key.
496 void MifareUWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
)
498 uint8_t blockNo
= arg0
;
499 bool useKey
= (arg1
== 1); //UL_C
500 bool usePwd
= (arg1
== 2); //UL_EV1/NTAG
501 byte_t blockdata
[4] = {0x00};
503 memcpy(blockdata
, datain
,4);
507 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
511 if(!iso14443a_select_card(NULL
, NULL
, NULL
, true, 0, true)) {
512 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
517 // UL-C authentication
519 uint8_t key
[16] = {0x00};
520 memcpy(key
, datain
+4, sizeof(key
) );
522 if ( !mifare_ultra_auth(key
) ) {
528 // UL-EV1 / NTAG authentication
530 uint8_t pwd
[4] = {0x00};
531 memcpy(pwd
, datain
+4, 4);
532 uint8_t pack
[4] = {0,0,0,0};
533 if (!mifare_ul_ev1_auth(pwd
, pack
)) {
539 if(mifare_ultra_writeblock(blockNo
, blockdata
)) {
540 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
545 if(mifare_ultra_halt()) {
546 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
551 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
553 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
555 cmd_send(CMD_ACK
,1,0,0,0,0);
559 void MifareUSetPwd(uint8_t arg0
, uint8_t *datain
){
561 uint8_t pwd
[16] = {0x00};
562 byte_t blockdata
[4] = {0x00};
564 memcpy(pwd
, datain
, 16);
566 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
567 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
571 if(!iso14443a_select_card(NULL
, NULL
, NULL
, true, 0, true)) {
572 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
577 blockdata
[0] = pwd
[7];
578 blockdata
[1] = pwd
[6];
579 blockdata
[2] = pwd
[5];
580 blockdata
[3] = pwd
[4];
581 if(mifare_ultra_writeblock( 44, blockdata
)) {
582 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
587 blockdata
[0] = pwd
[3];
588 blockdata
[1] = pwd
[2];
589 blockdata
[2] = pwd
[1];
590 blockdata
[3] = pwd
[0];
591 if(mifare_ultra_writeblock( 45, blockdata
)) {
592 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
597 blockdata
[0] = pwd
[15];
598 blockdata
[1] = pwd
[14];
599 blockdata
[2] = pwd
[13];
600 blockdata
[3] = pwd
[12];
601 if(mifare_ultra_writeblock( 46, blockdata
)) {
602 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
607 blockdata
[0] = pwd
[11];
608 blockdata
[1] = pwd
[10];
609 blockdata
[2] = pwd
[9];
610 blockdata
[3] = pwd
[8];
611 if(mifare_ultra_writeblock( 47, blockdata
)) {
612 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
617 if(mifare_ultra_halt()) {
618 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
623 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
625 cmd_send(CMD_ACK
,1,0,0,0,0);
629 // Return 1 if the nonce is invalid else return 0
630 int valid_nonce(uint32_t Nt
, uint32_t NtEnc
, uint32_t Ks1
, uint8_t *parity
) {
631 return ((oddparity8((Nt
>> 24) & 0xFF) == ((parity
[0]) ^ oddparity8((NtEnc
>> 24) & 0xFF) ^ BIT(Ks1
,16))) & \
632 (oddparity8((Nt
>> 16) & 0xFF) == ((parity
[1]) ^ oddparity8((NtEnc
>> 16) & 0xFF) ^ BIT(Ks1
,8))) & \
633 (oddparity8((Nt
>> 8) & 0xFF) == ((parity
[2]) ^ oddparity8((NtEnc
>> 8) & 0xFF) ^ BIT(Ks1
,0)))) ? 1 : 0;
637 //-----------------------------------------------------------------------------
638 // acquire encrypted nonces in order to perform the attack described in
639 // Carlo Meijer, Roel Verdult, "Ciphertext-only Cryptanalysis on Hardened
640 // Mifare Classic Cards" in Proceedings of the 22nd ACM SIGSAC Conference on
641 // Computer and Communications Security, 2015
642 //-----------------------------------------------------------------------------
643 void MifareAcquireEncryptedNonces(uint32_t arg0
, uint32_t arg1
, uint32_t flags
, uint8_t *datain
)
645 uint64_t ui64Key
= 0;
648 uint8_t cascade_levels
= 0;
649 struct Crypto1State mpcs
= {0, 0};
650 struct Crypto1State
*pcs
;
652 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
655 uint8_t nt_par_enc
= 0;
656 uint8_t buf
[USB_CMD_DATA_SIZE
];
659 uint8_t blockNo
= arg0
& 0xff;
660 uint8_t keyType
= (arg0
>> 8) & 0xff;
661 uint8_t targetBlockNo
= arg1
& 0xff;
662 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
663 ui64Key
= bytes_to_num(datain
, 6);
664 bool initialize
= flags
& 0x0001;
665 bool slow
= flags
& 0x0002;
666 bool field_off
= flags
& 0x0004;
672 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
679 uint16_t num_nonces
= 0;
680 bool have_uid
= false;
681 for (uint16_t i
= 0; i
<= USB_CMD_DATA_SIZE
- 9; ) {
683 // Test if the action was cancelled
690 if (!have_uid
) { // need a full select cycle to get the uid first
691 iso14a_card_select_t card_info
;
692 if(!iso14443a_select_card(uid
, &card_info
, &cuid
, true, 0, true)) {
693 if (MF_DBGLEVEL
>= 1) Dbprintf("AcquireNonces: Can't select card (ALL)");
696 switch (card_info
.uidlen
) {
697 case 4 : cascade_levels
= 1; break;
698 case 7 : cascade_levels
= 2; break;
699 case 10: cascade_levels
= 3; break;
703 } else { // no need for anticollision. We can directly select the card
704 if(!iso14443a_select_card(uid
, NULL
, NULL
, false, cascade_levels
, true)) {
705 if (MF_DBGLEVEL
>= 1) Dbprintf("AcquireNonces: Can't select card (UID)");
711 timeout
= GetCountSspClk() + HARDNESTED_PRE_AUTHENTICATION_LEADTIME
;
712 while(GetCountSspClk() < timeout
);
716 if (mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, NULL
, NULL
)) {
717 if (MF_DBGLEVEL
>= 1) Dbprintf("AcquireNonces: Auth1 error");
721 // nested authentication
722 uint16_t len
= mifare_sendcmd_short(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType
& 0x01), targetBlockNo
, receivedAnswer
, par_enc
, NULL
);
724 if (MF_DBGLEVEL
>= 1) Dbprintf("AcquireNonces: Auth2 error len=%d", len
);
728 // send an incomplete dummy response in order to trigger the card's authentication failure timeout
729 uint8_t dummy_answer
[1] = {0};
730 ReaderTransmit(dummy_answer
, 1, NULL
);
732 timeout
= GetCountSspClk() + HARDNESTED_AUTHENTICATION_TIMEOUT
;
735 if (num_nonces
% 2) {
736 memcpy(buf
+i
, receivedAnswer
, 4);
737 nt_par_enc
= par_enc
[0] & 0xf0;
739 nt_par_enc
|= par_enc
[0] >> 4;
740 memcpy(buf
+i
+4, receivedAnswer
, 4);
741 memcpy(buf
+i
+8, &nt_par_enc
, 1);
745 // wait for the card to become ready again
746 while(GetCountSspClk() < timeout
);
752 crypto1_destroy(pcs
);
755 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
760 cmd_send(CMD_ACK
, isOK
, cuid
, num_nonces
, buf
, sizeof(buf
));
763 if (MF_DBGLEVEL
>= 3) DbpString("AcquireEncryptedNonces finished");
768 //-----------------------------------------------------------------------------
769 // MIFARE nested authentication.
771 //-----------------------------------------------------------------------------
772 void MifareNested(uint32_t arg0
, uint32_t arg1
, uint32_t calibrate
, uint8_t *datain
) {
774 uint8_t blockNo
= arg0
& 0xff;
775 uint8_t keyType
= (arg0
>> 8) & 0xff;
776 uint8_t targetBlockNo
= arg1
& 0xff;
777 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
778 uint64_t ui64Key
= 0;
780 ui64Key
= bytes_to_num(datain
, 6);
782 uint16_t rtr
, i
, j
, len
;
784 static uint16_t dmin
, dmax
;
786 uint32_t cuid
, nt1
, nt2
, nttmp
, nttest
, ks1
;
788 uint32_t target_nt
[2], target_ks
[2];
789 uint8_t target_nt_duplicate_count
= 0;
791 uint8_t par_array
[4];
793 struct Crypto1State mpcs
= {0, 0};
794 struct Crypto1State
*pcs
;
796 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
798 uint32_t auth1_time
, auth2_time
, authentication_timeout
= 0;
799 static uint16_t delta_time
;
802 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
804 // free eventually allocated BigBuf memory
807 if (calibrate
) clear_trace();
810 // statistics on nonce distance
812 #define NESTED_MAX_TRIES 12
813 uint16_t unsuccessfull_tries
= 0;
814 if (calibrate
) { // for first call only. Otherwise reuse previous calibration
822 for (rtr
= 0; rtr
< 17; rtr
++) {
824 // prepare next select. No need to power down the card.
825 if (mifare_classic_halt(pcs
, cuid
)) {
826 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
831 // Test if the action was cancelled
832 if (BUTTON_PRESS()) {
837 if (!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
838 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
844 if (mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
, NULL
)) {
845 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
851 auth2_time
= auth1_time
+ delta_time
;
855 if (mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_NESTED
, &nt2
, &auth2_time
, NULL
)) {
856 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error");
861 nttmp
= prng_successor(nt1
, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160
862 for (i
= 101; i
< 1200; i
++) {
863 nttmp
= prng_successor(nttmp
, 1);
864 if (nttmp
== nt2
) break;
874 delta_time
= auth2_time
- auth1_time
+ 32; // allow some slack for proper timing
876 if (MF_DBGLEVEL
>= 3) Dbprintf("Nested: calibrating... ntdist=%d", i
);
878 unsuccessfull_tries
++;
879 if (unsuccessfull_tries
> NESTED_MAX_TRIES
) { // card isn't vulnerable to nested attack (random numbers are not predictable)
885 davg
= (davg
+ (rtr
- 1)/2) / (rtr
- 1);
887 if (MF_DBGLEVEL
>= 3) Dbprintf("rtr=%d isOK=%d min=%d max=%d avg=%d, delta_time=%d", rtr
, isOK
, dmin
, dmax
, davg
, delta_time
);
895 // -------------------------------------------------------------------------------------------------
899 // get crypted nonces for target sector
900 for (i
=0; i
< 2 && !isOK
; i
++) { // look for exactly two different nonces
903 while (target_nt
[i
] == 0 && !isOK
) { // continue until we have an unambiguous nonce
905 // prepare next select. No need to power down the card.
906 if(mifare_classic_halt(pcs
, cuid
)) {
907 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
911 // break out of the loop on button press
912 if (BUTTON_PRESS()) {
917 if (!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
918 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
923 authentication_timeout
= 0;
924 if (mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
, &authentication_timeout
)) {
925 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
929 // nested authentication
930 auth2_time
= auth1_time
+ delta_time
;
931 len
= mifare_sendcmd_short(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType
& 0x01), targetBlockNo
, receivedAnswer
, par
, &auth2_time
);
933 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error len=%d", len
);
937 nt2
= bytes_to_num(receivedAnswer
, 4);
938 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i
+1, nt1
, nt2
, par
[0]);
940 // Parity validity check
941 for (j
= 0; j
< 4; j
++) {
942 par_array
[j
] = (oddparity8(receivedAnswer
[j
]) != ((par
[0] >> (7-j
)) & 0x01));
946 nttest
= prng_successor(nt1
, dmin
- 1);
947 for (j
= dmin
; j
< dmax
+ 1; j
++) {
948 nttest
= prng_successor(nttest
, 1);
951 if (valid_nonce(nttest
, nt2
, ks1
, par_array
)){
952 if (ncount
> 0) { // we are only interested in disambiguous nonces, try again
953 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i
+1, j
);
957 target_nt
[i
] = nttest
;
960 if (i
== 1 && target_nt
[1] == target_nt
[0]) { // we need two different nonces
961 if( ++target_nt_duplicate_count
>= NESTED_MAX_TRIES
) { // unable to get a 2nd nonce after NESTED_MAX_TRIES tries, probably a fixed nonce
962 if (MF_DBGLEVEL
>= 2) Dbprintf("Nonce#2: cannot get nonce that != nonce#1, continuing anyway with single nonce! ntdist=%d", j
);
967 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j
);
970 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i
+1, j
);
973 if (target_nt
[i
] == 0 && j
== dmax
+1 && MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i
+1);
979 // ----------------------------- crypto1 destroy
980 crypto1_destroy(pcs
);
982 uint8_t buf
[4 + 4 * 4 + 4];
983 memcpy(buf
, &cuid
, 4);
984 memcpy(buf
+4, &target_nt
[0], 4);
985 memcpy(buf
+8, &target_ks
[0], 4);
986 memcpy(buf
+12, &target_nt
[1], 4);
987 memcpy(buf
+16, &target_ks
[1], 4);
988 memcpy(buf
+20, &authentication_timeout
, 4);
990 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
992 if (MF_DBGLEVEL
>= 3) DbpString("NESTED FINISHED");
995 cmd_send(CMD_ACK
, isOK
, 0, targetBlockNo
+ (targetKeyType
* 0x100), buf
, sizeof(buf
));
1002 //-----------------------------------------------------------------------------
1003 // MIFARE check keys. key count up to 85.
1005 //-----------------------------------------------------------------------------
1006 void MifareChkKeys(uint16_t arg0
, uint32_t arg1
, uint8_t arg2
, uint8_t *datain
) {
1008 uint8_t blockNo
= arg0
& 0xff;
1009 uint8_t keyType
= arg0
>> 8;
1010 bool clearTrace
= arg1
& 0x01;
1011 bool multisectorCheck
= arg1
& 0x02;
1012 bool init
= arg1
& 0x04;
1013 bool drop_field
= arg1
& 0x08;
1014 uint32_t auth_timeout
= arg1
>> 16;
1015 uint8_t keyCount
= arg2
;
1020 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1028 // clear debug level. We are expecting lots of authentication failures...
1029 int OLD_MF_DBGLEVEL
= MF_DBGLEVEL
;
1030 MF_DBGLEVEL
= MF_DBG_NONE
;
1033 if (multisectorCheck
) {
1034 TKeyIndex keyIndex
= {{0}};
1035 uint8_t sectorCnt
= blockNo
;
1036 res
= MifareMultisectorChk(datain
, keyCount
, sectorCnt
, keyType
, &auth_timeout
, OLD_MF_DBGLEVEL
, &keyIndex
);
1038 cmd_send(CMD_ACK
, 1, res
, 0, keyIndex
, 80);
1040 cmd_send(CMD_ACK
, 0, res
, 0, NULL
, 0);
1043 res
= MifareChkBlockKeys(datain
, keyCount
, blockNo
, keyType
, &auth_timeout
, OLD_MF_DBGLEVEL
);
1045 cmd_send(CMD_ACK
, 1, res
, 0, datain
+ (res
- 1) * 6, 6);
1047 cmd_send(CMD_ACK
, 0, res
, 0, NULL
, 0);
1051 if (drop_field
|| res
!= 0) {
1052 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1056 // restore debug level
1057 MF_DBGLEVEL
= OLD_MF_DBGLEVEL
;
1063 //-----------------------------------------------------------------------------
1064 // MIFARE Personalize UID. Only for Mifare Classic EV1 7Byte UID
1065 //-----------------------------------------------------------------------------
1066 void MifarePersonalizeUID(uint8_t keyType
, uint8_t perso_option
, uint8_t *data
) {
1070 struct Crypto1State mpcs
= {0, 0};
1071 struct Crypto1State
*pcs
;
1077 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1081 if (!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
1082 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1086 uint8_t block_number
= 0;
1087 uint64_t key
= bytes_to_num(data
, 6);
1088 if (mifare_classic_auth(pcs
, cuid
, block_number
, keyType
, key
, AUTH_FIRST
, NULL
)) {
1089 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
1093 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1094 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1095 int len
= mifare_sendcmd_short(pcs
, true, MIFARE_EV1_PERSONAL_UID
, perso_option
, receivedAnswer
, receivedAnswerPar
, NULL
);
1096 if (len
!= 1 || receivedAnswer
[0] != CARD_ACK
) {
1097 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
1104 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1107 crypto1_destroy(pcs
);
1109 if (MF_DBGLEVEL
>= 2) DbpString("PERSONALIZE UID FINISHED");
1111 cmd_send(CMD_ACK
, isOK
, 0, 0, NULL
, 0);
1116 //-----------------------------------------------------------------------------
1117 // MIFARE commands set debug level
1119 //-----------------------------------------------------------------------------
1120 void MifareSetDbgLvl(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1122 Dbprintf("Debug level: %d", MF_DBGLEVEL
);
1125 //-----------------------------------------------------------------------------
1126 // Work with emulator memory
1128 // Note: we call FpgaDownloadAndGo(FPGA_BITSTREAM_HF) here although FPGA is not
1129 // involved in dealing with emulator memory. But if it is called later, it might
1130 // destroy the Emulator Memory.
1131 //-----------------------------------------------------------------------------
1133 void MifareEMemClr(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1134 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1138 void MifareEMemSet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1139 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1140 emlSetMem(datain
, arg0
, arg1
); // data, block num, blocks count
1143 void MifareEMemGet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1144 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1145 byte_t buf
[USB_CMD_DATA_SIZE
];
1146 emlGetMem(buf
, arg0
, arg1
); // data, block num, blocks count (max 4)
1149 cmd_send(CMD_ACK
,arg0
,arg1
,0,buf
,USB_CMD_DATA_SIZE
);
1153 //-----------------------------------------------------------------------------
1154 // Load a card into the emulator memory
1156 //-----------------------------------------------------------------------------
1157 void MifareECardLoad(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1158 uint8_t numSectors
= arg0
;
1159 uint8_t keyType
= arg1
;
1160 uint64_t ui64Key
= 0;
1162 struct Crypto1State mpcs
= {0, 0};
1163 struct Crypto1State
*pcs
;
1167 byte_t dataoutbuf
[16];
1168 byte_t dataoutbuf2
[16];
1174 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1181 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
1183 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1186 for (uint8_t sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
1187 ui64Key
= emlGetKey(sectorNo
, keyType
);
1189 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
, NULL
)) {
1191 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo
);
1195 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_NESTED
, NULL
)) {
1197 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo
);
1202 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
1203 if(isOK
&& mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
)) {
1205 if (MF_DBGLEVEL
>= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo
, blockNo
);
1209 if (blockNo
< NumBlocksPerSector(sectorNo
) - 1) {
1210 emlSetMem(dataoutbuf
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
1211 } else { // sector trailer, keep the keys, set only the AC
1212 emlGetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
1213 memcpy(&dataoutbuf2
[6], &dataoutbuf
[6], 4);
1214 emlSetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
1221 if(mifare_classic_halt(pcs
, cuid
)) {
1222 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1225 // ----------------------------- crypto1 destroy
1226 crypto1_destroy(pcs
);
1228 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1231 if (MF_DBGLEVEL
>= 2) DbpString("EMUL FILL SECTORS FINISHED");
1236 //-----------------------------------------------------------------------------
1237 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
1239 //-----------------------------------------------------------------------------
1241 static bool isBlockTrailer(int blockN
) {
1242 if (blockN
>= 0 && blockN
< 128) {
1243 return ((blockN
& 0x03) == 0x03);
1245 if (blockN
>= 128 && blockN
<= 256) {
1246 return ((blockN
& 0x0F) == 0x0F);
1251 void MifareCWipe(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1254 uint32_t numBlocks
= arg0
;
1256 // bit 0 - wipe gen1a
1257 // bit 1 - fill card with default data
1258 // bit 2 - gen1a = 0, gen1b = 1
1259 uint8_t cmdParams
= arg1
;
1260 bool needWipe
= cmdParams
& 0x01;
1261 bool needFill
= cmdParams
& 0x02;
1262 bool gen1b
= cmdParams
& 0x04;
1264 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1265 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1267 uint8_t block0
[16] = {0x01, 0x02, 0x03, 0x04, 0x04, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBE, 0xAF};
1268 uint8_t block1
[16] = {0x00};
1269 uint8_t blockK
[16] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x08, 0x77, 0x8F, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1270 uint8_t d_block
[18] = {0x00};
1273 uint8_t wupC1
[] = { 0x40 };
1274 uint8_t wupC2
[] = { 0x43 };
1275 uint8_t wipeC
[] = { 0x41 };
1281 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1290 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1291 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != CARD_ACK
)) {
1292 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1296 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
1297 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != CARD_ACK
)) {
1298 if (MF_DBGLEVEL
>= 1) Dbprintf("wipeC error");
1302 if(mifare_classic_halt(NULL
, 0)) {
1303 if (MF_DBGLEVEL
> 2) Dbprintf("Halt error");
1310 ReaderTransmitBitsPar(wupC1
, 7, 0, NULL
);
1312 // gen1b magic tag : do no issue wupC2 and don't expect CARD_ACK response after SELECT_UID (after getting UID from chip in 'hf mf csetuid' command)
1315 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != CARD_ACK
)) {
1316 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1320 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1321 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != CARD_ACK
)) {
1322 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1327 // send blocks command
1328 for (int blockNo
= 0; blockNo
< numBlocks
; blockNo
++) {
1329 if ((mifare_sendcmd_short(NULL
, 0, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 1) || (receivedAnswer
[0] != CARD_ACK
)) {
1330 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send command error");
1334 // check type of block and add crc
1335 if (!isBlockTrailer(blockNo
)){
1336 memcpy(d_block
, block1
, 16);
1338 memcpy(d_block
, blockK
, 16);
1341 memcpy(d_block
, block0
, 16);
1343 AppendCrc14443a(d_block
, 16);
1345 // send write command
1346 ReaderTransmit(d_block
, sizeof(d_block
), NULL
);
1347 if ((ReaderReceive(receivedAnswer
, receivedAnswerPar
) != 1) || (receivedAnswer
[0] != CARD_ACK
)) {
1348 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send data error");
1354 // do no issue halt command for gen1b
1356 if (mifare_classic_halt(NULL
, 0)) {
1357 if (MF_DBGLEVEL
> 2) Dbprintf("Halt error");
1366 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1368 // send USB response
1370 cmd_send(CMD_ACK
,isOK
,0,0,NULL
,0);
1378 void MifareCSetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1381 uint8_t needWipe
= arg0
;
1382 // bit 0 - need get UID
1383 // bit 1 - need wupC
1384 // bit 2 - need HALT after sequence
1385 // bit 3 - need init FPGA and field before sequence
1386 // bit 4 - need reset FPGA and LED
1387 // bit 6 - gen1b backdoor type
1388 uint8_t workFlags
= arg1
;
1389 uint8_t blockNo
= arg2
;
1392 uint8_t wupC1
[] = { 0x40 };
1393 uint8_t wupC2
[] = { 0x43 };
1394 uint8_t wipeC
[] = { 0x41 };
1398 uint8_t uid
[10] = {0x00};
1399 uint8_t d_block
[18] = {0x00};
1402 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1403 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1405 // reset FPGA and LED
1406 if (workFlags
& 0x08) {
1410 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1418 // get UID from chip
1419 if (workFlags
& 0x01) {
1420 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
1421 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1422 // Continue, if we set wrong UID or wrong UID checksum or some ATQA or SAK we will can't select card. But we need to write block 0 to make card work.
1426 if(mifare_classic_halt(NULL
, cuid
)) {
1427 if (MF_DBGLEVEL
> 2) Dbprintf("Halt error");
1428 // Continue, some magic tags misbehavies and send an answer to it.
1434 // Wipe command don't work with gen1b
1435 if (needWipe
&& !(workFlags
& 0x40)){
1436 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1437 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != CARD_ACK
)) {
1438 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1442 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
1443 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != CARD_ACK
)) {
1444 if (MF_DBGLEVEL
>= 1) Dbprintf("wipeC error");
1448 if(mifare_classic_halt(NULL
, 0)) {
1449 if (MF_DBGLEVEL
> 2) Dbprintf("Halt error");
1450 // Continue, some magic tags misbehavies and send an answer to it.
1456 if (workFlags
& 0x02) {
1457 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1459 // gen1b magic tag : do no issue wupC2 and don't expect CARD_ACK response after SELECT_UID (after getting UID from chip in 'hf mf csetuid' command)
1460 if (!(workFlags
& 0x40)) {
1462 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != CARD_ACK
)) {
1463 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1467 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1468 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != CARD_ACK
)) {
1469 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1475 if ((mifare_sendcmd_short(NULL
, 0, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 1) || (receivedAnswer
[0] != CARD_ACK
)) {
1476 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send command error");
1480 memcpy(d_block
, datain
, 16);
1481 AppendCrc14443a(d_block
, 16);
1483 ReaderTransmit(d_block
, sizeof(d_block
), NULL
);
1484 if ((ReaderReceive(receivedAnswer
, receivedAnswerPar
) != 1) || (receivedAnswer
[0] != CARD_ACK
)) {
1485 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send data error");
1489 if (workFlags
& 0x04) {
1490 // do no issue halt command for gen1b magic tag (#db# halt error. response len: 1)
1491 if (!(workFlags
& 0x40)) {
1492 if (mifare_classic_halt(NULL
, 0)) {
1493 if (MF_DBGLEVEL
> 2) Dbprintf("Halt error");
1494 // Continue, some magic tags misbehavies and send an answer to it.
1504 if ((workFlags
& 0x10) || (!isOK
)) {
1505 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1509 cmd_send(CMD_ACK
,isOK
,0,0,uid
,4);
1516 void MifareCGetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1519 // bit 1 - need wupC
1520 // bit 2 - need HALT after sequence
1521 // bit 3 - need init FPGA and field before sequence
1522 // bit 4 - need reset FPGA and LED
1523 // bit 5 - need to set datain instead of issuing USB reply (called via ARM for StandAloneMode14a)
1524 // bit 6 - gen1b backdoor type
1525 uint8_t workFlags
= arg0
;
1526 uint8_t blockNo
= arg2
;
1529 uint8_t wupC1
[] = { 0x40 };
1530 uint8_t wupC2
[] = { 0x43 };
1534 uint8_t data
[18] = {0x00};
1537 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1538 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1540 if (workFlags
& 0x08) {
1544 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1551 if (workFlags
& 0x02) {
1552 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1553 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != CARD_ACK
)) {
1554 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1557 // do no issue for gen1b magic tag
1558 if (!(workFlags
& 0x40)) {
1559 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1560 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != CARD_ACK
)) {
1561 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1568 if ((mifare_sendcmd_short(NULL
, 0, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 18)) {
1569 if (MF_DBGLEVEL
>= 1) Dbprintf("read block send command error");
1572 memcpy(data
, receivedAnswer
, 18);
1574 if (workFlags
& 0x04) {
1575 // do no issue halt command for gen1b magic tag (#db# halt error. response len: 1)
1576 if (!(workFlags
& 0x40)) {
1577 if (mifare_classic_halt(NULL
, cuid
)) {
1578 if (MF_DBGLEVEL
> 1) Dbprintf("Halt error");
1579 // Continue, some magic tags misbehavies and send an answer to it.
1589 if ((workFlags
& 0x10) || (!isOK
)) {
1590 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1594 if (workFlags
& 0x20) {
1596 memcpy(datain
, data
, 18);
1599 cmd_send(CMD_ACK
,isOK
,0,0,data
,18);
1605 void MifareCIdent(){
1608 uint8_t wupC1
[] = { 0x40 };
1609 uint8_t wupC2
[] = { 0x43 };
1614 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1615 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1620 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1625 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1626 if(ReaderReceive(receivedAnswer
, receivedAnswerPar
) && (receivedAnswer
[0] == CARD_ACK
)) {
1629 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1630 if(ReaderReceive(receivedAnswer
, receivedAnswerPar
) && (receivedAnswer
[0] == CARD_ACK
)) {
1635 // From iceman1001: removed the if, since some magic tags misbehavies and send an answer to it.
1636 mifare_classic_halt(NULL
, 0);
1638 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1641 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
1651 void Mifare_DES_Auth1(uint8_t arg0
, uint8_t *datain
){
1653 byte_t dataout
[11] = {0x00};
1654 uint8_t uid
[10] = {0x00};
1657 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1660 int len
= iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true);
1662 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
1667 if(mifare_desfire_des_auth1(cuid
, dataout
)){
1668 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication part1: Fail.");
1673 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 1 FINISHED");
1675 cmd_send(CMD_ACK
, 1, cuid
, 0, dataout
, sizeof(dataout
));
1678 void Mifare_DES_Auth2(uint32_t arg0
, uint8_t *datain
){
1680 uint32_t cuid
= arg0
;
1681 uint8_t key
[16] = {0x00};
1683 byte_t dataout
[12] = {0x00};
1685 memcpy(key
, datain
, 16);
1687 isOK
= mifare_desfire_des_auth2(cuid
, key
, dataout
);
1690 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) Dbprintf("Authentication part2: Failed");
1695 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1697 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 2 FINISHED");
1699 cmd_send(CMD_ACK
, isOK
, 0, 0, dataout
, sizeof(dataout
));