]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/cmdanalyse.c
ADD: Added some new analyse functions like CHKSUM ADD/SUBTRACKT with one's complement
[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"
11
12static int CmdHelp(const char *Cmd);
13
14int 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}
53b3c3e8 28
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}
44
45int 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
812513bf 58static 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}
53b3c3e8 64
65static 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}
76static uint8_t calcSumCrumbAddOnes( uint8_t* bytes, uint8_t len, uint32_t mask) {
77 return ~calcSumCrumbAdd(bytes, len, mask);
78}
79static 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}
88static uint8_t calcSumNibbleAddOnes( uint8_t* bytes, uint8_t len, uint32_t mask){
89 return ~calcSumNibbleAdd(bytes, len, mask);
90}
91
92static 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
100static uint8_t calcSumByteAddOnes( uint8_t* bytes, uint8_t len, uint32_t mask) {
101 return ~calcSumByteAdd(bytes, len, mask);
102}
103
104static 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}
111static uint8_t calcSumByteSubOnes( uint8_t* bytes, uint8_t len, uint32_t mask){
112 return ~calcSumByteSub(bytes, len, mask);
113}
114static 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}
123static uint8_t calcSumNibbleSubOnes( uint8_t* bytes, uint8_t len, uint32_t mask) {
124 return ~calcSumNibbleSub(bytes, len, mask);
125}
126
812513bf 127int 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}
53b3c3e8 140int 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 PrintAndLog(" JA: CRC8: %X (0x6C expected)", CRC8ja(data, len) );
160
161 PrintAndLog("\nTests of reflection. Two current methods in source code");
162 PrintAndLog(" reflect(0x3e23L,3) is %04X == 0x3e26", reflect(0x3e23L,3) );
163 PrintAndLog(" SwapBits(0x3e23L,3) is %04X == 0x3e26", SwapBits(0x3e23L,3) );
164 PrintAndLog(" 0xB400 == %04X", reflect( (1 << 16 | 0xb400),16) );
165
166 //
167 // Test of CRC16, '123456789' string.
168 //
169 PrintAndLog("\nTests with '123456789' string");
170 uint8_t dataStr[] = { 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39 };
171 uint8_t legic8 = CRC8Legic(dataStr, sizeof(dataStr));
172
173 PrintAndLog("JA: CRC8 : %X (0x28 expected)", CRC8ja(dataStr, sizeof(dataStr)) );
174 PrintAndLog("LEGIC: CRC16: %X", CRC16Legic(dataStr, sizeof(dataStr), legic8));
175
176 //these below has been tested OK.
177 PrintAndLog("Confirmed CRC Implementations");
178 PrintAndLog("LEGIC: CRC8 : %X (0xC6 expected)", legic8);
179 PrintAndLog("MAXIM: CRC8 : %X (0xA1 expected)", CRC8Maxim(dataStr, sizeof(dataStr)));
180 PrintAndLog("DNP : CRC16: %X (0x82EA expected)", CRC16_DNP(dataStr, sizeof(dataStr)));
181 PrintAndLog("CCITT: CRC16: %X (0xE5CC expected)", CRC16_CCITT(dataStr, sizeof(dataStr)));
182
183 free(data);
184 return 0;
185}
186int CmdAnalyseCHKSUM(const char *Cmd){
187
188 uint8_t data[50];
189 uint8_t cmdp = 0;
190 uint32_t mask = 0xFF;
191 bool errors = false;
192 int len = 0;
193
194 while(param_getchar(Cmd, cmdp) != 0x00) {
195 switch(param_getchar(Cmd, cmdp)) {
196 case 'b':
197 case 'B':
198 param_gethex_ex(Cmd, cmdp+1, data, &len);
199 if ( len%2 ) errors = true;
200 len >>= 1;
201 cmdp += 2;
202 break;
203 case 'm':
204 case 'M':
205 mask = param_get32ex(Cmd, cmdp+1, 0, 16);
206 cmdp += 2;
207 break;
208 case 'h':
209 case 'H':
210 return usage_analyse_checksum();
211 default:
212 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
213 errors = true;
214 break;
215 }
216 if(errors) break;
217 }
218 //Validations
219 if(errors) return usage_analyse_checksum();
220
221 PrintAndLog("\nByte Add | 0x%X", calcSumByteAdd(data, len, mask));
222 PrintAndLog("Nibble Add | 0x%X", calcSumNibbleAdd(data, len, mask));
223 PrintAndLog("Crumb Add | 0x%X", calcSumCrumbAdd(data, len, mask));
224
225 PrintAndLog("\nByte Subtract | 0x%X", calcSumByteSub(data, len, mask));
226 PrintAndLog("Nibble Subtract | 0x%X", calcSumNibbleSub(data, len, mask));
227
228 PrintAndLog("\nCHECKSUM - One's complement");
229 PrintAndLog("Byte Add | 0x%X", calcSumByteAddOnes(data, len, mask));
230 PrintAndLog("Nibble Add | 0x%X", calcSumNibbleAddOnes(data, len, mask));
231 PrintAndLog("Crumb Add | 0x%X", calcSumCrumbAddOnes(data, len, mask));
232
233 PrintAndLog("Byte Subtract | 0x%X", calcSumByteSubOnes(data, len, mask));
234 PrintAndLog("Nibble Subtract | 0x%X", calcSumNibbleSubOnes(data, len, mask));
235
236 return 0;
237}
812513bf 238
5558d935 239int CmdAnalyseDates(const char *Cmd){
240 // look for datestamps in a given array of bytes
53b3c3e8 241 PrintAndLog("To be implemented. Feel free to contribute!");
5558d935 242 return 0;
243}
244
812513bf 245static command_t CommandTable[] = {
5558d935 246 {"help", CmdHelp, 1, "This help"},
53b3c3e8 247 {"lcr", CmdAnalyseLCR, 1, "Generate final byte for XOR LRC"},
248 {"crc", CmdAnalyseCRC, 1, "Stub method for CRC evaluations"},
249 {"chksum", CmdAnalyseCHKSUM, 1, "Checksum with adding, masking and one's complement"},
250 {"dates", CmdAnalyseDates, 1, "Look for datestamps in a given array of bytes"},
812513bf 251 {NULL, NULL, 0, NULL}
252};
253
254int CmdAnalyse(const char *Cmd) {
255 clearCommandBuffer();
256 CmdsParse(CommandTable, Cmd);
257 return 0;
258}
259
260int CmdHelp(const char *Cmd) {
261 CmdsHelp(CommandTable);
262 return 0;
263}
Impressum, Datenschutz