88
99import pytest
1010
11+ from cryptography .exceptions import InvalidTag
1112from cryptography .hazmat .backends .interfaces import CipherBackend
1213from cryptography .hazmat .primitives import padding
1314from cryptography .hazmat .primitives .ciphers import (
@@ -67,11 +68,19 @@ def test_aes_gcm(backend, wycheproof):
6768 dec .authenticate_additional_data (aad )
6869 computed_msg = dec .update (ct ) + dec .finalize ()
6970 assert computed_msg == msg
70- else :
71- # All invalid GCM tests are IV len 0 right now
72- assert len (iv ) == 0
71+ elif len (iv ) == 0 :
7372 with pytest .raises (ValueError ):
7473 Cipher (algorithms .AES (key ), modes .GCM (iv ), backend )
74+ else :
75+ dec = Cipher (
76+ algorithms .AES (key ),
77+ modes .GCM (iv , tag , min_tag_length = len (tag )),
78+ backend
79+ ).decryptor ()
80+ dec .authenticate_additional_data (aad )
81+ dec .update (ct )
82+ with pytest .raises (InvalidTag ):
83+ dec .finalize ()
7584
7685
7786@pytest .mark .requires_backend_interface (interface = CipherBackend )
@@ -89,8 +98,9 @@ def test_aes_gcm_aead_api(backend, wycheproof):
8998 assert computed_ct == ct + tag
9099 computed_msg = aesgcm .decrypt (iv , ct + tag , aad )
91100 assert computed_msg == msg
92- else :
93- # All invalid GCM tests are IV len 0 right now
94- assert len (iv ) == 0
101+ elif len (iv ) == 0 :
95102 with pytest .raises (ValueError ):
96103 aesgcm .encrypt (iv , msg , aad )
104+ else :
105+ with pytest .raises (InvalidTag ):
106+ aesgcm .decrypt (iv , ct + tag , aad )
0 commit comments