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
)
253 // free eventually allocated BigBuf memory
259 uint8_t blockNo
= arg0
;
260 uint16_t blocks
= arg1
;
261 bool useKey
= (arg2
== 1); //UL_C
262 bool usePwd
= (arg2
== 2); //UL_EV1/NTAG
263 uint32_t countblocks
= 0;
264 uint8_t *dataout
= BigBuf_malloc(CARD_MEMORY_SIZE
);
265 if (dataout
== NULL
){
266 Dbprintf("out of memory");
273 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
275 int len
= iso14443a_select_card(NULL
, NULL
, NULL
);
277 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%d)",len
);
282 // UL-C authentication
284 uint8_t key
[16] = {0x00};
285 memcpy(key
, datain
, sizeof(key
) );
287 if ( !mifare_ultra_auth(key
) ) {
293 // UL-EV1 / NTAG authentication
295 uint8_t pwd
[4] = {0x00};
296 memcpy(pwd
, datain
, sizeof(pwd
));
297 uint8_t pack
[4] = {0,0,0,0};
299 if (!mifare_ul_ev1_auth(pwd
, pack
)){
305 for (int i
= 0; i
< blocks
; i
++){
306 if ((i
*4) + 4 > CARD_MEMORY_SIZE
) {
307 Dbprintf("Data exceeds buffer!!");
311 len
= mifare_ultra_readblock(blockNo
+ i
, dataout
+ 4 * i
);
314 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Read block %d error",i
);
315 // if no blocks read - error out
320 //stop at last successful read block and return what we got
328 len
= mifare_ultra_halt();
330 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Halt error");
335 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) Dbprintf("Blocks read %d", countblocks
);
338 cmd_send(CMD_ACK
, 1, countblocks
, countblocks
, 0, 0);
339 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
343 //-----------------------------------------------------------------------------
344 // Select, Authenticate, Write a MIFARE tag.
346 //-----------------------------------------------------------------------------
347 void MifareWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
350 uint8_t blockNo
= arg0
;
351 uint8_t keyType
= arg1
;
352 uint64_t ui64Key
= 0;
353 byte_t blockdata
[16];
355 ui64Key
= bytes_to_num(datain
, 6);
356 memcpy(blockdata
, datain
+ 10, 16);
362 struct Crypto1State mpcs
= {0, 0};
363 struct Crypto1State
*pcs
;
369 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
376 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
377 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
381 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
382 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
386 if(mifare_classic_writeblock(pcs
, cuid
, blockNo
, blockdata
)) {
387 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
391 if(mifare_classic_halt(pcs
, cuid
)) {
392 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
400 // ----------------------------- crypto1 destroy
401 crypto1_destroy(pcs
);
403 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
406 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
411 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
415 void MifareUWriteBlock(uint8_t arg0
, uint8_t *datain
)
417 uint8_t blockNo
= arg0
;
418 byte_t blockdata
[16] = {0x00};
420 memcpy(blockdata
, datain
, 16);
422 uint8_t uid
[10] = {0x00};
424 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
427 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
429 if(!iso14443a_select_card(uid
, NULL
, NULL
)) {
430 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
435 if(mifare_ultra_writeblock(blockNo
, blockdata
)) {
436 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
440 if(mifare_ultra_halt()) {
441 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
446 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
448 cmd_send(CMD_ACK
,1,0,0,0,0);
449 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
453 void MifareUWriteBlock_Special(uint8_t arg0
, uint8_t *datain
)
455 uint8_t blockNo
= arg0
;
456 byte_t blockdata
[4] = {0x00};
458 memcpy(blockdata
, datain
,4);
463 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
465 if(!iso14443a_select_card(NULL
, NULL
, NULL
)) {
466 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
471 if(mifare_ultra_special_writeblock(blockNo
, blockdata
)) {
472 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
477 if(mifare_ultra_halt()) {
478 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
483 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
485 cmd_send(CMD_ACK
,1,0,0,0,0);
486 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
490 void MifareUSetPwd(uint8_t arg0
, uint8_t *datain
){
492 uint8_t pwd
[16] = {0x00};
493 byte_t blockdata
[4] = {0x00};
495 memcpy(pwd
, datain
, 16);
497 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
499 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
501 if(!iso14443a_select_card(NULL
, NULL
, NULL
)) {
502 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
507 blockdata
[0] = pwd
[7];
508 blockdata
[1] = pwd
[6];
509 blockdata
[2] = pwd
[5];
510 blockdata
[3] = pwd
[4];
511 if(mifare_ultra_special_writeblock( 44, blockdata
)) {
512 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
517 blockdata
[0] = pwd
[3];
518 blockdata
[1] = pwd
[2];
519 blockdata
[2] = pwd
[1];
520 blockdata
[3] = pwd
[0];
521 if(mifare_ultra_special_writeblock( 45, blockdata
)) {
522 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
527 blockdata
[0] = pwd
[15];
528 blockdata
[1] = pwd
[14];
529 blockdata
[2] = pwd
[13];
530 blockdata
[3] = pwd
[12];
531 if(mifare_ultra_special_writeblock( 46, blockdata
)) {
532 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
537 blockdata
[0] = pwd
[11];
538 blockdata
[1] = pwd
[10];
539 blockdata
[2] = pwd
[9];
540 blockdata
[3] = pwd
[8];
541 if(mifare_ultra_special_writeblock( 47, blockdata
)) {
542 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
547 if(mifare_ultra_halt()) {
548 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
553 cmd_send(CMD_ACK
,1,0,0,0,0);
554 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
558 // Return 1 if the nonce is invalid else return 0
559 int valid_nonce(uint32_t Nt
, uint32_t NtEnc
, uint32_t Ks1
, uint8_t *parity
) {
560 return ((oddparity((Nt
>> 24) & 0xFF) == ((parity
[0]) ^ oddparity((NtEnc
>> 24) & 0xFF) ^ BIT(Ks1
,16))) & \
561 (oddparity((Nt
>> 16) & 0xFF) == ((parity
[1]) ^ oddparity((NtEnc
>> 16) & 0xFF) ^ BIT(Ks1
,8))) & \
562 (oddparity((Nt
>> 8) & 0xFF) == ((parity
[2]) ^ oddparity((NtEnc
>> 8) & 0xFF) ^ BIT(Ks1
,0)))) ? 1 : 0;
566 //-----------------------------------------------------------------------------
567 // MIFARE nested authentication.
569 //-----------------------------------------------------------------------------
570 void MifareNested(uint32_t arg0
, uint32_t arg1
, uint32_t calibrate
, uint8_t *datain
)
573 uint8_t blockNo
= arg0
& 0xff;
574 uint8_t keyType
= (arg0
>> 8) & 0xff;
575 uint8_t targetBlockNo
= arg1
& 0xff;
576 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
577 uint64_t ui64Key
= 0;
579 ui64Key
= bytes_to_num(datain
, 6);
582 uint16_t rtr
, i
, j
, len
;
584 static uint16_t dmin
, dmax
;
586 uint32_t cuid
, nt1
, nt2
, nttmp
, nttest
, ks1
;
588 uint32_t target_nt
[2], target_ks
[2];
590 uint8_t par_array
[4];
592 struct Crypto1State mpcs
= {0, 0};
593 struct Crypto1State
*pcs
;
595 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
597 uint32_t auth1_time
, auth2_time
;
598 static uint16_t delta_time
;
600 // free eventually allocated BigBuf memory
606 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
612 // statistics on nonce distance
613 if (calibrate
) { // for first call only. Otherwise reuse previous calibration
621 for (rtr
= 0; rtr
< 17; rtr
++) {
623 // prepare next select. No need to power down the card.
624 if(mifare_classic_halt(pcs
, cuid
)) {
625 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
630 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
631 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
637 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
638 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
644 auth2_time
= auth1_time
+ delta_time
;
648 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_NESTED
, &nt2
, &auth2_time
)) {
649 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error");
654 nttmp
= prng_successor(nt1
, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160
655 for (i
= 101; i
< 1200; i
++) {
656 nttmp
= prng_successor(nttmp
, 1);
657 if (nttmp
== nt2
) break;
667 delta_time
= auth2_time
- auth1_time
+ 32; // allow some slack for proper timing
669 if (MF_DBGLEVEL
>= 3) Dbprintf("Nested: calibrating... ntdist=%d", i
);
673 if (rtr
<= 1) return;
675 davg
= (davg
+ (rtr
- 1)/2) / (rtr
- 1);
677 if (MF_DBGLEVEL
>= 3) Dbprintf("min=%d max=%d avg=%d, delta_time=%d", dmin
, dmax
, davg
, delta_time
);
685 // -------------------------------------------------------------------------------------------------
689 // get crypted nonces for target sector
690 for(i
=0; i
< 2; i
++) { // look for exactly two different nonces
693 while(target_nt
[i
] == 0) { // continue until we have an unambiguous nonce
695 // prepare next select. No need to power down the card.
696 if(mifare_classic_halt(pcs
, cuid
)) {
697 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
701 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
702 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
707 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
708 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
712 // nested authentication
713 auth2_time
= auth1_time
+ delta_time
;
714 len
= mifare_sendcmd_shortex(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType
& 0x01), targetBlockNo
, receivedAnswer
, par
, &auth2_time
);
716 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error len=%d", len
);
720 nt2
= bytes_to_num(receivedAnswer
, 4);
721 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i
+1, nt1
, nt2
, par
[0]);
723 // Parity validity check
724 for (j
= 0; j
< 4; j
++) {
725 par_array
[j
] = (oddparity(receivedAnswer
[j
]) != ((par
[0] >> (7-j
)) & 0x01));
729 nttest
= prng_successor(nt1
, dmin
- 1);
730 for (j
= dmin
; j
< dmax
+ 1; j
++) {
731 nttest
= prng_successor(nttest
, 1);
734 if (valid_nonce(nttest
, nt2
, ks1
, par_array
)){
735 if (ncount
> 0) { // we are only interested in disambiguous nonces, try again
736 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i
+1, j
);
740 target_nt
[i
] = nttest
;
743 if (i
== 1 && target_nt
[1] == target_nt
[0]) { // we need two different nonces
745 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j
);
748 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i
+1, j
);
751 if (target_nt
[i
] == 0 && j
== dmax
+1 && MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i
+1);
757 // ----------------------------- crypto1 destroy
758 crypto1_destroy(pcs
);
760 byte_t buf
[4 + 4 * 4];
761 memcpy(buf
, &cuid
, 4);
762 memcpy(buf
+4, &target_nt
[0], 4);
763 memcpy(buf
+8, &target_ks
[0], 4);
764 memcpy(buf
+12, &target_nt
[1], 4);
765 memcpy(buf
+16, &target_ks
[1], 4);
768 cmd_send(CMD_ACK
, 0, 2, targetBlockNo
+ (targetKeyType
* 0x100), buf
, sizeof(buf
));
771 if (MF_DBGLEVEL
>= 3) DbpString("NESTED FINISHED");
773 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
778 //-----------------------------------------------------------------------------
779 // MIFARE check keys. key count up to 85.
781 //-----------------------------------------------------------------------------
782 void MifareChkKeys(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
785 uint8_t blockNo
= arg0
;
786 uint8_t keyType
= arg1
;
787 uint8_t keyCount
= arg2
;
788 uint64_t ui64Key
= 0;
795 struct Crypto1State mpcs
= {0, 0};
796 struct Crypto1State
*pcs
;
800 int OLD_MF_DBGLEVEL
= MF_DBGLEVEL
;
801 MF_DBGLEVEL
= MF_DBG_NONE
;
807 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
813 for (i
= 0; i
< keyCount
; i
++) {
814 if(mifare_classic_halt(pcs
, cuid
)) {
815 if (MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Halt error");
818 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
819 if (OLD_MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Can't select card");
823 ui64Key
= bytes_to_num(datain
+ i
* 6, 6);
824 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
832 // ----------------------------- crypto1 destroy
833 crypto1_destroy(pcs
);
836 cmd_send(CMD_ACK
,isOK
,0,0,datain
+ i
* 6,6);
839 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
842 // restore debug level
843 MF_DBGLEVEL
= OLD_MF_DBGLEVEL
;
846 //-----------------------------------------------------------------------------
847 // MIFARE commands set debug level
849 //-----------------------------------------------------------------------------
850 void MifareSetDbgLvl(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
852 Dbprintf("Debug level: %d", MF_DBGLEVEL
);
855 //-----------------------------------------------------------------------------
856 // Work with emulator memory
858 //-----------------------------------------------------------------------------
859 void MifareEMemClr(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
863 void MifareEMemSet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
864 emlSetMem(datain
, arg0
, arg1
); // data, block num, blocks count
867 void MifareEMemGet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
868 byte_t buf
[USB_CMD_DATA_SIZE
];
869 emlGetMem(buf
, arg0
, arg1
); // data, block num, blocks count (max 4)
872 cmd_send(CMD_ACK
,arg0
,arg1
,0,buf
,USB_CMD_DATA_SIZE
);
876 //-----------------------------------------------------------------------------
877 // Load a card into the emulator memory
879 //-----------------------------------------------------------------------------
880 void MifareECardLoad(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
881 uint8_t numSectors
= arg0
;
882 uint8_t keyType
= arg1
;
883 uint64_t ui64Key
= 0;
885 struct Crypto1State mpcs
= {0, 0};
886 struct Crypto1State
*pcs
;
890 byte_t dataoutbuf
[16];
891 byte_t dataoutbuf2
[16];
898 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
906 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
908 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
911 for (uint8_t sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
912 ui64Key
= emlGetKey(sectorNo
, keyType
);
914 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
916 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo
);
920 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_NESTED
)) {
922 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo
);
927 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
928 if(isOK
&& mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
)) {
930 if (MF_DBGLEVEL
>= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo
, blockNo
);
934 if (blockNo
< NumBlocksPerSector(sectorNo
) - 1) {
935 emlSetMem(dataoutbuf
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
936 } else { // sector trailer, keep the keys, set only the AC
937 emlGetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
938 memcpy(&dataoutbuf2
[6], &dataoutbuf
[6], 4);
939 emlSetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
946 if(mifare_classic_halt(pcs
, cuid
)) {
947 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
950 // ----------------------------- crypto1 destroy
951 crypto1_destroy(pcs
);
953 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
956 if (MF_DBGLEVEL
>= 2) DbpString("EMUL FILL SECTORS FINISHED");
961 //-----------------------------------------------------------------------------
962 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
964 //-----------------------------------------------------------------------------
965 void MifareCSetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
968 uint8_t needWipe
= arg0
;
969 // bit 0 - need get UID
971 // bit 2 - need HALT after sequence
972 // bit 3 - need init FPGA and field before sequence
973 // bit 4 - need reset FPGA and LED
974 uint8_t workFlags
= arg1
;
975 uint8_t blockNo
= arg2
;
978 uint8_t wupC1
[] = { 0x40 };
979 uint8_t wupC2
[] = { 0x43 };
980 uint8_t wipeC
[] = { 0x41 };
984 uint8_t uid
[10] = {0x00};
985 uint8_t d_block
[18] = {0x00};
988 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
989 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
991 // reset FPGA and LED
992 if (workFlags
& 0x08) {
999 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1004 // get UID from chip
1005 if (workFlags
& 0x01) {
1006 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
1007 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1011 if(mifare_classic_halt(NULL
, cuid
)) {
1012 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1019 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1020 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1021 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1025 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
1026 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1027 if (MF_DBGLEVEL
>= 1) Dbprintf("wipeC error");
1031 if(mifare_classic_halt(NULL
, cuid
)) {
1032 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1038 if (workFlags
& 0x02) {
1039 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1040 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1041 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1045 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1046 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1047 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1052 if ((mifare_sendcmd_short(NULL
, 0, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1053 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send command error");
1057 memcpy(d_block
, datain
, 16);
1058 AppendCrc14443a(d_block
, 16);
1060 ReaderTransmit(d_block
, sizeof(d_block
), NULL
);
1061 if ((ReaderReceive(receivedAnswer
, receivedAnswerPar
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1062 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send data error");
1066 if (workFlags
& 0x04) {
1067 if (mifare_classic_halt(NULL
, cuid
)) {
1068 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1078 cmd_send(CMD_ACK
,isOK
,0,0,uid
,4);
1081 if ((workFlags
& 0x10) || (!isOK
)) {
1082 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1088 void MifareCGetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1091 // bit 1 - need wupC
1092 // bit 2 - need HALT after sequence
1093 // bit 3 - need init FPGA and field before sequence
1094 // bit 4 - need reset FPGA and LED
1095 uint8_t workFlags
= arg0
;
1096 uint8_t blockNo
= arg2
;
1099 uint8_t wupC1
[] = { 0x40 };
1100 uint8_t wupC2
[] = { 0x43 };
1104 uint8_t data
[18] = {0x00};
1107 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1108 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1110 if (workFlags
& 0x08) {
1117 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1121 if (workFlags
& 0x02) {
1122 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1123 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1124 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1128 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1129 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1130 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1136 if ((mifare_sendcmd_short(NULL
, 0, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 18)) {
1137 if (MF_DBGLEVEL
>= 1) Dbprintf("read block send command error");
1140 memcpy(data
, receivedAnswer
, 18);
1142 if (workFlags
& 0x04) {
1143 if (mifare_classic_halt(NULL
, cuid
)) {
1144 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1154 cmd_send(CMD_ACK
,isOK
,0,0,data
,18);
1157 if ((workFlags
& 0x10) || (!isOK
)) {
1158 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1163 void MifareCIdent(){
1166 uint8_t wupC1
[] = { 0x40 };
1167 uint8_t wupC2
[] = { 0x43 };
1172 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1173 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1175 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1176 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1180 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1181 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1185 if (mifare_classic_halt(NULL
, 0)) {
1189 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
1196 void Mifare_DES_Auth1(uint8_t arg0
, uint8_t *datain
){
1198 byte_t dataout
[11] = {0x00};
1199 uint8_t uid
[10] = {0x00};
1203 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1205 int len
= iso14443a_select_card(uid
, NULL
, &cuid
);
1207 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
1212 if(mifare_desfire_des_auth1(cuid
, dataout
)){
1213 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication part1: Fail.");
1218 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 1 FINISHED");
1219 cmd_send(CMD_ACK
,1,cuid
,0,dataout
, sizeof(dataout
));
1222 void Mifare_DES_Auth2(uint32_t arg0
, uint8_t *datain
){
1224 uint32_t cuid
= arg0
;
1225 uint8_t key
[16] = {0x00};
1227 byte_t dataout
[12] = {0x00};
1229 memcpy(key
, datain
, 16);
1231 isOK
= mifare_desfire_des_auth2(cuid
, key
, dataout
);
1234 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
)
1235 Dbprintf("Authentication part2: Failed");
1240 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
)
1241 DbpString("AUTH 2 FINISHED");
1243 cmd_send(CMD_ACK
, isOK
, 0, 0, dataout
, sizeof(dataout
));
1244 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1250 ReaderTransmit(deselect_cmd
, 3 , NULL
);
1251 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1255 void OnError(uint8_t reason
){
1257 ReaderTransmit(deselect_cmd
, 3 , NULL
);
1258 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1259 cmd_send(CMD_ACK
,0,reason
,0,0,0);