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