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