Skip to content

Commit 4de0049

Browse files
reaperhulkalex
authored andcommitted
add wycheproof gcm tests (#4349)
* add wycheproof gcm tests * add AEAD test
1 parent c563b57 commit 4de0049

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

tests/wycheproof/test_aes.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from cryptography.hazmat.primitives.ciphers import (
1414
Cipher, algorithms, modes
1515
)
16+
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
1617

1718

1819
@pytest.mark.requires_backend_interface(interface=CipherBackend)
@@ -40,3 +41,56 @@ def test_aes_cbc_pkcs5(backend, wycheproof):
4041
assert computed_ct != ct
4142
with pytest.raises(ValueError):
4243
unpadder.update(padded_msg) + unpadder.finalize()
44+
45+
46+
@pytest.mark.requires_backend_interface(interface=CipherBackend)
47+
@pytest.mark.wycheproof_tests("aes_gcm_test.json")
48+
def test_aes_gcm(backend, wycheproof):
49+
key = binascii.unhexlify(wycheproof.testcase["key"])
50+
iv = binascii.unhexlify(wycheproof.testcase["iv"])
51+
aad = binascii.unhexlify(wycheproof.testcase["aad"])
52+
msg = binascii.unhexlify(wycheproof.testcase["msg"])
53+
ct = binascii.unhexlify(wycheproof.testcase["ct"])
54+
tag = binascii.unhexlify(wycheproof.testcase["tag"])
55+
if wycheproof.valid or wycheproof.acceptable:
56+
enc = Cipher(algorithms.AES(key), modes.GCM(iv), backend).encryptor()
57+
enc.authenticate_additional_data(aad)
58+
computed_ct = enc.update(msg) + enc.finalize()
59+
computed_tag = enc.tag
60+
assert computed_ct == ct
61+
assert computed_tag == tag
62+
dec = Cipher(
63+
algorithms.AES(key),
64+
modes.GCM(iv, tag, min_tag_length=len(tag)),
65+
backend
66+
).decryptor()
67+
dec.authenticate_additional_data(aad)
68+
computed_msg = dec.update(ct) + dec.finalize()
69+
assert computed_msg == msg
70+
else:
71+
# All invalid GCM tests are IV len 0 right now
72+
assert len(iv) == 0
73+
with pytest.raises(ValueError):
74+
Cipher(algorithms.AES(key), modes.GCM(iv), backend)
75+
76+
77+
@pytest.mark.requires_backend_interface(interface=CipherBackend)
78+
@pytest.mark.wycheproof_tests("aes_gcm_test.json")
79+
def test_aes_gcm_aead_api(backend, wycheproof):
80+
key = binascii.unhexlify(wycheproof.testcase["key"])
81+
iv = binascii.unhexlify(wycheproof.testcase["iv"])
82+
aad = binascii.unhexlify(wycheproof.testcase["aad"])
83+
msg = binascii.unhexlify(wycheproof.testcase["msg"])
84+
ct = binascii.unhexlify(wycheproof.testcase["ct"])
85+
tag = binascii.unhexlify(wycheproof.testcase["tag"])
86+
aesgcm = AESGCM(key)
87+
if wycheproof.valid or wycheproof.acceptable:
88+
computed_ct = aesgcm.encrypt(iv, msg, aad)
89+
assert computed_ct == ct + tag
90+
computed_msg = aesgcm.decrypt(iv, ct + tag, aad)
91+
assert computed_msg == msg
92+
else:
93+
# All invalid GCM tests are IV len 0 right now
94+
assert len(iv) == 0
95+
with pytest.raises(ValueError):
96+
aesgcm.encrypt(iv, msg, aad)

0 commit comments

Comments
 (0)