]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdlfindala.c
1 //-----------------------------------------------------------------------------
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
6 //-----------------------------------------------------------------------------
7 // Low frequency Indala commands
8 // PSK1, rf/32, 64 or 224 bits (known)
9 //-----------------------------------------------------------------------------
13 #include "cmdlfindala.h"
14 #include "proxmark3.h"
17 #include "cmdparser.h"
18 #include "cmddata.h" //for g_debugMode, demodbuff cmds
19 #include "lfdemod.h" //for indala26decode
20 #include "util.h" //for sprint_bin_break
21 #include "cmdlf.h" //for CmdLFRead
22 #include "cmdmain.h" //for clearCommandBuffer
24 static int CmdHelp(const char *Cmd
);
26 // Indala 26 bit decode
28 // optional arguments - same as PSKDemod (clock & invert & maxerr)
29 int CmdIndalaDecode(const char *Cmd
) {
32 ans
= PSKDemod(Cmd
, 0);
33 } else { //default to RF/32
34 ans
= PSKDemod("32", 0);
38 if (g_debugMode
) PrintAndLog("Error1: %i",ans
);
42 size_t size
= DemodBufferLen
;
43 int startIdx
= indala64decode(DemodBuffer
, &size
, &invert
);
44 if (startIdx
< 0 || size
!= 64) {
47 size
= DemodBufferLen
;
48 startIdx
= indala224decode(DemodBuffer
, &size
, &invert
);
49 if (startIdx
< 0 || size
!= 224) {
50 if (g_debugMode
) PrintAndLog("Error2: %i",startIdx
);
54 setDemodBuf(DemodBuffer
, size
, (size_t)startIdx
);
55 setClockGrid(g_DemodClock
, g_DemodStartIdx
+ (startIdx
*g_DemodClock
));
58 PrintAndLog("Had to invert bits");
60 PrintAndLog("BitLen: %d",DemodBufferLen
);
62 uint32_t uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
;
63 uid1
=bytebits_to_byte(DemodBuffer
,32);
64 uid2
=bytebits_to_byte(DemodBuffer
+32,32);
65 if (DemodBufferLen
==64) {
66 PrintAndLog("Indala UID=%s (%x%08x)", sprint_bin_break(DemodBuffer
,DemodBufferLen
,16), uid1
, uid2
);
67 } else if (DemodBufferLen
==224) {
68 uid3
=bytebits_to_byte(DemodBuffer
+64,32);
69 uid4
=bytebits_to_byte(DemodBuffer
+96,32);
70 uid5
=bytebits_to_byte(DemodBuffer
+128,32);
71 uid6
=bytebits_to_byte(DemodBuffer
+160,32);
72 uid7
=bytebits_to_byte(DemodBuffer
+192,32);
73 PrintAndLog("Indala UID=%s (%x%08x%08x%08x%08x%08x%08x)",
74 sprint_bin_break(DemodBuffer
,DemodBufferLen
,16), uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
);
77 PrintAndLog("DEBUG: printing demodbuffer:");
83 int CmdIndalaRead(const char *Cmd
) {
85 return CmdIndalaDecode("");
88 // older alternative indala demodulate (has some positives and negatives)
89 // returns false positives more often - but runs against more sets of samples
90 // poor psk signal can be difficult to demod this approach might succeed when the other fails
91 // but the other appears to currently be more accurate than this approach most of the time.
92 int CmdIndalaDemod(const char *Cmd
) {
93 // Usage: recover 64bit UID by default, specify "224" as arg to recover a 224bit UID
99 // worst case with GraphTraceLen=64000 is < 4096
100 // under normal conditions it's < 2048
102 uint8_t rawbits
[4096];
104 int worst
= 0, worstPos
= 0;
106 //clear clock grid and demod plot
110 // PrintAndLog("Expecting a bit less than %d raw bits", GraphTraceLen / 32);
111 // loop through raw signal - since we know it is psk1 rf/32 fc/2 skip every other value (+=2)
112 for (i
= 0; i
< GraphTraceLen
-1; i
+= 2) {
114 if ((GraphBuffer
[i
] > GraphBuffer
[i
+ 1]) && (state
!= 1)) {
115 // appears redundant - marshmellow
117 for (j
= 0; j
< count
- 8; j
+= 16) {
118 rawbits
[rawbit
++] = 0;
120 if ((abs(count
- j
)) > worst
) {
121 worst
= abs(count
- j
);
127 } else if ((GraphBuffer
[i
] < GraphBuffer
[i
+ 1]) && (state
!= 0)) {
130 for (j
= 0; j
< count
- 8; j
+= 16) {
131 rawbits
[rawbit
++] = 1;
133 if ((abs(count
- j
)) > worst
) {
134 worst
= abs(count
- j
);
144 PrintAndLog("Recovered %d raw bits, expected: %d", rawbit
, GraphTraceLen
/32);
145 PrintAndLog("worst metric (0=best..7=worst): %d at pos %d", worst
, worstPos
);
150 // Finding the start of a UID
151 int uidlen
, long_wait
;
152 if (strcmp(Cmd
, "224") == 0) {
162 for (start
= 0; start
<= rawbit
- uidlen
; start
++) {
163 first
= rawbits
[start
];
164 for (i
= start
; i
< start
+ long_wait
; i
++) {
165 if (rawbits
[i
] != first
) {
169 if (i
== (start
+ long_wait
)) {
174 if (start
== rawbit
- uidlen
+ 1) {
175 PrintAndLog("nothing to wait for");
179 // Inverting signal if needed
181 for (i
= start
; i
< rawbit
; i
++) {
182 rawbits
[i
] = !rawbits
[i
];
187 uint8_t bits
[224] = {0x00};
188 char showbits
[225] = {0x00};
193 if (uidlen
> rawbit
) {
194 PrintAndLog("Warning: not enough raw bits to get a full UID");
195 for (bit
= 0; bit
< rawbit
; bit
++) {
196 bits
[bit
] = rawbits
[i
++];
197 // As we cannot know the parity, let's use "." and "/"
198 showbits
[bit
] = '.' + bits
[bit
];
200 showbits
[bit
+1]='\0';
201 PrintAndLog("Partial UID=%s", showbits
);
204 for (bit
= 0; bit
< uidlen
; bit
++) {
205 bits
[bit
] = rawbits
[i
++];
206 showbits
[bit
] = '0' + bits
[bit
];
212 uint32_t uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
;
217 for( idx
=0; idx
<64; idx
++) {
218 if (showbits
[idx
] == '0') {
219 uid1
=(uid1
<<1)|(uid2
>>31);
222 uid1
=(uid1
<<1)|(uid2
>>31);
226 PrintAndLog("UID=%s (%x%08x)", showbits
, uid1
, uid2
);
229 uid3
= uid4
= uid5
= uid6
= uid7
= 0;
231 for( idx
=0; idx
<224; idx
++) {
232 uid1
=(uid1
<<1)|(uid2
>>31);
233 uid2
=(uid2
<<1)|(uid3
>>31);
234 uid3
=(uid3
<<1)|(uid4
>>31);
235 uid4
=(uid4
<<1)|(uid5
>>31);
236 uid5
=(uid5
<<1)|(uid6
>>31);
237 uid6
=(uid6
<<1)|(uid7
>>31);
239 if (showbits
[idx
] == '0')
240 uid7
= (uid7
<<1) | 0;
242 uid7
= (uid7
<<1) | 1;
244 PrintAndLog("UID=%s (%x%08x%08x%08x%08x%08x%08x)", showbits
, uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
);
247 // Checking UID against next occurrences
249 for (; i
+ uidlen
<= rawbit
;) {
251 for (bit
= 0; bit
< uidlen
; bit
++) {
252 if (bits
[bit
] != rawbits
[i
++]) {
263 PrintAndLog("Occurrences: %d (expected %d)", times
, (rawbit
- start
) / uidlen
);
265 // Remodulating for tag cloning
266 // HACK: 2015-01-04 this will have an impact on our new way of seening lf commands (demod)
267 // since this changes graphbuffer data.
268 GraphTraceLen
= 32*uidlen
;
271 for (bit
= 0; bit
< uidlen
; bit
++) {
272 if (bits
[bit
] == 0) {
278 for (j
= 0; j
< 32; j
++) {
279 GraphBuffer
[i
++] = phase
;
284 RepaintGraphWindow();
288 int CmdIndalaClone(const char *Cmd
) {
290 unsigned int uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
;
292 uid1
= uid2
= uid3
= uid4
= uid5
= uid6
= uid7
= 0;
295 if (strchr(Cmd
,'l') != 0) {
296 while (sscanf(&Cmd
[i
++], "%1x", &n
) == 1) {
297 uid1
= (uid1
<< 4) | (uid2
>> 28);
298 uid2
= (uid2
<< 4) | (uid3
>> 28);
299 uid3
= (uid3
<< 4) | (uid4
>> 28);
300 uid4
= (uid4
<< 4) | (uid5
>> 28);
301 uid5
= (uid5
<< 4) | (uid6
>> 28);
302 uid6
= (uid6
<< 4) | (uid7
>> 28);
303 uid7
= (uid7
<< 4) | (n
& 0xf);
305 PrintAndLog("Cloning 224bit tag with UID %x%08x%08x%08x%08x%08x%08x", uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
);
306 c
.cmd
= CMD_INDALA_CLONE_TAG_L
;
307 c
.d
.asDwords
[0] = uid1
;
308 c
.d
.asDwords
[1] = uid2
;
309 c
.d
.asDwords
[2] = uid3
;
310 c
.d
.asDwords
[3] = uid4
;
311 c
.d
.asDwords
[4] = uid5
;
312 c
.d
.asDwords
[5] = uid6
;
313 c
.d
.asDwords
[6] = uid7
;
315 while (sscanf(&Cmd
[i
++], "%1x", &n
) == 1) {
316 uid1
= (uid1
<< 4) | (uid2
>> 28);
317 uid2
= (uid2
<< 4) | (n
& 0xf);
319 PrintAndLog("Cloning 64bit tag with UID %x%08x", uid1
, uid2
);
320 c
.cmd
= CMD_INDALA_CLONE_TAG
;
325 clearCommandBuffer();
330 static command_t CommandTable
[] = {
331 {"help", CmdHelp
, 1, "This help"},
332 {"demod", CmdIndalaDecode
, 1, "[clock] [invert<0|1>] -- Demodulate an indala tag (PSK1) from GraphBuffer (args optional)"},
333 {"read", CmdIndalaRead
, 0, "Read an Indala Prox tag from the antenna"},
334 {"clone", CmdIndalaClone
, 0, "<UID> ['l']-- Clone Indala to T55x7 (tag must be on antenna)(UID in HEX)(option 'l' for 224 UID"},
335 {"altdemod", CmdIndalaDemod
, 1, "['224'] -- Alternative method to Demodulate samples for Indala 64 bit UID (option '224' for 224 bit)"},
336 //{"sim", CmdIndalaSim, 0, "<ID> -- indala tag simulator"},
337 {NULL
, NULL
, 0, NULL
}
340 int CmdLFINDALA(const char *Cmd
) {
341 CmdsParse(CommandTable
, Cmd
);
345 int CmdHelp(const char *Cmd
) {
346 CmdsHelp(CommandTable
);