def test_raises_after_finalize(self, backend): key = b"2b7e151628aed2a6abf7158809cf4f3c" cmac = CMAC(AES(key), backend) cmac.finalize() with pytest.raises(AlreadyFinalized): cmac.update(b"foo") with pytest.raises(AlreadyFinalized): cmac.copy() with pytest.raises(AlreadyFinalized): cmac.finalize()
def test_buffer_protocol(self, backend): key = bytearray(b"2b7e151628aed2a6abf7158809cf4f3c") cmac = CMAC(AES(key), backend) cmac.update(b"6bc1bee22e409f96e93d7e117393172a") assert cmac.finalize() == binascii.unhexlify( b"a21e6e647bfeaf5ca0a5e1bcd957dfad" )
def test_aes_generate(self, backend, params): key = params["key"] message = params["message"] output = params["output"] cmac = CMAC(AES(binascii.unhexlify(key)), backend) cmac.update(binascii.unhexlify(message)) assert binascii.hexlify(cmac.finalize()) == output
def test_3des_generate(self, backend, params): key1 = params["key1"] key2 = params["key2"] key3 = params["key3"] key = key1 + key2 + key3 message = params["message"] output = params["output"] cmac = CMAC(TripleDES(binascii.unhexlify(key)), backend) cmac.update(binascii.unhexlify(message)) assert binascii.hexlify(cmac.finalize()) == output
class _KSP1_encryptor: """ DO NOT CALL Use KSP1().encryptor() instead """ def __init__(self, key, nonce, mac_key): self._aes = Cipher(algorithms.AES(key), modes.CTR(nonce), default_backend()).encryptor() self._mac = CMAC(algorithms.AES(mac_key), default_backend()) def update(self, data): data = self._aes.update(data) self._mac.update(data) return data def finalize(self): return self._mac.finalize()
def mac(key, msg): """ Default MAC function (CMAC using AES-128). Args: key: key for MAC creation. msg: Plaintext to be MACed, as a bytes object. Returns: MAC output, as a bytes object. Raises: ValueError: An error occurred when key is NULL or ciphertext is NULL. """ if key is None: raise ValueError('Key is NULL.') elif msg is None: raise ValueError('Message is NULL.') else: cobj = CMAC(AES(key), backend=default_backend()) cobj.update(msg) return cobj.finalize()
def test_copy_with_backend(self, backend): key = b"2b7e151628aed2a6abf7158809cf4f3c" cmac = CMAC(AES(key), backend) cmac.update(b"6bc1bee22e409f96e93d7e117393172a") copy_cmac = cmac.copy() assert cmac.finalize() == copy_cmac.finalize()
def aes_cmac(key, data): backend = default_backend() cmac = CMAC(algorithms.AES(key), backend=backend) cmac.update(data) return cmac.finalize()