]> git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdcrc.c
BUG: forgot to remove
[proxmark3-svn] / client / cmdcrc.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2015 iceman <iceman at iuse.se>
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 // CRC Calculations from the software reveng commands
9 //-----------------------------------------------------------------------------
10
11 #include <stdlib.h>
12 #ifdef _WIN32
13 # include <io.h>
14 # include <fcntl.h>
15 # ifndef STDIN_FILENO
16 # define STDIN_FILENO 0
17 # endif /* STDIN_FILENO */
18 #endif /* _WIN32 */
19
20 #include <stdio.h>
21 #include <string.h>
22 #include "cmdmain.h"
23 #include "cmdcrc.h"
24 #include "reveng/reveng.h"
25 #include "ui.h"
26 #include "util.h"
27
28 #define MAX_ARGS 20
29
30 int uerr(char *msg){
31 PrintAndLog("%s",msg);
32 return 0;
33 }
34
35 int split(char *str, char *arr[MAX_ARGS]){
36 int beginIndex = 0;
37 int endIndex;
38 int maxWords = MAX_ARGS;
39 int wordCnt = 0;
40
41 while(1){
42 while(isspace(str[beginIndex])) {
43 ++beginIndex;
44 }
45 if(str[beginIndex] == '\0') {
46 break;
47 }
48 endIndex = beginIndex;
49 while (str[endIndex] && !isspace(str[endIndex])) {
50 ++endIndex;
51 }
52 int len = endIndex - beginIndex;
53 char *tmp = calloc(len + 1, sizeof(char));
54 memcpy(tmp, &str[beginIndex], len);
55 arr[wordCnt++] = tmp;
56 //PrintAndLog("cnt: %d, %s",wordCnt-1, arr[wordCnt-1]);
57 beginIndex = endIndex;
58 if (wordCnt == maxWords)
59 break;
60 }
61 return wordCnt;
62 }
63
64 int CmdCrc(const char *Cmd)
65 {
66 char name[] = {"reveng "};
67 char Cmd2[50 + 7];
68 memcpy(Cmd2, name, 7);
69 memcpy(Cmd2 + 7, Cmd, 50);
70 char *argv[MAX_ARGS];
71 int argc = split(Cmd2, argv);
72
73 if (argc == 3 && memcmp(argv[1],"-g",2)==0) {
74 CmdrevengSearch(argv[2]);
75 } else {
76 reveng_main(argc, argv);
77 }
78 //PrintAndLog("DEBUG argc: %d, %s %s Cmd: %s",argc, argv[0], Cmd2, Cmd);
79 for(int i = 0; i < argc; ++i) {
80 free(argv[i]);
81 }
82
83 return 0;
84 }
85
86 //returns array of model names and the count of models returning
87 // as well as a width array for the width of each model
88 int GetModels(char *Models[], int *count, uint8_t *width){
89 /* default values */
90 static model_t model = {
91 PZERO, /* no CRC polynomial, user must specify */
92 PZERO, /* Init = 0 */
93 P_BE, /* RefIn = false, RefOut = false, plus P_RTJUST setting in reveng.h */
94 PZERO, /* XorOut = 0 */
95 PZERO, /* check value unused */
96 NULL /* no model name */
97 };
98
99 int ibperhx = 8;//, obperhx = 8;
100 int rflags = 0, uflags = 0; /* search and UI flags */
101 poly_t apoly, crc, qpoly = PZERO, *apolys = NULL, *pptr = NULL, *qptr = NULL;
102 model_t pset = model, *candmods, *mptr;
103
104 /* stdin must be binary */
105 #ifdef _WIN32
106 _setmode(STDIN_FILENO, _O_BINARY);
107 #endif /* _WIN32 */
108
109 SETBMP();
110
111 int args = 0, psets, pass;
112 int Cnt = 0;
113 if (width[0] == 0) { //reveng -D
114 *count = mcount();
115 if(!*count)
116 return uerr("no preset models available");
117
118 for(int mode = 0; mode < *count; ++mode) {
119 mbynum(&model, mode);
120 mcanon(&model);
121 size_t size = (model.name && *model.name) ? strlen(model.name) : 6;
122 char *tmp = calloc(size+1, sizeof(char));
123 if (tmp==NULL)
124 return uerr("out of memory?");
125
126 memcpy(tmp, model.name, size);
127 Models[mode] = tmp;
128 width[mode] = plen(model.spoly);
129 }
130 mfree(&model);
131 } else { //reveng -s
132
133 if(~model.flags & P_MULXN)
134 return uerr("cannot search for non-Williams compliant models");
135
136 praloc(&model.spoly, (unsigned long)width[0]);
137 praloc(&model.init, (unsigned long)width[0]);
138 praloc(&model.xorout, (unsigned long)width[0]);
139
140 if(!plen(model.spoly))
141 palloc(&model.spoly, (unsigned long)width[0]);
142 else
143 width[0] = (uint8_t)plen(model.spoly);
144
145 /* special case if qpoly is zero, search to end of range */
146 if(!ptst(qpoly))
147 rflags &= ~R_HAVEQ;
148
149 /* if endianness not specified, try
150 * little-endian then big-endian.
151 * NB: crossed-endian algorithms will not be
152 * searched.
153 */
154 /* scan against preset models */
155 if (~uflags & C_FORCE) {
156 pass = 0;
157 Cnt = 0;
158 do {
159 psets = mcount();
160
161 while(psets) {
162 mbynum(&pset, --psets);
163
164 /* skip if different width, or refin or refout don't match */
165 if( plen(pset.spoly) != width[0] || (model.flags ^ pset.flags) & (P_REFIN | P_REFOUT))
166 continue;
167 /* skip if the preset doesn't match specified parameters */
168 if (rflags & R_HAVEP && pcmp(&model.spoly, &pset.spoly))
169 continue;
170 if (rflags & R_HAVEI && psncmp(&model.init, &pset.init))
171 continue;
172 if (rflags & R_HAVEX && psncmp(&model.xorout, &pset.xorout))
173 continue;
174
175 //for additional args (not used yet, maybe future?)
176 apoly = pclone(pset.xorout);
177
178 if (pset.flags & P_REFOUT)
179 prev(&apoly);
180
181
182 for (qptr = apolys; qptr < pptr; ++qptr) {
183 crc = pcrc(*qptr, pset.spoly, pset.init, apoly, 0);
184 if (ptst(crc)) {
185 pfree(&crc);
186 break;
187 }
188 pfree(&crc);
189 }
190 pfree(&apoly);
191
192 if (qptr == pptr) {
193
194 /* the selected model solved all arguments */
195 mcanon(&pset);
196
197 size_t size = (pset.name && *pset.name) ? strlen(pset.name) : 6;
198 //PrintAndLog("Size: %d, %s, count: %d",size,pset.name, Cnt);
199 char *tmp = calloc(size+1, sizeof(char));
200 if (tmp == NULL){
201 PrintAndLog("out of memory?");
202 return 0;
203 }
204 width[Cnt] = width[0];
205 memcpy(tmp, pset.name, size);
206 Models[Cnt++] = tmp;
207 *count = Cnt;
208 uflags |= C_RESULT;
209 }
210 }
211 mfree(&pset);
212
213 /* toggle refIn/refOut and reflect arguments */
214 if (~rflags & R_HAVERI) {
215 model.flags ^= P_REFIN | P_REFOUT;
216 for (qptr = apolys; qptr < pptr; ++qptr) {
217 prevch(qptr, ibperhx);
218 }
219 }
220 } while (~rflags & R_HAVERI && ++pass < 2);
221 }
222 //got everything now free the memory...
223
224 if (uflags & C_RESULT) {
225 for (qptr = apolys; qptr < pptr; ++qptr) {
226 pfree(qptr);
227 }
228 }
229 if (!(model.flags & P_REFIN) != !(model.flags & P_REFOUT))
230 return uerr("cannot search for crossed-endian models");
231
232 pass = 0;
233 do {
234 mptr = candmods = reveng(&model, qpoly, rflags, args, apolys);
235 if (mptr && plen(mptr->spoly)) {
236 uflags |= C_RESULT;
237 }
238 while (mptr && plen(mptr->spoly)) {
239 mfree(mptr++);
240 }
241 free(candmods);
242 if (~rflags & R_HAVERI) {
243 model.flags ^= P_REFIN | P_REFOUT;
244 for (qptr = apolys; qptr < pptr; ++qptr) {
245 prevch(qptr, ibperhx);
246 }
247 }
248 } while (~rflags & R_HAVERI && ++pass < 2);
249
250 for (qptr = apolys; qptr < pptr; ++qptr) {
251 pfree(qptr);
252 }
253 free(apolys);
254 if (~uflags & C_RESULT)
255 return uerr("no models found");
256
257 mfree(&model);
258 }
259 return 1;
260 }
261
262 //-c || -v
263 //inModel = valid model name string - CRC-8
264 //inHexStr = input hex string to calculate crc on
265 //reverse = reverse calc option if true
266 //endian = {0 = calc default endian input and output, b = big endian input and output, B = big endian output, r = right justified
267 // l = little endian input and output, L = little endian output only, t = left justified}
268 //result = calculated crc hex string
269 int RunModel(char *inModel, char *inHexStr, bool reverse, char endian, char *result){
270 /* default values */
271 static model_t model = {
272 PZERO, // no CRC polynomial, user must specify
273 PZERO, // Init = 0
274 P_BE, // RefIn = false, RefOut = false, plus P_RTJUST setting in reveng.h
275 PZERO, // XorOut = 0
276 PZERO, // check value unused
277 NULL // no model name
278 };
279 int ibperhx = 8, obperhx = 8;
280 int rflags = 0; // search flags
281 int c;
282 poly_t apoly, crc;
283
284 char *string;
285
286 // stdin must be binary
287 #ifdef _WIN32
288 _setmode(STDIN_FILENO, _O_BINARY);
289 #endif /* _WIN32 */
290
291 SETBMP();
292 //set model
293 if (!(c = mbynam(&model, inModel))) {
294 PrintAndLog("error: preset model '%s' not found. Use reveng -D to list presets.", inModel);
295 return 0;
296 }
297 if (c < 0)
298 return uerr("no preset models available");
299
300 rflags |= R_HAVEP | R_HAVEI | R_HAVERI | R_HAVERO | R_HAVEX;
301
302 //set flags
303 switch (endian) {
304 case 'b': /* b big-endian (RefIn = false, RefOut = false ) */
305 model.flags &= ~P_REFIN;
306 rflags |= R_HAVERI;
307 /* fall through: */
308 case 'B': /* B big-endian output (RefOut = false) */
309 model.flags &= ~P_REFOUT;
310 rflags |= R_HAVERO;
311 mnovel(&model);
312 /* fall through: */
313 case 'r': /* r right-justified */
314 model.flags |= P_RTJUST;
315 break;
316 case 'l': /* l little-endian input and output */
317 model.flags |= P_REFIN;
318 rflags |= R_HAVERI;
319 /* fall through: */
320 case 'L': /* L little-endian output */
321 model.flags |= P_REFOUT;
322 rflags |= R_HAVERO;
323 mnovel(&model);
324 /* fall through: */
325 case 't': /* t left-justified */
326 model.flags &= ~P_RTJUST;
327 break;
328 }
329 /* canonicalise the model, so the one we dump is the one we
330 * calculate with (not with -s, spoly may be blank which will
331 * normalise to zero and clear init and xorout.)
332 */
333 mcanon(&model);
334
335
336 if (reverse) {
337 // v calculate reversed CRC
338 /* Distinct from the -V switch as this causes
339 * the arguments and output to be reversed as well.
340 */
341 // reciprocate Poly
342 prcp(&model.spoly);
343
344 /* mrev() does:
345 * if(refout) prev(init); else prev(xorout);
346 * but here the entire argument polynomial is
347 * reflected, not just the characters, so RefIn
348 * and RefOut are not inverted as with -V.
349 * Consequently Init is the mirror image of the
350 * one resulting from -V, and so we have:
351 */
352 if (~model.flags & P_REFOUT) {
353 prev(&model.init);
354 prev(&model.xorout);
355 }
356
357 // swap init and xorout
358 apoly = model.init;
359 model.init = model.xorout;
360 model.xorout = apoly;
361 }
362 // c calculate CRC
363
364 /* in the Williams model, xorout is applied after the refout stage.
365 * as refout is part of ptostr(), we reverse xorout here.
366 */
367 if (model.flags & P_REFOUT)
368 prev(&model.xorout);
369
370 apoly = strtop(inHexStr, model.flags, ibperhx);
371
372 if (reverse)
373 prev(&apoly);
374
375 crc = pcrc(apoly, model.spoly, model.init, model.xorout, model.flags);
376
377 if (reverse)
378 prev(&crc);
379
380 string = ptostr(crc, model.flags, obperhx);
381 for (int i = 0; i < 50; i++){
382 result[i] = string[i];
383 if (result[i]==0) break;
384 }
385 free(string);
386 pfree(&crc);
387 pfree(&apoly);
388 return 1;
389 }
390 //returns a calloced string (needs to be freed)
391 char *SwapEndianStr(const char *inStr, const size_t len, const uint8_t blockSize){
392 char *tmp = calloc(len+1, sizeof(char));
393 for (uint8_t block=0; block < (uint8_t)(len/blockSize); block++){
394 for (size_t i = 0; i < blockSize; i+=2){
395 tmp[i+(blockSize*block)] = inStr[(blockSize-1-i-1)+(blockSize*block)];
396 tmp[i+(blockSize*block)+1] = inStr[(blockSize-1-i)+(blockSize*block)];
397 }
398 }
399 return tmp;
400 }
401
402 // takes hex string in and searches for a matching result (hex string must include checksum)
403 int CmdrevengSearch(const char *Cmd){
404 char inHexStr[50] = {0x00};
405 int dataLen = param_getstr(Cmd, 0, inHexStr);
406 if (dataLen < 4) return 0;
407
408 char *Models[80];
409 int count = 0;
410 uint8_t width[80];
411 width[0] = 0;
412 uint8_t crcChars = 0;
413 char result[30];
414 char revResult[30];
415 int ans = GetModels(Models, &count, width);
416 bool found = false;
417 if (!ans) return 0;
418
419 // try each model and get result
420 for (int i = 0; i < count; i++){
421 /*if (found) {
422 free(Models[i]);
423 continue;
424 }*/
425 // round up to # of characters in this model's crc
426 crcChars = ((width[i]+7)/8)*2;
427 // can't test a model that has more crc digits than our data
428 if (crcChars >= dataLen)
429 continue;
430 memset(result, 0, 30);
431 char *inCRC = calloc(crcChars+1, sizeof(char));
432 memcpy(inCRC, inHexStr+(dataLen-crcChars), crcChars);
433
434 char *outHex = calloc(dataLen-crcChars+1, sizeof(char));
435 memcpy(outHex, inHexStr, dataLen-crcChars);
436
437 //PrintAndLog("DEBUG: dataLen: %d, crcChars: %d, Model: %s, CRC: %s, width: %d, outHex: %s",dataLen, crcChars, Models[i], inCRC, width[i], outHex);
438 ans = RunModel(Models[i], outHex, false, 0, result);
439 if (ans) {
440 //test for match
441 if (memcmp(result, inCRC, crcChars)==0){
442 PrintAndLog("\nFound a possible match!\nModel: %s\nValue: %s\n",Models[i], result);
443 //optional - stop searching if found...
444 found = true;
445 } else {
446 if (crcChars > 2){
447 char *swapEndian = SwapEndianStr(result, crcChars, crcChars);
448 if (memcmp(swapEndian, inCRC, crcChars)==0){
449 PrintAndLog("\nFound a possible match!\nModel: %s\nValue EndianSwapped: %s\n",Models[i], swapEndian);
450 //optional - stop searching if found...
451 found = true;
452 }
453 free(swapEndian);
454 }
455 }
456 }
457
458 //if (!found){
459 ans = RunModel(Models[i], outHex, true, 0, revResult);
460 if (ans) {
461 //test for match
462 if (memcmp(revResult, inCRC, crcChars)==0){
463 PrintAndLog("\nFound a possible match!\nModel Reversed: %s\nValue: %s\n",Models[i], revResult);
464 //optional - stop searching if found...
465 found = true;
466 } else {
467 if (crcChars > 2){
468 char *swapEndian = SwapEndianStr(revResult, crcChars, crcChars);
469 if (memcmp(swapEndian, inCRC, crcChars)==0){
470 PrintAndLog("\nFound a possible match!\nModel Reversed: %s\nValue EndianSwapped: %s\n",Models[i], swapEndian);
471 //optional - stop searching if found...
472 found = true;
473 }
474 free(swapEndian);
475 }
476 }
477 }
478 //}
479 free(inCRC);
480 free(outHex);
481 free(Models[i]);
482 }
483 if (!found) PrintAndLog("\nNo matches found\n");
484 return 1;
485 }
Impressum, Datenschutz