]> git.zerfleddert.de Git - proxmark3-svn/blame - client/cmdhf15.c
Client cleanup and restructuring. Stage 1...
[proxmark3-svn] / client / cmdhf15.c
CommitLineData
7fe9b0b7 1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <stdint.h>
5#include "proxusb.h"
6#include "data.h"
7#include "graph.h"
8#include "ui.h"
9#include "cmdparser.h"
10#include "cmdhf15.h"
11
12static int CmdHelp(const char *Cmd);
13
14static uint16_t Iso15693Crc(uint8_t *v, int n)
15{
16 uint32_t reg;
17 int i, j;
18
19 reg = 0xffff;
20 for (i = 0; i < n; i++) {
21 reg = reg ^ ((uint32_t)v[i]);
22 for (j = 0; j < 8; j++) {
23 if (reg & 0x0001) {
24 reg = (reg >> 1) ^ 0x8408;
25 } else {
26 reg = (reg >> 1);
27 }
28 }
29 }
30
31 return (uint16_t)~reg;
32}
33
34int CmdHF15Demod(const char *Cmd)
35{
36 // The sampling rate is 106.353 ksps/s, for T = 18.8 us
37
38 // SOF defined as
39 // 1) Unmodulated time of 56.64us
40 // 2) 24 pulses of 423.75khz
41 // 3) logic '1' (unmodulated for 18.88us followed by 8 pulses of 423.75khz)
42
43 static const int FrameSOF[] = {
44 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
45 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
46 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
47 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
48 -1, -1, -1, -1,
49 -1, -1, -1, -1,
50 1, 1, 1, 1,
51 1, 1, 1, 1
52 };
53 static const int Logic0[] = {
54 1, 1, 1, 1,
55 1, 1, 1, 1,
56 -1, -1, -1, -1,
57 -1, -1, -1, -1
58 };
59 static const int Logic1[] = {
60 -1, -1, -1, -1,
61 -1, -1, -1, -1,
62 1, 1, 1, 1,
63 1, 1, 1, 1
64 };
65
66 // EOF defined as
67 // 1) logic '0' (8 pulses of 423.75khz followed by unmodulated for 18.88us)
68 // 2) 24 pulses of 423.75khz
69 // 3) Unmodulated time of 56.64us
70
71 static const int FrameEOF[] = {
72 1, 1, 1, 1,
73 1, 1, 1, 1,
74 -1, -1, -1, -1,
75 -1, -1, -1, -1,
76 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
77 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
78 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
79 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
80 };
81
82 int i, j;
83 int max = 0, maxPos;
84
85 int skip = 4;
86
87 if (GraphTraceLen < 1000) return 0;
88
89 // First, correlate for SOF
90 for (i = 0; i < 100; i++) {
91 int corr = 0;
92 for (j = 0; j < arraylen(FrameSOF); j += skip) {
93 corr += FrameSOF[j] * GraphBuffer[i + (j / skip)];
94 }
95 if (corr > max) {
96 max = corr;
97 maxPos = i;
98 }
99 }
100 PrintAndLog("SOF at %d, correlation %d", maxPos,
101 max / (arraylen(FrameSOF) / skip));
102
103 i = maxPos + arraylen(FrameSOF) / skip;
104 int k = 0;
105 uint8_t outBuf[20];
106 memset(outBuf, 0, sizeof(outBuf));
107 uint8_t mask = 0x01;
108 for (;;) {
109 int corr0 = 0, corr1 = 0, corrEOF = 0;
110 for (j = 0; j < arraylen(Logic0); j += skip) {
111 corr0 += Logic0[j] * GraphBuffer[i + (j / skip)];
112 }
113 for (j = 0; j < arraylen(Logic1); j += skip) {
114 corr1 += Logic1[j] * GraphBuffer[i + (j / skip)];
115 }
116 for (j = 0; j < arraylen(FrameEOF); j += skip) {
117 corrEOF += FrameEOF[j] * GraphBuffer[i + (j / skip)];
118 }
119 // Even things out by the length of the target waveform.
120 corr0 *= 4;
121 corr1 *= 4;
122
123 if (corrEOF > corr1 && corrEOF > corr0) {
124 PrintAndLog("EOF at %d", i);
125 break;
126 } else if (corr1 > corr0) {
127 i += arraylen(Logic1) / skip;
128 outBuf[k] |= mask;
129 } else {
130 i += arraylen(Logic0) / skip;
131 }
132 mask <<= 1;
133 if (mask == 0) {
134 k++;
135 mask = 0x01;
136 }
137 if ((i + (int)arraylen(FrameEOF)) >= GraphTraceLen) {
138 PrintAndLog("ran off end!");
139 break;
140 }
141 }
142 if (mask != 0x01) {
143 PrintAndLog("error, uneven octet! (discard extra bits!)");
144 PrintAndLog(" mask=%02x", mask);
145 }
146 PrintAndLog("%d octets", k);
147
148 for (i = 0; i < k; i++) {
149 PrintAndLog("# %2d: %02x ", i, outBuf[i]);
150 }
151 PrintAndLog("CRC=%04x", Iso15693Crc(outBuf, k - 2));
152 return 0;
153}
154
155int CmdHF15Read(const char *Cmd)
156{
157 UsbCommand c = {CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_15693};
158 SendCommand(&c);
159 return 0;
160}
161
162int CmdHF15Reader(const char *Cmd)
163{
164 UsbCommand c = {CMD_READER_ISO_15693, {strtol(Cmd, NULL, 0), 0, 0}};
165 SendCommand(&c);
166 return 0;
167}
168
169int CmdHF15Sim(const char *Cmd)
170{
171 UsbCommand c = {CMD_SIMTAG_ISO_15693, {strtol(Cmd, NULL, 0), 0, 0}};
172 SendCommand(&c);
173 return 0;
174}
175
176static command_t CommandTable[] =
177{
178 {"help", CmdHelp, 1, "This help"},
179 {"demod", CmdHF15Demod, 1, "Demodulate ISO15693 from tag"},
180 {"read", CmdHF15Read, 0, "Read HF tag (ISO 15693)"},
181 {"reader", CmdHF15Reader, 0, "Act like an ISO15693 reader"},
182 {"sim", CmdHF15Sim, 0, "Fake an ISO15693 tag"},
183 {NULL, NULL, 0, NULL}
184};
185
186int CmdHF15(const char *Cmd)
187{
188 CmdsParse(CommandTable, Cmd);
189 return 0;
190}
191
192int CmdHelp(const char *Cmd)
193{
194 CmdsHelp(CommandTable);
195 return 0;
196}
Impressum, Datenschutz