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