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 | 36e651c | 2014-01-27 10:08:35 -0800 | [diff] [blame] | 14 | import collections |
Alex Gaynor | 2b3f942 | 2013-12-24 21:55:24 -0800 | [diff] [blame] | 15 | import os |
| 16 | |
Alex Stapleton | 58f27ac | 2014-02-02 19:30:03 +0000 | [diff] [blame] | 17 | import six |
Alex Gaynor | 2b3f942 | 2013-12-24 21:55:24 -0800 | [diff] [blame] | 18 | import pytest |
| 19 | |
| 20 | |
Alex Gaynor | 36e651c | 2014-01-27 10:08:35 -0800 | [diff] [blame] | 21 | HashVector = collections.namedtuple("HashVector", ["message", "digest"]) |
| 22 | KeyedHashVector = collections.namedtuple( |
| 23 | "KeyedHashVector", ["message", "digest", "key"] |
| 24 | ) |
| 25 | |
| 26 | |
Paul Kehrer | c421e63 | 2014-01-18 09:22:21 -0600 | [diff] [blame] | 27 | def select_backends(names, backend_list): |
| 28 | if names is None: |
| 29 | return backend_list |
| 30 | split_names = [x.strip() for x in names.split(',')] |
| 31 | # this must be duplicated and then removed to preserve the metadata |
| 32 | # 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] | 33 | selected_backends = [] |
| 34 | for backend in backend_list: |
| 35 | if backend.name in split_names: |
| 36 | selected_backends.append(backend) |
Paul Kehrer | c421e63 | 2014-01-18 09:22:21 -0600 | [diff] [blame] | 37 | |
Paul Kehrer | aed9e17 | 2014-01-19 12:09:27 -0600 | [diff] [blame] | 38 | if len(selected_backends) > 0: |
| 39 | return selected_backends |
Paul Kehrer | c421e63 | 2014-01-18 09:22:21 -0600 | [diff] [blame] | 40 | else: |
| 41 | raise ValueError( |
| 42 | "No backend selected. Tried to select: {0}".format(split_names) |
| 43 | ) |
Paul Kehrer | 34c075e | 2014-01-13 21:52:08 -0500 | [diff] [blame] | 44 | |
| 45 | |
Alex Gaynor | 2b3f942 | 2013-12-24 21:55:24 -0800 | [diff] [blame] | 46 | def check_for_iface(name, iface, item): |
| 47 | if name in item.keywords and "backend" in item.funcargs: |
| 48 | if not isinstance(item.funcargs["backend"], iface): |
| 49 | pytest.skip("{0} backend does not support {1}".format( |
| 50 | item.funcargs["backend"], name |
| 51 | )) |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 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: |
| 57 | if not supported.kwargs["only_if"](item.funcargs["backend"]): |
Paul Kehrer | f03334e | 2014-01-02 23:16:14 -0600 | [diff] [blame] | 58 | pytest.skip("{0} ({1})".format( |
| 59 | supported.kwargs["skip_message"], item.funcargs["backend"] |
| 60 | )) |
Paul Kehrer | 5a8fdf8 | 2013-12-26 20:13:45 -0600 | [diff] [blame] | 61 | elif supported: |
Paul Kehrer | ec49550 | 2013-12-27 15:51:40 -0600 | [diff] [blame] | 62 | raise ValueError("This mark is only available on methods that take a " |
| 63 | "backend") |
Paul Kehrer | 5a8fdf8 | 2013-12-26 20:13:45 -0600 | [diff] [blame] | 64 | |
| 65 | |
Paul Kehrer | f7f6a9f | 2013-11-11 20:43:52 -0600 | [diff] [blame] | 66 | def load_vectors_from_file(filename, loader): |
| 67 | base = os.path.join( |
| 68 | os.path.dirname(__file__), "hazmat", "primitives", "vectors", |
| 69 | ) |
| 70 | with open(os.path.join(base, filename), "r") as vector_file: |
| 71 | return loader(vector_file) |
| 72 | |
| 73 | |
Alex Gaynor | d3ce703 | 2013-11-11 14:46:20 -0800 | [diff] [blame] | 74 | def load_nist_vectors(vector_data): |
Paul Kehrer | 749ac5b | 2013-11-18 18:12:41 -0600 | [diff] [blame] | 75 | test_data = None |
| 76 | data = [] |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 77 | |
| 78 | for line in vector_data: |
| 79 | line = line.strip() |
| 80 | |
Paul Kehrer | 749ac5b | 2013-11-18 18:12:41 -0600 | [diff] [blame] | 81 | # Blank lines, comments, and section headers are ignored |
| 82 | if not line or line.startswith("#") or (line.startswith("[") |
| 83 | and line.endswith("]")): |
Alex Gaynor | 521c42d | 2013-11-11 14:25:59 -0800 | [diff] [blame] | 84 | continue |
| 85 | |
Paul Kehrer | a43b669 | 2013-11-12 15:35:49 -0600 | [diff] [blame] | 86 | if line.strip() == "FAIL": |
Paul Kehrer | 749ac5b | 2013-11-18 18:12:41 -0600 | [diff] [blame] | 87 | test_data["fail"] = True |
Paul Kehrer | a43b669 | 2013-11-12 15:35:49 -0600 | [diff] [blame] | 88 | continue |
| 89 | |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 90 | # Build our data using a simple Key = Value format |
Paul Kehrer | a43b669 | 2013-11-12 15:35:49 -0600 | [diff] [blame] | 91 | name, value = [c.strip() for c in line.split("=")] |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 92 | |
Paul Kehrer | 1050ddf | 2014-01-27 21:04:03 -0600 | [diff] [blame] | 93 | # Some tests (PBKDF2) contain \0, which should be interpreted as a |
| 94 | # null character rather than literal. |
| 95 | value = value.replace("\\0", "\0") |
| 96 | |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 97 | # COUNT is a special token that indicates a new block of data |
| 98 | if name.upper() == "COUNT": |
Paul Kehrer | 749ac5b | 2013-11-18 18:12:41 -0600 | [diff] [blame] | 99 | test_data = {} |
| 100 | data.append(test_data) |
| 101 | continue |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 102 | # For all other tokens we simply want the name, value stored in |
| 103 | # the dictionary |
| 104 | else: |
Paul Kehrer | 749ac5b | 2013-11-18 18:12:41 -0600 | [diff] [blame] | 105 | test_data[name.lower()] = value.encode("ascii") |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 106 | |
Paul Kehrer | 749ac5b | 2013-11-18 18:12:41 -0600 | [diff] [blame] | 107 | return data |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 108 | |
| 109 | |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 110 | def load_cryptrec_vectors(vector_data): |
Paul Kehrer | e580598 | 2013-09-27 11:26:01 -0500 | [diff] [blame] | 111 | cryptrec_list = [] |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 112 | |
| 113 | for line in vector_data: |
| 114 | line = line.strip() |
| 115 | |
| 116 | # Blank lines and comments are ignored |
| 117 | if not line or line.startswith("#"): |
| 118 | continue |
| 119 | |
| 120 | if line.startswith("K"): |
Paul Kehrer | e580598 | 2013-09-27 11:26:01 -0500 | [diff] [blame] | 121 | key = line.split(" : ")[1].replace(" ", "").encode("ascii") |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 122 | elif line.startswith("P"): |
Paul Kehrer | e580598 | 2013-09-27 11:26:01 -0500 | [diff] [blame] | 123 | pt = line.split(" : ")[1].replace(" ", "").encode("ascii") |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 124 | elif line.startswith("C"): |
Paul Kehrer | e580598 | 2013-09-27 11:26:01 -0500 | [diff] [blame] | 125 | ct = line.split(" : ")[1].replace(" ", "").encode("ascii") |
| 126 | # after a C is found the K+P+C tuple is complete |
| 127 | # there are many P+C pairs for each K |
Alex Gaynor | 1fe70b1 | 2013-10-16 11:59:17 -0700 | [diff] [blame] | 128 | cryptrec_list.append({ |
| 129 | "key": key, |
| 130 | "plaintext": pt, |
| 131 | "ciphertext": ct |
| 132 | }) |
Donald Stufft | 3359d7e | 2013-10-19 19:33:06 -0400 | [diff] [blame] | 133 | else: |
| 134 | raise ValueError("Invalid line in file '{}'".format(line)) |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 135 | return cryptrec_list |
| 136 | |
| 137 | |
Paul Kehrer | 69e0652 | 2013-10-18 17:28:39 -0500 | [diff] [blame] | 138 | def load_hash_vectors(vector_data): |
| 139 | vectors = [] |
Paul Kehrer | 1bb8b71 | 2013-10-27 17:00:14 -0500 | [diff] [blame] | 140 | key = None |
| 141 | msg = None |
| 142 | md = None |
Paul Kehrer | 69e0652 | 2013-10-18 17:28:39 -0500 | [diff] [blame] | 143 | |
| 144 | for line in vector_data: |
| 145 | line = line.strip() |
| 146 | |
Paul Kehrer | 87cd0db | 2013-10-18 18:01:26 -0500 | [diff] [blame] | 147 | if not line or line.startswith("#") or line.startswith("["): |
Paul Kehrer | 69e0652 | 2013-10-18 17:28:39 -0500 | [diff] [blame] | 148 | continue |
| 149 | |
| 150 | if line.startswith("Len"): |
| 151 | length = int(line.split(" = ")[1]) |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 152 | elif line.startswith("Key"): |
Alex Gaynor | 36e651c | 2014-01-27 10:08:35 -0800 | [diff] [blame] | 153 | # HMAC vectors contain a key attribute. Hash vectors do not. |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 154 | key = line.split(" = ")[1].encode("ascii") |
Paul Kehrer | 69e0652 | 2013-10-18 17:28:39 -0500 | [diff] [blame] | 155 | elif line.startswith("Msg"): |
Alex Gaynor | 36e651c | 2014-01-27 10:08:35 -0800 | [diff] [blame] | 156 | # In the NIST vectors they have chosen to represent an empty |
| 157 | # string as hex 00, which is of course not actually an empty |
| 158 | # string. So we parse the provided length and catch this edge case. |
Paul Kehrer | 69e0652 | 2013-10-18 17:28:39 -0500 | [diff] [blame] | 159 | msg = line.split(" = ")[1].encode("ascii") if length > 0 else b"" |
| 160 | elif line.startswith("MD"): |
| 161 | md = line.split(" = ")[1] |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 162 | # after MD is found the Msg+MD (+ potential key) tuple is complete |
Paul Kehrer | 00dd509 | 2013-10-23 09:41:49 -0500 | [diff] [blame] | 163 | if key is not None: |
Alex Gaynor | 36e651c | 2014-01-27 10:08:35 -0800 | [diff] [blame] | 164 | vectors.append(KeyedHashVector(msg, md, key)) |
Paul Kehrer | 1bb8b71 | 2013-10-27 17:00:14 -0500 | [diff] [blame] | 165 | key = None |
| 166 | msg = None |
| 167 | md = None |
Paul Kehrer | 00dd509 | 2013-10-23 09:41:49 -0500 | [diff] [blame] | 168 | else: |
Alex Gaynor | 36e651c | 2014-01-27 10:08:35 -0800 | [diff] [blame] | 169 | vectors.append(HashVector(msg, md)) |
Paul Kehrer | 1bb8b71 | 2013-10-27 17:00:14 -0500 | [diff] [blame] | 170 | msg = None |
| 171 | md = None |
Paul Kehrer | 69e0652 | 2013-10-18 17:28:39 -0500 | [diff] [blame] | 172 | else: |
| 173 | raise ValueError("Unknown line in hash vector") |
| 174 | return vectors |
Alex Stapleton | 58f27ac | 2014-02-02 19:30:03 +0000 | [diff] [blame] | 175 | |
| 176 | |
| 177 | def load_pkcs1_vectors(vector_data): |
| 178 | """ |
| 179 | Loads data out of RSA PKCS #1 vector files. |
Alex Stapleton | 58f27ac | 2014-02-02 19:30:03 +0000 | [diff] [blame] | 180 | """ |
| 181 | private_key_vector = None |
| 182 | public_key_vector = None |
| 183 | attr = None |
| 184 | key = None |
Paul Kehrer | efca280 | 2014-02-17 20:55:13 -0600 | [diff] [blame] | 185 | example_vector = None |
| 186 | examples = [] |
Alex Stapleton | 58f27ac | 2014-02-02 19:30:03 +0000 | [diff] [blame] | 187 | vectors = [] |
| 188 | for line in vector_data: |
Paul Kehrer | 7774a03 | 2014-02-17 22:56:55 -0600 | [diff] [blame^] | 189 | if ( |
| 190 | line.startswith("# PSS Example") or |
| 191 | line.startswith("# PKCS#1 v1.5 Signature") |
| 192 | ): |
Paul Kehrer | efca280 | 2014-02-17 20:55:13 -0600 | [diff] [blame] | 193 | if example_vector: |
| 194 | for key, value in six.iteritems(example_vector): |
| 195 | hex_str = "".join(value).replace(" ", "") |
| 196 | example_vector[key] = hex_str |
| 197 | examples.append(example_vector) |
| 198 | |
| 199 | attr = None |
| 200 | example_vector = collections.defaultdict(list) |
| 201 | |
| 202 | if line.startswith("# Message to be signed"): |
| 203 | attr = "msg" |
| 204 | continue |
| 205 | elif line.startswith("# Salt"): |
| 206 | attr = "salt" |
| 207 | continue |
| 208 | elif line.startswith("# Signature"): |
| 209 | attr = "signature" |
| 210 | continue |
| 211 | elif ( |
| 212 | example_vector and |
| 213 | line.startswith("# =============================================") |
| 214 | ): |
| 215 | for key, value in six.iteritems(example_vector): |
| 216 | hex_str = "".join(value).replace(" ", "") |
| 217 | example_vector[key] = hex_str |
| 218 | examples.append(example_vector) |
| 219 | example_vector = None |
| 220 | attr = None |
| 221 | elif example_vector and line.startswith("#"): |
| 222 | continue |
| 223 | else: |
| 224 | if attr is not None and example_vector is not None: |
| 225 | example_vector[attr].append(line.strip()) |
| 226 | continue |
| 227 | |
Alex Stapleton | 58f27ac | 2014-02-02 19:30:03 +0000 | [diff] [blame] | 228 | if ( |
| 229 | line.startswith("# Example") or |
| 230 | line.startswith("# =============================================") |
| 231 | ): |
| 232 | if key: |
| 233 | assert private_key_vector |
| 234 | assert public_key_vector |
| 235 | |
| 236 | for key, value in six.iteritems(public_key_vector): |
| 237 | hex_str = "".join(value).replace(" ", "") |
| 238 | public_key_vector[key] = int(hex_str, 16) |
| 239 | |
| 240 | for key, value in six.iteritems(private_key_vector): |
| 241 | hex_str = "".join(value).replace(" ", "") |
| 242 | private_key_vector[key] = int(hex_str, 16) |
| 243 | |
Paul Kehrer | efca280 | 2014-02-17 20:55:13 -0600 | [diff] [blame] | 244 | private_key_vector["examples"] = examples |
| 245 | examples = [] |
| 246 | |
Alex Stapleton | 58f27ac | 2014-02-02 19:30:03 +0000 | [diff] [blame] | 247 | assert ( |
| 248 | private_key_vector['public_exponent'] == |
| 249 | public_key_vector['public_exponent'] |
| 250 | ) |
| 251 | |
| 252 | assert ( |
| 253 | private_key_vector['modulus'] == |
| 254 | public_key_vector['modulus'] |
| 255 | ) |
| 256 | |
| 257 | vectors.append( |
| 258 | (private_key_vector, public_key_vector) |
| 259 | ) |
| 260 | |
| 261 | public_key_vector = collections.defaultdict(list) |
| 262 | private_key_vector = collections.defaultdict(list) |
| 263 | key = None |
| 264 | attr = None |
| 265 | |
| 266 | if private_key_vector is None or public_key_vector is None: |
| 267 | continue |
| 268 | |
| 269 | if line.startswith("# Private key"): |
| 270 | key = private_key_vector |
| 271 | elif line.startswith("# Public key"): |
| 272 | key = public_key_vector |
| 273 | elif line.startswith("# Modulus:"): |
| 274 | attr = "modulus" |
| 275 | elif line.startswith("# Public exponent:"): |
| 276 | attr = "public_exponent" |
| 277 | elif line.startswith("# Exponent:"): |
| 278 | if key is public_key_vector: |
| 279 | attr = "public_exponent" |
| 280 | else: |
| 281 | assert key is private_key_vector |
| 282 | attr = "private_exponent" |
| 283 | elif line.startswith("# Prime 1:"): |
| 284 | attr = "p" |
| 285 | elif line.startswith("# Prime 2:"): |
| 286 | attr = "q" |
Paul Kehrer | 09328bb | 2014-02-12 23:57:27 -0600 | [diff] [blame] | 287 | elif line.startswith("# Prime exponent 1:"): |
| 288 | attr = "dmp1" |
| 289 | elif line.startswith("# Prime exponent 2:"): |
| 290 | attr = "dmq1" |
| 291 | elif line.startswith("# Coefficient:"): |
| 292 | attr = "iqmp" |
Alex Stapleton | 58f27ac | 2014-02-02 19:30:03 +0000 | [diff] [blame] | 293 | elif line.startswith("#"): |
| 294 | attr = None |
| 295 | else: |
| 296 | if key is not None and attr is not None: |
| 297 | key[attr].append(line.strip()) |
| 298 | return vectors |