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 memset(uid
, 0x44, 4);
131 LogTrace(uid
, 4, 0, 0, TRUE
);
133 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16);
138 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
143 //-----------------------------------------------------------------------------
144 // Select, Authenticate, Read a MIFARE tag.
145 // read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)
146 //-----------------------------------------------------------------------------
147 void MifareReadSector(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
150 uint8_t sectorNo
= arg0
;
151 uint8_t keyType
= arg1
;
152 uint64_t ui64Key
= 0;
153 ui64Key
= bytes_to_num(datain
, 6);
157 byte_t dataoutbuf
[16 * 16];
160 struct Crypto1State mpcs
= {0, 0};
161 struct Crypto1State
*pcs
;
165 iso14a_clear_trace();
166 // iso14a_set_tracing(false);
168 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
175 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
177 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
181 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
183 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
186 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
187 if(mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
+ 16 * blockNo
)) {
189 if (MF_DBGLEVEL
>= 1) Dbprintf("Read sector %2d block %2d error", sectorNo
, blockNo
);
194 if(mifare_classic_halt(pcs
, cuid
)) {
195 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
199 // ----------------------------- crypto1 destroy
200 crypto1_destroy(pcs
);
202 if (MF_DBGLEVEL
>= 2) DbpString("READ SECTOR FINISHED");
205 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16*NumBlocksPerSector(sectorNo
));
209 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
211 // iso14a_set_tracing(TRUE);
215 void MifareUReadCard(uint8_t arg0
, uint8_t *datain
)
218 uint8_t sectorNo
= arg0
;
222 byte_t dataoutbuf
[16 * 4];
227 iso14a_clear_trace();
228 // iso14a_set_tracing(false);
230 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
237 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
238 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
241 for(int sec
=0;sec
<16;sec
++){
242 if(mifare_ultra_readblock(cuid
, sectorNo
* 4 + sec
, dataoutbuf
+ 4 * sec
)) {
243 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block %d error",sec
);
247 if(mifare_ultra_halt(cuid
)) {
248 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
256 if (MF_DBGLEVEL
>= 2) DbpString("READ CARD FINISHED");
259 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,64);
263 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
269 //-----------------------------------------------------------------------------
270 // Select, Authenticate, Write a MIFARE tag.
272 //-----------------------------------------------------------------------------
273 void MifareWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
276 uint8_t blockNo
= arg0
;
277 uint8_t keyType
= arg1
;
278 uint64_t ui64Key
= 0;
279 byte_t blockdata
[16];
281 ui64Key
= bytes_to_num(datain
, 6);
282 memcpy(blockdata
, datain
+ 10, 16);
288 struct Crypto1State mpcs
= {0, 0};
289 struct Crypto1State
*pcs
;
293 iso14a_clear_trace();
294 // iso14a_set_tracing(false);
296 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
303 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
304 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
308 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
309 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
313 if(mifare_classic_writeblock(pcs
, cuid
, blockNo
, blockdata
)) {
314 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
318 if(mifare_classic_halt(pcs
, cuid
)) {
319 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
327 // ----------------------------- crypto1 destroy
328 crypto1_destroy(pcs
);
330 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
333 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
338 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
340 // iso14a_set_tracing(TRUE);
345 void MifareUWriteBlock(uint8_t arg0
, uint8_t *datain
)
348 uint8_t blockNo
= arg0
;
349 byte_t blockdata
[16];
351 memset(blockdata
,'\0',16);
352 memcpy(blockdata
, datain
,16);
360 iso14a_clear_trace();
361 // iso14a_set_tracing(false);
363 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
370 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
371 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
375 if(mifare_ultra_writeblock(cuid
, blockNo
, blockdata
)) {
376 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
380 if(mifare_ultra_halt(cuid
)) {
381 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
389 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
392 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
397 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
399 // iso14a_set_tracing(TRUE);
403 void MifareUWriteBlock_Special(uint8_t arg0
, uint8_t *datain
)
406 uint8_t blockNo
= arg0
;
409 memcpy(blockdata
, datain
,4);
417 iso14a_clear_trace();
418 // iso14a_set_tracing(false);
420 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
427 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
428 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
432 if(mifare_ultra_special_writeblock(cuid
, blockNo
, blockdata
)) {
433 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
437 if(mifare_ultra_halt(cuid
)) {
438 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
446 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
449 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
454 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
456 // iso14a_set_tracing(TRUE);
461 // Return 1 if the nonce is invalid else return 0
462 int valid_nonce(uint32_t Nt
, uint32_t NtEnc
, uint32_t Ks1
, byte_t
* parity
) {
463 return ((oddparity((Nt
>> 24) & 0xFF) == ((parity
[0]) ^ oddparity((NtEnc
>> 24) & 0xFF) ^ BIT(Ks1
,16))) & \
464 (oddparity((Nt
>> 16) & 0xFF) == ((parity
[1]) ^ oddparity((NtEnc
>> 16) & 0xFF) ^ BIT(Ks1
,8))) & \
465 (oddparity((Nt
>> 8) & 0xFF) == ((parity
[2]) ^ oddparity((NtEnc
>> 8) & 0xFF) ^ BIT(Ks1
,0)))) ? 1 : 0;
469 //-----------------------------------------------------------------------------
470 // MIFARE nested authentication.
472 //-----------------------------------------------------------------------------
473 void MifareNested(uint32_t arg0
, uint32_t arg1
, uint32_t calibrate
, uint8_t *datain
)
476 uint8_t blockNo
= arg0
& 0xff;
477 uint8_t keyType
= (arg0
>> 8) & 0xff;
478 uint8_t targetBlockNo
= arg1
& 0xff;
479 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
480 uint64_t ui64Key
= 0;
482 ui64Key
= bytes_to_num(datain
, 6);
485 uint16_t rtr
, i
, j
, len
;
487 static uint16_t dmin
, dmax
;
489 uint32_t cuid
, nt1
, nt2
, nttmp
, nttest
, par
, ks1
;
490 uint32_t target_nt
[2], target_ks
[2];
492 uint8_t par_array
[4];
494 struct Crypto1State mpcs
= {0, 0};
495 struct Crypto1State
*pcs
;
497 uint8_t* receivedAnswer
= mifare_get_bigbufptr();
499 uint32_t auth1_time
, auth2_time
;
500 static uint16_t delta_time
;
503 iso14a_clear_trace();
504 iso14a_set_tracing(false);
506 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
512 // statistics on nonce distance
513 if (calibrate
) { // for first call only. Otherwise reuse previous calibration
520 for (rtr
= 0; rtr
< 17; rtr
++) {
522 // prepare next select. No need to power down the card.
523 if(mifare_classic_halt(pcs
, cuid
)) {
524 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
529 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
530 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
536 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
537 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
543 auth2_time
= auth1_time
+ delta_time
;
547 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_NESTED
, &nt2
, &auth2_time
)) {
548 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error");
553 nttmp
= prng_successor(nt1
, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160
554 for (i
= 101; i
< 1200; i
++) {
555 nttmp
= prng_successor(nttmp
, 1);
556 if (nttmp
== nt2
) break;
566 delta_time
= auth2_time
- auth1_time
+ 32; // allow some slack for proper timing
568 if (MF_DBGLEVEL
>= 3) Dbprintf("Nested: calibrating... ntdist=%d", i
);
572 if (rtr
<= 1) return;
574 davg
= (davg
+ (rtr
- 1)/2) / (rtr
- 1);
576 if (MF_DBGLEVEL
>= 3) Dbprintf("min=%d max=%d avg=%d, delta_time=%d", dmin
, dmax
, davg
, delta_time
);
584 // -------------------------------------------------------------------------------------------------
588 // get crypted nonces for target sector
589 for(i
=0; i
< 2; i
++) { // look for exactly two different nonces
592 while(target_nt
[i
] == 0) { // continue until we have an unambiguous nonce
594 // prepare next select. No need to power down the card.
595 if(mifare_classic_halt(pcs
, cuid
)) {
596 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
600 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
601 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
606 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
607 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
611 // nested authentication
612 auth2_time
= auth1_time
+ delta_time
;
613 len
= mifare_sendcmd_shortex(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType
& 0x01), targetBlockNo
, receivedAnswer
, &par
, &auth2_time
);
615 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error len=%d", len
);
619 nt2
= bytes_to_num(receivedAnswer
, 4);
620 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i
+1, nt1
, nt2
, par
);
622 // Parity validity check
623 for (j
= 0; j
< 4; j
++) {
624 par_array
[j
] = (oddparity(receivedAnswer
[j
]) != ((par
& 0x08) >> 3));
629 nttest
= prng_successor(nt1
, dmin
- 1);
630 for (j
= dmin
; j
< dmax
+ 1; j
++) {
631 nttest
= prng_successor(nttest
, 1);
634 if (valid_nonce(nttest
, nt2
, ks1
, par_array
)){
635 if (ncount
> 0) { // we are only interested in disambiguous nonces, try again
636 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i
+1, j
);
640 target_nt
[i
] = nttest
;
643 if (i
== 1 && target_nt
[1] == target_nt
[0]) { // we need two different nonces
645 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j
);
648 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i
+1, j
);
651 if (target_nt
[i
] == 0 && j
== dmax
+1 && MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i
+1);
657 // ----------------------------- crypto1 destroy
658 crypto1_destroy(pcs
);
661 memset(uid
, 0x44, 4);
662 LogTrace(uid
, 4, 0, 0, TRUE
);
664 byte_t buf
[4 + 4 * 4];
665 memcpy(buf
, &cuid
, 4);
666 memcpy(buf
+4, &target_nt
[0], 4);
667 memcpy(buf
+8, &target_ks
[0], 4);
668 memcpy(buf
+12, &target_nt
[1], 4);
669 memcpy(buf
+16, &target_ks
[1], 4);
672 cmd_send(CMD_ACK
, 0, 2, targetBlockNo
+ (targetKeyType
* 0x100), buf
, sizeof(buf
));
675 if (MF_DBGLEVEL
>= 3) DbpString("NESTED FINISHED");
677 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
679 iso14a_set_tracing(TRUE
);
682 //-----------------------------------------------------------------------------
683 // MIFARE check keys. key count up to 85.
685 //-----------------------------------------------------------------------------
686 void MifareChkKeys(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
689 uint8_t blockNo
= arg0
;
690 uint8_t keyType
= arg1
;
691 uint8_t keyCount
= arg2
;
692 uint64_t ui64Key
= 0;
699 struct Crypto1State mpcs
= {0, 0};
700 struct Crypto1State
*pcs
;
704 int OLD_MF_DBGLEVEL
= MF_DBGLEVEL
;
705 MF_DBGLEVEL
= MF_DBG_NONE
;
708 iso14a_clear_trace();
709 iso14a_set_tracing(TRUE
);
711 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
717 for (i
= 0; i
< keyCount
; i
++) {
718 if(mifare_classic_halt(pcs
, cuid
)) {
719 if (MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Halt error");
722 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
723 if (OLD_MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Can't select card");
727 ui64Key
= bytes_to_num(datain
+ i
* 6, 6);
728 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
736 // ----------------------------- crypto1 destroy
737 crypto1_destroy(pcs
);
740 cmd_send(CMD_ACK
,isOK
,0,0,datain
+ i
* 6,6);
744 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
747 // restore debug level
748 MF_DBGLEVEL
= OLD_MF_DBGLEVEL
;
751 //-----------------------------------------------------------------------------
752 // MIFARE commands set debug level
754 //-----------------------------------------------------------------------------
755 void MifareSetDbgLvl(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
757 Dbprintf("Debug level: %d", MF_DBGLEVEL
);
761 //-----------------------------------------------------------------------------
762 // Work with emulator memory
764 //-----------------------------------------------------------------------------
765 void MifareEMemClr(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
770 void MifareEMemSet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
771 emlSetMem(datain
, arg0
, arg1
); // data, block num, blocks count
775 void MifareEMemGet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
778 emlGetMem(buf
, arg0
, arg1
); // data, block num, blocks count (max 4)
781 cmd_send(CMD_ACK
,arg0
,arg1
,0,buf
,48);
786 //-----------------------------------------------------------------------------
787 // Load a card into the emulator memory
789 //-----------------------------------------------------------------------------
790 void MifareECardLoad(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
791 uint8_t numSectors
= arg0
;
792 uint8_t keyType
= arg1
;
793 uint64_t ui64Key
= 0;
795 struct Crypto1State mpcs
= {0, 0};
796 struct Crypto1State
*pcs
;
800 byte_t dataoutbuf
[16];
801 byte_t dataoutbuf2
[16];
805 iso14a_clear_trace();
806 iso14a_set_tracing(false);
808 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
816 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
818 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
821 for (uint8_t sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
822 ui64Key
= emlGetKey(sectorNo
, keyType
);
824 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
826 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo
);
830 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_NESTED
)) {
832 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo
);
837 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
838 if(isOK
&& mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
)) {
840 if (MF_DBGLEVEL
>= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo
, blockNo
);
844 if (blockNo
< NumBlocksPerSector(sectorNo
) - 1) {
845 emlSetMem(dataoutbuf
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
846 } else { // sector trailer, keep the keys, set only the AC
847 emlGetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
848 memcpy(&dataoutbuf2
[6], &dataoutbuf
[6], 4);
849 emlSetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
856 if(mifare_classic_halt(pcs
, cuid
)) {
857 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
860 // ----------------------------- crypto1 destroy
861 crypto1_destroy(pcs
);
863 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
866 if (MF_DBGLEVEL
>= 2) DbpString("EMUL FILL SECTORS FINISHED");
871 //-----------------------------------------------------------------------------
872 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
874 //-----------------------------------------------------------------------------
875 void MifareCSetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
878 uint8_t needWipe
= arg0
;
879 // bit 0 - need get UID
881 // bit 2 - need HALT after sequence
882 // bit 3 - need init FPGA and field before sequence
883 // bit 4 - need reset FPGA and LED
884 uint8_t workFlags
= arg1
;
885 uint8_t blockNo
= arg2
;
888 uint8_t wupC1
[] = { 0x40 };
889 uint8_t wupC2
[] = { 0x43 };
890 uint8_t wipeC
[] = { 0x41 };
898 memset(uid
, 0x00, 10);
899 uint8_t* receivedAnswer
= mifare_get_bigbufptr();
901 if (workFlags
& 0x08) {
903 iso14a_clear_trace();
904 iso14a_set_tracing(TRUE
);
906 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
913 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
915 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
920 if (workFlags
& 0x01) {
921 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
922 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
926 if(mifare_classic_halt(NULL
, cuid
)) {
927 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
934 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
935 if(!ReaderReceive(receivedAnswer
) || (receivedAnswer
[0] != 0x0a)) {
936 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
940 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
941 if(!ReaderReceive(receivedAnswer
) || (receivedAnswer
[0] != 0x0a)) {
942 if (MF_DBGLEVEL
>= 1) Dbprintf("wipeC error");
946 if(mifare_classic_halt(NULL
, cuid
)) {
947 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
953 if (workFlags
& 0x02) {
954 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
955 if(!ReaderReceive(receivedAnswer
) || (receivedAnswer
[0] != 0x0a)) {
956 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
960 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
961 if(!ReaderReceive(receivedAnswer
) || (receivedAnswer
[0] != 0x0a)) {
962 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
967 if ((mifare_sendcmd_short(NULL
, 0, 0xA0, blockNo
, receivedAnswer
, NULL
) != 1) || (receivedAnswer
[0] != 0x0a)) {
968 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send command error");
972 memcpy(d_block
, datain
, 16);
973 AppendCrc14443a(d_block
, 16);
975 ReaderTransmit(d_block
, sizeof(d_block
), NULL
);
976 if ((ReaderReceive(receivedAnswer
) != 1) || (receivedAnswer
[0] != 0x0a)) {
977 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send data error");
981 if (workFlags
& 0x04) {
982 if (mifare_classic_halt(NULL
, cuid
)) {
983 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
993 cmd_send(CMD_ACK
,isOK
,0,0,uid
,4);
996 if ((workFlags
& 0x10) || (!isOK
)) {
998 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1004 void MifareCGetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1007 // bit 1 - need wupC
1008 // bit 2 - need HALT after sequence
1009 // bit 3 - need init FPGA and field before sequence
1010 // bit 4 - need reset FPGA and LED
1011 uint8_t workFlags
= arg0
;
1012 uint8_t blockNo
= arg2
;
1015 uint8_t wupC1
[] = { 0x40 };
1016 uint8_t wupC2
[] = { 0x43 };
1023 memset(data
, 0x00, 18);
1024 uint8_t* receivedAnswer
= mifare_get_bigbufptr();
1026 if (workFlags
& 0x08) {
1028 iso14a_clear_trace();
1029 iso14a_set_tracing(TRUE
);
1031 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1038 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1040 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1044 if (workFlags
& 0x02) {
1045 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1046 if(!ReaderReceive(receivedAnswer
) || (receivedAnswer
[0] != 0x0a)) {
1047 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1051 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1052 if(!ReaderReceive(receivedAnswer
) || (receivedAnswer
[0] != 0x0a)) {
1053 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1059 if ((mifare_sendcmd_short(NULL
, 0, 0x30, blockNo
, receivedAnswer
, NULL
) != 18)) {
1060 if (MF_DBGLEVEL
>= 1) Dbprintf("read block send command error");
1063 memcpy(data
, receivedAnswer
, 18);
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,data
,18);
1080 if ((workFlags
& 0x10) || (!isOK
)) {
1082 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);