aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaul Kehrer <paul.l.kehrer@gmail.com>2013-10-19 17:23:58 -0500
committerPaul Kehrer <paul.l.kehrer@gmail.com>2013-10-21 08:50:09 -0500
commitafe8a896de570ab3135938a8e756bfa055fde241 (patch)
tree35797bb8e415c9e617320dd60dd9e7ed350a4778
parentdec09fc490a09785744e4885f705f231c7ba1eec (diff)
downloadcryptography-afe8a896de570ab3135938a8e756bfa055fde241.tar.gz
cryptography-afe8a896de570ab3135938a8e756bfa055fde241.tar.bz2
cryptography-afe8a896de570ab3135938a8e756bfa055fde241.zip
modified approach to encryption/decryption contexts
-rw-r--r--cryptography/bindings/openssl/api.py1
-rw-r--r--cryptography/primitives/block/base.py46
2 files changed, 25 insertions, 22 deletions
diff --git a/cryptography/bindings/openssl/api.py b/cryptography/bindings/openssl/api.py
index fdac4e91..fc61c4b8 100644
--- a/cryptography/bindings/openssl/api.py
+++ b/cryptography/bindings/openssl/api.py
@@ -208,4 +208,5 @@ class API(object):
assert res != 0
return copied_ctx
+
api = API()
diff --git a/cryptography/primitives/block/base.py b/cryptography/primitives/block/base.py
index b6f45778..adade3ca 100644
--- a/cryptography/primitives/block/base.py
+++ b/cryptography/primitives/block/base.py
@@ -44,37 +44,39 @@ class _BlockCipherContext(six.with_metaclass(abc.ABCMeta)):
self.cipher = cipher
self.mode = mode
self._api = api
- if isinstance(self, _BlockCipherEncryptionContext):
- ctx_method = self._api.create_block_cipher_encrypt_context
- else:
- ctx_method = self._api.create_block_cipher_decrypt_context
- self._ctx = ctx_method(self.cipher, self.mode)
- def finalize(self):
+ def _check_ctx(self):
if self._ctx is None:
raise ValueError("Context was already finalized")
- if isinstance(self, _BlockCipherEncryptionContext):
- result = self._api.finalize_encrypt_context(self._ctx)
- else:
- result = self._api.finalize_decrypt_context(self._ctx)
- self._ctx = None
- return result
+class _BlockCipherEncryptionContext(_BlockCipherContext):
+ def __init__(self, cipher, mode, api):
+ super(_BlockCipherEncryptionContext, self).__init__(cipher, mode, api)
+ self._ctx = self._api.create_block_cipher_encrypt_context(cipher, mode)
def update(self, data):
- if self._ctx is None:
- raise ValueError("Context was already finalized")
+ self._check_ctx()
+ return self._api.update_encrypt_context(self._ctx, data)
- if isinstance(self, _BlockCipherEncryptionContext):
- return self._api.update_encrypt_context(self._ctx, data)
- else:
- return self._api.update_decrypt_context(self._ctx, data)
+ def finalize(self):
+ self._check_ctx()
+ data = self._api.finalize_encrypt_context(self._ctx)
+ self._ctx = None
+ return data
-class _BlockCipherEncryptionContext(_BlockCipherContext):
- pass
+class _BlockCipherDecryptionContext(_BlockCipherContext):
+ def __init__(self, cipher, mode, api):
+ super(_BlockCipherDecryptionContext, self).__init__(cipher, mode, api)
+ self._ctx = self._api.create_block_cipher_decrypt_context(cipher, mode)
+ def update(self, data):
+ self._check_ctx()
+ return self._api.update_decrypt_context(self._ctx, data)
-class _BlockCipherDecryptionContext(_BlockCipherContext):
- pass
+ def finalize(self):
+ self._check_ctx()
+ data = self._api.finalize_decrypt_context(self._ctx)
+ self._ctx = None
+ return data