1 //-----------------------------------------------------------------------------
2 // Jonathan Westhues, split Nov 2006
3 // Modified by Greg Jones, Jan 2009
4 // Modified by Adrian Dabrowski "atrox", Mar-Sept 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 15693. This includes both the reader software and
11 // the `fake tag' modes, but at the moment I've implemented only the reader
12 // stuff, and that barely.
13 // Modified to perform modulation onboard in arm rather than on PC
14 // Also added additional reader commands (SELECT, READ etc.)
15 //-----------------------------------------------------------------------------
16 // The ISO 15693 describes two transmission modes from reader to tag, and 4
17 // transmission modes from tag to reader. As of Mar 2010 this code only
18 // supports one of each: "1of4" mode from reader to tag, and the highspeed
19 // variant with one subcarrier from card to reader.
20 // As long, as the card fully support ISO 15693 this is no problem, since the
21 // reader chooses both data rates, but some non-standard tags do not. Further for
22 // the simulation to work, we will need to support all data rates.
24 // VCD (reader) -> VICC (tag)
26 // data rate: 1,66 kbit/s (fc/8192)
27 // used for long range
29 // data rate: 26,48 kbit/s (fc/512)
30 // used for short range, high speed
32 // VICC (tag) -> VCD (reader)
34 // ASK / one subcarrier (423,75 khz)
35 // FSK / two subcarriers (423,75 khz && 484,28 khz)
36 // Data Rates / Modes:
37 // low ASK: 6,62 kbit/s
38 // low FSK: 6.67 kbit/s
39 // high ASK: 26,48 kbit/s
40 // high FSK: 26,69 kbit/s
41 //-----------------------------------------------------------------------------
42 // added "1 out of 256" mode (for VCD->PICC) - atrox 20100911
46 // *) UID is always used "transmission order" (LSB), which is reverse to display order
48 // TODO / BUGS / ISSUES:
49 // *) writing to tags takes longer: we miss the answer from the tag in most cases
50 // -> tweak the read-timeout times
51 // *) signal decoding from the card is still a bit shaky.
52 // *) signal decoding is unable to detect collissions.
53 // *) add anti-collission support for inventory-commands
54 // *) sniffing and simulation do only support one transmission mode. need to support
55 // all 8 transmission combinations
56 // *) remove or refactor code under "depricated"
57 // *) document all the functions
60 #include "proxmark3.h"
64 #include "iso15693tools.h"
67 #define arraylen(x) (sizeof(x)/sizeof((x)[0]))
69 ///////////////////////////////////////////////////////////////////////
70 // ISO 15693 Part 2 - Air Interface
71 // This section basicly contains transmission and receiving of bits
72 ///////////////////////////////////////////////////////////////////////
74 #define FrameSOF Iso15693FrameSOF
75 #define Logic0 Iso15693Logic0
76 #define Logic1 Iso15693Logic1
77 #define FrameEOF Iso15693FrameEOF
79 #define Crc(data,datalen) Iso15693Crc(data,datalen)
80 #define AddCrc(data,datalen) Iso15693AddCrc(data,datalen)
81 #define sprintUID(target,uid) Iso15693sprintUID(target,uid)
86 // ---------------------------
88 // ---------------------------
90 // prepare data using "1 out of 4" code for later transmission
91 // resulting data rate is 26,48 kbit/s (fc/512)
93 // n ... length of data
94 static void CodeIso15693AsReader(uint8_t *cmd
, int n
)
100 // Give it a bit of slack at the beginning
101 for(i
= 0; i
< 24; i
++) {
114 for(i
= 0; i
< n
; i
++) {
115 for(j
= 0; j
< 8; j
+= 2) {
116 int these
= (cmd
[i
] >> j
) & 3;
167 // And slack at the end, too.
168 for(i
= 0; i
< 24; i
++) {
173 // encode data using "1 out of 256" sheme
174 // data rate is 1,66 kbit/s (fc/8192)
175 // is designed for more robust communication over longer distances
176 static void CodeIso15693AsReader256(uint8_t *cmd
, int n
)
182 // Give it a bit of slack at the beginning
183 for(i
= 0; i
< 24; i
++) {
197 for(i
= 0; i
< n
; i
++) {
198 for (j
= 0; j
<=255; j
++) {
214 // And slack at the end, too.
215 for(i
= 0; i
< 24; i
++) {
221 // Transmit the command (to the tag) that was placed in ToSend[].
222 static void TransmitTo15693Tag(const uint8_t *cmd
, int len
, int *samples
, int *wait
)
226 // FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_MOD);
227 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX
);
228 if(*wait
< 10) { *wait
= 10; }
230 // for(c = 0; c < *wait;) {
231 // if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
232 // AT91C_BASE_SSC->SSC_THR = 0x00; // For exact timing!
235 // if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
236 // volatile uint32_t r = AT91C_BASE_SSC->SSC_RHR;
244 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
245 AT91C_BASE_SSC
->SSC_THR
= cmd
[c
];
251 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
252 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
257 *samples
= (c
+ *wait
) << 3;
260 //-----------------------------------------------------------------------------
261 // Transmit the command (to the reader) that was placed in ToSend[].
262 //-----------------------------------------------------------------------------
263 static void TransmitTo15693Reader(const uint8_t *cmd
, int len
, int *samples
, int *wait
)
267 // FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX);
268 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR
); // No requirement to energise my coils
269 if(*wait
< 10) { *wait
= 10; }
273 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
274 AT91C_BASE_SSC
->SSC_THR
= cmd
[c
];
280 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
281 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
286 *samples
= (c
+ *wait
) << 3;
297 // number of decoded bytes
298 static int GetIso15693AnswerFromTag(uint8_t *receivedResponse
, int maxLen
, int *samples
, int *elapsed
)
301 uint8_t *dest
= (uint8_t *)BigBuf
;
307 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
308 //spindelay(60); // greg - experiment to get rid of some of the 0 byte/failed reads
312 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
313 AT91C_BASE_SSC
->SSC_THR
= 0x43;
315 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
317 b
= (int8_t)AT91C_BASE_SSC
->SSC_RHR
;
319 // The samples are correlations against I and Q versions of the
320 // tone that the tag AM-modulates, so every other sample is I,
321 // every other is Q. We just want power, so abs(I) + abs(Q) is
322 // close to what we want.
337 dest
[c
++] = (uint8_t)r
;
350 //////////////////////////////////////////
351 /////////// DEMODULATE ///////////////////
352 //////////////////////////////////////////
355 int max
= 0, maxPos
=0;
359 // if(GraphTraceLen < 1000) return; // THIS CHECKS FOR A BUFFER TO SMALL
361 // First, correlate for SOF
362 for(i
= 0; i
< 100; i
++) {
364 for(j
= 0; j
< arraylen(FrameSOF
); j
+= skip
) {
365 corr
+= FrameSOF
[j
]*dest
[i
+(j
/skip
)];
372 // DbpString("SOF at %d, correlation %d", maxPos,max/(arraylen(FrameSOF)/skip));
374 int k
= 0; // this will be our return value
376 // greg - If correlation is less than 1 then there's little point in continuing
377 if ((max
/(arraylen(FrameSOF
)/skip
)) >= 1)
380 i
= maxPos
+ arraylen(FrameSOF
)/skip
;
383 memset(outBuf
, 0, sizeof(outBuf
));
386 int corr0
= 0, corr1
= 0, corrEOF
= 0;
387 for(j
= 0; j
< arraylen(Logic0
); j
+= skip
) {
388 corr0
+= Logic0
[j
]*dest
[i
+(j
/skip
)];
390 for(j
= 0; j
< arraylen(Logic1
); j
+= skip
) {
391 corr1
+= Logic1
[j
]*dest
[i
+(j
/skip
)];
393 for(j
= 0; j
< arraylen(FrameEOF
); j
+= skip
) {
394 corrEOF
+= FrameEOF
[j
]*dest
[i
+(j
/skip
)];
396 // Even things out by the length of the target waveform.
400 if(corrEOF
> corr1
&& corrEOF
> corr0
) {
401 // DbpString("EOF at %d", i);
403 } else if(corr1
> corr0
) {
404 i
+= arraylen(Logic1
)/skip
;
407 i
+= arraylen(Logic0
)/skip
;
414 if((i
+(int)arraylen(FrameEOF
)) >= 2000) {
415 DbpString("ran off end!");
419 if(mask
!= 0x01) { // this happens, when we miss the EOF
420 // TODO: for some reason this happens quite often
421 if (DEBUG
) Dbprintf("error, uneven octet! (extra bits!) mask=%02x", mask
);
422 if (mask
<0x08) k
--; // discard the last uneven octet;
423 // 0x08 is an assumption - but works quite often
427 // strncat(str1," octets read",8);
429 // DbpString( str1); // DbpString("%d octets", k);
431 // for(i = 0; i < k; i+=3) {
432 // //DbpString("# %2d: %02x ", i, outBuf[i]);
433 // DbpIntegers(outBuf[i],outBuf[i+1],outBuf[i+2]);
436 for(i
= 0; i
< k
; i
++) {
437 receivedResponse
[i
] = outBuf
[i
];
439 } // "end if correlation > 0" (max/(arraylen(FrameSOF)/skip))
440 return k
; // return the number of bytes demodulated
442 /// DbpString("CRC=%04x", Iso15693Crc(outBuf, k-2));
447 // Now the GetISO15693 message from sniffing command
448 static int GetIso15693AnswerFromSniff(uint8_t *receivedResponse
, int maxLen
, int *samples
, int *elapsed
)
451 uint8_t *dest
= (uint8_t *)BigBuf
;
457 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
458 //spindelay(60); // greg - experiment to get rid of some of the 0 byte/failed reads
462 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
463 AT91C_BASE_SSC
->SSC_THR
= 0x43;
465 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
467 b
= (int8_t)AT91C_BASE_SSC
->SSC_RHR
;
469 // The samples are correlations against I and Q versions of the
470 // tone that the tag AM-modulates, so every other sample is I,
471 // every other is Q. We just want power, so abs(I) + abs(Q) is
472 // close to what we want.
487 dest
[c
++] = (uint8_t)r
;
500 //////////////////////////////////////////
501 /////////// DEMODULATE ///////////////////
502 //////////////////////////////////////////
505 int max
= 0, maxPos
=0;
509 // if(GraphTraceLen < 1000) return; // THIS CHECKS FOR A BUFFER TO SMALL
511 // First, correlate for SOF
512 for(i
= 0; i
< 19000; i
++) {
514 for(j
= 0; j
< arraylen(FrameSOF
); j
+= skip
) {
515 corr
+= FrameSOF
[j
]*dest
[i
+(j
/skip
)];
522 // DbpString("SOF at %d, correlation %d", maxPos,max/(arraylen(FrameSOF)/skip));
524 int k
= 0; // this will be our return value
526 // greg - If correlation is less than 1 then there's little point in continuing
527 if ((max
/(arraylen(FrameSOF
)/skip
)) >= 1) // THIS SHOULD BE 1
530 i
= maxPos
+ arraylen(FrameSOF
)/skip
;
533 memset(outBuf
, 0, sizeof(outBuf
));
536 int corr0
= 0, corr1
= 0, corrEOF
= 0;
537 for(j
= 0; j
< arraylen(Logic0
); j
+= skip
) {
538 corr0
+= Logic0
[j
]*dest
[i
+(j
/skip
)];
540 for(j
= 0; j
< arraylen(Logic1
); j
+= skip
) {
541 corr1
+= Logic1
[j
]*dest
[i
+(j
/skip
)];
543 for(j
= 0; j
< arraylen(FrameEOF
); j
+= skip
) {
544 corrEOF
+= FrameEOF
[j
]*dest
[i
+(j
/skip
)];
546 // Even things out by the length of the target waveform.
550 if(corrEOF
> corr1
&& corrEOF
> corr0
) {
551 // DbpString("EOF at %d", i);
553 } else if(corr1
> corr0
) {
554 i
+= arraylen(Logic1
)/skip
;
557 i
+= arraylen(Logic0
)/skip
;
564 if((i
+(int)arraylen(FrameEOF
)) >= 2000) {
565 DbpString("ran off end!");
570 DbpString("sniff: error, uneven octet! (discard extra bits!)");
571 /// DbpString(" mask=%02x", mask);
575 // strncat(str1," octets read",8);
577 // DbpString( str1); // DbpString("%d octets", k);
579 // for(i = 0; i < k; i+=3) {
580 // //DbpString("# %2d: %02x ", i, outBuf[i]);
581 // DbpIntegers(outBuf[i],outBuf[i+1],outBuf[i+2]);
584 for(i
= 0; i
< k
; i
++) {
585 receivedResponse
[i
] = outBuf
[i
];
587 } // "end if correlation > 0" (max/(arraylen(FrameSOF)/skip))
588 return k
; // return the number of bytes demodulated
590 /// DbpString("CRC=%04x", Iso15693Crc(outBuf, k-2));
594 static void BuildIdentifyRequest(void);
595 //-----------------------------------------------------------------------------
596 // Start to read an ISO 15693 tag. We send an identify request, then wait
597 // for the response. The response is not demodulated, just left in the buffer
598 // so that it can be downloaded to a PC and processed there.
599 //-----------------------------------------------------------------------------
600 void AcquireRawAdcSamplesIso15693(void)
603 uint8_t *dest
= (uint8_t *)BigBuf
;
608 BuildIdentifyRequest();
610 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
612 // Give the tags time to energize
613 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
616 // Now send the command
618 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX
);
622 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
623 AT91C_BASE_SSC
->SSC_THR
= ToSend
[c
];
625 if(c
== ToSendMax
+3) {
629 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
630 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
636 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
641 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
642 AT91C_BASE_SSC
->SSC_THR
= 0x43;
644 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
646 b
= (int8_t)AT91C_BASE_SSC
->SSC_RHR
;
648 // The samples are correlations against I and Q versions of the
649 // tone that the tag AM-modulates, so every other sample is I,
650 // every other is Q. We just want power, so abs(I) + abs(Q) is
651 // close to what we want.
666 dest
[c
++] = (uint8_t)r
;
681 void RecordRawAdcSamplesIso15693(void)
684 uint8_t *dest
= (uint8_t *)BigBuf
;
692 // Start from off (no field generated)
693 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
696 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
700 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
705 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
706 AT91C_BASE_SSC
->SSC_THR
= 0x43;
708 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
710 b
= (int8_t)AT91C_BASE_SSC
->SSC_RHR
;
712 // The samples are correlations against I and Q versions of the
713 // tone that the tag AM-modulates, so every other sample is I,
714 // every other is Q. We just want power, so abs(I) + abs(Q) is
715 // close to what we want.
730 dest
[c
++] = (uint8_t)r
;
743 Dbprintf("fin record");
747 // Initialize the proxmark as iso15k reader
748 void Iso15693InitReader() {
757 // Start from off (no field generated)
758 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
761 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
764 // Give the tags time to energize
765 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
774 ///////////////////////////////////////////////////////////////////////
775 // ISO 15693 Part 3 - Air Interface
776 // This section basicly contains transmission and receiving of bits
777 ///////////////////////////////////////////////////////////////////////
779 // Encode (into the ToSend buffers) an identify request, which is the first
780 // thing that you must send to a tag to get a response.
781 static void BuildIdentifyRequest(void)
786 // one sub-carrier, inventory, 1 slot, fast rate
787 // AFI is at bit 5 (1<<4) when doing an INVENTORY
788 cmd
[0] = (1 << 2) | (1 << 5) | (1 << 1);
789 // inventory command code
798 CodeIso15693AsReader(cmd
, sizeof(cmd
));
801 // uid is in transmission order (which is reverse of display order)
802 static void BuildReadBlockRequest(uint8_t *uid
, uint8_t blockNumber
)
807 // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block
808 // followed by teh block data
809 // one sub-carrier, inventory, 1 slot, fast rate
810 cmd
[0] = (1 << 6)| (1 << 5) | (1 << 1); // no SELECT bit, ADDR bit, OPTION bit
811 // READ BLOCK command code
813 // UID may be optionally specified here
822 cmd
[9] = uid
[7]; // 0xe0; // always e0 (not exactly unique)
823 // Block number to read
824 cmd
[10] = blockNumber
;//0x00;
826 crc
= Crc(cmd
, 11); // the crc needs to be calculated over 12 bytes
827 cmd
[11] = crc
& 0xff;
830 CodeIso15693AsReader(cmd
, sizeof(cmd
));
833 // Now the VICC>VCD responses when we are simulating a tag
834 static void BuildInventoryResponse(void)
839 // one sub-carrier, inventory, 1 slot, fast rate
840 // AFI is at bit 5 (1<<4) when doing an INVENTORY
841 cmd
[0] = 0; //(1 << 2) | (1 << 5) | (1 << 1);
854 cmd
[10] = crc
& 0xff;
857 CodeIso15693AsReader(cmd
, sizeof(cmd
));
860 // Universal Method for sending to and recv from a tag
861 // init ... should we initialize the reader?
862 // speed ... 0 low speed, 1 hi speed
863 // **recv will return you a pointer to the received data
864 // If you do not need the answer use NULL for *recv[]
865 // return: lenght of received data
866 int SendDataTag(uint8_t *send
, int sendlen
, int init
, int speed
, uint8_t **recv
) {
879 uint8_t *answer
= (((uint8_t *)BigBuf
) + 3660);
880 if (recv
!=NULL
) memset(BigBuf
+ 3660, 0, 100);
882 if (init
) Iso15693InitReader();
885 // low speed (1 out of 256)
886 CodeIso15693AsReader256(send
, sendlen
);
888 // high speed (1 out of 4)
889 CodeIso15693AsReader(send
, sendlen
);
895 TransmitTo15693Tag(ToSend
,ToSendMax
,&tsamples
, &wait
);
896 // Now wait for a response
900 answerLen
= GetIso15693AnswerFromTag(answer
, 100, &samples
, &elapsed
) ;
913 // --------------------------------------------------------------------
915 // --------------------------------------------------------------------
917 // Decodes a message from a tag and displays its metadata and content
918 #define DBD15STATLEN 48
919 void DbdecodeIso15693Answer(int len
, uint8_t *d
) {
920 char status
[DBD15STATLEN
+1]={0};
925 strncat(status
,"ProtExt ",DBD15STATLEN
);
928 strncat(status
,"Error ",DBD15STATLEN
);
931 strncat(status
,"01:notSupp",DBD15STATLEN
);
934 strncat(status
,"02:notRecog",DBD15STATLEN
);
937 strncat(status
,"03:optNotSupp",DBD15STATLEN
);
940 strncat(status
,"0f:noInfo",DBD15STATLEN
);
943 strncat(status
,"10:dontExist",DBD15STATLEN
);
946 strncat(status
,"11:lockAgain",DBD15STATLEN
);
949 strncat(status
,"12:locked",DBD15STATLEN
);
952 strncat(status
,"13:progErr",DBD15STATLEN
);
955 strncat(status
,"14:lockErr",DBD15STATLEN
);
958 strncat(status
,"unknownErr",DBD15STATLEN
);
960 strncat(status
," ",DBD15STATLEN
);
962 strncat(status
,"NoErr ",DBD15STATLEN
);
966 if ( (( crc
& 0xff ) == d
[len
-2]) && (( crc
>> 8 ) == d
[len
-1]) )
967 strncat(status
,"CrcOK",DBD15STATLEN
);
969 strncat(status
,"CrcFail!",DBD15STATLEN
);
971 Dbprintf("%s",status
);
977 ///////////////////////////////////////////////////////////////////////
978 // Functions called via USB/Client
979 ///////////////////////////////////////////////////////////////////////
981 void SetDebugIso15693(uint32_t debug
) {
983 Dbprintf("Iso15693 Debug is now %s",DEBUG
?"on":"off");
989 //-----------------------------------------------------------------------------
990 // Simulate an ISO15693 reader, perform anti-collision and then attempt to read a sector
991 // all demodulation performed in arm rather than host. - greg
992 //-----------------------------------------------------------------------------
993 void ReaderIso15693(uint32_t parameter
)
1000 //DbpString(parameter);
1002 //uint8_t *answer0 = (((uint8_t *)BigBuf) + 3560); // allow 100 bytes per reponse (way too much)
1003 uint8_t *answer1
= (((uint8_t *)BigBuf
) + 3660); //
1004 uint8_t *answer2
= (((uint8_t *)BigBuf
) + 3760);
1005 uint8_t *answer3
= (((uint8_t *)BigBuf
) + 3860);
1006 //uint8_t *TagUID= (((uint8_t *)BigBuf) + 3960); // where we hold the uid for hi15reader
1007 // int answerLen0 = 0;
1014 memset(BigBuf
+ 3660, 0, 300);
1019 // Start from off (no field generated)
1020 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1023 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1026 // Give the tags time to energize
1027 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
1040 // FIRST WE RUN AN INVENTORY TO GET THE TAG UID
1041 // THIS MEANS WE CAN PRE-BUILD REQUESTS TO SAVE CPU TIME
1042 uint8_t TagUID
[8]; // where we hold the uid for hi15reader
1044 // BuildIdentifyRequest();
1045 // //TransmitTo15693Tag(ToSend,ToSendMax+3,&tsamples, &wait);
1046 // TransmitTo15693Tag(ToSend,ToSendMax,&tsamples, &wait); // No longer ToSendMax+3
1047 // // Now wait for a response
1048 // responseLen0 = GetIso15693AnswerFromTag(receivedAnswer0, 100, &samples, &elapsed) ;
1049 // if (responseLen0 >=12) // we should do a better check than this
1051 // // really we should check it is a valid mesg
1052 // // but for now just grab what we think is the uid
1053 // TagUID[0] = receivedAnswer0[2];
1054 // TagUID[1] = receivedAnswer0[3];
1055 // TagUID[2] = receivedAnswer0[4];
1056 // TagUID[3] = receivedAnswer0[5];
1057 // TagUID[4] = receivedAnswer0[6];
1058 // TagUID[5] = receivedAnswer0[7];
1059 // TagUID[6] = receivedAnswer0[8]; // IC Manufacturer code
1060 // DbpIntegers(TagUID[6],TagUID[5],TagUID[4]);
1063 // Now send the IDENTIFY command
1064 BuildIdentifyRequest();
1065 //TransmitTo15693Tag(ToSend,ToSendMax+3,&tsamples, &wait);
1066 TransmitTo15693Tag(ToSend
,ToSendMax
,&tsamples
, &wait
); // No longer ToSendMax+3
1067 // Now wait for a response
1068 answerLen1
= GetIso15693AnswerFromTag(answer1
, 100, &samples
, &elapsed
) ;
1070 if (answerLen1
>=12) // we should do a better check than this
1073 TagUID
[0] = answer1
[2];
1074 TagUID
[1] = answer1
[3];
1075 TagUID
[2] = answer1
[4];
1076 TagUID
[3] = answer1
[5];
1077 TagUID
[4] = answer1
[6];
1078 TagUID
[5] = answer1
[7];
1079 TagUID
[6] = answer1
[8]; // IC Manufacturer code
1080 TagUID
[7] = answer1
[9]; // always E0
1082 // Now send the SELECT command
1083 // since the SELECT command is optional, we should not rely on it.
1084 //// BuildSelectRequest(TagUID);
1085 // TransmitTo15693Tag(ToSend,ToSendMax,&tsamples, &wait); // No longer ToSendMax+3
1086 // Now wait for a response
1087 /// answerLen2 = GetIso15693AnswerFromTag(answer2, 100, &samples, &elapsed);
1089 // Now send the MULTI READ command
1090 // BuildArbitraryRequest(*TagUID,parameter);
1091 /// BuildArbitraryCustomRequest(TagUID,parameter);
1092 // BuildReadBlockRequest(*TagUID,parameter);
1093 // BuildSysInfoRequest(*TagUID);
1094 //TransmitTo15693Tag(ToSend,ToSendMax+3,&tsamples, &wait);
1095 /// TransmitTo15693Tag(ToSend,ToSendMax,&tsamples, &wait); // No longer ToSendMax+3
1096 // Now wait for a response
1097 /// answerLen3 = GetIso15693AnswerFromTag(answer3, 100, &samples, &elapsed) ;
1101 Dbprintf("%d octets read from IDENTIFY request:", answerLen1
);
1102 DbdecodeIso15693Answer(answerLen1
,answer1
);
1103 Dbhexdump(answerLen1
,answer1
);
1107 //Dbprintf("UID = %*D",8,TagUID," ");
1108 Dbprintf("UID = %02hX%02hX%02hX%02hX%02hX%02hX%02hX%02hX",TagUID
[7],TagUID
[6],TagUID
[5],
1109 TagUID
[4],TagUID
[3],TagUID
[2],TagUID
[1],TagUID
[0]);
1112 Dbprintf("%d octets read from SELECT request:", answerLen2
);
1113 DbdecodeIso15693Answer(answerLen2
,answer2
);
1114 Dbhexdump(answerLen2
,answer2
);
1116 Dbprintf("%d octets read from XXX request:", answerLen3
);
1117 DbdecodeIso15693Answer(answerLen3
,answer3
);
1118 Dbhexdump(answerLen3
,answer3
);
1122 if (answerLen1
>=12 && DEBUG
) {
1124 while (i
<32) { // sanity check, assume max 32 pages
1125 BuildReadBlockRequest(TagUID
,i
);
1126 TransmitTo15693Tag(ToSend
,ToSendMax
,&tsamples
, &wait
);
1127 answerLen2
= GetIso15693AnswerFromTag(answer2
, 100, &samples
, &elapsed
);
1129 Dbprintf("READ SINGLE BLOCK %d returned %d octets:",i
,answerLen2
);
1130 DbdecodeIso15693Answer(answerLen2
,answer2
);
1131 Dbhexdump(answerLen2
,answer2
);
1132 if ( *((uint32_t*) answer2
) == 0x07160101 ) break; // exit on NoPageErr
1139 // for(i = 0; i < responseLen3; i++) {
1140 // itoa(str1,receivedAnswer3[i]);
1141 // strncat(str2,str1,8);
1151 // Simulate an ISO15693 TAG, perform anti-collision and then print any reader commands
1152 // all demodulation performed in arm rather than host. - greg
1153 void SimTagIso15693(uint32_t parameter
)
1160 uint8_t *answer1
= (((uint8_t *)BigBuf
) + 3660); //
1164 memset(answer1
, 0, 100);
1169 // Start from off (no field generated)
1170 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1173 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1176 // Give the tags time to energize
1177 // FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR); // NO GOOD FOR SIM TAG!!!!
1190 answerLen1
= GetIso15693AnswerFromSniff(answer1
, 100, &samples
, &elapsed
) ;
1192 if (answerLen1
>=1) // we should do a better check than this
1194 // Build a suitable reponse to the reader INVENTORY cocmmand
1195 BuildInventoryResponse();
1196 TransmitTo15693Reader(ToSend
,ToSendMax
, &tsamples
, &wait
);
1199 Dbprintf("%d octets read from reader command: %x %x %x %x %x %x %x %x %x", answerLen1
,
1200 answer1
[0], answer1
[1], answer1
[2],
1201 answer1
[3], answer1
[4], answer1
[5],
1202 answer1
[6], answer1
[7], answer1
[8]);
1211 // Since there is no standardized way of reading the AFI out of a tag, we will brute force it
1212 // (some manufactures offer a way to read the AFI, though)
1213 void BruteforceIso15693Afi(uint32_t speed
)
1217 int datalen
=0, recvlen
=0;
1219 Iso15693InitReader();
1221 // first without AFI
1222 // Tags should respond wihtout AFI and with AFI=0 even when AFI is active
1224 data
[0]=ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
1225 ISO15_REQ_INVENTORY
| ISO15_REQINV_SLOT1
;
1226 data
[1]=ISO15_CMD_INVENTORY
;
1227 data
[2]=0; // mask length
1228 datalen
=AddCrc(data
,3);
1229 recvlen
=SendDataTag(data
,datalen
,0,speed
,&recv
);
1232 Dbprintf("NoAFI UID=%s",sprintUID(NULL
,&recv
[2]));
1237 data
[0]=ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
1238 ISO15_REQ_INVENTORY
| ISO15_REQINV_AFI
| ISO15_REQINV_SLOT1
;
1239 data
[1]=ISO15_CMD_INVENTORY
;
1241 data
[3]=0; // mask length
1243 for (int i
=0;i
<256;i
++) {
1245 datalen
=AddCrc(data
,4);
1246 recvlen
=SendDataTag(data
,datalen
,0,speed
,&recv
);
1249 Dbprintf("AFI=%i UID=%s",i
,sprintUID(NULL
,&recv
[2]));
1252 Dbprintf("AFI Bruteforcing done.");
1256 // Allows to directly send commands to the tag via the client
1257 void DirectTag15693Command(uint32_t datalen
,uint32_t speed
, uint32_t recv
, uint8_t data
[]) {
1260 uint8_t *recvbuf
=(uint8_t *)BigBuf
;
1265 Dbhexdump(datalen
,data
);
1268 recvlen
=SendDataTag(data
,datalen
,1,speed
,(recv
?&recvbuf
:NULL
));
1271 n
.cmd
=/* CMD_ISO_15693_COMMAND_DONE */ CMD_ACK
;
1272 n
.arg
[0]=recvlen
>48?48:recvlen
;
1273 memcpy(n
.d
.asBytes
, recvbuf
, 48);
1275 UsbSendPacket((uint8_t *)&n
, sizeof(n
));
1280 DbdecodeIso15693Answer(recvlen
,recvbuf
);
1281 Dbhexdump(recvlen
,recvbuf
);
1290 // --------------------------------------------------------------------
1291 // -- Misc & deprecated functions
1292 // --------------------------------------------------------------------
1295 // do not use; has a fix UID
1296 static void __attribute__((unused
)) BuildSysInfoRequest(uint8_t *uid
)
1301 // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block
1302 // followed by teh block data
1303 // one sub-carrier, inventory, 1 slot, fast rate
1304 cmd
[0] = (1 << 5) | (1 << 1); // no SELECT bit
1305 // System Information command code
1307 // UID may be optionally specified here
1316 cmd
[9]= 0xe0; // always e0 (not exactly unique)
1318 crc
= Crc(cmd
, 10); // the crc needs to be calculated over 2 bytes
1319 cmd
[10] = crc
& 0xff;
1322 CodeIso15693AsReader(cmd
, sizeof(cmd
));
1325 // do not use; has a fix UID
1326 static void __attribute__((unused
)) BuildSelectRequest( uint8_t uid
[])
1329 // uid[6]=0x31; // this is getting ignored - the uid array is not happening...
1333 // one sub-carrier, inventory, 1 slot, fast rate
1334 //cmd[0] = (1 << 2) | (1 << 5) | (1 << 1); // INVENTROY FLAGS
1335 cmd
[0] = (1 << 4) | (1 << 5) | (1 << 1); // Select and addressed FLAGS
1336 // SELECT command code
1339 // cmd[2] = uid[0];//0x32;
1340 // cmd[3]= uid[1];//0x4b;
1341 // cmd[4] = uid[2];//0x03;
1342 // cmd[5] = uid[3];//0x01;
1343 // cmd[6] = uid[4];//0x00;
1344 // cmd[7] = uid[5];//0x10;
1345 // cmd[8] = uid[6];//0x05;
1352 cmd
[8] = 0x05; // infineon?
1354 cmd
[9]= 0xe0; // always e0 (not exactly unique)
1356 // DbpIntegers(cmd[8],cmd[7],cmd[6]);
1358 crc
= Crc(cmd
, 10); // the crc needs to be calculated over 10 bytes
1359 cmd
[10] = crc
& 0xff;
1362 CodeIso15693AsReader(cmd
, sizeof(cmd
));
1367 // do not use; has a fix UID
1368 static void __attribute__((unused
)) BuildReadMultiBlockRequest(uint8_t *uid
)
1373 // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block
1374 // followed by teh block data
1375 // one sub-carrier, inventory, 1 slot, fast rate
1376 cmd
[0] = (1 << 5) | (1 << 1); // no SELECT bit
1377 // READ Multi BLOCK command code
1379 // UID may be optionally specified here
1388 cmd
[9]= 0xe0; // always e0 (not exactly unique)
1389 // First Block number to read
1391 // Number of Blocks to read
1392 cmd
[11] = 0x2f; // read quite a few
1394 crc
= Crc(cmd
, 12); // the crc needs to be calculated over 2 bytes
1395 cmd
[12] = crc
& 0xff;
1398 CodeIso15693AsReader(cmd
, sizeof(cmd
));
1401 // do not use; has a fix UID
1402 static void __attribute__((unused
)) BuildArbitraryRequest(uint8_t *uid
,uint8_t CmdCode
)
1407 // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block
1408 // followed by teh block data
1409 // one sub-carrier, inventory, 1 slot, fast rate
1410 cmd
[0] = (1 << 5) | (1 << 1); // no SELECT bit
1411 // READ BLOCK command code
1413 // UID may be optionally specified here
1422 cmd
[9]= 0xe0; // always e0 (not exactly unique)
1428 // cmd[13] = 0x00; //Now the CRC
1429 crc
= Crc(cmd
, 12); // the crc needs to be calculated over 2 bytes
1430 cmd
[12] = crc
& 0xff;
1433 CodeIso15693AsReader(cmd
, sizeof(cmd
));
1436 // do not use; has a fix UID
1437 static void __attribute__((unused
)) BuildArbitraryCustomRequest(uint8_t uid
[], uint8_t CmdCode
)
1442 // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block
1443 // followed by teh block data
1444 // one sub-carrier, inventory, 1 slot, fast rate
1445 cmd
[0] = (1 << 5) | (1 << 1); // no SELECT bit
1446 // READ BLOCK command code
1448 // UID may be optionally specified here
1457 cmd
[9]= 0xe0; // always e0 (not exactly unique)
1459 cmd
[10] = 0x05; // for custom codes this must be manufcturer code
1463 // cmd[13] = 0x00; //Now the CRC
1464 crc
= Crc(cmd
, 12); // the crc needs to be calculated over 2 bytes
1465 cmd
[12] = crc
& 0xff;
1468 CodeIso15693AsReader(cmd
, sizeof(cmd
));