From f2ce1ae856e43a292ea7a6aae26d75b508f0a363 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Thu, 3 Oct 2013 21:54:05 -0500 Subject: rebase and modify to support some changed behaviors * Update code to reflect new api object (ffi and lib are no longer private) * tests updated to take an api object * skipif marks removed for now as we need to use the api passed to each individual test. skip testing done inside the test * changed name of supports in api to supports_cipher (future PRs will contain supports_hash) --- cryptography/bindings/openssl/api.py | 11 +++++------ tests/bindings/test_openssl.py | 19 ++----------------- tests/primitives/test_cryptrec.py | 10 ++++------ tests/primitives/test_openssl_vectors.py | 13 ++++++------- 4 files changed, 17 insertions(+), 36 deletions(-) diff --git a/cryptography/bindings/openssl/api.py b/cryptography/bindings/openssl/api.py index 073cb532..c352d3b2 100644 --- a/cryptography/bindings/openssl/api.py +++ b/cryptography/bindings/openssl/api.py @@ -72,9 +72,9 @@ class API(object): """ return self.ffi.string(self.lib.OPENSSL_VERSION_TEXT).decode("ascii") - def supports(self, ciphername): - return (self._ffi.NULL != - self._lib.EVP_get_cipherbyname(ciphername.encode("ascii"))) + def supports_cipher(self, ciphername): + return (self.ffi.NULL != + self.lib.EVP_get_cipherbyname(ciphername.encode("ascii"))) def create_block_cipher_context(self, cipher, mode): ctx = self.ffi.new("EVP_CIPHER_CTX *") @@ -85,9 +85,8 @@ class API(object): ciphername = "{0}-{1}-{2}".format( cipher.name, cipher.key_size, mode.name ).lower() - evp_cipher = self._lib.EVP_get_cipherbyname(ciphername.encode("ascii")) - if evp_cipher == self._ffi.NULL: - raise AssertionError("Unsupported cipher: {0}".format(ciphername)) + evp_cipher = self.lib.EVP_get_cipherbyname(ciphername.encode("ascii")) + assert evp_cipher != self.ffi.NULL if isinstance(mode, interfaces.ModeWithInitializationVector): iv_nonce = mode.initialization_vector else: diff --git a/tests/bindings/test_openssl.py b/tests/bindings/test_openssl.py index 85ecc49c..e5b78d18 100644 --- a/tests/bindings/test_openssl.py +++ b/tests/bindings/test_openssl.py @@ -11,8 +11,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -import pytest - from cryptography.bindings.openssl.api import api @@ -31,18 +29,5 @@ class TestOpenSSL(object): """ assert api.openssl_version_text().startswith("OpenSSL") - def test_supports(self): - assert api.supports("not-a-real-cipher") is False - - def test_create_block_cipher_context_with_unsupported_cipher(self): - class FakeCipher(object): - name = "FakeCipher" - key_size = 24 - - class FakeMode(object): - name = "CCC" - - with pytest.raises(AssertionError): - cipher = FakeCipher() - mode = FakeMode() - api.create_block_cipher_context(cipher, mode) + def test_supports_cipher(self): + assert api.supports_cipher("not-a-real-cipher") is False diff --git a/tests/primitives/test_cryptrec.py b/tests/primitives/test_cryptrec.py index 54ae4d0c..fb2bf19a 100644 --- a/tests/primitives/test_cryptrec.py +++ b/tests/primitives/test_cryptrec.py @@ -23,13 +23,10 @@ import os import pytest -from cryptography.bindings.openssl.api import api from cryptography.primitives.block import BlockCipher, ciphers, modes from ..utils import load_cryptrec_vectors_from_file -CAMELLIA_ECB_SUPPORTED = api.supports('camellia-128-ecb') - def parameterize_encrypt_test(cipher, vector_type, params, fnames): return pytest.mark.parametrize(params, @@ -42,9 +39,7 @@ def parameterize_encrypt_test(cipher, vector_type, params, fnames): ) -@pytest.mark.skipif("not CAMELLIA_ECB_SUPPORTED") class TestCamelliaECB(object): - @parameterize_encrypt_test( "Camellia", "NTT", ("key", "plaintext", "ciphertext"), @@ -54,10 +49,13 @@ class TestCamelliaECB(object): "camellia-256-ecb.txt", ] ) - def test_NTT(self, key, plaintext, ciphertext): + def test_NTT(self, key, plaintext, ciphertext, api): + if not api.supports_cipher('camellia-128-ecb'): + pytest.skip() cipher = BlockCipher( ciphers.Camellia(binascii.unhexlify(key)), modes.ECB(), + api ) actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) actual_ciphertext += cipher.finalize() diff --git a/tests/primitives/test_openssl_vectors.py b/tests/primitives/test_openssl_vectors.py index 0ecbcd9d..ccd07194 100644 --- a/tests/primitives/test_openssl_vectors.py +++ b/tests/primitives/test_openssl_vectors.py @@ -28,10 +28,6 @@ from cryptography.primitives.block import BlockCipher, ciphers, modes from ..utils import load_openssl_vectors_from_file -CAMELLIA_CBC_SUPPORTED = api.supports('camellia-128-cbc') -CAMELLIA_OFB_SUPPORTED = api.supports('camellia-128-ofb') -CAMELLIA_CFB_SUPPORTED = api.supports('camellia-128-cfb') - def parameterize_encrypt_test(cipher, params, fnames): return pytest.mark.parametrize(params, @@ -42,7 +38,6 @@ def parameterize_encrypt_test(cipher, params, fnames): ) -@pytest.mark.skipif("not CAMELLIA_CBC_SUPPORTED") class TestCamelliaCBC(object): @parameterize_encrypt_test( @@ -53,6 +48,8 @@ class TestCamelliaCBC(object): ] ) def test_OpenSSL(self, key, iv, plaintext, ciphertext): + if not api.supports_cipher('camellia-128-cbc'): + pytest.skip() cipher = BlockCipher( ciphers.Camellia(binascii.unhexlify(key)), modes.CBC(binascii.unhexlify(iv)), @@ -62,7 +59,6 @@ class TestCamelliaCBC(object): assert binascii.hexlify(actual_ciphertext).upper() == ciphertext -@pytest.mark.skipif("not CAMELLIA_OFB_SUPPORTED") class TestCamelliaOFB(object): @parameterize_encrypt_test( @@ -73,6 +69,8 @@ class TestCamelliaOFB(object): ] ) def test_OpenSSL(self, key, iv, plaintext, ciphertext): + if not api.supports_cipher('camellia-128-ofb'): + pytest.skip() cipher = BlockCipher( ciphers.Camellia(binascii.unhexlify(key)), modes.OFB(binascii.unhexlify(iv)), @@ -82,7 +80,6 @@ class TestCamelliaOFB(object): assert binascii.hexlify(actual_ciphertext).upper() == ciphertext -@pytest.mark.skipif("not CAMELLIA_CFB_SUPPORTED") class TestCamelliaCFB(object): @parameterize_encrypt_test( @@ -93,6 +90,8 @@ class TestCamelliaCFB(object): ] ) def test_OpenSSL(self, key, iv, plaintext, ciphertext): + if not api.supports_cipher('camellia-128-cfb'): + pytest.skip() cipher = BlockCipher( ciphers.Camellia(binascii.unhexlify(key)), modes.CFB(binascii.unhexlify(iv)), -- cgit v1.2.3