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 //-----------------------------------------------------------------------------
23 // Select, Authenticate, Read a MIFARE tag.
25 //-----------------------------------------------------------------------------
26 void MifareReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
29 uint8_t blockNo
= arg0
;
30 uint8_t keyType
= arg1
;
32 ui64Key
= bytes_to_num(datain
, 6);
36 byte_t dataoutbuf
[16];
39 struct Crypto1State mpcs
= {0, 0};
40 struct Crypto1State
*pcs
;
45 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
52 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
53 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
57 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
58 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
62 if(mifare_classic_readblock(pcs
, cuid
, blockNo
, dataoutbuf
)) {
63 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block error");
67 if(mifare_classic_halt(pcs
, cuid
)) {
68 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
76 // ----------------------------- crypto1 destroy
79 if (MF_DBGLEVEL
>= 2) DbpString("READ BLOCK FINISHED");
82 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16);
85 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
90 void MifareUC_Auth(uint8_t arg0
, uint8_t *keybytes
){
92 bool turnOffField
= (arg0
== 1);
94 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
96 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
130 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
132 int len
= iso14443a_select_card(NULL
, NULL
, NULL
);
134 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%02X)",len
);
139 // UL-C authentication
141 uint8_t key
[16] = {0x00};
142 memcpy(key
, datain
, sizeof(key
) );
144 if ( !mifare_ultra_auth(key
) ) {
150 // UL-EV1 / NTAG authentication
152 uint8_t pwd
[4] = {0x00};
153 memcpy(pwd
, datain
, 4);
154 uint8_t pack
[4] = {0,0,0,0};
155 if (!mifare_ul_ev1_auth(pwd
, pack
)) {
161 if( mifare_ultra_readblock(blockNo
, dataout
) ) {
162 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Read block error");
167 if( mifare_ultra_halt() ) {
168 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Halt error");
173 cmd_send(CMD_ACK
,1,0,0,dataout
,16);
174 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
178 //-----------------------------------------------------------------------------
179 // Select, Authenticate, Read a MIFARE tag.
180 // read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)
181 //-----------------------------------------------------------------------------
182 void MifareReadSector(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
185 uint8_t sectorNo
= arg0
;
186 uint8_t keyType
= arg1
;
187 uint64_t ui64Key
= 0;
188 ui64Key
= bytes_to_num(datain
, 6);
192 byte_t dataoutbuf
[16 * 16];
195 struct Crypto1State mpcs
= {0, 0};
196 struct Crypto1State
*pcs
;
202 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
)
253 uint8_t blockNo
= arg0
;
254 uint16_t blocks
= arg1
;
255 bool useKey
= (arg2
== 1); //UL_C
256 bool usePwd
= (arg2
== 2); //UL_EV1/NTAG
257 uint32_t countblocks
= 0;
258 uint8_t *dataout
= BigBuf_get_addr();
263 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
265 int len
= iso14443a_select_card(NULL
, NULL
, NULL
);
267 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%d)",len
);
272 // UL-C authentication
274 uint8_t key
[16] = {0x00};
275 memcpy(key
, datain
, sizeof(key
) );
277 if ( !mifare_ultra_auth(key
) ) {
283 // UL-EV1 / NTAG authentication
285 uint8_t pwd
[4] = {0x00};
286 memcpy(pwd
, datain
, sizeof(pwd
));
287 uint8_t pack
[4] = {0,0,0,0};
289 if (!mifare_ul_ev1_auth(pwd
, pack
)){
295 for (int i
= 0; i
< blocks
; i
++){
296 if ((i
*4) + 4 > BigBuf_get_traceLen()) {
297 Dbprintf("Data exceeds buffer!!");
301 len
= mifare_ultra_readblock(blockNo
+ i
, dataout
+ 4 * i
);
304 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Read block %d error",i
);
305 // if no blocks read - error out
310 //stop at last successful read block and return what we got
318 len
= mifare_ultra_halt();
320 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Halt error");
325 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) Dbprintf("Blocks read %d", countblocks
);
328 cmd_send(CMD_ACK
, 1, countblocks
, countblocks
, 0, 0);
329 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
333 //-----------------------------------------------------------------------------
334 // Select, Authenticate, Write a MIFARE tag.
336 //-----------------------------------------------------------------------------
337 void MifareWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
340 uint8_t blockNo
= arg0
;
341 uint8_t keyType
= arg1
;
342 uint64_t ui64Key
= 0;
343 byte_t blockdata
[16];
345 ui64Key
= bytes_to_num(datain
, 6);
346 memcpy(blockdata
, datain
+ 10, 16);
352 struct Crypto1State mpcs
= {0, 0};
353 struct Crypto1State
*pcs
;
359 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
366 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
367 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
371 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
372 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
376 if(mifare_classic_writeblock(pcs
, cuid
, blockNo
, blockdata
)) {
377 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
381 if(mifare_classic_halt(pcs
, cuid
)) {
382 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
390 // ----------------------------- crypto1 destroy
391 crypto1_destroy(pcs
);
393 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
396 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
401 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
405 void MifareUWriteBlock(uint8_t arg0
, uint8_t *datain
)
407 uint8_t blockNo
= arg0
;
408 byte_t blockdata
[16] = {0x00};
410 memcpy(blockdata
, datain
, 16);
412 uint8_t uid
[10] = {0x00};
414 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
417 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
419 if(!iso14443a_select_card(uid
, NULL
, NULL
)) {
420 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
425 if(mifare_ultra_writeblock(blockNo
, blockdata
)) {
426 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
430 if(mifare_ultra_halt()) {
431 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
436 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
438 cmd_send(CMD_ACK
,1,0,0,0,0);
439 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
443 void MifareUWriteBlock_Special(uint8_t arg0
, uint8_t *datain
)
445 uint8_t blockNo
= arg0
;
446 byte_t blockdata
[4] = {0x00};
448 memcpy(blockdata
, datain
,4);
453 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
455 if(!iso14443a_select_card(NULL
, NULL
, NULL
)) {
456 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
461 if(mifare_ultra_special_writeblock(blockNo
, blockdata
)) {
462 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
467 if(mifare_ultra_halt()) {
468 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
473 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
475 cmd_send(CMD_ACK
,1,0,0,0,0);
476 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
480 void MifareUSetPwd(uint8_t arg0
, uint8_t *datain
){
482 uint8_t pwd
[16] = {0x00};
483 byte_t blockdata
[4] = {0x00};
485 memcpy(pwd
, datain
, 16);
487 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
489 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
491 if(!iso14443a_select_card(NULL
, NULL
, NULL
)) {
492 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
497 blockdata
[0] = pwd
[7];
498 blockdata
[1] = pwd
[6];
499 blockdata
[2] = pwd
[5];
500 blockdata
[3] = pwd
[4];
501 if(mifare_ultra_special_writeblock( 44, blockdata
)) {
502 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
507 blockdata
[0] = pwd
[3];
508 blockdata
[1] = pwd
[2];
509 blockdata
[2] = pwd
[1];
510 blockdata
[3] = pwd
[0];
511 if(mifare_ultra_special_writeblock( 45, blockdata
)) {
512 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
517 blockdata
[0] = pwd
[15];
518 blockdata
[1] = pwd
[14];
519 blockdata
[2] = pwd
[13];
520 blockdata
[3] = pwd
[12];
521 if(mifare_ultra_special_writeblock( 46, blockdata
)) {
522 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
527 blockdata
[0] = pwd
[11];
528 blockdata
[1] = pwd
[10];
529 blockdata
[2] = pwd
[9];
530 blockdata
[3] = pwd
[8];
531 if(mifare_ultra_special_writeblock( 47, blockdata
)) {
532 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
537 if(mifare_ultra_halt()) {
538 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
543 cmd_send(CMD_ACK
,1,0,0,0,0);
544 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
548 // Return 1 if the nonce is invalid else return 0
549 int valid_nonce(uint32_t Nt
, uint32_t NtEnc
, uint32_t Ks1
, uint8_t *parity
) {
550 return ((oddparity((Nt
>> 24) & 0xFF) == ((parity
[0]) ^ oddparity((NtEnc
>> 24) & 0xFF) ^ BIT(Ks1
,16))) & \
551 (oddparity((Nt
>> 16) & 0xFF) == ((parity
[1]) ^ oddparity((NtEnc
>> 16) & 0xFF) ^ BIT(Ks1
,8))) & \
552 (oddparity((Nt
>> 8) & 0xFF) == ((parity
[2]) ^ oddparity((NtEnc
>> 8) & 0xFF) ^ BIT(Ks1
,0)))) ? 1 : 0;
556 //-----------------------------------------------------------------------------
557 // MIFARE nested authentication.
559 //-----------------------------------------------------------------------------
560 void MifareNested(uint32_t arg0
, uint32_t arg1
, uint32_t calibrate
, uint8_t *datain
)
563 uint8_t blockNo
= arg0
& 0xff;
564 uint8_t keyType
= (arg0
>> 8) & 0xff;
565 uint8_t targetBlockNo
= arg1
& 0xff;
566 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
567 uint64_t ui64Key
= 0;
569 ui64Key
= bytes_to_num(datain
, 6);
572 uint16_t rtr
, i
, j
, len
;
574 static uint16_t dmin
, dmax
;
576 uint32_t cuid
, nt1
, nt2
, nttmp
, nttest
, ks1
;
578 uint32_t target_nt
[2], target_ks
[2];
580 uint8_t par_array
[4];
582 struct Crypto1State mpcs
= {0, 0};
583 struct Crypto1State
*pcs
;
585 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
587 uint32_t auth1_time
, auth2_time
;
588 static uint16_t delta_time
;
590 // free eventually allocated BigBuf memory
596 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
602 // statistics on nonce distance
603 if (calibrate
) { // for first call only. Otherwise reuse previous calibration
611 for (rtr
= 0; rtr
< 17; rtr
++) {
613 // prepare next select. No need to power down the card.
614 if(mifare_classic_halt(pcs
, cuid
)) {
615 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
620 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
621 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
627 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
628 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
634 auth2_time
= auth1_time
+ delta_time
;
638 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_NESTED
, &nt2
, &auth2_time
)) {
639 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error");
644 nttmp
= prng_successor(nt1
, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160
645 for (i
= 101; i
< 1200; i
++) {
646 nttmp
= prng_successor(nttmp
, 1);
647 if (nttmp
== nt2
) break;
657 delta_time
= auth2_time
- auth1_time
+ 32; // allow some slack for proper timing
659 if (MF_DBGLEVEL
>= 3) Dbprintf("Nested: calibrating... ntdist=%d", i
);
663 if (rtr
<= 1) return;
665 davg
= (davg
+ (rtr
- 1)/2) / (rtr
- 1);
667 if (MF_DBGLEVEL
>= 3) Dbprintf("min=%d max=%d avg=%d, delta_time=%d", dmin
, dmax
, davg
, delta_time
);
675 // -------------------------------------------------------------------------------------------------
679 // get crypted nonces for target sector
680 for(i
=0; i
< 2; i
++) { // look for exactly two different nonces
683 while(target_nt
[i
] == 0) { // continue until we have an unambiguous nonce
685 // prepare next select. No need to power down the card.
686 if(mifare_classic_halt(pcs
, cuid
)) {
687 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
691 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
692 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
697 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
698 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
702 // nested authentication
703 auth2_time
= auth1_time
+ delta_time
;
704 len
= mifare_sendcmd_shortex(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType
& 0x01), targetBlockNo
, receivedAnswer
, par
, &auth2_time
);
706 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error len=%d", len
);
710 nt2
= bytes_to_num(receivedAnswer
, 4);
711 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i
+1, nt1
, nt2
, par
[0]);
713 // Parity validity check
714 for (j
= 0; j
< 4; j
++) {
715 par_array
[j
] = (oddparity(receivedAnswer
[j
]) != ((par
[0] >> (7-j
)) & 0x01));
719 nttest
= prng_successor(nt1
, dmin
- 1);
720 for (j
= dmin
; j
< dmax
+ 1; j
++) {
721 nttest
= prng_successor(nttest
, 1);
724 if (valid_nonce(nttest
, nt2
, ks1
, par_array
)){
725 if (ncount
> 0) { // we are only interested in disambiguous nonces, try again
726 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i
+1, j
);
730 target_nt
[i
] = nttest
;
733 if (i
== 1 && target_nt
[1] == target_nt
[0]) { // we need two different nonces
735 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j
);
738 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i
+1, j
);
741 if (target_nt
[i
] == 0 && j
== dmax
+1 && MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i
+1);
747 // ----------------------------- crypto1 destroy
748 crypto1_destroy(pcs
);
750 byte_t buf
[4 + 4 * 4];
751 memcpy(buf
, &cuid
, 4);
752 memcpy(buf
+4, &target_nt
[0], 4);
753 memcpy(buf
+8, &target_ks
[0], 4);
754 memcpy(buf
+12, &target_nt
[1], 4);
755 memcpy(buf
+16, &target_ks
[1], 4);
758 cmd_send(CMD_ACK
, 0, 2, targetBlockNo
+ (targetKeyType
* 0x100), buf
, sizeof(buf
));
761 if (MF_DBGLEVEL
>= 3) DbpString("NESTED FINISHED");
763 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
768 //-----------------------------------------------------------------------------
769 // MIFARE check keys. key count up to 85.
771 //-----------------------------------------------------------------------------
772 void MifareChkKeys(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
775 uint8_t blockNo
= arg0
;
776 uint8_t keyType
= arg1
;
777 uint8_t keyCount
= arg2
;
778 uint64_t ui64Key
= 0;
785 struct Crypto1State mpcs
= {0, 0};
786 struct Crypto1State
*pcs
;
790 int OLD_MF_DBGLEVEL
= MF_DBGLEVEL
;
791 MF_DBGLEVEL
= MF_DBG_NONE
;
797 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
803 for (i
= 0; i
< keyCount
; i
++) {
804 if(mifare_classic_halt(pcs
, cuid
)) {
805 if (MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Halt error");
808 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
809 if (OLD_MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Can't select card");
813 ui64Key
= bytes_to_num(datain
+ i
* 6, 6);
814 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
822 // ----------------------------- crypto1 destroy
823 crypto1_destroy(pcs
);
826 cmd_send(CMD_ACK
,isOK
,0,0,datain
+ i
* 6,6);
829 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
832 // restore debug level
833 MF_DBGLEVEL
= OLD_MF_DBGLEVEL
;
836 //-----------------------------------------------------------------------------
837 // MIFARE commands set debug level
839 //-----------------------------------------------------------------------------
840 void MifareSetDbgLvl(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
842 Dbprintf("Debug level: %d", MF_DBGLEVEL
);
845 //-----------------------------------------------------------------------------
846 // Work with emulator memory
848 //-----------------------------------------------------------------------------
849 void MifareEMemClr(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
853 void MifareEMemSet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
854 emlSetMem(datain
, arg0
, arg1
); // data, block num, blocks count
857 void MifareEMemGet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
858 byte_t buf
[USB_CMD_DATA_SIZE
];
859 emlGetMem(buf
, arg0
, arg1
); // data, block num, blocks count (max 4)
862 cmd_send(CMD_ACK
,arg0
,arg1
,0,buf
,USB_CMD_DATA_SIZE
);
866 //-----------------------------------------------------------------------------
867 // Load a card into the emulator memory
869 //-----------------------------------------------------------------------------
870 void MifareECardLoad(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
871 uint8_t numSectors
= arg0
;
872 uint8_t keyType
= arg1
;
873 uint64_t ui64Key
= 0;
875 struct Crypto1State mpcs
= {0, 0};
876 struct Crypto1State
*pcs
;
880 byte_t dataoutbuf
[16];
881 byte_t dataoutbuf2
[16];
888 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
896 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
898 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
901 for (uint8_t sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
902 ui64Key
= emlGetKey(sectorNo
, keyType
);
904 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
906 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo
);
910 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_NESTED
)) {
912 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo
);
917 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
918 if(isOK
&& mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
)) {
920 if (MF_DBGLEVEL
>= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo
, blockNo
);
924 if (blockNo
< NumBlocksPerSector(sectorNo
) - 1) {
925 emlSetMem(dataoutbuf
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
926 } else { // sector trailer, keep the keys, set only the AC
927 emlGetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
928 memcpy(&dataoutbuf2
[6], &dataoutbuf
[6], 4);
929 emlSetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
936 if(mifare_classic_halt(pcs
, cuid
)) {
937 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
940 // ----------------------------- crypto1 destroy
941 crypto1_destroy(pcs
);
943 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
946 if (MF_DBGLEVEL
>= 2) DbpString("EMUL FILL SECTORS FINISHED");
951 //-----------------------------------------------------------------------------
952 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
954 //-----------------------------------------------------------------------------
955 void MifareCSetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
958 uint8_t needWipe
= arg0
;
959 // bit 0 - need get UID
961 // bit 2 - need HALT after sequence
962 // bit 3 - need init FPGA and field before sequence
963 // bit 4 - need reset FPGA and LED
964 uint8_t workFlags
= arg1
;
965 uint8_t blockNo
= arg2
;
968 uint8_t wupC1
[] = { 0x40 };
969 uint8_t wupC2
[] = { 0x43 };
970 uint8_t wipeC
[] = { 0x41 };
974 uint8_t uid
[10] = {0x00};
975 uint8_t d_block
[18] = {0x00};
978 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
979 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
981 // reset FPGA and LED
982 if (workFlags
& 0x08) {
989 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
995 if (workFlags
& 0x01) {
996 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
997 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1001 if(mifare_classic_halt(NULL
, cuid
)) {
1002 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1009 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1010 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1011 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1015 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
1016 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1017 if (MF_DBGLEVEL
>= 1) Dbprintf("wipeC error");
1021 if(mifare_classic_halt(NULL
, cuid
)) {
1022 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1028 if (workFlags
& 0x02) {
1029 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1030 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1031 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1035 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1036 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1037 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1042 if ((mifare_sendcmd_short(NULL
, 0, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1043 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send command error");
1047 memcpy(d_block
, datain
, 16);
1048 AppendCrc14443a(d_block
, 16);
1050 ReaderTransmit(d_block
, sizeof(d_block
), NULL
);
1051 if ((ReaderReceive(receivedAnswer
, receivedAnswerPar
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1052 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send data error");
1056 if (workFlags
& 0x04) {
1057 if (mifare_classic_halt(NULL
, cuid
)) {
1058 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1068 cmd_send(CMD_ACK
,isOK
,0,0,uid
,4);
1071 if ((workFlags
& 0x10) || (!isOK
)) {
1072 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1078 void MifareCGetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1081 // bit 1 - need wupC
1082 // bit 2 - need HALT after sequence
1083 // bit 3 - need init FPGA and field before sequence
1084 // bit 4 - need reset FPGA and LED
1085 uint8_t workFlags
= arg0
;
1086 uint8_t blockNo
= arg2
;
1089 uint8_t wupC1
[] = { 0x40 };
1090 uint8_t wupC2
[] = { 0x43 };
1094 uint8_t data
[18] = {0x00};
1097 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1098 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1100 if (workFlags
& 0x08) {
1107 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1111 if (workFlags
& 0x02) {
1112 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1113 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1114 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1118 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1119 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1120 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1126 if ((mifare_sendcmd_short(NULL
, 0, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 18)) {
1127 if (MF_DBGLEVEL
>= 1) Dbprintf("read block send command error");
1130 memcpy(data
, receivedAnswer
, 18);
1132 if (workFlags
& 0x04) {
1133 if (mifare_classic_halt(NULL
, cuid
)) {
1134 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1144 cmd_send(CMD_ACK
,isOK
,0,0,data
,18);
1147 if ((workFlags
& 0x10) || (!isOK
)) {
1148 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1153 void MifareCIdent(){
1156 uint8_t wupC1
[] = { 0x40 };
1157 uint8_t wupC2
[] = { 0x43 };
1162 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1163 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1165 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1166 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1170 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1171 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1175 if (mifare_classic_halt(NULL
, 0)) {
1179 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
1182 void MifareCollectNonces(uint32_t arg0
, uint32_t arg1
){
1186 uint32_t iterations
= arg0
;
1187 uint8_t uid
[10] = {0x00};
1189 uint8_t *response
= BigBuf_malloc(MAX_MIFARE_FRAME_SIZE
);
1190 uint8_t *responsePar
= BigBuf_malloc(MAX_MIFARE_PARITY_SIZE
);
1192 uint8_t mf_auth
[] = { 0x60,0x00,0xf5,0x7b };
1194 // get memory from BigBuf.
1195 uint8_t *nonces
= BigBuf_malloc(iterations
* 4);
1203 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1205 for (int i
= 0; i
< iterations
; i
++) {
1209 // Test if the action was cancelled
1210 if(BUTTON_PRESS()) break;
1212 // if(mifare_classic_halt(pcs, cuid)) {
1213 // if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
1216 if(!iso14443a_select_card(uid
, NULL
, NULL
)) {
1217 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1221 // Transmit MIFARE_CLASSIC_AUTH.
1222 ReaderTransmit(mf_auth
, sizeof(mf_auth
), NULL
);
1224 // Receive the (4 Byte) "random" nonce
1225 if (!ReaderReceive(response
, responsePar
)) {
1226 if (MF_DBGLEVEL
>= 1) Dbprintf("Couldn't receive tag nonce");
1230 nonces
[i
*4] = bytes_to_num(response
, 4);
1233 int packLen
= iterations
* 4;
1236 while (packLen
> 0) {
1237 packSize
= MIN(USB_CMD_DATA_SIZE
, packLen
);
1239 cmd_send(CMD_ACK
, 77, 0, packSize
, nonces
- packLen
, packSize
);
1242 packLen
-= packSize
;
1245 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1253 void Mifare_DES_Auth1(uint8_t arg0
, uint8_t *datain
){
1255 byte_t dataout
[11] = {0x00};
1256 uint8_t uid
[10] = {0x00};
1257 uint32_t cuid
= 0x00;
1260 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1262 int len
= iso14443a_select_card(uid
, NULL
, &cuid
);
1264 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
1269 if(mifare_desfire_des_auth1(cuid
, dataout
)){
1270 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication part1: Fail.");
1275 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 1 FINISHED");
1276 cmd_send(CMD_ACK
,1,cuid
,0,dataout
, sizeof(dataout
));
1279 void Mifare_DES_Auth2(uint32_t arg0
, uint8_t *datain
){
1281 uint32_t cuid
= arg0
;
1282 uint8_t key
[16] = {0x00};
1283 byte_t dataout
[12] = {0x00};
1286 memcpy(key
, datain
, 16);
1288 isOK
= mifare_desfire_des_auth2(cuid
, key
, dataout
);
1291 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) Dbprintf("Authentication part2: Failed");
1296 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 2 FINISHED");
1298 cmd_send(CMD_ACK
, isOK
, 0, 0, dataout
, sizeof(dataout
));
1299 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);