Alex Gaynor | 5951f46 | 2014-11-16 09:08:42 -0800 | [diff] [blame^] | 1 | # This file is dual licensed under the terms of the Apache License, Version |
| 2 | # 2.0, and the BSD License. See the LICENSE file in the root of this repository |
| 3 | # for complete details. |
Alex Gaynor | f312a5c | 2013-08-10 15:23:38 -0400 | [diff] [blame] | 4 | |
Alex Gaynor | c37feed | 2014-03-08 08:32:56 -0800 | [diff] [blame] | 5 | from __future__ import absolute_import, division, print_function |
| 6 | |
Alex Stapleton | c387cf7 | 2014-04-13 13:58:02 +0100 | [diff] [blame] | 7 | import binascii |
Alex Gaynor | 36e651c | 2014-01-27 10:08:35 -0800 | [diff] [blame] | 8 | import collections |
Alex Stapleton | c387cf7 | 2014-04-13 13:58:02 +0100 | [diff] [blame] | 9 | import re |
Alex Stapleton | 707b008 | 2014-04-20 22:24:41 +0100 | [diff] [blame] | 10 | from contextlib import contextmanager |
Paul Kehrer | 90450f3 | 2014-03-19 12:37:17 -0400 | [diff] [blame] | 11 | |
Paul Kehrer | a409ae1 | 2014-04-30 13:28:28 -0500 | [diff] [blame] | 12 | from pyasn1.codec.der import encoder |
Paul Kehrer | d3e3df9 | 2014-04-30 11:13:17 -0500 | [diff] [blame] | 13 | from pyasn1.type import namedtype, univ |
| 14 | |
Alex Stapleton | a39a319 | 2014-03-14 20:03:12 +0000 | [diff] [blame] | 15 | import pytest |
| 16 | |
Paul Kehrer | afc1ccd | 2014-03-19 11:49:32 -0400 | [diff] [blame] | 17 | import six |
Alex Gaynor | 2b3f942 | 2013-12-24 21:55:24 -0800 | [diff] [blame] | 18 | |
Alex Gaynor | 7a489db | 2014-03-22 15:09:34 -0700 | [diff] [blame] | 19 | from cryptography.exceptions import UnsupportedAlgorithm |
Alex Gaynor | 07c4dcc | 2014-04-05 11:22:07 -0700 | [diff] [blame] | 20 | |
Alex Stapleton | a39a319 | 2014-03-14 20:03:12 +0000 | [diff] [blame] | 21 | import cryptography_vectors |
Matthew Iversen | 68e77c7 | 2014-03-13 08:54:43 +1100 | [diff] [blame] | 22 | |
Alex Gaynor | 2b3f942 | 2013-12-24 21:55:24 -0800 | [diff] [blame] | 23 | |
Alex Gaynor | 36e651c | 2014-01-27 10:08:35 -0800 | [diff] [blame] | 24 | HashVector = collections.namedtuple("HashVector", ["message", "digest"]) |
| 25 | KeyedHashVector = collections.namedtuple( |
| 26 | "KeyedHashVector", ["message", "digest", "key"] |
| 27 | ) |
| 28 | |
| 29 | |
Paul Kehrer | c421e63 | 2014-01-18 09:22:21 -0600 | [diff] [blame] | 30 | def select_backends(names, backend_list): |
| 31 | if names is None: |
| 32 | return backend_list |
| 33 | split_names = [x.strip() for x in names.split(',')] |
Paul Kehrer | aed9e17 | 2014-01-19 12:09:27 -0600 | [diff] [blame] | 34 | selected_backends = [] |
| 35 | for backend in backend_list: |
| 36 | if backend.name in split_names: |
| 37 | selected_backends.append(backend) |
Paul Kehrer | c421e63 | 2014-01-18 09:22:21 -0600 | [diff] [blame] | 38 | |
Paul Kehrer | aed9e17 | 2014-01-19 12:09:27 -0600 | [diff] [blame] | 39 | if len(selected_backends) > 0: |
| 40 | return selected_backends |
Paul Kehrer | c421e63 | 2014-01-18 09:22:21 -0600 | [diff] [blame] | 41 | else: |
| 42 | raise ValueError( |
| 43 | "No backend selected. Tried to select: {0}".format(split_names) |
| 44 | ) |
Paul Kehrer | 34c075e | 2014-01-13 21:52:08 -0500 | [diff] [blame] | 45 | |
| 46 | |
Paul Kehrer | 902d8cf | 2014-10-25 12:22:10 -0700 | [diff] [blame] | 47 | def skip_if_empty(backend_list, required_interfaces): |
| 48 | if not backend_list: |
| 49 | pytest.skip( |
| 50 | "No backends provided supply the interface: {0}".format( |
| 51 | ", ".join(iface.__name__ for iface in required_interfaces) |
| 52 | ) |
| 53 | ) |
| 54 | |
| 55 | |
Paul Kehrer | 60fc8da | 2013-12-26 20:19:34 -0600 | [diff] [blame] | 56 | def check_backend_support(item): |
Paul Kehrer | 5a8fdf8 | 2013-12-26 20:13:45 -0600 | [diff] [blame] | 57 | supported = item.keywords.get("supported") |
| 58 | if supported and "backend" in item.funcargs: |
| 59 | if not supported.kwargs["only_if"](item.funcargs["backend"]): |
Paul Kehrer | f03334e | 2014-01-02 23:16:14 -0600 | [diff] [blame] | 60 | pytest.skip("{0} ({1})".format( |
| 61 | supported.kwargs["skip_message"], item.funcargs["backend"] |
| 62 | )) |
Paul Kehrer | 5a8fdf8 | 2013-12-26 20:13:45 -0600 | [diff] [blame] | 63 | elif supported: |
Paul Kehrer | ec49550 | 2013-12-27 15:51:40 -0600 | [diff] [blame] | 64 | raise ValueError("This mark is only available on methods that take a " |
| 65 | "backend") |
Paul Kehrer | 5a8fdf8 | 2013-12-26 20:13:45 -0600 | [diff] [blame] | 66 | |
| 67 | |
Alex Gaynor | 7a489db | 2014-03-22 15:09:34 -0700 | [diff] [blame] | 68 | @contextmanager |
Alex Stapleton | 5e4c8c3 | 2014-03-27 16:38:00 +0000 | [diff] [blame] | 69 | def raises_unsupported_algorithm(reason): |
Alex Gaynor | 7a489db | 2014-03-22 15:09:34 -0700 | [diff] [blame] | 70 | with pytest.raises(UnsupportedAlgorithm) as exc_info: |
Alex Stapleton | 112963e | 2014-03-26 17:39:29 +0000 | [diff] [blame] | 71 | yield exc_info |
Alex Stapleton | 5e4c8c3 | 2014-03-27 16:38:00 +0000 | [diff] [blame] | 72 | |
Alex Stapleton | 85a791f | 2014-03-27 16:55:41 +0000 | [diff] [blame] | 73 | assert exc_info.value._reason is reason |
Alex Gaynor | 7a489db | 2014-03-22 15:09:34 -0700 | [diff] [blame] | 74 | |
| 75 | |
Paul Kehrer | d0dc6a3 | 2014-04-30 12:12:50 -0500 | [diff] [blame] | 76 | class _DSSSigValue(univ.Sequence): |
Paul Kehrer | d3e3df9 | 2014-04-30 11:13:17 -0500 | [diff] [blame] | 77 | componentType = namedtype.NamedTypes( |
| 78 | namedtype.NamedType('r', univ.Integer()), |
| 79 | namedtype.NamedType('s', univ.Integer()) |
| 80 | ) |
Paul Kehrer | 3fc686e | 2014-04-30 09:07:27 -0500 | [diff] [blame] | 81 | |
| 82 | |
Paul Kehrer | 14951f4 | 2014-04-30 12:14:48 -0500 | [diff] [blame] | 83 | def der_encode_dsa_signature(r, s): |
Paul Kehrer | d0dc6a3 | 2014-04-30 12:12:50 -0500 | [diff] [blame] | 84 | sig = _DSSSigValue() |
Paul Kehrer | d3e3df9 | 2014-04-30 11:13:17 -0500 | [diff] [blame] | 85 | sig.setComponentByName('r', r) |
| 86 | sig.setComponentByName('s', s) |
| 87 | return encoder.encode(sig) |
Paul Kehrer | 3fc686e | 2014-04-30 09:07:27 -0500 | [diff] [blame] | 88 | |
| 89 | |
Paul Kehrer | f7f6a9f | 2013-11-11 20:43:52 -0600 | [diff] [blame] | 90 | def load_vectors_from_file(filename, loader): |
Alex Stapleton | a39a319 | 2014-03-14 20:03:12 +0000 | [diff] [blame] | 91 | with cryptography_vectors.open_vector_file(filename) as vector_file: |
| 92 | return loader(vector_file) |
Paul Kehrer | f7f6a9f | 2013-11-11 20:43:52 -0600 | [diff] [blame] | 93 | |
| 94 | |
Alex Gaynor | d3ce703 | 2013-11-11 14:46:20 -0800 | [diff] [blame] | 95 | def load_nist_vectors(vector_data): |
Paul Kehrer | 749ac5b | 2013-11-18 18:12:41 -0600 | [diff] [blame] | 96 | test_data = None |
| 97 | data = [] |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 98 | |
| 99 | for line in vector_data: |
| 100 | line = line.strip() |
| 101 | |
Paul Kehrer | 749ac5b | 2013-11-18 18:12:41 -0600 | [diff] [blame] | 102 | # Blank lines, comments, and section headers are ignored |
| 103 | if not line or line.startswith("#") or (line.startswith("[") |
| 104 | and line.endswith("]")): |
Alex Gaynor | 521c42d | 2013-11-11 14:25:59 -0800 | [diff] [blame] | 105 | continue |
| 106 | |
Paul Kehrer | a43b669 | 2013-11-12 15:35:49 -0600 | [diff] [blame] | 107 | if line.strip() == "FAIL": |
Paul Kehrer | 749ac5b | 2013-11-18 18:12:41 -0600 | [diff] [blame] | 108 | test_data["fail"] = True |
Paul Kehrer | a43b669 | 2013-11-12 15:35:49 -0600 | [diff] [blame] | 109 | continue |
| 110 | |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 111 | # Build our data using a simple Key = Value format |
Paul Kehrer | a43b669 | 2013-11-12 15:35:49 -0600 | [diff] [blame] | 112 | name, value = [c.strip() for c in line.split("=")] |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 113 | |
Paul Kehrer | 1050ddf | 2014-01-27 21:04:03 -0600 | [diff] [blame] | 114 | # Some tests (PBKDF2) contain \0, which should be interpreted as a |
| 115 | # null character rather than literal. |
| 116 | value = value.replace("\\0", "\0") |
| 117 | |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 118 | # COUNT is a special token that indicates a new block of data |
| 119 | if name.upper() == "COUNT": |
Paul Kehrer | 749ac5b | 2013-11-18 18:12:41 -0600 | [diff] [blame] | 120 | test_data = {} |
| 121 | data.append(test_data) |
| 122 | continue |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 123 | # For all other tokens we simply want the name, value stored in |
| 124 | # the dictionary |
| 125 | else: |
Paul Kehrer | 749ac5b | 2013-11-18 18:12:41 -0600 | [diff] [blame] | 126 | test_data[name.lower()] = value.encode("ascii") |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 127 | |
Paul Kehrer | 749ac5b | 2013-11-18 18:12:41 -0600 | [diff] [blame] | 128 | return data |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 129 | |
| 130 | |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 131 | def load_cryptrec_vectors(vector_data): |
Paul Kehrer | e580598 | 2013-09-27 11:26:01 -0500 | [diff] [blame] | 132 | cryptrec_list = [] |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 133 | |
| 134 | for line in vector_data: |
| 135 | line = line.strip() |
| 136 | |
| 137 | # Blank lines and comments are ignored |
| 138 | if not line or line.startswith("#"): |
| 139 | continue |
| 140 | |
| 141 | if line.startswith("K"): |
Paul Kehrer | e580598 | 2013-09-27 11:26:01 -0500 | [diff] [blame] | 142 | key = line.split(" : ")[1].replace(" ", "").encode("ascii") |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 143 | elif line.startswith("P"): |
Paul Kehrer | e580598 | 2013-09-27 11:26:01 -0500 | [diff] [blame] | 144 | pt = line.split(" : ")[1].replace(" ", "").encode("ascii") |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 145 | elif line.startswith("C"): |
Paul Kehrer | e580598 | 2013-09-27 11:26:01 -0500 | [diff] [blame] | 146 | ct = line.split(" : ")[1].replace(" ", "").encode("ascii") |
| 147 | # after a C is found the K+P+C tuple is complete |
| 148 | # there are many P+C pairs for each K |
Alex Gaynor | 1fe70b1 | 2013-10-16 11:59:17 -0700 | [diff] [blame] | 149 | cryptrec_list.append({ |
| 150 | "key": key, |
| 151 | "plaintext": pt, |
| 152 | "ciphertext": ct |
| 153 | }) |
Donald Stufft | 3359d7e | 2013-10-19 19:33:06 -0400 | [diff] [blame] | 154 | else: |
| 155 | raise ValueError("Invalid line in file '{}'".format(line)) |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 156 | return cryptrec_list |
| 157 | |
| 158 | |
Paul Kehrer | 69e0652 | 2013-10-18 17:28:39 -0500 | [diff] [blame] | 159 | def load_hash_vectors(vector_data): |
| 160 | vectors = [] |
Paul Kehrer | 1bb8b71 | 2013-10-27 17:00:14 -0500 | [diff] [blame] | 161 | key = None |
| 162 | msg = None |
| 163 | md = None |
Paul Kehrer | 69e0652 | 2013-10-18 17:28:39 -0500 | [diff] [blame] | 164 | |
| 165 | for line in vector_data: |
| 166 | line = line.strip() |
| 167 | |
Paul Kehrer | 87cd0db | 2013-10-18 18:01:26 -0500 | [diff] [blame] | 168 | if not line or line.startswith("#") or line.startswith("["): |
Paul Kehrer | 69e0652 | 2013-10-18 17:28:39 -0500 | [diff] [blame] | 169 | continue |
| 170 | |
| 171 | if line.startswith("Len"): |
| 172 | length = int(line.split(" = ")[1]) |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 173 | elif line.startswith("Key"): |
Alex Gaynor | 36e651c | 2014-01-27 10:08:35 -0800 | [diff] [blame] | 174 | # HMAC vectors contain a key attribute. Hash vectors do not. |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 175 | key = line.split(" = ")[1].encode("ascii") |
Paul Kehrer | 69e0652 | 2013-10-18 17:28:39 -0500 | [diff] [blame] | 176 | elif line.startswith("Msg"): |
Alex Gaynor | 36e651c | 2014-01-27 10:08:35 -0800 | [diff] [blame] | 177 | # In the NIST vectors they have chosen to represent an empty |
| 178 | # string as hex 00, which is of course not actually an empty |
| 179 | # string. So we parse the provided length and catch this edge case. |
Paul Kehrer | 69e0652 | 2013-10-18 17:28:39 -0500 | [diff] [blame] | 180 | msg = line.split(" = ")[1].encode("ascii") if length > 0 else b"" |
| 181 | elif line.startswith("MD"): |
| 182 | md = line.split(" = ")[1] |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 183 | # after MD is found the Msg+MD (+ potential key) tuple is complete |
Paul Kehrer | 00dd509 | 2013-10-23 09:41:49 -0500 | [diff] [blame] | 184 | if key is not None: |
Alex Gaynor | 36e651c | 2014-01-27 10:08:35 -0800 | [diff] [blame] | 185 | vectors.append(KeyedHashVector(msg, md, key)) |
Paul Kehrer | 1bb8b71 | 2013-10-27 17:00:14 -0500 | [diff] [blame] | 186 | key = None |
| 187 | msg = None |
| 188 | md = None |
Paul Kehrer | 00dd509 | 2013-10-23 09:41:49 -0500 | [diff] [blame] | 189 | else: |
Alex Gaynor | 36e651c | 2014-01-27 10:08:35 -0800 | [diff] [blame] | 190 | vectors.append(HashVector(msg, md)) |
Paul Kehrer | 1bb8b71 | 2013-10-27 17:00:14 -0500 | [diff] [blame] | 191 | msg = None |
| 192 | md = None |
Paul Kehrer | 69e0652 | 2013-10-18 17:28:39 -0500 | [diff] [blame] | 193 | else: |
| 194 | raise ValueError("Unknown line in hash vector") |
| 195 | return vectors |
Alex Stapleton | 58f27ac | 2014-02-02 19:30:03 +0000 | [diff] [blame] | 196 | |
| 197 | |
| 198 | def load_pkcs1_vectors(vector_data): |
| 199 | """ |
| 200 | Loads data out of RSA PKCS #1 vector files. |
Alex Stapleton | 58f27ac | 2014-02-02 19:30:03 +0000 | [diff] [blame] | 201 | """ |
| 202 | private_key_vector = None |
| 203 | public_key_vector = None |
| 204 | attr = None |
| 205 | key = None |
Paul Kehrer | efca280 | 2014-02-17 20:55:13 -0600 | [diff] [blame] | 206 | example_vector = None |
| 207 | examples = [] |
Alex Stapleton | 58f27ac | 2014-02-02 19:30:03 +0000 | [diff] [blame] | 208 | vectors = [] |
| 209 | for line in vector_data: |
Paul Kehrer | 7774a03 | 2014-02-17 22:56:55 -0600 | [diff] [blame] | 210 | if ( |
| 211 | line.startswith("# PSS Example") or |
Paul Kehrer | 3fe9150 | 2014-03-29 12:08:39 -0500 | [diff] [blame] | 212 | line.startswith("# OAEP Example") or |
| 213 | line.startswith("# PKCS#1 v1.5") |
Paul Kehrer | 7774a03 | 2014-02-17 22:56:55 -0600 | [diff] [blame] | 214 | ): |
Paul Kehrer | efca280 | 2014-02-17 20:55:13 -0600 | [diff] [blame] | 215 | if example_vector: |
| 216 | for key, value in six.iteritems(example_vector): |
Paul Kehrer | 2681180 | 2014-02-19 16:32:11 -0600 | [diff] [blame] | 217 | hex_str = "".join(value).replace(" ", "").encode("ascii") |
Paul Kehrer | efca280 | 2014-02-17 20:55:13 -0600 | [diff] [blame] | 218 | example_vector[key] = hex_str |
| 219 | examples.append(example_vector) |
| 220 | |
| 221 | attr = None |
| 222 | example_vector = collections.defaultdict(list) |
| 223 | |
Paul Kehrer | 3fe9150 | 2014-03-29 12:08:39 -0500 | [diff] [blame] | 224 | if line.startswith("# Message"): |
Paul Kehrer | 7d9c306 | 2014-02-18 08:27:39 -0600 | [diff] [blame] | 225 | attr = "message" |
Paul Kehrer | efca280 | 2014-02-17 20:55:13 -0600 | [diff] [blame] | 226 | continue |
| 227 | elif line.startswith("# Salt"): |
| 228 | attr = "salt" |
| 229 | continue |
Paul Kehrer | 3fe9150 | 2014-03-29 12:08:39 -0500 | [diff] [blame] | 230 | elif line.startswith("# Seed"): |
| 231 | attr = "seed" |
| 232 | continue |
Paul Kehrer | efca280 | 2014-02-17 20:55:13 -0600 | [diff] [blame] | 233 | elif line.startswith("# Signature"): |
| 234 | attr = "signature" |
| 235 | continue |
Paul Kehrer | 3fe9150 | 2014-03-29 12:08:39 -0500 | [diff] [blame] | 236 | elif line.startswith("# Encryption"): |
| 237 | attr = "encryption" |
| 238 | continue |
Paul Kehrer | efca280 | 2014-02-17 20:55:13 -0600 | [diff] [blame] | 239 | elif ( |
| 240 | example_vector and |
| 241 | line.startswith("# =============================================") |
| 242 | ): |
| 243 | for key, value in six.iteritems(example_vector): |
Paul Kehrer | 2681180 | 2014-02-19 16:32:11 -0600 | [diff] [blame] | 244 | hex_str = "".join(value).replace(" ", "").encode("ascii") |
Paul Kehrer | efca280 | 2014-02-17 20:55:13 -0600 | [diff] [blame] | 245 | example_vector[key] = hex_str |
| 246 | examples.append(example_vector) |
| 247 | example_vector = None |
| 248 | attr = None |
| 249 | elif example_vector and line.startswith("#"): |
| 250 | continue |
| 251 | else: |
| 252 | if attr is not None and example_vector is not None: |
| 253 | example_vector[attr].append(line.strip()) |
| 254 | continue |
| 255 | |
Alex Stapleton | 58f27ac | 2014-02-02 19:30:03 +0000 | [diff] [blame] | 256 | if ( |
| 257 | line.startswith("# Example") or |
| 258 | line.startswith("# =============================================") |
| 259 | ): |
| 260 | if key: |
| 261 | assert private_key_vector |
| 262 | assert public_key_vector |
| 263 | |
| 264 | for key, value in six.iteritems(public_key_vector): |
| 265 | hex_str = "".join(value).replace(" ", "") |
| 266 | public_key_vector[key] = int(hex_str, 16) |
| 267 | |
| 268 | for key, value in six.iteritems(private_key_vector): |
| 269 | hex_str = "".join(value).replace(" ", "") |
| 270 | private_key_vector[key] = int(hex_str, 16) |
| 271 | |
Paul Kehrer | efca280 | 2014-02-17 20:55:13 -0600 | [diff] [blame] | 272 | private_key_vector["examples"] = examples |
| 273 | examples = [] |
| 274 | |
Alex Stapleton | 58f27ac | 2014-02-02 19:30:03 +0000 | [diff] [blame] | 275 | assert ( |
| 276 | private_key_vector['public_exponent'] == |
| 277 | public_key_vector['public_exponent'] |
| 278 | ) |
| 279 | |
| 280 | assert ( |
| 281 | private_key_vector['modulus'] == |
| 282 | public_key_vector['modulus'] |
| 283 | ) |
| 284 | |
| 285 | vectors.append( |
| 286 | (private_key_vector, public_key_vector) |
| 287 | ) |
| 288 | |
| 289 | public_key_vector = collections.defaultdict(list) |
| 290 | private_key_vector = collections.defaultdict(list) |
| 291 | key = None |
| 292 | attr = None |
| 293 | |
| 294 | if private_key_vector is None or public_key_vector is None: |
| 295 | continue |
| 296 | |
| 297 | if line.startswith("# Private key"): |
| 298 | key = private_key_vector |
| 299 | elif line.startswith("# Public key"): |
| 300 | key = public_key_vector |
| 301 | elif line.startswith("# Modulus:"): |
| 302 | attr = "modulus" |
| 303 | elif line.startswith("# Public exponent:"): |
| 304 | attr = "public_exponent" |
| 305 | elif line.startswith("# Exponent:"): |
| 306 | if key is public_key_vector: |
| 307 | attr = "public_exponent" |
| 308 | else: |
| 309 | assert key is private_key_vector |
| 310 | attr = "private_exponent" |
| 311 | elif line.startswith("# Prime 1:"): |
| 312 | attr = "p" |
| 313 | elif line.startswith("# Prime 2:"): |
| 314 | attr = "q" |
Paul Kehrer | 09328bb | 2014-02-12 23:57:27 -0600 | [diff] [blame] | 315 | elif line.startswith("# Prime exponent 1:"): |
| 316 | attr = "dmp1" |
| 317 | elif line.startswith("# Prime exponent 2:"): |
| 318 | attr = "dmq1" |
| 319 | elif line.startswith("# Coefficient:"): |
| 320 | attr = "iqmp" |
Alex Stapleton | 58f27ac | 2014-02-02 19:30:03 +0000 | [diff] [blame] | 321 | elif line.startswith("#"): |
| 322 | attr = None |
| 323 | else: |
| 324 | if key is not None and attr is not None: |
| 325 | key[attr].append(line.strip()) |
| 326 | return vectors |
Paul Kehrer | 2f2a206 | 2014-03-10 23:30:28 -0400 | [diff] [blame] | 327 | |
| 328 | |
| 329 | def load_rsa_nist_vectors(vector_data): |
| 330 | test_data = None |
Paul Kehrer | 62707f1 | 2014-03-18 07:19:14 -0400 | [diff] [blame] | 331 | p = None |
Paul Kehrer | afc2518 | 2014-03-18 07:51:56 -0400 | [diff] [blame] | 332 | salt_length = None |
Paul Kehrer | 2f2a206 | 2014-03-10 23:30:28 -0400 | [diff] [blame] | 333 | data = [] |
| 334 | |
| 335 | for line in vector_data: |
| 336 | line = line.strip() |
| 337 | |
| 338 | # Blank lines and section headers are ignored |
| 339 | if not line or line.startswith("["): |
| 340 | continue |
| 341 | |
| 342 | if line.startswith("# Salt len:"): |
| 343 | salt_length = int(line.split(":")[1].strip()) |
| 344 | continue |
| 345 | elif line.startswith("#"): |
| 346 | continue |
| 347 | |
| 348 | # Build our data using a simple Key = Value format |
| 349 | name, value = [c.strip() for c in line.split("=")] |
| 350 | |
| 351 | if name == "n": |
| 352 | n = int(value, 16) |
Paul Kehrer | 62707f1 | 2014-03-18 07:19:14 -0400 | [diff] [blame] | 353 | elif name == "e" and p is None: |
Paul Kehrer | 2f2a206 | 2014-03-10 23:30:28 -0400 | [diff] [blame] | 354 | e = int(value, 16) |
Paul Kehrer | 62707f1 | 2014-03-18 07:19:14 -0400 | [diff] [blame] | 355 | elif name == "p": |
| 356 | p = int(value, 16) |
| 357 | elif name == "q": |
| 358 | q = int(value, 16) |
Paul Kehrer | 2f2a206 | 2014-03-10 23:30:28 -0400 | [diff] [blame] | 359 | elif name == "SHAAlg": |
Paul Kehrer | 62707f1 | 2014-03-18 07:19:14 -0400 | [diff] [blame] | 360 | if p is None: |
| 361 | test_data = { |
| 362 | "modulus": n, |
| 363 | "public_exponent": e, |
| 364 | "salt_length": salt_length, |
Paul Kehrer | e66f69a | 2014-03-18 07:57:26 -0400 | [diff] [blame] | 365 | "algorithm": value, |
Paul Kehrer | 62707f1 | 2014-03-18 07:19:14 -0400 | [diff] [blame] | 366 | "fail": False |
| 367 | } |
| 368 | else: |
| 369 | test_data = { |
| 370 | "modulus": n, |
| 371 | "p": p, |
| 372 | "q": q, |
Paul Kehrer | e66f69a | 2014-03-18 07:57:26 -0400 | [diff] [blame] | 373 | "algorithm": value |
Paul Kehrer | 62707f1 | 2014-03-18 07:19:14 -0400 | [diff] [blame] | 374 | } |
Paul Kehrer | afc2518 | 2014-03-18 07:51:56 -0400 | [diff] [blame] | 375 | if salt_length is not None: |
| 376 | test_data["salt_length"] = salt_length |
Paul Kehrer | 2f2a206 | 2014-03-10 23:30:28 -0400 | [diff] [blame] | 377 | data.append(test_data) |
Paul Kehrer | 62707f1 | 2014-03-18 07:19:14 -0400 | [diff] [blame] | 378 | elif name == "e" and p is not None: |
| 379 | test_data["public_exponent"] = int(value, 16) |
| 380 | elif name == "d": |
| 381 | test_data["private_exponent"] = int(value, 16) |
| 382 | elif name == "Result": |
| 383 | test_data["fail"] = value.startswith("F") |
Paul Kehrer | 2f2a206 | 2014-03-10 23:30:28 -0400 | [diff] [blame] | 384 | # For all other tokens we simply want the name, value stored in |
| 385 | # the dictionary |
| 386 | else: |
| 387 | test_data[name.lower()] = value.encode("ascii") |
| 388 | |
| 389 | return data |
Mohammed Attia | 987cc70 | 2014-03-12 16:07:21 +0200 | [diff] [blame] | 390 | |
| 391 | |
| 392 | def load_fips_dsa_key_pair_vectors(vector_data): |
| 393 | """ |
| 394 | Loads data out of the FIPS DSA KeyPair vector files. |
| 395 | """ |
| 396 | vectors = [] |
Mohammed Attia | 49b9259 | 2014-03-12 20:07:05 +0200 | [diff] [blame] | 397 | # When reading_key_data is set to True it tells the loader to continue |
| 398 | # constructing dictionaries. We set reading_key_data to False during the |
| 399 | # blocks of the vectors of N=224 because we don't support it. |
| 400 | reading_key_data = True |
Mohammed Attia | 987cc70 | 2014-03-12 16:07:21 +0200 | [diff] [blame] | 401 | for line in vector_data: |
| 402 | line = line.strip() |
| 403 | |
| 404 | if not line or line.startswith("#"): |
| 405 | continue |
Mohammed Attia | 49b9259 | 2014-03-12 20:07:05 +0200 | [diff] [blame] | 406 | elif line.startswith("[mod = L=1024"): |
Mohammed Attia | 987cc70 | 2014-03-12 16:07:21 +0200 | [diff] [blame] | 407 | continue |
Mohammed Attia | 49b9259 | 2014-03-12 20:07:05 +0200 | [diff] [blame] | 408 | elif line.startswith("[mod = L=2048, N=224"): |
| 409 | reading_key_data = False |
Mohammed Attia | 987cc70 | 2014-03-12 16:07:21 +0200 | [diff] [blame] | 410 | continue |
Mohammed Attia | 49b9259 | 2014-03-12 20:07:05 +0200 | [diff] [blame] | 411 | elif line.startswith("[mod = L=2048, N=256"): |
| 412 | reading_key_data = True |
Mohammed Attia | 987cc70 | 2014-03-12 16:07:21 +0200 | [diff] [blame] | 413 | continue |
Mohammed Attia | 49b9259 | 2014-03-12 20:07:05 +0200 | [diff] [blame] | 414 | elif line.startswith("[mod = L=3072"): |
Mohammed Attia | 987cc70 | 2014-03-12 16:07:21 +0200 | [diff] [blame] | 415 | continue |
| 416 | |
Mohammed Attia | 49b9259 | 2014-03-12 20:07:05 +0200 | [diff] [blame] | 417 | if not reading_key_data: |
Mohammed Attia | 987cc70 | 2014-03-12 16:07:21 +0200 | [diff] [blame] | 418 | continue |
| 419 | |
Mohammed Attia | 49b9259 | 2014-03-12 20:07:05 +0200 | [diff] [blame] | 420 | elif reading_key_data: |
Mohammed Attia | 987cc70 | 2014-03-12 16:07:21 +0200 | [diff] [blame] | 421 | if line.startswith("P"): |
| 422 | vectors.append({'p': int(line.split("=")[1], 16)}) |
Mohammed Attia | 22ccb87 | 2014-03-12 18:27:59 +0200 | [diff] [blame] | 423 | elif line.startswith("Q"): |
Mohammed Attia | 987cc70 | 2014-03-12 16:07:21 +0200 | [diff] [blame] | 424 | vectors[-1]['q'] = int(line.split("=")[1], 16) |
Mohammed Attia | 22ccb87 | 2014-03-12 18:27:59 +0200 | [diff] [blame] | 425 | elif line.startswith("G"): |
Mohammed Attia | 987cc70 | 2014-03-12 16:07:21 +0200 | [diff] [blame] | 426 | vectors[-1]['g'] = int(line.split("=")[1], 16) |
Mohammed Attia | 22ccb87 | 2014-03-12 18:27:59 +0200 | [diff] [blame] | 427 | elif line.startswith("X") and 'x' not in vectors[-1]: |
Mohammed Attia | 987cc70 | 2014-03-12 16:07:21 +0200 | [diff] [blame] | 428 | vectors[-1]['x'] = int(line.split("=")[1], 16) |
Mohammed Attia | 22ccb87 | 2014-03-12 18:27:59 +0200 | [diff] [blame] | 429 | elif line.startswith("X") and 'x' in vectors[-1]: |
Mohammed Attia | 987cc70 | 2014-03-12 16:07:21 +0200 | [diff] [blame] | 430 | vectors.append({'p': vectors[-1]['p'], |
| 431 | 'q': vectors[-1]['q'], |
| 432 | 'g': vectors[-1]['g'], |
| 433 | 'x': int(line.split("=")[1], 16) |
| 434 | }) |
Mohammed Attia | 22ccb87 | 2014-03-12 18:27:59 +0200 | [diff] [blame] | 435 | elif line.startswith("Y"): |
Mohammed Attia | 987cc70 | 2014-03-12 16:07:21 +0200 | [diff] [blame] | 436 | vectors[-1]['y'] = int(line.split("=")[1], 16) |
Mohammed Attia | 987cc70 | 2014-03-12 16:07:21 +0200 | [diff] [blame] | 437 | |
| 438 | return vectors |
Alex Stapleton | cf04860 | 2014-04-12 12:48:59 +0100 | [diff] [blame] | 439 | |
| 440 | |
Mohammed Attia | 3c9e158 | 2014-04-22 14:24:44 +0200 | [diff] [blame] | 441 | def load_fips_dsa_sig_vectors(vector_data): |
Mohammed Attia | 0fb5d85 | 2014-04-21 10:31:15 +0200 | [diff] [blame] | 442 | """ |
| 443 | Loads data out of the FIPS DSA SigVer vector files. |
| 444 | """ |
| 445 | vectors = [] |
| 446 | sha_regex = re.compile( |
| 447 | r"\[mod = L=...., N=..., SHA-(?P<sha>1|224|256|384|512)\]" |
| 448 | ) |
| 449 | # When reading_key_data is set to True it tells the loader to continue |
| 450 | # constructing dictionaries. We set reading_key_data to False during the |
| 451 | # blocks of the vectors of N=224 because we don't support it. |
| 452 | reading_key_data = True |
Mohammed Attia | 3c9e158 | 2014-04-22 14:24:44 +0200 | [diff] [blame] | 453 | |
Mohammed Attia | 0fb5d85 | 2014-04-21 10:31:15 +0200 | [diff] [blame] | 454 | for line in vector_data: |
| 455 | line = line.strip() |
| 456 | |
| 457 | if not line or line.startswith("#"): |
| 458 | continue |
| 459 | |
| 460 | sha_match = sha_regex.match(line) |
| 461 | if sha_match: |
| 462 | digest_algorithm = "SHA-{0}".format(sha_match.group("sha")) |
| 463 | |
Paul Kehrer | 7ef2f8f | 2014-04-22 08:37:58 -0500 | [diff] [blame] | 464 | if line.startswith("[mod = L=2048, N=224"): |
Mohammed Attia | 0fb5d85 | 2014-04-21 10:31:15 +0200 | [diff] [blame] | 465 | reading_key_data = False |
| 466 | continue |
Paul Kehrer | 7ef2f8f | 2014-04-22 08:37:58 -0500 | [diff] [blame] | 467 | elif line.startswith("[mod = L=2048, N=256"): |
Mohammed Attia | 0fb5d85 | 2014-04-21 10:31:15 +0200 | [diff] [blame] | 468 | reading_key_data = True |
| 469 | continue |
| 470 | |
| 471 | if not reading_key_data or line.startswith("[mod"): |
| 472 | continue |
| 473 | |
| 474 | name, value = [c.strip() for c in line.split("=")] |
| 475 | |
| 476 | if name == "P": |
| 477 | vectors.append({'p': int(value, 16), |
| 478 | 'digest_algorithm': digest_algorithm}) |
| 479 | elif name == "Q": |
| 480 | vectors[-1]['q'] = int(value, 16) |
| 481 | elif name == "G": |
| 482 | vectors[-1]['g'] = int(value, 16) |
| 483 | elif name == "Msg" and 'msg' not in vectors[-1]: |
| 484 | hexmsg = value.strip().encode("ascii") |
| 485 | vectors[-1]['msg'] = binascii.unhexlify(hexmsg) |
| 486 | elif name == "Msg" and 'msg' in vectors[-1]: |
| 487 | hexmsg = value.strip().encode("ascii") |
| 488 | vectors.append({'p': vectors[-1]['p'], |
| 489 | 'q': vectors[-1]['q'], |
| 490 | 'g': vectors[-1]['g'], |
| 491 | 'digest_algorithm': |
| 492 | vectors[-1]['digest_algorithm'], |
| 493 | 'msg': binascii.unhexlify(hexmsg)}) |
| 494 | elif name == "X": |
| 495 | vectors[-1]['x'] = int(value, 16) |
| 496 | elif name == "Y": |
| 497 | vectors[-1]['y'] = int(value, 16) |
| 498 | elif name == "R": |
| 499 | vectors[-1]['r'] = int(value, 16) |
| 500 | elif name == "S": |
| 501 | vectors[-1]['s'] = int(value, 16) |
| 502 | elif name == "Result": |
| 503 | vectors[-1]['result'] = value.split("(")[0].strip() |
| 504 | |
| 505 | return vectors |
| 506 | |
| 507 | |
Alex Stapleton | 44fe82d | 2014-04-19 09:44:26 +0100 | [diff] [blame] | 508 | # http://tools.ietf.org/html/rfc4492#appendix-A |
Alex Stapleton | c387cf7 | 2014-04-13 13:58:02 +0100 | [diff] [blame] | 509 | _ECDSA_CURVE_NAMES = { |
| 510 | "P-192": "secp192r1", |
| 511 | "P-224": "secp224r1", |
Alex Stapleton | 39e300f | 2014-04-18 22:44:02 +0100 | [diff] [blame] | 512 | "P-256": "secp256r1", |
Alex Stapleton | c387cf7 | 2014-04-13 13:58:02 +0100 | [diff] [blame] | 513 | "P-384": "secp384r1", |
| 514 | "P-521": "secp521r1", |
Alex Stapleton | 39e300f | 2014-04-18 22:44:02 +0100 | [diff] [blame] | 515 | |
Alex Stapleton | c387cf7 | 2014-04-13 13:58:02 +0100 | [diff] [blame] | 516 | "K-163": "sect163k1", |
| 517 | "K-233": "sect233k1", |
Alex Stapleton | 39e300f | 2014-04-18 22:44:02 +0100 | [diff] [blame] | 518 | "K-283": "sect283k1", |
Alex Stapleton | c387cf7 | 2014-04-13 13:58:02 +0100 | [diff] [blame] | 519 | "K-409": "sect409k1", |
| 520 | "K-571": "sect571k1", |
Alex Stapleton | 39e300f | 2014-04-18 22:44:02 +0100 | [diff] [blame] | 521 | |
Alex Stapleton | 44fe82d | 2014-04-19 09:44:26 +0100 | [diff] [blame] | 522 | "B-163": "sect163r2", |
Alex Stapleton | c387cf7 | 2014-04-13 13:58:02 +0100 | [diff] [blame] | 523 | "B-233": "sect233r1", |
| 524 | "B-283": "sect283r1", |
| 525 | "B-409": "sect409r1", |
| 526 | "B-571": "sect571r1", |
| 527 | } |
| 528 | |
| 529 | |
Alex Stapleton | cf04860 | 2014-04-12 12:48:59 +0100 | [diff] [blame] | 530 | def load_fips_ecdsa_key_pair_vectors(vector_data): |
| 531 | """ |
| 532 | Loads data out of the FIPS ECDSA KeyPair vector files. |
| 533 | """ |
| 534 | vectors = [] |
| 535 | key_data = None |
Alex Stapleton | cf04860 | 2014-04-12 12:48:59 +0100 | [diff] [blame] | 536 | for line in vector_data: |
| 537 | line = line.strip() |
| 538 | |
| 539 | if not line or line.startswith("#"): |
| 540 | continue |
| 541 | |
Alex Stapleton | c387cf7 | 2014-04-13 13:58:02 +0100 | [diff] [blame] | 542 | if line[1:-1] in _ECDSA_CURVE_NAMES: |
| 543 | curve_name = _ECDSA_CURVE_NAMES[line[1:-1]] |
Alex Stapleton | cf04860 | 2014-04-12 12:48:59 +0100 | [diff] [blame] | 544 | |
| 545 | elif line.startswith("d = "): |
| 546 | if key_data is not None: |
| 547 | vectors.append(key_data) |
| 548 | |
| 549 | key_data = { |
| 550 | "curve": curve_name, |
| 551 | "d": int(line.split("=")[1], 16) |
| 552 | } |
| 553 | |
| 554 | elif key_data is not None: |
| 555 | if line.startswith("Qx = "): |
| 556 | key_data["x"] = int(line.split("=")[1], 16) |
| 557 | elif line.startswith("Qy = "): |
| 558 | key_data["y"] = int(line.split("=")[1], 16) |
| 559 | |
| 560 | if key_data is not None: |
| 561 | vectors.append(key_data) |
| 562 | |
| 563 | return vectors |
Alex Stapleton | c387cf7 | 2014-04-13 13:58:02 +0100 | [diff] [blame] | 564 | |
| 565 | |
| 566 | def load_fips_ecdsa_signing_vectors(vector_data): |
| 567 | """ |
| 568 | Loads data out of the FIPS ECDSA SigGen vector files. |
| 569 | """ |
| 570 | vectors = [] |
| 571 | |
| 572 | curve_rx = re.compile( |
| 573 | r"\[(?P<curve>[PKB]-[0-9]{3}),SHA-(?P<sha>1|224|256|384|512)\]" |
| 574 | ) |
| 575 | |
| 576 | data = None |
| 577 | for line in vector_data: |
| 578 | line = line.strip() |
| 579 | |
| 580 | if not line or line.startswith("#"): |
| 581 | continue |
| 582 | |
| 583 | curve_match = curve_rx.match(line) |
| 584 | if curve_match: |
| 585 | curve_name = _ECDSA_CURVE_NAMES[curve_match.group("curve")] |
| 586 | digest_name = "SHA-{0}".format(curve_match.group("sha")) |
| 587 | |
| 588 | elif line.startswith("Msg = "): |
| 589 | if data is not None: |
| 590 | vectors.append(data) |
| 591 | |
| 592 | hexmsg = line.split("=")[1].strip().encode("ascii") |
| 593 | |
| 594 | data = { |
| 595 | "curve": curve_name, |
| 596 | "digest_algorithm": digest_name, |
| 597 | "message": binascii.unhexlify(hexmsg) |
| 598 | } |
| 599 | |
| 600 | elif data is not None: |
| 601 | if line.startswith("Qx = "): |
| 602 | data["x"] = int(line.split("=")[1], 16) |
| 603 | elif line.startswith("Qy = "): |
| 604 | data["y"] = int(line.split("=")[1], 16) |
| 605 | elif line.startswith("R = "): |
| 606 | data["r"] = int(line.split("=")[1], 16) |
| 607 | elif line.startswith("S = "): |
| 608 | data["s"] = int(line.split("=")[1], 16) |
| 609 | elif line.startswith("d = "): |
| 610 | data["d"] = int(line.split("=")[1], 16) |
Alex Stapleton | 6f72949 | 2014-04-19 09:01:25 +0100 | [diff] [blame] | 611 | elif line.startswith("Result = "): |
| 612 | data["fail"] = line.split("=")[1].strip()[0] == "F" |
Alex Stapleton | c387cf7 | 2014-04-13 13:58:02 +0100 | [diff] [blame] | 613 | |
| 614 | if data is not None: |
| 615 | vectors.append(data) |
Alex Stapleton | c387cf7 | 2014-04-13 13:58:02 +0100 | [diff] [blame] | 616 | return vectors |
Alex Stapleton | 839c09d | 2014-08-10 12:18:02 +0100 | [diff] [blame] | 617 | |
| 618 | |
| 619 | def load_kasvs_dh_vectors(vector_data): |
| 620 | """ |
| 621 | Loads data out of the KASVS key exchange vector data |
| 622 | """ |
| 623 | |
| 624 | result_rx = re.compile(r"([FP]) \(([0-9]+) -") |
| 625 | |
| 626 | vectors = [] |
| 627 | data = { |
| 628 | "fail_z": False, |
| 629 | "fail_agree": False |
| 630 | } |
| 631 | |
| 632 | for line in vector_data: |
| 633 | line = line.strip() |
| 634 | |
| 635 | if not line or line.startswith("#"): |
| 636 | continue |
| 637 | |
| 638 | if line.startswith("P = "): |
| 639 | data["p"] = int(line.split("=")[1], 16) |
| 640 | elif line.startswith("Q = "): |
| 641 | data["q"] = int(line.split("=")[1], 16) |
| 642 | elif line.startswith("G = "): |
| 643 | data["g"] = int(line.split("=")[1], 16) |
| 644 | elif line.startswith("Z = "): |
| 645 | z_hex = line.split("=")[1].strip().encode("ascii") |
| 646 | data["z"] = binascii.unhexlify(z_hex) |
| 647 | elif line.startswith("XstatCAVS = "): |
| 648 | data["x1"] = int(line.split("=")[1], 16) |
| 649 | elif line.startswith("YstatCAVS = "): |
| 650 | data["y1"] = int(line.split("=")[1], 16) |
| 651 | elif line.startswith("XstatIUT = "): |
| 652 | data["x2"] = int(line.split("=")[1], 16) |
| 653 | elif line.startswith("YstatIUT = "): |
| 654 | data["y2"] = int(line.split("=")[1], 16) |
| 655 | elif line.startswith("Result = "): |
| 656 | result_str = line.split("=")[1].strip() |
| 657 | match = result_rx.match(result_str) |
| 658 | |
| 659 | if match.group(1) == "F": |
| 660 | if int(match.group(2)) in (5, 10): |
| 661 | data["fail_z"] = True |
| 662 | else: |
| 663 | data["fail_agree"] = True |
| 664 | |
| 665 | vectors.append(data) |
| 666 | |
| 667 | data = { |
| 668 | "p": data["p"], |
| 669 | "q": data["q"], |
| 670 | "g": data["g"], |
| 671 | "fail_z": False, |
| 672 | "fail_agree": False |
| 673 | } |
| 674 | |
| 675 | return vectors |