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
) == 1){
110 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication failed");
114 cmd_send(CMD_ACK
,1,0,0,0,0);
117 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
123 // Arg1 = UsePwd bool
124 // datain = PWD bytes,
125 void MifareUReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
)
127 uint8_t blockNo
= arg0
;
128 byte_t dataout
[16] = {0x00};
129 bool useKey
= (arg1
== 1); //UL_C
130 bool usePwd
= (arg1
== 2); //UL_EV1/NTAG
135 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
137 int len
= iso14443a_select_card(NULL
, NULL
, NULL
);
139 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%02X)",len
);
144 // UL-C authentication
146 uint8_t key
[16] = {0x00};
147 memcpy(key
, datain
, sizeof(key
) );
149 if ( mifare_ultra_auth(key
) == 1 ) {
155 // UL-EV1 / NTAG authentication
157 uint8_t pwd
[4] = {0x00};
158 memcpy(pwd
, datain
, 4);
159 uint8_t pack
[4] = {0,0,0,0};
160 if (mifare_ul_ev1_auth(pwd
, pack
) == 1) {
166 if( mifare_ultra_readblock(blockNo
, dataout
) ) {
167 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Read block error");
172 if( mifare_ultra_halt() ) {
173 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Halt error");
178 cmd_send(CMD_ACK
,1,0,0,dataout
,16);
179 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
183 //-----------------------------------------------------------------------------
184 // Select, Authenticate, Read a MIFARE tag.
185 // read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)
186 //-----------------------------------------------------------------------------
187 void MifareReadSector(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
190 uint8_t sectorNo
= arg0
;
191 uint8_t keyType
= arg1
;
192 uint64_t ui64Key
= 0;
193 ui64Key
= bytes_to_num(datain
, 6);
197 byte_t dataoutbuf
[16 * 16];
200 struct Crypto1State mpcs
= {0, 0};
201 struct Crypto1State
*pcs
;
207 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
214 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
216 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
220 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
222 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
225 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
226 if(mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
+ 16 * blockNo
)) {
228 if (MF_DBGLEVEL
>= 1) Dbprintf("Read sector %2d block %2d error", sectorNo
, blockNo
);
233 if(mifare_classic_halt(pcs
, cuid
)) {
234 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
237 // ----------------------------- crypto1 destroy
238 crypto1_destroy(pcs
);
240 if (MF_DBGLEVEL
>= 2) DbpString("READ SECTOR FINISHED");
243 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16*NumBlocksPerSector(sectorNo
));
247 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
251 void MifareUReadCard(uint8_t arg0
, uint16_t arg1
, uint8_t arg2
, uint8_t *datain
)
254 uint8_t blockNo
= arg0
;
255 uint16_t blocks
= arg1
;
256 bool useKey
= (arg2
== 1); //UL_C
257 bool usePwd
= (arg2
== 2); //UL_EV1/NTAG
259 uint8_t dataout
[176] = {0x00};
264 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
266 int len
= iso14443a_select_card(NULL
, NULL
, NULL
);
268 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%d)",len
);
273 // UL-C authentication
275 uint8_t key
[16] = {0x00};
276 memcpy(key
, datain
, sizeof(key
) );
278 if ( mifare_ultra_auth(key
) == 1 ) {
284 // UL-EV1 / NTAG authentication
286 uint8_t pwd
[4] = {0x00};
287 memcpy(pwd
, datain
, sizeof(pwd
));
288 uint8_t pack
[4] = {0,0,0,0};
290 if (mifare_ul_ev1_auth(pwd
, pack
) == 1){
296 for (int i
= 0; i
< blocks
; i
++){
297 len
= mifare_ultra_readblock(blockNo
* 4 + i
, dataout
+ 4 * i
);
300 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Read block %d error",i
);
308 len
= mifare_ultra_halt();
310 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Halt error");
315 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) Dbprintf("Blocks read %d", countblocks
);
319 cmd_send(CMD_ACK
, 1, len
, 0, dataout
, len
);
320 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
324 //-----------------------------------------------------------------------------
325 // Select, Authenticate, Write a MIFARE tag.
327 //-----------------------------------------------------------------------------
328 void MifareWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
331 uint8_t blockNo
= arg0
;
332 uint8_t keyType
= arg1
;
333 uint64_t ui64Key
= 0;
334 byte_t blockdata
[16];
336 ui64Key
= bytes_to_num(datain
, 6);
337 memcpy(blockdata
, datain
+ 10, 16);
343 struct Crypto1State mpcs
= {0, 0};
344 struct Crypto1State
*pcs
;
350 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
357 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
358 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
362 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
363 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
367 if(mifare_classic_writeblock(pcs
, cuid
, blockNo
, blockdata
)) {
368 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
372 if(mifare_classic_halt(pcs
, cuid
)) {
373 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
381 // ----------------------------- crypto1 destroy
382 crypto1_destroy(pcs
);
384 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
387 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
392 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
396 void MifareUWriteBlock(uint8_t arg0
, uint8_t *datain
)
398 uint8_t blockNo
= arg0
;
399 byte_t blockdata
[16] = {0x00};
401 memcpy(blockdata
, datain
, 16);
403 uint8_t uid
[10] = {0x00};
405 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
408 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
410 if(!iso14443a_select_card(uid
, NULL
, NULL
)) {
411 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
416 if(mifare_ultra_writeblock(blockNo
, blockdata
)) {
417 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
421 if(mifare_ultra_halt()) {
422 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
427 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
429 cmd_send(CMD_ACK
,1,0,0,0,0);
430 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
434 void MifareUWriteBlock_Special(uint8_t arg0
, uint8_t *datain
)
436 uint8_t blockNo
= arg0
;
437 byte_t blockdata
[4] = {0x00};
439 memcpy(blockdata
, datain
,4);
444 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
446 if(!iso14443a_select_card(NULL
, NULL
, NULL
)) {
447 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
452 if(mifare_ultra_special_writeblock(blockNo
, blockdata
)) {
453 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
458 if(mifare_ultra_halt()) {
459 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
464 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
466 cmd_send(CMD_ACK
,1,0,0,0,0);
467 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
471 void MifareUSetPwd(uint8_t arg0
, uint8_t *datain
){
473 uint8_t pwd
[16] = {0x00};
474 byte_t blockdata
[4] = {0x00};
476 memcpy(pwd
, datain
, 16);
478 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
480 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
482 if(!iso14443a_select_card(NULL
, NULL
, NULL
)) {
483 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
488 blockdata
[0] = pwd
[7];
489 blockdata
[1] = pwd
[6];
490 blockdata
[2] = pwd
[5];
491 blockdata
[3] = pwd
[4];
492 if(mifare_ultra_special_writeblock( 44, blockdata
)) {
493 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
498 blockdata
[0] = pwd
[3];
499 blockdata
[1] = pwd
[2];
500 blockdata
[2] = pwd
[1];
501 blockdata
[3] = pwd
[0];
502 if(mifare_ultra_special_writeblock( 45, blockdata
)) {
503 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
508 blockdata
[0] = pwd
[15];
509 blockdata
[1] = pwd
[14];
510 blockdata
[2] = pwd
[13];
511 blockdata
[3] = pwd
[12];
512 if(mifare_ultra_special_writeblock( 46, blockdata
)) {
513 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
518 blockdata
[0] = pwd
[11];
519 blockdata
[1] = pwd
[10];
520 blockdata
[2] = pwd
[9];
521 blockdata
[3] = pwd
[8];
522 if(mifare_ultra_special_writeblock( 47, blockdata
)) {
523 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
528 if(mifare_ultra_halt()) {
529 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
534 cmd_send(CMD_ACK
,1,0,0,0,0);
535 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
539 // Return 1 if the nonce is invalid else return 0
540 int valid_nonce(uint32_t Nt
, uint32_t NtEnc
, uint32_t Ks1
, uint8_t *parity
) {
541 return ((oddparity((Nt
>> 24) & 0xFF) == ((parity
[0]) ^ oddparity((NtEnc
>> 24) & 0xFF) ^ BIT(Ks1
,16))) & \
542 (oddparity((Nt
>> 16) & 0xFF) == ((parity
[1]) ^ oddparity((NtEnc
>> 16) & 0xFF) ^ BIT(Ks1
,8))) & \
543 (oddparity((Nt
>> 8) & 0xFF) == ((parity
[2]) ^ oddparity((NtEnc
>> 8) & 0xFF) ^ BIT(Ks1
,0)))) ? 1 : 0;
547 //-----------------------------------------------------------------------------
548 // MIFARE nested authentication.
550 //-----------------------------------------------------------------------------
551 void MifareNested(uint32_t arg0
, uint32_t arg1
, uint32_t calibrate
, uint8_t *datain
)
554 uint8_t blockNo
= arg0
& 0xff;
555 uint8_t keyType
= (arg0
>> 8) & 0xff;
556 uint8_t targetBlockNo
= arg1
& 0xff;
557 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
558 uint64_t ui64Key
= 0;
560 ui64Key
= bytes_to_num(datain
, 6);
563 uint16_t rtr
, i
, j
, len
;
565 static uint16_t dmin
, dmax
;
567 uint32_t cuid
, nt1
, nt2
, nttmp
, nttest
, ks1
;
569 uint32_t target_nt
[2], target_ks
[2];
571 uint8_t par_array
[4];
573 struct Crypto1State mpcs
= {0, 0};
574 struct Crypto1State
*pcs
;
576 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
578 uint32_t auth1_time
, auth2_time
;
579 static uint16_t delta_time
;
581 // free eventually allocated BigBuf memory
587 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
593 // statistics on nonce distance
594 if (calibrate
) { // for first call only. Otherwise reuse previous calibration
602 for (rtr
= 0; rtr
< 17; rtr
++) {
604 // prepare next select. No need to power down the card.
605 if(mifare_classic_halt(pcs
, cuid
)) {
606 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
611 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
612 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
618 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
619 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
625 auth2_time
= auth1_time
+ delta_time
;
629 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_NESTED
, &nt2
, &auth2_time
)) {
630 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error");
635 nttmp
= prng_successor(nt1
, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160
636 for (i
= 101; i
< 1200; i
++) {
637 nttmp
= prng_successor(nttmp
, 1);
638 if (nttmp
== nt2
) break;
648 delta_time
= auth2_time
- auth1_time
+ 32; // allow some slack for proper timing
650 if (MF_DBGLEVEL
>= 3) Dbprintf("Nested: calibrating... ntdist=%d", i
);
654 if (rtr
<= 1) return;
656 davg
= (davg
+ (rtr
- 1)/2) / (rtr
- 1);
658 if (MF_DBGLEVEL
>= 3) Dbprintf("min=%d max=%d avg=%d, delta_time=%d", dmin
, dmax
, davg
, delta_time
);
666 // -------------------------------------------------------------------------------------------------
670 // get crypted nonces for target sector
671 for(i
=0; i
< 2; i
++) { // look for exactly two different nonces
674 while(target_nt
[i
] == 0) { // continue until we have an unambiguous nonce
676 // prepare next select. No need to power down the card.
677 if(mifare_classic_halt(pcs
, cuid
)) {
678 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
682 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
683 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
688 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
689 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
693 // nested authentication
694 auth2_time
= auth1_time
+ delta_time
;
695 len
= mifare_sendcmd_shortex(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType
& 0x01), targetBlockNo
, receivedAnswer
, par
, &auth2_time
);
697 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error len=%d", len
);
701 nt2
= bytes_to_num(receivedAnswer
, 4);
702 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i
+1, nt1
, nt2
, par
[0]);
704 // Parity validity check
705 for (j
= 0; j
< 4; j
++) {
706 par_array
[j
] = (oddparity(receivedAnswer
[j
]) != ((par
[0] >> (7-j
)) & 0x01));
710 nttest
= prng_successor(nt1
, dmin
- 1);
711 for (j
= dmin
; j
< dmax
+ 1; j
++) {
712 nttest
= prng_successor(nttest
, 1);
715 if (valid_nonce(nttest
, nt2
, ks1
, par_array
)){
716 if (ncount
> 0) { // we are only interested in disambiguous nonces, try again
717 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i
+1, j
);
721 target_nt
[i
] = nttest
;
724 if (i
== 1 && target_nt
[1] == target_nt
[0]) { // we need two different nonces
726 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j
);
729 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i
+1, j
);
732 if (target_nt
[i
] == 0 && j
== dmax
+1 && MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i
+1);
738 // ----------------------------- crypto1 destroy
739 crypto1_destroy(pcs
);
741 byte_t buf
[4 + 4 * 4];
742 memcpy(buf
, &cuid
, 4);
743 memcpy(buf
+4, &target_nt
[0], 4);
744 memcpy(buf
+8, &target_ks
[0], 4);
745 memcpy(buf
+12, &target_nt
[1], 4);
746 memcpy(buf
+16, &target_ks
[1], 4);
749 cmd_send(CMD_ACK
, 0, 2, targetBlockNo
+ (targetKeyType
* 0x100), buf
, sizeof(buf
));
752 if (MF_DBGLEVEL
>= 3) DbpString("NESTED FINISHED");
754 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
759 //-----------------------------------------------------------------------------
760 // MIFARE check keys. key count up to 85.
762 //-----------------------------------------------------------------------------
763 void MifareChkKeys(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
766 uint8_t blockNo
= arg0
;
767 uint8_t keyType
= arg1
;
768 uint8_t keyCount
= arg2
;
769 uint64_t ui64Key
= 0;
776 struct Crypto1State mpcs
= {0, 0};
777 struct Crypto1State
*pcs
;
781 int OLD_MF_DBGLEVEL
= MF_DBGLEVEL
;
782 MF_DBGLEVEL
= MF_DBG_NONE
;
788 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
794 for (i
= 0; i
< keyCount
; i
++) {
795 if(mifare_classic_halt(pcs
, cuid
)) {
796 if (MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Halt error");
799 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
800 if (OLD_MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Can't select card");
804 ui64Key
= bytes_to_num(datain
+ i
* 6, 6);
805 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
813 // ----------------------------- crypto1 destroy
814 crypto1_destroy(pcs
);
817 cmd_send(CMD_ACK
,isOK
,0,0,datain
+ i
* 6,6);
820 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
823 // restore debug level
824 MF_DBGLEVEL
= OLD_MF_DBGLEVEL
;
827 //-----------------------------------------------------------------------------
828 // MIFARE commands set debug level
830 //-----------------------------------------------------------------------------
831 void MifareSetDbgLvl(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
833 Dbprintf("Debug level: %d", MF_DBGLEVEL
);
836 //-----------------------------------------------------------------------------
837 // Work with emulator memory
839 //-----------------------------------------------------------------------------
840 void MifareEMemClr(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
844 void MifareEMemSet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
845 emlSetMem(datain
, arg0
, arg1
); // data, block num, blocks count
848 void MifareEMemGet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
849 byte_t buf
[USB_CMD_DATA_SIZE
];
850 emlGetMem(buf
, arg0
, arg1
); // data, block num, blocks count (max 4)
853 cmd_send(CMD_ACK
,arg0
,arg1
,0,buf
,USB_CMD_DATA_SIZE
);
857 //-----------------------------------------------------------------------------
858 // Load a card into the emulator memory
860 //-----------------------------------------------------------------------------
861 void MifareECardLoad(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
862 uint8_t numSectors
= arg0
;
863 uint8_t keyType
= arg1
;
864 uint64_t ui64Key
= 0;
866 struct Crypto1State mpcs
= {0, 0};
867 struct Crypto1State
*pcs
;
871 byte_t dataoutbuf
[16];
872 byte_t dataoutbuf2
[16];
879 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
887 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
889 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
892 for (uint8_t sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
893 ui64Key
= emlGetKey(sectorNo
, keyType
);
895 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
897 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo
);
901 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_NESTED
)) {
903 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo
);
908 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
909 if(isOK
&& mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
)) {
911 if (MF_DBGLEVEL
>= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo
, blockNo
);
915 if (blockNo
< NumBlocksPerSector(sectorNo
) - 1) {
916 emlSetMem(dataoutbuf
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
917 } else { // sector trailer, keep the keys, set only the AC
918 emlGetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
919 memcpy(&dataoutbuf2
[6], &dataoutbuf
[6], 4);
920 emlSetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
927 if(mifare_classic_halt(pcs
, cuid
)) {
928 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
931 // ----------------------------- crypto1 destroy
932 crypto1_destroy(pcs
);
934 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
937 if (MF_DBGLEVEL
>= 2) DbpString("EMUL FILL SECTORS FINISHED");
942 //-----------------------------------------------------------------------------
943 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
945 //-----------------------------------------------------------------------------
946 void MifareCSetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
949 uint8_t needWipe
= arg0
;
950 // bit 0 - need get UID
952 // bit 2 - need HALT after sequence
953 // bit 3 - need init FPGA and field before sequence
954 // bit 4 - need reset FPGA and LED
955 uint8_t workFlags
= arg1
;
956 uint8_t blockNo
= arg2
;
959 uint8_t wupC1
[] = { 0x40 };
960 uint8_t wupC2
[] = { 0x43 };
961 uint8_t wipeC
[] = { 0x41 };
965 uint8_t uid
[10] = {0x00};
966 uint8_t d_block
[18] = {0x00};
969 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
970 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
972 // reset FPGA and LED
973 if (workFlags
& 0x08) {
980 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
986 if (workFlags
& 0x01) {
987 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
988 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
992 if(mifare_classic_halt(NULL
, cuid
)) {
993 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1000 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1001 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1002 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1006 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
1007 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1008 if (MF_DBGLEVEL
>= 1) Dbprintf("wipeC error");
1012 if(mifare_classic_halt(NULL
, cuid
)) {
1013 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1019 if (workFlags
& 0x02) {
1020 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1021 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1022 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1026 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1027 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1028 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1033 if ((mifare_sendcmd_short(NULL
, 0, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1034 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send command error");
1038 memcpy(d_block
, datain
, 16);
1039 AppendCrc14443a(d_block
, 16);
1041 ReaderTransmit(d_block
, sizeof(d_block
), NULL
);
1042 if ((ReaderReceive(receivedAnswer
, receivedAnswerPar
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1043 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send data error");
1047 if (workFlags
& 0x04) {
1048 if (mifare_classic_halt(NULL
, cuid
)) {
1049 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1059 cmd_send(CMD_ACK
,isOK
,0,0,uid
,4);
1062 if ((workFlags
& 0x10) || (!isOK
)) {
1063 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1069 void MifareCGetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1072 // bit 1 - need wupC
1073 // bit 2 - need HALT after sequence
1074 // bit 3 - need init FPGA and field before sequence
1075 // bit 4 - need reset FPGA and LED
1076 uint8_t workFlags
= arg0
;
1077 uint8_t blockNo
= arg2
;
1080 uint8_t wupC1
[] = { 0x40 };
1081 uint8_t wupC2
[] = { 0x43 };
1085 uint8_t data
[18] = {0x00};
1088 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1089 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1091 if (workFlags
& 0x08) {
1098 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1102 if (workFlags
& 0x02) {
1103 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1104 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1105 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1109 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1110 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1111 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1117 if ((mifare_sendcmd_short(NULL
, 0, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 18)) {
1118 if (MF_DBGLEVEL
>= 1) Dbprintf("read block send command error");
1121 memcpy(data
, receivedAnswer
, 18);
1123 if (workFlags
& 0x04) {
1124 if (mifare_classic_halt(NULL
, cuid
)) {
1125 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1135 cmd_send(CMD_ACK
,isOK
,0,0,data
,18);
1138 if ((workFlags
& 0x10) || (!isOK
)) {
1139 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1144 void MifareCIdent(){
1147 uint8_t wupC1
[] = { 0x40 };
1148 uint8_t wupC2
[] = { 0x43 };
1153 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1154 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1156 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1157 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1161 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1162 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1166 if (mifare_classic_halt(NULL
, 0)) {
1170 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
1177 void Mifare_DES_Auth1(uint8_t arg0
, uint8_t *datain
){
1179 byte_t dataout
[11] = {0x00};
1180 uint8_t uid
[10] = {0x00};
1184 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1186 int len
= iso14443a_select_card(uid
, NULL
, &cuid
);
1188 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
1193 if(mifare_desfire_des_auth1(cuid
, dataout
)){
1194 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication part1: Fail.");
1199 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 1 FINISHED");
1200 cmd_send(CMD_ACK
,1,cuid
,0,dataout
, sizeof(dataout
));
1203 void Mifare_DES_Auth2(uint32_t arg0
, uint8_t *datain
){
1205 uint32_t cuid
= arg0
;
1206 uint8_t key
[16] = {0x00};
1208 byte_t dataout
[12] = {0x00};
1210 memcpy(key
, datain
, 16);
1212 isOK
= mifare_desfire_des_auth2(cuid
, key
, dataout
);
1215 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
)
1216 Dbprintf("Authentication part2: Failed");
1221 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
)
1222 DbpString("AUTH 2 FINISHED");
1224 cmd_send(CMD_ACK
, isOK
, 0, 0, dataout
, sizeof(dataout
));
1225 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1231 ReaderTransmit(deselect_cmd
, 3 , NULL
);
1232 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1236 void OnError(uint8_t reason
){
1238 ReaderTransmit(deselect_cmd
, 3 , NULL
);
1239 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1240 cmd_send(CMD_ACK
,0,reason
,0,0,0);