]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdcrc.c
f756cde43e2f33c87006569a7d896cad5cb5ab9a
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
);
73 //PrintAndLog("argc: %d, %s %s Cmd: %s",argc, argv[0], Cmd2, Cmd);
74 reveng_main(argc
, argv
);
75 for(int i
= 0; i
< argc
; ++i
){
83 int GetModels(char *Models
[], int *count
, uint32_t *width
){
85 static model_t model
= {
86 PZERO
, /* no CRC polynomial, user must specify */
88 P_BE
, /* RefIn = false, RefOut = false, plus P_RTJUST setting in reveng.h */
89 PZERO
, /* XorOut = 0 */
90 PZERO
, /* check value unused */
91 NULL
/* no model name */
94 int ibperhx
= 8;//, obperhx = 8;
95 int rflags
= 0, uflags
= 0; /* search and UI flags */
96 poly_t apoly
, crc
, qpoly
= PZERO
, *apolys
= NULL
, *pptr
= NULL
, *qptr
= NULL
;
97 model_t pset
= model
, *candmods
, *mptr
;
99 /* stdin must be binary */
101 _setmode(STDIN_FILENO
, _O_BINARY
);
106 int args
= 0, psets
, pass
;
108 if (*width
== 0) { //reveng -D
111 return uerr("no preset models available");
113 for(int mode
= 0; mode
< *count
; ++mode
) {
114 mbynum(&model
, mode
);
116 size_t size
= (model
.name
&& *model
.name
) ? strlen(model
.name
) : 6;
117 char *tmp
= calloc(size
+1, sizeof(char));
119 return uerr("out of memory?");
121 memcpy(tmp
, model
.name
, size
);
127 if(~model
.flags
& P_MULXN
)
128 return uerr("cannot search for non-Williams compliant models");
130 praloc(&model
.spoly
, *width
);
131 praloc(&model
.init
, *width
);
132 praloc(&model
.xorout
, *width
);
133 if(!plen(model
.spoly
))
134 palloc(&model
.spoly
, *width
);
136 *width
= plen(model
.spoly
);
138 /* special case if qpoly is zero, search to end of range */
143 /* not going to be sending additional args at this time (maybe future?)
145 // allocate argument array
146 args = argc - optind;
147 if(!(apolys = malloc(args * sizeof(poly_t))))
148 return uerr("cannot allocate memory for argument list");
150 for(pptr = apolys; optind < argc; ++optind) {
151 if(uflags & C_INFILE)
152 *pptr++ = rdpoly(argv[optind], model.flags, ibperhx);
154 *pptr++ = strtop(argv[optind], model.flags, ibperhx);
156 // exit value of pptr is used hereafter!
160 /* if endianness not specified, try
161 * little-endian then big-endian.
162 * NB: crossed-endian algorithms will not be
165 /* scan against preset models */
166 if(~uflags
& C_FORCE
) {
171 //PrintAndLog("psets: %d",psets);
173 mbynum(&pset
, --psets
);
175 /* skip if different width, or refin or refout don't match */
176 if(plen(pset
.spoly
) != *width
|| (model
.flags
^ pset
.flags
) & (P_REFIN
| P_REFOUT
))
178 /* skip if the preset doesn't match specified parameters */
179 if(rflags
& R_HAVEP
&& pcmp(&model
.spoly
, &pset
.spoly
))
181 if(rflags
& R_HAVEI
&& psncmp(&model
.init
, &pset
.init
))
183 if(rflags
& R_HAVEX
&& psncmp(&model
.xorout
, &pset
.xorout
))
186 //for additional args (not used yet, maybe future?)
187 apoly
= pclone(pset
.xorout
);
188 if(pset
.flags
& P_REFOUT
)
191 for(qptr
= apolys
; qptr
< pptr
; ++qptr
) {
192 crc
= pcrc(*qptr
, pset
.spoly
, pset
.init
, apoly
, 0);
202 /* the selected model solved all arguments */
206 size_t size
= (pset
.name
&& *pset
.name
) ? strlen(pset
.name
) : 6;
207 //PrintAndLog("Size: %d, %s, count: %d",size,pset.name, Cnt);
208 char *tmp
= calloc(size
+1, sizeof(char));
210 PrintAndLog("out of memory?");
213 memcpy(tmp
, pset
.name
, size
);
221 /* toggle refIn/refOut and reflect arguments */
222 if(~rflags
& R_HAVERI
) {
223 model
.flags
^= P_REFIN
| P_REFOUT
;
224 for(qptr
= apolys
; qptr
< pptr
; ++qptr
)
225 prevch(qptr
, ibperhx
);
227 } while(~rflags
& R_HAVERI
&& ++pass
< 2);
229 //got everything now free the memory...
231 if(uflags
& C_RESULT
) {
232 for(qptr
= apolys
; qptr
< pptr
; ++qptr
)
235 if(!(model
.flags
& P_REFIN
) != !(model
.flags
& P_REFOUT
))
236 return uerr("cannot search for crossed-endian models");
240 mptr
= candmods
= reveng(&model
, qpoly
, rflags
, args
, apolys
);
241 if(mptr
&& plen(mptr
->spoly
))
243 while(mptr
&& plen(mptr
->spoly
)) {
247 if(~rflags
& R_HAVERI
) {
248 model
.flags
^= P_REFIN
| P_REFOUT
;
249 for(qptr
= apolys
; qptr
< pptr
; ++qptr
)
250 prevch(qptr
, ibperhx
);
252 } while(~rflags
& R_HAVERI
&& ++pass
< 2);
253 for(qptr
= apolys
; qptr
< pptr
; ++qptr
)
256 if(~uflags
& C_RESULT
)
257 return uerr("no models found");
263 //test call to GetModels
264 int CmdrevengTest(const char *Cmd
){
268 width
= param_get8(Cmd
, 0);
269 //PrintAndLog("width: %d",width);
271 return uerr("Width cannot exceed 89");
273 int ans
= GetModels(Models
, &count
, &width
);
276 PrintAndLog("Count: %d",count
);
277 for (int i
= 0; i
< count
; i
++){
278 PrintAndLog("Model %d: %s",i
,Models
[i
]);
285 //inModel = valid model name string - CRC-8
286 //inHexStr = input hex string to calculate crc on
287 //reverse = reverse calc option if true
288 //endian = {0 = calc default endian input and output, b = big endian input and output, B = big endian output, r = right justified
289 // l = little endian input and output, L = little endian output only, t = left justified}
290 //result = calculated crc hex string
291 int RunModel(char *inModel
, char *inHexStr
, bool reverse
, char endian
, char *result
){
293 static model_t model
= {
294 PZERO
, // no CRC polynomial, user must specify
296 P_BE
, // RefIn = false, RefOut = false, plus P_RTJUST setting in reveng.h
298 PZERO
, // check value unused
299 NULL
// no model name
301 int ibperhx
= 8, obperhx
= 8;
302 int rflags
= 0; // search flags
304 unsigned long width
= 0UL;
309 // stdin must be binary
311 _setmode(STDIN_FILENO
, _O_BINARY
);
316 if(!(c
= mbynam(&model
, inModel
))) {
317 fprintf(stderr
,"error: preset model '%s' not found. Use reveng -D to list presets.\n", inModel
);
321 return uerr("no preset models available");
323 // must set width so that parameter to -ipx is not zeroed
324 width
= plen(model
.spoly
);
325 rflags
|= R_HAVEP
| R_HAVEI
| R_HAVERI
| R_HAVERO
| R_HAVEX
;
329 case 'b': /* b big-endian (RefIn = false, RefOut = false ) */
330 model
.flags
&= ~P_REFIN
;
333 case 'B': /* B big-endian output (RefOut = false) */
334 model
.flags
&= ~P_REFOUT
;
338 case 'r': /* r right-justified */
339 model
.flags
|= P_RTJUST
;
341 case 'l': /* l little-endian input and output */
342 model
.flags
|= P_REFIN
;
345 case 'L': /* L little-endian output */
346 model
.flags
|= P_REFOUT
;
350 case 't': /* t left-justified */
351 model
.flags
&= ~P_RTJUST
;
358 // v calculate reversed CRC
359 /* Distinct from the -V switch as this causes
360 * the arguments and output to be reversed as well.
366 * if(refout) prev(init); else prev(xorout);
367 * but here the entire argument polynomial is
368 * reflected, not just the characters, so RefIn
369 * and RefOut are not inverted as with -V.
370 * Consequently Init is the mirror image of the
371 * one resulting from -V, and so we have:
373 if(~model
.flags
& P_REFOUT
) {
378 // swap init and xorout
380 model
.init
= model
.xorout
;
381 model
.xorout
= apoly
;
386 /* if(plen(model.spoly) == 0) {
387 * fprintf(stderr,"%s: no polynomial specified for -%c (add -w WIDTH -p POLY)\n", myname, mode);
388 * exit(EXIT_FAILURE);
392 /* in the Williams model, xorout is applied after the refout stage.
393 * as refout is part of ptostr(), we reverse xorout here.
395 if(model
.flags
& P_REFOUT
)
398 apoly
= strtop(inHexStr
, model
.flags
, ibperhx
);
403 crc
= pcrc(apoly
, model
.spoly
, model
.init
, model
.xorout
, model
.flags
);
408 string
= ptostr(crc
, model
.flags
, obperhx
);
409 for (int i
= 0; i
< 50; i
++){
410 result
[i
] = string
[i
];
411 if (result
[i
]==0) break;
419 //test call to RunModel
420 int CmdrevengTestC(const char *Cmd
){
422 char inModel
[30] = {0x00};
423 char inHexStr
[30] = {0x00};
427 dataLen
= param_getstr(Cmd
, cmdp
++, inModel
);
428 if (dataLen
< 4) return 0;
429 dataLen
= param_getstr(Cmd
, cmdp
++, inHexStr
);
430 if (dataLen
< 4) return 0;
431 bool reverse
= (param_get8(Cmd
, cmdp
++)) ? true : false;
432 endian
= param_getchar(Cmd
, cmdp
++);
434 //PrintAndLog("mod: %s, hex: %s, rev %d", inModel, inHexStr, reverse);
435 int ans
= RunModel(inModel
, inHexStr
, reverse
, endian
, result
);
438 PrintAndLog("Result: %s",result
);