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"
19 //-----------------------------------------------------------------------------
20 // Select, Authenticate, Read a MIFARE tag.
22 //-----------------------------------------------------------------------------
23 void MifareReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
26 uint8_t blockNo
= arg0
;
27 uint8_t keyType
= arg1
;
29 ui64Key
= bytes_to_num(datain
, 6);
33 byte_t dataoutbuf
[16];
36 struct Crypto1State mpcs
= {0, 0};
37 struct Crypto1State
*pcs
;
42 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
49 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
50 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
54 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
55 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
59 if(mifare_classic_readblock(pcs
, cuid
, blockNo
, dataoutbuf
)) {
60 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block error");
64 if(mifare_classic_halt(pcs
, cuid
)) {
65 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
73 // ----------------------------- crypto1 destroy
76 if (MF_DBGLEVEL
>= 2) DbpString("READ BLOCK FINISHED");
79 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16);
83 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
87 void MifareUReadBlock(uint8_t arg0
,uint8_t *datain
)
90 uint8_t blockNo
= arg0
;
94 byte_t dataoutbuf
[16];
100 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
107 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
108 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
112 if(mifare_ultra_readblock(cuid
, blockNo
, dataoutbuf
)) {
113 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block error");
117 if(mifare_ultra_halt(cuid
)) {
118 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
126 if (MF_DBGLEVEL
>= 2) DbpString("READ BLOCK FINISHED");
129 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16);
131 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
135 //-----------------------------------------------------------------------------
136 // Select, Authenticate, Read a MIFARE tag.
137 // read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)
138 //-----------------------------------------------------------------------------
139 void MifareReadSector(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
142 uint8_t sectorNo
= arg0
;
143 uint8_t keyType
= arg1
;
144 uint64_t ui64Key
= 0;
145 ui64Key
= bytes_to_num(datain
, 6);
149 byte_t dataoutbuf
[16 * 16];
152 struct Crypto1State mpcs
= {0, 0};
153 struct Crypto1State
*pcs
;
157 iso14a_clear_trace();
159 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
166 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
168 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
172 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
174 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
177 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
178 if(mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
+ 16 * blockNo
)) {
180 if (MF_DBGLEVEL
>= 1) Dbprintf("Read sector %2d block %2d error", sectorNo
, blockNo
);
185 if(mifare_classic_halt(pcs
, cuid
)) {
186 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
189 // ----------------------------- crypto1 destroy
190 crypto1_destroy(pcs
);
192 if (MF_DBGLEVEL
>= 2) DbpString("READ SECTOR FINISHED");
195 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16*NumBlocksPerSector(sectorNo
));
199 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
204 void MifareUReadCard(uint8_t arg0
, uint8_t *datain
)
207 uint8_t sectorNo
= arg0
;
211 byte_t dataoutbuf
[16 * 4];
216 iso14a_clear_trace();
218 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
225 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
226 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
229 for(int sec
=0;sec
<16;sec
++){
230 if(mifare_ultra_readblock(cuid
, sectorNo
* 4 + sec
, dataoutbuf
+ 4 * sec
)) {
231 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block %d error",sec
);
235 if(mifare_ultra_halt(cuid
)) {
236 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
244 if (MF_DBGLEVEL
>= 2) DbpString("READ CARD FINISHED");
247 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,64);
251 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
257 //-----------------------------------------------------------------------------
258 // Select, Authenticate, Write a MIFARE tag.
260 //-----------------------------------------------------------------------------
261 void MifareWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
264 uint8_t blockNo
= arg0
;
265 uint8_t keyType
= arg1
;
266 uint64_t ui64Key
= 0;
267 byte_t blockdata
[16];
269 ui64Key
= bytes_to_num(datain
, 6);
270 memcpy(blockdata
, datain
+ 10, 16);
276 struct Crypto1State mpcs
= {0, 0};
277 struct Crypto1State
*pcs
;
281 iso14a_clear_trace();
283 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
290 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
291 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
295 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
296 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
300 if(mifare_classic_writeblock(pcs
, cuid
, blockNo
, blockdata
)) {
301 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
305 if(mifare_classic_halt(pcs
, cuid
)) {
306 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
314 // ----------------------------- crypto1 destroy
315 crypto1_destroy(pcs
);
317 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
320 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
325 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
329 void MifareUWriteBlock(uint8_t arg0
, uint8_t *datain
)
332 uint8_t blockNo
= arg0
;
333 byte_t blockdata
[16];
335 memset(blockdata
,'\0',16);
336 memcpy(blockdata
, datain
,16);
344 iso14a_clear_trace();
346 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
353 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
354 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
358 if(mifare_ultra_writeblock(cuid
, blockNo
, blockdata
)) {
359 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
363 if(mifare_ultra_halt(cuid
)) {
364 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
372 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
375 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
380 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
382 // iso14a_set_tracing(TRUE);
385 void MifareUWriteBlock_Special(uint8_t arg0
, uint8_t *datain
)
388 uint8_t blockNo
= arg0
;
391 memcpy(blockdata
, datain
,4);
399 iso14a_clear_trace();
401 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
408 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
409 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
413 if(mifare_ultra_special_writeblock(cuid
, blockNo
, blockdata
)) {
414 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
418 if(mifare_ultra_halt(cuid
)) {
419 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
427 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
430 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
434 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
438 // Return 1 if the nonce is invalid else return 0
439 int valid_nonce(uint32_t Nt
, uint32_t NtEnc
, uint32_t Ks1
, uint8_t *parity
) {
440 return ((oddparity((Nt
>> 24) & 0xFF) == ((parity
[0]) ^ oddparity((NtEnc
>> 24) & 0xFF) ^ BIT(Ks1
,16))) & \
441 (oddparity((Nt
>> 16) & 0xFF) == ((parity
[1]) ^ oddparity((NtEnc
>> 16) & 0xFF) ^ BIT(Ks1
,8))) & \
442 (oddparity((Nt
>> 8) & 0xFF) == ((parity
[2]) ^ oddparity((NtEnc
>> 8) & 0xFF) ^ BIT(Ks1
,0)))) ? 1 : 0;
446 //-----------------------------------------------------------------------------
447 // MIFARE nested authentication.
449 //-----------------------------------------------------------------------------
450 void MifareNested(uint32_t arg0
, uint32_t arg1
, uint32_t calibrate
, uint8_t *datain
)
453 uint8_t blockNo
= arg0
& 0xff;
454 uint8_t keyType
= (arg0
>> 8) & 0xff;
455 uint8_t targetBlockNo
= arg1
& 0xff;
456 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
457 uint64_t ui64Key
= 0;
459 ui64Key
= bytes_to_num(datain
, 6);
462 uint16_t rtr
, i
, j
, len
;
464 static uint16_t dmin
, dmax
;
466 uint32_t cuid
, nt1
, nt2
, nttmp
, nttest
, ks1
;
468 uint32_t target_nt
[2], target_ks
[2];
470 uint8_t par_array
[4];
472 struct Crypto1State mpcs
= {0, 0};
473 struct Crypto1State
*pcs
;
475 uint8_t* receivedAnswer
= get_bigbufptr_recvrespbuf();
477 uint32_t auth1_time
, auth2_time
;
478 static uint16_t delta_time
;
481 iso14a_clear_trace();
482 iso14a_set_tracing(false);
484 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
490 // statistics on nonce distance
491 if (calibrate
) { // for first call only. Otherwise reuse previous calibration
499 for (rtr
= 0; rtr
< 17; rtr
++) {
501 // prepare next select. No need to power down the card.
502 if(mifare_classic_halt(pcs
, cuid
)) {
503 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
508 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
509 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
515 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
516 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
522 auth2_time
= auth1_time
+ delta_time
;
526 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_NESTED
, &nt2
, &auth2_time
)) {
527 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error");
532 nttmp
= prng_successor(nt1
, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160
533 for (i
= 101; i
< 1200; i
++) {
534 nttmp
= prng_successor(nttmp
, 1);
535 if (nttmp
== nt2
) break;
545 delta_time
= auth2_time
- auth1_time
+ 32; // allow some slack for proper timing
547 if (MF_DBGLEVEL
>= 3) Dbprintf("Nested: calibrating... ntdist=%d", i
);
551 if (rtr
<= 1) return;
553 davg
= (davg
+ (rtr
- 1)/2) / (rtr
- 1);
555 if (MF_DBGLEVEL
>= 3) Dbprintf("min=%d max=%d avg=%d, delta_time=%d", dmin
, dmax
, davg
, delta_time
);
563 // -------------------------------------------------------------------------------------------------
567 // get crypted nonces for target sector
568 for(i
=0; i
< 2; i
++) { // look for exactly two different nonces
571 while(target_nt
[i
] == 0) { // continue until we have an unambiguous nonce
573 // prepare next select. No need to power down the card.
574 if(mifare_classic_halt(pcs
, cuid
)) {
575 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
579 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
580 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
585 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
586 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
590 // nested authentication
591 auth2_time
= auth1_time
+ delta_time
;
592 len
= mifare_sendcmd_shortex(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType
& 0x01), targetBlockNo
, receivedAnswer
, par
, &auth2_time
);
594 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error len=%d", len
);
598 nt2
= bytes_to_num(receivedAnswer
, 4);
599 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i
+1, nt1
, nt2
, par
[0]);
601 // Parity validity check
602 for (j
= 0; j
< 4; j
++) {
603 par_array
[j
] = (oddparity(receivedAnswer
[j
]) != ((par
[0] >> (7-j
)) & 0x01));
607 nttest
= prng_successor(nt1
, dmin
- 1);
608 for (j
= dmin
; j
< dmax
+ 1; j
++) {
609 nttest
= prng_successor(nttest
, 1);
612 if (valid_nonce(nttest
, nt2
, ks1
, par_array
)){
613 if (ncount
> 0) { // we are only interested in disambiguous nonces, try again
614 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i
+1, j
);
618 target_nt
[i
] = nttest
;
621 if (i
== 1 && target_nt
[1] == target_nt
[0]) { // we need two different nonces
623 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j
);
626 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i
+1, j
);
629 if (target_nt
[i
] == 0 && j
== dmax
+1 && MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i
+1);
635 // ----------------------------- crypto1 destroy
636 crypto1_destroy(pcs
);
638 byte_t buf
[4 + 4 * 4];
639 memcpy(buf
, &cuid
, 4);
640 memcpy(buf
+4, &target_nt
[0], 4);
641 memcpy(buf
+8, &target_ks
[0], 4);
642 memcpy(buf
+12, &target_nt
[1], 4);
643 memcpy(buf
+16, &target_ks
[1], 4);
646 cmd_send(CMD_ACK
, 0, 2, targetBlockNo
+ (targetKeyType
* 0x100), buf
, sizeof(buf
));
649 if (MF_DBGLEVEL
>= 3) DbpString("NESTED FINISHED");
651 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
653 iso14a_set_tracing(TRUE
);
656 //-----------------------------------------------------------------------------
657 // MIFARE check keys. key count up to 85.
659 //-----------------------------------------------------------------------------
660 void MifareChkKeys(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
663 uint8_t blockNo
= arg0
;
664 uint8_t keyType
= arg1
;
665 uint8_t keyCount
= arg2
;
666 uint64_t ui64Key
= 0;
673 struct Crypto1State mpcs
= {0, 0};
674 struct Crypto1State
*pcs
;
678 int OLD_MF_DBGLEVEL
= MF_DBGLEVEL
;
679 MF_DBGLEVEL
= MF_DBG_NONE
;
682 iso14a_clear_trace();
683 iso14a_set_tracing(TRUE
);
685 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
691 for (i
= 0; i
< keyCount
; i
++) {
692 if(mifare_classic_halt(pcs
, cuid
)) {
693 if (MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Halt error");
696 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
697 if (OLD_MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Can't select card");
701 ui64Key
= bytes_to_num(datain
+ i
* 6, 6);
702 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
710 // ----------------------------- crypto1 destroy
711 crypto1_destroy(pcs
);
714 cmd_send(CMD_ACK
,isOK
,0,0,datain
+ i
* 6,6);
717 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
720 // restore debug level
721 MF_DBGLEVEL
= OLD_MF_DBGLEVEL
;
724 //-----------------------------------------------------------------------------
725 // MIFARE commands set debug level
727 //-----------------------------------------------------------------------------
728 void MifareSetDbgLvl(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
730 Dbprintf("Debug level: %d", MF_DBGLEVEL
);
733 //-----------------------------------------------------------------------------
734 // Work with emulator memory
736 //-----------------------------------------------------------------------------
737 void MifareEMemClr(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
741 void MifareEMemSet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
742 emlSetMem(datain
, arg0
, arg1
); // data, block num, blocks count
745 void MifareEMemGet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
746 byte_t buf
[USB_CMD_DATA_SIZE
];
747 emlGetMem(buf
, arg0
, arg1
); // data, block num, blocks count (max 4)
750 cmd_send(CMD_ACK
,arg0
,arg1
,0,buf
,USB_CMD_DATA_SIZE
);
754 //-----------------------------------------------------------------------------
755 // Load a card into the emulator memory
757 //-----------------------------------------------------------------------------
758 void MifareECardLoad(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
759 uint8_t numSectors
= arg0
;
760 uint8_t keyType
= arg1
;
761 uint64_t ui64Key
= 0;
763 struct Crypto1State mpcs
= {0, 0};
764 struct Crypto1State
*pcs
;
768 byte_t dataoutbuf
[16];
769 byte_t dataoutbuf2
[16];
773 iso14a_clear_trace();
774 iso14a_set_tracing(false);
776 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
784 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
786 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
789 for (uint8_t sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
790 ui64Key
= emlGetKey(sectorNo
, keyType
);
792 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
794 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo
);
798 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_NESTED
)) {
800 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo
);
805 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
806 if(isOK
&& mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
)) {
808 if (MF_DBGLEVEL
>= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo
, blockNo
);
812 if (blockNo
< NumBlocksPerSector(sectorNo
) - 1) {
813 emlSetMem(dataoutbuf
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
814 } else { // sector trailer, keep the keys, set only the AC
815 emlGetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
816 memcpy(&dataoutbuf2
[6], &dataoutbuf
[6], 4);
817 emlSetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
824 if(mifare_classic_halt(pcs
, cuid
)) {
825 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
828 // ----------------------------- crypto1 destroy
829 crypto1_destroy(pcs
);
831 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
834 if (MF_DBGLEVEL
>= 2) DbpString("EMUL FILL SECTORS FINISHED");
839 //-----------------------------------------------------------------------------
840 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
842 //-----------------------------------------------------------------------------
843 void MifareCSetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
846 uint8_t needWipe
= arg0
;
847 // bit 0 - need get UID
849 // bit 2 - need HALT after sequence
850 // bit 3 - need init FPGA and field before sequence
851 // bit 4 - need reset FPGA and LED
852 uint8_t workFlags
= arg1
;
853 uint8_t blockNo
= arg2
;
856 uint8_t wupC1
[] = { 0x40 };
857 uint8_t wupC2
[] = { 0x43 };
858 uint8_t wipeC
[] = { 0x41 };
862 uint8_t uid
[10] = {0x00};
863 uint8_t d_block
[18] = {0x00};
866 uint8_t *receivedAnswer
= get_bigbufptr_recvrespbuf();
867 uint8_t *receivedAnswerPar
= receivedAnswer
+ MAX_FRAME_SIZE
;
869 // reset FPGA and LED
870 if (workFlags
& 0x08) {
875 iso14a_clear_trace();
876 iso14a_set_tracing(TRUE
);
877 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
883 if (workFlags
& 0x01) {
884 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
885 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
889 if(mifare_classic_halt(NULL
, cuid
)) {
890 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
897 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
898 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
899 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
903 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
904 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
905 if (MF_DBGLEVEL
>= 1) Dbprintf("wipeC error");
909 if(mifare_classic_halt(NULL
, cuid
)) {
910 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
916 if (workFlags
& 0x02) {
917 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
918 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
919 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
923 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
924 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
925 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
930 if ((mifare_sendcmd_short(NULL
, 0, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 1) || (receivedAnswer
[0] != 0x0a)) {
931 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send command error");
935 memcpy(d_block
, datain
, 16);
936 AppendCrc14443a(d_block
, 16);
938 ReaderTransmit(d_block
, sizeof(d_block
), NULL
);
939 if ((ReaderReceive(receivedAnswer
, receivedAnswerPar
) != 1) || (receivedAnswer
[0] != 0x0a)) {
940 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send data error");
944 if (workFlags
& 0x04) {
945 if (mifare_classic_halt(NULL
, cuid
)) {
946 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
956 cmd_send(CMD_ACK
,isOK
,0,0,uid
,4);
959 if ((workFlags
& 0x10) || (!isOK
)) {
960 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
966 void MifareCGetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
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
= arg0
;
974 uint8_t blockNo
= arg2
;
977 uint8_t wupC1
[] = { 0x40 };
978 uint8_t wupC2
[] = { 0x43 };
982 uint8_t data
[18] = {0x00};
985 uint8_t* receivedAnswer
= get_bigbufptr_recvrespbuf();
986 uint8_t *receivedAnswerPar
= receivedAnswer
+ MAX_FRAME_SIZE
;
988 if (workFlags
& 0x08) {
993 iso14a_clear_trace();
994 iso14a_set_tracing(TRUE
);
995 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
999 if (workFlags
& 0x02) {
1000 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1001 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1002 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1006 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1007 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1008 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1014 if ((mifare_sendcmd_short(NULL
, 0, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 18)) {
1015 if (MF_DBGLEVEL
>= 1) Dbprintf("read block send command error");
1018 memcpy(data
, receivedAnswer
, 18);
1020 if (workFlags
& 0x04) {
1021 if (mifare_classic_halt(NULL
, cuid
)) {
1022 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1032 cmd_send(CMD_ACK
,isOK
,0,0,data
,18);
1035 if ((workFlags
& 0x10) || (!isOK
)) {
1036 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1041 void MifareCIdent(){
1044 uint8_t wupC1
[] = { 0x40 };
1045 uint8_t wupC2
[] = { 0x43 };
1050 uint8_t* receivedAnswer
= get_bigbufptr_recvrespbuf();
1051 uint8_t *receivedAnswerPar
= receivedAnswer
+ MAX_FRAME_SIZE
;
1053 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1054 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1058 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1059 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1063 if (mifare_classic_halt(NULL
, 0)) {
1067 cmd_send(CMD_ACK
,isOK
,0,0,0,0);