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"
21 #include "fpgaloader.h"
23 #define HARDNESTED_AUTHENTICATION_TIMEOUT 848 // card times out 1ms after wrong authentication (according to NXP documentation)
24 #define HARDNESTED_PRE_AUTHENTICATION_LEADTIME 400 // some (non standard) cards need a pause after select before they are ready for first authentication
27 // the block number for the ISO14443-4 PCB
28 static uint8_t pcb_blocknum = 0;
29 // Deselect card by sending a s-block. the crc is precalced for speed
30 static uint8_t deselect_cmd[] = {0xc2,0xe0,0xb4};
32 static void OnSuccess(){
34 ReaderTransmit(deselect_cmd, 3 , NULL);
35 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
40 static void OnError(uint8_t reason
){
42 // ReaderTransmit(deselect_cmd, 3 , NULL);
43 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
45 cmd_send(CMD_ACK
,0,reason
,0,0,0);
49 //-----------------------------------------------------------------------------
50 // Select, Authenticate, Read a MIFARE tag.
52 //-----------------------------------------------------------------------------
53 void MifareReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
57 uint8_t blockNo
= arg0
;
58 uint8_t keyType
= arg1
;
60 ui64Key
= bytes_to_num(datain
, 6);
63 byte_t dataoutbuf
[16];
66 struct Crypto1State mpcs
= {0, 0};
67 struct Crypto1State
*pcs
;
70 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
75 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
76 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
80 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
81 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
85 if(mifare_classic_readblock(pcs
, cuid
, blockNo
, dataoutbuf
)) {
86 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block error");
90 if(mifare_classic_halt(pcs
, cuid
)) {
91 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
99 // ----------------------------- crypto1 destroy
100 crypto1_destroy(pcs
);
102 if (MF_DBGLEVEL
>= 2) DbpString("READ BLOCK FINISHED");
105 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16);
108 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
112 void MifareUC_Auth(uint8_t arg0
, uint8_t *keybytes
){
115 bool turnOffField
= (arg0
== 1);
117 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
119 if (!iso14443a_select_card(NULL
, NULL
, NULL
, true, 0, true)) {
120 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
125 if (!mifare_ultra_auth(keybytes
)){
126 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication failed");
132 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
136 cmd_send(CMD_ACK
,1,0,0,0,0);
141 // Arg1 = UsePwd bool
142 // datain = PWD bytes,
143 void MifareUReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
)
147 uint8_t blockNo
= arg0
;
148 byte_t dataout
[16] = {0x00};
149 bool useKey
= (arg1
== 1); //UL_C
150 bool usePwd
= (arg1
== 2); //UL_EV1/NTAG
152 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
154 int len
= iso14443a_select_card(NULL
, NULL
, NULL
, true, 0, true);
156 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%02X)",len
);
161 // UL-C authentication
163 uint8_t key
[16] = {0x00};
164 memcpy(key
, datain
, sizeof(key
) );
166 if ( !mifare_ultra_auth(key
) ) {
172 // UL-EV1 / NTAG authentication
174 uint8_t pwd
[4] = {0x00};
175 memcpy(pwd
, datain
, 4);
176 uint8_t pack
[4] = {0,0,0,0};
177 if (!mifare_ul_ev1_auth(pwd
, pack
)) {
183 if (mifare_ultra_readblock(blockNo
, dataout
)) {
184 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Read block error");
189 if (mifare_ultra_halt()) {
190 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Halt error");
195 cmd_send(CMD_ACK
,1,0,0,dataout
,16);
196 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
201 //-----------------------------------------------------------------------------
202 // Select, Authenticate, Read a MIFARE tag.
203 // read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)
204 //-----------------------------------------------------------------------------
205 void MifareReadSector(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
208 uint8_t sectorNo
= arg0
;
209 uint8_t keyType
= arg1
;
210 uint64_t ui64Key
= 0;
211 ui64Key
= bytes_to_num(datain
, 6);
215 byte_t dataoutbuf
[16 * 16];
218 struct Crypto1State mpcs
= {0, 0};
219 struct Crypto1State
*pcs
;
222 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
231 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
233 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
237 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
239 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
242 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
243 if(mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
+ 16 * blockNo
)) {
245 if (MF_DBGLEVEL
>= 1) Dbprintf("Read sector %2d block %2d error", sectorNo
, blockNo
);
250 if(mifare_classic_halt(pcs
, cuid
)) {
251 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
254 // ----------------------------- crypto1 destroy
255 crypto1_destroy(pcs
);
257 if (MF_DBGLEVEL
>= 2) DbpString("READ SECTOR FINISHED");
260 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16*NumBlocksPerSector(sectorNo
));
264 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
268 // arg0 = blockNo (start)
269 // arg1 = Pages (number of blocks)
271 // datain = KEY bytes
272 void MifareUReadCard(uint8_t arg0
, uint16_t arg1
, uint8_t arg2
, uint8_t *datain
)
275 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
277 // free eventually allocated BigBuf memory
281 uint8_t blockNo
= arg0
;
282 uint16_t blocks
= arg1
;
283 bool useKey
= (arg2
== 1); //UL_C
284 bool usePwd
= (arg2
== 2); //UL_EV1/NTAG
285 uint32_t countblocks
= 0;
286 uint8_t *dataout
= BigBuf_malloc(CARD_MEMORY_SIZE
);
287 if (dataout
== NULL
){
288 Dbprintf("out of memory");
293 int len
= iso14443a_select_card(NULL
, NULL
, NULL
, true, 0, true);
295 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%d)",len
);
300 // UL-C authentication
302 uint8_t key
[16] = {0x00};
303 memcpy(key
, datain
, sizeof(key
) );
305 if ( !mifare_ultra_auth(key
) ) {
311 // UL-EV1 / NTAG authentication
313 uint8_t pwd
[4] = {0x00};
314 memcpy(pwd
, datain
, sizeof(pwd
));
315 uint8_t pack
[4] = {0,0,0,0};
317 if (!mifare_ul_ev1_auth(pwd
, pack
)){
323 for (int i
= 0; i
< blocks
; i
++){
324 if ((i
*4) + 4 >= CARD_MEMORY_SIZE
) {
325 Dbprintf("Data exceeds buffer!!");
329 len
= mifare_ultra_readblock(blockNo
+ i
, dataout
+ 4 * i
);
332 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Read block %d error",i
);
333 // if no blocks read - error out
338 //stop at last successful read block and return what we got
346 len
= mifare_ultra_halt();
348 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Halt error");
353 if (MF_DBGLEVEL
>= MF_DBG_DEBUG
) Dbprintf("Blocks read %d", countblocks
);
355 cmd_send(CMD_ACK
, 1, countblocks
*4, BigBuf_max_traceLen(), 0, 0);
357 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
363 //-----------------------------------------------------------------------------
364 // Select, Authenticate, Write a MIFARE tag.
366 //-----------------------------------------------------------------------------
367 void MifareWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
370 uint8_t blockNo
= arg0
;
371 uint8_t keyType
= arg1
;
372 uint64_t ui64Key
= 0;
373 byte_t blockdata
[16];
375 ui64Key
= bytes_to_num(datain
, 6);
376 memcpy(blockdata
, datain
+ 10, 16);
382 struct Crypto1State mpcs
= {0, 0};
383 struct Crypto1State
*pcs
;
386 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
395 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
396 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
400 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
401 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
405 if(mifare_classic_writeblock(pcs
, cuid
, blockNo
, blockdata
)) {
406 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
410 if(mifare_classic_halt(pcs
, cuid
)) {
411 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
419 // ----------------------------- crypto1 destroy
420 crypto1_destroy(pcs
);
422 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
425 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
430 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
434 /* // Command not needed but left for future testing
435 void MifareUWriteBlockCompat(uint8_t arg0, uint8_t *datain)
437 uint8_t blockNo = arg0;
438 byte_t blockdata[16] = {0x00};
440 memcpy(blockdata, datain, 16);
442 uint8_t uid[10] = {0x00};
444 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
447 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
449 if(!iso14443a_select_card(uid, NULL, NULL, true, 0)) {
450 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
455 if(mifare_ultra_writeblock_compat(blockNo, blockdata)) {
456 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
460 if(mifare_ultra_halt()) {
461 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
466 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");
468 cmd_send(CMD_ACK,1,0,0,0,0);
469 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
474 // Arg0 : Block to write to.
475 // Arg1 : 0 = use no authentication.
476 // 1 = use 0x1A authentication.
477 // 2 = use 0x1B authentication.
478 // datain : 4 first bytes is data to be written.
479 // : 4/16 next bytes is authentication key.
480 void MifareUWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
)
482 uint8_t blockNo
= arg0
;
483 bool useKey
= (arg1
== 1); //UL_C
484 bool usePwd
= (arg1
== 2); //UL_EV1/NTAG
485 byte_t blockdata
[4] = {0x00};
487 memcpy(blockdata
, datain
,4);
491 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
495 if(!iso14443a_select_card(NULL
, NULL
, NULL
, true, 0, true)) {
496 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
501 // UL-C authentication
503 uint8_t key
[16] = {0x00};
504 memcpy(key
, datain
+4, sizeof(key
) );
506 if ( !mifare_ultra_auth(key
) ) {
512 // UL-EV1 / NTAG authentication
514 uint8_t pwd
[4] = {0x00};
515 memcpy(pwd
, datain
+4, 4);
516 uint8_t pack
[4] = {0,0,0,0};
517 if (!mifare_ul_ev1_auth(pwd
, pack
)) {
523 if(mifare_ultra_writeblock(blockNo
, blockdata
)) {
524 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
529 if(mifare_ultra_halt()) {
530 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
535 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
537 cmd_send(CMD_ACK
,1,0,0,0,0);
538 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
542 void MifareUSetPwd(uint8_t arg0
, uint8_t *datain
){
544 uint8_t pwd
[16] = {0x00};
545 byte_t blockdata
[4] = {0x00};
547 memcpy(pwd
, datain
, 16);
549 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
550 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
554 if(!iso14443a_select_card(NULL
, NULL
, NULL
, true, 0, true)) {
555 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
560 blockdata
[0] = pwd
[7];
561 blockdata
[1] = pwd
[6];
562 blockdata
[2] = pwd
[5];
563 blockdata
[3] = pwd
[4];
564 if(mifare_ultra_writeblock( 44, blockdata
)) {
565 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
570 blockdata
[0] = pwd
[3];
571 blockdata
[1] = pwd
[2];
572 blockdata
[2] = pwd
[1];
573 blockdata
[3] = pwd
[0];
574 if(mifare_ultra_writeblock( 45, blockdata
)) {
575 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
580 blockdata
[0] = pwd
[15];
581 blockdata
[1] = pwd
[14];
582 blockdata
[2] = pwd
[13];
583 blockdata
[3] = pwd
[12];
584 if(mifare_ultra_writeblock( 46, blockdata
)) {
585 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
590 blockdata
[0] = pwd
[11];
591 blockdata
[1] = pwd
[10];
592 blockdata
[2] = pwd
[9];
593 blockdata
[3] = pwd
[8];
594 if(mifare_ultra_writeblock( 47, blockdata
)) {
595 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
600 if(mifare_ultra_halt()) {
601 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
606 cmd_send(CMD_ACK
,1,0,0,0,0);
607 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
611 // Return 1 if the nonce is invalid else return 0
612 int valid_nonce(uint32_t Nt
, uint32_t NtEnc
, uint32_t Ks1
, uint8_t *parity
) {
613 return ((oddparity8((Nt
>> 24) & 0xFF) == ((parity
[0]) ^ oddparity8((NtEnc
>> 24) & 0xFF) ^ BIT(Ks1
,16))) & \
614 (oddparity8((Nt
>> 16) & 0xFF) == ((parity
[1]) ^ oddparity8((NtEnc
>> 16) & 0xFF) ^ BIT(Ks1
,8))) & \
615 (oddparity8((Nt
>> 8) & 0xFF) == ((parity
[2]) ^ oddparity8((NtEnc
>> 8) & 0xFF) ^ BIT(Ks1
,0)))) ? 1 : 0;
619 //-----------------------------------------------------------------------------
620 // acquire encrypted nonces in order to perform the attack described in
621 // Carlo Meijer, Roel Verdult, "Ciphertext-only Cryptanalysis on Hardened
622 // Mifare Classic Cards" in Proceedings of the 22nd ACM SIGSAC Conference on
623 // Computer and Communications Security, 2015
624 //-----------------------------------------------------------------------------
625 void MifareAcquireEncryptedNonces(uint32_t arg0
, uint32_t arg1
, uint32_t flags
, uint8_t *datain
)
627 uint64_t ui64Key
= 0;
630 uint8_t cascade_levels
= 0;
631 struct Crypto1State mpcs
= {0, 0};
632 struct Crypto1State
*pcs
;
634 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
637 uint8_t nt_par_enc
= 0;
638 uint8_t buf
[USB_CMD_DATA_SIZE
];
641 uint8_t blockNo
= arg0
& 0xff;
642 uint8_t keyType
= (arg0
>> 8) & 0xff;
643 uint8_t targetBlockNo
= arg1
& 0xff;
644 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
645 ui64Key
= bytes_to_num(datain
, 6);
646 bool initialize
= flags
& 0x0001;
647 bool slow
= flags
& 0x0002;
648 bool field_off
= flags
& 0x0004;
654 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
661 uint16_t num_nonces
= 0;
662 bool have_uid
= false;
663 for (uint16_t i
= 0; i
<= USB_CMD_DATA_SIZE
- 9; ) {
665 // Test if the action was cancelled
672 if (!have_uid
) { // need a full select cycle to get the uid first
673 iso14a_card_select_t card_info
;
674 if(!iso14443a_select_card(uid
, &card_info
, &cuid
, true, 0, true)) {
675 if (MF_DBGLEVEL
>= 1) Dbprintf("AcquireNonces: Can't select card (ALL)");
678 switch (card_info
.uidlen
) {
679 case 4 : cascade_levels
= 1; break;
680 case 7 : cascade_levels
= 2; break;
681 case 10: cascade_levels
= 3; break;
685 } else { // no need for anticollision. We can directly select the card
686 if(!iso14443a_select_card(uid
, NULL
, NULL
, false, cascade_levels
, true)) {
687 if (MF_DBGLEVEL
>= 1) Dbprintf("AcquireNonces: Can't select card (UID)");
693 timeout
= GetCountSspClk() + HARDNESTED_PRE_AUTHENTICATION_LEADTIME
;
694 while(GetCountSspClk() < timeout
);
698 if (mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, NULL
)) {
699 if (MF_DBGLEVEL
>= 1) Dbprintf("AcquireNonces: Auth1 error");
703 // nested authentication
704 uint16_t len
= mifare_sendcmd_short(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType
& 0x01), targetBlockNo
, receivedAnswer
, par_enc
, NULL
);
706 if (MF_DBGLEVEL
>= 1) Dbprintf("AcquireNonces: Auth2 error len=%d", len
);
710 // send an incomplete dummy response in order to trigger the card's authentication failure timeout
711 uint8_t dummy_answer
[1] = {0};
712 ReaderTransmit(dummy_answer
, 1, NULL
);
714 timeout
= GetCountSspClk() + HARDNESTED_AUTHENTICATION_TIMEOUT
;
717 if (num_nonces
% 2) {
718 memcpy(buf
+i
, receivedAnswer
, 4);
719 nt_par_enc
= par_enc
[0] & 0xf0;
721 nt_par_enc
|= par_enc
[0] >> 4;
722 memcpy(buf
+i
+4, receivedAnswer
, 4);
723 memcpy(buf
+i
+8, &nt_par_enc
, 1);
727 // wait for the card to become ready again
728 while(GetCountSspClk() < timeout
);
734 crypto1_destroy(pcs
);
737 cmd_send(CMD_ACK
, isOK
, cuid
, num_nonces
, buf
, sizeof(buf
));
740 if (MF_DBGLEVEL
>= 3) DbpString("AcquireEncryptedNonces finished");
743 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
749 //-----------------------------------------------------------------------------
750 // MIFARE nested authentication.
752 //-----------------------------------------------------------------------------
753 void MifareNested(uint32_t arg0
, uint32_t arg1
, uint32_t calibrate
, uint8_t *datain
)
756 uint8_t blockNo
= arg0
& 0xff;
757 uint8_t keyType
= (arg0
>> 8) & 0xff;
758 uint8_t targetBlockNo
= arg1
& 0xff;
759 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
760 uint64_t ui64Key
= 0;
762 ui64Key
= bytes_to_num(datain
, 6);
765 uint16_t rtr
, i
, j
, len
;
767 static uint16_t dmin
, dmax
;
769 uint32_t cuid
, nt1
, nt2
, nttmp
, nttest
, ks1
;
771 uint32_t target_nt
[2], target_ks
[2];
773 uint8_t par_array
[4];
775 struct Crypto1State mpcs
= {0, 0};
776 struct Crypto1State
*pcs
;
778 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
780 uint32_t auth1_time
, auth2_time
;
781 static uint16_t delta_time
;
785 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
787 // free eventually allocated BigBuf memory
790 if (calibrate
) clear_trace();
793 // statistics on nonce distance
795 #define NESTED_MAX_TRIES 12
796 uint16_t unsuccessfull_tries
= 0;
797 if (calibrate
) { // for first call only. Otherwise reuse previous calibration
805 for (rtr
= 0; rtr
< 17; rtr
++) {
807 // Test if the action was cancelled
813 // prepare next select. No need to power down the card.
814 if(mifare_classic_halt(pcs
, cuid
)) {
815 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
820 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
821 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
827 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
828 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
834 auth2_time
= auth1_time
+ delta_time
;
838 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_NESTED
, &nt2
, &auth2_time
)) {
839 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error");
844 nttmp
= prng_successor(nt1
, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160
845 for (i
= 101; i
< 1200; i
++) {
846 nttmp
= prng_successor(nttmp
, 1);
847 if (nttmp
== nt2
) break;
857 delta_time
= auth2_time
- auth1_time
+ 32; // allow some slack for proper timing
859 if (MF_DBGLEVEL
>= 3) Dbprintf("Nested: calibrating... ntdist=%d", i
);
861 unsuccessfull_tries
++;
862 if (unsuccessfull_tries
> NESTED_MAX_TRIES
) { // card isn't vulnerable to nested attack (random numbers are not predictable)
868 davg
= (davg
+ (rtr
- 1)/2) / (rtr
- 1);
870 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
);
878 // -------------------------------------------------------------------------------------------------
882 // get crypted nonces for target sector
883 for(i
=0; i
< 2 && !isOK
; i
++) { // look for exactly two different nonces
886 while(target_nt
[i
] == 0) { // continue until we have an unambiguous nonce
888 // prepare next select. No need to power down the card.
889 if(mifare_classic_halt(pcs
, cuid
)) {
890 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
894 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
895 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
900 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
901 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
905 // nested authentication
906 auth2_time
= auth1_time
+ delta_time
;
907 len
= mifare_sendcmd_short(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType
& 0x01), targetBlockNo
, receivedAnswer
, par
, &auth2_time
);
909 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error len=%d", len
);
913 nt2
= bytes_to_num(receivedAnswer
, 4);
914 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i
+1, nt1
, nt2
, par
[0]);
916 // Parity validity check
917 for (j
= 0; j
< 4; j
++) {
918 par_array
[j
] = (oddparity8(receivedAnswer
[j
]) != ((par
[0] >> (7-j
)) & 0x01));
922 nttest
= prng_successor(nt1
, dmin
- 1);
923 for (j
= dmin
; j
< dmax
+ 1; j
++) {
924 nttest
= prng_successor(nttest
, 1);
927 if (valid_nonce(nttest
, nt2
, ks1
, par_array
)){
928 if (ncount
> 0) { // we are only interested in disambiguous nonces, try again
929 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i
+1, j
);
933 target_nt
[i
] = nttest
;
936 if (i
== 1 && target_nt
[1] == target_nt
[0]) { // we need two different nonces
938 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j
);
941 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i
+1, j
);
944 if (target_nt
[i
] == 0 && j
== dmax
+1 && MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i
+1);
950 // ----------------------------- crypto1 destroy
951 crypto1_destroy(pcs
);
953 byte_t buf
[4 + 4 * 4];
954 memcpy(buf
, &cuid
, 4);
955 memcpy(buf
+4, &target_nt
[0], 4);
956 memcpy(buf
+8, &target_ks
[0], 4);
957 memcpy(buf
+12, &target_nt
[1], 4);
958 memcpy(buf
+16, &target_ks
[1], 4);
961 cmd_send(CMD_ACK
, isOK
, 0, targetBlockNo
+ (targetKeyType
* 0x100), buf
, sizeof(buf
));
964 if (MF_DBGLEVEL
>= 3) DbpString("NESTED FINISHED");
966 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
970 //-----------------------------------------------------------------------------
971 // MIFARE check keys. key count up to 85.
973 //-----------------------------------------------------------------------------
974 void MifareChkKeys(uint16_t arg0
, uint16_t arg1
, uint8_t arg2
, uint8_t *datain
)
976 uint8_t blockNo
= arg0
& 0xff;
977 uint8_t keyType
= (arg0
>> 8) & 0xff;
978 bool clearTrace
= arg1
& 0x01;
979 bool multisectorCheck
= arg1
& 0x02;
980 uint8_t set14aTimeout
= (arg1
>> 8) & 0xff;
981 uint8_t keyCount
= arg2
;
984 int OLD_MF_DBGLEVEL
= MF_DBGLEVEL
;
985 MF_DBGLEVEL
= MF_DBG_NONE
;
990 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
992 if (clearTrace
) clear_trace();
996 iso14a_set_timeout(set14aTimeout
* 10); // timeout: ms = x/106 35-minimum, 50-OK 106-recommended 500-safe
999 if (multisectorCheck
) {
1000 TKeyIndex keyIndex
= {{0}};
1001 uint8_t sectorCnt
= blockNo
;
1002 int res
= MifareMultisectorChk(datain
, keyCount
, sectorCnt
, keyType
, OLD_MF_DBGLEVEL
, &keyIndex
);
1006 cmd_send(CMD_ACK
, 1, 0, 0, keyIndex
, 80);
1008 cmd_send(CMD_ACK
, 0, 0, 0, NULL
, 0);
1012 int res
= MifareChkBlockKeys(datain
, keyCount
, blockNo
, keyType
, OLD_MF_DBGLEVEL
);
1016 cmd_send(CMD_ACK
, 1, 0, 0, datain
+ (res
- 1) * 6, 6);
1018 cmd_send(CMD_ACK
, 0, 0, 0, NULL
, 0);
1023 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
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 emlSetMem(datain
, arg0
, arg1
); // data, block num, blocks count
1057 void MifareEMemGet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1058 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1059 byte_t buf
[USB_CMD_DATA_SIZE
];
1060 emlGetMem(buf
, arg0
, arg1
); // data, block num, blocks count (max 4)
1063 cmd_send(CMD_ACK
,arg0
,arg1
,0,buf
,USB_CMD_DATA_SIZE
);
1067 //-----------------------------------------------------------------------------
1068 // Load a card into the emulator memory
1070 //-----------------------------------------------------------------------------
1071 void MifareECardLoad(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1072 uint8_t numSectors
= arg0
;
1073 uint8_t keyType
= arg1
;
1074 uint64_t ui64Key
= 0;
1076 struct Crypto1State mpcs
= {0, 0};
1077 struct Crypto1State
*pcs
;
1081 byte_t dataoutbuf
[16];
1082 byte_t dataoutbuf2
[16];
1088 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1095 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
1097 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1100 for (uint8_t sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
1101 ui64Key
= emlGetKey(sectorNo
, keyType
);
1103 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
1105 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo
);
1109 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_NESTED
)) {
1111 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo
);
1116 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
1117 if(isOK
&& mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
)) {
1119 if (MF_DBGLEVEL
>= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo
, blockNo
);
1123 if (blockNo
< NumBlocksPerSector(sectorNo
) - 1) {
1124 emlSetMem(dataoutbuf
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
1125 } else { // sector trailer, keep the keys, set only the AC
1126 emlGetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
1127 memcpy(&dataoutbuf2
[6], &dataoutbuf
[6], 4);
1128 emlSetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
1135 if(mifare_classic_halt(pcs
, cuid
)) {
1136 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1139 // ----------------------------- crypto1 destroy
1140 crypto1_destroy(pcs
);
1142 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1145 if (MF_DBGLEVEL
>= 2) DbpString("EMUL FILL SECTORS FINISHED");
1150 //-----------------------------------------------------------------------------
1151 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
1153 //-----------------------------------------------------------------------------
1155 static bool isBlockTrailer(int blockN
) {
1156 if (blockN
>= 0 && blockN
< 128) {
1157 return ((blockN
& 0x03) == 0x03);
1159 if (blockN
>= 128 && blockN
<= 256) {
1160 return ((blockN
& 0x0F) == 0x0F);
1165 void MifareCWipe(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1168 uint32_t numBlocks
= arg0
;
1170 // bit 0 - wipe gen1a
1171 // bit 1 - fill card with default data
1172 // bit 2 - gen1a = 0, gen1b = 1
1173 uint8_t cmdParams
= arg1
;
1174 bool needWipe
= cmdParams
& 0x01;
1175 bool needFill
= cmdParams
& 0x02;
1176 bool gen1b
= cmdParams
& 0x04;
1178 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1179 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1181 uint8_t block0
[16] = {0x01, 0x02, 0x03, 0x04, 0x04, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBE, 0xAF};
1182 uint8_t block1
[16] = {0x00};
1183 uint8_t blockK
[16] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x08, 0x77, 0x8F, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1184 uint8_t d_block
[18] = {0x00};
1187 uint8_t wupC1
[] = { 0x40 };
1188 uint8_t wupC2
[] = { 0x43 };
1189 uint8_t wipeC
[] = { 0x41 };
1195 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1204 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1205 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1206 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1210 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
1211 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1212 if (MF_DBGLEVEL
>= 1) Dbprintf("wipeC error");
1216 if(mifare_classic_halt(NULL
, 0)) {
1217 if (MF_DBGLEVEL
> 2) Dbprintf("Halt error");
1224 ReaderTransmitBitsPar(wupC1
, 7, 0, NULL
);
1226 // gen1b magic tag : do no issue wupC2 and don't expect 0x0a response after SELECT_UID (after getting UID from chip in 'hf mf csetuid' command)
1229 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1230 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1234 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1235 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1236 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1241 // send blocks command
1242 for (int blockNo
= 0; blockNo
< numBlocks
; blockNo
++) {
1243 if ((mifare_sendcmd_short(NULL
, 0, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1244 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send command error");
1248 // check type of block and add crc
1249 if (!isBlockTrailer(blockNo
)){
1250 memcpy(d_block
, block1
, 16);
1252 memcpy(d_block
, blockK
, 16);
1255 memcpy(d_block
, block0
, 16);
1257 AppendCrc14443a(d_block
, 16);
1259 // send write command
1260 ReaderTransmit(d_block
, sizeof(d_block
), NULL
);
1261 if ((ReaderReceive(receivedAnswer
, receivedAnswerPar
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1262 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send data error");
1268 // do no issue halt command for gen1b
1270 if (mifare_classic_halt(NULL
, 0)) {
1271 if (MF_DBGLEVEL
> 2) Dbprintf("Halt error");
1279 // send USB response
1281 cmd_send(CMD_ACK
,isOK
,0,0,NULL
,0);
1285 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1291 void MifareCSetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1294 uint8_t needWipe
= arg0
;
1295 // bit 0 - need get UID
1296 // bit 1 - need wupC
1297 // bit 2 - need HALT after sequence
1298 // bit 3 - need init FPGA and field before sequence
1299 // bit 4 - need reset FPGA and LED
1300 // bit 6 - gen1b backdoor type
1301 uint8_t workFlags
= arg1
;
1302 uint8_t blockNo
= arg2
;
1305 uint8_t wupC1
[] = { 0x40 };
1306 uint8_t wupC2
[] = { 0x43 };
1307 uint8_t wipeC
[] = { 0x41 };
1311 uint8_t uid
[10] = {0x00};
1312 uint8_t d_block
[18] = {0x00};
1315 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1316 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1318 // reset FPGA and LED
1319 if (workFlags
& 0x08) {
1323 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1331 // get UID from chip
1332 if (workFlags
& 0x01) {
1333 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
1334 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1335 // 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.
1339 if(mifare_classic_halt(NULL
, cuid
)) {
1340 if (MF_DBGLEVEL
> 2) Dbprintf("Halt error");
1341 // Continue, some magic tags misbehavies and send an answer to it.
1347 // Wipe command don't work with gen1b
1348 if (needWipe
&& !(workFlags
& 0x40)){
1349 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1350 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1351 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1355 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
1356 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1357 if (MF_DBGLEVEL
>= 1) Dbprintf("wipeC error");
1361 if(mifare_classic_halt(NULL
, 0)) {
1362 if (MF_DBGLEVEL
> 2) Dbprintf("Halt error");
1363 // Continue, some magic tags misbehavies and send an answer to it.
1369 if (workFlags
& 0x02) {
1370 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1372 // gen1b magic tag : do no issue wupC2 and don't expect 0x0a response after SELECT_UID (after getting UID from chip in 'hf mf csetuid' command)
1373 if (!(workFlags
& 0x40)) {
1375 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1376 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1380 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1381 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1382 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1388 if ((mifare_sendcmd_short(NULL
, 0, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1389 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send command error");
1393 memcpy(d_block
, datain
, 16);
1394 AppendCrc14443a(d_block
, 16);
1396 ReaderTransmit(d_block
, sizeof(d_block
), NULL
);
1397 if ((ReaderReceive(receivedAnswer
, receivedAnswerPar
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1398 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send data error");
1402 if (workFlags
& 0x04) {
1403 // do no issue halt command for gen1b magic tag (#db# halt error. response len: 1)
1404 if (!(workFlags
& 0x40)) {
1405 if (mifare_classic_halt(NULL
, 0)) {
1406 if (MF_DBGLEVEL
> 2) Dbprintf("Halt error");
1407 // Continue, some magic tags misbehavies and send an answer to it.
1418 cmd_send(CMD_ACK
,isOK
,0,0,uid
,4);
1421 if ((workFlags
& 0x10) || (!isOK
)) {
1422 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1428 void MifareCGetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1431 // bit 1 - need wupC
1432 // bit 2 - need HALT after sequence
1433 // bit 3 - need init FPGA and field before sequence
1434 // bit 4 - need reset FPGA and LED
1435 // bit 5 - need to set datain instead of issuing USB reply (called via ARM for StandAloneMode14a)
1436 // bit 6 - gen1b backdoor type
1437 uint8_t workFlags
= arg0
;
1438 uint8_t blockNo
= arg2
;
1441 uint8_t wupC1
[] = { 0x40 };
1442 uint8_t wupC2
[] = { 0x43 };
1446 uint8_t data
[18] = {0x00};
1449 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1450 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1452 if (workFlags
& 0x08) {
1456 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1463 if (workFlags
& 0x02) {
1464 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1465 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1466 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1469 // do no issue for gen1b magic tag
1470 if (!(workFlags
& 0x40)) {
1471 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1472 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1473 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1480 if ((mifare_sendcmd_short(NULL
, 0, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 18)) {
1481 if (MF_DBGLEVEL
>= 1) Dbprintf("read block send command error");
1484 memcpy(data
, receivedAnswer
, 18);
1486 if (workFlags
& 0x04) {
1487 // do no issue halt command for gen1b magic tag (#db# halt error. response len: 1)
1488 if (!(workFlags
& 0x40)) {
1489 if (mifare_classic_halt(NULL
, cuid
)) {
1490 if (MF_DBGLEVEL
> 1) Dbprintf("Halt error");
1491 // Continue, some magic tags misbehavies and send an answer to it.
1502 if (workFlags
& 0x20) {
1504 memcpy(datain
, data
, 18);
1507 cmd_send(CMD_ACK
,isOK
,0,0,data
,18);
1510 if ((workFlags
& 0x10) || (!isOK
)) {
1511 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1516 void MifareCIdent(){
1519 uint8_t wupC1
[] = { 0x40 };
1520 uint8_t wupC2
[] = { 0x43 };
1525 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1526 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1531 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1536 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1537 if(ReaderReceive(receivedAnswer
, receivedAnswerPar
) && (receivedAnswer
[0] == 0x0a)) {
1540 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1541 if(ReaderReceive(receivedAnswer
, receivedAnswerPar
) && (receivedAnswer
[0] == 0x0a)) {
1546 // From iceman1001: removed the if, since some magic tags misbehavies and send an answer to it.
1547 mifare_classic_halt(NULL
, 0);
1550 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
1553 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1561 void Mifare_DES_Auth1(uint8_t arg0
, uint8_t *datain
){
1563 byte_t dataout
[11] = {0x00};
1564 uint8_t uid
[10] = {0x00};
1567 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1570 int len
= iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true);
1572 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
1577 if(mifare_desfire_des_auth1(cuid
, dataout
)){
1578 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication part1: Fail.");
1583 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 1 FINISHED");
1584 cmd_send(CMD_ACK
,1,cuid
,0,dataout
, sizeof(dataout
));
1587 void Mifare_DES_Auth2(uint32_t arg0
, uint8_t *datain
){
1589 uint32_t cuid
= arg0
;
1590 uint8_t key
[16] = {0x00};
1592 byte_t dataout
[12] = {0x00};
1594 memcpy(key
, datain
, 16);
1596 isOK
= mifare_desfire_des_auth2(cuid
, key
, dataout
);
1599 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) Dbprintf("Authentication part2: Failed");
1604 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 2 FINISHED");
1606 cmd_send(CMD_ACK
, isOK
, 0, 0, dataout
, sizeof(dataout
));
1607 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);