]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdlfem4x.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
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
7 //-----------------------------------------------------------------------------
8 // Low frequency EM4x commands
9 //-----------------------------------------------------------------------------
14 //#include "proxusb.h"
15 #include "proxmark3.h"
18 #include "cmdparser.h"
21 #include "cmdlfem4x.h"
23 static int CmdHelp(const char *Cmd
);
25 /* Read the ID of an EM410x tag.
27 * 1111 1111 1 <-- standard non-repeatable header
28 * XXXX [row parity bit] <-- 10 rows of 5 bits for our 40 bit tag ID
30 * CCCC <-- each bit here is parity for the 10 bits above in corresponding column
31 * 0 <-- stop bit, end of tag
33 int CmdEM410xRead(const char *Cmd
)
35 int i
, j
, clock
, header
, rows
, bit
, hithigh
, hitlow
, first
, bit2idx
, high
, low
;
39 uint8_t BitStream
[MAX_GRAPH_TRACE_LEN
];
42 /* Detect high and lows and clock */
43 for (i
= 0; i
< GraphTraceLen
; i
++)
45 if (GraphBuffer
[i
] > high
)
46 high
= GraphBuffer
[i
];
47 else if (GraphBuffer
[i
] < low
)
52 clock
= GetClock(Cmd
, high
, 0);
54 /* parity for our 4 columns */
55 parity
[0] = parity
[1] = parity
[2] = parity
[3] = 0;
58 /* manchester demodulate */
60 for (i
= 0; i
< (int)(GraphTraceLen
/ clock
); i
++)
66 /* Find out if we hit both high and low peaks */
67 for (j
= 0; j
< clock
; j
++)
69 if (GraphBuffer
[(i
* clock
) + j
] == high
)
71 else if (GraphBuffer
[(i
* clock
) + j
] == low
)
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
))
81 if (hithigh
&& hitlow
)
85 /* If we didn't hit both high and low peaks, we had a bit transition */
86 if (!hithigh
|| !hitlow
)
89 BitStream
[bit2idx
++] = bit
;
93 /* We go till 5 before the graph ends because we'll get that far below */
94 for (i
= 1; i
< bit2idx
- 5; i
++)
96 /* Step 2: We have our header but need our tag ID */
97 if (header
== 9 && rows
< 10)
99 /* Confirm parity is correct */
100 if ((BitStream
[i
] ^ BitStream
[i
+1] ^ BitStream
[i
+2] ^ BitStream
[i
+3]) == BitStream
[i
+4])
102 /* Read another byte! */
103 sprintf(id
+rows
, "%x", (8 * BitStream
[i
]) + (4 * BitStream
[i
+1]) + (2 * BitStream
[i
+2]) + (1 * BitStream
[i
+3]));
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];
112 /* Move 4 bits ahead */
116 /* Damn, something wrong! reset */
119 PrintAndLog("Thought we had a valid tag but failed at word %d (i=%d)", rows
+ 1, i
);
121 /* Start back rows * 5 + 9 header bits, -1 to not start at same place */
122 i
-= 9 + (5 * rows
) - 5;
128 /* Step 3: Got our 40 bits! confirm column parity */
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] &&
137 PrintAndLog("EM410x Tag ID: %s", id
);
143 /* Crap! Incorrect parity or no stop bit, start all over */
148 /* Go back 59 bits (9 header bits + 10 rows at 4+1 parity) */
153 /* Step 1: get our header */
156 /* Need 9 consecutive 1's */
157 if (BitStream
[i
] == 1)
160 /* We don't have a header, not enough consecutive 1 bits */
166 /* if we've already retested after flipping bits, return */
170 /* if this didn't work, try flipping bits */
171 for (i
= 0; i
< bit2idx
; i
++)
177 /* emulate an EM410X tag
179 * 1111 1111 1 <-- standard non-repeatable header
180 * XXXX [row parity bit] <-- 10 rows of 5 bits for our 40 bit tag ID
182 * CCCC <-- each bit here is parity for the 10 bits above in corresponding column
183 * 0 <-- stop bit, end of tag
185 int CmdEM410xSim(const char *Cmd
)
187 int i
, n
, j
, h
, binary
[4], parity
[4];
189 /* clock is 64 in EM410x tags */
192 /* clear our graph */
195 /* write it out a few times */
196 for (h
= 0; h
< 4; h
++)
198 /* write 9 start bits */
199 for (i
= 0; i
< 9; i
++)
200 AppendGraph(0, clock
, 1);
202 /* for each hex char */
203 parity
[0] = parity
[1] = parity
[2] = parity
[3] = 0;
204 for (i
= 0; i
< 10; i
++)
206 /* read each hex char */
207 sscanf(&Cmd
[i
], "%1x", &n
);
208 for (j
= 3; j
>= 0; j
--, n
/= 2)
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]);
217 /* append parity bit */
218 AppendGraph(0, clock
, binary
[0] ^ binary
[1] ^ binary
[2] ^ binary
[3]);
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];
228 AppendGraph(0, clock
, parity
[0]);
229 AppendGraph(0, clock
, parity
[1]);
230 AppendGraph(0, clock
, parity
[2]);
231 AppendGraph(0, clock
, parity
[3]);
234 AppendGraph(0, clock
, 0);
237 /* modulate that biatch */
238 CmdManchesterMod("");
241 RepaintGraphWindow();
247 /* Function is equivalent of loread + losamples + em410xread
248 * looped until an EM410x tag is detected */
249 int CmdEM410xWatch(const char *Cmd
)
251 int read_h
= (*Cmd
== 'h');
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
258 // TBD: Auto-grow sample size based on detected sample rate. IE: If the
259 // rate gets lower, then grow the number of samples
261 // Changed by martin, 4000 x 4 = 16000,
262 // see http://www.proxmark.org/forum/viewtopic.php?pid=7235#p7235
264 } while ( ! CmdEM410xRead(""));
268 /* Read the transmitted data of an EM4x50 tag
271 * XXXXXXXX [row parity bit (even)] <- 8 bits plus parity
272 * XXXXXXXX [row parity bit (even)] <- 8 bits plus parity
273 * XXXXXXXX [row parity bit (even)] <- 8 bits plus parity
274 * XXXXXXXX [row parity bit (even)] <- 8 bits plus parity
275 * CCCCCCCC <- column parity bits
277 * LW <- Listen Window
279 * This pattern repeats for every block of data being transmitted.
280 * Transmission starts with two Listen Windows (LW - a modulated
281 * pattern of 320 cycles each (32/32/128/64/64)).
283 * Note that this data may or may not be the UID. It is whatever data
284 * is stored in the blocks defined in the control word First and Last
285 * Word Read values. UID is stored in block 32.
287 int CmdEM4x50Read(const char *Cmd
)
289 int i
, j
, startblock
, skip
, block
, start
, end
, low
, high
;
290 bool complete
= false;
291 int tmpbuff
[MAX_GRAPH_TRACE_LEN
/ 64];
295 memset(tmpbuff
, 0, MAX_GRAPH_TRACE_LEN
/ 64);
297 /* first get high and low values */
298 for (i
= 0; i
< GraphTraceLen
; i
++)
300 if (GraphBuffer
[i
] > high
)
301 high
= GraphBuffer
[i
];
302 else if (GraphBuffer
[i
] < low
)
303 low
= GraphBuffer
[i
];
306 /* populate a buffer with pulse lengths */
309 while (i
< GraphTraceLen
)
311 // measure from low to low
312 while ((GraphBuffer
[i
] > low
) && (i
<GraphTraceLen
))
315 while ((GraphBuffer
[i
] < high
) && (i
<GraphTraceLen
))
317 while ((GraphBuffer
[i
] > low
) && (i
<GraphTraceLen
))
319 if (j
>(MAX_GRAPH_TRACE_LEN
/64)) {
322 tmpbuff
[j
++]= i
- start
;
325 /* look for data start - should be 2 pairs of LW (pulses of 192,128) */
328 for (i
= 0; i
< j
- 4 ; ++i
)
331 if (tmpbuff
[i
] >= 190 && tmpbuff
[i
] <= 194)
332 if (tmpbuff
[i
+1] >= 126 && tmpbuff
[i
+1] <= 130)
333 if (tmpbuff
[i
+2] >= 190 && tmpbuff
[i
+2] <= 194)
334 if (tmpbuff
[i
+3] >= 126 && tmpbuff
[i
+3] <= 130)
342 /* skip over the remainder of the LW */
343 skip
+= tmpbuff
[i
+1]+tmpbuff
[i
+2];
344 while (skip
< MAX_GRAPH_TRACE_LEN
&& GraphBuffer
[skip
] > low
)
348 /* now do it again to find the end */
350 for (i
+= 3; i
< j
- 4 ; ++i
)
353 if (tmpbuff
[i
] >= 190 && tmpbuff
[i
] <= 194)
354 if (tmpbuff
[i
+1] >= 126 && tmpbuff
[i
+1] <= 130)
355 if (tmpbuff
[i
+2] >= 190 && tmpbuff
[i
+2] <= 194)
356 if (tmpbuff
[i
+3] >= 126 && tmpbuff
[i
+3] <= 130)
364 PrintAndLog("Found data at sample: %i",skip
);
367 PrintAndLog("No data found!");
368 PrintAndLog("Try again with more samples.");
374 PrintAndLog("*** Warning!");
375 PrintAndLog("Partial data - no end found!");
376 PrintAndLog("Try again with more samples.");
379 /* get rid of leading crap */
380 sprintf(tmp
,"%i",skip
);
383 /* now work through remaining buffer printing out data blocks */
388 PrintAndLog("Block %i:", block
);
389 // mandemod routine needs to be split so we can call it for data
390 // just print for now for debugging
391 CmdManchesterDemod("i 64");
393 /* look for LW before start of next block */
394 for ( ; i
< j
- 4 ; ++i
)
397 if (tmpbuff
[i
] >= 190 && tmpbuff
[i
] <= 194)
398 if (tmpbuff
[i
+1] >= 126 && tmpbuff
[i
+1] <= 130)
401 while (GraphBuffer
[skip
] > low
)
404 sprintf(tmp
,"%i",skip
);
412 int CmdEM410xWrite(const char *Cmd
)
414 uint64_t id
= 0xFFFFFFFFFFFFFFFF; // invalid id value
415 unsigned int card
= 0xFF; // invalid card value
416 unsigned int clock
= 0; // invalid clock value
418 sscanf(Cmd
, "%" PRIx64
" %d %d", &id
, &card
, &clock
);
421 if (id
== 0xFFFFFFFFFFFFFFFF) {
422 PrintAndLog("Error! ID is required.\n");
425 if (id
>= 0x10000000000) {
426 PrintAndLog("Error! Given EM410x ID is longer than 40 bits.\n");
432 PrintAndLog("Error! Card type required.\n");
436 PrintAndLog("Error! Bad card type selected.\n");
447 // Allowed clock rates: 16, 32 and 64
448 if ((clock
!= 16) && (clock
!= 32) && (clock
!= 64)) {
449 PrintAndLog("Error! Clock rate %d not valid. Supported clock rates are 16, 32 and 64.\n", clock
);
455 PrintAndLog("Error! Clock rate is only supported on T55x7 tags.\n");
460 PrintAndLog("Writing %s tag with UID 0x%010" PRIx64
" (clock rate: %d)", "T55x7", id
, clock
);
461 // NOTE: We really should pass the clock in as a separate argument, but to
462 // provide for backwards-compatibility for older firmware, and to avoid
463 // having to add another argument to CMD_EM410X_WRITE_TAG, we just store
464 // the clock rate in bits 8-15 of the card value
465 card
= (card
& 0xFF) | (((uint64_t)clock
<< 8) & 0xFF00);
468 PrintAndLog("Writing %s tag with UID 0x%010" PRIx64
, "T5555", id
, clock
);
470 PrintAndLog("Error! Bad card type selected.\n");
474 UsbCommand c
= {CMD_EM410X_WRITE_TAG
, {card
, (uint32_t)(id
>> 32), (uint32_t)id
}};
480 int CmdReadWord(const char *Cmd
)
482 int Word
= 16; //default to invalid word
485 sscanf(Cmd
, "%d", &Word
);
488 PrintAndLog("Word must be between 0 and 15");
492 PrintAndLog("Reading word %d", Word
);
494 c
.cmd
= CMD_EM4X_READ_WORD
;
495 c
.d
.asBytes
[0] = 0x0; //Normal mode
503 int CmdReadWordPWD(const char *Cmd
)
505 int Word
= 16; //default to invalid word
506 int Password
= 0xFFFFFFFF; //default to blank password
509 sscanf(Cmd
, "%d %x", &Word
, &Password
);
512 PrintAndLog("Word must be between 0 and 15");
516 PrintAndLog("Reading word %d with password %08X", Word
, Password
);
518 c
.cmd
= CMD_EM4X_READ_WORD
;
519 c
.d
.asBytes
[0] = 0x1; //Password mode
527 int CmdWriteWord(const char *Cmd
)
529 int Word
= 16; //default to invalid block
530 int Data
= 0xFFFFFFFF; //default to blank data
533 sscanf(Cmd
, "%x %d", &Data
, &Word
);
536 PrintAndLog("Word must be between 0 and 15");
540 PrintAndLog("Writting word %d with data %08X", Word
, Data
);
542 c
.cmd
= CMD_EM4X_WRITE_WORD
;
543 c
.d
.asBytes
[0] = 0x0; //Normal mode
551 int CmdWriteWordPWD(const char *Cmd
)
553 int Word
= 8; //default to invalid word
554 int Data
= 0xFFFFFFFF; //default to blank data
555 int Password
= 0xFFFFFFFF; //default to blank password
558 sscanf(Cmd
, "%x %d %x", &Data
, &Word
, &Password
);
561 PrintAndLog("Word must be between 0 and 15");
565 PrintAndLog("Writting word %d with data %08X and password %08X", Word
, Data
, Password
);
567 c
.cmd
= CMD_EM4X_WRITE_WORD
;
568 c
.d
.asBytes
[0] = 0x1; //Password mode
578 static command_t CommandTable
[] =
580 {"help", CmdHelp
, 1, "This help"},
581 {"em410xread", CmdEM410xRead
, 1, "[clock rate] -- Extract ID from EM410x tag"},
582 {"em410xsim", CmdEM410xSim
, 0, "<UID> -- Simulate EM410x tag"},
583 {"em410xwatch", CmdEM410xWatch
, 0, "['h'] -- Watches for EM410x 125/134 kHz tags (option 'h' for 134)"},
584 {"em410xwrite", CmdEM410xWrite
, 1, "<UID> <'0' T5555> <'1' T55x7> [clock rate] -- Write EM410x UID to T5555(Q5) or T55x7 tag, optionally setting clock rate"},
585 {"em4x50read", CmdEM4x50Read
, 1, "Extract data from EM4x50 tag"},
586 {"readword", CmdReadWord
, 1, "<Word> -- Read EM4xxx word data"},
587 {"readwordPWD", CmdReadWordPWD
, 1, "<Word> <Password> -- Read EM4xxx word data in password mode"},
588 {"writeword", CmdWriteWord
, 1, "<Data> <Word> -- Write EM4xxx word data"},
589 {"writewordPWD", CmdWriteWordPWD
, 1, "<Data> <Word> <Password> -- Write EM4xxx word data in password mode"},
590 {NULL
, NULL
, 0, NULL
}
593 int CmdLFEM4X(const char *Cmd
)
595 CmdsParse(CommandTable
, Cmd
);
599 int CmdHelp(const char *Cmd
)
601 CmdsHelp(CommandTable
);