aboutsummaryrefslogtreecommitdiffstats
path: root/tests/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/utils.py')
-rw-r--r--tests/utils.py146
1 files changed, 138 insertions, 8 deletions
diff --git a/tests/utils.py b/tests/utils.py
index 0d9567f9..35461821 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -11,12 +11,18 @@
# See the License for the specific language governing permissions and
# limitations under the License.
+from __future__ import absolute_import, division, print_function
+
import collections
-import os
+from contextlib import contextmanager
-import six
import pytest
+import six
+
+from cryptography.exceptions import UnsupportedAlgorithm
+import cryptography_vectors
+
HashVector = collections.namedtuple("HashVector", ["message", "digest"])
KeyedHashVector = collections.namedtuple(
@@ -63,11 +69,16 @@ def check_backend_support(item):
"backend")
+@contextmanager
+def raises_unsupported_algorithm(reason):
+ with pytest.raises(UnsupportedAlgorithm) as exc_info:
+ yield exc_info
+
+ assert exc_info.value._reason is reason
+
+
def load_vectors_from_file(filename, loader):
- base = os.path.join(
- os.path.dirname(__file__), "hazmat", "primitives", "vectors",
- )
- with open(os.path.join(base, filename), "r") as vector_file:
+ with cryptography_vectors.open_vector_file(filename) as vector_file:
return loader(vector_file)
@@ -188,7 +199,8 @@ def load_pkcs1_vectors(vector_data):
for line in vector_data:
if (
line.startswith("# PSS Example") or
- line.startswith("# PKCS#1 v1.5 Signature")
+ line.startswith("# OAEP Example") or
+ line.startswith("# PKCS#1 v1.5")
):
if example_vector:
for key, value in six.iteritems(example_vector):
@@ -199,15 +211,21 @@ def load_pkcs1_vectors(vector_data):
attr = None
example_vector = collections.defaultdict(list)
- if line.startswith("# Message to be signed"):
+ if line.startswith("# Message"):
attr = "message"
continue
elif line.startswith("# Salt"):
attr = "salt"
continue
+ elif line.startswith("# Seed"):
+ attr = "seed"
+ continue
elif line.startswith("# Signature"):
attr = "signature"
continue
+ elif line.startswith("# Encryption"):
+ attr = "encryption"
+ continue
elif (
example_vector and
line.startswith("# =============================================")
@@ -296,3 +314,115 @@ def load_pkcs1_vectors(vector_data):
if key is not None and attr is not None:
key[attr].append(line.strip())
return vectors
+
+
+def load_rsa_nist_vectors(vector_data):
+ test_data = None
+ p = None
+ salt_length = None
+ data = []
+
+ for line in vector_data:
+ line = line.strip()
+
+ # Blank lines and section headers are ignored
+ if not line or line.startswith("["):
+ continue
+
+ if line.startswith("# Salt len:"):
+ salt_length = int(line.split(":")[1].strip())
+ continue
+ elif line.startswith("#"):
+ continue
+
+ # Build our data using a simple Key = Value format
+ name, value = [c.strip() for c in line.split("=")]
+
+ if name == "n":
+ n = int(value, 16)
+ elif name == "e" and p is None:
+ e = int(value, 16)
+ elif name == "p":
+ p = int(value, 16)
+ elif name == "q":
+ q = int(value, 16)
+ elif name == "SHAAlg":
+ if p is None:
+ test_data = {
+ "modulus": n,
+ "public_exponent": e,
+ "salt_length": salt_length,
+ "algorithm": value,
+ "fail": False
+ }
+ else:
+ test_data = {
+ "modulus": n,
+ "p": p,
+ "q": q,
+ "algorithm": value
+ }
+ if salt_length is not None:
+ test_data["salt_length"] = salt_length
+ data.append(test_data)
+ elif name == "e" and p is not None:
+ test_data["public_exponent"] = int(value, 16)
+ elif name == "d":
+ test_data["private_exponent"] = int(value, 16)
+ elif name == "Result":
+ test_data["fail"] = value.startswith("F")
+ # For all other tokens we simply want the name, value stored in
+ # the dictionary
+ else:
+ test_data[name.lower()] = value.encode("ascii")
+
+ return data
+
+
+def load_fips_dsa_key_pair_vectors(vector_data):
+ """
+ Loads data out of the FIPS DSA KeyPair vector files.
+ """
+ vectors = []
+ # When reading_key_data is set to True it tells the loader to continue
+ # constructing dictionaries. We set reading_key_data to False during the
+ # blocks of the vectors of N=224 because we don't support it.
+ reading_key_data = True
+ for line in vector_data:
+ line = line.strip()
+
+ if not line or line.startswith("#"):
+ continue
+ elif line.startswith("[mod = L=1024"):
+ continue
+ elif line.startswith("[mod = L=2048, N=224"):
+ reading_key_data = False
+ continue
+ elif line.startswith("[mod = L=2048, N=256"):
+ reading_key_data = True
+ continue
+ elif line.startswith("[mod = L=3072"):
+ continue
+
+ if not reading_key_data:
+ continue
+
+ elif reading_key_data:
+ if line.startswith("P"):
+ vectors.append({'p': int(line.split("=")[1], 16)})
+ elif line.startswith("Q"):
+ vectors[-1]['q'] = int(line.split("=")[1], 16)
+ elif line.startswith("G"):
+ vectors[-1]['g'] = int(line.split("=")[1], 16)
+ elif line.startswith("X") and 'x' not in vectors[-1]:
+ vectors[-1]['x'] = int(line.split("=")[1], 16)
+ elif line.startswith("X") and 'x' in vectors[-1]:
+ vectors.append({'p': vectors[-1]['p'],
+ 'q': vectors[-1]['q'],
+ 'g': vectors[-1]['g'],
+ 'x': int(line.split("=")[1], 16)
+ })
+ elif line.startswith("Y"):
+ vectors[-1]['y'] = int(line.split("=")[1], 16)
+
+ return vectors