]>
cvs.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 */
24 #include "reveng/reveng.h"
31 PrintAndLog("%s",msg
);
35 int split(char *str
, char *arr
[MAX_ARGS
]){
38 int maxWords
= MAX_ARGS
;
42 while(isspace(str
[beginIndex
])){
45 if(str
[beginIndex
] == '\0')
47 endIndex
= beginIndex
;
48 while (str
[endIndex
] && !isspace(str
[endIndex
])){
51 int len
= endIndex
- beginIndex
;
52 char *tmp
= calloc(len
+ 1, sizeof(char));
53 memcpy(tmp
, &str
[beginIndex
], len
);
55 //PrintAndLog("cnt: %d, %s",wordCnt-1, arr[wordCnt-1]);
56 beginIndex
= endIndex
;
57 if (wordCnt
== maxWords
)
63 int CmdCrc(const char *Cmd
)
65 char name
[] = {"reveng "};
67 memcpy(Cmd2
, name
, 7);
68 memcpy(Cmd2
+ 7, Cmd
, 50);
70 int argc
= split(Cmd2
, argv
);
72 if (argc
== 3 && memcmp(argv
[1],"-g",2)==0) {
73 CmdrevengSearch(argv
[2]);
75 reveng_main(argc
, argv
);
77 //PrintAndLog("DEBUG argc: %d, %s %s Cmd: %s",argc, argv[0], Cmd2, Cmd);
78 for(int i
= 0; i
< argc
; ++i
) {
85 //returns array of model names and the count of models returning
86 // as well as a width array for the width of each model
87 int GetModels(char *Models
[], int *count
, uint8_t *width
){
89 static model_t model
= {
90 PZERO
, /* no CRC polynomial, user must specify */
92 P_BE
, /* RefIn = false, RefOut = false, plus P_RTJUST setting in reveng.h */
93 PZERO
, /* XorOut = 0 */
94 PZERO
, /* check value unused */
95 NULL
/* no model name */
98 int ibperhx
= 8;//, obperhx = 8;
99 int rflags
= 0, uflags
= 0; /* search and UI flags */
100 poly_t apoly
, crc
, qpoly
= PZERO
, *apolys
= NULL
, *pptr
= NULL
, *qptr
= NULL
;
101 model_t pset
= model
, *candmods
, *mptr
;
103 /* stdin must be binary */
105 _setmode(STDIN_FILENO
, _O_BINARY
);
110 int args
= 0, psets
, pass
;
112 if (width
[0] == 0) { //reveng -D
115 return uerr("no preset models available");
117 for(int mode
= 0; mode
< *count
; ++mode
) {
118 mbynum(&model
, mode
);
120 size_t size
= (model
.name
&& *model
.name
) ? strlen(model
.name
) : 6;
121 char *tmp
= calloc(size
+1, sizeof(char));
123 return uerr("out of memory?");
125 memcpy(tmp
, model
.name
, size
);
127 width
[mode
] = plen(model
.spoly
);
132 if(~model
.flags
& P_MULXN
)
133 return uerr("cannot search for non-Williams compliant models");
135 praloc(&model
.spoly
, (unsigned long)width
[0]);
136 praloc(&model
.init
, (unsigned long)width
[0]);
137 praloc(&model
.xorout
, (unsigned long)width
[0]);
139 if(!plen(model
.spoly
))
140 palloc(&model
.spoly
, (unsigned long)width
[0]);
142 width
[0] = (uint8_t)plen(model
.spoly
);
144 /* special case if qpoly is zero, search to end of range */
148 /* if endianness not specified, try
149 * little-endian then big-endian.
150 * NB: crossed-endian algorithms will not be
153 /* scan against preset models */
154 if(~uflags
& C_FORCE
) {
161 mbynum(&pset
, --psets
);
163 /* skip if different width, or refin or refout don't match */
164 if(plen(pset
.spoly
) != width
[0] || (model
.flags
^ pset
.flags
) & (P_REFIN
| P_REFOUT
))
166 /* skip if the preset doesn't match specified parameters */
167 if(rflags
& R_HAVEP
&& pcmp(&model
.spoly
, &pset
.spoly
))
169 if(rflags
& R_HAVEI
&& psncmp(&model
.init
, &pset
.init
))
171 if(rflags
& R_HAVEX
&& psncmp(&model
.xorout
, &pset
.xorout
))
174 //for additional args (not used yet, maybe future?)
175 apoly
= pclone(pset
.xorout
);
176 if(pset
.flags
& P_REFOUT
)
179 for(qptr
= apolys
; qptr
< pptr
; ++qptr
) {
180 crc
= pcrc(*qptr
, pset
.spoly
, pset
.init
, apoly
, 0);
190 /* the selected model solved all arguments */
194 size_t size
= (pset
.name
&& *pset
.name
) ? strlen(pset
.name
) : 6;
195 //PrintAndLog("Size: %d, %s, count: %d",size,pset.name, Cnt);
196 char *tmp
= calloc(size
+1, sizeof(char));
198 PrintAndLog("out of memory?");
201 width
[Cnt
] = width
[0];
202 memcpy(tmp
, pset
.name
, size
);
210 /* toggle refIn/refOut and reflect arguments */
211 if(~rflags
& R_HAVERI
) {
212 model
.flags
^= P_REFIN
| P_REFOUT
;
213 for(qptr
= apolys
; qptr
< pptr
; ++qptr
)
214 prevch(qptr
, ibperhx
);
216 } while(~rflags
& R_HAVERI
&& ++pass
< 2);
218 //got everything now free the memory...
220 if(uflags
& C_RESULT
) {
221 for(qptr
= apolys
; qptr
< pptr
; ++qptr
)
224 if(!(model
.flags
& P_REFIN
) != !(model
.flags
& P_REFOUT
))
225 return uerr("cannot search for crossed-endian models");
229 mptr
= candmods
= reveng(&model
, qpoly
, rflags
, args
, apolys
);
230 if(mptr
&& plen(mptr
->spoly
))
232 while(mptr
&& plen(mptr
->spoly
)) {
236 if(~rflags
& R_HAVERI
) {
237 model
.flags
^= P_REFIN
| P_REFOUT
;
238 for(qptr
= apolys
; qptr
< pptr
; ++qptr
)
239 prevch(qptr
, ibperhx
);
241 } while(~rflags
& R_HAVERI
&& ++pass
< 2);
242 for(qptr
= apolys
; qptr
< pptr
; ++qptr
)
245 if(~uflags
& C_RESULT
)
246 return uerr("no models found");
254 //inModel = valid model name string - CRC-8
255 //inHexStr = input hex string to calculate crc on
256 //reverse = reverse calc option if true
257 //endian = {0 = calc default endian input and output, b = big endian input and output, B = big endian output, r = right justified
258 // l = little endian input and output, L = little endian output only, t = left justified}
259 //result = calculated crc hex string
260 int RunModel(char *inModel
, char *inHexStr
, bool reverse
, char endian
, char *result
){
262 static model_t model
= {
263 PZERO
, // no CRC polynomial, user must specify
265 P_BE
, // RefIn = false, RefOut = false, plus P_RTJUST setting in reveng.h
267 PZERO
, // check value unused
268 NULL
// no model name
270 int ibperhx
= 8, obperhx
= 8;
271 int rflags
= 0; // search flags
277 // stdin must be binary
279 _setmode(STDIN_FILENO
, _O_BINARY
);
284 if(!(c
= mbynam(&model
, inModel
))) {
285 PrintAndLog("error: preset model '%s' not found. Use reveng -D to list presets.", inModel
);
289 return uerr("no preset models available");
291 rflags
|= R_HAVEP
| R_HAVEI
| R_HAVERI
| R_HAVERO
| R_HAVEX
;
295 case 'b': /* b big-endian (RefIn = false, RefOut = false ) */
296 model
.flags
&= ~P_REFIN
;
299 case 'B': /* B big-endian output (RefOut = false) */
300 model
.flags
&= ~P_REFOUT
;
304 case 'r': /* r right-justified */
305 model
.flags
|= P_RTJUST
;
307 case 'l': /* l little-endian input and output */
308 model
.flags
|= P_REFIN
;
311 case 'L': /* L little-endian output */
312 model
.flags
|= P_REFOUT
;
316 case 't': /* t left-justified */
317 model
.flags
&= ~P_RTJUST
;
320 /* canonicalise the model, so the one we dump is the one we
321 * calculate with (not with -s, spoly may be blank which will
322 * normalise to zero and clear init and xorout.)
328 // v calculate reversed CRC
329 /* Distinct from the -V switch as this causes
330 * the arguments and output to be reversed as well.
336 * if(refout) prev(init); else prev(xorout);
337 * but here the entire argument polynomial is
338 * reflected, not just the characters, so RefIn
339 * and RefOut are not inverted as with -V.
340 * Consequently Init is the mirror image of the
341 * one resulting from -V, and so we have:
343 if(~model
.flags
& P_REFOUT
) {
348 // swap init and xorout
350 model
.init
= model
.xorout
;
351 model
.xorout
= apoly
;
355 /* in the Williams model, xorout is applied after the refout stage.
356 * as refout is part of ptostr(), we reverse xorout here.
358 if(model
.flags
& P_REFOUT
)
361 apoly
= strtop(inHexStr
, model
.flags
, ibperhx
);
366 crc
= pcrc(apoly
, model
.spoly
, model
.init
, model
.xorout
, model
.flags
);
371 string
= ptostr(crc
, model
.flags
, obperhx
);
372 for (int i
= 0; i
< 50; i
++){
373 result
[i
] = string
[i
];
374 if (result
[i
]==0) break;
381 //returns a calloced string (needs to be freed)
382 char *SwapEndianStr(const char *inStr
, const size_t len
, const uint8_t blockSize
){
383 char *tmp
= calloc(len
+1, sizeof(char));
384 for (uint8_t block
=0; block
< (uint8_t)(len
/blockSize
); block
++){
385 for (size_t i
= 0; i
< blockSize
; i
+=2){
386 tmp
[i
+(blockSize
*block
)] = inStr
[(blockSize
-1-i
-1)+(blockSize
*block
)];
387 tmp
[i
+(blockSize
*block
)+1] = inStr
[(blockSize
-1-i
)+(blockSize
*block
)];
393 // takes hex string in and searches for a matching result (hex string must include checksum)
394 int CmdrevengSearch(const char *Cmd
){
395 char inHexStr
[50] = {0x00};
396 int dataLen
= param_getstr(Cmd
, 0, inHexStr
);
397 if (dataLen
< 4) return 0;
403 uint8_t crcChars
= 0;
406 int ans
= GetModels(Models
, &count
, width
);
410 // try each model and get result
411 for (int i
= 0; i
< count
; i
++){
416 // round up to # of characters in this model's crc
417 crcChars
= ((width
[i
]+7)/8)*2;
418 // can't test a model that has more crc digits than our data
419 if (crcChars
>= dataLen
)
421 memset(result
, 0, 30);
422 char *inCRC
= calloc(crcChars
+1, sizeof(char));
423 memcpy(inCRC
, inHexStr
+(dataLen
-crcChars
), crcChars
);
425 char *outHex
= calloc(dataLen
-crcChars
+1, sizeof(char));
426 memcpy(outHex
, inHexStr
, dataLen
-crcChars
);
428 //PrintAndLog("DEBUG: dataLen: %d, crcChars: %d, Model: %s, CRC: %s, width: %d, outHex: %s",dataLen, crcChars, Models[i], inCRC, width[i], outHex);
429 ans
= RunModel(Models
[i
], outHex
, false, 0, result
);
432 if (memcmp(result
, inCRC
, crcChars
)==0){
433 PrintAndLog("\nFound a possible match!\nModel: %s\nValue: %s\n",Models
[i
], result
);
434 //optional - stop searching if found...
438 char *swapEndian
= SwapEndianStr(result
, crcChars
, crcChars
);
439 if (memcmp(swapEndian
, inCRC
, crcChars
)==0){
440 PrintAndLog("\nFound a possible match!\nModel: %s\nValue EndianSwapped: %s\n",Models
[i
], swapEndian
);
441 //optional - stop searching if found...
450 ans
= RunModel(Models
[i
], outHex
, true, 0, revResult
);
453 if (memcmp(revResult
, inCRC
, crcChars
)==0){
454 PrintAndLog("\nFound a possible match!\nModel Reversed: %s\nValue: %s\n",Models
[i
], revResult
);
455 //optional - stop searching if found...
459 char *swapEndian
= SwapEndianStr(revResult
, crcChars
, crcChars
);
460 if (memcmp(swapEndian
, inCRC
, crcChars
)==0){
461 PrintAndLog("\nFound a possible match!\nModel Reversed: %s\nValue EndianSwapped: %s\n",Models
[i
], swapEndian
);
462 //optional - stop searching if found...
474 if (!found
) PrintAndLog("\nNo matches found\n");