]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/cmdcrc.c
fix: nice output..
[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 <stdlib.h>
23//#include <ctype.h>
24#include "cmdmain.h"
9962091e 25#include "cmdcrc.h"
a71ece51 26#include "reveng/reveng.h"
27#include "ui.h"
28#include "util.h"
9962091e 29
a71ece51 30#define MAX_ARGS 20
9962091e 31
60e86577 32int uerr(char *msg){
33 PrintAndLog("%s",msg);
34 return 0;
35}
36
a71ece51 37int split(char *str, char *arr[MAX_ARGS]){
38 int beginIndex = 0;
39 int endIndex;
40 int maxWords = MAX_ARGS;
41 int wordCnt = 0;
42
43 while(1){
44 while(isspace(str[beginIndex])){
45 ++beginIndex;
46 }
47 if(str[beginIndex] == '\0')
48 break;
49 endIndex = beginIndex;
50 while (str[endIndex] && !isspace(str[endIndex])){
51 ++endIndex;
52 }
53 int len = endIndex - beginIndex;
54 char *tmp = calloc(len + 1, sizeof(char));
55 memcpy(tmp, &str[beginIndex], len);
56 arr[wordCnt++] = tmp;
57 //PrintAndLog("cnt: %d, %s",wordCnt-1, arr[wordCnt-1]);
58 beginIndex = endIndex;
59 if (wordCnt == maxWords)
60 break;
61 }
62 return wordCnt;
63}
9962091e 64
65int CmdCrc(const char *Cmd)
66{
a71ece51 67 char name[] = {"reveng "};
68 char Cmd2[50 + 7];
69 memcpy(Cmd2, name, 7);
70 memcpy(Cmd2 + 7, Cmd, 50);
71 char *argv[MAX_ARGS];
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){
76 //puts(arr[i]);
77 free(argv[i]);
78 }
79
9962091e 80 return 0;
81}
82
60e86577 83int GetModels(char *Models[], int *count, uint32_t *width){
84 /* default values */
85 static model_t model = {
86 PZERO, /* no CRC polynomial, user must specify */
87 PZERO, /* Init = 0 */
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 */
92 };
dd1df490 93
60e86577 94 int ibperhx = 8;//, obperhx = 8;
95 int rflags = 0, uflags = 0; /* search and UI flags */
60e86577 96 poly_t apoly, crc, qpoly = PZERO, *apolys = NULL, *pptr = NULL, *qptr = NULL;
97 model_t pset = model, *candmods, *mptr;
98
99 /* stdin must be binary */
100 #ifdef _WIN32
101 _setmode(STDIN_FILENO, _O_BINARY);
102 #endif /* _WIN32 */
103
104 SETBMP();
105
106 int args = 0, psets, pass;
107 int Cnt = 0;
108 if (*width == 0) { //reveng -D
109 *count = mcount();
60e86577 110 if(!*count)
111 return uerr("no preset models available");
112
113 for(int mode = 0; mode < *count; ++mode) {
114 mbynum(&model, mode);
115 mcanon(&model);
116 size_t size = (model.name && *model.name) ? strlen(model.name) : 6;
60e86577 117 char *tmp = calloc(size+1, sizeof(char));
118 if (tmp==NULL)
119 return uerr("out of memory?");
120
121 memcpy(tmp, model.name, size);
122 Models[mode] = tmp;
123 }
dd1df490 124 mfree(&model);
60e86577 125 } else { //reveng -s
126
127 if(~model.flags & P_MULXN)
128 return uerr("cannot search for non-Williams compliant models");
129
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);
135 else
136 *width = plen(model.spoly);
137
138 /* special case if qpoly is zero, search to end of range */
139 if(!ptst(qpoly))
140 rflags &= ~R_HAVEQ;
141
142 /* if endianness not specified, try
143 * little-endian then big-endian.
144 * NB: crossed-endian algorithms will not be
145 * searched.
146 */
147 /* scan against preset models */
148 if(~uflags & C_FORCE) {
149 pass = 0;
150 Cnt = 0;
151 do {
152 psets = mcount();
153
154 while(psets) {
155 mbynum(&pset, --psets);
156
157 /* skip if different width, or refin or refout don't match */
158 if(plen(pset.spoly) != *width || (model.flags ^ pset.flags) & (P_REFIN | P_REFOUT))
159 continue;
160 /* skip if the preset doesn't match specified parameters */
161 if(rflags & R_HAVEP && pcmp(&model.spoly, &pset.spoly))
162 continue;
163 if(rflags & R_HAVEI && psncmp(&model.init, &pset.init))
164 continue;
165 if(rflags & R_HAVEX && psncmp(&model.xorout, &pset.xorout))
166 continue;
167
168 apoly = pclone(pset.xorout);
169 if(pset.flags & P_REFOUT)
170 prev(&apoly);
171 for(qptr = apolys; qptr < pptr; ++qptr) {
172 crc = pcrc(*qptr, pset.spoly, pset.init, apoly, 0);
173 if(ptst(crc)) {
174 pfree(&crc);
175 break;
176 } else
177 pfree(&crc);
178 }
179 pfree(&apoly);
180 if(qptr == pptr) {
181 /* the selected model solved all arguments */
182 mcanon(&pset);
183
184 size_t size = (pset.name && *pset.name) ? strlen(pset.name) : 6;
185 //PrintAndLog("Size: %d, %s, count: %d",size,pset.name, Cnt);
186 char *tmp = calloc(size+1, sizeof(char));
187
188 if (tmp == NULL){
189 PrintAndLog("out of memory?");
190 return 0;
191 }
192 memcpy(tmp, pset.name, size);
193 Models[Cnt++] = tmp;
194 *count = Cnt;
195 uflags |= C_RESULT;
196 }
197 }
198 mfree(&pset);
199
200 /* toggle refIn/refOut and reflect arguments */
201 if(~rflags & R_HAVERI) {
202 model.flags ^= P_REFIN | P_REFOUT;
203 for(qptr = apolys; qptr < pptr; ++qptr)
204 prevch(qptr, ibperhx);
205 }
206 } while(~rflags & R_HAVERI && ++pass < 2);
207 }
208 if(uflags & C_RESULT) {
209 for(qptr = apolys; qptr < pptr; ++qptr)
210 pfree(qptr);
60e86577 211 }
212 if(!(model.flags & P_REFIN) != !(model.flags & P_REFOUT))
213 return uerr("cannot search for crossed-endian models");
dd1df490 214
60e86577 215 pass = 0;
216 do {
217 mptr = candmods = reveng(&model, qpoly, rflags, args, apolys);
218 if(mptr && plen(mptr->spoly))
219 uflags |= C_RESULT;
220 while(mptr && plen(mptr->spoly)) {
60e86577 221 mfree(mptr++);
222 }
223 free(candmods);
224 if(~rflags & R_HAVERI) {
225 model.flags ^= P_REFIN | P_REFOUT;
226 for(qptr = apolys; qptr < pptr; ++qptr)
227 prevch(qptr, ibperhx);
228 }
229 } while(~rflags & R_HAVERI && ++pass < 2);
230 for(qptr = apolys; qptr < pptr; ++qptr)
231 pfree(qptr);
232 free(apolys);
233 if(~uflags & C_RESULT)
234 return uerr("no models found");
dd1df490 235 mfree(&model);
60e86577 236
60e86577 237 }
238 return 1;
239}
240
241//-c || -v
242//inModel = valid model name string - CRC-8
243//inHexStr = input hex string to calculate crc on
244//reverse = reverse calc option if true
245//endian = {0 = calc default endian input and output, b = big endian input and output, B = big endian output, r = right justified
246// l = little endian input and output, L = little endian output only, t = left justified}
247//result = calculated crc hex string
248int RunModel(char *inModel, char *inHexStr, bool reverse, char endian, char *result){
249 /* default values */
250 static model_t model = {
251 PZERO, // no CRC polynomial, user must specify
252 PZERO, // Init = 0
253 P_BE, // RefIn = false, RefOut = false, plus P_RTJUST setting in reveng.h
254 PZERO, // XorOut = 0
255 PZERO, // check value unused
256 NULL // no model name
257 };
258 int ibperhx = 8, obperhx = 8;
259 int rflags = 0; // search flags
260 int c;
261 unsigned long width = 0UL;
262 poly_t apoly, crc;
263
264 char *string;
265
266 // stdin must be binary
267 #ifdef _WIN32
268 _setmode(STDIN_FILENO, _O_BINARY);
269 #endif /* _WIN32 */
270
271 SETBMP();
272 //set model
273 if(!(c = mbynam(&model, inModel))) {
dd1df490 274 PrintAndLog("error: preset model '%s' not found. Use reveng -D to list presets.", inModel);
60e86577 275 return 0;
276 }
277 if(c < 0)
278 return uerr("no preset models available");
279
280 // must set width so that parameter to -ipx is not zeroed
281 width = plen(model.spoly);
282 rflags |= R_HAVEP | R_HAVEI | R_HAVERI | R_HAVERO | R_HAVEX;
283
284 //set flags
285 switch (endian) {
286 case 'b': /* b big-endian (RefIn = false, RefOut = false ) */
287 model.flags &= ~P_REFIN;
288 rflags |= R_HAVERI;
289 /* fall through: */
290 case 'B': /* B big-endian output (RefOut = false) */
291 model.flags &= ~P_REFOUT;
292 rflags |= R_HAVERO;
293 mnovel(&model);
294 /* fall through: */
295 case 'r': /* r right-justified */
296 model.flags |= P_RTJUST;
297 break;
298 case 'l': /* l little-endian input and output */
299 model.flags |= P_REFIN;
300 rflags |= R_HAVERI;
301 /* fall through: */
302 case 'L': /* L little-endian output */
303 model.flags |= P_REFOUT;
304 rflags |= R_HAVERO;
305 mnovel(&model);
306 /* fall through: */
307 case 't': /* t left-justified */
308 model.flags &= ~P_RTJUST;
309 break;
310 }
311
312 mcanon(&model);
313
314 if (reverse) {
315 // v calculate reversed CRC
316 /* Distinct from the -V switch as this causes
317 * the arguments and output to be reversed as well.
318 */
319 // reciprocate Poly
320 prcp(&model.spoly);
321
322 /* mrev() does:
323 * if(refout) prev(init); else prev(xorout);
324 * but here the entire argument polynomial is
325 * reflected, not just the characters, so RefIn
326 * and RefOut are not inverted as with -V.
327 * Consequently Init is the mirror image of the
328 * one resulting from -V, and so we have:
329 */
330 if(~model.flags & P_REFOUT) {
331 prev(&model.init);
332 prev(&model.xorout);
333 }
334
335 // swap init and xorout
336 apoly = model.init;
337 model.init = model.xorout;
338 model.xorout = apoly;
339 }
340 // c calculate CRC
341
342 /* in the Williams model, xorout is applied after the refout stage.
343 * as refout is part of ptostr(), we reverse xorout here.
344 */
345 if(model.flags & P_REFOUT)
346 prev(&model.xorout);
347
348 apoly = strtop(inHexStr, model.flags, ibperhx);
349
350 if(reverse)
351 prev(&apoly);
352
353 crc = pcrc(apoly, model.spoly, model.init, model.xorout, model.flags);
354
355 if(reverse)
356 prev(&crc);
357
358 string = ptostr(crc, model.flags, obperhx);
359 for (int i = 0; i < 50; i++){
360 result[i] = string[i];
361 if (result[i]==0) break;
362 }
363 free(string);
364 pfree(&crc);
365 pfree(&apoly);
366 return 1;
367}
Impressum, Datenschutz