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"
19 //#include "../client/loclass/des.h"
23 //-----------------------------------------------------------------------------
24 // Select, Authenticate, Read a MIFARE tag.
26 //-----------------------------------------------------------------------------
27 void MifareReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
30 uint8_t blockNo
= arg0
;
31 uint8_t keyType
= arg1
;
33 ui64Key
= bytes_to_num(datain
, 6);
37 byte_t dataoutbuf
[16];
40 struct Crypto1State mpcs
= {0, 0};
41 struct Crypto1State
*pcs
;
46 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
53 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
54 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
58 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
59 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
63 if(mifare_classic_readblock(pcs
, cuid
, blockNo
, dataoutbuf
)) {
64 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block error");
68 if(mifare_classic_halt(pcs
, cuid
)) {
69 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
77 // ----------------------------- crypto1 destroy
80 if (MF_DBGLEVEL
>= 2) DbpString("READ BLOCK FINISHED");
83 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16);
86 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
91 void MifareUC_Auth1(uint8_t arg0
, uint8_t *datain
){
93 byte_t dataoutbuf
[16] = {0x00};
94 uint8_t uid
[10] = {0x00};
97 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
100 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
102 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
103 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
108 if(mifare_ultra_auth1(dataoutbuf
)){
109 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication part1: Fail.");
114 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 1 FINISHED");
116 cmd_send(CMD_ACK
,1,cuid
,0,dataoutbuf
,11);
119 void MifareUC_Auth2(uint32_t arg0
, uint8_t *datain
){
121 uint8_t key
[16] = {0x00};
122 byte_t dataoutbuf
[16] = {0x00};
124 memcpy(key
, datain
, 16);
126 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
128 if(mifare_ultra_auth2(key
, dataoutbuf
)){
129 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication part2: Fail...");
134 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 2 FINISHED");
136 cmd_send(CMD_ACK
,1,0,0,dataoutbuf
,11);
137 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
142 // Arg1 = UsePwd bool
143 // datain = PWD bytes,
144 void MifareUReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
)
146 uint8_t blockNo
= arg0
;
147 byte_t dataout
[16] = {0x00};
148 uint8_t uid
[10] = {0x00};
149 bool usePwd
= (arg1
== 1);
154 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
156 int len
= iso14443a_select_card(uid
, NULL
, NULL
);
158 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%02X)",len
);
163 // authenticate here.
166 uint8_t key
[16] = {0x00};
167 memcpy(key
, datain
, 16);
169 uint8_t random_a
[8] = {1,1,1,1,1,1,1,1 };
170 uint8_t random_b
[8] = {0x00};
171 uint8_t enc_random_b
[8] = {0x00};
172 uint8_t rnd_ab
[16] = {0x00};
173 uint8_t IV
[8] = {0x00};
176 uint8_t receivedAnswer
[MAX_FRAME_SIZE
];
177 uint8_t receivedAnswerPar
[MAX_PARITY_SIZE
];
179 len
= mifare_sendcmd_short(NULL
, 1, 0x1A, 0x00, receivedAnswer
,receivedAnswerPar
,NULL
);
181 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
187 memcpy(enc_random_b
,receivedAnswer
+1,8);
190 tdes_2key_dec(random_b
, enc_random_b
, sizeof(random_b
), key
, IV
);
192 memcpy(rnd_ab
,random_a
,8);
193 memcpy(rnd_ab
+8,random_b
,8);
195 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) {
196 Dbprintf("enc_B: %02x %02x %02x %02x %02x %02x %02x %02x",
197 enc_random_b
[0],enc_random_b
[1],enc_random_b
[2],enc_random_b
[3],
198 enc_random_b
[4],enc_random_b
[5],enc_random_b
[6],enc_random_b
[7]);
200 Dbprintf(" B: %02x %02x %02x %02x %02x %02x %02x %02x",
201 random_b
[0],random_b
[1],random_b
[2],random_b
[3],
202 random_b
[4],random_b
[5],random_b
[6],random_b
[7]);
204 Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",
205 rnd_ab
[0],rnd_ab
[1],rnd_ab
[2],rnd_ab
[3],
206 rnd_ab
[4],rnd_ab
[5],rnd_ab
[6],rnd_ab
[7]);
208 Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",
209 rnd_ab
[8],rnd_ab
[9],rnd_ab
[10],rnd_ab
[11],
210 rnd_ab
[12],rnd_ab
[13],rnd_ab
[14],rnd_ab
[15] );
213 // encrypt out, in, length, key, iv
214 tdes_2key_enc(rnd_ab
, rnd_ab
, sizeof(rnd_ab
), key
, enc_random_b
);
217 len
= mifare_sendcmd_short_mfucauth(NULL
, 1, 0xAF, rnd_ab
, receivedAnswer
, receivedAnswerPar
, NULL
);
219 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
224 uint8_t enc_resp
[8] = { 0 };
225 uint8_t resp_random_a
[8] = { 0 };
226 memcpy(enc_resp
, receivedAnswer
+1, 8);
228 // decrypt out, in, length, key, iv
229 tdes_2key_dec(resp_random_a
, enc_resp
, 8, key
, enc_random_b
);
230 if ( memcmp(resp_random_a
, random_a
, 8) != 0 )
231 Dbprintf("failed authentication");
233 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) {
234 Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x",
235 rnd_ab
[0],rnd_ab
[1],rnd_ab
[2],rnd_ab
[3],
236 rnd_ab
[4],rnd_ab
[5],rnd_ab
[6],rnd_ab
[7]);
238 Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x",
239 rnd_ab
[8],rnd_ab
[9],rnd_ab
[10],rnd_ab
[11],
240 rnd_ab
[12],rnd_ab
[13],rnd_ab
[14],rnd_ab
[15]);
242 Dbprintf("a: %02x %02x %02x %02x %02x %02x %02x %02x",
243 random_a
[0],random_a
[1],random_a
[2],random_a
[3],
244 random_a
[4],random_a
[5],random_a
[6],random_a
[7]);
246 Dbprintf("b: %02x %02x %02x %02x %02x %02x %02x %02x",
247 resp_random_a
[0],resp_random_a
[1],resp_random_a
[2],resp_random_a
[3],
248 resp_random_a
[4],resp_random_a
[5],resp_random_a
[6],resp_random_a
[7]);
252 if( mifare_ultra_readblock(blockNo
, dataout
) ) {
253 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Read block error");
258 if( mifare_ultra_halt() ) {
259 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Halt error");
264 cmd_send(CMD_ACK
,1,0,0,dataout
,16);
265 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
268 //-----------------------------------------------------------------------------
269 // Select, Authenticate, Read a MIFARE tag.
270 // read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)
271 //-----------------------------------------------------------------------------
272 void MifareReadSector(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
275 uint8_t sectorNo
= arg0
;
276 uint8_t keyType
= arg1
;
277 uint64_t ui64Key
= 0;
278 ui64Key
= bytes_to_num(datain
, 6);
282 byte_t dataoutbuf
[16 * 16];
285 struct Crypto1State mpcs
= {0, 0};
286 struct Crypto1State
*pcs
;
292 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
299 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
301 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
305 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
307 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
310 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
311 if(mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
+ 16 * blockNo
)) {
313 if (MF_DBGLEVEL
>= 1) Dbprintf("Read sector %2d block %2d error", sectorNo
, blockNo
);
318 if(mifare_classic_halt(pcs
, cuid
)) {
319 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
322 // ----------------------------- crypto1 destroy
323 crypto1_destroy(pcs
);
325 if (MF_DBGLEVEL
>= 2) DbpString("READ SECTOR FINISHED");
328 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16*NumBlocksPerSector(sectorNo
));
332 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
336 // arg0 = blockNo (start)
337 // arg1 = Pages (number of blocks)
339 // datain = KEY bytes
340 void MifareUReadCard(uint8_t arg0
, uint16_t arg1
, uint8_t arg2
, uint8_t *datain
)
343 uint8_t blockNo
= arg0
;
344 uint16_t blocks
= arg1
;
345 bool useKey
= (arg2
== 1);
347 uint8_t dataout
[176] = {0x00};;
352 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
354 int len
= iso14443a_select_card(NULL
, NULL
, NULL
);
356 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%d)",len
);
364 uint8_t key
[16] = {0x00};
365 memcpy(key
, datain
, 16);
367 uint8_t random_a
[8] = {1,1,1,1,1,1,1,1 };
368 uint8_t random_b
[8] = {0x00};
369 uint8_t enc_random_b
[8] = {0x00};
370 uint8_t rnd_ab
[16] = {0x00};
371 uint8_t IV
[8] = {0x00};
374 uint8_t receivedAnswer
[MAX_FRAME_SIZE
];
375 uint8_t receivedAnswerPar
[MAX_PARITY_SIZE
];
377 len
= mifare_sendcmd_short(NULL
, 1, 0x1A, 0x00, receivedAnswer
,receivedAnswerPar
,NULL
);
379 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
385 memcpy(enc_random_b
,receivedAnswer
+1,8);
388 tdes_2key_dec(random_b
, enc_random_b
, sizeof(random_b
), key
, IV
);
390 memcpy(rnd_ab
,random_a
,8);
391 memcpy(rnd_ab
+8,random_b
,8);
393 // encrypt out, in, length, key, iv
394 tdes_2key_enc(rnd_ab
, rnd_ab
, sizeof(rnd_ab
), key
, enc_random_b
);
396 len
= mifare_sendcmd_short_mfucauth(NULL
, 1, 0xAF, rnd_ab
, receivedAnswer
, receivedAnswerPar
, NULL
);
402 uint8_t enc_resp
[8] = { 0 };
403 uint8_t resp_random_a
[8] = { 0 };
404 memcpy(enc_resp
, receivedAnswer
+1, 8);
406 // decrypt out, in, length, key, iv
407 tdes_2key_dec(resp_random_a
, enc_resp
, 8, key
, enc_random_b
);
408 if ( memcmp(resp_random_a
, random_a
, 8) != 0 )
409 Dbprintf("failed authentication");
412 for (int i
= 0; i
< blocks
; i
++){
414 len
= mifare_ultra_readblock(blockNo
* 4 + i
, dataout
+ 4 * i
);
417 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Read block %d error",i
);
425 if (mifare_ultra_halt()) {
426 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Halt error");
431 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) Dbprintf("blocks read %d", countpages
);
435 cmd_send(CMD_ACK
, 1, len
, 0, dataout
, len
);
436 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
440 //-----------------------------------------------------------------------------
441 // Select, Authenticate, Write a MIFARE tag.
443 //-----------------------------------------------------------------------------
444 void MifareWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
447 uint8_t blockNo
= arg0
;
448 uint8_t keyType
= arg1
;
449 uint64_t ui64Key
= 0;
450 byte_t blockdata
[16];
452 ui64Key
= bytes_to_num(datain
, 6);
453 memcpy(blockdata
, datain
+ 10, 16);
459 struct Crypto1State mpcs
= {0, 0};
460 struct Crypto1State
*pcs
;
466 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
473 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
474 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
478 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
479 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
483 if(mifare_classic_writeblock(pcs
, cuid
, blockNo
, blockdata
)) {
484 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
488 if(mifare_classic_halt(pcs
, cuid
)) {
489 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
497 // ----------------------------- crypto1 destroy
498 crypto1_destroy(pcs
);
500 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
503 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
508 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
512 void MifareUWriteBlock(uint8_t arg0
, uint8_t *datain
)
514 uint8_t blockNo
= arg0
;
515 byte_t blockdata
[16] = {0x00};
517 memcpy(blockdata
, datain
, 16);
519 uint8_t uid
[10] = {0x00};
521 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
524 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
526 if(!iso14443a_select_card(uid
, NULL
, NULL
)) {
527 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
532 if(mifare_ultra_writeblock(blockNo
, blockdata
)) {
533 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
537 if(mifare_ultra_halt()) {
538 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
543 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
545 cmd_send(CMD_ACK
,1,0,0,0,0);
546 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
550 void MifareUWriteBlock_Special(uint8_t arg0
, uint8_t *datain
)
552 uint8_t blockNo
= arg0
;
553 byte_t blockdata
[4] = {0x00};
554 uint8_t uid
[10] = {0x00};
556 memcpy(blockdata
, datain
,4);
561 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
563 if(!iso14443a_select_card(uid
, NULL
, NULL
)) {
564 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
569 if(mifare_ultra_special_writeblock(blockNo
, blockdata
)) {
570 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
575 if(mifare_ultra_halt()) {
576 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
581 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
583 cmd_send(CMD_ACK
,1,0,0,0,0);
584 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
588 void MifareUSetPwd(uint8_t arg0
, uint8_t *datain
){
590 uint8_t pwd
[16] = {0x00};
591 byte_t blockdata
[4] = {0x00};
593 memcpy(pwd
, datain
, 16);
595 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
597 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
599 if(!iso14443a_select_card(NULL
, NULL
, NULL
)) {
600 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
605 blockdata
[0] = pwd
[7];
606 blockdata
[1] = pwd
[6];
607 blockdata
[2] = pwd
[5];
608 blockdata
[3] = pwd
[4];
609 if(mifare_ultra_special_writeblock( 44, blockdata
)) {
610 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
615 blockdata
[0] = pwd
[3];
616 blockdata
[1] = pwd
[2];
617 blockdata
[2] = pwd
[1];
618 blockdata
[3] = pwd
[0];
619 if(mifare_ultra_special_writeblock( 45, blockdata
)) {
620 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
625 blockdata
[0] = pwd
[15];
626 blockdata
[1] = pwd
[14];
627 blockdata
[2] = pwd
[13];
628 blockdata
[3] = pwd
[12];
629 if(mifare_ultra_special_writeblock( 46, blockdata
)) {
630 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
635 blockdata
[0] = pwd
[11];
636 blockdata
[1] = pwd
[10];
637 blockdata
[2] = pwd
[9];
638 blockdata
[3] = pwd
[8];
639 if(mifare_ultra_special_writeblock( 47, blockdata
)) {
640 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
645 if(mifare_ultra_halt()) {
646 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
651 cmd_send(CMD_ACK
,1,0,0,0,0);
652 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
656 // Return 1 if the nonce is invalid else return 0
657 int valid_nonce(uint32_t Nt
, uint32_t NtEnc
, uint32_t Ks1
, uint8_t *parity
) {
658 return ((oddparity((Nt
>> 24) & 0xFF) == ((parity
[0]) ^ oddparity((NtEnc
>> 24) & 0xFF) ^ BIT(Ks1
,16))) & \
659 (oddparity((Nt
>> 16) & 0xFF) == ((parity
[1]) ^ oddparity((NtEnc
>> 16) & 0xFF) ^ BIT(Ks1
,8))) & \
660 (oddparity((Nt
>> 8) & 0xFF) == ((parity
[2]) ^ oddparity((NtEnc
>> 8) & 0xFF) ^ BIT(Ks1
,0)))) ? 1 : 0;
664 //-----------------------------------------------------------------------------
665 // MIFARE nested authentication.
667 //-----------------------------------------------------------------------------
668 void MifareNested(uint32_t arg0
, uint32_t arg1
, uint32_t calibrate
, uint8_t *datain
)
671 uint8_t blockNo
= arg0
& 0xff;
672 uint8_t keyType
= (arg0
>> 8) & 0xff;
673 uint8_t targetBlockNo
= arg1
& 0xff;
674 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
675 uint64_t ui64Key
= 0;
677 ui64Key
= bytes_to_num(datain
, 6);
680 uint16_t rtr
, i
, j
, len
;
682 static uint16_t dmin
, dmax
;
684 uint32_t cuid
, nt1
, nt2
, nttmp
, nttest
, ks1
;
686 uint32_t target_nt
[2], target_ks
[2];
688 uint8_t par_array
[4];
690 struct Crypto1State mpcs
= {0, 0};
691 struct Crypto1State
*pcs
;
693 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
695 uint32_t auth1_time
, auth2_time
;
696 static uint16_t delta_time
;
698 // free eventually allocated BigBuf memory
704 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
710 // statistics on nonce distance
711 if (calibrate
) { // for first call only. Otherwise reuse previous calibration
719 for (rtr
= 0; rtr
< 17; rtr
++) {
721 // prepare next select. No need to power down the card.
722 if(mifare_classic_halt(pcs
, cuid
)) {
723 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
728 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
729 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
735 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
736 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
742 auth2_time
= auth1_time
+ delta_time
;
746 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_NESTED
, &nt2
, &auth2_time
)) {
747 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error");
752 nttmp
= prng_successor(nt1
, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160
753 for (i
= 101; i
< 1200; i
++) {
754 nttmp
= prng_successor(nttmp
, 1);
755 if (nttmp
== nt2
) break;
765 delta_time
= auth2_time
- auth1_time
+ 32; // allow some slack for proper timing
767 if (MF_DBGLEVEL
>= 3) Dbprintf("Nested: calibrating... ntdist=%d", i
);
771 if (rtr
<= 1) return;
773 davg
= (davg
+ (rtr
- 1)/2) / (rtr
- 1);
775 if (MF_DBGLEVEL
>= 3) Dbprintf("min=%d max=%d avg=%d, delta_time=%d", dmin
, dmax
, davg
, delta_time
);
783 // -------------------------------------------------------------------------------------------------
787 // get crypted nonces for target sector
788 for(i
=0; i
< 2; i
++) { // look for exactly two different nonces
791 while(target_nt
[i
] == 0) { // continue until we have an unambiguous nonce
793 // prepare next select. No need to power down the card.
794 if(mifare_classic_halt(pcs
, cuid
)) {
795 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
799 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
800 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
805 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
806 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
810 // nested authentication
811 auth2_time
= auth1_time
+ delta_time
;
812 len
= mifare_sendcmd_shortex(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType
& 0x01), targetBlockNo
, receivedAnswer
, par
, &auth2_time
);
814 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error len=%d", len
);
818 nt2
= bytes_to_num(receivedAnswer
, 4);
819 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i
+1, nt1
, nt2
, par
[0]);
821 // Parity validity check
822 for (j
= 0; j
< 4; j
++) {
823 par_array
[j
] = (oddparity(receivedAnswer
[j
]) != ((par
[0] >> (7-j
)) & 0x01));
827 nttest
= prng_successor(nt1
, dmin
- 1);
828 for (j
= dmin
; j
< dmax
+ 1; j
++) {
829 nttest
= prng_successor(nttest
, 1);
832 if (valid_nonce(nttest
, nt2
, ks1
, par_array
)){
833 if (ncount
> 0) { // we are only interested in disambiguous nonces, try again
834 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i
+1, j
);
838 target_nt
[i
] = nttest
;
841 if (i
== 1 && target_nt
[1] == target_nt
[0]) { // we need two different nonces
843 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j
);
846 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i
+1, j
);
849 if (target_nt
[i
] == 0 && j
== dmax
+1 && MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i
+1);
855 // ----------------------------- crypto1 destroy
856 crypto1_destroy(pcs
);
858 byte_t buf
[4 + 4 * 4];
859 memcpy(buf
, &cuid
, 4);
860 memcpy(buf
+4, &target_nt
[0], 4);
861 memcpy(buf
+8, &target_ks
[0], 4);
862 memcpy(buf
+12, &target_nt
[1], 4);
863 memcpy(buf
+16, &target_ks
[1], 4);
866 cmd_send(CMD_ACK
, 0, 2, targetBlockNo
+ (targetKeyType
* 0x100), buf
, sizeof(buf
));
869 if (MF_DBGLEVEL
>= 3) DbpString("NESTED FINISHED");
871 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
876 //-----------------------------------------------------------------------------
877 // MIFARE check keys. key count up to 85.
879 //-----------------------------------------------------------------------------
880 void MifareChkKeys(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
883 uint8_t blockNo
= arg0
;
884 uint8_t keyType
= arg1
;
885 uint8_t keyCount
= arg2
;
886 uint64_t ui64Key
= 0;
893 struct Crypto1State mpcs
= {0, 0};
894 struct Crypto1State
*pcs
;
898 int OLD_MF_DBGLEVEL
= MF_DBGLEVEL
;
899 MF_DBGLEVEL
= MF_DBG_NONE
;
905 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
911 for (i
= 0; i
< keyCount
; i
++) {
912 if(mifare_classic_halt(pcs
, cuid
)) {
913 if (MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Halt error");
916 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
917 if (OLD_MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Can't select card");
921 ui64Key
= bytes_to_num(datain
+ i
* 6, 6);
922 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
930 // ----------------------------- crypto1 destroy
931 crypto1_destroy(pcs
);
934 cmd_send(CMD_ACK
,isOK
,0,0,datain
+ i
* 6,6);
937 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
940 // restore debug level
941 MF_DBGLEVEL
= OLD_MF_DBGLEVEL
;
944 //-----------------------------------------------------------------------------
945 // MIFARE commands set debug level
947 //-----------------------------------------------------------------------------
948 void MifareSetDbgLvl(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
950 Dbprintf("Debug level: %d", MF_DBGLEVEL
);
953 //-----------------------------------------------------------------------------
954 // Work with emulator memory
956 //-----------------------------------------------------------------------------
957 void MifareEMemClr(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
961 void MifareEMemSet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
962 emlSetMem(datain
, arg0
, arg1
); // data, block num, blocks count
965 void MifareEMemGet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
966 byte_t buf
[USB_CMD_DATA_SIZE
];
967 emlGetMem(buf
, arg0
, arg1
); // data, block num, blocks count (max 4)
970 cmd_send(CMD_ACK
,arg0
,arg1
,0,buf
,USB_CMD_DATA_SIZE
);
974 //-----------------------------------------------------------------------------
975 // Load a card into the emulator memory
977 //-----------------------------------------------------------------------------
978 void MifareECardLoad(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
979 uint8_t numSectors
= arg0
;
980 uint8_t keyType
= arg1
;
981 uint64_t ui64Key
= 0;
983 struct Crypto1State mpcs
= {0, 0};
984 struct Crypto1State
*pcs
;
988 byte_t dataoutbuf
[16];
989 byte_t dataoutbuf2
[16];
996 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1004 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
1006 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1009 for (uint8_t sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
1010 ui64Key
= emlGetKey(sectorNo
, keyType
);
1012 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
1014 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo
);
1018 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_NESTED
)) {
1020 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo
);
1025 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
1026 if(isOK
&& mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
)) {
1028 if (MF_DBGLEVEL
>= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo
, blockNo
);
1032 if (blockNo
< NumBlocksPerSector(sectorNo
) - 1) {
1033 emlSetMem(dataoutbuf
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
1034 } else { // sector trailer, keep the keys, set only the AC
1035 emlGetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
1036 memcpy(&dataoutbuf2
[6], &dataoutbuf
[6], 4);
1037 emlSetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
1044 if(mifare_classic_halt(pcs
, cuid
)) {
1045 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1048 // ----------------------------- crypto1 destroy
1049 crypto1_destroy(pcs
);
1051 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1054 if (MF_DBGLEVEL
>= 2) DbpString("EMUL FILL SECTORS FINISHED");
1059 //-----------------------------------------------------------------------------
1060 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
1062 //-----------------------------------------------------------------------------
1063 void MifareCSetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1066 uint8_t needWipe
= arg0
;
1067 // bit 0 - need get UID
1068 // bit 1 - need wupC
1069 // bit 2 - need HALT after sequence
1070 // bit 3 - need init FPGA and field before sequence
1071 // bit 4 - need reset FPGA and LED
1072 uint8_t workFlags
= arg1
;
1073 uint8_t blockNo
= arg2
;
1076 uint8_t wupC1
[] = { 0x40 };
1077 uint8_t wupC2
[] = { 0x43 };
1078 uint8_t wipeC
[] = { 0x41 };
1082 uint8_t uid
[10] = {0x00};
1083 uint8_t d_block
[18] = {0x00};
1086 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1087 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1089 // reset FPGA and LED
1090 if (workFlags
& 0x08) {
1097 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1102 // get UID from chip
1103 if (workFlags
& 0x01) {
1104 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
1105 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1109 if(mifare_classic_halt(NULL
, cuid
)) {
1110 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1117 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1118 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1119 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1123 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
1124 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1125 if (MF_DBGLEVEL
>= 1) Dbprintf("wipeC error");
1129 if(mifare_classic_halt(NULL
, cuid
)) {
1130 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1136 if (workFlags
& 0x02) {
1137 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1138 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1139 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1143 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1144 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1145 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1150 if ((mifare_sendcmd_short(NULL
, 0, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1151 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send command error");
1155 memcpy(d_block
, datain
, 16);
1156 AppendCrc14443a(d_block
, 16);
1158 ReaderTransmit(d_block
, sizeof(d_block
), NULL
);
1159 if ((ReaderReceive(receivedAnswer
, receivedAnswerPar
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1160 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send data error");
1164 if (workFlags
& 0x04) {
1165 if (mifare_classic_halt(NULL
, cuid
)) {
1166 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1176 cmd_send(CMD_ACK
,isOK
,0,0,uid
,4);
1179 if ((workFlags
& 0x10) || (!isOK
)) {
1180 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1186 void MifareCGetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1189 // bit 1 - need wupC
1190 // bit 2 - need HALT after sequence
1191 // bit 3 - need init FPGA and field before sequence
1192 // bit 4 - need reset FPGA and LED
1193 uint8_t workFlags
= arg0
;
1194 uint8_t blockNo
= arg2
;
1197 uint8_t wupC1
[] = { 0x40 };
1198 uint8_t wupC2
[] = { 0x43 };
1202 uint8_t data
[18] = {0x00};
1205 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1206 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1208 if (workFlags
& 0x08) {
1215 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1219 if (workFlags
& 0x02) {
1220 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1221 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1222 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1226 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1227 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1228 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1234 if ((mifare_sendcmd_short(NULL
, 0, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 18)) {
1235 if (MF_DBGLEVEL
>= 1) Dbprintf("read block send command error");
1238 memcpy(data
, receivedAnswer
, 18);
1240 if (workFlags
& 0x04) {
1241 if (mifare_classic_halt(NULL
, cuid
)) {
1242 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1252 cmd_send(CMD_ACK
,isOK
,0,0,data
,18);
1255 if ((workFlags
& 0x10) || (!isOK
)) {
1256 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1261 void MifareCIdent(){
1264 uint8_t wupC1
[] = { 0x40 };
1265 uint8_t wupC2
[] = { 0x43 };
1270 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1271 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1273 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1274 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1278 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1279 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1283 if (mifare_classic_halt(NULL
, 0)) {
1287 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
1290 void MifareCollectNonces(uint32_t arg0
, uint32_t arg1
){
1294 uint32_t iterations
= arg0
;
1295 uint8_t uid
[10] = {0x00};
1297 uint8_t *response
= BigBuf_malloc(MAX_MIFARE_FRAME_SIZE
);
1298 uint8_t *responsePar
= BigBuf_malloc(MAX_MIFARE_PARITY_SIZE
);
1300 uint8_t mf_auth
[] = { 0x60,0x00,0xf5,0x7b };
1302 // get memory from BigBuf.
1303 uint8_t *nonces
= BigBuf_malloc(iterations
* 4);
1311 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1313 for (int i
= 0; i
< iterations
; i
++) {
1317 // Test if the action was cancelled
1318 if(BUTTON_PRESS()) break;
1320 // if(mifare_classic_halt(pcs, cuid)) {
1321 // if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
1324 if(!iso14443a_select_card(uid
, NULL
, NULL
)) {
1325 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1329 // Transmit MIFARE_CLASSIC_AUTH.
1330 ReaderTransmit(mf_auth
, sizeof(mf_auth
), NULL
);
1332 // Receive the (4 Byte) "random" nonce
1333 if (!ReaderReceive(response
, responsePar
)) {
1334 if (MF_DBGLEVEL
>= 1) Dbprintf("Couldn't receive tag nonce");
1338 nonces
[i
*4] = bytes_to_num(response
, 4);
1341 int packLen
= iterations
* 4;
1344 while (packLen
> 0) {
1345 packSize
= MIN(USB_CMD_DATA_SIZE
, packLen
);
1347 cmd_send(CMD_ACK
, 77, 0, packSize
, nonces
- packLen
, packSize
);
1350 packLen
-= packSize
;
1353 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1361 void Mifare_DES_Auth1(uint8_t arg0
, uint8_t *datain
){
1363 byte_t dataout
[11] = {0x00};
1364 uint8_t uid
[10] = {0x00};
1365 uint32_t cuid
= 0x00;
1368 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1370 int len
= iso14443a_select_card(uid
, NULL
, &cuid
);
1372 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
1377 if(mifare_desfire_des_auth1(cuid
, dataout
)){
1378 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication part1: Fail.");
1383 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 1 FINISHED");
1384 cmd_send(CMD_ACK
,1,cuid
,0,dataout
, sizeof(dataout
));
1387 void Mifare_DES_Auth2(uint32_t arg0
, uint8_t *datain
){
1389 uint32_t cuid
= arg0
;
1390 uint8_t key
[16] = {0x00};
1391 byte_t dataout
[12] = {0x00};
1394 memcpy(key
, datain
, 16);
1396 isOK
= mifare_desfire_des_auth2(cuid
, key
, dataout
);
1399 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) Dbprintf("Authentication part2: Failed");
1404 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 2 FINISHED");
1406 cmd_send(CMD_ACK
, isOK
, 0, 0, dataout
, sizeof(dataout
));
1407 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);