diff options
| author | Paul Kehrer <paul.l.kehrer@gmail.com> | 2021-11-03 19:28:58 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-11-03 07:28:58 -0400 |
| commit | 4ab8ccea5ba3bf78d8e865954c9eb3c306cef25d (patch) | |
| tree | 5c587f7915602f56e7249107fa8372eea10ff249 | |
| parent | f7e2ec8fe11485f7725b840f811db0d2422e37f5 (diff) | |
| download | cryptography-4ab8ccea5ba3bf78d8e865954c9eb3c306cef25d.tar.gz | |
deprecate backend part 11 of n (#6530)
cipher class
| -rw-r--r-- | src/cryptography/hazmat/primitives/ciphers/base.py | 21 | ||||
| -rw-r--r-- | tests/hazmat/backends/test_openssl.py | 14 | ||||
| -rw-r--r-- | tests/hazmat/primitives/test_ciphers.py | 11 |
3 files changed, 14 insertions, 32 deletions
diff --git a/src/cryptography/hazmat/primitives/ciphers/base.py b/src/cryptography/hazmat/primitives/ciphers/base.py index 0ae519c6d..9b80421c2 100644 --- a/src/cryptography/hazmat/primitives/ciphers/base.py +++ b/src/cryptography/hazmat/primitives/ciphers/base.py @@ -11,11 +11,7 @@ from cryptography.exceptions import ( AlreadyFinalized, AlreadyUpdated, NotYetFinalized, - UnsupportedAlgorithm, - _Reasons, ) -from cryptography.hazmat.backends import _get_backend -from cryptography.hazmat.backends.interfaces import Backend, CipherBackend from cryptography.hazmat.primitives._cipheralgorithm import CipherAlgorithm from cryptography.hazmat.primitives.ciphers import modes @@ -73,14 +69,8 @@ class Cipher(object): self, algorithm: CipherAlgorithm, mode: typing.Optional[modes.Mode], - backend: typing.Optional[Backend] = None, + backend: typing.Any = None, ): - backend = _get_backend(backend) - if not isinstance(backend, CipherBackend): - raise UnsupportedAlgorithm( - "Backend object does not implement CipherBackend.", - _Reasons.BACKEND_MISSING_INTERFACE, - ) if not isinstance(algorithm, CipherAlgorithm): raise TypeError("Expected interface of CipherAlgorithm.") @@ -90,7 +80,6 @@ class Cipher(object): self.algorithm = algorithm self.mode = mode - self._backend = backend def encryptor(self): if isinstance(self.mode, modes.ModeWithAuthenticationTag): @@ -98,13 +87,17 @@ class Cipher(object): raise ValueError( "Authentication tag must be None when encrypting." ) - ctx = self._backend.create_symmetric_encryption_ctx( + from cryptography.hazmat.backends.openssl.backend import backend + + ctx = backend.create_symmetric_encryption_ctx( self.algorithm, self.mode ) return self._wrap_ctx(ctx, encrypt=True) def decryptor(self): - ctx = self._backend.create_symmetric_decryption_ctx( + from cryptography.hazmat.backends.openssl.backend import backend + + ctx = backend.create_symmetric_decryption_ctx( self.algorithm, self.mode ) return self._wrap_ctx(ctx, encrypt=False) diff --git a/tests/hazmat/backends/test_openssl.py b/tests/hazmat/backends/test_openssl.py index d61381d0f..5b793fc9e 100644 --- a/tests/hazmat/backends/test_openssl.py +++ b/tests/hazmat/backends/test_openssl.py @@ -13,7 +13,7 @@ import pytest from cryptography import utils, x509 from cryptography.exceptions import InternalError, _Reasons -from cryptography.hazmat.backends.openssl.backend import Backend, backend +from cryptography.hazmat.backends.openssl.backend import backend from cryptography.hazmat.backends.openssl.ec import _sn_to_elliptic_curve from cryptography.hazmat.primitives import hashes, serialization from cryptography.hazmat.primitives.asymmetric import dh, dsa, padding @@ -82,17 +82,17 @@ class TestOpenSSL(object): backend.register_cipher_adapter(AES, CBC, None) @pytest.mark.parametrize("mode", [DummyMode(), None]) - def test_nonexistent_cipher(self, mode): - b = Backend() - b.register_cipher_adapter( - DummyCipherAlgorithm, - type(mode), + def test_nonexistent_cipher(self, mode, backend, monkeypatch): + # We can't use register_cipher_adapter because backend is a + # global singleton and we want to revert the change after the test + monkeypatch.setitem( + backend._cipher_registry, + (DummyCipherAlgorithm, type(mode)), lambda backend, cipher, mode: backend._ffi.NULL, ) cipher = Cipher( DummyCipherAlgorithm(), mode, - backend=b, ) with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_CIPHER): cipher.encryptor() diff --git a/tests/hazmat/primitives/test_ciphers.py b/tests/hazmat/primitives/test_ciphers.py index bb5d0e8bd..ef577902b 100644 --- a/tests/hazmat/primitives/test_ciphers.py +++ b/tests/hazmat/primitives/test_ciphers.py @@ -198,17 +198,6 @@ class TestSEED(object): SEED("0" * 16) # type: ignore[arg-type] -def test_invalid_backend(): - pretend_backend = object() - - with raises_unsupported_algorithm(_Reasons.BACKEND_MISSING_INTERFACE): - ciphers.Cipher( - AES(b"AAAAAAAAAAAAAAAA"), - modes.ECB(), - pretend_backend, # type: ignore[arg-type] - ) - - def test_invalid_gcm_algorithm(): with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_CIPHER): ciphers.Cipher( |
