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"
22 // the block number for the ISO14443-4 PCB
23 uint8_t pcb_blocknum
= 0;
24 // Deselect card by sending a s-block. the crc is precalced for speed
25 static uint8_t deselect_cmd
[] = {0xc2,0xe0,0xb4};
28 //-----------------------------------------------------------------------------
29 // Select, Authenticate, Read a MIFARE tag.
31 //-----------------------------------------------------------------------------
32 void MifareReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
35 uint8_t blockNo
= arg0
;
36 uint8_t keyType
= arg1
;
38 ui64Key
= bytes_to_num(datain
, 6);
42 byte_t dataoutbuf
[16];
45 struct Crypto1State mpcs
= {0, 0};
46 struct Crypto1State
*pcs
;
51 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
58 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
59 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
63 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
64 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
68 if(mifare_classic_readblock(pcs
, cuid
, blockNo
, dataoutbuf
)) {
69 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block error");
73 if(mifare_classic_halt(pcs
, cuid
)) {
74 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
82 // ----------------------------- crypto1 destroy
85 if (MF_DBGLEVEL
>= 2) DbpString("READ BLOCK FINISHED");
88 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16);
91 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
95 void MifareUC_Auth(uint8_t arg0
, uint8_t *keybytes
){
97 bool turnOffField
= (arg0
== 1);
99 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
101 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
103 if(!iso14443a_select_card(NULL
, NULL
, NULL
)) {
104 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
109 if(!mifare_ultra_auth(keybytes
)){
110 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication failed");
116 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
119 cmd_send(CMD_ACK
,1,0,0,0,0);
121 cmd_send(CMD_ACK
,1,0,0,0,0);
125 // Arg1 = UsePwd bool
126 // datain = PWD bytes,
127 void MifareUReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
)
129 uint8_t blockNo
= arg0
;
130 byte_t dataout
[16] = {0x00};
131 bool useKey
= (arg1
== 1); //UL_C
132 bool usePwd
= (arg1
== 2); //UL_EV1/NTAG
137 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
139 int len
= iso14443a_select_card(NULL
, NULL
, NULL
);
141 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%02X)",len
);
146 // UL-C authentication
148 uint8_t key
[16] = {0x00};
149 memcpy(key
, datain
, sizeof(key
) );
151 if ( !mifare_ultra_auth(key
) ) {
157 // UL-EV1 / NTAG authentication
159 uint8_t pwd
[4] = {0x00};
160 memcpy(pwd
, datain
, 4);
161 uint8_t pack
[4] = {0,0,0,0};
162 if (!mifare_ul_ev1_auth(pwd
, pack
)) {
168 if( mifare_ultra_readblock(blockNo
, dataout
) ) {
169 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Read block error");
174 if( mifare_ultra_halt() ) {
175 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Halt error");
180 cmd_send(CMD_ACK
,1,0,0,dataout
,16);
181 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
185 //-----------------------------------------------------------------------------
186 // Select, Authenticate, Read a MIFARE tag.
187 // read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)
188 //-----------------------------------------------------------------------------
189 void MifareReadSector(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
192 uint8_t sectorNo
= arg0
;
193 uint8_t keyType
= arg1
;
194 uint64_t ui64Key
= 0;
195 ui64Key
= bytes_to_num(datain
, 6);
199 byte_t dataoutbuf
[16 * 16];
202 struct Crypto1State mpcs
= {0, 0};
203 struct Crypto1State
*pcs
;
209 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
216 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
218 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
222 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
224 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
227 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
228 if(mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
+ 16 * blockNo
)) {
230 if (MF_DBGLEVEL
>= 1) Dbprintf("Read sector %2d block %2d error", sectorNo
, blockNo
);
235 if(mifare_classic_halt(pcs
, cuid
)) {
236 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
239 // ----------------------------- crypto1 destroy
240 crypto1_destroy(pcs
);
242 if (MF_DBGLEVEL
>= 2) DbpString("READ SECTOR FINISHED");
245 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16*NumBlocksPerSector(sectorNo
));
249 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
253 // arg0 = blockNo (start)
254 // arg1 = Pages (number of blocks)
256 // datain = KEY bytes
257 void MifareUReadCard(uint8_t arg0
, uint16_t arg1
, uint8_t arg2
, uint8_t *datain
)
259 // free eventually allocated BigBuf memory
265 uint8_t blockNo
= arg0
;
266 uint16_t blocks
= arg1
;
267 bool useKey
= (arg2
== 1); //UL_C
268 bool usePwd
= (arg2
== 2); //UL_EV1/NTAG
269 uint32_t countblocks
= 0;
270 uint8_t *dataout
= BigBuf_malloc(CARD_MEMORY_SIZE
);
271 if (dataout
== NULL
){
272 Dbprintf("out of memory");
279 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
281 int len
= iso14443a_select_card(NULL
, NULL
, NULL
);
283 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%d)",len
);
288 // UL-C authentication
290 uint8_t key
[16] = {0x00};
291 memcpy(key
, datain
, sizeof(key
) );
293 if ( !mifare_ultra_auth(key
) ) {
299 // UL-EV1 / NTAG authentication
301 uint8_t pwd
[4] = {0x00};
302 memcpy(pwd
, datain
, sizeof(pwd
));
303 uint8_t pack
[4] = {0,0,0,0};
305 if (!mifare_ul_ev1_auth(pwd
, pack
)){
311 for (int i
= 0; i
< blocks
; i
++){
312 if ((i
*4) + 4 > CARD_MEMORY_SIZE
) {
313 Dbprintf("Data exceeds buffer!!");
317 // UL-C authentication
319 uint8_t key
[16] = {0x00};
320 memcpy(key
, datain
, sizeof(key
) );
322 if ( !mifare_ultra_auth(key
) ) {
328 // UL-EV1 / NTAG authentication
330 uint8_t pwd
[4] = {0x00};
331 memcpy(pwd
, datain
, sizeof(pwd
));
332 uint8_t pack
[4] = {0,0,0,0};
334 if (!mifare_ul_ev1_auth(pwd
, pack
)){
340 for (int i
= 0; i
< blocks
; i
++){
341 if ((i
*4) + 4 > CARD_MEMORY_SIZE
) {
342 Dbprintf("Data exceeds buffer!!");
346 len
= mifare_ultra_readblock(blockNo
+ i
, dataout
+ 4 * i
);
349 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Read block %d error",i
);
350 // if no blocks read - error out
355 //stop at last successful read block and return what we got
363 len
= mifare_ultra_halt();
365 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Halt error");
370 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) Dbprintf("Blocks read %d", countblocks
);
373 cmd_send(CMD_ACK
, 1, countblocks
, countblocks
, 0, 0);
374 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
378 //-----------------------------------------------------------------------------
379 // Select, Authenticate, Write a MIFARE tag.
381 //-----------------------------------------------------------------------------
382 void MifareWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
385 uint8_t blockNo
= arg0
;
386 uint8_t keyType
= arg1
;
387 uint64_t ui64Key
= 0;
388 byte_t blockdata
[16];
390 ui64Key
= bytes_to_num(datain
, 6);
391 memcpy(blockdata
, datain
+ 10, 16);
397 struct Crypto1State mpcs
= {0, 0};
398 struct Crypto1State
*pcs
;
404 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
411 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
412 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
416 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
417 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
421 if(mifare_classic_writeblock(pcs
, cuid
, blockNo
, blockdata
)) {
422 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
426 if(mifare_classic_halt(pcs
, cuid
)) {
427 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
435 // ----------------------------- crypto1 destroy
436 crypto1_destroy(pcs
);
438 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
441 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
446 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
450 void MifareUWriteBlock(uint8_t arg0
, uint8_t *datain
)
452 uint8_t blockNo
= arg0
;
453 byte_t blockdata
[16] = {0x00};
455 memcpy(blockdata
, datain
, 16);
457 uint8_t uid
[10] = {0x00};
459 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
462 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
464 if(!iso14443a_select_card(uid
, NULL
, NULL
)) {
465 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
470 if(mifare_ultra_writeblock(blockNo
, blockdata
)) {
471 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
475 if(mifare_ultra_halt()) {
476 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
481 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
483 cmd_send(CMD_ACK
,1,0,0,0,0);
484 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
488 // Arg0 : Block to write to.
489 // Arg1 : 0 = use no authentication.
490 // 1 = use 0x1A authentication.
491 // 2 = use 0x1B authentication.
492 // datain : 4 first bytes is data to be written.
493 // : 4/16 next bytes is authentication key.
494 void MifareUWriteBlock_Special(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
)
496 uint8_t blockNo
= arg0
;
497 bool useKey
= (arg1
== 1); //UL_C
498 bool usePwd
= (arg1
== 2); //UL_EV1/NTAG
499 byte_t blockdata
[4] = {0x00};
501 memcpy(blockdata
, datain
,4);
506 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
508 if(!iso14443a_select_card(NULL
, NULL
, NULL
)) {
509 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
514 // UL-C authentication
516 uint8_t key
[16] = {0x00};
517 memcpy(key
, datain
+4, sizeof(key
) );
519 if ( !mifare_ultra_auth(key
) ) {
525 // UL-EV1 / NTAG authentication
527 uint8_t pwd
[4] = {0x00};
528 memcpy(pwd
, datain
+4, 4);
529 uint8_t pack
[4] = {0,0,0,0};
530 if (!mifare_ul_ev1_auth(pwd
, pack
)) {
536 if(mifare_ultra_special_writeblock(blockNo
, blockdata
)) {
537 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
542 if(mifare_ultra_halt()) {
543 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
548 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
550 cmd_send(CMD_ACK
,1,0,0,0,0);
551 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
555 void MifareUSetPwd(uint8_t arg0
, uint8_t *datain
){
557 uint8_t pwd
[16] = {0x00};
558 byte_t blockdata
[4] = {0x00};
560 memcpy(pwd
, datain
, 16);
562 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
564 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
566 if(!iso14443a_select_card(NULL
, NULL
, NULL
)) {
567 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
572 blockdata
[0] = pwd
[7];
573 blockdata
[1] = pwd
[6];
574 blockdata
[2] = pwd
[5];
575 blockdata
[3] = pwd
[4];
576 if(mifare_ultra_special_writeblock( 44, blockdata
)) {
577 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
582 blockdata
[0] = pwd
[3];
583 blockdata
[1] = pwd
[2];
584 blockdata
[2] = pwd
[1];
585 blockdata
[3] = pwd
[0];
586 if(mifare_ultra_special_writeblock( 45, blockdata
)) {
587 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
592 blockdata
[0] = pwd
[15];
593 blockdata
[1] = pwd
[14];
594 blockdata
[2] = pwd
[13];
595 blockdata
[3] = pwd
[12];
596 if(mifare_ultra_special_writeblock( 46, blockdata
)) {
597 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
602 blockdata
[0] = pwd
[11];
603 blockdata
[1] = pwd
[10];
604 blockdata
[2] = pwd
[9];
605 blockdata
[3] = pwd
[8];
606 if(mifare_ultra_special_writeblock( 47, blockdata
)) {
607 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
612 if(mifare_ultra_halt()) {
613 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
618 cmd_send(CMD_ACK
,1,0,0,0,0);
619 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
623 // Return 1 if the nonce is invalid else return 0
624 int valid_nonce(uint32_t Nt
, uint32_t NtEnc
, uint32_t Ks1
, uint8_t *parity
) {
625 return ((oddparity((Nt
>> 24) & 0xFF) == ((parity
[0]) ^ oddparity((NtEnc
>> 24) & 0xFF) ^ BIT(Ks1
,16))) & \
626 (oddparity((Nt
>> 16) & 0xFF) == ((parity
[1]) ^ oddparity((NtEnc
>> 16) & 0xFF) ^ BIT(Ks1
,8))) & \
627 (oddparity((Nt
>> 8) & 0xFF) == ((parity
[2]) ^ oddparity((NtEnc
>> 8) & 0xFF) ^ BIT(Ks1
,0)))) ? 1 : 0;
631 //-----------------------------------------------------------------------------
632 // MIFARE nested authentication.
634 //-----------------------------------------------------------------------------
635 void MifareNested(uint32_t arg0
, uint32_t arg1
, uint32_t calibrate
, uint8_t *datain
)
638 uint8_t blockNo
= arg0
& 0xff;
639 uint8_t keyType
= (arg0
>> 8) & 0xff;
640 uint8_t targetBlockNo
= arg1
& 0xff;
641 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
642 uint64_t ui64Key
= 0;
644 ui64Key
= bytes_to_num(datain
, 6);
647 uint16_t rtr
, i
, j
, len
;
649 static uint16_t dmin
, dmax
;
651 uint32_t cuid
, nt1
, nt2
, nttmp
, nttest
, ks1
;
653 uint32_t target_nt
[2], target_ks
[2];
655 uint8_t par_array
[4];
657 struct Crypto1State mpcs
= {0, 0};
658 struct Crypto1State
*pcs
;
660 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
662 uint32_t auth1_time
, auth2_time
;
663 static uint16_t delta_time
;
665 // free eventually allocated BigBuf memory
671 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
677 // statistics on nonce distance
678 if (calibrate
) { // for first call only. Otherwise reuse previous calibration
686 for (rtr
= 0; rtr
< 17; rtr
++) {
688 // prepare next select. No need to power down the card.
689 if(mifare_classic_halt(pcs
, cuid
)) {
690 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
695 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
696 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
702 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
703 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
709 auth2_time
= auth1_time
+ delta_time
;
713 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_NESTED
, &nt2
, &auth2_time
)) {
714 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error");
719 nttmp
= prng_successor(nt1
, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160
720 for (i
= 101; i
< 1200; i
++) {
721 nttmp
= prng_successor(nttmp
, 1);
722 if (nttmp
== nt2
) break;
732 delta_time
= auth2_time
- auth1_time
+ 32; // allow some slack for proper timing
734 if (MF_DBGLEVEL
>= 3) Dbprintf("Nested: calibrating... ntdist=%d", i
);
738 if (rtr
<= 1) return;
740 davg
= (davg
+ (rtr
- 1)/2) / (rtr
- 1);
742 if (MF_DBGLEVEL
>= 3) Dbprintf("min=%d max=%d avg=%d, delta_time=%d", dmin
, dmax
, davg
, delta_time
);
750 // -------------------------------------------------------------------------------------------------
754 // get crypted nonces for target sector
755 for(i
=0; i
< 2; i
++) { // look for exactly two different nonces
758 while(target_nt
[i
] == 0) { // continue until we have an unambiguous nonce
760 // prepare next select. No need to power down the card.
761 if(mifare_classic_halt(pcs
, cuid
)) {
762 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
766 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
767 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
772 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
773 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
777 // nested authentication
778 auth2_time
= auth1_time
+ delta_time
;
779 len
= mifare_sendcmd_shortex(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType
& 0x01), targetBlockNo
, receivedAnswer
, par
, &auth2_time
);
781 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error len=%d", len
);
785 nt2
= bytes_to_num(receivedAnswer
, 4);
786 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i
+1, nt1
, nt2
, par
[0]);
788 // Parity validity check
789 for (j
= 0; j
< 4; j
++) {
790 par_array
[j
] = (oddparity(receivedAnswer
[j
]) != ((par
[0] >> (7-j
)) & 0x01));
794 nttest
= prng_successor(nt1
, dmin
- 1);
795 for (j
= dmin
; j
< dmax
+ 1; j
++) {
796 nttest
= prng_successor(nttest
, 1);
799 if (valid_nonce(nttest
, nt2
, ks1
, par_array
)){
800 if (ncount
> 0) { // we are only interested in disambiguous nonces, try again
801 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i
+1, j
);
805 target_nt
[i
] = nttest
;
808 if (i
== 1 && target_nt
[1] == target_nt
[0]) { // we need two different nonces
810 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j
);
813 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i
+1, j
);
816 if (target_nt
[i
] == 0 && j
== dmax
+1 && MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i
+1);
822 // ----------------------------- crypto1 destroy
823 crypto1_destroy(pcs
);
825 byte_t buf
[4 + 4 * 4];
826 memcpy(buf
, &cuid
, 4);
827 memcpy(buf
+4, &target_nt
[0], 4);
828 memcpy(buf
+8, &target_ks
[0], 4);
829 memcpy(buf
+12, &target_nt
[1], 4);
830 memcpy(buf
+16, &target_ks
[1], 4);
833 cmd_send(CMD_ACK
, 0, 2, targetBlockNo
+ (targetKeyType
* 0x100), buf
, sizeof(buf
));
836 if (MF_DBGLEVEL
>= 3) DbpString("NESTED FINISHED");
838 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
843 //-----------------------------------------------------------------------------
844 // MIFARE check keys. key count up to 85.
846 //-----------------------------------------------------------------------------
847 void MifareChkKeys(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
850 uint8_t blockNo
= arg0
;
851 uint8_t keyType
= arg1
;
852 uint8_t keyCount
= arg2
;
853 uint64_t ui64Key
= 0;
860 struct Crypto1State mpcs
= {0, 0};
861 struct Crypto1State
*pcs
;
865 int OLD_MF_DBGLEVEL
= MF_DBGLEVEL
;
866 MF_DBGLEVEL
= MF_DBG_NONE
;
872 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
878 for (i
= 0; i
< keyCount
; i
++) {
879 if(mifare_classic_halt(pcs
, cuid
)) {
880 if (MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Halt error");
883 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
884 if (OLD_MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Can't select card");
888 ui64Key
= bytes_to_num(datain
+ i
* 6, 6);
889 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
897 // ----------------------------- crypto1 destroy
898 crypto1_destroy(pcs
);
901 cmd_send(CMD_ACK
,isOK
,0,0,datain
+ i
* 6,6);
904 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
907 // restore debug level
908 MF_DBGLEVEL
= OLD_MF_DBGLEVEL
;
911 //-----------------------------------------------------------------------------
912 // MIFARE commands set debug level
914 //-----------------------------------------------------------------------------
915 void MifareSetDbgLvl(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
917 Dbprintf("Debug level: %d", MF_DBGLEVEL
);
920 //-----------------------------------------------------------------------------
921 // Work with emulator memory
923 //-----------------------------------------------------------------------------
924 void MifareEMemClr(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
928 void MifareEMemSet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
929 emlSetMem(datain
, arg0
, arg1
); // data, block num, blocks count
932 void MifareEMemGet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
933 byte_t buf
[USB_CMD_DATA_SIZE
];
934 emlGetMem(buf
, arg0
, arg1
); // data, block num, blocks count (max 4)
937 cmd_send(CMD_ACK
,arg0
,arg1
,0,buf
,USB_CMD_DATA_SIZE
);
941 //-----------------------------------------------------------------------------
942 // Load a card into the emulator memory
944 //-----------------------------------------------------------------------------
945 void MifareECardLoad(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
946 uint8_t numSectors
= arg0
;
947 uint8_t keyType
= arg1
;
948 uint64_t ui64Key
= 0;
950 struct Crypto1State mpcs
= {0, 0};
951 struct Crypto1State
*pcs
;
955 byte_t dataoutbuf
[16];
956 byte_t dataoutbuf2
[16];
963 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
971 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
973 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
976 for (uint8_t sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
977 ui64Key
= emlGetKey(sectorNo
, keyType
);
979 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
981 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo
);
985 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_NESTED
)) {
987 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo
);
992 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
993 if(isOK
&& mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
)) {
995 if (MF_DBGLEVEL
>= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo
, blockNo
);
999 if (blockNo
< NumBlocksPerSector(sectorNo
) - 1) {
1000 emlSetMem(dataoutbuf
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
1001 } else { // sector trailer, keep the keys, set only the AC
1002 emlGetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
1003 memcpy(&dataoutbuf2
[6], &dataoutbuf
[6], 4);
1004 emlSetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
1011 if(mifare_classic_halt(pcs
, cuid
)) {
1012 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1015 // ----------------------------- crypto1 destroy
1016 crypto1_destroy(pcs
);
1018 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1021 if (MF_DBGLEVEL
>= 2) DbpString("EMUL FILL SECTORS FINISHED");
1026 //-----------------------------------------------------------------------------
1027 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
1029 //-----------------------------------------------------------------------------
1030 void MifareCSetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1033 uint8_t needWipe
= arg0
;
1034 // bit 0 - need get UID
1035 // bit 1 - need wupC
1036 // bit 2 - need HALT after sequence
1037 // bit 3 - need init FPGA and field before sequence
1038 // bit 4 - need reset FPGA and LED
1039 uint8_t workFlags
= arg1
;
1040 uint8_t blockNo
= arg2
;
1043 uint8_t wupC1
[] = { 0x40 };
1044 uint8_t wupC2
[] = { 0x43 };
1045 uint8_t wipeC
[] = { 0x41 };
1049 uint8_t uid
[10] = {0x00};
1050 uint8_t d_block
[18] = {0x00};
1053 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1054 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1056 // reset FPGA and LED
1057 if (workFlags
& 0x08) {
1064 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1069 // get UID from chip
1070 if (workFlags
& 0x01) {
1071 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
1072 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1076 if(mifare_classic_halt(NULL
, cuid
)) {
1077 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1084 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1085 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1086 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1090 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
1091 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1092 if (MF_DBGLEVEL
>= 1) Dbprintf("wipeC error");
1096 if(mifare_classic_halt(NULL
, cuid
)) {
1097 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1103 if (workFlags
& 0x02) {
1104 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1105 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1106 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1110 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1111 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1112 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1117 if ((mifare_sendcmd_short(NULL
, 0, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1118 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send command error");
1122 memcpy(d_block
, datain
, 16);
1123 AppendCrc14443a(d_block
, 16);
1125 ReaderTransmit(d_block
, sizeof(d_block
), NULL
);
1126 if ((ReaderReceive(receivedAnswer
, receivedAnswerPar
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1127 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send data error");
1131 if (workFlags
& 0x04) {
1132 if (mifare_classic_halt(NULL
, cuid
)) {
1133 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1143 cmd_send(CMD_ACK
,isOK
,0,0,uid
,4);
1146 if ((workFlags
& 0x10) || (!isOK
)) {
1147 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1153 void MifareCGetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1156 // bit 1 - need wupC
1157 // bit 2 - need HALT after sequence
1158 // bit 3 - need init FPGA and field before sequence
1159 // bit 4 - need reset FPGA and LED
1160 uint8_t workFlags
= arg0
;
1161 uint8_t blockNo
= arg2
;
1164 uint8_t wupC1
[] = { 0x40 };
1165 uint8_t wupC2
[] = { 0x43 };
1169 uint8_t data
[18] = {0x00};
1172 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1173 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1175 if (workFlags
& 0x08) {
1182 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1186 if (workFlags
& 0x02) {
1187 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1188 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1189 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1193 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1194 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1195 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1201 if ((mifare_sendcmd_short(NULL
, 0, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 18)) {
1202 if (MF_DBGLEVEL
>= 1) Dbprintf("read block send command error");
1205 memcpy(data
, receivedAnswer
, 18);
1207 if (workFlags
& 0x04) {
1208 if (mifare_classic_halt(NULL
, cuid
)) {
1209 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1219 cmd_send(CMD_ACK
,isOK
,0,0,data
,18);
1222 if ((workFlags
& 0x10) || (!isOK
)) {
1223 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1228 void MifareCIdent(){
1231 uint8_t wupC1
[] = { 0x40 };
1232 uint8_t wupC2
[] = { 0x43 };
1237 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1238 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1240 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1241 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1245 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1246 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1250 if (mifare_classic_halt(NULL
, 0)) {
1254 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
1257 void MifareCollectNonces(uint32_t arg0
, uint32_t arg1
){
1261 uint32_t iterations
= arg0
;
1262 uint8_t uid
[10] = {0x00};
1264 uint8_t *response
= BigBuf_malloc(MAX_MIFARE_FRAME_SIZE
);
1265 uint8_t *responsePar
= BigBuf_malloc(MAX_MIFARE_PARITY_SIZE
);
1267 uint8_t mf_auth
[] = { 0x60,0x00,0xf5,0x7b };
1269 // get memory from BigBuf.
1270 uint8_t *nonces
= BigBuf_malloc(iterations
* 4);
1278 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1280 for (int i
= 0; i
< iterations
; i
++) {
1284 // Test if the action was cancelled
1285 if(BUTTON_PRESS()) break;
1287 // if(mifare_classic_halt(pcs, cuid)) {
1288 // if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
1291 if(!iso14443a_select_card(uid
, NULL
, NULL
)) {
1292 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1296 // Transmit MIFARE_CLASSIC_AUTH.
1297 ReaderTransmit(mf_auth
, sizeof(mf_auth
), NULL
);
1299 // Receive the (4 Byte) "random" nonce
1300 if (!ReaderReceive(response
, responsePar
)) {
1301 if (MF_DBGLEVEL
>= 1) Dbprintf("Couldn't receive tag nonce");
1305 nonces
[i
*4] = bytes_to_num(response
, 4);
1308 int packLen
= iterations
* 4;
1311 while (packLen
> 0) {
1312 packSize
= MIN(USB_CMD_DATA_SIZE
, packLen
);
1314 cmd_send(CMD_ACK
, 77, 0, packSize
, nonces
- packLen
, packSize
);
1317 packLen
-= packSize
;
1320 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1328 void Mifare_DES_Auth1(uint8_t arg0
, uint8_t *datain
){
1330 byte_t dataout
[11] = {0x00};
1331 uint8_t uid
[10] = {0x00};
1332 uint32_t cuid
= 0x00;
1335 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1337 int len
= iso14443a_select_card(uid
, NULL
, &cuid
);
1339 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
1344 if(mifare_desfire_des_auth1(cuid
, dataout
)){
1345 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication part1: Fail.");
1350 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 1 FINISHED");
1351 cmd_send(CMD_ACK
,1,cuid
,0,dataout
, sizeof(dataout
));
1354 void Mifare_DES_Auth2(uint32_t arg0
, uint8_t *datain
){
1356 uint32_t cuid
= arg0
;
1357 uint8_t key
[16] = {0x00};
1358 byte_t dataout
[12] = {0x00};
1361 memcpy(key
, datain
, 16);
1363 isOK
= mifare_desfire_des_auth2(cuid
, key
, dataout
);
1366 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) Dbprintf("Authentication part2: Failed");
1371 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 2 FINISHED");
1373 cmd_send(CMD_ACK
, isOK
, 0, 0, dataout
, sizeof(dataout
));
1374 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1380 ReaderTransmit(deselect_cmd
, 3 , NULL
);
1381 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1385 void OnError(uint8_t reason
){
1387 ReaderTransmit(deselect_cmd
, 3 , NULL
);
1388 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1389 cmd_send(CMD_ACK
,0,reason
,0,0,0);