]> git.zerfleddert.de Git - proxmark3-svn/blame - client/cmdcrc.c
CHG: moved includes into header file where it belong
[proxmark3-svn] / client / cmdcrc.c
CommitLineData
9962091e 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
60e86577 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
9962091e 20#include <stdio.h>
21#include <string.h>
a71ece51 22#include "cmdmain.h"
9962091e 23#include "cmdcrc.h"
a71ece51 24#include "reveng/reveng.h"
25#include "ui.h"
26#include "util.h"
9962091e 27
a71ece51 28#define MAX_ARGS 20
9962091e 29
60e86577 30int uerr(char *msg){
31 PrintAndLog("%s",msg);
32 return 0;
33}
34
a71ece51 35int split(char *str, char *arr[MAX_ARGS]){
36 int beginIndex = 0;
37 int endIndex;
38 int maxWords = MAX_ARGS;
39 int wordCnt = 0;
40
41 while(1){
42 while(isspace(str[beginIndex])){
43 ++beginIndex;
44 }
45 if(str[beginIndex] == '\0')
46 break;
47 endIndex = beginIndex;
48 while (str[endIndex] && !isspace(str[endIndex])){
49 ++endIndex;
50 }
51 int len = endIndex - beginIndex;
52 char *tmp = calloc(len + 1, sizeof(char));
53 memcpy(tmp, &str[beginIndex], len);
54 arr[wordCnt++] = tmp;
55 //PrintAndLog("cnt: %d, %s",wordCnt-1, arr[wordCnt-1]);
56 beginIndex = endIndex;
57 if (wordCnt == maxWords)
58 break;
59 }
60 return wordCnt;
61}
9962091e 62
63int CmdCrc(const char *Cmd)
64{
a71ece51 65 char name[] = {"reveng "};
66 char Cmd2[50 + 7];
67 memcpy(Cmd2, name, 7);
68 memcpy(Cmd2 + 7, Cmd, 50);
69 char *argv[MAX_ARGS];
70 int argc = split(Cmd2, argv);
99789601 71
72 if (argc == 3 && memcmp(argv[1],"-g",2)==0) {
73 CmdrevengSearch(argv[2]);
74 } else {
13629a71 75 reveng_main(argc, argv);
99789601 76 }
77 //PrintAndLog("DEBUG argc: %d, %s %s Cmd: %s",argc, argv[0], Cmd2, Cmd);
13629a71 78 for(int i = 0; i < argc; ++i) {
a71ece51 79 free(argv[i]);
80 }
81
9962091e 82 return 0;
83}
84
99789601 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
87int GetModels(char *Models[], int *count, uint8_t *width){
60e86577 88 /* default values */
89 static model_t model = {
90 PZERO, /* no CRC polynomial, user must specify */
91 PZERO, /* Init = 0 */
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 */
96 };
dd1df490 97
60e86577 98 int ibperhx = 8;//, obperhx = 8;
99 int rflags = 0, uflags = 0; /* search and UI flags */
60e86577 100 poly_t apoly, crc, qpoly = PZERO, *apolys = NULL, *pptr = NULL, *qptr = NULL;
101 model_t pset = model, *candmods, *mptr;
102
103 /* stdin must be binary */
104 #ifdef _WIN32
105 _setmode(STDIN_FILENO, _O_BINARY);
106 #endif /* _WIN32 */
107
108 SETBMP();
109
110 int args = 0, psets, pass;
111 int Cnt = 0;
99789601 112 if (width[0] == 0) { //reveng -D
60e86577 113 *count = mcount();
60e86577 114 if(!*count)
115 return uerr("no preset models available");
116
117 for(int mode = 0; mode < *count; ++mode) {
118 mbynum(&model, mode);
119 mcanon(&model);
120 size_t size = (model.name && *model.name) ? strlen(model.name) : 6;
60e86577 121 char *tmp = calloc(size+1, sizeof(char));
122 if (tmp==NULL)
123 return uerr("out of memory?");
124
125 memcpy(tmp, model.name, size);
126 Models[mode] = tmp;
99789601 127 width[mode] = plen(model.spoly);
60e86577 128 }
dd1df490 129 mfree(&model);
60e86577 130 } else { //reveng -s
131
13629a71 132 if(~model.flags & P_MULXN)
133 return uerr("cannot search for non-Williams compliant models");
60e86577 134
99789601 135 praloc(&model.spoly, (unsigned long)width[0]);
136 praloc(&model.init, (unsigned long)width[0]);
137 praloc(&model.xorout, (unsigned long)width[0]);
13629a71 138
139 if(!plen(model.spoly))
99789601 140 palloc(&model.spoly, (unsigned long)width[0]);
13629a71 141 else
99789601 142 width[0] = (uint8_t)plen(model.spoly);
60e86577 143
13629a71 144 /* special case if qpoly is zero, search to end of range */
145 if(!ptst(qpoly))
146 rflags &= ~R_HAVEQ;
60e86577 147
13629a71 148 /* if endianness not specified, try
149 * little-endian then big-endian.
150 * NB: crossed-endian algorithms will not be
151 * searched.
152 */
153 /* scan against preset models */
154 if(~uflags & C_FORCE) {
155 pass = 0;
156 Cnt = 0;
157 do {
158 psets = mcount();
159
160 while(psets) {
161 mbynum(&pset, --psets);
162
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))
165 continue;
166 /* skip if the preset doesn't match specified parameters */
167 if(rflags & R_HAVEP && pcmp(&model.spoly, &pset.spoly))
168 continue;
169 if(rflags & R_HAVEI && psncmp(&model.init, &pset.init))
170 continue;
171 if(rflags & R_HAVEX && psncmp(&model.xorout, &pset.xorout))
172 continue;
173
174 //for additional args (not used yet, maybe future?)
175 apoly = pclone(pset.xorout);
176 if(pset.flags & P_REFOUT)
177 prev(&apoly);
178
179 for(qptr = apolys; qptr < pptr; ++qptr) {
180 crc = pcrc(*qptr, pset.spoly, pset.init, apoly, 0);
181 if(ptst(crc)) {
182 pfree(&crc);
183 break;
184 } else
185 pfree(&crc);
60e86577 186 }
13629a71 187 pfree(&apoly);
188 if(qptr == pptr) {
99789601 189
13629a71 190 /* the selected model solved all arguments */
dd1df490 191
13629a71 192 mcanon(&pset);
193
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));
197 if (tmp == NULL){
198 PrintAndLog("out of memory?");
199 return 0;
200 }
201 width[Cnt] = width[0];
202 memcpy(tmp, pset.name, size);
203 Models[Cnt++] = tmp;
204 *count = Cnt;
205 uflags |= C_RESULT;
206 }
60e86577 207 }
13629a71 208 mfree(&pset);
209
210 /* toggle refIn/refOut and reflect arguments */
60e86577 211 if(~rflags & R_HAVERI) {
212 model.flags ^= P_REFIN | P_REFOUT;
213 for(qptr = apolys; qptr < pptr; ++qptr)
214 prevch(qptr, ibperhx);
215 }
216 } while(~rflags & R_HAVERI && ++pass < 2);
13629a71 217 }
218 //got everything now free the memory...
219
220 if(uflags & C_RESULT) {
60e86577 221 for(qptr = apolys; qptr < pptr; ++qptr)
222 pfree(qptr);
13629a71 223 }
224 if(!(model.flags & P_REFIN) != !(model.flags & P_REFOUT))
225 return uerr("cannot search for crossed-endian models");
226
227 pass = 0;
228 do {
229 mptr = candmods = reveng(&model, qpoly, rflags, args, apolys);
230 if(mptr && plen(mptr->spoly))
231 uflags |= C_RESULT;
232 while(mptr && plen(mptr->spoly)) {
233 mfree(mptr++);
234 }
235 free(candmods);
236 if(~rflags & R_HAVERI) {
237 model.flags ^= P_REFIN | P_REFOUT;
238 for(qptr = apolys; qptr < pptr; ++qptr)
239 prevch(qptr, ibperhx);
240 }
241 } while(~rflags & R_HAVERI && ++pass < 2);
242 for(qptr = apolys; qptr < pptr; ++qptr)
243 pfree(qptr);
244 free(apolys);
245 if(~uflags & C_RESULT)
246 return uerr("no models found");
247 mfree(&model);
60e86577 248
60e86577 249 }
250 return 1;
251}
252
253//-c || -v
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
260int RunModel(char *inModel, char *inHexStr, bool reverse, char endian, char *result){
261 /* default values */
262 static model_t model = {
263 PZERO, // no CRC polynomial, user must specify
264 PZERO, // Init = 0
265 P_BE, // RefIn = false, RefOut = false, plus P_RTJUST setting in reveng.h
266 PZERO, // XorOut = 0
267 PZERO, // check value unused
268 NULL // no model name
269 };
270 int ibperhx = 8, obperhx = 8;
271 int rflags = 0; // search flags
272 int c;
60e86577 273 poly_t apoly, crc;
274
275 char *string;
276
277 // stdin must be binary
278 #ifdef _WIN32
279 _setmode(STDIN_FILENO, _O_BINARY);
280 #endif /* _WIN32 */
281
282 SETBMP();
283 //set model
284 if(!(c = mbynam(&model, inModel))) {
dd1df490 285 PrintAndLog("error: preset model '%s' not found. Use reveng -D to list presets.", inModel);
60e86577 286 return 0;
287 }
288 if(c < 0)
289 return uerr("no preset models available");
290
60e86577 291 rflags |= R_HAVEP | R_HAVEI | R_HAVERI | R_HAVERO | R_HAVEX;
292
293 //set flags
294 switch (endian) {
295 case 'b': /* b big-endian (RefIn = false, RefOut = false ) */
296 model.flags &= ~P_REFIN;
297 rflags |= R_HAVERI;
298 /* fall through: */
299 case 'B': /* B big-endian output (RefOut = false) */
300 model.flags &= ~P_REFOUT;
301 rflags |= R_HAVERO;
302 mnovel(&model);
303 /* fall through: */
304 case 'r': /* r right-justified */
305 model.flags |= P_RTJUST;
306 break;
307 case 'l': /* l little-endian input and output */
308 model.flags |= P_REFIN;
309 rflags |= R_HAVERI;
310 /* fall through: */
311 case 'L': /* L little-endian output */
312 model.flags |= P_REFOUT;
313 rflags |= R_HAVERO;
314 mnovel(&model);
315 /* fall through: */
316 case 't': /* t left-justified */
317 model.flags &= ~P_RTJUST;
318 break;
319 }
13629a71 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.)
323 */
60e86577 324 mcanon(&model);
325
13629a71 326
60e86577 327 if (reverse) {
328 // v calculate reversed CRC
329 /* Distinct from the -V switch as this causes
330 * the arguments and output to be reversed as well.
331 */
332 // reciprocate Poly
333 prcp(&model.spoly);
334
335 /* mrev() does:
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:
342 */
343 if(~model.flags & P_REFOUT) {
344 prev(&model.init);
345 prev(&model.xorout);
346 }
347
348 // swap init and xorout
349 apoly = model.init;
350 model.init = model.xorout;
351 model.xorout = apoly;
352 }
353 // c calculate CRC
354
355 /* in the Williams model, xorout is applied after the refout stage.
356 * as refout is part of ptostr(), we reverse xorout here.
357 */
358 if(model.flags & P_REFOUT)
359 prev(&model.xorout);
360
361 apoly = strtop(inHexStr, model.flags, ibperhx);
362
363 if(reverse)
364 prev(&apoly);
365
366 crc = pcrc(apoly, model.spoly, model.init, model.xorout, model.flags);
367
368 if(reverse)
369 prev(&crc);
370
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;
375 }
376 free(string);
377 pfree(&crc);
378 pfree(&apoly);
379 return 1;
380}
99789601 381//returns a calloced string (needs to be freed)
382char *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)];
388 }
389 }
390 return tmp;
391}
392
393// takes hex string in and searches for a matching result (hex string must include checksum)
394int CmdrevengSearch(const char *Cmd){
395 char inHexStr[50] = {0x00};
396 int dataLen = param_getstr(Cmd, 0, inHexStr);
397 if (dataLen < 4) return 0;
398
399 char *Models[80];
400 int count = 0;
401 uint8_t width[80];
402 width[0] = 0;
403 uint8_t crcChars = 0;
404 char result[30];
405 char revResult[30];
406 int ans = GetModels(Models, &count, width);
407 bool found = false;
408 if (!ans) return 0;
409
410 // try each model and get result
411 for (int i = 0; i < count; i++){
412 /*if (found) {
413 free(Models[i]);
414 continue;
415 }*/
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)
420 continue;
421 memset(result, 0, 30);
422 char *inCRC = calloc(crcChars+1, sizeof(char));
423 memcpy(inCRC, inHexStr+(dataLen-crcChars), crcChars);
424
425 char *outHex = calloc(dataLen-crcChars+1, sizeof(char));
426 memcpy(outHex, inHexStr, dataLen-crcChars);
427
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);
430 if (ans) {
431 //test for match
432 if (memcmp(result, inCRC, crcChars)==0){
1417a7f9 433 PrintAndLog("\nFound a possible match!\nModel: %s\nValue: %s\n",Models[i], result);
99789601 434 //optional - stop searching if found...
435 found = true;
436 } else {
437 if (crcChars > 2){
438 char *swapEndian = SwapEndianStr(result, crcChars, crcChars);
439 if (memcmp(swapEndian, inCRC, crcChars)==0){
1417a7f9 440 PrintAndLog("\nFound a possible match!\nModel: %s\nValue EndianSwapped: %s\n",Models[i], swapEndian);
99789601 441 //optional - stop searching if found...
442 found = true;
443 }
444 free(swapEndian);
445 }
446 }
447 }
448
449 //if (!found){
450 ans = RunModel(Models[i], outHex, true, 0, revResult);
451 if (ans) {
452 //test for match
453 if (memcmp(revResult, inCRC, crcChars)==0){
1417a7f9 454 PrintAndLog("\nFound a possible match!\nModel Reversed: %s\nValue: %s\n",Models[i], revResult);
99789601 455 //optional - stop searching if found...
456 found = true;
457 } else {
458 if (crcChars > 2){
459 char *swapEndian = SwapEndianStr(revResult, crcChars, crcChars);
460 if (memcmp(swapEndian, inCRC, crcChars)==0){
1417a7f9 461 PrintAndLog("\nFound a possible match!\nModel Reversed: %s\nValue EndianSwapped: %s\n",Models[i], swapEndian);
99789601 462 //optional - stop searching if found...
463 found = true;
464 }
465 free(swapEndian);
466 }
467 }
468 }
469 //}
470 free(inCRC);
471 free(outHex);
472 free(Models[i]);
473 }
474 if (!found) PrintAndLog("\nNo matches found\n");
475 return 1;
1417a7f9 476}
Impressum, Datenschutz