From eb95cebbb67e1ce954de6b27c7d431753ec422b1 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Tue, 17 Jul 2018 23:08:43 +0800 Subject: [PATCH] add AEAD test --- tests/wycheproof/test_aes.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/wycheproof/test_aes.py b/tests/wycheproof/test_aes.py index f8a33f37f67d..929ad8dc9099 100644 --- a/tests/wycheproof/test_aes.py +++ b/tests/wycheproof/test_aes.py @@ -13,6 +13,7 @@ from cryptography.hazmat.primitives.ciphers import ( Cipher, algorithms, modes ) +from cryptography.hazmat.primitives.ciphers.aead import AESGCM @pytest.mark.requires_backend_interface(interface=CipherBackend) @@ -71,3 +72,25 @@ def test_aes_gcm(backend, wycheproof): assert len(iv) == 0 with pytest.raises(ValueError): Cipher(algorithms.AES(key), modes.GCM(iv), backend) + + +@pytest.mark.requires_backend_interface(interface=CipherBackend) +@pytest.mark.wycheproof_tests("aes_gcm_test.json") +def test_aes_gcm_aead_api(backend, wycheproof): + key = binascii.unhexlify(wycheproof.testcase["key"]) + iv = binascii.unhexlify(wycheproof.testcase["iv"]) + aad = binascii.unhexlify(wycheproof.testcase["aad"]) + msg = binascii.unhexlify(wycheproof.testcase["msg"]) + ct = binascii.unhexlify(wycheproof.testcase["ct"]) + tag = binascii.unhexlify(wycheproof.testcase["tag"]) + aesgcm = AESGCM(key) + if wycheproof.valid or wycheproof.acceptable: + computed_ct = aesgcm.encrypt(iv, msg, aad) + assert computed_ct == ct + tag + computed_msg = aesgcm.decrypt(iv, ct + tag, aad) + assert computed_msg == msg + else: + # All invalid GCM tests are IV len 0 right now + assert len(iv) == 0 + with pytest.raises(ValueError): + aesgcm.encrypt(iv, msg, aad)