]> git.zerfleddert.de Git - proxmark3-svn/blame - client/cmdhf14b.c
Minor refactoring
[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"
902cb3c0 17#include "proxmark3.h"
7fe9b0b7 18#include "data.h"
19#include "graph.h"
3fe4ff4f 20#include "util.h"
7fe9b0b7 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{
388c92bd
MHS
148 PrintAndLog("Deprecated command, use 'hf list 14b' instead");
149 return 0;
7fe9b0b7 150}
7fe9b0b7 151int CmdHF14BRead(const char *Cmd)
152{
153 UsbCommand c = {CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443, {strtol(Cmd, NULL, 0), 0, 0}};
154 SendCommand(&c);
155 return 0;
156}
157
158int CmdHF14Sim(const char *Cmd)
159{
160 UsbCommand c={CMD_SIMULATE_TAG_ISO_14443};
161 SendCommand(&c);
162 return 0;
163}
164
165int CmdHFSimlisten(const char *Cmd)
166{
167 UsbCommand c = {CMD_SIMULATE_TAG_HF_LISTEN};
168 SendCommand(&c);
169 return 0;
170}
171
172int CmdHF14BSnoop(const char *Cmd)
173{
174 UsbCommand c = {CMD_SNOOP_ISO_14443};
175 SendCommand(&c);
176 return 0;
177}
178
179/* New command to read the contents of a SRI512 tag
180 * SRI512 tags are ISO14443-B modulated memory tags,
181 * this command just dumps the contents of the memory
182 */
183int CmdSri512Read(const char *Cmd)
184{
185 UsbCommand c = {CMD_READ_SRI512_TAG, {strtol(Cmd, NULL, 0), 0, 0}};
186 SendCommand(&c);
187 return 0;
188}
189
190/* New command to read the contents of a SRIX4K tag
191 * SRIX4K tags are ISO14443-B modulated memory tags,
192 * this command just dumps the contents of the memory/
193 */
194int CmdSrix4kRead(const char *Cmd)
195{
196 UsbCommand c = {CMD_READ_SRIX4K_TAG, {strtol(Cmd, NULL, 0), 0, 0}};
197 SendCommand(&c);
198 return 0;
199}
200
7cf3ef20 201int CmdHF14BCmdRaw (const char *cmd) {
202 UsbCommand resp;
203 uint8_t *recv;
204 UsbCommand c = {CMD_ISO_14443B_COMMAND, {0, 0, 0}}; // len,recv?
205 uint8_t reply=1;
206 uint8_t crc=0;
207 uint8_t power=0;
208 char buf[5]="";
209 int i=0;
210 uint8_t data[100];
211 unsigned int datalen=0, temp;
212 char *hexout;
213
214 if (strlen(cmd)<3) {
215 PrintAndLog("Usage: hf 14b raw [-r] [-c] [-p] <0A 0B 0C ... hex>");
216 PrintAndLog(" -r do not read response");
217 PrintAndLog(" -c calculate and append CRC");
218 PrintAndLog(" -p leave the field on after receive");
219 return 0;
220 }
221
222 // strip
223 while (*cmd==' ' || *cmd=='\t') cmd++;
224
225 while (cmd[i]!='\0') {
226 if (cmd[i]==' ' || cmd[i]=='\t') { i++; continue; }
227 if (cmd[i]=='-') {
228 switch (cmd[i+1]) {
229 case 'r':
230 case 'R':
231 reply=0;
232 break;
233 case 'c':
234 case 'C':
235 crc=1;
236 break;
237 case 'p':
238 case 'P':
239 power=1;
240 break;
241 default:
242 PrintAndLog("Invalid option");
243 return 0;
244 }
245 i+=2;
246 continue;
247 }
248 if ((cmd[i]>='0' && cmd[i]<='9') ||
249 (cmd[i]>='a' && cmd[i]<='f') ||
250 (cmd[i]>='A' && cmd[i]<='F') ) {
251 buf[strlen(buf)+1]=0;
252 buf[strlen(buf)]=cmd[i];
253 i++;
254
255 if (strlen(buf)>=2) {
256 sscanf(buf,"%x",&temp);
257 data[datalen]=(uint8_t)(temp & 0xff);
258 datalen++;
259 *buf=0;
260 }
261 continue;
262 }
263 PrintAndLog("Invalid char on input");
264 return 0;
265 }
06b82e6a 266 if (datalen == 0)
267 {
268 PrintAndLog("Missing data input");
269 return 0;
270 }
7cf3ef20 271 if(crc)
272 {
273 uint8_t first, second;
274 ComputeCrc14443(CRC_14443_B, data, datalen, &first, &second);
275 data[datalen++] = first;
276 data[datalen++] = second;
277 }
278
279 c.arg[0] = datalen;
280 c.arg[1] = reply;
281 c.arg[2] = power;
282 memcpy(c.d.asBytes,data,datalen);
283
284 SendCommand(&c);
285
286 if (reply) {
287 if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) {
288 recv = resp.d.asBytes;
289 PrintAndLog("received %i octets",resp.arg[0]);
290 if(!resp.arg[0])
291 return 0;
292 hexout = (char *)malloc(resp.arg[0] * 3 + 1);
293 if (hexout != NULL) {
294 uint8_t first, second;
295 for (int i = 0; i < resp.arg[0]; i++) { // data in hex
7bb9d33e 296 sprintf(&hexout[i * 3], "%02X ", recv[i]);
7cf3ef20 297 }
298 PrintAndLog("%s", hexout);
299 free(hexout);
300 ComputeCrc14443(CRC_14443_B, recv, resp.arg[0]-2, &first, &second);
301 if(recv[resp.arg[0]-2]==first && recv[resp.arg[0]-1]==second) {
302 PrintAndLog("CRC OK");
303 } else {
304 PrintAndLog("CRC failed");
305 }
306 } else {
307 PrintAndLog("malloc failed your client has low memory?");
308 }
309 } else {
310 PrintAndLog("timeout while waiting for reply.");
311 }
312 } // if reply
313 return 0;
314}
315
3fe4ff4f 316int CmdHF14BWrite( const char *Cmd){
317
318/*
319 * For SRIX4K blocks 00 - 7F
320 * hf 14b raw -c -p 09 $srix4kwblock $srix4kwdata
321 *
322 * For SR512 blocks 00 - 0F
323 * hf 14b raw -c -p 09 $sr512wblock $sr512wdata
324 *
325 * Special block FF = otp_lock_reg block.
326 * Data len 4 bytes-
327 */
328 char cmdp = param_getchar(Cmd, 0);
329 uint8_t blockno = -1;
330 uint8_t data[4] = {0x00};
331 bool isSrix4k = true;
332 char str[20];
333
b5be31f9 334 if (strlen(Cmd) < 1 || cmdp == 'h' || cmdp == 'H') {
3fe4ff4f 335 PrintAndLog("Usage: hf 14b write <1|2> <BLOCK> <DATA>");
b5be31f9 336 PrintAndLog(" [1 = SRIX4K]");
337 PrintAndLog(" [2 = SRI512]");
338 PrintAndLog(" [BLOCK number depends on tag, special block == FF]");
339 PrintAndLog(" sample: hf 14b write 1 7F 11223344");
340 PrintAndLog(" : hf 14b write 1 FF 11223344");
341 PrintAndLog(" : hf 14b write 2 15 11223344");
342 PrintAndLog(" : hf 14b write 2 FF 11223344");
3fe4ff4f 343 return 0;
344 }
345
b5be31f9 346 if ( cmdp == '2' )
3fe4ff4f 347 isSrix4k = false;
348
b5be31f9 349 //blockno = param_get8(Cmd, 1);
350
351 if ( param_gethex(Cmd,1, &blockno, 2) ) {
352 PrintAndLog("Block number must include 2 HEX symbols");
353 return 0;
354 }
3fe4ff4f 355
356 if ( isSrix4k ){
357 if ( blockno > 0x7f && blockno != 0xff ){
358 PrintAndLog("Block number out of range");
359 return 0;
360 }
361 } else {
362 if ( blockno > 0x0f && blockno != 0xff ){
363 PrintAndLog("Block number out of range");
364 return 0;
365 }
366 }
367
368 if (param_gethex(Cmd, 2, data, 8)) {
369 PrintAndLog("Data must include 8 HEX symbols");
370 return 0;
371 }
372
373 if ( blockno == 0xff)
b5be31f9 374 PrintAndLog("[%s] Write special block %02X [ %s ]", (isSrix4k)?"SRIX4K":"SRI512" , blockno, sprint_hex(data,4) );
3fe4ff4f 375 else
b5be31f9 376 PrintAndLog("[%s] Write block %02X [ %s ]", (isSrix4k)?"SRIX4K":"SRI512", blockno, sprint_hex(data,4) );
3fe4ff4f 377
378 sprintf(str, "-c -p 09 %02x %02x%02x%02x%02x", blockno, data[0], data[1], data[2], data[3]);
b5be31f9 379
3fe4ff4f 380 CmdHF14BCmdRaw(str);
381 return 0;
382}
383
7fe9b0b7 384static command_t CommandTable[] =
385{
386 {"help", CmdHelp, 1, "This help"},
387 {"demod", CmdHF14BDemod, 1, "Demodulate ISO14443 Type B from tag"},
388c92bd 388 {"list", CmdHF14BList, 0, "[Deprecated] List ISO 14443b history"},
7fe9b0b7 389 {"read", CmdHF14BRead, 0, "Read HF tag (ISO 14443)"},
390 {"sim", CmdHF14Sim, 0, "Fake ISO 14443 tag"},
391 {"simlisten", CmdHFSimlisten, 0, "Get HF samples as fake tag"},
392 {"snoop", CmdHF14BSnoop, 0, "Eavesdrop ISO 14443"},
7cf3ef20 393 {"sri512read", CmdSri512Read, 0, "Read contents of a SRI512 tag"},
394 {"srix4kread", CmdSrix4kRead, 0, "Read contents of a SRIX4K tag"},
395 {"raw", CmdHF14BCmdRaw, 0, "Send raw hex data to tag"},
3fe4ff4f 396 {"write", CmdHF14BWrite, 0, "Write data to a SRI512 | SRIX4K tag"},
7fe9b0b7 397 {NULL, NULL, 0, NULL}
398};
399
400int CmdHF14B(const char *Cmd)
401{
402 CmdsParse(CommandTable, Cmd);
403 return 0;
404}
405
406int CmdHelp(const char *Cmd)
407{
408 CmdsHelp(CommandTable);
409 return 0;
410}
Impressum, Datenschutz