Beispiel #1
0
def test_cmac_subkeys():
    k = binascii.unhexlify("8195088CE6C393708EBBE6C7914ECB0B")
    kx = binascii.unhexlify("2D22571A33B2965A9B49FF4395A43046")

    k0 = LRP.eval_lrp(LRP.generate_plaintexts(k),
                      LRP.generate_updated_keys(k)[0], b"\x00" * 16, True)
    assert (_Element(k0) * _Element(4)).encode().hex() == kx.hex()
Beispiel #2
0
    def test4(self):
        # Test inversion
        one = _Element(1)

        x = one.inverse()
        self.assertEqual(int(x), 1)

        x = _Element(82323923)
        y = x.inverse()
        self.assertEqual(int(x * y), 1)
    def test4(self):
        # Test inversion
        one = _Element(1)

        x = one.inverse()
        self.assertEqual(int(x), 1)

        x = _Element(82323923)
        y = x.inverse()
        self.assertEqual(int(x * y), 1)
Beispiel #4
0
    def test1(self):
        # Test encondings
        e = _Element(256)
        self.assertEqual(int(e), 256)
        self.assertEqual(e.encode(), bchr(0) * 14 + b("\x01\x00"))

        e = _Element(bchr(0) * 14 + b("\x01\x10"))
        self.assertEqual(int(e), 0x110)
        self.assertEqual(e.encode(), bchr(0) * 14 + b("\x01\x10"))

        # Only 16 byte string are a valid encoding
        self.assertRaises(ValueError, _Element, bchr(0))
    def test1(self):
        # Test encondings
        e = _Element(256)
        self.assertEqual(int(e), 256)
        self.assertEqual(e.encode(), bchr(0)*14 + b("\x01\x00"))

        e = _Element(bchr(0)*14 + b("\x01\x10"))
        self.assertEqual(int(e), 0x110)
        self.assertEqual(e.encode(), bchr(0)*14 + b("\x01\x10"))

        # Only 16 byte string are a valid encoding
        self.assertRaises(ValueError, _Element, bchr(0))
Beispiel #6
0
    def test3(self):
        # Test multiplication
        zero = _Element(0)
        one = _Element(1)
        two = _Element(2)

        x = _Element(6) * zero
        self.assertEqual(int(x), 0)

        x = _Element(6) * one
        self.assertEqual(int(x), 6)

        x = _Element(2L**127) * two
        self.assertEqual(int(x), 1 + 2 + 4 + 128)
Beispiel #7
0
    def cmac(self, data: bytes) -> bytes:
        """
        Calculate CMAC_LRP
        (Huge thanks to @Pharisaeus for help with polynomial math.)
        :param data: message to be authenticated
        :return: CMAC result
        """
        stream = io.BytesIO(data)

        k0 = LRP.eval_lrp(self.p, self.kp, b"\x00" * 16, True)
        k1 = (_Element(k0) * _Element(2)).encode()
        k2 = (_Element(k0) * _Element(4)).encode()

        y = b"\x00" * AES.block_size

        while True:
            x = stream.read(AES.block_size)

            if len(x) < AES.block_size or stream.tell() == stream.getbuffer(
            ).nbytes:
                break

            y = strxor(x, y)
            y = LRP.eval_lrp(self.p, self.kp, y, True)

        pad_bytes = 0

        if len(x) < AES.block_size:
            pad_bytes = AES.block_size - len(x)
            x = x + b"\x80" + (b"\x00" * (pad_bytes - 1))

        y = strxor(x, y)

        if not pad_bytes:
            y = strxor(y, k1)
        else:
            y = strxor(y, k2)

        return LRP.eval_lrp(self.p, self.kp, y, True)
Beispiel #8
0
 def test2(self):
     # Test addition
     e = _Element(0x10)
     f = _Element(0x0A)
     self.assertEqual(int(e + f), 0x1A)
 def test2(self):
     # Test addition
     e = _Element(0x10)
     f = _Element(0x0A)
     self.assertEqual(int(e+f), 0x1A)