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