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