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 "../common/iso14443crc.h"
17 #include "proxmark3.h"
21 #include "cmdparser.h"
25 static int CmdHelp(const char *Cmd
);
27 int CmdHF14BDemod(const char *Cmd
)
32 bool negateI
, negateQ
;
37 // As received, the samples are pairs, correlations against I and Q
38 // square waves. So estimate angle of initial carrier (or just
39 // quadrant, actually), and then do the demod.
41 // First, estimate where the tag starts modulating.
42 for (i
= 0; i
< GraphTraceLen
; i
+= 2) {
43 if (abs(GraphBuffer
[i
]) + abs(GraphBuffer
[i
+ 1]) > 40) {
47 if (i
>= GraphTraceLen
) {
48 PrintAndLog("too weak to sync");
51 PrintAndLog("out of weak at %d", i
);
54 // Now, estimate the phase in the initial modulation of the tag
57 for (; i
< (outOfWeakAt
+ 16); i
+= 2) {
58 isum
+= GraphBuffer
[i
+ 0];
59 qsum
+= GraphBuffer
[i
+ 1];
64 // Turn the correlation pairs into soft decisions on the bit.
66 for (i
= 0; i
< GraphTraceLen
/ 2; i
++) {
67 int si
= GraphBuffer
[j
];
68 int sq
= GraphBuffer
[j
+ 1];
69 if (negateI
) si
= -si
;
70 if (negateQ
) sq
= -sq
;
71 GraphBuffer
[i
] = si
+ sq
;
77 while (GraphBuffer
[i
] > 0 && i
< GraphTraceLen
)
79 if (i
>= GraphTraceLen
) goto demodError
;
82 while (GraphBuffer
[i
] < 0 && i
< GraphTraceLen
)
84 if (i
>= GraphTraceLen
) goto demodError
;
85 if ((i
- iold
) > 23) goto demodError
;
87 PrintAndLog("make it to demod loop");
91 while (GraphBuffer
[i
] >= 0 && i
< GraphTraceLen
)
93 if (i
>= GraphTraceLen
) goto demodError
;
94 if ((i
- iold
) > 6) goto demodError
;
96 uint16_t shiftReg
= 0;
97 if (i
+ 20 >= GraphTraceLen
) goto demodError
;
99 for (j
= 0; j
< 10; j
++) {
100 int soft
= GraphBuffer
[i
] + GraphBuffer
[i
+ 1];
102 if (abs(soft
) < (abs(isum
) + abs(qsum
)) / 20) {
103 PrintAndLog("weak bit");
107 if(GraphBuffer
[i
] + GraphBuffer
[i
+1] >= 0) {
114 if ((shiftReg
& 0x200) && !(shiftReg
& 0x001))
116 // valid data byte, start and stop bits okay
117 PrintAndLog(" %02x", (shiftReg
>> 1) & 0xff);
118 data
[dataLen
++] = (shiftReg
>> 1) & 0xff;
119 if (dataLen
>= sizeof(data
)) {
122 } else if (shiftReg
== 0x000) {
130 uint8_t first
, second
;
131 ComputeCrc14443(CRC_14443_B
, data
, dataLen
-2, &first
, &second
);
132 PrintAndLog("CRC: %02x %02x (%s)\n", first
, second
,
133 (first
== data
[dataLen
-2] && second
== data
[dataLen
-1]) ?
134 "ok" : "****FAIL****");
136 RepaintGraphWindow();
140 PrintAndLog("demod error");
141 RepaintGraphWindow();
145 int CmdHF14BList(const char *Cmd
)
148 GetFromBigBuf(got
,sizeof(got
),0);
149 WaitForResponse(CMD_ACK
,NULL
);
151 PrintAndLog("recorded activity:");
152 PrintAndLog(" time :rssi: who bytes");
153 PrintAndLog("---------+----+----+-----------");
164 int timestamp
= *((uint32_t *)(got
+i
));
165 if(timestamp
& 0x80000000) {
166 timestamp
&= 0x7fffffff;
171 int metric
= *((uint32_t *)(got
+i
+4));
182 uint8_t *frame
= (got
+i
+9);
184 // Break and stick with current result if buffer was not completely full
185 if (frame
[0] == 0x44 && frame
[1] == 0x44 && frame
[2] == 0x44 && frame
[3] == 0x44) break;
187 char line
[1000] = "";
189 for(j
= 0; j
< len
; j
++) {
190 sprintf(line
+(j
*3), "%02x ", frame
[j
]);
196 ComputeCrc14443(CRC_14443_B
, frame
, len
-2, &b1
, &b2
);
197 if(b1
!= frame
[len
-2] || b2
!= frame
[len
-1]) {
198 crc
= "**FAIL CRC**";
206 char metricString
[100];
208 sprintf(metricString
, "%3d", metric
);
210 strcpy(metricString
, " ");
213 PrintAndLog(" +%7d: %s: %s %s %s",
214 (prev
< 0 ? 0 : timestamp
- prev
),
216 (isResponse
? "TAG" : " "), line
, crc
);
224 int CmdHF14BRead(const char *Cmd
)
226 UsbCommand c
= {CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443
, {strtol(Cmd
, NULL
, 0), 0, 0}};
231 int CmdHF14Sim(const char *Cmd
)
233 UsbCommand c
={CMD_SIMULATE_TAG_ISO_14443
};
238 int CmdHFSimlisten(const char *Cmd
)
240 UsbCommand c
= {CMD_SIMULATE_TAG_HF_LISTEN
};
245 int CmdHF14BSnoop(const char *Cmd
)
247 UsbCommand c
= {CMD_SNOOP_ISO_14443
};
252 /* New command to read the contents of a SRI512 tag
253 * SRI512 tags are ISO14443-B modulated memory tags,
254 * this command just dumps the contents of the memory
256 int CmdSri512Read(const char *Cmd
)
258 UsbCommand c
= {CMD_READ_SRI512_TAG
, {strtol(Cmd
, NULL
, 0), 0, 0}};
263 /* New command to read the contents of a SRIX4K tag
264 * SRIX4K tags are ISO14443-B modulated memory tags,
265 * this command just dumps the contents of the memory/
267 int CmdSrix4kRead(const char *Cmd
)
269 UsbCommand c
= {CMD_READ_SRIX4K_TAG
, {strtol(Cmd
, NULL
, 0), 0, 0}};
274 int CmdHF14BCmdRaw (const char *cmd
) {
277 UsbCommand c
= {CMD_ISO_14443B_COMMAND
, {0, 0, 0}}; // len,recv?
284 unsigned int datalen
=0, temp
;
288 PrintAndLog("Usage: hf 14b raw [-r] [-c] [-p] <0A 0B 0C ... hex>");
289 PrintAndLog(" -r do not read response");
290 PrintAndLog(" -c calculate and append CRC");
291 PrintAndLog(" -p leave the field on after receive");
296 while (*cmd
==' ' || *cmd
=='\t') cmd
++;
298 while (cmd
[i
]!='\0') {
299 if (cmd
[i
]==' ' || cmd
[i
]=='\t') { i
++; continue; }
315 PrintAndLog("Invalid option");
321 if ((cmd
[i
]>='0' && cmd
[i
]<='9') ||
322 (cmd
[i
]>='a' && cmd
[i
]<='f') ||
323 (cmd
[i
]>='A' && cmd
[i
]<='F') ) {
324 buf
[strlen(buf
)+1]=0;
325 buf
[strlen(buf
)]=cmd
[i
];
328 if (strlen(buf
)>=2) {
329 sscanf(buf
,"%x",&temp
);
330 data
[datalen
]=(uint8_t)(temp
& 0xff);
336 PrintAndLog("Invalid char on input");
341 PrintAndLog("Missing data input");
346 uint8_t first
, second
;
347 ComputeCrc14443(CRC_14443_B
, data
, datalen
, &first
, &second
);
348 data
[datalen
++] = first
;
349 data
[datalen
++] = second
;
355 memcpy(c
.d
.asBytes
,data
,datalen
);
360 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000)) {
361 recv
= resp
.d
.asBytes
;
362 PrintAndLog("received %i octets",resp
.arg
[0]);
365 hexout
= (char *)malloc(resp
.arg
[0] * 3 + 1);
366 if (hexout
!= NULL
) {
367 uint8_t first
, second
;
368 for (int i
= 0; i
< resp
.arg
[0]; i
++) { // data in hex
369 sprintf(&hexout
[i
* 3], "%02X ", recv
[i
]);
371 PrintAndLog("%s", hexout
);
373 ComputeCrc14443(CRC_14443_B
, recv
, resp
.arg
[0]-2, &first
, &second
);
374 if(recv
[resp
.arg
[0]-2]==first
&& recv
[resp
.arg
[0]-1]==second
) {
375 PrintAndLog("CRC OK");
377 PrintAndLog("CRC failed");
380 PrintAndLog("malloc failed your client has low memory?");
383 PrintAndLog("timeout while waiting for reply.");
389 static command_t CommandTable
[] =
391 {"help", CmdHelp
, 1, "This help"},
392 {"demod", CmdHF14BDemod
, 1, "Demodulate ISO14443 Type B from tag"},
393 {"list", CmdHF14BList
, 0, "List ISO 14443 history"},
394 {"read", CmdHF14BRead
, 0, "Read HF tag (ISO 14443)"},
395 {"sim", CmdHF14Sim
, 0, "Fake ISO 14443 tag"},
396 {"simlisten", CmdHFSimlisten
, 0, "Get HF samples as fake tag"},
397 {"snoop", CmdHF14BSnoop
, 0, "Eavesdrop ISO 14443"},
398 {"sri512read", CmdSri512Read
, 0, "Read contents of a SRI512 tag"},
399 {"srix4kread", CmdSrix4kRead
, 0, "Read contents of a SRIX4K tag"},
400 {"raw", CmdHF14BCmdRaw
, 0, "Send raw hex data to tag"},
401 {NULL
, NULL
, 0, NULL
}
404 int CmdHF14B(const char *Cmd
)
406 CmdsParse(CommandTable
, Cmd
);
410 int CmdHelp(const char *Cmd
)
412 CmdsHelp(CommandTable
);