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();
217 // iso14a_set_tracing(false);
219 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
226 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
227 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
230 for(int sec
=0;sec
<16;sec
++){
231 if(mifare_ultra_readblock(cuid
, sectorNo
* 4 + sec
, dataoutbuf
+ 4 * sec
)) {
232 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block %d error",sec
);
236 if(mifare_ultra_halt(cuid
)) {
237 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
245 if (MF_DBGLEVEL
>= 2) DbpString("READ CARD FINISHED");
248 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,64);
252 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
258 //-----------------------------------------------------------------------------
259 // Select, Authenticate, Write a MIFARE tag.
261 //-----------------------------------------------------------------------------
262 void MifareWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
265 uint8_t blockNo
= arg0
;
266 uint8_t keyType
= arg1
;
267 uint64_t ui64Key
= 0;
268 byte_t blockdata
[16];
270 ui64Key
= bytes_to_num(datain
, 6);
271 memcpy(blockdata
, datain
+ 10, 16);
277 struct Crypto1State mpcs
= {0, 0};
278 struct Crypto1State
*pcs
;
282 iso14a_clear_trace();
284 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
291 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
292 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
296 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
297 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
301 if(mifare_classic_writeblock(pcs
, cuid
, blockNo
, blockdata
)) {
302 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
306 if(mifare_classic_halt(pcs
, cuid
)) {
307 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
315 // ----------------------------- crypto1 destroy
316 crypto1_destroy(pcs
);
318 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
321 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
326 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
330 void MifareUWriteBlock(uint8_t arg0
, uint8_t *datain
)
333 uint8_t blockNo
= arg0
;
334 byte_t blockdata
[16];
336 memset(blockdata
,'\0',16);
337 memcpy(blockdata
, datain
,16);
345 iso14a_clear_trace();
347 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
354 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
355 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
359 if(mifare_ultra_writeblock(cuid
, blockNo
, blockdata
)) {
360 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
364 if(mifare_ultra_halt(cuid
)) {
365 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
373 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
376 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
381 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
383 // iso14a_set_tracing(TRUE);
386 void MifareUWriteBlock_Special(uint8_t arg0
, uint8_t *datain
)
389 uint8_t blockNo
= arg0
;
392 memcpy(blockdata
, datain
,4);
400 iso14a_clear_trace();
402 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
409 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
410 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
414 if(mifare_ultra_special_writeblock(cuid
, blockNo
, blockdata
)) {
415 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
419 if(mifare_ultra_halt(cuid
)) {
420 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
428 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
431 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
435 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
439 // Return 1 if the nonce is invalid else return 0
440 int valid_nonce(uint32_t Nt
, uint32_t NtEnc
, uint32_t Ks1
, uint8_t *parity
) {
441 return ((oddparity((Nt
>> 24) & 0xFF) == ((parity
[0]) ^ oddparity((NtEnc
>> 24) & 0xFF) ^ BIT(Ks1
,16))) & \
442 (oddparity((Nt
>> 16) & 0xFF) == ((parity
[1]) ^ oddparity((NtEnc
>> 16) & 0xFF) ^ BIT(Ks1
,8))) & \
443 (oddparity((Nt
>> 8) & 0xFF) == ((parity
[2]) ^ oddparity((NtEnc
>> 8) & 0xFF) ^ BIT(Ks1
,0)))) ? 1 : 0;
447 //-----------------------------------------------------------------------------
448 // MIFARE nested authentication.
450 //-----------------------------------------------------------------------------
451 void MifareNested(uint32_t arg0
, uint32_t arg1
, uint32_t calibrate
, uint8_t *datain
)
454 uint8_t blockNo
= arg0
& 0xff;
455 uint8_t keyType
= (arg0
>> 8) & 0xff;
456 uint8_t targetBlockNo
= arg1
& 0xff;
457 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
458 uint64_t ui64Key
= 0;
460 ui64Key
= bytes_to_num(datain
, 6);
463 uint16_t rtr
, i
, j
, len
;
465 static uint16_t dmin
, dmax
;
467 uint32_t cuid
, nt1
, nt2
, nttmp
, nttest
, ks1
;
469 uint32_t target_nt
[2], target_ks
[2];
471 uint8_t par_array
[4];
473 struct Crypto1State mpcs
= {0, 0};
474 struct Crypto1State
*pcs
;
476 uint8_t* receivedAnswer
= get_bigbufptr_recvrespbuf();
478 uint32_t auth1_time
, auth2_time
;
479 static uint16_t delta_time
;
482 iso14a_clear_trace();
483 iso14a_set_tracing(false);
485 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
491 // statistics on nonce distance
492 if (calibrate
) { // for first call only. Otherwise reuse previous calibration
500 for (rtr
= 0; rtr
< 17; rtr
++) {
502 // prepare next select. No need to power down the card.
503 if(mifare_classic_halt(pcs
, cuid
)) {
504 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
509 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
510 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
516 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
517 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
523 auth2_time
= auth1_time
+ delta_time
;
527 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_NESTED
, &nt2
, &auth2_time
)) {
528 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error");
533 nttmp
= prng_successor(nt1
, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160
534 for (i
= 101; i
< 1200; i
++) {
535 nttmp
= prng_successor(nttmp
, 1);
536 if (nttmp
== nt2
) break;
546 delta_time
= auth2_time
- auth1_time
+ 32; // allow some slack for proper timing
548 if (MF_DBGLEVEL
>= 3) Dbprintf("Nested: calibrating... ntdist=%d", i
);
552 if (rtr
<= 1) return;
554 davg
= (davg
+ (rtr
- 1)/2) / (rtr
- 1);
556 if (MF_DBGLEVEL
>= 3) Dbprintf("min=%d max=%d avg=%d, delta_time=%d", dmin
, dmax
, davg
, delta_time
);
564 // -------------------------------------------------------------------------------------------------
568 // get crypted nonces for target sector
569 for(i
=0; i
< 2; i
++) { // look for exactly two different nonces
572 while(target_nt
[i
] == 0) { // continue until we have an unambiguous nonce
574 // prepare next select. No need to power down the card.
575 if(mifare_classic_halt(pcs
, cuid
)) {
576 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
580 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
581 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
586 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
587 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
591 // nested authentication
592 auth2_time
= auth1_time
+ delta_time
;
593 len
= mifare_sendcmd_shortex(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType
& 0x01), targetBlockNo
, receivedAnswer
, par
, &auth2_time
);
595 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error len=%d", len
);
599 nt2
= bytes_to_num(receivedAnswer
, 4);
600 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i
+1, nt1
, nt2
, par
[0]);
602 // Parity validity check
603 for (j
= 0; j
< 4; j
++) {
604 par_array
[j
] = (oddparity(receivedAnswer
[j
]) != ((par
[0] >> (7-j
)) & 0x01));
608 nttest
= prng_successor(nt1
, dmin
- 1);
609 for (j
= dmin
; j
< dmax
+ 1; j
++) {
610 nttest
= prng_successor(nttest
, 1);
613 if (valid_nonce(nttest
, nt2
, ks1
, par_array
)){
614 if (ncount
> 0) { // we are only interested in disambiguous nonces, try again
615 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i
+1, j
);
619 target_nt
[i
] = nttest
;
622 if (i
== 1 && target_nt
[1] == target_nt
[0]) { // we need two different nonces
624 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j
);
627 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i
+1, j
);
630 if (target_nt
[i
] == 0 && j
== dmax
+1 && MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i
+1);
636 // ----------------------------- crypto1 destroy
637 crypto1_destroy(pcs
);
639 byte_t buf
[4 + 4 * 4];
640 memcpy(buf
, &cuid
, 4);
641 memcpy(buf
+4, &target_nt
[0], 4);
642 memcpy(buf
+8, &target_ks
[0], 4);
643 memcpy(buf
+12, &target_nt
[1], 4);
644 memcpy(buf
+16, &target_ks
[1], 4);
647 cmd_send(CMD_ACK
, 0, 2, targetBlockNo
+ (targetKeyType
* 0x100), buf
, sizeof(buf
));
650 if (MF_DBGLEVEL
>= 3) DbpString("NESTED FINISHED");
652 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
654 iso14a_set_tracing(TRUE
);
657 //-----------------------------------------------------------------------------
658 // MIFARE check keys. key count up to 85.
660 //-----------------------------------------------------------------------------
661 void MifareChkKeys(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
664 uint8_t blockNo
= arg0
;
665 uint8_t keyType
= arg1
;
666 uint8_t keyCount
= arg2
;
667 uint64_t ui64Key
= 0;
674 struct Crypto1State mpcs
= {0, 0};
675 struct Crypto1State
*pcs
;
679 int OLD_MF_DBGLEVEL
= MF_DBGLEVEL
;
680 MF_DBGLEVEL
= MF_DBG_NONE
;
683 iso14a_clear_trace();
684 iso14a_set_tracing(TRUE
);
686 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
692 for (i
= 0; i
< keyCount
; i
++) {
693 if(mifare_classic_halt(pcs
, cuid
)) {
694 if (MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Halt error");
697 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
698 if (OLD_MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Can't select card");
702 ui64Key
= bytes_to_num(datain
+ i
* 6, 6);
703 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
711 // ----------------------------- crypto1 destroy
712 crypto1_destroy(pcs
);
715 cmd_send(CMD_ACK
,isOK
,0,0,datain
+ i
* 6,6);
718 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
721 // restore debug level
722 MF_DBGLEVEL
= OLD_MF_DBGLEVEL
;
725 //-----------------------------------------------------------------------------
726 // MIFARE commands set debug level
728 //-----------------------------------------------------------------------------
729 void MifareSetDbgLvl(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
731 Dbprintf("Debug level: %d", MF_DBGLEVEL
);
734 //-----------------------------------------------------------------------------
735 // Work with emulator memory
737 //-----------------------------------------------------------------------------
738 void MifareEMemClr(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
742 void MifareEMemSet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
743 emlSetMem(datain
, arg0
, arg1
); // data, block num, blocks count
746 void MifareEMemGet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
747 byte_t buf
[USB_CMD_DATA_SIZE
];
748 emlGetMem(buf
, arg0
, arg1
); // data, block num, blocks count (max 4)
751 cmd_send(CMD_ACK
,arg0
,arg1
,0,buf
,USB_CMD_DATA_SIZE
);
755 //-----------------------------------------------------------------------------
756 // Load a card into the emulator memory
758 //-----------------------------------------------------------------------------
759 void MifareECardLoad(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
760 uint8_t numSectors
= arg0
;
761 uint8_t keyType
= arg1
;
762 uint64_t ui64Key
= 0;
764 struct Crypto1State mpcs
= {0, 0};
765 struct Crypto1State
*pcs
;
769 byte_t dataoutbuf
[16];
770 byte_t dataoutbuf2
[16];
774 iso14a_clear_trace();
775 iso14a_set_tracing(false);
777 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
785 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
787 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
790 for (uint8_t sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
791 ui64Key
= emlGetKey(sectorNo
, keyType
);
793 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
795 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo
);
799 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_NESTED
)) {
801 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo
);
806 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
807 if(isOK
&& mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
)) {
809 if (MF_DBGLEVEL
>= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo
, blockNo
);
813 if (blockNo
< NumBlocksPerSector(sectorNo
) - 1) {
814 emlSetMem(dataoutbuf
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
815 } else { // sector trailer, keep the keys, set only the AC
816 emlGetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
817 memcpy(&dataoutbuf2
[6], &dataoutbuf
[6], 4);
818 emlSetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
825 if(mifare_classic_halt(pcs
, cuid
)) {
826 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
829 // ----------------------------- crypto1 destroy
830 crypto1_destroy(pcs
);
832 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
835 if (MF_DBGLEVEL
>= 2) DbpString("EMUL FILL SECTORS FINISHED");
840 //-----------------------------------------------------------------------------
841 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
843 //-----------------------------------------------------------------------------
844 void MifareCSetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
847 uint8_t needWipe
= arg0
;
848 // bit 0 - need get UID
850 // bit 2 - need HALT after sequence
851 // bit 3 - need init FPGA and field before sequence
852 // bit 4 - need reset FPGA and LED
853 uint8_t workFlags
= arg1
;
854 uint8_t blockNo
= arg2
;
857 uint8_t wupC1
[] = { 0x40 };
858 uint8_t wupC2
[] = { 0x43 };
859 uint8_t wipeC
[] = { 0x41 };
863 uint8_t uid
[10] = {0x00};
864 uint8_t d_block
[18] = {0x00};
867 uint8_t *receivedAnswer
= get_bigbufptr_recvrespbuf();
868 uint8_t *receivedAnswerPar
= receivedAnswer
+ MAX_FRAME_SIZE
;
870 // reset FPGA and LED
871 if (workFlags
& 0x08) {
876 iso14a_clear_trace();
877 iso14a_set_tracing(TRUE
);
878 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
884 if (workFlags
& 0x01) {
885 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
886 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
890 if(mifare_classic_halt(NULL
, cuid
)) {
891 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
898 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
899 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
900 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
904 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
905 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
906 if (MF_DBGLEVEL
>= 1) Dbprintf("wipeC error");
910 if(mifare_classic_halt(NULL
, cuid
)) {
911 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
917 if (workFlags
& 0x02) {
918 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
919 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
920 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
924 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
925 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
926 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
931 if ((mifare_sendcmd_short(NULL
, 0, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 1) || (receivedAnswer
[0] != 0x0a)) {
932 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send command error");
936 memcpy(d_block
, datain
, 16);
937 AppendCrc14443a(d_block
, 16);
939 ReaderTransmit(d_block
, sizeof(d_block
), NULL
);
940 if ((ReaderReceive(receivedAnswer
, receivedAnswerPar
) != 1) || (receivedAnswer
[0] != 0x0a)) {
941 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send data error");
945 if (workFlags
& 0x04) {
946 if (mifare_classic_halt(NULL
, cuid
)) {
947 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
957 cmd_send(CMD_ACK
,isOK
,0,0,uid
,4);
960 if ((workFlags
& 0x10) || (!isOK
)) {
961 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
967 void MifareCGetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
971 // bit 2 - need HALT after sequence
972 // bit 3 - need init FPGA and field before sequence
973 // bit 4 - need reset FPGA and LED
974 uint8_t workFlags
= arg0
;
975 uint8_t blockNo
= arg2
;
978 uint8_t wupC1
[] = { 0x40 };
979 uint8_t wupC2
[] = { 0x43 };
983 uint8_t data
[18] = {0x00};
986 uint8_t* receivedAnswer
= get_bigbufptr_recvrespbuf();
987 uint8_t *receivedAnswerPar
= receivedAnswer
+ MAX_FRAME_SIZE
;
989 if (workFlags
& 0x08) {
994 iso14a_clear_trace();
995 iso14a_set_tracing(TRUE
);
996 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1000 if (workFlags
& 0x02) {
1001 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1002 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1003 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1007 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1008 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1009 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1015 if ((mifare_sendcmd_short(NULL
, 0, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 18)) {
1016 if (MF_DBGLEVEL
>= 1) Dbprintf("read block send command error");
1019 memcpy(data
, receivedAnswer
, 18);
1021 if (workFlags
& 0x04) {
1022 if (mifare_classic_halt(NULL
, cuid
)) {
1023 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1033 cmd_send(CMD_ACK
,isOK
,0,0,data
,18);
1036 if ((workFlags
& 0x10) || (!isOK
)) {
1037 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1042 void MifareCIdent(){
1045 uint8_t wupC1
[] = { 0x40 };
1046 uint8_t wupC2
[] = { 0x43 };
1051 uint8_t* receivedAnswer
= get_bigbufptr_recvrespbuf();
1052 uint8_t *receivedAnswerPar
= receivedAnswer
+ MAX_FRAME_SIZE
;
1054 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1055 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1059 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1060 if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) {
1064 if (mifare_classic_halt(NULL
, 0)) {
1068 cmd_send(CMD_ACK
,isOK
,0,0,0,0);