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
8 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
9 // at your option, any later version. See the LICENSE.txt file for the text of
11 //-----------------------------------------------------------------------------
12 // Routines to support ISO 14443 type A.
13 //-----------------------------------------------------------------------------
15 #include "mifarecmd.h"
18 //-----------------------------------------------------------------------------
19 // Select, Authenticaate, Read an MIFARE tag.
21 //-----------------------------------------------------------------------------
22 void MifareReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
25 uint8_t blockNo
= arg0
;
26 uint8_t keyType
= arg1
;
28 ui64Key
= bytes_to_num(datain
, 6);
32 byte_t dataoutbuf
[16];
35 struct Crypto1State mpcs
= {0, 0};
36 struct Crypto1State
*pcs
;
41 // iso14a_set_tracing(false);
43 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
50 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
51 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
55 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
56 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
60 if(mifare_classic_readblock(pcs
, cuid
, blockNo
, dataoutbuf
)) {
61 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block error");
65 if(mifare_classic_halt(pcs
, cuid
)) {
66 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
74 // ----------------------------- crypto1 destroy
77 if (MF_DBGLEVEL
>= 2) DbpString("READ BLOCK FINISHED");
81 LogTrace(uid
, 4, 0, 0, TRUE
);
84 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16);
89 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
91 //iso14a_set_tracing(TRUE);
96 void MifareUC_Auth1(uint8_t arg0
, uint8_t *datain
){
99 byte_t dataoutbuf
[16];
104 iso14a_clear_trace();
105 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
112 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
113 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card, something went wrong before auth");
116 if(mifare_ultra_auth1(cuid
, dataoutbuf
)){
117 if (MF_DBGLEVEL
>= 1) Dbprintf("Authentication part1: Fail.");
121 if (MF_DBGLEVEL
>= 2) DbpString("AUTH 1 FINISHED");
124 cmd_send(CMD_ACK
,isOK
,cuid
,0,dataoutbuf
,11);
128 //FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
131 void MifareUC_Auth2(uint32_t arg0
, uint8_t *datain
){
133 uint32_t cuid
= arg0
;
134 uint8_t key
[16]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
137 byte_t dataoutbuf
[16];
139 memcpy(key
, datain
, 16);
141 //iso14a_clear_trace();
142 //iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
148 // Dbprintf("Sending %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
149 // key[0],key[1],key[2],key[3],key[4],key[5],key[6],key[7],key[8],
150 // key[9],key[10],key[11],key[12],key[13],key[14],key[15]);
152 if(mifare_ultra_auth2(cuid
, key
, dataoutbuf
)){
153 if (MF_DBGLEVEL
>= 1) Dbprintf("Authentication part2: Fail...");
156 if (MF_DBGLEVEL
>= 2) DbpString("AUTH 2 FINISHED");
159 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,11);
163 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
167 void MifareUReadBlock(uint8_t arg0
, uint8_t *datain
)
170 uint8_t blockNo
= arg0
;
171 //uint8_t key[16]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
174 byte_t dataoutbuf
[16];
178 //memcpy(key, datain + 10, 26);
181 iso14a_clear_trace();
182 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
189 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
190 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
194 if(mifare_ultra_readblock(cuid
, blockNo
, dataoutbuf
)) {
195 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block error");
199 if(mifare_ultra_halt(cuid
)) {
200 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
207 if (MF_DBGLEVEL
>= 2) DbpString("READ BLOCK FINISHED");
210 memset(uid
, 0x44, 4);
211 LogTrace(uid
, 4, 0, 0, TRUE
);
213 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16);
218 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
222 //-----------------------------------------------------------------------------
223 // Select, Authenticaate, Read an MIFARE tag.
224 // read sector (data = 4 x 16 bytes = 64 bytes)
225 //-----------------------------------------------------------------------------
226 void MifareReadSector(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
229 uint8_t sectorNo
= arg0
;
230 uint8_t keyType
= arg1
;
231 uint64_t ui64Key
= 0;
232 ui64Key
= bytes_to_num(datain
, 6);
236 byte_t dataoutbuf
[16 * 4];
239 struct Crypto1State mpcs
= {0, 0};
240 struct Crypto1State
*pcs
;
244 iso14a_clear_trace();
245 // iso14a_set_tracing(false);
247 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
254 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
255 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
259 if(mifare_classic_auth(pcs
, cuid
, sectorNo
* 4, keyType
, ui64Key
, AUTH_FIRST
)) {
260 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
264 if(mifare_classic_readblock(pcs
, cuid
, sectorNo
* 4 + 0, dataoutbuf
+ 16 * 0)) {
265 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block 0 error");
268 if(mifare_classic_readblock(pcs
, cuid
, sectorNo
* 4 + 1, dataoutbuf
+ 16 * 1)) {
269 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block 1 error");
272 if(mifare_classic_readblock(pcs
, cuid
, sectorNo
* 4 + 2, dataoutbuf
+ 16 * 2)) {
273 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block 2 error");
276 if(mifare_classic_readblock(pcs
, cuid
, sectorNo
* 4 + 3, dataoutbuf
+ 16 * 3)) {
277 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block 3 error");
281 if(mifare_classic_halt(pcs
, cuid
)) {
282 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
290 // ----------------------------- crypto1 destroy
291 crypto1_destroy(pcs
);
293 if (MF_DBGLEVEL
>= 2) DbpString("READ SECTOR FINISHED");
296 memset(uid
, 0x44, 4);
297 LogTrace(uid
, 4, 0, 0, TRUE
);
299 // UsbCommand ack = {CMD_ACK, {isOK, 0, 0}};
300 // memcpy(ack.d.asBytes, dataoutbuf, 16 * 2);
303 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,32);
304 // UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));
307 // memcpy(ack.d.asBytes, dataoutbuf + 16 * 2, 16 * 2);
308 // UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));
309 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
+32, 32);
313 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
315 // iso14a_set_tracing(TRUE);
319 void MifareUReadCard(uint8_t arg0
, int arg1
, uint8_t *datain
)
322 uint8_t sectorNo
= arg0
;
327 byte_t dataoutbuf
[44 * 4];
332 iso14a_clear_trace();
333 // iso14a_set_tracing(false);
335 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
340 Dbprintf("Pages %d",Pages
);
342 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
343 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
346 for(int sec
=0;sec
<Pages
;sec
++){
347 if(mifare_ultra_readblock(cuid
, sectorNo
* 4 + sec
, dataoutbuf
+ 4 * sec
)) {
348 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block %d error",sec
);
354 if(mifare_ultra_halt(cuid
)) {
355 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
362 Dbprintf("Pages read %d",count_Pages
);
363 if (MF_DBGLEVEL
>= 2) DbpString("READ CARD FINISHED");
366 //memset(uid, 0x44, 4);
367 //LogTrace(uid, 4, 0, 0, TRUE);
370 if (Pages
==16) cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,64);
371 if (Pages
==44 && count_Pages
==16) cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,64);
372 if (Pages
==44 && count_Pages
>16) cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,176);
373 //cmd_send(CMD_ACK,isOK,0,0,dataoutbuf+32, 32);
377 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
379 // iso14a_set_tracing(TRUE);
384 //-----------------------------------------------------------------------------
385 // Select, Authenticaate, Read an MIFARE tag.
387 //-----------------------------------------------------------------------------
388 void MifareWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
391 uint8_t blockNo
= arg0
;
392 uint8_t keyType
= arg1
;
393 uint64_t ui64Key
= 0;
394 byte_t blockdata
[16];
396 ui64Key
= bytes_to_num(datain
, 6);
397 memcpy(blockdata
, datain
+ 10, 16);
403 struct Crypto1State mpcs
= {0, 0};
404 struct Crypto1State
*pcs
;
408 iso14a_clear_trace();
409 // iso14a_set_tracing(false);
411 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
418 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
419 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
423 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
424 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
428 if(mifare_classic_writeblock(pcs
, cuid
, blockNo
, blockdata
)) {
429 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
433 if(mifare_classic_halt(pcs
, cuid
)) {
434 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
442 // ----------------------------- crypto1 destroy
443 crypto1_destroy(pcs
);
445 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
448 memset(uid
, 0x44, 4);
449 LogTrace(uid
, 4, 0, 0, TRUE
);
451 // UsbCommand ack = {CMD_ACK, {isOK, 0, 0}};
454 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
455 // UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));
460 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
462 // iso14a_set_tracing(TRUE);
466 void MifareUWriteBlock(uint8_t arg0
, uint8_t *datain
)
469 uint8_t blockNo
= arg0
;
470 byte_t blockdata
[16];
472 memset(blockdata
,'\0',16);
473 memcpy(blockdata
, datain
,16);
481 iso14a_clear_trace();
482 // iso14a_set_tracing(false);
484 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
491 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
492 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
496 if(mifare_ultra_writeblock(cuid
, blockNo
, blockdata
)) {
497 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
501 if(mifare_ultra_halt(cuid
)) {
502 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
510 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
513 memset(uid
, 0x44, 4);
514 LogTrace(uid
, 4, 0, 0, TRUE
);
517 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
518 // UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));
523 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
525 // iso14a_set_tracing(TRUE);
529 void MifareUWriteBlock_Special(uint8_t arg0
, uint8_t *datain
)
532 uint8_t blockNo
= arg0
;
535 memcpy(blockdata
, datain
,4);
543 iso14a_clear_trace();
544 // iso14a_set_tracing(false);
546 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
553 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
554 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
558 if(mifare_ultra_special_writeblock(cuid
, blockNo
, blockdata
)) {
559 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
563 if(mifare_ultra_halt(cuid
)) {
564 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
572 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
575 memset(uid
, 0x44, 4);
576 LogTrace(uid
, 4, 0, 0, TRUE
);
579 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
580 // UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));
585 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
587 // iso14a_set_tracing(TRUE);
591 // Return 1 if the nonce is invalid else return 0
592 int valid_nonce(uint32_t Nt
, uint32_t NtEnc
, uint32_t Ks1
, byte_t
* parity
) {
593 return ((oddparity((Nt
>> 24) & 0xFF) == ((parity
[0]) ^ oddparity((NtEnc
>> 24) & 0xFF) ^ BIT(Ks1
,16))) & \
594 (oddparity((Nt
>> 16) & 0xFF) == ((parity
[1]) ^ oddparity((NtEnc
>> 16) & 0xFF) ^ BIT(Ks1
,8))) & \
595 (oddparity((Nt
>> 8) & 0xFF) == ((parity
[2]) ^ oddparity((NtEnc
>> 8) & 0xFF) ^ BIT(Ks1
,0)))) ? 1 : 0;
600 //-----------------------------------------------------------------------------
601 // MIFARE nested authentication.
603 //-----------------------------------------------------------------------------
604 void MifareNested(uint32_t arg0
, uint32_t arg1
, uint32_t calibrate
, uint8_t *datain
)
607 uint8_t blockNo
= arg0
& 0xff;
608 uint8_t keyType
= (arg0
>> 8) & 0xff;
609 uint8_t targetBlockNo
= arg1
& 0xff;
610 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
611 uint64_t ui64Key
= 0;
613 ui64Key
= bytes_to_num(datain
, 6);
616 uint16_t rtr
, i
, j
, len
;
618 static uint16_t dmin
, dmax
;
620 uint32_t cuid
, nt1
, nt2
, nttmp
, nttest
, par
, ks1
;
621 uint32_t target_nt
[2], target_ks
[2];
623 uint8_t par_array
[4];
625 struct Crypto1State mpcs
= {0, 0};
626 struct Crypto1State
*pcs
;
628 uint8_t* receivedAnswer
= mifare_get_bigbufptr();
630 uint32_t auth1_time
, auth2_time
;
631 static uint16_t delta_time
;
634 iso14a_clear_trace();
635 iso14a_set_tracing(false);
637 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
643 // statistics on nonce distance
644 if (calibrate
) { // for first call only. Otherwise reuse previous calibration
651 for (rtr
= 0; rtr
< 17; rtr
++) {
653 // prepare next select. No need to power down the card.
654 if(mifare_classic_halt(pcs
, cuid
)) {
655 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
660 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
661 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
667 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
668 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
674 auth2_time
= auth1_time
+ delta_time
;
678 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_NESTED
, &nt2
, &auth2_time
)) {
679 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error");
684 nttmp
= prng_successor(nt1
, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160
685 for (i
= 101; i
< 1200; i
++) {
686 nttmp
= prng_successor(nttmp
, 1);
687 if (nttmp
== nt2
) break;
697 delta_time
= auth2_time
- auth1_time
+ 32; // allow some slack for proper timing
699 if (MF_DBGLEVEL
>= 3) Dbprintf("Nested: calibrating... ntdist=%d", i
);
703 if (rtr
<= 1) return;
705 davg
= (davg
+ (rtr
- 1)/2) / (rtr
- 1);
707 if (MF_DBGLEVEL
>= 3) Dbprintf("min=%d max=%d avg=%d, delta_time=%d", dmin
, dmax
, davg
, delta_time
);
715 // -------------------------------------------------------------------------------------------------
719 // get crypted nonces for target sector
720 for(i
=0; i
< 2; i
++) { // look for exactly two different nonces
723 while(target_nt
[i
] == 0) { // continue until we have an unambiguous nonce
725 // prepare next select. No need to power down the card.
726 if(mifare_classic_halt(pcs
, cuid
)) {
727 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
731 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
732 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
737 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
738 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
742 // nested authentication
743 auth2_time
= auth1_time
+ delta_time
;
744 len
= mifare_sendcmd_shortex(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType
& 0x01), targetBlockNo
, receivedAnswer
, &par
, &auth2_time
);
746 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error len=%d", len
);
750 nt2
= bytes_to_num(receivedAnswer
, 4);
751 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i
+1, nt1
, nt2
, par
);
753 // Parity validity check
754 for (j
= 0; j
< 4; j
++) {
755 par_array
[j
] = (oddparity(receivedAnswer
[j
]) != ((par
& 0x08) >> 3));
760 nttest
= prng_successor(nt1
, dmin
- 1);
761 for (j
= dmin
; j
< dmax
+ 1; j
++) {
762 nttest
= prng_successor(nttest
, 1);
765 if (valid_nonce(nttest
, nt2
, ks1
, par_array
)){
766 if (ncount
> 0) { // we are only interested in disambiguous nonces, try again
767 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i
+1, j
);
771 target_nt
[i
] = nttest
;
774 if (i
== 1 && target_nt
[1] == target_nt
[0]) { // we need two different nonces
776 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j
);
779 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i
+1, j
);
782 if (target_nt
[i
] == 0 && j
== dmax
+1 && MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i
+1);
788 // ----------------------------- crypto1 destroy
789 crypto1_destroy(pcs
);
792 memset(uid
, 0x44, 4);
793 LogTrace(uid
, 4, 0, 0, TRUE
);
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
);
810 iso14a_set_tracing(TRUE
);
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
;
839 iso14a_clear_trace();
840 iso14a_set_tracing(TRUE
);
842 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
849 for (i
= 0; i
< keyCount
; i
++) {
850 // FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
852 // FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_MOD);
853 // prepare next select by sending a HALT. There is no need to power down the card.
854 if(mifare_classic_halt(pcs
, cuid
)) {
855 if (MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Halt error");
860 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
861 if (OLD_MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Can't select card");
865 ui64Key
= bytes_to_num(datain
+ i
* 6, 6);
866 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
874 // ----------------------------- crypto1 destroy
875 crypto1_destroy(pcs
);
878 memset(uid
, 0x44, 4);
879 LogTrace(uid
, 4, 0, 0, TRUE
);
882 cmd_send(CMD_ACK
,isOK
,0,0,datain
+ i
* 6,6);
886 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
889 // restore debug level
890 MF_DBGLEVEL
= OLD_MF_DBGLEVEL
;
893 //-----------------------------------------------------------------------------
894 // MIFARE commands set debug level
896 //-----------------------------------------------------------------------------
897 void MifareSetDbgLvl(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
899 Dbprintf("Debug level: %d", MF_DBGLEVEL
);
902 //-----------------------------------------------------------------------------
903 // Work with emulator memory
905 //-----------------------------------------------------------------------------
906 void MifareEMemClr(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
910 void MifareEMemSet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
911 emlSetMem(datain
, arg0
, arg1
); // data, block num, blocks count
914 void MifareEMemGet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
915 // UsbCommand ack = {CMD_ACK, {arg0, arg1, 0}};
918 emlGetMem(buf
, arg0
, arg1
); // data, block num, blocks count
921 cmd_send(CMD_ACK
,arg0
,arg1
,0,buf
,48);
922 // UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));
926 //-----------------------------------------------------------------------------
927 // Load a card into the emulator memory
929 //-----------------------------------------------------------------------------
930 void MifareECardLoad(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
932 uint8_t sectorNo
= 0;
933 uint8_t keyType
= arg1
;
934 uint64_t ui64Key
= 0;
936 struct Crypto1State mpcs
= {0, 0};
937 struct Crypto1State
*pcs
;
941 byte_t dataoutbuf
[16];
942 byte_t dataoutbuf2
[16];
946 iso14a_clear_trace();
947 iso14a_set_tracing(false);
949 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
956 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
957 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
961 for (i
= 0; i
< 16; i
++) {
963 ui64Key
= emlGetKey(sectorNo
, keyType
);
966 if(mifare_classic_auth(pcs
, cuid
, sectorNo
* 4, keyType
, ui64Key
, AUTH_FIRST
)) {
967 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%d]. Auth error", i
);
971 if(mifare_classic_auth(pcs
, cuid
, sectorNo
* 4, keyType
, ui64Key
, AUTH_NESTED
)) {
972 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%d]. Auth nested error", i
);
977 if(mifare_classic_readblock(pcs
, cuid
, sectorNo
* 4 + 0, dataoutbuf
)) {
978 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block 0 error");
981 emlSetMem(dataoutbuf
, sectorNo
* 4 + 0, 1);
983 if(mifare_classic_readblock(pcs
, cuid
, sectorNo
* 4 + 1, dataoutbuf
)) {
984 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block 1 error");
987 emlSetMem(dataoutbuf
, sectorNo
* 4 + 1, 1);
989 if(mifare_classic_readblock(pcs
, cuid
, sectorNo
* 4 + 2, dataoutbuf
)) {
990 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block 2 error");
993 emlSetMem(dataoutbuf
, sectorNo
* 4 + 2, 1);
995 // get block 3 bytes 6-9
996 if(mifare_classic_readblock(pcs
, cuid
, sectorNo
* 4 + 3, dataoutbuf
)) {
997 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block 3 error");
1000 emlGetMem(dataoutbuf2
, sectorNo
* 4 + 3, 1);
1001 memcpy(&dataoutbuf2
[6], &dataoutbuf
[6], 4);
1002 emlSetMem(dataoutbuf2
, sectorNo
* 4 + 3, 1);
1005 if(mifare_classic_halt(pcs
, cuid
)) {
1006 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1013 // ----------------------------- crypto1 destroy
1014 crypto1_destroy(pcs
);
1016 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1019 if (MF_DBGLEVEL
>= 2) DbpString("EMUL FILL SECTORS FINISHED");
1021 // add trace trailer
1022 memset(uid
, 0x44, 4);
1023 LogTrace(uid
, 4, 0, 0, TRUE
);
1026 //-----------------------------------------------------------------------------
1027 // MIFARE 1k emulator
1029 //-----------------------------------------------------------------------------
1032 //-----------------------------------------------------------------------------
1033 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
1035 //-----------------------------------------------------------------------------
1036 void MifareCSetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1039 uint8_t needWipe
= arg0
;
1040 // bit 0 - need get UID
1041 // bit 1 - need wupC
1042 // bit 2 - need HALT after sequence
1043 // bit 3 - need init FPGA and field before sequence
1044 // bit 4 - need reset FPGA and LED
1045 uint8_t workFlags
= arg1
;
1046 uint8_t blockNo
= arg2
;
1049 uint8_t wupC1
[] = { 0x40 };
1050 uint8_t wupC2
[] = { 0x43 };
1051 uint8_t wipeC
[] = { 0x41 };
1056 uint8_t d_block
[18];
1059 memset(uid
, 0x00, 10);
1060 uint8_t* receivedAnswer
= mifare_get_bigbufptr();
1062 if (workFlags
& 0x08) {
1064 iso14a_clear_trace();
1065 iso14a_set_tracing(TRUE
);
1067 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1074 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1076 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1080 // get UID from chip
1081 if (workFlags
& 0x01) {
1082 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
1083 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1087 if(mifare_classic_halt(NULL
, cuid
)) {
1088 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1095 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1096 if(!ReaderReceive(receivedAnswer
) || (receivedAnswer
[0] != 0x0a)) {
1097 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1101 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
1102 if(!ReaderReceive(receivedAnswer
) || (receivedAnswer
[0] != 0x0a)) {
1103 if (MF_DBGLEVEL
>= 1) Dbprintf("wipeC error");
1107 if(mifare_classic_halt(NULL
, cuid
)) {
1108 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1114 if (workFlags
& 0x02) {
1115 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1116 if(!ReaderReceive(receivedAnswer
) || (receivedAnswer
[0] != 0x0a)) {
1117 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1121 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1122 if(!ReaderReceive(receivedAnswer
) || (receivedAnswer
[0] != 0x0a)) {
1123 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1128 if ((mifare_sendcmd_short(NULL
, 0, 0xA0, blockNo
, receivedAnswer
, NULL
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1129 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send command error");
1133 memcpy(d_block
, datain
, 16);
1134 AppendCrc14443a(d_block
, 16);
1136 ReaderTransmit(d_block
, sizeof(d_block
), NULL
);
1137 if ((ReaderReceive(receivedAnswer
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1138 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send data error");
1142 if (workFlags
& 0x04) {
1143 if (mifare_classic_halt(NULL
, cuid
)) {
1144 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1153 // UsbCommand ack = {CMD_ACK, {isOK, 0, 0}};
1154 // if (isOK) memcpy(ack.d.asBytes, uid, 4);
1156 // add trace trailer
1158 * Removed by Martin, the uid is overwritten with 0x44,
1159 * which can 't be intended.
1161 * memset(uid, 0x44, 4);
1162 * LogTrace(uid, 4, 0, 0, TRUE);
1167 cmd_send(CMD_ACK
,isOK
,0,0,uid
,4);
1168 // UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));
1171 if ((workFlags
& 0x10) || (!isOK
)) {
1173 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1178 void MifareCGetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1181 // bit 1 - need wupC
1182 // bit 2 - need HALT after sequence
1183 // bit 3 - need init FPGA and field before sequence
1184 // bit 4 - need reset FPGA and LED
1185 uint8_t workFlags
= arg0
;
1186 uint8_t blockNo
= arg2
;
1189 uint8_t wupC1
[] = { 0x40 };
1190 uint8_t wupC2
[] = { 0x43 };
1197 memset(data
, 0x00, 18);
1198 uint8_t* receivedAnswer
= mifare_get_bigbufptr();
1200 if (workFlags
& 0x08) {
1202 iso14a_clear_trace();
1203 iso14a_set_tracing(TRUE
);
1205 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1212 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1214 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1218 if (workFlags
& 0x02) {
1219 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1220 if(!ReaderReceive(receivedAnswer
) || (receivedAnswer
[0] != 0x0a)) {
1221 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1225 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1226 if(!ReaderReceive(receivedAnswer
) || (receivedAnswer
[0] != 0x0a)) {
1227 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1233 if ((mifare_sendcmd_short(NULL
, 0, 0x30, blockNo
, receivedAnswer
, NULL
) != 18)) {
1234 if (MF_DBGLEVEL
>= 1) Dbprintf("read block send command error");
1237 memcpy(data
, receivedAnswer
, 18);
1239 if (workFlags
& 0x04) {
1240 if (mifare_classic_halt(NULL
, cuid
)) {
1241 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1250 // UsbCommand ack = {CMD_ACK, {isOK, 0, 0}};
1251 // if (isOK) memcpy(ack.d.asBytes, data, 18);
1253 // add trace trailer
1255 * Removed by Martin, this piece of overwrites the 'data' variable
1256 * which is sent two lines down, and is obviously not correct.
1258 * memset(data, 0x44, 4);
1259 * LogTrace(data, 4, 0, 0, TRUE);
1262 cmd_send(CMD_ACK
,isOK
,0,0,data
,18);
1263 // UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));
1266 if ((workFlags
& 0x10) || (!isOK
)) {
1268 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1277 void Mifare_DES_Auth1(uint8_t arg0
, uint8_t *datain
){
1280 byte_t dataoutbuf
[16];
1285 iso14a_clear_trace();
1286 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1293 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
1294 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card, something went wrong before auth");
1297 if(mifare_desfire_des_auth1(cuid
, dataoutbuf
)){
1298 if (MF_DBGLEVEL
>= 1) Dbprintf("Authentication part1: Fail.");
1302 if (MF_DBGLEVEL
>= 2) DbpString("AUTH 1 FINISHED");
1305 cmd_send(CMD_ACK
,isOK
,cuid
,0,dataoutbuf
,11);
1309 //FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1313 void Mifare_DES_Auth2(uint32_t arg0
, uint8_t *datain
){
1315 uint32_t cuid
= arg0
;
1316 uint8_t key
[16]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
1319 byte_t dataoutbuf
[16];
1321 memcpy(key
, datain
, 16);
1323 //iso14a_clear_trace();
1324 //iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
1330 // Dbprintf("Sending %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
1331 // key[0],key[1],key[2],key[3],key[4],key[5],key[6],key[7],key[8],
1332 // key[9],key[10],key[11],key[12],key[13],key[14],key[15]);
1334 if(mifare_desfire_des_auth2(cuid
, key
, dataoutbuf
)){
1335 if (MF_DBGLEVEL
>= 1) Dbprintf("Authentication part2: Fail...");
1338 if (MF_DBGLEVEL
>= 2) DbpString("AUTH 2 FINISHED");
1341 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,12);
1345 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);