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 "iso14443crc.h"
21 #include "cmdparser.h"
24 static int CmdHelp(const char *Cmd
);
26 int CmdHF14BDemod(const char *Cmd
)
31 bool negateI
, negateQ
;
36 // As received, the samples are pairs, correlations against I and Q
37 // square waves. So estimate angle of initial carrier (or just
38 // quadrant, actually), and then do the demod.
40 // First, estimate where the tag starts modulating.
41 for (i
= 0; i
< GraphTraceLen
; i
+= 2) {
42 if (abs(GraphBuffer
[i
]) + abs(GraphBuffer
[i
+ 1]) > 40) {
46 if (i
>= GraphTraceLen
) {
47 PrintAndLog("too weak to sync");
50 PrintAndLog("out of weak at %d", i
);
53 // Now, estimate the phase in the initial modulation of the tag
56 for (; i
< (outOfWeakAt
+ 16); i
+= 2) {
57 isum
+= GraphBuffer
[i
+ 0];
58 qsum
+= GraphBuffer
[i
+ 1];
63 // Turn the correlation pairs into soft decisions on the bit.
65 for (i
= 0; i
< GraphTraceLen
/ 2; i
++) {
66 int si
= GraphBuffer
[j
];
67 int sq
= GraphBuffer
[j
+ 1];
68 if (negateI
) si
= -si
;
69 if (negateQ
) sq
= -sq
;
70 GraphBuffer
[i
] = si
+ sq
;
76 while (GraphBuffer
[i
] > 0 && i
< GraphTraceLen
)
78 if (i
>= GraphTraceLen
) goto demodError
;
81 while (GraphBuffer
[i
] < 0 && i
< GraphTraceLen
)
83 if (i
>= GraphTraceLen
) goto demodError
;
84 if ((i
- iold
) > 23) goto demodError
;
86 PrintAndLog("make it to demod loop");
90 while (GraphBuffer
[i
] >= 0 && i
< GraphTraceLen
)
92 if (i
>= GraphTraceLen
) goto demodError
;
93 if ((i
- iold
) > 6) goto demodError
;
95 uint16_t shiftReg
= 0;
96 if (i
+ 20 >= GraphTraceLen
) goto demodError
;
98 for (j
= 0; j
< 10; j
++) {
99 int soft
= GraphBuffer
[i
] + GraphBuffer
[i
+ 1];
101 if (abs(soft
) < (abs(isum
) + abs(qsum
)) / 20) {
102 PrintAndLog("weak bit");
106 if(GraphBuffer
[i
] + GraphBuffer
[i
+1] >= 0) {
113 if ((shiftReg
& 0x200) && !(shiftReg
& 0x001))
115 // valid data byte, start and stop bits okay
116 PrintAndLog(" %02x", (shiftReg
>> 1) & 0xff);
117 data
[dataLen
++] = (shiftReg
>> 1) & 0xff;
118 if (dataLen
>= sizeof(data
)) {
121 } else if (shiftReg
== 0x000) {
129 uint8_t first
, second
;
130 ComputeCrc14443(CRC_14443_B
, data
, dataLen
-2, &first
, &second
);
131 PrintAndLog("CRC: %02x %02x (%s)\n", first
, second
,
132 (first
== data
[dataLen
-2] && second
== data
[dataLen
-1]) ?
133 "ok" : "****FAIL****");
135 RepaintGraphWindow();
139 PrintAndLog("demod error");
140 RepaintGraphWindow();
144 int CmdHF14BList(const char *Cmd
)
147 GetFromBigBuf(got
,sizeof(got
),0);
149 PrintAndLog("recorded activity:");
150 PrintAndLog(" time :rssi: who bytes");
151 PrintAndLog("---------+----+----+-----------");
162 int timestamp
= *((uint32_t *)(got
+i
));
163 if(timestamp
& 0x80000000) {
164 timestamp
&= 0x7fffffff;
169 int metric
= *((uint32_t *)(got
+i
+4));
180 uint8_t *frame
= (got
+i
+9);
182 char line
[1000] = "";
184 for(j
= 0; j
< len
; j
++) {
185 sprintf(line
+(j
*3), "%02x ", frame
[j
]);
191 ComputeCrc14443(CRC_14443_B
, frame
, len
-2, &b1
, &b2
);
192 if(b1
!= frame
[len
-2] || b2
!= frame
[len
-1]) {
193 crc
= "**FAIL CRC**";
201 char metricString
[100];
203 sprintf(metricString
, "%3d", metric
);
205 strcpy(metricString
, " ");
208 PrintAndLog(" +%7d: %s: %s %s %s",
209 (prev
< 0 ? 0 : timestamp
- prev
),
211 (isResponse
? "TAG" : " "), line
, crc
);
219 int CmdHF14BRead(const char *Cmd
)
221 UsbCommand c
= {CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443
, {strtol(Cmd
, NULL
, 0), 0, 0}};
226 int CmdHF14Sim(const char *Cmd
)
228 UsbCommand c
={CMD_SIMULATE_TAG_ISO_14443
};
233 int CmdHFSimlisten(const char *Cmd
)
235 UsbCommand c
= {CMD_SIMULATE_TAG_HF_LISTEN
};
240 int CmdHF14BSnoop(const char *Cmd
)
242 UsbCommand c
= {CMD_SNOOP_ISO_14443
};
247 /* New command to read the contents of a SRI512 tag
248 * SRI512 tags are ISO14443-B modulated memory tags,
249 * this command just dumps the contents of the memory
251 int CmdSri512Read(const char *Cmd
)
253 UsbCommand c
= {CMD_READ_SRI512_TAG
, {strtol(Cmd
, NULL
, 0), 0, 0}};
258 /* New command to read the contents of a SRIX4K tag
259 * SRIX4K tags are ISO14443-B modulated memory tags,
260 * this command just dumps the contents of the memory/
262 int CmdSrix4kRead(const char *Cmd
)
264 UsbCommand c
= {CMD_READ_SRIX4K_TAG
, {strtol(Cmd
, NULL
, 0), 0, 0}};
269 static command_t CommandTable
[] =
271 {"help", CmdHelp
, 1, "This help"},
272 {"demod", CmdHF14BDemod
, 1, "Demodulate ISO14443 Type B from tag"},
273 {"list", CmdHF14BList
, 0, "List ISO 14443 history"},
274 {"read", CmdHF14BRead
, 0, "Read HF tag (ISO 14443)"},
275 {"sim", CmdHF14Sim
, 0, "Fake ISO 14443 tag"},
276 {"simlisten", CmdHFSimlisten
, 0, "Get HF samples as fake tag"},
277 {"snoop", CmdHF14BSnoop
, 0, "Eavesdrop ISO 14443"},
278 {"sri512read", CmdSri512Read
, 0, "<int> -- Read contents of a SRI512 tag"},
279 {"srix4kread", CmdSrix4kRead
, 0, "<int> -- Read contents of a SRIX4K tag"},
280 {NULL
, NULL
, 0, NULL
}
283 int CmdHF14B(const char *Cmd
)
285 CmdsParse(CommandTable
, Cmd
);
289 int CmdHelp(const char *Cmd
)
291 CmdsHelp(CommandTable
);