]> git.zerfleddert.de Git - proxmark3-svn/blame - client/cmdlfjablotron.c
CHG: `lf em` - some minor refactoring in 4x50 commands
[proxmark3-svn] / client / cmdlfjablotron.c
CommitLineData
6c283951 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//-----------------------------------------------------------------------------
00abc7c1 9
6c283951 10#include "cmdlfjablotron.h"
11
12static int CmdHelp(const char *Cmd);
13
14int usage_lf_jablotron_clone(void){
15 PrintAndLog("clone a Jablotron tag to a T55x7 tag.");
8ae9b358 16 PrintAndLog("Usage: lf jablotron clone [h] <card ID> <Q5>");
17 PrintAndLog("Options:");
18 PrintAndLog(" h : This help");
19 PrintAndLog(" <card ID> : jablotron card ID");
20 PrintAndLog(" <Q5> : specify write to Q5 (t5555 instead of t55x7)");
6c283951 21 PrintAndLog("");
514ddaa2 22 PrintAndLog("Sample: lf jablotron clone 112233");
6c283951 23 return 0;
24}
25
26int usage_lf_jablotron_sim(void) {
27 PrintAndLog("Enables simulation of jablotron card with specified card number.");
28 PrintAndLog("Simulation runs until the button is pressed or another USB command is issued.");
6c283951 29 PrintAndLog("");
8ae9b358 30 PrintAndLog("Usage: lf jablotron sim [h] <card ID>");
31 PrintAndLog("Options:");
32 PrintAndLog(" h : This help");
33 PrintAndLog(" <card ID> : jablotron card ID");
6c283951 34 PrintAndLog("");
514ddaa2 35 PrintAndLog("Sample: lf jablotron sim 112233");
6c283951 36 return 0;
37}
38
29ce214c 39static uint8_t jablontron_chksum(uint8_t *bits){
40 uint8_t chksum = 0;
41 for (int i=16; i < 56; i += 8) {
42 chksum += bytebits_to_byte(bits+i,8);
43 }
44 chksum ^= 0x3A;
45 return chksum;
46}
47
6c283951 48int getJablotronBits(uint64_t fullcode, uint8_t *bits) {
49 //preamp
50 num_to_bytebits(0xFFFF, 16, bits);
51
52 //fullcode
53 num_to_bytebits(fullcode, 40, bits+16);
54
55 //chksum byte
29ce214c 56 uint8_t chksum = jablontron_chksum(bits);
57 num_to_bytebits(chksum, 8, bits+56);
6c283951 58 return 1;
59}
60
dc6c90c3 61static uint64_t getJablontronCardId( uint64_t rawcode ){
62 uint64_t id = 0;
63 uint8_t bytes[] = {0,0,0,0,0};
64 num_to_bytes(rawcode, 5, bytes);
65 for ( int i = 4, j = 0; i > -1; --i, j += 2 ) {
66 id += NIBBLE_LOW( bytes[i] ) * (int)pow(10,j);
67 id += NIBBLE_HIGH( bytes[i] ) * (int)pow(10,j+1);
68 }
69 return id;
70}
71
6c283951 72//see ASKDemod for what args are accepted
73int CmdJablotronDemod(const char *Cmd) {
74
75 //Differential Biphase / di-phase (inverted biphase)
76 //get binary from ask wave
77 if (!ASKbiphaseDemod("0 64 1 0", FALSE)) {
bb73c58d 78 if (g_debugMode) PrintAndLog("DEBUG: Error - Jablotron ASKbiphaseDemod failed");
6c283951 79 return 0;
80 }
81 size_t size = DemodBufferLen;
82 int ans = JablotronDemod(DemodBuffer, &size);
83 if (ans < 0){
84 if (g_debugMode){
29ce214c 85 if (ans == -1)
86 PrintAndLog("DEBUG: Error - Jablotron too few bits found");
deba67ab 87 else if (ans == -2)
6c283951 88 PrintAndLog("DEBUG: Error - Jablotron preamble not found");
deba67ab 89 else if (ans == -3)
90 PrintAndLog("DEBUG: Error - Jablotron size not correct: %d", size);
29ce214c 91 else if (ans == -5)
92 PrintAndLog("DEBUG: Error - Jablotron checksum failed");
6c283951 93 else
bb73c58d 94 PrintAndLog("DEBUG: Error - Jablotron ans: %d", ans);
6c283951 95 }
96 return 0;
97 }
29ce214c 98
99 setDemodBuf(DemodBuffer+ans, 64, 0);
100
6c283951 101 //got a good demod
29ce214c 102 uint32_t raw1 = bytebits_to_byte(DemodBuffer, 32);
103 uint32_t raw2 = bytebits_to_byte(DemodBuffer+32, 32);
dc6c90c3 104
105 uint64_t rawid = bytebits_to_byte(DemodBuffer+16, 40);
106 uint64_t id = getJablontronCardId(rawid);
107
a330987d 108 PrintAndLog("Jablotron Tag Found: Card ID %u :: Raw: %08X%08X", id, raw1 ,raw2);
6c283951 109
29ce214c 110 uint8_t chksum = raw2 & 0xFF;
111 PrintAndLog("Checksum: %02X [%s]",
112 chksum,
113 (chksum == jablontron_chksum(DemodBuffer)) ? "OK":"FAIL"
114 );
dc6c90c3 115
116 id = DEC2BCD(id);
29ce214c 117 // Printed format: 1410-nn-nnnn-nnnn
dc6c90c3 118 PrintAndLog("Printed: 1410-%02X-%04X-%04X",
119 (uint8_t)(id >> 32) & 0xFF,
120 (uint16_t)(id >> 16) & 0xFFFF,
121 (uint16_t)id & 0xFFFF
122 );
6c283951 123 return 1;
124}
125
126int CmdJablotronRead(const char *Cmd) {
6c283951 127 CmdLFRead("s");
53484563 128 getSamples("12000", TRUE);
6c283951 129 return CmdJablotronDemod(Cmd);
130}
131
132int CmdJablotronClone(const char *Cmd) {
133
134 uint64_t fullcode = 0;
ff9c043d 135 uint32_t blocks[3] = {T55x7_MODULATION_DIPHASE | T55x7_BITRATE_RF_64 | 2 << T55x7_MAXBLOCK_SHIFT, 0, 0};
6c283951 136
137 uint8_t bits[64];
138 uint8_t *bs = bits;
139 memset(bs, 0, sizeof(bits));
140
141 char cmdp = param_getchar(Cmd, 0);
142 if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_jablotron_clone();
143
8ae9b358 144 fullcode = param_get64ex(Cmd, 0, 0, 16);
6c283951 145
146 //Q5
8ae9b358 147 if (param_getchar(Cmd, 1) == 'Q' || param_getchar(Cmd, 1) == 'q') {
6c283951 148 //t5555 (Q5) BITRATE = (RF-2)/2 (iceman)
ff9c043d 149 blocks[0] = T5555_MODULATION_BIPHASE | T5555_INVERT_OUTPUT | ((64-2)>>1) << T5555_BITRATE_SHIFT | 2 << T5555_MAXBLOCK_SHIFT;
6c283951 150 }
151
29ce214c 152 // clearing the topbit needed for the preambl detection.
153 if ((fullcode & 0x7FFFFFFFFF) != fullcode) {
154 fullcode &= 0x7FFFFFFFFF;
f5538c1c 155 PrintAndLog("Card Number Truncated to 39bits: %"PRIx64, fullcode);
6c283951 156 }
29ce214c 157
6c283951 158 if ( !getJablotronBits(fullcode, bs)) {
159 PrintAndLog("Error with tag bitstream generation.");
160 return 1;
161 }
162
163 //
164 blocks[1] = bytebits_to_byte(bs,32);
165 blocks[2] = bytebits_to_byte(bs+32,32);
166
29ce214c 167 PrintAndLog("Preparing to clone Jablotron to T55x7 with FullCode: %"PRIx64, fullcode);
6c283951 168 PrintAndLog("Blk | Data ");
169 PrintAndLog("----+------------");
170 PrintAndLog(" 00 | 0x%08x", blocks[0]);
171 PrintAndLog(" 01 | 0x%08x", blocks[1]);
172 PrintAndLog(" 02 | 0x%08x", blocks[2]);
173
174 UsbCommand resp;
175 UsbCommand c = {CMD_T55XX_WRITE_BLOCK, {0,0,0}};
176
c5d88617 177 for (int i = 2; i >= 0; --i) {
6c283951 178 c.arg[0] = blocks[i];
179 c.arg[1] = i;
180 clearCommandBuffer();
181 SendCommand(&c);
42c235e7 182 if (!WaitForResponseTimeout(CMD_ACK, &resp, T55XX_WRITE_TIMEOUT)){
6c283951 183 PrintAndLog("Error occurred, device did not respond during write operation.");
184 return -1;
185 }
186 }
187 return 0;
188}
189
190int CmdJablotronSim(const char *Cmd) {
191 uint64_t fullcode = 0;
192
193 char cmdp = param_getchar(Cmd, 0);
194 if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_jablotron_sim();
195
8ae9b358 196 fullcode = param_get64ex(Cmd, 0, 0, 16);
29ce214c 197
198 // clearing the topbit needed for the preambl detection.
199 if ((fullcode & 0x7FFFFFFFFF) != fullcode) {
200 fullcode &= 0x7FFFFFFFFF;
f5538c1c 201 PrintAndLog("Card Number Truncated to 39bits: %"PRIx64, fullcode);
29ce214c 202 }
6c283951 203
204 uint8_t clk = 64, encoding = 2, separator = 0, invert = 1;
205 uint16_t arg1, arg2;
206 size_t size = 64;
207 arg1 = clk << 8 | encoding;
208 arg2 = invert << 8 | separator;
209
29ce214c 210 PrintAndLog("Simulating Jablotron - FullCode: %"PRIx64, fullcode);
6c283951 211
212 UsbCommand c = {CMD_ASK_SIM_TAG, {arg1, arg2, size}};
213 getJablotronBits(fullcode, c.d.asBytes);
214 clearCommandBuffer();
215 SendCommand(&c);
216 return 0;
217}
218
219static command_t CommandTable[] = {
220 {"help", CmdHelp, 1, "This help"},
8ae9b358 221 {"read", CmdJablotronRead, 0, "Attempt to read and extract tag data"},
222 {"clone", CmdJablotronClone, 0, "clone jablotron tag"},
223 {"sim", CmdJablotronSim, 0, "simulate jablotron tag"},
6c283951 224 {NULL, NULL, 0, NULL}
225};
226
227int CmdLFJablotron(const char *Cmd) {
228 clearCommandBuffer();
229 CmdsParse(CommandTable, Cmd);
230 return 0;
231}
232
233int CmdHelp(const char *Cmd) {
234 CmdsHelp(CommandTable);
235 return 0;
236}
Impressum, Datenschutz