aboutsummaryrefslogtreecommitdiffstats
path: root/tests/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/utils.py')
-rw-r--r--tests/utils.py78
1 files changed, 69 insertions, 9 deletions
diff --git a/tests/utils.py b/tests/utils.py
index 9362464e..6b1cfd79 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -14,7 +14,7 @@
import os.path
-def load_nist_vectors(vector_data, op, fields):
+def load_nist_vectors(vector_data, op):
section, count, data = None, None, {}
for line in vector_data:
@@ -44,18 +44,78 @@ def load_nist_vectors(vector_data, op, fields):
# For all other tokens we simply want the name, value stored in
# the dictionary
else:
- data[section][count][name.lower()] = value
+ data[section][count][name.lower()] = value.encode("ascii")
- # We want to test only for a particular operation
- return [
- tuple(vector[1][f].encode("ascii") for f in fields)
- for vector in sorted(data[op].items(), key=lambda v: v[0])
- ]
+ # We want to test only for a particular operation, we sort them for the
+ # benefit of the tests of this function.
+ return [v for k, v in sorted(data[op].items(), key=lambda kv: kv[0])]
-def load_nist_vectors_from_file(filename, op, fields):
+def load_nist_vectors_from_file(filename, op):
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)
+ return load_nist_vectors(vector_file, op)
+
+
+def load_cryptrec_vectors_from_file(filename):
+ base = os.path.join(
+ os.path.dirname(__file__), "primitives", "vectors", "CRYPTREC",
+ )
+ with open(os.path.join(base, filename), "r") as vector_file:
+ return load_cryptrec_vectors(vector_file)
+
+
+def load_cryptrec_vectors(vector_data):
+ cryptrec_list = []
+
+ for line in vector_data:
+ line = line.strip()
+
+ # Blank lines and comments are ignored
+ if not line or line.startswith("#"):
+ continue
+
+ if line.startswith("K"):
+ key = line.split(" : ")[1].replace(" ", "").encode("ascii")
+ elif line.startswith("P"):
+ pt = line.split(" : ")[1].replace(" ", "").encode("ascii")
+ elif line.startswith("C"):
+ ct = line.split(" : ")[1].replace(" ", "").encode("ascii")
+ # after a C is found the K+P+C tuple is complete
+ # there are many P+C pairs for each K
+ cryptrec_list.append({
+ "key": key,
+ "plaintext": pt,
+ "ciphertext": ct
+ })
+ return cryptrec_list
+
+
+def load_openssl_vectors_from_file(filename):
+ base = os.path.join(
+ os.path.dirname(__file__), "primitives", "vectors", "OpenSSL",
+ )
+ with open(os.path.join(base, filename), "r") as vector_file:
+ return load_openssl_vectors(vector_file)
+
+
+def load_openssl_vectors(vector_data):
+ vectors = []
+
+ for line in vector_data:
+ line = line.strip()
+
+ # Blank lines and comments are ignored
+ if not line or line.startswith("#"):
+ continue
+
+ vector = line.split(":")
+ vectors.append({
+ "key": vector[1].encode("ascii"),
+ "iv": vector[2].encode("ascii"),
+ "plaintext": vector[3].encode("ascii"),
+ "ciphertext": vector[4].encode("ascii"),
+ })
+ return vectors