def test_encryption_and_signing(): message = "Some arbitrary message" mac_key_1 = key.with_length(256) mac_key_2 = key.with_length(256) cbc_key = key.with_length(256) authenticated_encryption = Authenticated_Encryption((mac_key_1, mac_key_2, cbc_key)) mac = CBC_MAC((mac_key_1, mac_key_2)) cbc = AES_CBC(cbc_key) cipher, tag = authenticated_encryption.encrypt_and_sign(message) assert_equal(message, cbc.decrypt(cipher)) assert_equal(tag, mac.sign(cipher))
class Authenticated_Encryption(object): def __init__(self, keys): self.mac = CBC_MAC((keys[0], keys[1])) self.aes_cbc = AES_CBC(keys[2]) def encrypt_and_sign(self, message): cipher = self.aes_cbc.encrypt(message) tag = self.mac.sign(cipher) return cipher, tag def authenticate_and_decrypt(self, cipher, tag): authenticated = self.mac.verify(cipher, tag) message = "" if not authenticated else self.aes_cbc.decrypt(cipher) return authenticated, message
def __init__(self, keys): self.mac = CBC_MAC((keys[0], keys[1])) self.aes_cbc = AES_CBC(keys[2])