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