#include static unsigned long crctable[256]; static unsigned char crcinvtable[256]; void calc_crc_table(void) { unsigned long poly = 0xedb88320, value, idx, i; for (idx = 0 ; idx < 256 ; idx++) { for (value = idx, i = 0 ; i < 8 ; i++) { if (value & 1) { value >>= 1; value ^= poly; } else { value >>= 1; } } crctable[idx] = value; crcinvtable[value >> 24] = idx & 0xff; } } static inline unsigned long iter_crc(unsigned long crc, unsigned char chr) { return (crc >> 8) ^ crctable[chr ^ (crc & 255)]; } unsigned long calc_crc(char *buf, unsigned long buflen) { unsigned long crc = 0xffffffff; while (buflen--) crc = iter_crc(crc, *buf++); return ~crc; } static inline unsigned char l2c(unsigned long l, int nr) { return (l >> (8*(nr-1))) & 0xff; } void showmatch(unsigned char *b1, unsigned char *b2, unsigned char a, unsigned char b, unsigned char c, unsigned char d, unsigned char e) { unsigned long i; printf("match with ["); b1[10] = a; b1[12] = b; b1[14] = c; b1[16] = d; b1[18] = e; b2[10] = b2[12] = b2[14] = b2[16] = b2[18] = 0; for (i=0 ; i<19 ; i++) printf("0x%.2x, ", b1[i] ^ b2[i]); printf("]\n"); fflush(stdout); _exit(0); } void bf(unsigned char *buffer, unsigned char *ref) { unsigned long i, shtarget, sh0, sh1, sh2, sh3, sh4; unsigned char a, b, c, d, e, targethi; shtarget = 0xffffffff; for (i=0 ; i<19 ; i++) shtarget = iter_crc(shtarget, 0); targethi = shtarget >> 24; sh0 = 0xffffffff; for (i=0 ; i<10 ; i++) sh0 = iter_crc(sh0, buffer[i] ^ ref[i]); for (a=0 ; a<128 ; a++) { sh1 = iter_crc(iter_crc(sh0, a), buffer[11] ^ ref[11]); for (b=0 ; b<128 ; b++) { sh2 = iter_crc(iter_crc(sh1, b), buffer[13] ^ ref[13]); for (c=0 ; c<128 ; c++) { sh3 = iter_crc(iter_crc(sh2, c), buffer[15] ^ ref[15]); for (d=0 ; d<128 ; d++) { sh4 = iter_crc(iter_crc(sh3, d), buffer[17] ^ ref[17]); e = crcinvtable[targethi] ^ (sh4 & 255); sh4 = iter_crc(sh4, e); if (sh4 == shtarget) showmatch(buffer, ref, a, b, c, d, e); } } } } } int main(int argc, char **argv) { calc_crc_table(); if (argc < 3) _exit(0); bf(argv[1], argv[2]); return 0; }