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
)
252 // free eventually allocated BigBuf memory
258 uint8_t blockNo
= arg0
;
259 uint16_t blocks
= arg1
;
260 bool useKey
= (arg2
== 1); //UL_C
261 bool usePwd
= (arg2
== 2); //UL_EV1/NTAG
262 uint32_t countblocks
= 0;
263 uint8_t *dataout
= BigBuf_malloc(CARD_MEMORY_SIZE
);
264 if (dataout
== NULL
){
265 Dbprintf("out of memory");
272 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
274 int len
= iso14443a_select_card(NULL
, NULL
, NULL
);
276 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%d)",len
);
281 // UL-C authentication
283 uint8_t key
[16] = {0x00};
284 memcpy(key
, datain
, sizeof(key
) );
286 if ( !mifare_ultra_auth(key
) ) {
292 // UL-EV1 / NTAG authentication
294 uint8_t pwd
[4] = {0x00};
295 memcpy(pwd
, datain
, sizeof(pwd
));
296 uint8_t pack
[4] = {0,0,0,0};
298 if (!mifare_ul_ev1_auth(pwd
, pack
)){
304 for (int i
= 0; i
< blocks
; i
++){
305 if ((i
*4) + 4 > CARD_MEMORY_SIZE
) {
306 Dbprintf("Data exceeds buffer!!");
310 len
= mifare_ultra_readblock(blockNo
+ i
, dataout
+ 4 * i
);
313 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Read block %d error",i
);
314 // if no blocks read - error out
319 //stop at last successful read block and return what we got
327 len
= mifare_ultra_halt();
329 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Halt error");
334 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) Dbprintf("Blocks read %d", countblocks
);
337 cmd_send(CMD_ACK
, 1, countblocks
, countblocks
, 0, 0);
338 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
342 //-----------------------------------------------------------------------------
343 // Select, Authenticate, Write a MIFARE tag.
345 //-----------------------------------------------------------------------------
346 void MifareWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
349 uint8_t blockNo
= arg0
;
350 uint8_t keyType
= arg1
;
351 uint64_t ui64Key
= 0;
352 byte_t blockdata
[16];
354 ui64Key
= bytes_to_num(datain
, 6);
355 memcpy(blockdata
, datain
+ 10, 16);
361 struct Crypto1State mpcs
= {0, 0};
362 struct Crypto1State
*pcs
;
368 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 void MifareUWriteBlock(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(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
);
452 void MifareUWriteBlock_Special(uint8_t arg0
, uint8_t *datain
)
454 uint8_t blockNo
= arg0
;
455 byte_t blockdata
[4] = {0x00};
457 memcpy(blockdata
, datain
,4);
462 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
464 if(!iso14443a_select_card(NULL
, NULL
, NULL
)) {
465 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
470 if(mifare_ultra_special_writeblock(blockNo
, blockdata
)) {
471 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
476 if(mifare_ultra_halt()) {
477 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
482 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
484 cmd_send(CMD_ACK
,1,0,0,0,0);
485 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
489 void MifareUSetPwd(uint8_t arg0
, uint8_t *datain
){
491 uint8_t pwd
[16] = {0x00};
492 byte_t blockdata
[4] = {0x00};
494 memcpy(pwd
, datain
, 16);
496 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
498 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
500 if(!iso14443a_select_card(NULL
, NULL
, NULL
)) {
501 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
506 blockdata
[0] = pwd
[7];
507 blockdata
[1] = pwd
[6];
508 blockdata
[2] = pwd
[5];
509 blockdata
[3] = pwd
[4];
510 if(mifare_ultra_special_writeblock( 44, blockdata
)) {
511 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
516 blockdata
[0] = pwd
[3];
517 blockdata
[1] = pwd
[2];
518 blockdata
[2] = pwd
[1];
519 blockdata
[3] = pwd
[0];
520 if(mifare_ultra_special_writeblock( 45, blockdata
)) {
521 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
526 blockdata
[0] = pwd
[15];
527 blockdata
[1] = pwd
[14];
528 blockdata
[2] = pwd
[13];
529 blockdata
[3] = pwd
[12];
530 if(mifare_ultra_special_writeblock( 46, blockdata
)) {
531 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
536 blockdata
[0] = pwd
[11];
537 blockdata
[1] = pwd
[10];
538 blockdata
[2] = pwd
[9];
539 blockdata
[3] = pwd
[8];
540 if(mifare_ultra_special_writeblock( 47, blockdata
)) {
541 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
546 if(mifare_ultra_halt()) {
547 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
552 cmd_send(CMD_ACK
,1,0,0,0,0);
553 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
557 // Return 1 if the nonce is invalid else return 0
558 int valid_nonce(uint32_t Nt
, uint32_t NtEnc
, uint32_t Ks1
, uint8_t *parity
) {
559 return ((oddparity((Nt
>> 24) & 0xFF) == ((parity
[0]) ^ oddparity((NtEnc
>> 24) & 0xFF) ^ BIT(Ks1
,16))) & \
560 (oddparity((Nt
>> 16) & 0xFF) == ((parity
[1]) ^ oddparity((NtEnc
>> 16) & 0xFF) ^ BIT(Ks1
,8))) & \
561 (oddparity((Nt
>> 8) & 0xFF) == ((parity
[2]) ^ oddparity((NtEnc
>> 8) & 0xFF) ^ BIT(Ks1
,0)))) ? 1 : 0;
565 //-----------------------------------------------------------------------------
566 // MIFARE nested authentication.
568 //-----------------------------------------------------------------------------
569 void MifareNested(uint32_t arg0
, uint32_t arg1
, uint32_t calibrate
, uint8_t *datain
)
572 uint8_t blockNo
= arg0
& 0xff;
573 uint8_t keyType
= (arg0
>> 8) & 0xff;
574 uint8_t targetBlockNo
= arg1
& 0xff;
575 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
576 uint64_t ui64Key
= 0;
578 ui64Key
= bytes_to_num(datain
, 6);
581 uint16_t rtr
, i
, j
, len
;
583 static uint16_t dmin
, dmax
;
585 uint32_t cuid
, nt1
, nt2
, nttmp
, nttest
, ks1
;
587 uint32_t target_nt
[2], target_ks
[2];
589 uint8_t par_array
[4];
591 struct Crypto1State mpcs
= {0, 0};
592 struct Crypto1State
*pcs
;
594 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
596 uint32_t auth1_time
, auth2_time
;
597 static uint16_t delta_time
;
599 // free eventually allocated BigBuf memory
605 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
611 // statistics on nonce distance
612 if (calibrate
) { // for first call only. Otherwise reuse previous calibration
620 for (rtr
= 0; rtr
< 17; rtr
++) {
622 // prepare next select. No need to power down the card.
623 if(mifare_classic_halt(pcs
, cuid
)) {
624 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
629 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
630 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
636 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
637 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
643 auth2_time
= auth1_time
+ delta_time
;
647 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_NESTED
, &nt2
, &auth2_time
)) {
648 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error");
653 nttmp
= prng_successor(nt1
, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160
654 for (i
= 101; i
< 1200; i
++) {
655 nttmp
= prng_successor(nttmp
, 1);
656 if (nttmp
== nt2
) break;
666 delta_time
= auth2_time
- auth1_time
+ 32; // allow some slack for proper timing
668 if (MF_DBGLEVEL
>= 3) Dbprintf("Nested: calibrating... ntdist=%d", i
);
672 if (rtr
<= 1) return;
674 davg
= (davg
+ (rtr
- 1)/2) / (rtr
- 1);
676 if (MF_DBGLEVEL
>= 3) Dbprintf("min=%d max=%d avg=%d, delta_time=%d", dmin
, dmax
, davg
, delta_time
);
684 // -------------------------------------------------------------------------------------------------
688 // get crypted nonces for target sector
689 for(i
=0; i
< 2; i
++) { // look for exactly two different nonces
692 while(target_nt
[i
] == 0) { // continue until we have an unambiguous nonce
694 // prepare next select. No need to power down the card.
695 if(mifare_classic_halt(pcs
, cuid
)) {
696 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
700 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
701 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
706 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
707 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
711 // nested authentication
712 auth2_time
= auth1_time
+ delta_time
;
713 len
= mifare_sendcmd_shortex(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType
& 0x01), targetBlockNo
, receivedAnswer
, par
, &auth2_time
);
715 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error len=%d", len
);
719 nt2
= bytes_to_num(receivedAnswer
, 4);
720 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i
+1, nt1
, nt2
, par
[0]);
722 // Parity validity check
723 for (j
= 0; j
< 4; j
++) {
724 par_array
[j
] = (oddparity(receivedAnswer
[j
]) != ((par
[0] >> (7-j
)) & 0x01));
728 nttest
= prng_successor(nt1
, dmin
- 1);
729 for (j
= dmin
; j
< dmax
+ 1; j
++) {
730 nttest
= prng_successor(nttest
, 1);
733 if (valid_nonce(nttest
, nt2
, ks1
, par_array
)){
734 if (ncount
> 0) { // we are only interested in disambiguous nonces, try again
735 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i
+1, j
);
739 target_nt
[i
] = nttest
;
742 if (i
== 1 && target_nt
[1] == target_nt
[0]) { // we need two different nonces
744 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j
);
747 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i
+1, j
);
750 if (target_nt
[i
] == 0 && j
== dmax
+1 && MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i
+1);
756 // ----------------------------- crypto1 destroy
757 crypto1_destroy(pcs
);
759 byte_t buf
[4 + 4 * 4];
760 memcpy(buf
, &cuid
, 4);
761 memcpy(buf
+4, &target_nt
[0], 4);
762 memcpy(buf
+8, &target_ks
[0], 4);
763 memcpy(buf
+12, &target_nt
[1], 4);
764 memcpy(buf
+16, &target_ks
[1], 4);
767 cmd_send(CMD_ACK
, 0, 2, targetBlockNo
+ (targetKeyType
* 0x100), buf
, sizeof(buf
));
770 if (MF_DBGLEVEL
>= 3) DbpString("NESTED FINISHED");
772 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
777 //-----------------------------------------------------------------------------
778 // MIFARE check keys. key count up to 85.
780 //-----------------------------------------------------------------------------
781 void MifareChkKeys(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
784 uint8_t blockNo
= arg0
;
785 uint8_t keyType
= arg1
;
786 uint8_t keyCount
= arg2
;
787 uint64_t ui64Key
= 0;
794 struct Crypto1State mpcs
= {0, 0};
795 struct Crypto1State
*pcs
;
799 int OLD_MF_DBGLEVEL
= MF_DBGLEVEL
;
800 MF_DBGLEVEL
= MF_DBG_NONE
;
806 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
812 for (i
= 0; i
< keyCount
; i
++) {
813 if(mifare_classic_halt(pcs
, cuid
)) {
814 if (MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Halt error");
817 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
818 if (OLD_MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Can't select card");
822 ui64Key
= bytes_to_num(datain
+ i
* 6, 6);
823 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
831 // ----------------------------- crypto1 destroy
832 crypto1_destroy(pcs
);
835 cmd_send(CMD_ACK
,isOK
,0,0,datain
+ i
* 6,6);
838 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
841 // restore debug level
842 MF_DBGLEVEL
= OLD_MF_DBGLEVEL
;
845 //-----------------------------------------------------------------------------
846 // MIFARE commands set debug level
848 //-----------------------------------------------------------------------------
849 void MifareSetDbgLvl(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
851 Dbprintf("Debug level: %d", MF_DBGLEVEL
);
854 //-----------------------------------------------------------------------------
855 // Work with emulator memory
857 //-----------------------------------------------------------------------------
858 void MifareEMemClr(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
862 void MifareEMemSet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
863 emlSetMem(datain
, arg0
, arg1
); // data, block num, blocks count
866 void MifareEMemGet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
867 byte_t buf
[USB_CMD_DATA_SIZE
];
868 emlGetMem(buf
, arg0
, arg1
); // data, block num, blocks count (max 4)
871 cmd_send(CMD_ACK
,arg0
,arg1
,0,buf
,USB_CMD_DATA_SIZE
);
875 //-----------------------------------------------------------------------------
876 // Load a card into the emulator memory
878 //-----------------------------------------------------------------------------
879 void MifareECardLoad(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
880 uint8_t numSectors
= arg0
;
881 uint8_t keyType
= arg1
;
882 uint64_t ui64Key
= 0;
884 struct Crypto1State mpcs
= {0, 0};
885 struct Crypto1State
*pcs
;
889 byte_t dataoutbuf
[16];
890 byte_t dataoutbuf2
[16];
897 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
905 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
907 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
910 for (uint8_t sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
911 ui64Key
= emlGetKey(sectorNo
, keyType
);
913 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
915 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo
);
919 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_NESTED
)) {
921 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo
);
926 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
927 if(isOK
&& mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
)) {
929 if (MF_DBGLEVEL
>= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo
, blockNo
);
933 if (blockNo
< NumBlocksPerSector(sectorNo
) - 1) {
934 emlSetMem(dataoutbuf
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
935 } else { // sector trailer, keep the keys, set only the AC
936 emlGetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
937 memcpy(&dataoutbuf2
[6], &dataoutbuf
[6], 4);
938 emlSetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
945 if(mifare_classic_halt(pcs
, cuid
)) {
946 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
949 // ----------------------------- crypto1 destroy
950 crypto1_destroy(pcs
);
952 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
955 if (MF_DBGLEVEL
>= 2) DbpString("EMUL FILL SECTORS FINISHED");
960 //-----------------------------------------------------------------------------
961 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
963 //-----------------------------------------------------------------------------
964 void MifareCSetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
967 uint8_t needWipe
= arg0
;
968 // bit 0 - need get UID
970 // bit 2 - need HALT after sequence
971 // bit 3 - need init FPGA and field before sequence
972 // bit 4 - need reset FPGA and LED
973 uint8_t workFlags
= arg1
;
974 uint8_t blockNo
= arg2
;
977 uint8_t wupC1
[] = { 0x40 };
978 uint8_t wupC2
[] = { 0x43 };
979 uint8_t wipeC
[] = { 0x41 };
983 uint8_t uid
[10] = {0x00};
984 uint8_t d_block
[18] = {0x00};
987 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
988 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
990 // reset FPGA and LED
991 if (workFlags
& 0x08) {
998 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1003 // get UID from chip
1004 if (workFlags
& 0x01) {
1005 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
1006 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1010 if(mifare_classic_halt(NULL
, cuid
)) {
1011 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1018 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1019 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1020 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1024 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
1025 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1026 if (MF_DBGLEVEL
>= 1) Dbprintf("wipeC error");
1030 if(mifare_classic_halt(NULL
, cuid
)) {
1031 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1037 if (workFlags
& 0x02) {
1038 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1039 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1040 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1044 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1045 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1046 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1051 if ((mifare_sendcmd_short(NULL
, 0, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1052 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send command error");
1056 memcpy(d_block
, datain
, 16);
1057 AppendCrc14443a(d_block
, 16);
1059 ReaderTransmit(d_block
, sizeof(d_block
), NULL
);
1060 if ((ReaderReceive(receivedAnswer
, receivedAnswerPar
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1061 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send data error");
1065 if (workFlags
& 0x04) {
1066 if (mifare_classic_halt(NULL
, cuid
)) {
1067 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1077 cmd_send(CMD_ACK
,isOK
,0,0,uid
,4);
1080 if ((workFlags
& 0x10) || (!isOK
)) {
1081 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1087 void MifareCGetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1090 // bit 1 - need wupC
1091 // bit 2 - need HALT after sequence
1092 // bit 3 - need init FPGA and field before sequence
1093 // bit 4 - need reset FPGA and LED
1094 uint8_t workFlags
= arg0
;
1095 uint8_t blockNo
= arg2
;
1098 uint8_t wupC1
[] = { 0x40 };
1099 uint8_t wupC2
[] = { 0x43 };
1103 uint8_t data
[18] = {0x00};
1106 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1107 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1109 if (workFlags
& 0x08) {
1116 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1120 if (workFlags
& 0x02) {
1121 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1122 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1123 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1127 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1128 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1129 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1135 if ((mifare_sendcmd_short(NULL
, 0, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 18)) {
1136 if (MF_DBGLEVEL
>= 1) Dbprintf("read block send command error");
1139 memcpy(data
, receivedAnswer
, 18);
1141 if (workFlags
& 0x04) {
1142 if (mifare_classic_halt(NULL
, cuid
)) {
1143 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1153 cmd_send(CMD_ACK
,isOK
,0,0,data
,18);
1156 if ((workFlags
& 0x10) || (!isOK
)) {
1157 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1162 void MifareCIdent(){
1165 uint8_t wupC1
[] = { 0x40 };
1166 uint8_t wupC2
[] = { 0x43 };
1171 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
1172 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
1174 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1175 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1179 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1180 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1184 if (mifare_classic_halt(NULL
, 0)) {
1188 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
1191 void MifareCollectNonces(uint32_t arg0
, uint32_t arg1
){
1195 uint32_t iterations
= arg0
;
1196 uint8_t uid
[10] = {0x00};
1198 uint8_t *response
= BigBuf_malloc(MAX_MIFARE_FRAME_SIZE
);
1199 uint8_t *responsePar
= BigBuf_malloc(MAX_MIFARE_PARITY_SIZE
);
1201 uint8_t mf_auth
[] = { 0x60,0x00,0xf5,0x7b };
1203 // get memory from BigBuf.
1204 uint8_t *nonces
= BigBuf_malloc(iterations
* 4);
1212 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1214 for (int i
= 0; i
< iterations
; i
++) {
1218 // Test if the action was cancelled
1219 if(BUTTON_PRESS()) break;
1221 // if(mifare_classic_halt(pcs, cuid)) {
1222 // if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
1225 if(!iso14443a_select_card(uid
, NULL
, NULL
)) {
1226 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1230 // Transmit MIFARE_CLASSIC_AUTH.
1231 ReaderTransmit(mf_auth
, sizeof(mf_auth
), NULL
);
1233 // Receive the (4 Byte) "random" nonce
1234 if (!ReaderReceive(response
, responsePar
)) {
1235 if (MF_DBGLEVEL
>= 1) Dbprintf("Couldn't receive tag nonce");
1239 nonces
[i
*4] = bytes_to_num(response
, 4);
1242 int packLen
= iterations
* 4;
1245 while (packLen
> 0) {
1246 packSize
= MIN(USB_CMD_DATA_SIZE
, packLen
);
1248 cmd_send(CMD_ACK
, 77, 0, packSize
, nonces
- packLen
, packSize
);
1251 packLen
-= packSize
;
1254 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1262 void Mifare_DES_Auth1(uint8_t arg0
, uint8_t *datain
){
1264 byte_t dataout
[11] = {0x00};
1265 uint8_t uid
[10] = {0x00};
1266 uint32_t cuid
= 0x00;
1269 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1271 int len
= iso14443a_select_card(uid
, NULL
, &cuid
);
1273 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
1278 if(mifare_desfire_des_auth1(cuid
, dataout
)){
1279 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication part1: Fail.");
1284 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 1 FINISHED");
1285 cmd_send(CMD_ACK
,1,cuid
,0,dataout
, sizeof(dataout
));
1288 void Mifare_DES_Auth2(uint32_t arg0
, uint8_t *datain
){
1290 uint32_t cuid
= arg0
;
1291 uint8_t key
[16] = {0x00};
1292 byte_t dataout
[12] = {0x00};
1295 memcpy(key
, datain
, 16);
1297 isOK
= mifare_desfire_des_auth2(cuid
, key
, dataout
);
1300 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) Dbprintf("Authentication part2: Failed");
1305 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) DbpString("AUTH 2 FINISHED");
1307 cmd_send(CMD_ACK
, isOK
, 0, 0, dataout
, sizeof(dataout
));
1308 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);