aboutsummaryrefslogtreecommitdiffstats
path: root/src/cryptography/hazmat/backends/openssl/ciphers.py
diff options
context:
space:
mode:
authorPhilipp Gesang <phg@phi-gamma.net>2017-05-02 15:28:33 +0200
committerPaul Kehrer <paul.l.kehrer@gmail.com>2017-05-02 08:28:33 -0500
commit2e84daa8e2a3bdcb52750b0589e2ee7ee0fd17ec (patch)
treef9852f1d16b6fa11f524c46343b179c661bebac9 /src/cryptography/hazmat/backends/openssl/ciphers.py
parentcb94281f5b788f583f5f8a5b689dc9dce321ff8e (diff)
downloadcryptography-2e84daa8e2a3bdcb52750b0589e2ee7ee0fd17ec.tar.gz
cryptography-2e84daa8e2a3bdcb52750b0589e2ee7ee0fd17ec.tar.bz2
cryptography-2e84daa8e2a3bdcb52750b0589e2ee7ee0fd17ec.zip
postpone GCM authentication tag requirement until finalization (#3421)
* postpone GCM authentication tag requirement until finalization Add a .finalize_with_tag() variant of the .finalize() function of the GCM context. At the same time, do not enforce the requirement of supplying the tag with the mode ctor. This facilitates streamed decryption when the MAC is appended to the ciphertext and cannot be efficiently retrieved ahead of decryption. According to the GCM spec (section 7.2: “Algorithm for the Authenticated Decryption Function”), the tag itself is not needed until the ciphertext has been decrypted. Addresses #3380 Signed-off-by: Philipp Gesang <philipp.gesang@intra2net.com> * disallow delayed GCM tag passing for legacy OpenSSL Old versions of Ubuntu supported by Cryptography ship a v1.0.1 of OpenSSL which is no longer supported by upstream. This library seems to cause erratic test failures with the delayed GCM tag functionality which are not reproducible outside the CI. Unfortunately OpenSSL v1.0.1 does not even document the required API (``EVP_EncryptInit(3)``) so there is no by-the-book fix. For backends of version 1.0.1 and earlier, verify the GCM tag at the same stage as before. Also, indicate to the user that late passing of GCM tags is unsupported by throwing ``NotImplementedError`` for these backend versions if - the method ``finalize_with_tag()`` is invoked, or - the mode ctor is called without passing a tag. Unit tests have been adapted to account for different backend versions.
Diffstat (limited to 'src/cryptography/hazmat/backends/openssl/ciphers.py')
-rw-r--r--src/cryptography/hazmat/backends/openssl/ciphers.py22
1 files changed, 21 insertions, 1 deletions
diff --git a/src/cryptography/hazmat/backends/openssl/ciphers.py b/src/cryptography/hazmat/backends/openssl/ciphers.py
index 0e0918af..b6058150 100644
--- a/src/cryptography/hazmat/backends/openssl/ciphers.py
+++ b/src/cryptography/hazmat/backends/openssl/ciphers.py
@@ -78,7 +78,13 @@ class _CipherContext(object):
len(iv_nonce), self._backend._ffi.NULL
)
self._backend.openssl_assert(res != 0)
- if operation == self._DECRYPT:
+ if operation == self._DECRYPT and \
+ self._backend.openssl_version_number() < 0x10002000:
+ if mode.tag is None:
+ raise NotImplementedError(
+ "delayed passing of GCM tag requires OpenSSL >= 1.0.2."
+ " To use this feature please update OpenSSL"
+ )
res = self._backend._lib.EVP_CIPHER_CTX_ctrl(
ctx, self._backend._lib.EVP_CTRL_GCM_SET_TAG,
len(mode.tag), mode.tag
@@ -134,6 +140,20 @@ class _CipherContext(object):
if isinstance(self._mode, modes.GCM):
self.update(b"")
+ if self._operation == self._DECRYPT and \
+ isinstance(self._mode, modes.ModeWithAuthenticationTag) and \
+ self._backend.openssl_version_number() >= 0x10002000:
+ tag = self._mode.tag
+ if tag is None:
+ raise ValueError(
+ "Authentication tag must be provided when decrypting."
+ )
+ res = self._backend._lib.EVP_CIPHER_CTX_ctrl(
+ self._ctx, self._backend._lib.EVP_CTRL_GCM_SET_TAG,
+ len(tag), tag
+ )
+ self._backend.openssl_assert(res != 0)
+
buf = self._backend._ffi.new("unsigned char[]", self._block_size_bytes)
outlen = self._backend._ffi.new("int *")
res = self._backend._lib.EVP_CipherFinal_ex(self._ctx, buf, outlen)