def test_crcmod_c8d16_rev(self): data = NumberMask(0xE3F2, 16) inputPoly = NumberMask(0x1F0, 8) regInit = 0x0 xorOut = 0x0 reverse = True crc_func = crcmod.mkCrcFun(inputPoly.masterData(), rev=reverse, initCrc=regInit, xorOut=xorOut) crcLib = crc_func(data.toASCII()) # print "crc: {:X} {:X}".format( crc, crc2 ) crcProc = DivisionCRC() crcProc.setReversed(reverse) crcProc.setXorOutValue(xorOut) crcProc.setRegisterInitValue(regInit) data.reorderBytes() inputPoly.reverse() crc = crcProc.calculate3(data, inputPoly) # print "values: {} poly:{:X} init:{:X} xorOut:{:08b} rev:{} crc:{:08b} crcmod:{:08b} crcxor:{:08b}".format( data, inputPoly, regInit, xorOut, reverse, crc, crcLib, crc^crcLib ) self.assertEqual(crc, crcLib)
def checkCRC(self, dataMask, crcMask, crcKey, retList): ## dataMask: NumberMask ## crcMask: NumberMask ## crcKey: CRCKey ##print "Checking data:", dataMask, crc, crcMaskKey crcSize = crcMask.dataSize crc_forward = self.procFactory.createForwardProcessor(crcSize) crc_forward.setValues(crcKey) polyMask = NumberMask(crcKey.poly, crcSize) polyCRC = crc_forward.calculate3(dataMask, polyMask) if polyCRC == crcMask.dataNum: retList.add(crcKey) ## we assume that if key was found then testing on reversed input will fail return if crcKey.isReversedFully() is False: return #TODO: try to achieve compatibility without reversing ## check reversed input (crcmod compatibility) crc_forward.setInitCRC(crcKey.init, crcSize) revDataMask = dataMask.reorderedBytes() polyMask.reverse() polyCRC = crc_forward.calculate3(revDataMask, polyMask) if polyCRC == crcMask.dataNum: retList.add(crcKey)
def test_generateSubnumbers_2lenB(self): data = NumberMask(0x1, 4) subList = data.generateSubnumbers(3, 0) # print "ret list:", subList self.assertEqual(subList, set([SubNumber(0x1, 3, 0), SubNumber(0x1, 4, 0)]))
def test_crcmod_c8d64_random(self): data = NumberMask(random.randint(1, 0xFFFFFFFFFFFFFFFF), 64) crcSize = 8 crcMax = 2**8 - 1 inputPoly = NumberMask(0x100 | random.randint(1, crcMax), crcSize) ## 'regInit' and 'xorOut' are not incompatible regInit = 0x0 xorOut = 0x0 reverse = bool(random.randint(0, 1)) crc_func = crcmod.mkCrcFun(inputPoly.masterData(), rev=reverse, initCrc=regInit, xorOut=xorOut) crcLib = crc_func(data.toASCII()) # print "crc: {:X} {:X}".format( crc, crc2 ) crcProc = DivisionCRC() crcProc.setReversed(reverse) crcProc.setXorOutValue(xorOut) crcProc.setRegisterInitValue(regInit) # crcProc.setInitCRC( regInit ) if reverse: data.reorderBytes() inputPoly.reverse() crc = crcProc.calculate3(data, inputPoly) # print "values: {} poly:{:X} init:{:X} xorOut:{:08b} rev:{} crc:{:08b} crcmod:{:08b} crcxor:{:08b}".format( data, inputPoly, regInit, xorOut, reverse, crc, crcLib, crc^crcLib ) self.assertEqual(crc, crcLib)
def findPolysXOR(self, data1, crc1, data2, crc2, dataSize, crcSize, searchRange=0): xorData = data1 ^ data2 xorCRC = crc1 ^ crc2 if self.progress: print "Checking {:X} {:X} xor {:X} {:X} = {:X} {:X}, {} {}".format( data1, crc1, data2, crc2, xorData, xorCRC, dataSize, crcSize) xorMask = NumberMask(xorData, dataSize) crcMask = NumberMask(xorCRC, crcSize) retList = [] subList = xorMask.generateSubnumbers(xorMask.dataSize - searchRange, 0) listLen = len(subList) ind = 0 for sub in subList: ind += 1 # print "Checking subnumber {}".format(sub) if self.progress: #print "Checking substring {:X}".format(sub.dataNum) flush_string("{}/{} checking substring {}\n".format( ind, listLen, sub)) subMask = sub.toNumberMask() #TODO: what is initReg and xorVal for self.crcProc??? crcList = [] crcList += self._findBruteForcePoly(subMask, crcMask, False) crcList += self._findBruteForcePoly(subMask, crcMask, True) polyList = [] # List[ PolyKey ] for item in crcList: polyList.append(item.getPolyKey()) polyList += self._findBruteForcePolyReverse(subMask, crcMask) if len(polyList) < 1: continue for key in polyList: key.dataPos = sub.pos key.dataLen = sub.size retList += polyList # print "Found sub:", subRet, sub # if self.progress: # sys.stdout.write("\r") # sys.stdout.flush() return retList
def test_generateSubnumbers_2pos(self): data = NumberMask(0x9, 4) subList = data.generateSubnumbers(maxPos=0) # print "ret list:", subList self.assertEqual( subList, set([ SubNumber(0x1, 1, 0), SubNumber(0x1, 2, 0), SubNumber(0x1, 3, 0), SubNumber(0x9, 4, 0) ]))
def test_generateSubnumbers_1(self): data = NumberMask(0xF, 4) subList = data.generateSubnumbers() # print "ret list:", subList self.assertEqual( subList, set([ SubNumber(0x1, 1, 0), SubNumber(0x3, 2, 0), SubNumber(0x7, 3, 0), SubNumber(0xF, 4, 0) ]))
def test_calculateInitReg_check(self): dataSize = 8 data = NumberMask(0xC2, dataSize) crcSize = 8 inputPoly = NumberMask(0b100011101, crcSize) crc = 0x0F regInit = 0x0 xorOut = 0x0 retList = self.crcBackward.calculateInitReg(data, crc, inputPoly, xorOut) self.assertIn(regInit, retList)
def calculateCRC(self, data, dataSize, poly, crcSize, init=0, xorout=0, reverse=False): self.setReversed(reverse) self.setRegisterInitValue(init) self.setXorOutValue(xorout) dataMask = NumberMask(data, dataSize) polyMask = NumberMask(poly, crcSize) return self.calculate3(dataMask, polyMask)
def test_calculateInitReg_8_002(self): dataSize = 8 data = NumberMask(0xC6, dataSize) crcSize = 8 inputPoly = NumberMask(0b100011101, crcSize) regInit = 0x00 xorOut = 0x00 self.crcProc.setRegisterInitValue(regInit) self.crcProc.setXorOutValue(xorOut) crc = self.crcProc.calculate3(data, inputPoly) retList = self.crcBackward.calculateInitReg(data, crc, inputPoly, xorOut) self.assertIn(regInit, retList)
def __init__(self, inputData): self.inputData = inputData self.items = list() ## List[ (NumberMask, NumberMask) ] inputList = inputData.numbersList dataSize = inputData.dataSize crcSize = inputData.crcSize for num in inputList: data = num[0] crc = num[1] dataMask = NumberMask(data, dataSize) crcMask = NumberMask(crc, crcSize) self.items.append((dataMask, crcMask))
def findCRCKeyBits(self, dataMask, crcNum, crcSize, searchRange): if self.progress: print "Checking {:X} {:X}".format(dataMask.dataNum, crcNum) crcMask = NumberMask(crcNum, crcSize) retList = set() subList = dataMask.generateSubnumbers(dataMask.dataSize - searchRange, 0) for sub in subList: # print "Checking subnumber {}".format(sub) # if self.progress: # print "Checking substring {:X}".format(sub.dataNum) subRet = self.findCRC(sub, crcMask) if len(subRet) < 1: continue for key in subRet: key.dataPos = sub.pos key.dataLen = sub.size retList |= subRet # print "Found sub:", subRet, sub #if self.progress and len(retList)>0: # print "Found keys:", retList return retList
def test_generateSubnumbers_2(self): data = NumberMask(0x9, 4) subList = data.generateSubnumbers() # print "ret list:", subList self.assertEqual( subList, set([ SubNumber(0x0, 1, 1), SubNumber(0x0, 2, 1), SubNumber(0x1, 1, 0), SubNumber(0x1, 2, 0), SubNumber(0x1, 3, 0), SubNumber(0x2, 2, 2), SubNumber(0x4, 3, 1), SubNumber(0x9, 4, 0) ]))
def test_calculateInitReg_c16d16_random(self): dataSize = 16 crcSize = 16 data = NumberMask(random.randint(1, 2**dataSize - 1), dataSize) crcMax = 2**crcSize - 1 inputPoly = NumberMask(random.randint(1, crcMax), crcSize) regInit = random.randint(1, crcMax) xorOut = 0x0 self.crcProc.setRegisterInitValue(regInit) self.crcProc.setXorOutValue(xorOut) crc = self.crcProc.calculate3(data, inputPoly) retList = self.crcBackward.calculateInitReg(data, crc, inputPoly, xorOut) self.assertIn(regInit, retList)
def test_calculateInitReg_8_001(self): dataSize = 8 data = NumberMask(0x01, dataSize) crcSize = 8 inputPoly = NumberMask(0b100000001, crcSize) regInit = 0x01 xorOut = 0x00 self.crcProc.setRegisterInitValue(regInit) self.crcProc.setXorOutValue(xorOut) crc = self.crcProc.calculate3(data, inputPoly) # print "zzzz: 0x%X 0x%X 0x%X" % ( data.dataNum, inputPoly.dataNum, crc ) retList = self.crcBackward.calculateInitReg(data, crc, inputPoly, xorOut) self.assertIn(regInit, retList)
def test_calculate3_004(self): data = NumberMask(0x1000, 16) crcSize = 16 inputPoly = NumberMask(0x10001, crcSize) regInit = 0x0 xorOut = 0x0 reverse = False crcProc = HwCRC() crcProc.setReversed(reverse) crcProc.setXorOutValue(xorOut) crcProc.setInitCRC(regInit, crcSize) crc = crcProc.calculate3(data, inputPoly) # print "values: {} poly:{:X} init:{:X} xorOut:{:08b} rev:{} crc:{:08b} crcmod:{:08b} crcxor:{:08b}".format( data, inputPoly, regInit, xorOut, reverse, crc, crcLib, crc^crcLib ) self.assertEqual(crc, 0x1000)
def test_calculateInitReg_4rev(self): dataSize = 4 data = NumberMask(0x08, dataSize) crcSize = 4 inputPoly = NumberMask(0x12, crcSize) regInit = 0x00 xorOut = 0x00 reverse = True self.crcProc.setReversed(reverse) self.crcProc.setRegisterInitValue(regInit) self.crcProc.setXorOutValue(xorOut) crc = self.crcProc.calculate3(data, inputPoly) self.crcBackward.setReversed(reverse) retList = self.crcBackward.calculateInitReg(data, crc, inputPoly, xorOut) self.assertIn(regInit, retList)
def prepareTable(self, polyMask): lookup = {} crcSize = polyMask.dataSize crcProc = create_processor(crcSize) maxVal = 1 << self.lookupSize for i in range(0, maxVal): data = NumberMask(i, self.lookupSize) lookup[i] = crcProc.calculate3(data, polyMask) return lookup
def test_crcmod_8rohc(self): data = NumberMask(0xF00F, 16) inputPoly = NumberMask(0x107, 8) regInit = 0xFF xorOut = 0x0 crc_func = crcmod.predefined.mkCrcFun('crc-8-rohc') ## rev crcLib = crc_func(data.toASCII()) crcProc = ModCRC() crcProc.setReversed() crcProc.setRegisterInitValue(regInit) crcProc.setXorOutValue(xorOut) crc = crcProc.calculate3(data, inputPoly) # print "crcx: {:b} {:b}".format( crcLib, crc ) self.assertEqual(crc, crcLib)
def test_calculateInitReg_round_c8d8_init_random(self): dataSize = 8 crcSize = 8 data = NumberMask(random.randint(1, 2**dataSize - 1), dataSize) crcMax = 2**crcSize - 1 inputPoly = NumberMask(random.randint(1, crcMax), crcSize) regInit = random.randint(1, crcMax) xorOut = 0x0 reverse = random.randint(1, crcMax) self.crcProc.setReversed(reverse) self.crcProc.setRegisterInitValue(regInit) self.crcProc.setXorOutValue(xorOut) crc = self.crcProc.calculate3(data, inputPoly) self.crcBackward.setReversed(reverse) retList = self.crcBackward.calculateInitReg(data, crc, inputPoly, xorOut) self.assertIn(regInit, retList)
def test_crcmod_8darc(self): data = NumberMask(0x01, 8) inputPoly = NumberMask(0x139, 8) regInit = 0x0 xorOut = 0x0 crc_func = crcmod.predefined.mkCrcFun('crc-8-darc') crcLib = crc_func(data.toASCII()) crcProc = ModCRC() crcProc.setReversed() crcProc.setRegisterInitValue(regInit) crcProc.setXorOutValue(xorOut) revData = data.reorderedBytes() crc = crcProc.calculate3(revData, inputPoly) # print "crcx: {:b} {:b}".format( crc, crcLib ) self.assertEqual(crc, crcLib)
def test_shift(self): crcSize = 8 poly = NumberMask(0b100011101, crcSize) crc = 0x0F cb = DivisionCRCBackwardState(poly, crc) cb.shiftBit(True) self.assertEqual(cb.register, 0b10001001) self.assertEqual(cb.dataMask.dataSize, 1) self.assertEqual(cb.dataMask.dataNum, 0) cb.shiftBit(True) self.assertEqual(cb.register, 0b11001010) ## 202 self.assertEqual(cb.dataMask.dataSize, 2) self.assertEqual(cb.dataMask.dataNum, 0) cb.shiftBit(False) self.assertEqual(cb.register, 0b01100101) self.assertEqual(cb.dataMask.dataSize, 3) self.assertEqual(cb.dataMask.dataNum, 0) cb.shiftBit(True) self.assertEqual(cb.register, 0b10111100) self.assertEqual(cb.dataMask.dataSize, 4) self.assertEqual(cb.dataMask.dataNum, 0) cb.shiftBit(False) ## 6 self.assertEqual(cb.register, 0b01011110) self.assertEqual(cb.dataMask.dataSize, 5) self.assertEqual(cb.dataMask.dataNum, 0) cb.shiftBit(False) ## 6 self.assertEqual(cb.register, 0b00101111) self.assertEqual(cb.dataMask.dataSize, 6) self.assertEqual(cb.dataMask.dataNum, 0) cb.shiftBit(True) ## 5 self.assertEqual(cb.register, 0b10011001) self.assertEqual(cb.dataMask.dataSize, 7) self.assertEqual(cb.dataMask.dataNum, 0) cb.shiftBit(True) ## 4 self.assertEqual(cb.register, 0b11000010) self.assertEqual(cb.dataMask.dataSize, 8) self.assertEqual(cb.dataMask.dataNum, 0) for _ in range(0, 8): cb.shiftBit(False) self.assertEqual(cb.register, 0) self.assertEqual(cb.dataMask.dataSize, 16) self.assertEqual(cb.dataMask.dataNum, 0xC200)
def test_MSB_LSB(self): data = NumberMask(random.randint(1, 0xFF), 8) crcSize = 8 crcMax = 2**crcSize - 1 inputPoly = NumberMask(random.randint(1, crcMax), crcSize) regInit = random.randint(0, crcMax) xorOut = random.randint(0, crcMax) crcProc = DivisionCRC() crcProc.setRegisterInitValue(regInit) crcProc.setXorOutValue(xorOut) crc = crcProc.calculateMSB(data, inputPoly) revData = data.reversed() revPoly = inputPoly.reversed() revRegInit = reverse_number(regInit, crcSize) revXorOut = reverse_number(xorOut, crcSize) revCrcProc = DivisionCRC() revCrcProc.setReversed() revCrcProc.setRegisterInitValue(revRegInit) revCrcProc.setXorOutValue(revXorOut) crc2 = revCrcProc.calculateLSB(revData, revPoly) revCrc = reverse_number(crc2, crcSize) # print "values: data:{} poly:{:X} init:{:X} xorOut:{:08b} crc:{:08b} revcrc:{:08b}".format( data, inputPoly, regInit, xorOut, crc, revCrc ) self.assertEqual(crc, revCrc)
def test_crcmod_8_random(self): data = NumberMask(random.randint(1, 0xFFFFFFFFFFFFFFFF), 64) crcSize = 8 crcMax = 2**crcSize - 1 inputPoly = NumberMask((0x1 << crcSize) | random.randint(1, crcMax), crcSize) # regInit = random.randint(0, crcMax) # xorOut = random.randint(0, crcMax) regInit = 0x0 xorOut = 0x0 reverse = bool(random.randint(0, 1)) crc_func = crcmod.mkCrcFun(inputPoly.masterData(), rev=reverse, initCrc=regInit, xorOut=xorOut) crcLib = crc_func(data.toASCII()) # print "crc: {:X} {:X}".format( crc, crc2 ) crcProc = ModCRC() crcProc.setReversed(reverse) crcProc.setXorOutValue(xorOut) crcProc.setInitCRC(regInit, crcSize) crc = crcProc.calculate3(data, inputPoly) self.assertEqual(crc, crcLib)
def test_calculate3_c16_d80(self): data = NumberMask(0x8E843664A9CB222CE7EC, 80) crcSize = 16 inputPoly = NumberMask(0x1ABCD, crcSize) regInit = 0x0 xorOut = 0x0 reverse = False # masterPoly = inputPoly.masterData() # crc_func = crcmod.mkCrcFun(masterPoly, rev=reverse, initCrc=regInit, xorOut=xorOut) # crcLib = crc_func( data.toASCII() ) # # print "crc: {:X}".format( crcLib ) crcProc = HwCRC() crcProc.setReversed(reverse) crcProc.setXorOutValue(xorOut) crcProc.setInitCRC(regInit, crcSize) crc = crcProc.calculate3(data, inputPoly) # print "values: {} poly:{:X} init:{:X} xorOut:{:08b} rev:{} crc:{:08b} crcmod:{:08b} crcxor:{:08b}".format( data, inputPoly, regInit, xorOut, reverse, crc, crcLib, crc^crcLib ) # self.assertEqual( crc, crcLib ) self.assertEqual(crc, 0xD36F)
def findCommon(self, dataList, dataSize, crcSize, searchRange=0): retList = Counter() if len(dataList) < 1: return retList for i in xrange(0, len(dataList)): dataPair = dataList[i] dataMask = NumberMask(dataPair[0], dataSize) keys = self.findCRCKeyBits(dataMask, dataPair[1], crcSize, searchRange) retList.update(keys) return retList
def test_CRC_random(self): data = NumberMask(random.randint(1, 0xFFFFFFFFFFFFFFFF), 64) crcSize = 8 crcMax = 2**8 - 1 inputPoly = NumberMask(0x100 | random.randint(1, crcMax), crcSize) regInit = random.randint(0, crcMax) xorOut = random.randint(0, crcMax) reverse = bool(random.randint(0, 1)) crc_func = HwCRC() crc_func.setReversed(reverse) crc_func.setRegisterInitValue(regInit) crc_func.setXorOutValue(xorOut) crcLib = crc_func.calculate3(data, inputPoly) crcProc = DivisionCRC() crcProc.setReversed(reverse) crcProc.setRegisterInitValue(regInit) crcProc.setXorOutValue(xorOut) crc = crcProc.calculate3(data, inputPoly) # print "values: {} poly:{:X} init:{:X} xorOut:{:08b} rev:{} crc:{:08b} crcmod:{:08b} crcxor:{:08b}".format( data, inputPoly, regInit, xorOut, reverse, crc, crcLib, crc^crcLib ) # self.assertEqual( crc, crcLib, "Data: {} 0x{:X} 0x{:X} 0x{:X} {}".format(data, inputPoly, regInit, xorOut, reverse ) ) self.assertEqual(crc, crcLib)
def test_CRC(self): data = NumberMask(0xBF, 8) inputPoly = NumberMask(0x130, 8) regInit = 0x0 xorOut = 0x0 reverse = True crcProc = HwCRC() crcProc.setReversed(reverse) crcProc.setRegisterInitValue(regInit) crcProc.setXorOutValue(xorOut) crc = crcProc.calculate3(data, inputPoly) dcrcProc = DivisionCRC() dcrcProc.setReversed(reverse) dcrcProc.setRegisterInitValue(regInit) dcrcProc.setXorOutValue(xorOut) dcrc = dcrcProc.calculate3(data, inputPoly) # crc_func = crcmod.mkCrcFun(inputPoly, rev=reverse, initCrc=regInit, xorOut=xorOut) # crcLib = crc_func( data.toASCII() ) # print "crc: {:X} {:X} {:X} {:X}".format( dcrc, crc, crcLib, inputPoly ) self.assertEqual(dcrc, crc)
def test_crcmod_c16d64_random(self): data = NumberMask(random.randint(1, 0xFFFFFFFFFFFFFFFF), 64) crcSize = 16 crcMax = 2**crcSize - 1 inputPoly = NumberMask((0x1 << crcSize) | random.randint(1, crcMax), crcSize) # regInit = random.randint(0, crcMax) # xorOut = random.randint(0, crcMax) regInit = 0x0 xorOut = 0x0 reverse = bool(random.randint(0, 1)) crc_func = crcmod.mkCrcFun(inputPoly.masterData(), rev=reverse, initCrc=regInit, xorOut=xorOut) crcLib = crc_func(data.toASCII()) # print "crc: {:X} {:X}".format( crc, crc2 ) crcProc = HwCRC() crcProc.setReversed(reverse) crcProc.setXorOutValue(xorOut) if reverse: data.reorderBytes() inputPoly.reverse() crcInit = reverse_number(regInit ^ xorOut, crcSize) crcProc.setRegisterInitValue(crcInit) else: crcInit = regInit ^ xorOut crcProc.setRegisterInitValue(crcInit) crc = crcProc.calculate3(data, inputPoly) # print "values: {} poly:{:X} init:{:X} xorOut:{:08b} rev:{} crc:{:08b} crcmod:{:08b} crcxor:{:08b}".format( data, inputPoly, regInit, xorOut, reverse, crc, crcLib, crc^crcLib ) self.assertEqual(crc, crcLib)
def findBruteForceParams(self, dataCrc1, dataCrc2, polyKey): self.crcProc.setReversed(polyKey.isReversedFully()) # PolyKey crcSize = dataCrc1.crcSize polyMask = NumberMask(polyKey.poly, crcSize) dataMask1 = dataCrc1.dataMask() dataMask2 = dataCrc2.dataMask() paramMax = (0x1 << crcSize) - 1 # 0xFFFF polyList = [] initValStart = 0 initValEnd = paramMax if self.initVal is not None: initValStart = self.initVal initValEnd = initValStart crcMask1 = dataCrc1.crcMask() crcMask2 = dataCrc2.crcMask() inputData = [(dataMask1, crcMask1), (dataMask2, crcMask2)] crc_operator = self.crcProc.createDataOperator(crcSize, inputData) crc_found = crc_operator.verifyRange(polyMask, initValStart, initValEnd, 0, paramMax) for item in crc_found: initReg = item[0] xorVal = item[1] # print "Found CRC - poly: 0x{:X} initVal: 0x{:X} xorVal: 0x{:X}\n".format( polyMask.dataNum, initReg, xorVal ) newKey = CRCKey(polyKey.poly, initReg, xorVal, polyKey.dataPos, polyKey.dataLen, revOrd=polyKey.revOrd, refBits=polyKey.refBits) polyList.append(newKey) if self.progress: sys.stdout.write("\r") sys.stdout.flush() return polyList