| 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 | |
| 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 | |
| 20 | #include <stdio.h> |
| 21 | #include <string.h> |
| 22 | //#include <stdlib.h> |
| 23 | //#include <ctype.h> |
| 24 | #include "cmdmain.h" |
| 25 | #include "cmdcrc.h" |
| 26 | #include "reveng/reveng.h" |
| 27 | #include "ui.h" |
| 28 | #include "util.h" |
| 29 | |
| 30 | #define MAX_ARGS 20 |
| 31 | |
| 32 | int uerr(char *msg){ |
| 33 | PrintAndLog("%s",msg); |
| 34 | return 0; |
| 35 | } |
| 36 | |
| 37 | int 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 | } |
| 64 | |
| 65 | int CmdCrc(const char *Cmd) |
| 66 | { |
| 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 | |
| 74 | if (argc == 3 && memcmp(argv[1],"-g",2)==0) { |
| 75 | CmdrevengSearch(argv[2]); |
| 76 | } else { |
| 77 | reveng_main(argc, argv); |
| 78 | } |
| 79 | //PrintAndLog("DEBUG argc: %d, %s %s Cmd: %s",argc, argv[0], Cmd2, Cmd); |
| 80 | for(int i = 0; i < argc; ++i){ |
| 81 | free(argv[i]); |
| 82 | } |
| 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 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){ |
| 90 | /* default values */ |
| 91 | static model_t model = { |
| 92 | PZERO, /* no CRC polynomial, user must specify */ |
| 93 | PZERO, /* Init = 0 */ |
| 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 */ |
| 98 | }; |
| 99 | |
| 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; |
| 104 | |
| 105 | /* stdin must be binary */ |
| 106 | #ifdef _WIN32 |
| 107 | _setmode(STDIN_FILENO, _O_BINARY); |
| 108 | #endif /* _WIN32 */ |
| 109 | |
| 110 | SETBMP(); |
| 111 | |
| 112 | int args = 0, psets, pass; |
| 113 | int Cnt = 0; |
| 114 | if (width[0] == 0) { //reveng -D |
| 115 | *count = mcount(); |
| 116 | if(!*count) |
| 117 | return uerr("no preset models available"); |
| 118 | |
| 119 | for(int mode = 0; mode < *count; ++mode) { |
| 120 | mbynum(&model, mode); |
| 121 | mcanon(&model); |
| 122 | size_t size = (model.name && *model.name) ? strlen(model.name) : 6; |
| 123 | char *tmp = calloc(size+1, sizeof(char)); |
| 124 | if (tmp==NULL) |
| 125 | return uerr("out of memory?"); |
| 126 | |
| 127 | memcpy(tmp, model.name, size); |
| 128 | Models[mode] = tmp; |
| 129 | width[mode] = plen(model.spoly); |
| 130 | } |
| 131 | mfree(&model); |
| 132 | } else { //reveng -s |
| 133 | |
| 134 | if(~model.flags & P_MULXN) |
| 135 | return uerr("cannot search for non-Williams compliant models"); |
| 136 | |
| 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]); |
| 142 | else |
| 143 | width[0] = (uint8_t)plen(model.spoly); |
| 144 | |
| 145 | /* special case if qpoly is zero, search to end of range */ |
| 146 | if(!ptst(qpoly)) |
| 147 | rflags &= ~R_HAVEQ; |
| 148 | |
| 149 | /* if endianness not specified, try |
| 150 | * little-endian then big-endian. |
| 151 | * NB: crossed-endian algorithms will not be |
| 152 | * searched. |
| 153 | */ |
| 154 | /* scan against preset models */ |
| 155 | if(~uflags & C_FORCE) { |
| 156 | pass = 0; |
| 157 | Cnt = 0; |
| 158 | do { |
| 159 | psets = mcount(); |
| 160 | |
| 161 | while(psets) { |
| 162 | mbynum(&pset, --psets); |
| 163 | |
| 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)) |
| 166 | continue; |
| 167 | /* skip if the preset doesn't match specified parameters */ |
| 168 | if(rflags & R_HAVEP && pcmp(&model.spoly, &pset.spoly)) |
| 169 | continue; |
| 170 | if(rflags & R_HAVEI && psncmp(&model.init, &pset.init)) |
| 171 | continue; |
| 172 | if(rflags & R_HAVEX && psncmp(&model.xorout, &pset.xorout)) |
| 173 | continue; |
| 174 | |
| 175 | //for additional args (not used yet, maybe future?) |
| 176 | apoly = pclone(pset.xorout); |
| 177 | if(pset.flags & P_REFOUT) |
| 178 | prev(&apoly); |
| 179 | |
| 180 | for(qptr = apolys; qptr < pptr; ++qptr) { |
| 181 | crc = pcrc(*qptr, pset.spoly, pset.init, apoly, 0); |
| 182 | if(ptst(crc)) { |
| 183 | pfree(&crc); |
| 184 | break; |
| 185 | } else |
| 186 | pfree(&crc); |
| 187 | } |
| 188 | pfree(&apoly); |
| 189 | if(qptr == pptr) { |
| 190 | |
| 191 | /* the selected model solved all arguments */ |
| 192 | |
| 193 | mcanon(&pset); |
| 194 | |
| 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)); |
| 198 | if (tmp == NULL){ |
| 199 | PrintAndLog("out of memory?"); |
| 200 | return 0; |
| 201 | } |
| 202 | width[Cnt] = width[0]; |
| 203 | memcpy(tmp, pset.name, size); |
| 204 | Models[Cnt++] = tmp; |
| 205 | *count = Cnt; |
| 206 | uflags |= C_RESULT; |
| 207 | } |
| 208 | } |
| 209 | mfree(&pset); |
| 210 | |
| 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); |
| 216 | } |
| 217 | } while(~rflags & R_HAVERI && ++pass < 2); |
| 218 | } |
| 219 | //got everything now free the memory... |
| 220 | |
| 221 | if(uflags & C_RESULT) { |
| 222 | for(qptr = apolys; qptr < pptr; ++qptr) |
| 223 | pfree(qptr); |
| 224 | } |
| 225 | if(!(model.flags & P_REFIN) != !(model.flags & P_REFOUT)) |
| 226 | return uerr("cannot search for crossed-endian models"); |
| 227 | |
| 228 | pass = 0; |
| 229 | do { |
| 230 | mptr = candmods = reveng(&model, qpoly, rflags, args, apolys); |
| 231 | if(mptr && plen(mptr->spoly)) |
| 232 | uflags |= C_RESULT; |
| 233 | while(mptr && plen(mptr->spoly)) { |
| 234 | mfree(mptr++); |
| 235 | } |
| 236 | free(candmods); |
| 237 | if(~rflags & R_HAVERI) { |
| 238 | model.flags ^= P_REFIN | P_REFOUT; |
| 239 | for(qptr = apolys; qptr < pptr; ++qptr) |
| 240 | prevch(qptr, ibperhx); |
| 241 | } |
| 242 | } while(~rflags & R_HAVERI && ++pass < 2); |
| 243 | for(qptr = apolys; qptr < pptr; ++qptr) |
| 244 | pfree(qptr); |
| 245 | free(apolys); |
| 246 | if(~uflags & C_RESULT) |
| 247 | return uerr("no models found"); |
| 248 | mfree(&model); |
| 249 | |
| 250 | } |
| 251 | return 1; |
| 252 | } |
| 253 | |
| 254 | //-c || -v |
| 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){ |
| 262 | /* default values */ |
| 263 | static model_t model = { |
| 264 | PZERO, // no CRC polynomial, user must specify |
| 265 | PZERO, // Init = 0 |
| 266 | P_BE, // RefIn = false, RefOut = false, plus P_RTJUST setting in reveng.h |
| 267 | PZERO, // XorOut = 0 |
| 268 | PZERO, // check value unused |
| 269 | NULL // no model name |
| 270 | }; |
| 271 | int ibperhx = 8, obperhx = 8; |
| 272 | int rflags = 0; // search flags |
| 273 | int c; |
| 274 | unsigned long width = 0UL; |
| 275 | poly_t apoly, crc; |
| 276 | |
| 277 | char *string; |
| 278 | |
| 279 | // stdin must be binary |
| 280 | #ifdef _WIN32 |
| 281 | _setmode(STDIN_FILENO, _O_BINARY); |
| 282 | #endif /* _WIN32 */ |
| 283 | |
| 284 | SETBMP(); |
| 285 | //set model |
| 286 | if(!(c = mbynam(&model, inModel))) { |
| 287 | PrintAndLog("error: preset model '%s' not found. Use reveng -D to list presets.", inModel); |
| 288 | return 0; |
| 289 | } |
| 290 | if(c < 0) |
| 291 | return uerr("no preset models available"); |
| 292 | |
| 293 | // must set width so that parameter to -ipx is not zeroed |
| 294 | width = plen(model.spoly); |
| 295 | rflags |= R_HAVEP | R_HAVEI | R_HAVERI | R_HAVERO | R_HAVEX; |
| 296 | |
| 297 | //set flags |
| 298 | switch (endian) { |
| 299 | case 'b': /* b big-endian (RefIn = false, RefOut = false ) */ |
| 300 | model.flags &= ~P_REFIN; |
| 301 | rflags |= R_HAVERI; |
| 302 | /* fall through: */ |
| 303 | case 'B': /* B big-endian output (RefOut = false) */ |
| 304 | model.flags &= ~P_REFOUT; |
| 305 | rflags |= R_HAVERO; |
| 306 | mnovel(&model); |
| 307 | /* fall through: */ |
| 308 | case 'r': /* r right-justified */ |
| 309 | model.flags |= P_RTJUST; |
| 310 | break; |
| 311 | case 'l': /* l little-endian input and output */ |
| 312 | model.flags |= P_REFIN; |
| 313 | rflags |= R_HAVERI; |
| 314 | /* fall through: */ |
| 315 | case 'L': /* L little-endian output */ |
| 316 | model.flags |= P_REFOUT; |
| 317 | rflags |= R_HAVERO; |
| 318 | mnovel(&model); |
| 319 | /* fall through: */ |
| 320 | case 't': /* t left-justified */ |
| 321 | model.flags &= ~P_RTJUST; |
| 322 | break; |
| 323 | } |
| 324 | |
| 325 | mcanon(&model); |
| 326 | |
| 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 | } |
| 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)]; |
| 388 | } |
| 389 | } |
| 390 | return tmp; |
| 391 | } |
| 392 | |
| 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; |
| 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){ |
| 433 | PrintAndLog("\nFound a possible match!\nModel: %s\nValue: %s\n",Models[i], result); |
| 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){ |
| 440 | PrintAndLog("\nFound a possible match!\nModel: %s\nValue EndianSwapped: %s\n",Models[i], swapEndian); |
| 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){ |
| 454 | PrintAndLog("\nFound a possible match!\nModel Reversed: %s\nValue: %s\n",Models[i], revResult); |
| 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){ |
| 461 | PrintAndLog("\nFound a possible match!\nModel Reversed: %s\nValue EndianSwapped: %s\n",Models[i], swapEndian); |
| 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; |
| 476 | } |