Skip to content

Commit

Permalink
Simplify the code in the AEAD test (#6638)
Browse files Browse the repository at this point in the history
Only unhexlify each thing once
  • Loading branch information
alex committed Nov 20, 2021
1 parent 8be0f79 commit b14285e
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions tests/hazmat/primitives/utils.py
Expand Up @@ -107,9 +107,9 @@ def aead_test(backend, cipher_factory, mode_factory, params):
pytest.skip("Non-96-bit IVs unsupported in FIPS mode.")

if params.get("pt") is not None:
plaintext = params["pt"]
ciphertext = params["ct"]
aad = params["aad"]
plaintext = binascii.unhexlify(params["pt"])
ciphertext = binascii.unhexlify(params["ct"])
aad = binascii.unhexlify(params["aad"])
if params.get("fail") is True:
cipher = Cipher(
cipher_factory(binascii.unhexlify(params["key"])),
Expand All @@ -121,8 +121,8 @@ def aead_test(backend, cipher_factory, mode_factory, params):
backend,
)
decryptor = cipher.decryptor()
decryptor.authenticate_additional_data(binascii.unhexlify(aad))
actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext))
decryptor.authenticate_additional_data(aad)
actual_plaintext = decryptor.update(ciphertext)
with pytest.raises(InvalidTag):
decryptor.finalize()
else:
Expand All @@ -132,8 +132,8 @@ def aead_test(backend, cipher_factory, mode_factory, params):
backend,
)
encryptor = cipher.encryptor()
encryptor.authenticate_additional_data(binascii.unhexlify(aad))
actual_ciphertext = encryptor.update(binascii.unhexlify(plaintext))
encryptor.authenticate_additional_data(aad)
actual_ciphertext = encryptor.update(plaintext)
actual_ciphertext += encryptor.finalize()
tag_len = len(binascii.unhexlify(params["tag"]))
assert binascii.hexlify(encryptor.tag[:tag_len]) == params["tag"]
Expand All @@ -147,10 +147,10 @@ def aead_test(backend, cipher_factory, mode_factory, params):
backend,
)
decryptor = cipher.decryptor()
decryptor.authenticate_additional_data(binascii.unhexlify(aad))
actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext))
decryptor.authenticate_additional_data(aad)
actual_plaintext = decryptor.update(ciphertext)
actual_plaintext += decryptor.finalize()
assert actual_plaintext == binascii.unhexlify(plaintext)
assert actual_plaintext == plaintext


def generate_stream_encryption_test(
Expand Down

0 comments on commit b14285e

Please sign in to comment.