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