1 //-----------------------------------------------------------------------------
2 // Merlok - June 2011, 2012
3 // Gerhard de Koning Gans - May 2008
4 // Hagen Fritsch - June 2010
6 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
7 // at your option, any later version. See the LICENSE.txt file for the text of
9 //-----------------------------------------------------------------------------
10 // Routines to support ISO 14443 type A.
11 //-----------------------------------------------------------------------------
13 #include "mifarecmd.h"
16 //-----------------------------------------------------------------------------
17 // Select, Authenticate, Read a MIFARE tag.
19 //-----------------------------------------------------------------------------
20 void MifareReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
23 uint8_t blockNo
= arg0
;
24 uint8_t keyType
= arg1
;
26 ui64Key
= bytes_to_num(datain
, 6);
30 byte_t dataoutbuf
[16];
33 struct Crypto1State mpcs
= {0, 0};
34 struct Crypto1State
*pcs
;
39 // iso14a_set_tracing(false);
41 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
48 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
49 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
53 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
54 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
58 if(mifare_classic_readblock(pcs
, cuid
, blockNo
, dataoutbuf
)) {
59 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block error");
63 if(mifare_classic_halt(pcs
, cuid
)) {
64 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
72 // ----------------------------- crypto1 destroy
75 if (MF_DBGLEVEL
>= 2) DbpString("READ BLOCK FINISHED");
78 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16);
82 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
84 // iso14a_set_tracing(TRUE);
88 void MifareUReadBlock(uint8_t arg0
,uint8_t *datain
)
91 uint8_t blockNo
= arg0
;
95 byte_t dataoutbuf
[16];
100 iso14a_clear_trace();
101 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
108 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
109 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
113 if(mifare_ultra_readblock(cuid
, blockNo
, dataoutbuf
)) {
114 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block error");
118 if(mifare_ultra_halt(cuid
)) {
119 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
127 if (MF_DBGLEVEL
>= 2) DbpString("READ BLOCK FINISHED");
130 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16);
135 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
140 //-----------------------------------------------------------------------------
141 // Select, Authenticate, Read a MIFARE tag.
142 // read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)
143 //-----------------------------------------------------------------------------
144 void MifareReadSector(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
147 uint8_t sectorNo
= arg0
;
148 uint8_t keyType
= arg1
;
149 uint64_t ui64Key
= 0;
150 ui64Key
= bytes_to_num(datain
, 6);
154 byte_t dataoutbuf
[16 * 16];
157 struct Crypto1State mpcs
= {0, 0};
158 struct Crypto1State
*pcs
;
162 iso14a_clear_trace();
163 // iso14a_set_tracing(false);
165 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
172 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
174 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
178 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
180 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
183 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
184 if(mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
+ 16 * blockNo
)) {
186 if (MF_DBGLEVEL
>= 1) Dbprintf("Read sector %2d block %2d error", sectorNo
, blockNo
);
191 if(mifare_classic_halt(pcs
, cuid
)) {
192 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
196 // ----------------------------- crypto1 destroy
197 crypto1_destroy(pcs
);
199 if (MF_DBGLEVEL
>= 2) DbpString("READ SECTOR FINISHED");
202 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16*NumBlocksPerSector(sectorNo
));
206 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
208 // iso14a_set_tracing(TRUE);
212 void MifareUReadCard(uint8_t arg0
, uint8_t *datain
)
215 uint8_t sectorNo
= arg0
;
219 byte_t dataoutbuf
[16 * 4];
224 iso14a_clear_trace();
225 // iso14a_set_tracing(false);
227 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
234 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
235 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
238 for(int sec
=0;sec
<16;sec
++){
239 if(mifare_ultra_readblock(cuid
, sectorNo
* 4 + sec
, dataoutbuf
+ 4 * sec
)) {
240 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block %d error",sec
);
244 if(mifare_ultra_halt(cuid
)) {
245 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
253 if (MF_DBGLEVEL
>= 2) DbpString("READ CARD FINISHED");
256 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,64);
260 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
266 //-----------------------------------------------------------------------------
267 // Select, Authenticate, Write a MIFARE tag.
269 //-----------------------------------------------------------------------------
270 void MifareWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
273 uint8_t blockNo
= arg0
;
274 uint8_t keyType
= arg1
;
275 uint64_t ui64Key
= 0;
276 byte_t blockdata
[16];
278 ui64Key
= bytes_to_num(datain
, 6);
279 memcpy(blockdata
, datain
+ 10, 16);
285 struct Crypto1State mpcs
= {0, 0};
286 struct Crypto1State
*pcs
;
290 iso14a_clear_trace();
291 // iso14a_set_tracing(false);
293 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
300 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
301 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
305 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
306 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
310 if(mifare_classic_writeblock(pcs
, cuid
, blockNo
, blockdata
)) {
311 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
315 if(mifare_classic_halt(pcs
, cuid
)) {
316 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
324 // ----------------------------- crypto1 destroy
325 crypto1_destroy(pcs
);
327 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
330 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
335 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
337 // iso14a_set_tracing(TRUE);
342 void MifareUWriteBlock(uint8_t arg0
, uint8_t *datain
)
345 uint8_t blockNo
= arg0
;
346 byte_t blockdata
[16];
348 memset(blockdata
,'\0',16);
349 memcpy(blockdata
, datain
,16);
357 iso14a_clear_trace();
358 // iso14a_set_tracing(false);
360 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
367 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
368 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
372 if(mifare_ultra_writeblock(cuid
, blockNo
, blockdata
)) {
373 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
377 if(mifare_ultra_halt(cuid
)) {
378 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
386 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
389 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
394 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
396 // iso14a_set_tracing(TRUE);
400 void MifareUWriteBlock_Special(uint8_t arg0
, uint8_t *datain
)
403 uint8_t blockNo
= arg0
;
406 memcpy(blockdata
, datain
,4);
414 iso14a_clear_trace();
415 // iso14a_set_tracing(false);
417 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
424 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
425 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
429 if(mifare_ultra_special_writeblock(cuid
, blockNo
, blockdata
)) {
430 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
434 if(mifare_ultra_halt(cuid
)) {
435 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
443 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
446 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
451 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
453 // iso14a_set_tracing(TRUE);
458 // Return 1 if the nonce is invalid else return 0
459 int valid_nonce(uint32_t Nt
, uint32_t NtEnc
, uint32_t Ks1
, uint8_t *parity
) {
460 return ((oddparity((Nt
>> 24) & 0xFF) == ((parity
[0]) ^ oddparity((NtEnc
>> 24) & 0xFF) ^ BIT(Ks1
,16))) & \
461 (oddparity((Nt
>> 16) & 0xFF) == ((parity
[1]) ^ oddparity((NtEnc
>> 16) & 0xFF) ^ BIT(Ks1
,8))) & \
462 (oddparity((Nt
>> 8) & 0xFF) == ((parity
[2]) ^ oddparity((NtEnc
>> 8) & 0xFF) ^ BIT(Ks1
,0)))) ? 1 : 0;
466 //-----------------------------------------------------------------------------
467 // MIFARE nested authentication.
469 //-----------------------------------------------------------------------------
470 void MifareNested(uint32_t arg0
, uint32_t arg1
, uint32_t calibrate
, uint8_t *datain
)
473 uint8_t blockNo
= arg0
& 0xff;
474 uint8_t keyType
= (arg0
>> 8) & 0xff;
475 uint8_t targetBlockNo
= arg1
& 0xff;
476 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
477 uint64_t ui64Key
= 0;
479 ui64Key
= bytes_to_num(datain
, 6);
482 uint16_t rtr
, i
, j
, len
;
484 static uint16_t dmin
, dmax
;
486 uint32_t cuid
, nt1
, nt2
, nttmp
, nttest
, ks1
;
488 uint32_t target_nt
[2], target_ks
[2];
490 uint8_t par_array
[4];
492 struct Crypto1State mpcs
= {0, 0};
493 struct Crypto1State
*pcs
;
495 uint8_t* receivedAnswer
= get_bigbufptr_recvrespbuf();
497 uint32_t auth1_time
, auth2_time
;
498 static uint16_t delta_time
;
501 iso14a_clear_trace();
502 iso14a_set_tracing(false);
504 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
510 // statistics on nonce distance
511 if (calibrate
) { // for first call only. Otherwise reuse previous calibration
518 for (rtr
= 0; rtr
< 17; rtr
++) {
520 // prepare next select. No need to power down the card.
521 if(mifare_classic_halt(pcs
, cuid
)) {
522 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
527 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
528 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
534 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
535 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
541 auth2_time
= auth1_time
+ delta_time
;
545 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_NESTED
, &nt2
, &auth2_time
)) {
546 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error");
551 nttmp
= prng_successor(nt1
, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160
552 for (i
= 101; i
< 1200; i
++) {
553 nttmp
= prng_successor(nttmp
, 1);
554 if (nttmp
== nt2
) break;
564 delta_time
= auth2_time
- auth1_time
+ 32; // allow some slack for proper timing
566 if (MF_DBGLEVEL
>= 3) Dbprintf("Nested: calibrating... ntdist=%d", i
);
570 if (rtr
<= 1) return;
572 davg
= (davg
+ (rtr
- 1)/2) / (rtr
- 1);
574 if (MF_DBGLEVEL
>= 3) Dbprintf("min=%d max=%d avg=%d, delta_time=%d", dmin
, dmax
, davg
, delta_time
);
582 // -------------------------------------------------------------------------------------------------
586 // get crypted nonces for target sector
587 for(i
=0; i
< 2; i
++) { // look for exactly two different nonces
590 while(target_nt
[i
] == 0) { // continue until we have an unambiguous nonce
592 // prepare next select. No need to power down the card.
593 if(mifare_classic_halt(pcs
, cuid
)) {
594 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
598 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
599 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
604 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
605 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
609 // nested authentication
610 auth2_time
= auth1_time
+ delta_time
;
611 len
= mifare_sendcmd_shortex(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType
& 0x01), targetBlockNo
, receivedAnswer
, par
, &auth2_time
);
613 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error len=%d", len
);
617 nt2
= bytes_to_num(receivedAnswer
, 4);
618 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i
+1, nt1
, nt2
, par
[0]);
620 // Parity validity check
621 for (j
= 0; j
< 4; j
++) {
622 par_array
[j
] = (oddparity(receivedAnswer
[j
]) != ((par
[0] >> (7-j
)) & 0x01));
626 nttest
= prng_successor(nt1
, dmin
- 1);
627 for (j
= dmin
; j
< dmax
+ 1; j
++) {
628 nttest
= prng_successor(nttest
, 1);
631 if (valid_nonce(nttest
, nt2
, ks1
, par_array
)){
632 if (ncount
> 0) { // we are only interested in disambiguous nonces, try again
633 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i
+1, j
);
637 target_nt
[i
] = nttest
;
640 if (i
== 1 && target_nt
[1] == target_nt
[0]) { // we need two different nonces
642 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j
);
645 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i
+1, j
);
648 if (target_nt
[i
] == 0 && j
== dmax
+1 && MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i
+1);
654 // ----------------------------- crypto1 destroy
655 crypto1_destroy(pcs
);
657 byte_t buf
[4 + 4 * 4];
658 memcpy(buf
, &cuid
, 4);
659 memcpy(buf
+4, &target_nt
[0], 4);
660 memcpy(buf
+8, &target_ks
[0], 4);
661 memcpy(buf
+12, &target_nt
[1], 4);
662 memcpy(buf
+16, &target_ks
[1], 4);
665 cmd_send(CMD_ACK
, 0, 2, targetBlockNo
+ (targetKeyType
* 0x100), buf
, sizeof(buf
));
668 if (MF_DBGLEVEL
>= 3) DbpString("NESTED FINISHED");
670 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
672 iso14a_set_tracing(TRUE
);
675 //-----------------------------------------------------------------------------
676 // MIFARE check keys. key count up to 85.
678 //-----------------------------------------------------------------------------
679 void MifareChkKeys(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
682 uint8_t blockNo
= arg0
;
683 uint8_t keyType
= arg1
;
684 uint8_t keyCount
= arg2
;
685 uint64_t ui64Key
= 0;
692 struct Crypto1State mpcs
= {0, 0};
693 struct Crypto1State
*pcs
;
697 int OLD_MF_DBGLEVEL
= MF_DBGLEVEL
;
698 MF_DBGLEVEL
= MF_DBG_NONE
;
701 iso14a_clear_trace();
702 iso14a_set_tracing(TRUE
);
704 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
710 for (i
= 0; i
< keyCount
; i
++) {
711 if(mifare_classic_halt(pcs
, cuid
)) {
712 if (MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Halt error");
715 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
716 if (OLD_MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Can't select card");
720 ui64Key
= bytes_to_num(datain
+ i
* 6, 6);
721 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
729 // ----------------------------- crypto1 destroy
730 crypto1_destroy(pcs
);
733 cmd_send(CMD_ACK
,isOK
,0,0,datain
+ i
* 6,6);
737 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
740 // restore debug level
741 MF_DBGLEVEL
= OLD_MF_DBGLEVEL
;
744 //-----------------------------------------------------------------------------
745 // MIFARE commands set debug level
747 //-----------------------------------------------------------------------------
748 void MifareSetDbgLvl(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
750 Dbprintf("Debug level: %d", MF_DBGLEVEL
);
754 //-----------------------------------------------------------------------------
755 // Work with emulator memory
757 //-----------------------------------------------------------------------------
758 void MifareEMemClr(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
763 void MifareEMemSet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
764 emlSetMem(datain
, arg0
, arg1
); // data, block num, blocks count
768 void MifareEMemGet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
771 emlGetMem(buf
, arg0
, arg1
); // data, block num, blocks count (max 4)
774 cmd_send(CMD_ACK
,arg0
,arg1
,0,buf
,48);
779 //-----------------------------------------------------------------------------
780 // Load a card into the emulator memory
782 //-----------------------------------------------------------------------------
783 void MifareECardLoad(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
784 uint8_t numSectors
= arg0
;
785 uint8_t keyType
= arg1
;
786 uint64_t ui64Key
= 0;
788 struct Crypto1State mpcs
= {0, 0};
789 struct Crypto1State
*pcs
;
793 byte_t dataoutbuf
[16];
794 byte_t dataoutbuf2
[16];
798 iso14a_clear_trace();
799 iso14a_set_tracing(false);
801 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
809 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
811 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
814 for (uint8_t sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
815 ui64Key
= emlGetKey(sectorNo
, keyType
);
817 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
819 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo
);
823 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_NESTED
)) {
825 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo
);
830 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
831 if(isOK
&& mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
)) {
833 if (MF_DBGLEVEL
>= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo
, blockNo
);
837 if (blockNo
< NumBlocksPerSector(sectorNo
) - 1) {
838 emlSetMem(dataoutbuf
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
839 } else { // sector trailer, keep the keys, set only the AC
840 emlGetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
841 memcpy(&dataoutbuf2
[6], &dataoutbuf
[6], 4);
842 emlSetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
849 if(mifare_classic_halt(pcs
, cuid
)) {
850 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
853 // ----------------------------- crypto1 destroy
854 crypto1_destroy(pcs
);
856 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
859 if (MF_DBGLEVEL
>= 2) DbpString("EMUL FILL SECTORS FINISHED");
864 //-----------------------------------------------------------------------------
865 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
867 //-----------------------------------------------------------------------------
868 void MifareCSetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
871 uint8_t needWipe
= arg0
;
872 // bit 0 - need get UID
874 // bit 2 - need HALT after sequence
875 // bit 3 - need init FPGA and field before sequence
876 // bit 4 - need reset FPGA and LED
877 uint8_t workFlags
= arg1
;
878 uint8_t blockNo
= arg2
;
881 uint8_t wupC1
[] = { 0x40 };
882 uint8_t wupC2
[] = { 0x43 };
883 uint8_t wipeC
[] = { 0x41 };
891 memset(uid
, 0x00, 10);
892 uint8_t *receivedAnswer
= get_bigbufptr_recvrespbuf();
893 uint8_t *receivedAnswerPar
= receivedAnswer
+ MAX_FRAME_SIZE
;
895 if (workFlags
& 0x08) {
897 iso14a_clear_trace();
898 iso14a_set_tracing(TRUE
);
900 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
907 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
909 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
914 if (workFlags
& 0x01) {
915 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
916 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
920 if(mifare_classic_halt(NULL
, cuid
)) {
921 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
928 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
929 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
930 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
934 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
935 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
936 if (MF_DBGLEVEL
>= 1) Dbprintf("wipeC error");
940 if(mifare_classic_halt(NULL
, cuid
)) {
941 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
947 if (workFlags
& 0x02) {
948 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
949 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
950 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
954 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
955 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
956 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
961 if ((mifare_sendcmd_short(NULL
, 0, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 1) || (receivedAnswer
[0] != 0x0a)) {
962 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send command error");
966 memcpy(d_block
, datain
, 16);
967 AppendCrc14443a(d_block
, 16);
969 ReaderTransmit(d_block
, sizeof(d_block
), NULL
);
970 if ((ReaderReceive(receivedAnswer
, receivedAnswerPar
) != 1) || (receivedAnswer
[0] != 0x0a)) {
971 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send data error");
975 if (workFlags
& 0x04) {
976 if (mifare_classic_halt(NULL
, cuid
)) {
977 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
987 cmd_send(CMD_ACK
,isOK
,0,0,uid
,4);
990 if ((workFlags
& 0x10) || (!isOK
)) {
992 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
998 void MifareCGetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1001 // bit 1 - need wupC
1002 // bit 2 - need HALT after sequence
1003 // bit 3 - need init FPGA and field before sequence
1004 // bit 4 - need reset FPGA and LED
1005 uint8_t workFlags
= arg0
;
1006 uint8_t blockNo
= arg2
;
1009 uint8_t wupC1
[] = { 0x40 };
1010 uint8_t wupC2
[] = { 0x43 };
1017 memset(data
, 0x00, 18);
1018 uint8_t* receivedAnswer
= get_bigbufptr_recvrespbuf();
1019 uint8_t *receivedAnswerPar
= receivedAnswer
+ MAX_FRAME_SIZE
;
1021 if (workFlags
& 0x08) {
1023 iso14a_clear_trace();
1024 iso14a_set_tracing(TRUE
);
1026 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1033 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1035 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1039 if (workFlags
& 0x02) {
1040 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1041 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1042 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1046 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1047 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1048 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1054 if ((mifare_sendcmd_short(NULL
, 0, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 18)) {
1055 if (MF_DBGLEVEL
>= 1) Dbprintf("read block send command error");
1058 memcpy(data
, receivedAnswer
, 18);
1060 if (workFlags
& 0x04) {
1061 if (mifare_classic_halt(NULL
, cuid
)) {
1062 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1072 cmd_send(CMD_ACK
,isOK
,0,0,data
,18);
1075 if ((workFlags
& 0x10) || (!isOK
)) {
1077 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);