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 //-----------------------------------------------------------------------------
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
;
45 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
);
90 void MifareUC_Auth(uint8_t arg0
, uint8_t *keybytes
){
92 bool turnOffField
= (arg0
== 1);
94 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
96 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
98 if(!iso14443a_select_card(NULL
, NULL
, NULL
)) {
99 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
104 if(!mifare_ultra_auth(keybytes
)){
105 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication failed");
111 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
114 cmd_send(CMD_ACK
,1,0,0,0,0);
118 // Arg1 = UsePwd bool
119 // datain = PWD bytes,
120 void MifareUReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
)
122 uint8_t blockNo
= arg0
;
123 byte_t dataout
[16] = {0x00};
124 bool useKey
= (arg1
== 1); //UL_C
125 bool usePwd
= (arg1
== 2); //UL_EV1/NTAG
130 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
132 int len
= iso14443a_select_card(NULL
, NULL
, NULL
);
134 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%02X)",len
);
139 // UL-C authentication
141 uint8_t key
[16] = {0x00};
142 memcpy(key
, datain
, sizeof(key
) );
144 if ( !mifare_ultra_auth(key
) ) {
150 // UL-EV1 / NTAG authentication
152 uint8_t pwd
[4] = {0x00};
153 memcpy(pwd
, datain
, 4);
154 uint8_t pack
[4] = {0,0,0,0};
155 if (!mifare_ul_ev1_auth(pwd
, pack
)) {
161 if( mifare_ultra_readblock(blockNo
, dataout
) ) {
162 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Read block error");
167 if( mifare_ultra_halt() ) {
168 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Halt error");
173 cmd_send(CMD_ACK
,1,0,0,dataout
,16);
174 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
178 //-----------------------------------------------------------------------------
179 // Select, Authenticate, Read a MIFARE tag.
180 // read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)
181 //-----------------------------------------------------------------------------
182 void MifareReadSector(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
185 uint8_t sectorNo
= arg0
;
186 uint8_t keyType
= arg1
;
187 uint64_t ui64Key
= 0;
188 ui64Key
= bytes_to_num(datain
, 6);
192 byte_t dataoutbuf
[16 * 16];
195 struct Crypto1State mpcs
= {0, 0};
196 struct Crypto1State
*pcs
;
202 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
209 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
211 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
215 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
217 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
220 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
221 if(mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
+ 16 * blockNo
)) {
223 if (MF_DBGLEVEL
>= 1) Dbprintf("Read sector %2d block %2d error", sectorNo
, blockNo
);
228 if(mifare_classic_halt(pcs
, cuid
)) {
229 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
232 // ----------------------------- crypto1 destroy
233 crypto1_destroy(pcs
);
235 if (MF_DBGLEVEL
>= 2) DbpString("READ SECTOR FINISHED");
238 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16*NumBlocksPerSector(sectorNo
));
242 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
246 // arg0 = blockNo (start)
247 // arg1 = Pages (number of blocks)
249 // datain = KEY bytes
250 void MifareUReadCard(uint8_t arg0
, uint16_t arg1
, uint8_t arg2
, uint8_t *datain
)
252 // free eventually allocated BigBuf memory
258 uint8_t blockNo
= arg0
;
259 uint16_t blocks
= arg1
;
260 bool useKey
= (arg2
== 1); //UL_C
261 bool usePwd
= (arg2
== 2); //UL_EV1/NTAG
262 uint32_t countblocks
= 0;
263 uint8_t *dataout
= BigBuf_malloc(CARD_MEMORY_SIZE
);
264 if (dataout
== NULL
){
265 Dbprintf("out of memory");
272 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
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
);
337 cmd_send(CMD_ACK
, 1, countblocks
, countblocks
, 0, 0);
338 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
342 //-----------------------------------------------------------------------------
343 // Select, Authenticate, Write a MIFARE tag.
345 //-----------------------------------------------------------------------------
346 void MifareWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
349 uint8_t blockNo
= arg0
;
350 uint8_t keyType
= arg1
;
351 uint64_t ui64Key
= 0;
352 byte_t blockdata
[16];
354 ui64Key
= bytes_to_num(datain
, 6);
355 memcpy(blockdata
, datain
+ 10, 16);
361 struct Crypto1State mpcs
= {0, 0};
362 struct Crypto1State
*pcs
;
368 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
375 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
376 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
380 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
381 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
385 if(mifare_classic_writeblock(pcs
, cuid
, blockNo
, blockdata
)) {
386 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
390 if(mifare_classic_halt(pcs
, cuid
)) {
391 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
399 // ----------------------------- crypto1 destroy
400 crypto1_destroy(pcs
);
402 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
405 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
410 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
414 void MifareUWriteBlock(uint8_t arg0
, uint8_t *datain
)
416 uint8_t blockNo
= arg0
;
417 byte_t blockdata
[16] = {0x00};
419 memcpy(blockdata
, datain
, 16);
421 uint8_t uid
[10] = {0x00};
423 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
426 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
428 if(!iso14443a_select_card(uid
, NULL
, NULL
)) {
429 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
434 if(mifare_ultra_writeblock(blockNo
, blockdata
)) {
435 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
439 if(mifare_ultra_halt()) {
440 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
445 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
447 cmd_send(CMD_ACK
,1,0,0,0,0);
448 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
452 // Arg0 : Block to write to.
453 // Arg1 : 0 = use no authentication.
454 // 1 = use 0x1A authentication.
455 // 2 = use 0x1B authentication.
456 // datain : 4 first bytes is data to be written.
457 // : 4/16 next bytes is authentication key.
458 void MifareUWriteBlock_Special(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
)
460 uint8_t blockNo
= arg0
;
461 bool useKey
= (arg1
== 1); //UL_C
462 bool usePwd
= (arg1
== 2); //UL_EV1/NTAG
463 byte_t blockdata
[4] = {0x00};
465 memcpy(blockdata
, datain
, 4);
470 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
472 if(!iso14443a_select_card(NULL
, NULL
, NULL
)) {
473 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
478 // UL-C authentication
480 uint8_t key
[16] = {0x00};
481 memcpy(key
, datain
+4, sizeof(key
) );
483 if ( !mifare_ultra_auth(key
) ) {
489 // UL-EV1 / NTAG authentication
491 uint8_t pwd
[4] = {0x00};
492 memcpy(pwd
, datain
+4, 4);
493 uint8_t pack
[4] = {0,0,0,0};
494 if (!mifare_ul_ev1_auth(pwd
, pack
)) {
500 if(mifare_ultra_special_writeblock(blockNo
, blockdata
)) {
501 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
506 if(mifare_ultra_halt()) {
507 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
512 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
514 cmd_send(CMD_ACK
,1,0,0,0,0);
515 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
519 void MifareUSetPwd(uint8_t arg0
, uint8_t *datain
){
521 uint8_t pwd
[16] = {0x00};
522 byte_t blockdata
[4] = {0x00};
524 memcpy(pwd
, datain
, 16);
526 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
528 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
530 if(!iso14443a_select_card(NULL
, NULL
, NULL
)) {
531 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
536 blockdata
[0] = pwd
[7];
537 blockdata
[1] = pwd
[6];
538 blockdata
[2] = pwd
[5];
539 blockdata
[3] = pwd
[4];
540 if(mifare_ultra_special_writeblock( 44, blockdata
)) {
541 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
546 blockdata
[0] = pwd
[3];
547 blockdata
[1] = pwd
[2];
548 blockdata
[2] = pwd
[1];
549 blockdata
[3] = pwd
[0];
550 if(mifare_ultra_special_writeblock( 45, blockdata
)) {
551 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
556 blockdata
[0] = pwd
[15];
557 blockdata
[1] = pwd
[14];
558 blockdata
[2] = pwd
[13];
559 blockdata
[3] = pwd
[12];
560 if(mifare_ultra_special_writeblock( 46, blockdata
)) {
561 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
566 blockdata
[0] = pwd
[11];
567 blockdata
[1] = pwd
[10];
568 blockdata
[2] = pwd
[9];
569 blockdata
[3] = pwd
[8];
570 if(mifare_ultra_special_writeblock( 47, blockdata
)) {
571 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
576 if(mifare_ultra_halt()) {
577 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
582 cmd_send(CMD_ACK
,1,0,0,0,0);
583 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
587 // Return 1 if the nonce is invalid else return 0
588 int valid_nonce(uint32_t Nt
, uint32_t NtEnc
, uint32_t Ks1
, uint8_t *parity
) {
589 return ((oddparity((Nt
>> 24) & 0xFF) == ((parity
[0]) ^ oddparity((NtEnc
>> 24) & 0xFF) ^ BIT(Ks1
,16))) & \
590 (oddparity((Nt
>> 16) & 0xFF) == ((parity
[1]) ^ oddparity((NtEnc
>> 16) & 0xFF) ^ BIT(Ks1
,8))) & \
591 (oddparity((Nt
>> 8) & 0xFF) == ((parity
[2]) ^ oddparity((NtEnc
>> 8) & 0xFF) ^ BIT(Ks1
,0)))) ? 1 : 0;
595 //-----------------------------------------------------------------------------
596 // MIFARE nested authentication.
598 //-----------------------------------------------------------------------------
599 void MifareNested(uint32_t arg0
, uint32_t arg1
, uint32_t calibrate
, uint8_t *datain
)
602 uint8_t blockNo
= arg0
& 0xff;
603 uint8_t keyType
= (arg0
>> 8) & 0xff;
604 uint8_t targetBlockNo
= arg1
& 0xff;
605 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
606 uint64_t ui64Key
= 0;
608 ui64Key
= bytes_to_num(datain
, 6);
611 uint16_t rtr
, i
, j
, len
;
613 static uint16_t dmin
, dmax
;
615 uint32_t cuid
, nt1
, nt2
, nttmp
, nttest
, ks1
;
617 uint32_t target_nt
[2], target_ks
[2];
619 uint8_t par_array
[4];
621 struct Crypto1State mpcs
= {0, 0};
622 struct Crypto1State
*pcs
;
624 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
626 uint32_t auth1_time
, auth2_time
;
627 static uint16_t delta_time
;
629 // free eventually allocated BigBuf memory
635 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
641 // statistics on nonce distance
642 if (calibrate
) { // for first call only. Otherwise reuse previous calibration
650 for (rtr
= 0; rtr
< 17; rtr
++) {
652 // prepare next select. No need to power down the card.
653 if(mifare_classic_halt(pcs
, cuid
)) {
654 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
659 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
660 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
666 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
667 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
673 auth2_time
= auth1_time
+ delta_time
;
677 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_NESTED
, &nt2
, &auth2_time
)) {
678 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error");
683 nttmp
= prng_successor(nt1
, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160
684 for (i
= 101; i
< 1200; i
++) {
685 nttmp
= prng_successor(nttmp
, 1);
686 if (nttmp
== nt2
) break;
696 delta_time
= auth2_time
- auth1_time
+ 32; // allow some slack for proper timing
698 if (MF_DBGLEVEL
>= 3) Dbprintf("Nested: calibrating... ntdist=%d", i
);
702 if (rtr
<= 1) return;
704 davg
= (davg
+ (rtr
- 1)/2) / (rtr
- 1);
706 if (MF_DBGLEVEL
>= 3) Dbprintf("min=%d max=%d avg=%d, delta_time=%d", dmin
, dmax
, davg
, delta_time
);
714 // -------------------------------------------------------------------------------------------------
718 // get crypted nonces for target sector
719 for(i
=0; i
< 2; i
++) { // look for exactly two different nonces
722 while(target_nt
[i
] == 0) { // continue until we have an unambiguous nonce
724 // prepare next select. No need to power down the card.
725 if(mifare_classic_halt(pcs
, cuid
)) {
726 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
730 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
731 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
736 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
737 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
741 // nested authentication
742 auth2_time
= auth1_time
+ delta_time
;
743 len
= mifare_sendcmd_shortex(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType
& 0x01), targetBlockNo
, receivedAnswer
, par
, &auth2_time
);
745 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error len=%d", len
);
749 nt2
= bytes_to_num(receivedAnswer
, 4);
750 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i
+1, nt1
, nt2
, par
[0]);
752 // Parity validity check
753 for (j
= 0; j
< 4; j
++) {
754 par_array
[j
] = (oddparity(receivedAnswer
[j
]) != ((par
[0] >> (7-j
)) & 0x01));
758 nttest
= prng_successor(nt1
, dmin
- 1);
759 for (j
= dmin
; j
< dmax
+ 1; j
++) {
760 nttest
= prng_successor(nttest
, 1);
763 if (valid_nonce(nttest
, nt2
, ks1
, par_array
)){
764 if (ncount
> 0) { // we are only interested in disambiguous nonces, try again
765 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i
+1, j
);
769 target_nt
[i
] = nttest
;
772 if (i
== 1 && target_nt
[1] == target_nt
[0]) { // we need two different nonces
774 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j
);
777 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i
+1, j
);
780 if (target_nt
[i
] == 0 && j
== dmax
+1 && MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i
+1);
786 // ----------------------------- crypto1 destroy
787 crypto1_destroy(pcs
);
789 byte_t buf
[4 + 4 * 4];
790 memcpy(buf
, &cuid
, 4);
791 memcpy(buf
+4, &target_nt
[0], 4);
792 memcpy(buf
+8, &target_ks
[0], 4);
793 memcpy(buf
+12, &target_nt
[1], 4);
794 memcpy(buf
+16, &target_ks
[1], 4);
797 cmd_send(CMD_ACK
, 0, 2, targetBlockNo
+ (targetKeyType
* 0x100), buf
, sizeof(buf
));
800 if (MF_DBGLEVEL
>= 3) DbpString("NESTED FINISHED");
802 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
807 //-----------------------------------------------------------------------------
808 // MIFARE check keys. key count up to 85.
810 //-----------------------------------------------------------------------------
811 void MifareChkKeys(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
814 uint8_t blockNo
= arg0
;
815 uint8_t keyType
= arg1
;
816 uint8_t keyCount
= arg2
;
817 uint64_t ui64Key
= 0;
824 struct Crypto1State mpcs
= {0, 0};
825 struct Crypto1State
*pcs
;
829 int OLD_MF_DBGLEVEL
= MF_DBGLEVEL
;
830 MF_DBGLEVEL
= MF_DBG_NONE
;
836 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
842 for (i
= 0; i
< keyCount
; i
++) {
843 if(mifare_classic_halt(pcs
, cuid
)) {
844 if (MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Halt error");
847 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
848 if (OLD_MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Can't select card");
852 ui64Key
= bytes_to_num(datain
+ i
* 6, 6);
853 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
861 // ----------------------------- crypto1 destroy
862 crypto1_destroy(pcs
);
865 cmd_send(CMD_ACK
,isOK
,0,0,datain
+ i
* 6,6);
868 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
871 // restore debug level
872 MF_DBGLEVEL
= OLD_MF_DBGLEVEL
;
875 //-----------------------------------------------------------------------------
876 // MIFARE commands set debug level
878 //-----------------------------------------------------------------------------
879 void MifareSetDbgLvl(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
881 Dbprintf("Debug level: %d", MF_DBGLEVEL
);
884 //-----------------------------------------------------------------------------
885 // Work with emulator memory
887 //-----------------------------------------------------------------------------
888 void MifareEMemClr(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
892 void MifareEMemSet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
893 emlSetMem(datain
, arg0
, arg1
); // data, block num, blocks count
896 void MifareEMemGet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
897 byte_t buf
[USB_CMD_DATA_SIZE
];
898 emlGetMem(buf
, arg0
, arg1
); // data, block num, blocks count (max 4)
901 cmd_send(CMD_ACK
,arg0
,arg1
,0,buf
,USB_CMD_DATA_SIZE
);
905 //-----------------------------------------------------------------------------
906 // Load a card into the emulator memory
908 //-----------------------------------------------------------------------------
909 void MifareECardLoad(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
910 uint8_t numSectors
= arg0
;
911 uint8_t keyType
= arg1
;
912 uint64_t ui64Key
= 0;
914 struct Crypto1State mpcs
= {0, 0};
915 struct Crypto1State
*pcs
;
919 byte_t dataoutbuf
[16];
920 byte_t dataoutbuf2
[16];
927 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
935 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
937 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
940 for (uint8_t sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
941 ui64Key
= emlGetKey(sectorNo
, keyType
);
943 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
945 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo
);
949 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_NESTED
)) {
951 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo
);
956 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
957 if(isOK
&& mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
)) {
959 if (MF_DBGLEVEL
>= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo
, blockNo
);
963 if (blockNo
< NumBlocksPerSector(sectorNo
) - 1) {
964 emlSetMem(dataoutbuf
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
965 } else { // sector trailer, keep the keys, set only the AC
966 emlGetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
967 memcpy(&dataoutbuf2
[6], &dataoutbuf
[6], 4);
968 emlSetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
975 if(mifare_classic_halt(pcs
, cuid
)) {
976 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
979 // ----------------------------- crypto1 destroy
980 crypto1_destroy(pcs
);
982 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
985 if (MF_DBGLEVEL
>= 2) DbpString("EMUL FILL SECTORS FINISHED");
990 //-----------------------------------------------------------------------------
991 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
993 //-----------------------------------------------------------------------------
994 void MifareCSetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
997 uint8_t needWipe
= arg0
;
998 // bit 0 - need get UID
1000 // bit 2 - need HALT after sequence
1001 // bit 3 - need init FPGA and field before sequence
1002 // bit 4 - need reset FPGA and LED
1003 uint8_t workFlags
= arg1
;
1004 uint8_t blockNo
= arg2
;
1007 uint8_t wupC1
[] = { 0x40 };
1008 uint8_t wupC2
[] = { 0x43 };
1009 uint8_t wipeC
[] = { 0x41 };
1013 uint8_t uid
[10] = {0x00};
1014 uint8_t d_block
[18] = {0x00};
1017 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1018 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1020 // reset FPGA and LED
1021 if (workFlags
& 0x08) {
1028 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1033 // get UID from chip
1034 if (workFlags
& 0x01) {
1035 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
1036 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1040 if(mifare_classic_halt(NULL
, cuid
)) {
1041 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1048 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1049 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1050 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1054 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
1055 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1056 if (MF_DBGLEVEL
>= 1) Dbprintf("wipeC error");
1060 if(mifare_classic_halt(NULL
, cuid
)) {
1061 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1067 if (workFlags
& 0x02) {
1068 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1069 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1070 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1074 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1075 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1076 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1081 if ((mifare_sendcmd_short(NULL
, 0, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1082 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send command error");
1086 memcpy(d_block
, datain
, 16);
1087 AppendCrc14443a(d_block
, 16);
1089 ReaderTransmit(d_block
, sizeof(d_block
), NULL
);
1090 if ((ReaderReceive(receivedAnswer
, receivedAnswerPar
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1091 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send data error");
1095 if (workFlags
& 0x04) {
1096 if (mifare_classic_halt(NULL
, cuid
)) {
1097 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1107 cmd_send(CMD_ACK
,isOK
,0,0,uid
,4);
1110 if ((workFlags
& 0x10) || (!isOK
)) {
1111 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1117 void MifareCGetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1120 // bit 1 - need wupC
1121 // bit 2 - need HALT after sequence
1122 // bit 3 - need init FPGA and field before sequence
1123 // bit 4 - need reset FPGA and LED
1124 uint8_t workFlags
= arg0
;
1125 uint8_t blockNo
= arg2
;
1128 uint8_t wupC1
[] = { 0x40 };
1129 uint8_t wupC2
[] = { 0x43 };
1133 uint8_t data
[18] = {0x00};
1136 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1137 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1139 if (workFlags
& 0x08) {
1146 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1150 if (workFlags
& 0x02) {
1151 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1152 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1153 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1157 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1158 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1159 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1165 if ((mifare_sendcmd_short(NULL
, 0, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 18)) {
1166 if (MF_DBGLEVEL
>= 1) Dbprintf("read block send command error");
1169 memcpy(data
, receivedAnswer
, 18);
1171 if (workFlags
& 0x04) {
1172 if (mifare_classic_halt(NULL
, cuid
)) {
1173 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1183 cmd_send(CMD_ACK
,isOK
,0,0,data
,18);
1186 if ((workFlags
& 0x10) || (!isOK
)) {
1187 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1192 void MifareCIdent(){
1195 uint8_t wupC1
[] = { 0x40 };
1196 uint8_t wupC2
[] = { 0x43 };
1201 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1202 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1204 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1205 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1209 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1210 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1214 if (mifare_classic_halt(NULL
, 0)) {
1218 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
1221 void MifareCollectNonces(uint32_t arg0
, uint32_t arg1
){
1225 uint32_t iterations
= arg0
;
1226 uint8_t uid
[10] = {0x00};
1228 uint8_t *response
= BigBuf_malloc(MAX_MIFARE_FRAME_SIZE
);
1229 uint8_t *responsePar
= BigBuf_malloc(MAX_MIFARE_PARITY_SIZE
);
1231 uint8_t mf_auth
[] = { 0x60,0x00,0xf5,0x7b };
1233 // get memory from BigBuf.
1234 uint8_t *nonces
= BigBuf_malloc(iterations
* 4);
1242 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1244 for (int i
= 0; i
< iterations
; i
++) {
1248 // Test if the action was cancelled
1249 if(BUTTON_PRESS()) break;
1251 // if(mifare_classic_halt(pcs, cuid)) {
1252 // if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
1255 if(!iso14443a_select_card(uid
, NULL
, NULL
)) {
1256 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1260 // Transmit MIFARE_CLASSIC_AUTH.
1261 ReaderTransmit(mf_auth
, sizeof(mf_auth
), NULL
);
1263 // Receive the (4 Byte) "random" nonce
1264 if (!ReaderReceive(response
, responsePar
)) {
1265 if (MF_DBGLEVEL
>= 1) Dbprintf("Couldn't receive tag nonce");
1269 nonces
[i
*4] = bytes_to_num(response
, 4);
1272 int packLen
= iterations
* 4;
1275 while (packLen
> 0) {
1276 packSize
= MIN(USB_CMD_DATA_SIZE
, packLen
);
1278 cmd_send(CMD_ACK
, 77, 0, packSize
, nonces
- packLen
, packSize
);
1281 packLen
-= packSize
;
1284 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1292 void Mifare_DES_Auth1(uint8_t arg0
, uint8_t *datain
){
1294 byte_t dataout
[11] = {0x00};
1295 uint8_t uid
[10] = {0x00};
1296 uint32_t cuid
= 0x00;
1299 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1301 int len
= iso14443a_select_card(uid
, NULL
, &cuid
);
1303 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
1308 if(mifare_desfire_des_auth1(cuid
, dataout
)){
1309 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication part1: Fail.");
1314 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 1 FINISHED");
1315 cmd_send(CMD_ACK
,1,cuid
,0,dataout
, sizeof(dataout
));
1318 void Mifare_DES_Auth2(uint32_t arg0
, uint8_t *datain
){
1320 uint32_t cuid
= arg0
;
1321 uint8_t key
[16] = {0x00};
1322 byte_t dataout
[12] = {0x00};
1325 memcpy(key
, datain
, 16);
1327 isOK
= mifare_desfire_des_auth2(cuid
, key
, dataout
);
1330 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) Dbprintf("Authentication part2: Failed");
1335 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 2 FINISHED");
1337 cmd_send(CMD_ACK
, isOK
, 0, 0, dataout
, sizeof(dataout
));
1338 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);