]>
Commit | Line | Data |
---|---|---|
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" | |
11 | #include "nonce2key/nonce2key.h" | |
12 | ||
13 | static int CmdHelp(const char *Cmd); | |
14 | ||
15 | int 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 | } | |
29 | int 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 | } | |
44 | int 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 | } | |
56 | int 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 | } | |
70 | ||
71 | static 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 | } | |
77 | ||
78 | static uint16_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 | } | |
86 | sum &= mask; | |
87 | return sum; | |
88 | } | |
89 | static uint16_t calcSumCrumbAddOnes( uint8_t* bytes, uint8_t len, uint32_t mask) { | |
90 | return (~calcSumCrumbAdd(bytes, len, mask) & mask); | |
91 | } | |
92 | static uint16_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 | } | |
98 | sum &= mask; | |
99 | return sum; | |
100 | } | |
101 | static uint16_t calcSumNibbleAddOnes( uint8_t* bytes, uint8_t len, uint32_t mask){ | |
102 | return (~calcSumNibbleAdd(bytes, len, mask) & mask); | |
103 | } | |
104 | static uint16_t calcSumCrumbXor( uint8_t* bytes, uint8_t len, uint32_t mask) { | |
105 | uint8_t sum = 0; | |
106 | for (uint8_t i = 0; i < len; i++) { | |
107 | sum ^= CRUMB(bytes[i], 0); | |
108 | sum ^= CRUMB(bytes[i], 2); | |
109 | sum ^= CRUMB(bytes[i], 4); | |
110 | sum ^= CRUMB(bytes[i], 6); | |
111 | } | |
112 | sum &= mask; | |
113 | return sum; | |
114 | } | |
115 | static uint16_t calcSumNibbleXor( uint8_t* bytes, uint8_t len, uint32_t mask) { | |
116 | uint8_t sum = 0; | |
117 | for (uint8_t i = 0; i < len; i++) { | |
118 | sum ^= NIBBLE_LOW(bytes[i]); | |
119 | sum ^= NIBBLE_HIGH(bytes[i]); | |
120 | } | |
121 | sum &= mask; | |
122 | return sum; | |
123 | } | |
124 | static uint16_t calcSumByteXor( uint8_t* bytes, uint8_t len, uint32_t mask) { | |
125 | uint8_t sum = 0; | |
126 | for (uint8_t i = 0; i < len; i++) | |
127 | sum ^= bytes[i]; | |
128 | sum &= mask; | |
129 | return sum; | |
130 | } | |
131 | static uint16_t calcSumByteAdd( uint8_t* bytes, uint8_t len, uint32_t mask) { | |
132 | uint8_t sum = 0; | |
133 | for (uint8_t i = 0; i < len; i++) | |
134 | sum += bytes[i]; | |
135 | sum &= mask; | |
136 | return sum; | |
137 | } | |
138 | // Ones complement | |
139 | static uint16_t calcSumByteAddOnes( uint8_t* bytes, uint8_t len, uint32_t mask) { | |
140 | return (~calcSumByteAdd(bytes, len, mask) & mask); | |
141 | } | |
142 | ||
143 | static uint16_t calcSumByteSub( uint8_t* bytes, uint8_t len, uint32_t mask) { | |
144 | uint8_t sum = 0; | |
145 | for (uint8_t i = 0; i < len; i++) | |
146 | sum -= bytes[i]; | |
147 | sum &= mask; | |
148 | return sum; | |
149 | } | |
150 | static uint16_t calcSumByteSubOnes( uint8_t* bytes, uint8_t len, uint32_t mask){ | |
151 | return (~calcSumByteSub(bytes, len, mask) & mask); | |
152 | } | |
153 | static uint16_t calcSumNibbleSub( uint8_t* bytes, uint8_t len, uint32_t mask) { | |
154 | uint8_t sum = 0; | |
155 | for (uint8_t i = 0; i < len; i++) { | |
156 | sum -= NIBBLE_LOW(bytes[i]); | |
157 | sum -= NIBBLE_HIGH(bytes[i]); | |
158 | } | |
159 | sum &= mask; | |
160 | return sum; | |
161 | } | |
162 | static uint16_t calcSumNibbleSubOnes( uint8_t* bytes, uint8_t len, uint32_t mask) { | |
163 | return (~calcSumNibbleSub(bytes, len, mask) & mask); | |
164 | } | |
165 | ||
166 | // BSD shift checksum 8bit version | |
167 | static uint16_t calcBSDchecksum8( uint8_t* bytes, uint8_t len, uint32_t mask){ | |
168 | uint16_t sum = 0; | |
169 | for(uint8_t i = 0; i < len; i++){ | |
170 | sum = ((sum & 0xFF) >> 1) | ((sum & 0x1) << 7); // rotate accumulator | |
171 | sum += bytes[i]; // add next byte | |
172 | sum &= 0xFF; // | |
173 | } | |
174 | sum &= mask; | |
175 | return sum; | |
176 | } | |
177 | // BSD shift checksum 4bit version | |
178 | static uint16_t calcBSDchecksum4( uint8_t* bytes, uint8_t len, uint32_t mask){ | |
179 | uint16_t sum = 0; | |
180 | for(uint8_t i = 0; i < len; i++){ | |
181 | sum = ((sum & 0xF) >> 1) | ((sum & 0x1) << 3); // rotate accumulator | |
182 | sum += NIBBLE_HIGH(bytes[i]); // add high nibble | |
183 | sum &= 0xF; // | |
184 | sum = ((sum & 0xF) >> 1) | ((sum & 0x1) << 3); // rotate accumulator | |
185 | sum += NIBBLE_LOW(bytes[i]); // add low nibble | |
186 | sum &= 0xF; // | |
187 | } | |
188 | sum &= mask; | |
189 | return sum; | |
190 | } | |
191 | ||
192 | // measuring LFSR maximum length | |
193 | int CmdAnalyseLfsr(const char *Cmd){ | |
194 | ||
195 | uint16_t start_state = 0; /* Any nonzero start state will work. */ | |
196 | uint16_t lfsr = start_state; | |
197 | //uint32_t period = 0; | |
198 | ||
199 | uint8_t iv = param_get8ex(Cmd, 0, 0, 16); | |
200 | uint8_t find = param_get8ex(Cmd, 1, 0, 16); | |
201 | ||
202 | printf("LEGIC LFSR IV 0x%02X: \n", iv); | |
203 | printf(" bit# | lfsr | ^0x40 | 0x%02X ^ lfsr \n",find); | |
204 | ||
205 | for (uint8_t i = 0x01; i < 0x30; i += 1) { | |
206 | //period = 0; | |
207 | legic_prng_init(iv); | |
208 | legic_prng_forward(i); | |
209 | lfsr = legic_prng_get_bits(12); | |
210 | ||
211 | printf(" %02X | %03X | %03X | %03X \n",i, lfsr, 0x40 ^ lfsr, find ^ lfsr); | |
212 | } | |
213 | return 0; | |
214 | } | |
215 | int CmdAnalyseLCR(const char *Cmd) { | |
216 | uint8_t data[50]; | |
217 | char cmdp = param_getchar(Cmd, 0); | |
218 | if (strlen(Cmd) == 0|| cmdp == 'h' || cmdp == 'H') return usage_analyse_lcr(); | |
219 | ||
220 | int len = 0; | |
221 | param_gethex_ex(Cmd, 0, data, &len); | |
222 | if ( len%2 ) return usage_analyse_lcr(); | |
223 | len >>= 1; | |
224 | uint8_t finalXor = calculateLRC(data, len); | |
225 | PrintAndLog("Target [%02X] requires final LRC XOR byte value: 0x%02X",data[len-1] ,finalXor); | |
226 | return 0; | |
227 | } | |
228 | int CmdAnalyseCRC(const char *Cmd) { | |
229 | ||
230 | char cmdp = param_getchar(Cmd, 0); | |
231 | if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_analyse_crc(); | |
232 | ||
233 | int len = strlen(Cmd); | |
234 | if ( len & 1 ) return usage_analyse_crc(); | |
235 | ||
236 | // add 1 for null terminator. | |
237 | uint8_t *data = malloc(len+1); | |
238 | if ( data == NULL ) return 1; | |
239 | ||
240 | if ( param_gethex(Cmd, 0, data, len)) { | |
241 | free(data); | |
242 | return usage_analyse_crc(); | |
243 | } | |
244 | len >>= 1; | |
245 | ||
246 | //PrintAndLog("\nTests with '%s' hex bytes", sprint_hex(data, len)); | |
247 | ||
248 | PrintAndLog("\nTests of reflection. Two current methods in source code"); | |
249 | PrintAndLog(" reflect(0x3e23L,3) is %04X == 0x3e26", reflect(0x3e23L,3) ); | |
250 | PrintAndLog(" SwapBits(0x3e23L,3) is %04X == 0x3e26", SwapBits(0x3e23L,3) ); | |
251 | PrintAndLog(" 0xB400 == %04X", reflect( (1 << 16 | 0xb400),16) ); | |
252 | ||
253 | // | |
254 | // Test of CRC16, '123456789' string. | |
255 | // | |
256 | PrintAndLog("\nTests with '123456789' string"); | |
257 | uint8_t dataStr[] = { 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39 }; | |
258 | uint8_t legic8 = CRC8Legic(dataStr, sizeof(dataStr)); | |
259 | ||
260 | PrintAndLog("LEGIC: CRC16: %X", CRC16Legic(dataStr, sizeof(dataStr), legic8)); | |
261 | ||
262 | //these below has been tested OK. | |
263 | PrintAndLog("Confirmed CRC Implementations"); | |
264 | PrintAndLog("LEGIC: CRC8 : %X (0xC6 expected)", legic8); | |
265 | PrintAndLog("MAXIM: CRC8 : %X (0xA1 expected)", CRC8Maxim(dataStr, sizeof(dataStr))); | |
266 | PrintAndLog("DNP : CRC16: %X (0x82EA expected)", CRC16_DNP(dataStr, sizeof(dataStr))); | |
267 | PrintAndLog("CCITT: CRC16: %X (0xE5CC expected)", CRC16_CCITT(dataStr, sizeof(dataStr))); | |
268 | ||
269 | PrintAndLog("ICLASS org: CRC16: %X (0x expected)",iclass_crc16( (char*)dataStr, sizeof(dataStr))); | |
270 | PrintAndLog("ICLASS ice: CRC16: %X (0x expected)",CRC16_ICLASS(dataStr, sizeof(dataStr))); | |
271 | ||
272 | ||
273 | ||
274 | uint8_t dataStr1234[] = { 0x1,0x2,0x3,0x4}; | |
275 | PrintAndLog("ISO15693 org: : CRC16: %X (0xF0B8 expected)", Iso15693Crc(dataStr1234, sizeof(dataStr1234))); | |
276 | PrintAndLog("ISO15693 ice: : CRC16: %X (0xF0B8 expected)", CRC16_Iso15693(dataStr1234, sizeof(dataStr1234))); | |
277 | ||
278 | free(data); | |
279 | return 0; | |
280 | } | |
281 | int CmdAnalyseCHKSUM(const char *Cmd){ | |
282 | ||
283 | uint8_t data[50]; | |
284 | uint8_t cmdp = 0; | |
285 | uint32_t mask = 0xFFFF; | |
286 | bool errors = false; | |
287 | bool useHeader = false; | |
288 | int len = 0; | |
289 | memset(data, 0x0, sizeof(data)); | |
290 | ||
291 | while(param_getchar(Cmd, cmdp) != 0x00) { | |
292 | switch(param_getchar(Cmd, cmdp)) { | |
293 | case 'b': | |
294 | case 'B': | |
295 | param_gethex_ex(Cmd, cmdp+1, data, &len); | |
296 | if ( len%2 ) errors = true; | |
297 | len >>= 1; | |
298 | cmdp += 2; | |
299 | break; | |
300 | case 'm': | |
301 | case 'M': | |
302 | mask = param_get32ex(Cmd, cmdp+1, 0, 16); | |
303 | cmdp += 2; | |
304 | break; | |
305 | case 'v': | |
306 | case 'V': | |
307 | useHeader = true; | |
308 | cmdp++; | |
309 | break; | |
310 | case 'h': | |
311 | case 'H': | |
312 | return usage_analyse_checksum(); | |
313 | default: | |
314 | PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp)); | |
315 | errors = true; | |
316 | break; | |
317 | } | |
318 | if(errors) break; | |
319 | } | |
320 | //Validations | |
321 | if(errors) return usage_analyse_checksum(); | |
322 | ||
323 | if (useHeader) { | |
324 | PrintAndLog(" add | sub | add 1's compl | sub 1's compl | xor"); | |
325 | PrintAndLog("byte nibble crumb | byte nibble | byte nibble cumb | byte nibble | byte nibble cumb | BSD |"); | |
326 | PrintAndLog("------------------+-------------+------------------+-----------------+--------------------"); | |
327 | } | |
328 | PrintAndLog("0x%X 0x%X 0x%X | 0x%X 0x%X | 0x%X 0x%X 0x%X | 0x%X 0x%X | 0x%X 0x%X 0x%X | 0x%X 0x%X |\n", | |
329 | calcSumByteAdd(data, len, mask) | |
330 | , calcSumNibbleAdd(data, len, mask) | |
331 | , calcSumCrumbAdd(data, len, mask) | |
332 | , calcSumByteSub(data, len, mask) | |
333 | , calcSumNibbleSub(data, len, mask) | |
334 | , calcSumByteAddOnes(data, len, mask) | |
335 | , calcSumNibbleAddOnes(data, len, mask) | |
336 | , calcSumCrumbAddOnes(data, len, mask) | |
337 | , calcSumByteSubOnes(data, len, mask) | |
338 | , calcSumNibbleSubOnes(data, len, mask) | |
339 | , calcSumByteXor(data, len, mask) | |
340 | , calcSumNibbleXor(data, len, mask) | |
341 | , calcSumCrumbXor(data, len, mask) | |
342 | , calcBSDchecksum8(data, len, mask) | |
343 | , calcBSDchecksum4(data, len, mask) | |
344 | ); | |
345 | return 0; | |
346 | } | |
347 | ||
348 | int CmdAnalyseDates(const char *Cmd){ | |
349 | // look for datestamps in a given array of bytes | |
350 | PrintAndLog("To be implemented. Feel free to contribute!"); | |
351 | return 0; | |
352 | } | |
353 | int CmdAnalyseTEASelfTest(const char *Cmd){ | |
354 | ||
355 | uint8_t v[8], v_le[8]; | |
356 | memset(v, 0x00, sizeof(v)); | |
357 | memset(v_le, 0x00, sizeof(v_le)); | |
358 | uint8_t* v_ptr = v_le; | |
359 | ||
360 | uint8_t cmdlen = strlen(Cmd); | |
361 | cmdlen = ( sizeof(v)<<2 < cmdlen ) ? sizeof(v)<<2 : cmdlen; | |
362 | ||
363 | if ( param_gethex(Cmd, 0, v, cmdlen) > 0 ){ | |
364 | PrintAndLog("can't read hex chars, uneven? :: %u", cmdlen); | |
365 | return 1; | |
366 | } | |
367 | ||
368 | SwapEndian64ex(v , 8, 4, v_ptr); | |
369 | ||
370 | // ENCRYPTION KEY: | |
371 | uint8_t key[16] = {0x55,0xFE,0xF6,0x30,0x62,0xBF,0x0B,0xC1,0xC9,0xB3,0x7C,0x34,0x97,0x3E,0x29,0xFB }; | |
372 | uint8_t keyle[16]; | |
373 | uint8_t* key_ptr = keyle; | |
374 | SwapEndian64ex(key , sizeof(key), 4, key_ptr); | |
375 | ||
376 | PrintAndLog("TEST LE enc| %s", sprint_hex(v_ptr, 8)); | |
377 | ||
378 | tea_decrypt(v_ptr, key_ptr); | |
379 | PrintAndLog("TEST LE dec | %s", sprint_hex_ascii(v_ptr, 8)); | |
380 | ||
381 | tea_encrypt(v_ptr, key_ptr); | |
382 | tea_encrypt(v_ptr, key_ptr); | |
383 | PrintAndLog("TEST enc2 | %s", sprint_hex_ascii(v_ptr, 8)); | |
384 | ||
385 | return 0; | |
386 | } | |
387 | ||
388 | int CmdAnalyseA(const char *Cmd){ | |
389 | /* | |
390 | piwi | |
391 | // uid(2e086b1a) nt(230736f6) ks(0b0008000804000e) nr(000000000) | |
392 | // uid(2e086b1a) nt(230736f6) ks(0e0b0e0b090c0d02) nr(000000001) | |
393 | // uid(2e086b1a) nt(230736f6) ks(0e05060e01080b08) nr(000000002) | |
394 | uint64_t d1[] = {0x2e086b1a, 0x230736f6, 0x0000001, 0x0e0b0e0b090c0d02}; | |
395 | uint64_t d2[] = {0x2e086b1a, 0x230736f6, 0x0000002, 0x0e05060e01080b08}; | |
396 | ||
397 | // uid(17758822) nt(c0c69e59) ks(080105020705040e) nr(00000001) | |
398 | // uid(17758822) nt(c0c69e59) ks(01070a05050c0705) nr(00000002) | |
399 | uint64_t d1[] = {0x17758822, 0xc0c69e59, 0x0000001, 0x080105020705040e}; | |
400 | uint64_t d2[] = {0x17758822, 0xc0c69e59, 0x0000002, 0x01070a05050c0705}; | |
401 | ||
402 | // uid(6e442129) nt(8f699195) ks(090d0b0305020f02) nr(00000001) | |
403 | // uid(6e442129) nt(8f699195) ks(03030508030b0c0e) nr(00000002) | |
404 | // uid(6e442129) nt(8f699195) ks(02010f030c0d050d) nr(00000003) | |
405 | // uid(6e442129) nt(8f699195) ks(00040f0f0305030e) nr(00000004) | |
406 | uint64_t d1[] = {0x6e442129, 0x8f699195, 0x0000001, 0x090d0b0305020f02}; | |
407 | uint64_t d2[] = {0x6e442129, 0x8f699195, 0x0000004, 0x00040f0f0305030e}; | |
408 | ||
409 | uid(3e172b29) nt(039b7bd2) ks(0c0e0f0505080800) nr(00000001) | |
410 | uid(3e172b29) nt(039b7bd2) ks(0e06090d03000b0f) nr(00000002) | |
411 | */ | |
412 | uint64_t key = 0; | |
413 | uint64_t d1[] = {0x3e172b29, 0x039b7bd2, 0x0000001, 0x0c0e0f0505080800}; | |
414 | uint64_t d2[] = {0x3e172b29, 0x039b7bd2, 0x0000002, 0x0e06090d03000b0f}; | |
415 | ||
416 | nonce2key_ex(0, 0 , d1[0], d1[1], d1[2], d1[3], &key); | |
417 | nonce2key_ex(0, 0 , d2[0], d2[1], d2[2], d2[3], &key); | |
418 | return 0; | |
419 | } | |
420 | ||
421 | static void permute(uint8_t *data, uint8_t len, uint8_t *output){ | |
422 | #define KEY_SIZE 8 | |
423 | ||
424 | if ( len > KEY_SIZE ) { | |
425 | for(uint8_t m = 0; m < len; m += KEY_SIZE){ | |
426 | permute(data+m, KEY_SIZE, output+m); | |
427 | } | |
428 | return; | |
429 | } | |
430 | if ( len != KEY_SIZE ) { | |
431 | printf("wrong key size\n"); | |
432 | return; | |
433 | } | |
434 | uint8_t i,j,p, mask; | |
435 | for( i=0; i < KEY_SIZE; ++i){ | |
436 | p = 0; | |
437 | mask = 0x80 >> i; | |
438 | for( j=0; j < KEY_SIZE; ++j){ | |
439 | p >>= 1; | |
440 | if (data[j] & mask) | |
441 | p |= 0x80; | |
442 | } | |
443 | output[i] = p; | |
444 | } | |
445 | } | |
446 | static void permute_rev(uint8_t *data, uint8_t len, uint8_t *output){ | |
447 | permute(data, len, output); | |
448 | permute(output, len, data); | |
449 | permute(data, len, output); | |
450 | } | |
451 | static void simple_crc(uint8_t *data, uint8_t len, uint8_t *output){ | |
452 | uint8_t crc = 0; | |
453 | for( uint8_t i=0; i < len; ++i){ | |
454 | // seventh byte contains the crc. | |
455 | if ( (i & 0x7) == 0x7 ) { | |
456 | output[i] = crc ^ 0xFF; | |
457 | crc = 0; | |
458 | } else { | |
459 | output[i] = data[i]; | |
460 | crc ^= data[i]; | |
461 | } | |
462 | } | |
463 | } | |
464 | // DES doesn't use the MSB. | |
465 | static void shave(uint8_t *data, uint8_t len){ | |
466 | for (uint8_t i=0; i<len; ++i) | |
467 | data[i] &= 0xFE; | |
468 | } | |
469 | static void generate_rev(uint8_t *data, uint8_t len) { | |
470 | uint8_t *key = calloc(len,1); | |
471 | printf("input permuted key | %s \n", sprint_hex(data, len)); | |
472 | permute_rev(data, len, key); | |
473 | printf(" unpermuted key | %s \n", sprint_hex(key, len)); | |
474 | shave(key, len); | |
475 | printf(" key | %s \n", sprint_hex(key, len)); | |
476 | free(key); | |
477 | } | |
478 | static void generate(uint8_t *data, uint8_t len) { | |
479 | uint8_t *key = calloc(len,1); | |
480 | uint8_t *pkey = calloc(len,1); | |
481 | printf(" input key | %s \n", sprint_hex(data, len)); | |
482 | permute(data, len, pkey); | |
483 | printf(" permuted key | %s \n", sprint_hex(pkey, len)); | |
484 | simple_crc(pkey, len, key ); | |
485 | printf(" CRC'ed key | %s \n", sprint_hex(key, len)); | |
486 | free(key); | |
487 | free(pkey); | |
488 | } | |
489 | int CmdAnalyseHid(const char *Cmd){ | |
490 | ||
491 | uint8_t key[8] = {0}; | |
492 | uint8_t key_std_format[8] = {0}; | |
493 | uint8_t key_iclass_format[8] = {0}; | |
494 | uint8_t data[16] = {0}; | |
495 | bool isReverse = FALSE; | |
496 | int len = 0; | |
497 | char cmdp = param_getchar(Cmd, 0); | |
498 | if (strlen(Cmd) == 0|| cmdp == 'h' || cmdp == 'H') return usage_analyse_hid(); | |
499 | ||
500 | if ( cmdp == 'r' || cmdp == 'R' ) | |
501 | isReverse = TRUE; | |
502 | ||
503 | param_gethex_ex(Cmd, 1, data, &len); | |
504 | if ( len%2 ) return usage_analyse_hid(); | |
505 | ||
506 | len >>= 1; | |
507 | ||
508 | memcpy(key, data, 8); | |
509 | ||
510 | if ( isReverse ) { | |
511 | generate_rev(data, len); | |
512 | permutekey_rev(key, key_std_format); | |
513 | printf(" holiman iclass key | %s \n", sprint_hex(key_std_format, 8)); | |
514 | } | |
515 | else { | |
516 | generate(data, len); | |
517 | permutekey(key, key_iclass_format); | |
518 | printf(" holiman std key | %s \n", sprint_hex(key_iclass_format, 8)); | |
519 | } | |
520 | return 0; | |
521 | } | |
522 | ||
523 | static command_t CommandTable[] = { | |
524 | {"help", CmdHelp, 1, "This help"}, | |
525 | {"lcr", CmdAnalyseLCR, 1, "Generate final byte for XOR LRC"}, | |
526 | {"crc", CmdAnalyseCRC, 1, "Stub method for CRC evaluations"}, | |
527 | {"chksum", CmdAnalyseCHKSUM, 1, "Checksum with adding, masking and one's complement"}, | |
528 | {"dates", CmdAnalyseDates, 1, "Look for datestamps in a given array of bytes"}, | |
529 | {"tea", CmdAnalyseTEASelfTest, 1, "Crypto TEA test"}, | |
530 | {"lfsr", CmdAnalyseLfsr, 1, "LFSR tests"}, | |
531 | {"a", CmdAnalyseA, 1, "num bits test"}, | |
532 | {"hid", CmdAnalyseHid, 1, "Permute function from 'heart of darkness' paper"}, | |
533 | {NULL, NULL, 0, NULL} | |
534 | }; | |
535 | ||
536 | int CmdAnalyse(const char *Cmd) { | |
537 | clearCommandBuffer(); | |
538 | CmdsParse(CommandTable, Cmd); | |
539 | return 0; | |
540 | } | |
541 | ||
542 | int CmdHelp(const char *Cmd) { | |
543 | CmdsHelp(CommandTable); | |
544 | return 0; | |
545 | } |