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