]> git.zerfleddert.de Git - proxmark3-svn/blame - client/cmdanalyse.c
CHG: enhanced the debug output for some LF demod/decode
[proxmark3-svn] / client / cmdanalyse.c
CommitLineData
812513bf 1//-----------------------------------------------------------------------------
2// Copyright (C) 2016 iceman
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// Analyse bytes commands
9//-----------------------------------------------------------------------------
10#include "cmdanalyse.h"
b403c300 11#include "nonce2key/nonce2key.h"
812513bf 12
13static int CmdHelp(const char *Cmd);
14
15int usage_analyse_lcr(void) {
16 PrintAndLog("Specifying the bytes of a UID with a known LRC will find the last byte value");
17 PrintAndLog("needed to generate that LRC with a rolling XOR. All bytes should be specified in HEX.");
18 PrintAndLog("");
19 PrintAndLog("Usage: analyse lcr [h] <bytes>");
20 PrintAndLog("Options:");
21 PrintAndLog(" h This help");
22 PrintAndLog(" <bytes> bytes to calc missing XOR in a LCR");
23 PrintAndLog("");
24 PrintAndLog("Samples:");
25 PrintAndLog(" analyse lcr 04008064BA");
26 PrintAndLog("expected output: Target (BA) requires final LRC XOR byte value: 5A");
27 return 0;
28}
53b3c3e8 29int usage_analyse_checksum(void) {
30 PrintAndLog("The bytes will be added with eachother and than limited with the applied mask");
31 PrintAndLog("Finally compute ones' complement of the least significant bytes");
32 PrintAndLog("");
33 PrintAndLog("Usage: analyse chksum [h] b <bytes> m <mask>");
34 PrintAndLog("Options:");
35 PrintAndLog(" h This help");
36 PrintAndLog(" b <bytes> bytes to calc missing XOR in a LCR");
37 PrintAndLog(" m <mask> bit mask to limit the outpuyt");
38 PrintAndLog("");
39 PrintAndLog("Samples:");
40 PrintAndLog(" analyse chksum b 137AF00A0A0D m FF");
41 PrintAndLog("expected output: 0x61");
42 return 0;
43}
53b3c3e8 44int usage_analyse_crc(void){
45 PrintAndLog("A stub method to test different crc implementations inside the PM3 sourcecode. Just because you figured out the poly, doesn't mean you get the desired output");
46 PrintAndLog("");
47 PrintAndLog("Usage: analyse crc [h] <bytes>");
48 PrintAndLog("Options:");
49 PrintAndLog(" h This help");
50 PrintAndLog(" <bytes> bytes to calc crc");
51 PrintAndLog("");
52 PrintAndLog("Samples:");
53 PrintAndLog(" analyse crc 137AF00A0A0D");
54 return 0;
55}
09bb01c7 56int usage_analyse_hid(void){
57 PrintAndLog("Permute function from 'heart of darkness' paper.");
58 PrintAndLog("");
59 PrintAndLog("Usage: analyse hid [h] <r|f> <bytes>");
60 PrintAndLog("Options:");
61 PrintAndLog(" h This help");
62 PrintAndLog(" r reverse permuted key");
63 PrintAndLog(" f permute key");
64 PrintAndLog(" <bytes> input bytes");
65 PrintAndLog("");
66 PrintAndLog("Samples:");
67 PrintAndLog(" analyse hid r 0123456789abcdef");
68 return 0;
69}
53b3c3e8 70
812513bf 71static uint8_t calculateLRC( uint8_t* bytes, uint8_t len) {
72 uint8_t LRC = 0;
73 for (uint8_t i = 0; i < len; i++)
74 LRC ^= bytes[i];
75 return LRC;
76}
53b3c3e8 77
78static uint8_t calcSumCrumbAdd( uint8_t* bytes, uint8_t len, uint32_t mask) {
79 uint8_t sum = 0;
80 for (uint8_t i = 0; i < len; i++) {
81 sum += CRUMB(bytes[i], 0);
82 sum += CRUMB(bytes[i], 2);
83 sum += CRUMB(bytes[i], 4);
84 sum += CRUMB(bytes[i], 6);
85 }
6c283951 86 sum &= mask;
53b3c3e8 87 return sum;
88}
89static uint8_t calcSumCrumbAddOnes( uint8_t* bytes, uint8_t len, uint32_t mask) {
90 return ~calcSumCrumbAdd(bytes, len, mask);
91}
92static uint8_t calcSumNibbleAdd( uint8_t* bytes, uint8_t len, uint32_t mask) {
93 uint8_t sum = 0;
94 for (uint8_t i = 0; i < len; i++) {
95 sum += NIBBLE_LOW(bytes[i]);
96 sum += NIBBLE_HIGH(bytes[i]);
97 }
6c283951 98 sum &= mask;
53b3c3e8 99 return sum;
100}
101static uint8_t calcSumNibbleAddOnes( uint8_t* bytes, uint8_t len, uint32_t mask){
102 return ~calcSumNibbleAdd(bytes, len, mask);
103}
104
105static uint8_t calcSumByteAdd( uint8_t* bytes, uint8_t len, uint32_t mask) {
106 uint8_t sum = 0;
107 for (uint8_t i = 0; i < len; i++)
108 sum += bytes[i];
6c283951 109 sum &= mask;
53b3c3e8 110 return sum;
111}
112// Ones complement
113static uint8_t calcSumByteAddOnes( uint8_t* bytes, uint8_t len, uint32_t mask) {
114 return ~calcSumByteAdd(bytes, len, mask);
115}
116
117static uint8_t calcSumByteSub( uint8_t* bytes, uint8_t len, uint32_t mask) {
118 uint8_t sum = 0;
119 for (uint8_t i = 0; i < len; i++)
120 sum -= bytes[i];
6c283951 121 sum &= mask;
53b3c3e8 122 return sum;
123}
124static uint8_t calcSumByteSubOnes( uint8_t* bytes, uint8_t len, uint32_t mask){
125 return ~calcSumByteSub(bytes, len, mask);
126}
127static uint8_t calcSumNibbleSub( uint8_t* bytes, uint8_t len, uint32_t mask) {
128 uint8_t sum = 0;
129 for (uint8_t i = 0; i < len; i++) {
130 sum -= NIBBLE_LOW(bytes[i]);
131 sum -= NIBBLE_HIGH(bytes[i]);
132 }
6c283951 133 sum &= mask;
53b3c3e8 134 return sum;
135}
136static uint8_t calcSumNibbleSubOnes( uint8_t* bytes, uint8_t len, uint32_t mask) {
137 return ~calcSumNibbleSub(bytes, len, mask);
138}
139
b403c300 140// measuring LFSR maximum length
141int CmdAnalyseLfsr(const char *Cmd){
142
143 uint16_t start_state = 0; /* Any nonzero start state will work. */
144 uint16_t lfsr = start_state;
145 //uint32_t period = 0;
146
147 uint8_t iv = param_get8ex(Cmd, 0, 0, 16);
148 uint8_t find = param_get8ex(Cmd, 1, 0, 16);
149
150 printf("LEGIC LFSR IV 0x%02X: \n", iv);
151 printf(" bit# | lfsr | ^0x40 | 0x%02X ^ lfsr \n",find);
152
153 for (uint8_t i = 0x01; i < 0x30; i += 1) {
154 //period = 0;
155 legic_prng_init(iv);
156 legic_prng_forward(i);
157 lfsr = legic_prng_get_bits(12);
158
159 printf(" %02X | %03X | %03X | %03X \n",i, lfsr, 0x40 ^ lfsr, find ^ lfsr);
160 }
161 return 0;
162}
812513bf 163int CmdAnalyseLCR(const char *Cmd) {
164 uint8_t data[50];
165 char cmdp = param_getchar(Cmd, 0);
166 if (strlen(Cmd) == 0|| cmdp == 'h' || cmdp == 'H') return usage_analyse_lcr();
167
168 int len = 0;
169 param_gethex_ex(Cmd, 0, data, &len);
170 if ( len%2 ) return usage_analyse_lcr();
171 len >>= 1;
172 uint8_t finalXor = calculateLRC(data, len);
173 PrintAndLog("Target [%02X] requires final LRC XOR byte value: 0x%02X",data[len-1] ,finalXor);
174 return 0;
175}
53b3c3e8 176int CmdAnalyseCRC(const char *Cmd) {
177
178 char cmdp = param_getchar(Cmd, 0);
179 if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_analyse_crc();
180
181 int len = strlen(Cmd);
182 if ( len & 1 ) return usage_analyse_crc();
183
184 // add 1 for null terminator.
185 uint8_t *data = malloc(len+1);
186 if ( data == NULL ) return 1;
187
188 if ( param_gethex(Cmd, 0, data, len)) {
189 free(data);
190 return usage_analyse_crc();
191 }
192 len >>= 1;
193
6c283951 194 //PrintAndLog("\nTests with '%s' hex bytes", sprint_hex(data, len));
53b3c3e8 195
196 PrintAndLog("\nTests of reflection. Two current methods in source code");
197 PrintAndLog(" reflect(0x3e23L,3) is %04X == 0x3e26", reflect(0x3e23L,3) );
198 PrintAndLog(" SwapBits(0x3e23L,3) is %04X == 0x3e26", SwapBits(0x3e23L,3) );
199 PrintAndLog(" 0xB400 == %04X", reflect( (1 << 16 | 0xb400),16) );
200
201 //
202 // Test of CRC16, '123456789' string.
203 //
204 PrintAndLog("\nTests with '123456789' string");
205 uint8_t dataStr[] = { 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39 };
206 uint8_t legic8 = CRC8Legic(dataStr, sizeof(dataStr));
207
53b3c3e8 208 PrintAndLog("LEGIC: CRC16: %X", CRC16Legic(dataStr, sizeof(dataStr), legic8));
209
210 //these below has been tested OK.
211 PrintAndLog("Confirmed CRC Implementations");
212 PrintAndLog("LEGIC: CRC8 : %X (0xC6 expected)", legic8);
213 PrintAndLog("MAXIM: CRC8 : %X (0xA1 expected)", CRC8Maxim(dataStr, sizeof(dataStr)));
214 PrintAndLog("DNP : CRC16: %X (0x82EA expected)", CRC16_DNP(dataStr, sizeof(dataStr)));
df007486 215 PrintAndLog("CCITT: CRC16: %X (0xE5CC expected)", CRC16_CCITT(dataStr, sizeof(dataStr)));
216
217 PrintAndLog("ICLASS org: CRC16: %X (0x expected)",iclass_crc16( (char*)dataStr, sizeof(dataStr)));
218 PrintAndLog("ICLASS ice: CRC16: %X (0x expected)",CRC16_ICLASS(dataStr, sizeof(dataStr)));
219
220
221
222 uint8_t dataStr1234[] = { 0x1,0x2,0x3,0x4};
223 PrintAndLog("ISO15693 org: : CRC16: %X (0xF0B8 expected)", Iso15693Crc(dataStr1234, sizeof(dataStr1234)));
224 PrintAndLog("ISO15693 ice: : CRC16: %X (0xF0B8 expected)", CRC16_Iso15693(dataStr1234, sizeof(dataStr1234)));
53b3c3e8 225
226 free(data);
227 return 0;
228}
229int CmdAnalyseCHKSUM(const char *Cmd){
230
231 uint8_t data[50];
232 uint8_t cmdp = 0;
233 uint32_t mask = 0xFF;
234 bool errors = false;
235 int len = 0;
5f7e30f8 236 memset(data, 0x0, sizeof(data));
53b3c3e8 237
238 while(param_getchar(Cmd, cmdp) != 0x00) {
239 switch(param_getchar(Cmd, cmdp)) {
240 case 'b':
241 case 'B':
242 param_gethex_ex(Cmd, cmdp+1, data, &len);
243 if ( len%2 ) errors = true;
244 len >>= 1;
245 cmdp += 2;
246 break;
247 case 'm':
248 case 'M':
249 mask = param_get32ex(Cmd, cmdp+1, 0, 16);
250 cmdp += 2;
251 break;
252 case 'h':
253 case 'H':
254 return usage_analyse_checksum();
255 default:
256 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
257 errors = true;
258 break;
259 }
260 if(errors) break;
261 }
262 //Validations
263 if(errors) return usage_analyse_checksum();
264
265 PrintAndLog("\nByte Add | 0x%X", calcSumByteAdd(data, len, mask));
266 PrintAndLog("Nibble Add | 0x%X", calcSumNibbleAdd(data, len, mask));
267 PrintAndLog("Crumb Add | 0x%X", calcSumCrumbAdd(data, len, mask));
268
269 PrintAndLog("\nByte Subtract | 0x%X", calcSumByteSub(data, len, mask));
270 PrintAndLog("Nibble Subtract | 0x%X", calcSumNibbleSub(data, len, mask));
271
272 PrintAndLog("\nCHECKSUM - One's complement");
273 PrintAndLog("Byte Add | 0x%X", calcSumByteAddOnes(data, len, mask));
274 PrintAndLog("Nibble Add | 0x%X", calcSumNibbleAddOnes(data, len, mask));
275 PrintAndLog("Crumb Add | 0x%X", calcSumCrumbAddOnes(data, len, mask));
276
277 PrintAndLog("Byte Subtract | 0x%X", calcSumByteSubOnes(data, len, mask));
278 PrintAndLog("Nibble Subtract | 0x%X", calcSumNibbleSubOnes(data, len, mask));
279
280 return 0;
281}
812513bf 282
5558d935 283int CmdAnalyseDates(const char *Cmd){
284 // look for datestamps in a given array of bytes
53b3c3e8 285 PrintAndLog("To be implemented. Feel free to contribute!");
5558d935 286 return 0;
287}
16658b1f 288int CmdAnalyseTEASelfTest(const char *Cmd){
289
290 uint8_t v[8], v_le[8];
291 memset(v, 0x00, sizeof(v));
292 memset(v_le, 0x00, sizeof(v_le));
293 uint8_t* v_ptr = v_le;
294
295 uint8_t cmdlen = strlen(Cmd);
296 cmdlen = ( sizeof(v)<<2 < cmdlen ) ? sizeof(v)<<2 : cmdlen;
297
298 if ( param_gethex(Cmd, 0, v, cmdlen) > 0 ){
299 PrintAndLog("can't read hex chars, uneven? :: %u", cmdlen);
300 return 1;
301 }
302
303 SwapEndian64ex(v , 8, 4, v_ptr);
304
305 // ENCRYPTION KEY:
306 uint8_t key[16] = {0x55,0xFE,0xF6,0x30,0x62,0xBF,0x0B,0xC1,0xC9,0xB3,0x7C,0x34,0x97,0x3E,0x29,0xFB };
307 uint8_t keyle[16];
308 uint8_t* key_ptr = keyle;
309 SwapEndian64ex(key , sizeof(key), 4, key_ptr);
310
311 PrintAndLog("TEST LE enc| %s", sprint_hex(v_ptr, 8));
312
313 tea_decrypt(v_ptr, key_ptr);
314 PrintAndLog("TEST LE dec | %s", sprint_hex_ascii(v_ptr, 8));
315
316 tea_encrypt(v_ptr, key_ptr);
317 tea_encrypt(v_ptr, key_ptr);
318 PrintAndLog("TEST enc2 | %s", sprint_hex_ascii(v_ptr, 8));
319
320 return 0;
321}
5558d935 322
b403c300 323int CmdAnalyseA(const char *Cmd){
09bb01c7 324/*
325piwi
326// uid(2e086b1a) nt(230736f6) ks(0b0008000804000e) nr(000000000)
327// uid(2e086b1a) nt(230736f6) ks(0e0b0e0b090c0d02) nr(000000001)
328// uid(2e086b1a) nt(230736f6) ks(0e05060e01080b08) nr(000000002)
329uint64_t d1[] = {0x2e086b1a, 0x230736f6, 0x0000001, 0x0e0b0e0b090c0d02};
330uint64_t d2[] = {0x2e086b1a, 0x230736f6, 0x0000002, 0x0e05060e01080b08};
b403c300 331
09bb01c7 332// uid(17758822) nt(c0c69e59) ks(080105020705040e) nr(00000001)
333// uid(17758822) nt(c0c69e59) ks(01070a05050c0705) nr(00000002)
334uint64_t d1[] = {0x17758822, 0xc0c69e59, 0x0000001, 0x080105020705040e};
335uint64_t d2[] = {0x17758822, 0xc0c69e59, 0x0000002, 0x01070a05050c0705};
336
337// uid(6e442129) nt(8f699195) ks(090d0b0305020f02) nr(00000001)
338// uid(6e442129) nt(8f699195) ks(03030508030b0c0e) nr(00000002)
339// uid(6e442129) nt(8f699195) ks(02010f030c0d050d) nr(00000003)
340// uid(6e442129) nt(8f699195) ks(00040f0f0305030e) nr(00000004)
341uint64_t d1[] = {0x6e442129, 0x8f699195, 0x0000001, 0x090d0b0305020f02};
342uint64_t d2[] = {0x6e442129, 0x8f699195, 0x0000004, 0x00040f0f0305030e};
343
344uid(3e172b29) nt(039b7bd2) ks(0c0e0f0505080800) nr(00000001)
345uid(3e172b29) nt(039b7bd2) ks(0e06090d03000b0f) nr(00000002)
346*/
347 uint64_t key = 0;
348 uint64_t d1[] = {0x3e172b29, 0x039b7bd2, 0x0000001, 0x0c0e0f0505080800};
349 uint64_t d2[] = {0x3e172b29, 0x039b7bd2, 0x0000002, 0x0e06090d03000b0f};
350
351 nonce2key_ex(0, 0 , d1[0], d1[1], d1[2], d1[3], &key);
352 nonce2key_ex(0, 0 , d2[0], d2[1], d2[2], d2[3], &key);
353 return 0;
354}
b403c300 355
09bb01c7 356static void permute(uint8_t *data, uint8_t len, uint8_t *output){
357#define KEY_SIZE 8
b403c300 358
09bb01c7 359 if ( len > KEY_SIZE ) {
360 for(uint8_t m = 0; m < len; m += KEY_SIZE){
361 permute(data+m, KEY_SIZE, output+m);
362 }
363 return;
364 }
365 if ( len != KEY_SIZE ) {
366 printf("wrong key size\n");
367 return;
368 }
369 uint8_t i,j,p, mask;
370 for( i=0; i < KEY_SIZE; ++i){
371 p = 0;
372 mask = 0x80 >> i;
373 for( j=0; j < KEY_SIZE; ++j){
374 p >>= 1;
375 if (data[j] & mask)
376 p |= 0x80;
377 }
378 output[i] = p;
379 }
380}
381static void permute_rev(uint8_t *data, uint8_t len, uint8_t *output){
382 permute(data, len, output);
383 permute(output, len, data);
384 permute(data, len, output);
385}
386static void simple_crc(uint8_t *data, uint8_t len, uint8_t *output){
387 uint8_t crc = 0;
388 for( uint8_t i=0; i < len; ++i){
389 // seventh byte contains the crc.
390 if ( (i & 0x7) == 0x7 ) {
391 output[i] = crc ^ 0xFF;
392 crc = 0;
393 } else {
394 output[i] = data[i];
395 crc ^= data[i];
396 }
397 }
398}
399// DES doesn't use the MSB.
400static void shave(uint8_t *data, uint8_t len){
401 for (uint8_t i=0; i<len; ++i)
402 data[i] &= 0xFE;
403}
404static void generate_rev(uint8_t *data, uint8_t len) {
405 uint8_t *key = calloc(len,1);
406 printf("input permuted key | %s \n", sprint_hex(data, len));
407 permute_rev(data, len, key);
408 printf(" unpermuted key | %s \n", sprint_hex(key, len));
409 shave(key, len);
410 printf(" key | %s \n", sprint_hex(key, len));
411 free(key);
412}
413static void generate(uint8_t *data, uint8_t len) {
414 uint8_t *key = calloc(len,1);
415 uint8_t *pkey = calloc(len,1);
416 printf(" input key | %s \n", sprint_hex(data, len));
417 permute(data, len, pkey);
418 printf(" permuted key | %s \n", sprint_hex(pkey, len));
419 simple_crc(pkey, len, key );
420 printf(" CRC'ed key | %s \n", sprint_hex(key, len));
421 free(key);
422 free(pkey);
423}
424int CmdAnalyseHid(const char *Cmd){
b403c300 425
09bb01c7 426 uint8_t data[16] = {0};
427 bool isReverse = FALSE;
428 int len = 0;
429 char cmdp = param_getchar(Cmd, 0);
430 if (strlen(Cmd) == 0|| cmdp == 'h' || cmdp == 'H') return usage_analyse_hid();
431
432 if ( cmdp == 'r' || cmdp == 'R' )
433 isReverse = TRUE;
434
435 param_gethex_ex(Cmd, 1, data, &len);
436 if ( len%2 ) return usage_analyse_hid();
437
438 len >>= 1;
439
440 if ( isReverse )
441 generate_rev(data, len);
442 else
443 generate(data, len);
b403c300 444 return 0;
445}
446
812513bf 447static command_t CommandTable[] = {
5558d935 448 {"help", CmdHelp, 1, "This help"},
53b3c3e8 449 {"lcr", CmdAnalyseLCR, 1, "Generate final byte for XOR LRC"},
450 {"crc", CmdAnalyseCRC, 1, "Stub method for CRC evaluations"},
451 {"chksum", CmdAnalyseCHKSUM, 1, "Checksum with adding, masking and one's complement"},
452 {"dates", CmdAnalyseDates, 1, "Look for datestamps in a given array of bytes"},
16658b1f 453 {"tea", CmdAnalyseTEASelfTest, 1, "Crypto TEA test"},
b403c300 454 {"lfsr", CmdAnalyseLfsr, 1, "LFSR tests"},
455 {"a", CmdAnalyseA, 1, "num bits test"},
09bb01c7 456 {"hid", CmdAnalyseHid, 1, "Permute function from 'heart of darkness' paper"},
812513bf 457 {NULL, NULL, 0, NULL}
458};
459
460int CmdAnalyse(const char *Cmd) {
461 clearCommandBuffer();
462 CmdsParse(CommandTable, Cmd);
463 return 0;
464}
465
466int CmdHelp(const char *Cmd) {
467 CmdsHelp(CommandTable);
468 return 0;
469}
Impressum, Datenschutz