aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorAlex Gaynor <alex.gaynor@gmail.com>2015-07-01 22:23:14 -0400
committerAlex Gaynor <alex.gaynor@gmail.com>2015-07-01 22:23:14 -0400
commitfed316361545660e6161f9b4981971b5abf72b93 (patch)
tree50d286f6104fbae075b9432d11b70754a4cb5925 /src
parentec3cc9bd730b6799424dc3f69b79d490eaa2f07d (diff)
parent326502a8535e72fe76fdf61762cdf66198370799 (diff)
downloadcryptography-fed316361545660e6161f9b4981971b5abf72b93.tar.gz
cryptography-fed316361545660e6161f9b4981971b5abf72b93.tar.bz2
cryptography-fed316361545660e6161f9b4981971b5abf72b93.zip
Merge pull request #2093 from reaperhulk/gcm-fix-forever-maybe
GCM AAD and encrypted byte limit checks in AEADCipherContext
Diffstat (limited to 'src')
-rw-r--r--src/cryptography/hazmat/primitives/ciphers/base.py19
-rw-r--r--src/cryptography/hazmat/primitives/ciphers/modes.py2
2 files changed, 21 insertions, 0 deletions
diff --git a/src/cryptography/hazmat/primitives/ciphers/base.py b/src/cryptography/hazmat/primitives/ciphers/base.py
index 8f3028fc..dae93655 100644
--- a/src/cryptography/hazmat/primitives/ciphers/base.py
+++ b/src/cryptography/hazmat/primitives/ciphers/base.py
@@ -149,6 +149,8 @@ class _CipherContext(object):
class _AEADCipherContext(object):
def __init__(self, ctx):
self._ctx = ctx
+ self._bytes_processed = 0
+ self._aad_bytes_processed = 0
self._tag = None
self._updated = False
@@ -156,6 +158,14 @@ class _AEADCipherContext(object):
if self._ctx is None:
raise AlreadyFinalized("Context was already finalized.")
self._updated = True
+ self._bytes_processed += len(data)
+ if self._bytes_processed > self._ctx._mode._MAX_ENCRYPTED_BYTES:
+ raise ValueError(
+ "{0} has a maximum encrypted byte limit of {1}".format(
+ self._ctx._mode.name, self._ctx._mode._MAX_ENCRYPTED_BYTES
+ )
+ )
+
return self._ctx.update(data)
def finalize(self):
@@ -171,6 +181,15 @@ class _AEADCipherContext(object):
raise AlreadyFinalized("Context was already finalized.")
if self._updated:
raise AlreadyUpdated("Update has been called on this context.")
+
+ self._aad_bytes_processed += len(data)
+ if self._aad_bytes_processed > self._ctx._mode._MAX_AAD_BYTES:
+ raise ValueError(
+ "{0} has a maximum AAD byte limit of {0}".format(
+ self._ctx._mode.name, self._ctx._mode._MAX_AAD_BYTES
+ )
+ )
+
self._ctx.authenticate_additional_data(data)
diff --git a/src/cryptography/hazmat/primitives/ciphers/modes.py b/src/cryptography/hazmat/primitives/ciphers/modes.py
index e31c9060..4284042d 100644
--- a/src/cryptography/hazmat/primitives/ciphers/modes.py
+++ b/src/cryptography/hazmat/primitives/ciphers/modes.py
@@ -139,6 +139,8 @@ class CTR(object):
@utils.register_interface(ModeWithAuthenticationTag)
class GCM(object):
name = "GCM"
+ _MAX_ENCRYPTED_BYTES = (2 ** 39 - 256) // 8
+ _MAX_AAD_BYTES = (2 ** 64) // 8
def __init__(self, initialization_vector, tag=None, min_tag_length=16):
# len(initialization_vector) must in [1, 2 ** 64), but it's impossible