diff options
author | Alex Stapleton <alexs@prol.etari.at> | 2014-02-02 19:30:03 +0000 |
---|---|---|
committer | Alex Stapleton <alexs@prol.etari.at> | 2014-02-05 18:06:16 +0000 |
commit | 58f27accf2fb6329922e20266d4ccb5b2a5d0fa2 (patch) | |
tree | c7ff7cb1969693e1740d6c9c060180c06c7d08ee /tests/utils.py | |
parent | 48ef0c1a319ea3787ac865d67c15dc303c588d70 (diff) | |
download | cryptography-58f27accf2fb6329922e20266d4ccb5b2a5d0fa2.tar.gz cryptography-58f27accf2fb6329922e20266d4ccb5b2a5d0fa2.tar.bz2 cryptography-58f27accf2fb6329922e20266d4ccb5b2a5d0fa2.zip |
PKCS #1 RSA test vector loader
Diffstat (limited to 'tests/utils.py')
-rw-r--r-- | tests/utils.py | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/tests/utils.py b/tests/utils.py index 5c0e524f..408b05f6 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -14,6 +14,7 @@ import collections import os +import six import pytest @@ -191,3 +192,79 @@ def load_hash_vectors(vector_data): else: raise ValueError("Unknown line in hash vector") return vectors + + +def load_pkcs1_vectors(vector_data): + """ + Loads data out of RSA PKCS #1 vector files. + + Currently only returns the key pairs. + """ + private_key_vector = None + public_key_vector = None + attr = None + key = None + vectors = [] + for line in vector_data: + if ( + line.startswith("# Example") or + line.startswith("# =============================================") + ): + if key: + assert private_key_vector + assert public_key_vector + + for key, value in six.iteritems(public_key_vector): + hex_str = "".join(value).replace(" ", "") + public_key_vector[key] = int(hex_str, 16) + + for key, value in six.iteritems(private_key_vector): + hex_str = "".join(value).replace(" ", "") + private_key_vector[key] = int(hex_str, 16) + + assert ( + private_key_vector['public_exponent'] == + public_key_vector['public_exponent'] + ) + + assert ( + private_key_vector['modulus'] == + public_key_vector['modulus'] + ) + + vectors.append( + (private_key_vector, public_key_vector) + ) + + public_key_vector = collections.defaultdict(list) + private_key_vector = collections.defaultdict(list) + key = None + attr = None + + if private_key_vector is None or public_key_vector is None: + continue + + if line.startswith("# Private key"): + key = private_key_vector + elif line.startswith("# Public key"): + key = public_key_vector + elif line.startswith("# Modulus:"): + attr = "modulus" + elif line.startswith("# Public exponent:"): + attr = "public_exponent" + elif line.startswith("# Exponent:"): + if key is public_key_vector: + attr = "public_exponent" + else: + assert key is private_key_vector + attr = "private_exponent" + elif line.startswith("# Prime 1:"): + attr = "p" + elif line.startswith("# Prime 2:"): + attr = "q" + elif line.startswith("#"): + attr = None + else: + if key is not None and attr is not None: + key[attr].append(line.strip()) + return vectors |