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