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(uint8_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);
143 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
149 // Arg1 = UsePwd bool
150 // datain = PWD bytes,
151 void MifareUReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
)
153 uint8_t blockNo
= arg0
;
154 byte_t dataout
[16] = {0x00};
155 uint8_t uid
[10] = {0x00};
156 uint8_t key
[16] = {0x00};
157 bool usePwd
= (arg1
== 1);
162 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
164 int len
= iso14443a_select_card(uid
, NULL
, NULL
);
166 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%02X)",len
);
171 // authenticate here.
174 memcpy(key
, datain
, 16);
176 // 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] );
177 // 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] );
179 uint8_t random_a
[8] = {1,1,1,1,1,1,1,1 };
180 uint8_t random_b
[8] = {0x00};
181 uint8_t enc_random_b
[8] = {0x00};
182 uint8_t rnd_ab
[16] = {0x00};
183 uint8_t IV
[8] = {0x00};
186 uint8_t receivedAnswer
[MAX_FRAME_SIZE
];
187 uint8_t receivedAnswerPar
[MAX_PARITY_SIZE
];
189 len
= mifare_sendcmd_short(NULL
, 1, 0x1A, 0x00, receivedAnswer
,receivedAnswerPar
,NULL
);
191 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
197 memcpy(enc_random_b
,receivedAnswer
+1,8);
200 tdes_2key_dec(random_b
, enc_random_b
, sizeof(random_b
), key
, IV
);
202 memcpy(rnd_ab
,random_a
,8);
203 memcpy(rnd_ab
+8,random_b
,8);
205 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) {
206 Dbprintf("enc_B: %02x %02x %02x %02x %02x %02x %02x %02x",
207 enc_random_b
[0],enc_random_b
[1],enc_random_b
[2],enc_random_b
[3],
208 enc_random_b
[4],enc_random_b
[5],enc_random_b
[6],enc_random_b
[7]);
210 Dbprintf(" B: %02x %02x %02x %02x %02x %02x %02x %02x",
211 random_b
[0],random_b
[1],random_b
[2],random_b
[3],
212 random_b
[4],random_b
[5],random_b
[6],random_b
[7]);
214 Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",
215 rnd_ab
[0],rnd_ab
[1],rnd_ab
[2],rnd_ab
[3],
216 rnd_ab
[4],rnd_ab
[5],rnd_ab
[6],rnd_ab
[7]);
218 Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",
219 rnd_ab
[8],rnd_ab
[9],rnd_ab
[10],rnd_ab
[11],
220 rnd_ab
[12],rnd_ab
[13],rnd_ab
[14],rnd_ab
[15] );
223 // encrypt out, in, length, key, iv
224 tdes_2key_enc(rnd_ab
, rnd_ab
, sizeof(rnd_ab
), key
, enc_random_b
);
226 len
= mifare_sendcmd_short_mfucauth(NULL
, 1, 0xAF, rnd_ab
, receivedAnswer
, receivedAnswerPar
, NULL
);
228 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
233 uint8_t enc_resp
[8] = { 0 };
234 uint8_t resp_random_a
[8] = { 0 };
235 memcpy(enc_resp
, receivedAnswer
+1, 8);
237 // decrypt out, in, length, key, iv
238 tdes_2key_dec(resp_random_a
, enc_resp
, 8, key
, enc_random_b
);
239 if ( memcmp(resp_random_a
, random_a
, 8) != 0 )
240 Dbprintf("failed authentication");
242 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) {
243 Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x",
244 rnd_ab
[0],rnd_ab
[1],rnd_ab
[2],rnd_ab
[3],
245 rnd_ab
[4],rnd_ab
[5],rnd_ab
[6],rnd_ab
[7]);
247 Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x",
248 rnd_ab
[8],rnd_ab
[9],rnd_ab
[10],rnd_ab
[11],
249 rnd_ab
[12],rnd_ab
[13],rnd_ab
[14],rnd_ab
[15]);
251 Dbprintf("a: %02x %02x %02x %02x %02x %02x %02x %02x",
252 random_a
[0],random_a
[1],random_a
[2],random_a
[3],
253 random_a
[4],random_a
[5],random_a
[6],random_a
[7]);
255 Dbprintf("b: %02x %02x %02x %02x %02x %02x %02x %02x",
256 resp_random_a
[0],resp_random_a
[1],resp_random_a
[2],resp_random_a
[3],
257 resp_random_a
[4],resp_random_a
[5],resp_random_a
[6],resp_random_a
[7]);
261 if( mifare_ultra_readblock(blockNo
, dataout
) ) {
262 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Read block error");
267 if( mifare_ultra_halt() ) {
268 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Halt error");
273 cmd_send(CMD_ACK
,1,0,0,dataout
,16);
274 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
278 //-----------------------------------------------------------------------------
279 // Select, Authenticate, Read a MIFARE tag.
280 // read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)
281 //-----------------------------------------------------------------------------
282 void MifareReadSector(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
285 uint8_t sectorNo
= arg0
;
286 uint8_t keyType
= arg1
;
287 uint64_t ui64Key
= 0;
288 ui64Key
= bytes_to_num(datain
, 6);
292 byte_t dataoutbuf
[16 * 16];
295 struct Crypto1State mpcs
= {0, 0};
296 struct Crypto1State
*pcs
;
302 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
309 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
311 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
315 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
317 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
320 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
321 if(mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
+ 16 * blockNo
)) {
323 if (MF_DBGLEVEL
>= 1) Dbprintf("Read sector %2d block %2d error", sectorNo
, blockNo
);
328 if(mifare_classic_halt(pcs
, cuid
)) {
329 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
332 // ----------------------------- crypto1 destroy
333 crypto1_destroy(pcs
);
335 if (MF_DBGLEVEL
>= 2) DbpString("READ SECTOR FINISHED");
338 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16*NumBlocksPerSector(sectorNo
));
342 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
346 void MifareUReadCard(uint8_t arg0
, uint16_t arg1
, uint8_t arg2
, uint8_t *datain
)
349 uint8_t blockNo
= arg0
;
350 uint16_t blocks
= arg1
;
351 bool useKey
= (arg2
== 1); //UL_C
352 bool usePwd
= (arg2
== 2); //UL_EV1/NTAG
354 uint8_t dataout
[176] = {0x00};
359 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
361 int len
= iso14443a_select_card(NULL
, NULL
, NULL
);
363 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%d)",len
);
370 uint8_t key
[16] = {0x00};
371 memcpy(key
, datain
, 16);
373 uint8_t random_a
[8] = {1,1,1,1,1,1,1,1 };
374 uint8_t random_b
[8] = {0x00};
375 uint8_t enc_random_b
[8] = {0x00};
376 uint8_t rnd_ab
[16] = {0x00};
377 uint8_t IV
[8] = {0x00};
380 uint8_t receivedAnswer
[MAX_FRAME_SIZE
];
381 uint8_t receivedAnswerPar
[MAX_PARITY_SIZE
];
383 len2
= mifare_sendcmd_short(NULL
, 1, 0x1A, 0x00, receivedAnswer
,receivedAnswerPar
,NULL
);
385 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
391 memcpy(enc_random_b
,receivedAnswer
+1,8);
394 tdes_2key_dec(random_b
, enc_random_b
, sizeof(random_b
), key
, IV
);
396 memcpy(rnd_ab
,random_a
,8);
397 memcpy(rnd_ab
+8,random_b
,8);
399 // encrypt out, in, length, key, iv
400 tdes_2key_enc(rnd_ab
, rnd_ab
, sizeof(rnd_ab
), key
, enc_random_b
);
402 len2
= mifare_sendcmd_short_mfucauth(NULL
, 1, 0xAF, rnd_ab
, receivedAnswer
, receivedAnswerPar
, NULL
);
408 uint8_t enc_resp
[8] = { 0 };
409 uint8_t resp_random_a
[8] = { 0 };
410 memcpy(enc_resp
, receivedAnswer
+1, 8);
412 // decrypt out, in, length, key, iv
413 tdes_2key_dec(resp_random_a
, enc_resp
, 8, key
, enc_random_b
);
414 if ( memcmp(resp_random_a
, random_a
, 8) != 0 )
415 Dbprintf("failed authentication");
418 if (usePwd
) { //ev1 or ntag auth
419 uint8_t Pwd
[4] = {0x00};
420 memcpy(Pwd
, datain
, 4);
421 uint8_t pack
[4] = {0,0,0,0};
423 if (mifare_ul_ev1_auth(Pwd
, pack
)){
425 Dbprintf("failed authentication");
430 for (int i
= 0; i
< blocks
; i
++){
431 len
= mifare_ultra_readblock(blockNo
* 4 + i
, dataout
+ 4 * i
);
434 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Read block %d error",i
);
442 len
= mifare_ultra_halt();
444 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Halt error");
449 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) Dbprintf("Blocks read %d", countblocks
);
453 cmd_send(CMD_ACK
, 1, len
, 0, dataout
, len
);
454 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
458 //-----------------------------------------------------------------------------
459 // Select, Authenticate, Write a MIFARE tag.
461 //-----------------------------------------------------------------------------
462 void MifareWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
465 uint8_t blockNo
= arg0
;
466 uint8_t keyType
= arg1
;
467 uint64_t ui64Key
= 0;
468 byte_t blockdata
[16];
470 ui64Key
= bytes_to_num(datain
, 6);
471 memcpy(blockdata
, datain
+ 10, 16);
477 struct Crypto1State mpcs
= {0, 0};
478 struct Crypto1State
*pcs
;
484 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
491 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
492 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
496 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
497 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
501 if(mifare_classic_writeblock(pcs
, cuid
, blockNo
, blockdata
)) {
502 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
506 if(mifare_classic_halt(pcs
, cuid
)) {
507 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
515 // ----------------------------- crypto1 destroy
516 crypto1_destroy(pcs
);
518 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
521 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
526 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
530 void MifareUWriteBlock(uint8_t arg0
, uint8_t *datain
)
532 uint8_t blockNo
= arg0
;
533 byte_t blockdata
[16] = {0x00};
535 memcpy(blockdata
, datain
, 16);
537 uint8_t uid
[10] = {0x00};
539 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
542 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
544 if(!iso14443a_select_card(uid
, NULL
, NULL
)) {
545 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
550 if(mifare_ultra_writeblock(blockNo
, blockdata
)) {
551 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
555 if(mifare_ultra_halt()) {
556 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
561 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
563 cmd_send(CMD_ACK
,1,0,0,0,0);
564 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
568 void MifareUWriteBlock_Special(uint8_t arg0
, uint8_t *datain
)
570 uint8_t blockNo
= arg0
;
571 byte_t blockdata
[4] = {0x00};
573 memcpy(blockdata
, datain
,4);
575 uint8_t uid
[10] = {0x00};
577 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
579 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
581 if(!iso14443a_select_card(uid
, NULL
, NULL
)) {
582 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
587 if(mifare_ultra_special_writeblock(blockNo
, blockdata
)) {
588 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
593 if(mifare_ultra_halt()) {
594 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
599 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
601 cmd_send(CMD_ACK
,1,0,0,0,0);
602 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
606 void MifareUSetPwd(uint8_t arg0
, uint8_t *datain
){
608 uint8_t pwd
[16] = {0x00};
609 byte_t blockdata
[4] = {0x00};
611 memcpy(pwd
, datain
, 16);
613 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
615 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
617 if(!iso14443a_select_card(NULL
, NULL
, NULL
)) {
618 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
623 blockdata
[0] = pwd
[7];
624 blockdata
[1] = pwd
[6];
625 blockdata
[2] = pwd
[5];
626 blockdata
[3] = pwd
[4];
627 if(mifare_ultra_special_writeblock( 44, blockdata
)) {
628 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
633 blockdata
[0] = pwd
[3];
634 blockdata
[1] = pwd
[2];
635 blockdata
[2] = pwd
[1];
636 blockdata
[3] = pwd
[0];
637 if(mifare_ultra_special_writeblock( 45, blockdata
)) {
638 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
643 blockdata
[0] = pwd
[15];
644 blockdata
[1] = pwd
[14];
645 blockdata
[2] = pwd
[13];
646 blockdata
[3] = pwd
[12];
647 if(mifare_ultra_special_writeblock( 46, blockdata
)) {
648 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
653 blockdata
[0] = pwd
[11];
654 blockdata
[1] = pwd
[10];
655 blockdata
[2] = pwd
[9];
656 blockdata
[3] = pwd
[8];
657 if(mifare_ultra_special_writeblock( 47, blockdata
)) {
658 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
663 if(mifare_ultra_halt()) {
664 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
669 cmd_send(CMD_ACK
,1,0,0,0,0);
670 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
674 // Return 1 if the nonce is invalid else return 0
675 int valid_nonce(uint32_t Nt
, uint32_t NtEnc
, uint32_t Ks1
, uint8_t *parity
) {
676 return ((oddparity((Nt
>> 24) & 0xFF) == ((parity
[0]) ^ oddparity((NtEnc
>> 24) & 0xFF) ^ BIT(Ks1
,16))) & \
677 (oddparity((Nt
>> 16) & 0xFF) == ((parity
[1]) ^ oddparity((NtEnc
>> 16) & 0xFF) ^ BIT(Ks1
,8))) & \
678 (oddparity((Nt
>> 8) & 0xFF) == ((parity
[2]) ^ oddparity((NtEnc
>> 8) & 0xFF) ^ BIT(Ks1
,0)))) ? 1 : 0;
682 //-----------------------------------------------------------------------------
683 // MIFARE nested authentication.
685 //-----------------------------------------------------------------------------
686 void MifareNested(uint32_t arg0
, uint32_t arg1
, uint32_t calibrate
, uint8_t *datain
)
689 uint8_t blockNo
= arg0
& 0xff;
690 uint8_t keyType
= (arg0
>> 8) & 0xff;
691 uint8_t targetBlockNo
= arg1
& 0xff;
692 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
693 uint64_t ui64Key
= 0;
695 ui64Key
= bytes_to_num(datain
, 6);
698 uint16_t rtr
, i
, j
, len
;
700 static uint16_t dmin
, dmax
;
702 uint32_t cuid
, nt1
, nt2
, nttmp
, nttest
, ks1
;
704 uint32_t target_nt
[2], target_ks
[2];
706 uint8_t par_array
[4];
708 struct Crypto1State mpcs
= {0, 0};
709 struct Crypto1State
*pcs
;
711 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
713 uint32_t auth1_time
, auth2_time
;
714 static uint16_t delta_time
;
716 // free eventually allocated BigBuf memory
722 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
728 // statistics on nonce distance
729 if (calibrate
) { // for first call only. Otherwise reuse previous calibration
737 for (rtr
= 0; rtr
< 17; rtr
++) {
739 // prepare next select. No need to power down the card.
740 if(mifare_classic_halt(pcs
, cuid
)) {
741 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
746 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
747 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
753 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
754 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
760 auth2_time
= auth1_time
+ delta_time
;
764 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_NESTED
, &nt2
, &auth2_time
)) {
765 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error");
770 nttmp
= prng_successor(nt1
, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160
771 for (i
= 101; i
< 1200; i
++) {
772 nttmp
= prng_successor(nttmp
, 1);
773 if (nttmp
== nt2
) break;
783 delta_time
= auth2_time
- auth1_time
+ 32; // allow some slack for proper timing
785 if (MF_DBGLEVEL
>= 3) Dbprintf("Nested: calibrating... ntdist=%d", i
);
789 if (rtr
<= 1) return;
791 davg
= (davg
+ (rtr
- 1)/2) / (rtr
- 1);
793 if (MF_DBGLEVEL
>= 3) Dbprintf("min=%d max=%d avg=%d, delta_time=%d", dmin
, dmax
, davg
, delta_time
);
801 // -------------------------------------------------------------------------------------------------
805 // get crypted nonces for target sector
806 for(i
=0; i
< 2; i
++) { // look for exactly two different nonces
809 while(target_nt
[i
] == 0) { // continue until we have an unambiguous nonce
811 // prepare next select. No need to power down the card.
812 if(mifare_classic_halt(pcs
, cuid
)) {
813 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
817 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
818 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
823 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
824 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
828 // nested authentication
829 auth2_time
= auth1_time
+ delta_time
;
830 len
= mifare_sendcmd_shortex(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType
& 0x01), targetBlockNo
, receivedAnswer
, par
, &auth2_time
);
832 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error len=%d", len
);
836 nt2
= bytes_to_num(receivedAnswer
, 4);
837 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i
+1, nt1
, nt2
, par
[0]);
839 // Parity validity check
840 for (j
= 0; j
< 4; j
++) {
841 par_array
[j
] = (oddparity(receivedAnswer
[j
]) != ((par
[0] >> (7-j
)) & 0x01));
845 nttest
= prng_successor(nt1
, dmin
- 1);
846 for (j
= dmin
; j
< dmax
+ 1; j
++) {
847 nttest
= prng_successor(nttest
, 1);
850 if (valid_nonce(nttest
, nt2
, ks1
, par_array
)){
851 if (ncount
> 0) { // we are only interested in disambiguous nonces, try again
852 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i
+1, j
);
856 target_nt
[i
] = nttest
;
859 if (i
== 1 && target_nt
[1] == target_nt
[0]) { // we need two different nonces
861 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j
);
864 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i
+1, j
);
867 if (target_nt
[i
] == 0 && j
== dmax
+1 && MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i
+1);
873 // ----------------------------- crypto1 destroy
874 crypto1_destroy(pcs
);
876 byte_t buf
[4 + 4 * 4];
877 memcpy(buf
, &cuid
, 4);
878 memcpy(buf
+4, &target_nt
[0], 4);
879 memcpy(buf
+8, &target_ks
[0], 4);
880 memcpy(buf
+12, &target_nt
[1], 4);
881 memcpy(buf
+16, &target_ks
[1], 4);
884 cmd_send(CMD_ACK
, 0, 2, targetBlockNo
+ (targetKeyType
* 0x100), buf
, sizeof(buf
));
887 if (MF_DBGLEVEL
>= 3) DbpString("NESTED FINISHED");
889 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
894 //-----------------------------------------------------------------------------
895 // MIFARE check keys. key count up to 85.
897 //-----------------------------------------------------------------------------
898 void MifareChkKeys(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
901 uint8_t blockNo
= arg0
;
902 uint8_t keyType
= arg1
;
903 uint8_t keyCount
= arg2
;
904 uint64_t ui64Key
= 0;
911 struct Crypto1State mpcs
= {0, 0};
912 struct Crypto1State
*pcs
;
916 int OLD_MF_DBGLEVEL
= MF_DBGLEVEL
;
917 MF_DBGLEVEL
= MF_DBG_NONE
;
923 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
929 for (i
= 0; i
< keyCount
; i
++) {
930 if(mifare_classic_halt(pcs
, cuid
)) {
931 if (MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Halt error");
934 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
935 if (OLD_MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Can't select card");
939 ui64Key
= bytes_to_num(datain
+ i
* 6, 6);
940 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
948 // ----------------------------- crypto1 destroy
949 crypto1_destroy(pcs
);
952 cmd_send(CMD_ACK
,isOK
,0,0,datain
+ i
* 6,6);
955 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
958 // restore debug level
959 MF_DBGLEVEL
= OLD_MF_DBGLEVEL
;
962 //-----------------------------------------------------------------------------
963 // MIFARE commands set debug level
965 //-----------------------------------------------------------------------------
966 void MifareSetDbgLvl(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
968 Dbprintf("Debug level: %d", MF_DBGLEVEL
);
971 //-----------------------------------------------------------------------------
972 // Work with emulator memory
974 //-----------------------------------------------------------------------------
975 void MifareEMemClr(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
979 void MifareEMemSet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
980 emlSetMem(datain
, arg0
, arg1
); // data, block num, blocks count
983 void MifareEMemGet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
984 byte_t buf
[USB_CMD_DATA_SIZE
];
985 emlGetMem(buf
, arg0
, arg1
); // data, block num, blocks count (max 4)
988 cmd_send(CMD_ACK
,arg0
,arg1
,0,buf
,USB_CMD_DATA_SIZE
);
992 //-----------------------------------------------------------------------------
993 // Load a card into the emulator memory
995 //-----------------------------------------------------------------------------
996 void MifareECardLoad(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
997 uint8_t numSectors
= arg0
;
998 uint8_t keyType
= arg1
;
999 uint64_t ui64Key
= 0;
1001 struct Crypto1State mpcs
= {0, 0};
1002 struct Crypto1State
*pcs
;
1006 byte_t dataoutbuf
[16];
1007 byte_t dataoutbuf2
[16];
1014 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1022 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
1024 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1027 for (uint8_t sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
1028 ui64Key
= emlGetKey(sectorNo
, keyType
);
1030 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
1032 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo
);
1036 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_NESTED
)) {
1038 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo
);
1043 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
1044 if(isOK
&& mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
)) {
1046 if (MF_DBGLEVEL
>= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo
, blockNo
);
1050 if (blockNo
< NumBlocksPerSector(sectorNo
) - 1) {
1051 emlSetMem(dataoutbuf
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
1052 } else { // sector trailer, keep the keys, set only the AC
1053 emlGetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
1054 memcpy(&dataoutbuf2
[6], &dataoutbuf
[6], 4);
1055 emlSetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
1062 if(mifare_classic_halt(pcs
, cuid
)) {
1063 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1066 // ----------------------------- crypto1 destroy
1067 crypto1_destroy(pcs
);
1069 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1072 if (MF_DBGLEVEL
>= 2) DbpString("EMUL FILL SECTORS FINISHED");
1077 //-----------------------------------------------------------------------------
1078 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
1080 //-----------------------------------------------------------------------------
1081 void MifareCSetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1084 uint8_t needWipe
= arg0
;
1085 // bit 0 - need get UID
1086 // bit 1 - need wupC
1087 // bit 2 - need HALT after sequence
1088 // bit 3 - need init FPGA and field before sequence
1089 // bit 4 - need reset FPGA and LED
1090 uint8_t workFlags
= arg1
;
1091 uint8_t blockNo
= arg2
;
1094 uint8_t wupC1
[] = { 0x40 };
1095 uint8_t wupC2
[] = { 0x43 };
1096 uint8_t wipeC
[] = { 0x41 };
1100 uint8_t uid
[10] = {0x00};
1101 uint8_t d_block
[18] = {0x00};
1104 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1105 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1107 // reset FPGA and LED
1108 if (workFlags
& 0x08) {
1115 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1120 // get UID from chip
1121 if (workFlags
& 0x01) {
1122 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
1123 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1127 if(mifare_classic_halt(NULL
, cuid
)) {
1128 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1135 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1136 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1137 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1141 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
1142 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1143 if (MF_DBGLEVEL
>= 1) Dbprintf("wipeC error");
1147 if(mifare_classic_halt(NULL
, cuid
)) {
1148 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1154 if (workFlags
& 0x02) {
1155 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1156 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1157 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1161 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1162 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1163 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1168 if ((mifare_sendcmd_short(NULL
, 0, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1169 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send command error");
1173 memcpy(d_block
, datain
, 16);
1174 AppendCrc14443a(d_block
, 16);
1176 ReaderTransmit(d_block
, sizeof(d_block
), NULL
);
1177 if ((ReaderReceive(receivedAnswer
, receivedAnswerPar
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1178 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send data error");
1182 if (workFlags
& 0x04) {
1183 if (mifare_classic_halt(NULL
, cuid
)) {
1184 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1194 cmd_send(CMD_ACK
,isOK
,0,0,uid
,4);
1197 if ((workFlags
& 0x10) || (!isOK
)) {
1198 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1204 void MifareCGetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1207 // bit 1 - need wupC
1208 // bit 2 - need HALT after sequence
1209 // bit 3 - need init FPGA and field before sequence
1210 // bit 4 - need reset FPGA and LED
1211 uint8_t workFlags
= arg0
;
1212 uint8_t blockNo
= arg2
;
1215 uint8_t wupC1
[] = { 0x40 };
1216 uint8_t wupC2
[] = { 0x43 };
1220 uint8_t data
[18] = {0x00};
1223 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1224 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1226 if (workFlags
& 0x08) {
1233 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1237 if (workFlags
& 0x02) {
1238 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1239 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1240 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1244 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1245 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1246 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1252 if ((mifare_sendcmd_short(NULL
, 0, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 18)) {
1253 if (MF_DBGLEVEL
>= 1) Dbprintf("read block send command error");
1256 memcpy(data
, receivedAnswer
, 18);
1258 if (workFlags
& 0x04) {
1259 if (mifare_classic_halt(NULL
, cuid
)) {
1260 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1270 cmd_send(CMD_ACK
,isOK
,0,0,data
,18);
1273 if ((workFlags
& 0x10) || (!isOK
)) {
1274 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1279 void MifareCIdent(){
1282 uint8_t wupC1
[] = { 0x40 };
1283 uint8_t wupC2
[] = { 0x43 };
1288 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1289 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1291 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1292 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1296 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1297 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1301 if (mifare_classic_halt(NULL
, 0)) {
1305 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
1312 void Mifare_DES_Auth1(uint8_t arg0
, uint8_t *datain
){
1314 byte_t dataout
[11] = {0x00};
1315 uint8_t uid
[10] = {0x00};
1319 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1321 int len
= iso14443a_select_card(uid
, NULL
, &cuid
);
1323 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
1324 Dbprintf("Can't select card");
1329 if(mifare_desfire_des_auth1(cuid
, dataout
)){
1330 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
1331 Dbprintf("Authentication part1: Fail.");
1336 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 1 FINISHED");
1338 cmd_send(CMD_ACK
,1,cuid
,0,dataout
, sizeof(dataout
));
1341 void Mifare_DES_Auth2(uint32_t arg0
, uint8_t *datain
){
1343 uint32_t cuid
= arg0
;
1344 uint8_t key
[16] = {0x00};
1346 byte_t dataout
[12] = {0x00};
1348 memcpy(key
, datain
, 16);
1350 isOK
= mifare_desfire_des_auth2(cuid
, key
, dataout
);
1353 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
)
1354 Dbprintf("Authentication part2: Failed");
1359 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
)
1360 DbpString("AUTH 2 FINISHED");
1362 cmd_send(CMD_ACK
, isOK
, 0, 0, dataout
, sizeof(dataout
));
1363 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1369 ReaderTransmit(deselect_cmd
, 3 , NULL
);
1370 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1374 void OnError(uint8_t reason
){
1376 ReaderTransmit(deselect_cmd
, 3 , NULL
);
1377 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1378 cmd_send(CMD_ACK
,0,reason
,0,0,0);