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, Authenticate, Read a 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] = {0x00};
94 uint8_t uid
[10] = {0x00};
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");
110 if(mifare_ultra_auth1(cuid
, dataoutbuf
)){
111 if (MF_DBGLEVEL
>= 1) Dbprintf("Authentication part1: Fail.");
115 if (MF_DBGLEVEL
>= 2) DbpString("AUTH 1 FINISHED");
118 cmd_send(CMD_ACK
,isOK
,cuid
,0,dataoutbuf
,11);
122 void MifareUC_Auth2(uint32_t arg0
, uint8_t *datain
){
124 uint32_t cuid
= arg0
;
125 uint8_t key
[16] = {0x00};
127 byte_t dataoutbuf
[16] = {0x00};
129 memcpy(key
, datain
, 16);
135 if(mifare_ultra_auth2(cuid
, key
, dataoutbuf
)){
136 if (MF_DBGLEVEL
>= 1) Dbprintf("Authentication part2: Fail...");
139 if (MF_DBGLEVEL
>= 2) DbpString("AUTH 2 FINISHED");
142 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,11);
145 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
149 void MifareUReadBlock(uint8_t arg0
,uint8_t *datain
)
151 uint8_t blockNo
= arg0
;
152 byte_t dataout
[16] = {0x00};
153 uint8_t uid
[10] = {0x00};
160 iso14a_clear_trace();
161 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
163 int len
= iso14443a_select_card(uid
, NULL
, &cuid
);
165 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
170 len
= mifare_ultra_readblock(cuid
, blockNo
, dataout
);
172 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Read block error");
177 len
= mifare_ultra_halt(cuid
);
179 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Halt error");
184 cmd_send(CMD_ACK
,1,0,0,dataout
,16);
185 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
189 //-----------------------------------------------------------------------------
190 // Select, Authenticate, Read a MIFARE tag.
191 // read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)
192 //-----------------------------------------------------------------------------
193 void MifareReadSector(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
196 uint8_t sectorNo
= arg0
;
197 uint8_t keyType
= arg1
;
198 uint64_t ui64Key
= 0;
199 ui64Key
= bytes_to_num(datain
, 6);
203 byte_t dataoutbuf
[16 * 16];
206 struct Crypto1State mpcs
= {0, 0};
207 struct Crypto1State
*pcs
;
211 iso14a_clear_trace();
213 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
220 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
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 void MifareUReadCard(uint8_t arg0
, int arg1
, uint8_t *datain
)
260 uint8_t sectorNo
= arg0
;
263 byte_t dataout
[176] = {0x00};;
264 uint8_t uid
[10] = {0x00};
271 if (MF_DBGLEVEL
>= MF_DBG_ALL
)
272 Dbprintf("Pages %d",Pages
);
274 iso14a_clear_trace();
275 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
277 int len
= iso14443a_select_card(uid
, NULL
, &cuid
);
280 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
281 Dbprintf("Can't select card");
286 for (int i
= 0; i
< Pages
; i
++){
288 len
= mifare_ultra_readblock(cuid
, sectorNo
* 4 + i
, dataout
+ 4 * i
);
291 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
292 Dbprintf("Read block %d error",i
);
300 len
= mifare_ultra_halt(cuid
);
302 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
303 Dbprintf("Halt error");
308 if (MF_DBGLEVEL
>= MF_DBG_ALL
) {
309 Dbprintf("Pages read %d", count_Pages
);
312 len
= 16*4; //64 bytes
315 if (Pages
== 44 && count_Pages
> 16)
318 cmd_send(CMD_ACK
, 1, 0, 0, dataout
, len
);
319 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
;
348 iso14a_clear_trace();
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
)
399 uint8_t blockNo
= arg0
;
400 byte_t blockdata
[16] = {0x00};
402 memcpy(blockdata
, datain
, 16);
406 uint8_t uid
[10] = {0x00};
409 iso14a_clear_trace();
410 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
417 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
418 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
422 if(mifare_ultra_writeblock(cuid
, blockNo
, blockdata
)) {
423 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
427 if(mifare_ultra_halt(cuid
)) {
428 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
436 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
438 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
439 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
443 void MifareUWriteBlock_Special(uint8_t arg0
, uint8_t *datain
)
446 uint8_t blockNo
= arg0
;
447 byte_t blockdata
[4] = {0x00};
449 memcpy(blockdata
, datain
,4);
453 uint8_t uid
[10] = {0x00};
456 iso14a_clear_trace();
457 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
464 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
465 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
469 if(mifare_ultra_special_writeblock(cuid
, blockNo
, blockdata
)) {
470 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
474 if(mifare_ultra_halt(cuid
)) {
475 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
483 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
485 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
486 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
490 // Return 1 if the nonce is invalid else return 0
491 int valid_nonce(uint32_t Nt
, uint32_t NtEnc
, uint32_t Ks1
, uint8_t * parity
) {
492 return ((oddparity((Nt
>> 24) & 0xFF) == ((parity
[0]) ^ oddparity((NtEnc
>> 24) & 0xFF) ^ BIT(Ks1
,16))) & \
493 (oddparity((Nt
>> 16) & 0xFF) == ((parity
[1]) ^ oddparity((NtEnc
>> 16) & 0xFF) ^ BIT(Ks1
,8))) & \
494 (oddparity((Nt
>> 8) & 0xFF) == ((parity
[2]) ^ oddparity((NtEnc
>> 8) & 0xFF) ^ BIT(Ks1
,0)))) ? 1 : 0;
498 //-----------------------------------------------------------------------------
499 // MIFARE nested authentication.
501 //-----------------------------------------------------------------------------
502 void MifareNested(uint32_t arg0
, uint32_t arg1
, uint32_t calibrate
, uint8_t *datain
)
505 uint8_t blockNo
= arg0
& 0xff;
506 uint8_t keyType
= (arg0
>> 8) & 0xff;
507 uint8_t targetBlockNo
= arg1
& 0xff;
508 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
509 uint64_t ui64Key
= 0;
511 ui64Key
= bytes_to_num(datain
, 6);
514 uint16_t rtr
, i
, j
, len
;
516 static uint16_t dmin
, dmax
;
518 uint32_t cuid
, nt1
, nt2
, nttmp
, nttest
, ks1
;
520 uint32_t target_nt
[2], target_ks
[2];
522 uint8_t par_array
[4];
524 struct Crypto1State mpcs
= {0, 0};
525 struct Crypto1State
*pcs
;
527 uint8_t* receivedAnswer
= get_bigbufptr_recvrespbuf();
529 uint32_t auth1_time
, auth2_time
;
530 static uint16_t delta_time
;
533 iso14a_clear_trace();
534 iso14a_set_tracing(false);
536 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
542 // statistics on nonce distance
543 if (calibrate
) { // for first call only. Otherwise reuse previous calibration
551 for (rtr
= 0; rtr
< 17; rtr
++) {
553 // prepare next select. No need to power down the card.
554 if(mifare_classic_halt(pcs
, cuid
)) {
555 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
560 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
561 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
567 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
568 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
574 auth2_time
= auth1_time
+ delta_time
;
578 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_NESTED
, &nt2
, &auth2_time
)) {
579 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error");
584 nttmp
= prng_successor(nt1
, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160
585 for (i
= 141; i
< 1200; i
++) {
586 nttmp
= prng_successor(nttmp
, 1);
587 if (nttmp
== nt2
) break;
597 delta_time
= auth2_time
- auth1_time
+ 32; // allow some slack for proper timing
599 if (MF_DBGLEVEL
>= 3) Dbprintf("Nested: calibrating... ntdist=%d", i
);
603 if (rtr
<= 1) return;
605 davg
= (davg
+ (rtr
- 1)/2) / (rtr
- 1);
607 if (MF_DBGLEVEL
>= 3) Dbprintf("min=%d max=%d avg=%d, delta_time=%d", dmin
, dmax
, davg
, delta_time
);
615 // -------------------------------------------------------------------------------------------------
619 // get crypted nonces for target sector
620 for(i
=0; i
< 2; i
++) { // look for exactly two different nonces
624 DbpString("Nested: cancelled");
625 crypto1_destroy(pcs
);
626 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
632 while(target_nt
[i
] == 0) { // continue until we have an unambiguous nonce
634 // prepare next select. No need to power down the card.
635 if(mifare_classic_halt(pcs
, cuid
)) {
636 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
640 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
641 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
646 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
647 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
651 // nested authentication
652 auth2_time
= auth1_time
+ delta_time
;
653 len
= mifare_sendcmd_shortex(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType
& 0x01), targetBlockNo
, receivedAnswer
, par
, &auth2_time
);
655 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error len=%d", len
);
659 nt2
= bytes_to_num(receivedAnswer
, 4);
660 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i
+1, nt1
, nt2
, par
[0]);
662 // Parity validity check
663 for (j
= 0; j
< 4; j
++) {
664 par_array
[j
] = (oddparity(receivedAnswer
[j
]) != ((par
[0] >> (7-j
)) & 0x01));
668 nttest
= prng_successor(nt1
, dmin
- 1);
669 for (j
= dmin
; j
< dmax
+ 1; j
++) {
670 nttest
= prng_successor(nttest
, 1);
673 if (valid_nonce(nttest
, nt2
, ks1
, par_array
)){
674 if (ncount
> 0) { // we are only interested in disambiguous nonces, try again
675 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i
+1, j
);
679 target_nt
[i
] = nttest
;
682 if (i
== 1 && target_nt
[1] == target_nt
[0]) { // we need two different nonces
684 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j
);
687 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i
+1, j
);
690 if (target_nt
[i
] == 0 && j
== dmax
+1 && MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i
+1);
696 // ----------------------------- crypto1 destroy
697 crypto1_destroy(pcs
);
699 byte_t buf
[4 + 4 * 4];
700 memcpy(buf
, &cuid
, 4);
701 memcpy(buf
+4, &target_nt
[0], 4);
702 memcpy(buf
+8, &target_ks
[0], 4);
703 memcpy(buf
+12, &target_nt
[1], 4);
704 memcpy(buf
+16, &target_ks
[1], 4);
707 cmd_send(CMD_ACK
, 0, 2, targetBlockNo
+ (targetKeyType
* 0x100), buf
, sizeof(buf
));
710 if (MF_DBGLEVEL
>= 3) DbpString("NESTED FINISHED");
712 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
714 iso14a_set_tracing(TRUE
);
717 //-----------------------------------------------------------------------------
718 // MIFARE check keys. key count up to 85.
720 //-----------------------------------------------------------------------------
721 void MifareChkKeys(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
724 uint8_t blockNo
= arg0
;
725 uint8_t keyType
= arg1
;
726 uint8_t keyCount
= arg2
;
727 uint64_t ui64Key
= 0;
734 struct Crypto1State mpcs
= {0, 0};
735 struct Crypto1State
*pcs
;
739 int OLD_MF_DBGLEVEL
= MF_DBGLEVEL
;
740 MF_DBGLEVEL
= MF_DBG_NONE
;
743 iso14a_clear_trace();
744 iso14a_set_tracing(TRUE
);
746 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
752 for (i
= 0; i
< keyCount
; i
++) {
753 if(mifare_classic_halt(pcs
, cuid
)) {
754 if (MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Halt error");
757 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
758 if (OLD_MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Can't select card");
762 ui64Key
= bytes_to_num(datain
+ i
* 6, 6);
763 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
771 // ----------------------------- crypto1 destroy
772 crypto1_destroy(pcs
);
775 cmd_send(CMD_ACK
,isOK
,0,0,datain
+ i
* 6,6);
778 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
781 // restore debug level
782 MF_DBGLEVEL
= OLD_MF_DBGLEVEL
;
785 //-----------------------------------------------------------------------------
786 // MIFARE commands set debug level
788 //-----------------------------------------------------------------------------
789 void MifareSetDbgLvl(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
791 Dbprintf("Debug level: %d", MF_DBGLEVEL
);
794 //-----------------------------------------------------------------------------
795 // Work with emulator memory
797 //-----------------------------------------------------------------------------
798 void MifareEMemClr(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
802 void MifareEMemSet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
803 emlSetMem(datain
, arg0
, arg1
); // data, block num, blocks count
806 void MifareEMemGet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
807 byte_t buf
[USB_CMD_DATA_SIZE
];
808 emlGetMem(buf
, arg0
, arg1
); // data, block num, blocks count (max 4)
811 cmd_send(CMD_ACK
,arg0
,arg1
,0,buf
,USB_CMD_DATA_SIZE
);
815 //-----------------------------------------------------------------------------
816 // Load a card into the emulator memory
818 //-----------------------------------------------------------------------------
819 void MifareECardLoad(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
820 uint8_t numSectors
= arg0
;
821 uint8_t keyType
= arg1
;
822 uint64_t ui64Key
= 0;
824 struct Crypto1State mpcs
= {0, 0};
825 struct Crypto1State
*pcs
;
829 byte_t dataoutbuf
[16];
830 byte_t dataoutbuf2
[16];
834 iso14a_clear_trace();
835 iso14a_set_tracing(false);
837 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
845 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
847 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
850 for (uint8_t sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
851 ui64Key
= emlGetKey(sectorNo
, keyType
);
853 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
855 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo
);
859 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_NESTED
)) {
861 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo
);
866 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
867 if(isOK
&& mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
)) {
869 if (MF_DBGLEVEL
>= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo
, blockNo
);
873 if (blockNo
< NumBlocksPerSector(sectorNo
) - 1) {
874 emlSetMem(dataoutbuf
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
875 } else { // sector trailer, keep the keys, set only the AC
876 emlGetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
877 memcpy(&dataoutbuf2
[6], &dataoutbuf
[6], 4);
878 emlSetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
885 if(mifare_classic_halt(pcs
, cuid
)) {
886 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
889 // ----------------------------- crypto1 destroy
890 crypto1_destroy(pcs
);
892 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
895 if (MF_DBGLEVEL
>= 2) DbpString("EMUL FILL SECTORS FINISHED");
900 //-----------------------------------------------------------------------------
901 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
903 //-----------------------------------------------------------------------------
904 void MifareCSetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
907 uint8_t needWipe
= arg0
;
908 // bit 0 - need get UID
910 // bit 2 - need HALT after sequence
911 // bit 3 - need init FPGA and field before sequence
912 // bit 4 - need reset FPGA and LED
913 uint8_t workFlags
= arg1
;
914 uint8_t blockNo
= arg2
;
917 uint8_t wupC1
[] = { 0x40 };
918 uint8_t wupC2
[] = { 0x43 };
919 uint8_t wipeC
[] = { 0x41 };
923 uint8_t uid
[10] = {0x00};
924 uint8_t d_block
[18] = {0x00};
927 uint8_t* receivedAnswer
= get_bigbufptr_recvrespbuf();
928 uint8_t *receivedAnswerPar
= receivedAnswer
+ MAX_FRAME_SIZE
;
930 // reset FPGA and LED
931 if (workFlags
& 0x08) {
936 iso14a_clear_trace();
937 iso14a_set_tracing(TRUE
);
938 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
944 if (workFlags
& 0x01) {
945 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
946 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
950 if(mifare_classic_halt(NULL
, cuid
)) {
951 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
958 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
959 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
960 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
964 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
965 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
966 if (MF_DBGLEVEL
>= 1) Dbprintf("wipeC error");
970 if(mifare_classic_halt(NULL
, cuid
)) {
971 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
977 if (workFlags
& 0x02) {
978 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
979 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
980 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
984 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
985 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
986 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
991 if ((mifare_sendcmd_short(NULL
, 0, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 1) || (receivedAnswer
[0] != 0x0a)) {
992 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send command error");
996 memcpy(d_block
, datain
, 16);
997 AppendCrc14443a(d_block
, 16);
999 ReaderTransmit(d_block
, sizeof(d_block
), NULL
);
1000 if ((ReaderReceive(receivedAnswer
, receivedAnswerPar
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1001 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send data error");
1005 if (workFlags
& 0x04) {
1006 if (mifare_classic_halt(NULL
, cuid
)) {
1007 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1017 cmd_send(CMD_ACK
,isOK
,0,0,uid
,4);
1020 if ((workFlags
& 0x10) || (!isOK
)) {
1021 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1027 void MifareCGetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1030 // bit 1 - need wupC
1031 // bit 2 - need HALT after sequence
1032 // bit 3 - need init FPGA and field before sequence
1033 // bit 4 - need reset FPGA and LED
1034 uint8_t workFlags
= arg0
;
1035 uint8_t blockNo
= arg2
;
1038 uint8_t wupC1
[] = { 0x40 };
1039 uint8_t wupC2
[] = { 0x43 };
1043 uint8_t data
[18] = {0x00};
1046 uint8_t* receivedAnswer
= get_bigbufptr_recvrespbuf();
1047 uint8_t *receivedAnswerPar
= receivedAnswer
+ MAX_FRAME_SIZE
;
1049 if (workFlags
& 0x08) {
1054 iso14a_clear_trace();
1055 iso14a_set_tracing(TRUE
);
1056 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1060 if (workFlags
& 0x02) {
1061 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1062 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1063 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1067 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1068 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1069 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1075 if ((mifare_sendcmd_short(NULL
, 0, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 18)) {
1076 if (MF_DBGLEVEL
>= 1) Dbprintf("read block send command error");
1079 memcpy(data
, receivedAnswer
, 18);
1081 if (workFlags
& 0x04) {
1082 if (mifare_classic_halt(NULL
, cuid
)) {
1083 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1093 cmd_send(CMD_ACK
,isOK
,0,0,data
,18);
1096 if ((workFlags
& 0x10) || (!isOK
)) {
1097 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1102 void MifareCIdent(){
1105 uint8_t wupC1
[] = { 0x40 };
1106 uint8_t wupC2
[] = { 0x43 };
1111 uint8_t* receivedAnswer
= get_bigbufptr_recvrespbuf();
1112 uint8_t *receivedAnswerPar
= receivedAnswer
+ MAX_FRAME_SIZE
;
1114 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1115 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1119 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1120 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1124 if (mifare_classic_halt(NULL
, 0)) {
1128 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
1135 void Mifare_DES_Auth1(uint8_t arg0
, uint8_t *datain
){
1137 byte_t dataout
[11] = {0x00};
1138 uint8_t uid
[10] = {0x00};
1141 iso14a_clear_trace();
1142 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1144 int len
= iso14443a_select_card(uid
, NULL
, &cuid
);
1146 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
1147 Dbprintf("Can't select card");
1152 if(mifare_desfire_des_auth1(cuid
, dataout
)){
1153 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
1154 Dbprintf("Authentication part1: Fail.");
1159 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 1 FINISHED");
1161 cmd_send(CMD_ACK
,1,cuid
,0,dataout
, sizeof(dataout
));
1164 void Mifare_DES_Auth2(uint32_t arg0
, uint8_t *datain
){
1166 uint32_t cuid
= arg0
;
1167 uint8_t key
[16] = {0x00};
1169 byte_t dataout
[12] = {0x00};
1171 memcpy(key
, datain
, 16);
1173 isOK
= mifare_desfire_des_auth2(cuid
, key
, dataout
);
1176 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
)
1177 Dbprintf("Authentication part2: Failed");
1182 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
)
1183 DbpString("AUTH 2 FINISHED");
1185 cmd_send(CMD_ACK
, isOK
, 0, 0, dataout
, sizeof(dataout
));
1186 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);