X-Git-Url: http://git.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/blobdiff_plain/37f4270a244eae21f8a423e81b384e62a7a2582d..3775e9e8651ee9d11c11efca550442128f468ee5:/client/cmdcrc.c diff --git a/client/cmdcrc.c b/client/cmdcrc.c index f93ab99d..0ca2b8b1 100644 --- a/client/cmdcrc.c +++ b/client/cmdcrc.c @@ -8,7 +8,6 @@ // CRC Calculations from the software reveng commands //----------------------------------------------------------------------------- -#include #ifdef _WIN32 # include # include @@ -19,8 +18,8 @@ #include #include -//#include -//#include +#include +#include #include "cmdmain.h" #include "cmdcrc.h" #include "reveng/reveng.h" @@ -41,20 +40,21 @@ int split(char *str, char *arr[MAX_ARGS]){ int wordCnt = 0; while(1){ - while(isspace(str[beginIndex])){ + while(isspace((unsigned char)str[beginIndex])){ ++beginIndex; } - if(str[beginIndex] == '\0') + if(str[beginIndex] == '\0') { break; - endIndex = beginIndex; - while (str[endIndex] && !isspace(str[endIndex])){ + } + endIndex = beginIndex; + while (str[endIndex] && !isspace((unsigned char)str[endIndex])){ ++endIndex; } int len = endIndex - beginIndex; char *tmp = calloc(len + 1, sizeof(char)); memcpy(tmp, &str[beginIndex], len); arr[wordCnt++] = tmp; - //PrintAndLog("cnt: %d, %s",wordCnt-1, arr[wordCnt-1]); + //PrintAndLog("DEBUG cnt: %d, %s",wordCnt-1, arr[wordCnt-1]); beginIndex = endIndex; if (wordCnt == maxWords) break; @@ -70,17 +70,23 @@ int CmdCrc(const char *Cmd) memcpy(Cmd2 + 7, Cmd, 50); char *argv[MAX_ARGS]; int argc = split(Cmd2, argv); - //PrintAndLog("argc: %d, %s %s Cmd: %s",argc, argv[0], Cmd2, Cmd); - reveng_main(argc, argv); + + if (argc == 3 && memcmp(argv[1],"-g",2)==0) { + CmdrevengSearch(argv[2]); + } else { + reveng_main(argc, argv); + } + //PrintAndLog("DEBUG argc: %d, %s %s Cmd: %s",argc, argv[0], Cmd2, Cmd); for(int i = 0; i < argc; ++i){ - //puts(arr[i]); free(argv[i]); } return 0; } -int GetModels(char *Models[], int *count, uint32_t *width){ +//returns array of model names and the count of models returning +// as well as a width array for the width of each model +int GetModels(char *Models[], int *count, uint8_t *width){ /* default values */ static model_t model = { PZERO, /* no CRC polynomial, user must specify */ @@ -90,16 +96,11 @@ int GetModels(char *Models[], int *count, uint32_t *width){ PZERO, /* check value unused */ NULL /* no model name */ }; + int ibperhx = 8;//, obperhx = 8; int rflags = 0, uflags = 0; /* search and UI flags */ - - //unsigned long width = 0UL; - //int c, mode = 0, args, psets, pass; poly_t apoly, crc, qpoly = PZERO, *apolys = NULL, *pptr = NULL, *qptr = NULL; model_t pset = model, *candmods, *mptr; - //char *string; - - //myname = argv[0]; /* stdin must be binary */ #ifdef _WIN32 @@ -108,14 +109,10 @@ int GetModels(char *Models[], int *count, uint32_t *width){ SETBMP(); - //pos=0; - //optind=1; - int args = 0, psets, pass; int Cnt = 0; - if (*width == 0) { //reveng -D + if (width[0] == 0) { //reveng -D *count = mcount(); - //PrintAndLog("Count: %d",*count); if(!*count) return uerr("no preset models available"); @@ -123,153 +120,151 @@ int GetModels(char *Models[], int *count, uint32_t *width){ mbynum(&model, mode); mcanon(&model); size_t size = (model.name && *model.name) ? strlen(model.name) : 6; - //PrintAndLog("Size: %d, %s",size,model.name); char *tmp = calloc(size+1, sizeof(char)); if (tmp==NULL) return uerr("out of memory?"); memcpy(tmp, model.name, size); Models[mode] = tmp; - //ufound(&model); + width[mode] = plen(model.spoly); } + mfree(&model); } else { //reveng -s - if(~model.flags & P_MULXN) - return uerr("cannot search for non-Williams compliant models"); + if(~model.flags & P_MULXN) + return uerr("cannot search for non-Williams compliant models"); - praloc(&model.spoly, *width); - praloc(&model.init, *width); - praloc(&model.xorout, *width); - if(!plen(model.spoly)) - palloc(&model.spoly, *width); - else - *width = plen(model.spoly); + praloc(&model.spoly, (unsigned long)width[0]); + praloc(&model.init, (unsigned long)width[0]); + praloc(&model.xorout, (unsigned long)width[0]); + if(!plen(model.spoly)) + palloc(&model.spoly, (unsigned long)width[0]); + else + width[0] = (uint8_t)plen(model.spoly); - /* special case if qpoly is zero, search to end of range */ - if(!ptst(qpoly)) - rflags &= ~R_HAVEQ; + /* special case if qpoly is zero, search to end of range */ + if(!ptst(qpoly)) + rflags &= ~R_HAVEQ; - /* not going to be sending additional args + /* not going to be sending additional args at this time (maybe future?) - // allocate argument array - args = argc - optind; - if(!(apolys = malloc(args * sizeof(poly_t)))) - return uerr("cannot allocate memory for argument list"); + // allocate argument array + args = argc - optind; + if(!(apolys = malloc(args * sizeof(poly_t)))) + return uerr("cannot allocate memory for argument list"); - for(pptr = apolys; optind < argc; ++optind) { - if(uflags & C_INFILE) - *pptr++ = rdpoly(argv[optind], model.flags, ibperhx); - else - *pptr++ = strtop(argv[optind], model.flags, ibperhx); - } - // exit value of pptr is used hereafter! - - */ - - /* if endianness not specified, try - * little-endian then big-endian. - * NB: crossed-endian algorithms will not be - * searched. - */ - /* scan against preset models */ - if(~uflags & C_FORCE) { - pass = 0; - Cnt = 0; - do { - psets = mcount(); - //PrintAndLog("psets: %d",psets); - while(psets) { - mbynum(&pset, --psets); - - /* skip if different width, or refin or refout don't match */ - if(plen(pset.spoly) != *width || (model.flags ^ pset.flags) & (P_REFIN | P_REFOUT)) - continue; - /* skip if the preset doesn't match specified parameters */ - if(rflags & R_HAVEP && pcmp(&model.spoly, &pset.spoly)) - continue; - if(rflags & R_HAVEI && psncmp(&model.init, &pset.init)) - continue; - if(rflags & R_HAVEX && psncmp(&model.xorout, &pset.xorout)) - continue; - - apoly = pclone(pset.xorout); - if(pset.flags & P_REFOUT) - prev(&apoly); - for(qptr = apolys; qptr < pptr; ++qptr) { - crc = pcrc(*qptr, pset.spoly, pset.init, apoly, 0); - if(ptst(crc)) { - pfree(&crc); - break; - } else - pfree(&crc); - } - pfree(&apoly); - if(qptr == pptr) { - /* the selected model solved all arguments */ - mcanon(&pset); - - size_t size = (pset.name && *pset.name) ? strlen(pset.name) : 6; - //PrintAndLog("Size: %d, %s, count: %d",size,pset.name, Cnt); - char *tmp = calloc(size+1, sizeof(char)); - if (tmp==NULL){ - PrintAndLog("out of memory?"); - return 0; - } - memcpy(tmp, pset.name, size); - Models[Cnt++] = tmp; - *count = Cnt; - //ufound(&pset); - uflags |= C_RESULT; - } - } - mfree(&pset); + for(pptr = apolys; optind < argc; ++optind) { + if(uflags & C_INFILE) + *pptr++ = rdpoly(argv[optind], model.flags, ibperhx); + else + *pptr++ = strtop(argv[optind], model.flags, ibperhx); + } + // exit value of pptr is used hereafter! + + */ - /* toggle refIn/refOut and reflect arguments */ - if(~rflags & R_HAVERI) { - model.flags ^= P_REFIN | P_REFOUT; - for(qptr = apolys; qptr < pptr; ++qptr) - prevch(qptr, ibperhx); - } - } while(~rflags & R_HAVERI && ++pass < 2); - } - if(uflags & C_RESULT) { - for(qptr = apolys; qptr < pptr; ++qptr) - pfree(qptr); - return 1; - //exit(EXIT_SUCCESS); - } - if(!(model.flags & P_REFIN) != !(model.flags & P_REFOUT)) - return uerr("cannot search for crossed-endian models"); + /* if endianness not specified, try + * little-endian then big-endian. + * NB: crossed-endian algorithms will not be + * searched. + */ + /* scan against preset models */ + if(~uflags & C_FORCE) { pass = 0; + Cnt = 0; do { - mptr = candmods = reveng(&model, qpoly, rflags, args, apolys); - if(mptr && plen(mptr->spoly)) - uflags |= C_RESULT; - while(mptr && plen(mptr->spoly)) { - /* results were printed by the callback - * string = mtostr(mptr); - * puts(string); - * free(string); - */ - mfree(mptr++); + psets = mcount(); + //PrintAndLog("psets: %d",psets); + while(psets) { + mbynum(&pset, --psets); + + /* skip if different width, or refin or refout don't match */ + if(plen(pset.spoly) != width[0] || (model.flags ^ pset.flags) & (P_REFIN | P_REFOUT)) + continue; + /* skip if the preset doesn't match specified parameters */ + if(rflags & R_HAVEP && pcmp(&model.spoly, &pset.spoly)) + continue; + if(rflags & R_HAVEI && psncmp(&model.init, &pset.init)) + continue; + if(rflags & R_HAVEX && psncmp(&model.xorout, &pset.xorout)) + continue; + + //for additional args (not used yet, maybe future?) + apoly = pclone(pset.xorout); + if(pset.flags & P_REFOUT) + prev(&apoly); + + for(qptr = apolys; qptr < pptr; ++qptr) { + crc = pcrc(*qptr, pset.spoly, pset.init, apoly, 0); + if(ptst(crc)) { + pfree(&crc); + break; + } else + pfree(&crc); + } + pfree(&apoly); + if(qptr == pptr) { + + /* the selected model solved all arguments */ + + mcanon(&pset); + + size_t size = (pset.name && *pset.name) ? strlen(pset.name) : 6; + //PrintAndLog("Size: %d, %s, count: %d",size,pset.name, Cnt); + char *tmp = calloc(size+1, sizeof(char)); + if (tmp==NULL){ + PrintAndLog("out of memory?"); + return 0; + } + width[Cnt] = width[0]; + memcpy(tmp, pset.name, size); + Models[Cnt++] = tmp; + *count = Cnt; + uflags |= C_RESULT; + } } - free(candmods); + mfree(&pset); + + /* toggle refIn/refOut and reflect arguments */ if(~rflags & R_HAVERI) { model.flags ^= P_REFIN | P_REFOUT; for(qptr = apolys; qptr < pptr; ++qptr) prevch(qptr, ibperhx); } } while(~rflags & R_HAVERI && ++pass < 2); + } + //got everything now free the memory... + + if(uflags & C_RESULT) { for(qptr = apolys; qptr < pptr; ++qptr) pfree(qptr); - free(apolys); - if(~uflags & C_RESULT) - return uerr("no models found"); - - + } + if(!(model.flags & P_REFIN) != !(model.flags & P_REFOUT)) + return uerr("cannot search for crossed-endian models"); + + pass = 0; + do { + mptr = candmods = reveng(&model, qpoly, rflags, args, apolys); + if(mptr && plen(mptr->spoly)) + uflags |= C_RESULT; + while(mptr && plen(mptr->spoly)) { + mfree(mptr++); + } + free(candmods); + if(~rflags & R_HAVERI) { + model.flags ^= P_REFIN | P_REFOUT; + for(qptr = apolys; qptr < pptr; ++qptr) + prevch(qptr, ibperhx); + } + } while(~rflags & R_HAVERI && ++pass < 2); + for(qptr = apolys; qptr < pptr; ++qptr) + pfree(qptr); + free(apolys); + if(~uflags & C_RESULT) + return uerr("no models found"); + mfree(&model); } - //PrintAndLog("DONE"); return 1; } @@ -277,25 +272,33 @@ int GetModels(char *Models[], int *count, uint32_t *width){ int CmdrevengTest(const char *Cmd){ char *Models[80]; int count = 0; - uint32_t width = 0; + uint8_t widtharr[80] = {0}; + uint8_t width = 0; width = param_get8(Cmd, 0); //PrintAndLog("width: %d",width); if (width > 89) return uerr("Width cannot exceed 89"); - int ans = GetModels(Models, &count, &width); + widtharr[0] = width; + int ans = GetModels(Models, &count, widtharr); if (!ans) return 0; PrintAndLog("Count: %d",count); for (int i = 0; i < count; i++){ - PrintAndLog("Model %d: %s",i,Models[i]); + PrintAndLog("Model %d: %s, width: %d",i,Models[i], widtharr[i]); free(Models[i]); } return 1; } //-c || -v -int RunModel(char *inModel, char *inHexStr, bool reverse, char *result){ +//inModel = valid model name string - CRC-8 +//inHexStr = input hex string to calculate crc on +//reverse = reverse calc option if true +//endian = {0 = calc default endian input and output, b = big endian input and output, B = big endian output, r = right justified +// l = little endian input and output, L = little endian output only, t = left justified} +//result = calculated crc hex string +int RunModel(char *inModel, char *inHexStr, bool reverse, char endian, char *result){ /* default values */ static model_t model = { PZERO, // no CRC polynomial, user must specify @@ -308,7 +311,7 @@ int RunModel(char *inModel, char *inHexStr, bool reverse, char *result){ int ibperhx = 8, obperhx = 8; int rflags = 0; // search flags int c; - unsigned long width = 0UL; + //unsigned long width; poly_t apoly, crc; char *string; @@ -328,9 +331,37 @@ int RunModel(char *inModel, char *inHexStr, bool reverse, char *result){ return uerr("no preset models available"); // must set width so that parameter to -ipx is not zeroed - width = plen(model.spoly); + //width = plen(model.spoly); rflags |= R_HAVEP | R_HAVEI | R_HAVERI | R_HAVERO | R_HAVEX; + //set flags + switch (endian) { + case 'b': /* b big-endian (RefIn = false, RefOut = false ) */ + model.flags &= ~P_REFIN; + rflags |= R_HAVERI; + /* fall through: */ + case 'B': /* B big-endian output (RefOut = false) */ + model.flags &= ~P_REFOUT; + rflags |= R_HAVERO; + mnovel(&model); + /* fall through: */ + case 'r': /* r right-justified */ + model.flags |= P_RTJUST; + break; + case 'l': /* l little-endian input and output */ + model.flags |= P_REFIN; + rflags |= R_HAVERI; + /* fall through: */ + case 'L': /* L little-endian output */ + model.flags |= P_REFOUT; + rflags |= R_HAVERO; + mnovel(&model); + /* fall through: */ + case 't': /* t left-justified */ + model.flags &= ~P_RTJUST; + break; + } + mcanon(&model); if (reverse) { @@ -402,17 +433,115 @@ int CmdrevengTestC(const char *Cmd){ char inHexStr[30] = {0x00}; char result[30]; int dataLen; - - dataLen = param_getstr(Cmd, cmdp++, inModel); + char endian = 0; + dataLen = param_getstr(Cmd, cmdp++, inModel, sizeof(inModel)); if (dataLen < 4) return 0; - dataLen = param_getstr(Cmd, cmdp++, inHexStr); + dataLen = param_getstr(Cmd, cmdp++, inHexStr, sizeof(inHexStr)); if (dataLen < 4) return 0; - bool reverse = (param_get8(Cmd, cmdp)) ? true : false; + bool reverse = (param_get8(Cmd, cmdp++)) ? true : false; + endian = param_getchar(Cmd, cmdp++); //PrintAndLog("mod: %s, hex: %s, rev %d", inModel, inHexStr, reverse); - int ans = RunModel(inModel, inHexStr, reverse, result); + int ans = RunModel(inModel, inHexStr, reverse, endian, result); if (!ans) return 0; PrintAndLog("Result: %s",result); return 1; } + +//returns a calloced string (needs to be freed) +char *SwapEndianStr(const char *inStr, const size_t len, const uint8_t blockSize){ + char *tmp = calloc(len+1, sizeof(char)); + for (uint8_t block=0; block < (uint8_t)(len/blockSize); block++){ + for (size_t i = 0; i < blockSize; i+=2){ + tmp[i+(blockSize*block)] = inStr[(blockSize-1-i-1)+(blockSize*block)]; + tmp[i+(blockSize*block)+1] = inStr[(blockSize-1-i)+(blockSize*block)]; + } + } + return tmp; +} + +// takes hex string in and searches for a matching result (hex string must include checksum) +int CmdrevengSearch(const char *Cmd){ + char inHexStr[50] = {0x00}; + int dataLen = param_getstr(Cmd, 0, inHexStr, sizeof(inHexStr)); + if (dataLen < 4) return 0; + + char *Models[80]; + int count = 0; + uint8_t width[80]; + width[0] = 0; + uint8_t crcChars = 0; + char result[30]; + char revResult[30]; + int ans = GetModels(Models, &count, width); + bool found = false; + if (!ans) return 0; + + // try each model and get result + for (int i = 0; i < count; i++){ + /*if (found) { + free(Models[i]); + continue; + }*/ + // round up to # of characters in this model's crc + crcChars = ((width[i]+7)/8)*2; + // can't test a model that has more crc digits than our data + if (crcChars >= dataLen) + continue; + memset(result, 0, 30); + char *inCRC = calloc(crcChars+1, sizeof(char)); + memcpy(inCRC, inHexStr+(dataLen-crcChars), crcChars); + + char *outHex = calloc(dataLen-crcChars+1, sizeof(char)); + memcpy(outHex, inHexStr, dataLen-crcChars); + + //PrintAndLog("DEBUG: dataLen: %d, crcChars: %d, Model: %s, CRC: %s, width: %d, outHex: %s",dataLen, crcChars, Models[i], inCRC, width[i], outHex); + ans = RunModel(Models[i], outHex, false, 0, result); + if (ans) { + //test for match + if (memcmp(result, inCRC, crcChars)==0){ + PrintAndLog("\nFound a possible match!\nModel: %s\nValue: %s\n",Models[i], result); + //optional - stop searching if found... + found = true; + } else { + if (crcChars > 2){ + char *swapEndian = SwapEndianStr(result, crcChars, crcChars); + if (memcmp(swapEndian, inCRC, crcChars)==0){ + PrintAndLog("\nFound a possible match!\nModel: %s\nValue EndianSwapped: %s\n",Models[i], swapEndian); + //optional - stop searching if found... + found = true; + } + free(swapEndian); + } + } + } + + //if (!found){ + ans = RunModel(Models[i], outHex, true, 0, revResult); + if (ans) { + //test for match + if (memcmp(revResult, inCRC, crcChars)==0){ + PrintAndLog("\nFound a possible match!\nModel Reversed: %s\nValue: %s\n",Models[i], revResult); + //optional - stop searching if found... + found = true; + } else { + if (crcChars > 2){ + char *swapEndian = SwapEndianStr(revResult, crcChars, crcChars); + if (memcmp(swapEndian, inCRC, crcChars)==0){ + PrintAndLog("\nFound a possible match!\nModel Reversed: %s\nValue EndianSwapped: %s\n",Models[i], swapEndian); + //optional - stop searching if found... + found = true; + } + free(swapEndian); + } + } + } + //} + free(inCRC); + free(outHex); + free(Models[i]); + } + if (!found) PrintAndLog("\nNo matches found\n"); + return 1; +}