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
;
986 int OLD_MF_DBGLEVEL
= MF_DBGLEVEL
;
987 MF_DBGLEVEL
= MF_DBG_NONE
;
989 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
997 iso14a_set_timeout(set14aTimeout
* 10); // timeout: ms = x/106 35-minimum, 50-OK 106-recommended 500-safe
1000 if (multisectorCheck
) {
1001 TKeyIndex keyIndex
= {{0}};
1002 uint8_t sectorCnt
= blockNo
;
1003 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);
1011 int res
= MifareChkBlockKeys(datain
, keyCount
, blockNo
, keyType
, OLD_MF_DBGLEVEL
);
1014 cmd_send(CMD_ACK
, 1, 0, 0, datain
+ (res
- 1) * 6, 6);
1016 cmd_send(CMD_ACK
, 0, 0, 0, NULL
, 0);
1020 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1023 // restore debug level
1024 MF_DBGLEVEL
= OLD_MF_DBGLEVEL
;
1029 //-----------------------------------------------------------------------------
1030 // MIFARE commands set debug level
1032 //-----------------------------------------------------------------------------
1033 void MifareSetDbgLvl(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1035 Dbprintf("Debug level: %d", MF_DBGLEVEL
);
1038 //-----------------------------------------------------------------------------
1039 // Work with emulator memory
1041 // Note: we call FpgaDownloadAndGo(FPGA_BITSTREAM_HF) here although FPGA is not
1042 // involved in dealing with emulator memory. But if it is called later, it might
1043 // destroy the Emulator Memory.
1044 //-----------------------------------------------------------------------------
1046 void MifareEMemClr(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1047 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1051 void MifareEMemSet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1052 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1053 emlSetMem(datain
, arg0
, arg1
); // data, block num, blocks count
1056 void MifareEMemGet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1057 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1058 byte_t buf
[USB_CMD_DATA_SIZE
];
1059 emlGetMem(buf
, arg0
, arg1
); // data, block num, blocks count (max 4)
1062 cmd_send(CMD_ACK
,arg0
,arg1
,0,buf
,USB_CMD_DATA_SIZE
);
1066 //-----------------------------------------------------------------------------
1067 // Load a card into the emulator memory
1069 //-----------------------------------------------------------------------------
1070 void MifareECardLoad(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1071 uint8_t numSectors
= arg0
;
1072 uint8_t keyType
= arg1
;
1073 uint64_t ui64Key
= 0;
1075 struct Crypto1State mpcs
= {0, 0};
1076 struct Crypto1State
*pcs
;
1080 byte_t dataoutbuf
[16];
1081 byte_t dataoutbuf2
[16];
1087 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1094 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
1096 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1099 for (uint8_t sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
1100 ui64Key
= emlGetKey(sectorNo
, keyType
);
1102 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
1104 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo
);
1108 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_NESTED
)) {
1110 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo
);
1115 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
1116 if(isOK
&& mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
)) {
1118 if (MF_DBGLEVEL
>= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo
, blockNo
);
1122 if (blockNo
< NumBlocksPerSector(sectorNo
) - 1) {
1123 emlSetMem(dataoutbuf
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
1124 } else { // sector trailer, keep the keys, set only the AC
1125 emlGetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
1126 memcpy(&dataoutbuf2
[6], &dataoutbuf
[6], 4);
1127 emlSetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
1134 if(mifare_classic_halt(pcs
, cuid
)) {
1135 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1138 // ----------------------------- crypto1 destroy
1139 crypto1_destroy(pcs
);
1141 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1144 if (MF_DBGLEVEL
>= 2) DbpString("EMUL FILL SECTORS FINISHED");
1149 //-----------------------------------------------------------------------------
1150 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
1152 //-----------------------------------------------------------------------------
1154 static bool isBlockTrailer(int blockN
) {
1155 if (blockN
>= 0 && blockN
< 128) {
1156 return ((blockN
& 0x03) == 0x03);
1158 if (blockN
>= 128 && blockN
<= 256) {
1159 return ((blockN
& 0x0F) == 0x0F);
1164 void MifareCWipe(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1167 uint32_t numBlocks
= arg0
;
1169 // bit 0 - wipe gen1a
1170 // bit 1 - fill card with default data
1171 // bit 2 - gen1a = 0, gen1b = 1
1172 uint8_t cmdParams
= arg1
;
1173 bool needWipe
= cmdParams
& 0x01;
1174 bool needFill
= cmdParams
& 0x02;
1175 bool gen1b
= cmdParams
& 0x04;
1177 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1178 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1180 uint8_t block0
[16] = {0x01, 0x02, 0x03, 0x04, 0x04, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBE, 0xAF};
1181 uint8_t block1
[16] = {0x00};
1182 uint8_t blockK
[16] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x08, 0x77, 0x8F, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1183 uint8_t d_block
[18] = {0x00};
1186 uint8_t wupC1
[] = { 0x40 };
1187 uint8_t wupC2
[] = { 0x43 };
1188 uint8_t wipeC
[] = { 0x41 };
1194 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1203 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1204 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1205 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1209 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
1210 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1211 if (MF_DBGLEVEL
>= 1) Dbprintf("wipeC error");
1215 if(mifare_classic_halt(NULL
, 0)) {
1216 if (MF_DBGLEVEL
> 2) Dbprintf("Halt error");
1223 ReaderTransmitBitsPar(wupC1
, 7, 0, NULL
);
1225 // 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)
1228 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1229 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1233 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1234 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1235 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1240 // send blocks command
1241 for (int blockNo
= 0; blockNo
< numBlocks
; blockNo
++) {
1242 if ((mifare_sendcmd_short(NULL
, 0, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1243 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send command error");
1247 // check type of block and add crc
1248 if (!isBlockTrailer(blockNo
)){
1249 memcpy(d_block
, block1
, 16);
1251 memcpy(d_block
, blockK
, 16);
1254 memcpy(d_block
, block0
, 16);
1256 AppendCrc14443a(d_block
, 16);
1258 // send write command
1259 ReaderTransmit(d_block
, sizeof(d_block
), NULL
);
1260 if ((ReaderReceive(receivedAnswer
, receivedAnswerPar
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1261 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send data error");
1267 // do no issue halt command for gen1b
1269 if (mifare_classic_halt(NULL
, 0)) {
1270 if (MF_DBGLEVEL
> 2) Dbprintf("Halt error");
1278 // send USB response
1280 cmd_send(CMD_ACK
,isOK
,0,0,NULL
,0);
1284 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1290 void MifareCSetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1293 uint8_t needWipe
= arg0
;
1294 // bit 0 - need get UID
1295 // bit 1 - need wupC
1296 // bit 2 - need HALT after sequence
1297 // bit 3 - need init FPGA and field before sequence
1298 // bit 4 - need reset FPGA and LED
1299 // bit 6 - gen1b backdoor type
1300 uint8_t workFlags
= arg1
;
1301 uint8_t blockNo
= arg2
;
1304 uint8_t wupC1
[] = { 0x40 };
1305 uint8_t wupC2
[] = { 0x43 };
1306 uint8_t wipeC
[] = { 0x41 };
1310 uint8_t uid
[10] = {0x00};
1311 uint8_t d_block
[18] = {0x00};
1314 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1315 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1317 // reset FPGA and LED
1318 if (workFlags
& 0x08) {
1322 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1330 // get UID from chip
1331 if (workFlags
& 0x01) {
1332 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
1333 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1334 // 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.
1338 if(mifare_classic_halt(NULL
, cuid
)) {
1339 if (MF_DBGLEVEL
> 2) Dbprintf("Halt error");
1340 // Continue, some magic tags misbehavies and send an answer to it.
1346 // Wipe command don't work with gen1b
1347 if (needWipe
&& !(workFlags
& 0x40)){
1348 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1349 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1350 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1354 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
1355 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1356 if (MF_DBGLEVEL
>= 1) Dbprintf("wipeC error");
1360 if(mifare_classic_halt(NULL
, 0)) {
1361 if (MF_DBGLEVEL
> 2) Dbprintf("Halt error");
1362 // Continue, some magic tags misbehavies and send an answer to it.
1368 if (workFlags
& 0x02) {
1369 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1371 // 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)
1372 if (!(workFlags
& 0x40)) {
1374 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1375 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1379 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1380 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1381 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1387 if ((mifare_sendcmd_short(NULL
, 0, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1388 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send command error");
1392 memcpy(d_block
, datain
, 16);
1393 AppendCrc14443a(d_block
, 16);
1395 ReaderTransmit(d_block
, sizeof(d_block
), NULL
);
1396 if ((ReaderReceive(receivedAnswer
, receivedAnswerPar
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1397 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send data error");
1401 if (workFlags
& 0x04) {
1402 // do no issue halt command for gen1b magic tag (#db# halt error. response len: 1)
1403 if (!(workFlags
& 0x40)) {
1404 if (mifare_classic_halt(NULL
, 0)) {
1405 if (MF_DBGLEVEL
> 2) Dbprintf("Halt error");
1406 // Continue, some magic tags misbehavies and send an answer to it.
1417 cmd_send(CMD_ACK
,isOK
,0,0,uid
,4);
1420 if ((workFlags
& 0x10) || (!isOK
)) {
1421 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1427 void MifareCGetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1430 // bit 1 - need wupC
1431 // bit 2 - need HALT after sequence
1432 // bit 3 - need init FPGA and field before sequence
1433 // bit 4 - need reset FPGA and LED
1434 // bit 5 - need to set datain instead of issuing USB reply (called via ARM for StandAloneMode14a)
1435 // bit 6 - gen1b backdoor type
1436 uint8_t workFlags
= arg0
;
1437 uint8_t blockNo
= arg2
;
1440 uint8_t wupC1
[] = { 0x40 };
1441 uint8_t wupC2
[] = { 0x43 };
1445 uint8_t data
[18] = {0x00};
1448 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1449 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1451 if (workFlags
& 0x08) {
1455 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1462 if (workFlags
& 0x02) {
1463 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1464 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1465 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1468 // do no issue for gen1b magic tag
1469 if (!(workFlags
& 0x40)) {
1470 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1471 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1472 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1479 if ((mifare_sendcmd_short(NULL
, 0, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 18)) {
1480 if (MF_DBGLEVEL
>= 1) Dbprintf("read block send command error");
1483 memcpy(data
, receivedAnswer
, 18);
1485 if (workFlags
& 0x04) {
1486 // do no issue halt command for gen1b magic tag (#db# halt error. response len: 1)
1487 if (!(workFlags
& 0x40)) {
1488 if (mifare_classic_halt(NULL
, cuid
)) {
1489 if (MF_DBGLEVEL
> 1) Dbprintf("Halt error");
1490 // Continue, some magic tags misbehavies and send an answer to it.
1501 if (workFlags
& 0x20) {
1503 memcpy(datain
, data
, 18);
1506 cmd_send(CMD_ACK
,isOK
,0,0,data
,18);
1509 if ((workFlags
& 0x10) || (!isOK
)) {
1510 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1515 void MifareCIdent(){
1518 uint8_t wupC1
[] = { 0x40 };
1519 uint8_t wupC2
[] = { 0x43 };
1524 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1525 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1530 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1535 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1536 if(ReaderReceive(receivedAnswer
, receivedAnswerPar
) && (receivedAnswer
[0] == 0x0a)) {
1539 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1540 if(ReaderReceive(receivedAnswer
, receivedAnswerPar
) && (receivedAnswer
[0] == 0x0a)) {
1545 // From iceman1001: removed the if, since some magic tags misbehavies and send an answer to it.
1546 mifare_classic_halt(NULL
, 0);
1549 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
1552 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1560 void Mifare_DES_Auth1(uint8_t arg0
, uint8_t *datain
){
1562 byte_t dataout
[11] = {0x00};
1563 uint8_t uid
[10] = {0x00};
1566 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1569 int len
= iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true);
1571 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
1576 if(mifare_desfire_des_auth1(cuid
, dataout
)){
1577 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication part1: Fail.");
1582 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 1 FINISHED");
1583 cmd_send(CMD_ACK
,1,cuid
,0,dataout
, sizeof(dataout
));
1586 void Mifare_DES_Auth2(uint32_t arg0
, uint8_t *datain
){
1588 uint32_t cuid
= arg0
;
1589 uint8_t key
[16] = {0x00};
1591 byte_t dataout
[12] = {0x00};
1593 memcpy(key
, datain
, 16);
1595 isOK
= mifare_desfire_des_auth2(cuid
, key
, dataout
);
1598 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) Dbprintf("Authentication part2: Failed");
1603 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 2 FINISHED");
1605 cmd_send(CMD_ACK
, isOK
, 0, 0, dataout
, sizeof(dataout
));
1606 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);