diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/cryptography/hazmat/primitives/ciphers/aead.py | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/cryptography/hazmat/primitives/ciphers/aead.py b/src/cryptography/hazmat/primitives/ciphers/aead.py index 415a45a9..9794d768 100644 --- a/src/cryptography/hazmat/primitives/ciphers/aead.py +++ b/src/cryptography/hazmat/primitives/ciphers/aead.py @@ -12,6 +12,8 @@ from cryptography.hazmat.backends.openssl.backend import backend class ChaCha20Poly1305(object): + _MAX_SIZE = 2 ** 32 + def __init__(self, key): if not backend.aead_cipher_supported(self): raise exceptions.UnsupportedAlgorithm( @@ -33,6 +35,12 @@ class ChaCha20Poly1305(object): if associated_data is None: associated_data = b"" + if len(data) > self._MAX_SIZE or len(associated_data) > self._MAX_SIZE: + # This is OverflowError to match what cffi would raise + raise OverflowError( + "Data or associated data too long. Max 2**32 bytes" + ) + self._check_params(nonce, data, associated_data) return aead._encrypt( backend, self, nonce, data, associated_data, 16 @@ -56,6 +64,8 @@ class ChaCha20Poly1305(object): class AESCCM(object): + _MAX_SIZE = 2 ** 32 + def __init__(self, key, tag_length=16): utils._check_bytes("key", key) if len(key) not in (16, 24, 32): @@ -90,6 +100,12 @@ class AESCCM(object): if associated_data is None: associated_data = b"" + if len(data) > self._MAX_SIZE or len(associated_data) > self._MAX_SIZE: + # This is OverflowError to match what cffi would raise + raise OverflowError( + "Data or associated data too long. Max 2**32 bytes" + ) + self._check_params(nonce, data, associated_data) self._validate_lengths(nonce, len(data)) return aead._encrypt( @@ -121,6 +137,8 @@ class AESCCM(object): class AESGCM(object): + _MAX_SIZE = 2 ** 32 + def __init__(self, key): utils._check_bytes("key", key) if len(key) not in (16, 24, 32): @@ -142,6 +160,12 @@ class AESGCM(object): if associated_data is None: associated_data = b"" + if len(data) > self._MAX_SIZE or len(associated_data) > self._MAX_SIZE: + # This is OverflowError to match what cffi would raise + raise OverflowError( + "Data or associated data too long. Max 2**32 bytes" + ) + self._check_params(nonce, data, associated_data) return aead._encrypt( backend, self, nonce, data, associated_data, 16 |