Skip to content

Commit d09e1ed

Browse files
authored
Final polish on removing backends from places they aren't required (#6644)
1 parent 5bd0c10 commit d09e1ed

File tree

5 files changed

+12
-49
lines changed

5 files changed

+12
-49
lines changed

src/cryptography/fernet.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
from cryptography import utils
1414
from cryptography.exceptions import InvalidSignature
15-
from cryptography.hazmat.backends import _get_backend
16-
from cryptography.hazmat.backends.interfaces import Backend
1715
from cryptography.hazmat.primitives import hashes, padding
1816
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
1917
from cryptography.hazmat.primitives.hmac import HMAC
@@ -30,10 +28,8 @@ class Fernet(object):
3028
def __init__(
3129
self,
3230
key: typing.Union[bytes, str],
33-
backend: typing.Optional[Backend] = None,
31+
backend: typing.Any = None,
3432
):
35-
backend = _get_backend(backend)
36-
3733
key = base64.urlsafe_b64decode(key)
3834
if len(key) != 32:
3935
raise ValueError(
@@ -42,7 +38,6 @@ def __init__(
4238

4339
self._signing_key = key[:16]
4440
self._encryption_key = key[16:]
45-
self._backend = backend
4641

4742
@classmethod
4843
def generate_key(cls) -> bytes:
@@ -63,15 +58,16 @@ def _encrypt_from_parts(
6358
padder = padding.PKCS7(algorithms.AES.block_size).padder()
6459
padded_data = padder.update(data) + padder.finalize()
6560
encryptor = Cipher(
66-
algorithms.AES(self._encryption_key), modes.CBC(iv), self._backend
61+
algorithms.AES(self._encryption_key),
62+
modes.CBC(iv),
6763
).encryptor()
6864
ciphertext = encryptor.update(padded_data) + encryptor.finalize()
6965

7066
basic_parts = (
7167
b"\x80" + struct.pack(">Q", current_time) + iv + ciphertext
7268
)
7369

74-
h = HMAC(self._signing_key, hashes.SHA256(), backend=self._backend)
70+
h = HMAC(self._signing_key, hashes.SHA256())
7571
h.update(basic_parts)
7672
hmac = h.finalize()
7773
return base64.urlsafe_b64encode(basic_parts + hmac)
@@ -118,7 +114,7 @@ def _get_unverified_token_data(token: bytes) -> typing.Tuple[int, bytes]:
118114
return timestamp, data
119115

120116
def _verify_signature(self, data: bytes) -> None:
121-
h = HMAC(self._signing_key, hashes.SHA256(), backend=self._backend)
117+
h = HMAC(self._signing_key, hashes.SHA256())
122118
h.update(data[:-32])
123119
try:
124120
h.verify(data[-32:])
@@ -144,7 +140,7 @@ def _decrypt_data(
144140
iv = data[9:25]
145141
ciphertext = data[25:-32]
146142
decryptor = Cipher(
147-
algorithms.AES(self._encryption_key), modes.CBC(iv), self._backend
143+
algorithms.AES(self._encryption_key), modes.CBC(iv)
148144
).decryptor()
149145
plaintext_padded = decryptor.update(ciphertext)
150146
try:

src/cryptography/hazmat/backends/__init__.py

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,10 @@
22
# 2.0, and the BSD License. See the LICENSE file in the root of this repository
33
# for complete details.
44

5-
import typing
6-
75
from cryptography.hazmat.backends.interfaces import Backend
86

9-
_default_backend: typing.Optional[Backend] = None
10-
117

128
def default_backend() -> Backend:
13-
global _default_backend
14-
15-
if _default_backend is None:
16-
from cryptography.hazmat.backends.openssl.backend import backend
17-
18-
_default_backend = backend
19-
20-
return _default_backend
21-
9+
from cryptography.hazmat.backends.openssl.backend import backend
2210

23-
def _get_backend(backend: typing.Optional[Backend]) -> Backend:
24-
if backend is None:
25-
return default_backend()
26-
else:
27-
return backend
11+
return backend

tests/hazmat/backends/test_no_backend.py

Lines changed: 0 additions & 15 deletions
This file was deleted.

tests/hazmat/backends/test_openssl.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from cryptography import utils, x509
1515
from cryptography.exceptions import InternalError, _Reasons
16+
from cryptography.hazmat.backends import default_backend
1617
from cryptography.hazmat.backends.openssl.backend import backend
1718
from cryptography.hazmat.backends.openssl.ec import _sn_to_elliptic_curve
1819
from cryptography.hazmat.primitives import hashes, serialization
@@ -58,6 +59,9 @@ class TestOpenSSL(object):
5859
def test_backend_exists(self):
5960
assert backend
6061

62+
def test_is_default_backend(self):
63+
assert backend is default_backend()
64+
6165
def test_openssl_version_text(self):
6266
"""
6367
This test checks the value of OPENSSL_VERSION_TEXT.

tests/test_fernet.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import pytest
1717

1818
from cryptography.fernet import Fernet, InvalidToken, MultiFernet
19-
from cryptography.hazmat.backends import default_backend
2019
from cryptography.hazmat.primitives.ciphers import algorithms, modes
2120

2221
import cryptography_vectors
@@ -33,11 +32,6 @@ def json_parametrize(keys, filename):
3332
)
3433

3534

36-
def test_default_backend():
37-
f = Fernet(Fernet.generate_key())
38-
assert f._backend is default_backend()
39-
40-
4135
@pytest.mark.supported(
4236
only_if=lambda backend: backend.cipher_supported(
4337
algorithms.AES(b"\x00" * 32), modes.CBC(b"\x00" * 16)

0 commit comments

Comments
 (0)