From fcaf9761d1a2a570b2249c2ef3fff322dbb0b37b Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Thu, 30 Jan 2014 11:23:57 -0800 Subject: Initial work on a prioritized multibackend for which implements CipherBackend --- tests/conftest.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/conftest.py b/tests/conftest.py index ecad1b23..00c44ca9 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -4,13 +4,16 @@ from cryptography.hazmat.backends import _ALL_BACKENDS from cryptography.hazmat.backends.interfaces import ( HMACBackend, CipherBackend, HashBackend, PBKDF2HMACBackend ) +from cryptography.hazmat.backends.multibackend import PrioritizedMultiBackend from .utils import check_for_iface, check_backend_support, select_backends def pytest_generate_tests(metafunc): names = metafunc.config.getoption("--backend") - selected_backends = select_backends(names, _ALL_BACKENDS) + selected_backends = select_backends( + names, _ALL_BACKENDS + [PrioritizedMultiBackend(_ALL_BACKENDS)] + ) if "backend" in metafunc.fixturenames: metafunc.parametrize("backend", selected_backends) -- cgit v1.2.3 From 19e19ae75385f652e1161e1153e9aea599cd6a07 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Thu, 30 Jan 2014 11:46:01 -0800 Subject: Replace re-running all tests with some unit tests --- tests/conftest.py | 5 +---- tests/hazmat/backends/test_multibackend.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 tests/hazmat/backends/test_multibackend.py (limited to 'tests') diff --git a/tests/conftest.py b/tests/conftest.py index 00c44ca9..ecad1b23 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -4,16 +4,13 @@ from cryptography.hazmat.backends import _ALL_BACKENDS from cryptography.hazmat.backends.interfaces import ( HMACBackend, CipherBackend, HashBackend, PBKDF2HMACBackend ) -from cryptography.hazmat.backends.multibackend import PrioritizedMultiBackend from .utils import check_for_iface, check_backend_support, select_backends def pytest_generate_tests(metafunc): names = metafunc.config.getoption("--backend") - selected_backends = select_backends( - names, _ALL_BACKENDS + [PrioritizedMultiBackend(_ALL_BACKENDS)] - ) + selected_backends = select_backends(names, _ALL_BACKENDS) if "backend" in metafunc.fixturenames: metafunc.parametrize("backend", selected_backends) diff --git a/tests/hazmat/backends/test_multibackend.py b/tests/hazmat/backends/test_multibackend.py new file mode 100644 index 00000000..aaf6d7c1 --- /dev/null +++ b/tests/hazmat/backends/test_multibackend.py @@ -0,0 +1,16 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +class TestPrioritizedMultiBackend(object): + pass -- cgit v1.2.3 From 6ced2f6444c956b88d42cc61fe9a38751b912c9a Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Thu, 30 Jan 2014 11:53:44 -0800 Subject: Direct tests for the cipher support --- tests/hazmat/backends/test_multibackend.py | 50 +++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/hazmat/backends/test_multibackend.py b/tests/hazmat/backends/test_multibackend.py index aaf6d7c1..dc58a585 100644 --- a/tests/hazmat/backends/test_multibackend.py +++ b/tests/hazmat/backends/test_multibackend.py @@ -11,6 +11,54 @@ # See the License for the specific language governing permissions and # limitations under the License. +import pytest + +from cryptography.exceptions import UnsupportedAlgorithm +from cryptography.hazmat.backends.multibackend import PrioritizedMultiBackend +from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes + + +class DummyCipherBackend(object): + def __init__(self, supported_ciphers): + self._ciphers = supported_ciphers + + def cipher_supported(self, algorithm, mode): + return (type(algorithm), type(mode)) in self._ciphers + + def create_symmetric_encryption_ctx(self, algorithm, mode): + if not self.cipher_supported(algorithm, mode): + raise UnsupportedAlgorithm + + def create_symmetric_decryption_ctx(self, algorithm, mode): + if not self.cipher_supported(algorithm, mode): + raise UnsupportedAlgorithm + class TestPrioritizedMultiBackend(object): - pass + def test_ciphers(self): + backend = PrioritizedMultiBackend([ + DummyCipherBackend([ + (algorithms.AES, modes.CBC), + ]) + ]) + assert backend.cipher_supported( + algorithms.AES(b"\x00" * 16), modes.CBC(b"\x00" * 16) + ) + + cipher = Cipher( + algorithms.AES(b"\x00" * 16), + modes.CBC(b"\x00" * 16), + backend=backend + ) + cipher.encryptor() + cipher.decryptor() + + cipher = Cipher( + algorithms.Camellia(b"\x00" * 16), + modes.CBC(b"\x00" * 16), + backend=backend + ) + with pytest.raises(UnsupportedAlgorithm): + cipher.encryptor() + with pytest.raises(UnsupportedAlgorithm): + cipher.decryptor() -- cgit v1.2.3 From 87899d4fd55881a4dbec1ee32b79a5bd315ec33f Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Thu, 30 Jan 2014 11:58:40 -0800 Subject: Direct tests for the hash support --- tests/hazmat/backends/test_multibackend.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'tests') diff --git a/tests/hazmat/backends/test_multibackend.py b/tests/hazmat/backends/test_multibackend.py index dc58a585..f300b6c8 100644 --- a/tests/hazmat/backends/test_multibackend.py +++ b/tests/hazmat/backends/test_multibackend.py @@ -15,6 +15,7 @@ import pytest from cryptography.exceptions import UnsupportedAlgorithm from cryptography.hazmat.backends.multibackend import PrioritizedMultiBackend +from cryptography.hazmat.primitives import hashes from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes @@ -34,6 +35,19 @@ class DummyCipherBackend(object): raise UnsupportedAlgorithm +class DummyHashBackend(object): + def __init__(self, supported_algorithms): + self._algorithms = supported_algorithms + + def hash_supported(self, algorithm): + return type(algorithm) in self._algorithms + + def create_hash_ctx(self, algorithm): + if not self.hash_supported(algorithm): + raise UnsupportedAlgorithm + + + class TestPrioritizedMultiBackend(object): def test_ciphers(self): backend = PrioritizedMultiBackend([ @@ -62,3 +76,14 @@ class TestPrioritizedMultiBackend(object): cipher.encryptor() with pytest.raises(UnsupportedAlgorithm): cipher.decryptor() + + def test_hashes(self): + backend = PrioritizedMultiBackend([ + DummyHashBackend([hashes.MD5]) + ]) + assert backend.hash_supported(hashes.MD5()) + + hashes.Hash(hashes.MD5(), backend=backend) + + with pytest.raises(UnsupportedAlgorithm): + hashes.Hash(hashes.SHA1(), backend=backend) -- cgit v1.2.3 From 723bb9675b7f78e8515be0a80305f6fd009a80fa Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Thu, 30 Jan 2014 15:14:25 -0800 Subject: Not true anymore --- tests/hazmat/backends/test_openssl.py | 3 --- 1 file changed, 3 deletions(-) (limited to 'tests') diff --git a/tests/hazmat/backends/test_openssl.py b/tests/hazmat/backends/test_openssl.py index f01c3f64..f3b0bb15 100644 --- a/tests/hazmat/backends/test_openssl.py +++ b/tests/hazmat/backends/test_openssl.py @@ -40,9 +40,6 @@ class TestOpenSSL(object): def test_backend_exists(self): assert backend - def test_is_default(self): - assert backend == default_backend() - def test_openssl_version_text(self): """ This test checks the value of OPENSSL_VERSION_TEXT. -- cgit v1.2.3 From 0929f8ff07e71410bd2ce89d407805bd476c1761 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Thu, 30 Jan 2014 15:23:17 -0800 Subject: More direct tests --- tests/hazmat/backends/test_multibackend.py | 50 +++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/hazmat/backends/test_multibackend.py b/tests/hazmat/backends/test_multibackend.py index f300b6c8..03b3187b 100644 --- a/tests/hazmat/backends/test_multibackend.py +++ b/tests/hazmat/backends/test_multibackend.py @@ -15,8 +15,9 @@ import pytest from cryptography.exceptions import UnsupportedAlgorithm from cryptography.hazmat.backends.multibackend import PrioritizedMultiBackend -from cryptography.hazmat.primitives import hashes +from cryptography.hazmat.primitives import hashes, hmac from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes +from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC class DummyCipherBackend(object): @@ -47,6 +48,31 @@ class DummyHashBackend(object): raise UnsupportedAlgorithm +class DummyHMACBackend(object): + def __init__(self, supported_algorithms): + self._algorithms = supported_algorithms + + def hmac_supported(self, algorithm): + return type(algorithm) in self._algorithms + + def create_hmac_ctx(self, key, algorithm): + if not self.hmac_supported(algorithm): + raise UnsupportedAlgorithm + + +class DummyPBKDF2HMAC(object): + def __init__(self, supported_algorithms): + self._algorithms = supported_algorithms + + def pbkdf2_hmac_supported(self, algorithm): + return type(algorithm) in self._algorithms + + def derive_pbkdf2_hmac(self, algorithm, length, salt, iterations, + key_material): + if not self.pbkdf2_hmac_supported(algorithm): + raise UnsupportedAlgorithm + + class TestPrioritizedMultiBackend(object): def test_ciphers(self): @@ -87,3 +113,25 @@ class TestPrioritizedMultiBackend(object): with pytest.raises(UnsupportedAlgorithm): hashes.Hash(hashes.SHA1(), backend=backend) + + def test_hmac(self): + backend = PrioritizedMultiBackend([ + DummyHMACBackend([hashes.MD5]) + ]) + assert backend.hmac_supported(hashes.MD5()) + + hmac.HMAC(b"", hashes.MD5(), backend=backend) + + with pytest.raises(UnsupportedAlgorithm): + hmac.HMAC(b"", hashes.SHA1(), backend=backend) + + def test_pbkdf2(self): + backend = PrioritizedMultiBackend([ + DummyPBKDF2HMAC([hashes.MD5]) + ]) + assert backend.pbkdf2_hmac_supported(hashes.MD5()) + + backend.derive_pbkdf2_hmac(hashes.MD5(), 10, b"", 10, b"") + + with pytest.raises(UnsupportedAlgorithm): + backend.derive_pbkdf2_hmac(hashes.SHA1(), 10, b"", 10, b"") -- cgit v1.2.3 From ebc5161f606fe1ac7d51b6ab997f663cfcf9be9b Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Thu, 30 Jan 2014 15:42:37 -0800 Subject: Fix --- tests/hazmat/backends/test_multibackend.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/hazmat/backends/test_multibackend.py b/tests/hazmat/backends/test_multibackend.py index 03b3187b..127c0d3e 100644 --- a/tests/hazmat/backends/test_multibackend.py +++ b/tests/hazmat/backends/test_multibackend.py @@ -13,13 +13,17 @@ import pytest +from cryptography import utils from cryptography.exceptions import UnsupportedAlgorithm +from cryptography.hazmat.backends.interfaces import ( + CipherBackend, HashBackend, HMACBackend, PBKDF2HMACBackend +) from cryptography.hazmat.backends.multibackend import PrioritizedMultiBackend from cryptography.hazmat.primitives import hashes, hmac from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes -from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC +@utils.register_interface(CipherBackend) class DummyCipherBackend(object): def __init__(self, supported_ciphers): self._ciphers = supported_ciphers @@ -36,6 +40,7 @@ class DummyCipherBackend(object): raise UnsupportedAlgorithm +@utils.register_interface(HashBackend) class DummyHashBackend(object): def __init__(self, supported_algorithms): self._algorithms = supported_algorithms @@ -48,6 +53,7 @@ class DummyHashBackend(object): raise UnsupportedAlgorithm +@utils.register_interface(HMACBackend) class DummyHMACBackend(object): def __init__(self, supported_algorithms): self._algorithms = supported_algorithms @@ -60,6 +66,7 @@ class DummyHMACBackend(object): raise UnsupportedAlgorithm +@utils.register_interface(PBKDF2HMACBackend) class DummyPBKDF2HMAC(object): def __init__(self, supported_algorithms): self._algorithms = supported_algorithms @@ -77,6 +84,7 @@ class DummyPBKDF2HMAC(object): class TestPrioritizedMultiBackend(object): def test_ciphers(self): backend = PrioritizedMultiBackend([ + DummyHashBackend([]), DummyCipherBackend([ (algorithms.AES, modes.CBC), ]) -- cgit v1.2.3 From a45a770f88ad4444c80e52966f5022c4fb287ce5 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Fri, 31 Jan 2014 07:51:41 -0800 Subject: pep8 fixes --- tests/hazmat/backends/test_multibackend.py | 1 - tests/hazmat/backends/test_openssl.py | 1 - 2 files changed, 2 deletions(-) (limited to 'tests') diff --git a/tests/hazmat/backends/test_multibackend.py b/tests/hazmat/backends/test_multibackend.py index 127c0d3e..f77d2680 100644 --- a/tests/hazmat/backends/test_multibackend.py +++ b/tests/hazmat/backends/test_multibackend.py @@ -80,7 +80,6 @@ class DummyPBKDF2HMAC(object): raise UnsupportedAlgorithm - class TestPrioritizedMultiBackend(object): def test_ciphers(self): backend = PrioritizedMultiBackend([ diff --git a/tests/hazmat/backends/test_openssl.py b/tests/hazmat/backends/test_openssl.py index f3b0bb15..0aff41e5 100644 --- a/tests/hazmat/backends/test_openssl.py +++ b/tests/hazmat/backends/test_openssl.py @@ -15,7 +15,6 @@ import pytest from cryptography import utils from cryptography.exceptions import UnsupportedAlgorithm, InternalError -from cryptography.hazmat.backends import default_backend from cryptography.hazmat.backends.openssl.backend import backend, Backend from cryptography.hazmat.primitives import interfaces from cryptography.hazmat.primitives.ciphers import Cipher -- cgit v1.2.3 From 450bb4c6609d246ded86959de3d925df81afdba9 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Mon, 3 Feb 2014 15:42:04 -0800 Subject: Rename and document --- tests/hazmat/backends/test_multibackend.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'tests') diff --git a/tests/hazmat/backends/test_multibackend.py b/tests/hazmat/backends/test_multibackend.py index f77d2680..ca21c9fc 100644 --- a/tests/hazmat/backends/test_multibackend.py +++ b/tests/hazmat/backends/test_multibackend.py @@ -18,7 +18,7 @@ from cryptography.exceptions import UnsupportedAlgorithm from cryptography.hazmat.backends.interfaces import ( CipherBackend, HashBackend, HMACBackend, PBKDF2HMACBackend ) -from cryptography.hazmat.backends.multibackend import PrioritizedMultiBackend +from cryptography.hazmat.backends.multibackend import MultiBackend from cryptography.hazmat.primitives import hashes, hmac from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes @@ -80,9 +80,9 @@ class DummyPBKDF2HMAC(object): raise UnsupportedAlgorithm -class TestPrioritizedMultiBackend(object): +class TestMultiBackend(object): def test_ciphers(self): - backend = PrioritizedMultiBackend([ + backend = MultiBackend([ DummyHashBackend([]), DummyCipherBackend([ (algorithms.AES, modes.CBC), @@ -111,7 +111,7 @@ class TestPrioritizedMultiBackend(object): cipher.decryptor() def test_hashes(self): - backend = PrioritizedMultiBackend([ + backend = MultiBackend([ DummyHashBackend([hashes.MD5]) ]) assert backend.hash_supported(hashes.MD5()) @@ -122,7 +122,7 @@ class TestPrioritizedMultiBackend(object): hashes.Hash(hashes.SHA1(), backend=backend) def test_hmac(self): - backend = PrioritizedMultiBackend([ + backend = MultiBackend([ DummyHMACBackend([hashes.MD5]) ]) assert backend.hmac_supported(hashes.MD5()) @@ -133,7 +133,7 @@ class TestPrioritizedMultiBackend(object): hmac.HMAC(b"", hashes.SHA1(), backend=backend) def test_pbkdf2(self): - backend = PrioritizedMultiBackend([ + backend = MultiBackend([ DummyPBKDF2HMAC([hashes.MD5]) ]) assert backend.pbkdf2_hmac_supported(hashes.MD5()) -- cgit v1.2.3