Skip to content

Commit db62ec9

Browse files
reaperhulkalex
authored andcommitted
also check iv length for GCM nonce in AEAD (#4350)
* also check iv length for GCM nonce in AEAD * ugh
1 parent 12a1cac commit db62ec9

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

src/cryptography/hazmat/primitives/ciphers/aead.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,5 @@ def _check_params(self, nonce, data, associated_data):
184184
utils._check_bytes("nonce", nonce)
185185
utils._check_bytes("data", data)
186186
utils._check_bytes("associated_data", associated_data)
187+
if len(nonce) == 0:
188+
raise ValueError("Nonce must be at least 1 byte")

tests/hazmat/primitives/test_aead.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,12 @@ def test_params_not_bytes(self, nonce, data, associated_data, backend):
383383
with pytest.raises(TypeError):
384384
aesgcm.decrypt(nonce, data, associated_data)
385385

386+
def test_invalid_nonce_length(self, backend):
387+
key = AESGCM.generate_key(128)
388+
aesgcm = AESGCM(key)
389+
with pytest.raises(ValueError):
390+
aesgcm.encrypt(b"", b"hi", None)
391+
386392
def test_bad_key(self, backend):
387393
with pytest.raises(TypeError):
388394
AESGCM(object())

0 commit comments

Comments
 (0)