aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--cryptography/hazmat/primitives/ciphers/base.py7
-rw-r--r--cryptography/hazmat/primitives/hashes.py6
-rw-r--r--cryptography/hazmat/primitives/hmac.py8
-rw-r--r--tests/hazmat/primitives/test_block.py16
-rw-r--r--tests/hazmat/primitives/test_hashes.py21
-rw-r--r--tests/hazmat/primitives/test_hmac.py13
-rw-r--r--tests/hazmat/primitives/utils.py10
7 files changed, 27 insertions, 54 deletions
diff --git a/cryptography/hazmat/primitives/ciphers/base.py b/cryptography/hazmat/primitives/ciphers/base.py
index 3d733afc..48e6da6f 100644
--- a/cryptography/hazmat/primitives/ciphers/base.py
+++ b/cryptography/hazmat/primitives/ciphers/base.py
@@ -19,12 +19,7 @@ from cryptography.hazmat.primitives import interfaces
class Cipher(object):
- def __init__(self, algorithm, mode, backend=None):
- if backend is None:
- from cryptography.hazmat.bindings import (
- _default_backend as backend,
- )
-
+ def __init__(self, algorithm, mode, backend):
if not isinstance(algorithm, interfaces.CipherAlgorithm):
raise TypeError("Expected interface of interfaces.CipherAlgorithm")
diff --git a/cryptography/hazmat/primitives/hashes.py b/cryptography/hazmat/primitives/hashes.py
index 93fc8c42..bee188b3 100644
--- a/cryptography/hazmat/primitives/hashes.py
+++ b/cryptography/hazmat/primitives/hashes.py
@@ -22,15 +22,11 @@ from cryptography.hazmat.primitives import interfaces
@utils.register_interface(interfaces.HashContext)
class Hash(object):
- def __init__(self, algorithm, backend=None, ctx=None):
+ def __init__(self, algorithm, backend, ctx=None):
if not isinstance(algorithm, interfaces.HashAlgorithm):
raise TypeError("Expected instance of interfaces.HashAlgorithm.")
self.algorithm = algorithm
- if backend is None:
- from cryptography.hazmat.bindings import _default_backend
- backend = _default_backend
-
self._backend = backend
if ctx is None:
diff --git a/cryptography/hazmat/primitives/hmac.py b/cryptography/hazmat/primitives/hmac.py
index 08dfae01..2bd8e4d3 100644
--- a/cryptography/hazmat/primitives/hmac.py
+++ b/cryptography/hazmat/primitives/hmac.py
@@ -22,15 +22,11 @@ from cryptography.hazmat.primitives import interfaces
@utils.register_interface(interfaces.HashContext)
class HMAC(object):
- def __init__(self, key, algorithm, ctx=None, backend=None):
+ def __init__(self, key, algorithm, backend, ctx=None):
if not isinstance(algorithm, interfaces.HashAlgorithm):
raise TypeError("Expected instance of interfaces.HashAlgorithm.")
self.algorithm = algorithm
- if backend is None:
- from cryptography.hazmat.bindings import _default_backend
- backend = _default_backend
-
self._backend = backend
self._key = key
if ctx is None:
@@ -51,7 +47,7 @@ class HMAC(object):
return HMAC(
self._key,
self.algorithm,
- backend=self._backend,
+ self._backend,
ctx=self._ctx.copy()
)
diff --git a/tests/hazmat/primitives/test_block.py b/tests/hazmat/primitives/test_block.py
index 9460c53d..70d7098b 100644
--- a/tests/hazmat/primitives/test_block.py
+++ b/tests/hazmat/primitives/test_block.py
@@ -31,23 +31,19 @@ class DummyCipher(object):
class TestCipher(object):
- def test_instantiate_without_backend(self):
- Cipher(
- algorithms.AES(binascii.unhexlify(b"0" * 32)),
- modes.CBC(binascii.unhexlify(b"0" * 32))
- )
-
- def test_creates_encryptor(self):
+ def test_creates_encryptor(self, backend):
cipher = Cipher(
algorithms.AES(binascii.unhexlify(b"0" * 32)),
- modes.CBC(binascii.unhexlify(b"0" * 32))
+ modes.CBC(binascii.unhexlify(b"0" * 32)),
+ backend
)
assert isinstance(cipher.encryptor(), interfaces.CipherContext)
- def test_creates_decryptor(self):
+ def test_creates_decryptor(self, backend):
cipher = Cipher(
algorithms.AES(binascii.unhexlify(b"0" * 32)),
- modes.CBC(binascii.unhexlify(b"0" * 32))
+ modes.CBC(binascii.unhexlify(b"0" * 32)),
+ backend
)
assert isinstance(cipher.decryptor(), interfaces.CipherContext)
diff --git a/tests/hazmat/primitives/test_hashes.py b/tests/hazmat/primitives/test_hashes.py
index 367e764f..321082f6 100644
--- a/tests/hazmat/primitives/test_hashes.py
+++ b/tests/hazmat/primitives/test_hashes.py
@@ -20,7 +20,6 @@ import pytest
import six
from cryptography.exceptions import AlreadyFinalized
-from cryptography.hazmat.bindings import _default_backend
from cryptography.hazmat.primitives import hashes
from .utils import generate_base_hash_test
@@ -28,7 +27,7 @@ from .utils import generate_base_hash_test
class TestHashContext(object):
def test_hash_reject_unicode(self, backend):
- m = hashes.Hash(hashes.SHA1(), backend=backend)
+ m = hashes.Hash(hashes.SHA1(), backend)
with pytest.raises(TypeError):
m.update(six.u("\u00FC"))
@@ -36,24 +35,16 @@ class TestHashContext(object):
pretend_backend = pretend.stub()
copied_ctx = pretend.stub()
pretend_ctx = pretend.stub(copy=lambda: copied_ctx)
- h = hashes.Hash(hashes.SHA1(), backend=pretend_backend,
- ctx=pretend_ctx)
+ h = hashes.Hash(hashes.SHA1(), pretend_backend, ctx=pretend_ctx)
assert h._backend is pretend_backend
assert h.copy()._backend is h._backend
- def test_default_backend_creation(self):
- """
- This test assumes the presence of SHA1 in the default backend.
- """
- h = hashes.Hash(hashes.SHA1())
- assert h._backend is _default_backend
-
- def test_hash_algorithm_instance(self):
+ def test_hash_algorithm_instance(self, backend):
with pytest.raises(TypeError):
- hashes.Hash(hashes.SHA1)
+ hashes.Hash(hashes.SHA1, backend)
- def test_raises_after_finalize(self):
- h = hashes.Hash(hashes.SHA1())
+ def test_raises_after_finalize(self, backend):
+ h = hashes.Hash(hashes.SHA1(), backend)
h.finalize()
with pytest.raises(AlreadyFinalized):
diff --git a/tests/hazmat/primitives/test_hmac.py b/tests/hazmat/primitives/test_hmac.py
index d17049e3..622f186a 100644
--- a/tests/hazmat/primitives/test_hmac.py
+++ b/tests/hazmat/primitives/test_hmac.py
@@ -33,7 +33,7 @@ class TestHMAC(object):
)
def test_hmac_reject_unicode(self, backend):
- h = hmac.HMAC(b"mykey", hashes.SHA1(), backend=backend)
+ h = hmac.HMAC(b"mykey", hashes.SHA1(), backend)
with pytest.raises(TypeError):
h.update(six.u("\u00FC"))
@@ -42,17 +42,16 @@ class TestHMAC(object):
pretend_backend = pretend.stub(hmacs=pretend_hmac)
copied_ctx = pretend.stub()
pretend_ctx = pretend.stub(copy=lambda: copied_ctx)
- h = hmac.HMAC(b"key", hashes.SHA1(), backend=pretend_backend,
- ctx=pretend_ctx)
+ h = hmac.HMAC(b"key", hashes.SHA1(), pretend_backend, ctx=pretend_ctx)
assert h._backend is pretend_backend
assert h.copy()._backend is pretend_backend
- def test_hmac_algorithm_instance(self):
+ def test_hmac_algorithm_instance(self, backend):
with pytest.raises(TypeError):
- hmac.HMAC(b"key", hashes.SHA1)
+ hmac.HMAC(b"key", hashes.SHA1, backend)
- def test_raises_after_finalize(self):
- h = hmac.HMAC(b"key", hashes.SHA1())
+ def test_raises_after_finalize(self, backend):
+ h = hmac.HMAC(b"key", hashes.SHA1(), backend)
h.finalize()
with pytest.raises(AlreadyFinalized):
diff --git a/tests/hazmat/primitives/utils.py b/tests/hazmat/primitives/utils.py
index 9327b0eb..c476a75c 100644
--- a/tests/hazmat/primitives/utils.py
+++ b/tests/hazmat/primitives/utils.py
@@ -121,7 +121,7 @@ def hash_test(backend, algorithm, params, only_if, skip_message):
pytest.skip(skip_message)
msg = params[0]
md = params[1]
- m = hashes.Hash(algorithm, backend=backend)
+ m = hashes.Hash(algorithm, backend)
m.update(binascii.unhexlify(msg))
expected_md = md.replace(" ", "").lower().encode("ascii")
assert m.finalize() == binascii.unhexlify(expected_md)
@@ -148,7 +148,7 @@ def base_hash_test(backend, algorithm, digest_size, block_size, only_if,
if only_if is not None and not only_if(backend):
pytest.skip(skip_message)
- m = hashes.Hash(algorithm, backend=backend)
+ m = hashes.Hash(algorithm, backend)
assert m.algorithm.digest_size == digest_size
assert m.algorithm.block_size == block_size
m_copy = m.copy()
@@ -180,7 +180,7 @@ def generate_long_string_hash_test(hash_factory, md, only_if=None,
def long_string_hash_test(backend, algorithm, md, only_if, skip_message):
if only_if is not None and not only_if(backend):
pytest.skip(skip_message)
- m = hashes.Hash(algorithm, backend=backend)
+ m = hashes.Hash(algorithm, backend)
m.update(b"a" * 1000000)
assert m.finalize() == binascii.unhexlify(md.lower().encode("ascii"))
@@ -211,7 +211,7 @@ def hmac_test(backend, algorithm, params, only_if, skip_message):
msg = params[0]
md = params[1]
key = params[2]
- h = hmac.HMAC(binascii.unhexlify(key), algorithm)
+ h = hmac.HMAC(binascii.unhexlify(key), algorithm, backend)
h.update(binascii.unhexlify(msg))
assert h.finalize() == binascii.unhexlify(md.encode("ascii"))
@@ -233,7 +233,7 @@ def base_hmac_test(backend, algorithm, only_if, skip_message):
if only_if is not None and not only_if(backend):
pytest.skip(skip_message)
key = b"ab"
- h = hmac.HMAC(binascii.unhexlify(key), algorithm)
+ h = hmac.HMAC(binascii.unhexlify(key), algorithm, backend)
h_copy = h.copy()
assert h != h_copy
assert h._ctx != h_copy._ctx