Skip to content

Commit 12a1cac

Browse files
reaperhulkalex
authored andcommitted
raise ValueError on zero length GCM IV (#4348)
1 parent 7ca0e46 commit 12a1cac

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

docs/hazmat/primitives/symmetric-encryption.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,8 @@ Modes
399399
this is ``16``, meaning tag truncation is not allowed. Allowing tag
400400
truncation is strongly discouraged for most applications.
401401

402-
:raises ValueError: This is raised if ``len(tag) < min_tag_length``.
402+
:raises ValueError: This is raised if ``len(tag) < min_tag_length`` or the
403+
``initialization_vector`` is too short.
403404

404405
:raises NotImplementedError: This is raised if the version of the OpenSSL
405406
backend used is 1.0.1 or earlier.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ def __init__(self, initialization_vector, tag=None, min_tag_length=16):
208208
# for it
209209
if not isinstance(initialization_vector, bytes):
210210
raise TypeError("initialization_vector must be bytes")
211+
if len(initialization_vector) == 0:
212+
raise ValueError("initialization_vector must be at least 1 byte")
211213
self._initialization_vector = initialization_vector
212214
if tag is not None:
213215
if not isinstance(tag, bytes):

tests/hazmat/primitives/test_block.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,10 @@ def test_ctr(self, backend):
191191
backend,
192192
)
193193

194+
def test_gcm(self):
195+
with pytest.raises(ValueError):
196+
modes.GCM(b"")
197+
194198

195199
class TestModesRequireBytes(object):
196200
def test_cbc(self):

0 commit comments

Comments
 (0)