Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a test utility functions to read & process NIST vector files #29

Merged
merged 1 commit into from
Aug 9, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions tests/primitives/vectors/NIST/AES/KAT/CBCGFSbox256.rsp
Original file line number Diff line number Diff line change
@@ -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

193 changes: 193 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -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",
),
]
48 changes: 48 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -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)
5 changes: 3 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ envlist = py26,py27,pypy,py32,py33,docs,pep8

[testenv]
deps = pytest-cov
commands = py.test --cov=cryptography/
commands = py.test --cov=cryptography/ --cov=tests/

[testenv:docs]
deps = sphinx
Expand All @@ -13,4 +13,5 @@ commands = sphinx-build -W -b html -d {envtmpdir}/doctrees . _build/html

[testenv:pep8]
deps = flake8
commands = flake8 cryptography/ tests/ docs/
# E128 continuation line under-indented for visual indent
commands = flake8 --ignore="E128" cryptography/ tests/ docs/