From aef7ee86b8568f33586dfef750c932e7fc8bfd58 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Thu, 8 Aug 2013 22:31:11 -0700 Subject: Reduce code duplication --- tests/primitives/test_nist.py | 99 +++++++++---------------------------------- 1 file changed, 19 insertions(+), 80 deletions(-) (limited to 'tests/primitives/test_nist.py') diff --git a/tests/primitives/test_nist.py b/tests/primitives/test_nist.py index eb46a846..d68cd27b 100644 --- a/tests/primitives/test_nist.py +++ b/tests/primitives/test_nist.py @@ -2,6 +2,7 @@ Test using the NIST Test Vectors """ import binascii +import os import pytest @@ -10,14 +11,18 @@ from cryptography.primitives.block import BlockCipher, ciphers, modes, padding from ..utils import load_nist_vectors_from_file -class TestAES_CBC(object): - @pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"), +def parameterize_encrypt(fname): + return pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"), load_nist_vectors_from_file( - "AES/KAT/CBCGFSbox128.rsp", + os.path.join("AES/KAT/", fname), "ENCRYPT", ["key", "iv", "plaintext", "ciphertext"], ), ) + + +class TestAES_CBC(object): + @parameterize_encrypt("CBCGFSbox128.rsp") def test_KAT_GFSbox_128_encrypt(self, key, iv, plaintext, ciphertext): cipher = BlockCipher( ciphers.AES(binascii.unhexlify(key)), @@ -26,13 +31,7 @@ class TestAES_CBC(object): actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() assert binascii.hexlify(actual_ciphertext) == ciphertext - @pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"), - load_nist_vectors_from_file( - "AES/KAT/CBCGFSbox192.rsp", - "ENCRYPT", - ["key", "iv", "plaintext", "ciphertext"], - ), - ) + @parameterize_encrypt("CBCGFSbox192.rsp") def test_KAT_GFSbox_192_encrypt(self, key, iv, plaintext, ciphertext): cipher = BlockCipher( ciphers.AES(binascii.unhexlify(key)), @@ -41,13 +40,7 @@ class TestAES_CBC(object): actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() assert binascii.hexlify(actual_ciphertext) == ciphertext - @pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"), - load_nist_vectors_from_file( - "AES/KAT/CBCGFSbox256.rsp", - "ENCRYPT", - ["key", "iv", "plaintext", "ciphertext"], - ), - ) + @parameterize_encrypt("CBCGFSbox256.rsp") def test_KAT_GFSbox_256_encrypt(self, key, iv, plaintext, ciphertext): cipher = BlockCipher( ciphers.AES(binascii.unhexlify(key)), @@ -56,13 +49,7 @@ class TestAES_CBC(object): actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() assert binascii.hexlify(actual_ciphertext) == ciphertext - @pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"), - load_nist_vectors_from_file( - "AES/KAT/CBCKeySbox128.rsp", - "ENCRYPT", - ["key", "iv", "plaintext", "ciphertext"], - ), - ) + @parameterize_encrypt("CBCKeySbox128.rsp") def test_KAT_KeySbox_128_encrypt(self, key, iv, plaintext, ciphertext): cipher = BlockCipher( ciphers.AES(binascii.unhexlify(key)), @@ -71,13 +58,7 @@ class TestAES_CBC(object): actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() assert binascii.hexlify(actual_ciphertext) == ciphertext - @pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"), - load_nist_vectors_from_file( - "AES/KAT/CBCKeySbox192.rsp", - "ENCRYPT", - ["key", "iv", "plaintext", "ciphertext"], - ), - ) + @parameterize_encrypt("CBCKeySbox192.rsp") def test_KAT_KeySbox_192_encrypt(self, key, iv, plaintext, ciphertext): cipher = BlockCipher( ciphers.AES(binascii.unhexlify(key)), @@ -86,13 +67,7 @@ class TestAES_CBC(object): actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() assert binascii.hexlify(actual_ciphertext) == ciphertext - @pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"), - load_nist_vectors_from_file( - "AES/KAT/CBCKeySbox256.rsp", - "ENCRYPT", - ["key", "iv", "plaintext", "ciphertext"], - ), - ) + @parameterize_encrypt("CBCKeySbox256.rsp") def test_KAT_KeySbox_256_encrypt(self, key, iv, plaintext, ciphertext): cipher = BlockCipher( ciphers.AES(binascii.unhexlify(key)), @@ -101,13 +76,7 @@ class TestAES_CBC(object): actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() assert binascii.hexlify(actual_ciphertext) == ciphertext - @pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"), - load_nist_vectors_from_file( - "AES/KAT/CBCVarKey128.rsp", - "ENCRYPT", - ["key", "iv", "plaintext", "ciphertext"], - ), - ) + @parameterize_encrypt("CBCVarKey128.rsp") def test_KAT_VarKey_128_encrypt(self, key, iv, plaintext, ciphertext): cipher = BlockCipher( ciphers.AES(binascii.unhexlify(key)), @@ -116,13 +85,7 @@ class TestAES_CBC(object): actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() assert binascii.hexlify(actual_ciphertext) == ciphertext - @pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"), - load_nist_vectors_from_file( - "AES/KAT/CBCVarKey192.rsp", - "ENCRYPT", - ["key", "iv", "plaintext", "ciphertext"], - ), - ) + @parameterize_encrypt("CBCVarKey192.rsp") def test_KAT_VarKey_192_encrypt(self, key, iv, plaintext, ciphertext): cipher = BlockCipher( ciphers.AES(binascii.unhexlify(key)), @@ -131,13 +94,7 @@ class TestAES_CBC(object): actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() assert binascii.hexlify(actual_ciphertext) == ciphertext - @pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"), - load_nist_vectors_from_file( - "AES/KAT/CBCVarKey256.rsp", - "ENCRYPT", - ["key", "iv", "plaintext", "ciphertext"], - ), - ) + @parameterize_encrypt("CBCVarKey256.rsp") def test_KAT_VarKey_256_encrypt(self, key, iv, plaintext, ciphertext): cipher = BlockCipher( ciphers.AES(binascii.unhexlify(key)), @@ -146,13 +103,7 @@ class TestAES_CBC(object): actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() assert binascii.hexlify(actual_ciphertext) == ciphertext - @pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"), - load_nist_vectors_from_file( - "AES/KAT/CBCVarTxt128.rsp", - "ENCRYPT", - ["key", "iv", "plaintext", "ciphertext"], - ), - ) + @parameterize_encrypt("CBCVarTxt128.rsp") def test_KAT_VarTxt_128_encrypt(self, key, iv, plaintext, ciphertext): cipher = BlockCipher( ciphers.AES(binascii.unhexlify(key)), @@ -161,13 +112,7 @@ class TestAES_CBC(object): actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() assert binascii.hexlify(actual_ciphertext) == ciphertext - @pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"), - load_nist_vectors_from_file( - "AES/KAT/CBCVarTxt192.rsp", - "ENCRYPT", - ["key", "iv", "plaintext", "ciphertext"], - ), - ) + @parameterize_encrypt("CBCVarTxt192.rsp") def test_KAT_VarTxt_192_encrypt(self, key, iv, plaintext, ciphertext): cipher = BlockCipher( ciphers.AES(binascii.unhexlify(key)), @@ -176,13 +121,7 @@ class TestAES_CBC(object): actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() assert binascii.hexlify(actual_ciphertext) == ciphertext - @pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"), - load_nist_vectors_from_file( - "AES/KAT/CBCVarTxt256.rsp", - "ENCRYPT", - ["key", "iv", "plaintext", "ciphertext"], - ), - ) + @parameterize_encrypt("CBCVarTxt256.rsp") def test_KAT_VarTxt_256_encrypt(self, key, iv, plaintext, ciphertext): cipher = BlockCipher( ciphers.AES(binascii.unhexlify(key)), -- cgit v1.2.3