Skip to content

Commit

Permalink
raise ValueError on zero length GCM IV (#4348)
Browse files Browse the repository at this point in the history
  • Loading branch information
reaperhulk authored and alex committed Jul 17, 2018
1 parent 7ca0e46 commit 12a1cac
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
3 changes: 2 additions & 1 deletion docs/hazmat/primitives/symmetric-encryption.rst
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,8 @@ Modes
this is ``16``, meaning tag truncation is not allowed. Allowing tag
truncation is strongly discouraged for most applications.

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

:raises NotImplementedError: This is raised if the version of the OpenSSL
backend used is 1.0.1 or earlier.
Expand Down
2 changes: 2 additions & 0 deletions src/cryptography/hazmat/primitives/ciphers/modes.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ def __init__(self, initialization_vector, tag=None, min_tag_length=16):
# for it
if not isinstance(initialization_vector, bytes):
raise TypeError("initialization_vector must be bytes")
if len(initialization_vector) == 0:
raise ValueError("initialization_vector must be at least 1 byte")
self._initialization_vector = initialization_vector
if tag is not None:
if not isinstance(tag, bytes):
Expand Down
4 changes: 4 additions & 0 deletions tests/hazmat/primitives/test_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ def test_ctr(self, backend):
backend,
)

def test_gcm(self):
with pytest.raises(ValueError):
modes.GCM(b"")


class TestModesRequireBytes(object):
def test_cbc(self):
Expand Down

0 comments on commit 12a1cac

Please sign in to comment.