From 016eed1cc1cc26ff404ac31ed3858de362ca37f2 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Wed, 16 Oct 2013 14:16:04 -0700 Subject: Ported openssl vector tests --- tests/primitives/test_cryptrec.py | 4 +- tests/primitives/test_nist.py | 25 +++------ tests/primitives/test_openssl_vectors.py | 78 +++++++------------------- tests/primitives/utils.py | 9 +-- tests/test_utils.py | 96 ++++++++++++++++---------------- tests/utils.py | 17 ++---- 6 files changed, 88 insertions(+), 141 deletions(-) (limited to 'tests') diff --git a/tests/primitives/test_cryptrec.py b/tests/primitives/test_cryptrec.py index c8e0af0f..59d8b24b 100644 --- a/tests/primitives/test_cryptrec.py +++ b/tests/primitives/test_cryptrec.py @@ -18,6 +18,7 @@ Tests using the CRYPTREC (Camellia) Test Vectors from __future__ import absolute_import, division, print_function import binascii +import os from cryptography.primitives.block import ciphers, modes @@ -28,8 +29,7 @@ from ..utils import load_cryptrec_vectors_from_file class TestCamelliaECB(object): test_NTT = generate_encrypt_test( load_cryptrec_vectors_from_file, - "Camellia", - "NTT", + os.path.join("Camellia", "NTT"), ["camellia-128-ecb", "camellia-192-ecb", "camellia-256"], lambda key: ciphers.Camellia(binascii.unhexlify((key))), lambda key: modes.EBC(), diff --git a/tests/primitives/test_nist.py b/tests/primitives/test_nist.py index 0c9569f1..e7778f1f 100644 --- a/tests/primitives/test_nist.py +++ b/tests/primitives/test_nist.py @@ -18,6 +18,7 @@ Test using the NIST Test Vectors from __future__ import absolute_import, division, print_function import binascii +import os from cryptography.primitives.block import ciphers, modes @@ -28,8 +29,7 @@ from ..utils import load_nist_vectors_from_file class TestAES_CBC(object): test_KAT = generate_encrypt_test( lambda path: load_nist_vectors_from_file(path, "ENCRYPT"), - "AES", - "KAT", + os.path.join("AES", "KAT"), [ "CBCGFSbox128.rsp", "CBCGFSbox192.rsp", @@ -50,8 +50,7 @@ class TestAES_CBC(object): test_MMT = generate_encrypt_test( lambda path: load_nist_vectors_from_file(path, "ENCRYPT"), - "AES", - "MMT", + os.path.join("AES", "MMT"), [ "CBCMMT128.rsp", "CBCMMT192.rsp", @@ -65,8 +64,7 @@ class TestAES_CBC(object): class TestAES_ECB(object): test_KAT = generate_encrypt_test( lambda path: load_nist_vectors_from_file(path, "ENCRYPT"), - "AES", - "KAT", + os.path.join("AES", "KAT"), [ "ECBGFSbox128.rsp", "ECBGFSbox192.rsp", @@ -87,8 +85,7 @@ class TestAES_ECB(object): test_MMT = generate_encrypt_test( lambda path: load_nist_vectors_from_file(path, "ENCRYPT"), - "AES", - "MMT", + os.path.join("AES", "MMT"), [ "ECBMMT128.rsp", "ECBMMT192.rsp", @@ -102,8 +99,7 @@ class TestAES_ECB(object): class TestAES_OFB(object): test_KAT = generate_encrypt_test( lambda path: load_nist_vectors_from_file(path, "ENCRYPT"), - "AES", - "KAT", + os.path.join("AES", "KAT"), [ "OFBGFSbox128.rsp", "OFBGFSbox192.rsp", @@ -124,8 +120,7 @@ class TestAES_OFB(object): test_MMT = generate_encrypt_test( lambda path: load_nist_vectors_from_file(path, "ENCRYPT"), - "AES", - "MMT", + os.path.join("AES", "MMT"), [ "OFBMMT128.rsp", "OFBMMT192.rsp", @@ -139,8 +134,7 @@ class TestAES_OFB(object): class TestAES_CFB(object): test_KAT = generate_encrypt_test( lambda path: load_nist_vectors_from_file(path, "ENCRYPT"), - "AES", - "KAT", + os.path.join("AES", "KAT"), [ "CFB128GFSbox128.rsp", "CFB128GFSbox192.rsp", @@ -162,8 +156,7 @@ class TestAES_CFB(object): test_MMT = generate_encrypt_test( lambda path: load_nist_vectors_from_file(path, "ENCRYPT"), - "AES", - "MMT", + os.path.join("AES", "MMT"), [ "CFB128MMT128.rsp", "CFB128MMT192.rsp", diff --git a/tests/primitives/test_openssl_vectors.py b/tests/primitives/test_openssl_vectors.py index d30efa5c..f9c4e1fa 100644 --- a/tests/primitives/test_openssl_vectors.py +++ b/tests/primitives/test_openssl_vectors.py @@ -23,78 +23,40 @@ import os import pytest -from cryptography.primitives.block import BlockCipher, ciphers, modes +from cryptography.primitives.block import ciphers, modes +from .utils import generate_encrypt_test from ..utils import load_openssl_vectors_from_file -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 - )) - ) - - class TestCamelliaCBC(object): - - @parameterize_encrypt_test( + test_OpenSSL = generate_encrypt_test( + load_openssl_vectors_from_file, "Camellia", - ("key", "iv", "plaintext", "ciphertext"), - [ - "camellia-cbc.txt", - ] + ["camellia-cbc.txt"], + lambda key, iv: ciphers.Camellia(binascii.unhexlify(key)), + lambda key, iv: modes.CBC(binascii.unhexlify(iv)), + only_if=lambda api: api.supports_cipher("camellia-128-cbc") ) - def test_OpenSSL(self, key, iv, plaintext, ciphertext, api): - if not api.supports_cipher("camellia-128-cbc"): - pytest.skip("Does not support Camellia CBC") # pragma: no cover - 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 class TestCamelliaOFB(object): - - @parameterize_encrypt_test( + test_OpenSSL = generate_encrypt_test( + load_openssl_vectors_from_file, "Camellia", - ("key", "iv", "plaintext", "ciphertext"), - [ - "camellia-ofb.txt", - ] + ["camellia-ofb.txt"], + lambda key, iv: ciphers.Camellia(binascii.unhexlify(key)), + lambda key, iv: modes.OFB(binascii.unhexlify(iv)), + only_if=lambda api: api.supports_cipher("camellia-128-ofb") ) - def test_OpenSSL(self, key, iv, plaintext, ciphertext, api): - if not api.supports_cipher("camellia-128-ofb"): - pytest.skip("Does not support Camellia OFB") # pragma: no cover - 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 class TestCamelliaCFB(object): - - @parameterize_encrypt_test( + test_OpenSSL = generate_encrypt_test( + load_openssl_vectors_from_file, "Camellia", - ("key", "iv", "plaintext", "ciphertext"), - [ - "camellia-cfb.txt", - ] + ["camellia-cfb.txt"], + lambda key, iv: ciphers.Camellia(binascii.unhexlify(key)), + lambda key, iv: modes.CFB(binascii.unhexlify(iv)), + only_if=lambda api: api.supports_cipher("camellia-128-cfb") ) - def test_OpenSSL(self, key, iv, plaintext, ciphertext, api): - if not api.supports_cipher("camellia-128-cfb"): - pytest.skip("Does not support Camellia CFB") # pragma: no cover - 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 diff --git a/tests/primitives/utils.py b/tests/primitives/utils.py index e35c915a..13bf0d89 100644 --- a/tests/primitives/utils.py +++ b/tests/primitives/utils.py @@ -7,18 +7,15 @@ from cryptography.bindings import openssl from cryptography.primitives.block import BlockCipher -def generate_encrypt_test(param_loader, cipher_name, vector_type, file_names, - cipher_factory, mode_factory, - only_if=lambda api: True): +def generate_encrypt_test(param_loader, path, file_names, cipher_factory, + mode_factory, only_if=lambda api: True): def test_encryption(self): for api in [openssl.api]: if not only_if(api): yield encrypt_skipped else: for file_name in file_names: - for params in param_loader( - os.path.join(cipher_name, vector_type, file_name) - ): + for params in param_loader(os.path.join(path, file_name)): yield encrypt_test, api, cipher_factory, mode_factory, params return test_encryption diff --git a/tests/test_utils.py b/tests/test_utils.py index 8141faf3..84e38571 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -304,30 +304,30 @@ def test_load_openssl_vectors(): ).splitlines() assert load_openssl_vectors(vector_data) == [ - ( - b"2B7E151628AED2A6ABF7158809CF4F3C", - b"000102030405060708090A0B0C0D0E0F", - b"6BC1BEE22E409F96E93D7E117393172A", - b"14F7646187817EB586599146B82BD719", - ), - ( - b"2B7E151628AED2A6ABF7158809CF4F3C", - b"14F7646187817EB586599146B82BD719", - b"AE2D8A571E03AC9C9EB76FAC45AF8E51", - b"A53D28BB82DF741103EA4F921A44880B", - ), - ( - b"2B7E151628AED2A6ABF7158809CF4F3C", - b"000102030405060708090A0B0C0D0E0F", - b"6BC1BEE22E409F96E93D7E117393172A", - b"14F7646187817EB586599146B82BD719", - ), - ( - b"2B7E151628AED2A6ABF7158809CF4F3C", - b"14F7646187817EB586599146B82BD719", - b"AE2D8A571E03AC9C9EB76FAC45AF8E51", - b"A53D28BB82DF741103EA4F921A44880B", - ), + { + "key": b"2B7E151628AED2A6ABF7158809CF4F3C", + "iv": b"000102030405060708090A0B0C0D0E0F", + "plaintext": b"6BC1BEE22E409F96E93D7E117393172A", + "ciphertext": b"14F7646187817EB586599146B82BD719", + }, + { + "key": b"2B7E151628AED2A6ABF7158809CF4F3C", + "iv": b"14F7646187817EB586599146B82BD719", + "plaintext": b"AE2D8A571E03AC9C9EB76FAC45AF8E51", + "ciphertext": b"A53D28BB82DF741103EA4F921A44880B", + }, + { + "key": b"2B7E151628AED2A6ABF7158809CF4F3C", + "iv": b"000102030405060708090A0B0C0D0E0F", + "plaintext": b"6BC1BEE22E409F96E93D7E117393172A", + "ciphertext": b"14F7646187817EB586599146B82BD719", + }, + { + "key": b"2B7E151628AED2A6ABF7158809CF4F3C", + "iv": b"14F7646187817EB586599146B82BD719", + "plaintext": b"AE2D8A571E03AC9C9EB76FAC45AF8E51", + "ciphertext": b"A53D28BB82DF741103EA4F921A44880B", + }, ] @@ -335,28 +335,28 @@ def test_load_openssl_vectors_from_file(): test_list = load_openssl_vectors_from_file("Camellia/camellia-ofb.txt") assert len(test_list) == 24 assert test_list[:4] == [ - ( - b"2B7E151628AED2A6ABF7158809CF4F3C", - b"000102030405060708090A0B0C0D0E0F", - b"6BC1BEE22E409F96E93D7E117393172A", - b"14F7646187817EB586599146B82BD719", - ), - ( - b"2B7E151628AED2A6ABF7158809CF4F3C", - b"50FE67CC996D32B6DA0937E99BAFEC60", - b"AE2D8A571E03AC9C9EB76FAC45AF8E51", - b"25623DB569CA51E01482649977E28D84", - ), - ( - b"2B7E151628AED2A6ABF7158809CF4F3C", - b"D9A4DADA0892239F6B8B3D7680E15674", - b"30C81C46A35CE411E5FBC1191A0A52EF", - b"C776634A60729DC657D12B9FCA801E98", - ), - ( - b"2B7E151628AED2A6ABF7158809CF4F3C", - b"A78819583F0308E7A6BF36B1386ABF23", - b"F69F2445DF4F9B17AD2B417BE66C3710", - b"D776379BE0E50825E681DA1A4C980E8E", - ), + { + "key": b"2B7E151628AED2A6ABF7158809CF4F3C", + "iv": b"000102030405060708090A0B0C0D0E0F", + "plaintext": b"6BC1BEE22E409F96E93D7E117393172A", + "ciphertext": b"14F7646187817EB586599146B82BD719", + }, + { + "key": b"2B7E151628AED2A6ABF7158809CF4F3C", + "iv": b"50FE67CC996D32B6DA0937E99BAFEC60", + "plaintext": b"AE2D8A571E03AC9C9EB76FAC45AF8E51", + "ciphertext": b"25623DB569CA51E01482649977E28D84", + }, + { + "key": b"2B7E151628AED2A6ABF7158809CF4F3C", + "iv": b"D9A4DADA0892239F6B8B3D7680E15674", + "plaintext": b"30C81C46A35CE411E5FBC1191A0A52EF", + "ciphertext": b"C776634A60729DC657D12B9FCA801E98", + }, + { + "key": b"2B7E151628AED2A6ABF7158809CF4F3C", + "iv": b"A78819583F0308E7A6BF36B1386ABF23", + "plaintext": b"F69F2445DF4F9B17AD2B417BE66C3710", + "ciphertext": b"D776379BE0E50825E681DA1A4C980E8E", + }, ] diff --git a/tests/utils.py b/tests/utils.py index 22208225..6b1cfd79 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -112,15 +112,10 @@ def load_openssl_vectors(vector_data): continue vector = line.split(":") - params = ( - # key - vector[1].encode("ascii"), - # iv - vector[2].encode("ascii"), - # plaintext - vector[3].encode("ascii"), - # ciphertext - vector[4].encode("ascii") - ) - vectors.append(params) + vectors.append({ + "key": vector[1].encode("ascii"), + "iv": vector[2].encode("ascii"), + "plaintext": vector[3].encode("ascii"), + "ciphertext": vector[4].encode("ascii"), + }) return vectors -- cgit v1.2.3