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"
20 #include "protocols.h"
22 //-----------------------------------------------------------------------------
23 // Select, Authenticate, Read a MIFARE tag.
25 //-----------------------------------------------------------------------------
26 void MifareReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
29 uint8_t blockNo
= arg0
;
30 uint8_t keyType
= arg1
;
32 ui64Key
= bytes_to_num(datain
, 6);
36 byte_t dataoutbuf
[16];
39 struct Crypto1State mpcs
= {0, 0};
40 struct Crypto1State
*pcs
;
43 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
52 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
53 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
57 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
58 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
62 if(mifare_classic_readblock(pcs
, cuid
, blockNo
, dataoutbuf
)) {
63 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block error");
67 if(mifare_classic_halt(pcs
, cuid
)) {
68 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
76 // ----------------------------- crypto1 destroy
79 if (MF_DBGLEVEL
>= 2) DbpString("READ BLOCK FINISHED");
82 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16);
85 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
89 void MifareUC_Auth(uint8_t arg0
, uint8_t *keybytes
){
91 bool turnOffField
= (arg0
== 1);
93 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
95 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
99 if(!iso14443a_select_card(NULL
, NULL
, NULL
)) {
100 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
105 if(!mifare_ultra_auth(keybytes
)){
106 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication failed");
112 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
115 cmd_send(CMD_ACK
,1,0,0,0,0);
119 // Arg1 = UsePwd bool
120 // datain = PWD bytes,
121 void MifareUReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
)
123 uint8_t blockNo
= arg0
;
124 byte_t dataout
[16] = {0x00};
125 bool useKey
= (arg1
== 1); //UL_C
126 bool usePwd
= (arg1
== 2); //UL_EV1/NTAG
130 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
134 int len
= iso14443a_select_card(NULL
, NULL
, NULL
);
136 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%02X)",len
);
141 // UL-C authentication
143 uint8_t key
[16] = {0x00};
144 memcpy(key
, datain
, sizeof(key
) );
146 if ( !mifare_ultra_auth(key
) ) {
152 // UL-EV1 / NTAG authentication
154 uint8_t pwd
[4] = {0x00};
155 memcpy(pwd
, datain
, 4);
156 uint8_t pack
[4] = {0,0,0,0};
157 if (!mifare_ul_ev1_auth(pwd
, pack
)) {
163 if( mifare_ultra_readblock(blockNo
, dataout
) ) {
164 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Read block error");
169 if( mifare_ultra_halt() ) {
170 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Halt error");
175 cmd_send(CMD_ACK
,1,0,0,dataout
,16);
176 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
180 //-----------------------------------------------------------------------------
181 // Select, Authenticate, Read a MIFARE tag.
182 // read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)
183 //-----------------------------------------------------------------------------
184 void MifareReadSector(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
187 uint8_t sectorNo
= arg0
;
188 uint8_t keyType
= arg1
;
189 uint64_t ui64Key
= 0;
190 ui64Key
= bytes_to_num(datain
, 6);
194 byte_t dataoutbuf
[16 * 16];
197 struct Crypto1State mpcs
= {0, 0};
198 struct Crypto1State
*pcs
;
201 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
210 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
212 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
216 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
218 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
221 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
222 if(mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
+ 16 * blockNo
)) {
224 if (MF_DBGLEVEL
>= 1) Dbprintf("Read sector %2d block %2d error", sectorNo
, blockNo
);
229 if(mifare_classic_halt(pcs
, cuid
)) {
230 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
233 // ----------------------------- crypto1 destroy
234 crypto1_destroy(pcs
);
236 if (MF_DBGLEVEL
>= 2) DbpString("READ SECTOR FINISHED");
239 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16*NumBlocksPerSector(sectorNo
));
243 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
247 // arg0 = blockNo (start)
248 // arg1 = Pages (number of blocks)
250 // datain = KEY bytes
251 void MifareUReadCard(uint8_t arg0
, uint16_t arg1
, uint8_t arg2
, uint8_t *datain
)
255 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
257 // free eventually allocated BigBuf memory
262 uint8_t blockNo
= arg0
;
263 uint16_t blocks
= arg1
;
264 bool useKey
= (arg2
== 1); //UL_C
265 bool usePwd
= (arg2
== 2); //UL_EV1/NTAG
266 uint32_t countblocks
= 0;
267 uint8_t *dataout
= BigBuf_malloc(CARD_MEMORY_SIZE
);
268 if (dataout
== NULL
){
269 Dbprintf("out of memory");
274 int len
= iso14443a_select_card(NULL
, NULL
, NULL
);
276 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%d)",len
);
281 // UL-C authentication
283 uint8_t key
[16] = {0x00};
284 memcpy(key
, datain
, sizeof(key
) );
286 if ( !mifare_ultra_auth(key
) ) {
292 // UL-EV1 / NTAG authentication
294 uint8_t pwd
[4] = {0x00};
295 memcpy(pwd
, datain
, sizeof(pwd
));
296 uint8_t pack
[4] = {0,0,0,0};
298 if (!mifare_ul_ev1_auth(pwd
, pack
)){
304 for (int i
= 0; i
< blocks
; i
++){
305 if ((i
*4) + 4 >= CARD_MEMORY_SIZE
) {
306 Dbprintf("Data exceeds buffer!!");
310 len
= mifare_ultra_readblock(blockNo
+ i
, dataout
+ 4 * i
);
313 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Read block %d error",i
);
314 // if no blocks read - error out
319 //stop at last successful read block and return what we got
327 len
= mifare_ultra_halt();
329 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Halt error");
334 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) Dbprintf("Blocks read %d", countblocks
);
338 cmd_send(CMD_ACK
, 1, countblocks
, BigBuf_max_traceLen(), 0, 0);
339 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
344 //-----------------------------------------------------------------------------
345 // Select, Authenticate, Write a MIFARE tag.
347 //-----------------------------------------------------------------------------
348 void MifareWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
351 uint8_t blockNo
= arg0
;
352 uint8_t keyType
= arg1
;
353 uint64_t ui64Key
= 0;
354 byte_t blockdata
[16];
356 ui64Key
= bytes_to_num(datain
, 6);
357 memcpy(blockdata
, datain
+ 10, 16);
363 struct Crypto1State mpcs
= {0, 0};
364 struct Crypto1State
*pcs
;
367 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 /* // Command not needed but left for future testing
416 void MifareUWriteBlockCompat(uint8_t arg0, uint8_t *datain)
418 uint8_t blockNo = arg0;
419 byte_t blockdata[16] = {0x00};
421 memcpy(blockdata, datain, 16);
423 uint8_t uid[10] = {0x00};
425 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
428 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
430 if(!iso14443a_select_card(uid, NULL, NULL)) {
431 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
436 if(mifare_ultra_writeblock_compat(blockNo, blockdata)) {
437 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
441 if(mifare_ultra_halt()) {
442 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
447 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");
449 cmd_send(CMD_ACK,1,0,0,0,0);
450 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
455 // Arg0 : Block to write to.
456 // Arg1 : 0 = use no authentication.
457 // 1 = use 0x1A authentication.
458 // 2 = use 0x1B authentication.
459 // datain : 4 first bytes is data to be written.
460 // : 4/16 next bytes is authentication key.
461 void MifareUWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
)
463 uint8_t blockNo
= arg0
;
464 bool useKey
= (arg1
== 1); //UL_C
465 bool usePwd
= (arg1
== 2); //UL_EV1/NTAG
466 byte_t blockdata
[4] = {0x00};
468 memcpy(blockdata
, datain
,4);
472 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
476 if(!iso14443a_select_card(NULL
, NULL
, NULL
)) {
477 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
482 // UL-C authentication
484 uint8_t key
[16] = {0x00};
485 memcpy(key
, datain
+4, sizeof(key
) );
487 if ( !mifare_ultra_auth(key
) ) {
493 // UL-EV1 / NTAG authentication
495 uint8_t pwd
[4] = {0x00};
496 memcpy(pwd
, datain
+4, 4);
497 uint8_t pack
[4] = {0,0,0,0};
498 if (!mifare_ul_ev1_auth(pwd
, pack
)) {
504 if(mifare_ultra_writeblock(blockNo
, blockdata
)) {
505 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
510 if(mifare_ultra_halt()) {
511 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
516 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
518 cmd_send(CMD_ACK
,1,0,0,0,0);
519 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
523 void MifareUSetPwd(uint8_t arg0
, uint8_t *datain
){
525 uint8_t pwd
[16] = {0x00};
526 byte_t blockdata
[4] = {0x00};
528 memcpy(pwd
, datain
, 16);
530 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
531 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
535 if(!iso14443a_select_card(NULL
, NULL
, NULL
)) {
536 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
541 blockdata
[0] = pwd
[7];
542 blockdata
[1] = pwd
[6];
543 blockdata
[2] = pwd
[5];
544 blockdata
[3] = pwd
[4];
545 if(mifare_ultra_writeblock( 44, blockdata
)) {
546 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
551 blockdata
[0] = pwd
[3];
552 blockdata
[1] = pwd
[2];
553 blockdata
[2] = pwd
[1];
554 blockdata
[3] = pwd
[0];
555 if(mifare_ultra_writeblock( 45, blockdata
)) {
556 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
561 blockdata
[0] = pwd
[15];
562 blockdata
[1] = pwd
[14];
563 blockdata
[2] = pwd
[13];
564 blockdata
[3] = pwd
[12];
565 if(mifare_ultra_writeblock( 46, blockdata
)) {
566 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
571 blockdata
[0] = pwd
[11];
572 blockdata
[1] = pwd
[10];
573 blockdata
[2] = pwd
[9];
574 blockdata
[3] = pwd
[8];
575 if(mifare_ultra_writeblock( 47, blockdata
)) {
576 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
581 if(mifare_ultra_halt()) {
582 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
587 cmd_send(CMD_ACK
,1,0,0,0,0);
588 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
592 // Return 1 if the nonce is invalid else return 0
593 int valid_nonce(uint32_t Nt
, uint32_t NtEnc
, uint32_t Ks1
, uint8_t *parity
) {
594 return ((oddparity((Nt
>> 24) & 0xFF) == ((parity
[0]) ^ oddparity((NtEnc
>> 24) & 0xFF) ^ BIT(Ks1
,16))) & \
595 (oddparity((Nt
>> 16) & 0xFF) == ((parity
[1]) ^ oddparity((NtEnc
>> 16) & 0xFF) ^ BIT(Ks1
,8))) & \
596 (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
, ks1
;
622 uint32_t target_nt
[2], target_ks
[2];
624 uint8_t par_array
[4];
626 struct Crypto1State mpcs
= {0, 0};
627 struct Crypto1State
*pcs
;
629 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
631 uint32_t auth1_time
, auth2_time
;
632 static uint16_t delta_time
;
636 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
638 // free eventually allocated BigBuf memory
641 if (calibrate
) clear_trace();
644 // statistics on nonce distance
646 #define NESTED_MAX_TRIES 12
647 uint16_t unsuccessfull_tries
= 0;
648 if (calibrate
) { // for first call only. Otherwise reuse previous calibration
656 for (rtr
= 0; rtr
< 17; rtr
++) {
658 // Test if the action was cancelled
664 // prepare next select. No need to power down the card.
665 if(mifare_classic_halt(pcs
, cuid
)) {
666 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
671 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
672 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
678 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
679 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
685 auth2_time
= auth1_time
+ delta_time
;
689 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_NESTED
, &nt2
, &auth2_time
)) {
690 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error");
695 nttmp
= prng_successor(nt1
, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160
696 for (i
= 101; i
< 1200; i
++) {
697 nttmp
= prng_successor(nttmp
, 1);
698 if (nttmp
== nt2
) break;
708 delta_time
= auth2_time
- auth1_time
+ 32; // allow some slack for proper timing
710 if (MF_DBGLEVEL
>= 3) Dbprintf("Nested: calibrating... ntdist=%d", i
);
712 unsuccessfull_tries
++;
713 if (unsuccessfull_tries
> NESTED_MAX_TRIES
) { // card isn't vulnerable to nested attack (random numbers are not predictable)
719 davg
= (davg
+ (rtr
- 1)/2) / (rtr
- 1);
721 if (MF_DBGLEVEL
>= 3) Dbprintf("rtr=%d isOK=%d min=%d max=%d avg=%d, delta_time=%d", rtr
, isOK
, dmin
, dmax
, davg
, delta_time
);
728 // -------------------------------------------------------------------------------------------------
732 // get crypted nonces for target sector
733 for(i
=0; i
< 2 && !isOK
; i
++) { // look for exactly two different nonces
736 while(target_nt
[i
] == 0) { // continue until we have an unambiguous nonce
738 // prepare next select. No need to power down the card.
739 if(mifare_classic_halt(pcs
, cuid
)) {
740 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
744 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
745 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
750 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
751 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
755 // nested authentication
756 auth2_time
= auth1_time
+ delta_time
;
757 len
= mifare_sendcmd_short(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType
& 0x01), targetBlockNo
, receivedAnswer
, par
, &auth2_time
);
759 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error len=%d", len
);
763 nt2
= bytes_to_num(receivedAnswer
, 4);
764 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i
+1, nt1
, nt2
, par
[0]);
766 // Parity validity check
767 for (j
= 0; j
< 4; j
++) {
768 par_array
[j
] = (oddparity(receivedAnswer
[j
]) != ((par
[0] >> (7-j
)) & 0x01));
772 nttest
= prng_successor(nt1
, dmin
- 1);
773 for (j
= dmin
; j
< dmax
+ 1; j
++) {
774 nttest
= prng_successor(nttest
, 1);
777 if (valid_nonce(nttest
, nt2
, ks1
, par_array
)){
778 if (ncount
> 0) { // we are only interested in disambiguous nonces, try again
779 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i
+1, j
);
783 target_nt
[i
] = nttest
;
786 if (i
== 1 && target_nt
[1] == target_nt
[0]) { // we need two different nonces
788 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j
);
791 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i
+1, j
);
794 if (target_nt
[i
] == 0 && j
== dmax
+1 && MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i
+1);
800 // ----------------------------- crypto1 destroy
801 crypto1_destroy(pcs
);
803 byte_t buf
[4 + 4 * 4];
804 memcpy(buf
, &cuid
, 4);
805 memcpy(buf
+4, &target_nt
[0], 4);
806 memcpy(buf
+8, &target_ks
[0], 4);
807 memcpy(buf
+12, &target_nt
[1], 4);
808 memcpy(buf
+16, &target_ks
[1], 4);
811 cmd_send(CMD_ACK
, isOK
, 0, targetBlockNo
+ (targetKeyType
* 0x100), buf
, sizeof(buf
));
814 if (MF_DBGLEVEL
>= 3) DbpString("NESTED FINISHED");
816 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
821 //-----------------------------------------------------------------------------
822 // MIFARE check keys. key count up to 85.
824 //-----------------------------------------------------------------------------
825 void MifareChkKeys(uint16_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
828 uint8_t blockNo
= arg0
& 0xff;
829 uint8_t keyType
= (arg0
>> 8) & 0xff;
830 bool clearTrace
= arg1
;
831 uint8_t keyCount
= arg2
;
832 uint64_t ui64Key
= 0;
839 struct Crypto1State mpcs
= {0, 0};
840 struct Crypto1State
*pcs
;
844 int OLD_MF_DBGLEVEL
= MF_DBGLEVEL
;
845 MF_DBGLEVEL
= MF_DBG_NONE
;
850 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
852 if (clearTrace
) clear_trace();
855 for (i
= 0; i
< keyCount
; i
++) {
856 if(mifare_classic_halt(pcs
, cuid
)) {
857 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 cmd_send(CMD_ACK
,isOK
,0,0,datain
+ i
* 6,6);
881 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
885 // restore debug level
886 MF_DBGLEVEL
= OLD_MF_DBGLEVEL
;
889 //-----------------------------------------------------------------------------
890 // MIFARE commands set debug level
892 //-----------------------------------------------------------------------------
893 void MifareSetDbgLvl(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
895 Dbprintf("Debug level: %d", MF_DBGLEVEL
);
898 //-----------------------------------------------------------------------------
899 // Work with emulator memory
901 // Note: we call FpgaDownloadAndGo(FPGA_BITSTREAM_HF) here although FPGA is not
902 // involved in dealing with emulator memory. But if it is called later, it might
903 // destroy the Emulator Memory.
904 //-----------------------------------------------------------------------------
906 void MifareEMemClr(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
907 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
911 void MifareEMemSet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
912 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
913 //emlSetMem(datain, arg0, arg1); // data, block num, blocks count
914 emlSetMem_xt(datain
, arg0
, arg1
, arg2
); // data, block num, blocks count, block byte width
917 void MifareEMemGet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
918 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
919 byte_t buf
[USB_CMD_DATA_SIZE
];
920 emlGetMem(buf
, arg0
, arg1
); // data, block num, blocks count (max 4)
923 cmd_send(CMD_ACK
,arg0
,arg1
,0,buf
,USB_CMD_DATA_SIZE
);
927 //-----------------------------------------------------------------------------
928 // Load a card into the emulator memory
930 //-----------------------------------------------------------------------------
931 void MifareECardLoad(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
932 uint8_t numSectors
= arg0
;
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];
948 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
955 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
957 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
960 for (uint8_t sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
961 ui64Key
= emlGetKey(sectorNo
, keyType
);
963 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
965 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo
);
969 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_NESTED
)) {
971 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo
);
976 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
977 if(isOK
&& mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
)) {
979 if (MF_DBGLEVEL
>= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo
, blockNo
);
983 if (blockNo
< NumBlocksPerSector(sectorNo
) - 1) {
984 emlSetMem(dataoutbuf
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
985 } else { // sector trailer, keep the keys, set only the AC
986 emlGetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
987 memcpy(&dataoutbuf2
[6], &dataoutbuf
[6], 4);
988 emlSetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
995 if(mifare_classic_halt(pcs
, cuid
)) {
996 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
999 // ----------------------------- crypto1 destroy
1000 crypto1_destroy(pcs
);
1002 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1005 if (MF_DBGLEVEL
>= 2) DbpString("EMUL FILL SECTORS FINISHED");
1011 //-----------------------------------------------------------------------------
1012 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
1014 // PARAMS - workFlags
1015 // bit 0 - need get UID
1016 // bit 1 - need wupC
1017 // bit 2 - need HALT after sequence
1018 // bit 3 - need turn on FPGA before sequence
1019 // bit 4 - need turn off FPGA
1020 // bit 5 - need to set datain instead of issuing USB reply (called via ARM for StandAloneMode14a)
1021 // bit 6 - wipe tag.
1022 //-----------------------------------------------------------------------------
1023 // magic uid card generation 1 commands
1024 uint8_t wupC1
[] = { MIFARE_MAGICWUPC1
};
1025 uint8_t wupC2
[] = { MIFARE_MAGICWUPC2
};
1026 uint8_t wipeC
[] = { MIFARE_MAGICWIPEC
};
1028 void MifareCSetBlock(uint32_t arg0
, uint32_t arg1
, uint8_t *datain
){
1031 uint8_t workFlags
= arg0
;
1032 uint8_t blockNo
= arg1
;
1034 Dbprintf("ICE :: CSetBlocks Flags %02x", workFlags
);
1037 uint8_t uid
[10] = {0x00};
1038 uint8_t data
[18] = {0x00};
1041 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1042 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1044 if (workFlags
& MAGIC_INIT
) {
1047 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1052 // read UID and return to client
1053 if (workFlags
& MAGIC_UID
) {
1054 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
1055 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
1056 OnErrorMagic(MAGIC_UID
);
1060 // wipe tag, fill it with zeros
1061 if (workFlags
& MAGIC_WIPE
){
1062 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1063 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1064 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("wupC1 error");
1065 OnErrorMagic(MAGIC_WIPE
);
1068 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
1069 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1070 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("wipeC error");
1071 OnErrorMagic(MAGIC_WIPE
);
1076 if (workFlags
& MAGIC_WUPC
) {
1077 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1078 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1079 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("wupC1 error");
1080 OnErrorMagic(MAGIC_WUPC
);
1083 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1084 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1085 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("wupC2 error");
1086 OnErrorMagic(MAGIC_WUPC
);
1090 if ((mifare_sendcmd_short(NULL
, 0, ISO14443A_CMD_WRITEBLOCK
, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1091 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("write block send command error");
1095 memcpy(data
, datain
, 16);
1096 AppendCrc14443a(data
, 16);
1098 ReaderTransmit(data
, sizeof(data
), NULL
);
1099 if ((ReaderReceive(receivedAnswer
, receivedAnswerPar
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1100 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("write block send data error");
1104 if (workFlags
& MAGIC_OFF
)
1105 mifare_classic_halt_ex(NULL
);
1108 // check if uid is cuid?
1109 cmd_send(CMD_ACK
,1,0,0,uid
,sizeof(uid
));
1112 if (workFlags
& MAGIC_OFF
)
1116 void MifareCGetBlock(uint32_t arg0
, uint32_t arg1
, uint8_t *datain
){
1118 uint8_t workFlags
= arg0
;
1119 uint8_t blockNo
= arg1
;
1122 uint8_t data
[MAX_MIFARE_FRAME_SIZE
];
1123 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1124 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1126 memset(data
, 0x00, sizeof(data
));
1128 if (workFlags
& MAGIC_INIT
) {
1131 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1136 if (workFlags
& MAGIC_WUPC
) {
1137 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1138 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1139 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("wupC1 error");
1140 OnErrorMagic(MAGIC_WUPC
);
1143 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1144 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1145 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("wupC2 error");
1146 OnErrorMagic(MAGIC_WUPC
);
1151 if ((mifare_sendcmd_short(NULL
, 0, ISO14443A_CMD_READBLOCK
, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 18)) {
1152 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("read block send command error");
1156 memcpy(data
, receivedAnswer
, sizeof(data
));
1159 if (workFlags
& MAGIC_HALT
)
1160 mifare_classic_halt_ex(NULL
);
1164 // if MAGIC_DATAIN, the data stays on device side.
1165 if (workFlags
& MAGIC_DATAIN
)
1166 memcpy(datain
, data
, sizeof(data
));
1168 cmd_send(CMD_ACK
,1,0,0,data
,sizeof(data
));
1172 if (workFlags
& MAGIC_OFF
)
1176 void MifareCIdent(){
1180 uint8_t receivedAnswer
[1];
1181 uint8_t receivedAnswerPar
[1];
1183 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1184 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1188 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1189 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1193 // removed the if, since some magic tags misbehavies and send an answer to it.
1194 mifare_classic_halt(NULL
, 0);
1195 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
1198 void OnSuccessMagic(){
1199 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1203 void OnErrorMagic(uint8_t reason
){
1204 // ACK, ISOK, reason,0,0,0
1205 cmd_send(CMD_ACK
,0,reason
,0,0,0);
1209 void MifareCollectNonces(uint32_t arg0
, uint32_t arg1
){
1216 void Mifare_DES_Auth1(uint8_t arg0
, uint8_t *datain
){
1218 byte_t dataout
[11] = {0x00};
1219 uint8_t uid
[10] = {0x00};
1220 uint32_t cuid
= 0x00;
1222 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1225 int len
= iso14443a_select_card(uid
, NULL
, &cuid
);
1227 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
1232 if(mifare_desfire_des_auth1(cuid
, dataout
)){
1233 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication part1: Fail.");
1238 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 1 FINISHED");
1239 cmd_send(CMD_ACK
,1,cuid
,0,dataout
, sizeof(dataout
));
1242 void Mifare_DES_Auth2(uint32_t arg0
, uint8_t *datain
){
1244 uint32_t cuid
= arg0
;
1245 uint8_t key
[16] = {0x00};
1246 byte_t dataout
[12] = {0x00};
1249 memcpy(key
, datain
, 16);
1251 isOK
= mifare_desfire_des_auth2(cuid
, key
, dataout
);
1254 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) Dbprintf("Authentication part2: Failed");
1259 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 2 FINISHED");
1261 cmd_send(CMD_ACK
, isOK
, 0, 0, dataout
, sizeof(dataout
));
1262 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);