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 //-----------------------------------------------------------------------------
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};
101 iso14a_clear_trace();
102 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
104 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
105 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
106 Dbprintf("Can't select card");
111 if(mifare_ultra_auth1(cuid
, dataoutbuf
)){
112 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
113 Dbprintf("Authentication part1: Fail.");
119 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
)
120 DbpString("AUTH 1 FINISHED");
122 cmd_send(CMD_ACK
,isOK
,cuid
,0,dataoutbuf
,11);
125 void MifareUC_Auth2(uint32_t arg0
, uint8_t *datain
){
127 uint32_t cuid
= arg0
;
128 uint8_t key
[16] = {0x00};
130 byte_t dataoutbuf
[16] = {0x00};
132 memcpy(key
, datain
, 16);
138 if(mifare_ultra_auth2(cuid
, key
, dataoutbuf
)){
139 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
140 Dbprintf("Authentication part2: Fail...");
146 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
)
147 DbpString("AUTH 2 FINISHED");
149 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,11);
150 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
154 void MifareUReadBlock(uint8_t arg0
,uint8_t *datain
)
156 uint8_t blockNo
= arg0
;
157 byte_t dataout
[16] = {0x00};
158 uint8_t uid
[10] = {0x00};
165 iso14a_clear_trace();
166 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
168 int len
= iso14443a_select_card(uid
, NULL
, &cuid
);
170 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
175 len
= mifare_ultra_readblock(cuid
, blockNo
, dataout
);
177 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Read block error");
182 len
= mifare_ultra_halt(cuid
);
184 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Halt error");
189 cmd_send(CMD_ACK
,1,0,0,dataout
,16);
190 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
194 //-----------------------------------------------------------------------------
195 // Select, Authenticate, Read a MIFARE tag.
196 // read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)
197 //-----------------------------------------------------------------------------
198 void MifareReadSector(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
201 uint8_t sectorNo
= arg0
;
202 uint8_t keyType
= arg1
;
203 uint64_t ui64Key
= 0;
204 ui64Key
= bytes_to_num(datain
, 6);
208 byte_t dataoutbuf
[16 * 16];
211 struct Crypto1State mpcs
= {0, 0};
212 struct Crypto1State
*pcs
;
216 iso14a_clear_trace();
218 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
225 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
227 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
231 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
233 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
236 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
237 if(mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
+ 16 * blockNo
)) {
239 if (MF_DBGLEVEL
>= 1) Dbprintf("Read sector %2d block %2d error", sectorNo
, blockNo
);
244 if(mifare_classic_halt(pcs
, cuid
)) {
245 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
248 // ----------------------------- crypto1 destroy
249 crypto1_destroy(pcs
);
251 if (MF_DBGLEVEL
>= 2) DbpString("READ SECTOR FINISHED");
254 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16*NumBlocksPerSector(sectorNo
));
258 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
262 void MifareUReadCard(uint8_t arg0
, int arg1
, uint8_t *datain
)
265 uint8_t sectorNo
= arg0
;
268 byte_t dataout
[176] = {0x00};;
269 uint8_t uid
[10] = {0x00};
276 if (MF_DBGLEVEL
>= MF_DBG_ALL
)
277 Dbprintf("Pages %d",Pages
);
279 iso14a_clear_trace();
280 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
282 int len
= iso14443a_select_card(uid
, NULL
, &cuid
);
285 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
286 Dbprintf("Can't select card");
291 for (int i
= 0; i
< Pages
; i
++){
293 len
= mifare_ultra_readblock(cuid
, sectorNo
* 4 + i
, dataout
+ 4 * i
);
296 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
297 Dbprintf("Read block %d error",i
);
305 len
= mifare_ultra_halt(cuid
);
307 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
308 Dbprintf("Halt error");
313 if (MF_DBGLEVEL
>= MF_DBG_ALL
) {
314 Dbprintf("Pages read %d", count_Pages
);
317 len
= 16*4; //64 bytes
320 if (Pages
== 44 && count_Pages
> 16)
323 cmd_send(CMD_ACK
, 1, 0, 0, dataout
, len
);
324 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] = {0x00};
407 memcpy(blockdata
, datain
,16);
411 uint8_t uid
[10] = {0x00};
414 iso14a_clear_trace();
415 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
422 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
423 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
427 if(mifare_ultra_writeblock(cuid
, blockNo
, blockdata
)) {
428 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
432 if(mifare_ultra_halt(cuid
)) {
433 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
441 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
443 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
444 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
448 void MifareUWriteBlock_Special(uint8_t arg0
, uint8_t *datain
)
451 uint8_t blockNo
= arg0
;
452 byte_t blockdata
[4] = {0x00};
454 memcpy(blockdata
, datain
,4);
458 uint8_t uid
[10] = {0x00};
461 iso14a_clear_trace();
462 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
469 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
470 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
474 if(mifare_ultra_special_writeblock(cuid
, blockNo
, blockdata
)) {
475 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
479 if(mifare_ultra_halt(cuid
)) {
480 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
488 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
490 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
491 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
495 // Return 1 if the nonce is invalid else return 0
496 int valid_nonce(uint32_t Nt
, uint32_t NtEnc
, uint32_t Ks1
, uint8_t *parity
) {
497 return ((oddparity((Nt
>> 24) & 0xFF) == ((parity
[0]) ^ oddparity((NtEnc
>> 24) & 0xFF) ^ BIT(Ks1
,16))) & \
498 (oddparity((Nt
>> 16) & 0xFF) == ((parity
[1]) ^ oddparity((NtEnc
>> 16) & 0xFF) ^ BIT(Ks1
,8))) & \
499 (oddparity((Nt
>> 8) & 0xFF) == ((parity
[2]) ^ oddparity((NtEnc
>> 8) & 0xFF) ^ BIT(Ks1
,0)))) ? 1 : 0;
503 //-----------------------------------------------------------------------------
504 // MIFARE nested authentication.
506 //-----------------------------------------------------------------------------
507 void MifareNested(uint32_t arg0
, uint32_t arg1
, uint32_t calibrate
, uint8_t *datain
)
510 uint8_t blockNo
= arg0
& 0xff;
511 uint8_t keyType
= (arg0
>> 8) & 0xff;
512 uint8_t targetBlockNo
= arg1
& 0xff;
513 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
514 uint64_t ui64Key
= 0;
516 ui64Key
= bytes_to_num(datain
, 6);
519 uint16_t rtr
, i
, j
, len
;
521 static uint16_t dmin
, dmax
;
523 uint32_t cuid
, nt1
, nt2
, nttmp
, nttest
, ks1
;
525 uint32_t target_nt
[2], target_ks
[2];
527 uint8_t par_array
[4];
529 struct Crypto1State mpcs
= {0, 0};
530 struct Crypto1State
*pcs
;
532 uint8_t* receivedAnswer
= get_bigbufptr_recvrespbuf();
534 uint32_t auth1_time
, auth2_time
;
535 static uint16_t delta_time
;
538 iso14a_clear_trace();
539 iso14a_set_tracing(false);
541 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
547 // statistics on nonce distance
548 if (calibrate
) { // for first call only. Otherwise reuse previous calibration
556 for (rtr
= 0; rtr
< 17; rtr
++) {
558 // prepare next select. No need to power down the card.
559 if(mifare_classic_halt(pcs
, cuid
)) {
560 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
565 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
566 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
572 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
573 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
579 auth2_time
= auth1_time
+ delta_time
;
583 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_NESTED
, &nt2
, &auth2_time
)) {
584 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error");
589 nttmp
= prng_successor(nt1
, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160
590 for (i
= 101; i
< 1200; i
++) {
591 nttmp
= prng_successor(nttmp
, 1);
592 if (nttmp
== nt2
) break;
602 delta_time
= auth2_time
- auth1_time
+ 32; // allow some slack for proper timing
604 if (MF_DBGLEVEL
>= 3) Dbprintf("Nested: calibrating... ntdist=%d", i
);
608 if (rtr
<= 1) return;
610 davg
= (davg
+ (rtr
- 1)/2) / (rtr
- 1);
612 if (MF_DBGLEVEL
>= 3) Dbprintf("min=%d max=%d avg=%d, delta_time=%d", dmin
, dmax
, davg
, delta_time
);
620 // -------------------------------------------------------------------------------------------------
624 // get crypted nonces for target sector
625 for(i
=0; i
< 2; i
++) { // look for exactly two different nonces
628 while(target_nt
[i
] == 0) { // continue until we have an unambiguous nonce
630 // prepare next select. No need to power down the card.
631 if(mifare_classic_halt(pcs
, cuid
)) {
632 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
636 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
637 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
642 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
643 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
647 // nested authentication
648 auth2_time
= auth1_time
+ delta_time
;
649 len
= mifare_sendcmd_shortex(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType
& 0x01), targetBlockNo
, receivedAnswer
, par
, &auth2_time
);
651 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error len=%d", len
);
655 nt2
= bytes_to_num(receivedAnswer
, 4);
656 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i
+1, nt1
, nt2
, par
[0]);
658 // Parity validity check
659 for (j
= 0; j
< 4; j
++) {
660 par_array
[j
] = (oddparity(receivedAnswer
[j
]) != ((par
[0] >> (7-j
)) & 0x01));
664 nttest
= prng_successor(nt1
, dmin
- 1);
665 for (j
= dmin
; j
< dmax
+ 1; j
++) {
666 nttest
= prng_successor(nttest
, 1);
669 if (valid_nonce(nttest
, nt2
, ks1
, par_array
)){
670 if (ncount
> 0) { // we are only interested in disambiguous nonces, try again
671 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i
+1, j
);
675 target_nt
[i
] = nttest
;
678 if (i
== 1 && target_nt
[1] == target_nt
[0]) { // we need two different nonces
680 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j
);
683 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i
+1, j
);
686 if (target_nt
[i
] == 0 && j
== dmax
+1 && MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i
+1);
692 // ----------------------------- crypto1 destroy
693 crypto1_destroy(pcs
);
695 byte_t buf
[4 + 4 * 4];
696 memcpy(buf
, &cuid
, 4);
697 memcpy(buf
+4, &target_nt
[0], 4);
698 memcpy(buf
+8, &target_ks
[0], 4);
699 memcpy(buf
+12, &target_nt
[1], 4);
700 memcpy(buf
+16, &target_ks
[1], 4);
703 cmd_send(CMD_ACK
, 0, 2, targetBlockNo
+ (targetKeyType
* 0x100), buf
, sizeof(buf
));
706 if (MF_DBGLEVEL
>= 3) DbpString("NESTED FINISHED");
708 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
710 iso14a_set_tracing(TRUE
);
713 //-----------------------------------------------------------------------------
714 // MIFARE check keys. key count up to 85.
716 //-----------------------------------------------------------------------------
717 void MifareChkKeys(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
720 uint8_t blockNo
= arg0
;
721 uint8_t keyType
= arg1
;
722 uint8_t keyCount
= arg2
;
723 uint64_t ui64Key
= 0;
730 struct Crypto1State mpcs
= {0, 0};
731 struct Crypto1State
*pcs
;
735 int OLD_MF_DBGLEVEL
= MF_DBGLEVEL
;
736 MF_DBGLEVEL
= MF_DBG_NONE
;
739 iso14a_clear_trace();
740 iso14a_set_tracing(TRUE
);
742 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
748 for (i
= 0; i
< keyCount
; i
++) {
749 if(mifare_classic_halt(pcs
, cuid
)) {
750 if (MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Halt error");
753 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
754 if (OLD_MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Can't select card");
758 ui64Key
= bytes_to_num(datain
+ i
* 6, 6);
759 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
767 // ----------------------------- crypto1 destroy
768 crypto1_destroy(pcs
);
771 cmd_send(CMD_ACK
,isOK
,0,0,datain
+ i
* 6,6);
774 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
777 // restore debug level
778 MF_DBGLEVEL
= OLD_MF_DBGLEVEL
;
781 //-----------------------------------------------------------------------------
782 // MIFARE commands set debug level
784 //-----------------------------------------------------------------------------
785 void MifareSetDbgLvl(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
787 Dbprintf("Debug level: %d", MF_DBGLEVEL
);
790 //-----------------------------------------------------------------------------
791 // Work with emulator memory
793 //-----------------------------------------------------------------------------
794 void MifareEMemClr(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
798 void MifareEMemSet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
799 emlSetMem(datain
, arg0
, arg1
); // data, block num, blocks count
802 void MifareEMemGet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
803 byte_t buf
[USB_CMD_DATA_SIZE
];
804 emlGetMem(buf
, arg0
, arg1
); // data, block num, blocks count (max 4)
807 cmd_send(CMD_ACK
,arg0
,arg1
,0,buf
,USB_CMD_DATA_SIZE
);
811 //-----------------------------------------------------------------------------
812 // Load a card into the emulator memory
814 //-----------------------------------------------------------------------------
815 void MifareECardLoad(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
816 uint8_t numSectors
= arg0
;
817 uint8_t keyType
= arg1
;
818 uint64_t ui64Key
= 0;
820 struct Crypto1State mpcs
= {0, 0};
821 struct Crypto1State
*pcs
;
825 byte_t dataoutbuf
[16];
826 byte_t dataoutbuf2
[16];
830 iso14a_clear_trace();
831 iso14a_set_tracing(false);
833 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
841 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
843 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
846 for (uint8_t sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
847 ui64Key
= emlGetKey(sectorNo
, keyType
);
849 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
851 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo
);
855 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_NESTED
)) {
857 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo
);
862 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
863 if(isOK
&& mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
)) {
865 if (MF_DBGLEVEL
>= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo
, blockNo
);
869 if (blockNo
< NumBlocksPerSector(sectorNo
) - 1) {
870 emlSetMem(dataoutbuf
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
871 } else { // sector trailer, keep the keys, set only the AC
872 emlGetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
873 memcpy(&dataoutbuf2
[6], &dataoutbuf
[6], 4);
874 emlSetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
881 if(mifare_classic_halt(pcs
, cuid
)) {
882 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
885 // ----------------------------- crypto1 destroy
886 crypto1_destroy(pcs
);
888 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
891 if (MF_DBGLEVEL
>= 2) DbpString("EMUL FILL SECTORS FINISHED");
896 //-----------------------------------------------------------------------------
897 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
899 //-----------------------------------------------------------------------------
900 void MifareCSetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
903 uint8_t needWipe
= arg0
;
904 // bit 0 - need get UID
906 // bit 2 - need HALT after sequence
907 // bit 3 - need init FPGA and field before sequence
908 // bit 4 - need reset FPGA and LED
909 uint8_t workFlags
= arg1
;
910 uint8_t blockNo
= arg2
;
913 uint8_t wupC1
[] = { 0x40 };
914 uint8_t wupC2
[] = { 0x43 };
915 uint8_t wipeC
[] = { 0x41 };
919 uint8_t uid
[10] = {0x00};
920 uint8_t d_block
[18] = {0x00};
923 uint8_t *receivedAnswer
= get_bigbufptr_recvrespbuf();
924 uint8_t *receivedAnswerPar
= receivedAnswer
+ MAX_FRAME_SIZE
;
926 // reset FPGA and LED
927 if (workFlags
& 0x08) {
932 iso14a_clear_trace();
933 iso14a_set_tracing(TRUE
);
934 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
940 if (workFlags
& 0x01) {
941 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
942 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
946 if(mifare_classic_halt(NULL
, cuid
)) {
947 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
954 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
955 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
956 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
960 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
961 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
962 if (MF_DBGLEVEL
>= 1) Dbprintf("wipeC error");
966 if(mifare_classic_halt(NULL
, cuid
)) {
967 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
973 if (workFlags
& 0x02) {
974 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
975 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
976 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
980 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
981 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
982 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
987 if ((mifare_sendcmd_short(NULL
, 0, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 1) || (receivedAnswer
[0] != 0x0a)) {
988 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send command error");
992 memcpy(d_block
, datain
, 16);
993 AppendCrc14443a(d_block
, 16);
995 ReaderTransmit(d_block
, sizeof(d_block
), NULL
);
996 if ((ReaderReceive(receivedAnswer
, receivedAnswerPar
) != 1) || (receivedAnswer
[0] != 0x0a)) {
997 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send data error");
1001 if (workFlags
& 0x04) {
1002 if (mifare_classic_halt(NULL
, cuid
)) {
1003 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1013 cmd_send(CMD_ACK
,isOK
,0,0,uid
,4);
1016 if ((workFlags
& 0x10) || (!isOK
)) {
1017 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1023 void MifareCGetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1026 // bit 1 - need wupC
1027 // bit 2 - need HALT after sequence
1028 // bit 3 - need init FPGA and field before sequence
1029 // bit 4 - need reset FPGA and LED
1030 uint8_t workFlags
= arg0
;
1031 uint8_t blockNo
= arg2
;
1034 uint8_t wupC1
[] = { 0x40 };
1035 uint8_t wupC2
[] = { 0x43 };
1039 uint8_t data
[18] = {0x00};
1042 uint8_t* receivedAnswer
= get_bigbufptr_recvrespbuf();
1043 uint8_t *receivedAnswerPar
= receivedAnswer
+ MAX_FRAME_SIZE
;
1045 if (workFlags
& 0x08) {
1050 iso14a_clear_trace();
1051 iso14a_set_tracing(TRUE
);
1052 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1056 if (workFlags
& 0x02) {
1057 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1058 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1059 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1063 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1064 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1065 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1071 if ((mifare_sendcmd_short(NULL
, 0, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 18)) {
1072 if (MF_DBGLEVEL
>= 1) Dbprintf("read block send command error");
1075 memcpy(data
, receivedAnswer
, 18);
1077 if (workFlags
& 0x04) {
1078 if (mifare_classic_halt(NULL
, cuid
)) {
1079 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1089 cmd_send(CMD_ACK
,isOK
,0,0,data
,18);
1092 if ((workFlags
& 0x10) || (!isOK
)) {
1093 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1098 void MifareCIdent(){
1101 uint8_t wupC1
[] = { 0x40 };
1102 uint8_t wupC2
[] = { 0x43 };
1107 uint8_t* receivedAnswer
= get_bigbufptr_recvrespbuf();
1108 uint8_t *receivedAnswerPar
= receivedAnswer
+ MAX_FRAME_SIZE
;
1110 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1111 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1115 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1116 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1120 if (mifare_classic_halt(NULL
, 0)) {
1124 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
1131 void Mifare_DES_Auth1(uint8_t arg0
, uint8_t *datain
){
1133 byte_t dataout
[11] = {0x00};
1134 uint8_t uid
[10] = {0x00};
1137 iso14a_clear_trace();
1138 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1140 int len
= iso14443a_select_card(uid
, NULL
, &cuid
);
1142 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
1143 Dbprintf("Can't select card");
1148 if(mifare_desfire_des_auth1(cuid
, dataout
)){
1149 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
1150 Dbprintf("Authentication part1: Fail.");
1155 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 1 FINISHED");
1157 cmd_send(CMD_ACK
,1,cuid
,0,dataout
, sizeof(dataout
));
1160 void Mifare_DES_Auth2(uint32_t arg0
, uint8_t *datain
){
1162 uint32_t cuid
= arg0
;
1163 uint8_t key
[16] = {0x00};
1165 byte_t dataout
[12] = {0x00};
1167 memcpy(key
, datain
, 16);
1169 isOK
= mifare_desfire_des_auth2(cuid
, key
, dataout
);
1172 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
)
1173 Dbprintf("Authentication part2: Failed");
1178 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
)
1179 DbpString("AUTH 2 FINISHED");
1181 cmd_send(CMD_ACK
, isOK
, 0, 0, dataout
, sizeof(dataout
));
1182 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);