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
;
42 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
);
98 if(!iso14443a_select_card(NULL
, NULL
, NULL
)) {
99 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
104 if(!mifare_ultra_auth(keybytes
)){
105 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication failed");
111 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
114 cmd_send(CMD_ACK
,1,0,0,0,0);
118 // Arg1 = UsePwd bool
119 // datain = PWD bytes,
120 void MifareUReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
)
122 uint8_t blockNo
= arg0
;
123 byte_t dataout
[16] = {0x00};
124 bool useKey
= (arg1
== 1); //UL_C
125 bool usePwd
= (arg1
== 2); //UL_EV1/NTAG
129 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
133 int len
= iso14443a_select_card(NULL
, NULL
, NULL
);
135 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%02X)",len
);
140 // UL-C authentication
142 uint8_t key
[16] = {0x00};
143 memcpy(key
, datain
, sizeof(key
) );
145 if ( !mifare_ultra_auth(key
) ) {
151 // UL-EV1 / NTAG authentication
153 uint8_t pwd
[4] = {0x00};
154 memcpy(pwd
, datain
, 4);
155 uint8_t pack
[4] = {0,0,0,0};
156 if (!mifare_ul_ev1_auth(pwd
, pack
)) {
162 if( mifare_ultra_readblock(blockNo
, dataout
) ) {
163 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Read block error");
168 if( mifare_ultra_halt() ) {
169 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Halt error");
174 cmd_send(CMD_ACK
,1,0,0,dataout
,16);
175 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
179 //-----------------------------------------------------------------------------
180 // Select, Authenticate, Read a MIFARE tag.
181 // read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)
182 //-----------------------------------------------------------------------------
183 void MifareReadSector(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
186 uint8_t sectorNo
= arg0
;
187 uint8_t keyType
= arg1
;
188 uint64_t ui64Key
= 0;
189 ui64Key
= bytes_to_num(datain
, 6);
193 byte_t dataoutbuf
[16 * 16];
196 struct Crypto1State mpcs
= {0, 0};
197 struct Crypto1State
*pcs
;
200 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
209 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
211 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
215 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
217 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
220 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
221 if(mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
+ 16 * blockNo
)) {
223 if (MF_DBGLEVEL
>= 1) Dbprintf("Read sector %2d block %2d error", sectorNo
, blockNo
);
228 if(mifare_classic_halt(pcs
, cuid
)) {
229 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
232 // ----------------------------- crypto1 destroy
233 crypto1_destroy(pcs
);
235 if (MF_DBGLEVEL
>= 2) DbpString("READ SECTOR FINISHED");
238 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16*NumBlocksPerSector(sectorNo
));
242 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
246 // arg0 = blockNo (start)
247 // arg1 = Pages (number of blocks)
249 // datain = KEY bytes
250 void MifareUReadCard(uint8_t arg0
, uint16_t arg1
, uint8_t arg2
, uint8_t *datain
)
254 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
256 // free eventually allocated BigBuf memory
261 uint8_t blockNo
= arg0
;
262 uint16_t blocks
= arg1
;
263 bool useKey
= (arg2
== 1); //UL_C
264 bool usePwd
= (arg2
== 2); //UL_EV1/NTAG
265 uint32_t countblocks
= 0;
266 uint8_t *dataout
= BigBuf_malloc(CARD_MEMORY_SIZE
);
267 if (dataout
== NULL
){
268 Dbprintf("out of memory");
273 int len
= iso14443a_select_card(NULL
, NULL
, NULL
);
275 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%d)",len
);
280 // UL-C authentication
282 uint8_t key
[16] = {0x00};
283 memcpy(key
, datain
, sizeof(key
) );
285 if ( !mifare_ultra_auth(key
) ) {
291 // UL-EV1 / NTAG authentication
293 uint8_t pwd
[4] = {0x00};
294 memcpy(pwd
, datain
, sizeof(pwd
));
295 uint8_t pack
[4] = {0,0,0,0};
297 if (!mifare_ul_ev1_auth(pwd
, pack
)){
303 for (int i
= 0; i
< blocks
; i
++){
304 if ((i
*4) + 4 >= CARD_MEMORY_SIZE
) {
305 Dbprintf("Data exceeds buffer!!");
309 len
= mifare_ultra_readblock(blockNo
+ i
, dataout
+ 4 * i
);
312 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Read block %d error",i
);
313 // if no blocks read - error out
318 //stop at last successful read block and return what we got
326 len
= mifare_ultra_halt();
328 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Halt error");
333 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) Dbprintf("Blocks read %d", countblocks
);
337 cmd_send(CMD_ACK
, 1, countblocks
, BigBuf_max_traceLen(), 0, 0);
338 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
343 //-----------------------------------------------------------------------------
344 // Select, Authenticate, Write a MIFARE tag.
346 //-----------------------------------------------------------------------------
347 void MifareWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
350 uint8_t blockNo
= arg0
;
351 uint8_t keyType
= arg1
;
352 uint64_t ui64Key
= 0;
353 byte_t blockdata
[16];
355 ui64Key
= bytes_to_num(datain
, 6);
356 memcpy(blockdata
, datain
+ 10, 16);
362 struct Crypto1State mpcs
= {0, 0};
363 struct Crypto1State
*pcs
;
366 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
375 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
376 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
380 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
381 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
385 if(mifare_classic_writeblock(pcs
, cuid
, blockNo
, blockdata
)) {
386 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
390 if(mifare_classic_halt(pcs
, cuid
)) {
391 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
399 // ----------------------------- crypto1 destroy
400 crypto1_destroy(pcs
);
402 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
405 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
410 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
414 /* // Command not needed but left for future testing
415 void MifareUWriteBlockCompat(uint8_t arg0, uint8_t *datain)
417 uint8_t blockNo = arg0;
418 byte_t blockdata[16] = {0x00};
420 memcpy(blockdata, datain, 16);
422 uint8_t uid[10] = {0x00};
424 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
427 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
429 if(!iso14443a_select_card(uid, NULL, NULL)) {
430 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
435 if(mifare_ultra_writeblock_compat(blockNo, blockdata)) {
436 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
440 if(mifare_ultra_halt()) {
441 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
446 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");
448 cmd_send(CMD_ACK,1,0,0,0,0);
449 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
454 // Arg0 : Block to write to.
455 // Arg1 : 0 = use no authentication.
456 // 1 = use 0x1A authentication.
457 // 2 = use 0x1B authentication.
458 // datain : 4 first bytes is data to be written.
459 // : 4/16 next bytes is authentication key.
460 void MifareUWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
)
462 uint8_t blockNo
= arg0
;
463 bool useKey
= (arg1
== 1); //UL_C
464 bool usePwd
= (arg1
== 2); //UL_EV1/NTAG
465 byte_t blockdata
[4] = {0x00};
467 memcpy(blockdata
, datain
,4);
471 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
475 if(!iso14443a_select_card(NULL
, NULL
, NULL
)) {
476 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
481 // UL-C authentication
483 uint8_t key
[16] = {0x00};
484 memcpy(key
, datain
+4, sizeof(key
) );
486 if ( !mifare_ultra_auth(key
) ) {
492 // UL-EV1 / NTAG authentication
494 uint8_t pwd
[4] = {0x00};
495 memcpy(pwd
, datain
+4, 4);
496 uint8_t pack
[4] = {0,0,0,0};
497 if (!mifare_ul_ev1_auth(pwd
, pack
)) {
503 if(mifare_ultra_writeblock(blockNo
, blockdata
)) {
504 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
509 if(mifare_ultra_halt()) {
510 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
515 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
517 cmd_send(CMD_ACK
,1,0,0,0,0);
518 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
522 void MifareUSetPwd(uint8_t arg0
, uint8_t *datain
){
524 uint8_t pwd
[16] = {0x00};
525 byte_t blockdata
[4] = {0x00};
527 memcpy(pwd
, datain
, 16);
529 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
530 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
534 if(!iso14443a_select_card(NULL
, NULL
, NULL
)) {
535 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
540 blockdata
[0] = pwd
[7];
541 blockdata
[1] = pwd
[6];
542 blockdata
[2] = pwd
[5];
543 blockdata
[3] = pwd
[4];
544 if(mifare_ultra_writeblock( 44, blockdata
)) {
545 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
550 blockdata
[0] = pwd
[3];
551 blockdata
[1] = pwd
[2];
552 blockdata
[2] = pwd
[1];
553 blockdata
[3] = pwd
[0];
554 if(mifare_ultra_writeblock( 45, blockdata
)) {
555 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
560 blockdata
[0] = pwd
[15];
561 blockdata
[1] = pwd
[14];
562 blockdata
[2] = pwd
[13];
563 blockdata
[3] = pwd
[12];
564 if(mifare_ultra_writeblock( 46, blockdata
)) {
565 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
570 blockdata
[0] = pwd
[11];
571 blockdata
[1] = pwd
[10];
572 blockdata
[2] = pwd
[9];
573 blockdata
[3] = pwd
[8];
574 if(mifare_ultra_writeblock( 47, blockdata
)) {
575 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
580 if(mifare_ultra_halt()) {
581 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
586 cmd_send(CMD_ACK
,1,0,0,0,0);
587 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
591 // Return 1 if the nonce is invalid else return 0
592 int valid_nonce(uint32_t Nt
, uint32_t NtEnc
, uint32_t Ks1
, uint8_t *parity
) {
593 return ((oddparity((Nt
>> 24) & 0xFF) == ((parity
[0]) ^ oddparity((NtEnc
>> 24) & 0xFF) ^ BIT(Ks1
,16))) & \
594 (oddparity((Nt
>> 16) & 0xFF) == ((parity
[1]) ^ oddparity((NtEnc
>> 16) & 0xFF) ^ BIT(Ks1
,8))) & \
595 (oddparity((Nt
>> 8) & 0xFF) == ((parity
[2]) ^ oddparity((NtEnc
>> 8) & 0xFF) ^ BIT(Ks1
,0)))) ? 1 : 0;
599 //-----------------------------------------------------------------------------
600 // MIFARE nested authentication.
602 //-----------------------------------------------------------------------------
603 void MifareNested(uint32_t arg0
, uint32_t arg1
, uint32_t calibrate
, uint8_t *datain
)
606 uint8_t blockNo
= arg0
& 0xff;
607 uint8_t keyType
= (arg0
>> 8) & 0xff;
608 uint8_t targetBlockNo
= arg1
& 0xff;
609 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
610 uint64_t ui64Key
= 0;
612 ui64Key
= bytes_to_num(datain
, 6);
615 uint16_t rtr
, i
, j
, len
;
617 static uint16_t dmin
, dmax
;
619 uint32_t cuid
, nt1
, nt2
, nttmp
, nttest
, ks1
;
621 uint32_t target_nt
[2], target_ks
[2];
623 uint8_t par_array
[4];
625 struct Crypto1State mpcs
= {0, 0};
626 struct Crypto1State
*pcs
;
628 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
630 uint32_t auth1_time
, auth2_time
;
631 static uint16_t delta_time
;
635 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
637 // free eventually allocated BigBuf memory
640 if (calibrate
) clear_trace();
643 // statistics on nonce distance
645 #define NESTED_MAX_TRIES 12
646 uint16_t unsuccessfull_tries
= 0;
647 if (calibrate
) { // for first call only. Otherwise reuse previous calibration
655 for (rtr
= 0; rtr
< 17; rtr
++) {
657 // Test if the action was cancelled
663 // prepare next select. No need to power down the card.
664 if(mifare_classic_halt(pcs
, cuid
)) {
665 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
670 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
671 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
677 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
678 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
684 auth2_time
= auth1_time
+ delta_time
;
688 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_NESTED
, &nt2
, &auth2_time
)) {
689 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error");
694 nttmp
= prng_successor(nt1
, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160
695 for (i
= 101; i
< 1200; i
++) {
696 nttmp
= prng_successor(nttmp
, 1);
697 if (nttmp
== nt2
) break;
707 delta_time
= auth2_time
- auth1_time
+ 32; // allow some slack for proper timing
709 if (MF_DBGLEVEL
>= 3) Dbprintf("Nested: calibrating... ntdist=%d", i
);
711 unsuccessfull_tries
++;
712 if (unsuccessfull_tries
> NESTED_MAX_TRIES
) { // card isn't vulnerable to nested attack (random numbers are not predictable)
718 davg
= (davg
+ (rtr
- 1)/2) / (rtr
- 1);
720 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
);
728 // -------------------------------------------------------------------------------------------------
732 // get crypted nonces for target sector
733 for(i
=0; i
< 2 && !isOK
; i
++) { // look for exactly two different nonces
736 while(target_nt
[i
] == 0) { // continue until we have an unambiguous nonce
738 // prepare next select. No need to power down the card.
739 if(mifare_classic_halt(pcs
, cuid
)) {
740 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
744 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
745 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
750 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
751 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
755 // nested authentication
756 auth2_time
= auth1_time
+ delta_time
;
757 len
= mifare_sendcmd_short(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType
& 0x01), targetBlockNo
, receivedAnswer
, par
, &auth2_time
);
759 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error len=%d", len
);
763 nt2
= bytes_to_num(receivedAnswer
, 4);
764 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i
+1, nt1
, nt2
, par
[0]);
766 // Parity validity check
767 for (j
= 0; j
< 4; j
++) {
768 par_array
[j
] = (oddparity(receivedAnswer
[j
]) != ((par
[0] >> (7-j
)) & 0x01));
772 nttest
= prng_successor(nt1
, dmin
- 1);
773 for (j
= dmin
; j
< dmax
+ 1; j
++) {
774 nttest
= prng_successor(nttest
, 1);
777 if (valid_nonce(nttest
, nt2
, ks1
, par_array
)){
778 if (ncount
> 0) { // we are only interested in disambiguous nonces, try again
779 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i
+1, j
);
783 target_nt
[i
] = nttest
;
786 if (i
== 1 && target_nt
[1] == target_nt
[0]) { // we need two different nonces
788 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j
);
791 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i
+1, j
);
794 if (target_nt
[i
] == 0 && j
== dmax
+1 && MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i
+1);
800 // ----------------------------- crypto1 destroy
801 crypto1_destroy(pcs
);
803 byte_t buf
[4 + 4 * 4];
804 memcpy(buf
, &cuid
, 4);
805 memcpy(buf
+4, &target_nt
[0], 4);
806 memcpy(buf
+8, &target_ks
[0], 4);
807 memcpy(buf
+12, &target_nt
[1], 4);
808 memcpy(buf
+16, &target_ks
[1], 4);
811 cmd_send(CMD_ACK
, isOK
, 0, targetBlockNo
+ (targetKeyType
* 0x100), buf
, sizeof(buf
));
814 if (MF_DBGLEVEL
>= 3) DbpString("NESTED FINISHED");
816 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
820 //-----------------------------------------------------------------------------
821 // MIFARE check keys. key count up to 85.
823 //-----------------------------------------------------------------------------
824 void MifareChkKeys(uint16_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
827 uint8_t blockNo
= arg0
& 0xff;
828 uint8_t keyType
= (arg0
>> 8) & 0xff;
829 bool clearTrace
= arg1
;
830 uint8_t keyCount
= arg2
;
831 uint64_t ui64Key
= 0;
838 struct Crypto1State mpcs
= {0, 0};
839 struct Crypto1State
*pcs
;
843 int OLD_MF_DBGLEVEL
= MF_DBGLEVEL
;
844 MF_DBGLEVEL
= MF_DBG_NONE
;
849 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
851 if (clearTrace
) clear_trace();
854 for (i
= 0; i
< keyCount
; i
++) {
855 if(mifare_classic_halt(pcs
, cuid
)) {
856 if (MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Halt error");
859 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
860 if (OLD_MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Can't select card");
864 ui64Key
= bytes_to_num(datain
+ i
* 6, 6);
865 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
873 // ----------------------------- crypto1 destroy
874 crypto1_destroy(pcs
);
877 cmd_send(CMD_ACK
,isOK
,0,0,datain
+ i
* 6,6);
880 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
883 // restore debug level
884 MF_DBGLEVEL
= OLD_MF_DBGLEVEL
;
887 //-----------------------------------------------------------------------------
888 // MIFARE commands set debug level
890 //-----------------------------------------------------------------------------
891 void MifareSetDbgLvl(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
893 Dbprintf("Debug level: %d", MF_DBGLEVEL
);
896 //-----------------------------------------------------------------------------
897 // Work with emulator memory
899 // Note: we call FpgaDownloadAndGo(FPGA_BITSTREAM_HF) here although FPGA is not
900 // involved in dealing with emulator memory. But if it is called later, it might
901 // destroy the Emulator Memory.
902 //-----------------------------------------------------------------------------
904 void MifareEMemClr(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
905 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
909 void MifareEMemSet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
910 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
911 //emlSetMem(datain, arg0, arg1); // data, block num, blocks count
912 emlSetMem_xt(datain
, arg0
, arg1
, arg2
); // data, block num, blocks count, block byte width
915 void MifareEMemGet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
916 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
917 byte_t buf
[USB_CMD_DATA_SIZE
];
918 emlGetMem(buf
, arg0
, arg1
); // data, block num, blocks count (max 4)
921 cmd_send(CMD_ACK
,arg0
,arg1
,0,buf
,USB_CMD_DATA_SIZE
);
925 //-----------------------------------------------------------------------------
926 // Load a card into the emulator memory
928 //-----------------------------------------------------------------------------
929 void MifareECardLoad(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
930 uint8_t numSectors
= arg0
;
931 uint8_t keyType
= arg1
;
932 uint64_t ui64Key
= 0;
934 struct Crypto1State mpcs
= {0, 0};
935 struct Crypto1State
*pcs
;
939 byte_t dataoutbuf
[16];
940 byte_t dataoutbuf2
[16];
946 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
953 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
955 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
958 for (uint8_t sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
959 ui64Key
= emlGetKey(sectorNo
, keyType
);
961 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
963 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo
);
967 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_NESTED
)) {
969 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo
);
974 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
975 if(isOK
&& mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
)) {
977 if (MF_DBGLEVEL
>= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo
, blockNo
);
981 if (blockNo
< NumBlocksPerSector(sectorNo
) - 1) {
982 emlSetMem(dataoutbuf
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
983 } else { // sector trailer, keep the keys, set only the AC
984 emlGetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
985 memcpy(&dataoutbuf2
[6], &dataoutbuf
[6], 4);
986 emlSetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
993 if(mifare_classic_halt(pcs
, cuid
)) {
994 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
997 // ----------------------------- crypto1 destroy
998 crypto1_destroy(pcs
);
1000 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1003 if (MF_DBGLEVEL
>= 2) DbpString("EMUL FILL SECTORS FINISHED");
1008 //-----------------------------------------------------------------------------
1009 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
1011 //-----------------------------------------------------------------------------
1012 void MifareCSetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1015 uint8_t needWipe
= arg0
;
1016 // bit 0 - need get UID
1017 // bit 1 - need wupC
1018 // bit 2 - need HALT after sequence
1019 // bit 3 - need init FPGA and field before sequence
1020 // bit 4 - need reset FPGA and LED
1021 uint8_t workFlags
= arg1
;
1022 uint8_t blockNo
= arg2
;
1025 uint8_t wupC1
[] = { 0x40 };
1026 uint8_t wupC2
[] = { 0x43 };
1027 uint8_t wipeC
[] = { 0x41 };
1031 uint8_t uid
[10] = {0x00};
1032 uint8_t d_block
[18] = {0x00};
1035 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1036 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1038 // reset FPGA and LED
1039 if (workFlags
& 0x08) {
1043 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1051 // get UID from chip
1052 if (workFlags
& 0x01) {
1053 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
1054 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1058 if(mifare_classic_halt(NULL
, cuid
)) {
1059 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1066 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1067 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1068 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1072 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
1073 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1074 if (MF_DBGLEVEL
>= 1) Dbprintf("wipeC error");
1078 if(mifare_classic_halt(NULL
, cuid
)) {
1079 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1085 if (workFlags
& 0x02) {
1086 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1087 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1088 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1092 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1093 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1094 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1099 if ((mifare_sendcmd_short(NULL
, 0, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1100 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send command error");
1104 memcpy(d_block
, datain
, 16);
1105 AppendCrc14443a(d_block
, 16);
1107 ReaderTransmit(d_block
, sizeof(d_block
), NULL
);
1108 if ((ReaderReceive(receivedAnswer
, receivedAnswerPar
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1109 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send data error");
1113 if (workFlags
& 0x04) {
1114 if (mifare_classic_halt(NULL
, cuid
)) {
1115 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1125 cmd_send(CMD_ACK
,isOK
,0,0,uid
,4);
1128 if ((workFlags
& 0x10) || (!isOK
)) {
1129 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1135 void MifareCGetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1138 // bit 1 - need wupC
1139 // bit 2 - need HALT after sequence
1140 // bit 3 - need init FPGA and field before sequence
1141 // bit 4 - need reset FPGA and LED
1142 // bit 5 - need to set datain instead of issuing USB reply (called via ARM for StandAloneMode14a)
1143 uint8_t workFlags
= arg0
;
1144 uint8_t blockNo
= arg2
;
1147 uint8_t wupC1
[] = { 0x40 };
1148 uint8_t wupC2
[] = { 0x43 };
1152 uint8_t data
[18] = {0x00};
1155 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1156 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1158 if (workFlags
& 0x08) {
1162 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1169 if (workFlags
& 0x02) {
1170 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1171 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1172 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1176 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1177 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1178 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1184 if ((mifare_sendcmd_short(NULL
, 0, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 18)) {
1185 if (MF_DBGLEVEL
>= 1) Dbprintf("read block send command error");
1188 memcpy(data
, receivedAnswer
, 18);
1190 if (workFlags
& 0x04) {
1191 if (mifare_classic_halt(NULL
, cuid
)) {
1192 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1202 if (workFlags
& 0x20) {
1204 memcpy(datain
, data
, 18);
1207 cmd_send(CMD_ACK
,isOK
,0,0,data
,18);
1210 if ((workFlags
& 0x10) || (!isOK
)) {
1211 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1216 void MifareCIdent(){
1219 uint8_t wupC1
[] = { 0x40 };
1220 uint8_t wupC2
[] = { 0x43 };
1225 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1226 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1228 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1229 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1233 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1234 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1238 if (mifare_classic_halt(NULL
, 0)) {
1242 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
1245 void MifareCollectNonces(uint32_t arg0
, uint32_t arg1
){
1249 uint32_t iterations
= arg0
;
1250 uint8_t uid
[10] = {0x00};
1252 uint8_t *response
= BigBuf_malloc(MAX_MIFARE_FRAME_SIZE
);
1253 uint8_t *responsePar
= BigBuf_malloc(MAX_MIFARE_PARITY_SIZE
);
1255 uint8_t mf_auth
[] = { 0x60,0x00,0xf5,0x7b };
1257 // get memory from BigBuf.
1258 uint8_t *nonces
= BigBuf_malloc(iterations
* 4);
1264 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1269 for (int i
= 0; i
< iterations
; i
++) {
1273 // Test if the action was cancelled
1274 if(BUTTON_PRESS()) break;
1276 // if(mifare_classic_halt(pcs, cuid)) {
1277 // if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
1280 if(!iso14443a_select_card(uid
, NULL
, NULL
)) {
1281 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1285 // Transmit MIFARE_CLASSIC_AUTH.
1286 ReaderTransmit(mf_auth
, sizeof(mf_auth
), NULL
);
1288 // Receive the (4 Byte) "random" nonce
1289 if (!ReaderReceive(response
, responsePar
)) {
1290 if (MF_DBGLEVEL
>= 1) Dbprintf("Couldn't receive tag nonce");
1294 nonces
[i
*4] = bytes_to_num(response
, 4);
1297 int packLen
= iterations
* 4;
1300 while (packLen
> 0) {
1301 packSize
= MIN(USB_CMD_DATA_SIZE
, packLen
);
1303 cmd_send(CMD_ACK
, 77, 0, packSize
, nonces
- packLen
, packSize
);
1306 packLen
-= packSize
;
1309 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1317 void Mifare_DES_Auth1(uint8_t arg0
, uint8_t *datain
){
1319 byte_t dataout
[11] = {0x00};
1320 uint8_t uid
[10] = {0x00};
1321 uint32_t cuid
= 0x00;
1323 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1326 int len
= iso14443a_select_card(uid
, NULL
, &cuid
);
1328 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
1333 if(mifare_desfire_des_auth1(cuid
, dataout
)){
1334 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication part1: Fail.");
1339 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 1 FINISHED");
1340 cmd_send(CMD_ACK
,1,cuid
,0,dataout
, sizeof(dataout
));
1343 void Mifare_DES_Auth2(uint32_t arg0
, uint8_t *datain
){
1345 uint32_t cuid
= arg0
;
1346 uint8_t key
[16] = {0x00};
1347 byte_t dataout
[12] = {0x00};
1350 memcpy(key
, datain
, 16);
1352 isOK
= mifare_desfire_des_auth2(cuid
, key
, dataout
);
1355 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) Dbprintf("Authentication part2: Failed");
1360 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 2 FINISHED");
1362 cmd_send(CMD_ACK
, isOK
, 0, 0, dataout
, sizeof(dataout
));
1363 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);