1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
8 // High frequency ISO14443B commands
9 //-----------------------------------------------------------------------------
16 #include "iso14443crc.h"
17 #include "proxmark3.h"
22 #include "cmdparser.h"
29 static int CmdHelp(const char *Cmd
);
31 int CmdHF14BDemod(const char *Cmd
)
36 bool negateI
, negateQ
;
41 // As received, the samples are pairs, correlations against I and Q
42 // square waves. So estimate angle of initial carrier (or just
43 // quadrant, actually), and then do the demod.
45 // First, estimate where the tag starts modulating.
46 for (i
= 0; i
< GraphTraceLen
; i
+= 2) {
47 if (abs(GraphBuffer
[i
]) + abs(GraphBuffer
[i
+ 1]) > 40) {
51 if (i
>= GraphTraceLen
) {
52 PrintAndLog("too weak to sync");
55 PrintAndLog("out of weak at %d", i
);
58 // Now, estimate the phase in the initial modulation of the tag
61 for (; i
< (outOfWeakAt
+ 16); i
+= 2) {
62 isum
+= GraphBuffer
[i
+ 0];
63 qsum
+= GraphBuffer
[i
+ 1];
68 // Turn the correlation pairs into soft decisions on the bit.
70 for (i
= 0; i
< GraphTraceLen
/ 2; i
++) {
71 int si
= GraphBuffer
[j
];
72 int sq
= GraphBuffer
[j
+ 1];
73 if (negateI
) si
= -si
;
74 if (negateQ
) sq
= -sq
;
75 GraphBuffer
[i
] = si
+ sq
;
81 while (GraphBuffer
[i
] > 0 && i
< GraphTraceLen
)
83 if (i
>= GraphTraceLen
) goto demodError
;
86 while (GraphBuffer
[i
] < 0 && i
< GraphTraceLen
)
88 if (i
>= GraphTraceLen
) goto demodError
;
89 if ((i
- iold
) > 23) goto demodError
;
91 PrintAndLog("make it to demod loop");
95 while (GraphBuffer
[i
] >= 0 && i
< GraphTraceLen
)
97 if (i
>= GraphTraceLen
) goto demodError
;
98 if ((i
- iold
) > 6) goto demodError
;
100 uint16_t shiftReg
= 0;
101 if (i
+ 20 >= GraphTraceLen
) goto demodError
;
103 for (j
= 0; j
< 10; j
++) {
104 int soft
= GraphBuffer
[i
] + GraphBuffer
[i
+ 1];
106 if (abs(soft
) < (abs(isum
) + abs(qsum
)) / 20) {
107 PrintAndLog("weak bit");
111 if(GraphBuffer
[i
] + GraphBuffer
[i
+1] >= 0) {
118 if ((shiftReg
& 0x200) && !(shiftReg
& 0x001))
120 // valid data byte, start and stop bits okay
121 PrintAndLog(" %02x", (shiftReg
>> 1) & 0xff);
122 data
[dataLen
++] = (shiftReg
>> 1) & 0xff;
123 if (dataLen
>= sizeof(data
)) {
126 } else if (shiftReg
== 0x000) {
134 uint8_t first
, second
;
135 ComputeCrc14443(CRC_14443_B
, data
, dataLen
-2, &first
, &second
);
136 PrintAndLog("CRC: %02x %02x (%s)\n", first
, second
,
137 (first
== data
[dataLen
-2] && second
== data
[dataLen
-1]) ?
138 "ok" : "****FAIL****");
140 RepaintGraphWindow();
144 PrintAndLog("demod error");
145 RepaintGraphWindow();
149 int CmdHF14BList(const char *Cmd
)
151 PrintAndLog("Deprecated command, use 'hf list 14b' instead");
156 int CmdHF14Sim(const char *Cmd
)
158 UsbCommand c
={CMD_SIMULATE_TAG_ISO_14443
};
159 clearCommandBuffer();
164 int CmdHFSimlisten(const char *Cmd
)
166 UsbCommand c
= {CMD_SIMULATE_TAG_HF_LISTEN
};
167 clearCommandBuffer();
172 int CmdHF14BSnoop(const char *Cmd
)
174 UsbCommand c
= {CMD_SNOOP_ISO_14443
};
175 clearCommandBuffer();
180 /* New command to read the contents of a SRI512 tag
181 * SRI512 tags are ISO14443-B modulated memory tags,
182 * this command just dumps the contents of the memory
184 int CmdSri512Read(const char *Cmd
)
186 UsbCommand c
= {CMD_READ_SRI512_TAG
, {strtol(Cmd
, NULL
, 0), 0, 0}};
187 clearCommandBuffer();
192 /* New command to read the contents of a SRIX4K tag
193 * SRIX4K tags are ISO14443-B modulated memory tags,
194 * this command just dumps the contents of the memory/
196 int CmdSrix4kRead(const char *Cmd
)
198 UsbCommand c
= {CMD_READ_SRIX4K_TAG
, {strtol(Cmd
, NULL
, 0), 0, 0}};
199 clearCommandBuffer();
206 UsbCommand c
= {CMD_ISO_14443B_COMMAND
, {0, 0, 0}};
207 clearCommandBuffer();
209 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1000)) {
215 int HF14BCmdRaw(bool reply
, bool *crc
, uint8_t power_trace
, uint8_t *data
, uint8_t *datalen
, bool verbose
){
217 UsbCommand c
= {CMD_ISO_14443B_COMMAND
, {0, 0, 0}}; // len,recv,power/trace
220 uint8_t first
, second
;
221 ComputeCrc14443(CRC_14443_B
, data
, *datalen
, &first
, &second
);
222 data
[*datalen
] = first
;
223 data
[*datalen
+ 1] = second
;
229 c
.arg
[2] = power_trace
;
230 memcpy(c
.d
.asBytes
,data
,*datalen
);
231 clearCommandBuffer();
234 if (!reply
) return 1;
236 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1000)) {
237 if (verbose
) PrintAndLog("timeout while waiting for reply.");
240 *datalen
= resp
.arg
[0];
241 if (verbose
) PrintAndLog("received %u octets", *datalen
);
242 if(*datalen
<2) return 0;
244 memcpy(data
, resp
.d
.asBytes
, *datalen
);
245 if (verbose
) PrintAndLog("%s", sprint_hex(data
, *datalen
));
247 uint8_t first
, second
;
248 ComputeCrc14443(CRC_14443_B
, data
, *datalen
-2, &first
, &second
);
249 if(data
[*datalen
-2] == first
&& data
[*datalen
-1] == second
) {
250 if (verbose
) PrintAndLog("CRC OK");
253 if (verbose
) PrintAndLog("CRC failed");
259 int CmdHF14BCmdRaw (const char *Cmd
) {
262 uint8_t power_trace
= 0;
264 uint8_t data
[100] = {0x00};
269 PrintAndLog("Usage: hf 14b raw [-r] [-c] [-p] <0A 0B 0C ... hex>");
270 PrintAndLog(" -r do not read response");
271 PrintAndLog(" -c calculate and append CRC");
272 PrintAndLog(" -p leave the field on after receive");
277 while (*Cmd
==' ' || *Cmd
=='\t') Cmd
++;
279 while (Cmd
[i
]!='\0') {
280 if (Cmd
[i
]==' ' || Cmd
[i
]=='\t') { i
++; continue; }
296 PrintAndLog("Invalid option");
302 if ((Cmd
[i
]>='0' && Cmd
[i
]<='9') ||
303 (Cmd
[i
]>='a' && Cmd
[i
]<='f') ||
304 (Cmd
[i
]>='A' && Cmd
[i
]<='F') ) {
305 buf
[strlen(buf
)+1]=0;
306 buf
[strlen(buf
)]=Cmd
[i
];
309 if (strlen(buf
)>=2) {
310 sscanf(buf
,"%x",&temp
);
311 data
[datalen
++]=(uint8_t)(temp
& 0xff);
316 PrintAndLog("Invalid char on input");
321 PrintAndLog("Missing data input");
325 return HF14BCmdRaw(reply
, &crc
, power_trace
, data
, &datalen
, true);
328 static void print_atqb_resp(uint8_t *data
){
329 PrintAndLog (" UID: %s", sprint_hex(data
+1,4));
330 PrintAndLog (" App Data: %s", sprint_hex(data
+5,4));
331 PrintAndLog (" Protocol: %s", sprint_hex(data
+9,3));
332 uint8_t BitRate
= data
[9];
334 PrintAndLog (" Bit Rate: 106 kbit/s only PICC <-> PCD");
336 PrintAndLog (" Bit Rate: 212 kbit/s PICC -> PCD supported");
338 PrintAndLog (" Bit Rate: 424 kbit/s PICC -> PCD supported");
340 PrintAndLog (" Bit Rate: 847 kbit/s PICC -> PCD supported");
342 PrintAndLog (" Bit Rate: 212 kbit/s PICC <- PCD supported");
344 PrintAndLog (" Bit Rate: 424 kbit/s PICC <- PCD supported");
346 PrintAndLog (" Bit Rate: 847 kbit/s PICC <- PCD supported");
348 PrintAndLog (" Same bit rate <-> required");
350 uint16_t maxFrame
= data
[10]>>4;
352 maxFrame
= 8*maxFrame
+ 16;
353 else if (maxFrame
== 5)
355 else if (maxFrame
== 6)
357 else if (maxFrame
== 7)
359 else if (maxFrame
== 8)
364 PrintAndLog ("Max Frame Size: %d%s",maxFrame
, (maxFrame
== 257) ? "+ RFU" : "");
366 uint8_t protocolT
= data
[10] & 0xF;
367 PrintAndLog (" Protocol Type: Protocol is %scompliant with ISO/IEC 14443-4",(protocolT
) ? "" : "not " );
368 PrintAndLog ("Frame Wait Int: %d", data
[11]>>4);
369 PrintAndLog (" App Data Code: Application is %s",(data
[11]&4) ? "Standard" : "Proprietary");
370 PrintAndLog (" Frame Options: NAD is %ssupported",(data
[11]&2) ? "" : "not ");
371 PrintAndLog (" Frame Options: CID is %ssupported",(data
[11]&1) ? "" : "not ");
376 char *get_ST_Chip_Model(uint8_t data
){
377 static char model
[20];
378 char *retStr
= model
;
379 memset(model
,0, sizeof(model
));
382 case 0x0: sprintf(retStr
, "SRIX4K (Special)"); break;
383 case 0x2: sprintf(retStr
, "SR176"); break;
384 case 0x3: sprintf(retStr
, "SRIX4K"); break;
385 case 0x4: sprintf(retStr
, "SRIX512"); break;
386 case 0x6: sprintf(retStr
, "SRI512"); break;
387 case 0x7: sprintf(retStr
, "SRI4K"); break;
388 case 0xC: sprintf(retStr
, "SRT512"); break;
389 default: sprintf(retStr
, "Unknown"); break;
394 static void print_st_info(uint8_t *data
){
395 //uid = first 8 bytes in data
396 PrintAndLog(" UID: %s", sprint_hex(data
,8));
397 PrintAndLog(" MFG: %02X, %s", data
[1], getTagInfo(data
[1]));
398 PrintAndLog("Chip: %02X, %s", data
[2]>>2, get_ST_Chip_Model(data
[2]>>2));
402 int HF14BStdRead(uint8_t *data
, uint8_t *datalen
){
410 if (HF14BCmdRaw(true, &crc
, 0, data
, datalen
, false)==0) return 0;
412 if (data
[0] != 0x50 || *datalen
!= 14 || !crc
) return 0;
414 PrintAndLog ("\n14443-3b tag found:");
415 print_atqb_resp(data
);
420 int HF14B_ST_Read(uint8_t *data
, uint8_t *datalen
){
428 // verbose on for now for testing - turn off when functional
429 if (HF14BCmdRaw(true, &crc
, 1, data
, datalen
, true)==0) return rawClose();
431 if (*datalen
!= 3 || !crc
) return rawClose();
433 uint8_t chipID
= data
[0];
440 // verbose on for now for testing - turn off when functional
441 if (HF14BCmdRaw(true, &crc
, 1, data
, datalen
, true)==0) return rawClose();
443 if (*datalen
!= 3 || !crc
|| data
[0] != chipID
) return rawClose();
450 // verbose on for now for testing - turn off when functional
451 if (HF14BCmdRaw(true, &crc
, 1, data
, datalen
, true)==0) return 0;
453 if (*datalen
!= 10 || !crc
) return 0;
455 PrintAndLog("\n14443-3b ST tag found:");
460 int HF14BReader(bool verbose
){
464 // try std 14b (atqb)
465 if (HF14BStdRead(data
, &datalen
)) return 1;
468 if (HF14B_ST_Read(data
, &datalen
)) return 1;
470 if (verbose
) PrintAndLog("no 14443B tag found");
474 int CmdHF14BReader(const char *Cmd
){
475 return HF14BReader(true);
478 int CmdHFRawSamples(const char *Cmd
){
480 UsbCommand c
= {CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443
, {strtol(Cmd
,NULL
,0), 0, 0}};
483 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1000)) {
484 PrintAndLog("timeout while waiting for reply.");
487 getSamples("39999", true);
491 int CmdHF14BWrite( const char *Cmd
){
493 * For SRIX4K blocks 00 - 7F
494 * hf 14b raw -c -p 09 $srix4kwblock $srix4kwdata
496 * For SR512 blocks 00 - 0F
497 * hf 14b raw -c -p 09 $sr512wblock $sr512wdata
499 * Special block FF = otp_lock_reg block.
502 char cmdp
= param_getchar(Cmd
, 0);
503 uint8_t blockno
= -1;
504 uint8_t data
[4] = {0x00};
505 bool isSrix4k
= true;
508 if (strlen(Cmd
) < 1 || cmdp
== 'h' || cmdp
== 'H') {
509 PrintAndLog("Usage: hf 14b write <1|2> <BLOCK> <DATA>");
510 PrintAndLog(" [1 = SRIX4K]");
511 PrintAndLog(" [2 = SRI512]");
512 PrintAndLog(" [BLOCK number depends on tag, special block == FF]");
513 PrintAndLog(" sample: hf 14b write 1 7F 11223344");
514 PrintAndLog(" : hf 14b write 1 FF 11223344");
515 PrintAndLog(" : hf 14b write 2 15 11223344");
516 PrintAndLog(" : hf 14b write 2 FF 11223344");
523 //blockno = param_get8(Cmd, 1);
525 if ( param_gethex(Cmd
,1, &blockno
, 2) ) {
526 PrintAndLog("Block number must include 2 HEX symbols");
531 if ( blockno
> 0x7f && blockno
!= 0xff ){
532 PrintAndLog("Block number out of range");
536 if ( blockno
> 0x0f && blockno
!= 0xff ){
537 PrintAndLog("Block number out of range");
542 if (param_gethex(Cmd
, 2, data
, 8)) {
543 PrintAndLog("Data must include 8 HEX symbols");
547 if ( blockno
== 0xff)
548 PrintAndLog("[%s] Write special block %02X [ %s ]", (isSrix4k
)?"SRIX4K":"SRI512" , blockno
, sprint_hex(data
,4) );
550 PrintAndLog("[%s] Write block %02X [ %s ]", (isSrix4k
)?"SRIX4K":"SRI512", blockno
, sprint_hex(data
,4) );
552 sprintf(str
, "-c 09 %02x %02x%02x%02x%02x", blockno
, data
[0], data
[1], data
[2], data
[3]);
558 static command_t CommandTable
[] =
560 {"help", CmdHelp
, 1, "This help"},
561 {"demod", CmdHF14BDemod
, 1, "Demodulate ISO14443 Type B from tag"},
562 {"getsamples", CmdHFRawSamples
,0, "[atqb=0 or ST=1] Send wake cmd and Get raw HF samples to GraphBuffer"},
563 {"list", CmdHF14BList
, 0, "[Deprecated] List ISO 14443b history"},
564 {"reader", CmdHF14BReader
, 0, "Find 14b tag (HF ISO 14443b)"},
565 {"sim", CmdHF14Sim
, 0, "Fake ISO 14443 tag"},
566 {"simlisten", CmdHFSimlisten
, 0, "Get HF samples as fake tag"},
567 {"snoop", CmdHF14BSnoop
, 0, "Eavesdrop ISO 14443"},
568 {"sri512read", CmdSri512Read
, 0, "Read contents of a SRI512 tag"},
569 {"srix4kread", CmdSrix4kRead
, 0, "Read contents of a SRIX4K tag"},
570 {"raw", CmdHF14BCmdRaw
, 0, "Send raw hex data to tag"},
571 {"write", CmdHF14BWrite
, 0, "Write data to a SRI512 | SRIX4K tag"},
572 {NULL
, NULL
, 0, NULL
}
575 int CmdHF14B(const char *Cmd
)
577 CmdsParse(CommandTable
, Cmd
);
581 int CmdHelp(const char *Cmd
)
583 CmdsHelp(CommandTable
);