From 69e0652b1a48815c7d61fed8b625264419ac2796 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Fri, 18 Oct 2013 17:28:39 -0500 Subject: hash vector loader and tests --- tests/utils.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'tests/utils.py') diff --git a/tests/utils.py b/tests/utils.py index 6b1cfd79..cf255334 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -119,3 +119,38 @@ def load_openssl_vectors(vector_data): "ciphertext": vector[4].encode("ascii"), }) return vectors + + +def load_hash_vectors(vector_data): + vectors = [] + + for line in vector_data: + line = line.strip() + + if not line or line.startswith("#"): + continue + + if line.startswith("Len"): + length = int(line.split(" = ")[1]) + elif line.startswith("Msg"): + """ + In the NIST vectors they have chosen to represent an empty + string as hex 00, which is of course not actually an empty + string. So we parse the provided length and catch this edge case. + """ + msg = line.split(" = ")[1].encode("ascii") if length > 0 else b"" + elif line.startswith("MD"): + md = line.split(" = ")[1] + # after MD is found the Msg+MD tuple is complete + vectors.append((msg, md)) + else: + raise ValueError("Unknown line in hash vector") + return vectors + + +def load_hash_vectors_from_file(filename): + base = os.path.join( + os.path.dirname(__file__), "primitives", "vectors" + ) + with open(os.path.join(base, filename), "r") as vector_file: + return load_hash_vectors(vector_file) -- cgit v1.2.3 From 87cd0db396e157b3fb160b8b6fd770e2c661ace2 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Fri, 18 Oct 2013 18:01:26 -0500 Subject: update loader and test to handle lines with brackets --- tests/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/utils.py') diff --git a/tests/utils.py b/tests/utils.py index cf255334..03b780f8 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -127,7 +127,7 @@ def load_hash_vectors(vector_data): for line in vector_data: line = line.strip() - if not line or line.startswith("#"): + if not line or line.startswith("#") or line.startswith("["): continue if line.startswith("Len"): -- cgit v1.2.3 From 3359d7efdc5e96ae25c69226a2a1452a8f6641f7 Mon Sep 17 00:00:00 2001 From: Donald Stufft Date: Sat, 19 Oct 2013 19:33:06 -0400 Subject: Test what happens when an invalid line is in the cryptrec vectors --- tests/utils.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'tests/utils.py') diff --git a/tests/utils.py b/tests/utils.py index 03b780f8..fa7cc68d 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -90,6 +90,8 @@ def load_cryptrec_vectors(vector_data): "plaintext": pt, "ciphertext": ct }) + else: + raise ValueError("Invalid line in file '{}'".format(line)) return cryptrec_list -- cgit v1.2.3