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"
26 static int CmdHelp(const char *Cmd
);
28 int CmdHF14BDemod(const char *Cmd
)
33 bool negateI
, negateQ
;
38 // As received, the samples are pairs, correlations against I and Q
39 // square waves. So estimate angle of initial carrier (or just
40 // quadrant, actually), and then do the demod.
42 // First, estimate where the tag starts modulating.
43 for (i
= 0; i
< GraphTraceLen
; i
+= 2) {
44 if (abs(GraphBuffer
[i
]) + abs(GraphBuffer
[i
+ 1]) > 40) {
48 if (i
>= GraphTraceLen
) {
49 PrintAndLog("too weak to sync");
52 PrintAndLog("out of weak at %d", i
);
55 // Now, estimate the phase in the initial modulation of the tag
58 for (; i
< (outOfWeakAt
+ 16); i
+= 2) {
59 isum
+= GraphBuffer
[i
+ 0];
60 qsum
+= GraphBuffer
[i
+ 1];
65 // Turn the correlation pairs into soft decisions on the bit.
67 for (i
= 0; i
< GraphTraceLen
/ 2; i
++) {
68 int si
= GraphBuffer
[j
];
69 int sq
= GraphBuffer
[j
+ 1];
70 if (negateI
) si
= -si
;
71 if (negateQ
) sq
= -sq
;
72 GraphBuffer
[i
] = si
+ sq
;
78 while (GraphBuffer
[i
] > 0 && i
< GraphTraceLen
)
80 if (i
>= GraphTraceLen
) goto demodError
;
83 while (GraphBuffer
[i
] < 0 && i
< GraphTraceLen
)
85 if (i
>= GraphTraceLen
) goto demodError
;
86 if ((i
- iold
) > 23) goto demodError
;
88 PrintAndLog("make it to demod loop");
92 while (GraphBuffer
[i
] >= 0 && i
< GraphTraceLen
)
94 if (i
>= GraphTraceLen
) goto demodError
;
95 if ((i
- iold
) > 6) goto demodError
;
97 uint16_t shiftReg
= 0;
98 if (i
+ 20 >= GraphTraceLen
) goto demodError
;
100 for (j
= 0; j
< 10; j
++) {
101 int soft
= GraphBuffer
[i
] + GraphBuffer
[i
+ 1];
103 if (abs(soft
) < (abs(isum
) + abs(qsum
)) / 20) {
104 PrintAndLog("weak bit");
108 if(GraphBuffer
[i
] + GraphBuffer
[i
+1] >= 0) {
115 if ((shiftReg
& 0x200) && !(shiftReg
& 0x001))
117 // valid data byte, start and stop bits okay
118 PrintAndLog(" %02x", (shiftReg
>> 1) & 0xff);
119 data
[dataLen
++] = (shiftReg
>> 1) & 0xff;
120 if (dataLen
>= sizeof(data
)) {
123 } else if (shiftReg
== 0x000) {
131 uint8_t first
, second
;
132 ComputeCrc14443(CRC_14443_B
, data
, dataLen
-2, &first
, &second
);
133 PrintAndLog("CRC: %02x %02x (%s)\n", first
, second
,
134 (first
== data
[dataLen
-2] && second
== data
[dataLen
-1]) ?
135 "ok" : "****FAIL****");
137 RepaintGraphWindow();
141 PrintAndLog("demod error");
142 RepaintGraphWindow();
146 int CmdHF14BList(const char *Cmd
)
148 PrintAndLog("Deprecated command, use 'hf list 14b' instead");
153 int CmdHF14Sim(const char *Cmd
)
155 UsbCommand c
={CMD_SIMULATE_TAG_ISO_14443
};
160 int CmdHFSimlisten(const char *Cmd
)
162 UsbCommand c
= {CMD_SIMULATE_TAG_HF_LISTEN
};
167 int CmdHF14BSnoop(const char *Cmd
)
169 UsbCommand c
= {CMD_SNOOP_ISO_14443
};
174 /* New command to read the contents of a SRI512 tag
175 * SRI512 tags are ISO14443-B modulated memory tags,
176 * this command just dumps the contents of the memory
178 int CmdSri512Read(const char *Cmd
)
180 UsbCommand c
= {CMD_READ_SRI512_TAG
, {strtol(Cmd
, NULL
, 0), 0, 0}};
185 /* New command to read the contents of a SRIX4K tag
186 * SRIX4K tags are ISO14443-B modulated memory tags,
187 * this command just dumps the contents of the memory/
189 int CmdSrix4kRead(const char *Cmd
)
191 UsbCommand c
= {CMD_READ_SRIX4K_TAG
, {strtol(Cmd
, NULL
, 0), 0, 0}};
196 int HF14BCmdRaw(bool reply
, bool *crc
, bool power
, uint8_t *data
, uint8_t *datalen
, bool verbose
){
198 UsbCommand c
= {CMD_ISO_14443B_COMMAND
, {0, 0, 0}}; // len,recv?
201 uint8_t first
, second
;
202 ComputeCrc14443(CRC_14443_B
, data
, *datalen
, &first
, &second
);
203 data
[*datalen
] = first
;
204 data
[*datalen
+ 1] = second
;
211 memcpy(c
.d
.asBytes
,data
,*datalen
);
214 if (!reply
) return 1;
216 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1000)) {
217 if (verbose
) PrintAndLog("timeout while waiting for reply.");
220 *datalen
= resp
.arg
[0];
221 if (verbose
) PrintAndLog("received %i octets", *datalen
);
225 memcpy(data
, resp
.d
.asBytes
, *datalen
);
226 if (verbose
) PrintAndLog("%s", sprint_hex(data
, *datalen
));
228 uint8_t first
, second
;
229 ComputeCrc14443(CRC_14443_B
, data
, *datalen
-2, &first
, &second
);
230 if(data
[*datalen
-2] == first
&& data
[*datalen
-1] == second
) {
231 if (verbose
) PrintAndLog("CRC OK");
234 if (verbose
) PrintAndLog("CRC failed");
240 int CmdHF14BCmdRaw (const char *Cmd
) {
245 uint8_t data
[100] = {0x00};
250 PrintAndLog("Usage: hf 14b raw [-r] [-c] [-p] <0A 0B 0C ... hex>");
251 PrintAndLog(" -r do not read response");
252 PrintAndLog(" -c calculate and append CRC");
253 PrintAndLog(" -p leave the field on after receive");
258 while (*Cmd
==' ' || *Cmd
=='\t') Cmd
++;
260 while (Cmd
[i
]!='\0') {
261 if (Cmd
[i
]==' ' || Cmd
[i
]=='\t') { i
++; continue; }
277 PrintAndLog("Invalid option");
283 if ((Cmd
[i
]>='0' && Cmd
[i
]<='9') ||
284 (Cmd
[i
]>='a' && Cmd
[i
]<='f') ||
285 (Cmd
[i
]>='A' && Cmd
[i
]<='F') ) {
286 buf
[strlen(buf
)+1]=0;
287 buf
[strlen(buf
)]=Cmd
[i
];
290 if (strlen(buf
)>=2) {
291 sscanf(buf
,"%x",&temp
);
292 data
[datalen
++]=(uint8_t)(temp
& 0xff);
297 PrintAndLog("Invalid char on input");
302 PrintAndLog("Missing data input");
306 return HF14BCmdRaw(reply
, &crc
, power
, data
, &datalen
, true);
310 void print_atqb_resp(uint8_t *data
){
311 PrintAndLog (" UID: %s", sprint_hex(data
+1,4));
312 PrintAndLog (" App Data: %s", sprint_hex(data
+5,4));
313 PrintAndLog (" Protocol: %s", sprint_hex(data
+9,3));
314 uint8_t BitRate
= data
[9];
316 PrintAndLog (" Bit Rate: 106 kbit/s only PICC <-> PCD");
318 PrintAndLog (" Bit Rate: 212 kbit/s PICC -> PCD supported");
320 PrintAndLog (" Bit Rate: 424 kbit/s PICC -> PCD supported");
322 PrintAndLog (" Bit Rate: 847 kbit/s PICC -> PCD supported");
324 PrintAndLog (" Bit Rate: 212 kbit/s PICC <- PCD supported");
326 PrintAndLog (" Bit Rate: 424 kbit/s PICC <- PCD supported");
328 PrintAndLog (" Bit Rate: 847 kbit/s PICC <- PCD supported");
330 PrintAndLog (" Same bit rate <-> required");
332 uint16_t maxFrame
= data
[10]>>4;
334 maxFrame
= 8*maxFrame
+ 16;
335 else if (maxFrame
== 5)
337 else if (maxFrame
== 6)
339 else if (maxFrame
== 7)
341 else if (maxFrame
== 8)
346 PrintAndLog ("Max Frame Size: %d%s",maxFrame
, (maxFrame
== 257) ? "+ RFU" : "");
348 uint8_t protocolT
= data
[10] & 0xF;
349 PrintAndLog (" Protocol Type: Protocol is %scompliant with ISO/IEC 14443-4",(protocolT
) ? "" : "not " );
350 PrintAndLog ("Frame Wait Int: %d", data
[11]>>4);
351 PrintAndLog (" App Data Code: Application is %s",(data
[11]&4) ? "Standard" : "Proprietary");
352 PrintAndLog (" Frame Options: NAD is %ssupported",(data
[11]&2) ? "" : "not ");
353 PrintAndLog (" Frame Options: CID is %ssupported",(data
[11]&1) ? "" : "not ");
358 int HF14BStdRead(uint8_t *data
, uint8_t *datalen
){
368 int ans
= HF14BCmdRaw(true, &crc
, false, data
, datalen
, false);
371 if (data
[0] != 0x50 || *datalen
< 14 || !crc
) return 0;
373 PrintAndLog ("\n14443-3b tag found:");
374 print_atqb_resp(data
);
379 int HF14B_ST_Read(uint8_t *data
, uint8_t *datalen
){
385 int ans
= HF14BCmdRaw(true, &crc
, true, data
, datalen
, false);
388 if (*datalen
< 3 || !crc
) return 0;
390 uint8_t chipID
= data
[0];
394 ans
= HF14BCmdRaw(true, &crc
, true, data
, datalen
, false);
397 if (*datalen
< 3 || !crc
) return 0;
401 ans
= HF14BCmdRaw(true, &crc
, false, data
, datalen
, false);
404 if (*datalen
< 10 || !crc
) return 0;
406 PrintAndLog ("14443-3b ST tag found");
407 //uid = first 8 bytes in data
408 PrintAndLog ("UID: %s", sprint_hex(data
,8));
413 int CmdHF14BReader(const char *Cmd
)
418 // try std 14b (atqb)
419 int ans
= HF14BStdRead(data
, &datalen
);
423 ans
= HF14B_ST_Read(data
, &datalen
);
426 //UsbCommand c = {CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443, {strtol(Cmd, NULL, 0), 0, 0}};
431 int CmdHF14BWrite( const char *Cmd
){
434 * For SRIX4K blocks 00 - 7F
435 * hf 14b raw -c -p 09 $srix4kwblock $srix4kwdata
437 * For SR512 blocks 00 - 0F
438 * hf 14b raw -c -p 09 $sr512wblock $sr512wdata
440 * Special block FF = otp_lock_reg block.
443 char cmdp
= param_getchar(Cmd
, 0);
444 uint8_t blockno
= -1;
445 uint8_t data
[4] = {0x00};
446 bool isSrix4k
= true;
449 if (strlen(Cmd
) < 1 || cmdp
== 'h' || cmdp
== 'H') {
450 PrintAndLog("Usage: hf 14b write <1|2> <BLOCK> <DATA>");
451 PrintAndLog(" [1 = SRIX4K]");
452 PrintAndLog(" [2 = SRI512]");
453 PrintAndLog(" [BLOCK number depends on tag, special block == FF]");
454 PrintAndLog(" sample: hf 14b write 1 7F 11223344");
455 PrintAndLog(" : hf 14b write 1 FF 11223344");
456 PrintAndLog(" : hf 14b write 2 15 11223344");
457 PrintAndLog(" : hf 14b write 2 FF 11223344");
464 //blockno = param_get8(Cmd, 1);
466 if ( param_gethex(Cmd
,1, &blockno
, 2) ) {
467 PrintAndLog("Block number must include 2 HEX symbols");
472 if ( blockno
> 0x7f && blockno
!= 0xff ){
473 PrintAndLog("Block number out of range");
477 if ( blockno
> 0x0f && blockno
!= 0xff ){
478 PrintAndLog("Block number out of range");
483 if (param_gethex(Cmd
, 2, data
, 8)) {
484 PrintAndLog("Data must include 8 HEX symbols");
488 if ( blockno
== 0xff)
489 PrintAndLog("[%s] Write special block %02X [ %s ]", (isSrix4k
)?"SRIX4K":"SRI512" , blockno
, sprint_hex(data
,4) );
491 PrintAndLog("[%s] Write block %02X [ %s ]", (isSrix4k
)?"SRIX4K":"SRI512", blockno
, sprint_hex(data
,4) );
493 sprintf(str
, "-c 09 %02x %02x%02x%02x%02x", blockno
, data
[0], data
[1], data
[2], data
[3]);
499 static command_t CommandTable
[] =
501 {"help", CmdHelp
, 1, "This help"},
502 {"demod", CmdHF14BDemod
, 1, "Demodulate ISO14443 Type B from tag"},
503 {"list", CmdHF14BList
, 0, "[Deprecated] List ISO 14443b history"},
504 {"reader", CmdHF14BReader
, 0, "Find 14b tag (HF ISO 14443b)"},
505 {"sim", CmdHF14Sim
, 0, "Fake ISO 14443 tag"},
506 {"simlisten", CmdHFSimlisten
, 0, "Get HF samples as fake tag"},
507 {"snoop", CmdHF14BSnoop
, 0, "Eavesdrop ISO 14443"},
508 {"sri512read", CmdSri512Read
, 0, "Read contents of a SRI512 tag"},
509 {"srix4kread", CmdSrix4kRead
, 0, "Read contents of a SRIX4K tag"},
510 {"raw", CmdHF14BCmdRaw
, 0, "Send raw hex data to tag"},
511 {"write", CmdHF14BWrite
, 0, "Write data to a SRI512 | SRIX4K tag"},
512 {NULL
, NULL
, 0, NULL
}
515 int CmdHF14B(const char *Cmd
)
517 CmdsParse(CommandTable
, Cmd
);
521 int CmdHelp(const char *Cmd
)
523 CmdsHelp(CommandTable
);