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 | |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 14 | import os.path |
| 15 | |
| 16 | |
Paul Kehrer | f7f6a9f | 2013-11-11 20:43:52 -0600 | [diff] [blame] | 17 | def load_vectors_from_file(filename, loader): |
| 18 | base = os.path.join( |
| 19 | os.path.dirname(__file__), "hazmat", "primitives", "vectors", |
| 20 | ) |
| 21 | with open(os.path.join(base, filename), "r") as vector_file: |
| 22 | return loader(vector_file) |
| 23 | |
| 24 | |
Alex Gaynor | d3ce703 | 2013-11-11 14:46:20 -0800 | [diff] [blame] | 25 | def load_nist_vectors(vector_data): |
Alex Gaynor | 521c42d | 2013-11-11 14:25:59 -0800 | [diff] [blame] | 26 | section = None |
| 27 | count = None |
| 28 | data = {} |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 29 | |
| 30 | for line in vector_data: |
| 31 | line = line.strip() |
| 32 | |
| 33 | # Blank lines are ignored |
| 34 | if not line: |
| 35 | continue |
| 36 | |
| 37 | # Lines starting with # are comments |
| 38 | if line.startswith("#"): |
| 39 | continue |
| 40 | |
| 41 | # Look for section headers |
| 42 | if line.startswith("[") and line.endswith("]"): |
| 43 | section = line[1:-1] |
Alex Gaynor | 521c42d | 2013-11-11 14:25:59 -0800 | [diff] [blame] | 44 | continue |
| 45 | |
Paul Kehrer | a43b669 | 2013-11-12 15:35:49 -0600 | [diff] [blame] | 46 | if line.strip() == "FAIL": |
| 47 | data[section, count]["fail"] = True |
| 48 | continue |
| 49 | |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 50 | # Build our data using a simple Key = Value format |
Paul Kehrer | a43b669 | 2013-11-12 15:35:49 -0600 | [diff] [blame] | 51 | name, value = [c.strip() for c in line.split("=")] |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 52 | |
| 53 | # COUNT is a special token that indicates a new block of data |
| 54 | if name.upper() == "COUNT": |
| 55 | count = value |
Alex Gaynor | d3ce703 | 2013-11-11 14:46:20 -0800 | [diff] [blame] | 56 | data[section, count] = {} |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 57 | # For all other tokens we simply want the name, value stored in |
| 58 | # the dictionary |
| 59 | else: |
Alex Gaynor | d3ce703 | 2013-11-11 14:46:20 -0800 | [diff] [blame] | 60 | data[section, count][name.lower()] = value.encode("ascii") |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 61 | |
Alex Gaynor | 1fe70b1 | 2013-10-16 11:59:17 -0700 | [diff] [blame] | 62 | # We want to test only for a particular operation, we sort them for the |
| 63 | # benefit of the tests of this function. |
Alex Gaynor | 521c42d | 2013-11-11 14:25:59 -0800 | [diff] [blame] | 64 | return [v for k, v in sorted(data.items(), key=lambda kv: kv[0])] |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 65 | |
| 66 | |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 67 | def load_cryptrec_vectors(vector_data): |
Paul Kehrer | e580598 | 2013-09-27 11:26:01 -0500 | [diff] [blame] | 68 | cryptrec_list = [] |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 69 | |
| 70 | for line in vector_data: |
| 71 | line = line.strip() |
| 72 | |
| 73 | # Blank lines and comments are ignored |
| 74 | if not line or line.startswith("#"): |
| 75 | continue |
| 76 | |
| 77 | if line.startswith("K"): |
Paul Kehrer | e580598 | 2013-09-27 11:26:01 -0500 | [diff] [blame] | 78 | key = line.split(" : ")[1].replace(" ", "").encode("ascii") |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 79 | elif line.startswith("P"): |
Paul Kehrer | e580598 | 2013-09-27 11:26:01 -0500 | [diff] [blame] | 80 | pt = line.split(" : ")[1].replace(" ", "").encode("ascii") |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 81 | elif line.startswith("C"): |
Paul Kehrer | e580598 | 2013-09-27 11:26:01 -0500 | [diff] [blame] | 82 | ct = line.split(" : ")[1].replace(" ", "").encode("ascii") |
| 83 | # after a C is found the K+P+C tuple is complete |
| 84 | # there are many P+C pairs for each K |
Alex Gaynor | 1fe70b1 | 2013-10-16 11:59:17 -0700 | [diff] [blame] | 85 | cryptrec_list.append({ |
| 86 | "key": key, |
| 87 | "plaintext": pt, |
| 88 | "ciphertext": ct |
| 89 | }) |
Donald Stufft | 3359d7e | 2013-10-19 19:33:06 -0400 | [diff] [blame] | 90 | else: |
| 91 | raise ValueError("Invalid line in file '{}'".format(line)) |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 92 | return cryptrec_list |
| 93 | |
| 94 | |
Paul Kehrer | 6b99a1b | 2013-09-24 16:50:21 -0500 | [diff] [blame] | 95 | def load_openssl_vectors(vector_data): |
| 96 | vectors = [] |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 97 | |
| 98 | for line in vector_data: |
| 99 | line = line.strip() |
| 100 | |
| 101 | # Blank lines and comments are ignored |
| 102 | if not line or line.startswith("#"): |
| 103 | continue |
| 104 | |
| 105 | vector = line.split(":") |
Alex Gaynor | 016eed1 | 2013-10-16 14:16:04 -0700 | [diff] [blame] | 106 | vectors.append({ |
| 107 | "key": vector[1].encode("ascii"), |
| 108 | "iv": vector[2].encode("ascii"), |
| 109 | "plaintext": vector[3].encode("ascii"), |
| 110 | "ciphertext": vector[4].encode("ascii"), |
| 111 | }) |
Paul Kehrer | 6b99a1b | 2013-09-24 16:50:21 -0500 | [diff] [blame] | 112 | return vectors |
Paul Kehrer | 69e0652 | 2013-10-18 17:28:39 -0500 | [diff] [blame] | 113 | |
| 114 | |
| 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"): |
| 130 | """ |
| 131 | HMAC vectors contain a key attribute. Hash vectors do not. |
| 132 | """ |
| 133 | key = line.split(" = ")[1].encode("ascii") |
Paul Kehrer | 69e0652 | 2013-10-18 17:28:39 -0500 | [diff] [blame] | 134 | elif line.startswith("Msg"): |
| 135 | """ |
| 136 | In the NIST vectors they have chosen to represent an empty |
| 137 | string as hex 00, which is of course not actually an empty |
| 138 | string. So we parse the provided length and catch this edge case. |
| 139 | """ |
| 140 | msg = line.split(" = ")[1].encode("ascii") if length > 0 else b"" |
| 141 | elif line.startswith("MD"): |
| 142 | md = line.split(" = ")[1] |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 143 | # after MD is found the Msg+MD (+ potential key) tuple is complete |
Paul Kehrer | 00dd509 | 2013-10-23 09:41:49 -0500 | [diff] [blame] | 144 | if key is not None: |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 145 | vectors.append((msg, md, key)) |
Paul Kehrer | 1bb8b71 | 2013-10-27 17:00:14 -0500 | [diff] [blame] | 146 | key = None |
| 147 | msg = None |
| 148 | md = None |
Paul Kehrer | 00dd509 | 2013-10-23 09:41:49 -0500 | [diff] [blame] | 149 | else: |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 150 | vectors.append((msg, md)) |
Paul Kehrer | 1bb8b71 | 2013-10-27 17:00:14 -0500 | [diff] [blame] | 151 | msg = None |
| 152 | md = None |
Paul Kehrer | 69e0652 | 2013-10-18 17:28:39 -0500 | [diff] [blame] | 153 | else: |
| 154 | raise ValueError("Unknown line in hash vector") |
| 155 | return vectors |