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