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
) {
644 uint64_t ui64Key
= 0;
647 uint8_t cascade_levels
= 0;
648 struct Crypto1State mpcs
= {0, 0};
649 struct Crypto1State
*pcs
;
651 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
654 uint8_t nt_par_enc
= 0;
655 uint8_t buf
[USB_CMD_DATA_SIZE
];
658 uint8_t blockNo
= arg0
& 0xff;
659 uint8_t keyType
= (arg0
>> 8) & 0xff;
660 uint8_t targetBlockNo
= arg1
& 0xff;
661 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
662 ui64Key
= bytes_to_num(datain
, 6);
663 bool initialize
= flags
& 0x0001;
664 bool slow
= flags
& 0x0002;
665 bool field_off
= flags
& 0x0004;
670 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
675 uint16_t num_nonces
= 0;
676 bool have_uid
= false;
677 for (uint16_t i
= 0; i
<= USB_CMD_DATA_SIZE
- 9; ) {
679 // Test if the action was cancelled
686 if (!have_uid
) { // need a full select cycle to get the uid first
687 iso14a_card_select_t card_info
;
688 if(!iso14443a_select_card(uid
, &card_info
, &cuid
, true, 0, true)) {
689 if (MF_DBGLEVEL
>= 1) Dbprintf("AcquireNonces: Can't select card (ALL)");
692 switch (card_info
.uidlen
) {
693 case 4 : cascade_levels
= 1; break;
694 case 7 : cascade_levels
= 2; break;
695 case 10: cascade_levels
= 3; break;
699 } else { // no need for anticollision. We can directly select the card
700 if(!iso14443a_select_card(uid
, NULL
, NULL
, false, cascade_levels
, true)) {
701 if (MF_DBGLEVEL
>= 1) Dbprintf("AcquireNonces: Can't select card (UID)");
707 timeout
= GetCountSspClk() + HARDNESTED_PRE_AUTHENTICATION_LEADTIME
;
708 while(GetCountSspClk() < timeout
);
712 if (mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, NULL
, NULL
)) {
713 if (MF_DBGLEVEL
>= 1) Dbprintf("AcquireNonces: Auth1 error");
717 // nested authentication
718 uint16_t len
= mifare_sendcmd_short(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType
& 0x01), targetBlockNo
, receivedAnswer
, par_enc
, NULL
);
720 if (MF_DBGLEVEL
>= 1) Dbprintf("AcquireNonces: Auth2 error len=%d", len
);
724 // send an incomplete dummy response in order to trigger the card's authentication failure timeout
725 uint8_t dummy_answer
[1] = {0};
726 ReaderTransmit(dummy_answer
, 1, NULL
);
728 timeout
= GetCountSspClk() + HARDNESTED_AUTHENTICATION_TIMEOUT
;
731 if (num_nonces
% 2) {
732 memcpy(buf
+i
, receivedAnswer
, 4);
733 nt_par_enc
= par_enc
[0] & 0xf0;
735 nt_par_enc
|= par_enc
[0] >> 4;
736 memcpy(buf
+i
+4, receivedAnswer
, 4);
737 memcpy(buf
+i
+8, &nt_par_enc
, 1);
741 // wait for the card to become ready again
742 while(GetCountSspClk() < timeout
);
746 crypto1_destroy(pcs
);
749 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
753 if (MF_DBGLEVEL
>= 3) DbpString("AcquireEncryptedNonces finished");
755 cmd_send(CMD_ACK
, isOK
, cuid
, num_nonces
, buf
, sizeof(buf
));
761 //-----------------------------------------------------------------------------
762 // MIFARE nested authentication.
764 //-----------------------------------------------------------------------------
765 void MifareNested(uint32_t arg0
, uint32_t arg1
, uint32_t calibrate
, uint8_t *datain
) {
767 uint8_t blockNo
= arg0
& 0xff;
768 uint8_t keyType
= (arg0
>> 8) & 0xff;
769 uint8_t targetBlockNo
= arg1
& 0xff;
770 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
771 uint64_t ui64Key
= 0;
773 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_enc
, nttmp
, nttest
, ks1
;
781 uint32_t target_nt
[2], target_ks
[2];
782 uint32_t fixed_nt
= 0;
783 uint8_t target_nt_duplicate_count
= 0;
785 uint8_t par_array
[4];
787 struct Crypto1State mpcs
= {0, 0};
788 struct Crypto1State
*pcs
;
790 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
792 uint32_t auth1_time
, auth2_time
, authentication_timeout
= 0;
793 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
815 for (rtr
= 0; rtr
< 17; rtr
++) {
817 // prepare next select. No need to power down the card.
818 if (mifare_classic_halt(pcs
, cuid
)) {
819 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
824 // Test if the action was cancelled
825 if (BUTTON_PRESS()) {
830 if (!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
831 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
837 if (mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
, NULL
)) {
838 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_enc
, &auth2_time
, NULL
)) {
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_enc
) 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
);
887 // -------------------------------------------------------------------------------------------------
889 // get crypted nonces for target sector
890 for (i
= 0; i
< 2 && !isOK
; i
++) { // look for exactly two different nonces
893 while (target_nt
[i
] == 0 && !isOK
) { // continue until we have an unambiguous nonce
895 // prepare next select. No need to power down the card.
896 if (mifare_classic_halt(pcs
, cuid
)) {
897 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
901 // break out of the loop on button press
902 if (BUTTON_PRESS()) {
907 if (!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
908 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
913 authentication_timeout
= 0;
914 if (mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
, &authentication_timeout
)) {
915 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
919 // nested authentication
920 auth2_time
= auth1_time
+ delta_time
;
921 len
= mifare_sendcmd_short(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType
& 0x01), targetBlockNo
, receivedAnswer
, par
, &auth2_time
);
923 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error len=%d", len
);
927 nt2_enc
= bytes_to_num(receivedAnswer
, 4);
928 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i
+1, nt1
, nt2_enc
, par
[0]);
930 // Parity validity check
931 for (j
= 0; j
< 4; j
++) {
932 par_array
[j
] = (oddparity8(receivedAnswer
[j
]) != ((par
[0] >> (7-j
)) & 0x01));
936 nttest
= prng_successor(nt1
, dmin
- 1);
937 for (j
= dmin
; j
< dmax
+ 1; j
++) {
938 nttest
= prng_successor(nttest
, 1);
939 ks1
= nt2_enc
^ nttest
;
941 if (valid_nonce(nttest
, nt2_enc
, ks1
, par_array
)){
942 if (ncount
> 0) { // we are only interested in disambiguous nonces, try again
943 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i
+1, j
);
947 target_nt
[i
] = nttest
;
950 if (i
== 1 && target_nt
[1] == target_nt
[0]) { // we need two different nonces
951 if( ++target_nt_duplicate_count
>= NESTED_MAX_TRIES
) { // unable to get a 2nd nonce after NESTED_MAX_TRIES tries, probably a fixed nonce
952 if (MF_DBGLEVEL
>= 2) Dbprintf("Nonce#2: cannot get nonce that != nonce#1, continuing anyway with single nonce! ntdist=%d", j
);
957 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j
);
960 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i
+1, j
);
963 if (target_nt
[i
] == 0 && j
== dmax
+1 && MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i
+1);
967 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
970 crypto1_destroy(pcs
);
972 uint8_t buf
[4 + 4 * 4 + 4 + 4];
973 memcpy(buf
, &cuid
, 4);
974 memcpy(buf
+4, &target_nt
[0], 4);
975 memcpy(buf
+8, &target_ks
[0], 4);
976 memcpy(buf
+12, &target_nt
[1], 4);
977 memcpy(buf
+16, &target_ks
[1], 4);
978 memcpy(buf
+20, &authentication_timeout
, 4);
979 memcpy(buf
+24, &fixed_nt
, 4);
981 if (MF_DBGLEVEL
>= 3) DbpString("NESTED FINISHED");
983 cmd_send(CMD_ACK
, isOK
, 0, targetBlockNo
+ (targetKeyType
* 0x100), buf
, sizeof(buf
));
989 //-----------------------------------------------------------------------------
990 // MIFARE check keys. key count up to 85.
992 //-----------------------------------------------------------------------------
993 void MifareChkKeys(uint16_t arg0
, uint32_t arg1
, uint8_t arg2
, uint8_t *datain
) {
995 uint8_t blockNo
= arg0
& 0xff;
996 uint8_t keyType
= arg0
>> 8;
997 bool clearTrace
= arg1
& 0x01;
998 bool multisectorCheck
= arg1
& 0x02;
999 bool init
= arg1
& 0x04;
1000 bool drop_field
= arg1
& 0x08;
1001 bool fixed_nonce
= arg1
& 0x10;
1002 uint32_t auth_timeout
= arg1
>> 16;
1003 uint8_t keyCount
= arg2
;
1008 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1016 // clear debug level. We are expecting lots of authentication failures...
1017 int OLD_MF_DBGLEVEL
= MF_DBGLEVEL
;
1018 MF_DBGLEVEL
= MF_DBG_NONE
;
1021 if (multisectorCheck
) {
1022 TKeyIndex keyIndex
= {{0}};
1023 uint8_t sectorCnt
= blockNo
;
1024 res
= MifareMultisectorChk(datain
, keyCount
, sectorCnt
, keyType
, &auth_timeout
, OLD_MF_DBGLEVEL
, &keyIndex
);
1026 cmd_send(CMD_ACK
, 1, res
, 0, keyIndex
, 80);
1028 cmd_send(CMD_ACK
, 0, res
, 0, NULL
, 0);
1030 } else if (fixed_nonce
) {
1031 res
= MifareChkBlockKeysFixedNonce(datain
, keyCount
, blockNo
, keyType
, &auth_timeout
, OLD_MF_DBGLEVEL
);
1033 cmd_send(CMD_ACK
, 1, res
, 0, NULL
, 0); // key found
1035 cmd_send(CMD_ACK
, 0, res
, 0, NULL
, 0); // no key found or aborted
1038 res
= MifareChkBlockKeys(datain
, keyCount
, blockNo
, keyType
, &auth_timeout
, OLD_MF_DBGLEVEL
);
1040 cmd_send(CMD_ACK
, 1, res
, 0, datain
+ (res
- 1) * 6, 6);
1042 cmd_send(CMD_ACK
, 0, res
, 0, NULL
, 0);
1046 if (drop_field
|| res
!= 0) {
1047 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1051 // restore debug level
1052 MF_DBGLEVEL
= OLD_MF_DBGLEVEL
;
1058 //-----------------------------------------------------------------------------
1059 // MIFARE Personalize UID. Only for Mifare Classic EV1 7Byte UID
1060 //-----------------------------------------------------------------------------
1061 void MifarePersonalizeUID(uint8_t keyType
, uint8_t perso_option
, uint8_t *data
) {
1065 struct Crypto1State mpcs
= {0, 0};
1066 struct Crypto1State
*pcs
;
1072 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1076 if (!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
1077 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1081 uint8_t block_number
= 0;
1082 uint64_t key
= bytes_to_num(data
, 6);
1083 if (mifare_classic_auth(pcs
, cuid
, block_number
, keyType
, key
, AUTH_FIRST
, NULL
)) {
1084 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
1088 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1089 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1090 int len
= mifare_sendcmd_short(pcs
, true, MIFARE_EV1_PERSONAL_UID
, perso_option
, receivedAnswer
, receivedAnswerPar
, NULL
);
1091 if (len
!= 1 || receivedAnswer
[0] != CARD_ACK
) {
1092 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
1099 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1102 crypto1_destroy(pcs
);
1104 if (MF_DBGLEVEL
>= 2) DbpString("PERSONALIZE UID FINISHED");
1106 cmd_send(CMD_ACK
, isOK
, 0, 0, NULL
, 0);
1111 //-----------------------------------------------------------------------------
1112 // MIFARE commands set debug level
1114 //-----------------------------------------------------------------------------
1115 void MifareSetDbgLvl(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1117 Dbprintf("Debug level: %d", MF_DBGLEVEL
);
1120 //-----------------------------------------------------------------------------
1121 // Work with emulator memory
1123 // Note: we call FpgaDownloadAndGo(FPGA_BITSTREAM_HF) here although FPGA is not
1124 // involved in dealing with emulator memory. But if it is called later, it might
1125 // destroy the Emulator Memory.
1126 //-----------------------------------------------------------------------------
1128 void MifareEMemClr(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1129 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1133 void MifareEMemSet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1134 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1135 emlSetMem(datain
, arg0
, arg1
); // data, block num, blocks count
1138 void MifareEMemGet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1139 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1140 byte_t buf
[USB_CMD_DATA_SIZE
];
1141 emlGetMem(buf
, arg0
, arg1
); // data, block num, blocks count (max 4)
1144 cmd_send(CMD_ACK
,arg0
,arg1
,0,buf
,USB_CMD_DATA_SIZE
);
1148 //-----------------------------------------------------------------------------
1149 // Load a card into the emulator memory
1151 //-----------------------------------------------------------------------------
1152 void MifareECardLoad(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1153 uint8_t numSectors
= arg0
;
1154 uint8_t keyType
= arg1
;
1155 uint64_t ui64Key
= 0;
1157 struct Crypto1State mpcs
= {0, 0};
1158 struct Crypto1State
*pcs
;
1162 byte_t dataoutbuf
[16];
1163 byte_t dataoutbuf2
[16];
1169 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1176 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
1178 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1181 for (uint8_t sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
1182 ui64Key
= emlGetKey(sectorNo
, keyType
);
1184 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
, NULL
)) {
1186 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo
);
1190 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_NESTED
, NULL
)) {
1192 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo
);
1197 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
1198 if(isOK
&& mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
)) {
1200 if (MF_DBGLEVEL
>= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo
, blockNo
);
1204 if (blockNo
< NumBlocksPerSector(sectorNo
) - 1) {
1205 emlSetMem(dataoutbuf
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
1206 } else { // sector trailer, keep the keys, set only the AC
1207 emlGetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
1208 memcpy(&dataoutbuf2
[6], &dataoutbuf
[6], 4);
1209 emlSetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
1216 if(mifare_classic_halt(pcs
, cuid
)) {
1217 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1220 // ----------------------------- crypto1 destroy
1221 crypto1_destroy(pcs
);
1223 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1226 if (MF_DBGLEVEL
>= 2) DbpString("EMUL FILL SECTORS FINISHED");
1231 //-----------------------------------------------------------------------------
1232 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
1234 //-----------------------------------------------------------------------------
1236 static bool isBlockTrailer(int blockN
) {
1237 if (blockN
>= 0 && blockN
< 128) {
1238 return ((blockN
& 0x03) == 0x03);
1240 if (blockN
>= 128 && blockN
<= 256) {
1241 return ((blockN
& 0x0F) == 0x0F);
1246 void MifareCWipe(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1249 uint32_t numBlocks
= arg0
;
1251 // bit 0 - wipe gen1a
1252 // bit 1 - fill card with default data
1253 // bit 2 - gen1a = 0, gen1b = 1
1254 uint8_t cmdParams
= arg1
;
1255 bool needWipe
= cmdParams
& 0x01;
1256 bool needFill
= cmdParams
& 0x02;
1257 bool gen1b
= cmdParams
& 0x04;
1259 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1260 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1262 uint8_t block0
[16] = {0x01, 0x02, 0x03, 0x04, 0x04, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBE, 0xAF};
1263 uint8_t block1
[16] = {0x00};
1264 uint8_t blockK
[16] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x08, 0x77, 0x8F, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1265 uint8_t d_block
[18] = {0x00};
1268 uint8_t wupC1
[] = { 0x40 };
1269 uint8_t wupC2
[] = { 0x43 };
1270 uint8_t wipeC
[] = { 0x41 };
1276 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1285 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1286 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != CARD_ACK
)) {
1287 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1291 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
1292 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != CARD_ACK
)) {
1293 if (MF_DBGLEVEL
>= 1) Dbprintf("wipeC error");
1297 if(mifare_classic_halt(NULL
, 0)) {
1298 if (MF_DBGLEVEL
> 2) Dbprintf("Halt error");
1305 ReaderTransmitBitsPar(wupC1
, 7, 0, NULL
);
1307 // 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)
1310 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != CARD_ACK
)) {
1311 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1315 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1316 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != CARD_ACK
)) {
1317 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1322 // send blocks command
1323 for (int blockNo
= 0; blockNo
< numBlocks
; blockNo
++) {
1324 if ((mifare_sendcmd_short(NULL
, 0, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 1) || (receivedAnswer
[0] != CARD_ACK
)) {
1325 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send command error");
1329 // check type of block and add crc
1330 if (!isBlockTrailer(blockNo
)){
1331 memcpy(d_block
, block1
, 16);
1333 memcpy(d_block
, blockK
, 16);
1336 memcpy(d_block
, block0
, 16);
1338 AppendCrc14443a(d_block
, 16);
1340 // send write command
1341 ReaderTransmit(d_block
, sizeof(d_block
), NULL
);
1342 if ((ReaderReceive(receivedAnswer
, receivedAnswerPar
) != 1) || (receivedAnswer
[0] != CARD_ACK
)) {
1343 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send data error");
1349 // do no issue halt command for gen1b
1351 if (mifare_classic_halt(NULL
, 0)) {
1352 if (MF_DBGLEVEL
> 2) Dbprintf("Halt error");
1361 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1363 // send USB response
1365 cmd_send(CMD_ACK
,isOK
,0,0,NULL
,0);
1373 void MifareCSetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1376 uint8_t needWipe
= arg0
;
1377 // bit 0 - need get UID
1378 // bit 1 - need wupC
1379 // bit 2 - need HALT after sequence
1380 // bit 3 - need init FPGA and field before sequence
1381 // bit 4 - need reset FPGA and LED
1382 // bit 6 - gen1b backdoor type
1383 uint8_t workFlags
= arg1
;
1384 uint8_t blockNo
= arg2
;
1387 uint8_t wupC1
[] = { 0x40 };
1388 uint8_t wupC2
[] = { 0x43 };
1389 uint8_t wipeC
[] = { 0x41 };
1393 uint8_t uid
[10] = {0x00};
1394 uint8_t d_block
[18] = {0x00};
1397 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1398 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1400 // reset FPGA and LED
1401 if (workFlags
& 0x08) {
1405 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1413 // get UID from chip
1414 if (workFlags
& 0x01) {
1415 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
1416 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1417 // 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.
1421 if(mifare_classic_halt(NULL
, cuid
)) {
1422 if (MF_DBGLEVEL
> 2) Dbprintf("Halt error");
1423 // Continue, some magic tags misbehavies and send an answer to it.
1429 // Wipe command don't work with gen1b
1430 if (needWipe
&& !(workFlags
& 0x40)){
1431 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1432 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != CARD_ACK
)) {
1433 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1437 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
1438 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != CARD_ACK
)) {
1439 if (MF_DBGLEVEL
>= 1) Dbprintf("wipeC error");
1443 if(mifare_classic_halt(NULL
, 0)) {
1444 if (MF_DBGLEVEL
> 2) Dbprintf("Halt error");
1445 // Continue, some magic tags misbehavies and send an answer to it.
1451 if (workFlags
& 0x02) {
1452 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1454 // 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)
1455 if (!(workFlags
& 0x40)) {
1457 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != CARD_ACK
)) {
1458 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1462 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1463 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != CARD_ACK
)) {
1464 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1470 if ((mifare_sendcmd_short(NULL
, 0, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 1) || (receivedAnswer
[0] != CARD_ACK
)) {
1471 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send command error");
1475 memcpy(d_block
, datain
, 16);
1476 AppendCrc14443a(d_block
, 16);
1478 ReaderTransmit(d_block
, sizeof(d_block
), NULL
);
1479 if ((ReaderReceive(receivedAnswer
, receivedAnswerPar
) != 1) || (receivedAnswer
[0] != CARD_ACK
)) {
1480 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send data error");
1484 if (workFlags
& 0x04) {
1485 // do no issue halt command for gen1b magic tag (#db# halt error. response len: 1)
1486 if (!(workFlags
& 0x40)) {
1487 if (mifare_classic_halt(NULL
, 0)) {
1488 if (MF_DBGLEVEL
> 2) Dbprintf("Halt error");
1489 // Continue, some magic tags misbehavies and send an answer to it.
1499 if ((workFlags
& 0x10) || (!isOK
)) {
1500 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1504 cmd_send(CMD_ACK
,isOK
,0,0,uid
,4);
1511 void MifareCGetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1514 // bit 1 - need wupC
1515 // bit 2 - need HALT after sequence
1516 // bit 3 - need init FPGA and field before sequence
1517 // bit 4 - need reset FPGA and LED
1518 // bit 5 - need to set datain instead of issuing USB reply (called via ARM for StandAloneMode14a)
1519 // bit 6 - gen1b backdoor type
1520 uint8_t workFlags
= arg0
;
1521 uint8_t blockNo
= arg2
;
1524 uint8_t wupC1
[] = { 0x40 };
1525 uint8_t wupC2
[] = { 0x43 };
1529 uint8_t data
[18] = {0x00};
1532 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1533 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1535 if (workFlags
& 0x08) {
1539 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1546 if (workFlags
& 0x02) {
1547 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1548 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != CARD_ACK
)) {
1549 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1552 // do no issue for gen1b magic tag
1553 if (!(workFlags
& 0x40)) {
1554 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1555 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != CARD_ACK
)) {
1556 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1563 if ((mifare_sendcmd_short(NULL
, 0, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 18)) {
1564 if (MF_DBGLEVEL
>= 1) Dbprintf("read block send command error");
1567 memcpy(data
, receivedAnswer
, 18);
1569 if (workFlags
& 0x04) {
1570 // do no issue halt command for gen1b magic tag (#db# halt error. response len: 1)
1571 if (!(workFlags
& 0x40)) {
1572 if (mifare_classic_halt(NULL
, cuid
)) {
1573 if (MF_DBGLEVEL
> 1) Dbprintf("Halt error");
1574 // Continue, some magic tags misbehavies and send an answer to it.
1584 if ((workFlags
& 0x10) || (!isOK
)) {
1585 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1589 if (workFlags
& 0x20) {
1591 memcpy(datain
, data
, 18);
1594 cmd_send(CMD_ACK
,isOK
,0,0,data
,18);
1600 void MifareCIdent(){
1603 uint8_t wupC1
[] = { 0x40 };
1604 uint8_t wupC2
[] = { 0x43 };
1609 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1610 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1615 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1620 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1621 if(ReaderReceive(receivedAnswer
, receivedAnswerPar
) && (receivedAnswer
[0] == CARD_ACK
)) {
1624 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1625 if(ReaderReceive(receivedAnswer
, receivedAnswerPar
) && (receivedAnswer
[0] == CARD_ACK
)) {
1630 // From iceman1001: removed the if, since some magic tags misbehavies and send an answer to it.
1631 mifare_classic_halt(NULL
, 0);
1633 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1636 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
1646 void Mifare_DES_Auth1(uint8_t arg0
, uint8_t *datain
){
1648 byte_t dataout
[11] = {0x00};
1649 uint8_t uid
[10] = {0x00};
1652 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1655 int len
= iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true);
1657 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
1662 if(mifare_desfire_des_auth1(cuid
, dataout
)){
1663 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication part1: Fail.");
1668 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 1 FINISHED");
1670 cmd_send(CMD_ACK
, 1, cuid
, 0, dataout
, sizeof(dataout
));
1673 void Mifare_DES_Auth2(uint32_t arg0
, uint8_t *datain
){
1675 uint32_t cuid
= arg0
;
1676 uint8_t key
[16] = {0x00};
1678 byte_t dataout
[12] = {0x00};
1680 memcpy(key
, datain
, 16);
1682 isOK
= mifare_desfire_des_auth2(cuid
, key
, dataout
);
1685 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) Dbprintf("Authentication part2: Failed");
1690 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1692 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 2 FINISHED");
1694 cmd_send(CMD_ACK
, isOK
, 0, 0, dataout
, sizeof(dataout
));