blob: 693a0c8f443a48d459fa7aa1bb5db752e5a9ea1d [file] [log] [blame]
Alex Gaynorf312a5c2013-08-10 15:23:38 -04001# 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 Gaynor2b3f9422013-12-24 21:55:24 -080014import os
15
16import pytest
17
18
19def check_for_iface(name, iface, item):
20 if name in item.keywords and "backend" in item.funcargs:
21 if not isinstance(item.funcargs["backend"], iface):
22 pytest.skip("{0} backend does not support {1}".format(
23 item.funcargs["backend"], name
24 ))
Donald Stufft9e1a48b2013-08-09 00:32:30 -040025
26
Paul Kehrer60fc8da2013-12-26 20:19:34 -060027def check_backend_support(item):
Paul Kehrer5a8fdf82013-12-26 20:13:45 -060028 supported = item.keywords.get("supported")
29 if supported and "backend" in item.funcargs:
30 if not supported.kwargs["only_if"](item.funcargs["backend"]):
Paul Kehrerf03334e2014-01-02 23:16:14 -060031 pytest.skip("{0} ({1})".format(
32 supported.kwargs["skip_message"], item.funcargs["backend"]
33 ))
Paul Kehrer5a8fdf82013-12-26 20:13:45 -060034 elif supported:
Paul Kehrerec495502013-12-27 15:51:40 -060035 raise ValueError("This mark is only available on methods that take a "
36 "backend")
Paul Kehrer5a8fdf82013-12-26 20:13:45 -060037
38
Paul Kehrerf7f6a9f2013-11-11 20:43:52 -060039def load_vectors_from_file(filename, loader):
40 base = os.path.join(
41 os.path.dirname(__file__), "hazmat", "primitives", "vectors",
42 )
43 with open(os.path.join(base, filename), "r") as vector_file:
44 return loader(vector_file)
45
46
Alex Gaynord3ce7032013-11-11 14:46:20 -080047def load_nist_vectors(vector_data):
Paul Kehrer749ac5b2013-11-18 18:12:41 -060048 test_data = None
49 data = []
Donald Stufft9e1a48b2013-08-09 00:32:30 -040050
51 for line in vector_data:
52 line = line.strip()
53
Paul Kehrer749ac5b2013-11-18 18:12:41 -060054 # Blank lines, comments, and section headers are ignored
55 if not line or line.startswith("#") or (line.startswith("[")
56 and line.endswith("]")):
Alex Gaynor521c42d2013-11-11 14:25:59 -080057 continue
58
Paul Kehrera43b6692013-11-12 15:35:49 -060059 if line.strip() == "FAIL":
Paul Kehrer749ac5b2013-11-18 18:12:41 -060060 test_data["fail"] = True
Paul Kehrera43b6692013-11-12 15:35:49 -060061 continue
62
Donald Stufft9e1a48b2013-08-09 00:32:30 -040063 # Build our data using a simple Key = Value format
Paul Kehrera43b6692013-11-12 15:35:49 -060064 name, value = [c.strip() for c in line.split("=")]
Donald Stufft9e1a48b2013-08-09 00:32:30 -040065
66 # COUNT is a special token that indicates a new block of data
67 if name.upper() == "COUNT":
Paul Kehrer749ac5b2013-11-18 18:12:41 -060068 test_data = {}
69 data.append(test_data)
70 continue
Donald Stufft9e1a48b2013-08-09 00:32:30 -040071 # For all other tokens we simply want the name, value stored in
72 # the dictionary
73 else:
Paul Kehrer749ac5b2013-11-18 18:12:41 -060074 test_data[name.lower()] = value.encode("ascii")
Donald Stufft9e1a48b2013-08-09 00:32:30 -040075
Paul Kehrer749ac5b2013-11-18 18:12:41 -060076 return data
Donald Stufft9e1a48b2013-08-09 00:32:30 -040077
78
Paul Kehrer1951bf62013-09-15 12:05:43 -050079def load_cryptrec_vectors(vector_data):
Paul Kehrere5805982013-09-27 11:26:01 -050080 cryptrec_list = []
Paul Kehrer1951bf62013-09-15 12:05:43 -050081
82 for line in vector_data:
83 line = line.strip()
84
85 # Blank lines and comments are ignored
86 if not line or line.startswith("#"):
87 continue
88
89 if line.startswith("K"):
Paul Kehrere5805982013-09-27 11:26:01 -050090 key = line.split(" : ")[1].replace(" ", "").encode("ascii")
Paul Kehrer1951bf62013-09-15 12:05:43 -050091 elif line.startswith("P"):
Paul Kehrere5805982013-09-27 11:26:01 -050092 pt = line.split(" : ")[1].replace(" ", "").encode("ascii")
Paul Kehrer1951bf62013-09-15 12:05:43 -050093 elif line.startswith("C"):
Paul Kehrere5805982013-09-27 11:26:01 -050094 ct = line.split(" : ")[1].replace(" ", "").encode("ascii")
95 # after a C is found the K+P+C tuple is complete
96 # there are many P+C pairs for each K
Alex Gaynor1fe70b12013-10-16 11:59:17 -070097 cryptrec_list.append({
98 "key": key,
99 "plaintext": pt,
100 "ciphertext": ct
101 })
Donald Stufft3359d7e2013-10-19 19:33:06 -0400102 else:
103 raise ValueError("Invalid line in file '{}'".format(line))
Paul Kehrer1951bf62013-09-15 12:05:43 -0500104 return cryptrec_list
105
106
Paul Kehrer6b99a1b2013-09-24 16:50:21 -0500107def load_openssl_vectors(vector_data):
108 vectors = []
Paul Kehrer1951bf62013-09-15 12:05:43 -0500109
110 for line in vector_data:
111 line = line.strip()
112
113 # Blank lines and comments are ignored
114 if not line or line.startswith("#"):
115 continue
116
117 vector = line.split(":")
Alex Gaynor016eed12013-10-16 14:16:04 -0700118 vectors.append({
119 "key": vector[1].encode("ascii"),
120 "iv": vector[2].encode("ascii"),
121 "plaintext": vector[3].encode("ascii"),
122 "ciphertext": vector[4].encode("ascii"),
123 })
Paul Kehrer6b99a1b2013-09-24 16:50:21 -0500124 return vectors
Paul Kehrer69e06522013-10-18 17:28:39 -0500125
126
127def load_hash_vectors(vector_data):
128 vectors = []
Paul Kehrer1bb8b712013-10-27 17:00:14 -0500129 key = None
130 msg = None
131 md = None
Paul Kehrer69e06522013-10-18 17:28:39 -0500132
133 for line in vector_data:
134 line = line.strip()
135
Paul Kehrer87cd0db2013-10-18 18:01:26 -0500136 if not line or line.startswith("#") or line.startswith("["):
Paul Kehrer69e06522013-10-18 17:28:39 -0500137 continue
138
139 if line.startswith("Len"):
140 length = int(line.split(" = ")[1])
Paul Kehrer0317b042013-10-28 17:34:27 -0500141 elif line.startswith("Key"):
142 """
143 HMAC vectors contain a key attribute. Hash vectors do not.
144 """
145 key = line.split(" = ")[1].encode("ascii")
Paul Kehrer69e06522013-10-18 17:28:39 -0500146 elif line.startswith("Msg"):
147 """
148 In the NIST vectors they have chosen to represent an empty
149 string as hex 00, which is of course not actually an empty
150 string. So we parse the provided length and catch this edge case.
151 """
152 msg = line.split(" = ")[1].encode("ascii") if length > 0 else b""
153 elif line.startswith("MD"):
154 md = line.split(" = ")[1]
Paul Kehrer0317b042013-10-28 17:34:27 -0500155 # after MD is found the Msg+MD (+ potential key) tuple is complete
Paul Kehrer00dd5092013-10-23 09:41:49 -0500156 if key is not None:
Paul Kehrer0317b042013-10-28 17:34:27 -0500157 vectors.append((msg, md, key))
Paul Kehrer1bb8b712013-10-27 17:00:14 -0500158 key = None
159 msg = None
160 md = None
Paul Kehrer00dd5092013-10-23 09:41:49 -0500161 else:
Paul Kehrer0317b042013-10-28 17:34:27 -0500162 vectors.append((msg, md))
Paul Kehrer1bb8b712013-10-27 17:00:14 -0500163 msg = None
164 md = None
Paul Kehrer69e06522013-10-18 17:28:39 -0500165 else:
166 raise ValueError("Unknown line in hash vector")
167 return vectors