1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
3 // Modified 2010 by <adrian -at- atrox.at>
5 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
6 // at your option, any later version. See the LICENSE.txt file for the text of
8 //-----------------------------------------------------------------------------
9 // High frequency ISO15693 commands
10 //-----------------------------------------------------------------------------
11 // There are three basic operation modes, depending on which device (proxmark/pc)
12 // the signal processing, (de)modulation, transmission protocol and logic is done.
14 // All steps are done on the proxmark, the output of the commands is returned via
15 // USB-debug-print commands.
17 // The protocol is done on the PC, passing only Iso15693 data frames via USB. This
18 // allows direct communication with a tag on command level
20 // The proxmark just samples the antenna and passes this "analog" data via USB to
21 // the client. Signal Processing & decoding is done on the pc. This is the slowest
22 // variant, but offers the possibility to analyze the waveforms directly.
32 #include "cmdparser.h"
34 #include "iso15693tools.h"
37 #define FrameSOF Iso15693FrameSOF
38 #define Logic0 Iso15693Logic0
39 #define Logic1 Iso15693Logic1
40 #define FrameEOF Iso15693FrameEOF
42 #define Crc(data,datalen) Iso15693Crc(data,datalen)
43 #define AddCrc(data,datalen) Iso15693AddCrc(data,datalen)
44 #define sprintUID(target,uid) Iso15693sprintUID(target,uid)
46 static int CmdHelp(const char *Cmd
);
48 // structure and database for uid -> tagtype lookups
51 int mask
; // how many MSB bits used
56 const productName uidmapping
[] = {
57 { 0xE007000000000000LL
, 16, "Texas Instrument; " },
58 { 0xE007000000000000LL
, 20, "Texas Instrument; Tag-it HF-I Plus Inlay; 64x32bit" },
59 { 0xE007100000000000LL
, 20, "Texas Instrument; Tag-it HF-I Plus Chip; 64x32bit" },
60 { 0xE007C00000000000LL
, 23, "Texas Instrument; Tag-it HF-I Standard; 8x32bit" },
61 { 0xE007C40000000000LL
, 23, "Texas Instrument; Tag-it HF-I Pro; 8x23bit; password" },
62 { 0xE005000000000000LL
, 16, "Infineon" },
63 { 0xE005400000000000LL
, 24, "Infineon; 56x32bit" },
64 { 0xE004000000000000LL
, 16, "Philips" },
65 { 0xE002000000000000LL
, 16, "STMicroelectronics" },
66 { 0xE016000000000000LL
, 16, "EM-Marin SA (Skidata)" },
67 { 0xE016040000000000LL
, 24, "EM-Marin SA (Skidata Keycard-eco); no memory" },
68 { 0xE016100000000000LL
, 24, "EM-Marin SA (Skidata); EM4135; 36x64bit start page 13" },
69 { 0,0,"no tag-info available" } // must be the last entry
73 // fast method to just read the UID of an tag (collission detection not supported)
74 // *buf shouls be large enough to fit the 64bit uid
75 // returns 1 if suceeded
76 int getUID(uint8_t *buf
)
80 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
81 uint8_t *req
=c
.d
.asBytes
;
84 for (int retry
=0;retry
<3; retry
++) { // don't give up the at the first try
86 req
[0]= ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
87 ISO15_REQ_INVENTORY
| ISO15_REQINV_SLOT1
;
88 req
[1]=ISO15_CMD_INVENTORY
;
89 req
[2]=0; // mask length
95 r
=WaitForResponseTimeout(CMD_ACK
,1000);
99 if (r
->arg
[0]>=12 && ISO15_CRC_CHECK
==Crc(recv
,12)) {
100 memcpy(buf
,&recv
[2],8);
110 // get a product description based on the UID
112 // returns description of the best match
113 static char* getTagInfo(uint8_t *uid
) {
116 memcpy(&myuid
,uid
,sizeof(uint64_t));
117 while (uidmapping
[i
].mask
>0) {
118 mask
=(~0LL) <<(64-uidmapping
[i
].mask
);
119 if ((myuid
& mask
) == uidmapping
[i
].uid
) {
123 if (uidmapping
[i
].mask
>uidmapping
[best
].mask
) {
131 if (best
>=0) return uidmapping
[best
].desc
;
133 return uidmapping
[i
].desc
;
137 // return a clear-text message to an errorcode
138 static char* TagErrorStr(uint8_t error
) {
140 case 0x01: return "The command is not supported";
141 case 0x02: return "The command is not recognised";
142 case 0x03: return "The option is not supported.";
143 case 0x0f: return "Unknown error.";
144 case 0x10: return "The specified block is not available (doesn’t exist).";
145 case 0x11: return "The specified block is already -locked and thus cannot be locked again";
146 case 0x12: return "The specified block is locked and its content cannot be changed.";
147 case 0x13: return "The specified block was not successfully programmed.";
148 case 0x14: return "The specified block was not successfully locked.";
149 default: return "Reserved for Future Use or Custom command error.";
155 int CmdHF15Demod(const char *Cmd
)
157 // The sampling rate is 106.353 ksps/s, for T = 18.8 us
160 int max
= 0, maxPos
= 0;
164 if (GraphTraceLen
< 1000) return 0;
166 // First, correlate for SOF
167 for (i
= 0; i
< 100; i
++) {
169 for (j
= 0; j
< arraylen(FrameSOF
); j
+= skip
) {
170 corr
+= FrameSOF
[j
] * GraphBuffer
[i
+ (j
/ skip
)];
177 PrintAndLog("SOF at %d, correlation %d", maxPos
,
178 max
/ (arraylen(FrameSOF
) / skip
));
180 i
= maxPos
+ arraylen(FrameSOF
) / skip
;
183 memset(outBuf
, 0, sizeof(outBuf
));
186 int corr0
= 0, corr1
= 0, corrEOF
= 0;
187 for (j
= 0; j
< arraylen(Logic0
); j
+= skip
) {
188 corr0
+= Logic0
[j
] * GraphBuffer
[i
+ (j
/ skip
)];
190 for (j
= 0; j
< arraylen(Logic1
); j
+= skip
) {
191 corr1
+= Logic1
[j
] * GraphBuffer
[i
+ (j
/ skip
)];
193 for (j
= 0; j
< arraylen(FrameEOF
); j
+= skip
) {
194 corrEOF
+= FrameEOF
[j
] * GraphBuffer
[i
+ (j
/ skip
)];
196 // Even things out by the length of the target waveform.
200 if (corrEOF
> corr1
&& corrEOF
> corr0
) {
201 PrintAndLog("EOF at %d", i
);
203 } else if (corr1
> corr0
) {
204 i
+= arraylen(Logic1
) / skip
;
207 i
+= arraylen(Logic0
) / skip
;
214 if ((i
+ (int)arraylen(FrameEOF
)) >= GraphTraceLen
) {
215 PrintAndLog("ran off end!");
220 PrintAndLog("error, uneven octet! (discard extra bits!)");
221 PrintAndLog(" mask=%02x", mask
);
223 PrintAndLog("%d octets", k
);
225 for (i
= 0; i
< k
; i
++) {
226 PrintAndLog("# %2d: %02x ", i
, outBuf
[i
]);
228 PrintAndLog("CRC=%04x", Iso15693Crc(outBuf
, k
- 2));
234 // * Acquire Samples as Reader (enables carrier, sends inquiry)
235 int CmdHF15Read(const char *Cmd
)
237 UsbCommand c
= {CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_15693
};
242 // Record Activity without enabeling carrier
243 int CmdHF15Record(const char *Cmd
)
245 UsbCommand c
= {CMD_RECORD_RAW_ADC_SAMPLES_ISO_15693
};
250 int CmdHF15Reader(const char *Cmd
)
252 UsbCommand c
= {CMD_READER_ISO_15693
, {strtol(Cmd
, NULL
, 0), 0, 0}};
257 // Simulation is still not working very good
258 int CmdHF15Sim(const char *Cmd
)
260 UsbCommand c
= {CMD_SIMTAG_ISO_15693
, {strtol(Cmd
, NULL
, 0), 0, 0}};
265 // finds the AFI (Application Family Idendifier) of a card, by trying all values
266 // (There is no standard way of reading the AFI, allthough some tags support this)
267 int CmdHF15Afi(const char *Cmd
)
269 UsbCommand c
= {CMD_ISO_15693_FIND_AFI
, {strtol(Cmd
, NULL
, 0), 0, 0}};
274 // Reads all memory pages
275 int CmdHF15DumpMem(const char*Cmd
) {
279 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
280 uint8_t *req
=c
.d
.asBytes
;
286 PrintAndLog("No Tag found.");
290 PrintAndLog("Reading memory from tag UID=%s",sprintUID(NULL
,uid
));
291 PrintAndLog("Tag Info: %s",getTagInfo(uid
));
293 for (int retry
=0; retry
<5; retry
++) {
295 req
[0]= ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
296 ISO15_REQ_NONINVENTORY
| ISO15_REQ_ADDRESS
;
297 req
[1]=ISO15_CMD_READ
;
298 memcpy(&req
[2],uid
,8);
300 reqlen
=AddCrc(req
,11);
305 r
=WaitForResponseTimeout(CMD_ACK
,1000);
309 if (ISO15_CRC_CHECK
==Crc(recv
,r
->arg
[0])) {
310 if (!(recv
[0] & ISO15_RES_ERROR
)) {
312 *output
=0; // reset outputstring
313 sprintf(output
, "Block %2i ",blocknum
);
314 for ( int i
=1; i
<r
->arg
[0]-2; i
++) { // data in hex
315 sprintf(output
+strlen(output
),"%02hX ",recv
[i
]);
318 for ( int i
=1; i
<r
->arg
[0]-2; i
++) { // data in cleaned ascii
319 sprintf(output
+strlen(output
),"%c",(recv
[i
]>31 && recv
[i
]<127)?recv
[i
]:'.');
321 PrintAndLog("%s",output
);
323 // PrintAndLog("bn=%i",blocknum);
325 PrintAndLog("Tag returned Error %i: %s",recv
[1],TagErrorStr(recv
[1]));
328 } // else PrintAndLog("crc");
329 } // else PrintAndLog("r null");
332 if (r
&& r
->arg
[0]<3)
333 PrintAndLog("Lost Connection");
334 else if (r
&& ISO15_CRC_CHECK
!=Crc(r
->d
.asBytes
,r
->arg
[0]))
335 PrintAndLog("CRC Failed");
337 PrintAndLog("Tag returned Error %i: %s",recv
[1],TagErrorStr(recv
[1]));
344 static command_t CommandTable15
[] =
346 {"help", CmdHF15Help
, 1, "This help"},
347 {"demod", CmdHF15Demod
, 1, "Demodulate ISO15693 from tag"},
348 {"read", CmdHF15Read
, 0, "Read HF tag (ISO 15693)"},
349 {"record", CmdHF15Record
, 0, "Record Samples (ISO 15693)"}, // atrox
350 {"reader", CmdHF15Reader
, 0, "Act like an ISO15693 reader"},
351 {"sim", CmdHF15Sim
, 0, "Fake an ISO15693 tag"},
352 {"cmd", CmdHF15Cmd
, 0, "Send direct commands to ISO15693 tag"},
353 {"findafi", CmdHF15Afi
, 0, "Brute force AFI of an ISO15693 tag"},
354 {"dumpmemory", CmdHF15DumpMem
, 0, "Read all memory pages of an ISO15693 tag"},
355 {NULL
, NULL
, 0, NULL
}
358 int CmdHF15(const char *Cmd
)
360 CmdsParse(CommandTable15
, Cmd
);
364 int CmdHF15Help(const char *Cmd
)
366 CmdsHelp(CommandTable15
);
371 // "HF 15 Cmd" Interface
372 // Allows direct communication with the tag on command level
374 int CmdHF15CmdInquiry(const char *Cmd
)
378 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
379 uint8_t *req
=c
.d
.asBytes
;
382 req
[0]= ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
383 ISO15_REQ_INVENTORY
| ISO15_REQINV_SLOT1
;
384 req
[1]=ISO15_CMD_INVENTORY
;
385 req
[2]=0; // mask length
386 reqlen
=AddCrc(req
,3);
391 r
=WaitForResponseTimeout(CMD_ACK
,1000);
396 PrintAndLog("UID=%s",sprintUID(NULL
,&recv
[2]));
397 PrintAndLog("Tag Info: %s",getTagInfo(&recv
[2]));
399 PrintAndLog("Response to short, just %i bytes. No tag?\n",r
->arg
[0]);
402 PrintAndLog("timeout.");
408 // Turns debugging on(1)/off(0)
409 int CmdHF15CmdDebug( const char *cmd
) {
412 PrintAndLog("Usage: hf 15 cmd debug <0/1>");
413 PrintAndLog(" 0..no debugging output 1..turn debugging on");
417 UsbCommand c
= {CMD_ISO_15693_DEBUG
, {debug
, 0, 0}};
423 int CmdHF15CmdRaw (const char *cmd
) {
426 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
433 unsigned int datalen
=0, temp
;
437 PrintAndLog("Usage: hf 15 cmd raw [-r] [-2] [-c] <0A 0B 0C ... hex>");
438 PrintAndLog(" -r do not read response");
439 PrintAndLog(" -2 use slower '1 out of 256' mode");
440 PrintAndLog(" -c calculate and append CRC");
441 PrintAndLog(" Tip: turn on debugging for verbose output");
446 while (*cmd
==' ' || *cmd
=='\t') cmd
++;
448 while (cmd
[i
]!='\0') {
449 if (cmd
[i
]==' ' || cmd
[i
]=='\t') { i
++; continue; }
464 PrintAndLog("Invalid option");
470 if ((cmd
[i
]>='0' && cmd
[i
]<='9') ||
471 (cmd
[i
]>='a' && cmd
[i
]<='f') ||
472 (cmd
[i
]>='A' && cmd
[i
]<='F') ) {
473 buf
[strlen(buf
)+1]=0;
474 buf
[strlen(buf
)]=cmd
[i
];
477 if (strlen(buf
)>=2) {
478 sscanf(buf
,"%x",&temp
);
479 data
[datalen
]=(uint8_t)(temp
& 0xff);
485 PrintAndLog("Invalid char on input");
488 if (crc
) datalen
=AddCrc(data
,datalen
);
493 memcpy(c
.d
.asBytes
,data
,datalen
);
498 r
=WaitForResponseTimeout(CMD_ACK
,1000);
502 PrintAndLog("received %i octets",r
->arg
[0]);
505 PrintAndLog("timeout while waiting for reply.");
513 int prepareHF15Cmd(char **cmd
, UsbCommand
*c
, uint8_t iso15cmd
[], int iso15cmdlen
) {
515 uint8_t *req
=c
->d
.asBytes
, uid
[8];
519 while (**cmd
==' ' || **cmd
=='\t') (*cmd
)++;
521 if (strstr(*cmd
,"-2")==*cmd
) {
522 c
->arg
[1]=0; // quse 1of256
527 while (**cmd
==' ' || **cmd
=='\t') (*cmd
)++;
529 if (strstr(*cmd
,"-o")==*cmd
) {
530 req
[reqlen
]=ISO15_REQ_OPTION
;
535 while (**cmd
==' ' || **cmd
=='\t') (*cmd
)++;
539 PrintAndLog("missing addr");
544 // you must have selected the tag earlier
545 req
[reqlen
++]|= ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
546 ISO15_REQ_NONINVENTORY
| ISO15_REQ_SELECT
;
547 memcpy(&req
[reqlen
],&iso15cmd
[0],iso15cmdlen
);
552 // unaddressed mode may not be supported by all vendors
553 req
[reqlen
++]|= ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
554 ISO15_REQ_NONINVENTORY
;
555 memcpy(&req
[reqlen
],&iso15cmd
[0],iso15cmdlen
);
559 req
[reqlen
++]|= ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
560 ISO15_REQ_NONINVENTORY
| ISO15_REQ_ADDRESS
;
561 memcpy(&req
[reqlen
],&iso15cmd
[0],iso15cmdlen
);
564 PrintAndLog("No Tag found");
567 memcpy(req
+reqlen
,uid
,8);
568 PrintAndLog("Detected UID %s",sprintUID(NULL
,uid
));
572 req
[reqlen
++]|= ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
573 ISO15_REQ_NONINVENTORY
| ISO15_REQ_ADDRESS
;
574 memcpy(&req
[reqlen
],&iso15cmd
[0],iso15cmdlen
);
577 /* sscanf(cmd,"%hX%hX%hX%hX%hX%hX%hX%hX",
578 (short unsigned int *)&uid[7],(short unsigned int *)&uid[6],
579 (short unsigned int *)&uid[5],(short unsigned int *)&uid[4],
580 (short unsigned int *)&uid[3],(short unsigned int *)&uid[2],
581 (short unsigned int *)&uid[1],(short unsigned int *)&uid[0]); */
582 for (int i
=0;i
<8 && (*cmd
)[i
*2] && (*cmd
)[i
*2+1];i
++) { // parse UID
583 sscanf((char[]){(*cmd
)[i
*2],(*cmd
)[i
*2+1],0},"%X",&temp
);
587 PrintAndLog("Using UID %s",sprintUID(NULL
,uid
));
588 memcpy(&req
[reqlen
],&uid
[0],8);
591 // skip to next space
592 while (**cmd
!=' ' && **cmd
!='\t') (*cmd
)++;
593 // skip over the space
594 while (**cmd
==' ' || **cmd
=='\t') (*cmd
)++;
602 int CmdHF15CmdRead(const char *Cmd
) {
605 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
606 uint8_t *req
=c
.d
.asBytes
;
607 int reqlen
=0, pagenum
;
616 PrintAndLog("Usage: hf 15 cmd read [options] <uid|s|*> <page#>");
617 PrintAndLog(" options:");
618 PrintAndLog(" -2 use slower '1 out of 256' mode");
619 PrintAndLog(" uid (either): ");
620 PrintAndLog(" <8B hex> full UID eg E011223344556677");
621 PrintAndLog(" s selected tag");
622 PrintAndLog(" u unaddressed mode");
623 PrintAndLog(" * scan for tag");
624 PrintAndLog(" page#: page number 0-255");
628 prepareHF15Cmd(&cmd
, &c
,(uint8_t[]){ISO15_CMD_READ
},1);
631 pagenum
=strtol(cmd
,NULL
,0);
633 PrintAndLog("invalid pagenum");
637 req
[reqlen
++]=(uint8_t)pagenum
;
639 reqlen
=AddCrc(req
,reqlen
);
645 r
=WaitForResponseTimeout(CMD_ACK
,1000);
647 if (r
!=NULL
&& r
->arg
[0]>2) {
649 if (ISO15_CRC_CHECK
==Crc(recv
,r
->arg
[0])) {
650 if (!(recv
[0] & ISO15_RES_ERROR
)) {
651 *output
=0; // reset outputstring
652 //sprintf(output, "Block %2i ",blocknum);
653 for ( int i
=1; i
<r
->arg
[0]-2; i
++) {
654 sprintf(output
+strlen(output
),"%02hX ",recv
[i
]);
657 for ( int i
=2; i
<r
->arg
[0]-2; i
++) {
658 sprintf(output
+strlen(output
),"%c",recv
[i
]>31 || recv
[i
]<127?recv
[i
]:'.');
660 PrintAndLog("%s",output
);
662 PrintAndLog("Tag returned Error %i: %s",recv
[1],TagErrorStr(recv
[1]));
665 PrintAndLog("CRC failed");
668 PrintAndLog("no answer");
675 int CmdHF15CmdWrite(const char *Cmd
) {
678 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
679 uint8_t *req
=c
.d
.asBytes
;
680 int reqlen
=0, pagenum
, temp
;
689 PrintAndLog("Usage: hf 15 cmd write [options] <uid|s|*> <page#> <hexdata>");
690 PrintAndLog(" options:");
691 PrintAndLog(" -2 use slower '1 out of 256' mode");
692 PrintAndLog(" -o set OPTION Flag (needed for TI)");
693 PrintAndLog(" uid (either): ");
694 PrintAndLog(" <8B hex> full UID eg E011223344556677");
695 PrintAndLog(" s selected tag");
696 PrintAndLog(" u unaddressed mode");
697 PrintAndLog(" * scan for tag");
698 PrintAndLog(" page#: page number 0-255");
699 PrintAndLog(" hexdata: data to be written eg AA BB CC DD");
703 prepareHF15Cmd(&cmd
, &c
,(uint8_t[]){ISO15_CMD_WRITE
},1);
706 // *cmd -> page num ; *cmd2 -> data
708 while (*cmd2
!=' ' && *cmd2
!='\t' && *cmd2
) cmd2
++;
712 pagenum
=strtol(cmd
,NULL
,0);
714 PrintAndLog("invalid pagenum");
717 req
[reqlen
++]=(uint8_t)pagenum
;
720 while (cmd2
[0] && cmd2
[1]) { // hexdata, read by 2 hexchars
725 sscanf((char[]){cmd2
[0],cmd2
[1],0},"%X",&temp
);
726 req
[reqlen
++]=temp
& 0xff;
730 reqlen
=AddCrc(req
,reqlen
);
736 r
=WaitForResponseTimeout(CMD_ACK
,2000);
738 if (r
!=NULL
&& r
->arg
[0]>2) {
740 if (ISO15_CRC_CHECK
==Crc(recv
,r
->arg
[0])) {
741 if (!(recv
[0] & ISO15_RES_ERROR
)) {
744 PrintAndLog("Tag returned Error %i: %s",recv
[1],TagErrorStr(recv
[1]));
747 PrintAndLog("CRC failed");
750 PrintAndLog("no answer");
758 static command_t CommandTable15Cmd
[] =
760 {"help", CmdHF15CmdHelp
, 1, "This Help"},
761 {"inquiry", CmdHF15CmdInquiry
, 0, "Search for tags in range"},
763 {"select", CmdHF15CmdSelect, 0, "Select an tag with a specific UID for further commands"},
765 {"read", CmdHF15CmdRead
, 0, "Read a block"},
766 {"write", CmdHF15CmdWrite
, 0, "Write a block"},
768 {"readmulti",CmdHF15CmdReadmulti, 0, "Reads multiple Blocks"},
770 {"raw", CmdHF15CmdRaw
, 0, "Send raw hex data to tag"},
771 {"debug", CmdHF15CmdDebug
, 0, "Turn debugging on/off"},
772 {NULL
, NULL
, 0, NULL
}
775 int CmdHF15Cmd(const char *Cmd
)
777 CmdsParse(CommandTable15Cmd
, Cmd
);
781 int CmdHF15CmdHelp(const char *Cmd
)
783 CmdsHelp(CommandTable15Cmd
);