]>
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 */
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')
49 endIndex
= beginIndex
;
50 while (str
[endIndex
] && !isspace(str
[endIndex
])){
53 int len
= endIndex
- beginIndex
;
54 char *tmp
= calloc(len
+ 1, sizeof(char));
55 memcpy(tmp
, &str
[beginIndex
], len
);
57 //PrintAndLog("cnt: %d, %s",wordCnt-1, arr[wordCnt-1]);
58 beginIndex
= endIndex
;
59 if (wordCnt
== maxWords
)
65 int CmdCrc(const char *Cmd
)
67 char name
[] = {"reveng "};
69 memcpy(Cmd2
, name
, 7);
70 memcpy(Cmd2
+ 7, Cmd
, 50);
72 int argc
= split(Cmd2
, argv
);
74 if (argc
== 3 && memcmp(argv
[1],"-g",2)==0) {
75 CmdrevengSearch(argv
[2]);
77 reveng_main(argc
, argv
);
79 //PrintAndLog("DEBUG argc: %d, %s %s Cmd: %s",argc, argv[0], Cmd2, Cmd);
80 for(int i
= 0; i
< argc
; ++i
){
87 //returns array of model names and the count of models returning
88 // as well as a width array for the width of each model
89 int GetModels(char *Models
[], int *count
, uint8_t *width
){
91 static model_t model
= {
92 PZERO
, /* no CRC polynomial, user must specify */
94 P_BE
, /* RefIn = false, RefOut = false, plus P_RTJUST setting in reveng.h */
95 PZERO
, /* XorOut = 0 */
96 PZERO
, /* check value unused */
97 NULL
/* no model name */
100 int ibperhx
= 8;//, obperhx = 8;
101 int rflags
= 0, uflags
= 0; /* search and UI flags */
102 poly_t apoly
, crc
, qpoly
= PZERO
, *apolys
= NULL
, *pptr
= NULL
, *qptr
= NULL
;
103 model_t pset
= model
, *candmods
, *mptr
;
105 /* stdin must be binary */
107 _setmode(STDIN_FILENO
, _O_BINARY
);
112 int args
= 0, psets
, pass
;
114 if (width
[0] == 0) { //reveng -D
117 return uerr("no preset models available");
119 for(int mode
= 0; mode
< *count
; ++mode
) {
120 mbynum(&model
, mode
);
122 size_t size
= (model
.name
&& *model
.name
) ? strlen(model
.name
) : 6;
123 char *tmp
= calloc(size
+1, sizeof(char));
125 return uerr("out of memory?");
127 memcpy(tmp
, model
.name
, size
);
129 width
[mode
] = plen(model
.spoly
);
134 if(~model
.flags
& P_MULXN
)
135 return uerr("cannot search for non-Williams compliant models");
137 praloc(&model
.spoly
, (unsigned long)width
[0]);
138 praloc(&model
.init
, (unsigned long)width
[0]);
139 praloc(&model
.xorout
, (unsigned long)width
[0]);
140 if(!plen(model
.spoly
))
141 palloc(&model
.spoly
, (unsigned long)width
[0]);
143 width
[0] = (uint8_t)plen(model
.spoly
);
145 /* special case if qpoly is zero, search to end of range */
149 /* if endianness not specified, try
150 * little-endian then big-endian.
151 * NB: crossed-endian algorithms will not be
154 /* scan against preset models */
155 if(~uflags
& C_FORCE
) {
162 mbynum(&pset
, --psets
);
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
))
167 /* skip if the preset doesn't match specified parameters */
168 if(rflags
& R_HAVEP
&& pcmp(&model
.spoly
, &pset
.spoly
))
170 if(rflags
& R_HAVEI
&& psncmp(&model
.init
, &pset
.init
))
172 if(rflags
& R_HAVEX
&& psncmp(&model
.xorout
, &pset
.xorout
))
175 //for additional args (not used yet, maybe future?)
176 apoly
= pclone(pset
.xorout
);
177 if(pset
.flags
& P_REFOUT
)
180 for(qptr
= apolys
; qptr
< pptr
; ++qptr
) {
181 crc
= pcrc(*qptr
, pset
.spoly
, pset
.init
, apoly
, 0);
191 /* the selected model solved all arguments */
195 size_t size
= (pset
.name
&& *pset
.name
) ? strlen(pset
.name
) : 6;
196 //PrintAndLog("Size: %d, %s, count: %d",size,pset.name, Cnt);
197 char *tmp
= calloc(size
+1, sizeof(char));
199 PrintAndLog("out of memory?");
202 width
[Cnt
] = width
[0];
203 memcpy(tmp
, pset
.name
, size
);
211 /* toggle refIn/refOut and reflect arguments */
212 if(~rflags
& R_HAVERI
) {
213 model
.flags
^= P_REFIN
| P_REFOUT
;
214 for(qptr
= apolys
; qptr
< pptr
; ++qptr
)
215 prevch(qptr
, ibperhx
);
217 } while(~rflags
& R_HAVERI
&& ++pass
< 2);
219 //got everything now free the memory...
221 if(uflags
& C_RESULT
) {
222 for(qptr
= apolys
; qptr
< pptr
; ++qptr
)
225 if(!(model
.flags
& P_REFIN
) != !(model
.flags
& P_REFOUT
))
226 return uerr("cannot search for crossed-endian models");
230 mptr
= candmods
= reveng(&model
, qpoly
, rflags
, args
, apolys
);
231 if(mptr
&& plen(mptr
->spoly
))
233 while(mptr
&& plen(mptr
->spoly
)) {
237 if(~rflags
& R_HAVERI
) {
238 model
.flags
^= P_REFIN
| P_REFOUT
;
239 for(qptr
= apolys
; qptr
< pptr
; ++qptr
)
240 prevch(qptr
, ibperhx
);
242 } while(~rflags
& R_HAVERI
&& ++pass
< 2);
243 for(qptr
= apolys
; qptr
< pptr
; ++qptr
)
246 if(~uflags
& C_RESULT
)
247 return uerr("no models found");
255 //inModel = valid model name string - CRC-8
256 //inHexStr = input hex string to calculate crc on
257 //reverse = reverse calc option if true
258 //endian = {0 = calc default endian input and output, b = big endian input and output, B = big endian output, r = right justified
259 // l = little endian input and output, L = little endian output only, t = left justified}
260 //result = calculated crc hex string
261 int RunModel(char *inModel
, char *inHexStr
, bool reverse
, char endian
, char *result
){
263 static model_t model
= {
264 PZERO
, // no CRC polynomial, user must specify
266 P_BE
, // RefIn = false, RefOut = false, plus P_RTJUST setting in reveng.h
268 PZERO
, // check value unused
269 NULL
// no model name
271 int ibperhx
= 8, obperhx
= 8;
272 int rflags
= 0; // search flags
278 // stdin must be binary
280 _setmode(STDIN_FILENO
, _O_BINARY
);
285 if(!(c
= mbynam(&model
, inModel
))) {
286 PrintAndLog("error: preset model '%s' not found. Use reveng -D to list presets.", inModel
);
290 return uerr("no preset models available");
292 rflags
|= R_HAVEP
| R_HAVEI
| R_HAVERI
| R_HAVERO
| R_HAVEX
;
296 case 'b': /* b big-endian (RefIn = false, RefOut = false ) */
297 model
.flags
&= ~P_REFIN
;
300 case 'B': /* B big-endian output (RefOut = false) */
301 model
.flags
&= ~P_REFOUT
;
305 case 'r': /* r right-justified */
306 model
.flags
|= P_RTJUST
;
308 case 'l': /* l little-endian input and output */
309 model
.flags
|= P_REFIN
;
312 case 'L': /* L little-endian output */
313 model
.flags
|= P_REFOUT
;
317 case 't': /* t left-justified */
318 model
.flags
&= ~P_RTJUST
;
325 // v calculate reversed CRC
326 /* Distinct from the -V switch as this causes
327 * the arguments and output to be reversed as well.
333 * if(refout) prev(init); else prev(xorout);
334 * but here the entire argument polynomial is
335 * reflected, not just the characters, so RefIn
336 * and RefOut are not inverted as with -V.
337 * Consequently Init is the mirror image of the
338 * one resulting from -V, and so we have:
340 if(~model
.flags
& P_REFOUT
) {
345 // swap init and xorout
347 model
.init
= model
.xorout
;
348 model
.xorout
= apoly
;
352 /* in the Williams model, xorout is applied after the refout stage.
353 * as refout is part of ptostr(), we reverse xorout here.
355 if(model
.flags
& P_REFOUT
)
358 apoly
= strtop(inHexStr
, model
.flags
, ibperhx
);
363 crc
= pcrc(apoly
, model
.spoly
, model
.init
, model
.xorout
, model
.flags
);
368 string
= ptostr(crc
, model
.flags
, obperhx
);
369 for (int i
= 0; i
< 50; i
++){
370 result
[i
] = string
[i
];
371 if (result
[i
]==0) break;
378 //returns a calloced string (needs to be freed)
379 char *SwapEndianStr(const char *inStr
, const size_t len
, const uint8_t blockSize
){
380 char *tmp
= calloc(len
+1, sizeof(char));
381 for (uint8_t block
=0; block
< (uint8_t)(len
/blockSize
); block
++){
382 for (size_t i
= 0; i
< blockSize
; i
+=2){
383 tmp
[i
+(blockSize
*block
)] = inStr
[(blockSize
-1-i
-1)+(blockSize
*block
)];
384 tmp
[i
+(blockSize
*block
)+1] = inStr
[(blockSize
-1-i
)+(blockSize
*block
)];
390 // takes hex string in and searches for a matching result (hex string must include checksum)
391 int CmdrevengSearch(const char *Cmd
){
392 char inHexStr
[50] = {0x00};
393 int dataLen
= param_getstr(Cmd
, 0, inHexStr
);
394 if (dataLen
< 4) return 0;
400 uint8_t crcChars
= 0;
403 int ans
= GetModels(Models
, &count
, width
);
407 // try each model and get result
408 for (int i
= 0; i
< count
; i
++){
413 // round up to # of characters in this model's crc
414 crcChars
= ((width
[i
]+7)/8)*2;
415 // can't test a model that has more crc digits than our data
416 if (crcChars
>= dataLen
)
418 memset(result
, 0, 30);
419 char *inCRC
= calloc(crcChars
+1, sizeof(char));
420 memcpy(inCRC
, inHexStr
+(dataLen
-crcChars
), crcChars
);
422 char *outHex
= calloc(dataLen
-crcChars
+1, sizeof(char));
423 memcpy(outHex
, inHexStr
, dataLen
-crcChars
);
425 //PrintAndLog("DEBUG: dataLen: %d, crcChars: %d, Model: %s, CRC: %s, width: %d, outHex: %s",dataLen, crcChars, Models[i], inCRC, width[i], outHex);
426 ans
= RunModel(Models
[i
], outHex
, false, 0, result
);
429 if (memcmp(result
, inCRC
, crcChars
)==0){
430 PrintAndLog("\nFound a possible match!\nModel: %s\nValue: %s\n",Models
[i
], result
);
431 //optional - stop searching if found...
435 char *swapEndian
= SwapEndianStr(result
, crcChars
, crcChars
);
436 if (memcmp(swapEndian
, inCRC
, crcChars
)==0){
437 PrintAndLog("\nFound a possible match!\nModel: %s\nValue EndianSwapped: %s\n",Models
[i
], swapEndian
);
438 //optional - stop searching if found...
447 ans
= RunModel(Models
[i
], outHex
, true, 0, revResult
);
450 if (memcmp(revResult
, inCRC
, crcChars
)==0){
451 PrintAndLog("\nFound a possible match!\nModel Reversed: %s\nValue: %s\n",Models
[i
], revResult
);
452 //optional - stop searching if found...
456 char *swapEndian
= SwapEndianStr(revResult
, crcChars
, crcChars
);
457 if (memcmp(swapEndian
, inCRC
, crcChars
)==0){
458 PrintAndLog("\nFound a possible match!\nModel Reversed: %s\nValue EndianSwapped: %s\n",Models
[i
], swapEndian
);
459 //optional - stop searching if found...
471 if (!found
) PrintAndLog("\nNo matches found\n");