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 | |
| 17 | import pytest |
| 18 | |
| 19 | |
Alex Gaynor | 36e651c | 2014-01-27 10:08:35 -0800 | [diff] [blame] | 20 | HashVector = collections.namedtuple("HashVector", ["message", "digest"]) |
| 21 | KeyedHashVector = collections.namedtuple( |
| 22 | "KeyedHashVector", ["message", "digest", "key"] |
| 23 | ) |
| 24 | |
| 25 | |
Paul Kehrer | c421e63 | 2014-01-18 09:22:21 -0600 | [diff] [blame] | 26 | def select_backends(names, backend_list): |
| 27 | if names is None: |
| 28 | return backend_list |
| 29 | split_names = [x.strip() for x in names.split(',')] |
| 30 | # this must be duplicated and then removed to preserve the metadata |
| 31 | # 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] | 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 | |
Alex Gaynor | 2b3f942 | 2013-12-24 21:55:24 -0800 | [diff] [blame] | 45 | def check_for_iface(name, iface, item): |
| 46 | if name in item.keywords and "backend" in item.funcargs: |
| 47 | if not isinstance(item.funcargs["backend"], iface): |
| 48 | pytest.skip("{0} backend does not support {1}".format( |
| 49 | item.funcargs["backend"], name |
| 50 | )) |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 51 | |
| 52 | |
Paul Kehrer | 60fc8da | 2013-12-26 20:19:34 -0600 | [diff] [blame] | 53 | def check_backend_support(item): |
Paul Kehrer | 5a8fdf8 | 2013-12-26 20:13:45 -0600 | [diff] [blame] | 54 | supported = item.keywords.get("supported") |
| 55 | if supported and "backend" in item.funcargs: |
| 56 | if not supported.kwargs["only_if"](item.funcargs["backend"]): |
Paul Kehrer | f03334e | 2014-01-02 23:16:14 -0600 | [diff] [blame] | 57 | pytest.skip("{0} ({1})".format( |
| 58 | supported.kwargs["skip_message"], item.funcargs["backend"] |
| 59 | )) |
Paul Kehrer | 5a8fdf8 | 2013-12-26 20:13:45 -0600 | [diff] [blame] | 60 | elif supported: |
Paul Kehrer | ec49550 | 2013-12-27 15:51:40 -0600 | [diff] [blame] | 61 | raise ValueError("This mark is only available on methods that take a " |
| 62 | "backend") |
Paul Kehrer | 5a8fdf8 | 2013-12-26 20:13:45 -0600 | [diff] [blame] | 63 | |
| 64 | |
Paul Kehrer | f7f6a9f | 2013-11-11 20:43:52 -0600 | [diff] [blame] | 65 | def load_vectors_from_file(filename, loader): |
| 66 | base = os.path.join( |
| 67 | os.path.dirname(__file__), "hazmat", "primitives", "vectors", |
| 68 | ) |
| 69 | with open(os.path.join(base, filename), "r") as vector_file: |
| 70 | return loader(vector_file) |
| 71 | |
| 72 | |
Alex Gaynor | d3ce703 | 2013-11-11 14:46:20 -0800 | [diff] [blame] | 73 | def load_nist_vectors(vector_data): |
Paul Kehrer | 749ac5b | 2013-11-18 18:12:41 -0600 | [diff] [blame] | 74 | test_data = None |
| 75 | data = [] |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 76 | |
| 77 | for line in vector_data: |
| 78 | line = line.strip() |
| 79 | |
Paul Kehrer | 749ac5b | 2013-11-18 18:12:41 -0600 | [diff] [blame] | 80 | # Blank lines, comments, and section headers are ignored |
| 81 | if not line or line.startswith("#") or (line.startswith("[") |
| 82 | and line.endswith("]")): |
Alex Gaynor | 521c42d | 2013-11-11 14:25:59 -0800 | [diff] [blame] | 83 | continue |
| 84 | |
Paul Kehrer | a43b669 | 2013-11-12 15:35:49 -0600 | [diff] [blame] | 85 | if line.strip() == "FAIL": |
Paul Kehrer | 749ac5b | 2013-11-18 18:12:41 -0600 | [diff] [blame] | 86 | test_data["fail"] = True |
Paul Kehrer | a43b669 | 2013-11-12 15:35:49 -0600 | [diff] [blame] | 87 | continue |
| 88 | |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 89 | # Build our data using a simple Key = Value format |
Paul Kehrer | a43b669 | 2013-11-12 15:35:49 -0600 | [diff] [blame] | 90 | name, value = [c.strip() for c in line.split("=")] |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 91 | |
Paul Kehrer | 1050ddf | 2014-01-27 21:04:03 -0600 | [diff] [blame^] | 92 | # Some tests (PBKDF2) contain \0, which should be interpreted as a |
| 93 | # null character rather than literal. |
| 94 | value = value.replace("\\0", "\0") |
| 95 | |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 96 | # COUNT is a special token that indicates a new block of data |
| 97 | if name.upper() == "COUNT": |
Paul Kehrer | 749ac5b | 2013-11-18 18:12:41 -0600 | [diff] [blame] | 98 | test_data = {} |
| 99 | data.append(test_data) |
| 100 | continue |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 101 | # For all other tokens we simply want the name, value stored in |
| 102 | # the dictionary |
| 103 | else: |
Paul Kehrer | 749ac5b | 2013-11-18 18:12:41 -0600 | [diff] [blame] | 104 | test_data[name.lower()] = value.encode("ascii") |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 105 | |
Paul Kehrer | 749ac5b | 2013-11-18 18:12:41 -0600 | [diff] [blame] | 106 | return data |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 107 | |
| 108 | |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 109 | def load_cryptrec_vectors(vector_data): |
Paul Kehrer | e580598 | 2013-09-27 11:26:01 -0500 | [diff] [blame] | 110 | cryptrec_list = [] |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 111 | |
| 112 | for line in vector_data: |
| 113 | line = line.strip() |
| 114 | |
| 115 | # Blank lines and comments are ignored |
| 116 | if not line or line.startswith("#"): |
| 117 | continue |
| 118 | |
| 119 | if line.startswith("K"): |
Paul Kehrer | e580598 | 2013-09-27 11:26:01 -0500 | [diff] [blame] | 120 | key = line.split(" : ")[1].replace(" ", "").encode("ascii") |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 121 | elif line.startswith("P"): |
Paul Kehrer | e580598 | 2013-09-27 11:26:01 -0500 | [diff] [blame] | 122 | pt = line.split(" : ")[1].replace(" ", "").encode("ascii") |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 123 | elif line.startswith("C"): |
Paul Kehrer | e580598 | 2013-09-27 11:26:01 -0500 | [diff] [blame] | 124 | ct = line.split(" : ")[1].replace(" ", "").encode("ascii") |
| 125 | # after a C is found the K+P+C tuple is complete |
| 126 | # there are many P+C pairs for each K |
Alex Gaynor | 1fe70b1 | 2013-10-16 11:59:17 -0700 | [diff] [blame] | 127 | cryptrec_list.append({ |
| 128 | "key": key, |
| 129 | "plaintext": pt, |
| 130 | "ciphertext": ct |
| 131 | }) |
Donald Stufft | 3359d7e | 2013-10-19 19:33:06 -0400 | [diff] [blame] | 132 | else: |
| 133 | raise ValueError("Invalid line in file '{}'".format(line)) |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 134 | return cryptrec_list |
| 135 | |
| 136 | |
Paul Kehrer | 6b99a1b | 2013-09-24 16:50:21 -0500 | [diff] [blame] | 137 | def load_openssl_vectors(vector_data): |
| 138 | vectors = [] |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 139 | |
| 140 | for line in vector_data: |
| 141 | line = line.strip() |
| 142 | |
| 143 | # Blank lines and comments are ignored |
| 144 | if not line or line.startswith("#"): |
| 145 | continue |
| 146 | |
| 147 | vector = line.split(":") |
Alex Gaynor | 016eed1 | 2013-10-16 14:16:04 -0700 | [diff] [blame] | 148 | vectors.append({ |
| 149 | "key": vector[1].encode("ascii"), |
| 150 | "iv": vector[2].encode("ascii"), |
| 151 | "plaintext": vector[3].encode("ascii"), |
| 152 | "ciphertext": vector[4].encode("ascii"), |
| 153 | }) |
Paul Kehrer | 6b99a1b | 2013-09-24 16:50:21 -0500 | [diff] [blame] | 154 | return vectors |
Paul Kehrer | 69e0652 | 2013-10-18 17:28:39 -0500 | [diff] [blame] | 155 | |
| 156 | |
| 157 | def load_hash_vectors(vector_data): |
| 158 | vectors = [] |
Paul Kehrer | 1bb8b71 | 2013-10-27 17:00:14 -0500 | [diff] [blame] | 159 | key = None |
| 160 | msg = None |
| 161 | md = None |
Paul Kehrer | 69e0652 | 2013-10-18 17:28:39 -0500 | [diff] [blame] | 162 | |
| 163 | for line in vector_data: |
| 164 | line = line.strip() |
| 165 | |
Paul Kehrer | 87cd0db | 2013-10-18 18:01:26 -0500 | [diff] [blame] | 166 | if not line or line.startswith("#") or line.startswith("["): |
Paul Kehrer | 69e0652 | 2013-10-18 17:28:39 -0500 | [diff] [blame] | 167 | continue |
| 168 | |
| 169 | if line.startswith("Len"): |
| 170 | length = int(line.split(" = ")[1]) |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 171 | elif line.startswith("Key"): |
Alex Gaynor | 36e651c | 2014-01-27 10:08:35 -0800 | [diff] [blame] | 172 | # HMAC vectors contain a key attribute. Hash vectors do not. |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 173 | key = line.split(" = ")[1].encode("ascii") |
Paul Kehrer | 69e0652 | 2013-10-18 17:28:39 -0500 | [diff] [blame] | 174 | elif line.startswith("Msg"): |
Alex Gaynor | 36e651c | 2014-01-27 10:08:35 -0800 | [diff] [blame] | 175 | # In the NIST vectors they have chosen to represent an empty |
| 176 | # string as hex 00, which is of course not actually an empty |
| 177 | # string. So we parse the provided length and catch this edge case. |
Paul Kehrer | 69e0652 | 2013-10-18 17:28:39 -0500 | [diff] [blame] | 178 | msg = line.split(" = ")[1].encode("ascii") if length > 0 else b"" |
| 179 | elif line.startswith("MD"): |
| 180 | md = line.split(" = ")[1] |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 181 | # after MD is found the Msg+MD (+ potential key) tuple is complete |
Paul Kehrer | 00dd509 | 2013-10-23 09:41:49 -0500 | [diff] [blame] | 182 | if key is not None: |
Alex Gaynor | 36e651c | 2014-01-27 10:08:35 -0800 | [diff] [blame] | 183 | vectors.append(KeyedHashVector(msg, md, key)) |
Paul Kehrer | 1bb8b71 | 2013-10-27 17:00:14 -0500 | [diff] [blame] | 184 | key = None |
| 185 | msg = None |
| 186 | md = None |
Paul Kehrer | 00dd509 | 2013-10-23 09:41:49 -0500 | [diff] [blame] | 187 | else: |
Alex Gaynor | 36e651c | 2014-01-27 10:08:35 -0800 | [diff] [blame] | 188 | vectors.append(HashVector(msg, md)) |
Paul Kehrer | 1bb8b71 | 2013-10-27 17:00:14 -0500 | [diff] [blame] | 189 | msg = None |
| 190 | md = None |
Paul Kehrer | 69e0652 | 2013-10-18 17:28:39 -0500 | [diff] [blame] | 191 | else: |
| 192 | raise ValueError("Unknown line in hash vector") |
| 193 | return vectors |