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"
23 // the block number for the ISO14443-4 PCB
24 uint8_t pcb_blocknum
= 0;
25 // Deselect card by sending a s-block. the crc is precalced for speed
26 static uint8_t deselect_cmd
[] = {0xc2,0xe0,0xb4};
29 //-----------------------------------------------------------------------------
30 // Select, Authenticate, Read a MIFARE tag.
32 //-----------------------------------------------------------------------------
33 void MifareReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
36 uint8_t blockNo
= arg0
;
37 uint8_t keyType
= arg1
;
39 ui64Key
= bytes_to_num(datain
, 6);
43 byte_t dataoutbuf
[16];
46 struct Crypto1State mpcs
= {0, 0};
47 struct Crypto1State
*pcs
;
52 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
59 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
60 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
64 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
65 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
69 if(mifare_classic_readblock(pcs
, cuid
, blockNo
, dataoutbuf
)) {
70 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block error");
74 if(mifare_classic_halt(pcs
, cuid
)) {
75 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
83 // ----------------------------- crypto1 destroy
86 if (MF_DBGLEVEL
>= 2) DbpString("READ BLOCK FINISHED");
89 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16);
92 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
96 void MifareUC_Auth1(uint8_t arg0
, uint8_t *datain
){
98 byte_t dataoutbuf
[16] = {0x00};
99 uint8_t uid
[10] = {0x00};
100 uint32_t cuid
= 0x00;
102 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
105 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
107 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
108 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
113 if(mifare_ultra_auth1(dataoutbuf
)){
114 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication part1: Fail.");
119 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 1 FINISHED");
121 cmd_send(CMD_ACK
,1,cuid
,0,dataoutbuf
,11);
124 void MifareUC_Auth2(uint32_t arg0
, uint8_t *datain
){
126 uint8_t key
[16] = {0x00};
127 byte_t dataoutbuf
[16] = {0x00};
129 memcpy(key
, datain
, 16);
131 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
133 if(mifare_ultra_auth2(key
, dataoutbuf
)){
134 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication part2: Fail...");
139 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 2 FINISHED");
141 cmd_send(CMD_ACK
,1,0,0,dataoutbuf
,11);
142 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
147 // Arg1 = UsePwd bool
148 // datain = PWD bytes,
149 void MifareUReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
)
151 uint8_t blockNo
= arg0
;
152 byte_t dataout
[16] = {0x00};
153 uint8_t uid
[10] = {0x00};
154 uint8_t key
[16] = {0x00};
155 bool usePwd
= (arg1
== 1);
160 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
162 int len
= iso14443a_select_card(uid
, NULL
, NULL
);
164 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%02X)",len
);
169 // authenticate here.
172 memcpy(key
, datain
, 16);
174 // Dbprintf("KEY: %02x %02x %02x %02x %02x %02x %02x %02x", key[0],key[1],key[2],key[3],key[4],key[5],key[6],key[7] );
175 // Dbprintf("KEY: %02x %02x %02x %02x %02x %02x %02x %02x", key[8],key[9],key[10],key[11],key[12],key[13],key[14],key[15] );
177 uint8_t random_a
[8] = {1,1,1,1,1,1,1,1 };
178 uint8_t random_b
[8] = {0x00};
179 uint8_t enc_random_b
[8] = {0x00};
180 uint8_t rnd_ab
[16] = {0x00};
181 uint8_t IV
[8] = {0x00};
184 uint8_t receivedAnswer
[MAX_FRAME_SIZE
];
185 uint8_t receivedAnswerPar
[MAX_PARITY_SIZE
];
187 len
= mifare_sendcmd_short(NULL
, 1, 0x1A, 0x00, receivedAnswer
,receivedAnswerPar
,NULL
);
189 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
195 memcpy(enc_random_b
,receivedAnswer
+1,8);
198 tdes_2key_dec(random_b
, enc_random_b
, sizeof(random_b
), key
, IV
);
200 memcpy(rnd_ab
,random_a
,8);
201 memcpy(rnd_ab
+8,random_b
,8);
203 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) {
204 Dbprintf("enc_B: %02x %02x %02x %02x %02x %02x %02x %02x",
205 enc_random_b
[0],enc_random_b
[1],enc_random_b
[2],enc_random_b
[3],
206 enc_random_b
[4],enc_random_b
[5],enc_random_b
[6],enc_random_b
[7]);
208 Dbprintf(" B: %02x %02x %02x %02x %02x %02x %02x %02x",
209 random_b
[0],random_b
[1],random_b
[2],random_b
[3],
210 random_b
[4],random_b
[5],random_b
[6],random_b
[7]);
212 Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",
213 rnd_ab
[0],rnd_ab
[1],rnd_ab
[2],rnd_ab
[3],
214 rnd_ab
[4],rnd_ab
[5],rnd_ab
[6],rnd_ab
[7]);
216 Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",
217 rnd_ab
[8],rnd_ab
[9],rnd_ab
[10],rnd_ab
[11],
218 rnd_ab
[12],rnd_ab
[13],rnd_ab
[14],rnd_ab
[15] );
221 // encrypt out, in, length, key, iv
222 tdes_2key_enc(rnd_ab
, rnd_ab
, sizeof(rnd_ab
), key
, enc_random_b
);
224 len
= mifare_sendcmd_short_mfucauth(NULL
, 1, 0xAF, rnd_ab
, receivedAnswer
, receivedAnswerPar
, NULL
);
226 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
231 uint8_t enc_resp
[8] = { 0 };
232 uint8_t resp_random_a
[8] = { 0 };
233 memcpy(enc_resp
, receivedAnswer
+1, 8);
235 // decrypt out, in, length, key, iv
236 tdes_2key_dec(resp_random_a
, enc_resp
, 8, key
, enc_random_b
);
237 if ( memcmp(resp_random_a
, random_a
, 8) != 0 )
238 Dbprintf("failed authentication");
240 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) {
241 Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x",
242 rnd_ab
[0],rnd_ab
[1],rnd_ab
[2],rnd_ab
[3],
243 rnd_ab
[4],rnd_ab
[5],rnd_ab
[6],rnd_ab
[7]);
245 Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x",
246 rnd_ab
[8],rnd_ab
[9],rnd_ab
[10],rnd_ab
[11],
247 rnd_ab
[12],rnd_ab
[13],rnd_ab
[14],rnd_ab
[15]);
249 Dbprintf("a: %02x %02x %02x %02x %02x %02x %02x %02x",
250 random_a
[0],random_a
[1],random_a
[2],random_a
[3],
251 random_a
[4],random_a
[5],random_a
[6],random_a
[7]);
253 Dbprintf("b: %02x %02x %02x %02x %02x %02x %02x %02x",
254 resp_random_a
[0],resp_random_a
[1],resp_random_a
[2],resp_random_a
[3],
255 resp_random_a
[4],resp_random_a
[5],resp_random_a
[6],resp_random_a
[7]);
259 if( mifare_ultra_readblock(blockNo
, dataout
) ) {
260 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Read block error");
265 if( mifare_ultra_halt() ) {
266 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Halt error");
271 cmd_send(CMD_ACK
,1,0,0,dataout
,16);
272 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
276 //-----------------------------------------------------------------------------
277 // Select, Authenticate, Read a MIFARE tag.
278 // read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)
279 //-----------------------------------------------------------------------------
280 void MifareReadSector(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
283 uint8_t sectorNo
= arg0
;
284 uint8_t keyType
= arg1
;
285 uint64_t ui64Key
= 0;
286 ui64Key
= bytes_to_num(datain
, 6);
290 byte_t dataoutbuf
[16 * 16];
293 struct Crypto1State mpcs
= {0, 0};
294 struct Crypto1State
*pcs
;
300 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
307 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
309 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
313 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
315 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
318 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
319 if(mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
+ 16 * blockNo
)) {
321 if (MF_DBGLEVEL
>= 1) Dbprintf("Read sector %2d block %2d error", sectorNo
, blockNo
);
326 if(mifare_classic_halt(pcs
, cuid
)) {
327 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
330 // ----------------------------- crypto1 destroy
331 crypto1_destroy(pcs
);
333 if (MF_DBGLEVEL
>= 2) DbpString("READ SECTOR FINISHED");
336 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16*NumBlocksPerSector(sectorNo
));
340 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
344 void MifareUReadCard(uint8_t arg0
, uint16_t arg1
, uint8_t arg2
, uint8_t *datain
)
347 uint8_t blockNo
= arg0
;
348 uint16_t blocks
= arg1
;
349 bool useKey
= (arg2
== 1);
351 uint8_t dataout
[176] = {0x00};
356 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
358 int len
= iso14443a_select_card(NULL
, NULL
, NULL
);
360 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%d)",len
);
367 uint8_t key
[16] = {0x00};
368 memcpy(key
, datain
, 16);
370 uint8_t random_a
[8] = {1,1,1,1,1,1,1,1 };
371 uint8_t random_b
[8] = {0x00};
372 uint8_t enc_random_b
[8] = {0x00};
373 uint8_t rnd_ab
[16] = {0x00};
374 uint8_t IV
[8] = {0x00};
377 uint8_t receivedAnswer
[MAX_FRAME_SIZE
];
378 uint8_t receivedAnswerPar
[MAX_PARITY_SIZE
];
380 len
= mifare_sendcmd_short(NULL
, 1, 0x1A, 0x00, receivedAnswer
,receivedAnswerPar
,NULL
);
382 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
388 memcpy(enc_random_b
,receivedAnswer
+1,8);
391 tdes_2key_dec(random_b
, enc_random_b
, sizeof(random_b
), key
, IV
);
393 memcpy(rnd_ab
,random_a
,8);
394 memcpy(rnd_ab
+8,random_b
,8);
396 // encrypt out, in, length, key, iv
397 tdes_2key_enc(rnd_ab
, rnd_ab
, sizeof(rnd_ab
), key
, enc_random_b
);
399 len
= mifare_sendcmd_short_mfucauth(NULL
, 1, 0xAF, rnd_ab
, receivedAnswer
, receivedAnswerPar
, NULL
);
405 uint8_t enc_resp
[8] = { 0 };
406 uint8_t resp_random_a
[8] = { 0 };
407 memcpy(enc_resp
, receivedAnswer
+1, 8);
409 // decrypt out, in, length, key, iv
410 tdes_2key_dec(resp_random_a
, enc_resp
, 8, key
, enc_random_b
);
411 if ( memcmp(resp_random_a
, random_a
, 8) != 0 )
412 Dbprintf("failed authentication");
415 for (int i
= 0; i
< blocks
; i
++){
416 len
= mifare_ultra_readblock(blockNo
* 4 + i
, dataout
+ 4 * i
);
419 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Read block %d error",i
);
427 len
= mifare_ultra_halt();
429 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Halt error");
434 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) Dbprintf("Blocks read %d", countblocks
);
438 cmd_send(CMD_ACK
, 1, len
, 0, dataout
, len
);
439 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
443 //-----------------------------------------------------------------------------
444 // Select, Authenticate, Write a MIFARE tag.
446 //-----------------------------------------------------------------------------
447 void MifareWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
450 uint8_t blockNo
= arg0
;
451 uint8_t keyType
= arg1
;
452 uint64_t ui64Key
= 0;
453 byte_t blockdata
[16];
455 ui64Key
= bytes_to_num(datain
, 6);
456 memcpy(blockdata
, datain
+ 10, 16);
462 struct Crypto1State mpcs
= {0, 0};
463 struct Crypto1State
*pcs
;
469 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
476 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
477 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
481 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
482 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
486 if(mifare_classic_writeblock(pcs
, cuid
, blockNo
, blockdata
)) {
487 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
491 if(mifare_classic_halt(pcs
, cuid
)) {
492 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
500 // ----------------------------- crypto1 destroy
501 crypto1_destroy(pcs
);
503 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
506 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
511 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
515 void MifareUWriteBlock(uint8_t arg0
, uint8_t *datain
)
517 uint8_t blockNo
= arg0
;
518 byte_t blockdata
[16] = {0x00};
520 memcpy(blockdata
, datain
, 16);
522 uint8_t uid
[10] = {0x00};
524 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
527 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
529 if(!iso14443a_select_card(uid
, NULL
, NULL
)) {
530 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
535 if(mifare_ultra_writeblock(blockNo
, blockdata
)) {
536 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
540 if(mifare_ultra_halt()) {
541 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
546 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
548 cmd_send(CMD_ACK
,1,0,0,0,0);
549 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
553 void MifareUWriteBlock_Special(uint8_t arg0
, uint8_t *datain
)
555 uint8_t blockNo
= arg0
;
556 byte_t blockdata
[4] = {0x00};
558 memcpy(blockdata
, datain
,4);
560 uint8_t uid
[10] = {0x00};
562 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
564 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
566 if(!iso14443a_select_card(uid
, NULL
, NULL
)) {
567 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
572 if(mifare_ultra_special_writeblock(blockNo
, blockdata
)) {
573 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
578 if(mifare_ultra_halt()) {
579 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
584 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
586 cmd_send(CMD_ACK
,1,0,0,0,0);
587 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
591 void MifareUSetPwd(uint8_t arg0
, uint8_t *datain
){
593 uint8_t pwd
[16] = {0x00};
594 byte_t blockdata
[4] = {0x00};
596 memcpy(pwd
, datain
, 16);
598 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
600 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
602 if(!iso14443a_select_card(NULL
, NULL
, NULL
)) {
603 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
608 blockdata
[0] = pwd
[7];
609 blockdata
[1] = pwd
[6];
610 blockdata
[2] = pwd
[5];
611 blockdata
[3] = pwd
[4];
612 if(mifare_ultra_special_writeblock( 44, blockdata
)) {
613 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
618 blockdata
[0] = pwd
[3];
619 blockdata
[1] = pwd
[2];
620 blockdata
[2] = pwd
[1];
621 blockdata
[3] = pwd
[0];
622 if(mifare_ultra_special_writeblock( 45, blockdata
)) {
623 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
628 blockdata
[0] = pwd
[15];
629 blockdata
[1] = pwd
[14];
630 blockdata
[2] = pwd
[13];
631 blockdata
[3] = pwd
[12];
632 if(mifare_ultra_special_writeblock( 46, blockdata
)) {
633 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
638 blockdata
[0] = pwd
[11];
639 blockdata
[1] = pwd
[10];
640 blockdata
[2] = pwd
[9];
641 blockdata
[3] = pwd
[8];
642 if(mifare_ultra_special_writeblock( 47, blockdata
)) {
643 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
648 if(mifare_ultra_halt()) {
649 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
654 cmd_send(CMD_ACK
,1,0,0,0,0);
655 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
659 // Return 1 if the nonce is invalid else return 0
660 int valid_nonce(uint32_t Nt
, uint32_t NtEnc
, uint32_t Ks1
, uint8_t *parity
) {
661 return ((oddparity((Nt
>> 24) & 0xFF) == ((parity
[0]) ^ oddparity((NtEnc
>> 24) & 0xFF) ^ BIT(Ks1
,16))) & \
662 (oddparity((Nt
>> 16) & 0xFF) == ((parity
[1]) ^ oddparity((NtEnc
>> 16) & 0xFF) ^ BIT(Ks1
,8))) & \
663 (oddparity((Nt
>> 8) & 0xFF) == ((parity
[2]) ^ oddparity((NtEnc
>> 8) & 0xFF) ^ BIT(Ks1
,0)))) ? 1 : 0;
667 //-----------------------------------------------------------------------------
668 // MIFARE nested authentication.
670 //-----------------------------------------------------------------------------
671 void MifareNested(uint32_t arg0
, uint32_t arg1
, uint32_t calibrate
, uint8_t *datain
)
674 uint8_t blockNo
= arg0
& 0xff;
675 uint8_t keyType
= (arg0
>> 8) & 0xff;
676 uint8_t targetBlockNo
= arg1
& 0xff;
677 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
678 uint64_t ui64Key
= 0;
680 ui64Key
= bytes_to_num(datain
, 6);
683 uint16_t rtr
, i
, j
, len
;
685 static uint16_t dmin
, dmax
;
687 uint32_t cuid
, nt1
, nt2
, nttmp
, nttest
, ks1
;
689 uint32_t target_nt
[2], target_ks
[2];
691 uint8_t par_array
[4];
693 struct Crypto1State mpcs
= {0, 0};
694 struct Crypto1State
*pcs
;
696 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
698 uint32_t auth1_time
, auth2_time
;
699 static uint16_t delta_time
;
701 // free eventually allocated BigBuf memory
707 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
713 // statistics on nonce distance
714 if (calibrate
) { // for first call only. Otherwise reuse previous calibration
722 for (rtr
= 0; rtr
< 17; rtr
++) {
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");
731 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
732 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
738 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
739 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
745 auth2_time
= auth1_time
+ delta_time
;
749 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_NESTED
, &nt2
, &auth2_time
)) {
750 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error");
755 nttmp
= prng_successor(nt1
, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160
756 for (i
= 101; i
< 1200; i
++) {
757 nttmp
= prng_successor(nttmp
, 1);
758 if (nttmp
== nt2
) break;
768 delta_time
= auth2_time
- auth1_time
+ 32; // allow some slack for proper timing
770 if (MF_DBGLEVEL
>= 3) Dbprintf("Nested: calibrating... ntdist=%d", i
);
774 if (rtr
<= 1) return;
776 davg
= (davg
+ (rtr
- 1)/2) / (rtr
- 1);
778 if (MF_DBGLEVEL
>= 3) Dbprintf("min=%d max=%d avg=%d, delta_time=%d", dmin
, dmax
, davg
, delta_time
);
786 // -------------------------------------------------------------------------------------------------
790 // get crypted nonces for target sector
791 for(i
=0; i
< 2; i
++) { // look for exactly two different nonces
794 while(target_nt
[i
] == 0) { // continue until we have an unambiguous nonce
796 // prepare next select. No need to power down the card.
797 if(mifare_classic_halt(pcs
, cuid
)) {
798 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
802 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
803 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
808 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
809 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
813 // nested authentication
814 auth2_time
= auth1_time
+ delta_time
;
815 len
= mifare_sendcmd_shortex(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType
& 0x01), targetBlockNo
, receivedAnswer
, par
, &auth2_time
);
817 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error len=%d", len
);
821 nt2
= bytes_to_num(receivedAnswer
, 4);
822 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i
+1, nt1
, nt2
, par
[0]);
824 // Parity validity check
825 for (j
= 0; j
< 4; j
++) {
826 par_array
[j
] = (oddparity(receivedAnswer
[j
]) != ((par
[0] >> (7-j
)) & 0x01));
830 nttest
= prng_successor(nt1
, dmin
- 1);
831 for (j
= dmin
; j
< dmax
+ 1; j
++) {
832 nttest
= prng_successor(nttest
, 1);
835 if (valid_nonce(nttest
, nt2
, ks1
, par_array
)){
836 if (ncount
> 0) { // we are only interested in disambiguous nonces, try again
837 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i
+1, j
);
841 target_nt
[i
] = nttest
;
844 if (i
== 1 && target_nt
[1] == target_nt
[0]) { // we need two different nonces
846 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j
);
849 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i
+1, j
);
852 if (target_nt
[i
] == 0 && j
== dmax
+1 && MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i
+1);
858 // ----------------------------- crypto1 destroy
859 crypto1_destroy(pcs
);
861 byte_t buf
[4 + 4 * 4];
862 memcpy(buf
, &cuid
, 4);
863 memcpy(buf
+4, &target_nt
[0], 4);
864 memcpy(buf
+8, &target_ks
[0], 4);
865 memcpy(buf
+12, &target_nt
[1], 4);
866 memcpy(buf
+16, &target_ks
[1], 4);
869 cmd_send(CMD_ACK
, 0, 2, targetBlockNo
+ (targetKeyType
* 0x100), buf
, sizeof(buf
));
872 if (MF_DBGLEVEL
>= 3) DbpString("NESTED FINISHED");
874 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
879 //-----------------------------------------------------------------------------
880 // MIFARE check keys. key count up to 85.
882 //-----------------------------------------------------------------------------
883 void MifareChkKeys(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
886 uint8_t blockNo
= arg0
;
887 uint8_t keyType
= arg1
;
888 uint8_t keyCount
= arg2
;
889 uint64_t ui64Key
= 0;
896 struct Crypto1State mpcs
= {0, 0};
897 struct Crypto1State
*pcs
;
901 int OLD_MF_DBGLEVEL
= MF_DBGLEVEL
;
902 MF_DBGLEVEL
= MF_DBG_NONE
;
908 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
914 for (i
= 0; i
< keyCount
; i
++) {
915 if(mifare_classic_halt(pcs
, cuid
)) {
916 if (MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Halt error");
919 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
920 if (OLD_MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Can't select card");
924 ui64Key
= bytes_to_num(datain
+ i
* 6, 6);
925 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
933 // ----------------------------- crypto1 destroy
934 crypto1_destroy(pcs
);
937 cmd_send(CMD_ACK
,isOK
,0,0,datain
+ i
* 6,6);
940 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
943 // restore debug level
944 MF_DBGLEVEL
= OLD_MF_DBGLEVEL
;
947 //-----------------------------------------------------------------------------
948 // MIFARE commands set debug level
950 //-----------------------------------------------------------------------------
951 void MifareSetDbgLvl(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
953 Dbprintf("Debug level: %d", MF_DBGLEVEL
);
956 //-----------------------------------------------------------------------------
957 // Work with emulator memory
959 //-----------------------------------------------------------------------------
960 void MifareEMemClr(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
964 void MifareEMemSet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
965 emlSetMem(datain
, arg0
, arg1
); // data, block num, blocks count
968 void MifareEMemGet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
969 byte_t buf
[USB_CMD_DATA_SIZE
];
970 emlGetMem(buf
, arg0
, arg1
); // data, block num, blocks count (max 4)
973 cmd_send(CMD_ACK
,arg0
,arg1
,0,buf
,USB_CMD_DATA_SIZE
);
977 //-----------------------------------------------------------------------------
978 // Load a card into the emulator memory
980 //-----------------------------------------------------------------------------
981 void MifareECardLoad(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
982 uint8_t numSectors
= arg0
;
983 uint8_t keyType
= arg1
;
984 uint64_t ui64Key
= 0;
986 struct Crypto1State mpcs
= {0, 0};
987 struct Crypto1State
*pcs
;
991 byte_t dataoutbuf
[16];
992 byte_t dataoutbuf2
[16];
999 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1007 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
1009 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1012 for (uint8_t sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
1013 ui64Key
= emlGetKey(sectorNo
, keyType
);
1015 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
1017 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo
);
1021 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_NESTED
)) {
1023 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo
);
1028 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
1029 if(isOK
&& mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
)) {
1031 if (MF_DBGLEVEL
>= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo
, blockNo
);
1035 if (blockNo
< NumBlocksPerSector(sectorNo
) - 1) {
1036 emlSetMem(dataoutbuf
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
1037 } else { // sector trailer, keep the keys, set only the AC
1038 emlGetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
1039 memcpy(&dataoutbuf2
[6], &dataoutbuf
[6], 4);
1040 emlSetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
1047 if(mifare_classic_halt(pcs
, cuid
)) {
1048 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1051 // ----------------------------- crypto1 destroy
1052 crypto1_destroy(pcs
);
1054 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1057 if (MF_DBGLEVEL
>= 2) DbpString("EMUL FILL SECTORS FINISHED");
1062 //-----------------------------------------------------------------------------
1063 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
1065 //-----------------------------------------------------------------------------
1066 void MifareCSetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1069 uint8_t needWipe
= arg0
;
1070 // bit 0 - need get UID
1071 // bit 1 - need wupC
1072 // bit 2 - need HALT after sequence
1073 // bit 3 - need init FPGA and field before sequence
1074 // bit 4 - need reset FPGA and LED
1075 uint8_t workFlags
= arg1
;
1076 uint8_t blockNo
= arg2
;
1079 uint8_t wupC1
[] = { 0x40 };
1080 uint8_t wupC2
[] = { 0x43 };
1081 uint8_t wipeC
[] = { 0x41 };
1085 uint8_t uid
[10] = {0x00};
1086 uint8_t d_block
[18] = {0x00};
1089 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1090 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1092 // reset FPGA and LED
1093 if (workFlags
& 0x08) {
1100 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1105 // get UID from chip
1106 if (workFlags
& 0x01) {
1107 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
1108 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1112 if(mifare_classic_halt(NULL
, cuid
)) {
1113 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1120 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1121 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1122 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1126 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
1127 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1128 if (MF_DBGLEVEL
>= 1) Dbprintf("wipeC error");
1132 if(mifare_classic_halt(NULL
, cuid
)) {
1133 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1139 if (workFlags
& 0x02) {
1140 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1141 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1142 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1146 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1147 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1148 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1153 if ((mifare_sendcmd_short(NULL
, 0, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1154 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send command error");
1158 memcpy(d_block
, datain
, 16);
1159 AppendCrc14443a(d_block
, 16);
1161 ReaderTransmit(d_block
, sizeof(d_block
), NULL
);
1162 if ((ReaderReceive(receivedAnswer
, receivedAnswerPar
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1163 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send data error");
1167 if (workFlags
& 0x04) {
1168 if (mifare_classic_halt(NULL
, cuid
)) {
1169 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1179 cmd_send(CMD_ACK
,isOK
,0,0,uid
,4);
1182 if ((workFlags
& 0x10) || (!isOK
)) {
1183 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1189 void MifareCGetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1192 // bit 1 - need wupC
1193 // bit 2 - need HALT after sequence
1194 // bit 3 - need init FPGA and field before sequence
1195 // bit 4 - need reset FPGA and LED
1196 uint8_t workFlags
= arg0
;
1197 uint8_t blockNo
= arg2
;
1200 uint8_t wupC1
[] = { 0x40 };
1201 uint8_t wupC2
[] = { 0x43 };
1205 uint8_t data
[18] = {0x00};
1208 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1209 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1211 if (workFlags
& 0x08) {
1218 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1222 if (workFlags
& 0x02) {
1223 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1224 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1225 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1229 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1230 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1231 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1237 if ((mifare_sendcmd_short(NULL
, 0, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 18)) {
1238 if (MF_DBGLEVEL
>= 1) Dbprintf("read block send command error");
1241 memcpy(data
, receivedAnswer
, 18);
1243 if (workFlags
& 0x04) {
1244 if (mifare_classic_halt(NULL
, cuid
)) {
1245 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1255 cmd_send(CMD_ACK
,isOK
,0,0,data
,18);
1258 if ((workFlags
& 0x10) || (!isOK
)) {
1259 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1264 void MifareCIdent(){
1267 uint8_t wupC1
[] = { 0x40 };
1268 uint8_t wupC2
[] = { 0x43 };
1273 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1274 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1276 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1277 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1281 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1282 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1286 if (mifare_classic_halt(NULL
, 0)) {
1290 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
1297 void Mifare_DES_Auth1(uint8_t arg0
, uint8_t *datain
){
1299 byte_t dataout
[11] = {0x00};
1300 uint8_t uid
[10] = {0x00};
1304 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1306 int len
= iso14443a_select_card(uid
, NULL
, &cuid
);
1308 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
1309 Dbprintf("Can't select card");
1314 if(mifare_desfire_des_auth1(cuid
, dataout
)){
1315 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
1316 Dbprintf("Authentication part1: Fail.");
1321 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 1 FINISHED");
1323 cmd_send(CMD_ACK
,1,cuid
,0,dataout
, sizeof(dataout
));
1326 void Mifare_DES_Auth2(uint32_t arg0
, uint8_t *datain
){
1328 uint32_t cuid
= arg0
;
1329 uint8_t key
[16] = {0x00};
1331 byte_t dataout
[12] = {0x00};
1333 memcpy(key
, datain
, 16);
1335 isOK
= mifare_desfire_des_auth2(cuid
, key
, dataout
);
1338 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
)
1339 Dbprintf("Authentication part2: Failed");
1344 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
)
1345 DbpString("AUTH 2 FINISHED");
1347 cmd_send(CMD_ACK
, isOK
, 0, 0, dataout
, sizeof(dataout
));
1348 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1354 ReaderTransmit(deselect_cmd
, 3 , NULL
);
1355 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1359 void OnError(uint8_t reason
){
1361 ReaderTransmit(deselect_cmd
, 3 , NULL
);
1362 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1363 cmd_send(CMD_ACK
,0,reason
,0,0,0);