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"
21 // the block number for the ISO14443-4 PCB
22 uint8_t pcb_blocknum
= 0;
23 // Deselect card by sending a s-block. the crc is precalced for speed
24 static uint8_t deselect_cmd
[] = {0xc2,0xe0,0xb4};
26 //-----------------------------------------------------------------------------
27 // Select, Authenticate, Read a MIFARE tag.
29 //-----------------------------------------------------------------------------
30 void MifareReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
33 uint8_t blockNo
= arg0
;
34 uint8_t keyType
= arg1
;
36 ui64Key
= bytes_to_num(datain
, 6);
40 byte_t dataoutbuf
[16];
43 struct Crypto1State mpcs
= {0, 0};
44 struct Crypto1State
*pcs
;
49 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
56 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
57 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
61 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
62 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
66 if(mifare_classic_readblock(pcs
, cuid
, blockNo
, dataoutbuf
)) {
67 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block error");
71 if(mifare_classic_halt(pcs
, cuid
)) {
72 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
80 // ----------------------------- crypto1 destroy
83 if (MF_DBGLEVEL
>= 2) DbpString("READ BLOCK FINISHED");
86 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16);
89 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
93 void MifareUC_Auth(uint8_t arg0
, uint8_t *keybytes
){
95 bool turnOffField
= (arg0
== 1);
97 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
99 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
101 if(!iso14443a_select_card(NULL
, NULL
, NULL
)) {
102 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
107 if(!mifare_ultra_auth(keybytes
)){
108 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication failed");
114 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
117 cmd_send(CMD_ACK
,1,0,0,0,0);
121 // Arg1 = UsePwd bool
122 // datain = PWD bytes,
123 void MifareUReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
)
125 uint8_t blockNo
= arg0
;
126 byte_t dataout
[16] = {0x00};
127 bool useKey
= (arg1
== 1); //UL_C
128 bool usePwd
= (arg1
== 2); //UL_EV1/NTAG
133 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
135 int len
= iso14443a_select_card(NULL
, NULL
, NULL
);
137 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%02X)",len
);
142 // UL-C authentication
144 uint8_t key
[16] = {0x00};
145 memcpy(key
, datain
, sizeof(key
) );
147 if ( !mifare_ultra_auth(key
) ) {
153 // UL-EV1 / NTAG authentication
155 uint8_t pwd
[4] = {0x00};
156 memcpy(pwd
, datain
, 4);
157 uint8_t pack
[4] = {0,0,0,0};
158 if (!mifare_ul_ev1_auth(pwd
, pack
)) {
164 if( mifare_ultra_readblock(blockNo
, dataout
) ) {
165 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Read block error");
170 if( mifare_ultra_halt() ) {
171 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Halt error");
176 cmd_send(CMD_ACK
,1,0,0,dataout
,16);
177 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
181 //-----------------------------------------------------------------------------
182 // Select, Authenticate, Read a MIFARE tag.
183 // read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)
184 //-----------------------------------------------------------------------------
185 void MifareReadSector(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
188 uint8_t sectorNo
= arg0
;
189 uint8_t keyType
= arg1
;
190 uint64_t ui64Key
= 0;
191 ui64Key
= bytes_to_num(datain
, 6);
195 byte_t dataoutbuf
[16 * 16];
198 struct Crypto1State mpcs
= {0, 0};
199 struct Crypto1State
*pcs
;
205 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
212 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
214 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
218 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
220 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
223 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
224 if(mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
+ 16 * blockNo
)) {
226 if (MF_DBGLEVEL
>= 1) Dbprintf("Read sector %2d block %2d error", sectorNo
, blockNo
);
231 if(mifare_classic_halt(pcs
, cuid
)) {
232 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
235 // ----------------------------- crypto1 destroy
236 crypto1_destroy(pcs
);
238 if (MF_DBGLEVEL
>= 2) DbpString("READ SECTOR FINISHED");
241 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16*NumBlocksPerSector(sectorNo
));
245 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
249 // arg0 = blockNo (start)
250 // arg1 = Pages (number of blocks)
252 // datain = KEY bytes
253 void MifareUReadCard(uint8_t arg0
, uint16_t arg1
, uint8_t arg2
, uint8_t *datain
)
255 // free eventually allocated BigBuf memory
260 uint8_t blockNo
= arg0
;
261 uint16_t blocks
= arg1
;
262 bool useKey
= (arg2
== 1); //UL_C
263 bool usePwd
= (arg2
== 2); //UL_EV1/NTAG
264 uint32_t countblocks
= 0;
265 uint8_t *dataout
= BigBuf_malloc(CARD_MEMORY_SIZE
);
266 if (dataout
== NULL
){
267 Dbprintf("out of memory");
274 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
276 int len
= iso14443a_select_card(NULL
, NULL
, NULL
);
278 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%d)",len
);
283 // UL-C authentication
285 uint8_t key
[16] = {0x00};
286 memcpy(key
, datain
, sizeof(key
) );
288 if ( !mifare_ultra_auth(key
) ) {
294 // UL-EV1 / NTAG authentication
296 uint8_t pwd
[4] = {0x00};
297 memcpy(pwd
, datain
, sizeof(pwd
));
298 uint8_t pack
[4] = {0,0,0,0};
300 if (!mifare_ul_ev1_auth(pwd
, pack
)){
306 for (int i
= 0; i
< blocks
; i
++){
307 if ((i
*4) + 4 >= CARD_MEMORY_SIZE
) {
308 Dbprintf("Data exceeds buffer!!");
312 len
= mifare_ultra_readblock(blockNo
+ i
, dataout
+ 4 * i
);
315 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Read block %d error",i
);
316 // if no blocks read - error out
321 //stop at last successful read block and return what we got
329 len
= mifare_ultra_halt();
331 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Halt error");
336 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) Dbprintf("Blocks read %d", countblocks
);
340 cmd_send(CMD_ACK
, 1, countblocks
, BigBuf_max_traceLen(), 0, 0);
341 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
346 //-----------------------------------------------------------------------------
347 // Select, Authenticate, Write a MIFARE tag.
349 //-----------------------------------------------------------------------------
350 void MifareWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
353 uint8_t blockNo
= arg0
;
354 uint8_t keyType
= arg1
;
355 uint64_t ui64Key
= 0;
356 byte_t blockdata
[16];
358 ui64Key
= bytes_to_num(datain
, 6);
359 memcpy(blockdata
, datain
+ 10, 16);
365 struct Crypto1State mpcs
= {0, 0};
366 struct Crypto1State
*pcs
;
372 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
379 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
380 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
384 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
385 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
389 if(mifare_classic_writeblock(pcs
, cuid
, blockNo
, blockdata
)) {
390 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
394 if(mifare_classic_halt(pcs
, cuid
)) {
395 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
403 // ----------------------------- crypto1 destroy
404 crypto1_destroy(pcs
);
406 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
409 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
414 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
418 /* // Command not needed but left for future testing
419 void MifareUWriteBlockCompat(uint8_t arg0, uint8_t *datain)
421 uint8_t blockNo = arg0;
422 byte_t blockdata[16] = {0x00};
424 memcpy(blockdata, datain, 16);
426 uint8_t uid[10] = {0x00};
428 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
431 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
433 if(!iso14443a_select_card(uid, NULL, NULL)) {
434 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
439 if(mifare_ultra_writeblock_compat(blockNo, blockdata)) {
440 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
444 if(mifare_ultra_halt()) {
445 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
450 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");
452 cmd_send(CMD_ACK,1,0,0,0,0);
453 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
458 // Arg0 : Block to write to.
459 // Arg1 : 0 = use no authentication.
460 // 1 = use 0x1A authentication.
461 // 2 = use 0x1B authentication.
462 // datain : 4 first bytes is data to be written.
463 // : 4/16 next bytes is authentication key.
464 void MifareUWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
)
466 uint8_t blockNo
= arg0
;
467 bool useKey
= (arg1
== 1); //UL_C
468 bool usePwd
= (arg1
== 2); //UL_EV1/NTAG
469 byte_t blockdata
[4] = {0x00};
471 memcpy(blockdata
, datain
,4);
476 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
478 if(!iso14443a_select_card(NULL
, NULL
, NULL
)) {
479 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
484 // UL-C authentication
486 uint8_t key
[16] = {0x00};
487 memcpy(key
, datain
+4, sizeof(key
) );
489 if ( !mifare_ultra_auth(key
) ) {
495 // UL-EV1 / NTAG authentication
497 uint8_t pwd
[4] = {0x00};
498 memcpy(pwd
, datain
+4, 4);
499 uint8_t pack
[4] = {0,0,0,0};
500 if (!mifare_ul_ev1_auth(pwd
, pack
)) {
506 if(mifare_ultra_writeblock(blockNo
, blockdata
)) {
507 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
512 if(mifare_ultra_halt()) {
513 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
518 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
520 cmd_send(CMD_ACK
,1,0,0,0,0);
521 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
525 void MifareUSetPwd(uint8_t arg0
, uint8_t *datain
){
527 uint8_t pwd
[16] = {0x00};
528 byte_t blockdata
[4] = {0x00};
530 memcpy(pwd
, datain
, 16);
532 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
534 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
536 if(!iso14443a_select_card(NULL
, NULL
, NULL
)) {
537 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
542 blockdata
[0] = pwd
[7];
543 blockdata
[1] = pwd
[6];
544 blockdata
[2] = pwd
[5];
545 blockdata
[3] = pwd
[4];
546 if(mifare_ultra_writeblock( 44, blockdata
)) {
547 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
552 blockdata
[0] = pwd
[3];
553 blockdata
[1] = pwd
[2];
554 blockdata
[2] = pwd
[1];
555 blockdata
[3] = pwd
[0];
556 if(mifare_ultra_writeblock( 45, blockdata
)) {
557 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
562 blockdata
[0] = pwd
[15];
563 blockdata
[1] = pwd
[14];
564 blockdata
[2] = pwd
[13];
565 blockdata
[3] = pwd
[12];
566 if(mifare_ultra_writeblock( 46, blockdata
)) {
567 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
572 blockdata
[0] = pwd
[11];
573 blockdata
[1] = pwd
[10];
574 blockdata
[2] = pwd
[9];
575 blockdata
[3] = pwd
[8];
576 if(mifare_ultra_writeblock( 47, blockdata
)) {
577 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
582 if(mifare_ultra_halt()) {
583 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
588 cmd_send(CMD_ACK
,1,0,0,0,0);
589 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
593 // Return 1 if the nonce is invalid else return 0
594 int valid_nonce(uint32_t Nt
, uint32_t NtEnc
, uint32_t Ks1
, uint8_t *parity
) {
595 return ((oddparity((Nt
>> 24) & 0xFF) == ((parity
[0]) ^ oddparity((NtEnc
>> 24) & 0xFF) ^ BIT(Ks1
,16))) & \
596 (oddparity((Nt
>> 16) & 0xFF) == ((parity
[1]) ^ oddparity((NtEnc
>> 16) & 0xFF) ^ BIT(Ks1
,8))) & \
597 (oddparity((Nt
>> 8) & 0xFF) == ((parity
[2]) ^ oddparity((NtEnc
>> 8) & 0xFF) ^ BIT(Ks1
,0)))) ? 1 : 0;
601 //-----------------------------------------------------------------------------
602 // MIFARE nested authentication.
604 //-----------------------------------------------------------------------------
605 void MifareNested(uint32_t arg0
, uint32_t arg1
, uint32_t calibrate
, uint8_t *datain
)
608 uint8_t blockNo
= arg0
& 0xff;
609 uint8_t keyType
= (arg0
>> 8) & 0xff;
610 uint8_t targetBlockNo
= arg1
& 0xff;
611 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
612 uint64_t ui64Key
= 0;
614 ui64Key
= bytes_to_num(datain
, 6);
617 uint16_t rtr
, i
, j
, len
;
619 static uint16_t dmin
, dmax
;
621 uint32_t cuid
, nt1
, nt2
, nttmp
, nttest
, ks1
;
623 uint32_t target_nt
[2], target_ks
[2];
625 uint8_t par_array
[4];
627 struct Crypto1State mpcs
= {0, 0};
628 struct Crypto1State
*pcs
;
630 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
632 uint32_t auth1_time
, auth2_time
;
633 static uint16_t delta_time
;
635 // free eventually allocated BigBuf memory
641 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
647 // statistics on nonce distance
648 if (calibrate
) { // for first call only. Otherwise reuse previous calibration
656 for (rtr
= 0; rtr
< 17; rtr
++) {
658 // prepare next select. No need to power down the card.
659 if(mifare_classic_halt(pcs
, cuid
)) {
660 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
665 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
666 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
672 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
673 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
679 auth2_time
= auth1_time
+ delta_time
;
683 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_NESTED
, &nt2
, &auth2_time
)) {
684 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error");
689 nttmp
= prng_successor(nt1
, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160
690 for (i
= 101; i
< 1200; i
++) {
691 nttmp
= prng_successor(nttmp
, 1);
692 if (nttmp
== nt2
) break;
702 delta_time
= auth2_time
- auth1_time
+ 32; // allow some slack for proper timing
704 if (MF_DBGLEVEL
>= 3) Dbprintf("Nested: calibrating... ntdist=%d", i
);
708 if (rtr
<= 1) return;
710 davg
= (davg
+ (rtr
- 1)/2) / (rtr
- 1);
712 if (MF_DBGLEVEL
>= 3) Dbprintf("min=%d max=%d avg=%d, delta_time=%d", dmin
, dmax
, davg
, delta_time
);
720 // -------------------------------------------------------------------------------------------------
724 // get crypted nonces for target sector
725 for(i
=0; i
< 2; i
++) { // look for exactly two different nonces
728 while(target_nt
[i
] == 0) { // continue until we have an unambiguous nonce
730 // prepare next select. No need to power down the card.
731 if(mifare_classic_halt(pcs
, cuid
)) {
732 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
736 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
737 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
742 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
743 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
747 // nested authentication
748 auth2_time
= auth1_time
+ delta_time
;
749 len
= mifare_sendcmd_short(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType
& 0x01), targetBlockNo
, receivedAnswer
, par
, &auth2_time
);
751 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error len=%d", len
);
755 nt2
= bytes_to_num(receivedAnswer
, 4);
756 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i
+1, nt1
, nt2
, par
[0]);
758 // Parity validity check
759 for (j
= 0; j
< 4; j
++) {
760 par_array
[j
] = (oddparity(receivedAnswer
[j
]) != ((par
[0] >> (7-j
)) & 0x01));
764 nttest
= prng_successor(nt1
, dmin
- 1);
765 for (j
= dmin
; j
< dmax
+ 1; j
++) {
766 nttest
= prng_successor(nttest
, 1);
769 if (valid_nonce(nttest
, nt2
, ks1
, par_array
)){
770 if (ncount
> 0) { // we are only interested in disambiguous nonces, try again
771 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i
+1, j
);
775 target_nt
[i
] = nttest
;
778 if (i
== 1 && target_nt
[1] == target_nt
[0]) { // we need two different nonces
780 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j
);
783 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i
+1, j
);
786 if (target_nt
[i
] == 0 && j
== dmax
+1 && MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i
+1);
792 // ----------------------------- crypto1 destroy
793 crypto1_destroy(pcs
);
795 byte_t buf
[4 + 4 * 4];
796 memcpy(buf
, &cuid
, 4);
797 memcpy(buf
+4, &target_nt
[0], 4);
798 memcpy(buf
+8, &target_ks
[0], 4);
799 memcpy(buf
+12, &target_nt
[1], 4);
800 memcpy(buf
+16, &target_ks
[1], 4);
803 cmd_send(CMD_ACK
, 0, 2, targetBlockNo
+ (targetKeyType
* 0x100), buf
, sizeof(buf
));
806 if (MF_DBGLEVEL
>= 3) DbpString("NESTED FINISHED");
808 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
813 //-----------------------------------------------------------------------------
814 // MIFARE check keys. key count up to 85.
816 //-----------------------------------------------------------------------------
817 void MifareChkKeys(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
820 uint8_t blockNo
= arg0
;
821 uint8_t keyType
= arg1
;
822 uint8_t keyCount
= arg2
;
823 uint64_t ui64Key
= 0;
830 struct Crypto1State mpcs
= {0, 0};
831 struct Crypto1State
*pcs
;
835 int OLD_MF_DBGLEVEL
= MF_DBGLEVEL
;
836 MF_DBGLEVEL
= MF_DBG_NONE
;
842 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
848 for (i
= 0; i
< keyCount
; i
++) {
849 if(mifare_classic_halt(pcs
, cuid
)) {
850 if (MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Halt error");
853 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
854 if (OLD_MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Can't select card");
858 ui64Key
= bytes_to_num(datain
+ i
* 6, 6);
859 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
867 // ----------------------------- crypto1 destroy
868 crypto1_destroy(pcs
);
871 cmd_send(CMD_ACK
,isOK
,0,0,datain
+ i
* 6,6);
874 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
877 // restore debug level
878 MF_DBGLEVEL
= OLD_MF_DBGLEVEL
;
881 //-----------------------------------------------------------------------------
882 // MIFARE commands set debug level
884 //-----------------------------------------------------------------------------
885 void MifareSetDbgLvl(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
887 Dbprintf("Debug level: %d", MF_DBGLEVEL
);
890 //-----------------------------------------------------------------------------
891 // Work with emulator memory
893 //-----------------------------------------------------------------------------
894 void MifareEMemClr(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
898 void MifareEMemSet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
899 emlSetMem(datain
, arg0
, arg1
); // data, block num, blocks count
902 void MifareEMemGet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
903 byte_t buf
[USB_CMD_DATA_SIZE
];
904 emlGetMem(buf
, arg0
, arg1
); // data, block num, blocks count (max 4)
907 cmd_send(CMD_ACK
,arg0
,arg1
,0,buf
,USB_CMD_DATA_SIZE
);
911 //-----------------------------------------------------------------------------
912 // Load a card into the emulator memory
914 //-----------------------------------------------------------------------------
915 void MifareECardLoad(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
916 uint8_t numSectors
= arg0
;
917 uint8_t keyType
= arg1
;
918 uint64_t ui64Key
= 0;
920 struct Crypto1State mpcs
= {0, 0};
921 struct Crypto1State
*pcs
;
925 byte_t dataoutbuf
[16];
926 byte_t dataoutbuf2
[16];
933 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
941 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
943 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
946 for (uint8_t sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
947 ui64Key
= emlGetKey(sectorNo
, keyType
);
949 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
951 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo
);
955 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_NESTED
)) {
957 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo
);
962 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
963 if(isOK
&& mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
)) {
965 if (MF_DBGLEVEL
>= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo
, blockNo
);
969 if (blockNo
< NumBlocksPerSector(sectorNo
) - 1) {
970 emlSetMem(dataoutbuf
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
971 } else { // sector trailer, keep the keys, set only the AC
972 emlGetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
973 memcpy(&dataoutbuf2
[6], &dataoutbuf
[6], 4);
974 emlSetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
981 if(mifare_classic_halt(pcs
, cuid
)) {
982 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
985 // ----------------------------- crypto1 destroy
986 crypto1_destroy(pcs
);
988 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
991 if (MF_DBGLEVEL
>= 2) DbpString("EMUL FILL SECTORS FINISHED");
996 //-----------------------------------------------------------------------------
997 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
999 //-----------------------------------------------------------------------------
1000 void MifareCSetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1003 uint8_t needWipe
= arg0
;
1004 // bit 0 - need get UID
1005 // bit 1 - need wupC
1006 // bit 2 - need HALT after sequence
1007 // bit 3 - need init FPGA and field before sequence
1008 // bit 4 - need reset FPGA and LED
1009 uint8_t workFlags
= arg1
;
1010 uint8_t blockNo
= arg2
;
1013 uint8_t wupC1
[] = { 0x40 };
1014 uint8_t wupC2
[] = { 0x43 };
1015 uint8_t wipeC
[] = { 0x41 };
1019 uint8_t uid
[10] = {0x00};
1020 uint8_t d_block
[18] = {0x00};
1023 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1024 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1026 // reset FPGA and LED
1027 if (workFlags
& 0x08) {
1034 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1039 // get UID from chip
1040 if (workFlags
& 0x01) {
1041 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
1042 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1046 if(mifare_classic_halt(NULL
, cuid
)) {
1047 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1054 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1055 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1056 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1060 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
1061 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1062 if (MF_DBGLEVEL
>= 1) Dbprintf("wipeC error");
1066 if(mifare_classic_halt(NULL
, cuid
)) {
1067 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1073 if (workFlags
& 0x02) {
1074 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1075 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1076 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1080 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1081 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1082 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1087 if ((mifare_sendcmd_short(NULL
, 0, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1088 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send command error");
1092 memcpy(d_block
, datain
, 16);
1093 AppendCrc14443a(d_block
, 16);
1095 ReaderTransmit(d_block
, sizeof(d_block
), NULL
);
1096 if ((ReaderReceive(receivedAnswer
, receivedAnswerPar
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1097 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send data error");
1101 if (workFlags
& 0x04) {
1102 if (mifare_classic_halt(NULL
, cuid
)) {
1103 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1113 cmd_send(CMD_ACK
,isOK
,0,0,uid
,4);
1116 if ((workFlags
& 0x10) || (!isOK
)) {
1117 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1123 void MifareCGetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1126 // bit 1 - need wupC
1127 // bit 2 - need HALT after sequence
1128 // bit 3 - need init FPGA and field before sequence
1129 // bit 4 - need reset FPGA and LED
1130 uint8_t workFlags
= arg0
;
1131 uint8_t blockNo
= arg2
;
1134 uint8_t wupC1
[] = { 0x40 };
1135 uint8_t wupC2
[] = { 0x43 };
1139 uint8_t data
[18] = {0x00};
1142 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1143 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1145 if (workFlags
& 0x08) {
1152 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1156 if (workFlags
& 0x02) {
1157 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1158 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1159 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1163 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1164 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1165 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1171 if ((mifare_sendcmd_short(NULL
, 0, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 18)) {
1172 if (MF_DBGLEVEL
>= 1) Dbprintf("read block send command error");
1175 memcpy(data
, receivedAnswer
, 18);
1177 if (workFlags
& 0x04) {
1178 if (mifare_classic_halt(NULL
, cuid
)) {
1179 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1189 cmd_send(CMD_ACK
,isOK
,0,0,data
,18);
1192 if ((workFlags
& 0x10) || (!isOK
)) {
1193 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1198 void MifareCIdent(){
1201 uint8_t wupC1
[] = { 0x40 };
1202 uint8_t wupC2
[] = { 0x43 };
1207 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1208 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1210 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1211 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1215 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1216 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1220 if (mifare_classic_halt(NULL
, 0)) {
1224 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
1231 void Mifare_DES_Auth1(uint8_t arg0
, uint8_t *datain
){
1233 byte_t dataout
[11] = {0x00};
1234 uint8_t uid
[10] = {0x00};
1238 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1240 int len
= iso14443a_select_card(uid
, NULL
, &cuid
);
1242 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
1247 if(mifare_desfire_des_auth1(cuid
, dataout
)){
1248 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication part1: Fail.");
1253 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 1 FINISHED");
1254 cmd_send(CMD_ACK
,1,cuid
,0,dataout
, sizeof(dataout
));
1257 void Mifare_DES_Auth2(uint32_t arg0
, uint8_t *datain
){
1259 uint32_t cuid
= arg0
;
1260 uint8_t key
[16] = {0x00};
1262 byte_t dataout
[12] = {0x00};
1264 memcpy(key
, datain
, 16);
1266 isOK
= mifare_desfire_des_auth2(cuid
, key
, dataout
);
1269 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) Dbprintf("Authentication part2: Failed");
1274 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 2 FINISHED");
1276 cmd_send(CMD_ACK
, isOK
, 0, 0, dataout
, sizeof(dataout
));
1277 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1283 ReaderTransmit(deselect_cmd
, 3 , NULL
);
1284 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1288 void OnError(uint8_t reason
){
1290 ReaderTransmit(deselect_cmd
, 3 , NULL
);
1291 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1292 cmd_send(CMD_ACK
,0,reason
,0,0,0);