From dff22d4707a50b8164c5c6acd5521bcd91160cd1 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Fri, 27 Sep 2013 13:43:06 -0500 Subject: Camellia block cipher support * Tests for CBC, OFB, CFB, and ECB * Tests will be automatically skipped if camellia support is not present in your OpenSSL library (e.g. OS X 10.8 with default OpenSSL) * Test for unsupported cipher in create_block_cipher_context * Docs for the cipher --- tests/primitives/test_openssl_vectors.py | 102 +++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 tests/primitives/test_openssl_vectors.py (limited to 'tests/primitives/test_openssl_vectors.py') diff --git a/tests/primitives/test_openssl_vectors.py b/tests/primitives/test_openssl_vectors.py new file mode 100644 index 00000000..0ecbcd9d --- /dev/null +++ b/tests/primitives/test_openssl_vectors.py @@ -0,0 +1,102 @@ +# 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. + +""" +Test using the OpenSSL Test Vectors +""" + +from __future__ import absolute_import, division, print_function + +import binascii +import itertools +import os + +import pytest + +from cryptography.bindings.openssl.api import api +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, + list(itertools.chain.from_iterable( + load_openssl_vectors_from_file(os.path.join(cipher, fname)) + for fname in fnames + )) + ) + + +@pytest.mark.skipif("not CAMELLIA_CBC_SUPPORTED") +class TestCamelliaCBC(object): + + @parameterize_encrypt_test( + "Camellia", + ("key", "iv", "plaintext", "ciphertext"), + [ + "camellia-cbc.txt", + ] + ) + def test_OpenSSL(self, key, iv, plaintext, ciphertext): + cipher = BlockCipher( + ciphers.Camellia(binascii.unhexlify(key)), + modes.CBC(binascii.unhexlify(iv)), + ) + actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) + actual_ciphertext += cipher.finalize() + assert binascii.hexlify(actual_ciphertext).upper() == ciphertext + + +@pytest.mark.skipif("not CAMELLIA_OFB_SUPPORTED") +class TestCamelliaOFB(object): + + @parameterize_encrypt_test( + "Camellia", + ("key", "iv", "plaintext", "ciphertext"), + [ + "camellia-ofb.txt", + ] + ) + def test_OpenSSL(self, key, iv, plaintext, ciphertext): + cipher = BlockCipher( + ciphers.Camellia(binascii.unhexlify(key)), + modes.OFB(binascii.unhexlify(iv)), + ) + actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) + actual_ciphertext += cipher.finalize() + assert binascii.hexlify(actual_ciphertext).upper() == ciphertext + + +@pytest.mark.skipif("not CAMELLIA_CFB_SUPPORTED") +class TestCamelliaCFB(object): + + @parameterize_encrypt_test( + "Camellia", + ("key", "iv", "plaintext", "ciphertext"), + [ + "camellia-cfb.txt", + ] + ) + def test_OpenSSL(self, key, iv, plaintext, ciphertext): + cipher = BlockCipher( + ciphers.Camellia(binascii.unhexlify(key)), + modes.CFB(binascii.unhexlify(iv)), + ) + actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) + actual_ciphertext += cipher.finalize() + assert binascii.hexlify(actual_ciphertext).upper() == ciphertext -- cgit v1.2.3 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) --- tests/primitives/test_openssl_vectors.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'tests/primitives/test_openssl_vectors.py') 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 From 84bb1bb2c04c8e05a072587f9e622f6a5e99de56 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sun, 6 Oct 2013 12:24:47 -0500 Subject: remove openssl api dependency in test_openssl_vectors Update some single quotes to double for consistency --- tests/primitives/test_openssl_vectors.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) (limited to 'tests/primitives/test_openssl_vectors.py') diff --git a/tests/primitives/test_openssl_vectors.py b/tests/primitives/test_openssl_vectors.py index ccd07194..acb982a5 100644 --- a/tests/primitives/test_openssl_vectors.py +++ b/tests/primitives/test_openssl_vectors.py @@ -23,7 +23,6 @@ import os import pytest -from cryptography.bindings.openssl.api import api from cryptography.primitives.block import BlockCipher, ciphers, modes from ..utils import load_openssl_vectors_from_file @@ -47,9 +46,9 @@ class TestCamelliaCBC(object): "camellia-cbc.txt", ] ) - def test_OpenSSL(self, key, iv, plaintext, ciphertext): - if not api.supports_cipher('camellia-128-cbc'): - pytest.skip() + def test_OpenSSL(self, key, iv, plaintext, ciphertext, api): + if not api.supports_cipher("camellia-128-cbc"): + pytest.skip("Does not support Camellia CBC") cipher = BlockCipher( ciphers.Camellia(binascii.unhexlify(key)), modes.CBC(binascii.unhexlify(iv)), @@ -68,9 +67,9 @@ class TestCamelliaOFB(object): "camellia-ofb.txt", ] ) - def test_OpenSSL(self, key, iv, plaintext, ciphertext): - if not api.supports_cipher('camellia-128-ofb'): - pytest.skip() + def test_OpenSSL(self, key, iv, plaintext, ciphertext, api): + if not api.supports_cipher("camellia-128-ofb"): + pytest.skip("Does not support Camellia OFB") cipher = BlockCipher( ciphers.Camellia(binascii.unhexlify(key)), modes.OFB(binascii.unhexlify(iv)), @@ -89,9 +88,9 @@ class TestCamelliaCFB(object): "camellia-cfb.txt", ] ) - def test_OpenSSL(self, key, iv, plaintext, ciphertext): - if not api.supports_cipher('camellia-128-cfb'): - pytest.skip() + def test_OpenSSL(self, key, iv, plaintext, ciphertext, api): + if not api.supports_cipher("camellia-128-cfb"): + pytest.skip("Does not support Camellia CFB") cipher = BlockCipher( ciphers.Camellia(binascii.unhexlify(key)), modes.CFB(binascii.unhexlify(iv)), -- cgit v1.2.3 From 20034b110a9e8b4e0b539bd0b8e28aa510ec9afc Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Tue, 15 Oct 2013 19:10:53 -0500 Subject: add pragma: no cover to handle coverage in the tests for the moment --- tests/primitives/test_openssl_vectors.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'tests/primitives/test_openssl_vectors.py') diff --git a/tests/primitives/test_openssl_vectors.py b/tests/primitives/test_openssl_vectors.py index acb982a5..d30efa5c 100644 --- a/tests/primitives/test_openssl_vectors.py +++ b/tests/primitives/test_openssl_vectors.py @@ -48,7 +48,7 @@ class TestCamelliaCBC(object): ) def test_OpenSSL(self, key, iv, plaintext, ciphertext, api): if not api.supports_cipher("camellia-128-cbc"): - pytest.skip("Does not support Camellia CBC") + pytest.skip("Does not support Camellia CBC") # pragma: no cover cipher = BlockCipher( ciphers.Camellia(binascii.unhexlify(key)), modes.CBC(binascii.unhexlify(iv)), @@ -69,7 +69,7 @@ class TestCamelliaOFB(object): ) def test_OpenSSL(self, key, iv, plaintext, ciphertext, api): if not api.supports_cipher("camellia-128-ofb"): - pytest.skip("Does not support Camellia OFB") + pytest.skip("Does not support Camellia OFB") # pragma: no cover cipher = BlockCipher( ciphers.Camellia(binascii.unhexlify(key)), modes.OFB(binascii.unhexlify(iv)), @@ -90,7 +90,7 @@ class TestCamelliaCFB(object): ) def test_OpenSSL(self, key, iv, plaintext, ciphertext, api): if not api.supports_cipher("camellia-128-cfb"): - pytest.skip("Does not support Camellia CFB") + pytest.skip("Does not support Camellia CFB") # pragma: no cover cipher = BlockCipher( ciphers.Camellia(binascii.unhexlify(key)), modes.CFB(binascii.unhexlify(iv)), -- cgit v1.2.3