]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdcrc.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2015 iceman <iceman at iuse.se>
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
7 //-----------------------------------------------------------------------------
8 // CRC Calculations from the software reveng commands
9 //-----------------------------------------------------------------------------
16 # define STDIN_FILENO 0
17 # endif /* STDIN_FILENO */
26 #include "reveng/reveng.h"
33 PrintAndLog("%s",msg
);
37 int split(char *str
, char *arr
[MAX_ARGS
]){
40 int maxWords
= MAX_ARGS
;
44 while(isspace(str
[beginIndex
])){
47 if(str
[beginIndex
] == '\0') {
50 endIndex
= beginIndex
;
51 while (str
[endIndex
] && !isspace(str
[endIndex
])){
54 int len
= endIndex
- beginIndex
;
55 char *tmp
= calloc(len
+ 1, sizeof(char));
56 memcpy(tmp
, &str
[beginIndex
], len
);
58 //PrintAndLog("DEBUG cnt: %d, %s",wordCnt-1, arr[wordCnt-1]);
59 beginIndex
= endIndex
;
60 if (wordCnt
== maxWords
)
66 int CmdCrc(const char *Cmd
)
68 char name
[] = {"reveng "};
70 memcpy(Cmd2
, name
, 7);
71 memcpy(Cmd2
+ 7, Cmd
, 50);
73 int argc
= split(Cmd2
, argv
);
75 if (argc
== 3 && memcmp(argv
[1],"-g",2)==0) {
76 CmdrevengSearch(argv
[2]);
78 reveng_main(argc
, argv
);
80 //PrintAndLog("DEBUG argc: %d, %s %s Cmd: %s",argc, argv[0], Cmd2, Cmd);
81 for(int i
= 0; i
< argc
; ++i
){
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
){
92 static model_t model
= {
93 PZERO
, /* no CRC polynomial, user must specify */
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 */
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
;
106 /* stdin must be binary */
108 _setmode(STDIN_FILENO
, _O_BINARY
);
113 int args
= 0, psets
, pass
;
115 if (width
[0] == 0) { //reveng -D
118 return uerr("no preset models available");
120 for(int mode
= 0; mode
< *count
; ++mode
) {
121 mbynum(&model
, mode
);
123 size_t size
= (model
.name
&& *model
.name
) ? strlen(model
.name
) : 6;
124 char *tmp
= calloc(size
+1, sizeof(char));
126 return uerr("out of memory?");
128 memcpy(tmp
, model
.name
, size
);
130 width
[mode
] = plen(model
.spoly
);
135 if(~model
.flags
& P_MULXN
)
136 return uerr("cannot search for non-Williams compliant models");
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]);
144 width
[0] = (uint8_t)plen(model
.spoly
);
146 /* special case if qpoly is zero, search to end of range */
151 /* not going to be sending additional args at this time (maybe future?)
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");
158 for(pptr = apolys; optind < argc; ++optind) {
159 if(uflags & C_INFILE)
160 *pptr++ = rdpoly(argv[optind], model.flags, ibperhx);
162 *pptr++ = strtop(argv[optind], model.flags, ibperhx);
164 // exit value of pptr is used hereafter!
168 /* if endianness not specified, try
169 * little-endian then big-endian.
170 * NB: crossed-endian algorithms will not be
173 /* scan against preset models */
174 if(~uflags
& C_FORCE
) {
179 //PrintAndLog("psets: %d",psets);
181 mbynum(&pset
, --psets
);
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
))
186 /* skip if the preset doesn't match specified parameters */
187 if(rflags
& R_HAVEP
&& pcmp(&model
.spoly
, &pset
.spoly
))
189 if(rflags
& R_HAVEI
&& psncmp(&model
.init
, &pset
.init
))
191 if(rflags
& R_HAVEX
&& psncmp(&model
.xorout
, &pset
.xorout
))
194 //for additional args (not used yet, maybe future?)
195 apoly
= pclone(pset
.xorout
);
196 if(pset
.flags
& P_REFOUT
)
199 for(qptr
= apolys
; qptr
< pptr
; ++qptr
) {
200 crc
= pcrc(*qptr
, pset
.spoly
, pset
.init
, apoly
, 0);
210 /* the selected model solved all arguments */
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));
218 PrintAndLog("out of memory?");
221 width
[Cnt
] = width
[0];
222 memcpy(tmp
, pset
.name
, size
);
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
);
236 } while(~rflags
& R_HAVERI
&& ++pass
< 2);
238 //got everything now free the memory...
240 if(uflags
& C_RESULT
) {
241 for(qptr
= apolys
; qptr
< pptr
; ++qptr
)
244 if(!(model
.flags
& P_REFIN
) != !(model
.flags
& P_REFOUT
))
245 return uerr("cannot search for crossed-endian models");
249 mptr
= candmods
= reveng(&model
, qpoly
, rflags
, args
, apolys
);
250 if(mptr
&& plen(mptr
->spoly
))
252 while(mptr
&& plen(mptr
->spoly
)) {
256 if(~rflags
& R_HAVERI
) {
257 model
.flags
^= P_REFIN
| P_REFOUT
;
258 for(qptr
= apolys
; qptr
< pptr
; ++qptr
)
259 prevch(qptr
, ibperhx
);
261 } while(~rflags
& R_HAVERI
&& ++pass
< 2);
262 for(qptr
= apolys
; qptr
< pptr
; ++qptr
)
265 if(~uflags
& C_RESULT
)
266 return uerr("no models found");
272 //test call to GetModels
273 int CmdrevengTest(const char *Cmd
){
276 uint8_t widtharr
[80] = {0};
278 width
= param_get8(Cmd
, 0);
279 //PrintAndLog("width: %d",width);
281 return uerr("Width cannot exceed 89");
284 int ans
= GetModels(Models
, &count
, widtharr
);
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
]);
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
){
304 static model_t model
= {
305 PZERO
, // no CRC polynomial, user must specify
307 P_BE
, // RefIn = false, RefOut = false, plus P_RTJUST setting in reveng.h
309 PZERO
, // check value unused
310 NULL
// no model name
312 int ibperhx
= 8, obperhx
= 8;
313 int rflags
= 0; // search flags
315 //unsigned long width;
320 // stdin must be binary
322 _setmode(STDIN_FILENO
, _O_BINARY
);
327 if(!(c
= mbynam(&model
, inModel
))) {
328 fprintf(stderr
,"error: preset model '%s' not found. Use reveng -D to list presets.\n", inModel
);
332 return uerr("no preset models available");
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
;
340 case 'b': /* b big-endian (RefIn = false, RefOut = false ) */
341 model
.flags
&= ~P_REFIN
;
344 case 'B': /* B big-endian output (RefOut = false) */
345 model
.flags
&= ~P_REFOUT
;
349 case 'r': /* r right-justified */
350 model
.flags
|= P_RTJUST
;
352 case 'l': /* l little-endian input and output */
353 model
.flags
|= P_REFIN
;
356 case 'L': /* L little-endian output */
357 model
.flags
|= P_REFOUT
;
361 case 't': /* t left-justified */
362 model
.flags
&= ~P_RTJUST
;
369 // v calculate reversed CRC
370 /* Distinct from the -V switch as this causes
371 * the arguments and output to be reversed as well.
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:
384 if(~model
.flags
& P_REFOUT
) {
389 // swap init and xorout
391 model
.init
= model
.xorout
;
392 model
.xorout
= apoly
;
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);
403 /* in the Williams model, xorout is applied after the refout stage.
404 * as refout is part of ptostr(), we reverse xorout here.
406 if(model
.flags
& P_REFOUT
)
409 apoly
= strtop(inHexStr
, model
.flags
, ibperhx
);
414 crc
= pcrc(apoly
, model
.spoly
, model
.init
, model
.xorout
, model
.flags
);
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;
430 //test call to RunModel
431 int CmdrevengTestC(const char *Cmd
){
433 char inModel
[30] = {0x00};
434 char inHexStr
[30] = {0x00};
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
++);
445 //PrintAndLog("mod: %s, hex: %s, rev %d", inModel, inHexStr, reverse);
446 int ans
= RunModel(inModel
, inHexStr
, reverse
, endian
, result
);
449 PrintAndLog("Result: %s",result
);
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
)];
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;
475 uint8_t crcChars
= 0;
478 int ans
= GetModels(Models
, &count
, width
);
482 // try each model and get result
483 for (int i
= 0; i
< count
; i
++){
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
)
493 memset(result
, 0, 30);
494 char *inCRC
= calloc(crcChars
+1, sizeof(char));
495 memcpy(inCRC
, inHexStr
+(dataLen
-crcChars
), crcChars
);
497 char *outHex
= calloc(dataLen
-crcChars
+1, sizeof(char));
498 memcpy(outHex
, inHexStr
, dataLen
-crcChars
);
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
);
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...
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...
522 ans
= RunModel(Models
[i
], outHex
, true, 0, revResult
);
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...
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...
546 if (!found
) PrintAndLog("\nNo matches found\n");