1 //-----------------------------------------------------------------------------
2 // Merlok - June 2011, 2012
3 // Gerhard de Koning Gans - May 2008
4 // Hagen Fritsch - June 2010
5 // Midnitesnake - Dec 2013
6 // Andy Davies - Apr 2014
9 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
10 // at your option, any later version. See the LICENSE.txt file for the text of
12 //-----------------------------------------------------------------------------
13 // Routines to support ISO 14443 type A.
14 //-----------------------------------------------------------------------------
16 #include "mifarecmd.h"
22 // the block number for the ISO14443-4 PCB
23 uint8_t pcb_blocknum
= 0;
24 // Deselect card by sending a s-block. the crc is precalced for speed
25 static uint8_t deselect_cmd
[] = {0xc2,0xe0,0xb4};
27 //-----------------------------------------------------------------------------
28 // Select, Authenticate, Read a MIFARE tag.
30 //-----------------------------------------------------------------------------
31 void MifareReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
34 uint8_t blockNo
= arg0
;
35 uint8_t keyType
= arg1
;
37 ui64Key
= bytes_to_num(datain
, 6);
41 byte_t dataoutbuf
[16];
44 struct Crypto1State mpcs
= {0, 0};
45 struct Crypto1State
*pcs
;
48 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
57 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
58 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
62 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
63 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
67 if(mifare_classic_readblock(pcs
, cuid
, blockNo
, dataoutbuf
)) {
68 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block error");
72 if(mifare_classic_halt(pcs
, cuid
)) {
73 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
81 // ----------------------------- crypto1 destroy
84 if (MF_DBGLEVEL
>= 2) DbpString("READ BLOCK FINISHED");
87 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16);
90 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
94 void MifareUC_Auth(uint8_t arg0
, uint8_t *keybytes
){
96 bool turnOffField
= (arg0
== 1);
98 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
100 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
104 if(!iso14443a_select_card(NULL
, NULL
, NULL
)) {
105 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
110 if(!mifare_ultra_auth(keybytes
)){
111 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication failed");
117 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
120 cmd_send(CMD_ACK
,1,0,0,0,0);
124 // Arg1 = UsePwd bool
125 // datain = PWD bytes,
126 void MifareUReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
)
128 uint8_t blockNo
= arg0
;
129 byte_t dataout
[16] = {0x00};
130 bool useKey
= (arg1
== 1); //UL_C
131 bool usePwd
= (arg1
== 2); //UL_EV1/NTAG
135 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
139 int len
= iso14443a_select_card(NULL
, NULL
, NULL
);
141 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%02X)",len
);
146 // UL-C authentication
148 uint8_t key
[16] = {0x00};
149 memcpy(key
, datain
, sizeof(key
) );
151 if ( !mifare_ultra_auth(key
) ) {
157 // UL-EV1 / NTAG authentication
159 uint8_t pwd
[4] = {0x00};
160 memcpy(pwd
, datain
, 4);
161 uint8_t pack
[4] = {0,0,0,0};
162 if (!mifare_ul_ev1_auth(pwd
, pack
)) {
168 if( mifare_ultra_readblock(blockNo
, dataout
) ) {
169 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Read block error");
174 if( mifare_ultra_halt() ) {
175 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Halt error");
180 cmd_send(CMD_ACK
,1,0,0,dataout
,16);
181 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
185 //-----------------------------------------------------------------------------
186 // Select, Authenticate, Read a MIFARE tag.
187 // read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)
188 //-----------------------------------------------------------------------------
189 void MifareReadSector(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
192 uint8_t sectorNo
= arg0
;
193 uint8_t keyType
= arg1
;
194 uint64_t ui64Key
= 0;
195 ui64Key
= bytes_to_num(datain
, 6);
199 byte_t dataoutbuf
[16 * 16];
202 struct Crypto1State mpcs
= {0, 0};
203 struct Crypto1State
*pcs
;
206 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
215 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
217 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
221 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
223 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
226 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
227 if(mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
+ 16 * blockNo
)) {
229 if (MF_DBGLEVEL
>= 1) Dbprintf("Read sector %2d block %2d error", sectorNo
, blockNo
);
234 if(mifare_classic_halt(pcs
, cuid
)) {
235 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
238 // ----------------------------- crypto1 destroy
239 crypto1_destroy(pcs
);
241 if (MF_DBGLEVEL
>= 2) DbpString("READ SECTOR FINISHED");
244 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16*NumBlocksPerSector(sectorNo
));
248 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
252 // arg0 = blockNo (start)
253 // arg1 = Pages (number of blocks)
255 // datain = KEY bytes
256 void MifareUReadCard(uint8_t arg0
, uint16_t arg1
, uint8_t arg2
, uint8_t *datain
)
260 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
262 // free eventually allocated BigBuf memory
267 uint8_t blockNo
= arg0
;
268 uint16_t blocks
= arg1
;
269 bool useKey
= (arg2
== 1); //UL_C
270 bool usePwd
= (arg2
== 2); //UL_EV1/NTAG
271 uint32_t countblocks
= 0;
272 uint8_t *dataout
= BigBuf_malloc(CARD_MEMORY_SIZE
);
273 if (dataout
== NULL
){
274 Dbprintf("out of memory");
279 int len
= iso14443a_select_card(NULL
, NULL
, NULL
);
281 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%d)",len
);
286 // UL-C authentication
288 uint8_t key
[16] = {0x00};
289 memcpy(key
, datain
, sizeof(key
) );
291 if ( !mifare_ultra_auth(key
) ) {
297 // UL-EV1 / NTAG authentication
299 uint8_t pwd
[4] = {0x00};
300 memcpy(pwd
, datain
, sizeof(pwd
));
301 uint8_t pack
[4] = {0,0,0,0};
303 if (!mifare_ul_ev1_auth(pwd
, pack
)){
309 for (int i
= 0; i
< blocks
; i
++){
310 if ((i
*4) + 4 >= CARD_MEMORY_SIZE
) {
311 Dbprintf("Data exceeds buffer!!");
315 len
= mifare_ultra_readblock(blockNo
+ i
, dataout
+ 4 * i
);
318 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Read block %d error",i
);
319 // if no blocks read - error out
324 //stop at last successful read block and return what we got
332 len
= mifare_ultra_halt();
334 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Halt error");
339 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) Dbprintf("Blocks read %d", countblocks
);
343 cmd_send(CMD_ACK
, 1, countblocks
, BigBuf_max_traceLen(), 0, 0);
344 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
349 //-----------------------------------------------------------------------------
350 // Select, Authenticate, Write a MIFARE tag.
352 //-----------------------------------------------------------------------------
353 void MifareWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
356 uint8_t blockNo
= arg0
;
357 uint8_t keyType
= arg1
;
358 uint64_t ui64Key
= 0;
359 byte_t blockdata
[16];
361 ui64Key
= bytes_to_num(datain
, 6);
362 memcpy(blockdata
, datain
+ 10, 16);
368 struct Crypto1State mpcs
= {0, 0};
369 struct Crypto1State
*pcs
;
372 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
381 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
382 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
386 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
387 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
391 if(mifare_classic_writeblock(pcs
, cuid
, blockNo
, blockdata
)) {
392 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
396 if(mifare_classic_halt(pcs
, cuid
)) {
397 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
405 // ----------------------------- crypto1 destroy
406 crypto1_destroy(pcs
);
408 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
411 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
416 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
420 /* // Command not needed but left for future testing
421 void MifareUWriteBlockCompat(uint8_t arg0, uint8_t *datain)
423 uint8_t blockNo = arg0;
424 byte_t blockdata[16] = {0x00};
426 memcpy(blockdata, datain, 16);
428 uint8_t uid[10] = {0x00};
430 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
433 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
435 if(!iso14443a_select_card(uid, NULL, NULL)) {
436 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
441 if(mifare_ultra_writeblock_compat(blockNo, blockdata)) {
442 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
446 if(mifare_ultra_halt()) {
447 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
452 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");
454 cmd_send(CMD_ACK,1,0,0,0,0);
455 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
460 // Arg0 : Block to write to.
461 // Arg1 : 0 = use no authentication.
462 // 1 = use 0x1A authentication.
463 // 2 = use 0x1B authentication.
464 // datain : 4 first bytes is data to be written.
465 // : 4/16 next bytes is authentication key.
466 void MifareUWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
)
468 uint8_t blockNo
= arg0
;
469 bool useKey
= (arg1
== 1); //UL_C
470 bool usePwd
= (arg1
== 2); //UL_EV1/NTAG
471 byte_t blockdata
[4] = {0x00};
473 memcpy(blockdata
, datain
,4);
477 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
481 if(!iso14443a_select_card(NULL
, NULL
, NULL
)) {
482 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
487 // UL-C authentication
489 uint8_t key
[16] = {0x00};
490 memcpy(key
, datain
+4, sizeof(key
) );
492 if ( !mifare_ultra_auth(key
) ) {
498 // UL-EV1 / NTAG authentication
500 uint8_t pwd
[4] = {0x00};
501 memcpy(pwd
, datain
+4, 4);
502 uint8_t pack
[4] = {0,0,0,0};
503 if (!mifare_ul_ev1_auth(pwd
, pack
)) {
509 if(mifare_ultra_writeblock(blockNo
, blockdata
)) {
510 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
515 if(mifare_ultra_halt()) {
516 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
521 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
523 cmd_send(CMD_ACK
,1,0,0,0,0);
524 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
528 void MifareUSetPwd(uint8_t arg0
, uint8_t *datain
){
530 uint8_t pwd
[16] = {0x00};
531 byte_t blockdata
[4] = {0x00};
533 memcpy(pwd
, datain
, 16);
535 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
536 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
540 if(!iso14443a_select_card(NULL
, NULL
, NULL
)) {
541 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
546 blockdata
[0] = pwd
[7];
547 blockdata
[1] = pwd
[6];
548 blockdata
[2] = pwd
[5];
549 blockdata
[3] = pwd
[4];
550 if(mifare_ultra_writeblock( 44, blockdata
)) {
551 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
556 blockdata
[0] = pwd
[3];
557 blockdata
[1] = pwd
[2];
558 blockdata
[2] = pwd
[1];
559 blockdata
[3] = pwd
[0];
560 if(mifare_ultra_writeblock( 45, blockdata
)) {
561 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
566 blockdata
[0] = pwd
[15];
567 blockdata
[1] = pwd
[14];
568 blockdata
[2] = pwd
[13];
569 blockdata
[3] = pwd
[12];
570 if(mifare_ultra_writeblock( 46, blockdata
)) {
571 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
576 blockdata
[0] = pwd
[11];
577 blockdata
[1] = pwd
[10];
578 blockdata
[2] = pwd
[9];
579 blockdata
[3] = pwd
[8];
580 if(mifare_ultra_writeblock( 47, blockdata
)) {
581 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
586 if(mifare_ultra_halt()) {
587 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
592 cmd_send(CMD_ACK
,1,0,0,0,0);
593 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
597 // Return 1 if the nonce is invalid else return 0
598 int valid_nonce(uint32_t Nt
, uint32_t NtEnc
, uint32_t Ks1
, uint8_t *parity
) {
599 return ((oddparity8((Nt
>> 24) & 0xFF) == ((parity
[0]) ^ oddparity8((NtEnc
>> 24) & 0xFF) ^ BIT(Ks1
,16))) & \
600 (oddparity8((Nt
>> 16) & 0xFF) == ((parity
[1]) ^ oddparity8((NtEnc
>> 16) & 0xFF) ^ BIT(Ks1
,8))) & \
601 (oddparity8((Nt
>> 8) & 0xFF) == ((parity
[2]) ^ oddparity8((NtEnc
>> 8) & 0xFF) ^ BIT(Ks1
,0)))) ? 1 : 0;
605 //-----------------------------------------------------------------------------
606 // MIFARE nested authentication.
608 //-----------------------------------------------------------------------------
609 void MifareNested(uint32_t arg0
, uint32_t arg1
, uint32_t calibrate
, uint8_t *datain
)
612 uint8_t blockNo
= arg0
& 0xff;
613 uint8_t keyType
= (arg0
>> 8) & 0xff;
614 uint8_t targetBlockNo
= arg1
& 0xff;
615 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
616 uint64_t ui64Key
= 0;
618 ui64Key
= bytes_to_num(datain
, 6);
621 uint16_t rtr
, i
, j
, len
;
623 static uint16_t dmin
, dmax
;
625 uint32_t cuid
, nt1
, nt2
, nttmp
, nttest
, ks1
;
627 uint32_t target_nt
[2], target_ks
[2];
629 uint8_t par_array
[4];
631 struct Crypto1State mpcs
= {0, 0};
632 struct Crypto1State
*pcs
;
634 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
636 uint32_t auth1_time
, auth2_time
;
637 static uint16_t delta_time
;
641 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
643 // free eventually allocated BigBuf memory
646 if (calibrate
) clear_trace();
649 // statistics on nonce distance
651 #define NESTED_MAX_TRIES 12
652 uint16_t unsuccessfull_tries
= 0;
653 if (calibrate
) { // for first call only. Otherwise reuse previous calibration
661 for (rtr
= 0; rtr
< 17; rtr
++) {
663 // Test if the action was cancelled
669 // prepare next select. No need to power down the card.
670 if(mifare_classic_halt(pcs
, cuid
)) {
671 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
676 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
677 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
683 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
684 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
690 auth2_time
= auth1_time
+ delta_time
;
694 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_NESTED
, &nt2
, &auth2_time
)) {
695 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error");
700 nttmp
= prng_successor(nt1
, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160
701 for (i
= 101; i
< 1200; i
++) {
702 nttmp
= prng_successor(nttmp
, 1);
703 if (nttmp
== nt2
) break;
713 delta_time
= auth2_time
- auth1_time
+ 32; // allow some slack for proper timing
715 if (MF_DBGLEVEL
>= 3) Dbprintf("Nested: calibrating... ntdist=%d", i
);
717 unsuccessfull_tries
++;
718 if (unsuccessfull_tries
> NESTED_MAX_TRIES
) { // card isn't vulnerable to nested attack (random numbers are not predictable)
724 davg
= (davg
+ (rtr
- 1)/2) / (rtr
- 1);
726 if (MF_DBGLEVEL
>= 3) Dbprintf("rtr=%d isOK=%d min=%d max=%d avg=%d, delta_time=%d", rtr
, isOK
, dmin
, dmax
, davg
, delta_time
);
734 // -------------------------------------------------------------------------------------------------
738 // get crypted nonces for target sector
739 for(i
=0; i
< 2 && !isOK
; i
++) { // look for exactly two different nonces
742 while(target_nt
[i
] == 0) { // continue until we have an unambiguous nonce
744 // prepare next select. No need to power down the card.
745 if(mifare_classic_halt(pcs
, cuid
)) {
746 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
750 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
751 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
756 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
757 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
761 // nested authentication
762 auth2_time
= auth1_time
+ delta_time
;
763 len
= mifare_sendcmd_short(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType
& 0x01), targetBlockNo
, receivedAnswer
, par
, &auth2_time
);
765 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error len=%d", len
);
769 nt2
= bytes_to_num(receivedAnswer
, 4);
770 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i
+1, nt1
, nt2
, par
[0]);
772 // Parity validity check
773 for (j
= 0; j
< 4; j
++) {
774 par_array
[j
] = (oddparity8(receivedAnswer
[j
]) != ((par
[0] >> (7-j
)) & 0x01));
778 nttest
= prng_successor(nt1
, dmin
- 1);
779 for (j
= dmin
; j
< dmax
+ 1; j
++) {
780 nttest
= prng_successor(nttest
, 1);
783 if (valid_nonce(nttest
, nt2
, ks1
, par_array
)){
784 if (ncount
> 0) { // we are only interested in disambiguous nonces, try again
785 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i
+1, j
);
789 target_nt
[i
] = nttest
;
792 if (i
== 1 && target_nt
[1] == target_nt
[0]) { // we need two different nonces
794 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j
);
797 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i
+1, j
);
800 if (target_nt
[i
] == 0 && j
== dmax
+1 && MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i
+1);
806 // ----------------------------- crypto1 destroy
807 crypto1_destroy(pcs
);
809 byte_t buf
[4 + 4 * 4];
810 memcpy(buf
, &cuid
, 4);
811 memcpy(buf
+4, &target_nt
[0], 4);
812 memcpy(buf
+8, &target_ks
[0], 4);
813 memcpy(buf
+12, &target_nt
[1], 4);
814 memcpy(buf
+16, &target_ks
[1], 4);
817 cmd_send(CMD_ACK
, isOK
, 0, targetBlockNo
+ (targetKeyType
* 0x100), buf
, sizeof(buf
));
820 if (MF_DBGLEVEL
>= 3) DbpString("NESTED FINISHED");
822 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
826 //-----------------------------------------------------------------------------
827 // MIFARE check keys. key count up to 85.
829 //-----------------------------------------------------------------------------
830 void MifareChkKeys(uint16_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
833 uint8_t blockNo
= arg0
& 0xff;
834 uint8_t keyType
= (arg0
>> 8) & 0xff;
835 bool clearTrace
= arg1
;
836 uint8_t keyCount
= arg2
;
837 uint64_t ui64Key
= 0;
844 struct Crypto1State mpcs
= {0, 0};
845 struct Crypto1State
*pcs
;
849 int OLD_MF_DBGLEVEL
= MF_DBGLEVEL
;
850 MF_DBGLEVEL
= MF_DBG_NONE
;
855 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
857 if (clearTrace
) clear_trace();
860 for (i
= 0; i
< keyCount
; i
++) {
861 if(mifare_classic_halt(pcs
, cuid
)) {
862 if (MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Halt error");
865 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
866 if (OLD_MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Can't select card");
870 ui64Key
= bytes_to_num(datain
+ i
* 6, 6);
871 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
879 // ----------------------------- crypto1 destroy
880 crypto1_destroy(pcs
);
883 cmd_send(CMD_ACK
,isOK
,0,0,datain
+ i
* 6,6);
886 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
889 // restore debug level
890 MF_DBGLEVEL
= OLD_MF_DBGLEVEL
;
893 //-----------------------------------------------------------------------------
894 // MIFARE commands set debug level
896 //-----------------------------------------------------------------------------
897 void MifareSetDbgLvl(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
899 Dbprintf("Debug level: %d", MF_DBGLEVEL
);
902 //-----------------------------------------------------------------------------
903 // Work with emulator memory
905 // Note: we call FpgaDownloadAndGo(FPGA_BITSTREAM_HF) here although FPGA is not
906 // involved in dealing with emulator memory. But if it is called later, it might
907 // destroy the Emulator Memory.
908 //-----------------------------------------------------------------------------
910 void MifareEMemClr(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
911 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
915 void MifareEMemSet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
916 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
917 emlSetMem(datain
, arg0
, arg1
); // data, block num, blocks count
920 void MifareEMemGet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
921 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
922 byte_t buf
[USB_CMD_DATA_SIZE
];
923 emlGetMem(buf
, arg0
, arg1
); // data, block num, blocks count (max 4)
926 cmd_send(CMD_ACK
,arg0
,arg1
,0,buf
,USB_CMD_DATA_SIZE
);
930 //-----------------------------------------------------------------------------
931 // Load a card into the emulator memory
933 //-----------------------------------------------------------------------------
934 void MifareECardLoad(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
935 uint8_t numSectors
= arg0
;
936 uint8_t keyType
= arg1
;
937 uint64_t ui64Key
= 0;
939 struct Crypto1State mpcs
= {0, 0};
940 struct Crypto1State
*pcs
;
944 byte_t dataoutbuf
[16];
945 byte_t dataoutbuf2
[16];
951 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
958 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
960 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
963 for (uint8_t sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
964 ui64Key
= emlGetKey(sectorNo
, keyType
);
966 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
968 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo
);
972 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_NESTED
)) {
974 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo
);
979 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
980 if(isOK
&& mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
)) {
982 if (MF_DBGLEVEL
>= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo
, blockNo
);
986 if (blockNo
< NumBlocksPerSector(sectorNo
) - 1) {
987 emlSetMem(dataoutbuf
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
988 } else { // sector trailer, keep the keys, set only the AC
989 emlGetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
990 memcpy(&dataoutbuf2
[6], &dataoutbuf
[6], 4);
991 emlSetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
998 if(mifare_classic_halt(pcs
, cuid
)) {
999 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1002 // ----------------------------- crypto1 destroy
1003 crypto1_destroy(pcs
);
1005 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1008 if (MF_DBGLEVEL
>= 2) DbpString("EMUL FILL SECTORS FINISHED");
1013 //-----------------------------------------------------------------------------
1014 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
1016 //-----------------------------------------------------------------------------
1017 void MifareCSetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1020 uint8_t needWipe
= arg0
;
1021 // bit 0 - need get UID
1022 // bit 1 - need wupC
1023 // bit 2 - need HALT after sequence
1024 // bit 3 - need init FPGA and field before sequence
1025 // bit 4 - need reset FPGA and LED
1026 uint8_t workFlags
= arg1
;
1027 uint8_t blockNo
= arg2
;
1030 uint8_t wupC1
[] = { 0x40 };
1031 uint8_t wupC2
[] = { 0x43 };
1032 uint8_t wipeC
[] = { 0x41 };
1036 uint8_t uid
[10] = {0x00};
1037 uint8_t d_block
[18] = {0x00};
1040 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1041 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1043 // reset FPGA and LED
1044 if (workFlags
& 0x08) {
1048 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1056 // get UID from chip
1057 if (workFlags
& 0x01) {
1058 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
1059 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1063 if(mifare_classic_halt(NULL
, cuid
)) {
1064 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1071 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1072 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1073 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1077 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
1078 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1079 if (MF_DBGLEVEL
>= 1) Dbprintf("wipeC error");
1083 if(mifare_classic_halt(NULL
, cuid
)) {
1084 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1090 if (workFlags
& 0x02) {
1091 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1092 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1093 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1097 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1098 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1099 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1104 if ((mifare_sendcmd_short(NULL
, 0, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1105 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send command error");
1109 memcpy(d_block
, datain
, 16);
1110 AppendCrc14443a(d_block
, 16);
1112 ReaderTransmit(d_block
, sizeof(d_block
), NULL
);
1113 if ((ReaderReceive(receivedAnswer
, receivedAnswerPar
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1114 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send data error");
1118 if (workFlags
& 0x04) {
1119 if (mifare_classic_halt(NULL
, cuid
)) {
1120 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1130 cmd_send(CMD_ACK
,isOK
,0,0,uid
,4);
1133 if ((workFlags
& 0x10) || (!isOK
)) {
1134 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1140 void MifareCGetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1143 // bit 1 - need wupC
1144 // bit 2 - need HALT after sequence
1145 // bit 3 - need init FPGA and field before sequence
1146 // bit 4 - need reset FPGA and LED
1147 // bit 5 - need to set datain instead of issuing USB reply (called via ARM for StandAloneMode14a)
1148 uint8_t workFlags
= arg0
;
1149 uint8_t blockNo
= arg2
;
1152 uint8_t wupC1
[] = { 0x40 };
1153 uint8_t wupC2
[] = { 0x43 };
1157 uint8_t data
[18] = {0x00};
1160 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1161 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1163 if (workFlags
& 0x08) {
1167 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1174 if (workFlags
& 0x02) {
1175 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1176 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1177 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1181 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1182 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1183 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1189 if ((mifare_sendcmd_short(NULL
, 0, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 18)) {
1190 if (MF_DBGLEVEL
>= 1) Dbprintf("read block send command error");
1193 memcpy(data
, receivedAnswer
, 18);
1195 if (workFlags
& 0x04) {
1196 if (mifare_classic_halt(NULL
, cuid
)) {
1197 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1207 if (workFlags
& 0x20) {
1209 memcpy(datain
, data
, 18);
1212 cmd_send(CMD_ACK
,isOK
,0,0,data
,18);
1215 if ((workFlags
& 0x10) || (!isOK
)) {
1216 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1221 void MifareCIdent(){
1224 uint8_t wupC1
[] = { 0x40 };
1225 uint8_t wupC2
[] = { 0x43 };
1230 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1231 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1233 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1234 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1238 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1239 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1243 if (mifare_classic_halt(NULL
, 0)) {
1247 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
1254 void Mifare_DES_Auth1(uint8_t arg0
, uint8_t *datain
){
1256 byte_t dataout
[11] = {0x00};
1257 uint8_t uid
[10] = {0x00};
1260 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1263 int len
= iso14443a_select_card(uid
, NULL
, &cuid
);
1265 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
1270 if(mifare_desfire_des_auth1(cuid
, dataout
)){
1271 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication part1: Fail.");
1276 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 1 FINISHED");
1277 cmd_send(CMD_ACK
,1,cuid
,0,dataout
, sizeof(dataout
));
1280 void Mifare_DES_Auth2(uint32_t arg0
, uint8_t *datain
){
1282 uint32_t cuid
= arg0
;
1283 uint8_t key
[16] = {0x00};
1285 byte_t dataout
[12] = {0x00};
1287 memcpy(key
, datain
, 16);
1289 isOK
= mifare_desfire_des_auth2(cuid
, key
, dataout
);
1292 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) Dbprintf("Authentication part2: Failed");
1297 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 2 FINISHED");
1299 cmd_send(CMD_ACK
, isOK
, 0, 0, dataout
, sizeof(dataout
));
1300 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1306 ReaderTransmit(deselect_cmd
, 3 , NULL
);
1307 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1311 void OnError(uint8_t reason
){
1313 ReaderTransmit(deselect_cmd
, 3 , NULL
);
1314 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1315 cmd_send(CMD_ACK
,0,reason
,0,0,0);