1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2009 Michael Gernoth <michael at gernoth.net>
3 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
5 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
6 // at your option, any later version. See the LICENSE.txt file for the text of
8 //-----------------------------------------------------------------------------
10 //-----------------------------------------------------------------------------
17 #include <readline/readline.h>
19 #include "loclass/cipherutils.h"
23 //#include <liquid/liquid.h>
24 #define M_PI 3.14159265358979323846264338327
26 double CursorScaleFactor
;
27 int PlotGridX
, PlotGridY
, PlotGridXdefault
= 64, PlotGridYdefault
= 64;
29 int flushAfterWrite
= 0; //buzzy
30 extern pthread_mutex_t print_lock
;
32 static char *logfilename
= "proxmark3.log";
34 void PrintAndLog(char *fmt
, ...)
38 va_list argptr
, argptr2
;
39 static FILE *logfile
= NULL
;
42 // lock this section to avoid interlacing prints from different threats
43 pthread_mutex_lock(&print_lock
);
45 if (logging
&& !logfile
) {
46 logfile
=fopen(logfilename
, "a");
48 fprintf(stderr
, "Can't open logfile, logging disabled!\n");
53 int need_hack
= (rl_readline_state
& RL_STATE_READCMD
) > 0;
56 saved_point
= rl_point
;
57 saved_line
= rl_copy_text(0, rl_end
);
59 rl_replace_line("", 0);
63 va_start(argptr
, fmt
);
64 va_copy(argptr2
, argptr
);
66 printf(" "); // cleaning prompt
72 rl_replace_line(saved_line
, 0);
73 rl_point
= saved_point
;
78 if (logging
&& logfile
) {
79 vfprintf(logfile
, fmt
, argptr2
);
80 fprintf(logfile
,"\n");
85 if (flushAfterWrite
== 1) //buzzy
90 pthread_mutex_unlock(&print_lock
);
93 void SetLogFilename(char *fn
)
98 int manchester_decode( int * data
, const size_t len
, uint8_t * dataout
){
101 int i
, clock
, high
, low
, startindex
;
102 low
= startindex
= 0;
104 uint8_t * bitStream
= (uint8_t* ) malloc(sizeof(uint8_t) * len
);
105 memset(bitStream
, 0x00, len
);
107 /* Detect high and lows */
108 for (i
= 0; i
< len
; i
++) {
111 else if (data
[i
] < low
)
116 clock
= GetT55x7Clock( data
, len
, high
);
117 startindex
= DetectFirstTransition(data
, len
, high
);
119 PrintAndLog(" Clock : %d", clock
);
122 bitlength
= ManchesterConvertFrom255(data
, len
, bitStream
, high
, low
, clock
, startindex
);
124 bitlength
= ManchesterConvertFrom1(data
, len
, bitStream
, clock
, startindex
);
126 memcpy(dataout
, bitStream
, bitlength
);
131 int GetT55x7Clock( const int * data
, const size_t len
, int peak
){
133 int i
,lastpeak
,clock
;
137 /* Detect peak if we don't have one */
139 for (i
= 0; i
< len
; ++i
) {
140 if (data
[i
] > peak
) {
146 for (i
= 1; i
< len
; ++i
) {
147 /* if this is the beginning of a peak */
148 if ( data
[i
-1] != data
[i
] && data
[i
] == peak
) {
149 /* find lowest difference between peaks */
150 if (lastpeak
&& i
- lastpeak
< clock
)
151 clock
= i
- lastpeak
;
156 //defaults clock to precise values.
171 //PrintAndLog(" Found Clock : %d - trying to adjust", clock);
173 // When detected clock is 31 or 33 then then return
174 int clockmod
= clock
%8;
177 else if ( clockmod
== 1 )
183 int DetectFirstTransition(const int * data
, const size_t len
, int threshold
){
186 /* now look for the first threshold */
187 for (; i
< len
; ++i
) {
188 if (data
[i
] == threshold
) {
195 int ManchesterConvertFrom255(const int * data
, const size_t len
, uint8_t * dataout
, int high
, int low
, int clock
, int startIndex
){
197 int i
, j
, z
, hithigh
, hitlow
, bitIndex
, startType
;
202 int damplimit
= (int)((high
/ 2) * 0.3);
203 int dampHi
= (high
/2)+damplimit
;
204 int dampLow
= (high
/2)-damplimit
;
207 // i = clock frame of data
208 for (; i
< (int)(len
/ clock
); i
++)
213 z
= startIndex
+ (i
*clock
);
216 /* Find out if we hit both high and low peaks */
217 for (j
= 0; j
< clock
; j
++)
219 if (data
[z
+j
] == high
){
221 if ( startType
== -1)
225 if (data
[z
+j
] == low
){
227 if ( startType
== -1)
231 if (hithigh
&& hitlow
)
235 // No high value found, are we in a dampening field?
237 //PrintAndLog(" # Entering damp test at index : %d (%d)", z+j, j);
238 for (j
= 0; j
< clock
; j
++) {
240 (data
[z
+j
] <= dampHi
&& data
[z
+j
] >= dampLow
)
247 /* Manchester Switching..
252 dataout
[bitIndex
++] = 1;
253 else if (startType
== 1)
254 dataout
[bitIndex
++] = 0;
256 dataout
[bitIndex
++] = 2;
258 if ( isDamp
> clock
/2 ) {
268 int ManchesterConvertFrom1(const int * data
, const size_t len
, uint8_t * dataout
, int clock
, int startIndex
){
270 PrintAndLog(" Path B");
272 int i
,j
, bitindex
, lc
, tolerance
, warnings
;
274 int upperlimit
= len
*2/clock
+8;
278 uint8_t decodedArr
[len
];
280 /* Detect duration between 2 successive transitions */
281 for (bitindex
= 1; i
< len
; i
++) {
283 if (data
[i
-1] != data
[i
]) {
287 // Error check: if bitindex becomes too large, we do not
288 // have a Manchester encoded bitstream or the clock is really wrong!
289 if (bitindex
> upperlimit
) {
290 PrintAndLog("Error: the clock you gave is probably wrong, aborting.");
293 // Then switch depending on lc length:
294 // Tolerance is 1/4 of clock rate (arbitrary)
295 if (abs((lc
-clock
)/2) < tolerance
) {
296 // Short pulse : either "1" or "0"
297 decodedArr
[bitindex
++] = data
[i
-1];
298 } else if (abs(lc
-clock
) < tolerance
) {
299 // Long pulse: either "11" or "00"
300 decodedArr
[bitindex
++] = data
[i
-1];
301 decodedArr
[bitindex
++] = data
[i
-1];
304 PrintAndLog("Warning: Manchester decode error for pulse width detection.");
306 PrintAndLog("Error: too many detection errors, aborting.");
314 * We have a decodedArr of "01" ("1") or "10" ("0")
315 * parse it into final decoded dataout
317 for (i
= 0; i
< bitindex
; i
+= 2) {
319 if ((decodedArr
[i
] == 0) && (decodedArr
[i
+1] == 1)) {
321 } else if ((decodedArr
[i
] == 1) && (decodedArr
[i
+1] == 0)) {
326 PrintAndLog("Unsynchronized, resync...");
327 PrintAndLog("(too many of those messages mean the stream is not Manchester encoded)");
330 PrintAndLog("Error: too many decode errors, aborting.");
336 PrintAndLog("%s", sprint_hex(dataout
, j
));
340 void ManchesterDiffDecodedString(const uint8_t* bitstream
, size_t len
, uint8_t invert
){
342 * We have a bitstream of "01" ("1") or "10" ("0")
343 * parse it into final decoded bitstream
346 uint8_t decodedArr
[(len
/2)+1];
352 for (i
= 0; i
< len
; i
+= 2) {
354 uint8_t first
= bitstream
[i
];
355 uint8_t second
= bitstream
[i
+1];
357 if ( first
== second
) {
361 PrintAndLog("Error: too many decode errors, aborting.");
365 else if ( lastbit
!= first
) {
366 decodedArr
[j
++] = 0 ^ invert
;
369 decodedArr
[j
++] = 1 ^ invert
;
374 PrintAndLog("%s", sprint_hex(decodedArr
, j
));
377 void PrintPaddedManchester( uint8_t* bitStream
, size_t len
, size_t blocksize
){
379 PrintAndLog(" Manchester decoded : %d bits", len
);
381 uint8_t mod
= len
% blocksize
;
382 uint8_t div
= len
/ blocksize
;
385 // Now output the bitstream to the scrollback by line of 16 bits
386 for (i
= 0; i
< div
*blocksize
; i
+=blocksize
) {
387 PrintAndLog(" %s", sprint_bin(bitStream
+i
,blocksize
) );
391 PrintAndLog(" %s", sprint_bin(bitStream
+i
, mod
) );
397 void iceFsk2(int * data
, const size_t len
){
400 int * output
= (int* ) malloc(sizeof(int) * len
);
401 memset(output
, 0x00, len
);
403 // for (i=0; i<len-5; ++i){
404 // for ( j=1; j <=5; ++j) {
405 // output[i] += data[i*j];
411 for (i
=0; i
<len
; ++i
){
415 tmp
= (100 * (data
[i
]-rest
)) / rest
;
416 output
[i
] = (tmp
> 60)? 100:0;
420 for (j
=0; j
<len
; ++j
)
426 void iceFsk3(int * data
, const size_t len
){
430 int * output
= (int* ) malloc(sizeof(int) * len
);
431 memset(output
, 0x00, len
);
432 float fc
= 0.1125f
; // center frequency
433 size_t adjustedLen
= len
;
435 // create very simple low-pass filter to remove images (2nd-order Butterworth)
436 float complex iir_buf
[3] = {0,0,0};
437 float b
[3] = {0.003621681514929, 0.007243363029857, 0.003621681514929};
438 float a
[3] = {1.000000000000000, -1.822694925196308, 0.837181651256023};
440 float sample
= 0; // input sample read from file
441 float complex x_prime
= 1.0f
; // save sample for estimating frequency
444 for (i
=0; i
<adjustedLen
; ++i
) {
446 sample
= data
[i
]+128;
448 // remove DC offset and mix to complex baseband
449 x
= (sample
- 127.5f
) * cexpf( _Complex_I
* 2 * M_PI
* fc
* i
);
451 // apply low-pass filter, removing spectral image (IIR using direct-form II)
452 iir_buf
[2] = iir_buf
[1];
453 iir_buf
[1] = iir_buf
[0];
454 iir_buf
[0] = x
- a
[1]*iir_buf
[1] - a
[2]*iir_buf
[2];
455 x
= b
[0]*iir_buf
[0] +
459 // compute instantaneous frequency by looking at phase difference
460 // between adjacent samples
461 float freq
= cargf(x
*conjf(x_prime
));
462 x_prime
= x
; // retain this sample for next iteration
464 output
[i
] =(freq
> 0)? 10 : -10;
468 for (j
=0; j
<adjustedLen
; ++j
)
475 for (j
=0; j
<adjustedLen
; ++j
){
476 if ( data
[j
] == 10) break;
480 for (;j
<adjustedLen
; ++j
){
481 if ( data
[j
] == -10 ) break;
485 int fieldlen
= stopOne
-startOne
;
487 fieldlen
= (fieldlen
== 39 || fieldlen
== 41)? 40 : fieldlen
;
488 fieldlen
= (fieldlen
== 59 || fieldlen
== 51)? 50 : fieldlen
;
489 if ( fieldlen
!= 40 && fieldlen
!= 50){
490 printf("Detected field Length: %d \n", fieldlen
);
491 printf("Can only handle 40 or 50. Aborting...\n");
495 // FSK sequence start == 000111
497 for (i
=0; i
<adjustedLen
; ++i
){
499 for ( j
= 0; j
< 6*fieldlen
; ++j
){
508 printf("000111 position: %d \n", startPos
);
510 startPos
+= 6*fieldlen
+5;
515 for (i
=startPos
; i
< adjustedLen
; i
+= 40){
516 bit
= data
[i
]>0 ? 1:0;
522 for (i
=startPos
; i
< adjustedLen
; i
+= 50){
523 bit
= data
[i
]>0 ? 1:0;
524 printf("%d", bit
); }
530 float complex cexpf (float complex Z
)
533 double rho
= exp (__real__ Z
);
534 __real__ Res
= rho
* cosf(__imag__ Z
);
535 __imag__ Res
= rho
* sinf(__imag__ Z
);