From 9e1a48b84cd3525f3469ca4c46a413d27f1aba78 Mon Sep 17 00:00:00 2001 From: Donald Stufft Date: Fri, 9 Aug 2013 00:32:30 -0400 Subject: Add a test utility functions to read & process NIST vector files --- .../vectors/NIST/AES/KAT/CBCGFSbox256.rsp | 71 ++++++++ tests/test_utils.py | 193 +++++++++++++++++++++ tests/utils.py | 48 +++++ 3 files changed, 312 insertions(+) create mode 100755 tests/primitives/vectors/NIST/AES/KAT/CBCGFSbox256.rsp create mode 100644 tests/test_utils.py create mode 100644 tests/utils.py (limited to 'tests') diff --git a/tests/primitives/vectors/NIST/AES/KAT/CBCGFSbox256.rsp b/tests/primitives/vectors/NIST/AES/KAT/CBCGFSbox256.rsp new file mode 100755 index 00000000..ad811435 --- /dev/null +++ b/tests/primitives/vectors/NIST/AES/KAT/CBCGFSbox256.rsp @@ -0,0 +1,71 @@ +# CAVS 11.1 +# Config info for aes_values +# AESVS GFSbox test data for CBC +# State : Encrypt and Decrypt +# Key Length : 256 +# Generated on Fri Apr 22 15:11:38 2011 + +[ENCRYPT] + +COUNT = 0 +KEY = 0000000000000000000000000000000000000000000000000000000000000000 +IV = 00000000000000000000000000000000 +PLAINTEXT = 014730f80ac625fe84f026c60bfd547d +CIPHERTEXT = 5c9d844ed46f9885085e5d6a4f94c7d7 + +COUNT = 1 +KEY = 0000000000000000000000000000000000000000000000000000000000000000 +IV = 00000000000000000000000000000000 +PLAINTEXT = 0b24af36193ce4665f2825d7b4749c98 +CIPHERTEXT = a9ff75bd7cf6613d3731c77c3b6d0c04 + +COUNT = 2 +KEY = 0000000000000000000000000000000000000000000000000000000000000000 +IV = 00000000000000000000000000000000 +PLAINTEXT = 761c1fe41a18acf20d241650611d90f1 +CIPHERTEXT = 623a52fcea5d443e48d9181ab32c7421 + +COUNT = 3 +KEY = 0000000000000000000000000000000000000000000000000000000000000000 +IV = 00000000000000000000000000000000 +PLAINTEXT = 8a560769d605868ad80d819bdba03771 +CIPHERTEXT = 38f2c7ae10612415d27ca190d27da8b4 + +COUNT = 4 +KEY = 0000000000000000000000000000000000000000000000000000000000000000 +IV = 00000000000000000000000000000000 +PLAINTEXT = 91fbef2d15a97816060bee1feaa49afe +CIPHERTEXT = 1bc704f1bce135ceb810341b216d7abe + +[DECRYPT] + +COUNT = 0 +KEY = 0000000000000000000000000000000000000000000000000000000000000000 +IV = 00000000000000000000000000000000 +CIPHERTEXT = 5c9d844ed46f9885085e5d6a4f94c7d7 +PLAINTEXT = 014730f80ac625fe84f026c60bfd547d + +COUNT = 1 +KEY = 0000000000000000000000000000000000000000000000000000000000000000 +IV = 00000000000000000000000000000000 +CIPHERTEXT = a9ff75bd7cf6613d3731c77c3b6d0c04 +PLAINTEXT = 0b24af36193ce4665f2825d7b4749c98 + +COUNT = 2 +KEY = 0000000000000000000000000000000000000000000000000000000000000000 +IV = 00000000000000000000000000000000 +CIPHERTEXT = 623a52fcea5d443e48d9181ab32c7421 +PLAINTEXT = 761c1fe41a18acf20d241650611d90f1 + +COUNT = 3 +KEY = 0000000000000000000000000000000000000000000000000000000000000000 +IV = 00000000000000000000000000000000 +CIPHERTEXT = 38f2c7ae10612415d27ca190d27da8b4 +PLAINTEXT = 8a560769d605868ad80d819bdba03771 + +COUNT = 4 +KEY = 0000000000000000000000000000000000000000000000000000000000000000 +IV = 00000000000000000000000000000000 +CIPHERTEXT = 1bc704f1bce135ceb810341b216d7abe +PLAINTEXT = 91fbef2d15a97816060bee1feaa49afe + diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 00000000..acb36d4c --- /dev/null +++ b/tests/test_utils.py @@ -0,0 +1,193 @@ +import textwrap + +from .utils import load_nist_vectors, load_nist_vectors_from_file + + +def test_load_nist_vectors_encrypt(): + vector_data = textwrap.dedent(""" + # CAVS 11.1 + # Config info for aes_values + # AESVS GFSbox test data for CBC + # State : Encrypt and Decrypt + # Key Length : 128 + # Generated on Fri Apr 22 15:11:33 2011 + + [ENCRYPT] + + COUNT = 0 + KEY = 00000000000000000000000000000000 + IV = 00000000000000000000000000000000 + PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6 + CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e + + COUNT = 1 + KEY = 00000000000000000000000000000000 + IV = 00000000000000000000000000000000 + PLAINTEXT = 9798c4640bad75c7c3227db910174e72 + CIPHERTEXT = a9a1631bf4996954ebc093957b234589 + + [DECRYPT] + + COUNT = 0 + KEY = 00000000000000000000000000000000 + IV = 00000000000000000000000000000000 + CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e + PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6 + + COUNT = 1 + KEY = 00000000000000000000000000000000 + IV = 00000000000000000000000000000000 + CIPHERTEXT = a9a1631bf4996954ebc093957b234589 + PLAINTEXT = 9798c4640bad75c7c3227db910174e72 + """).splitlines() + + assert load_nist_vectors(vector_data, "ENCRYPT", + ["key", "iv", "plaintext", "ciphertext"], + ) == [ + ( + "00000000000000000000000000000000", + "00000000000000000000000000000000", + "f34481ec3cc627bacd5dc3fb08f273e6", + "0336763e966d92595a567cc9ce537f5e", + ), + ( + "00000000000000000000000000000000", + "00000000000000000000000000000000", + "9798c4640bad75c7c3227db910174e72", + "a9a1631bf4996954ebc093957b234589", + ), + ] + + +def test_load_nist_vectors_decrypt(): + vector_data = textwrap.dedent(""" + # CAVS 11.1 + # Config info for aes_values + # AESVS GFSbox test data for CBC + # State : Encrypt and Decrypt + # Key Length : 128 + # Generated on Fri Apr 22 15:11:33 2011 + + [ENCRYPT] + + COUNT = 0 + KEY = 00000000000000000000000000000000 + IV = 00000000000000000000000000000000 + PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6 + CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e + + COUNT = 1 + KEY = 00000000000000000000000000000000 + IV = 00000000000000000000000000000000 + PLAINTEXT = 9798c4640bad75c7c3227db910174e72 + CIPHERTEXT = a9a1631bf4996954ebc093957b234589 + + [DECRYPT] + + COUNT = 0 + KEY = 00000000000000000000000000000000 + IV = 00000000000000000000000000000000 + CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e + PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6 + + COUNT = 1 + KEY = 00000000000000000000000000000000 + IV = 00000000000000000000000000000000 + CIPHERTEXT = a9a1631bf4996954ebc093957b234589 + PLAINTEXT = 9798c4640bad75c7c3227db910174e72 + """).splitlines() + + assert load_nist_vectors(vector_data, "DECRYPT", + ["key", "iv", "ciphertext", "plaintext"], + ) == [ + ( + "00000000000000000000000000000000", + "00000000000000000000000000000000", + "0336763e966d92595a567cc9ce537f5e", + "f34481ec3cc627bacd5dc3fb08f273e6", + ), + ( + "00000000000000000000000000000000", + "00000000000000000000000000000000", + "a9a1631bf4996954ebc093957b234589", + "9798c4640bad75c7c3227db910174e72", + ), + ] + + +def test_load_nist_vectors_from_file_encrypt(): + assert load_nist_vectors_from_file( + "AES/KAT/CBCGFSbox256.rsp", + "ENCRYPT", + ["key", "iv", "plaintext", "ciphertext"], + ) == [ + ( + "0000000000000000000000000000000000000000000000000000000000000000", + "00000000000000000000000000000000", + "014730f80ac625fe84f026c60bfd547d", + "5c9d844ed46f9885085e5d6a4f94c7d7", + ), + ( + "0000000000000000000000000000000000000000000000000000000000000000", + "00000000000000000000000000000000", + "0b24af36193ce4665f2825d7b4749c98", + "a9ff75bd7cf6613d3731c77c3b6d0c04", + ), + ( + "0000000000000000000000000000000000000000000000000000000000000000", + "00000000000000000000000000000000", + "761c1fe41a18acf20d241650611d90f1", + "623a52fcea5d443e48d9181ab32c7421", + ), + ( + "0000000000000000000000000000000000000000000000000000000000000000", + "00000000000000000000000000000000", + "8a560769d605868ad80d819bdba03771", + "38f2c7ae10612415d27ca190d27da8b4", + ), + ( + "0000000000000000000000000000000000000000000000000000000000000000", + "00000000000000000000000000000000", + "91fbef2d15a97816060bee1feaa49afe", + "1bc704f1bce135ceb810341b216d7abe", + ), + ] + + +def test_load_nist_vectors_from_file_decypt(): + assert load_nist_vectors_from_file( + "AES/KAT/CBCGFSbox256.rsp", + "DECRYPT", + ["key", "iv", "ciphertext", "plaintext"], + ) == [ + ( + "0000000000000000000000000000000000000000000000000000000000000000", + "00000000000000000000000000000000", + "5c9d844ed46f9885085e5d6a4f94c7d7", + "014730f80ac625fe84f026c60bfd547d", + ), + ( + "0000000000000000000000000000000000000000000000000000000000000000", + "00000000000000000000000000000000", + "a9ff75bd7cf6613d3731c77c3b6d0c04", + "0b24af36193ce4665f2825d7b4749c98", + ), + ( + "0000000000000000000000000000000000000000000000000000000000000000", + "00000000000000000000000000000000", + "623a52fcea5d443e48d9181ab32c7421", + "761c1fe41a18acf20d241650611d90f1", + ), + ( + "0000000000000000000000000000000000000000000000000000000000000000", + "00000000000000000000000000000000", + "38f2c7ae10612415d27ca190d27da8b4", + "8a560769d605868ad80d819bdba03771", + ), + ( + "0000000000000000000000000000000000000000000000000000000000000000", + "00000000000000000000000000000000", + "1bc704f1bce135ceb810341b216d7abe", + "91fbef2d15a97816060bee1feaa49afe", + ), + ] diff --git a/tests/utils.py b/tests/utils.py new file mode 100644 index 00000000..9fe35311 --- /dev/null +++ b/tests/utils.py @@ -0,0 +1,48 @@ +import os.path + + +def load_nist_vectors(vector_data, op, fields): + section, count, data = None, None, {} + + for line in vector_data: + line = line.strip() + + # Blank lines are ignored + if not line: + continue + + # Lines starting with # are comments + if line.startswith("#"): + continue + + # Look for section headers + if line.startswith("[") and line.endswith("]"): + section = line[1:-1] + data[section] = {} + continue + + # Build our data using a simple Key = Value format + name, value = line.split(" = ") + + # COUNT is a special token that indicates a new block of data + if name.upper() == "COUNT": + count = value + data[section][count] = {} + # For all other tokens we simply want the name, value stored in + # the dictionary + else: + data[section][count][name.lower()] = value + + # We want to test only for a particular operation + return [ + tuple(vector[1][f] for f in fields) + for vector in sorted(data[op].items(), key=lambda v: v[0]) + ] + + +def load_nist_vectors_from_file(filename, op, fields): + base = os.path.join( + os.path.dirname(__file__), "primitives", "vectors", "NIST", + ) + with open(os.path.join(base, filename), "r") as vector_file: + return load_nist_vectors(vector_file, op, fields) -- cgit v1.2.3