]> git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdanalyse.c
ADD: LF JABLOTRON functionality. with clone/sim and detection in LF SEARCH.
[proxmark3-svn] / client / cmdanalyse.c
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
12 static int CmdHelp(const char *Cmd);
13
14 int usage_analyse_lcr(void) {
15 PrintAndLog("Specifying the bytes of a UID with a known LRC will find the last byte value");
16 PrintAndLog("needed to generate that LRC with a rolling XOR. All bytes should be specified in HEX.");
17 PrintAndLog("");
18 PrintAndLog("Usage: analyse lcr [h] <bytes>");
19 PrintAndLog("Options:");
20 PrintAndLog(" h This help");
21 PrintAndLog(" <bytes> bytes to calc missing XOR in a LCR");
22 PrintAndLog("");
23 PrintAndLog("Samples:");
24 PrintAndLog(" analyse lcr 04008064BA");
25 PrintAndLog("expected output: Target (BA) requires final LRC XOR byte value: 5A");
26 return 0;
27 }
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
45 int usage_analyse_crc(void){
46 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");
47 PrintAndLog("");
48 PrintAndLog("Usage: analyse crc [h] <bytes>");
49 PrintAndLog("Options:");
50 PrintAndLog(" h This help");
51 PrintAndLog(" <bytes> bytes to calc crc");
52 PrintAndLog("");
53 PrintAndLog("Samples:");
54 PrintAndLog(" analyse crc 137AF00A0A0D");
55 return 0;
56 }
57
58 static uint8_t calculateLRC( uint8_t* bytes, uint8_t len) {
59 uint8_t LRC = 0;
60 for (uint8_t i = 0; i < len; i++)
61 LRC ^= bytes[i];
62 return LRC;
63 }
64
65 static uint8_t calcSumCrumbAdd( uint8_t* bytes, uint8_t len, uint32_t mask) {
66 uint8_t sum = 0;
67 for (uint8_t i = 0; i < len; i++) {
68 sum += CRUMB(bytes[i], 0);
69 sum += CRUMB(bytes[i], 2);
70 sum += CRUMB(bytes[i], 4);
71 sum += CRUMB(bytes[i], 6);
72 }
73 sum &= mask;
74 return sum;
75 }
76 static uint8_t calcSumCrumbAddOnes( uint8_t* bytes, uint8_t len, uint32_t mask) {
77 return ~calcSumCrumbAdd(bytes, len, mask);
78 }
79 static uint8_t calcSumNibbleAdd( uint8_t* bytes, uint8_t len, uint32_t mask) {
80 uint8_t sum = 0;
81 for (uint8_t i = 0; i < len; i++) {
82 sum += NIBBLE_LOW(bytes[i]);
83 sum += NIBBLE_HIGH(bytes[i]);
84 }
85 sum &= mask;
86 return sum;
87 }
88 static uint8_t calcSumNibbleAddOnes( uint8_t* bytes, uint8_t len, uint32_t mask){
89 return ~calcSumNibbleAdd(bytes, len, mask);
90 }
91
92 static uint8_t calcSumByteAdd( 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 += bytes[i];
96 sum &= mask;
97 return sum;
98 }
99 // Ones complement
100 static uint8_t calcSumByteAddOnes( uint8_t* bytes, uint8_t len, uint32_t mask) {
101 return ~calcSumByteAdd(bytes, len, mask);
102 }
103
104 static uint8_t calcSumByteSub( 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 -= bytes[i];
108 sum &= mask;
109 return sum;
110 }
111 static uint8_t calcSumByteSubOnes( uint8_t* bytes, uint8_t len, uint32_t mask){
112 return ~calcSumByteSub(bytes, len, mask);
113 }
114 static uint8_t calcSumNibbleSub( uint8_t* bytes, uint8_t len, uint32_t mask) {
115 uint8_t sum = 0;
116 for (uint8_t i = 0; i < len; i++) {
117 sum -= NIBBLE_LOW(bytes[i]);
118 sum -= NIBBLE_HIGH(bytes[i]);
119 }
120 sum &= mask;
121 return sum;
122 }
123 static uint8_t calcSumNibbleSubOnes( uint8_t* bytes, uint8_t len, uint32_t mask) {
124 return ~calcSumNibbleSub(bytes, len, mask);
125 }
126
127 int CmdAnalyseLCR(const char *Cmd) {
128 uint8_t data[50];
129 char cmdp = param_getchar(Cmd, 0);
130 if (strlen(Cmd) == 0|| cmdp == 'h' || cmdp == 'H') return usage_analyse_lcr();
131
132 int len = 0;
133 param_gethex_ex(Cmd, 0, data, &len);
134 if ( len%2 ) return usage_analyse_lcr();
135 len >>= 1;
136 uint8_t finalXor = calculateLRC(data, len);
137 PrintAndLog("Target [%02X] requires final LRC XOR byte value: 0x%02X",data[len-1] ,finalXor);
138 return 0;
139 }
140 int CmdAnalyseCRC(const char *Cmd) {
141
142 char cmdp = param_getchar(Cmd, 0);
143 if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_analyse_crc();
144
145 int len = strlen(Cmd);
146 if ( len & 1 ) return usage_analyse_crc();
147
148 // add 1 for null terminator.
149 uint8_t *data = malloc(len+1);
150 if ( data == NULL ) return 1;
151
152 if ( param_gethex(Cmd, 0, data, len)) {
153 free(data);
154 return usage_analyse_crc();
155 }
156 len >>= 1;
157
158 //PrintAndLog("\nTests with '%s' hex bytes", sprint_hex(data, len));
159
160 PrintAndLog("\nTests of reflection. Two current methods in source code");
161 PrintAndLog(" reflect(0x3e23L,3) is %04X == 0x3e26", reflect(0x3e23L,3) );
162 PrintAndLog(" SwapBits(0x3e23L,3) is %04X == 0x3e26", SwapBits(0x3e23L,3) );
163 PrintAndLog(" 0xB400 == %04X", reflect( (1 << 16 | 0xb400),16) );
164
165 //
166 // Test of CRC16, '123456789' string.
167 //
168 PrintAndLog("\nTests with '123456789' string");
169 uint8_t dataStr[] = { 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39 };
170 uint8_t legic8 = CRC8Legic(dataStr, sizeof(dataStr));
171
172 PrintAndLog("LEGIC: CRC16: %X", CRC16Legic(dataStr, sizeof(dataStr), legic8));
173
174 //these below has been tested OK.
175 PrintAndLog("Confirmed CRC Implementations");
176 PrintAndLog("LEGIC: CRC8 : %X (0xC6 expected)", legic8);
177 PrintAndLog("MAXIM: CRC8 : %X (0xA1 expected)", CRC8Maxim(dataStr, sizeof(dataStr)));
178 PrintAndLog("DNP : CRC16: %X (0x82EA expected)", CRC16_DNP(dataStr, sizeof(dataStr)));
179 PrintAndLog("CCITT: CRC16: %X (0xE5CC expected)", CRC16_CCITT(dataStr, sizeof(dataStr)));
180
181 free(data);
182 return 0;
183 }
184 int CmdAnalyseCHKSUM(const char *Cmd){
185
186 uint8_t data[50];
187 uint8_t cmdp = 0;
188 uint32_t mask = 0xFF;
189 bool errors = false;
190 int len = 0;
191
192 while(param_getchar(Cmd, cmdp) != 0x00) {
193 switch(param_getchar(Cmd, cmdp)) {
194 case 'b':
195 case 'B':
196 param_gethex_ex(Cmd, cmdp+1, data, &len);
197 if ( len%2 ) errors = true;
198 len >>= 1;
199 cmdp += 2;
200 break;
201 case 'm':
202 case 'M':
203 mask = param_get32ex(Cmd, cmdp+1, 0, 16);
204 cmdp += 2;
205 break;
206 case 'h':
207 case 'H':
208 return usage_analyse_checksum();
209 default:
210 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
211 errors = true;
212 break;
213 }
214 if(errors) break;
215 }
216 //Validations
217 if(errors) return usage_analyse_checksum();
218
219 PrintAndLog("\nByte Add | 0x%X", calcSumByteAdd(data, len, mask));
220 PrintAndLog("Nibble Add | 0x%X", calcSumNibbleAdd(data, len, mask));
221 PrintAndLog("Crumb Add | 0x%X", calcSumCrumbAdd(data, len, mask));
222
223 PrintAndLog("\nByte Subtract | 0x%X", calcSumByteSub(data, len, mask));
224 PrintAndLog("Nibble Subtract | 0x%X", calcSumNibbleSub(data, len, mask));
225
226 PrintAndLog("\nCHECKSUM - One's complement");
227 PrintAndLog("Byte Add | 0x%X", calcSumByteAddOnes(data, len, mask));
228 PrintAndLog("Nibble Add | 0x%X", calcSumNibbleAddOnes(data, len, mask));
229 PrintAndLog("Crumb Add | 0x%X", calcSumCrumbAddOnes(data, len, mask));
230
231 PrintAndLog("Byte Subtract | 0x%X", calcSumByteSubOnes(data, len, mask));
232 PrintAndLog("Nibble Subtract | 0x%X", calcSumNibbleSubOnes(data, len, mask));
233
234 return 0;
235 }
236
237 int CmdAnalyseDates(const char *Cmd){
238 // look for datestamps in a given array of bytes
239 PrintAndLog("To be implemented. Feel free to contribute!");
240 return 0;
241 }
242
243 static command_t CommandTable[] = {
244 {"help", CmdHelp, 1, "This help"},
245 {"lcr", CmdAnalyseLCR, 1, "Generate final byte for XOR LRC"},
246 {"crc", CmdAnalyseCRC, 1, "Stub method for CRC evaluations"},
247 {"chksum", CmdAnalyseCHKSUM, 1, "Checksum with adding, masking and one's complement"},
248 {"dates", CmdAnalyseDates, 1, "Look for datestamps in a given array of bytes"},
249 {NULL, NULL, 0, NULL}
250 };
251
252 int CmdAnalyse(const char *Cmd) {
253 clearCommandBuffer();
254 CmdsParse(CommandTable, Cmd);
255 return 0;
256 }
257
258 int CmdHelp(const char *Cmd) {
259 CmdsHelp(CommandTable);
260 return 0;
261 }
Impressum, Datenschutz