]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/cmdlfem4x.c
Support for cloning to T55x7 at different clock rates & 134KHz
[proxmark3-svn] / client / cmdlfem4x.c
CommitLineData
a553f267 1//-----------------------------------------------------------------------------
2// Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
3//
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
6// the license.
7//-----------------------------------------------------------------------------
8// Low frequency EM4x commands
9//-----------------------------------------------------------------------------
10
7fe9b0b7 11#include <stdio.h>
9e13f875 12#include <string.h>
ec564290 13#include <inttypes.h>
28fdb04f 14//#include "proxusb.h"
902cb3c0 15#include "proxmark3.h"
7fe9b0b7 16#include "ui.h"
17#include "graph.h"
18#include "cmdparser.h"
19#include "cmddata.h"
20#include "cmdlf.h"
21#include "cmdlfem4x.h"
22
23static int CmdHelp(const char *Cmd);
24
25/* Read the ID of an EM410x tag.
26 * Format:
27 * 1111 1111 1 <-- standard non-repeatable header
28 * XXXX [row parity bit] <-- 10 rows of 5 bits for our 40 bit tag ID
29 * ....
30 * CCCC <-- each bit here is parity for the 10 bits above in corresponding column
31 * 0 <-- stop bit, end of tag
32 */
33int CmdEM410xRead(const char *Cmd)
34{
35 int i, j, clock, header, rows, bit, hithigh, hitlow, first, bit2idx, high, low;
36 int parity[4];
37 char id[11];
38 int retested = 0;
15cdabd4 39 uint8_t BitStream[MAX_GRAPH_TRACE_LEN];
7fe9b0b7 40 high = low = 0;
41
42 /* Detect high and lows and clock */
43 for (i = 0; i < GraphTraceLen; i++)
44 {
45 if (GraphBuffer[i] > high)
46 high = GraphBuffer[i];
47 else if (GraphBuffer[i] < low)
48 low = GraphBuffer[i];
49 }
50
51 /* get clock */
52 clock = GetClock(Cmd, high, 0);
53
54 /* parity for our 4 columns */
55 parity[0] = parity[1] = parity[2] = parity[3] = 0;
56 header = rows = 0;
57
58 /* manchester demodulate */
59 bit = bit2idx = 0;
60 for (i = 0; i < (int)(GraphTraceLen / clock); i++)
61 {
62 hithigh = 0;
63 hitlow = 0;
64 first = 1;
65
66 /* Find out if we hit both high and low peaks */
67 for (j = 0; j < clock; j++)
68 {
69 if (GraphBuffer[(i * clock) + j] == high)
70 hithigh = 1;
71 else if (GraphBuffer[(i * clock) + j] == low)
72 hitlow = 1;
73
74 /* it doesn't count if it's the first part of our read
75 because it's really just trailing from the last sequence */
76 if (first && (hithigh || hitlow))
77 hithigh = hitlow = 0;
78 else
79 first = 0;
80
81 if (hithigh && hitlow)
82 break;
83 }
84
85 /* If we didn't hit both high and low peaks, we had a bit transition */
86 if (!hithigh || !hitlow)
87 bit ^= 1;
88
89 BitStream[bit2idx++] = bit;
90 }
91
92retest:
93 /* We go till 5 before the graph ends because we'll get that far below */
94 for (i = 1; i < bit2idx - 5; i++)
95 {
96 /* Step 2: We have our header but need our tag ID */
97 if (header == 9 && rows < 10)
98 {
99 /* Confirm parity is correct */
100 if ((BitStream[i] ^ BitStream[i+1] ^ BitStream[i+2] ^ BitStream[i+3]) == BitStream[i+4])
101 {
102 /* Read another byte! */
103 sprintf(id+rows, "%x", (8 * BitStream[i]) + (4 * BitStream[i+1]) + (2 * BitStream[i+2]) + (1 * BitStream[i+3]));
104 rows++;
105
106 /* Keep parity info */
107 parity[0] ^= BitStream[i];
108 parity[1] ^= BitStream[i+1];
109 parity[2] ^= BitStream[i+2];
110 parity[3] ^= BitStream[i+3];
111
112 /* Move 4 bits ahead */
113 i += 4;
114 }
115
116 /* Damn, something wrong! reset */
117 else
118 {
119 PrintAndLog("Thought we had a valid tag but failed at word %d (i=%d)", rows + 1, i);
120
121 /* Start back rows * 5 + 9 header bits, -1 to not start at same place */
122 i -= 9 + (5 * rows) - 5;
123
124 rows = header = 0;
125 }
126 }
127
128 /* Step 3: Got our 40 bits! confirm column parity */
129 else if (rows == 10)
130 {
131 /* We need to make sure our 4 bits of parity are correct and we have a stop bit */
132 if (BitStream[i] == parity[0] && BitStream[i+1] == parity[1] &&
133 BitStream[i+2] == parity[2] && BitStream[i+3] == parity[3] &&
134 BitStream[i+4] == 0)
135 {
136 /* Sweet! */
137 PrintAndLog("EM410x Tag ID: %s", id);
138
139 /* Stop any loops */
140 return 1;
141 }
142
143 /* Crap! Incorrect parity or no stop bit, start all over */
144 else
145 {
146 rows = header = 0;
147
148 /* Go back 59 bits (9 header bits + 10 rows at 4+1 parity) */
149 i -= 59;
150 }
151 }
152
153 /* Step 1: get our header */
154 else if (header < 9)
155 {
156 /* Need 9 consecutive 1's */
157 if (BitStream[i] == 1)
158 header++;
159
160 /* We don't have a header, not enough consecutive 1 bits */
161 else
162 header = 0;
163 }
164 }
165
166 /* if we've already retested after flipping bits, return */
167 if (retested++)
168 return 0;
169
170 /* if this didn't work, try flipping bits */
171 for (i = 0; i < bit2idx; i++)
172 BitStream[i] ^= 1;
173
174 goto retest;
175}
176
177/* emulate an EM410X tag
178 * Format:
179 * 1111 1111 1 <-- standard non-repeatable header
180 * XXXX [row parity bit] <-- 10 rows of 5 bits for our 40 bit tag ID
181 * ....
182 * CCCC <-- each bit here is parity for the 10 bits above in corresponding column
183 * 0 <-- stop bit, end of tag
184 */
185int CmdEM410xSim(const char *Cmd)
186{
187 int i, n, j, h, binary[4], parity[4];
188
189 /* clock is 64 in EM410x tags */
190 int clock = 64;
191
192 /* clear our graph */
193 ClearGraph(0);
194
195 /* write it out a few times */
196 for (h = 0; h < 4; h++)
197 {
198 /* write 9 start bits */
199 for (i = 0; i < 9; i++)
200 AppendGraph(0, clock, 1);
201
202 /* for each hex char */
203 parity[0] = parity[1] = parity[2] = parity[3] = 0;
204 for (i = 0; i < 10; i++)
205 {
206 /* read each hex char */
207 sscanf(&Cmd[i], "%1x", &n);
208 for (j = 3; j >= 0; j--, n/= 2)
209 binary[j] = n % 2;
210
211 /* append each bit */
212 AppendGraph(0, clock, binary[0]);
213 AppendGraph(0, clock, binary[1]);
214 AppendGraph(0, clock, binary[2]);
215 AppendGraph(0, clock, binary[3]);
216
217 /* append parity bit */
218 AppendGraph(0, clock, binary[0] ^ binary[1] ^ binary[2] ^ binary[3]);
219
220 /* keep track of column parity */
221 parity[0] ^= binary[0];
222 parity[1] ^= binary[1];
223 parity[2] ^= binary[2];
224 parity[3] ^= binary[3];
225 }
226
227 /* parity columns */
228 AppendGraph(0, clock, parity[0]);
229 AppendGraph(0, clock, parity[1]);
230 AppendGraph(0, clock, parity[2]);
231 AppendGraph(0, clock, parity[3]);
232
233 /* stop bit */
234 AppendGraph(0, clock, 0);
235 }
236
237 /* modulate that biatch */
238 CmdManchesterMod("");
239
240 /* booyah! */
241 RepaintGraphWindow();
242
243 CmdLFSim("");
244 return 0;
245}
246
247/* Function is equivalent of loread + losamples + em410xread
248 * looped until an EM410x tag is detected */
249int CmdEM410xWatch(const char *Cmd)
250{
e67b06b7 251 int read_h = (*Cmd == 'h');
7fe9b0b7 252 do
253 {
e67b06b7 254 CmdLFRead(read_h ? "h" : "");
255 // 2000 samples is OK for clock=64, but not clock=32. Probably want
256 // 8000 for clock=16. Don't want to go too high since old HID driver
257 // is very slow
258 // TBD: Auto-grow sample size based on detected sample rate. IE: If the
259 // rate gets lower, then grow the number of samples
260 CmdSamples("4000");
ab2fd3d6 261 } while ( ! CmdEM410xRead(""));
7fe9b0b7 262 return 0;
263}
264
265/* Read the transmitted data of an EM4x50 tag
266 * Format:
267 *
268 * XXXXXXXX [row parity bit (even)] <- 8 bits plus parity
269 * XXXXXXXX [row parity bit (even)] <- 8 bits plus parity
270 * XXXXXXXX [row parity bit (even)] <- 8 bits plus parity
271 * XXXXXXXX [row parity bit (even)] <- 8 bits plus parity
272 * CCCCCCCC <- column parity bits
273 * 0 <- stop bit
274 * LW <- Listen Window
275 *
276 * This pattern repeats for every block of data being transmitted.
277 * Transmission starts with two Listen Windows (LW - a modulated
278 * pattern of 320 cycles each (32/32/128/64/64)).
279 *
280 * Note that this data may or may not be the UID. It is whatever data
281 * is stored in the blocks defined in the control word First and Last
282 * Word Read values. UID is stored in block 32.
283 */
284int CmdEM4x50Read(const char *Cmd)
285{
31b6e9af 286 int i, j, startblock, skip, block, start, end, low, high;
7fe9b0b7 287 bool complete= false;
288 int tmpbuff[MAX_GRAPH_TRACE_LEN / 64];
289 char tmp[6];
290
291 high= low= 0;
913d23c6 292 memset(tmpbuff, 0, MAX_GRAPH_TRACE_LEN / 64);
7fe9b0b7 293
294 /* first get high and low values */
295 for (i = 0; i < GraphTraceLen; i++)
296 {
297 if (GraphBuffer[i] > high)
298 high = GraphBuffer[i];
299 else if (GraphBuffer[i] < low)
300 low = GraphBuffer[i];
301 }
302
303 /* populate a buffer with pulse lengths */
304 i= 0;
305 j= 0;
306 while (i < GraphTraceLen)
307 {
308 // measure from low to low
309 while ((GraphBuffer[i] > low) && (i<GraphTraceLen))
310 ++i;
311 start= i;
312 while ((GraphBuffer[i] < high) && (i<GraphTraceLen))
313 ++i;
314 while ((GraphBuffer[i] > low) && (i<GraphTraceLen))
315 ++i;
316 if (j>(MAX_GRAPH_TRACE_LEN/64)) {
317 break;
318 }
319 tmpbuff[j++]= i - start;
320 }
321
322 /* look for data start - should be 2 pairs of LW (pulses of 192,128) */
323 start= -1;
324 skip= 0;
325 for (i= 0; i < j - 4 ; ++i)
326 {
327 skip += tmpbuff[i];
328 if (tmpbuff[i] >= 190 && tmpbuff[i] <= 194)
329 if (tmpbuff[i+1] >= 126 && tmpbuff[i+1] <= 130)
330 if (tmpbuff[i+2] >= 190 && tmpbuff[i+2] <= 194)
331 if (tmpbuff[i+3] >= 126 && tmpbuff[i+3] <= 130)
332 {
333 start= i + 3;
334 break;
335 }
336 }
337 startblock= i + 3;
338
339 /* skip over the remainder of the LW */
340 skip += tmpbuff[i+1]+tmpbuff[i+2];
341 while (skip < MAX_GRAPH_TRACE_LEN && GraphBuffer[skip] > low)
342 ++skip;
343 skip += 8;
344
345 /* now do it again to find the end */
346 end= start;
347 for (i += 3; i < j - 4 ; ++i)
348 {
349 end += tmpbuff[i];
350 if (tmpbuff[i] >= 190 && tmpbuff[i] <= 194)
351 if (tmpbuff[i+1] >= 126 && tmpbuff[i+1] <= 130)
352 if (tmpbuff[i+2] >= 190 && tmpbuff[i+2] <= 194)
353 if (tmpbuff[i+3] >= 126 && tmpbuff[i+3] <= 130)
354 {
355 complete= true;
356 break;
357 }
358 }
359
360 if (start >= 0)
361 PrintAndLog("Found data at sample: %i",skip);
362 else
363 {
364 PrintAndLog("No data found!");
365 PrintAndLog("Try again with more samples.");
366 return 0;
367 }
368
369 if (!complete)
370 {
371 PrintAndLog("*** Warning!");
372 PrintAndLog("Partial data - no end found!");
373 PrintAndLog("Try again with more samples.");
374 }
375
376 /* get rid of leading crap */
377 sprintf(tmp,"%i",skip);
378 CmdLtrim(tmp);
379
380 /* now work through remaining buffer printing out data blocks */
381 block= 0;
382 i= startblock;
383 while (block < 6)
384 {
385 PrintAndLog("Block %i:", block);
386 // mandemod routine needs to be split so we can call it for data
387 // just print for now for debugging
388 CmdManchesterDemod("i 64");
389 skip= 0;
390 /* look for LW before start of next block */
391 for ( ; i < j - 4 ; ++i)
392 {
393 skip += tmpbuff[i];
394 if (tmpbuff[i] >= 190 && tmpbuff[i] <= 194)
395 if (tmpbuff[i+1] >= 126 && tmpbuff[i+1] <= 130)
396 break;
397 }
398 while (GraphBuffer[skip] > low)
399 ++skip;
400 skip += 8;
401 sprintf(tmp,"%i",skip);
402 CmdLtrim(tmp);
403 start += skip;
404 block++;
405 }
406 return 0;
407}
408
2d4eae76 409int CmdEM410xWrite(const char *Cmd)
410{
e67b06b7 411 uint64_t id = 0xFFFFFFFFFFFFFFFF; // invalid id value
412 unsigned int card = 0xFF; // invalid card value
413 unsigned int clock = 0; // invalid clock value
414
415 sscanf(Cmd, "%" PRIx64 " %d %d", &id, &card, &clock);
416
417 // Check ID
418 if (id == 0xFFFFFFFFFFFFFFFF) {
419 PrintAndLog("Error! ID is required.\n");
420 return 0;
421 }
422 if (id >= 0x10000000000) {
423 PrintAndLog("Error! Given EM410x ID is longer than 40 bits.\n");
424 return 0;
425 }
426
427 // Check Card
428 if (card == 0xFF) {
429 PrintAndLog("Error! Card type required.\n");
430 return 0;
431 }
432 if (card < 0) {
433 PrintAndLog("Error! Bad card type selected.\n");
434 return 0;
435 }
436
437 // Check Clock
438 if (card == 1)
439 {
440 // Default: 64
441 if (clock == 0)
442 clock = 64;
443
444 // Allowed clock rates: 16, 32 and 64
445 if ((clock != 16) && (clock != 32) && (clock != 64)) {
446 PrintAndLog("Error! Clock rate %d not valid. Supported clock rates are 16, 32 and 64.\n", clock);
447 return 0;
448 }
449 }
450 else if (clock != 0)
451 {
452 PrintAndLog("Error! Clock rate is only supported on T55x7 tags.\n");
453 return 0;
454 }
455
456 if (card == 1) {
457 PrintAndLog("Writing %s tag with UID 0x%010" PRIx64 " (clock rate: %d)", "T55x7", id, clock);
458 // NOTE: We really should pass the clock in as a separate argument, but to
459 // provide for backwards-compatibility for older firmware, and to avoid
460 // having to add another argument to CMD_EM410X_WRITE_TAG, we just store
461 // the clock rate in bits 8-15 of the card value
462 card = (card & 0xFF) | (((uint64_t)clock << 8) & 0xFF00);
463 }
464 else if (card == 0)
465 PrintAndLog("Writing %s tag with UID 0x%010" PRIx64, "T5555", id, clock);
466 else {
467 PrintAndLog("Error! Bad card type selected.\n");
468 return 0;
469 }
2d4eae76 470
2d4eae76 471 UsbCommand c = {CMD_EM410X_WRITE_TAG, {card, (uint32_t)(id >> 32), (uint32_t)id}};
472 SendCommand(&c);
473
474 return 0;
475}
476
54a942b0 477int CmdReadWord(const char *Cmd)
478{
479 int Word = 16; //default to invalid word
480 UsbCommand c;
481
482 sscanf(Cmd, "%d", &Word);
483
484 if (Word > 15) {
485 PrintAndLog("Word must be between 0 and 15");
486 return 1;
487 }
488
489 PrintAndLog("Reading word %d", Word);
490
491 c.cmd = CMD_EM4X_READ_WORD;
492 c.d.asBytes[0] = 0x0; //Normal mode
493 c.arg[0] = 0;
494 c.arg[1] = Word;
495 c.arg[2] = 0;
496 SendCommand(&c);
497 return 0;
498}
499
500int CmdReadWordPWD(const char *Cmd)
501{
502 int Word = 16; //default to invalid word
503 int Password = 0xFFFFFFFF; //default to blank password
504 UsbCommand c;
505
506 sscanf(Cmd, "%d %x", &Word, &Password);
507
508 if (Word > 15) {
509 PrintAndLog("Word must be between 0 and 15");
510 return 1;
511 }
512
513 PrintAndLog("Reading word %d with password %08X", Word, Password);
514
515 c.cmd = CMD_EM4X_READ_WORD;
516 c.d.asBytes[0] = 0x1; //Password mode
517 c.arg[0] = 0;
518 c.arg[1] = Word;
519 c.arg[2] = Password;
520 SendCommand(&c);
521 return 0;
522}
523
524int CmdWriteWord(const char *Cmd)
525{
526 int Word = 16; //default to invalid block
527 int Data = 0xFFFFFFFF; //default to blank data
528 UsbCommand c;
529
530 sscanf(Cmd, "%x %d", &Data, &Word);
531
532 if (Word > 15) {
533 PrintAndLog("Word must be between 0 and 15");
534 return 1;
535 }
536
537 PrintAndLog("Writting word %d with data %08X", Word, Data);
538
539 c.cmd = CMD_EM4X_WRITE_WORD;
540 c.d.asBytes[0] = 0x0; //Normal mode
541 c.arg[0] = Data;
542 c.arg[1] = Word;
543 c.arg[2] = 0;
544 SendCommand(&c);
545 return 0;
546}
547
548int CmdWriteWordPWD(const char *Cmd)
549{
550 int Word = 8; //default to invalid word
551 int Data = 0xFFFFFFFF; //default to blank data
552 int Password = 0xFFFFFFFF; //default to blank password
553 UsbCommand c;
554
555 sscanf(Cmd, "%x %d %x", &Data, &Word, &Password);
556
557 if (Word > 15) {
558 PrintAndLog("Word must be between 0 and 15");
559 return 1;
560 }
561
562 PrintAndLog("Writting word %d with data %08X and password %08X", Word, Data, Password);
563
564 c.cmd = CMD_EM4X_WRITE_WORD;
565 c.d.asBytes[0] = 0x1; //Password mode
566 c.arg[0] = Data;
567 c.arg[1] = Word;
568 c.arg[2] = Password;
569 SendCommand(&c);
570 return 0;
571}
572
573
574
2d4eae76 575static command_t CommandTable[] =
7fe9b0b7 576{
54a942b0 577 {"help", CmdHelp, 1, "This help"},
578 {"em410xread", CmdEM410xRead, 1, "[clock rate] -- Extract ID from EM410x tag"},
579 {"em410xsim", CmdEM410xSim, 0, "<UID> -- Simulate EM410x tag"},
e67b06b7 580 {"em410xwatch", CmdEM410xWatch, 0, "['h'] -- Watches for EM410x 125/134 kHz tags (option 'h' for 134)"},
581 {"em410xwrite", CmdEM410xWrite, 1, "<UID> <'0' T5555> <'1' T55x7> [clock rate] -- Write EM410x UID to T5555(Q5) or T55x7 tag, optionally setting clock rate"},
54a942b0 582 {"em4x50read", CmdEM4x50Read, 1, "Extract data from EM4x50 tag"},
583 {"readword", CmdReadWord, 1, "<Word> -- Read EM4xxx word data"},
584 {"readwordPWD", CmdReadWordPWD, 1, "<Word> <Password> -- Read EM4xxx word data in password mode"},
585 {"writeword", CmdWriteWord, 1, "<Data> <Word> -- Write EM4xxx word data"},
586 {"writewordPWD", CmdWriteWordPWD, 1, "<Data> <Word> <Password> -- Write EM4xxx word data in password mode"},
7fe9b0b7 587 {NULL, NULL, 0, NULL}
588};
589
590int CmdLFEM4X(const char *Cmd)
591{
592 CmdsParse(CommandTable, Cmd);
593 return 0;
594}
595
596int CmdHelp(const char *Cmd)
597{
598 CmdsHelp(CommandTable);
599 return 0;
600}
Impressum, Datenschutz