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
)
152 uint8_t blockNo
= arg0
;
156 byte_t dataoutbuf
[16] = {0x00};
157 uint8_t uid
[10] = {0x00};
161 iso14a_clear_trace();
162 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
169 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
170 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
174 if(mifare_ultra_readblock(cuid
, blockNo
, dataoutbuf
)) {
175 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block error");
179 if(mifare_ultra_halt(cuid
)) {
180 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
188 if (MF_DBGLEVEL
>= 2) DbpString("READ BLOCK FINISHED");
191 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16);
193 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
197 //-----------------------------------------------------------------------------
198 // Select, Authenticate, Read a MIFARE tag.
199 // read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)
200 //-----------------------------------------------------------------------------
201 void MifareReadSector(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
204 uint8_t sectorNo
= arg0
;
205 uint8_t keyType
= arg1
;
206 uint64_t ui64Key
= 0;
207 ui64Key
= bytes_to_num(datain
, 6);
211 byte_t dataoutbuf
[16 * 16];
214 struct Crypto1State mpcs
= {0, 0};
215 struct Crypto1State
*pcs
;
219 iso14a_clear_trace();
221 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
228 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
230 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
234 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
236 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
239 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
240 if(mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
+ 16 * blockNo
)) {
242 if (MF_DBGLEVEL
>= 1) Dbprintf("Read sector %2d block %2d error", sectorNo
, blockNo
);
247 if(mifare_classic_halt(pcs
, cuid
)) {
248 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
251 // ----------------------------- crypto1 destroy
252 crypto1_destroy(pcs
);
254 if (MF_DBGLEVEL
>= 2) DbpString("READ SECTOR FINISHED");
257 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16*NumBlocksPerSector(sectorNo
));
261 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
265 void MifareUReadCard(uint8_t arg0
, int arg1
, uint8_t *datain
)
268 uint8_t sectorNo
= arg0
;
271 byte_t dataoutbuf
[176] = {0x00};;
272 uint8_t uid
[10] = {0x00};
275 iso14a_clear_trace();
276 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
282 if (MF_DBGLEVEL
>= MF_DBG_ALL
)
283 Dbprintf("Pages %d",Pages
);
285 if (!iso14443a_select_card(uid
, NULL
, &cuid
)) {
286 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
287 Dbprintf("Can't select card");
292 for (int i
= 0; i
< Pages
; i
++){
293 if (mifare_ultra_readblock(cuid
, sectorNo
* 4 + i
, dataoutbuf
+ 4 * i
)) {
294 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
295 Dbprintf("Read block %d error",i
);
303 if (mifare_ultra_halt(cuid
)) {
304 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
305 Dbprintf("Halt error");
310 if (MF_DBGLEVEL
>= MF_DBG_ALL
) {
311 Dbprintf("Pages read %d",count_Pages
);
312 DbpString("Read card finished");
315 int len
= 16*4; //64 bytes
318 if (Pages
== 44 && count_Pages
> 16)
321 cmd_send(CMD_ACK
, 1, 0, 0, dataoutbuf
, len
);
323 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
328 //-----------------------------------------------------------------------------
329 // Select, Authenticate, Write a MIFARE tag.
331 //-----------------------------------------------------------------------------
332 void MifareWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
335 uint8_t blockNo
= arg0
;
336 uint8_t keyType
= arg1
;
337 uint64_t ui64Key
= 0;
338 byte_t blockdata
[16];
340 ui64Key
= bytes_to_num(datain
, 6);
341 memcpy(blockdata
, datain
+ 10, 16);
347 struct Crypto1State mpcs
= {0, 0};
348 struct Crypto1State
*pcs
;
352 iso14a_clear_trace();
354 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
361 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
362 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
366 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
367 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
371 if(mifare_classic_writeblock(pcs
, cuid
, blockNo
, blockdata
)) {
372 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
376 if(mifare_classic_halt(pcs
, cuid
)) {
377 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
385 // ----------------------------- crypto1 destroy
386 crypto1_destroy(pcs
);
388 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
391 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
396 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
400 void MifareUWriteBlock(uint8_t arg0
, uint8_t *datain
)
403 uint8_t blockNo
= arg0
;
404 byte_t blockdata
[16] = {0x00};
406 memcpy(blockdata
, datain
, 16);
410 uint8_t uid
[10] = {0x00};
413 iso14a_clear_trace();
414 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
421 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
422 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
426 if(mifare_ultra_writeblock(cuid
, blockNo
, blockdata
)) {
427 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
431 if(mifare_ultra_halt(cuid
)) {
432 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
440 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
442 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
443 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
447 void MifareUWriteBlock_Special(uint8_t arg0
, uint8_t *datain
)
450 uint8_t blockNo
= arg0
;
451 byte_t blockdata
[4] = {0x00};
453 memcpy(blockdata
, datain
,4);
457 uint8_t uid
[10] = {0x00};
460 iso14a_clear_trace();
461 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
468 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
469 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
473 if(mifare_ultra_special_writeblock(cuid
, blockNo
, blockdata
)) {
474 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
478 if(mifare_ultra_halt(cuid
)) {
479 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
487 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
489 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
490 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
494 // Return 1 if the nonce is invalid else return 0
495 int valid_nonce(uint32_t Nt
, uint32_t NtEnc
, uint32_t Ks1
, uint8_t * parity
) {
496 return ((oddparity((Nt
>> 24) & 0xFF) == ((parity
[0]) ^ oddparity((NtEnc
>> 24) & 0xFF) ^ BIT(Ks1
,16))) & \
497 (oddparity((Nt
>> 16) & 0xFF) == ((parity
[1]) ^ oddparity((NtEnc
>> 16) & 0xFF) ^ BIT(Ks1
,8))) & \
498 (oddparity((Nt
>> 8) & 0xFF) == ((parity
[2]) ^ oddparity((NtEnc
>> 8) & 0xFF) ^ BIT(Ks1
,0)))) ? 1 : 0;
502 //-----------------------------------------------------------------------------
503 // MIFARE nested authentication.
505 //-----------------------------------------------------------------------------
506 void MifareNested(uint32_t arg0
, uint32_t arg1
, uint32_t calibrate
, uint8_t *datain
)
509 uint8_t blockNo
= arg0
& 0xff;
510 uint8_t keyType
= (arg0
>> 8) & 0xff;
511 uint8_t targetBlockNo
= arg1
& 0xff;
512 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
513 uint64_t ui64Key
= 0;
515 ui64Key
= bytes_to_num(datain
, 6);
518 uint16_t rtr
, i
, j
, len
;
520 static uint16_t dmin
, dmax
;
522 uint32_t cuid
, nt1
, nt2
, nttmp
, nttest
, ks1
;
524 uint32_t target_nt
[2], target_ks
[2];
526 uint8_t par_array
[4];
528 struct Crypto1State mpcs
= {0, 0};
529 struct Crypto1State
*pcs
;
531 uint8_t* receivedAnswer
= get_bigbufptr_recvrespbuf();
533 uint32_t auth1_time
, auth2_time
;
534 static uint16_t delta_time
;
537 iso14a_clear_trace();
538 iso14a_set_tracing(false);
540 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
546 // statistics on nonce distance
547 if (calibrate
) { // for first call only. Otherwise reuse previous calibration
555 for (rtr
= 0; rtr
< 17; rtr
++) {
557 // prepare next select. No need to power down the card.
558 if(mifare_classic_halt(pcs
, cuid
)) {
559 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
564 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
565 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
571 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
572 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
578 auth2_time
= auth1_time
+ delta_time
;
582 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_NESTED
, &nt2
, &auth2_time
)) {
583 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error");
588 nttmp
= prng_successor(nt1
, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160
589 for (i
= 141; i
< 1200; i
++) {
590 nttmp
= prng_successor(nttmp
, 1);
591 if (nttmp
== nt2
) break;
601 delta_time
= auth2_time
- auth1_time
+ 32; // allow some slack for proper timing
603 if (MF_DBGLEVEL
>= 3) Dbprintf("Nested: calibrating... ntdist=%d", i
);
607 if (rtr
<= 1) return;
609 davg
= (davg
+ (rtr
- 1)/2) / (rtr
- 1);
611 if (MF_DBGLEVEL
>= 3) Dbprintf("min=%d max=%d avg=%d, delta_time=%d", dmin
, dmax
, davg
, delta_time
);
619 // -------------------------------------------------------------------------------------------------
623 // get crypted nonces for target sector
624 for(i
=0; i
< 2; i
++) { // look for exactly two different nonces
628 DbpString("Nested: cancelled");
629 crypto1_destroy(pcs
);
630 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
636 while(target_nt
[i
] == 0) { // continue until we have an unambiguous nonce
638 // prepare next select. No need to power down the card.
639 if(mifare_classic_halt(pcs
, cuid
)) {
640 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
644 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
645 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
650 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
651 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
655 // nested authentication
656 auth2_time
= auth1_time
+ delta_time
;
657 len
= mifare_sendcmd_shortex(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType
& 0x01), targetBlockNo
, receivedAnswer
, par
, &auth2_time
);
659 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error len=%d", len
);
663 nt2
= bytes_to_num(receivedAnswer
, 4);
664 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i
+1, nt1
, nt2
, par
[0]);
666 // Parity validity check
667 for (j
= 0; j
< 4; j
++) {
668 par_array
[j
] = (oddparity(receivedAnswer
[j
]) != ((par
[0] >> (7-j
)) & 0x01));
672 nttest
= prng_successor(nt1
, dmin
- 1);
673 for (j
= dmin
; j
< dmax
+ 1; j
++) {
674 nttest
= prng_successor(nttest
, 1);
677 if (valid_nonce(nttest
, nt2
, ks1
, par_array
)){
678 if (ncount
> 0) { // we are only interested in disambiguous nonces, try again
679 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i
+1, j
);
683 target_nt
[i
] = nttest
;
686 if (i
== 1 && target_nt
[1] == target_nt
[0]) { // we need two different nonces
688 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j
);
691 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i
+1, j
);
694 if (target_nt
[i
] == 0 && j
== dmax
+1 && MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i
+1);
700 // ----------------------------- crypto1 destroy
701 crypto1_destroy(pcs
);
703 byte_t buf
[4 + 4 * 4];
704 memcpy(buf
, &cuid
, 4);
705 memcpy(buf
+4, &target_nt
[0], 4);
706 memcpy(buf
+8, &target_ks
[0], 4);
707 memcpy(buf
+12, &target_nt
[1], 4);
708 memcpy(buf
+16, &target_ks
[1], 4);
711 cmd_send(CMD_ACK
, 0, 2, targetBlockNo
+ (targetKeyType
* 0x100), buf
, sizeof(buf
));
714 if (MF_DBGLEVEL
>= 3) DbpString("NESTED FINISHED");
716 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
718 iso14a_set_tracing(TRUE
);
721 //-----------------------------------------------------------------------------
722 // MIFARE check keys. key count up to 85.
724 //-----------------------------------------------------------------------------
725 void MifareChkKeys(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
728 uint8_t blockNo
= arg0
;
729 uint8_t keyType
= arg1
;
730 uint8_t keyCount
= arg2
;
731 uint64_t ui64Key
= 0;
738 struct Crypto1State mpcs
= {0, 0};
739 struct Crypto1State
*pcs
;
743 int OLD_MF_DBGLEVEL
= MF_DBGLEVEL
;
744 MF_DBGLEVEL
= MF_DBG_NONE
;
747 iso14a_clear_trace();
748 iso14a_set_tracing(TRUE
);
750 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
756 for (i
= 0; i
< keyCount
; i
++) {
757 if(mifare_classic_halt(pcs
, cuid
)) {
758 if (MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Halt error");
761 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
762 if (OLD_MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Can't select card");
766 ui64Key
= bytes_to_num(datain
+ i
* 6, 6);
767 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
775 // ----------------------------- crypto1 destroy
776 crypto1_destroy(pcs
);
779 cmd_send(CMD_ACK
,isOK
,0,0,datain
+ i
* 6,6);
782 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
785 // restore debug level
786 MF_DBGLEVEL
= OLD_MF_DBGLEVEL
;
789 //-----------------------------------------------------------------------------
790 // MIFARE commands set debug level
792 //-----------------------------------------------------------------------------
793 void MifareSetDbgLvl(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
795 Dbprintf("Debug level: %d", MF_DBGLEVEL
);
798 //-----------------------------------------------------------------------------
799 // Work with emulator memory
801 //-----------------------------------------------------------------------------
802 void MifareEMemClr(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
806 void MifareEMemSet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
807 emlSetMem(datain
, arg0
, arg1
); // data, block num, blocks count
810 void MifareEMemGet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
811 byte_t buf
[USB_CMD_DATA_SIZE
];
812 emlGetMem(buf
, arg0
, arg1
); // data, block num, blocks count (max 4)
815 cmd_send(CMD_ACK
,arg0
,arg1
,0,buf
,USB_CMD_DATA_SIZE
);
819 //-----------------------------------------------------------------------------
820 // Load a card into the emulator memory
822 //-----------------------------------------------------------------------------
823 void MifareECardLoad(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
824 uint8_t numSectors
= arg0
;
825 uint8_t keyType
= arg1
;
826 uint64_t ui64Key
= 0;
828 struct Crypto1State mpcs
= {0, 0};
829 struct Crypto1State
*pcs
;
833 byte_t dataoutbuf
[16];
834 byte_t dataoutbuf2
[16];
838 iso14a_clear_trace();
839 iso14a_set_tracing(false);
841 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
849 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
851 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
854 for (uint8_t sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
855 ui64Key
= emlGetKey(sectorNo
, keyType
);
857 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
859 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo
);
863 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_NESTED
)) {
865 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo
);
870 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
871 if(isOK
&& mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
)) {
873 if (MF_DBGLEVEL
>= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo
, blockNo
);
877 if (blockNo
< NumBlocksPerSector(sectorNo
) - 1) {
878 emlSetMem(dataoutbuf
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
879 } else { // sector trailer, keep the keys, set only the AC
880 emlGetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
881 memcpy(&dataoutbuf2
[6], &dataoutbuf
[6], 4);
882 emlSetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
889 if(mifare_classic_halt(pcs
, cuid
)) {
890 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
893 // ----------------------------- crypto1 destroy
894 crypto1_destroy(pcs
);
896 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
899 if (MF_DBGLEVEL
>= 2) DbpString("EMUL FILL SECTORS FINISHED");
904 //-----------------------------------------------------------------------------
905 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
907 //-----------------------------------------------------------------------------
908 void MifareCSetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
911 uint8_t needWipe
= arg0
;
912 // bit 0 - need get UID
914 // bit 2 - need HALT after sequence
915 // bit 3 - need init FPGA and field before sequence
916 // bit 4 - need reset FPGA and LED
917 uint8_t workFlags
= arg1
;
918 uint8_t blockNo
= arg2
;
921 uint8_t wupC1
[] = { 0x40 };
922 uint8_t wupC2
[] = { 0x43 };
923 uint8_t wipeC
[] = { 0x41 };
927 uint8_t uid
[10] = {0x00};
928 uint8_t d_block
[18] = {0x00};
931 uint8_t* receivedAnswer
= get_bigbufptr_recvrespbuf();
932 uint8_t *receivedAnswerPar
= receivedAnswer
+ MAX_FRAME_SIZE
;
934 // reset FPGA and LED
935 if (workFlags
& 0x08) {
940 iso14a_clear_trace();
941 iso14a_set_tracing(TRUE
);
942 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
948 if (workFlags
& 0x01) {
949 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
950 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
954 if(mifare_classic_halt(NULL
, cuid
)) {
955 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
962 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
963 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
964 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
968 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
969 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
970 if (MF_DBGLEVEL
>= 1) Dbprintf("wipeC error");
974 if(mifare_classic_halt(NULL
, cuid
)) {
975 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
981 if (workFlags
& 0x02) {
982 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
983 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
984 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
988 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
989 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
990 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
995 if ((mifare_sendcmd_short(NULL
, 0, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 1) || (receivedAnswer
[0] != 0x0a)) {
996 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send command error");
1000 memcpy(d_block
, datain
, 16);
1001 AppendCrc14443a(d_block
, 16);
1003 ReaderTransmit(d_block
, sizeof(d_block
), NULL
);
1004 if ((ReaderReceive(receivedAnswer
, receivedAnswerPar
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1005 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send data error");
1009 if (workFlags
& 0x04) {
1010 if (mifare_classic_halt(NULL
, cuid
)) {
1011 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1021 cmd_send(CMD_ACK
,isOK
,0,0,uid
,4);
1024 if ((workFlags
& 0x10) || (!isOK
)) {
1025 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1031 void MifareCGetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1034 // bit 1 - need wupC
1035 // bit 2 - need HALT after sequence
1036 // bit 3 - need init FPGA and field before sequence
1037 // bit 4 - need reset FPGA and LED
1038 uint8_t workFlags
= arg0
;
1039 uint8_t blockNo
= arg2
;
1042 uint8_t wupC1
[] = { 0x40 };
1043 uint8_t wupC2
[] = { 0x43 };
1047 uint8_t data
[18] = {0x00};
1050 uint8_t* receivedAnswer
= get_bigbufptr_recvrespbuf();
1051 uint8_t *receivedAnswerPar
= receivedAnswer
+ MAX_FRAME_SIZE
;
1053 if (workFlags
& 0x08) {
1058 iso14a_clear_trace();
1059 iso14a_set_tracing(TRUE
);
1060 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1064 if (workFlags
& 0x02) {
1065 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1066 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1067 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1071 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1072 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1073 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1079 if ((mifare_sendcmd_short(NULL
, 0, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 18)) {
1080 if (MF_DBGLEVEL
>= 1) Dbprintf("read block send command error");
1083 memcpy(data
, receivedAnswer
, 18);
1085 if (workFlags
& 0x04) {
1086 if (mifare_classic_halt(NULL
, cuid
)) {
1087 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1097 cmd_send(CMD_ACK
,isOK
,0,0,data
,18);
1100 if ((workFlags
& 0x10) || (!isOK
)) {
1101 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1106 void MifareCIdent(){
1109 uint8_t wupC1
[] = { 0x40 };
1110 uint8_t wupC2
[] = { 0x43 };
1115 uint8_t* receivedAnswer
= get_bigbufptr_recvrespbuf();
1116 uint8_t *receivedAnswerPar
= receivedAnswer
+ MAX_FRAME_SIZE
;
1118 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1119 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1123 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1124 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1128 if (mifare_classic_halt(NULL
, 0)) {
1132 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
1139 void Mifare_DES_Auth1(uint8_t arg0
, uint8_t *datain
){
1142 byte_t dataout
[11] = {0x00};
1143 uint8_t uid
[10] = {0x00};
1146 iso14a_clear_trace();
1147 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1149 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
1150 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card, something went wrong before auth");
1153 if(mifare_desfire_des_auth1(cuid
, dataout
)){
1154 if (MF_DBGLEVEL
>= 1)
1155 Dbprintf("Authentication part1: Fail.");
1159 if (MF_DBGLEVEL
>= 2) DbpString("AUTH 1 FINISHED");
1161 cmd_send(CMD_ACK
,isOK
,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);
1177 isOK
= mifare_desfire_des_auth2(cuid
, key
, dataout
);
1180 if (MF_DBGLEVEL
>= 2)
1181 DbpString("AUTH 2 FINISHED");
1182 cmd_send(CMD_ACK
,isOK
,0,0,dataout
,sizeof(dataout
));
1185 if (MF_DBGLEVEL
>= 2)
1186 Dbprintf("Authentication part2: Failed");
1190 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);