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 | |
| 17 | def load_nist_vectors(vector_data, op, fields): |
| 18 | section, count, data = None, None, {} |
| 19 | |
| 20 | for line in vector_data: |
| 21 | line = line.strip() |
| 22 | |
| 23 | # Blank lines are ignored |
| 24 | if not line: |
| 25 | continue |
| 26 | |
| 27 | # Lines starting with # are comments |
| 28 | if line.startswith("#"): |
| 29 | continue |
| 30 | |
| 31 | # Look for section headers |
| 32 | if line.startswith("[") and line.endswith("]"): |
| 33 | section = line[1:-1] |
| 34 | data[section] = {} |
| 35 | continue |
| 36 | |
| 37 | # Build our data using a simple Key = Value format |
| 38 | name, value = line.split(" = ") |
| 39 | |
| 40 | # COUNT is a special token that indicates a new block of data |
| 41 | if name.upper() == "COUNT": |
| 42 | count = value |
| 43 | data[section][count] = {} |
| 44 | # For all other tokens we simply want the name, value stored in |
| 45 | # the dictionary |
| 46 | else: |
| 47 | data[section][count][name.lower()] = value |
| 48 | |
| 49 | # We want to test only for a particular operation |
| 50 | return [ |
Alex Gaynor | f312a5c | 2013-08-10 15:23:38 -0400 | [diff] [blame] | 51 | tuple(vector[1][f].encode("ascii") for f in fields) |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 52 | for vector in sorted(data[op].items(), key=lambda v: v[0]) |
| 53 | ] |
| 54 | |
| 55 | |
| 56 | def load_nist_vectors_from_file(filename, op, fields): |
| 57 | base = os.path.join( |
| 58 | os.path.dirname(__file__), "primitives", "vectors", "NIST", |
| 59 | ) |
| 60 | with open(os.path.join(base, filename), "r") as vector_file: |
| 61 | return load_nist_vectors(vector_file, op, fields) |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 62 | |
| 63 | |
| 64 | def load_cryptrec_vectors_from_file(filename): |
| 65 | base = os.path.join( |
| 66 | os.path.dirname(__file__), "primitives", "vectors", "CRYPTREC", |
| 67 | ) |
| 68 | with open(os.path.join(base, filename), "r") as vector_file: |
| 69 | return load_cryptrec_vectors(vector_file) |
| 70 | |
| 71 | |
| 72 | def load_cryptrec_vectors(vector_data): |
Paul Kehrer | e580598 | 2013-09-27 11:26:01 -0500 | [diff] [blame] | 73 | cryptrec_list = [] |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 74 | |
| 75 | for line in vector_data: |
| 76 | line = line.strip() |
| 77 | |
| 78 | # Blank lines and comments are ignored |
| 79 | if not line or line.startswith("#"): |
| 80 | continue |
| 81 | |
| 82 | if line.startswith("K"): |
Paul Kehrer | e580598 | 2013-09-27 11:26:01 -0500 | [diff] [blame] | 83 | key = line.split(" : ")[1].replace(" ", "").encode("ascii") |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 84 | elif line.startswith("P"): |
Paul Kehrer | e580598 | 2013-09-27 11:26:01 -0500 | [diff] [blame] | 85 | pt = line.split(" : ")[1].replace(" ", "").encode("ascii") |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 86 | elif line.startswith("C"): |
Paul Kehrer | e580598 | 2013-09-27 11:26:01 -0500 | [diff] [blame] | 87 | ct = line.split(" : ")[1].replace(" ", "").encode("ascii") |
| 88 | # after a C is found the K+P+C tuple is complete |
| 89 | # there are many P+C pairs for each K |
| 90 | cryptrec_list.append((key, pt, ct)) |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 91 | return cryptrec_list |
| 92 | |
| 93 | |
Paul Kehrer | 6b99a1b | 2013-09-24 16:50:21 -0500 | [diff] [blame] | 94 | def load_openssl_vectors_from_file(filename): |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 95 | base = os.path.join( |
| 96 | os.path.dirname(__file__), "primitives", "vectors", "OpenSSL", |
| 97 | ) |
| 98 | with open(os.path.join(base, filename), "r") as vector_file: |
Paul Kehrer | 6b99a1b | 2013-09-24 16:50:21 -0500 | [diff] [blame] | 99 | return load_openssl_vectors(vector_file) |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 100 | |
| 101 | |
Paul Kehrer | 6b99a1b | 2013-09-24 16:50:21 -0500 | [diff] [blame] | 102 | def load_openssl_vectors(vector_data): |
| 103 | vectors = [] |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 104 | |
| 105 | for line in vector_data: |
| 106 | line = line.strip() |
| 107 | |
| 108 | # Blank lines and comments are ignored |
| 109 | if not line or line.startswith("#"): |
| 110 | continue |
| 111 | |
| 112 | vector = line.split(":") |
Paul Kehrer | e580598 | 2013-09-27 11:26:01 -0500 | [diff] [blame] | 113 | params = ( |
| 114 | # key |
| 115 | vector[1].encode("ascii"), |
| 116 | # iv |
| 117 | vector[2].encode("ascii"), |
| 118 | # plaintext |
| 119 | vector[3].encode("ascii"), |
| 120 | # ciphertext |
| 121 | vector[4].encode("ascii") |
| 122 | ) |
Paul Kehrer | 6b99a1b | 2013-09-24 16:50:21 -0500 | [diff] [blame] | 123 | vectors.append(params) |
| 124 | return vectors |