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"
20 #include "../common/crc.h"
22 //-----------------------------------------------------------------------------
23 // Select, Authenticaate, Read an MIFARE tag.
25 //-----------------------------------------------------------------------------
26 void MifareReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
29 uint8_t blockNo
= arg0
;
30 uint8_t keyType
= arg1
;
32 ui64Key
= bytes_to_num(datain
, 6);
36 byte_t dataoutbuf
[16];
39 struct Crypto1State mpcs
= {0, 0};
40 struct Crypto1State
*pcs
;
45 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
52 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
53 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
57 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
58 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
62 if(mifare_classic_readblock(pcs
, cuid
, blockNo
, dataoutbuf
)) {
63 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block error");
67 if(mifare_classic_halt(pcs
, cuid
)) {
68 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
76 // ----------------------------- crypto1 destroy
79 if (MF_DBGLEVEL
>= 2) DbpString("READ BLOCK FINISHED");
82 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16);
85 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
90 void MifareUC_Auth1(uint8_t arg0
, uint8_t *datain
){
93 byte_t dataoutbuf
[16];
99 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
106 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
107 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card, something went wrong before auth");
113 if(mifare_ultra_auth1(cuid
, dataoutbuf
)){
114 if (MF_DBGLEVEL
>= 1) Dbprintf("Authentication part1: Fail.");
118 if (MF_DBGLEVEL
>= 2) DbpString("AUTH 1 FINISHED");
121 cmd_send(CMD_ACK
,isOK
,cuid
,0,dataoutbuf
,11);
127 void MifareUC_Auth2(uint32_t arg0
, uint8_t *datain
){
129 uint32_t cuid
= arg0
;
130 uint8_t key
[16]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
133 byte_t dataoutbuf
[16];
135 memcpy(key
, datain
, 16);
141 if(mifare_ultra_auth2(cuid
, key
, dataoutbuf
)){
142 if (MF_DBGLEVEL
>= 1) Dbprintf("Authentication part2: Fail...");
145 if (MF_DBGLEVEL
>= 2) DbpString("AUTH 2 FINISHED");
148 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,11);
151 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
155 void MifareUReadBlock(uint8_t arg0
,uint8_t *datain
)
158 uint8_t blockNo
= arg0
;
162 byte_t dataoutbuf
[16];
167 iso14a_clear_trace();
168 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
175 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
176 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
180 if(mifare_ultra_readblock(cuid
, blockNo
, dataoutbuf
)) {
181 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block error");
185 if(mifare_ultra_halt(cuid
)) {
186 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
194 if (MF_DBGLEVEL
>= 2) DbpString("READ BLOCK FINISHED");
197 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16);
199 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
203 //-----------------------------------------------------------------------------
204 // Select, Authenticate, Read a MIFARE tag.
205 // read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)
206 //-----------------------------------------------------------------------------
207 void MifareReadSector(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
210 uint8_t sectorNo
= arg0
;
211 uint8_t keyType
= arg1
;
212 uint64_t ui64Key
= 0;
213 ui64Key
= bytes_to_num(datain
, 6);
217 byte_t dataoutbuf
[16 * 16];
220 struct Crypto1State mpcs
= {0, 0};
221 struct Crypto1State
*pcs
;
225 iso14a_clear_trace();
227 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
234 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
236 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
240 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
242 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
245 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
246 if(mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
+ 16 * blockNo
)) {
248 if (MF_DBGLEVEL
>= 1) Dbprintf("Read sector %2d block %2d error", sectorNo
, blockNo
);
253 if(mifare_classic_halt(pcs
, cuid
)) {
254 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
257 // ----------------------------- crypto1 destroy
258 crypto1_destroy(pcs
);
260 if (MF_DBGLEVEL
>= 2) DbpString("READ SECTOR FINISHED");
263 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16*NumBlocksPerSector(sectorNo
));
267 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
271 void MifareUReadCard(uint8_t arg0
, int arg1
, uint8_t *datain
)
274 uint8_t sectorNo
= arg0
;
279 byte_t dataoutbuf
[44 * 4];
284 iso14a_clear_trace();
286 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
291 Dbprintf("Pages %d",Pages
);
293 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
294 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
297 for(int sec
=0;sec
<Pages
;sec
++){
298 if(mifare_ultra_readblock(cuid
, sectorNo
* 4 + sec
, dataoutbuf
+ 4 * sec
)) {
299 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block %d error",sec
);
305 if(mifare_ultra_halt(cuid
)) {
306 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
313 Dbprintf("Pages read %d",count_Pages
);
314 if (MF_DBGLEVEL
>= 2) DbpString("READ CARD FINISHED");
317 if (Pages
==16) cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,64);
318 if (Pages
==44 && count_Pages
==16) cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,64);
319 if (Pages
==44 && count_Pages
>16) cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,176);
323 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
329 //-----------------------------------------------------------------------------
330 // Select, Authenticate, Write a MIFARE tag.
332 //-----------------------------------------------------------------------------
333 void MifareWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
336 uint8_t blockNo
= arg0
;
337 uint8_t keyType
= arg1
;
338 uint64_t ui64Key
= 0;
339 byte_t blockdata
[16];
341 ui64Key
= bytes_to_num(datain
, 6);
342 memcpy(blockdata
, datain
+ 10, 16);
348 struct Crypto1State mpcs
= {0, 0};
349 struct Crypto1State
*pcs
;
353 iso14a_clear_trace();
355 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
362 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
363 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
367 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
368 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
372 if(mifare_classic_writeblock(pcs
, cuid
, blockNo
, blockdata
)) {
373 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
377 if(mifare_classic_halt(pcs
, cuid
)) {
378 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
386 // ----------------------------- crypto1 destroy
387 crypto1_destroy(pcs
);
389 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
392 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
397 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
401 void MifareUWriteBlock(uint8_t arg0
, uint8_t *datain
)
404 uint8_t blockNo
= arg0
;
405 byte_t blockdata
[16];
407 memset(blockdata
,'\0',16);
408 memcpy(blockdata
, datain
,16);
416 iso14a_clear_trace();
418 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
425 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
426 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
430 if(mifare_ultra_writeblock(cuid
, blockNo
, blockdata
)) {
431 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
435 if(mifare_ultra_halt(cuid
)) {
436 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
444 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
447 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
452 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
454 // iso14a_set_tracing(TRUE);
457 void MifareUWriteBlock_Special(uint8_t arg0
, uint8_t *datain
)
460 uint8_t blockNo
= arg0
;
463 memcpy(blockdata
, datain
,4);
471 iso14a_clear_trace();
473 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
480 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
481 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
485 if(mifare_ultra_special_writeblock(cuid
, blockNo
, blockdata
)) {
486 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
490 if(mifare_ultra_halt(cuid
)) {
491 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
499 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
502 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
506 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
510 // Return 1 if the nonce is invalid else return 0
511 int valid_nonce(uint32_t Nt
, uint32_t NtEnc
, uint32_t Ks1
, byte_t
* parity
) {
512 return ((oddparity((Nt
>> 24) & 0xFF) == ((parity
[0]) ^ oddparity((NtEnc
>> 24) & 0xFF) ^ BIT(Ks1
,16))) & \
513 (oddparity((Nt
>> 16) & 0xFF) == ((parity
[1]) ^ oddparity((NtEnc
>> 16) & 0xFF) ^ BIT(Ks1
,8))) & \
514 (oddparity((Nt
>> 8) & 0xFF) == ((parity
[2]) ^ oddparity((NtEnc
>> 8) & 0xFF) ^ BIT(Ks1
,0)))) ? 1 : 0;
518 //-----------------------------------------------------------------------------
519 // MIFARE nested authentication.
521 //-----------------------------------------------------------------------------
522 void MifareNested(uint32_t arg0
, uint32_t arg1
, uint32_t calibrate
, uint8_t *datain
)
525 uint8_t blockNo
= arg0
& 0xff;
526 uint8_t keyType
= (arg0
>> 8) & 0xff;
527 uint8_t targetBlockNo
= arg1
& 0xff;
528 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
529 uint64_t ui64Key
= 0;
531 ui64Key
= bytes_to_num(datain
, 6);
534 uint16_t rtr
, i
, j
, len
;
536 static uint16_t dmin
, dmax
;
538 uint32_t cuid
, nt1
, nt2
, nttmp
, nttest
, par
, ks1
;
539 uint32_t target_nt
[2], target_ks
[2];
541 uint8_t par_array
[4];
543 struct Crypto1State mpcs
= {0, 0};
544 struct Crypto1State
*pcs
;
546 uint8_t* receivedAnswer
= mifare_get_bigbufptr();
548 uint32_t auth1_time
, auth2_time
;
549 static uint16_t delta_time
;
552 iso14a_clear_trace();
553 iso14a_set_tracing(false);
555 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
561 // statistics on nonce distance
562 if (calibrate
) { // for first call only. Otherwise reuse previous calibration
569 for (rtr
= 0; rtr
< 17; rtr
++) {
571 // prepare next select. No need to power down the card.
572 if(mifare_classic_halt(pcs
, cuid
)) {
573 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
578 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
579 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
585 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
586 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
592 auth2_time
= auth1_time
+ delta_time
;
596 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_NESTED
, &nt2
, &auth2_time
)) {
597 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error");
602 nttmp
= prng_successor(nt1
, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160
603 for (i
= 101; i
< 1200; i
++) {
604 nttmp
= prng_successor(nttmp
, 1);
605 if (nttmp
== nt2
) break;
615 delta_time
= auth2_time
- auth1_time
+ 32; // allow some slack for proper timing
617 if (MF_DBGLEVEL
>= 3) Dbprintf("Nested: calibrating... ntdist=%d", i
);
621 if (rtr
<= 1) return;
623 davg
= (davg
+ (rtr
- 1)/2) / (rtr
- 1);
625 if (MF_DBGLEVEL
>= 3) Dbprintf("min=%d max=%d avg=%d, delta_time=%d", dmin
, dmax
, davg
, delta_time
);
633 // -------------------------------------------------------------------------------------------------
637 // get crypted nonces for target sector
638 for(i
=0; i
< 2; i
++) { // look for exactly two different nonces
641 while(target_nt
[i
] == 0) { // continue until we have an unambiguous nonce
643 // prepare next select. No need to power down the card.
644 if(mifare_classic_halt(pcs
, cuid
)) {
645 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
649 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
650 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
655 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
656 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
660 // nested authentication
661 auth2_time
= auth1_time
+ delta_time
;
662 len
= mifare_sendcmd_shortex(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType
& 0x01), targetBlockNo
, receivedAnswer
, &par
, &auth2_time
);
664 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error len=%d", len
);
668 nt2
= bytes_to_num(receivedAnswer
, 4);
669 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i
+1, nt1
, nt2
, par
);
671 // Parity validity check
672 for (j
= 0; j
< 4; j
++) {
673 par_array
[j
] = (oddparity(receivedAnswer
[j
]) != ((par
& 0x08) >> 3));
678 nttest
= prng_successor(nt1
, dmin
- 1);
679 for (j
= dmin
; j
< dmax
+ 1; j
++) {
680 nttest
= prng_successor(nttest
, 1);
683 if (valid_nonce(nttest
, nt2
, ks1
, par_array
)){
684 if (ncount
> 0) { // we are only interested in disambiguous nonces, try again
685 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i
+1, j
);
689 target_nt
[i
] = nttest
;
692 if (i
== 1 && target_nt
[1] == target_nt
[0]) { // we need two different nonces
694 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j
);
697 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i
+1, j
);
700 if (target_nt
[i
] == 0 && j
== dmax
+1 && MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i
+1);
706 // ----------------------------- crypto1 destroy
707 crypto1_destroy(pcs
);
710 memset(uid
, 0x44, 4);
711 LogTrace(uid
, 4, 0, 0, TRUE
);
713 byte_t buf
[4 + 4 * 4];
714 memcpy(buf
, &cuid
, 4);
715 memcpy(buf
+4, &target_nt
[0], 4);
716 memcpy(buf
+8, &target_ks
[0], 4);
717 memcpy(buf
+12, &target_nt
[1], 4);
718 memcpy(buf
+16, &target_ks
[1], 4);
721 cmd_send(CMD_ACK
, 0, 2, targetBlockNo
+ (targetKeyType
* 0x100), buf
, sizeof(buf
));
724 if (MF_DBGLEVEL
>= 3) DbpString("NESTED FINISHED");
726 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
728 iso14a_set_tracing(TRUE
);
731 //-----------------------------------------------------------------------------
732 // MIFARE check keys. key count up to 85.
734 //-----------------------------------------------------------------------------
735 void MifareChkKeys(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
738 uint8_t blockNo
= arg0
;
739 uint8_t keyType
= arg1
;
740 uint8_t keyCount
= arg2
;
741 uint64_t ui64Key
= 0;
748 struct Crypto1State mpcs
= {0, 0};
749 struct Crypto1State
*pcs
;
753 int OLD_MF_DBGLEVEL
= MF_DBGLEVEL
;
754 MF_DBGLEVEL
= MF_DBG_NONE
;
757 iso14a_clear_trace();
758 iso14a_set_tracing(TRUE
);
760 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
766 for (i
= 0; i
< keyCount
; i
++) {
767 if(mifare_classic_halt(pcs
, cuid
)) {
768 if (MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Halt error");
771 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
772 if (OLD_MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Can't select card");
776 ui64Key
= bytes_to_num(datain
+ i
* 6, 6);
777 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
785 // ----------------------------- crypto1 destroy
786 crypto1_destroy(pcs
);
789 cmd_send(CMD_ACK
,isOK
,0,0,datain
+ i
* 6,6);
793 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
796 // restore debug level
797 MF_DBGLEVEL
= OLD_MF_DBGLEVEL
;
800 //-----------------------------------------------------------------------------
801 // MIFARE commands set debug level
803 //-----------------------------------------------------------------------------
804 void MifareSetDbgLvl(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
806 Dbprintf("Debug level: %d", MF_DBGLEVEL
);
809 //-----------------------------------------------------------------------------
810 // Work with emulator memory
812 //-----------------------------------------------------------------------------
813 void MifareEMemClr(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
817 void MifareEMemSet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
818 emlSetMem(datain
, arg0
, arg1
); // data, block num, blocks count
821 void MifareEMemGet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
823 emlGetMem(buf
, arg0
, arg1
); // data, block num, blocks count (max 4)
826 cmd_send(CMD_ACK
,arg0
,arg1
,0,buf
,48);
830 //-----------------------------------------------------------------------------
831 // Load a card into the emulator memory
833 //-----------------------------------------------------------------------------
834 void MifareECardLoad(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
835 uint8_t numSectors
= arg0
;
836 uint8_t keyType
= arg1
;
837 uint64_t ui64Key
= 0;
839 struct Crypto1State mpcs
= {0, 0};
840 struct Crypto1State
*pcs
;
844 byte_t dataoutbuf
[16];
845 byte_t dataoutbuf2
[16];
849 iso14a_clear_trace();
850 iso14a_set_tracing(false);
852 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
860 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
862 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
865 for (uint8_t sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
866 ui64Key
= emlGetKey(sectorNo
, keyType
);
868 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
870 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo
);
874 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_NESTED
)) {
876 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo
);
881 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
882 if(isOK
&& mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
)) {
884 if (MF_DBGLEVEL
>= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo
, blockNo
);
888 if (blockNo
< NumBlocksPerSector(sectorNo
) - 1) {
889 emlSetMem(dataoutbuf
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
890 } else { // sector trailer, keep the keys, set only the AC
891 emlGetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
892 memcpy(&dataoutbuf2
[6], &dataoutbuf
[6], 4);
893 emlSetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
900 if(mifare_classic_halt(pcs
, cuid
)) {
901 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
904 // ----------------------------- crypto1 destroy
905 crypto1_destroy(pcs
);
907 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
910 if (MF_DBGLEVEL
>= 2) DbpString("EMUL FILL SECTORS FINISHED");
915 //-----------------------------------------------------------------------------
916 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
918 //-----------------------------------------------------------------------------
919 void MifareCSetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
922 uint8_t needWipe
= arg0
;
923 // bit 0 - need get UID
925 // bit 2 - need HALT after sequence
926 // bit 3 - need init FPGA and field before sequence
927 // bit 4 - need reset FPGA and LED
928 uint8_t workFlags
= arg1
;
929 uint8_t blockNo
= arg2
;
932 uint8_t wupC1
[] = { 0x40 };
933 uint8_t wupC2
[] = { 0x43 };
934 uint8_t wipeC
[] = { 0x41 };
942 memset(uid
, 0x00, 10);
943 uint8_t* receivedAnswer
= mifare_get_bigbufptr();
945 if (workFlags
& 0x08) {
947 iso14a_clear_trace();
948 iso14a_set_tracing(TRUE
);
950 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
957 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
959 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
964 if (workFlags
& 0x01) {
965 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
966 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
970 if(mifare_classic_halt(NULL
, cuid
)) {
971 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
978 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
979 if(!ReaderReceive(receivedAnswer
) || (receivedAnswer
[0] != 0x0a)) {
980 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
984 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
985 if(!ReaderReceive(receivedAnswer
) || (receivedAnswer
[0] != 0x0a)) {
986 if (MF_DBGLEVEL
>= 1) Dbprintf("wipeC error");
990 if(mifare_classic_halt(NULL
, cuid
)) {
991 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
997 if (workFlags
& 0x02) {
998 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
999 if(!ReaderReceive(receivedAnswer
) || (receivedAnswer
[0] != 0x0a)) {
1000 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1004 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1005 if(!ReaderReceive(receivedAnswer
) || (receivedAnswer
[0] != 0x0a)) {
1006 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1011 if ((mifare_sendcmd_short(NULL
, 0, 0xA0, blockNo
, receivedAnswer
, NULL
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1012 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send command error");
1016 memcpy(d_block
, datain
, 16);
1017 AppendCrc14443a(d_block
, 16);
1019 ReaderTransmit(d_block
, sizeof(d_block
), NULL
);
1020 if ((ReaderReceive(receivedAnswer
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1021 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send data error");
1025 if (workFlags
& 0x04) {
1026 if (mifare_classic_halt(NULL
, cuid
)) {
1027 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1037 cmd_send(CMD_ACK
,isOK
,0,0,uid
,4);
1040 if ((workFlags
& 0x10) || (!isOK
)) {
1042 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1048 void MifareCGetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1051 // bit 1 - need wupC
1052 // bit 2 - need HALT after sequence
1053 // bit 3 - need init FPGA and field before sequence
1054 // bit 4 - need reset FPGA and LED
1055 uint8_t workFlags
= arg0
;
1056 uint8_t blockNo
= arg2
;
1059 uint8_t wupC1
[] = { 0x40 };
1060 uint8_t wupC2
[] = { 0x43 };
1067 memset(data
, 0x00, 18);
1068 uint8_t* receivedAnswer
= mifare_get_bigbufptr();
1070 if (workFlags
& 0x08) {
1072 iso14a_clear_trace();
1073 iso14a_set_tracing(TRUE
);
1075 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1082 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1084 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1088 if (workFlags
& 0x02) {
1089 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1090 if(!ReaderReceive(receivedAnswer
) || (receivedAnswer
[0] != 0x0a)) {
1091 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1095 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1096 if(!ReaderReceive(receivedAnswer
) || (receivedAnswer
[0] != 0x0a)) {
1097 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1103 if ((mifare_sendcmd_short(NULL
, 0, 0x30, blockNo
, receivedAnswer
, NULL
) != 18)) {
1104 if (MF_DBGLEVEL
>= 1) Dbprintf("read block send command error");
1107 memcpy(data
, receivedAnswer
, 18);
1109 if (workFlags
& 0x04) {
1110 if (mifare_classic_halt(NULL
, cuid
)) {
1111 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1121 cmd_send(CMD_ACK
,isOK
,0,0,data
,18);
1124 if ((workFlags
& 0x10) || (!isOK
)) {
1126 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);