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"
20 //-----------------------------------------------------------------------------
21 // Select, Authenticate, Read a MIFARE tag.
23 //-----------------------------------------------------------------------------
24 void MifareReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
27 uint8_t blockNo
= arg0
;
28 uint8_t keyType
= arg1
;
30 ui64Key
= bytes_to_num(datain
, 6);
34 byte_t dataoutbuf
[16];
37 struct Crypto1State mpcs
= {0, 0};
38 struct Crypto1State
*pcs
;
43 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
50 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
51 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
55 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
56 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
60 if(mifare_classic_readblock(pcs
, cuid
, blockNo
, dataoutbuf
)) {
61 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block error");
65 if(mifare_classic_halt(pcs
, cuid
)) {
66 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
74 // ----------------------------- crypto1 destroy
77 if (MF_DBGLEVEL
>= 2) DbpString("READ BLOCK FINISHED");
80 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16);
84 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
88 void MifareUReadBlock(uint8_t arg0
,uint8_t *datain
)
90 uint8_t blockNo
= arg0
;
91 byte_t dataout
[16] = {0x00};
92 uint8_t uid
[10] = {0x00};
100 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
102 int len
= iso14443a_select_card(uid
, NULL
, &cuid
);
104 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Can't select card");
109 len
= mifare_ultra_readblock(cuid
, blockNo
, dataout
);
111 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Read block error");
116 len
= mifare_ultra_halt(cuid
);
118 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Halt error");
123 cmd_send(CMD_ACK
,1,0,0,dataout
,16);
124 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
128 //-----------------------------------------------------------------------------
129 // Select, Authenticate, Read a MIFARE tag.
130 // read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)
131 //-----------------------------------------------------------------------------
132 void MifareReadSector(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
135 uint8_t sectorNo
= arg0
;
136 uint8_t keyType
= arg1
;
137 uint64_t ui64Key
= 0;
138 ui64Key
= bytes_to_num(datain
, 6);
142 byte_t dataoutbuf
[16 * 16];
145 struct Crypto1State mpcs
= {0, 0};
146 struct Crypto1State
*pcs
;
150 iso14a_clear_trace();
152 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
159 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
161 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
165 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
167 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
170 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
171 if(mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
+ 16 * blockNo
)) {
173 if (MF_DBGLEVEL
>= 1) Dbprintf("Read sector %2d block %2d error", sectorNo
, blockNo
);
178 if(mifare_classic_halt(pcs
, cuid
)) {
179 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
182 // ----------------------------- crypto1 destroy
183 crypto1_destroy(pcs
);
185 if (MF_DBGLEVEL
>= 2) DbpString("READ SECTOR FINISHED");
188 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16*NumBlocksPerSector(sectorNo
));
192 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
196 void MifareUReadCard(uint8_t arg0
, int arg1
, uint8_t *datain
)
199 uint8_t sectorNo
= arg0
;
202 byte_t dataout
[176] = {0x00};;
203 uint8_t uid
[10] = {0x00};
210 if (MF_DBGLEVEL
>= MF_DBG_ALL
)
211 Dbprintf("Pages %d",Pages
);
213 iso14a_clear_trace();
214 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
216 int len
= iso14443a_select_card(uid
, NULL
, &cuid
);
219 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
220 Dbprintf("Can't select card");
225 for (int i
= 0; i
< Pages
; i
++){
227 len
= mifare_ultra_readblock(cuid
, sectorNo
* 4 + i
, dataout
+ 4 * i
);
230 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
231 Dbprintf("Read block %d error",i
);
239 len
= mifare_ultra_halt(cuid
);
241 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
242 Dbprintf("Halt error");
247 if (MF_DBGLEVEL
>= MF_DBG_ALL
) {
248 Dbprintf("Pages read %d", count_Pages
);
251 len
= 16*4; //64 bytes
254 if (Pages
== 44 && count_Pages
> 16)
257 cmd_send(CMD_ACK
, 1, 0, 0, dataout
, len
);
258 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
263 //-----------------------------------------------------------------------------
264 // Select, Authenticate, Write a MIFARE tag.
266 //-----------------------------------------------------------------------------
267 void MifareWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
270 uint8_t blockNo
= arg0
;
271 uint8_t keyType
= arg1
;
272 uint64_t ui64Key
= 0;
273 byte_t blockdata
[16];
275 ui64Key
= bytes_to_num(datain
, 6);
276 memcpy(blockdata
, datain
+ 10, 16);
282 struct Crypto1State mpcs
= {0, 0};
283 struct Crypto1State
*pcs
;
287 iso14a_clear_trace();
289 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
296 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
297 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
301 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
302 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
306 if(mifare_classic_writeblock(pcs
, cuid
, blockNo
, blockdata
)) {
307 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
311 if(mifare_classic_halt(pcs
, cuid
)) {
312 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
320 // ----------------------------- crypto1 destroy
321 crypto1_destroy(pcs
);
323 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
326 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
331 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
335 void MifareUWriteBlock(uint8_t arg0
, uint8_t *datain
)
338 uint8_t blockNo
= arg0
;
339 byte_t blockdata
[16] = {0x00};
341 memcpy(blockdata
, datain
,16);
345 uint8_t uid
[10] = {0x00};
348 iso14a_clear_trace();
349 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
356 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
357 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
361 if(mifare_ultra_writeblock(cuid
, blockNo
, blockdata
)) {
362 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
366 if(mifare_ultra_halt(cuid
)) {
367 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
375 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
377 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
378 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
382 void MifareUWriteBlock_Special(uint8_t arg0
, uint8_t *datain
)
385 uint8_t blockNo
= arg0
;
386 byte_t blockdata
[4] = {0x00};
388 memcpy(blockdata
, datain
,4);
392 uint8_t uid
[10] = {0x00};
395 iso14a_clear_trace();
396 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
403 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
404 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
408 if(mifare_ultra_special_writeblock(cuid
, blockNo
, blockdata
)) {
409 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
413 if(mifare_ultra_halt(cuid
)) {
414 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
422 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
424 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
425 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
429 // Return 1 if the nonce is invalid else return 0
430 int valid_nonce(uint32_t Nt
, uint32_t NtEnc
, uint32_t Ks1
, uint8_t *parity
) {
431 return ((oddparity((Nt
>> 24) & 0xFF) == ((parity
[0]) ^ oddparity((NtEnc
>> 24) & 0xFF) ^ BIT(Ks1
,16))) & \
432 (oddparity((Nt
>> 16) & 0xFF) == ((parity
[1]) ^ oddparity((NtEnc
>> 16) & 0xFF) ^ BIT(Ks1
,8))) & \
433 (oddparity((Nt
>> 8) & 0xFF) == ((parity
[2]) ^ oddparity((NtEnc
>> 8) & 0xFF) ^ BIT(Ks1
,0)))) ? 1 : 0;
437 //-----------------------------------------------------------------------------
438 // MIFARE nested authentication.
440 //-----------------------------------------------------------------------------
441 void MifareNested(uint32_t arg0
, uint32_t arg1
, uint32_t calibrate
, uint8_t *datain
)
444 uint8_t blockNo
= arg0
& 0xff;
445 uint8_t keyType
= (arg0
>> 8) & 0xff;
446 uint8_t targetBlockNo
= arg1
& 0xff;
447 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
448 uint64_t ui64Key
= 0;
450 ui64Key
= bytes_to_num(datain
, 6);
453 uint16_t rtr
, i
, j
, len
;
455 static uint16_t dmin
, dmax
;
457 uint32_t cuid
, nt1
, nt2
, nttmp
, nttest
, ks1
;
459 uint32_t target_nt
[2], target_ks
[2];
461 uint8_t par_array
[4];
463 struct Crypto1State mpcs
= {0, 0};
464 struct Crypto1State
*pcs
;
466 uint8_t* receivedAnswer
= get_bigbufptr_recvrespbuf();
468 uint32_t auth1_time
, auth2_time
;
469 static uint16_t delta_time
;
472 iso14a_clear_trace();
473 iso14a_set_tracing(false);
475 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
481 // statistics on nonce distance
482 if (calibrate
) { // for first call only. Otherwise reuse previous calibration
490 for (rtr
= 0; rtr
< 17; rtr
++) {
492 // prepare next select. No need to power down the card.
493 if(mifare_classic_halt(pcs
, cuid
)) {
494 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
499 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
500 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
506 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
507 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
513 auth2_time
= auth1_time
+ delta_time
;
517 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_NESTED
, &nt2
, &auth2_time
)) {
518 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error");
523 nttmp
= prng_successor(nt1
, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160
524 for (i
= 101; i
< 1200; i
++) {
525 nttmp
= prng_successor(nttmp
, 1);
526 if (nttmp
== nt2
) break;
536 delta_time
= auth2_time
- auth1_time
+ 32; // allow some slack for proper timing
538 if (MF_DBGLEVEL
>= 3) Dbprintf("Nested: calibrating... ntdist=%d", i
);
542 if (rtr
<= 1) return;
544 davg
= (davg
+ (rtr
- 1)/2) / (rtr
- 1);
546 if (MF_DBGLEVEL
>= 3) Dbprintf("min=%d max=%d avg=%d, delta_time=%d", dmin
, dmax
, davg
, delta_time
);
554 // -------------------------------------------------------------------------------------------------
558 // get crypted nonces for target sector
559 for(i
=0; i
< 2; i
++) { // look for exactly two different nonces
562 while(target_nt
[i
] == 0) { // continue until we have an unambiguous nonce
564 // prepare next select. No need to power down the card.
565 if(mifare_classic_halt(pcs
, cuid
)) {
566 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
570 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
571 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
576 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
577 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
581 // nested authentication
582 auth2_time
= auth1_time
+ delta_time
;
583 len
= mifare_sendcmd_shortex(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType
& 0x01), targetBlockNo
, receivedAnswer
, par
, &auth2_time
);
585 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error len=%d", len
);
589 nt2
= bytes_to_num(receivedAnswer
, 4);
590 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i
+1, nt1
, nt2
, par
[0]);
592 // Parity validity check
593 for (j
= 0; j
< 4; j
++) {
594 par_array
[j
] = (oddparity(receivedAnswer
[j
]) != ((par
[0] >> (7-j
)) & 0x01));
598 nttest
= prng_successor(nt1
, dmin
- 1);
599 for (j
= dmin
; j
< dmax
+ 1; j
++) {
600 nttest
= prng_successor(nttest
, 1);
603 if (valid_nonce(nttest
, nt2
, ks1
, par_array
)){
604 if (ncount
> 0) { // we are only interested in disambiguous nonces, try again
605 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i
+1, j
);
609 target_nt
[i
] = nttest
;
612 if (i
== 1 && target_nt
[1] == target_nt
[0]) { // we need two different nonces
614 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j
);
617 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i
+1, j
);
620 if (target_nt
[i
] == 0 && j
== dmax
+1 && MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i
+1);
626 // ----------------------------- crypto1 destroy
627 crypto1_destroy(pcs
);
629 byte_t buf
[4 + 4 * 4];
630 memcpy(buf
, &cuid
, 4);
631 memcpy(buf
+4, &target_nt
[0], 4);
632 memcpy(buf
+8, &target_ks
[0], 4);
633 memcpy(buf
+12, &target_nt
[1], 4);
634 memcpy(buf
+16, &target_ks
[1], 4);
637 cmd_send(CMD_ACK
, 0, 2, targetBlockNo
+ (targetKeyType
* 0x100), buf
, sizeof(buf
));
640 if (MF_DBGLEVEL
>= 3) DbpString("NESTED FINISHED");
642 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
644 iso14a_set_tracing(TRUE
);
647 //-----------------------------------------------------------------------------
648 // MIFARE check keys. key count up to 85.
650 //-----------------------------------------------------------------------------
651 void MifareChkKeys(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
654 uint8_t blockNo
= arg0
;
655 uint8_t keyType
= arg1
;
656 uint8_t keyCount
= arg2
;
657 uint64_t ui64Key
= 0;
664 struct Crypto1State mpcs
= {0, 0};
665 struct Crypto1State
*pcs
;
669 int OLD_MF_DBGLEVEL
= MF_DBGLEVEL
;
670 MF_DBGLEVEL
= MF_DBG_NONE
;
673 iso14a_clear_trace();
674 iso14a_set_tracing(TRUE
);
676 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
682 for (i
= 0; i
< keyCount
; i
++) {
683 if(mifare_classic_halt(pcs
, cuid
)) {
684 if (MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Halt error");
687 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
688 if (OLD_MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Can't select card");
692 ui64Key
= bytes_to_num(datain
+ i
* 6, 6);
693 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
701 // ----------------------------- crypto1 destroy
702 crypto1_destroy(pcs
);
705 cmd_send(CMD_ACK
,isOK
,0,0,datain
+ i
* 6,6);
708 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
711 // restore debug level
712 MF_DBGLEVEL
= OLD_MF_DBGLEVEL
;
715 //-----------------------------------------------------------------------------
716 // MIFARE commands set debug level
718 //-----------------------------------------------------------------------------
719 void MifareSetDbgLvl(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
721 Dbprintf("Debug level: %d", MF_DBGLEVEL
);
724 //-----------------------------------------------------------------------------
725 // Work with emulator memory
727 //-----------------------------------------------------------------------------
728 void MifareEMemClr(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
732 void MifareEMemSet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
733 emlSetMem(datain
, arg0
, arg1
); // data, block num, blocks count
736 void MifareEMemGet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
737 byte_t buf
[USB_CMD_DATA_SIZE
];
738 emlGetMem(buf
, arg0
, arg1
); // data, block num, blocks count (max 4)
741 cmd_send(CMD_ACK
,arg0
,arg1
,0,buf
,USB_CMD_DATA_SIZE
);
745 //-----------------------------------------------------------------------------
746 // Load a card into the emulator memory
748 //-----------------------------------------------------------------------------
749 void MifareECardLoad(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
750 uint8_t numSectors
= arg0
;
751 uint8_t keyType
= arg1
;
752 uint64_t ui64Key
= 0;
754 struct Crypto1State mpcs
= {0, 0};
755 struct Crypto1State
*pcs
;
759 byte_t dataoutbuf
[16];
760 byte_t dataoutbuf2
[16];
764 iso14a_clear_trace();
765 iso14a_set_tracing(false);
767 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
775 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
777 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
780 for (uint8_t sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
781 ui64Key
= emlGetKey(sectorNo
, keyType
);
783 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
785 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo
);
789 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_NESTED
)) {
791 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo
);
796 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
797 if(isOK
&& mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
)) {
799 if (MF_DBGLEVEL
>= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo
, blockNo
);
803 if (blockNo
< NumBlocksPerSector(sectorNo
) - 1) {
804 emlSetMem(dataoutbuf
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
805 } else { // sector trailer, keep the keys, set only the AC
806 emlGetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
807 memcpy(&dataoutbuf2
[6], &dataoutbuf
[6], 4);
808 emlSetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
815 if(mifare_classic_halt(pcs
, cuid
)) {
816 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
819 // ----------------------------- crypto1 destroy
820 crypto1_destroy(pcs
);
822 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
825 if (MF_DBGLEVEL
>= 2) DbpString("EMUL FILL SECTORS FINISHED");
830 //-----------------------------------------------------------------------------
831 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
833 //-----------------------------------------------------------------------------
834 void MifareCSetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
837 uint8_t needWipe
= arg0
;
838 // bit 0 - need get UID
840 // bit 2 - need HALT after sequence
841 // bit 3 - need init FPGA and field before sequence
842 // bit 4 - need reset FPGA and LED
843 uint8_t workFlags
= arg1
;
844 uint8_t blockNo
= arg2
;
847 uint8_t wupC1
[] = { 0x40 };
848 uint8_t wupC2
[] = { 0x43 };
849 uint8_t wipeC
[] = { 0x41 };
853 uint8_t uid
[10] = {0x00};
854 uint8_t d_block
[18] = {0x00};
857 uint8_t *receivedAnswer
= get_bigbufptr_recvrespbuf();
858 uint8_t *receivedAnswerPar
= receivedAnswer
+ MAX_FRAME_SIZE
;
860 // reset FPGA and LED
861 if (workFlags
& 0x08) {
866 iso14a_clear_trace();
867 iso14a_set_tracing(TRUE
);
868 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
874 if (workFlags
& 0x01) {
875 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
876 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
880 if(mifare_classic_halt(NULL
, cuid
)) {
881 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
888 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
889 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
890 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
894 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
895 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
896 if (MF_DBGLEVEL
>= 1) Dbprintf("wipeC error");
900 if(mifare_classic_halt(NULL
, cuid
)) {
901 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
907 if (workFlags
& 0x02) {
908 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
909 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
910 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
914 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
915 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
916 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
921 if ((mifare_sendcmd_short(NULL
, 0, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 1) || (receivedAnswer
[0] != 0x0a)) {
922 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send command error");
926 memcpy(d_block
, datain
, 16);
927 AppendCrc14443a(d_block
, 16);
929 ReaderTransmit(d_block
, sizeof(d_block
), NULL
);
930 if ((ReaderReceive(receivedAnswer
, receivedAnswerPar
) != 1) || (receivedAnswer
[0] != 0x0a)) {
931 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send data error");
935 if (workFlags
& 0x04) {
936 if (mifare_classic_halt(NULL
, cuid
)) {
937 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
947 cmd_send(CMD_ACK
,isOK
,0,0,uid
,4);
950 if ((workFlags
& 0x10) || (!isOK
)) {
951 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
957 void MifareCGetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
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
= arg0
;
965 uint8_t blockNo
= arg2
;
968 uint8_t wupC1
[] = { 0x40 };
969 uint8_t wupC2
[] = { 0x43 };
973 uint8_t data
[18] = {0x00};
976 uint8_t* receivedAnswer
= get_bigbufptr_recvrespbuf();
977 uint8_t *receivedAnswerPar
= receivedAnswer
+ MAX_FRAME_SIZE
;
979 if (workFlags
& 0x08) {
984 iso14a_clear_trace();
985 iso14a_set_tracing(TRUE
);
986 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
990 if (workFlags
& 0x02) {
991 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
992 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
993 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
997 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
998 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
999 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1005 if ((mifare_sendcmd_short(NULL
, 0, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 18)) {
1006 if (MF_DBGLEVEL
>= 1) Dbprintf("read block send command error");
1009 memcpy(data
, receivedAnswer
, 18);
1011 if (workFlags
& 0x04) {
1012 if (mifare_classic_halt(NULL
, cuid
)) {
1013 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1023 cmd_send(CMD_ACK
,isOK
,0,0,data
,18);
1026 if ((workFlags
& 0x10) || (!isOK
)) {
1027 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1032 void MifareCIdent(){
1035 uint8_t wupC1
[] = { 0x40 };
1036 uint8_t wupC2
[] = { 0x43 };
1041 uint8_t* receivedAnswer
= get_bigbufptr_recvrespbuf();
1042 uint8_t *receivedAnswerPar
= receivedAnswer
+ MAX_FRAME_SIZE
;
1044 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1045 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1049 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1050 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1054 if (mifare_classic_halt(NULL
, 0)) {
1058 cmd_send(CMD_ACK
,isOK
,0,0,0,0);