]> git.zerfleddert.de Git - proxmark3-svn/blame - client/cmdhf14b.c
Changed size of version info
[proxmark3-svn] / client / cmdhf14b.c
CommitLineData
a553f267 1//-----------------------------------------------------------------------------
2// Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
3//
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
6// the license.
7//-----------------------------------------------------------------------------
8// High frequency ISO14443B commands
9//-----------------------------------------------------------------------------
10
7fe9b0b7 11#include <stdio.h>
12#include <stdlib.h>
13#include <stdbool.h>
14#include <string.h>
15#include <stdint.h>
16#include "iso14443crc.h"
28fdb04f 17//#include "proxusb.h"
902cb3c0 18#include "proxmark3.h"
7fe9b0b7 19#include "data.h"
20#include "graph.h"
21#include "ui.h"
22#include "cmdparser.h"
23#include "cmdhf14b.h"
7cf3ef20 24#include "cmdmain.h"
7fe9b0b7 25
26static int CmdHelp(const char *Cmd);
27
28int CmdHF14BDemod(const char *Cmd)
29{
30 int i, j, iold;
31 int isum, qsum;
32 int outOfWeakAt;
33 bool negateI, negateQ;
34
35 uint8_t data[256];
36 int dataLen = 0;
37
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.
41
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) {
45 break;
46 }
47 }
48 if (i >= GraphTraceLen) {
49 PrintAndLog("too weak to sync");
50 return 0;
51 }
52 PrintAndLog("out of weak at %d", i);
53 outOfWeakAt = i;
54
55 // Now, estimate the phase in the initial modulation of the tag
56 isum = 0;
57 qsum = 0;
58 for (; i < (outOfWeakAt + 16); i += 2) {
59 isum += GraphBuffer[i + 0];
60 qsum += GraphBuffer[i + 1];
61 }
62 negateI = (isum < 0);
63 negateQ = (qsum < 0);
64
65 // Turn the correlation pairs into soft decisions on the bit.
66 j = 0;
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;
73 j += 2;
74 }
75 GraphTraceLen = i;
76
77 i = outOfWeakAt / 2;
78 while (GraphBuffer[i] > 0 && i < GraphTraceLen)
79 i++;
80 if (i >= GraphTraceLen) goto demodError;
81
82 iold = i;
83 while (GraphBuffer[i] < 0 && i < GraphTraceLen)
84 i++;
85 if (i >= GraphTraceLen) goto demodError;
86 if ((i - iold) > 23) goto demodError;
87
88 PrintAndLog("make it to demod loop");
89
90 for (;;) {
91 iold = i;
92 while (GraphBuffer[i] >= 0 && i < GraphTraceLen)
93 i++;
94 if (i >= GraphTraceLen) goto demodError;
95 if ((i - iold) > 6) goto demodError;
96
97 uint16_t shiftReg = 0;
98 if (i + 20 >= GraphTraceLen) goto demodError;
99
100 for (j = 0; j < 10; j++) {
101 int soft = GraphBuffer[i] + GraphBuffer[i + 1];
102
103 if (abs(soft) < (abs(isum) + abs(qsum)) / 20) {
104 PrintAndLog("weak bit");
105 }
106
107 shiftReg >>= 1;
108 if(GraphBuffer[i] + GraphBuffer[i+1] >= 0) {
109 shiftReg |= 0x200;
110 }
111
112 i+= 2;
113 }
114
115 if ((shiftReg & 0x200) && !(shiftReg & 0x001))
116 {
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)) {
121 return 0;
122 }
123 } else if (shiftReg == 0x000) {
124 // this is EOF
125 break;
126 } else {
127 goto demodError;
128 }
129 }
130
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****");
136
137 RepaintGraphWindow();
138 return 0;
139
140demodError:
141 PrintAndLog("demod error");
142 RepaintGraphWindow();
143 return 0;
144}
145
146int CmdHF14BList(const char *Cmd)
147{
148 uint8_t got[960];
db09cb3a 149 GetFromBigBuf(got,sizeof(got),0);
869cabf7 150 WaitForResponse(CMD_ACK,NULL);
7fe9b0b7 151
152 PrintAndLog("recorded activity:");
153 PrintAndLog(" time :rssi: who bytes");
154 PrintAndLog("---------+----+----+-----------");
155
156 int i = 0;
157 int prev = -1;
158
159 for(;;) {
160 if(i >= 900) {
161 break;
162 }
163
164 bool isResponse;
165 int timestamp = *((uint32_t *)(got+i));
166 if(timestamp & 0x80000000) {
167 timestamp &= 0x7fffffff;
168 isResponse = 1;
169 } else {
170 isResponse = 0;
171 }
172 int metric = *((uint32_t *)(got+i+4));
173
174 int len = got[i+8];
175
176 if(len > 100) {
177 break;
178 }
179 if(i + len >= 900) {
180 break;
181 }
182
183 uint8_t *frame = (got+i+9);
184
7bc95e2e 185 // Break and stick with current result if buffer was not completely full
186 if (frame[0] == 0x44 && frame[1] == 0x44 && frame[2] == 0x44 && frame[3] == 0x44) break;
187
7fe9b0b7 188 char line[1000] = "";
189 int j;
190 for(j = 0; j < len; j++) {
191 sprintf(line+(j*3), "%02x ", frame[j]);
192 }
193
194 char *crc;
195 if(len > 2) {
196 uint8_t b1, b2;
197 ComputeCrc14443(CRC_14443_B, frame, len-2, &b1, &b2);
198 if(b1 != frame[len-2] || b2 != frame[len-1]) {
199 crc = "**FAIL CRC**";
200 } else {
201 crc = "";
202 }
203 } else {
204 crc = "(SHORT)";
205 }
206
207 char metricString[100];
208 if(isResponse) {
209 sprintf(metricString, "%3d", metric);
210 } else {
211 strcpy(metricString, " ");
212 }
213
214 PrintAndLog(" +%7d: %s: %s %s %s",
215 (prev < 0 ? 0 : timestamp - prev),
216 metricString,
217 (isResponse ? "TAG" : " "), line, crc);
218
219 prev = timestamp;
220 i += (len + 9);
221 }
222 return 0;
223}
224
225int CmdHF14BRead(const char *Cmd)
226{
227 UsbCommand c = {CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443, {strtol(Cmd, NULL, 0), 0, 0}};
228 SendCommand(&c);
229 return 0;
230}
231
232int CmdHF14Sim(const char *Cmd)
233{
234 UsbCommand c={CMD_SIMULATE_TAG_ISO_14443};
235 SendCommand(&c);
236 return 0;
237}
238
239int CmdHFSimlisten(const char *Cmd)
240{
241 UsbCommand c = {CMD_SIMULATE_TAG_HF_LISTEN};
242 SendCommand(&c);
243 return 0;
244}
245
246int CmdHF14BSnoop(const char *Cmd)
247{
248 UsbCommand c = {CMD_SNOOP_ISO_14443};
249 SendCommand(&c);
250 return 0;
251}
252
253/* New command to read the contents of a SRI512 tag
254 * SRI512 tags are ISO14443-B modulated memory tags,
255 * this command just dumps the contents of the memory
256 */
257int CmdSri512Read(const char *Cmd)
258{
259 UsbCommand c = {CMD_READ_SRI512_TAG, {strtol(Cmd, NULL, 0), 0, 0}};
260 SendCommand(&c);
261 return 0;
262}
263
264/* New command to read the contents of a SRIX4K tag
265 * SRIX4K tags are ISO14443-B modulated memory tags,
266 * this command just dumps the contents of the memory/
267 */
268int CmdSrix4kRead(const char *Cmd)
269{
270 UsbCommand c = {CMD_READ_SRIX4K_TAG, {strtol(Cmd, NULL, 0), 0, 0}};
271 SendCommand(&c);
272 return 0;
273}
274
7cf3ef20 275int CmdHF14BCmdRaw (const char *cmd) {
276 UsbCommand resp;
277 uint8_t *recv;
278 UsbCommand c = {CMD_ISO_14443B_COMMAND, {0, 0, 0}}; // len,recv?
279 uint8_t reply=1;
280 uint8_t crc=0;
281 uint8_t power=0;
282 char buf[5]="";
283 int i=0;
284 uint8_t data[100];
285 unsigned int datalen=0, temp;
286 char *hexout;
287
288 if (strlen(cmd)<3) {
289 PrintAndLog("Usage: hf 14b raw [-r] [-c] [-p] <0A 0B 0C ... hex>");
290 PrintAndLog(" -r do not read response");
291 PrintAndLog(" -c calculate and append CRC");
292 PrintAndLog(" -p leave the field on after receive");
293 return 0;
294 }
295
296 // strip
297 while (*cmd==' ' || *cmd=='\t') cmd++;
298
299 while (cmd[i]!='\0') {
300 if (cmd[i]==' ' || cmd[i]=='\t') { i++; continue; }
301 if (cmd[i]=='-') {
302 switch (cmd[i+1]) {
303 case 'r':
304 case 'R':
305 reply=0;
306 break;
307 case 'c':
308 case 'C':
309 crc=1;
310 break;
311 case 'p':
312 case 'P':
313 power=1;
314 break;
315 default:
316 PrintAndLog("Invalid option");
317 return 0;
318 }
319 i+=2;
320 continue;
321 }
322 if ((cmd[i]>='0' && cmd[i]<='9') ||
323 (cmd[i]>='a' && cmd[i]<='f') ||
324 (cmd[i]>='A' && cmd[i]<='F') ) {
325 buf[strlen(buf)+1]=0;
326 buf[strlen(buf)]=cmd[i];
327 i++;
328
329 if (strlen(buf)>=2) {
330 sscanf(buf,"%x",&temp);
331 data[datalen]=(uint8_t)(temp & 0xff);
332 datalen++;
333 *buf=0;
334 }
335 continue;
336 }
337 PrintAndLog("Invalid char on input");
338 return 0;
339 }
06b82e6a 340 if (datalen == 0)
341 {
342 PrintAndLog("Missing data input");
343 return 0;
344 }
7cf3ef20 345 if(crc)
346 {
347 uint8_t first, second;
348 ComputeCrc14443(CRC_14443_B, data, datalen, &first, &second);
349 data[datalen++] = first;
350 data[datalen++] = second;
351 }
352
353 c.arg[0] = datalen;
354 c.arg[1] = reply;
355 c.arg[2] = power;
356 memcpy(c.d.asBytes,data,datalen);
357
358 SendCommand(&c);
359
360 if (reply) {
361 if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) {
362 recv = resp.d.asBytes;
363 PrintAndLog("received %i octets",resp.arg[0]);
364 if(!resp.arg[0])
365 return 0;
366 hexout = (char *)malloc(resp.arg[0] * 3 + 1);
367 if (hexout != NULL) {
368 uint8_t first, second;
369 for (int i = 0; i < resp.arg[0]; i++) { // data in hex
7bb9d33e 370 sprintf(&hexout[i * 3], "%02X ", recv[i]);
7cf3ef20 371 }
372 PrintAndLog("%s", hexout);
373 free(hexout);
374 ComputeCrc14443(CRC_14443_B, recv, resp.arg[0]-2, &first, &second);
375 if(recv[resp.arg[0]-2]==first && recv[resp.arg[0]-1]==second) {
376 PrintAndLog("CRC OK");
377 } else {
378 PrintAndLog("CRC failed");
379 }
380 } else {
381 PrintAndLog("malloc failed your client has low memory?");
382 }
383 } else {
384 PrintAndLog("timeout while waiting for reply.");
385 }
386 } // if reply
387 return 0;
388}
389
7fe9b0b7 390static command_t CommandTable[] =
391{
392 {"help", CmdHelp, 1, "This help"},
393 {"demod", CmdHF14BDemod, 1, "Demodulate ISO14443 Type B from tag"},
394 {"list", CmdHF14BList, 0, "List ISO 14443 history"},
395 {"read", CmdHF14BRead, 0, "Read HF tag (ISO 14443)"},
396 {"sim", CmdHF14Sim, 0, "Fake ISO 14443 tag"},
397 {"simlisten", CmdHFSimlisten, 0, "Get HF samples as fake tag"},
398 {"snoop", CmdHF14BSnoop, 0, "Eavesdrop ISO 14443"},
7cf3ef20 399 {"sri512read", CmdSri512Read, 0, "Read contents of a SRI512 tag"},
400 {"srix4kread", CmdSrix4kRead, 0, "Read contents of a SRIX4K tag"},
401 {"raw", CmdHF14BCmdRaw, 0, "Send raw hex data to tag"},
7fe9b0b7 402 {NULL, NULL, 0, NULL}
403};
404
405int CmdHF14B(const char *Cmd)
406{
407 CmdsParse(CommandTable, Cmd);
408 return 0;
409}
410
411int CmdHelp(const char *Cmd)
412{
413 CmdsHelp(CommandTable);
414 return 0;
415}
Impressum, Datenschutz