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