proxmark3/client/reveng/reveng.c

504 lines
17 KiB
C
Raw Normal View History

/* reveng.c
2019-03-16 14:18:58 +08:00
* Greg Cook, 23/Feb/2019
*/
/* CRC RevEng: arbitrary-precision CRC calculator and algorithm finder
2019-03-16 14:18:58 +08:00
* Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018,
* 2019 Gregory Cook
*
* This file is part of CRC RevEng.
*
* CRC RevEng is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CRC RevEng is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CRC RevEng. If not, see <https://www.gnu.org/licenses/>.
*/
/* 2013-09-16: calini(), calout() work on shortest argument
* 2013-06-11: added sequence number to uprog() calls
* 2013-02-08: added polynomial range search
* 2013-01-18: refactored model checking to pshres(); renamed chkres()
* 2012-05-24: efficiently build Init contribution string
* 2012-05-24: removed broken search for crossed-endian algorithms
* 2012-05-23: rewrote engini() after Ewing; removed modini()
* 2011-01-17: fixed ANSI C warnings
* 2011-01-08: fixed calini(), modini() caters for crossed-endian algos
* 2011-01-04: renamed functions, added calini(), factored pshres();
2019-03-10 06:35:06 +08:00
* rewrote engini() and implemented quick Init search
* 2011-01-01: reveng() initialises terminating entry, addparms()
2019-03-10 06:35:06 +08:00
* initialises all fields
* 2010-12-26: renamed CRC RevEng. right results, rejects polys faster
* 2010-12-24: completed, first tests (unsuccessful)
* 2010-12-21: completed modulate(), partial sketch of reveng()
* 2010-12-19: started reveng
*/
/* reveng() can in theory be modified to search for polynomials shorter
* than the full width as well, but this imposes a heavy time burden on
* the full width search, which is the primary use case, as well as
* complicating the search range function introduced in version 1.1.0.
* It is more effective to search for each shorter width directly.
*/
#include <stdlib.h>
#define FILE void
#include "reveng.h"
static poly_t *modpol(const poly_t init, int rflags, int args, const poly_t *argpolys);
static void engini(int *resc, model_t **result, const poly_t divisor, int flags, int args, const poly_t *argpolys);
static void calout(int *resc, model_t **result, const poly_t divisor, const poly_t init, int flags, int args, const poly_t *argpolys);
static void calini(int *resc, model_t **result, const poly_t divisor, int flags, const poly_t xorout, int args, const poly_t *argpolys);
static void chkres(int *resc, model_t **result, const poly_t divisor, const poly_t init, int flags, const poly_t xorout, int args, const poly_t *argpolys);
static const poly_t pzero = PZERO;
model_t *
reveng(const model_t *guess, const poly_t qpoly, int rflags, int args, const poly_t *argpolys) {
2019-03-10 06:35:06 +08:00
/* Complete the parameters of a model by calculation or brute search. */
poly_t *pworks, *wptr, rem, gpoly;
model_t *result = NULL, *rptr;
int resc = 0;
unsigned long spin = 0, seq = 0;
2019-03-10 07:00:59 +08:00
if (~rflags & R_HAVEP) {
2019-03-10 06:35:06 +08:00
/* The poly is not known.
* Produce a list of differences between the arguments.
*/
pworks = modpol(guess->init, rflags, args, argpolys);
2019-03-10 07:00:59 +08:00
if (!pworks || !plen(*pworks)) {
2019-03-10 06:35:06 +08:00
free(pworks);
goto requit;
}
/* Initialise the guessed poly to the starting value. */
gpoly = pclone(guess->spoly);
/* Clear the least significant term, to be set in the
* loop. qpoly does not need fixing as it is only
* compared with odd polys.
*/
2019-03-10 07:00:59 +08:00
if (plen(gpoly))
2019-03-10 06:35:06 +08:00
pshift(&gpoly, gpoly, 0UL, 0UL, plen(gpoly) - 1UL, 1UL);
2019-03-10 07:00:59 +08:00
while (piter(&gpoly) && (~rflags & R_HAVEQ || pcmp(&gpoly, &qpoly) < 0)) {
2019-03-10 06:35:06 +08:00
/* For each possible poly of this size, try
* dividing all the differences in the list.
*/
2019-03-10 07:00:59 +08:00
if (!(spin++ & R_SPMASK)) {
2019-03-10 06:35:06 +08:00
uprog(gpoly, guess->flags, seq++);
}
2019-03-10 07:00:59 +08:00
for (wptr = pworks; plen(*wptr); ++wptr) {
2019-03-10 06:35:06 +08:00
/* straight divide message by poly, don't multiply by x^n */
rem = pcrc(*wptr, gpoly, pzero, pzero, 0);
2019-03-10 07:00:59 +08:00
if (ptst(rem)) {
2019-03-10 06:35:06 +08:00
pfree(&rem);
break;
} else
pfree(&rem);
}
/* If gpoly divides all the differences, it is a
* candidate. Search for an Init value for this
* poly or if Init is known, log the result.
*/
2019-03-10 07:00:59 +08:00
if (!plen(*wptr)) {
2019-03-10 06:35:06 +08:00
/* gpoly is a candidate poly */
2019-03-10 07:00:59 +08:00
if (rflags & R_HAVEI && rflags & R_HAVEX)
2019-03-10 06:35:06 +08:00
chkres(&resc, &result, gpoly, guess->init, guess->flags, guess->xorout, args, argpolys);
2019-03-10 07:00:59 +08:00
else if (rflags & R_HAVEI)
2019-03-10 06:35:06 +08:00
calout(&resc, &result, gpoly, guess->init, guess->flags, args, argpolys);
2019-03-10 07:00:59 +08:00
else if (rflags & R_HAVEX)
2019-03-10 06:35:06 +08:00
calini(&resc, &result, gpoly, guess->flags, guess->xorout, args, argpolys);
else
engini(&resc, &result, gpoly, guess->flags, args, argpolys);
}
2019-03-10 07:00:59 +08:00
if (!piter(&gpoly))
2019-03-10 06:35:06 +08:00
break;
}
/* Finished with gpoly and the differences list, free them.
*/
pfree(&gpoly);
2019-03-10 07:00:59 +08:00
for (wptr = pworks; plen(*wptr); ++wptr)
2019-03-10 06:35:06 +08:00
pfree(wptr);
free(pworks);
2019-03-10 07:00:59 +08:00
} else if (rflags & R_HAVEI && rflags & R_HAVEX)
2019-03-10 06:35:06 +08:00
/* All parameters are known! Submit the result if we get here */
chkres(&resc, &result, guess->spoly, guess->init, guess->flags, guess->xorout, args, argpolys);
2019-03-10 07:00:59 +08:00
else if (rflags & R_HAVEI)
2019-03-10 06:35:06 +08:00
/* Poly and Init are known, calculate XorOut */
calout(&resc, &result, guess->spoly, guess->init, guess->flags, args, argpolys);
2019-03-10 07:00:59 +08:00
else if (rflags & R_HAVEX)
2019-03-10 06:35:06 +08:00
/* Poly and XorOut are known, calculate Init */
calini(&resc, &result, guess->spoly, guess->flags, guess->xorout, args, argpolys);
else
/* Poly is known but not Init; search for Init. */
engini(&resc, &result, guess->spoly, guess->flags, args, argpolys);
requit:
2019-03-10 07:00:59 +08:00
if (!(result = realloc(result, ++resc * sizeof(model_t)))) {
2019-03-10 06:35:06 +08:00
uerror("cannot reallocate result array");
return NULL;
}
rptr = result + resc - 1;
rptr->spoly = pzero;
rptr->init = pzero;
rptr->flags = 0;
rptr->xorout = pzero;
rptr->check = pzero;
rptr->magic = pzero;
rptr->name = NULL;
2019-03-10 07:00:59 +08:00
return (result);
}
static poly_t *
modpol(const poly_t init, int rflags, int args, const poly_t *argpolys) {
2019-03-10 06:35:06 +08:00
/* Produce, in ascending length order, a list of differences
* between the arguments in the list by summing pairs of arguments.
* If R_HAVEI is not set in rflags, only pairs of equal length are
* summed.
* Otherwise, sums of right-aligned pairs are also returned, with
* the supplied init poly added to the leftmost terms of each
* poly of the pair.
*/
poly_t work, swap, *result, *rptr, *iptr;
const poly_t *aptr, *bptr, *eptr = argpolys + args;
unsigned long alen, blen;
2019-03-10 07:00:59 +08:00
if (args < 2) return (NULL);
2019-03-10 06:35:06 +08:00
result = calloc(((((args - 1) * args) >> 1) + 1) * sizeof(poly_t), sizeof(char));
2019-03-10 07:00:59 +08:00
if (!result)
2019-03-10 06:35:06 +08:00
uerror("cannot allocate memory for codeword table");
rptr = result;
2019-03-10 07:00:59 +08:00
for (aptr = argpolys; aptr < eptr; ++aptr) {
2019-03-10 06:35:06 +08:00
alen = plen(*aptr);
2019-03-10 07:00:59 +08:00
for (bptr = aptr + 1; bptr < eptr; ++bptr) {
2019-03-10 06:35:06 +08:00
blen = plen(*bptr);
2019-03-10 07:00:59 +08:00
if (alen == blen) {
2019-03-10 06:35:06 +08:00
work = pclone(*aptr);
psum(&work, *bptr, 0UL);
2019-03-10 07:00:59 +08:00
} else if (rflags & R_HAVEI && alen < blen) {
2019-03-10 06:35:06 +08:00
work = pclone(*bptr);
psum(&work, *aptr, blen - alen);
psum(&work, init, 0UL);
psum(&work, init, blen - alen);
2019-03-10 07:00:59 +08:00
} else if (rflags & R_HAVEI /* && alen > blen */) {
2019-03-10 06:35:06 +08:00
work = pclone(*aptr);
psum(&work, *bptr, alen - blen);
psum(&work, init, 0UL);
psum(&work, init, alen - blen);
} else
work = pzero;
2019-03-10 07:00:59 +08:00
if (plen(work))
2019-03-10 06:35:06 +08:00
pnorm(&work);
2019-03-10 07:00:59 +08:00
if ((blen = plen(work))) {
2019-03-10 06:35:06 +08:00
/* insert work into result[] in ascending order of length */
2019-03-10 07:00:59 +08:00
for (iptr = result; iptr < rptr; ++iptr) {
if (plen(work) < plen(*iptr)) {
2019-03-10 06:35:06 +08:00
swap = *iptr;
*iptr = work;
work = swap;
2019-03-10 07:00:59 +08:00
} else if (plen(*iptr) == blen && !pcmp(&work, iptr)) {
2019-03-10 06:35:06 +08:00
pfree(&work);
work = *--rptr;
break;
}
}
*rptr++ = work;
}
}
}
*rptr = pzero;
2019-03-10 07:00:59 +08:00
return (result);
}
static void
engini(int *resc, model_t **result, const poly_t divisor, int flags, int args, const poly_t *argpolys) {
2019-03-10 06:35:06 +08:00
/* Search for init values implied by the arguments.
* Method from: Ewing, Gregory C. (March 2010).
* "Reverse-Engineering a CRC Algorithm". Christchurch:
* University of Canterbury.
* <http://www.cosc.canterbury.ac.nz/greg.ewing/essays/
* CRC-Reverse-Engineering.html>
*/
poly_t apoly = PZERO, bpoly, pone = PZERO, *mat, *jptr;
const poly_t *aptr, *bptr, *iptr;
unsigned long alen, blen, dlen, ilen, i, j;
int cy;
dlen = plen(divisor);
/* Allocate the CRC matrix */
mat = (poly_t *) calloc((dlen << 1) * sizeof(poly_t), sizeof(char));
2019-03-10 07:00:59 +08:00
if (!mat)
2019-03-10 06:35:06 +08:00
uerror("cannot allocate memory for CRC matrix");
/* Find arguments of the two shortest lengths */
alen = blen = plen(*(aptr = bptr = iptr = argpolys));
2019-03-10 07:00:59 +08:00
for (++iptr; iptr < argpolys + args; ++iptr) {
2019-03-10 06:35:06 +08:00
ilen = plen(*iptr);
2019-03-10 07:00:59 +08:00
if (ilen < alen) {
bptr = aptr;
blen = alen;
aptr = iptr;
alen = ilen;
} else if (ilen > alen && (aptr == bptr || ilen < blen)) {
bptr = iptr;
blen = ilen;
2019-03-10 06:35:06 +08:00
}
}
2019-03-10 07:00:59 +08:00
if (aptr == bptr) {
2019-03-10 06:35:06 +08:00
/* if no arguments are suitable, calculate Init with an
* assumed XorOut of 0. Create a padded XorOut
*/
palloc(&apoly, dlen);
calini(resc, result, divisor, flags, apoly, args, argpolys);
pfree(&apoly);
free(mat);
return;
}
/* Find the potential contribution of the bottom bit of Init */
palloc(&pone, 1UL);
piter(&pone);
2019-03-10 07:00:59 +08:00
if (blen < (dlen << 1)) {
2019-03-10 06:35:06 +08:00
palloc(&apoly, dlen); /* >= 1 */
psum(&apoly, pone, (dlen << 1) - 1UL - blen); /* >= 0 */
psum(&apoly, pone, (dlen << 1) - 1UL - alen); /* >= 1 */
} else {
palloc(&apoly, blen - dlen + 1UL); /* > dlen */
psum(&apoly, pone, 0UL);
psum(&apoly, pone, blen - alen); /* >= 1 */
}
2019-03-10 07:00:59 +08:00
if (plen(apoly) > dlen) {
2019-03-10 06:35:06 +08:00
mat[dlen] = pcrc(apoly, divisor, pzero, pzero, 0);
pfree(&apoly);
} else {
mat[dlen] = apoly;
}
/* Find the actual contribution of Init */
apoly = pcrc(*aptr, divisor, pzero, pzero, 0);
bpoly = pcrc(*bptr, divisor, pzero, apoly, 0);
/* Populate the matrix */
palloc(&apoly, 1UL);
2019-03-10 07:00:59 +08:00
for (jptr = mat; jptr < mat + dlen; ++jptr)
2019-03-10 06:35:06 +08:00
*jptr = pzero;
2019-03-10 07:00:59 +08:00
for (iptr = jptr++; jptr < mat + (dlen << 1); iptr = jptr++)
* jptr = pcrc(apoly, divisor, *iptr, pzero, P_MULXN);
2019-03-10 06:35:06 +08:00
pfree(&apoly);
/* Transpose the matrix, augment with the Init contribution
* and convert to row echelon form
*/
2019-03-10 07:00:59 +08:00
for (i = 0UL; i < dlen; ++i) {
2019-03-10 06:35:06 +08:00
apoly = pzero;
iptr = mat + (dlen << 1);
2019-03-10 07:00:59 +08:00
for (j = 0UL; j < dlen; ++j)
2019-03-10 06:35:06 +08:00
ppaste(&apoly, *--iptr, i, j, j + 1UL, dlen + 1UL);
2019-03-10 07:00:59 +08:00
if (ptst(apoly))
2019-03-10 06:35:06 +08:00
ppaste(&apoly, bpoly, i, dlen, dlen + 1UL, dlen + 1UL);
j = pfirst(apoly);
2019-03-10 07:00:59 +08:00
while (j < dlen && !pident(mat[j], pzero)) {
2019-03-10 06:35:06 +08:00
psum(&apoly, mat[j], 0UL); /* pfirst(apoly) > j */
j = pfirst(apoly);
}
2019-03-10 07:00:59 +08:00
if (j < dlen)
2019-03-10 06:35:06 +08:00
mat[j] = apoly; /* pident(mat[j], pzero) || pfirst(mat[j]) == j */
else
pfree(&apoly);
}
palloc(&bpoly, dlen + 1UL);
psum(&bpoly, pone, dlen);
/* Iterate through all solutions */
do {
/* Solve the matrix by Gaussian elimination.
* The parity of the result, masked by each row, should be even.
*/
cy = 1;
apoly = pclone(bpoly);
jptr = mat + dlen;
2019-03-10 07:00:59 +08:00
for (i = 0UL; i < dlen; ++i) {
2019-03-10 06:35:06 +08:00
/* Compute next bit of Init */
2019-03-10 07:00:59 +08:00
if (pmpar(apoly, *--jptr))
2019-03-10 06:35:06 +08:00
psum(&apoly, pone, dlen - 1UL - i);
/* Toggle each zero row with carry, for next iteration */
2019-03-10 07:00:59 +08:00
if (cy) {
if (pident(*jptr, pzero)) {
/* 0 to 1, no carry */
*jptr = bpoly;
cy = 0;
} else if (pident(*jptr, bpoly)) {
/* 1 to 0, carry forward */
*jptr = pzero;
}
2019-03-10 06:35:06 +08:00
}
}
/* Trim the augment mask bit */
praloc(&apoly, dlen);
/* Test the Init value and add to results if correct */
calout(resc, result, divisor, apoly, flags, args, argpolys);
pfree(&apoly);
2019-03-10 07:00:59 +08:00
} while (!cy);
2019-03-10 06:35:06 +08:00
pfree(&pone);
pfree(&bpoly);
/* Free the matrix. */
2019-03-10 07:00:59 +08:00
for (jptr = mat; jptr < mat + (dlen << 1); ++jptr)
2019-03-10 06:35:06 +08:00
pfree(jptr);
free(mat);
}
static void
calout(int *resc, model_t **result, const poly_t divisor, const poly_t init, int flags, int args, const poly_t *argpolys) {
2019-03-10 06:35:06 +08:00
/* Calculate Xorout, check it against all the arguments and
* add to results if consistent.
*/
poly_t xorout;
const poly_t *aptr, *iptr;
unsigned long alen, ilen;
2019-03-10 07:00:59 +08:00
if (args < 1) return;
2019-03-10 06:35:06 +08:00
/* find argument of the shortest length */
alen = plen(*(aptr = iptr = argpolys));
2019-03-10 07:00:59 +08:00
for (++iptr; iptr < argpolys + args; ++iptr) {
2019-03-10 06:35:06 +08:00
ilen = plen(*iptr);
2019-03-10 07:00:59 +08:00
if (ilen < alen) {
aptr = iptr;
alen = ilen;
2019-03-10 06:35:06 +08:00
}
}
xorout = pcrc(*aptr, divisor, init, pzero, 0);
/* On little-endian algorithms, the calculations yield
* the reverse of the actual xorout: in the Williams
* model, the refout stage intervenes between init and
* xorout.
*/
2019-03-10 07:00:59 +08:00
if (flags & P_REFOUT)
2019-03-10 06:35:06 +08:00
prev(&xorout);
/* Submit the model to the results table.
* Could skip the shortest argument but we wish to check our
* calculation.
*/
chkres(resc, result, divisor, init, flags, xorout, args, argpolys);
pfree(&xorout);
}
static void
calini(int *resc, model_t **result, const poly_t divisor, int flags, const poly_t xorout, int args, const poly_t *argpolys) {
2019-03-10 06:35:06 +08:00
/* Calculate Init, check it against all the arguments and add to
* results if consistent.
*/
poly_t rcpdiv, rxor, arg, init;
const poly_t *aptr, *iptr;
unsigned long alen, ilen;
2019-03-10 07:00:59 +08:00
if (args < 1) return;
2019-03-10 06:35:06 +08:00
/* find argument of the shortest length */
alen = plen(*(aptr = iptr = argpolys));
2019-03-10 07:00:59 +08:00
for (++iptr; iptr < argpolys + args; ++iptr) {
2019-03-10 06:35:06 +08:00
ilen = plen(*iptr);
2019-03-10 07:00:59 +08:00
if (ilen < alen) {
aptr = iptr;
alen = ilen;
2019-03-10 06:35:06 +08:00
}
}
rcpdiv = pclone(divisor);
prcp(&rcpdiv);
/* If the algorithm is reflected, an ordinary CRC requires the
* model's XorOut to be reversed, as XorOut follows the RefOut
* stage. To reverse the CRC calculation we need rxor to be the
* mirror image of the forward XorOut.
*/
rxor = pclone(xorout);
2019-03-10 07:00:59 +08:00
if (~flags & P_REFOUT)
2019-03-10 06:35:06 +08:00
prev(&rxor);
arg = pclone(*aptr);
prev(&arg);
init = pcrc(arg, rcpdiv, rxor, pzero, 0);
pfree(&arg);
pfree(&rxor);
pfree(&rcpdiv);
prev(&init);
/* Submit the model to the results table.
* Could skip the shortest argument but we wish to check our
* calculation.
*/
chkres(resc, result, divisor, init, flags, xorout, args, argpolys);
pfree(&init);
}
static void
chkres(int *resc, model_t **result, const poly_t divisor, const poly_t init, int flags, const poly_t xorout, int args, const poly_t *argpolys) {
2019-03-10 06:35:06 +08:00
/* Checks a model against the argument list, and adds to the
* external results table if consistent.
* Extends the result array and updates the external pointer if
* necessary.
*/
model_t *rptr;
poly_t xor, crc;
const poly_t *aptr = argpolys, *const eptr = argpolys + args;
/* If the algorithm is reflected, an ordinary CRC requires the
* model's XorOut to be reversed, as XorOut follows the RefOut
* stage.
*/
xor = pclone(xorout);
2019-03-10 07:00:59 +08:00
if (flags & P_REFOUT)
2019-03-10 06:35:06 +08:00
prev(&xor);
2019-03-10 07:00:59 +08:00
for (; aptr < eptr; ++aptr) {
2019-03-10 06:35:06 +08:00
crc = pcrc(*aptr, divisor, init, xor, 0);
2019-03-10 07:00:59 +08:00
if (ptst(crc)) {
2019-03-10 06:35:06 +08:00
pfree(&crc);
break;
} else {
pfree(&crc);
}
}
pfree(&xor);
2019-03-10 07:00:59 +08:00
if (aptr != eptr) return;
2019-03-10 06:35:06 +08:00
*result = realloc(*result, ++*resc * sizeof(model_t));
if (!*result) {
uerror("cannot reallocate result array");
return;
}
rptr = *result + *resc - 1;
rptr->spoly = pclone(divisor);
rptr->init = pclone(init);
rptr->flags = flags;
rptr->xorout = pclone(xorout);
rptr->check = pzero;
rptr->magic = pzero;
rptr->name = NULL;
/* compute check value for this model */
mcheck(rptr);
/* callback to notify new model */
ufound(rptr);
}