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"
23 #define AUTHENTICATION_TIMEOUT 848 // card times out 1ms after wrong authentication (according to NXP documentation)
24 #define 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 //-----------------------------------------------------------------------------
33 // Select, Authenticate, Read a MIFARE tag.
35 //-----------------------------------------------------------------------------
36 void MifareReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
39 uint8_t blockNo
= arg0
;
40 uint8_t keyType
= arg1
;
42 ui64Key
= bytes_to_num(datain
, 6);
46 byte_t dataoutbuf
[16];
49 struct Crypto1State mpcs
= {0, 0};
50 struct Crypto1State
*pcs
;
53 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
62 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
63 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
67 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
68 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
72 if(mifare_classic_readblock(pcs
, cuid
, blockNo
, dataoutbuf
)) {
73 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block error");
77 if(mifare_classic_halt(pcs
, cuid
)) {
78 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
86 // ----------------------------- crypto1 destroy
89 if (MF_DBGLEVEL
>= 2) DbpString("READ BLOCK FINISHED");
92 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16);
95 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
99 void MifareUC_Auth(uint8_t arg0
, uint8_t *keybytes
){
101 bool turnOffField
= (arg0
== 1);
103 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
105 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
109 if(!iso14443a_select_card(NULL
, NULL
, NULL
, true, 0, true)) {
110 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
115 if(!mifare_ultra_auth(keybytes
)){
116 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication failed");
122 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
125 cmd_send(CMD_ACK
,1,0,0,0,0);
129 // Arg1 = UsePwd bool
130 // datain = PWD bytes,
131 void MifareUReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
)
133 uint8_t blockNo
= arg0
;
134 byte_t dataout
[16] = {0x00};
135 bool useKey
= (arg1
== 1); //UL_C
136 bool usePwd
= (arg1
== 2); //UL_EV1/NTAG
140 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
144 int len
= iso14443a_select_card(NULL
, NULL
, NULL
, true, 0, true);
146 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%02X)",len
);
151 // UL-C authentication
153 uint8_t key
[16] = {0x00};
154 memcpy(key
, datain
, sizeof(key
) );
156 if ( !mifare_ultra_auth(key
) ) {
162 // UL-EV1 / NTAG authentication
164 uint8_t pwd
[4] = {0x00};
165 memcpy(pwd
, datain
, 4);
166 uint8_t pack
[4] = {0,0,0,0};
167 if (!mifare_ul_ev1_auth(pwd
, pack
)) {
173 if( mifare_ultra_readblock(blockNo
, dataout
) ) {
174 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Read block error");
179 if( mifare_ultra_halt() ) {
180 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Halt error");
185 cmd_send(CMD_ACK
,1,0,0,dataout
,16);
186 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
190 //-----------------------------------------------------------------------------
191 // Select, Authenticate, Read a MIFARE tag.
192 // read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)
193 //-----------------------------------------------------------------------------
194 void MifareReadSector(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
197 uint8_t sectorNo
= arg0
;
198 uint8_t keyType
= arg1
;
199 uint64_t ui64Key
= 0;
200 ui64Key
= bytes_to_num(datain
, 6);
204 byte_t dataoutbuf
[16 * 16];
207 struct Crypto1State mpcs
= {0, 0};
208 struct Crypto1State
*pcs
;
211 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
220 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
222 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
226 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
228 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
231 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
232 if(mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
+ 16 * blockNo
)) {
234 if (MF_DBGLEVEL
>= 1) Dbprintf("Read sector %2d block %2d error", sectorNo
, blockNo
);
239 if(mifare_classic_halt(pcs
, cuid
)) {
240 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
243 // ----------------------------- crypto1 destroy
244 crypto1_destroy(pcs
);
246 if (MF_DBGLEVEL
>= 2) DbpString("READ SECTOR FINISHED");
249 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16*NumBlocksPerSector(sectorNo
));
253 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
257 // arg0 = blockNo (start)
258 // arg1 = Pages (number of blocks)
260 // datain = KEY bytes
261 void MifareUReadCard(uint8_t arg0
, uint16_t arg1
, uint8_t arg2
, uint8_t *datain
)
265 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
267 // free eventually allocated BigBuf memory
272 uint8_t blockNo
= arg0
;
273 uint16_t blocks
= arg1
;
274 bool useKey
= (arg2
== 1); //UL_C
275 bool usePwd
= (arg2
== 2); //UL_EV1/NTAG
276 uint32_t countblocks
= 0;
277 uint8_t *dataout
= BigBuf_malloc(CARD_MEMORY_SIZE
);
278 if (dataout
== NULL
){
279 Dbprintf("out of memory");
284 int len
= iso14443a_select_card(NULL
, NULL
, NULL
, true, 0, true);
286 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%d)",len
);
291 // UL-C authentication
293 uint8_t key
[16] = {0x00};
294 memcpy(key
, datain
, sizeof(key
) );
296 if ( !mifare_ultra_auth(key
) ) {
302 // UL-EV1 / NTAG authentication
304 uint8_t pwd
[4] = {0x00};
305 memcpy(pwd
, datain
, sizeof(pwd
));
306 uint8_t pack
[4] = {0,0,0,0};
308 if (!mifare_ul_ev1_auth(pwd
, pack
)){
314 for (int i
= 0; i
< blocks
; i
++){
315 if ((i
*4) + 4 >= CARD_MEMORY_SIZE
) {
316 Dbprintf("Data exceeds buffer!!");
320 len
= mifare_ultra_readblock(blockNo
+ i
, dataout
+ 4 * i
);
323 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Read block %d error",i
);
324 // if no blocks read - error out
329 //stop at last successful read block and return what we got
337 len
= mifare_ultra_halt();
339 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Halt error");
344 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) Dbprintf("Blocks read %d", countblocks
);
348 cmd_send(CMD_ACK
, 1, countblocks
, BigBuf_max_traceLen(), 0, 0);
349 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
354 //-----------------------------------------------------------------------------
355 // Select, Authenticate, Write a MIFARE tag.
357 //-----------------------------------------------------------------------------
358 void MifareWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
361 uint8_t blockNo
= arg0
;
362 uint8_t keyType
= arg1
;
363 uint64_t ui64Key
= 0;
364 byte_t blockdata
[16];
366 ui64Key
= bytes_to_num(datain
, 6);
367 memcpy(blockdata
, datain
+ 10, 16);
373 struct Crypto1State mpcs
= {0, 0};
374 struct Crypto1State
*pcs
;
377 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
386 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
387 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
391 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
392 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
396 if(mifare_classic_writeblock(pcs
, cuid
, blockNo
, blockdata
)) {
397 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
401 if(mifare_classic_halt(pcs
, cuid
)) {
402 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
410 // ----------------------------- crypto1 destroy
411 crypto1_destroy(pcs
);
413 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
416 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
421 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
425 /* // Command not needed but left for future testing
426 void MifareUWriteBlockCompat(uint8_t arg0, uint8_t *datain)
428 uint8_t blockNo = arg0;
429 byte_t blockdata[16] = {0x00};
431 memcpy(blockdata, datain, 16);
433 uint8_t uid[10] = {0x00};
435 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
438 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
440 if(!iso14443a_select_card(uid, NULL, NULL, true, 0)) {
441 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
446 if(mifare_ultra_writeblock_compat(blockNo, blockdata)) {
447 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
451 if(mifare_ultra_halt()) {
452 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
457 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");
459 cmd_send(CMD_ACK,1,0,0,0,0);
460 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
465 // Arg0 : Block to write to.
466 // Arg1 : 0 = use no authentication.
467 // 1 = use 0x1A authentication.
468 // 2 = use 0x1B authentication.
469 // datain : 4 first bytes is data to be written.
470 // : 4/16 next bytes is authentication key.
471 void MifareUWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
)
473 uint8_t blockNo
= arg0
;
474 bool useKey
= (arg1
== 1); //UL_C
475 bool usePwd
= (arg1
== 2); //UL_EV1/NTAG
476 byte_t blockdata
[4] = {0x00};
478 memcpy(blockdata
, datain
,4);
482 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
486 if(!iso14443a_select_card(NULL
, NULL
, NULL
, true, 0, true)) {
487 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
492 // UL-C authentication
494 uint8_t key
[16] = {0x00};
495 memcpy(key
, datain
+4, sizeof(key
) );
497 if ( !mifare_ultra_auth(key
) ) {
503 // UL-EV1 / NTAG authentication
505 uint8_t pwd
[4] = {0x00};
506 memcpy(pwd
, datain
+4, 4);
507 uint8_t pack
[4] = {0,0,0,0};
508 if (!mifare_ul_ev1_auth(pwd
, pack
)) {
514 if(mifare_ultra_writeblock(blockNo
, blockdata
)) {
515 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
520 if(mifare_ultra_halt()) {
521 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
526 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
528 cmd_send(CMD_ACK
,1,0,0,0,0);
529 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
533 void MifareUSetPwd(uint8_t arg0
, uint8_t *datain
){
535 uint8_t pwd
[16] = {0x00};
536 byte_t blockdata
[4] = {0x00};
538 memcpy(pwd
, datain
, 16);
540 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
541 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
545 if(!iso14443a_select_card(NULL
, NULL
, NULL
, true, 0, true)) {
546 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
551 blockdata
[0] = pwd
[7];
552 blockdata
[1] = pwd
[6];
553 blockdata
[2] = pwd
[5];
554 blockdata
[3] = pwd
[4];
555 if(mifare_ultra_writeblock( 44, blockdata
)) {
556 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
561 blockdata
[0] = pwd
[3];
562 blockdata
[1] = pwd
[2];
563 blockdata
[2] = pwd
[1];
564 blockdata
[3] = pwd
[0];
565 if(mifare_ultra_writeblock( 45, blockdata
)) {
566 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
571 blockdata
[0] = pwd
[15];
572 blockdata
[1] = pwd
[14];
573 blockdata
[2] = pwd
[13];
574 blockdata
[3] = pwd
[12];
575 if(mifare_ultra_writeblock( 46, blockdata
)) {
576 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
581 blockdata
[0] = pwd
[11];
582 blockdata
[1] = pwd
[10];
583 blockdata
[2] = pwd
[9];
584 blockdata
[3] = pwd
[8];
585 if(mifare_ultra_writeblock( 47, blockdata
)) {
586 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
591 if(mifare_ultra_halt()) {
592 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
597 cmd_send(CMD_ACK
,1,0,0,0,0);
598 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
602 // Return 1 if the nonce is invalid else return 0
603 int valid_nonce(uint32_t Nt
, uint32_t NtEnc
, uint32_t Ks1
, uint8_t *parity
) {
604 return ((oddparity8((Nt
>> 24) & 0xFF) == ((parity
[0]) ^ oddparity8((NtEnc
>> 24) & 0xFF) ^ BIT(Ks1
,16))) & \
605 (oddparity8((Nt
>> 16) & 0xFF) == ((parity
[1]) ^ oddparity8((NtEnc
>> 16) & 0xFF) ^ BIT(Ks1
,8))) & \
606 (oddparity8((Nt
>> 8) & 0xFF) == ((parity
[2]) ^ oddparity8((NtEnc
>> 8) & 0xFF) ^ BIT(Ks1
,0)))) ? 1 : 0;
610 //-----------------------------------------------------------------------------
611 // acquire encrypted nonces in order to perform the attack described in
612 // Carlo Meijer, Roel Verdult, "Ciphertext-only Cryptanalysis on Hardened
613 // Mifare Classic Cards" in Proceedings of the 22nd ACM SIGSAC Conference on
614 // Computer and Communications Security, 2015
615 //-----------------------------------------------------------------------------
616 void MifareAcquireEncryptedNonces(uint32_t arg0
, uint32_t arg1
, uint32_t flags
, uint8_t *datain
)
618 uint64_t ui64Key
= 0;
621 uint8_t cascade_levels
= 0;
622 struct Crypto1State mpcs
= {0, 0};
623 struct Crypto1State
*pcs
;
625 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
628 uint8_t nt_par_enc
= 0;
629 uint8_t buf
[USB_CMD_DATA_SIZE
];
632 uint8_t blockNo
= arg0
& 0xff;
633 uint8_t keyType
= (arg0
>> 8) & 0xff;
634 uint8_t targetBlockNo
= arg1
& 0xff;
635 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
636 ui64Key
= bytes_to_num(datain
, 6);
637 bool initialize
= flags
& 0x0001;
638 bool slow
= flags
& 0x0002;
639 bool field_off
= flags
& 0x0004;
645 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
652 uint16_t num_nonces
= 0;
653 bool have_uid
= false;
654 for (uint16_t i
= 0; i
<= USB_CMD_DATA_SIZE
- 9; ) {
656 // Test if the action was cancelled
663 if (!have_uid
) { // need a full select cycle to get the uid first
664 iso14a_card_select_t card_info
;
665 if(!iso14443a_select_card(uid
, &card_info
, &cuid
, true, 0, true)) {
666 if (MF_DBGLEVEL
>= 1) Dbprintf("AcquireNonces: Can't select card (ALL)");
669 switch (card_info
.uidlen
) {
670 case 4 : cascade_levels
= 1; break;
671 case 7 : cascade_levels
= 2; break;
672 case 10: cascade_levels
= 3; break;
676 } else { // no need for anticollision. We can directly select the card
677 if(!iso14443a_select_card(uid
, NULL
, NULL
, false, cascade_levels
, true)) {
678 if (MF_DBGLEVEL
>= 1) Dbprintf("AcquireNonces: Can't select card (UID)");
684 timeout
= GetCountSspClk() + PRE_AUTHENTICATION_LEADTIME
;
685 while(GetCountSspClk() < timeout
);
689 if (mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, NULL
)) {
690 if (MF_DBGLEVEL
>= 1) Dbprintf("AcquireNonces: Auth1 error");
694 // nested authentication
695 uint16_t len
= mifare_sendcmd_short(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType
& 0x01), targetBlockNo
, receivedAnswer
, par_enc
, NULL
);
697 if (MF_DBGLEVEL
>= 1) Dbprintf("AcquireNonces: Auth2 error len=%d", len
);
701 // send a dummy byte as reader response in order to trigger the cards authentication timeout
702 uint8_t dummy_answer
= 0;
703 ReaderTransmit(&dummy_answer
, 1, NULL
);
704 timeout
= GetCountSspClk() + AUTHENTICATION_TIMEOUT
;
707 if (num_nonces
% 2) {
708 memcpy(buf
+i
, receivedAnswer
, 4);
709 nt_par_enc
= par_enc
[0] & 0xf0;
711 nt_par_enc
|= par_enc
[0] >> 4;
712 memcpy(buf
+i
+4, receivedAnswer
, 4);
713 memcpy(buf
+i
+8, &nt_par_enc
, 1);
717 // wait for the card to become ready again
718 while(GetCountSspClk() < timeout
);
724 crypto1_destroy(pcs
);
727 cmd_send(CMD_ACK
, isOK
, cuid
, num_nonces
, buf
, sizeof(buf
));
730 if (MF_DBGLEVEL
>= 3) DbpString("AcquireEncryptedNonces finished");
733 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
739 //-----------------------------------------------------------------------------
740 // MIFARE nested authentication.
742 //-----------------------------------------------------------------------------
743 void MifareNested(uint32_t arg0
, uint32_t arg1
, uint32_t calibrate
, uint8_t *datain
)
746 uint8_t blockNo
= arg0
& 0xff;
747 uint8_t keyType
= (arg0
>> 8) & 0xff;
748 uint8_t targetBlockNo
= arg1
& 0xff;
749 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
750 uint64_t ui64Key
= 0;
752 ui64Key
= bytes_to_num(datain
, 6);
755 uint16_t rtr
, i
, j
, len
;
757 static uint16_t dmin
, dmax
;
759 uint32_t cuid
, nt1
, nt2
, nttmp
, nttest
, ks1
;
761 uint32_t target_nt
[2], target_ks
[2];
763 uint8_t par_array
[4];
765 struct Crypto1State mpcs
= {0, 0};
766 struct Crypto1State
*pcs
;
768 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
770 uint32_t auth1_time
, auth2_time
;
771 static uint16_t delta_time
;
775 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
777 // free eventually allocated BigBuf memory
780 if (calibrate
) clear_trace();
783 // statistics on nonce distance
785 #define NESTED_MAX_TRIES 12
786 uint16_t unsuccessfull_tries
= 0;
787 if (calibrate
) { // for first call only. Otherwise reuse previous calibration
795 for (rtr
= 0; rtr
< 17; rtr
++) {
797 // Test if the action was cancelled
803 // prepare next select. No need to power down the card.
804 if(mifare_classic_halt(pcs
, cuid
)) {
805 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
810 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
811 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
817 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
818 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
824 auth2_time
= auth1_time
+ delta_time
;
828 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_NESTED
, &nt2
, &auth2_time
)) {
829 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error");
834 nttmp
= prng_successor(nt1
, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160
835 for (i
= 101; i
< 1200; i
++) {
836 nttmp
= prng_successor(nttmp
, 1);
837 if (nttmp
== nt2
) break;
847 delta_time
= auth2_time
- auth1_time
+ 32; // allow some slack for proper timing
849 if (MF_DBGLEVEL
>= 3) Dbprintf("Nested: calibrating... ntdist=%d", i
);
851 unsuccessfull_tries
++;
852 if (unsuccessfull_tries
> NESTED_MAX_TRIES
) { // card isn't vulnerable to nested attack (random numbers are not predictable)
858 davg
= (davg
+ (rtr
- 1)/2) / (rtr
- 1);
860 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
);
868 // -------------------------------------------------------------------------------------------------
872 // get crypted nonces for target sector
873 for(i
=0; i
< 2 && !isOK
; i
++) { // look for exactly two different nonces
876 while(target_nt
[i
] == 0) { // continue until we have an unambiguous nonce
878 // prepare next select. No need to power down the card.
879 if(mifare_classic_halt(pcs
, cuid
)) {
880 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
884 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
885 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
890 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
891 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
895 // nested authentication
896 auth2_time
= auth1_time
+ delta_time
;
897 len
= mifare_sendcmd_short(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType
& 0x01), targetBlockNo
, receivedAnswer
, par
, &auth2_time
);
899 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error len=%d", len
);
903 nt2
= bytes_to_num(receivedAnswer
, 4);
904 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i
+1, nt1
, nt2
, par
[0]);
906 // Parity validity check
907 for (j
= 0; j
< 4; j
++) {
908 par_array
[j
] = (oddparity8(receivedAnswer
[j
]) != ((par
[0] >> (7-j
)) & 0x01));
912 nttest
= prng_successor(nt1
, dmin
- 1);
913 for (j
= dmin
; j
< dmax
+ 1; j
++) {
914 nttest
= prng_successor(nttest
, 1);
917 if (valid_nonce(nttest
, nt2
, ks1
, par_array
)){
918 if (ncount
> 0) { // we are only interested in disambiguous nonces, try again
919 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i
+1, j
);
923 target_nt
[i
] = nttest
;
926 if (i
== 1 && target_nt
[1] == target_nt
[0]) { // we need two different nonces
928 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j
);
931 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i
+1, j
);
934 if (target_nt
[i
] == 0 && j
== dmax
+1 && MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i
+1);
940 // ----------------------------- crypto1 destroy
941 crypto1_destroy(pcs
);
943 byte_t buf
[4 + 4 * 4];
944 memcpy(buf
, &cuid
, 4);
945 memcpy(buf
+4, &target_nt
[0], 4);
946 memcpy(buf
+8, &target_ks
[0], 4);
947 memcpy(buf
+12, &target_nt
[1], 4);
948 memcpy(buf
+16, &target_ks
[1], 4);
951 cmd_send(CMD_ACK
, isOK
, 0, targetBlockNo
+ (targetKeyType
* 0x100), buf
, sizeof(buf
));
954 if (MF_DBGLEVEL
>= 3) DbpString("NESTED FINISHED");
956 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
960 //-----------------------------------------------------------------------------
961 // MIFARE check keys. key count up to 85.
963 //-----------------------------------------------------------------------------
964 void MifareChkKeys(uint16_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
966 uint8_t blockNo
= arg0
& 0xff;
967 uint8_t keyType
= (arg0
>> 8) & 0xff;
968 bool clearTrace
= arg1
;
969 uint8_t keyCount
= arg2
;
970 uint64_t ui64Key
= 0;
972 bool have_uid
= false;
973 uint8_t cascade_levels
= 0;
974 uint32_t timeout
= 0;
979 struct Crypto1State mpcs
= {0, 0};
980 struct Crypto1State
*pcs
;
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();
995 for (i
= 0; i
< keyCount
; i
++) {
996 // if(mifare_classic_halt(pcs, cuid)) {
997 // if (MF_DBGLEVEL >= 1) Dbprintf("ChkKeys: Halt error");
1000 // Iceman: use piwi's faster nonce collecting part in hardnested.
1001 if (!have_uid
) { // need a full select cycle to get the uid first
1002 iso14a_card_select_t card_info
;
1003 if(!iso14443a_select_card(uid
, &card_info
, &cuid
, true, 0, true)) {
1004 if (OLD_MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Can't select card");
1005 --i
; // try same key once again
1008 switch (card_info
.uidlen
) {
1009 case 4 : cascade_levels
= 1; break;
1010 case 7 : cascade_levels
= 2; break;
1011 case 10: cascade_levels
= 3; break;
1015 } else { // no need for anticollision. We can directly select the card
1016 if(!iso14443a_select_card(uid
, NULL
, NULL
, false, cascade_levels
, true)) {
1017 if (OLD_MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Can't select card (UID)");
1018 --i
; // try same key once again
1023 ui64Key
= bytes_to_num(datain
+ i
* 6, 6);
1024 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
1025 uint8_t dummy_answer
= 0;
1026 ReaderTransmit(&dummy_answer
, 1, NULL
);
1027 timeout
= GetCountSspClk() + AUTHENTICATION_TIMEOUT
;
1029 // wait for the card to become ready again
1030 while(GetCountSspClk() < timeout
);
1039 cmd_send(CMD_ACK
,isOK
,0,0,datain
+ i
* 6,6);
1042 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1045 // restore debug level
1046 MF_DBGLEVEL
= OLD_MF_DBGLEVEL
;
1049 //-----------------------------------------------------------------------------
1050 // MIFARE commands set debug level
1052 //-----------------------------------------------------------------------------
1053 void MifareSetDbgLvl(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1055 Dbprintf("Debug level: %d", MF_DBGLEVEL
);
1058 //-----------------------------------------------------------------------------
1059 // Work with emulator memory
1061 // Note: we call FpgaDownloadAndGo(FPGA_BITSTREAM_HF) here although FPGA is not
1062 // involved in dealing with emulator memory. But if it is called later, it might
1063 // destroy the Emulator Memory.
1064 //-----------------------------------------------------------------------------
1066 void MifareEMemClr(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1067 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1071 void MifareEMemSet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1072 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1073 emlSetMem(datain
, arg0
, arg1
); // data, block num, blocks count
1076 void MifareEMemGet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1077 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1078 byte_t buf
[USB_CMD_DATA_SIZE
];
1079 emlGetMem(buf
, arg0
, arg1
); // data, block num, blocks count (max 4)
1082 cmd_send(CMD_ACK
,arg0
,arg1
,0,buf
,USB_CMD_DATA_SIZE
);
1086 //-----------------------------------------------------------------------------
1087 // Load a card into the emulator memory
1089 //-----------------------------------------------------------------------------
1090 void MifareECardLoad(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1091 uint8_t numSectors
= arg0
;
1092 uint8_t keyType
= arg1
;
1093 uint64_t ui64Key
= 0;
1095 struct Crypto1State mpcs
= {0, 0};
1096 struct Crypto1State
*pcs
;
1100 byte_t dataoutbuf
[16];
1101 byte_t dataoutbuf2
[16];
1107 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1114 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
1116 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1119 for (uint8_t sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
1120 ui64Key
= emlGetKey(sectorNo
, keyType
);
1122 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
1124 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo
);
1128 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_NESTED
)) {
1130 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo
);
1135 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
1136 if(isOK
&& mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
)) {
1138 if (MF_DBGLEVEL
>= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo
, blockNo
);
1142 if (blockNo
< NumBlocksPerSector(sectorNo
) - 1) {
1143 emlSetMem(dataoutbuf
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
1144 } else { // sector trailer, keep the keys, set only the AC
1145 emlGetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
1146 memcpy(&dataoutbuf2
[6], &dataoutbuf
[6], 4);
1147 emlSetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
1154 if(mifare_classic_halt(pcs
, cuid
)) {
1155 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1158 // ----------------------------- crypto1 destroy
1159 crypto1_destroy(pcs
);
1161 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1164 if (MF_DBGLEVEL
>= 2) DbpString("EMUL FILL SECTORS FINISHED");
1169 //-----------------------------------------------------------------------------
1170 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
1172 //-----------------------------------------------------------------------------
1174 static bool isBlockTrailer(int blockN
) {
1175 if (blockN
>= 0 && blockN
< 128) {
1176 return ((blockN
& 0x03) == 0x03);
1178 if (blockN
>= 128 && blockN
<= 256) {
1179 return ((blockN
& 0x0F) == 0x0F);
1184 void MifareCWipe(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1187 uint32_t numBlocks
= arg0
;
1189 // bit 0 - wipe gen1a
1190 // bit 1 - fill card with default data
1191 // bit 2 - gen1a = 0, gen1b = 1
1192 uint8_t cmdParams
= arg1
;
1193 bool needWipe
= cmdParams
& 0x01;
1194 bool needFill
= cmdParams
& 0x02;
1195 bool gen1b
= cmdParams
& 0x04;
1197 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1198 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1200 uint8_t block0
[16] = {0x01, 0x02, 0x03, 0x04, 0x04, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBE, 0xAF};
1201 uint8_t block1
[16] = {0x00};
1202 uint8_t blockK
[16] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x08, 0x77, 0x8F, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1203 uint8_t d_block
[18] = {0x00};
1206 uint8_t wupC1
[] = { 0x40 };
1207 uint8_t wupC2
[] = { 0x43 };
1208 uint8_t wipeC
[] = { 0x41 };
1214 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1223 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1224 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1225 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1229 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
1230 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1231 if (MF_DBGLEVEL
>= 1) Dbprintf("wipeC error");
1235 if(mifare_classic_halt(NULL
, 0)) {
1236 if (MF_DBGLEVEL
> 2) Dbprintf("Halt error");
1243 ReaderTransmitBitsPar(wupC1
, 7, 0, NULL
);
1245 // 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)
1248 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1249 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1253 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1254 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1255 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1260 // send blocks command
1261 for (int blockNo
= 0; blockNo
< numBlocks
; blockNo
++) {
1262 if ((mifare_sendcmd_short(NULL
, 0, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1263 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send command error");
1267 // check type of block and add crc
1268 if (!isBlockTrailer(blockNo
)){
1269 memcpy(d_block
, block1
, 16);
1271 memcpy(d_block
, blockK
, 16);
1274 memcpy(d_block
, block0
, 16);
1276 AppendCrc14443a(d_block
, 16);
1278 // send write command
1279 ReaderTransmit(d_block
, sizeof(d_block
), NULL
);
1280 if ((ReaderReceive(receivedAnswer
, receivedAnswerPar
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1281 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send data error");
1287 // do no issue halt command for gen1b
1289 if (mifare_classic_halt(NULL
, 0)) {
1290 if (MF_DBGLEVEL
> 2) Dbprintf("Halt error");
1298 // send USB response
1300 cmd_send(CMD_ACK
,isOK
,0,0,NULL
,0);
1304 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1310 void MifareCSetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1313 uint8_t needWipe
= arg0
;
1314 // bit 0 - need get UID
1315 // bit 1 - need wupC
1316 // bit 2 - need HALT after sequence
1317 // bit 3 - need init FPGA and field before sequence
1318 // bit 4 - need reset FPGA and LED
1319 // bit 6 - gen1b backdoor type
1320 uint8_t workFlags
= arg1
;
1321 uint8_t blockNo
= arg2
;
1324 uint8_t wupC1
[] = { 0x40 };
1325 uint8_t wupC2
[] = { 0x43 };
1326 uint8_t wipeC
[] = { 0x41 };
1330 uint8_t uid
[10] = {0x00};
1331 uint8_t d_block
[18] = {0x00};
1334 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1335 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1337 // reset FPGA and LED
1338 if (workFlags
& 0x08) {
1342 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1350 // get UID from chip
1351 if (workFlags
& 0x01) {
1352 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
1353 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1354 // 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.
1358 if(mifare_classic_halt(NULL
, cuid
)) {
1359 if (MF_DBGLEVEL
> 2) Dbprintf("Halt error");
1360 // Continue, some magic tags misbehavies and send an answer to it.
1366 // Wipe command don't work with gen1b
1367 if (needWipe
&& !(workFlags
& 0x40)){
1368 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1369 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1370 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1374 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
1375 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1376 if (MF_DBGLEVEL
>= 1) Dbprintf("wipeC error");
1380 if(mifare_classic_halt(NULL
, 0)) {
1381 if (MF_DBGLEVEL
> 2) Dbprintf("Halt error");
1382 // Continue, some magic tags misbehavies and send an answer to it.
1388 if (workFlags
& 0x02) {
1389 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1391 // 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)
1392 if (!(workFlags
& 0x40)) {
1394 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1395 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1399 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1400 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1401 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1407 if ((mifare_sendcmd_short(NULL
, 0, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1408 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send command error");
1412 memcpy(d_block
, datain
, 16);
1413 AppendCrc14443a(d_block
, 16);
1415 ReaderTransmit(d_block
, sizeof(d_block
), NULL
);
1416 if ((ReaderReceive(receivedAnswer
, receivedAnswerPar
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1417 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send data error");
1421 if (workFlags
& 0x04) {
1422 // do no issue halt command for gen1b magic tag (#db# halt error. response len: 1)
1423 if (!(workFlags
& 0x40)) {
1424 if (mifare_classic_halt(NULL
, 0)) {
1425 if (MF_DBGLEVEL
> 2) Dbprintf("Halt error");
1426 // Continue, some magic tags misbehavies and send an answer to it.
1437 cmd_send(CMD_ACK
,isOK
,0,0,uid
,4);
1440 if ((workFlags
& 0x10) || (!isOK
)) {
1441 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1447 void MifareCGetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1450 // bit 1 - need wupC
1451 // bit 2 - need HALT after sequence
1452 // bit 3 - need init FPGA and field before sequence
1453 // bit 4 - need reset FPGA and LED
1454 // bit 5 - need to set datain instead of issuing USB reply (called via ARM for StandAloneMode14a)
1455 // bit 6 - gen1b backdoor type
1456 uint8_t workFlags
= arg0
;
1457 uint8_t blockNo
= arg2
;
1460 uint8_t wupC1
[] = { 0x40 };
1461 uint8_t wupC2
[] = { 0x43 };
1465 uint8_t data
[18] = {0x00};
1468 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1469 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1471 if (workFlags
& 0x08) {
1475 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1482 if (workFlags
& 0x02) {
1483 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1484 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1485 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1488 // do no issue for gen1b magic tag
1489 if (!(workFlags
& 0x40)) {
1490 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1491 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1492 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1499 if ((mifare_sendcmd_short(NULL
, 0, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 18)) {
1500 if (MF_DBGLEVEL
>= 1) Dbprintf("read block send command error");
1503 memcpy(data
, receivedAnswer
, 18);
1505 if (workFlags
& 0x04) {
1506 // do no issue halt command for gen1b magic tag (#db# halt error. response len: 1)
1507 if (!(workFlags
& 0x40)) {
1508 if (mifare_classic_halt(NULL
, cuid
)) {
1509 if (MF_DBGLEVEL
> 1) Dbprintf("Halt error");
1510 // Continue, some magic tags misbehavies and send an answer to it.
1521 if (workFlags
& 0x20) {
1523 memcpy(datain
, data
, 18);
1526 cmd_send(CMD_ACK
,isOK
,0,0,data
,18);
1529 if ((workFlags
& 0x10) || (!isOK
)) {
1530 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1535 void MifareCIdent(){
1538 uint8_t wupC1
[] = { 0x40 };
1539 uint8_t wupC2
[] = { 0x43 };
1544 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1545 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1547 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1548 if(ReaderReceive(receivedAnswer
, receivedAnswerPar
) && (receivedAnswer
[0] == 0x0a)) {
1551 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1552 if(ReaderReceive(receivedAnswer
, receivedAnswerPar
) && (receivedAnswer
[0] == 0x0a)) {
1557 // From iceman1001: removed the if, since some magic tags misbehavies and send an answer to it.
1558 mifare_classic_halt(NULL
, 0);
1560 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
1567 void Mifare_DES_Auth1(uint8_t arg0
, uint8_t *datain
){
1569 byte_t dataout
[11] = {0x00};
1570 uint8_t uid
[10] = {0x00};
1573 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1576 int len
= iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true);
1578 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
1583 if(mifare_desfire_des_auth1(cuid
, dataout
)){
1584 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication part1: Fail.");
1589 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 1 FINISHED");
1590 cmd_send(CMD_ACK
,1,cuid
,0,dataout
, sizeof(dataout
));
1593 void Mifare_DES_Auth2(uint32_t arg0
, uint8_t *datain
){
1595 uint32_t cuid
= arg0
;
1596 uint8_t key
[16] = {0x00};
1598 byte_t dataout
[12] = {0x00};
1600 memcpy(key
, datain
, 16);
1602 isOK
= mifare_desfire_des_auth2(cuid
, key
, dataout
);
1605 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) Dbprintf("Authentication part2: Failed");
1610 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 2 FINISHED");
1612 cmd_send(CMD_ACK
, isOK
, 0, 0, dataout
, sizeof(dataout
));
1613 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1619 ReaderTransmit(deselect_cmd
, 3 , NULL
);
1620 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1624 void OnError(uint8_t reason
){
1626 ReaderTransmit(deselect_cmd
, 3 , NULL
);
1627 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1628 cmd_send(CMD_ACK
,0,reason
,0,0,0);