Alex Gaynor | f312a5c | 2013-08-10 15:23:38 -0400 | [diff] [blame] | 1 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | # you may not use this file except in compliance with the License. |
| 3 | # You may obtain a copy of the License at |
| 4 | # |
| 5 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | # |
| 7 | # Unless required by applicable law or agreed to in writing, software |
| 8 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 10 | # implied. |
| 11 | # See the License for the specific language governing permissions and |
| 12 | # limitations under the License. |
| 13 | |
Alex Gaynor | c37feed | 2014-03-08 08:32:56 -0800 | [diff] [blame] | 14 | from __future__ import absolute_import, division, print_function |
| 15 | |
Alex Gaynor | 36e651c | 2014-01-27 10:08:35 -0800 | [diff] [blame] | 16 | import collections |
Alex Gaynor | 7a489db | 2014-03-22 15:09:34 -0700 | [diff] [blame] | 17 | from contextlib import contextmanager |
Paul Kehrer | 90450f3 | 2014-03-19 12:37:17 -0400 | [diff] [blame] | 18 | |
Alex Stapleton | a39a319 | 2014-03-14 20:03:12 +0000 | [diff] [blame] | 19 | import pytest |
| 20 | |
Paul Kehrer | afc1ccd | 2014-03-19 11:49:32 -0400 | [diff] [blame] | 21 | import six |
Alex Gaynor | 2b3f942 | 2013-12-24 21:55:24 -0800 | [diff] [blame] | 22 | |
Alex Gaynor | 7a489db | 2014-03-22 15:09:34 -0700 | [diff] [blame] | 23 | from cryptography.exceptions import UnsupportedAlgorithm |
Alex Gaynor | 07c4dcc | 2014-04-05 11:22:07 -0700 | [diff] [blame] | 24 | |
Alex Stapleton | a39a319 | 2014-03-14 20:03:12 +0000 | [diff] [blame] | 25 | import cryptography_vectors |
Matthew Iversen | 68e77c7 | 2014-03-13 08:54:43 +1100 | [diff] [blame] | 26 | |
Alex Gaynor | 2b3f942 | 2013-12-24 21:55:24 -0800 | [diff] [blame] | 27 | |
Alex Gaynor | 36e651c | 2014-01-27 10:08:35 -0800 | [diff] [blame] | 28 | HashVector = collections.namedtuple("HashVector", ["message", "digest"]) |
| 29 | KeyedHashVector = collections.namedtuple( |
| 30 | "KeyedHashVector", ["message", "digest", "key"] |
| 31 | ) |
| 32 | |
| 33 | |
Paul Kehrer | c421e63 | 2014-01-18 09:22:21 -0600 | [diff] [blame] | 34 | def select_backends(names, backend_list): |
| 35 | if names is None: |
| 36 | return backend_list |
| 37 | split_names = [x.strip() for x in names.split(',')] |
| 38 | # this must be duplicated and then removed to preserve the metadata |
| 39 | # pytest associates. Appending backends to a new list doesn't seem to work |
Paul Kehrer | aed9e17 | 2014-01-19 12:09:27 -0600 | [diff] [blame] | 40 | selected_backends = [] |
| 41 | for backend in backend_list: |
| 42 | if backend.name in split_names: |
| 43 | selected_backends.append(backend) |
Paul Kehrer | c421e63 | 2014-01-18 09:22:21 -0600 | [diff] [blame] | 44 | |
Paul Kehrer | aed9e17 | 2014-01-19 12:09:27 -0600 | [diff] [blame] | 45 | if len(selected_backends) > 0: |
| 46 | return selected_backends |
Paul Kehrer | c421e63 | 2014-01-18 09:22:21 -0600 | [diff] [blame] | 47 | else: |
| 48 | raise ValueError( |
| 49 | "No backend selected. Tried to select: {0}".format(split_names) |
| 50 | ) |
Paul Kehrer | 34c075e | 2014-01-13 21:52:08 -0500 | [diff] [blame] | 51 | |
| 52 | |
Alex Gaynor | 2b3f942 | 2013-12-24 21:55:24 -0800 | [diff] [blame] | 53 | def check_for_iface(name, iface, item): |
| 54 | if name in item.keywords and "backend" in item.funcargs: |
| 55 | if not isinstance(item.funcargs["backend"], iface): |
| 56 | pytest.skip("{0} backend does not support {1}".format( |
| 57 | item.funcargs["backend"], name |
| 58 | )) |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 59 | |
| 60 | |
Paul Kehrer | 60fc8da | 2013-12-26 20:19:34 -0600 | [diff] [blame] | 61 | def check_backend_support(item): |
Paul Kehrer | 5a8fdf8 | 2013-12-26 20:13:45 -0600 | [diff] [blame] | 62 | supported = item.keywords.get("supported") |
| 63 | if supported and "backend" in item.funcargs: |
| 64 | if not supported.kwargs["only_if"](item.funcargs["backend"]): |
Paul Kehrer | f03334e | 2014-01-02 23:16:14 -0600 | [diff] [blame] | 65 | pytest.skip("{0} ({1})".format( |
| 66 | supported.kwargs["skip_message"], item.funcargs["backend"] |
| 67 | )) |
Paul Kehrer | 5a8fdf8 | 2013-12-26 20:13:45 -0600 | [diff] [blame] | 68 | elif supported: |
Paul Kehrer | ec49550 | 2013-12-27 15:51:40 -0600 | [diff] [blame] | 69 | raise ValueError("This mark is only available on methods that take a " |
| 70 | "backend") |
Paul Kehrer | 5a8fdf8 | 2013-12-26 20:13:45 -0600 | [diff] [blame] | 71 | |
| 72 | |
Alex Gaynor | 7a489db | 2014-03-22 15:09:34 -0700 | [diff] [blame] | 73 | @contextmanager |
Alex Stapleton | 5e4c8c3 | 2014-03-27 16:38:00 +0000 | [diff] [blame] | 74 | def raises_unsupported_algorithm(reason): |
Alex Gaynor | 7a489db | 2014-03-22 15:09:34 -0700 | [diff] [blame] | 75 | with pytest.raises(UnsupportedAlgorithm) as exc_info: |
Alex Stapleton | 112963e | 2014-03-26 17:39:29 +0000 | [diff] [blame] | 76 | yield exc_info |
Alex Stapleton | 5e4c8c3 | 2014-03-27 16:38:00 +0000 | [diff] [blame] | 77 | |
Alex Stapleton | 85a791f | 2014-03-27 16:55:41 +0000 | [diff] [blame] | 78 | assert exc_info.value._reason is reason |
Alex Gaynor | 7a489db | 2014-03-22 15:09:34 -0700 | [diff] [blame] | 79 | |
| 80 | |
Paul Kehrer | f7f6a9f | 2013-11-11 20:43:52 -0600 | [diff] [blame] | 81 | def load_vectors_from_file(filename, loader): |
Alex Stapleton | a39a319 | 2014-03-14 20:03:12 +0000 | [diff] [blame] | 82 | with cryptography_vectors.open_vector_file(filename) as vector_file: |
| 83 | return loader(vector_file) |
Paul Kehrer | f7f6a9f | 2013-11-11 20:43:52 -0600 | [diff] [blame] | 84 | |
| 85 | |
Alex Gaynor | d3ce703 | 2013-11-11 14:46:20 -0800 | [diff] [blame] | 86 | def load_nist_vectors(vector_data): |
Paul Kehrer | 749ac5b | 2013-11-18 18:12:41 -0600 | [diff] [blame] | 87 | test_data = None |
| 88 | data = [] |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 89 | |
| 90 | for line in vector_data: |
| 91 | line = line.strip() |
| 92 | |
Paul Kehrer | 749ac5b | 2013-11-18 18:12:41 -0600 | [diff] [blame] | 93 | # Blank lines, comments, and section headers are ignored |
| 94 | if not line or line.startswith("#") or (line.startswith("[") |
| 95 | and line.endswith("]")): |
Alex Gaynor | 521c42d | 2013-11-11 14:25:59 -0800 | [diff] [blame] | 96 | continue |
| 97 | |
Paul Kehrer | a43b669 | 2013-11-12 15:35:49 -0600 | [diff] [blame] | 98 | if line.strip() == "FAIL": |
Paul Kehrer | 749ac5b | 2013-11-18 18:12:41 -0600 | [diff] [blame] | 99 | test_data["fail"] = True |
Paul Kehrer | a43b669 | 2013-11-12 15:35:49 -0600 | [diff] [blame] | 100 | continue |
| 101 | |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 102 | # Build our data using a simple Key = Value format |
Paul Kehrer | a43b669 | 2013-11-12 15:35:49 -0600 | [diff] [blame] | 103 | name, value = [c.strip() for c in line.split("=")] |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 104 | |
Paul Kehrer | 1050ddf | 2014-01-27 21:04:03 -0600 | [diff] [blame] | 105 | # Some tests (PBKDF2) contain \0, which should be interpreted as a |
| 106 | # null character rather than literal. |
| 107 | value = value.replace("\\0", "\0") |
| 108 | |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 109 | # COUNT is a special token that indicates a new block of data |
| 110 | if name.upper() == "COUNT": |
Paul Kehrer | 749ac5b | 2013-11-18 18:12:41 -0600 | [diff] [blame] | 111 | test_data = {} |
| 112 | data.append(test_data) |
| 113 | continue |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 114 | # For all other tokens we simply want the name, value stored in |
| 115 | # the dictionary |
| 116 | else: |
Paul Kehrer | 749ac5b | 2013-11-18 18:12:41 -0600 | [diff] [blame] | 117 | test_data[name.lower()] = value.encode("ascii") |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 118 | |
Paul Kehrer | 749ac5b | 2013-11-18 18:12:41 -0600 | [diff] [blame] | 119 | return data |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 120 | |
| 121 | |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 122 | def load_cryptrec_vectors(vector_data): |
Paul Kehrer | e580598 | 2013-09-27 11:26:01 -0500 | [diff] [blame] | 123 | cryptrec_list = [] |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 124 | |
| 125 | for line in vector_data: |
| 126 | line = line.strip() |
| 127 | |
| 128 | # Blank lines and comments are ignored |
| 129 | if not line or line.startswith("#"): |
| 130 | continue |
| 131 | |
| 132 | if line.startswith("K"): |
Paul Kehrer | e580598 | 2013-09-27 11:26:01 -0500 | [diff] [blame] | 133 | key = line.split(" : ")[1].replace(" ", "").encode("ascii") |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 134 | elif line.startswith("P"): |
Paul Kehrer | e580598 | 2013-09-27 11:26:01 -0500 | [diff] [blame] | 135 | pt = line.split(" : ")[1].replace(" ", "").encode("ascii") |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 136 | elif line.startswith("C"): |
Paul Kehrer | e580598 | 2013-09-27 11:26:01 -0500 | [diff] [blame] | 137 | ct = line.split(" : ")[1].replace(" ", "").encode("ascii") |
| 138 | # after a C is found the K+P+C tuple is complete |
| 139 | # there are many P+C pairs for each K |
Alex Gaynor | 1fe70b1 | 2013-10-16 11:59:17 -0700 | [diff] [blame] | 140 | cryptrec_list.append({ |
| 141 | "key": key, |
| 142 | "plaintext": pt, |
| 143 | "ciphertext": ct |
| 144 | }) |
Donald Stufft | 3359d7e | 2013-10-19 19:33:06 -0400 | [diff] [blame] | 145 | else: |
| 146 | raise ValueError("Invalid line in file '{}'".format(line)) |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 147 | return cryptrec_list |
| 148 | |
| 149 | |
Paul Kehrer | 69e0652 | 2013-10-18 17:28:39 -0500 | [diff] [blame] | 150 | def load_hash_vectors(vector_data): |
| 151 | vectors = [] |
Paul Kehrer | 1bb8b71 | 2013-10-27 17:00:14 -0500 | [diff] [blame] | 152 | key = None |
| 153 | msg = None |
| 154 | md = None |
Paul Kehrer | 69e0652 | 2013-10-18 17:28:39 -0500 | [diff] [blame] | 155 | |
| 156 | for line in vector_data: |
| 157 | line = line.strip() |
| 158 | |
Paul Kehrer | 87cd0db | 2013-10-18 18:01:26 -0500 | [diff] [blame] | 159 | if not line or line.startswith("#") or line.startswith("["): |
Paul Kehrer | 69e0652 | 2013-10-18 17:28:39 -0500 | [diff] [blame] | 160 | continue |
| 161 | |
| 162 | if line.startswith("Len"): |
| 163 | length = int(line.split(" = ")[1]) |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 164 | elif line.startswith("Key"): |
Alex Gaynor | 36e651c | 2014-01-27 10:08:35 -0800 | [diff] [blame] | 165 | # HMAC vectors contain a key attribute. Hash vectors do not. |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 166 | key = line.split(" = ")[1].encode("ascii") |
Paul Kehrer | 69e0652 | 2013-10-18 17:28:39 -0500 | [diff] [blame] | 167 | elif line.startswith("Msg"): |
Alex Gaynor | 36e651c | 2014-01-27 10:08:35 -0800 | [diff] [blame] | 168 | # In the NIST vectors they have chosen to represent an empty |
| 169 | # string as hex 00, which is of course not actually an empty |
| 170 | # string. So we parse the provided length and catch this edge case. |
Paul Kehrer | 69e0652 | 2013-10-18 17:28:39 -0500 | [diff] [blame] | 171 | msg = line.split(" = ")[1].encode("ascii") if length > 0 else b"" |
| 172 | elif line.startswith("MD"): |
| 173 | md = line.split(" = ")[1] |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 174 | # after MD is found the Msg+MD (+ potential key) tuple is complete |
Paul Kehrer | 00dd509 | 2013-10-23 09:41:49 -0500 | [diff] [blame] | 175 | if key is not None: |
Alex Gaynor | 36e651c | 2014-01-27 10:08:35 -0800 | [diff] [blame] | 176 | vectors.append(KeyedHashVector(msg, md, key)) |
Paul Kehrer | 1bb8b71 | 2013-10-27 17:00:14 -0500 | [diff] [blame] | 177 | key = None |
| 178 | msg = None |
| 179 | md = None |
Paul Kehrer | 00dd509 | 2013-10-23 09:41:49 -0500 | [diff] [blame] | 180 | else: |
Alex Gaynor | 36e651c | 2014-01-27 10:08:35 -0800 | [diff] [blame] | 181 | vectors.append(HashVector(msg, md)) |
Paul Kehrer | 1bb8b71 | 2013-10-27 17:00:14 -0500 | [diff] [blame] | 182 | msg = None |
| 183 | md = None |
Paul Kehrer | 69e0652 | 2013-10-18 17:28:39 -0500 | [diff] [blame] | 184 | else: |
| 185 | raise ValueError("Unknown line in hash vector") |
| 186 | return vectors |
Alex Stapleton | 58f27ac | 2014-02-02 19:30:03 +0000 | [diff] [blame] | 187 | |
| 188 | |
| 189 | def load_pkcs1_vectors(vector_data): |
| 190 | """ |
| 191 | Loads data out of RSA PKCS #1 vector files. |
Alex Stapleton | 58f27ac | 2014-02-02 19:30:03 +0000 | [diff] [blame] | 192 | """ |
| 193 | private_key_vector = None |
| 194 | public_key_vector = None |
| 195 | attr = None |
| 196 | key = None |
Paul Kehrer | efca280 | 2014-02-17 20:55:13 -0600 | [diff] [blame] | 197 | example_vector = None |
| 198 | examples = [] |
Alex Stapleton | 58f27ac | 2014-02-02 19:30:03 +0000 | [diff] [blame] | 199 | vectors = [] |
| 200 | for line in vector_data: |
Paul Kehrer | 7774a03 | 2014-02-17 22:56:55 -0600 | [diff] [blame] | 201 | if ( |
| 202 | line.startswith("# PSS Example") or |
Paul Kehrer | 3fe9150 | 2014-03-29 12:08:39 -0500 | [diff] [blame] | 203 | line.startswith("# OAEP Example") or |
| 204 | line.startswith("# PKCS#1 v1.5") |
Paul Kehrer | 7774a03 | 2014-02-17 22:56:55 -0600 | [diff] [blame] | 205 | ): |
Paul Kehrer | efca280 | 2014-02-17 20:55:13 -0600 | [diff] [blame] | 206 | if example_vector: |
| 207 | for key, value in six.iteritems(example_vector): |
Paul Kehrer | 2681180 | 2014-02-19 16:32:11 -0600 | [diff] [blame] | 208 | hex_str = "".join(value).replace(" ", "").encode("ascii") |
Paul Kehrer | efca280 | 2014-02-17 20:55:13 -0600 | [diff] [blame] | 209 | example_vector[key] = hex_str |
| 210 | examples.append(example_vector) |
| 211 | |
| 212 | attr = None |
| 213 | example_vector = collections.defaultdict(list) |
| 214 | |
Paul Kehrer | 3fe9150 | 2014-03-29 12:08:39 -0500 | [diff] [blame] | 215 | if line.startswith("# Message"): |
Paul Kehrer | 7d9c306 | 2014-02-18 08:27:39 -0600 | [diff] [blame] | 216 | attr = "message" |
Paul Kehrer | efca280 | 2014-02-17 20:55:13 -0600 | [diff] [blame] | 217 | continue |
| 218 | elif line.startswith("# Salt"): |
| 219 | attr = "salt" |
| 220 | continue |
Paul Kehrer | 3fe9150 | 2014-03-29 12:08:39 -0500 | [diff] [blame] | 221 | elif line.startswith("# Seed"): |
| 222 | attr = "seed" |
| 223 | continue |
Paul Kehrer | efca280 | 2014-02-17 20:55:13 -0600 | [diff] [blame] | 224 | elif line.startswith("# Signature"): |
| 225 | attr = "signature" |
| 226 | continue |
Paul Kehrer | 3fe9150 | 2014-03-29 12:08:39 -0500 | [diff] [blame] | 227 | elif line.startswith("# Encryption"): |
| 228 | attr = "encryption" |
| 229 | continue |
Paul Kehrer | efca280 | 2014-02-17 20:55:13 -0600 | [diff] [blame] | 230 | elif ( |
| 231 | example_vector and |
| 232 | line.startswith("# =============================================") |
| 233 | ): |
| 234 | for key, value in six.iteritems(example_vector): |
Paul Kehrer | 2681180 | 2014-02-19 16:32:11 -0600 | [diff] [blame] | 235 | hex_str = "".join(value).replace(" ", "").encode("ascii") |
Paul Kehrer | efca280 | 2014-02-17 20:55:13 -0600 | [diff] [blame] | 236 | example_vector[key] = hex_str |
| 237 | examples.append(example_vector) |
| 238 | example_vector = None |
| 239 | attr = None |
| 240 | elif example_vector and line.startswith("#"): |
| 241 | continue |
| 242 | else: |
| 243 | if attr is not None and example_vector is not None: |
| 244 | example_vector[attr].append(line.strip()) |
| 245 | continue |
| 246 | |
Alex Stapleton | 58f27ac | 2014-02-02 19:30:03 +0000 | [diff] [blame] | 247 | if ( |
| 248 | line.startswith("# Example") or |
| 249 | line.startswith("# =============================================") |
| 250 | ): |
| 251 | if key: |
| 252 | assert private_key_vector |
| 253 | assert public_key_vector |
| 254 | |
| 255 | for key, value in six.iteritems(public_key_vector): |
| 256 | hex_str = "".join(value).replace(" ", "") |
| 257 | public_key_vector[key] = int(hex_str, 16) |
| 258 | |
| 259 | for key, value in six.iteritems(private_key_vector): |
| 260 | hex_str = "".join(value).replace(" ", "") |
| 261 | private_key_vector[key] = int(hex_str, 16) |
| 262 | |
Paul Kehrer | efca280 | 2014-02-17 20:55:13 -0600 | [diff] [blame] | 263 | private_key_vector["examples"] = examples |
| 264 | examples = [] |
| 265 | |
Alex Stapleton | 58f27ac | 2014-02-02 19:30:03 +0000 | [diff] [blame] | 266 | assert ( |
| 267 | private_key_vector['public_exponent'] == |
| 268 | public_key_vector['public_exponent'] |
| 269 | ) |
| 270 | |
| 271 | assert ( |
| 272 | private_key_vector['modulus'] == |
| 273 | public_key_vector['modulus'] |
| 274 | ) |
| 275 | |
| 276 | vectors.append( |
| 277 | (private_key_vector, public_key_vector) |
| 278 | ) |
| 279 | |
| 280 | public_key_vector = collections.defaultdict(list) |
| 281 | private_key_vector = collections.defaultdict(list) |
| 282 | key = None |
| 283 | attr = None |
| 284 | |
| 285 | if private_key_vector is None or public_key_vector is None: |
| 286 | continue |
| 287 | |
| 288 | if line.startswith("# Private key"): |
| 289 | key = private_key_vector |
| 290 | elif line.startswith("# Public key"): |
| 291 | key = public_key_vector |
| 292 | elif line.startswith("# Modulus:"): |
| 293 | attr = "modulus" |
| 294 | elif line.startswith("# Public exponent:"): |
| 295 | attr = "public_exponent" |
| 296 | elif line.startswith("# Exponent:"): |
| 297 | if key is public_key_vector: |
| 298 | attr = "public_exponent" |
| 299 | else: |
| 300 | assert key is private_key_vector |
| 301 | attr = "private_exponent" |
| 302 | elif line.startswith("# Prime 1:"): |
| 303 | attr = "p" |
| 304 | elif line.startswith("# Prime 2:"): |
| 305 | attr = "q" |
Paul Kehrer | 09328bb | 2014-02-12 23:57:27 -0600 | [diff] [blame] | 306 | elif line.startswith("# Prime exponent 1:"): |
| 307 | attr = "dmp1" |
| 308 | elif line.startswith("# Prime exponent 2:"): |
| 309 | attr = "dmq1" |
| 310 | elif line.startswith("# Coefficient:"): |
| 311 | attr = "iqmp" |
Alex Stapleton | 58f27ac | 2014-02-02 19:30:03 +0000 | [diff] [blame] | 312 | elif line.startswith("#"): |
| 313 | attr = None |
| 314 | else: |
| 315 | if key is not None and attr is not None: |
| 316 | key[attr].append(line.strip()) |
| 317 | return vectors |
Paul Kehrer | 2f2a206 | 2014-03-10 23:30:28 -0400 | [diff] [blame] | 318 | |
| 319 | |
| 320 | def load_rsa_nist_vectors(vector_data): |
| 321 | test_data = None |
Paul Kehrer | 62707f1 | 2014-03-18 07:19:14 -0400 | [diff] [blame] | 322 | p = None |
Paul Kehrer | afc2518 | 2014-03-18 07:51:56 -0400 | [diff] [blame] | 323 | salt_length = None |
Paul Kehrer | 2f2a206 | 2014-03-10 23:30:28 -0400 | [diff] [blame] | 324 | data = [] |
| 325 | |
| 326 | for line in vector_data: |
| 327 | line = line.strip() |
| 328 | |
| 329 | # Blank lines and section headers are ignored |
| 330 | if not line or line.startswith("["): |
| 331 | continue |
| 332 | |
| 333 | if line.startswith("# Salt len:"): |
| 334 | salt_length = int(line.split(":")[1].strip()) |
| 335 | continue |
| 336 | elif line.startswith("#"): |
| 337 | continue |
| 338 | |
| 339 | # Build our data using a simple Key = Value format |
| 340 | name, value = [c.strip() for c in line.split("=")] |
| 341 | |
| 342 | if name == "n": |
| 343 | n = int(value, 16) |
Paul Kehrer | 62707f1 | 2014-03-18 07:19:14 -0400 | [diff] [blame] | 344 | elif name == "e" and p is None: |
Paul Kehrer | 2f2a206 | 2014-03-10 23:30:28 -0400 | [diff] [blame] | 345 | e = int(value, 16) |
Paul Kehrer | 62707f1 | 2014-03-18 07:19:14 -0400 | [diff] [blame] | 346 | elif name == "p": |
| 347 | p = int(value, 16) |
| 348 | elif name == "q": |
| 349 | q = int(value, 16) |
Paul Kehrer | 2f2a206 | 2014-03-10 23:30:28 -0400 | [diff] [blame] | 350 | elif name == "SHAAlg": |
Paul Kehrer | 62707f1 | 2014-03-18 07:19:14 -0400 | [diff] [blame] | 351 | if p is None: |
| 352 | test_data = { |
| 353 | "modulus": n, |
| 354 | "public_exponent": e, |
| 355 | "salt_length": salt_length, |
Paul Kehrer | e66f69a | 2014-03-18 07:57:26 -0400 | [diff] [blame] | 356 | "algorithm": value, |
Paul Kehrer | 62707f1 | 2014-03-18 07:19:14 -0400 | [diff] [blame] | 357 | "fail": False |
| 358 | } |
| 359 | else: |
| 360 | test_data = { |
| 361 | "modulus": n, |
| 362 | "p": p, |
| 363 | "q": q, |
Paul Kehrer | e66f69a | 2014-03-18 07:57:26 -0400 | [diff] [blame] | 364 | "algorithm": value |
Paul Kehrer | 62707f1 | 2014-03-18 07:19:14 -0400 | [diff] [blame] | 365 | } |
Paul Kehrer | afc2518 | 2014-03-18 07:51:56 -0400 | [diff] [blame] | 366 | if salt_length is not None: |
| 367 | test_data["salt_length"] = salt_length |
Paul Kehrer | 2f2a206 | 2014-03-10 23:30:28 -0400 | [diff] [blame] | 368 | data.append(test_data) |
Paul Kehrer | 62707f1 | 2014-03-18 07:19:14 -0400 | [diff] [blame] | 369 | elif name == "e" and p is not None: |
| 370 | test_data["public_exponent"] = int(value, 16) |
| 371 | elif name == "d": |
| 372 | test_data["private_exponent"] = int(value, 16) |
| 373 | elif name == "Result": |
| 374 | test_data["fail"] = value.startswith("F") |
Paul Kehrer | 2f2a206 | 2014-03-10 23:30:28 -0400 | [diff] [blame] | 375 | # For all other tokens we simply want the name, value stored in |
| 376 | # the dictionary |
| 377 | else: |
| 378 | test_data[name.lower()] = value.encode("ascii") |
| 379 | |
| 380 | return data |
Mohammed Attia | 987cc70 | 2014-03-12 16:07:21 +0200 | [diff] [blame] | 381 | |
| 382 | |
| 383 | def load_fips_dsa_key_pair_vectors(vector_data): |
| 384 | """ |
| 385 | Loads data out of the FIPS DSA KeyPair vector files. |
| 386 | """ |
| 387 | vectors = [] |
Mohammed Attia | 49b9259 | 2014-03-12 20:07:05 +0200 | [diff] [blame] | 388 | # When reading_key_data is set to True it tells the loader to continue |
| 389 | # constructing dictionaries. We set reading_key_data to False during the |
| 390 | # blocks of the vectors of N=224 because we don't support it. |
| 391 | reading_key_data = True |
Mohammed Attia | 987cc70 | 2014-03-12 16:07:21 +0200 | [diff] [blame] | 392 | for line in vector_data: |
| 393 | line = line.strip() |
| 394 | |
| 395 | if not line or line.startswith("#"): |
| 396 | continue |
Mohammed Attia | 49b9259 | 2014-03-12 20:07:05 +0200 | [diff] [blame] | 397 | elif line.startswith("[mod = L=1024"): |
Mohammed Attia | 987cc70 | 2014-03-12 16:07:21 +0200 | [diff] [blame] | 398 | continue |
Mohammed Attia | 49b9259 | 2014-03-12 20:07:05 +0200 | [diff] [blame] | 399 | elif line.startswith("[mod = L=2048, N=224"): |
| 400 | reading_key_data = False |
Mohammed Attia | 987cc70 | 2014-03-12 16:07:21 +0200 | [diff] [blame] | 401 | continue |
Mohammed Attia | 49b9259 | 2014-03-12 20:07:05 +0200 | [diff] [blame] | 402 | elif line.startswith("[mod = L=2048, N=256"): |
| 403 | reading_key_data = True |
Mohammed Attia | 987cc70 | 2014-03-12 16:07:21 +0200 | [diff] [blame] | 404 | continue |
Mohammed Attia | 49b9259 | 2014-03-12 20:07:05 +0200 | [diff] [blame] | 405 | elif line.startswith("[mod = L=3072"): |
Mohammed Attia | 987cc70 | 2014-03-12 16:07:21 +0200 | [diff] [blame] | 406 | continue |
| 407 | |
Mohammed Attia | 49b9259 | 2014-03-12 20:07:05 +0200 | [diff] [blame] | 408 | if not reading_key_data: |
Mohammed Attia | 987cc70 | 2014-03-12 16:07:21 +0200 | [diff] [blame] | 409 | continue |
| 410 | |
Mohammed Attia | 49b9259 | 2014-03-12 20:07:05 +0200 | [diff] [blame] | 411 | elif reading_key_data: |
Mohammed Attia | 987cc70 | 2014-03-12 16:07:21 +0200 | [diff] [blame] | 412 | if line.startswith("P"): |
| 413 | vectors.append({'p': int(line.split("=")[1], 16)}) |
Mohammed Attia | 22ccb87 | 2014-03-12 18:27:59 +0200 | [diff] [blame] | 414 | elif line.startswith("Q"): |
Mohammed Attia | 987cc70 | 2014-03-12 16:07:21 +0200 | [diff] [blame] | 415 | vectors[-1]['q'] = int(line.split("=")[1], 16) |
Mohammed Attia | 22ccb87 | 2014-03-12 18:27:59 +0200 | [diff] [blame] | 416 | elif line.startswith("G"): |
Mohammed Attia | 987cc70 | 2014-03-12 16:07:21 +0200 | [diff] [blame] | 417 | vectors[-1]['g'] = int(line.split("=")[1], 16) |
Mohammed Attia | 22ccb87 | 2014-03-12 18:27:59 +0200 | [diff] [blame] | 418 | elif line.startswith("X") and 'x' not in vectors[-1]: |
Mohammed Attia | 987cc70 | 2014-03-12 16:07:21 +0200 | [diff] [blame] | 419 | vectors[-1]['x'] = int(line.split("=")[1], 16) |
Mohammed Attia | 22ccb87 | 2014-03-12 18:27:59 +0200 | [diff] [blame] | 420 | elif line.startswith("X") and 'x' in vectors[-1]: |
Mohammed Attia | 987cc70 | 2014-03-12 16:07:21 +0200 | [diff] [blame] | 421 | vectors.append({'p': vectors[-1]['p'], |
| 422 | 'q': vectors[-1]['q'], |
| 423 | 'g': vectors[-1]['g'], |
| 424 | 'x': int(line.split("=")[1], 16) |
| 425 | }) |
Mohammed Attia | 22ccb87 | 2014-03-12 18:27:59 +0200 | [diff] [blame] | 426 | elif line.startswith("Y"): |
Mohammed Attia | 987cc70 | 2014-03-12 16:07:21 +0200 | [diff] [blame] | 427 | vectors[-1]['y'] = int(line.split("=")[1], 16) |
Mohammed Attia | 987cc70 | 2014-03-12 16:07:21 +0200 | [diff] [blame] | 428 | |
| 429 | return vectors |
Alex Stapleton | cf04860 | 2014-04-12 12:48:59 +0100 | [diff] [blame] | 430 | |
| 431 | |
| 432 | def load_fips_ecdsa_key_pair_vectors(vector_data): |
| 433 | """ |
| 434 | Loads data out of the FIPS ECDSA KeyPair vector files. |
| 435 | """ |
| 436 | vectors = [] |
| 437 | key_data = None |
| 438 | |
| 439 | nist_name_map = { |
| 440 | "[P-192]": "secp192r1", |
| 441 | "[P-224]": "secp224r1", |
| 442 | "[P-256]": "secp192r1", |
| 443 | "[P-384]": "secp384r1", |
| 444 | "[P-521]": "secp521r1", |
| 445 | "[K-163]": "sect163k1", |
| 446 | "[K-233]": "sect233k1", |
| 447 | "[K-283]": "sect233k1", |
| 448 | "[K-409]": "sect409k1", |
| 449 | "[K-571]": "sect571k1", |
| 450 | "[B-163]": "sect163r2", |
| 451 | "[B-233]": "sect233r1", |
| 452 | "[B-283]": "sect283r1", |
| 453 | "[B-409]": "sect409r1", |
| 454 | "[B-571]": "sect571r1", |
| 455 | } |
| 456 | |
| 457 | for line in vector_data: |
| 458 | line = line.strip() |
| 459 | |
| 460 | if not line or line.startswith("#"): |
| 461 | continue |
| 462 | |
| 463 | if line in nist_name_map: |
| 464 | curve_name = nist_name_map[line] |
| 465 | |
| 466 | elif line.startswith("d = "): |
| 467 | if key_data is not None: |
| 468 | vectors.append(key_data) |
| 469 | |
| 470 | key_data = { |
| 471 | "curve": curve_name, |
| 472 | "d": int(line.split("=")[1], 16) |
| 473 | } |
| 474 | |
| 475 | elif key_data is not None: |
| 476 | if line.startswith("Qx = "): |
| 477 | key_data["x"] = int(line.split("=")[1], 16) |
| 478 | elif line.startswith("Qy = "): |
| 479 | key_data["y"] = int(line.split("=")[1], 16) |
| 480 | |
| 481 | if key_data is not None: |
| 482 | vectors.append(key_data) |
| 483 | |
| 484 | return vectors |