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 "protocols.h"
23 //-----------------------------------------------------------------------------
24 // Select, Authenticate, Read a MIFARE tag.
26 //-----------------------------------------------------------------------------
27 void MifareReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
30 uint8_t blockNo
= arg0
;
31 uint8_t keyType
= arg1
;
33 ui64Key
= bytes_to_num(datain
, 6);
37 byte_t dataoutbuf
[16] = {0x00};
38 uint8_t uid
[10] = {0x00};
40 struct Crypto1State mpcs
= {0, 0};
41 struct Crypto1State
*pcs
;
44 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
54 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0)) {
55 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
59 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
60 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
64 if(mifare_classic_readblock(pcs
, cuid
, blockNo
, dataoutbuf
)) {
65 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block error");
69 if(mifare_classic_halt(pcs
, cuid
)) {
70 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
78 // ----------------------------- crypto1 destroy
81 if (MF_DBGLEVEL
>= 2) DbpString("READ BLOCK FINISHED");
84 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16);
87 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
91 void MifareUC_Auth(uint8_t arg0
, uint8_t *keybytes
){
93 bool turnOffField
= (arg0
== 1);
95 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
97 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
102 if(!iso14443a_select_card(NULL
, NULL
, NULL
, true, 0)) {
103 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
108 if(!mifare_ultra_auth(keybytes
)){
109 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication failed");
115 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
118 cmd_send(CMD_ACK
,1,0,0,0,0);
122 // Arg1 = UsePwd bool
123 // datain = PWD bytes,
124 void MifareUReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
)
126 uint8_t blockNo
= arg0
;
127 byte_t dataout
[16] = {0x00};
128 bool useKey
= (arg1
== 1); //UL_C
129 bool usePwd
= (arg1
== 2); //UL_EV1/NTAG
133 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
138 int len
= iso14443a_select_card(NULL
, NULL
, NULL
, true, 0);
140 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%02X)",len
);
145 // UL-C authentication
147 uint8_t key
[16] = {0x00};
148 memcpy(key
, datain
, sizeof(key
) );
150 if ( !mifare_ultra_auth(key
) ) {
156 // UL-EV1 / NTAG authentication
158 uint8_t pwd
[4] = {0x00};
159 memcpy(pwd
, datain
, 4);
160 uint8_t pack
[4] = {0,0,0,0};
161 if (!mifare_ul_ev1_auth(pwd
, pack
)) {
167 if( mifare_ultra_readblock(blockNo
, dataout
) ) {
168 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Read block error");
173 if( mifare_ultra_halt() ) {
174 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Halt error");
179 cmd_send(CMD_ACK
,1,0,0,dataout
,16);
180 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
184 //-----------------------------------------------------------------------------
185 // Select, Authenticate, Read a MIFARE tag.
186 // read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)
187 //-----------------------------------------------------------------------------
188 void MifareReadSector(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
191 uint8_t sectorNo
= arg0
;
192 uint8_t keyType
= arg1
;
193 uint64_t ui64Key
= 0;
194 ui64Key
= bytes_to_num(datain
, 6);
198 byte_t dataoutbuf
[16 * 16];
199 uint8_t uid
[10] = {0x00};
201 struct Crypto1State mpcs
= {0, 0};
202 struct Crypto1State
*pcs
;
205 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
215 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0)) {
217 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
221 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
223 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
226 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
227 if(mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
+ 16 * blockNo
)) {
229 if (MF_DBGLEVEL
>= 1) Dbprintf("Read sector %2d block %2d error", sectorNo
, blockNo
);
234 if(mifare_classic_halt(pcs
, cuid
)) {
235 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
238 // ----------------------------- crypto1 destroy
239 crypto1_destroy(pcs
);
241 if (MF_DBGLEVEL
>= 2) DbpString("READ SECTOR FINISHED");
244 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16*NumBlocksPerSector(sectorNo
));
248 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
252 // arg0 = blockNo (start)
253 // arg1 = Pages (number of blocks)
255 // datain = KEY bytes
256 void MifareUReadCard(uint8_t arg0
, uint16_t arg1
, uint8_t arg2
, uint8_t *datain
)
260 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
262 // free eventually allocated BigBuf memory
263 BigBuf_free(); BigBuf_Clear_ext(false);
268 uint8_t blockNo
= arg0
;
269 uint16_t blocks
= arg1
;
270 bool useKey
= (arg2
== 1); //UL_C
271 bool usePwd
= (arg2
== 2); //UL_EV1/NTAG
272 uint32_t countblocks
= 0;
273 uint8_t *dataout
= BigBuf_malloc(CARD_MEMORY_SIZE
);
274 if (dataout
== NULL
){
275 Dbprintf("out of memory");
280 int len
= iso14443a_select_card(NULL
, NULL
, NULL
, true, 0);
282 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%d)",len
);
287 // UL-C authentication
289 uint8_t key
[16] = {0x00};
290 memcpy(key
, datain
, sizeof(key
) );
292 if ( !mifare_ultra_auth(key
) ) {
298 // UL-EV1 / NTAG authentication
300 uint8_t pwd
[4] = {0x00};
301 memcpy(pwd
, datain
, sizeof(pwd
));
302 uint8_t pack
[4] = {0,0,0,0};
304 if (!mifare_ul_ev1_auth(pwd
, pack
)){
310 for (int i
= 0; i
< blocks
; i
++){
311 if ((i
*4) + 4 >= CARD_MEMORY_SIZE
) {
312 Dbprintf("Data exceeds buffer!!");
316 len
= mifare_ultra_readblock(blockNo
+ i
, dataout
+ 4 * i
);
319 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Read block %d error",i
);
320 // if no blocks read - error out
325 //stop at last successful read block and return what we got
333 len
= mifare_ultra_halt();
335 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Halt error");
340 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) Dbprintf("Blocks read %d", countblocks
);
344 cmd_send(CMD_ACK
, 1, countblocks
, BigBuf_max_traceLen(), 0, 0);
345 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
350 //-----------------------------------------------------------------------------
351 // Select, Authenticate, Write a MIFARE tag.
353 //-----------------------------------------------------------------------------
354 void MifareWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
357 uint8_t blockNo
= arg0
;
358 uint8_t keyType
= arg1
;
359 uint64_t ui64Key
= 0;
360 byte_t blockdata
[16] = {0x00};
362 ui64Key
= bytes_to_num(datain
, 6);
363 memcpy(blockdata
, datain
+ 10, 16);
367 uint8_t uid
[10] = {0x00};
369 struct Crypto1State mpcs
= {0, 0};
370 struct Crypto1State
*pcs
;
373 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
383 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0)) {
384 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
388 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
389 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
393 if(mifare_classic_writeblock(pcs
, cuid
, blockNo
, blockdata
)) {
394 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
398 if(mifare_classic_halt(pcs
, cuid
)) {
399 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
407 // ----------------------------- crypto1 destroy
408 crypto1_destroy(pcs
);
410 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
413 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
418 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
422 /* // Command not needed but left for future testing
423 void MifareUWriteBlockCompat(uint8_t arg0, uint8_t *datain)
425 uint8_t blockNo = arg0;
426 byte_t blockdata[16] = {0x00};
428 memcpy(blockdata, datain, 16);
430 uint8_t uid[10] = {0x00};
432 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
436 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
438 if(!iso14443a_select_card(uid, NULL, NULL, true, 0)) {
439 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
444 if(mifare_ultra_writeblock_compat(blockNo, blockdata)) {
445 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
449 if(mifare_ultra_halt()) {
450 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
455 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");
457 cmd_send(CMD_ACK,1,0,0,0,0);
458 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
463 // Arg0 : Block to write to.
464 // Arg1 : 0 = use no authentication.
465 // 1 = use 0x1A authentication.
466 // 2 = use 0x1B authentication.
467 // datain : 4 first bytes is data to be written.
468 // : 4/16 next bytes is authentication key.
469 void MifareUWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
)
471 uint8_t blockNo
= arg0
;
472 bool useKey
= (arg1
== 1); //UL_C
473 bool usePwd
= (arg1
== 2); //UL_EV1/NTAG
474 byte_t blockdata
[4] = {0x00};
476 memcpy(blockdata
, datain
,4);
480 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
485 if(!iso14443a_select_card(NULL
, NULL
, NULL
, true, 0)) {
486 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
491 // UL-C authentication
493 uint8_t key
[16] = {0x00};
494 memcpy(key
, datain
+4, sizeof(key
) );
496 if ( !mifare_ultra_auth(key
) ) {
502 // UL-EV1 / NTAG authentication
504 uint8_t pwd
[4] = {0x00};
505 memcpy(pwd
, datain
+4, 4);
506 uint8_t pack
[4] = {0,0,0,0};
507 if (!mifare_ul_ev1_auth(pwd
, pack
)) {
513 if(mifare_ultra_writeblock(blockNo
, blockdata
)) {
514 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
519 if(mifare_ultra_halt()) {
520 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
525 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
527 cmd_send(CMD_ACK
,1,0,0,0,0);
528 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
532 void MifareUSetPwd(uint8_t arg0
, uint8_t *datain
){
534 uint8_t pwd
[16] = {0x00};
535 byte_t blockdata
[4] = {0x00};
537 memcpy(pwd
, datain
, 16);
539 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
540 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
545 if(!iso14443a_select_card(NULL
, NULL
, NULL
, true, 0)) {
546 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
551 blockdata
[0] = pwd
[7];
552 blockdata
[1] = pwd
[6];
553 blockdata
[2] = pwd
[5];
554 blockdata
[3] = pwd
[4];
555 if(mifare_ultra_writeblock( 44, blockdata
)) {
556 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
561 blockdata
[0] = pwd
[3];
562 blockdata
[1] = pwd
[2];
563 blockdata
[2] = pwd
[1];
564 blockdata
[3] = pwd
[0];
565 if(mifare_ultra_writeblock( 45, blockdata
)) {
566 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
571 blockdata
[0] = pwd
[15];
572 blockdata
[1] = pwd
[14];
573 blockdata
[2] = pwd
[13];
574 blockdata
[3] = pwd
[12];
575 if(mifare_ultra_writeblock( 46, blockdata
)) {
576 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
581 blockdata
[0] = pwd
[11];
582 blockdata
[1] = pwd
[10];
583 blockdata
[2] = pwd
[9];
584 blockdata
[3] = pwd
[8];
585 if(mifare_ultra_writeblock( 47, blockdata
)) {
586 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
591 if(mifare_ultra_halt()) {
592 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
597 cmd_send(CMD_ACK
,1,0,0,0,0);
598 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
602 // Return 1 if the nonce is invalid else return 0
603 int valid_nonce(uint32_t Nt
, uint32_t NtEnc
, uint32_t Ks1
, uint8_t *parity
) {
604 return ((oddparity8((Nt
>> 24) & 0xFF) == ((parity
[0]) ^ oddparity8((NtEnc
>> 24) & 0xFF) ^ BIT(Ks1
,16))) & \
605 (oddparity8((Nt
>> 16) & 0xFF) == ((parity
[1]) ^ oddparity8((NtEnc
>> 16) & 0xFF) ^ BIT(Ks1
,8))) & \
606 (oddparity8((Nt
>> 8) & 0xFF) == ((parity
[2]) ^ oddparity8((NtEnc
>> 8) & 0xFF) ^ BIT(Ks1
,0)))) ? 1 : 0;
610 //-----------------------------------------------------------------------------
611 // acquire encrypted nonces in order to perform the attack described in
612 // Carlo Meijer, Roel Verdult, "Ciphertext-only Cryptanalysis on Hardened
613 // Mifare Classic Cards" in Proceedings of the 22nd ACM SIGSAC Conference on
614 // Computer and Communications Security, 2015
615 //-----------------------------------------------------------------------------
616 void MifareAcquireEncryptedNonces(uint32_t arg0
, uint32_t arg1
, uint32_t flags
, uint8_t *datain
)
618 uint64_t ui64Key
= 0;
619 uint8_t uid
[10] = {0x00};
621 uint8_t cascade_levels
= 0;
622 struct Crypto1State mpcs
= {0, 0};
623 struct Crypto1State
*pcs
;
625 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
] = {0x00};
627 uint8_t par_enc
[1] = {0x00};
628 uint8_t nt_par_enc
= 0;
629 uint8_t buf
[USB_CMD_DATA_SIZE
] = {0x00};
630 uint32_t timeout
= 0;
632 uint8_t blockNo
= arg0
& 0xff;
633 uint8_t keyType
= (arg0
>> 8) & 0xff;
634 uint8_t targetBlockNo
= arg1
& 0xff;
635 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
636 ui64Key
= bytes_to_num(datain
, 6);
637 bool initialize
= flags
& 0x0001;
638 bool slow
= flags
& 0x0002;
639 bool field_off
= flags
& 0x0004;
641 #define AUTHENTICATION_TIMEOUT 848 // card times out 1ms after wrong authentication (according to NXP documentation)
642 #define PRE_AUTHENTICATION_LEADTIME 400 // some (non standard) cards need a pause after select before they are ready for first authentication
648 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
655 uint16_t num_nonces
= 0;
656 bool have_uid
= false;
657 for (uint16_t i
= 0; i
<= USB_CMD_DATA_SIZE
- 9; ) {
659 // Test if the action was cancelled
666 if (!have_uid
) { // need a full select cycle to get the uid first
667 iso14a_card_select_t card_info
;
668 if(!iso14443a_select_card(uid
, &card_info
, &cuid
, true, 0)) {
669 if (MF_DBGLEVEL
>= 1) Dbprintf("AcquireNonces: Can't select card (ALL)");
672 switch (card_info
.uidlen
) {
673 case 4 : cascade_levels
= 1; break;
674 case 7 : cascade_levels
= 2; break;
675 case 10: cascade_levels
= 3; break;
679 } else { // no need for anticollision. We can directly select the card
680 if(!iso14443a_select_card(uid
, NULL
, NULL
, false, cascade_levels
)) {
681 if (MF_DBGLEVEL
>= 1) Dbprintf("AcquireNonces: Can't select card (UID)");
687 timeout
= GetCountSspClk() + PRE_AUTHENTICATION_LEADTIME
;
688 while(GetCountSspClk() < timeout
);
692 if (mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, NULL
)) {
693 if (MF_DBGLEVEL
>= 1) Dbprintf("AcquireNonces: Auth1 error");
697 // nested authentication
698 uint16_t len
= mifare_sendcmd_short(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType
& 0x01), targetBlockNo
, receivedAnswer
, par_enc
, NULL
);
700 if (MF_DBGLEVEL
>= 1) Dbprintf("AcquireNonces: Auth2 error len=%d", len
);
704 // send a dummy byte as reader response in order to trigger the cards authentication timeout
705 uint8_t dummy_answer
= 0;
706 ReaderTransmit(&dummy_answer
, 1, NULL
);
707 timeout
= GetCountSspClk() + AUTHENTICATION_TIMEOUT
;
710 if (num_nonces
% 2) {
711 memcpy(buf
+i
, receivedAnswer
, 4);
712 nt_par_enc
= par_enc
[0] & 0xf0;
714 nt_par_enc
|= par_enc
[0] >> 4;
715 memcpy(buf
+i
+4, receivedAnswer
, 4);
716 memcpy(buf
+i
+8, &nt_par_enc
, 1);
720 // wait for the card to become ready again
721 while(GetCountSspClk() < timeout
);
727 crypto1_destroy(pcs
);
730 cmd_send(CMD_ACK
, isOK
, cuid
, num_nonces
, buf
, sizeof(buf
));
733 if (MF_DBGLEVEL
>= 3) DbpString("AcquireEncryptedNonces finished");
736 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
742 //-----------------------------------------------------------------------------
743 // MIFARE nested authentication.
745 //-----------------------------------------------------------------------------
746 void MifareNested(uint32_t arg0
, uint32_t arg1
, uint32_t calibrate
, uint8_t *datain
)
749 uint8_t blockNo
= arg0
& 0xff;
750 uint8_t keyType
= (arg0
>> 8) & 0xff;
751 uint8_t targetBlockNo
= arg1
& 0xff;
752 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
753 uint64_t ui64Key
= 0;
755 ui64Key
= bytes_to_num(datain
, 6);
758 uint16_t rtr
, i
, j
, len
;
760 static uint16_t dmin
, dmax
;
761 uint8_t uid
[10] = {0x00};
762 uint32_t cuid
= 0, nt1
, nt2
, nttmp
, nttest
, ks1
;
763 uint8_t par
[1] = {0x00};
764 uint32_t target_nt
[2] = {0x00}, target_ks
[2] = {0x00};
766 uint8_t par_array
[4] = {0x00};
768 struct Crypto1State mpcs
= {0, 0};
769 struct Crypto1State
*pcs
;
771 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
] = {0x00};
773 uint32_t auth1_time
, auth2_time
;
774 static uint16_t delta_time
= 0;
778 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
780 // free eventually allocated BigBuf memory
781 BigBuf_free(); BigBuf_Clear_ext(false);
783 if (calibrate
) clear_trace();
786 // statistics on nonce distance
788 #define NESTED_MAX_TRIES 12
789 uint16_t unsuccessfull_tries
= 0;
790 if (calibrate
) { // for first call only. Otherwise reuse previous calibration
798 for (rtr
= 0; rtr
< 17; rtr
++) {
800 // Test if the action was cancelled
806 // prepare next select. No need to power down the card.
807 if(mifare_classic_halt(pcs
, cuid
)) {
808 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
813 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0)) {
814 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
820 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
821 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
825 auth2_time
= (delta_time
) ? auth1_time
+ delta_time
: 0;
827 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_NESTED
, &nt2
, &auth2_time
)) {
828 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error");
833 nttmp
= prng_successor(nt1
, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160
834 for (i
= 101; i
< 1200; i
++) {
835 nttmp
= prng_successor_one(nttmp
);
836 if (nttmp
== nt2
) break;
846 delta_time
= auth2_time
- auth1_time
+ 32; // allow some slack for proper timing
848 if (MF_DBGLEVEL
>= 3) Dbprintf("Nested: calibrating... ntdist=%d", i
);
850 unsuccessfull_tries
++;
851 if (unsuccessfull_tries
> NESTED_MAX_TRIES
) { // card isn't vulnerable to nested attack (random numbers are not predictable)
857 davg
= (davg
+ (rtr
- 1)/2) / (rtr
- 1);
859 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
);
866 // -------------------------------------------------------------------------------------------------
870 // get crypted nonces for target sector
871 for(i
=0; i
< 2 && !isOK
; i
++) { // look for exactly two different nonces
874 while(target_nt
[i
] == 0) { // continue until we have an unambiguous nonce
876 // prepare next select. No need to power down the card.
877 if(mifare_classic_halt(pcs
, cuid
)) {
878 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
882 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0)) {
883 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
888 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
889 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
893 // nested authentication
894 auth2_time
= auth1_time
+ delta_time
;
896 len
= mifare_sendcmd_short(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType
& 0x01), targetBlockNo
, receivedAnswer
, par
, &auth2_time
);
898 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error len=%d", len
);
902 nt2
= bytes_to_num(receivedAnswer
, 4);
903 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i
+1, nt1
, nt2
, par
[0]);
905 // Parity validity check
906 // for (j = 0; j < 4; j++) {
907 // par_array[j] = (oddparity8(receivedAnswer[j]) != ((par[0] >> (7-j)) & 0x01));
909 par_array
[0] = (oddparity8(receivedAnswer
[0]) != ((par
[0] >> (7-0)) & 0x01));
910 par_array
[1] = (oddparity8(receivedAnswer
[1]) != ((par
[0] >> (7-1)) & 0x01));
911 par_array
[2] = (oddparity8(receivedAnswer
[2]) != ((par
[0] >> (7-2)) & 0x01));
912 par_array
[3] = (oddparity8(receivedAnswer
[3]) != ((par
[0] >> (7-3)) & 0x01));
915 nttest
= prng_successor(nt1
, dmin
- 1);
916 for (j
= dmin
; j
< dmax
+ 1; j
++) {
917 nttest
= prng_successor_one(nttest
);
920 if (valid_nonce(nttest
, nt2
, ks1
, par_array
)){
921 if (ncount
> 0) { // we are only interested in disambiguous nonces, try again
922 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i
+1, j
);
926 target_nt
[i
] = nttest
;
929 if (i
== 1 && target_nt
[1] == target_nt
[0]) { // we need two different nonces
931 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j
);
934 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i
+1, j
);
937 if (target_nt
[i
] == 0 && j
== dmax
+1 && MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i
+1);
943 // ----------------------------- crypto1 destroy
944 crypto1_destroy(pcs
);
946 byte_t buf
[4 + 4 * 4] = {0};
947 memcpy(buf
, &cuid
, 4);
948 memcpy(buf
+4, &target_nt
[0], 4);
949 memcpy(buf
+8, &target_ks
[0], 4);
950 memcpy(buf
+12, &target_nt
[1], 4);
951 memcpy(buf
+16, &target_ks
[1], 4);
954 cmd_send(CMD_ACK
, isOK
, 0, targetBlockNo
+ (targetKeyType
* 0x100), buf
, sizeof(buf
));
957 if (MF_DBGLEVEL
>= 3) DbpString("NESTED FINISHED");
959 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
964 //-----------------------------------------------------------------------------
965 // MIFARE check keys. key count up to 85.
967 //-----------------------------------------------------------------------------
968 void MifareChkKeys(uint16_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
971 uint8_t blockNo
= arg0
& 0xff;
972 uint8_t keyType
= (arg0
>> 8) & 0xff;
973 bool clearTrace
= arg1
;
974 uint8_t keyCount
= arg2
;
975 uint64_t ui64Key
= 0;
980 uint8_t uid
[10] = {0x00};
982 struct Crypto1State mpcs
= {0, 0};
983 struct Crypto1State
*pcs
;
987 int OLD_MF_DBGLEVEL
= MF_DBGLEVEL
;
988 MF_DBGLEVEL
= MF_DBG_NONE
;
993 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1000 for (i
= 0; i
< keyCount
; ++i
) {
1002 if (!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0)) {
1003 if (OLD_MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Can't select card");
1007 ui64Key
= bytes_to_num(datain
+ i
* 6, 6);
1008 if (mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
1009 if (mifare_classic_halt(pcs
, cuid
))
1010 if (MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Halt error");
1019 cmd_send(CMD_ACK
,isOK
,0,0,datain
+ i
* 6,6);
1021 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1024 crypto1_destroy(pcs
);
1026 // restore debug level
1027 MF_DBGLEVEL
= OLD_MF_DBGLEVEL
;
1030 //-----------------------------------------------------------------------------
1031 // MIFARE commands set debug level
1033 //-----------------------------------------------------------------------------
1034 void MifareSetDbgLvl(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1036 Dbprintf("Debug level: %d", MF_DBGLEVEL
);
1039 //-----------------------------------------------------------------------------
1040 // Work with emulator memory
1042 // Note: we call FpgaDownloadAndGo(FPGA_BITSTREAM_HF) here although FPGA is not
1043 // involved in dealing with emulator memory. But if it is called later, it might
1044 // destroy the Emulator Memory.
1045 //-----------------------------------------------------------------------------
1047 void MifareEMemClr(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1048 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1052 void MifareEMemSet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1053 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1054 if (arg2
==0) arg2
= 16; // backwards compat... default bytewidth
1055 emlSetMem_xt(datain
, arg0
, arg1
, arg2
); // data, block num, blocks count, block byte width
1058 void MifareEMemGet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1059 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1060 byte_t buf
[USB_CMD_DATA_SIZE
] = {0x00};
1061 emlGetMem(buf
, arg0
, arg1
); // data, block num, blocks count (max 4)
1064 cmd_send(CMD_ACK
,arg0
,arg1
,0,buf
,USB_CMD_DATA_SIZE
);
1068 //-----------------------------------------------------------------------------
1069 // Load a card into the emulator memory
1071 //-----------------------------------------------------------------------------
1072 void MifareECardLoad(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1073 uint8_t numSectors
= arg0
;
1074 uint8_t keyType
= arg1
;
1075 uint64_t ui64Key
= 0;
1077 struct Crypto1State mpcs
= {0, 0};
1078 struct Crypto1State
*pcs
;
1082 byte_t dataoutbuf
[16] = {0x00};
1083 byte_t dataoutbuf2
[16] = {0x00};
1084 uint8_t uid
[10] = {0x00};
1089 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1096 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0)) {
1098 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1101 for (uint8_t sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
1102 ui64Key
= emlGetKey(sectorNo
, keyType
);
1104 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
1106 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo
);
1110 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_NESTED
)) {
1112 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo
);
1117 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
1118 if(isOK
&& mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
)) {
1120 if (MF_DBGLEVEL
>= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo
, blockNo
);
1124 if (blockNo
< NumBlocksPerSector(sectorNo
) - 1) {
1125 emlSetMem(dataoutbuf
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
1126 } else { // sector trailer, keep the keys, set only the AC
1127 emlGetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
1128 memcpy(&dataoutbuf2
[6], &dataoutbuf
[6], 4);
1129 emlSetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
1136 if(mifare_classic_halt(pcs
, cuid
))
1137 if (MF_DBGLEVEL
>= 1)
1138 Dbprintf("Halt error");
1140 // ----------------------------- crypto1 destroy
1141 crypto1_destroy(pcs
);
1143 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1146 if (MF_DBGLEVEL
>= 2) DbpString("EMUL FILL SECTORS FINISHED");
1152 //-----------------------------------------------------------------------------
1153 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
1155 // PARAMS - workFlags
1156 // bit 0 - need get UID
1157 // bit 1 - need wupC
1158 // bit 2 - need HALT after sequence
1159 // bit 3 - need turn on FPGA before sequence
1160 // bit 4 - need turn off FPGA
1161 // bit 5 - need to set datain instead of issuing USB reply (called via ARM for StandAloneMode14a)
1162 // bit 6 - wipe tag.
1163 //-----------------------------------------------------------------------------
1164 // magic uid card generation 1 commands
1165 uint8_t wupC1
[] = { MIFARE_MAGICWUPC1
};
1166 uint8_t wupC2
[] = { MIFARE_MAGICWUPC2
};
1167 uint8_t wipeC
[] = { MIFARE_MAGICWIPEC
};
1169 void MifareCSetBlock(uint32_t arg0
, uint32_t arg1
, uint8_t *datain
){
1172 uint8_t workFlags
= arg0
;
1173 uint8_t blockNo
= arg1
;
1176 bool isOK
= false; //assume we will get an error
1177 uint8_t errormsg
= 0x00;
1178 uint8_t uid
[10] = {0x00};
1179 uint8_t data
[18] = {0x00};
1182 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
] = {0x00};
1183 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
] = {0x00};
1185 if (workFlags
& MAGIC_INIT
) {
1188 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1193 //loop doesn't loop just breaks out if error
1195 // read UID and return to client with write
1196 if (workFlags
& MAGIC_UID
) {
1197 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0)) {
1198 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
1199 errormsg
= MAGIC_UID
;
1203 if ( mifare_classic_halt_ex(NULL
) ) break;
1206 // wipe tag, fill it with zeros
1207 if (workFlags
& MAGIC_WIPE
){
1208 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1209 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1210 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("wupC1 error");
1211 errormsg
= MAGIC_WIPE
;
1215 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
1216 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1217 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("wipeC error");
1218 errormsg
= MAGIC_WIPE
;
1222 if ( mifare_classic_halt_ex(NULL
) ) break;
1226 if (workFlags
& MAGIC_WUPC
) {
1227 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1228 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1229 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("wupC1 error");
1230 errormsg
= MAGIC_WUPC
;
1234 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1235 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1236 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("wupC2 error");
1237 errormsg
= MAGIC_WUPC
;
1242 if ((mifare_sendcmd_short(NULL
, 0, ISO14443A_CMD_WRITEBLOCK
, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1243 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("write block send command error");
1248 memcpy(data
, datain
, 16);
1249 AppendCrc14443a(data
, 16);
1251 ReaderTransmit(data
, sizeof(data
), NULL
);
1252 if ((ReaderReceive(receivedAnswer
, receivedAnswerPar
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1253 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("write block send data error");
1258 if (workFlags
& MAGIC_OFF
)
1259 if ( mifare_classic_halt_ex(NULL
) ) break;
1267 cmd_send(CMD_ACK
,1,0,0,uid
,sizeof(uid
));
1269 OnErrorMagic(errormsg
);
1271 if (workFlags
& MAGIC_OFF
)
1275 void MifareCGetBlock(uint32_t arg0
, uint32_t arg1
, uint8_t *datain
){
1277 uint8_t workFlags
= arg0
;
1278 uint8_t blockNo
= arg1
;
1279 uint8_t errormsg
= 0x00;
1280 bool isOK
= false; //assume we will get an error
1283 uint8_t data
[MAX_MIFARE_FRAME_SIZE
];
1284 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
] = {0x00};
1285 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
] = {0x00};
1287 memset(data
, 0x00, sizeof(data
));
1289 if (workFlags
& MAGIC_INIT
) {
1292 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1297 //loop doesn't loop just breaks out if error or done
1299 if (workFlags
& MAGIC_WUPC
) {
1300 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1301 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1302 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("wupC1 error");
1303 errormsg
= MAGIC_WUPC
;
1307 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1308 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1309 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("wupC2 error");
1310 errormsg
= MAGIC_WUPC
;
1316 if ((mifare_sendcmd_short(NULL
, 0, ISO14443A_CMD_READBLOCK
, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 18)) {
1317 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("read block send command error");
1322 memcpy(data
, receivedAnswer
, sizeof(data
));
1325 if (workFlags
& MAGIC_HALT
)
1326 mifare_classic_halt_ex(NULL
);
1331 // if MAGIC_DATAIN, the data stays on device side.
1332 if (workFlags
& MAGIC_DATAIN
) {
1334 memcpy(datain
, data
, sizeof(data
));
1337 cmd_send(CMD_ACK
,1,0,0,data
,sizeof(data
));
1339 OnErrorMagic(errormsg
);
1342 if (workFlags
& MAGIC_OFF
)
1346 void MifareCIdent(){
1350 uint8_t receivedAnswer
[1] = {0x00};
1351 uint8_t receivedAnswerPar
[1] = {0x00};
1353 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1354 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1358 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1359 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1363 // removed the if, since some magic tags misbehavies and send an answer to it.
1364 mifare_classic_halt(NULL
, 0);
1365 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
1368 void OnSuccessMagic(){
1369 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1373 void OnErrorMagic(uint8_t reason
){
1374 // ACK, ISOK, reason,0,0,0
1375 cmd_send(CMD_ACK
,0,reason
,0,0,0);
1379 void MifareCollectNonces(uint32_t arg0
, uint32_t arg1
){
1386 void Mifare_DES_Auth1(uint8_t arg0
, uint8_t *datain
){
1388 byte_t dataout
[12] = {0x00};
1389 uint8_t uid
[10] = {0x00};
1392 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1396 int len
= iso14443a_select_card(uid
, NULL
, &cuid
, true, 0);
1398 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
1403 if(mifare_desfire_des_auth1(cuid
, dataout
)){
1404 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication part1: Fail.");
1409 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 1 FINISHED");
1410 cmd_send(CMD_ACK
,1,cuid
,0,dataout
, sizeof(dataout
));
1413 void Mifare_DES_Auth2(uint32_t arg0
, uint8_t *datain
){
1415 uint32_t cuid
= arg0
;
1416 uint8_t key
[16] = {0x00};
1417 byte_t dataout
[12] = {0x00};
1420 memcpy(key
, datain
, 16);
1422 isOK
= mifare_desfire_des_auth2(cuid
, key
, dataout
);
1425 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) Dbprintf("Authentication part2: Failed");
1430 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 2 FINISHED");
1432 cmd_send(CMD_ACK
, isOK
, 0, 0, dataout
, sizeof(dataout
));
1433 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);