]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/cmdlffdx.c
split fdx-b demod to its own file
[proxmark3-svn] / client / cmdlffdx.c
CommitLineData
4db6f3bb 1//-----------------------------------------------------------------------------
2//
3// This code is licensed to you under the terms of the GNU GPL, version 2 or,
4// at your option, any later version. See the LICENSE.txt file for the text of
5// the license.
6//-----------------------------------------------------------------------------
7// Low frequency fdx-b tag commands
8//-----------------------------------------------------------------------------
9
10#include "cmdlffdx.h"
11
12#include <inttypes.h>
13#include "proxmark3.h"
14#include "ui.h" // for PrintAndLog
15#include "util.h"
16#include "cmdparser.h"
17#include "cmddata.h"
18#include "cmdmain.h"
19#include "cmdlf.h"
20#include "crc16.h"
21#include "protocols.h"
22#include "lfdemod.h"
23
24/*
25 FDX-B ISO11784/85 demod (aka animal tag) BIPHASE, inverted, rf/32, with preamble of 00000000001 (128bits)
26 8 databits + 1 parity (1)
27 CIITT 16 checksum
28 NATIONAL CODE, ICAR database
29 COUNTRY CODE (ISO3166) or http://cms.abvma.ca/uploads/ManufacturersISOsandCountryCodes.pdf
30 FLAG (animal/non-animal)
31
32 38 IDbits
33 10 country code
34 1 extra app bit
35 14 reserved bits
36 1 animal bit
37 16 ccitt CRC chksum over 64bit ID CODE.
38 24 appli bits.
39
40 sample: 985121004515220 [ 37FF65B88EF94 ]
41*/
42
43static int CmdHelp(const char *Cmd);
44
45int usage_lf_fdx_clone(void){
46 PrintAndLog("Clone a FDX-B animal tag to a T55x7 tag.");
47 PrintAndLog("Usage: lf animal clone [h] <country id> <animal id> <Q5>");
48 PrintAndLog("Options:");
49 PrintAndLog(" h : This help");
50 PrintAndLog(" <country id> : Country id");
51 PrintAndLog(" <animal id> : Animal id");
52 // has extended data?
53 //reserved/rfu
54 //is animal tag
55 // extended data
56 PrintAndLog(" <Q5> : Specify write to Q5 (t5555 instead of t55x7)");
57 PrintAndLog("");
58 PrintAndLog("Sample: lf animal clone 999 112233");
59 return 0;
60}
61
62int usage_lf_fdx_sim(void) {
63 PrintAndLog("Enables simulation of FDX-B animal tag");
64 PrintAndLog("Simulation runs until the button is pressed or another USB command is issued.");
65 PrintAndLog("");
66 PrintAndLog("Usage: lf animal sim [h] <country id> <animal id>");
67 PrintAndLog("Options:");
68 PrintAndLog(" h : This help");
69 PrintAndLog(" <country id> : Country ID");
70 PrintAndLog(" <animal id> : Animal ID");
71 PrintAndLog("");
72 PrintAndLog("Sample: lf animal sim 999 112233");
73 return 0;
74}
75// clearing the topbit needed for the preambl detection.
76static void verify_values(uint32_t countryid, uint64_t animalid){
77 if ((animalid & 0x3FFFFFFFFF) != animalid) {
78 animalid &= 0x3FFFFFFFFF;
79 PrintAndLog("Animal ID Truncated to 38bits: %"PRIx64, animalid);
80 }
81 if ( (countryid & 0x3ff) != countryid ) {
82 countryid &= 0x3ff;
83 PrintAndLog("Country ID Truncated to 10bits: %03d", countryid);
84 }
85}
86
87int getFDXBits(uint64_t national_id, uint16_t country, uint8_t isanimal, uint8_t isextended, uint32_t extended, uint8_t *bits) {
88
89 // 128bits
90 // every 9th bit is 0x01, but we can just fill the rest with 0x01 and overwrite
91 memset(bits, 0x01, 128);
92
93 // add preamble ten 0x00 and one 0x01
94 memset(bits, 0x00, 10);
95
96 // add reserved
97 num_to_bytebitsLSBF(0x00, 7, bits + 66);
98 num_to_bytebitsLSBF(0x00 >> 7, 7, bits + 74);
99
100 // add animal flag - OK
101 bits[65] = isanimal;
102
103 // add extended flag - OK
104 bits[81] = isextended;
105
106 // add national code 40bits - OK
107 num_to_bytebitsLSBF(national_id >> 0, 8, bits+11);
108 num_to_bytebitsLSBF(national_id >> 8, 8, bits+20);
109 num_to_bytebitsLSBF(national_id >> 16, 8, bits+29);
110 num_to_bytebitsLSBF(national_id >> 24, 8, bits+38);
111 num_to_bytebitsLSBF(national_id >> 32, 6, bits+47);
112
113 // add country code - OK
114 num_to_bytebitsLSBF(country >> 0, 2, bits+53);
115 num_to_bytebitsLSBF(country >> 2, 8, bits+56);
116
117 // add crc-16 - OK
118 uint8_t raw[8];
119 for (uint8_t i=0; i<8; ++i)
120 raw[i] = bytebits_to_byte(bits + 11 + i * 9, 8);
121
122 uint16_t crc = crc16_ccitt_kermit(raw, 8);
123 num_to_bytebitsLSBF(crc >> 0, 8, bits+83);
124 num_to_bytebitsLSBF(crc >> 8, 8, bits+92);
125
126 // extended data - OK
127 num_to_bytebitsLSBF( extended >> 0 , 8, bits+101);
128 num_to_bytebitsLSBF( extended >> 8 , 8, bits+110);
129 num_to_bytebitsLSBF( extended >> 16, 8, bits+119);
130 return 1;
131}
132
133int CmdFdxDemod(const char *Cmd){
134
135 //Differential Biphase / di-phase (inverted biphase)
136 //get binary from ask wave
137 if (!ASKbiphaseDemod("0 32 1 0", FALSE)) {
138 if (g_debugMode) PrintAndLog("DEBUG: Error - FDX-B ASKbiphaseDemod failed");
139 return 0;
140 }
141 size_t size = DemodBufferLen;
142 int preambleIndex = FDXBdemodBI(DemodBuffer, &size);
143 if (preambleIndex < 0){
144 if (g_debugMode){
145 if (preambleIndex == -1)
146 PrintAndLog("DEBUG: Error - FDX-B too few bits found");
147 else if (preambleIndex == -2)
148 PrintAndLog("DEBUG: Error - FDX-B preamble not found");
149 else if (preambleIndex == -3)
150 PrintAndLog("DEBUG: Error - FDX-B Size not correct: %d", size);
151 else
152 PrintAndLog("DEBUG: Error - FDX-B ans: %d", preambleIndex);
153 }
154 return 0;
155 }
156
157 // set and leave DemodBuffer intact
158 setDemodBuf(DemodBuffer, 128, preambleIndex);
159 uint8_t bits_no_spacer[117];
160 memcpy(bits_no_spacer, DemodBuffer + 11, 117);
161
162 // remove marker bits (1's every 9th digit after preamble) (pType = 2)
163 size = removeParity(bits_no_spacer, 0, 9, 2, 117);
164 if ( size != 104 ) {
165 if (g_debugMode) PrintAndLog("DEBUG: Error removeParity:: %d", size);
166 return 0;
167 }
168
169 //got a good demod
170 uint64_t NationalCode = ((uint64_t)(bytebits_to_byteLSBF(bits_no_spacer+32,6)) << 32) | bytebits_to_byteLSBF(bits_no_spacer,32);
171 uint32_t countryCode = bytebits_to_byteLSBF(bits_no_spacer+38,10);
172 uint8_t dataBlockBit = bits_no_spacer[48];
173 uint32_t reservedCode = bytebits_to_byteLSBF(bits_no_spacer+49,14);
174 uint8_t animalBit = bits_no_spacer[63];
175 uint32_t crc16 = bytebits_to_byteLSBF(bits_no_spacer+64,16);
176 uint32_t extended = bytebits_to_byteLSBF(bits_no_spacer+80,24);
177
178 uint64_t rawid = ((uint64_t)bytebits_to_byte(bits_no_spacer,32)<<32) | bytebits_to_byte(bits_no_spacer+32,32);
179 uint8_t raw[8];
180 num_to_bytes(rawid, 8, raw);
181
182 if (g_debugMode) {
183 PrintAndLog("DEBUG: bits_no_spacer:\n%s",sprint_bin_break(bits_no_spacer,size,16));
184 PrintAndLog("DEBUG: Start marker %d; Size %d", preambleIndex, size);
185 PrintAndLog("DEBUG: Raw ID Hex: %s", sprint_hex(raw,8));
186 }
187
188 uint16_t calcCrc = crc16_ccitt_kermit(raw, 8);
189 PrintAndLog("\nFDX-B / ISO 11784/5 Animal Tag ID Found:");
190 PrintAndLog("Animal ID: %04u-%012" PRIu64, countryCode, NationalCode);
191 PrintAndLog("National Code: %012" PRIu64, NationalCode);
192 PrintAndLog("CountryCode: %04u", countryCode);
193 PrintAndLog("Reserved Code: %u", reservedCode);
194 PrintAndLog("Animal Tag: %s", animalBit ? "True" : "False");
195 PrintAndLog("Has Extended: %s [0x%X]", dataBlockBit ? "True" : "False", extended);
196 PrintAndLog("CRC: 0x%04X - 0x%04X - [%s]\n", crc16, calcCrc, (calcCrc == crc16) ? "Passed" : "Failed");
197
198 // set block 0 for later
199 //g_DemodConfig = T55x7_MODULATION_DIPHASE | T55x7_BITRATE_RF_32 | 4 << T55x7_MAXBLOCK_SHIFT;
200 return 1;
201}
202
203int CmdFdxRead(const char *Cmd) {
204 CmdLFRead("s");
205 getSamples("10000", TRUE);
206 return CmdFdxDemod(Cmd);
207}
208
209int CmdFdxClone(const char *Cmd) {
210
211 uint32_t countryid = 0;
212 uint64_t animalid = 0;
213 uint32_t blocks[5] = {T55x7_MODULATION_DIPHASE | T55x7_BITRATE_RF_32 | 4 << T55x7_MAXBLOCK_SHIFT, 0, 0, 0, 0};
214 uint8_t bits[128];
215 uint8_t *bs = bits;
216 memset(bs, 0, sizeof(bits));
217
218 char cmdp = param_getchar(Cmd, 0);
219 if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_fdx_clone();
220
221 countryid = param_get32ex(Cmd, 0, 0, 10);
222 animalid = param_get64ex(Cmd, 1, 0, 10);
223
224 //Q5
225 if (param_getchar(Cmd, 2) == 'Q' || param_getchar(Cmd, 2) == 'q') {
226 //t5555 (Q5) BITRATE = (RF-2)/2 (iceman)
227 blocks[0] = T5555_MODULATION_BIPHASE | T5555_INVERT_OUTPUT | ((32-2)>>1) << T5555_BITRATE_SHIFT | 4 << T5555_MAXBLOCK_SHIFT;
228 }
229
230 verify_values(countryid, animalid);
231
232 // getFDXBits(uint64_t national_id, uint16_t country, uint8_t isanimal, uint8_t isextended, uint32_t extended, uint8_t *bits)
233 if ( !getFDXBits(animalid, countryid, 1, 0, 0, bs)) {
234 PrintAndLog("Error with tag bitstream generation.");
235 return 1;
236 }
237
238 // convert from bit stream to block data
239 blocks[1] = bytebits_to_byte(bs,32);
240 blocks[2] = bytebits_to_byte(bs+32,32);
241 blocks[3] = bytebits_to_byte(bs+64,32);
242 blocks[4] = bytebits_to_byte(bs+96,32);
243
244 PrintAndLog("Preparing to clone FDX-B to T55x7 with animal ID: %04u-%"PRIu64, countryid, animalid);
245 PrintAndLog("Blk | Data ");
246 PrintAndLog("----+------------");
247 PrintAndLog(" 00 | 0x%08x", blocks[0]);
248 PrintAndLog(" 01 | 0x%08x", blocks[1]);
249 PrintAndLog(" 02 | 0x%08x", blocks[2]);
250 PrintAndLog(" 03 | 0x%08x", blocks[3]);
251 PrintAndLog(" 04 | 0x%08x", blocks[4]);
252
253 UsbCommand resp;
254 UsbCommand c = {CMD_T55XX_WRITE_BLOCK, {0,0,0}};
255
256 for (int i = 4; i >= 0; --i) {
257 c.arg[0] = blocks[i];
258 c.arg[1] = i;
259 clearCommandBuffer();
260 SendCommand(&c);
261 if (!WaitForResponseTimeout(CMD_ACK, &resp, T55XX_WRITE_TIMEOUT)){
262 PrintAndLog("Error occurred, device did not respond during write operation.");
263 return -1;
264 }
265 }
266 return 0;
267}
268
269int CmdFdxSim(const char *Cmd) {
270 uint32_t countryid = 0;
271 uint64_t animalid = 0;
272
273 char cmdp = param_getchar(Cmd, 0);
274 if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_fdx_sim();
275
276 countryid = param_get32ex(Cmd, 0, 0, 10);
277 animalid = param_get64ex(Cmd, 1, 0, 10);
278
279 verify_values(countryid, animalid);
280
281 uint8_t clk = 32, encoding = 2, separator = 0, invert = 1;
282 uint16_t arg1, arg2;
283 size_t size = 128;
284 arg1 = clk << 8 | encoding;
285 arg2 = invert << 8 | separator;
286
287 PrintAndLog("Simulating FDX-B animal ID: %04u-%"PRIu64, countryid, animalid);
288
289 UsbCommand c = {CMD_ASK_SIM_TAG, {arg1, arg2, size}};
290
291 //getFDXBits(uint64_t national_id, uint16_t country, uint8_t isanimal, uint8_t isextended, uint32_t extended, uint8_t *bits)
292 getFDXBits(animalid, countryid, 1, 0, 0, c.d.asBytes);
293 clearCommandBuffer();
294 SendCommand(&c);
295 return 0;
296}
297
298static command_t CommandTable[] = {
299 {"help", CmdHelp, 1, "This help"},
300 {"demod", CmdFdxDemod,1, "Attempt to extract FDX-B ISO11784/85 data from the GraphBuffer"},
301 {"read", CmdFdxRead, 0, "Attempt to read and extract FDX-B ISO11784/85 data"},
302 {"clone", CmdFdxClone,0, "Clone animal ID tag to T55x7 (or to q5/T5555)"},
303 {"sim", CmdFdxSim, 0, "Animal ID tag simulator"},
304 {NULL, NULL, 0, NULL}
305};
306
307int CmdLFFdx(const char *Cmd) {
308 clearCommandBuffer();
309 CmdsParse(CommandTable, Cmd);
310 return 0;
311}
312
313int CmdHelp(const char *Cmd) {
314 CmdsHelp(CommandTable);
315 return 0;
316}
Impressum, Datenschutz