Beispiel #1
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)
Beispiel #2
0
    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)
Beispiel #3
0
    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)
Beispiel #4
0
    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)