def test_copy(self): m = MAC(self.k, b"foo") c = m.copy() m.update(b"barbaz") c.update(b"bar") c.update(b"baz") self.assertSequenceEqual(m.digest(), c.digest())
def wrap_gost(ukm, kek, cek, sbox=DEFAULT_SBOX): """28147-89 key wrapping :param ukm: UKM :type ukm: bytes, 8 bytes :param kek: key encryption key :type kek: bytes, 32 bytes :param cek: content encryption key :type cek: bytes, 32 bytes :returns: wrapped key :rtype: bytes, 44 bytes """ cek_mac = MAC(kek, data=cek, iv=ukm, sbox=sbox).digest()[:4] cek_enc = ecb_encrypt(kek, cek, sbox=sbox) return ukm + cek_enc + cek_mac
def unwrap_gost(kek, data, sbox=DEFAULT_SBOX): """28147-89 key unwrapping :param kek: key encryption key :type kek: bytes, 32 bytes :param data: wrapped key :type data: bytes, 44 bytes :returns: unwrapped CEK :rtype: 32 bytes """ if len(data) != 44: raise ValueError("Invalid data length") ukm, cek_enc, cek_mac = data[:8], data[8:8 + 32], data[-4:] cek = ecb_decrypt(kek, cek_enc, sbox=sbox) if MAC(kek, data=cek, iv=ukm, sbox=sbox).digest()[:4] != cek_mac: raise ValueError("Invalid MAC") return cek
def test_parts(self): m = MAC(self.k) m.update(b"foo") m.update(b"bar") self.assertSequenceEqual(m.digest(), MAC(self.k, b"foobar").digest())
def test_13x(self): self.assertSequenceEqual( MAC(self.k, 13 * b"x").hexdigest(), "917ee1f1a668fbd3", )
def test_128U(self): self.assertSequenceEqual( MAC(self.k, 128 * b"U").hexdigest(), "1a06d1bad74580ef", )
def test_abc(self): self.assertSequenceEqual( MAC(self.k, b"abc").hexdigest(), "28661e40805b1ff9", )
def test_a(self): self.assertSequenceEqual( MAC(self.k, b"a").hexdigest(), "bd5d3b5b2b7b57af", )