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, Authenticaate, Read an 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);
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");
79 LogTrace(uid
, 4, 0, 0, TRUE
);
81 // UsbCommand ack = {CMD_ACK, {isOK, 0, 0}};
82 // memcpy(ack.d.asBytes, dataoutbuf, 16);
85 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16);
86 // UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));
91 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
93 // iso14a_set_tracing(TRUE);
97 void MifareUReadBlock(uint8_t arg0
,uint8_t *datain
)
100 uint8_t blockNo
= arg0
;
104 byte_t dataoutbuf
[16];
109 iso14a_clear_trace();
117 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
118 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
122 if(mifare_ultra_readblock(cuid
, blockNo
, dataoutbuf
)) {
123 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block error");
127 if(mifare_ultra_halt(cuid
)) {
128 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
136 if (MF_DBGLEVEL
>= 2) DbpString("READ BLOCK FINISHED");
139 memset(uid
, 0x44, 4);
140 LogTrace(uid
, 4, 0, 0, TRUE
);
142 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16);
147 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
151 //-----------------------------------------------------------------------------
152 // Select, Authenticaate, Read an MIFARE tag.
153 // read sector (data = 4 x 16 bytes = 64 bytes)
154 //-----------------------------------------------------------------------------
155 void MifareReadSector(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
158 uint8_t sectorNo
= arg0
;
159 uint8_t keyType
= arg1
;
160 uint64_t ui64Key
= 0;
161 ui64Key
= bytes_to_num(datain
, 6);
165 byte_t dataoutbuf
[16 * 4];
168 struct Crypto1State mpcs
= {0, 0};
169 struct Crypto1State
*pcs
;
173 iso14a_clear_trace();
174 // iso14a_set_tracing(false);
183 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
184 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
188 if(mifare_classic_auth(pcs
, cuid
, sectorNo
* 4, keyType
, ui64Key
, AUTH_FIRST
)) {
189 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
193 if(mifare_classic_readblock(pcs
, cuid
, sectorNo
* 4 + 0, dataoutbuf
+ 16 * 0)) {
194 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block 0 error");
197 if(mifare_classic_readblock(pcs
, cuid
, sectorNo
* 4 + 1, dataoutbuf
+ 16 * 1)) {
198 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block 1 error");
201 if(mifare_classic_readblock(pcs
, cuid
, sectorNo
* 4 + 2, dataoutbuf
+ 16 * 2)) {
202 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block 2 error");
205 if(mifare_classic_readblock(pcs
, cuid
, sectorNo
* 4 + 3, dataoutbuf
+ 16 * 3)) {
206 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block 3 error");
210 if(mifare_classic_halt(pcs
, cuid
)) {
211 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
219 // ----------------------------- crypto1 destroy
220 crypto1_destroy(pcs
);
222 if (MF_DBGLEVEL
>= 2) DbpString("READ SECTOR FINISHED");
225 memset(uid
, 0x44, 4);
226 LogTrace(uid
, 4, 0, 0, TRUE
);
228 // UsbCommand ack = {CMD_ACK, {isOK, 0, 0}};
229 // memcpy(ack.d.asBytes, dataoutbuf, 16 * 2);
232 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,32);
233 // UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));
236 // memcpy(ack.d.asBytes, dataoutbuf + 16 * 2, 16 * 2);
237 // UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));
238 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
+32, 32);
242 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
244 // iso14a_set_tracing(TRUE);
248 void MifareUReadCard(uint8_t arg0
, uint8_t *datain
)
251 uint8_t sectorNo
= arg0
;
255 byte_t dataoutbuf
[16 * 4];
260 iso14a_clear_trace();
261 // iso14a_set_tracing(false);
270 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
271 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
274 for(int sec
=0;sec
<16;sec
++){
275 if(mifare_ultra_readblock(cuid
, sectorNo
* 4 + sec
, dataoutbuf
+ 4 * sec
)) {
276 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block %d error",sec
);
280 if(mifare_ultra_halt(cuid
)) {
281 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
289 if (MF_DBGLEVEL
>= 2) DbpString("READ CARD FINISHED");
292 memset(uid
, 0x44, 4);
293 LogTrace(uid
, 4, 0, 0, TRUE
);
296 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,64);
297 //cmd_send(CMD_ACK,isOK,0,0,dataoutbuf+32, 32);
301 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
303 // iso14a_set_tracing(TRUE);
308 //-----------------------------------------------------------------------------
309 // Select, Authenticaate, Read an MIFARE tag.
311 //-----------------------------------------------------------------------------
312 void MifareWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
315 uint8_t blockNo
= arg0
;
316 uint8_t keyType
= arg1
;
317 uint64_t ui64Key
= 0;
318 byte_t blockdata
[16];
320 ui64Key
= bytes_to_num(datain
, 6);
321 memcpy(blockdata
, datain
+ 10, 16);
327 struct Crypto1State mpcs
= {0, 0};
328 struct Crypto1State
*pcs
;
332 iso14a_clear_trace();
333 // iso14a_set_tracing(false);
342 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
343 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
347 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
348 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
352 if(mifare_classic_writeblock(pcs
, cuid
, blockNo
, blockdata
)) {
353 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
357 if(mifare_classic_halt(pcs
, cuid
)) {
358 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
366 // ----------------------------- crypto1 destroy
367 crypto1_destroy(pcs
);
369 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
372 memset(uid
, 0x44, 4);
373 LogTrace(uid
, 4, 0, 0, TRUE
);
375 // UsbCommand ack = {CMD_ACK, {isOK, 0, 0}};
378 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
379 // UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));
384 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
386 // iso14a_set_tracing(TRUE);
390 void MifareUWriteBlock(uint8_t arg0
, uint8_t *datain
)
393 uint8_t blockNo
= arg0
;
394 byte_t blockdata
[16];
396 memset(blockdata
,'\0',16);
397 memcpy(blockdata
, datain
,16);
405 iso14a_clear_trace();
406 // iso14a_set_tracing(false);
415 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
416 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
420 if(mifare_ultra_writeblock(cuid
, blockNo
, blockdata
)) {
421 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
425 if(mifare_ultra_halt(cuid
)) {
426 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
434 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
437 memset(uid
, 0x44, 4);
438 LogTrace(uid
, 4, 0, 0, TRUE
);
441 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
442 // UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));
447 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
449 // iso14a_set_tracing(TRUE);
453 void MifareUWriteBlock_Special(uint8_t arg0
, uint8_t *datain
)
456 uint8_t blockNo
= arg0
;
459 memcpy(blockdata
, datain
,4);
467 iso14a_clear_trace();
468 // iso14a_set_tracing(false);
477 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
478 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
482 if(mifare_ultra_special_writeblock(cuid
, blockNo
, blockdata
)) {
483 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
487 if(mifare_ultra_halt(cuid
)) {
488 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
496 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
499 memset(uid
, 0x44, 4);
500 LogTrace(uid
, 4, 0, 0, TRUE
);
503 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
504 // UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));
509 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
511 // iso14a_set_tracing(TRUE);
515 // Return 1 if the nonce is invalid else return 0
516 int valid_nonce(uint32_t Nt
, uint32_t NtEnc
, uint32_t Ks1
, byte_t
* parity
) {
517 return ((oddparity((Nt
>> 24) & 0xFF) == ((parity
[0]) ^ oddparity((NtEnc
>> 24) & 0xFF) ^ BIT(Ks1
,16))) & \
518 (oddparity((Nt
>> 16) & 0xFF) == ((parity
[1]) ^ oddparity((NtEnc
>> 16) & 0xFF) ^ BIT(Ks1
,8))) & \
519 (oddparity((Nt
>> 8) & 0xFF) == ((parity
[2]) ^ oddparity((NtEnc
>> 8) & 0xFF) ^ BIT(Ks1
,0)))) ? 1 : 0;
524 //-----------------------------------------------------------------------------
525 // MIFARE nested authentication.
527 //-----------------------------------------------------------------------------
528 void MifareNested(uint32_t arg0
, uint32_t arg1
, uint32_t calibrate
, uint8_t *datain
)
531 uint8_t blockNo
= arg0
& 0xff;
532 uint8_t keyType
= (arg0
>> 8) & 0xff;
533 uint8_t targetBlockNo
= arg1
& 0xff;
534 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
535 uint64_t ui64Key
= 0;
537 ui64Key
= bytes_to_num(datain
, 6);
540 uint16_t rtr
, i
, j
, len
;
542 static uint16_t dmin
, dmax
;
544 uint32_t cuid
, nt1
, nt2
, nttmp
, nttest
, par
, ks1
;
545 uint32_t target_nt
[2], target_ks
[2];
547 uint8_t par_array
[4];
549 struct Crypto1State mpcs
= {0, 0};
550 struct Crypto1State
*pcs
;
552 uint8_t* receivedAnswer
= mifare_get_bigbufptr();
554 uint32_t auth1_time
, auth2_time
;
555 static uint16_t delta_time
;
560 iso14a_clear_trace();
561 iso14a_set_tracing(false);
569 while((GetCountMifare() & 0xffff0000) != 0x00010000); // wait for counter to reset and "warm up"
571 // statistics on nonce distance
572 if (calibrate
) { // for first call only. Otherwise reuse previous calibration
579 for (rtr
= 0; rtr
< 17; rtr
++) {
581 // prepare next select. No need to power down the card.
582 if(mifare_classic_halt(pcs
, cuid
)) {
583 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
588 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
589 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
595 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
596 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
602 auth2_time
= auth1_time
+ delta_time
;
606 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_NESTED
, &nt2
, &auth2_time
)) {
607 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error");
612 nttmp
= prng_successor(nt1
, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160
613 for (i
= 101; i
< 1200; i
++) {
614 nttmp
= prng_successor(nttmp
, 1);
615 if (nttmp
== nt2
) break;
625 delta_time
= auth2_time
- auth1_time
+ 32; // allow some slack for proper timing
627 if (MF_DBGLEVEL
>= 3) Dbprintf("Nested: calibrating... ntdist=%d", i
);
631 if (rtr
<= 1) return;
633 davg
= (davg
+ (rtr
- 1)/2) / (rtr
- 1);
635 if (MF_DBGLEVEL
>= 3) Dbprintf("min=%d max=%d avg=%d, delta_time=%d", dmin
, dmax
, davg
, delta_time
);
643 // -------------------------------------------------------------------------------------------------
647 // get crypted nonces for target sector
648 for(i
=0; i
< 2; i
++) { // look for exactly two different nonces
651 while(target_nt
[i
] == 0) { // continue until we have an unambiguous nonce
653 // prepare next select. No need to power down the card.
654 if(mifare_classic_halt(pcs
, cuid
)) {
655 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
659 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
660 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
665 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
666 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
670 // nested authentication
671 auth2_time
= auth1_time
+ delta_time
;
672 len
= mifare_sendcmd_shortex(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType
& 0x01), targetBlockNo
, receivedAnswer
, &par
, &auth2_time
);
674 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error len=%d", len
);
678 nt2
= bytes_to_num(receivedAnswer
, 4);
679 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i
+1, nt1
, nt2
, par
);
681 // Parity validity check
682 for (j
= 0; j
< 4; j
++) {
683 par_array
[j
] = (oddparity(receivedAnswer
[j
]) != ((par
& 0x08) >> 3));
688 nttest
= prng_successor(nt1
, dmin
- 1);
689 for (j
= dmin
; j
< dmax
+ 1; j
++) {
690 nttest
= prng_successor(nttest
, 1);
693 if (valid_nonce(nttest
, nt2
, ks1
, par_array
)){
694 if (ncount
> 0) { // we are only interested in disambiguous nonces, try again
695 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i
+1, j
);
699 target_nt
[i
] = nttest
;
702 if (i
== 1 && target_nt
[1] == target_nt
[0]) { // we need two different nonces
704 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j
);
707 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i
+1, j
);
710 if (target_nt
[i
] == 0 && j
== dmax
+1 && MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i
+1);
716 // ----------------------------- crypto1 destroy
717 crypto1_destroy(pcs
);
720 memset(uid
, 0x44, 4);
721 LogTrace(uid
, 4, 0, 0, TRUE
);
723 byte_t buf
[4 + 4 * 4];
724 memcpy(buf
, &cuid
, 4);
725 memcpy(buf
+4, &target_nt
[0], 4);
726 memcpy(buf
+8, &target_ks
[0], 4);
727 memcpy(buf
+12, &target_nt
[1], 4);
728 memcpy(buf
+16, &target_ks
[1], 4);
731 cmd_send(CMD_ACK
, 0, 2, targetBlockNo
+ (targetKeyType
* 0x100), buf
, sizeof(buf
));
734 if (MF_DBGLEVEL
>= 3) DbpString("NESTED FINISHED");
736 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
738 iso14a_set_tracing(TRUE
);
741 //-----------------------------------------------------------------------------
742 // MIFARE check keys. key count up to 85.
744 //-----------------------------------------------------------------------------
745 void MifareChkKeys(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
748 uint8_t blockNo
= arg0
;
749 uint8_t keyType
= arg1
;
750 uint8_t keyCount
= arg2
;
751 uint64_t ui64Key
= 0;
758 struct Crypto1State mpcs
= {0, 0};
759 struct Crypto1State
*pcs
;
763 int OLD_MF_DBGLEVEL
= MF_DBGLEVEL
;
764 MF_DBGLEVEL
= MF_DBG_NONE
;
767 iso14a_clear_trace();
768 iso14a_set_tracing(TRUE
);
777 for (i
= 0; i
< keyCount
; i
++) {
778 // FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
780 // FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_MOD);
781 // prepare next select by sending a HALT. There is no need to power down the card.
782 if(mifare_classic_halt(pcs
, cuid
)) {
783 if (MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Halt error");
788 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
789 if (OLD_MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Can't select card");
793 ui64Key
= bytes_to_num(datain
+ i
* 6, 6);
794 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
802 // ----------------------------- crypto1 destroy
803 crypto1_destroy(pcs
);
806 memset(uid
, 0x44, 4);
807 LogTrace(uid
, 4, 0, 0, TRUE
);
810 cmd_send(CMD_ACK
,isOK
,0,0,datain
+ i
* 6,6);
814 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
817 // restore debug level
818 MF_DBGLEVEL
= OLD_MF_DBGLEVEL
;
821 //-----------------------------------------------------------------------------
822 // MIFARE commands set debug level
824 //-----------------------------------------------------------------------------
825 void MifareSetDbgLvl(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
827 Dbprintf("Debug level: %d", MF_DBGLEVEL
);
830 //-----------------------------------------------------------------------------
831 // Work with emulator memory
833 //-----------------------------------------------------------------------------
834 void MifareEMemClr(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
838 void MifareEMemSet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
839 emlSetMem(datain
, arg0
, arg1
); // data, block num, blocks count
842 void MifareEMemGet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
843 // UsbCommand ack = {CMD_ACK, {arg0, arg1, 0}};
846 emlGetMem(buf
, arg0
, arg1
); // data, block num, blocks count
849 cmd_send(CMD_ACK
,arg0
,arg1
,0,buf
,48);
850 // UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));
854 //-----------------------------------------------------------------------------
855 // Load a card into the emulator memory
857 //-----------------------------------------------------------------------------
858 void MifareECardLoad(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
860 uint8_t sectorNo
= 0;
861 uint8_t keyType
= arg1
;
862 uint64_t ui64Key
= 0;
864 struct Crypto1State mpcs
= {0, 0};
865 struct Crypto1State
*pcs
;
869 byte_t dataoutbuf
[16];
870 byte_t dataoutbuf2
[16];
874 iso14a_clear_trace();
875 iso14a_set_tracing(false);
884 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
885 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
889 for (i
= 0; i
< 16; i
++) {
891 ui64Key
= emlGetKey(sectorNo
, keyType
);
894 if(mifare_classic_auth(pcs
, cuid
, sectorNo
* 4, keyType
, ui64Key
, AUTH_FIRST
)) {
895 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%d]. Auth error", i
);
899 if(mifare_classic_auth(pcs
, cuid
, sectorNo
* 4, keyType
, ui64Key
, AUTH_NESTED
)) {
900 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%d]. Auth nested error", i
);
905 if(mifare_classic_readblock(pcs
, cuid
, sectorNo
* 4 + 0, dataoutbuf
)) {
906 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block 0 error");
909 emlSetMem(dataoutbuf
, sectorNo
* 4 + 0, 1);
911 if(mifare_classic_readblock(pcs
, cuid
, sectorNo
* 4 + 1, dataoutbuf
)) {
912 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block 1 error");
915 emlSetMem(dataoutbuf
, sectorNo
* 4 + 1, 1);
917 if(mifare_classic_readblock(pcs
, cuid
, sectorNo
* 4 + 2, dataoutbuf
)) {
918 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block 2 error");
921 emlSetMem(dataoutbuf
, sectorNo
* 4 + 2, 1);
923 // get block 3 bytes 6-9
924 if(mifare_classic_readblock(pcs
, cuid
, sectorNo
* 4 + 3, dataoutbuf
)) {
925 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block 3 error");
928 emlGetMem(dataoutbuf2
, sectorNo
* 4 + 3, 1);
929 memcpy(&dataoutbuf2
[6], &dataoutbuf
[6], 4);
930 emlSetMem(dataoutbuf2
, sectorNo
* 4 + 3, 1);
933 if(mifare_classic_halt(pcs
, cuid
)) {
934 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
941 // ----------------------------- crypto1 destroy
942 crypto1_destroy(pcs
);
944 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
947 if (MF_DBGLEVEL
>= 2) DbpString("EMUL FILL SECTORS FINISHED");
950 memset(uid
, 0x44, 4);
951 LogTrace(uid
, 4, 0, 0, TRUE
);
954 //-----------------------------------------------------------------------------
955 // MIFARE 1k emulator
957 //-----------------------------------------------------------------------------
960 //-----------------------------------------------------------------------------
961 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
963 //-----------------------------------------------------------------------------
964 void MifareCSetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
967 uint8_t needWipe
= arg0
;
968 // bit 0 - need get UID
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
= arg1
;
974 uint8_t blockNo
= arg2
;
977 uint8_t wupC1
[] = { 0x40 };
978 uint8_t wupC2
[] = { 0x43 };
979 uint8_t wipeC
[] = { 0x41 };
987 memset(uid
, 0x00, 10);
988 uint8_t* receivedAnswer
= mifare_get_bigbufptr();
990 if (workFlags
& 0x08) {
992 iso14a_clear_trace();
993 iso14a_set_tracing(TRUE
);
1002 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1004 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1008 // get UID from chip
1009 if (workFlags
& 0x01) {
1010 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
1011 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
1015 if(mifare_classic_halt(NULL
, cuid
)) {
1016 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1023 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1024 if(!ReaderReceive(receivedAnswer
) || (receivedAnswer
[0] != 0x0a)) {
1025 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1029 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
1030 if(!ReaderReceive(receivedAnswer
) || (receivedAnswer
[0] != 0x0a)) {
1031 if (MF_DBGLEVEL
>= 1) Dbprintf("wipeC error");
1035 if(mifare_classic_halt(NULL
, cuid
)) {
1036 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1042 if (workFlags
& 0x02) {
1043 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1044 if(!ReaderReceive(receivedAnswer
) || (receivedAnswer
[0] != 0x0a)) {
1045 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1049 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1050 if(!ReaderReceive(receivedAnswer
) || (receivedAnswer
[0] != 0x0a)) {
1051 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1056 if ((mifare_sendcmd_short(NULL
, 0, 0xA0, blockNo
, receivedAnswer
, NULL
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1057 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send command error");
1061 memcpy(d_block
, datain
, 16);
1062 AppendCrc14443a(d_block
, 16);
1064 ReaderTransmit(d_block
, sizeof(d_block
), NULL
);
1065 if ((ReaderReceive(receivedAnswer
) != 1) || (receivedAnswer
[0] != 0x0a)) {
1066 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send data error");
1070 if (workFlags
& 0x04) {
1071 if (mifare_classic_halt(NULL
, cuid
)) {
1072 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1081 // UsbCommand ack = {CMD_ACK, {isOK, 0, 0}};
1082 // if (isOK) memcpy(ack.d.asBytes, uid, 4);
1084 // add trace trailer
1086 * Removed by Martin, the uid is overwritten with 0x44,
1087 * which can 't be intended.
1089 * memset(uid, 0x44, 4);
1090 * LogTrace(uid, 4, 0, 0, TRUE);
1095 cmd_send(CMD_ACK
,isOK
,0,0,uid
,4);
1096 // UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));
1099 if ((workFlags
& 0x10) || (!isOK
)) {
1101 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1106 void MifareCGetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
1109 // bit 1 - need wupC
1110 // bit 2 - need HALT after sequence
1111 // bit 3 - need init FPGA and field before sequence
1112 // bit 4 - need reset FPGA and LED
1113 uint8_t workFlags
= arg0
;
1114 uint8_t blockNo
= arg2
;
1117 uint8_t wupC1
[] = { 0x40 };
1118 uint8_t wupC2
[] = { 0x43 };
1125 memset(data
, 0x00, 18);
1126 uint8_t* receivedAnswer
= mifare_get_bigbufptr();
1128 if (workFlags
& 0x08) {
1130 iso14a_clear_trace();
1131 iso14a_set_tracing(TRUE
);
1140 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1142 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1146 if (workFlags
& 0x02) {
1147 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1148 if(!ReaderReceive(receivedAnswer
) || (receivedAnswer
[0] != 0x0a)) {
1149 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1153 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1154 if(!ReaderReceive(receivedAnswer
) || (receivedAnswer
[0] != 0x0a)) {
1155 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1161 if ((mifare_sendcmd_short(NULL
, 0, 0x30, blockNo
, receivedAnswer
, NULL
) != 18)) {
1162 if (MF_DBGLEVEL
>= 1) Dbprintf("read block send command error");
1165 memcpy(data
, receivedAnswer
, 18);
1167 if (workFlags
& 0x04) {
1168 if (mifare_classic_halt(NULL
, cuid
)) {
1169 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1178 // UsbCommand ack = {CMD_ACK, {isOK, 0, 0}};
1179 // if (isOK) memcpy(ack.d.asBytes, data, 18);
1181 // add trace trailer
1183 * Removed by Martin, this piece of overwrites the 'data' variable
1184 * which is sent two lines down, and is obviously not correct.
1186 * memset(data, 0x44, 4);
1187 * LogTrace(data, 4, 0, 0, TRUE);
1190 cmd_send(CMD_ACK
,isOK
,0,0,data
,18);
1191 // UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));
1194 if ((workFlags
& 0x10) || (!isOK
)) {
1196 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);