Alex Gaynor | 5951f46 | 2014-11-16 09:08:42 -0800 | [diff] [blame] | 1 | # This file is dual licensed under the terms of the Apache License, Version |
| 2 | # 2.0, and the BSD License. See the LICENSE file in the root of this repository |
| 3 | # for complete details. |
Alex Gaynor | f312a5c | 2013-08-10 15:23:38 -0400 | [diff] [blame] | 4 | |
Alex Gaynor | c37feed | 2014-03-08 08:32:56 -0800 | [diff] [blame] | 5 | from __future__ import absolute_import, division, print_function |
| 6 | |
Alex Stapleton | c387cf7 | 2014-04-13 13:58:02 +0100 | [diff] [blame] | 7 | import binascii |
Alex Gaynor | 36e651c | 2014-01-27 10:08:35 -0800 | [diff] [blame] | 8 | import collections |
Simo Sorce | 7600dee | 2015-09-22 21:56:20 -0400 | [diff] [blame] | 9 | import math |
Alex Stapleton | c387cf7 | 2014-04-13 13:58:02 +0100 | [diff] [blame] | 10 | import re |
Alex Stapleton | 707b008 | 2014-04-20 22:24:41 +0100 | [diff] [blame] | 11 | from contextlib import contextmanager |
Paul Kehrer | 90450f3 | 2014-03-19 12:37:17 -0400 | [diff] [blame] | 12 | |
Alex Stapleton | a39a319 | 2014-03-14 20:03:12 +0000 | [diff] [blame] | 13 | import pytest |
| 14 | |
Paul Kehrer | afc1ccd | 2014-03-19 11:49:32 -0400 | [diff] [blame] | 15 | import six |
Alex Gaynor | 2b3f942 | 2013-12-24 21:55:24 -0800 | [diff] [blame] | 16 | |
Alex Gaynor | 7a489db | 2014-03-22 15:09:34 -0700 | [diff] [blame] | 17 | from cryptography.exceptions import UnsupportedAlgorithm |
Alex Gaynor | 07c4dcc | 2014-04-05 11:22:07 -0700 | [diff] [blame] | 18 | |
Alex Stapleton | a39a319 | 2014-03-14 20:03:12 +0000 | [diff] [blame] | 19 | import cryptography_vectors |
Matthew Iversen | 68e77c7 | 2014-03-13 08:54:43 +1100 | [diff] [blame] | 20 | |
Alex Gaynor | 2b3f942 | 2013-12-24 21:55:24 -0800 | [diff] [blame] | 21 | |
Alex Gaynor | 36e651c | 2014-01-27 10:08:35 -0800 | [diff] [blame] | 22 | HashVector = collections.namedtuple("HashVector", ["message", "digest"]) |
| 23 | KeyedHashVector = collections.namedtuple( |
| 24 | "KeyedHashVector", ["message", "digest", "key"] |
| 25 | ) |
| 26 | |
| 27 | |
Paul Kehrer | c421e63 | 2014-01-18 09:22:21 -0600 | [diff] [blame] | 28 | def select_backends(names, backend_list): |
| 29 | if names is None: |
| 30 | return backend_list |
| 31 | split_names = [x.strip() for x in names.split(',')] |
Paul Kehrer | aed9e17 | 2014-01-19 12:09:27 -0600 | [diff] [blame] | 32 | selected_backends = [] |
| 33 | for backend in backend_list: |
| 34 | if backend.name in split_names: |
| 35 | selected_backends.append(backend) |
Paul Kehrer | c421e63 | 2014-01-18 09:22:21 -0600 | [diff] [blame] | 36 | |
Paul Kehrer | aed9e17 | 2014-01-19 12:09:27 -0600 | [diff] [blame] | 37 | if len(selected_backends) > 0: |
| 38 | return selected_backends |
Paul Kehrer | c421e63 | 2014-01-18 09:22:21 -0600 | [diff] [blame] | 39 | else: |
| 40 | raise ValueError( |
| 41 | "No backend selected. Tried to select: {0}".format(split_names) |
| 42 | ) |
Paul Kehrer | 34c075e | 2014-01-13 21:52:08 -0500 | [diff] [blame] | 43 | |
| 44 | |
Paul Kehrer | 902d8cf | 2014-10-25 12:22:10 -0700 | [diff] [blame] | 45 | def skip_if_empty(backend_list, required_interfaces): |
| 46 | if not backend_list: |
| 47 | pytest.skip( |
| 48 | "No backends provided supply the interface: {0}".format( |
| 49 | ", ".join(iface.__name__ for iface in required_interfaces) |
| 50 | ) |
| 51 | ) |
| 52 | |
| 53 | |
Paul Kehrer | 60fc8da | 2013-12-26 20:19:34 -0600 | [diff] [blame] | 54 | def check_backend_support(item): |
Paul Kehrer | 5a8fdf8 | 2013-12-26 20:13:45 -0600 | [diff] [blame] | 55 | supported = item.keywords.get("supported") |
| 56 | if supported and "backend" in item.funcargs: |
Alex Gaynor | 50ebb48 | 2015-07-02 00:21:41 -0400 | [diff] [blame] | 57 | for mark in supported: |
| 58 | if not mark.kwargs["only_if"](item.funcargs["backend"]): |
| 59 | pytest.skip("{0} ({1})".format( |
| 60 | mark.kwargs["skip_message"], item.funcargs["backend"] |
| 61 | )) |
Paul Kehrer | 5a8fdf8 | 2013-12-26 20:13:45 -0600 | [diff] [blame] | 62 | elif supported: |
Paul Kehrer | ec49550 | 2013-12-27 15:51:40 -0600 | [diff] [blame] | 63 | raise ValueError("This mark is only available on methods that take a " |
| 64 | "backend") |
Paul Kehrer | 5a8fdf8 | 2013-12-26 20:13:45 -0600 | [diff] [blame] | 65 | |
| 66 | |
Alex Gaynor | 7a489db | 2014-03-22 15:09:34 -0700 | [diff] [blame] | 67 | @contextmanager |
Alex Stapleton | 5e4c8c3 | 2014-03-27 16:38:00 +0000 | [diff] [blame] | 68 | def raises_unsupported_algorithm(reason): |
Alex Gaynor | 7a489db | 2014-03-22 15:09:34 -0700 | [diff] [blame] | 69 | with pytest.raises(UnsupportedAlgorithm) as exc_info: |
Alex Stapleton | 112963e | 2014-03-26 17:39:29 +0000 | [diff] [blame] | 70 | yield exc_info |
Alex Stapleton | 5e4c8c3 | 2014-03-27 16:38:00 +0000 | [diff] [blame] | 71 | |
Alex Stapleton | 85a791f | 2014-03-27 16:55:41 +0000 | [diff] [blame] | 72 | assert exc_info.value._reason is reason |
Alex Gaynor | 7a489db | 2014-03-22 15:09:34 -0700 | [diff] [blame] | 73 | |
| 74 | |
Paul Kehrer | fdae070 | 2014-11-27 07:50:46 -1000 | [diff] [blame] | 75 | def load_vectors_from_file(filename, loader, mode="r"): |
| 76 | with cryptography_vectors.open_vector_file(filename, mode) as vector_file: |
Alex Stapleton | a39a319 | 2014-03-14 20:03:12 +0000 | [diff] [blame] | 77 | return loader(vector_file) |
Paul Kehrer | f7f6a9f | 2013-11-11 20:43:52 -0600 | [diff] [blame] | 78 | |
| 79 | |
Alex Gaynor | d3ce703 | 2013-11-11 14:46:20 -0800 | [diff] [blame] | 80 | def load_nist_vectors(vector_data): |
Paul Kehrer | 749ac5b | 2013-11-18 18:12:41 -0600 | [diff] [blame] | 81 | test_data = None |
| 82 | data = [] |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 83 | |
| 84 | for line in vector_data: |
| 85 | line = line.strip() |
| 86 | |
Paul Kehrer | 749ac5b | 2013-11-18 18:12:41 -0600 | [diff] [blame] | 87 | # Blank lines, comments, and section headers are ignored |
Alex Gaynor | e0a879f | 2015-02-15 20:54:34 -0800 | [diff] [blame] | 88 | if not line or line.startswith("#") or (line.startswith("[") and |
| 89 | line.endswith("]")): |
Alex Gaynor | 521c42d | 2013-11-11 14:25:59 -0800 | [diff] [blame] | 90 | continue |
| 91 | |
Paul Kehrer | a43b669 | 2013-11-12 15:35:49 -0600 | [diff] [blame] | 92 | if line.strip() == "FAIL": |
Paul Kehrer | 749ac5b | 2013-11-18 18:12:41 -0600 | [diff] [blame] | 93 | test_data["fail"] = True |
Paul Kehrer | a43b669 | 2013-11-12 15:35:49 -0600 | [diff] [blame] | 94 | continue |
| 95 | |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 96 | # Build our data using a simple Key = Value format |
Paul Kehrer | a43b669 | 2013-11-12 15:35:49 -0600 | [diff] [blame] | 97 | name, value = [c.strip() for c in line.split("=")] |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 98 | |
Paul Kehrer | 1050ddf | 2014-01-27 21:04:03 -0600 | [diff] [blame] | 99 | # Some tests (PBKDF2) contain \0, which should be interpreted as a |
| 100 | # null character rather than literal. |
| 101 | value = value.replace("\\0", "\0") |
| 102 | |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 103 | # COUNT is a special token that indicates a new block of data |
| 104 | if name.upper() == "COUNT": |
Paul Kehrer | 749ac5b | 2013-11-18 18:12:41 -0600 | [diff] [blame] | 105 | test_data = {} |
| 106 | data.append(test_data) |
| 107 | continue |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 108 | # For all other tokens we simply want the name, value stored in |
| 109 | # the dictionary |
| 110 | else: |
Paul Kehrer | 749ac5b | 2013-11-18 18:12:41 -0600 | [diff] [blame] | 111 | test_data[name.lower()] = value.encode("ascii") |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 112 | |
Paul Kehrer | 749ac5b | 2013-11-18 18:12:41 -0600 | [diff] [blame] | 113 | return data |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 114 | |
| 115 | |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 116 | def load_cryptrec_vectors(vector_data): |
Paul Kehrer | e580598 | 2013-09-27 11:26:01 -0500 | [diff] [blame] | 117 | cryptrec_list = [] |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 118 | |
| 119 | for line in vector_data: |
| 120 | line = line.strip() |
| 121 | |
| 122 | # Blank lines and comments are ignored |
| 123 | if not line or line.startswith("#"): |
| 124 | continue |
| 125 | |
| 126 | if line.startswith("K"): |
Paul Kehrer | e580598 | 2013-09-27 11:26:01 -0500 | [diff] [blame] | 127 | key = line.split(" : ")[1].replace(" ", "").encode("ascii") |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 128 | elif line.startswith("P"): |
Paul Kehrer | e580598 | 2013-09-27 11:26:01 -0500 | [diff] [blame] | 129 | pt = line.split(" : ")[1].replace(" ", "").encode("ascii") |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 130 | elif line.startswith("C"): |
Paul Kehrer | e580598 | 2013-09-27 11:26:01 -0500 | [diff] [blame] | 131 | ct = line.split(" : ")[1].replace(" ", "").encode("ascii") |
| 132 | # after a C is found the K+P+C tuple is complete |
| 133 | # there are many P+C pairs for each K |
Alex Gaynor | 1fe70b1 | 2013-10-16 11:59:17 -0700 | [diff] [blame] | 134 | cryptrec_list.append({ |
| 135 | "key": key, |
| 136 | "plaintext": pt, |
| 137 | "ciphertext": ct |
| 138 | }) |
Donald Stufft | 3359d7e | 2013-10-19 19:33:06 -0400 | [diff] [blame] | 139 | else: |
| 140 | raise ValueError("Invalid line in file '{}'".format(line)) |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 141 | return cryptrec_list |
| 142 | |
| 143 | |
Paul Kehrer | 69e0652 | 2013-10-18 17:28:39 -0500 | [diff] [blame] | 144 | def load_hash_vectors(vector_data): |
| 145 | vectors = [] |
Paul Kehrer | 1bb8b71 | 2013-10-27 17:00:14 -0500 | [diff] [blame] | 146 | key = None |
| 147 | msg = None |
| 148 | md = None |
Paul Kehrer | 69e0652 | 2013-10-18 17:28:39 -0500 | [diff] [blame] | 149 | |
| 150 | for line in vector_data: |
| 151 | line = line.strip() |
| 152 | |
Paul Kehrer | 87cd0db | 2013-10-18 18:01:26 -0500 | [diff] [blame] | 153 | if not line or line.startswith("#") or line.startswith("["): |
Paul Kehrer | 69e0652 | 2013-10-18 17:28:39 -0500 | [diff] [blame] | 154 | continue |
| 155 | |
| 156 | if line.startswith("Len"): |
| 157 | length = int(line.split(" = ")[1]) |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 158 | elif line.startswith("Key"): |
Alex Gaynor | 36e651c | 2014-01-27 10:08:35 -0800 | [diff] [blame] | 159 | # HMAC vectors contain a key attribute. Hash vectors do not. |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 160 | key = line.split(" = ")[1].encode("ascii") |
Paul Kehrer | 69e0652 | 2013-10-18 17:28:39 -0500 | [diff] [blame] | 161 | elif line.startswith("Msg"): |
Alex Gaynor | 36e651c | 2014-01-27 10:08:35 -0800 | [diff] [blame] | 162 | # In the NIST vectors they have chosen to represent an empty |
| 163 | # string as hex 00, which is of course not actually an empty |
| 164 | # string. So we parse the provided length and catch this edge case. |
Paul Kehrer | 69e0652 | 2013-10-18 17:28:39 -0500 | [diff] [blame] | 165 | msg = line.split(" = ")[1].encode("ascii") if length > 0 else b"" |
| 166 | elif line.startswith("MD"): |
| 167 | md = line.split(" = ")[1] |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 168 | # after MD is found the Msg+MD (+ potential key) tuple is complete |
Paul Kehrer | 00dd509 | 2013-10-23 09:41:49 -0500 | [diff] [blame] | 169 | if key is not None: |
Alex Gaynor | 36e651c | 2014-01-27 10:08:35 -0800 | [diff] [blame] | 170 | vectors.append(KeyedHashVector(msg, md, key)) |
Paul Kehrer | 1bb8b71 | 2013-10-27 17:00:14 -0500 | [diff] [blame] | 171 | key = None |
| 172 | msg = None |
| 173 | md = None |
Paul Kehrer | 00dd509 | 2013-10-23 09:41:49 -0500 | [diff] [blame] | 174 | else: |
Alex Gaynor | 36e651c | 2014-01-27 10:08:35 -0800 | [diff] [blame] | 175 | vectors.append(HashVector(msg, md)) |
Paul Kehrer | 1bb8b71 | 2013-10-27 17:00:14 -0500 | [diff] [blame] | 176 | msg = None |
| 177 | md = None |
Paul Kehrer | 69e0652 | 2013-10-18 17:28:39 -0500 | [diff] [blame] | 178 | else: |
| 179 | raise ValueError("Unknown line in hash vector") |
| 180 | return vectors |
Alex Stapleton | 58f27ac | 2014-02-02 19:30:03 +0000 | [diff] [blame] | 181 | |
| 182 | |
| 183 | def load_pkcs1_vectors(vector_data): |
| 184 | """ |
| 185 | Loads data out of RSA PKCS #1 vector files. |
Alex Stapleton | 58f27ac | 2014-02-02 19:30:03 +0000 | [diff] [blame] | 186 | """ |
| 187 | private_key_vector = None |
| 188 | public_key_vector = None |
| 189 | attr = None |
| 190 | key = None |
Paul Kehrer | efca280 | 2014-02-17 20:55:13 -0600 | [diff] [blame] | 191 | example_vector = None |
| 192 | examples = [] |
Alex Stapleton | 58f27ac | 2014-02-02 19:30:03 +0000 | [diff] [blame] | 193 | vectors = [] |
| 194 | for line in vector_data: |
Paul Kehrer | 7774a03 | 2014-02-17 22:56:55 -0600 | [diff] [blame] | 195 | if ( |
| 196 | line.startswith("# PSS Example") or |
Paul Kehrer | 3fe9150 | 2014-03-29 12:08:39 -0500 | [diff] [blame] | 197 | line.startswith("# OAEP Example") or |
| 198 | line.startswith("# PKCS#1 v1.5") |
Paul Kehrer | 7774a03 | 2014-02-17 22:56:55 -0600 | [diff] [blame] | 199 | ): |
Paul Kehrer | efca280 | 2014-02-17 20:55:13 -0600 | [diff] [blame] | 200 | if example_vector: |
| 201 | for key, value in six.iteritems(example_vector): |
Paul Kehrer | 2681180 | 2014-02-19 16:32:11 -0600 | [diff] [blame] | 202 | hex_str = "".join(value).replace(" ", "").encode("ascii") |
Paul Kehrer | efca280 | 2014-02-17 20:55:13 -0600 | [diff] [blame] | 203 | example_vector[key] = hex_str |
| 204 | examples.append(example_vector) |
| 205 | |
| 206 | attr = None |
| 207 | example_vector = collections.defaultdict(list) |
| 208 | |
Paul Kehrer | 3fe9150 | 2014-03-29 12:08:39 -0500 | [diff] [blame] | 209 | if line.startswith("# Message"): |
Paul Kehrer | 7d9c306 | 2014-02-18 08:27:39 -0600 | [diff] [blame] | 210 | attr = "message" |
Paul Kehrer | efca280 | 2014-02-17 20:55:13 -0600 | [diff] [blame] | 211 | continue |
| 212 | elif line.startswith("# Salt"): |
| 213 | attr = "salt" |
| 214 | continue |
Paul Kehrer | 3fe9150 | 2014-03-29 12:08:39 -0500 | [diff] [blame] | 215 | elif line.startswith("# Seed"): |
| 216 | attr = "seed" |
| 217 | continue |
Paul Kehrer | efca280 | 2014-02-17 20:55:13 -0600 | [diff] [blame] | 218 | elif line.startswith("# Signature"): |
| 219 | attr = "signature" |
| 220 | continue |
Paul Kehrer | 3fe9150 | 2014-03-29 12:08:39 -0500 | [diff] [blame] | 221 | elif line.startswith("# Encryption"): |
| 222 | attr = "encryption" |
| 223 | continue |
Paul Kehrer | efca280 | 2014-02-17 20:55:13 -0600 | [diff] [blame] | 224 | elif ( |
| 225 | example_vector and |
| 226 | line.startswith("# =============================================") |
| 227 | ): |
| 228 | for key, value in six.iteritems(example_vector): |
Paul Kehrer | 2681180 | 2014-02-19 16:32:11 -0600 | [diff] [blame] | 229 | hex_str = "".join(value).replace(" ", "").encode("ascii") |
Paul Kehrer | efca280 | 2014-02-17 20:55:13 -0600 | [diff] [blame] | 230 | example_vector[key] = hex_str |
| 231 | examples.append(example_vector) |
| 232 | example_vector = None |
| 233 | attr = None |
| 234 | elif example_vector and line.startswith("#"): |
| 235 | continue |
| 236 | else: |
| 237 | if attr is not None and example_vector is not None: |
| 238 | example_vector[attr].append(line.strip()) |
| 239 | continue |
| 240 | |
Alex Stapleton | 58f27ac | 2014-02-02 19:30:03 +0000 | [diff] [blame] | 241 | if ( |
| 242 | line.startswith("# Example") or |
| 243 | line.startswith("# =============================================") |
| 244 | ): |
| 245 | if key: |
| 246 | assert private_key_vector |
| 247 | assert public_key_vector |
| 248 | |
| 249 | for key, value in six.iteritems(public_key_vector): |
| 250 | hex_str = "".join(value).replace(" ", "") |
| 251 | public_key_vector[key] = int(hex_str, 16) |
| 252 | |
| 253 | for key, value in six.iteritems(private_key_vector): |
| 254 | hex_str = "".join(value).replace(" ", "") |
| 255 | private_key_vector[key] = int(hex_str, 16) |
| 256 | |
Paul Kehrer | efca280 | 2014-02-17 20:55:13 -0600 | [diff] [blame] | 257 | private_key_vector["examples"] = examples |
| 258 | examples = [] |
| 259 | |
Alex Stapleton | 58f27ac | 2014-02-02 19:30:03 +0000 | [diff] [blame] | 260 | assert ( |
| 261 | private_key_vector['public_exponent'] == |
| 262 | public_key_vector['public_exponent'] |
| 263 | ) |
| 264 | |
| 265 | assert ( |
| 266 | private_key_vector['modulus'] == |
| 267 | public_key_vector['modulus'] |
| 268 | ) |
| 269 | |
| 270 | vectors.append( |
| 271 | (private_key_vector, public_key_vector) |
| 272 | ) |
| 273 | |
| 274 | public_key_vector = collections.defaultdict(list) |
| 275 | private_key_vector = collections.defaultdict(list) |
| 276 | key = None |
| 277 | attr = None |
| 278 | |
| 279 | if private_key_vector is None or public_key_vector is None: |
| 280 | continue |
| 281 | |
| 282 | if line.startswith("# Private key"): |
| 283 | key = private_key_vector |
| 284 | elif line.startswith("# Public key"): |
| 285 | key = public_key_vector |
| 286 | elif line.startswith("# Modulus:"): |
| 287 | attr = "modulus" |
| 288 | elif line.startswith("# Public exponent:"): |
| 289 | attr = "public_exponent" |
| 290 | elif line.startswith("# Exponent:"): |
| 291 | if key is public_key_vector: |
| 292 | attr = "public_exponent" |
| 293 | else: |
| 294 | assert key is private_key_vector |
| 295 | attr = "private_exponent" |
| 296 | elif line.startswith("# Prime 1:"): |
| 297 | attr = "p" |
| 298 | elif line.startswith("# Prime 2:"): |
| 299 | attr = "q" |
Paul Kehrer | 09328bb | 2014-02-12 23:57:27 -0600 | [diff] [blame] | 300 | elif line.startswith("# Prime exponent 1:"): |
| 301 | attr = "dmp1" |
| 302 | elif line.startswith("# Prime exponent 2:"): |
| 303 | attr = "dmq1" |
| 304 | elif line.startswith("# Coefficient:"): |
| 305 | attr = "iqmp" |
Alex Stapleton | 58f27ac | 2014-02-02 19:30:03 +0000 | [diff] [blame] | 306 | elif line.startswith("#"): |
| 307 | attr = None |
| 308 | else: |
| 309 | if key is not None and attr is not None: |
| 310 | key[attr].append(line.strip()) |
| 311 | return vectors |
Paul Kehrer | 2f2a206 | 2014-03-10 23:30:28 -0400 | [diff] [blame] | 312 | |
| 313 | |
| 314 | def load_rsa_nist_vectors(vector_data): |
| 315 | test_data = None |
Paul Kehrer | 62707f1 | 2014-03-18 07:19:14 -0400 | [diff] [blame] | 316 | p = None |
Paul Kehrer | afc2518 | 2014-03-18 07:51:56 -0400 | [diff] [blame] | 317 | salt_length = None |
Paul Kehrer | 2f2a206 | 2014-03-10 23:30:28 -0400 | [diff] [blame] | 318 | data = [] |
| 319 | |
| 320 | for line in vector_data: |
| 321 | line = line.strip() |
| 322 | |
| 323 | # Blank lines and section headers are ignored |
| 324 | if not line or line.startswith("["): |
| 325 | continue |
| 326 | |
| 327 | if line.startswith("# Salt len:"): |
| 328 | salt_length = int(line.split(":")[1].strip()) |
| 329 | continue |
| 330 | elif line.startswith("#"): |
| 331 | continue |
| 332 | |
| 333 | # Build our data using a simple Key = Value format |
| 334 | name, value = [c.strip() for c in line.split("=")] |
| 335 | |
| 336 | if name == "n": |
| 337 | n = int(value, 16) |
Paul Kehrer | 62707f1 | 2014-03-18 07:19:14 -0400 | [diff] [blame] | 338 | elif name == "e" and p is None: |
Paul Kehrer | 2f2a206 | 2014-03-10 23:30:28 -0400 | [diff] [blame] | 339 | e = int(value, 16) |
Paul Kehrer | 62707f1 | 2014-03-18 07:19:14 -0400 | [diff] [blame] | 340 | elif name == "p": |
| 341 | p = int(value, 16) |
| 342 | elif name == "q": |
| 343 | q = int(value, 16) |
Paul Kehrer | 2f2a206 | 2014-03-10 23:30:28 -0400 | [diff] [blame] | 344 | elif name == "SHAAlg": |
Paul Kehrer | 62707f1 | 2014-03-18 07:19:14 -0400 | [diff] [blame] | 345 | if p is None: |
| 346 | test_data = { |
| 347 | "modulus": n, |
| 348 | "public_exponent": e, |
| 349 | "salt_length": salt_length, |
Paul Kehrer | e66f69a | 2014-03-18 07:57:26 -0400 | [diff] [blame] | 350 | "algorithm": value, |
Paul Kehrer | 62707f1 | 2014-03-18 07:19:14 -0400 | [diff] [blame] | 351 | "fail": False |
| 352 | } |
| 353 | else: |
| 354 | test_data = { |
| 355 | "modulus": n, |
| 356 | "p": p, |
| 357 | "q": q, |
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 | } |
Paul Kehrer | afc2518 | 2014-03-18 07:51:56 -0400 | [diff] [blame] | 360 | if salt_length is not None: |
| 361 | test_data["salt_length"] = salt_length |
Paul Kehrer | 2f2a206 | 2014-03-10 23:30:28 -0400 | [diff] [blame] | 362 | data.append(test_data) |
Paul Kehrer | 62707f1 | 2014-03-18 07:19:14 -0400 | [diff] [blame] | 363 | elif name == "e" and p is not None: |
| 364 | test_data["public_exponent"] = int(value, 16) |
| 365 | elif name == "d": |
| 366 | test_data["private_exponent"] = int(value, 16) |
| 367 | elif name == "Result": |
| 368 | test_data["fail"] = value.startswith("F") |
Paul Kehrer | 2f2a206 | 2014-03-10 23:30:28 -0400 | [diff] [blame] | 369 | # For all other tokens we simply want the name, value stored in |
| 370 | # the dictionary |
| 371 | else: |
| 372 | test_data[name.lower()] = value.encode("ascii") |
| 373 | |
| 374 | return data |
Mohammed Attia | 987cc70 | 2014-03-12 16:07:21 +0200 | [diff] [blame] | 375 | |
| 376 | |
| 377 | def load_fips_dsa_key_pair_vectors(vector_data): |
| 378 | """ |
| 379 | Loads data out of the FIPS DSA KeyPair vector files. |
| 380 | """ |
| 381 | vectors = [] |
Mohammed Attia | 49b9259 | 2014-03-12 20:07:05 +0200 | [diff] [blame] | 382 | # When reading_key_data is set to True it tells the loader to continue |
| 383 | # constructing dictionaries. We set reading_key_data to False during the |
| 384 | # blocks of the vectors of N=224 because we don't support it. |
| 385 | reading_key_data = True |
Mohammed Attia | 987cc70 | 2014-03-12 16:07:21 +0200 | [diff] [blame] | 386 | for line in vector_data: |
| 387 | line = line.strip() |
| 388 | |
| 389 | if not line or line.startswith("#"): |
| 390 | continue |
Mohammed Attia | 49b9259 | 2014-03-12 20:07:05 +0200 | [diff] [blame] | 391 | elif line.startswith("[mod = L=1024"): |
Mohammed Attia | 987cc70 | 2014-03-12 16:07:21 +0200 | [diff] [blame] | 392 | continue |
Mohammed Attia | 49b9259 | 2014-03-12 20:07:05 +0200 | [diff] [blame] | 393 | elif line.startswith("[mod = L=2048, N=224"): |
| 394 | reading_key_data = False |
Mohammed Attia | 987cc70 | 2014-03-12 16:07:21 +0200 | [diff] [blame] | 395 | continue |
Mohammed Attia | 49b9259 | 2014-03-12 20:07:05 +0200 | [diff] [blame] | 396 | elif line.startswith("[mod = L=2048, N=256"): |
| 397 | reading_key_data = True |
Mohammed Attia | 987cc70 | 2014-03-12 16:07:21 +0200 | [diff] [blame] | 398 | continue |
Mohammed Attia | 49b9259 | 2014-03-12 20:07:05 +0200 | [diff] [blame] | 399 | elif line.startswith("[mod = L=3072"): |
Mohammed Attia | 987cc70 | 2014-03-12 16:07:21 +0200 | [diff] [blame] | 400 | continue |
Alex Gaynor | 0fe7db6 | 2015-06-27 17:20:59 -0400 | [diff] [blame] | 401 | |
| 402 | if reading_key_data: |
Mohammed Attia | 987cc70 | 2014-03-12 16:07:21 +0200 | [diff] [blame] | 403 | if line.startswith("P"): |
| 404 | vectors.append({'p': int(line.split("=")[1], 16)}) |
Mohammed Attia | 22ccb87 | 2014-03-12 18:27:59 +0200 | [diff] [blame] | 405 | elif line.startswith("Q"): |
Mohammed Attia | 987cc70 | 2014-03-12 16:07:21 +0200 | [diff] [blame] | 406 | vectors[-1]['q'] = int(line.split("=")[1], 16) |
Mohammed Attia | 22ccb87 | 2014-03-12 18:27:59 +0200 | [diff] [blame] | 407 | elif line.startswith("G"): |
Mohammed Attia | 987cc70 | 2014-03-12 16:07:21 +0200 | [diff] [blame] | 408 | vectors[-1]['g'] = int(line.split("=")[1], 16) |
Mohammed Attia | 22ccb87 | 2014-03-12 18:27:59 +0200 | [diff] [blame] | 409 | elif line.startswith("X") and 'x' not in vectors[-1]: |
Mohammed Attia | 987cc70 | 2014-03-12 16:07:21 +0200 | [diff] [blame] | 410 | vectors[-1]['x'] = int(line.split("=")[1], 16) |
Mohammed Attia | 22ccb87 | 2014-03-12 18:27:59 +0200 | [diff] [blame] | 411 | elif line.startswith("X") and 'x' in vectors[-1]: |
Mohammed Attia | 987cc70 | 2014-03-12 16:07:21 +0200 | [diff] [blame] | 412 | vectors.append({'p': vectors[-1]['p'], |
| 413 | 'q': vectors[-1]['q'], |
| 414 | 'g': vectors[-1]['g'], |
| 415 | 'x': int(line.split("=")[1], 16) |
| 416 | }) |
Mohammed Attia | 22ccb87 | 2014-03-12 18:27:59 +0200 | [diff] [blame] | 417 | elif line.startswith("Y"): |
Mohammed Attia | 987cc70 | 2014-03-12 16:07:21 +0200 | [diff] [blame] | 418 | vectors[-1]['y'] = int(line.split("=")[1], 16) |
Mohammed Attia | 987cc70 | 2014-03-12 16:07:21 +0200 | [diff] [blame] | 419 | |
| 420 | return vectors |
Alex Stapleton | cf04860 | 2014-04-12 12:48:59 +0100 | [diff] [blame] | 421 | |
| 422 | |
Mohammed Attia | 3c9e158 | 2014-04-22 14:24:44 +0200 | [diff] [blame] | 423 | def load_fips_dsa_sig_vectors(vector_data): |
Mohammed Attia | 0fb5d85 | 2014-04-21 10:31:15 +0200 | [diff] [blame] | 424 | """ |
| 425 | Loads data out of the FIPS DSA SigVer vector files. |
| 426 | """ |
| 427 | vectors = [] |
| 428 | sha_regex = re.compile( |
| 429 | r"\[mod = L=...., N=..., SHA-(?P<sha>1|224|256|384|512)\]" |
| 430 | ) |
| 431 | # When reading_key_data is set to True it tells the loader to continue |
| 432 | # constructing dictionaries. We set reading_key_data to False during the |
| 433 | # blocks of the vectors of N=224 because we don't support it. |
| 434 | reading_key_data = True |
Mohammed Attia | 3c9e158 | 2014-04-22 14:24:44 +0200 | [diff] [blame] | 435 | |
Mohammed Attia | 0fb5d85 | 2014-04-21 10:31:15 +0200 | [diff] [blame] | 436 | for line in vector_data: |
| 437 | line = line.strip() |
| 438 | |
| 439 | if not line or line.startswith("#"): |
| 440 | continue |
| 441 | |
| 442 | sha_match = sha_regex.match(line) |
| 443 | if sha_match: |
| 444 | digest_algorithm = "SHA-{0}".format(sha_match.group("sha")) |
| 445 | |
Paul Kehrer | 7ef2f8f | 2014-04-22 08:37:58 -0500 | [diff] [blame] | 446 | if line.startswith("[mod = L=2048, N=224"): |
Mohammed Attia | 0fb5d85 | 2014-04-21 10:31:15 +0200 | [diff] [blame] | 447 | reading_key_data = False |
| 448 | continue |
Paul Kehrer | 7ef2f8f | 2014-04-22 08:37:58 -0500 | [diff] [blame] | 449 | elif line.startswith("[mod = L=2048, N=256"): |
Mohammed Attia | 0fb5d85 | 2014-04-21 10:31:15 +0200 | [diff] [blame] | 450 | reading_key_data = True |
| 451 | continue |
| 452 | |
| 453 | if not reading_key_data or line.startswith("[mod"): |
| 454 | continue |
| 455 | |
| 456 | name, value = [c.strip() for c in line.split("=")] |
| 457 | |
| 458 | if name == "P": |
| 459 | vectors.append({'p': int(value, 16), |
| 460 | 'digest_algorithm': digest_algorithm}) |
| 461 | elif name == "Q": |
| 462 | vectors[-1]['q'] = int(value, 16) |
| 463 | elif name == "G": |
| 464 | vectors[-1]['g'] = int(value, 16) |
| 465 | elif name == "Msg" and 'msg' not in vectors[-1]: |
| 466 | hexmsg = value.strip().encode("ascii") |
| 467 | vectors[-1]['msg'] = binascii.unhexlify(hexmsg) |
| 468 | elif name == "Msg" and 'msg' in vectors[-1]: |
| 469 | hexmsg = value.strip().encode("ascii") |
| 470 | vectors.append({'p': vectors[-1]['p'], |
| 471 | 'q': vectors[-1]['q'], |
| 472 | 'g': vectors[-1]['g'], |
| 473 | 'digest_algorithm': |
| 474 | vectors[-1]['digest_algorithm'], |
| 475 | 'msg': binascii.unhexlify(hexmsg)}) |
| 476 | elif name == "X": |
| 477 | vectors[-1]['x'] = int(value, 16) |
| 478 | elif name == "Y": |
| 479 | vectors[-1]['y'] = int(value, 16) |
| 480 | elif name == "R": |
| 481 | vectors[-1]['r'] = int(value, 16) |
| 482 | elif name == "S": |
| 483 | vectors[-1]['s'] = int(value, 16) |
| 484 | elif name == "Result": |
| 485 | vectors[-1]['result'] = value.split("(")[0].strip() |
| 486 | |
| 487 | return vectors |
| 488 | |
| 489 | |
Alex Stapleton | 44fe82d | 2014-04-19 09:44:26 +0100 | [diff] [blame] | 490 | # http://tools.ietf.org/html/rfc4492#appendix-A |
Alex Stapleton | c387cf7 | 2014-04-13 13:58:02 +0100 | [diff] [blame] | 491 | _ECDSA_CURVE_NAMES = { |
| 492 | "P-192": "secp192r1", |
| 493 | "P-224": "secp224r1", |
Alex Stapleton | 39e300f | 2014-04-18 22:44:02 +0100 | [diff] [blame] | 494 | "P-256": "secp256r1", |
Alex Stapleton | c387cf7 | 2014-04-13 13:58:02 +0100 | [diff] [blame] | 495 | "P-384": "secp384r1", |
| 496 | "P-521": "secp521r1", |
Alex Stapleton | 39e300f | 2014-04-18 22:44:02 +0100 | [diff] [blame] | 497 | |
Alex Stapleton | c387cf7 | 2014-04-13 13:58:02 +0100 | [diff] [blame] | 498 | "K-163": "sect163k1", |
| 499 | "K-233": "sect233k1", |
Alex Stapleton | f6a1cf6 | 2015-05-03 12:16:19 +0100 | [diff] [blame] | 500 | "K-256": "secp256k1", |
Alex Stapleton | 39e300f | 2014-04-18 22:44:02 +0100 | [diff] [blame] | 501 | "K-283": "sect283k1", |
Alex Stapleton | c387cf7 | 2014-04-13 13:58:02 +0100 | [diff] [blame] | 502 | "K-409": "sect409k1", |
| 503 | "K-571": "sect571k1", |
Alex Stapleton | 39e300f | 2014-04-18 22:44:02 +0100 | [diff] [blame] | 504 | |
Alex Stapleton | 44fe82d | 2014-04-19 09:44:26 +0100 | [diff] [blame] | 505 | "B-163": "sect163r2", |
Alex Stapleton | c387cf7 | 2014-04-13 13:58:02 +0100 | [diff] [blame] | 506 | "B-233": "sect233r1", |
| 507 | "B-283": "sect283r1", |
| 508 | "B-409": "sect409r1", |
| 509 | "B-571": "sect571r1", |
| 510 | } |
| 511 | |
| 512 | |
Alex Stapleton | cf04860 | 2014-04-12 12:48:59 +0100 | [diff] [blame] | 513 | def load_fips_ecdsa_key_pair_vectors(vector_data): |
| 514 | """ |
| 515 | Loads data out of the FIPS ECDSA KeyPair vector files. |
| 516 | """ |
| 517 | vectors = [] |
| 518 | key_data = None |
Alex Stapleton | cf04860 | 2014-04-12 12:48:59 +0100 | [diff] [blame] | 519 | for line in vector_data: |
| 520 | line = line.strip() |
| 521 | |
| 522 | if not line or line.startswith("#"): |
| 523 | continue |
| 524 | |
Alex Stapleton | c387cf7 | 2014-04-13 13:58:02 +0100 | [diff] [blame] | 525 | if line[1:-1] in _ECDSA_CURVE_NAMES: |
| 526 | curve_name = _ECDSA_CURVE_NAMES[line[1:-1]] |
Alex Stapleton | cf04860 | 2014-04-12 12:48:59 +0100 | [diff] [blame] | 527 | |
| 528 | elif line.startswith("d = "): |
| 529 | if key_data is not None: |
| 530 | vectors.append(key_data) |
| 531 | |
| 532 | key_data = { |
| 533 | "curve": curve_name, |
| 534 | "d": int(line.split("=")[1], 16) |
| 535 | } |
| 536 | |
| 537 | elif key_data is not None: |
| 538 | if line.startswith("Qx = "): |
| 539 | key_data["x"] = int(line.split("=")[1], 16) |
| 540 | elif line.startswith("Qy = "): |
| 541 | key_data["y"] = int(line.split("=")[1], 16) |
| 542 | |
Paul Kehrer | b60b8dd | 2015-08-01 19:47:22 +0100 | [diff] [blame] | 543 | assert key_data is not None |
| 544 | vectors.append(key_data) |
Alex Stapleton | cf04860 | 2014-04-12 12:48:59 +0100 | [diff] [blame] | 545 | |
| 546 | return vectors |
Alex Stapleton | c387cf7 | 2014-04-13 13:58:02 +0100 | [diff] [blame] | 547 | |
| 548 | |
| 549 | def load_fips_ecdsa_signing_vectors(vector_data): |
| 550 | """ |
| 551 | Loads data out of the FIPS ECDSA SigGen vector files. |
| 552 | """ |
| 553 | vectors = [] |
| 554 | |
| 555 | curve_rx = re.compile( |
| 556 | r"\[(?P<curve>[PKB]-[0-9]{3}),SHA-(?P<sha>1|224|256|384|512)\]" |
| 557 | ) |
| 558 | |
| 559 | data = None |
| 560 | for line in vector_data: |
| 561 | line = line.strip() |
| 562 | |
Alex Stapleton | c387cf7 | 2014-04-13 13:58:02 +0100 | [diff] [blame] | 563 | curve_match = curve_rx.match(line) |
| 564 | if curve_match: |
| 565 | curve_name = _ECDSA_CURVE_NAMES[curve_match.group("curve")] |
| 566 | digest_name = "SHA-{0}".format(curve_match.group("sha")) |
| 567 | |
| 568 | elif line.startswith("Msg = "): |
| 569 | if data is not None: |
| 570 | vectors.append(data) |
| 571 | |
| 572 | hexmsg = line.split("=")[1].strip().encode("ascii") |
| 573 | |
| 574 | data = { |
| 575 | "curve": curve_name, |
| 576 | "digest_algorithm": digest_name, |
| 577 | "message": binascii.unhexlify(hexmsg) |
| 578 | } |
| 579 | |
| 580 | elif data is not None: |
| 581 | if line.startswith("Qx = "): |
| 582 | data["x"] = int(line.split("=")[1], 16) |
| 583 | elif line.startswith("Qy = "): |
| 584 | data["y"] = int(line.split("=")[1], 16) |
| 585 | elif line.startswith("R = "): |
| 586 | data["r"] = int(line.split("=")[1], 16) |
| 587 | elif line.startswith("S = "): |
| 588 | data["s"] = int(line.split("=")[1], 16) |
| 589 | elif line.startswith("d = "): |
| 590 | data["d"] = int(line.split("=")[1], 16) |
Alex Stapleton | 6f72949 | 2014-04-19 09:01:25 +0100 | [diff] [blame] | 591 | elif line.startswith("Result = "): |
| 592 | data["fail"] = line.split("=")[1].strip()[0] == "F" |
Alex Stapleton | c387cf7 | 2014-04-13 13:58:02 +0100 | [diff] [blame] | 593 | |
Paul Kehrer | b60b8dd | 2015-08-01 19:47:22 +0100 | [diff] [blame] | 594 | assert data is not None |
| 595 | vectors.append(data) |
Alex Stapleton | c387cf7 | 2014-04-13 13:58:02 +0100 | [diff] [blame] | 596 | return vectors |
Alex Stapleton | 839c09d | 2014-08-10 12:18:02 +0100 | [diff] [blame] | 597 | |
| 598 | |
| 599 | def load_kasvs_dh_vectors(vector_data): |
| 600 | """ |
| 601 | Loads data out of the KASVS key exchange vector data |
| 602 | """ |
| 603 | |
| 604 | result_rx = re.compile(r"([FP]) \(([0-9]+) -") |
| 605 | |
| 606 | vectors = [] |
| 607 | data = { |
| 608 | "fail_z": False, |
| 609 | "fail_agree": False |
| 610 | } |
| 611 | |
| 612 | for line in vector_data: |
| 613 | line = line.strip() |
| 614 | |
| 615 | if not line or line.startswith("#"): |
| 616 | continue |
| 617 | |
| 618 | if line.startswith("P = "): |
| 619 | data["p"] = int(line.split("=")[1], 16) |
| 620 | elif line.startswith("Q = "): |
| 621 | data["q"] = int(line.split("=")[1], 16) |
| 622 | elif line.startswith("G = "): |
| 623 | data["g"] = int(line.split("=")[1], 16) |
| 624 | elif line.startswith("Z = "): |
| 625 | z_hex = line.split("=")[1].strip().encode("ascii") |
| 626 | data["z"] = binascii.unhexlify(z_hex) |
| 627 | elif line.startswith("XstatCAVS = "): |
| 628 | data["x1"] = int(line.split("=")[1], 16) |
| 629 | elif line.startswith("YstatCAVS = "): |
| 630 | data["y1"] = int(line.split("=")[1], 16) |
| 631 | elif line.startswith("XstatIUT = "): |
| 632 | data["x2"] = int(line.split("=")[1], 16) |
| 633 | elif line.startswith("YstatIUT = "): |
| 634 | data["y2"] = int(line.split("=")[1], 16) |
| 635 | elif line.startswith("Result = "): |
| 636 | result_str = line.split("=")[1].strip() |
| 637 | match = result_rx.match(result_str) |
| 638 | |
| 639 | if match.group(1) == "F": |
| 640 | if int(match.group(2)) in (5, 10): |
| 641 | data["fail_z"] = True |
| 642 | else: |
| 643 | data["fail_agree"] = True |
| 644 | |
| 645 | vectors.append(data) |
| 646 | |
| 647 | data = { |
| 648 | "p": data["p"], |
| 649 | "q": data["q"], |
| 650 | "g": data["g"], |
| 651 | "fail_z": False, |
| 652 | "fail_agree": False |
| 653 | } |
| 654 | |
| 655 | return vectors |
Simo Sorce | 917addb | 2015-04-29 19:41:26 -0400 | [diff] [blame] | 656 | |
| 657 | |
| 658 | def load_kasvs_ecdh_vectors(vector_data): |
| 659 | """ |
| 660 | Loads data out of the KASVS key exchange vector data |
| 661 | """ |
| 662 | |
| 663 | curve_name_map = { |
| 664 | "P-192": "secp192r1", |
| 665 | "P-224": "secp224r1", |
| 666 | "P-256": "secp256r1", |
| 667 | "P-384": "secp384r1", |
| 668 | "P-521": "secp521r1", |
| 669 | } |
| 670 | |
| 671 | result_rx = re.compile(r"([FP]) \(([0-9]+) -") |
| 672 | |
| 673 | tags = [] |
Alex Gaynor | ace036d | 2015-09-24 20:23:08 -0400 | [diff] [blame] | 674 | sets = {} |
Simo Sorce | 917addb | 2015-04-29 19:41:26 -0400 | [diff] [blame] | 675 | vectors = [] |
| 676 | |
| 677 | # find info in header |
| 678 | for line in vector_data: |
| 679 | line = line.strip() |
| 680 | |
| 681 | if line.startswith("#"): |
| 682 | parm = line.split("Parameter set(s) supported:") |
| 683 | if len(parm) == 2: |
| 684 | names = parm[1].strip().split() |
| 685 | for n in names: |
| 686 | tags.append("[%s]" % n) |
| 687 | break |
| 688 | |
| 689 | # Sets Metadata |
| 690 | tag = None |
| 691 | curve = None |
| 692 | for line in vector_data: |
| 693 | line = line.strip() |
| 694 | |
| 695 | if not line or line.startswith("#"): |
| 696 | continue |
| 697 | |
| 698 | if line in tags: |
| 699 | tag = line |
| 700 | curve = None |
| 701 | elif line.startswith("[Curve selected:"): |
| 702 | curve = curve_name_map[line.split(':')[1].strip()[:-1]] |
| 703 | |
| 704 | if tag is not None and curve is not None: |
| 705 | sets[tag.strip("[]")] = curve |
| 706 | tag = None |
| 707 | if len(tags) == len(sets): |
| 708 | break |
| 709 | |
| 710 | # Data |
| 711 | data = { |
Alex Gaynor | ace036d | 2015-09-24 20:23:08 -0400 | [diff] [blame] | 712 | "CAVS": {}, |
| 713 | "IUT": {}, |
Simo Sorce | 917addb | 2015-04-29 19:41:26 -0400 | [diff] [blame] | 714 | } |
| 715 | tag = None |
| 716 | for line in vector_data: |
| 717 | line = line.strip() |
| 718 | |
| 719 | if not line or line.startswith("#"): |
| 720 | continue |
| 721 | |
| 722 | if line.startswith("["): |
| 723 | tag = line.split()[0][1:] |
| 724 | elif line.startswith("COUNT = "): |
| 725 | data["COUNT"] = int(line.split("=")[1], 16) |
| 726 | elif line.startswith("dsCAVS = "): |
| 727 | data["CAVS"]["d"] = int(line.split("=")[1], 16) |
| 728 | elif line.startswith("QsCAVSx = "): |
| 729 | data["CAVS"]["x"] = int(line.split("=")[1], 16) |
| 730 | elif line.startswith("QsCAVSy = "): |
| 731 | data["CAVS"]["y"] = int(line.split("=")[1], 16) |
| 732 | elif line.startswith("dsIUT = "): |
| 733 | data["IUT"]["d"] = int(line.split("=")[1], 16) |
| 734 | elif line.startswith("QsIUTx = "): |
| 735 | data["IUT"]["x"] = int(line.split("=")[1], 16) |
| 736 | elif line.startswith("QsIUTy = "): |
| 737 | data["IUT"]["y"] = int(line.split("=")[1], 16) |
Simo Sorce | 83e563e | 2015-05-06 10:56:31 -0400 | [diff] [blame] | 738 | elif line.startswith("OI = "): |
| 739 | data["OI"] = int(line.split("=")[1], 16) |
Simo Sorce | 917addb | 2015-04-29 19:41:26 -0400 | [diff] [blame] | 740 | elif line.startswith("Z = "): |
| 741 | data["Z"] = int(line.split("=")[1], 16) |
Simo Sorce | 83e563e | 2015-05-06 10:56:31 -0400 | [diff] [blame] | 742 | elif line.startswith("DKM = "): |
| 743 | data["DKM"] = int(line.split("=")[1], 16) |
Simo Sorce | 917addb | 2015-04-29 19:41:26 -0400 | [diff] [blame] | 744 | elif line.startswith("Result = "): |
| 745 | result_str = line.split("=")[1].strip() |
| 746 | match = result_rx.match(result_str) |
| 747 | |
| 748 | if match.group(1) == "F": |
| 749 | data["fail"] = True |
| 750 | else: |
| 751 | data["fail"] = False |
| 752 | data["errno"] = int(match.group(2)) |
| 753 | |
| 754 | data["curve"] = sets[tag] |
| 755 | |
| 756 | vectors.append(data) |
| 757 | |
| 758 | data = { |
Alex Gaynor | ace036d | 2015-09-24 20:23:08 -0400 | [diff] [blame] | 759 | "CAVS": {}, |
| 760 | "IUT": {}, |
Simo Sorce | 917addb | 2015-04-29 19:41:26 -0400 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | return vectors |
Simo Sorce | 7600dee | 2015-09-22 21:56:20 -0400 | [diff] [blame] | 764 | |
| 765 | |
| 766 | def load_x963_vectors(vector_data): |
| 767 | """ |
| 768 | Loads data out of the X9.63 vector data |
| 769 | """ |
| 770 | |
| 771 | vectors = [] |
| 772 | |
| 773 | # Sets Metadata |
| 774 | hashname = None |
Alex Gaynor | ace036d | 2015-09-24 20:23:08 -0400 | [diff] [blame] | 775 | vector = {} |
Simo Sorce | 7600dee | 2015-09-22 21:56:20 -0400 | [diff] [blame] | 776 | for line in vector_data: |
| 777 | line = line.strip() |
| 778 | |
| 779 | if line.startswith("[SHA"): |
| 780 | hashname = line[1:-1] |
| 781 | shared_secret_len = 0 |
| 782 | shared_info_len = 0 |
| 783 | key_data_len = 0 |
| 784 | elif line.startswith("[shared secret length"): |
| 785 | shared_secret_len = int(line[1:-1].split("=")[1].strip()) |
| 786 | elif line.startswith("[SharedInfo length"): |
| 787 | shared_info_len = int(line[1:-1].split("=")[1].strip()) |
| 788 | elif line.startswith("[key data length"): |
| 789 | key_data_len = int(line[1:-1].split("=")[1].strip()) |
| 790 | elif line.startswith("COUNT"): |
| 791 | count = int(line.split("=")[1].strip()) |
| 792 | vector["hash"] = hashname |
| 793 | vector["count"] = count |
Alex Gaynor | ace036d | 2015-09-24 20:23:08 -0400 | [diff] [blame] | 794 | vector["shared_secret_length"] = shared_secret_len |
| 795 | vector["sharedinfo_length"] = shared_info_len |
| 796 | vector["key_data_length"] = key_data_len |
Simo Sorce | 7600dee | 2015-09-22 21:56:20 -0400 | [diff] [blame] | 797 | elif line.startswith("Z"): |
| 798 | vector["Z"] = line.split("=")[1].strip() |
| 799 | assert math.ceil(shared_secret_len / 8) * 2 == len(vector["Z"]) |
| 800 | elif line.startswith("SharedInfo"): |
| 801 | if shared_info_len != 0: |
Alex Gaynor | ace036d | 2015-09-24 20:23:08 -0400 | [diff] [blame] | 802 | vector["sharedinfo"] = line.split("=")[1].strip() |
| 803 | silen = len(vector["sharedinfo"]) |
Simo Sorce | 7600dee | 2015-09-22 21:56:20 -0400 | [diff] [blame] | 804 | assert math.ceil(shared_info_len / 8) * 2 == silen |
| 805 | elif line.startswith("key_data"): |
| 806 | vector["key_data"] = line.split("=")[1].strip() |
| 807 | assert math.ceil(key_data_len / 8) * 2 == len(vector["key_data"]) |
| 808 | vectors.append(vector) |
Alex Gaynor | ace036d | 2015-09-24 20:23:08 -0400 | [diff] [blame] | 809 | vector = {} |
Simo Sorce | 7600dee | 2015-09-22 21:56:20 -0400 | [diff] [blame] | 810 | |
| 811 | return vectors |