diff options
author | Alex Gaynor <alex.gaynor@gmail.com> | 2013-09-27 10:55:58 -0700 |
---|---|---|
committer | Alex Gaynor <alex.gaynor@gmail.com> | 2013-09-27 10:55:58 -0700 |
commit | cc97d3898fd9aef00e487344e5bfe5fba12f0fd6 (patch) | |
tree | e8367bfd3b2ca14650cff8ce38cd4157ba8d6774 /tests/utils.py | |
parent | 85dfb96c479ccf1279394c5354580b3195c8ac8a (diff) | |
parent | e580598228caa28544fdd4fa53a29bb870858421 (diff) | |
download | cryptography-cc97d3898fd9aef00e487344e5bfe5fba12f0fd6.tar.gz cryptography-cc97d3898fd9aef00e487344e5bfe5fba12f0fd6.tar.bz2 cryptography-cc97d3898fd9aef00e487344e5bfe5fba12f0fd6.zip |
Merge pull request #71 from reaperhulk/cryptrec-openssl-loader
add cryptrec and openssl test vector loaders + tests
Diffstat (limited to 'tests/utils.py')
-rw-r--r-- | tests/utils.py | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/tests/utils.py b/tests/utils.py index 9362464e..d06c9e3b 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -59,3 +59,66 @@ def load_nist_vectors_from_file(filename, op, fields): ) with open(os.path.join(base, filename), "r") as vector_file: return load_nist_vectors(vector_file, op, fields) + + +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, pt, 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(":") + params = ( + # key + vector[1].encode("ascii"), + # iv + vector[2].encode("ascii"), + # plaintext + vector[3].encode("ascii"), + # ciphertext + vector[4].encode("ascii") + ) + vectors.append(params) + return vectors |