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
);
141 void MifareUReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
)
143 uint8_t blockNo
= arg0
;
144 byte_t dataout
[16] = {0x00};
145 uint8_t uid
[10] = {0x00};
146 uint8_t key
[16] = {0x00};
147 bool usePwd
= (arg1
== 1);
149 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
152 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
154 int len
= iso14443a_select_card(uid
, NULL
, NULL
);
156 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%02X)",len
);
161 // authenticate here.
164 memcpy(key
, datain
, 16);
166 uint8_t random_a
[8] = {1,1,1,1,1,1,1,1 };
167 uint8_t random_b
[8] = {0x00};
168 uint8_t enc_random_b
[8] = {0x00};
169 uint8_t rnd_ab
[16] = {0x00};
170 uint8_t IV
[8] = {0x00};
173 uint8_t receivedAnswer
[MAX_FRAME_SIZE
];
174 uint8_t receivedAnswerPar
[MAX_PARITY_SIZE
];
176 len
= mifare_sendcmd_short(NULL
, 1, 0x1A, 0x00, receivedAnswer
,receivedAnswerPar
,NULL
);
178 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
184 memcpy(enc_random_b
,receivedAnswer
+1,8);
187 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 void MifareUReadCard(uint8_t arg0
, int arg1
, uint8_t *datain
)
339 uint8_t sectorNo
= arg0
;
342 uint8_t dataout
[176] = {0x00};;
347 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
349 int len
= iso14443a_select_card(NULL
, NULL
, NULL
);
351 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%d)",len
);
356 for (int i
= 0; i
< Pages
; i
++){
358 len
= mifare_ultra_readblock(sectorNo
* 4 + i
, dataout
+ 4 * i
);
361 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Read block %d error",i
);
369 if (mifare_ultra_halt()) {
370 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Halt error");
375 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) Dbprintf("Pages read %d", countpages
);
379 cmd_send(CMD_ACK
, 1, 0, 0, dataout
, len
);
380 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
384 //-----------------------------------------------------------------------------
385 // Select, Authenticate, Write a 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
;
410 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
417 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
418 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
422 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
423 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
427 if(mifare_classic_writeblock(pcs
, cuid
, blockNo
, blockdata
)) {
428 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
432 if(mifare_classic_halt(pcs
, cuid
)) {
433 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
441 // ----------------------------- crypto1 destroy
442 crypto1_destroy(pcs
);
444 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
447 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
452 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
456 void MifareUWriteBlock(uint8_t arg0
, uint8_t *datain
)
458 uint8_t blockNo
= arg0
;
459 byte_t blockdata
[16] = {0x00};
461 memcpy(blockdata
, datain
, 16);
463 uint8_t uid
[10] = {0x00};
465 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
468 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
470 if(!iso14443a_select_card(uid
, NULL
, NULL
)) {
471 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
476 if(mifare_ultra_writeblock(blockNo
, blockdata
)) {
477 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
481 if(mifare_ultra_halt()) {
482 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
487 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
489 cmd_send(CMD_ACK
,1,0,0,0,0);
490 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
494 void MifareUWriteBlock_Special(uint8_t arg0
, uint8_t *datain
)
496 uint8_t blockNo
= arg0
;
497 byte_t blockdata
[4] = {0x00};
499 memcpy(blockdata
, datain
,4);
501 uint8_t uid
[10] = {0x00};
503 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
505 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
507 if(!iso14443a_select_card(uid
, NULL
, NULL
)) {
508 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
513 if(mifare_ultra_special_writeblock(blockNo
, blockdata
)) {
514 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
519 if(mifare_ultra_halt()) {
520 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
525 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
527 cmd_send(CMD_ACK
,1,0,0,0,0);
528 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
532 void MifareUSetPwd(uint8_t arg0
, uint8_t *datain
){
534 uint8_t pwd
[16] = {0x00};
535 byte_t blockdata
[4] = {0x00};
537 memcpy(pwd
, datain
, 16);
539 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
541 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
543 if(!iso14443a_select_card(NULL
, NULL
, NULL
)) {
544 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
549 blockdata
[0] = pwd
[7];
550 blockdata
[1] = pwd
[6];
551 blockdata
[2] = pwd
[5];
552 blockdata
[3] = pwd
[4];
553 if(mifare_ultra_special_writeblock( 44, blockdata
)) {
554 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
559 blockdata
[0] = pwd
[3];
560 blockdata
[1] = pwd
[2];
561 blockdata
[2] = pwd
[1];
562 blockdata
[3] = pwd
[0];
563 if(mifare_ultra_special_writeblock( 45, blockdata
)) {
564 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
569 blockdata
[0] = pwd
[15];
570 blockdata
[1] = pwd
[14];
571 blockdata
[2] = pwd
[13];
572 blockdata
[3] = pwd
[12];
573 if(mifare_ultra_special_writeblock( 46, blockdata
)) {
574 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
579 blockdata
[0] = pwd
[11];
580 blockdata
[1] = pwd
[10];
581 blockdata
[2] = pwd
[9];
582 blockdata
[3] = pwd
[8];
583 if(mifare_ultra_special_writeblock( 47, blockdata
)) {
584 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
589 if(mifare_ultra_halt()) {
590 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
595 cmd_send(CMD_ACK
,1,0,0,0,0);
596 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
600 // Return 1 if the nonce is invalid else return 0
601 int valid_nonce(uint32_t Nt
, uint32_t NtEnc
, uint32_t Ks1
, uint8_t *parity
) {
602 return ((oddparity((Nt
>> 24) & 0xFF) == ((parity
[0]) ^ oddparity((NtEnc
>> 24) & 0xFF) ^ BIT(Ks1
,16))) & \
603 (oddparity((Nt
>> 16) & 0xFF) == ((parity
[1]) ^ oddparity((NtEnc
>> 16) & 0xFF) ^ BIT(Ks1
,8))) & \
604 (oddparity((Nt
>> 8) & 0xFF) == ((parity
[2]) ^ oddparity((NtEnc
>> 8) & 0xFF) ^ BIT(Ks1
,0)))) ? 1 : 0;
608 //-----------------------------------------------------------------------------
609 // MIFARE nested authentication.
611 //-----------------------------------------------------------------------------
612 void MifareNested(uint32_t arg0
, uint32_t arg1
, uint32_t calibrate
, uint8_t *datain
)
615 uint8_t blockNo
= arg0
& 0xff;
616 uint8_t keyType
= (arg0
>> 8) & 0xff;
617 uint8_t targetBlockNo
= arg1
& 0xff;
618 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
619 uint64_t ui64Key
= 0;
621 ui64Key
= bytes_to_num(datain
, 6);
624 uint16_t rtr
, i
, j
, len
;
626 static uint16_t dmin
, dmax
;
628 uint32_t cuid
, nt1
, nt2
, nttmp
, nttest
, ks1
;
630 uint32_t target_nt
[2], target_ks
[2];
632 uint8_t par_array
[4];
634 struct Crypto1State mpcs
= {0, 0};
635 struct Crypto1State
*pcs
;
637 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
639 uint32_t auth1_time
, auth2_time
;
640 static uint16_t delta_time
;
642 // free eventually allocated BigBuf memory
648 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
654 // statistics on nonce distance
655 if (calibrate
) { // for first call only. Otherwise reuse previous calibration
663 for (rtr
= 0; rtr
< 17; rtr
++) {
665 // prepare next select. No need to power down the card.
666 if(mifare_classic_halt(pcs
, cuid
)) {
667 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
672 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
673 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
679 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
680 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
686 auth2_time
= auth1_time
+ delta_time
;
690 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_NESTED
, &nt2
, &auth2_time
)) {
691 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error");
696 nttmp
= prng_successor(nt1
, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160
697 for (i
= 101; i
< 1200; i
++) {
698 nttmp
= prng_successor(nttmp
, 1);
699 if (nttmp
== nt2
) break;
709 delta_time
= auth2_time
- auth1_time
+ 32; // allow some slack for proper timing
711 if (MF_DBGLEVEL
>= 3) Dbprintf("Nested: calibrating... ntdist=%d", i
);
715 if (rtr
<= 1) return;
717 davg
= (davg
+ (rtr
- 1)/2) / (rtr
- 1);
719 if (MF_DBGLEVEL
>= 3) Dbprintf("min=%d max=%d avg=%d, delta_time=%d", dmin
, dmax
, davg
, delta_time
);
727 // -------------------------------------------------------------------------------------------------
731 // get crypted nonces for target sector
732 for(i
=0; i
< 2; i
++) { // look for exactly two different nonces
735 while(target_nt
[i
] == 0) { // continue until we have an unambiguous nonce
737 // prepare next select. No need to power down the card.
738 if(mifare_classic_halt(pcs
, cuid
)) {
739 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
743 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
744 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
749 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
750 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
754 // nested authentication
755 auth2_time
= auth1_time
+ delta_time
;
756 len
= mifare_sendcmd_shortex(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType
& 0x01), targetBlockNo
, receivedAnswer
, par
, &auth2_time
);
758 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error len=%d", len
);
762 nt2
= bytes_to_num(receivedAnswer
, 4);
763 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i
+1, nt1
, nt2
, par
[0]);
765 // Parity validity check
766 for (j
= 0; j
< 4; j
++) {
767 par_array
[j
] = (oddparity(receivedAnswer
[j
]) != ((par
[0] >> (7-j
)) & 0x01));
771 nttest
= prng_successor(nt1
, dmin
- 1);
772 for (j
= dmin
; j
< dmax
+ 1; j
++) {
773 nttest
= prng_successor(nttest
, 1);
776 if (valid_nonce(nttest
, nt2
, ks1
, par_array
)){
777 if (ncount
> 0) { // we are only interested in disambiguous nonces, try again
778 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i
+1, j
);
782 target_nt
[i
] = nttest
;
785 if (i
== 1 && target_nt
[1] == target_nt
[0]) { // we need two different nonces
787 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j
);
790 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i
+1, j
);
793 if (target_nt
[i
] == 0 && j
== dmax
+1 && MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i
+1);
799 // ----------------------------- crypto1 destroy
800 crypto1_destroy(pcs
);
802 byte_t buf
[4 + 4 * 4];
803 memcpy(buf
, &cuid
, 4);
804 memcpy(buf
+4, &target_nt
[0], 4);
805 memcpy(buf
+8, &target_ks
[0], 4);
806 memcpy(buf
+12, &target_nt
[1], 4);
807 memcpy(buf
+16, &target_ks
[1], 4);
810 cmd_send(CMD_ACK
, 0, 2, targetBlockNo
+ (targetKeyType
* 0x100), buf
, sizeof(buf
));
813 if (MF_DBGLEVEL
>= 3) DbpString("NESTED FINISHED");
815 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
820 //-----------------------------------------------------------------------------
821 // MIFARE check keys. key count up to 85.
823 //-----------------------------------------------------------------------------
824 void MifareChkKeys(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
827 uint8_t blockNo
= arg0
;
828 uint8_t keyType
= arg1
;
829 uint8_t keyCount
= arg2
;
830 uint64_t ui64Key
= 0;
837 struct Crypto1State mpcs
= {0, 0};
838 struct Crypto1State
*pcs
;
842 int OLD_MF_DBGLEVEL
= MF_DBGLEVEL
;
843 MF_DBGLEVEL
= MF_DBG_NONE
;
849 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
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
);
884 // restore debug level
885 MF_DBGLEVEL
= OLD_MF_DBGLEVEL
;
888 //-----------------------------------------------------------------------------
889 // MIFARE commands set debug level
891 //-----------------------------------------------------------------------------
892 void MifareSetDbgLvl(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
894 Dbprintf("Debug level: %d", MF_DBGLEVEL
);
897 //-----------------------------------------------------------------------------
898 // Work with emulator memory
900 //-----------------------------------------------------------------------------
901 void MifareEMemClr(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
905 void MifareEMemSet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
906 emlSetMem(datain
, arg0
, arg1
); // data, block num, blocks count
909 void MifareEMemGet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
910 byte_t buf
[USB_CMD_DATA_SIZE
];
911 emlGetMem(buf
, arg0
, arg1
); // data, block num, blocks count (max 4)
914 cmd_send(CMD_ACK
,arg0
,arg1
,0,buf
,USB_CMD_DATA_SIZE
);
918 //-----------------------------------------------------------------------------
919 // Load a card into the emulator memory
921 //-----------------------------------------------------------------------------
922 void MifareECardLoad(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
923 uint8_t numSectors
= arg0
;
924 uint8_t keyType
= arg1
;
925 uint64_t ui64Key
= 0;
927 struct Crypto1State mpcs
= {0, 0};
928 struct Crypto1State
*pcs
;
932 byte_t dataoutbuf
[16];
933 byte_t dataoutbuf2
[16];
940 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
948 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
950 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
953 for (uint8_t sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
954 ui64Key
= emlGetKey(sectorNo
, keyType
);
956 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
958 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo
);
962 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_NESTED
)) {
964 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo
);
969 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
970 if(isOK
&& mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
)) {
972 if (MF_DBGLEVEL
>= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo
, blockNo
);
976 if (blockNo
< NumBlocksPerSector(sectorNo
) - 1) {
977 emlSetMem(dataoutbuf
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
978 } else { // sector trailer, keep the keys, set only the AC
979 emlGetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
980 memcpy(&dataoutbuf2
[6], &dataoutbuf
[6], 4);
981 emlSetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
988 if(mifare_classic_halt(pcs
, cuid
)) {
989 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
992 // ----------------------------- crypto1 destroy
993 crypto1_destroy(pcs
);
995 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
998 if (MF_DBGLEVEL
>= 2) DbpString("EMUL FILL SECTORS FINISHED");
1003 //-----------------------------------------------------------------------------
1004 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
1006 //-----------------------------------------------------------------------------
1007 void MifareCSetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1010 uint8_t needWipe
= arg0
;
1011 // bit 0 - need get UID
1012 // bit 1 - need wupC
1013 // bit 2 - need HALT after sequence
1014 // bit 3 - need init FPGA and field before sequence
1015 // bit 4 - need reset FPGA and LED
1016 uint8_t workFlags
= arg1
;
1017 uint8_t blockNo
= arg2
;
1020 uint8_t wupC1
[] = { 0x40 };
1021 uint8_t wupC2
[] = { 0x43 };
1022 uint8_t wipeC
[] = { 0x41 };
1026 uint8_t uid
[10] = {0x00};
1027 uint8_t d_block
[18] = {0x00};
1030 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1031 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1033 // reset FPGA and LED
1034 if (workFlags
& 0x08) {
1041 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1046 // get UID from chip
1047 if (workFlags
& 0x01) {
1048 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
1049 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1053 if(mifare_classic_halt(NULL
, cuid
)) {
1054 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1061 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1062 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1063 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1067 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
1068 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1069 if (MF_DBGLEVEL
>= 1) Dbprintf("wipeC error");
1073 if(mifare_classic_halt(NULL
, cuid
)) {
1074 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1080 if (workFlags
& 0x02) {
1081 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1082 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1083 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1087 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1088 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1089 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1094 if ((mifare_sendcmd_short(NULL
, 0, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1095 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send command error");
1099 memcpy(d_block
, datain
, 16);
1100 AppendCrc14443a(d_block
, 16);
1102 ReaderTransmit(d_block
, sizeof(d_block
), NULL
);
1103 if ((ReaderReceive(receivedAnswer
, receivedAnswerPar
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1104 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send data error");
1108 if (workFlags
& 0x04) {
1109 if (mifare_classic_halt(NULL
, cuid
)) {
1110 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1120 cmd_send(CMD_ACK
,isOK
,0,0,uid
,4);
1123 if ((workFlags
& 0x10) || (!isOK
)) {
1124 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1130 void MifareCGetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1133 // bit 1 - need wupC
1134 // bit 2 - need HALT after sequence
1135 // bit 3 - need init FPGA and field before sequence
1136 // bit 4 - need reset FPGA and LED
1137 uint8_t workFlags
= arg0
;
1138 uint8_t blockNo
= arg2
;
1141 uint8_t wupC1
[] = { 0x40 };
1142 uint8_t wupC2
[] = { 0x43 };
1146 uint8_t data
[18] = {0x00};
1149 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1150 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1152 if (workFlags
& 0x08) {
1159 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1163 if (workFlags
& 0x02) {
1164 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1165 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1166 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1170 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1171 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1172 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1178 if ((mifare_sendcmd_short(NULL
, 0, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 18)) {
1179 if (MF_DBGLEVEL
>= 1) Dbprintf("read block send command error");
1182 memcpy(data
, receivedAnswer
, 18);
1184 if (workFlags
& 0x04) {
1185 if (mifare_classic_halt(NULL
, cuid
)) {
1186 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1196 cmd_send(CMD_ACK
,isOK
,0,0,data
,18);
1199 if ((workFlags
& 0x10) || (!isOK
)) {
1200 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1205 void MifareCIdent(){
1208 uint8_t wupC1
[] = { 0x40 };
1209 uint8_t wupC2
[] = { 0x43 };
1214 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1215 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1217 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1218 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1222 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1223 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1227 if (mifare_classic_halt(NULL
, 0)) {
1231 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
1234 void MifareCollectNonces(uint32_t arg0
, uint32_t arg1
){
1238 uint32_t iterations
= arg0
;
1239 uint8_t uid
[10] = {0x00};
1241 uint8_t *response
= BigBuf_malloc(MAX_MIFARE_FRAME_SIZE
);
1242 uint8_t *responsePar
= BigBuf_malloc(MAX_MIFARE_PARITY_SIZE
);
1244 uint8_t mf_auth
[] = { 0x60,0x00,0xf5,0x7b };
1246 // get memory from BigBuf.
1247 uint8_t *nonces
= BigBuf_malloc(iterations
* 4);
1255 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1257 for (int i
= 0; i
< iterations
; i
++) {
1261 // Test if the action was cancelled
1262 if(BUTTON_PRESS()) break;
1264 // if(mifare_classic_halt(pcs, cuid)) {
1265 // if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
1268 if(!iso14443a_select_card(uid
, NULL
, NULL
)) {
1269 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1273 // Transmit MIFARE_CLASSIC_AUTH.
1274 ReaderTransmit(mf_auth
, sizeof(mf_auth
), NULL
);
1276 // Receive the (4 Byte) "random" nonce
1277 if (!ReaderReceive(response
, responsePar
)) {
1278 if (MF_DBGLEVEL
>= 1) Dbprintf("Couldn't receive tag nonce");
1282 nonces
[i
*4] = bytes_to_num(response
, 4);
1285 int packLen
= iterations
* 4;
1288 while (packLen
> 0) {
1289 packSize
= MIN(USB_CMD_DATA_SIZE
, packLen
);
1291 cmd_send(CMD_ACK
, 77, 0, packSize
, nonces
- packLen
, packSize
);
1294 packLen
-= packSize
;
1298 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1306 void Mifare_DES_Auth1(uint8_t arg0
, uint8_t *datain
){
1308 byte_t dataout
[11] = {0x00};
1309 uint8_t uid
[10] = {0x00};
1310 uint32_t cuid
= 0x00;
1313 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1315 int len
= iso14443a_select_card(uid
, NULL
, &cuid
);
1317 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
1322 if(mifare_desfire_des_auth1(cuid
, dataout
)){
1323 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication part1: Fail.");
1328 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 1 FINISHED");
1329 cmd_send(CMD_ACK
,1,cuid
,0,dataout
, sizeof(dataout
));
1332 void Mifare_DES_Auth2(uint32_t arg0
, uint8_t *datain
){
1334 uint32_t cuid
= arg0
;
1335 uint8_t key
[16] = {0x00};
1336 byte_t dataout
[12] = {0x00};
1339 memcpy(key
, datain
, 16);
1341 isOK
= mifare_desfire_des_auth2(cuid
, key
, dataout
);
1344 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) Dbprintf("Authentication part2: Failed");
1349 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 2 FINISHED");
1351 cmd_send(CMD_ACK
, isOK
, 0, 0, dataout
, sizeof(dataout
));
1352 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);