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];
40 struct Crypto1State mpcs
= {0, 0};
41 struct Crypto1State
*pcs
;
44 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
53 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0)) {
54 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
58 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
59 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
63 if(mifare_classic_readblock(pcs
, cuid
, blockNo
, dataoutbuf
)) {
64 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block error");
68 if(mifare_classic_halt(pcs
, cuid
)) {
69 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
77 // ----------------------------- crypto1 destroy
80 if (MF_DBGLEVEL
>= 2) DbpString("READ BLOCK FINISHED");
83 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16);
86 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
90 void MifareUC_Auth(uint8_t arg0
, uint8_t *keybytes
){
92 bool turnOffField
= (arg0
== 1);
94 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
96 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
100 if(!iso14443a_select_card(NULL
, NULL
, NULL
, true, 0)) {
101 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
106 if(!mifare_ultra_auth(keybytes
)){
107 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication failed");
113 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
116 cmd_send(CMD_ACK
,1,0,0,0,0);
120 // Arg1 = UsePwd bool
121 // datain = PWD bytes,
122 void MifareUReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
)
124 uint8_t blockNo
= arg0
;
125 byte_t dataout
[16] = {0x00};
126 bool useKey
= (arg1
== 1); //UL_C
127 bool usePwd
= (arg1
== 2); //UL_EV1/NTAG
131 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
135 int len
= iso14443a_select_card(NULL
, NULL
, NULL
, true, 0);
137 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%02X)",len
);
142 // UL-C authentication
144 uint8_t key
[16] = {0x00};
145 memcpy(key
, datain
, sizeof(key
) );
147 if ( !mifare_ultra_auth(key
) ) {
153 // UL-EV1 / NTAG authentication
155 uint8_t pwd
[4] = {0x00};
156 memcpy(pwd
, datain
, 4);
157 uint8_t pack
[4] = {0,0,0,0};
158 if (!mifare_ul_ev1_auth(pwd
, pack
)) {
164 if( mifare_ultra_readblock(blockNo
, dataout
) ) {
165 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Read block error");
170 if( mifare_ultra_halt() ) {
171 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Halt error");
176 cmd_send(CMD_ACK
,1,0,0,dataout
,16);
177 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
181 //-----------------------------------------------------------------------------
182 // Select, Authenticate, Read a MIFARE tag.
183 // read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)
184 //-----------------------------------------------------------------------------
185 void MifareReadSector(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
188 uint8_t sectorNo
= arg0
;
189 uint8_t keyType
= arg1
;
190 uint64_t ui64Key
= 0;
191 ui64Key
= bytes_to_num(datain
, 6);
195 byte_t dataoutbuf
[16 * 16];
198 struct Crypto1State mpcs
= {0, 0};
199 struct Crypto1State
*pcs
;
202 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
211 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0)) {
213 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
217 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
219 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
222 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
223 if(mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
+ 16 * blockNo
)) {
225 if (MF_DBGLEVEL
>= 1) Dbprintf("Read sector %2d block %2d error", sectorNo
, blockNo
);
230 if(mifare_classic_halt(pcs
, cuid
)) {
231 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
234 // ----------------------------- crypto1 destroy
235 crypto1_destroy(pcs
);
237 if (MF_DBGLEVEL
>= 2) DbpString("READ SECTOR FINISHED");
240 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16*NumBlocksPerSector(sectorNo
));
244 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
248 // arg0 = blockNo (start)
249 // arg1 = Pages (number of blocks)
251 // datain = KEY bytes
252 void MifareUReadCard(uint8_t arg0
, uint16_t arg1
, uint8_t arg2
, uint8_t *datain
)
256 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
258 // free eventually allocated BigBuf memory
263 uint8_t blockNo
= arg0
;
264 uint16_t blocks
= arg1
;
265 bool useKey
= (arg2
== 1); //UL_C
266 bool usePwd
= (arg2
== 2); //UL_EV1/NTAG
267 uint32_t countblocks
= 0;
268 uint8_t *dataout
= BigBuf_malloc(CARD_MEMORY_SIZE
);
269 if (dataout
== NULL
){
270 Dbprintf("out of memory");
275 int len
= iso14443a_select_card(NULL
, NULL
, NULL
, true, 0);
277 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%d)",len
);
282 // UL-C authentication
284 uint8_t key
[16] = {0x00};
285 memcpy(key
, datain
, sizeof(key
) );
287 if ( !mifare_ultra_auth(key
) ) {
293 // UL-EV1 / NTAG authentication
295 uint8_t pwd
[4] = {0x00};
296 memcpy(pwd
, datain
, sizeof(pwd
));
297 uint8_t pack
[4] = {0,0,0,0};
299 if (!mifare_ul_ev1_auth(pwd
, pack
)){
305 for (int i
= 0; i
< blocks
; i
++){
306 if ((i
*4) + 4 >= CARD_MEMORY_SIZE
) {
307 Dbprintf("Data exceeds buffer!!");
311 len
= mifare_ultra_readblock(blockNo
+ i
, dataout
+ 4 * i
);
314 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Read block %d error",i
);
315 // if no blocks read - error out
320 //stop at last successful read block and return what we got
328 len
= mifare_ultra_halt();
330 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Halt error");
335 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) Dbprintf("Blocks read %d", countblocks
);
339 cmd_send(CMD_ACK
, 1, countblocks
, BigBuf_max_traceLen(), 0, 0);
340 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
345 //-----------------------------------------------------------------------------
346 // Select, Authenticate, Write a MIFARE tag.
348 //-----------------------------------------------------------------------------
349 void MifareWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
352 uint8_t blockNo
= arg0
;
353 uint8_t keyType
= arg1
;
354 uint64_t ui64Key
= 0;
355 byte_t blockdata
[16];
357 ui64Key
= bytes_to_num(datain
, 6);
358 memcpy(blockdata
, datain
+ 10, 16);
364 struct Crypto1State mpcs
= {0, 0};
365 struct Crypto1State
*pcs
;
368 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
377 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0)) {
378 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
382 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
383 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
387 if(mifare_classic_writeblock(pcs
, cuid
, blockNo
, blockdata
)) {
388 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
392 if(mifare_classic_halt(pcs
, cuid
)) {
393 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
401 // ----------------------------- crypto1 destroy
402 crypto1_destroy(pcs
);
404 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
407 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
412 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
416 /* // Command not needed but left for future testing
417 void MifareUWriteBlockCompat(uint8_t arg0, uint8_t *datain)
419 uint8_t blockNo = arg0;
420 byte_t blockdata[16] = {0x00};
422 memcpy(blockdata, datain, 16);
424 uint8_t uid[10] = {0x00};
426 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
429 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
431 if(!iso14443a_select_card(uid, NULL, NULL, true, 0)) {
432 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
437 if(mifare_ultra_writeblock_compat(blockNo, blockdata)) {
438 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
442 if(mifare_ultra_halt()) {
443 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
448 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");
450 cmd_send(CMD_ACK,1,0,0,0,0);
451 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
456 // Arg0 : Block to write to.
457 // Arg1 : 0 = use no authentication.
458 // 1 = use 0x1A authentication.
459 // 2 = use 0x1B authentication.
460 // datain : 4 first bytes is data to be written.
461 // : 4/16 next bytes is authentication key.
462 void MifareUWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
)
464 uint8_t blockNo
= arg0
;
465 bool useKey
= (arg1
== 1); //UL_C
466 bool usePwd
= (arg1
== 2); //UL_EV1/NTAG
467 byte_t blockdata
[4] = {0x00};
469 memcpy(blockdata
, datain
,4);
473 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
477 if(!iso14443a_select_card(NULL
, NULL
, NULL
, true, 0)) {
478 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
483 // UL-C authentication
485 uint8_t key
[16] = {0x00};
486 memcpy(key
, datain
+4, sizeof(key
) );
488 if ( !mifare_ultra_auth(key
) ) {
494 // UL-EV1 / NTAG authentication
496 uint8_t pwd
[4] = {0x00};
497 memcpy(pwd
, datain
+4, 4);
498 uint8_t pack
[4] = {0,0,0,0};
499 if (!mifare_ul_ev1_auth(pwd
, pack
)) {
505 if(mifare_ultra_writeblock(blockNo
, blockdata
)) {
506 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
511 if(mifare_ultra_halt()) {
512 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
517 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
519 cmd_send(CMD_ACK
,1,0,0,0,0);
520 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
524 void MifareUSetPwd(uint8_t arg0
, uint8_t *datain
){
526 uint8_t pwd
[16] = {0x00};
527 byte_t blockdata
[4] = {0x00};
529 memcpy(pwd
, datain
, 16);
531 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
532 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
536 if(!iso14443a_select_card(NULL
, NULL
, NULL
, true, 0)) {
537 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
542 blockdata
[0] = pwd
[7];
543 blockdata
[1] = pwd
[6];
544 blockdata
[2] = pwd
[5];
545 blockdata
[3] = pwd
[4];
546 if(mifare_ultra_writeblock( 44, blockdata
)) {
547 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
552 blockdata
[0] = pwd
[3];
553 blockdata
[1] = pwd
[2];
554 blockdata
[2] = pwd
[1];
555 blockdata
[3] = pwd
[0];
556 if(mifare_ultra_writeblock( 45, blockdata
)) {
557 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
562 blockdata
[0] = pwd
[15];
563 blockdata
[1] = pwd
[14];
564 blockdata
[2] = pwd
[13];
565 blockdata
[3] = pwd
[12];
566 if(mifare_ultra_writeblock( 46, blockdata
)) {
567 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
572 blockdata
[0] = pwd
[11];
573 blockdata
[1] = pwd
[10];
574 blockdata
[2] = pwd
[9];
575 blockdata
[3] = pwd
[8];
576 if(mifare_ultra_writeblock( 47, blockdata
)) {
577 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
582 if(mifare_ultra_halt()) {
583 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
588 cmd_send(CMD_ACK
,1,0,0,0,0);
589 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
593 // Return 1 if the nonce is invalid else return 0
594 int valid_nonce(uint32_t Nt
, uint32_t NtEnc
, uint32_t Ks1
, uint8_t *parity
) {
595 return ((oddparity8((Nt
>> 24) & 0xFF) == ((parity
[0]) ^ oddparity8((NtEnc
>> 24) & 0xFF) ^ BIT(Ks1
,16))) & \
596 (oddparity8((Nt
>> 16) & 0xFF) == ((parity
[1]) ^ oddparity8((NtEnc
>> 16) & 0xFF) ^ BIT(Ks1
,8))) & \
597 (oddparity8((Nt
>> 8) & 0xFF) == ((parity
[2]) ^ oddparity8((NtEnc
>> 8) & 0xFF) ^ BIT(Ks1
,0)))) ? 1 : 0;
601 //-----------------------------------------------------------------------------
602 // acquire encrypted nonces in order to perform the attack described in
603 // Carlo Meijer, Roel Verdult, "Ciphertext-only Cryptanalysis on Hardened
604 // Mifare Classic Cards" in Proceedings of the 22nd ACM SIGSAC Conference on
605 // Computer and Communications Security, 2015
606 //-----------------------------------------------------------------------------
607 void MifareAcquireEncryptedNonces(uint32_t arg0
, uint32_t arg1
, uint32_t flags
, uint8_t *datain
)
609 uint64_t ui64Key
= 0;
612 uint8_t cascade_levels
= 0;
613 struct Crypto1State mpcs
= {0, 0};
614 struct Crypto1State
*pcs
;
616 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
619 uint8_t nt_par_enc
= 0;
620 uint8_t buf
[USB_CMD_DATA_SIZE
];
623 uint8_t blockNo
= arg0
& 0xff;
624 uint8_t keyType
= (arg0
>> 8) & 0xff;
625 uint8_t targetBlockNo
= arg1
& 0xff;
626 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
627 ui64Key
= bytes_to_num(datain
, 6);
628 bool initialize
= flags
& 0x0001;
629 bool slow
= flags
& 0x0002;
630 bool field_off
= flags
& 0x0004;
632 #define AUTHENTICATION_TIMEOUT 848 // card times out 1ms after wrong authentication (according to NXP documentation)
633 #define PRE_AUTHENTICATION_LEADTIME 400 // some (non standard) cards need a pause after select before they are ready for first authentication
639 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
646 uint16_t num_nonces
= 0;
647 bool have_uid
= false;
648 for (uint16_t i
= 0; i
<= USB_CMD_DATA_SIZE
- 9; ) {
650 // Test if the action was cancelled
657 if (!have_uid
) { // need a full select cycle to get the uid first
658 iso14a_card_select_t card_info
;
659 if(!iso14443a_select_card(uid
, &card_info
, &cuid
, true, 0)) {
660 if (MF_DBGLEVEL
>= 1) Dbprintf("AcquireNonces: Can't select card (ALL)");
663 switch (card_info
.uidlen
) {
664 case 4 : cascade_levels
= 1; break;
665 case 7 : cascade_levels
= 2; break;
666 case 10: cascade_levels
= 3; break;
670 } else { // no need for anticollision. We can directly select the card
671 if(!iso14443a_select_card(uid
, NULL
, NULL
, false, cascade_levels
)) {
672 if (MF_DBGLEVEL
>= 1) Dbprintf("AcquireNonces: Can't select card (UID)");
678 timeout
= GetCountSspClk() + PRE_AUTHENTICATION_LEADTIME
;
679 while(GetCountSspClk() < timeout
);
683 if (mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, NULL
)) {
684 if (MF_DBGLEVEL
>= 1) Dbprintf("AcquireNonces: Auth1 error");
688 // nested authentication
689 uint16_t len
= mifare_sendcmd_short(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType
& 0x01), targetBlockNo
, receivedAnswer
, par_enc
, NULL
);
691 if (MF_DBGLEVEL
>= 1) Dbprintf("AcquireNonces: Auth2 error len=%d", len
);
695 // send a dummy byte as reader response in order to trigger the cards authentication timeout
696 uint8_t dummy_answer
= 0;
697 ReaderTransmit(&dummy_answer
, 1, NULL
);
698 timeout
= GetCountSspClk() + AUTHENTICATION_TIMEOUT
;
701 if (num_nonces
% 2) {
702 memcpy(buf
+i
, receivedAnswer
, 4);
703 nt_par_enc
= par_enc
[0] & 0xf0;
705 nt_par_enc
|= par_enc
[0] >> 4;
706 memcpy(buf
+i
+4, receivedAnswer
, 4);
707 memcpy(buf
+i
+8, &nt_par_enc
, 1);
711 // wait for the card to become ready again
712 while(GetCountSspClk() < timeout
);
718 crypto1_destroy(pcs
);
721 cmd_send(CMD_ACK
, isOK
, cuid
, num_nonces
, buf
, sizeof(buf
));
724 if (MF_DBGLEVEL
>= 3) DbpString("AcquireEncryptedNonces finished");
727 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
733 //-----------------------------------------------------------------------------
734 // MIFARE nested authentication.
736 //-----------------------------------------------------------------------------
737 void MifareNested(uint32_t arg0
, uint32_t arg1
, uint32_t calibrate
, uint8_t *datain
)
740 uint8_t blockNo
= arg0
& 0xff;
741 uint8_t keyType
= (arg0
>> 8) & 0xff;
742 uint8_t targetBlockNo
= arg1
& 0xff;
743 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
744 uint64_t ui64Key
= 0;
746 ui64Key
= bytes_to_num(datain
, 6);
749 uint16_t rtr
, i
, j
, len
;
751 static uint16_t dmin
, dmax
;
753 uint32_t cuid
, nt1
, nt2
, nttmp
, nttest
, ks1
;
755 uint32_t target_nt
[2], target_ks
[2];
757 uint8_t par_array
[4];
759 struct Crypto1State mpcs
= {0, 0};
760 struct Crypto1State
*pcs
;
762 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
764 uint32_t auth1_time
, auth2_time
;
765 static uint16_t delta_time
;
769 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
771 // free eventually allocated BigBuf memory
774 if (calibrate
) clear_trace();
777 // statistics on nonce distance
779 #define NESTED_MAX_TRIES 12
780 uint16_t unsuccessfull_tries
= 0;
781 if (calibrate
) { // for first call only. Otherwise reuse previous calibration
789 for (rtr
= 0; rtr
< 17; rtr
++) {
791 // Test if the action was cancelled
797 // prepare next select. No need to power down the card.
798 if(mifare_classic_halt(pcs
, cuid
)) {
799 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
804 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0)) {
805 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
811 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
812 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
818 auth2_time
= auth1_time
+ delta_time
;
822 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_NESTED
, &nt2
, &auth2_time
)) {
823 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error");
828 nttmp
= prng_successor(nt1
, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160
829 for (i
= 101; i
< 1200; i
++) {
830 nttmp
= prng_successor(nttmp
, 1);
831 if (nttmp
== nt2
) break;
841 delta_time
= auth2_time
- auth1_time
+ 32; // allow some slack for proper timing
843 if (MF_DBGLEVEL
>= 3) Dbprintf("Nested: calibrating... ntdist=%d", i
);
845 unsuccessfull_tries
++;
846 if (unsuccessfull_tries
> NESTED_MAX_TRIES
) { // card isn't vulnerable to nested attack (random numbers are not predictable)
852 davg
= (davg
+ (rtr
- 1)/2) / (rtr
- 1);
854 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
);
861 // -------------------------------------------------------------------------------------------------
865 // get crypted nonces for target sector
866 for(i
=0; i
< 2 && !isOK
; i
++) { // look for exactly two different nonces
869 while(target_nt
[i
] == 0) { // continue until we have an unambiguous nonce
871 // prepare next select. No need to power down the card.
872 if(mifare_classic_halt(pcs
, cuid
)) {
873 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
877 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0)) {
878 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
883 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
884 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
888 // nested authentication
889 auth2_time
= auth1_time
+ delta_time
;
890 len
= mifare_sendcmd_short(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType
& 0x01), targetBlockNo
, receivedAnswer
, par
, &auth2_time
);
892 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error len=%d", len
);
896 nt2
= bytes_to_num(receivedAnswer
, 4);
897 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i
+1, nt1
, nt2
, par
[0]);
899 // Parity validity check
900 for (j
= 0; j
< 4; j
++) {
901 par_array
[j
] = (oddparity8(receivedAnswer
[j
]) != ((par
[0] >> (7-j
)) & 0x01));
905 nttest
= prng_successor(nt1
, dmin
- 1);
906 for (j
= dmin
; j
< dmax
+ 1; j
++) {
907 nttest
= prng_successor(nttest
, 1);
910 if (valid_nonce(nttest
, nt2
, ks1
, par_array
)){
911 if (ncount
> 0) { // we are only interested in disambiguous nonces, try again
912 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i
+1, j
);
916 target_nt
[i
] = nttest
;
919 if (i
== 1 && target_nt
[1] == target_nt
[0]) { // we need two different nonces
921 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j
);
924 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i
+1, j
);
927 if (target_nt
[i
] == 0 && j
== dmax
+1 && MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i
+1);
933 // ----------------------------- crypto1 destroy
934 crypto1_destroy(pcs
);
936 byte_t buf
[4 + 4 * 4];
937 memcpy(buf
, &cuid
, 4);
938 memcpy(buf
+4, &target_nt
[0], 4);
939 memcpy(buf
+8, &target_ks
[0], 4);
940 memcpy(buf
+12, &target_nt
[1], 4);
941 memcpy(buf
+16, &target_ks
[1], 4);
944 cmd_send(CMD_ACK
, isOK
, 0, targetBlockNo
+ (targetKeyType
* 0x100), buf
, sizeof(buf
));
947 if (MF_DBGLEVEL
>= 3) DbpString("NESTED FINISHED");
949 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
954 //-----------------------------------------------------------------------------
955 // MIFARE check keys. key count up to 85.
957 //-----------------------------------------------------------------------------
958 void MifareChkKeys(uint16_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
961 uint8_t blockNo
= arg0
& 0xff;
962 uint8_t keyType
= (arg0
>> 8) & 0xff;
963 bool clearTrace
= arg1
;
964 uint8_t keyCount
= arg2
;
965 uint64_t ui64Key
= 0;
972 struct Crypto1State mpcs
= {0, 0};
973 struct Crypto1State
*pcs
;
977 int OLD_MF_DBGLEVEL
= MF_DBGLEVEL
;
978 MF_DBGLEVEL
= MF_DBG_NONE
;
983 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
985 if (clearTrace
) clear_trace();
988 for (i
= 0; i
< keyCount
; i
++) {
989 if(mifare_classic_halt(pcs
, cuid
)) {
990 if (MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Halt error");
993 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0)) {
994 if (OLD_MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Can't select card");
998 ui64Key
= bytes_to_num(datain
+ i
* 6, 6);
999 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
1007 // ----------------------------- crypto1 destroy
1008 crypto1_destroy(pcs
);
1011 cmd_send(CMD_ACK
,isOK
,0,0,datain
+ i
* 6,6);
1014 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1018 // restore debug level
1019 MF_DBGLEVEL
= OLD_MF_DBGLEVEL
;
1022 //-----------------------------------------------------------------------------
1023 // MIFARE commands set debug level
1025 //-----------------------------------------------------------------------------
1026 void MifareSetDbgLvl(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1028 Dbprintf("Debug level: %d", MF_DBGLEVEL
);
1031 //-----------------------------------------------------------------------------
1032 // Work with emulator memory
1034 // Note: we call FpgaDownloadAndGo(FPGA_BITSTREAM_HF) here although FPGA is not
1035 // involved in dealing with emulator memory. But if it is called later, it might
1036 // destroy the Emulator Memory.
1037 //-----------------------------------------------------------------------------
1039 void MifareEMemClr(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1040 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1044 void MifareEMemSet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1045 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1046 if (arg2
==0) arg2
= 16; // backwards compat... default bytewidth
1047 emlSetMem_xt(datain
, arg0
, arg1
, arg2
); // data, block num, blocks count, block byte width
1050 void MifareEMemGet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1051 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1052 byte_t buf
[USB_CMD_DATA_SIZE
];
1053 emlGetMem(buf
, arg0
, arg1
); // data, block num, blocks count (max 4)
1056 cmd_send(CMD_ACK
,arg0
,arg1
,0,buf
,USB_CMD_DATA_SIZE
);
1060 //-----------------------------------------------------------------------------
1061 // Load a card into the emulator memory
1063 //-----------------------------------------------------------------------------
1064 void MifareECardLoad(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1065 uint8_t numSectors
= arg0
;
1066 uint8_t keyType
= arg1
;
1067 uint64_t ui64Key
= 0;
1069 struct Crypto1State mpcs
= {0, 0};
1070 struct Crypto1State
*pcs
;
1074 byte_t dataoutbuf
[16];
1075 byte_t dataoutbuf2
[16];
1081 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1088 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0)) {
1090 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1093 for (uint8_t sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
1094 ui64Key
= emlGetKey(sectorNo
, keyType
);
1096 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
1098 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo
);
1102 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_NESTED
)) {
1104 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo
);
1109 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
1110 if(isOK
&& mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
)) {
1112 if (MF_DBGLEVEL
>= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo
, blockNo
);
1116 if (blockNo
< NumBlocksPerSector(sectorNo
) - 1) {
1117 emlSetMem(dataoutbuf
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
1118 } else { // sector trailer, keep the keys, set only the AC
1119 emlGetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
1120 memcpy(&dataoutbuf2
[6], &dataoutbuf
[6], 4);
1121 emlSetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
1128 if(mifare_classic_halt(pcs
, cuid
)) {
1129 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1132 // ----------------------------- crypto1 destroy
1133 crypto1_destroy(pcs
);
1135 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1138 if (MF_DBGLEVEL
>= 2) DbpString("EMUL FILL SECTORS FINISHED");
1144 //-----------------------------------------------------------------------------
1145 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
1147 // PARAMS - workFlags
1148 // bit 0 - need get UID
1149 // bit 1 - need wupC
1150 // bit 2 - need HALT after sequence
1151 // bit 3 - need turn on FPGA before sequence
1152 // bit 4 - need turn off FPGA
1153 // bit 5 - need to set datain instead of issuing USB reply (called via ARM for StandAloneMode14a)
1154 // bit 6 - wipe tag.
1155 //-----------------------------------------------------------------------------
1156 // magic uid card generation 1 commands
1157 uint8_t wupC1
[] = { MIFARE_MAGICWUPC1
};
1158 uint8_t wupC2
[] = { MIFARE_MAGICWUPC2
};
1159 uint8_t wipeC
[] = { MIFARE_MAGICWIPEC
};
1161 void MifareCSetBlock(uint32_t arg0
, uint32_t arg1
, uint8_t *datain
){
1164 uint8_t workFlags
= arg0
;
1165 uint8_t blockNo
= arg1
;
1167 Dbprintf("ICE :: CSetBlocks Flags %02x", workFlags
);
1170 uint8_t uid
[10] = {0x00};
1171 uint8_t data
[18] = {0x00};
1174 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1175 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1177 if (workFlags
& MAGIC_INIT
) {
1180 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1185 // read UID and return to client
1186 if (workFlags
& MAGIC_UID
) {
1187 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0)) {
1188 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
1189 OnErrorMagic(MAGIC_UID
);
1193 // wipe tag, fill it with zeros
1194 if (workFlags
& MAGIC_WIPE
){
1195 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1196 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1197 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("wupC1 error");
1198 OnErrorMagic(MAGIC_WIPE
);
1201 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
1202 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1203 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("wipeC error");
1204 OnErrorMagic(MAGIC_WIPE
);
1209 if (workFlags
& MAGIC_WUPC
) {
1210 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1211 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1212 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("wupC1 error");
1213 OnErrorMagic(MAGIC_WUPC
);
1216 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1217 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1218 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("wupC2 error");
1219 OnErrorMagic(MAGIC_WUPC
);
1223 if ((mifare_sendcmd_short(NULL
, 0, ISO14443A_CMD_WRITEBLOCK
, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1224 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("write block send command error");
1228 memcpy(data
, datain
, 16);
1229 AppendCrc14443a(data
, 16);
1231 ReaderTransmit(data
, sizeof(data
), NULL
);
1232 if ((ReaderReceive(receivedAnswer
, receivedAnswerPar
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1233 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("write block send data error");
1237 if (workFlags
& MAGIC_OFF
)
1238 mifare_classic_halt_ex(NULL
);
1241 // check if uid is cuid?
1242 cmd_send(CMD_ACK
,1,0,0,uid
,sizeof(uid
));
1245 if (workFlags
& MAGIC_OFF
)
1249 void MifareCGetBlock(uint32_t arg0
, uint32_t arg1
, uint8_t *datain
){
1251 uint8_t workFlags
= arg0
;
1252 uint8_t blockNo
= arg1
;
1255 uint8_t data
[MAX_MIFARE_FRAME_SIZE
];
1256 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1257 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1259 memset(data
, 0x00, sizeof(data
));
1261 if (workFlags
& MAGIC_INIT
) {
1264 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1269 if (workFlags
& MAGIC_WUPC
) {
1270 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1271 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1272 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("wupC1 error");
1273 OnErrorMagic(MAGIC_WUPC
);
1276 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1277 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1278 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("wupC2 error");
1279 OnErrorMagic(MAGIC_WUPC
);
1284 if ((mifare_sendcmd_short(NULL
, 0, ISO14443A_CMD_READBLOCK
, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 18)) {
1285 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("read block send command error");
1289 memcpy(data
, receivedAnswer
, sizeof(data
));
1292 if (workFlags
& MAGIC_HALT
)
1293 mifare_classic_halt_ex(NULL
);
1297 // if MAGIC_DATAIN, the data stays on device side.
1298 if (workFlags
& MAGIC_DATAIN
)
1299 memcpy(datain
, data
, sizeof(data
));
1301 cmd_send(CMD_ACK
,1,0,0,data
,sizeof(data
));
1305 if (workFlags
& MAGIC_OFF
)
1309 void MifareCIdent(){
1313 uint8_t receivedAnswer
[1];
1314 uint8_t receivedAnswerPar
[1];
1316 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1317 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1321 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1322 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1326 // removed the if, since some magic tags misbehavies and send an answer to it.
1327 mifare_classic_halt(NULL
, 0);
1328 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
1331 void OnSuccessMagic(){
1332 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1336 void OnErrorMagic(uint8_t reason
){
1337 // ACK, ISOK, reason,0,0,0
1338 cmd_send(CMD_ACK
,0,reason
,0,0,0);
1342 void MifareCollectNonces(uint32_t arg0
, uint32_t arg1
){
1349 void Mifare_DES_Auth1(uint8_t arg0
, uint8_t *datain
){
1351 byte_t dataout
[11] = {0x00};
1352 uint8_t uid
[10] = {0x00};
1353 uint32_t cuid
= 0x00;
1355 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1358 int len
= iso14443a_select_card(uid
, NULL
, &cuid
, true, 0);
1360 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
1365 if(mifare_desfire_des_auth1(cuid
, dataout
)){
1366 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication part1: Fail.");
1371 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 1 FINISHED");
1372 cmd_send(CMD_ACK
,1,cuid
,0,dataout
, sizeof(dataout
));
1375 void Mifare_DES_Auth2(uint32_t arg0
, uint8_t *datain
){
1377 uint32_t cuid
= arg0
;
1378 uint8_t key
[16] = {0x00};
1379 byte_t dataout
[12] = {0x00};
1382 memcpy(key
, datain
, 16);
1384 isOK
= mifare_desfire_des_auth2(cuid
, key
, dataout
);
1387 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) Dbprintf("Authentication part2: Failed");
1392 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 2 FINISHED");
1394 cmd_send(CMD_ACK
, isOK
, 0, 0, dataout
, sizeof(dataout
));
1395 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);