diff options
author | Paul Kehrer <paul.l.kehrer@gmail.com> | 2017-07-09 07:34:58 -0500 |
---|---|---|
committer | Alex Gaynor <alex.gaynor@gmail.com> | 2017-07-09 08:34:58 -0400 |
commit | 9d5fc3e5dbe581e1fea9303e684ec9248936df55 (patch) | |
tree | 6a769a585ba9c2c5e121bb55c169dba17a814c77 /src/cryptography/hazmat/backends/openssl/aead.py | |
parent | 0c9aed91697c5bc1eb16c2254406149e2395fdae (diff) | |
download | cryptography-9d5fc3e5dbe581e1fea9303e684ec9248936df55.tar.gz cryptography-9d5fc3e5dbe581e1fea9303e684ec9248936df55.tar.bz2 cryptography-9d5fc3e5dbe581e1fea9303e684ec9248936df55.zip |
use an instance in aead_cipher_supported (#3772)
* use an instance in aead_cipher_supported
* test for chacha20poly1305 compatibility via init exception
* pep8
Diffstat (limited to 'src/cryptography/hazmat/backends/openssl/aead.py')
-rw-r--r-- | src/cryptography/hazmat/backends/openssl/aead.py | 19 |
1 files changed, 8 insertions, 11 deletions
diff --git a/src/cryptography/hazmat/backends/openssl/aead.py b/src/cryptography/hazmat/backends/openssl/aead.py index 53b10e99..4fde6eae 100644 --- a/src/cryptography/hazmat/backends/openssl/aead.py +++ b/src/cryptography/hazmat/backends/openssl/aead.py @@ -11,12 +11,11 @@ _ENCRYPT = 1 _DECRYPT = 0 -def _aead_cipher_name(cls, key_length): +def _aead_cipher_name(cipher): from cryptography.hazmat.primitives.ciphers.aead import ( ChaCha20Poly1305 ) - assert cls is ChaCha20Poly1305 - assert key_length == 32 or key_length is None + assert isinstance(cipher, ChaCha20Poly1305) return b"chacha20-poly1305" @@ -78,11 +77,10 @@ def _process_data(backend, ctx, data): return backend._ffi.buffer(buf, outlen[0])[:] -def _encrypt(backend, cipher_cls, key, nonce, data, associated_data, - tag_length): - cipher_name = _aead_cipher_name(cipher_cls, len(key)) +def _encrypt(backend, cipher, nonce, data, associated_data, tag_length): + cipher_name = _aead_cipher_name(cipher) ctx = _aead_setup( - backend, cipher_name, key, nonce, None, tag_length, _ENCRYPT + backend, cipher_name, cipher._key, nonce, None, tag_length, _ENCRYPT ) _process_aad(backend, ctx, associated_data) @@ -101,15 +99,14 @@ def _encrypt(backend, cipher_cls, key, nonce, data, associated_data, return processed_data + tag -def _decrypt(backend, cipher_cls, key, nonce, data, associated_data, - tag_length): +def _decrypt(backend, cipher, nonce, data, associated_data, tag_length): if len(data) < tag_length: raise InvalidTag tag = data[-tag_length:] data = data[:-tag_length] - cipher_name = _aead_cipher_name(cipher_cls, len(key)) + cipher_name = _aead_cipher_name(cipher) ctx = _aead_setup( - backend, cipher_name, key, nonce, tag, tag_length, _DECRYPT + backend, cipher_name, cipher._key, nonce, tag, tag_length, _DECRYPT ) _process_aad(backend, ctx, associated_data) processed_data = _process_data(backend, ctx, data) |