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"
21 //-----------------------------------------------------------------------------
22 // Select, Authenticate, Read a MIFARE tag.
24 //-----------------------------------------------------------------------------
25 void MifareReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
28 uint8_t blockNo
= arg0
;
29 uint8_t keyType
= arg1
;
31 ui64Key
= bytes_to_num(datain
, 6);
35 byte_t dataoutbuf
[16];
38 struct Crypto1State mpcs
= {0, 0};
39 struct Crypto1State
*pcs
;
44 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
51 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
52 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
56 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
57 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
61 if(mifare_classic_readblock(pcs
, cuid
, blockNo
, dataoutbuf
)) {
62 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block error");
66 if(mifare_classic_halt(pcs
, cuid
)) {
67 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
75 // ----------------------------- crypto1 destroy
78 if (MF_DBGLEVEL
>= 2) DbpString("READ BLOCK FINISHED");
81 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16);
84 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
88 void MifareUC_Auth(uint8_t arg0
, uint8_t *keybytes
){
90 bool turnOffField
= (arg0
== 1);
92 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
94 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
96 if(!iso14443a_select_card(NULL
, NULL
, NULL
)) {
97 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
102 if(!mifare_ultra_auth(keybytes
)){
103 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication failed");
109 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
112 cmd_send(CMD_ACK
,1,0,0,0,0);
116 // Arg1 = UsePwd bool
117 // datain = PWD bytes,
118 void MifareUReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
)
120 uint8_t blockNo
= arg0
;
121 byte_t dataout
[16] = {0x00};
122 bool useKey
= (arg1
== 1); //UL_C
123 bool usePwd
= (arg1
== 2); //UL_EV1/NTAG
128 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
130 int len
= iso14443a_select_card(NULL
, NULL
, NULL
);
132 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%02X)",len
);
137 // UL-C authentication
139 uint8_t key
[16] = {0x00};
140 memcpy(key
, datain
, sizeof(key
) );
142 if ( !mifare_ultra_auth(key
) ) {
148 // UL-EV1 / NTAG authentication
150 uint8_t pwd
[4] = {0x00};
151 memcpy(pwd
, datain
, 4);
152 uint8_t pack
[4] = {0,0,0,0};
153 if (!mifare_ul_ev1_auth(pwd
, pack
)) {
159 if( mifare_ultra_readblock(blockNo
, dataout
) ) {
160 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Read block error");
165 if( mifare_ultra_halt() ) {
166 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Halt error");
171 cmd_send(CMD_ACK
,1,0,0,dataout
,16);
172 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
176 //-----------------------------------------------------------------------------
177 // Select, Authenticate, Read a MIFARE tag.
178 // read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)
179 //-----------------------------------------------------------------------------
180 void MifareReadSector(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
183 uint8_t sectorNo
= arg0
;
184 uint8_t keyType
= arg1
;
185 uint64_t ui64Key
= 0;
186 ui64Key
= bytes_to_num(datain
, 6);
190 byte_t dataoutbuf
[16 * 16];
193 struct Crypto1State mpcs
= {0, 0};
194 struct Crypto1State
*pcs
;
200 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
207 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
209 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
213 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
215 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
218 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
219 if(mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
+ 16 * blockNo
)) {
221 if (MF_DBGLEVEL
>= 1) Dbprintf("Read sector %2d block %2d error", sectorNo
, blockNo
);
226 if(mifare_classic_halt(pcs
, cuid
)) {
227 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
230 // ----------------------------- crypto1 destroy
231 crypto1_destroy(pcs
);
233 if (MF_DBGLEVEL
>= 2) DbpString("READ SECTOR FINISHED");
236 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16*NumBlocksPerSector(sectorNo
));
240 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
244 // arg0 = blockNo (start)
245 // arg1 = Pages (number of blocks)
247 // datain = KEY bytes
248 void MifareUReadCard(uint8_t arg0
, uint16_t arg1
, uint8_t arg2
, uint8_t *datain
)
250 // free eventually allocated BigBuf memory
255 uint8_t blockNo
= arg0
;
256 uint16_t blocks
= arg1
;
257 bool useKey
= (arg2
== 1); //UL_C
258 bool usePwd
= (arg2
== 2); //UL_EV1/NTAG
259 uint32_t countblocks
= 0;
260 uint8_t *dataout
= BigBuf_malloc(CARD_MEMORY_SIZE
);
261 if (dataout
== NULL
){
262 Dbprintf("out of memory");
269 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
271 int len
= iso14443a_select_card(NULL
, NULL
, NULL
);
273 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%d)",len
);
278 // UL-C authentication
280 uint8_t key
[16] = {0x00};
281 memcpy(key
, datain
, sizeof(key
) );
283 if ( !mifare_ultra_auth(key
) ) {
289 // UL-EV1 / NTAG authentication
291 uint8_t pwd
[4] = {0x00};
292 memcpy(pwd
, datain
, sizeof(pwd
));
293 uint8_t pack
[4] = {0,0,0,0};
295 if (!mifare_ul_ev1_auth(pwd
, pack
)){
301 for (int i
= 0; i
< blocks
; i
++){
302 if ((i
*4) + 4 >= CARD_MEMORY_SIZE
) {
303 Dbprintf("Data exceeds buffer!!");
307 len
= mifare_ultra_readblock(blockNo
+ i
, dataout
+ 4 * i
);
310 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Read block %d error",i
);
311 // if no blocks read - error out
316 //stop at last successful read block and return what we got
324 len
= mifare_ultra_halt();
326 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Halt error");
331 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) Dbprintf("Blocks read %d", countblocks
);
335 cmd_send(CMD_ACK
, 1, countblocks
, BigBuf_max_traceLen(), 0, 0);
336 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
341 //-----------------------------------------------------------------------------
342 // Select, Authenticate, Write a MIFARE tag.
344 //-----------------------------------------------------------------------------
345 void MifareWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
348 uint8_t blockNo
= arg0
;
349 uint8_t keyType
= arg1
;
350 uint64_t ui64Key
= 0;
351 byte_t blockdata
[16];
353 ui64Key
= bytes_to_num(datain
, 6);
354 memcpy(blockdata
, datain
+ 10, 16);
360 struct Crypto1State mpcs
= {0, 0};
361 struct Crypto1State
*pcs
;
367 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
374 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
375 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
379 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
380 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
384 if(mifare_classic_writeblock(pcs
, cuid
, blockNo
, blockdata
)) {
385 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
389 if(mifare_classic_halt(pcs
, cuid
)) {
390 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
398 // ----------------------------- crypto1 destroy
399 crypto1_destroy(pcs
);
401 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
404 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
409 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
413 /* // Command not needed but left for future testing
414 void MifareUWriteBlockCompat(uint8_t arg0, uint8_t *datain)
416 uint8_t blockNo = arg0;
417 byte_t blockdata[16] = {0x00};
419 memcpy(blockdata, datain, 16);
421 uint8_t uid[10] = {0x00};
423 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
426 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
428 if(!iso14443a_select_card(uid, NULL, NULL)) {
429 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
434 if(mifare_ultra_writeblock_compat(blockNo, blockdata)) {
435 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
439 if(mifare_ultra_halt()) {
440 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
445 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");
447 cmd_send(CMD_ACK,1,0,0,0,0);
448 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
453 // Arg0 : Block to write to.
454 // Arg1 : 0 = use no authentication.
455 // 1 = use 0x1A authentication.
456 // 2 = use 0x1B authentication.
457 // datain : 4 first bytes is data to be written.
458 // : 4/16 next bytes is authentication key.
459 void MifareUWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
)
461 uint8_t blockNo
= arg0
;
462 bool useKey
= (arg1
== 1); //UL_C
463 bool usePwd
= (arg1
== 2); //UL_EV1/NTAG
464 byte_t blockdata
[4] = {0x00};
466 memcpy(blockdata
, datain
,4);
471 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
473 if(!iso14443a_select_card(NULL
, NULL
, NULL
)) {
474 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
479 // UL-C authentication
481 uint8_t key
[16] = {0x00};
482 memcpy(key
, datain
+4, sizeof(key
) );
484 if ( !mifare_ultra_auth(key
) ) {
490 // UL-EV1 / NTAG authentication
492 uint8_t pwd
[4] = {0x00};
493 memcpy(pwd
, datain
+4, 4);
494 uint8_t pack
[4] = {0,0,0,0};
495 if (!mifare_ul_ev1_auth(pwd
, pack
)) {
501 if(mifare_ultra_writeblock(blockNo
, blockdata
)) {
502 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
507 if(mifare_ultra_halt()) {
508 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
513 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
515 cmd_send(CMD_ACK
,1,0,0,0,0);
516 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
520 void MifareUSetPwd(uint8_t arg0
, uint8_t *datain
){
522 uint8_t pwd
[16] = {0x00};
523 byte_t blockdata
[4] = {0x00};
525 memcpy(pwd
, datain
, 16);
527 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
529 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
531 if(!iso14443a_select_card(NULL
, NULL
, NULL
)) {
532 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
537 blockdata
[0] = pwd
[7];
538 blockdata
[1] = pwd
[6];
539 blockdata
[2] = pwd
[5];
540 blockdata
[3] = pwd
[4];
541 if(mifare_ultra_writeblock( 44, blockdata
)) {
542 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
547 blockdata
[0] = pwd
[3];
548 blockdata
[1] = pwd
[2];
549 blockdata
[2] = pwd
[1];
550 blockdata
[3] = pwd
[0];
551 if(mifare_ultra_writeblock( 45, blockdata
)) {
552 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
557 blockdata
[0] = pwd
[15];
558 blockdata
[1] = pwd
[14];
559 blockdata
[2] = pwd
[13];
560 blockdata
[3] = pwd
[12];
561 if(mifare_ultra_writeblock( 46, blockdata
)) {
562 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
567 blockdata
[0] = pwd
[11];
568 blockdata
[1] = pwd
[10];
569 blockdata
[2] = pwd
[9];
570 blockdata
[3] = pwd
[8];
571 if(mifare_ultra_writeblock( 47, blockdata
)) {
572 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
577 if(mifare_ultra_halt()) {
578 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
583 cmd_send(CMD_ACK
,1,0,0,0,0);
584 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
588 // Return 1 if the nonce is invalid else return 0
589 int valid_nonce(uint32_t Nt
, uint32_t NtEnc
, uint32_t Ks1
, uint8_t *parity
) {
590 return ((oddparity((Nt
>> 24) & 0xFF) == ((parity
[0]) ^ oddparity((NtEnc
>> 24) & 0xFF) ^ BIT(Ks1
,16))) & \
591 (oddparity((Nt
>> 16) & 0xFF) == ((parity
[1]) ^ oddparity((NtEnc
>> 16) & 0xFF) ^ BIT(Ks1
,8))) & \
592 (oddparity((Nt
>> 8) & 0xFF) == ((parity
[2]) ^ oddparity((NtEnc
>> 8) & 0xFF) ^ BIT(Ks1
,0)))) ? 1 : 0;
596 //-----------------------------------------------------------------------------
597 // MIFARE nested authentication.
599 //-----------------------------------------------------------------------------
600 void MifareNested(uint32_t arg0
, uint32_t arg1
, uint32_t calibrate
, uint8_t *datain
)
603 uint8_t blockNo
= arg0
& 0xff;
604 uint8_t keyType
= (arg0
>> 8) & 0xff;
605 uint8_t targetBlockNo
= arg1
& 0xff;
606 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
607 uint64_t ui64Key
= 0;
609 ui64Key
= bytes_to_num(datain
, 6);
612 uint16_t rtr
, i
, j
, len
;
614 static uint16_t dmin
, dmax
;
616 uint32_t cuid
, nt1
, nt2
, nttmp
, nttest
, ks1
;
618 uint32_t target_nt
[2], target_ks
[2];
620 uint8_t par_array
[4];
622 struct Crypto1State mpcs
= {0, 0};
623 struct Crypto1State
*pcs
;
625 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
627 uint32_t auth1_time
, auth2_time
;
628 static uint16_t delta_time
;
630 // free eventually allocated BigBuf memory
636 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
642 // statistics on nonce distance
644 #define NESTED_MAX_TRIES 12
645 uint16_t unsuccessfull_tries
= 0;
646 if (calibrate
) { // for first call only. Otherwise reuse previous calibration
654 for (rtr
= 0; rtr
< 17; rtr
++) {
656 // Test if the action was cancelled
662 // prepare next select. No need to power down the card.
663 if(mifare_classic_halt(pcs
, cuid
)) {
664 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
669 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
670 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
676 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
677 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
683 auth2_time
= auth1_time
+ delta_time
;
687 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_NESTED
, &nt2
, &auth2_time
)) {
688 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error");
693 nttmp
= prng_successor(nt1
, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160
694 for (i
= 101; i
< 1200; i
++) {
695 nttmp
= prng_successor(nttmp
, 1);
696 if (nttmp
== nt2
) break;
706 delta_time
= auth2_time
- auth1_time
+ 32; // allow some slack for proper timing
708 if (MF_DBGLEVEL
>= 3) Dbprintf("Nested: calibrating... ntdist=%d", i
);
710 unsuccessfull_tries
++;
711 if (unsuccessfull_tries
> NESTED_MAX_TRIES
) { // card isn't vulnerable to nested attack (random numbers are not predictable)
717 davg
= (davg
+ (rtr
- 1)/2) / (rtr
- 1);
719 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
);
727 // -------------------------------------------------------------------------------------------------
731 // get crypted nonces for target sector
732 for(i
=0; i
< 2 && !isOK
; 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_short(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
, isOK
, 0, 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
;
1297 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1305 void Mifare_DES_Auth1(uint8_t arg0
, uint8_t *datain
){
1307 byte_t dataout
[11] = {0x00};
1308 uint8_t uid
[10] = {0x00};
1309 uint32_t cuid
= 0x00;
1312 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1314 int len
= iso14443a_select_card(uid
, NULL
, &cuid
);
1316 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
1321 if(mifare_desfire_des_auth1(cuid
, dataout
)){
1322 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication part1: Fail.");
1327 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 1 FINISHED");
1328 cmd_send(CMD_ACK
,1,cuid
,0,dataout
, sizeof(dataout
));
1331 void Mifare_DES_Auth2(uint32_t arg0
, uint8_t *datain
){
1333 uint32_t cuid
= arg0
;
1334 uint8_t key
[16] = {0x00};
1335 byte_t dataout
[12] = {0x00};
1338 memcpy(key
, datain
, 16);
1340 isOK
= mifare_desfire_des_auth2(cuid
, key
, dataout
);
1343 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) Dbprintf("Authentication part2: Failed");
1348 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 2 FINISHED");
1350 cmd_send(CMD_ACK
, isOK
, 0, 0, dataout
, sizeof(dataout
));
1351 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);