1 //-----------------------------------------------------------------------------
2 // Merlok - June 2011, 2012
3 // Gerhard de Koning Gans - May 2008
4 // Hagen Fritsch - June 2010
5 // Midnitesnake - Dec 2013
6 // Andy Davies - Apr 2014
9 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
10 // at your option, any later version. See the LICENSE.txt file for the text of
12 //-----------------------------------------------------------------------------
13 // Routines to support ISO 14443 type A.
14 //-----------------------------------------------------------------------------
16 #include "mifarecmd.h"
22 // the block number for the ISO14443-4 PCB
23 uint8_t pcb_blocknum
= 0;
24 // Deselect card by sending a s-block. the crc is precalced for speed
25 static uint8_t deselect_cmd
[] = {0xc2,0xe0,0xb4};
28 //-----------------------------------------------------------------------------
29 // Select, Authenticate, Read a MIFARE tag.
31 //-----------------------------------------------------------------------------
32 void MifareReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
35 uint8_t blockNo
= arg0
;
36 uint8_t keyType
= arg1
;
38 ui64Key
= bytes_to_num(datain
, 6);
42 byte_t dataoutbuf
[16];
45 struct Crypto1State mpcs
= {0, 0};
46 struct Crypto1State
*pcs
;
51 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
58 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
59 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
63 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
64 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
68 if(mifare_classic_readblock(pcs
, cuid
, blockNo
, dataoutbuf
)) {
69 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block error");
73 if(mifare_classic_halt(pcs
, cuid
)) {
74 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
82 // ----------------------------- crypto1 destroy
85 if (MF_DBGLEVEL
>= 2) DbpString("READ BLOCK FINISHED");
88 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16);
91 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
95 void MifareUC_Auth(uint8_t arg0
, uint8_t *keybytes
){
97 bool turnOffField
= (arg0
== 1);
99 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
101 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
103 if(!iso14443a_select_card(NULL
, NULL
, NULL
)) {
104 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
109 if(!mifare_ultra_auth(keybytes
)){
110 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication failed");
116 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
119 cmd_send(CMD_ACK
,1,0,0,0,0);
123 // Arg1 = UsePwd bool
124 // datain = PWD bytes,
125 void MifareUReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
)
127 uint8_t blockNo
= arg0
;
128 byte_t dataout
[16] = {0x00};
129 bool useKey
= (arg1
== 1); //UL_C
130 bool usePwd
= (arg1
== 2); //UL_EV1/NTAG
135 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
137 int len
= iso14443a_select_card(NULL
, NULL
, NULL
);
139 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%02X)",len
);
144 // UL-C authentication
146 uint8_t key
[16] = {0x00};
147 memcpy(key
, datain
, sizeof(key
) );
149 if ( !mifare_ultra_auth(key
) ) {
155 // UL-EV1 / NTAG authentication
157 uint8_t pwd
[4] = {0x00};
158 memcpy(pwd
, datain
, 4);
159 uint8_t pack
[4] = {0,0,0,0};
160 if (!mifare_ul_ev1_auth(pwd
, pack
)) {
166 if( mifare_ultra_readblock(blockNo
, dataout
) ) {
167 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Read block error");
172 if( mifare_ultra_halt() ) {
173 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Halt error");
178 cmd_send(CMD_ACK
,1,0,0,dataout
,16);
179 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
183 //-----------------------------------------------------------------------------
184 // Select, Authenticate, Read a MIFARE tag.
185 // read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)
186 //-----------------------------------------------------------------------------
187 void MifareReadSector(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
190 uint8_t sectorNo
= arg0
;
191 uint8_t keyType
= arg1
;
192 uint64_t ui64Key
= 0;
193 ui64Key
= bytes_to_num(datain
, 6);
197 byte_t dataoutbuf
[16 * 16];
200 struct Crypto1State mpcs
= {0, 0};
201 struct Crypto1State
*pcs
;
207 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
214 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
216 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
220 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
222 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
225 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
226 if(mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
+ 16 * blockNo
)) {
228 if (MF_DBGLEVEL
>= 1) Dbprintf("Read sector %2d block %2d error", sectorNo
, blockNo
);
233 if(mifare_classic_halt(pcs
, cuid
)) {
234 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
237 // ----------------------------- crypto1 destroy
238 crypto1_destroy(pcs
);
240 if (MF_DBGLEVEL
>= 2) DbpString("READ SECTOR FINISHED");
243 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16*NumBlocksPerSector(sectorNo
));
247 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
251 void MifareUReadCard(uint8_t arg0
, uint16_t arg1
, uint8_t arg2
, uint8_t *datain
)
254 uint8_t blockNo
= arg0
;
255 uint16_t blocks
= arg1
;
256 bool useKey
= (arg2
== 1); //UL_C
257 bool usePwd
= (arg2
== 2); //UL_EV1/NTAG
258 uint32_t countblocks
= 0;
259 uint8_t *dataout
= BigBuf_get_addr();
264 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
266 int len
= iso14443a_select_card(NULL
, NULL
, NULL
);
268 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%d)",len
);
273 // UL-C authentication
275 uint8_t key
[16] = {0x00};
276 memcpy(key
, datain
, sizeof(key
) );
278 if ( !mifare_ultra_auth(key
) ) {
284 // UL-EV1 / NTAG authentication
286 uint8_t pwd
[4] = {0x00};
287 memcpy(pwd
, datain
, sizeof(pwd
));
288 uint8_t pack
[4] = {0,0,0,0};
290 if (!mifare_ul_ev1_auth(pwd
, pack
)){
296 for (int i
= 0; i
< blocks
; i
++){
297 if ((i
*4) + 4 > BigBuf_get_traceLen()) {
298 Dbprintf("Data exceeds buffer!!");
302 len
= mifare_ultra_readblock(blockNo
+ i
, dataout
+ 4 * i
);
305 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Read block %d error",i
);
306 // if no blocks read - error out
311 //stop at last successful read block and return what we got
319 len
= mifare_ultra_halt();
321 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Halt error");
326 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) Dbprintf("Blocks read %d", countblocks
);
329 cmd_send(CMD_ACK
, 1, countblocks
, countblocks
, 0, 0);
330 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
334 //-----------------------------------------------------------------------------
335 // Select, Authenticate, Write a MIFARE tag.
337 //-----------------------------------------------------------------------------
338 void MifareWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
341 uint8_t blockNo
= arg0
;
342 uint8_t keyType
= arg1
;
343 uint64_t ui64Key
= 0;
344 byte_t blockdata
[16];
346 ui64Key
= bytes_to_num(datain
, 6);
347 memcpy(blockdata
, datain
+ 10, 16);
353 struct Crypto1State mpcs
= {0, 0};
354 struct Crypto1State
*pcs
;
360 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
367 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
368 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
372 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
373 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
377 if(mifare_classic_writeblock(pcs
, cuid
, blockNo
, blockdata
)) {
378 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
382 if(mifare_classic_halt(pcs
, cuid
)) {
383 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
391 // ----------------------------- crypto1 destroy
392 crypto1_destroy(pcs
);
394 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
397 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
402 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
406 void MifareUWriteBlock(uint8_t arg0
, uint8_t *datain
)
408 uint8_t blockNo
= arg0
;
409 byte_t blockdata
[16] = {0x00};
411 memcpy(blockdata
, datain
, 16);
413 uint8_t uid
[10] = {0x00};
415 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
418 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
420 if(!iso14443a_select_card(uid
, NULL
, NULL
)) {
421 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
426 if(mifare_ultra_writeblock(blockNo
, blockdata
)) {
427 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
431 if(mifare_ultra_halt()) {
432 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
437 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
439 cmd_send(CMD_ACK
,1,0,0,0,0);
440 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
444 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);
454 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
456 if(!iso14443a_select_card(NULL
, NULL
, NULL
)) {
457 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
462 if(mifare_ultra_special_writeblock(blockNo
, blockdata
)) {
463 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
468 if(mifare_ultra_halt()) {
469 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
474 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
476 cmd_send(CMD_ACK
,1,0,0,0,0);
477 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
481 void MifareUSetPwd(uint8_t arg0
, uint8_t *datain
){
483 uint8_t pwd
[16] = {0x00};
484 byte_t blockdata
[4] = {0x00};
486 memcpy(pwd
, datain
, 16);
488 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
490 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
492 if(!iso14443a_select_card(NULL
, NULL
, NULL
)) {
493 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
498 blockdata
[0] = pwd
[7];
499 blockdata
[1] = pwd
[6];
500 blockdata
[2] = pwd
[5];
501 blockdata
[3] = pwd
[4];
502 if(mifare_ultra_special_writeblock( 44, blockdata
)) {
503 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
508 blockdata
[0] = pwd
[3];
509 blockdata
[1] = pwd
[2];
510 blockdata
[2] = pwd
[1];
511 blockdata
[3] = pwd
[0];
512 if(mifare_ultra_special_writeblock( 45, blockdata
)) {
513 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
518 blockdata
[0] = pwd
[15];
519 blockdata
[1] = pwd
[14];
520 blockdata
[2] = pwd
[13];
521 blockdata
[3] = pwd
[12];
522 if(mifare_ultra_special_writeblock( 46, blockdata
)) {
523 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
528 blockdata
[0] = pwd
[11];
529 blockdata
[1] = pwd
[10];
530 blockdata
[2] = pwd
[9];
531 blockdata
[3] = pwd
[8];
532 if(mifare_ultra_special_writeblock( 47, blockdata
)) {
533 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
538 if(mifare_ultra_halt()) {
539 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
544 cmd_send(CMD_ACK
,1,0,0,0,0);
545 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
549 // Return 1 if the nonce is invalid else return 0
550 int valid_nonce(uint32_t Nt
, uint32_t NtEnc
, uint32_t Ks1
, uint8_t *parity
) {
551 return ((oddparity((Nt
>> 24) & 0xFF) == ((parity
[0]) ^ oddparity((NtEnc
>> 24) & 0xFF) ^ BIT(Ks1
,16))) & \
552 (oddparity((Nt
>> 16) & 0xFF) == ((parity
[1]) ^ oddparity((NtEnc
>> 16) & 0xFF) ^ BIT(Ks1
,8))) & \
553 (oddparity((Nt
>> 8) & 0xFF) == ((parity
[2]) ^ oddparity((NtEnc
>> 8) & 0xFF) ^ BIT(Ks1
,0)))) ? 1 : 0;
557 //-----------------------------------------------------------------------------
558 // MIFARE nested authentication.
560 //-----------------------------------------------------------------------------
561 void MifareNested(uint32_t arg0
, uint32_t arg1
, uint32_t calibrate
, uint8_t *datain
)
564 uint8_t blockNo
= arg0
& 0xff;
565 uint8_t keyType
= (arg0
>> 8) & 0xff;
566 uint8_t targetBlockNo
= arg1
& 0xff;
567 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
568 uint64_t ui64Key
= 0;
570 ui64Key
= bytes_to_num(datain
, 6);
573 uint16_t rtr
, i
, j
, len
;
575 static uint16_t dmin
, dmax
;
577 uint32_t cuid
, nt1
, nt2
, nttmp
, nttest
, ks1
;
579 uint32_t target_nt
[2], target_ks
[2];
581 uint8_t par_array
[4];
583 struct Crypto1State mpcs
= {0, 0};
584 struct Crypto1State
*pcs
;
586 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
588 uint32_t auth1_time
, auth2_time
;
589 static uint16_t delta_time
;
591 // free eventually allocated BigBuf memory
597 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
603 // statistics on nonce distance
604 if (calibrate
) { // for first call only. Otherwise reuse previous calibration
612 for (rtr
= 0; rtr
< 17; rtr
++) {
614 // prepare next select. No need to power down the card.
615 if(mifare_classic_halt(pcs
, cuid
)) {
616 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
621 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
622 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
628 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
629 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
635 auth2_time
= auth1_time
+ delta_time
;
639 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_NESTED
, &nt2
, &auth2_time
)) {
640 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error");
645 nttmp
= prng_successor(nt1
, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160
646 for (i
= 101; i
< 1200; i
++) {
647 nttmp
= prng_successor(nttmp
, 1);
648 if (nttmp
== nt2
) break;
658 delta_time
= auth2_time
- auth1_time
+ 32; // allow some slack for proper timing
660 if (MF_DBGLEVEL
>= 3) Dbprintf("Nested: calibrating... ntdist=%d", i
);
664 if (rtr
<= 1) return;
666 davg
= (davg
+ (rtr
- 1)/2) / (rtr
- 1);
668 if (MF_DBGLEVEL
>= 3) Dbprintf("min=%d max=%d avg=%d, delta_time=%d", dmin
, dmax
, davg
, delta_time
);
676 // -------------------------------------------------------------------------------------------------
680 // get crypted nonces for target sector
681 for(i
=0; i
< 2; i
++) { // look for exactly two different nonces
684 while(target_nt
[i
] == 0) { // continue until we have an unambiguous nonce
686 // prepare next select. No need to power down the card.
687 if(mifare_classic_halt(pcs
, cuid
)) {
688 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
692 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
693 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
698 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
699 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
703 // nested authentication
704 auth2_time
= auth1_time
+ delta_time
;
705 len
= mifare_sendcmd_shortex(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType
& 0x01), targetBlockNo
, receivedAnswer
, par
, &auth2_time
);
707 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error len=%d", len
);
711 nt2
= bytes_to_num(receivedAnswer
, 4);
712 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i
+1, nt1
, nt2
, par
[0]);
714 // Parity validity check
715 for (j
= 0; j
< 4; j
++) {
716 par_array
[j
] = (oddparity(receivedAnswer
[j
]) != ((par
[0] >> (7-j
)) & 0x01));
720 nttest
= prng_successor(nt1
, dmin
- 1);
721 for (j
= dmin
; j
< dmax
+ 1; j
++) {
722 nttest
= prng_successor(nttest
, 1);
725 if (valid_nonce(nttest
, nt2
, ks1
, par_array
)){
726 if (ncount
> 0) { // we are only interested in disambiguous nonces, try again
727 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i
+1, j
);
731 target_nt
[i
] = nttest
;
734 if (i
== 1 && target_nt
[1] == target_nt
[0]) { // we need two different nonces
736 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j
);
739 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i
+1, j
);
742 if (target_nt
[i
] == 0 && j
== dmax
+1 && MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i
+1);
748 // ----------------------------- crypto1 destroy
749 crypto1_destroy(pcs
);
751 byte_t buf
[4 + 4 * 4];
752 memcpy(buf
, &cuid
, 4);
753 memcpy(buf
+4, &target_nt
[0], 4);
754 memcpy(buf
+8, &target_ks
[0], 4);
755 memcpy(buf
+12, &target_nt
[1], 4);
756 memcpy(buf
+16, &target_ks
[1], 4);
759 cmd_send(CMD_ACK
, 0, 2, targetBlockNo
+ (targetKeyType
* 0x100), buf
, sizeof(buf
));
762 if (MF_DBGLEVEL
>= 3) DbpString("NESTED FINISHED");
764 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
769 //-----------------------------------------------------------------------------
770 // MIFARE check keys. key count up to 85.
772 //-----------------------------------------------------------------------------
773 void MifareChkKeys(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
776 uint8_t blockNo
= arg0
;
777 uint8_t keyType
= arg1
;
778 uint8_t keyCount
= arg2
;
779 uint64_t ui64Key
= 0;
786 struct Crypto1State mpcs
= {0, 0};
787 struct Crypto1State
*pcs
;
791 int OLD_MF_DBGLEVEL
= MF_DBGLEVEL
;
792 MF_DBGLEVEL
= MF_DBG_NONE
;
798 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
804 for (i
= 0; i
< keyCount
; i
++) {
805 if(mifare_classic_halt(pcs
, cuid
)) {
806 if (MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Halt error");
809 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
810 if (OLD_MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Can't select card");
814 ui64Key
= bytes_to_num(datain
+ i
* 6, 6);
815 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
823 // ----------------------------- crypto1 destroy
824 crypto1_destroy(pcs
);
827 cmd_send(CMD_ACK
,isOK
,0,0,datain
+ i
* 6,6);
830 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
833 // restore debug level
834 MF_DBGLEVEL
= OLD_MF_DBGLEVEL
;
837 //-----------------------------------------------------------------------------
838 // MIFARE commands set debug level
840 //-----------------------------------------------------------------------------
841 void MifareSetDbgLvl(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
843 Dbprintf("Debug level: %d", MF_DBGLEVEL
);
846 //-----------------------------------------------------------------------------
847 // Work with emulator memory
849 //-----------------------------------------------------------------------------
850 void MifareEMemClr(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
854 void MifareEMemSet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
855 emlSetMem(datain
, arg0
, arg1
); // data, block num, blocks count
858 void MifareEMemGet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
859 byte_t buf
[USB_CMD_DATA_SIZE
];
860 emlGetMem(buf
, arg0
, arg1
); // data, block num, blocks count (max 4)
863 cmd_send(CMD_ACK
,arg0
,arg1
,0,buf
,USB_CMD_DATA_SIZE
);
867 //-----------------------------------------------------------------------------
868 // Load a card into the emulator memory
870 //-----------------------------------------------------------------------------
871 void MifareECardLoad(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
872 uint8_t numSectors
= arg0
;
873 uint8_t keyType
= arg1
;
874 uint64_t ui64Key
= 0;
876 struct Crypto1State mpcs
= {0, 0};
877 struct Crypto1State
*pcs
;
881 byte_t dataoutbuf
[16];
882 byte_t dataoutbuf2
[16];
889 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
897 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
899 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
902 for (uint8_t sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
903 ui64Key
= emlGetKey(sectorNo
, keyType
);
905 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
907 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo
);
911 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_NESTED
)) {
913 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo
);
918 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
919 if(isOK
&& mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
)) {
921 if (MF_DBGLEVEL
>= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo
, blockNo
);
925 if (blockNo
< NumBlocksPerSector(sectorNo
) - 1) {
926 emlSetMem(dataoutbuf
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
927 } else { // sector trailer, keep the keys, set only the AC
928 emlGetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
929 memcpy(&dataoutbuf2
[6], &dataoutbuf
[6], 4);
930 emlSetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
937 if(mifare_classic_halt(pcs
, cuid
)) {
938 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
941 // ----------------------------- crypto1 destroy
942 crypto1_destroy(pcs
);
944 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
947 if (MF_DBGLEVEL
>= 2) DbpString("EMUL FILL SECTORS FINISHED");
952 //-----------------------------------------------------------------------------
953 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
955 //-----------------------------------------------------------------------------
956 void MifareCSetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
959 uint8_t needWipe
= arg0
;
960 // bit 0 - need get UID
962 // bit 2 - need HALT after sequence
963 // bit 3 - need init FPGA and field before sequence
964 // bit 4 - need reset FPGA and LED
965 uint8_t workFlags
= arg1
;
966 uint8_t blockNo
= arg2
;
969 uint8_t wupC1
[] = { 0x40 };
970 uint8_t wupC2
[] = { 0x43 };
971 uint8_t wipeC
[] = { 0x41 };
975 uint8_t uid
[10] = {0x00};
976 uint8_t d_block
[18] = {0x00};
979 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
980 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
982 // reset FPGA and LED
983 if (workFlags
& 0x08) {
990 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
996 if (workFlags
& 0x01) {
997 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
998 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1002 if(mifare_classic_halt(NULL
, cuid
)) {
1003 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1010 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1011 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1012 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1016 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
1017 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1018 if (MF_DBGLEVEL
>= 1) Dbprintf("wipeC error");
1022 if(mifare_classic_halt(NULL
, cuid
)) {
1023 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1029 if (workFlags
& 0x02) {
1030 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1031 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1032 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1036 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1037 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1038 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1043 if ((mifare_sendcmd_short(NULL
, 0, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1044 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send command error");
1048 memcpy(d_block
, datain
, 16);
1049 AppendCrc14443a(d_block
, 16);
1051 ReaderTransmit(d_block
, sizeof(d_block
), NULL
);
1052 if ((ReaderReceive(receivedAnswer
, receivedAnswerPar
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1053 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send data error");
1057 if (workFlags
& 0x04) {
1058 if (mifare_classic_halt(NULL
, cuid
)) {
1059 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1069 cmd_send(CMD_ACK
,isOK
,0,0,uid
,4);
1072 if ((workFlags
& 0x10) || (!isOK
)) {
1073 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1079 void MifareCGetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1082 // bit 1 - need wupC
1083 // bit 2 - need HALT after sequence
1084 // bit 3 - need init FPGA and field before sequence
1085 // bit 4 - need reset FPGA and LED
1086 uint8_t workFlags
= arg0
;
1087 uint8_t blockNo
= arg2
;
1090 uint8_t wupC1
[] = { 0x40 };
1091 uint8_t wupC2
[] = { 0x43 };
1095 uint8_t data
[18] = {0x00};
1098 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1099 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1101 if (workFlags
& 0x08) {
1108 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1112 if (workFlags
& 0x02) {
1113 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1114 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1115 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1119 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1120 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1121 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1127 if ((mifare_sendcmd_short(NULL
, 0, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 18)) {
1128 if (MF_DBGLEVEL
>= 1) Dbprintf("read block send command error");
1131 memcpy(data
, receivedAnswer
, 18);
1133 if (workFlags
& 0x04) {
1134 if (mifare_classic_halt(NULL
, cuid
)) {
1135 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1145 cmd_send(CMD_ACK
,isOK
,0,0,data
,18);
1148 if ((workFlags
& 0x10) || (!isOK
)) {
1149 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1154 void MifareCIdent(){
1157 uint8_t wupC1
[] = { 0x40 };
1158 uint8_t wupC2
[] = { 0x43 };
1163 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1164 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1166 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1167 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1171 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1172 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1176 if (mifare_classic_halt(NULL
, 0)) {
1180 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
1187 void Mifare_DES_Auth1(uint8_t arg0
, uint8_t *datain
){
1189 byte_t dataout
[11] = {0x00};
1190 uint8_t uid
[10] = {0x00};
1194 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1196 int len
= iso14443a_select_card(uid
, NULL
, &cuid
);
1198 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
1203 if(mifare_desfire_des_auth1(cuid
, dataout
)){
1204 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication part1: Fail.");
1209 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 1 FINISHED");
1210 cmd_send(CMD_ACK
,1,cuid
,0,dataout
, sizeof(dataout
));
1213 void Mifare_DES_Auth2(uint32_t arg0
, uint8_t *datain
){
1215 uint32_t cuid
= arg0
;
1216 uint8_t key
[16] = {0x00};
1218 byte_t dataout
[12] = {0x00};
1220 memcpy(key
, datain
, 16);
1222 isOK
= mifare_desfire_des_auth2(cuid
, key
, dataout
);
1225 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
)
1226 Dbprintf("Authentication part2: Failed");
1231 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
)
1232 DbpString("AUTH 2 FINISHED");
1234 cmd_send(CMD_ACK
, isOK
, 0, 0, dataout
, sizeof(dataout
));
1235 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1241 ReaderTransmit(deselect_cmd
, 3 , NULL
);
1242 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1246 void OnError(uint8_t reason
){
1248 ReaderTransmit(deselect_cmd
, 3 , NULL
);
1249 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1250 cmd_send(CMD_ACK
,0,reason
,0,0,0);