blob: 4640c2ea816d2382506bbfc3b3c46ca4594e6beb [file] [log] [blame]
Alex Gaynorc37feed2014-03-08 08:32:56 -08001# 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
14from __future__ import absolute_import, division, print_function
15
Alex Gaynorbd458ae2013-10-16 11:59:30 -070016import binascii
David Reid5443e9d2014-01-22 17:18:49 -080017import itertools
Paul Kehrerafc1ccd2014-03-19 11:49:32 -040018import os
David Reid5443e9d2014-01-22 17:18:49 -080019
Alex Gaynorbd458ae2013-10-16 11:59:30 -070020import pytest
21
Paul Kehrerafc1ccd2014-03-19 11:49:32 -040022from cryptography.exceptions import (
Paul Kehrer49c8e212014-03-18 07:54:34 -040023 AlreadyFinalized, AlreadyUpdated, InvalidSignature, InvalidTag,
24 NotYetFinalized
Paul Kehrerafc1ccd2014-03-19 11:49:32 -040025)
Paul Kehrer22e80cb2013-11-20 21:27:00 -060026from cryptography.hazmat.primitives import hashes, hmac
Paul Kehrerc85f1792014-03-19 09:45:42 -040027from cryptography.hazmat.primitives.asymmetric import rsa
Paul Kehrer21dde562013-11-06 12:22:09 +080028from cryptography.hazmat.primitives.ciphers import Cipher
Ayrxac1a0792014-05-07 17:02:21 +080029from cryptography.hazmat.primitives.kdf.hkdf import HKDF, HKDFExpand
Paul Kehrerafc1ccd2014-03-19 11:49:32 -040030from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
Alex Gaynorbd458ae2013-10-16 11:59:30 -070031
Paul Kehrerf7f6a9f2013-11-11 20:43:52 -060032from ...utils import load_vectors_from_file
33
Alex Gaynorbd458ae2013-10-16 11:59:30 -070034
Alex Gaynore5c5eec2013-12-13 08:10:20 -080035def _load_all_params(path, file_names, param_loader):
36 all_params = []
37 for file_name in file_names:
38 all_params.extend(
39 load_vectors_from_file(os.path.join(path, file_name), param_loader)
40 )
41 return all_params
42
Alex Gaynor4eec0bb2013-12-13 09:12:19 -080043
Alex Gaynor016eed12013-10-16 14:16:04 -070044def generate_encrypt_test(param_loader, path, file_names, cipher_factory,
Paul Kehrer783479c2013-12-26 21:08:45 -060045 mode_factory):
Alex Gaynore5c5eec2013-12-13 08:10:20 -080046 all_params = _load_all_params(path, file_names, param_loader)
47
48 @pytest.mark.parametrize("params", all_params)
49 def test_encryption(self, backend, params):
Paul Kehrer783479c2013-12-26 21:08:45 -060050 encrypt_test(backend, cipher_factory, mode_factory, params)
Alex Gaynore5c5eec2013-12-13 08:10:20 -080051
Alex Gaynorbd458ae2013-10-16 11:59:30 -070052 return test_encryption
53
54
Paul Kehrer783479c2013-12-26 21:08:45 -060055def encrypt_test(backend, cipher_factory, mode_factory, params):
Paul Kehrera620b7d2013-12-20 22:59:02 -060056 plaintext = params["plaintext"]
57 ciphertext = params["ciphertext"]
Alex Gaynor21919e22013-12-13 08:56:32 -080058 cipher = Cipher(
59 cipher_factory(**params),
60 mode_factory(**params),
61 backend=backend
62 )
63 encryptor = cipher.encryptor()
64 actual_ciphertext = encryptor.update(binascii.unhexlify(plaintext))
65 actual_ciphertext += encryptor.finalize()
66 assert actual_ciphertext == binascii.unhexlify(ciphertext)
67 decryptor = cipher.decryptor()
68 actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext))
69 actual_plaintext += decryptor.finalize()
70 assert actual_plaintext == binascii.unhexlify(plaintext)
71
72
Paul Kehrer22e80cb2013-11-20 21:27:00 -060073def generate_aead_test(param_loader, path, file_names, cipher_factory,
Paul Kehrer783479c2013-12-26 21:08:45 -060074 mode_factory):
Alex Gaynore5c5eec2013-12-13 08:10:20 -080075 all_params = _load_all_params(path, file_names, param_loader)
76
77 @pytest.mark.parametrize("params", all_params)
78 def test_aead(self, backend, params):
Paul Kehrer783479c2013-12-26 21:08:45 -060079 aead_test(backend, cipher_factory, mode_factory, params)
Alex Gaynore5c5eec2013-12-13 08:10:20 -080080
Paul Kehrer22e80cb2013-11-20 21:27:00 -060081 return test_aead
82
83
Paul Kehrer783479c2013-12-26 21:08:45 -060084def aead_test(backend, cipher_factory, mode_factory, params):
Alex Gaynor21919e22013-12-13 08:56:32 -080085 if params.get("pt") is not None:
Paul Kehrera620b7d2013-12-20 22:59:02 -060086 plaintext = params["pt"]
87 ciphertext = params["ct"]
88 aad = params["aad"]
Alex Gaynor21919e22013-12-13 08:56:32 -080089 if params.get("fail") is True:
90 cipher = Cipher(
91 cipher_factory(binascii.unhexlify(params["key"])),
92 mode_factory(binascii.unhexlify(params["iv"]),
Alex Gaynor8f1b8e82014-06-29 20:43:29 -070093 binascii.unhexlify(params["tag"]),
94 len(binascii.unhexlify(params["tag"]))),
Alex Gaynor21919e22013-12-13 08:56:32 -080095 backend
96 )
97 decryptor = cipher.decryptor()
98 decryptor.authenticate_additional_data(binascii.unhexlify(aad))
99 actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext))
100 with pytest.raises(InvalidTag):
101 decryptor.finalize()
102 else:
103 cipher = Cipher(
104 cipher_factory(binascii.unhexlify(params["key"])),
105 mode_factory(binascii.unhexlify(params["iv"]), None),
106 backend
107 )
108 encryptor = cipher.encryptor()
109 encryptor.authenticate_additional_data(binascii.unhexlify(aad))
110 actual_ciphertext = encryptor.update(binascii.unhexlify(plaintext))
111 actual_ciphertext += encryptor.finalize()
Alex Gaynor8f1b8e82014-06-29 20:43:29 -0700112 tag_len = len(binascii.unhexlify(params["tag"]))
113 assert binascii.hexlify(encryptor.tag[:tag_len]) == params["tag"]
Alex Gaynor21919e22013-12-13 08:56:32 -0800114 cipher = Cipher(
115 cipher_factory(binascii.unhexlify(params["key"])),
116 mode_factory(binascii.unhexlify(params["iv"]),
Alex Gaynor8f1b8e82014-06-29 20:43:29 -0700117 binascii.unhexlify(params["tag"]),
118 min_tag_length=tag_len),
Alex Gaynor21919e22013-12-13 08:56:32 -0800119 backend
120 )
121 decryptor = cipher.decryptor()
122 decryptor.authenticate_additional_data(binascii.unhexlify(aad))
123 actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext))
124 actual_plaintext += decryptor.finalize()
125 assert actual_plaintext == binascii.unhexlify(plaintext)
126
127
Paul Kehrer4da28c32013-11-07 07:50:17 +0800128def generate_stream_encryption_test(param_loader, path, file_names,
Paul Kehrer783479c2013-12-26 21:08:45 -0600129 cipher_factory):
Alex Gaynore5c5eec2013-12-13 08:10:20 -0800130 all_params = _load_all_params(path, file_names, param_loader)
131
132 @pytest.mark.parametrize("params", all_params)
133 def test_stream_encryption(self, backend, params):
Paul Kehrer783479c2013-12-26 21:08:45 -0600134 stream_encryption_test(backend, cipher_factory, params)
Paul Kehrer4da28c32013-11-07 07:50:17 +0800135 return test_stream_encryption
136
137
Paul Kehrer783479c2013-12-26 21:08:45 -0600138def stream_encryption_test(backend, cipher_factory, params):
Paul Kehrera620b7d2013-12-20 22:59:02 -0600139 plaintext = params["plaintext"]
140 ciphertext = params["ciphertext"]
141 offset = params["offset"]
Alex Gaynor21919e22013-12-13 08:56:32 -0800142 cipher = Cipher(cipher_factory(**params), None, backend=backend)
143 encryptor = cipher.encryptor()
144 # throw away offset bytes
145 encryptor.update(b"\x00" * int(offset))
146 actual_ciphertext = encryptor.update(binascii.unhexlify(plaintext))
147 actual_ciphertext += encryptor.finalize()
148 assert actual_ciphertext == binascii.unhexlify(ciphertext)
149 decryptor = cipher.decryptor()
150 decryptor.update(b"\x00" * int(offset))
151 actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext))
152 actual_plaintext += decryptor.finalize()
153 assert actual_plaintext == binascii.unhexlify(plaintext)
154
155
Paul Kehrer783479c2013-12-26 21:08:45 -0600156def generate_hash_test(param_loader, path, file_names, hash_cls):
Alex Gaynore5c5eec2013-12-13 08:10:20 -0800157 all_params = _load_all_params(path, file_names, param_loader)
Paul Kehrer4da28c32013-11-07 07:50:17 +0800158
Alex Gaynore5c5eec2013-12-13 08:10:20 -0800159 @pytest.mark.parametrize("params", all_params)
160 def test_hash(self, backend, params):
Paul Kehrer783479c2013-12-26 21:08:45 -0600161 hash_test(backend, hash_cls, params)
Paul Kehrerbde6fb52013-10-18 18:08:49 -0500162 return test_hash
163
164
Paul Kehrer783479c2013-12-26 21:08:45 -0600165def hash_test(backend, algorithm, params):
Alex Gaynor36e651c2014-01-27 10:08:35 -0800166 msg, md = params
Alex Gaynor21919e22013-12-13 08:56:32 -0800167 m = hashes.Hash(algorithm, backend=backend)
168 m.update(binascii.unhexlify(msg))
169 expected_md = md.replace(" ", "").lower().encode("ascii")
170 assert m.finalize() == binascii.unhexlify(expected_md)
171
172
Paul Kehrerb078d8e2013-12-27 16:33:14 -0600173def generate_base_hash_test(algorithm, digest_size, block_size):
174 def test_base_hash(self, backend):
175 base_hash_test(backend, algorithm, digest_size, block_size)
176 return test_base_hash
177
178
Paul Kehrer783479c2013-12-26 21:08:45 -0600179def base_hash_test(backend, algorithm, digest_size, block_size):
Alex Gaynor21919e22013-12-13 08:56:32 -0800180 m = hashes.Hash(algorithm, backend=backend)
181 assert m.algorithm.digest_size == digest_size
182 assert m.algorithm.block_size == block_size
183 m_copy = m.copy()
184 assert m != m_copy
185 assert m._ctx != m_copy._ctx
186
187 m.update(b"abc")
188 copy = m.copy()
189 copy.update(b"123")
190 m.update(b"123")
191 assert copy.finalize() == m.finalize()
192
193
Paul Kehrer783479c2013-12-26 21:08:45 -0600194def generate_long_string_hash_test(hash_factory, md):
Alex Gaynore5c5eec2013-12-13 08:10:20 -0800195 def test_long_string_hash(self, backend):
Paul Kehrer783479c2013-12-26 21:08:45 -0600196 long_string_hash_test(backend, hash_factory, md)
Paul Kehrerc1794072013-10-18 21:42:57 -0500197 return test_long_string_hash
198
199
Paul Kehrer783479c2013-12-26 21:08:45 -0600200def long_string_hash_test(backend, algorithm, md):
Alex Gaynor21919e22013-12-13 08:56:32 -0800201 m = hashes.Hash(algorithm, backend=backend)
202 m.update(b"a" * 1000000)
203 assert m.finalize() == binascii.unhexlify(md.lower().encode("ascii"))
204
205
Paul Kehrerb078d8e2013-12-27 16:33:14 -0600206def generate_base_hmac_test(hash_cls):
207 def test_base_hmac(self, backend):
208 base_hmac_test(backend, hash_cls)
209 return test_base_hmac
210
211
212def base_hmac_test(backend, algorithm):
213 key = b"ab"
214 h = hmac.HMAC(binascii.unhexlify(key), algorithm, backend=backend)
215 h_copy = h.copy()
216 assert h != h_copy
217 assert h._ctx != h_copy._ctx
218
219
Paul Kehrer783479c2013-12-26 21:08:45 -0600220def generate_hmac_test(param_loader, path, file_names, algorithm):
Alex Gaynore5c5eec2013-12-13 08:10:20 -0800221 all_params = _load_all_params(path, file_names, param_loader)
Paul Kehrer0317b042013-10-28 17:34:27 -0500222
Alex Gaynore5c5eec2013-12-13 08:10:20 -0800223 @pytest.mark.parametrize("params", all_params)
224 def test_hmac(self, backend, params):
Paul Kehrer783479c2013-12-26 21:08:45 -0600225 hmac_test(backend, algorithm, params)
Paul Kehrer0317b042013-10-28 17:34:27 -0500226 return test_hmac
227
228
Paul Kehrer783479c2013-12-26 21:08:45 -0600229def hmac_test(backend, algorithm, params):
Alex Gaynor36e651c2014-01-27 10:08:35 -0800230 msg, md, key = params
Alex Gaynor21919e22013-12-13 08:56:32 -0800231 h = hmac.HMAC(binascii.unhexlify(key), algorithm, backend=backend)
232 h.update(binascii.unhexlify(msg))
233 assert h.finalize() == binascii.unhexlify(md.encode("ascii"))
Paul Kehrer0317b042013-10-28 17:34:27 -0500234
Alex Gaynor21919e22013-12-13 08:56:32 -0800235
Paul Kehrer1050ddf2014-01-27 21:04:03 -0600236def generate_pbkdf2_test(param_loader, path, file_names, algorithm):
237 all_params = _load_all_params(path, file_names, param_loader)
238
239 @pytest.mark.parametrize("params", all_params)
240 def test_pbkdf2(self, backend, params):
241 pbkdf2_test(backend, algorithm, params)
242 return test_pbkdf2
243
244
245def pbkdf2_test(backend, algorithm, params):
246 # Password and salt can contain \0, which should be loaded as a null char.
247 # The NIST loader loads them as literal strings so we replace with the
248 # proper value.
Paul Kehrer1277bc72014-01-28 17:09:59 -0600249 kdf = PBKDF2HMAC(
Paul Kehrer1050ddf2014-01-27 21:04:03 -0600250 algorithm,
251 int(params["length"]),
252 params["salt"],
253 int(params["iterations"]),
254 backend
255 )
256 derived_key = kdf.derive(params["password"])
257 assert binascii.hexlify(derived_key) == params["derived_key"]
258
259
Paul Kehrer783479c2013-12-26 21:08:45 -0600260def generate_aead_exception_test(cipher_factory, mode_factory):
Alex Gaynor21919e22013-12-13 08:56:32 -0800261 def test_aead_exception(self, backend):
Paul Kehrer783479c2013-12-26 21:08:45 -0600262 aead_exception_test(backend, cipher_factory, mode_factory)
Paul Kehrerce9c6112013-11-22 14:10:59 -0600263 return test_aead_exception
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600264
265
Paul Kehrer783479c2013-12-26 21:08:45 -0600266def aead_exception_test(backend, cipher_factory, mode_factory):
Alex Gaynor21919e22013-12-13 08:56:32 -0800267 cipher = Cipher(
268 cipher_factory(binascii.unhexlify(b"0" * 32)),
269 mode_factory(binascii.unhexlify(b"0" * 24)),
270 backend
271 )
272 encryptor = cipher.encryptor()
273 encryptor.update(b"a" * 16)
274 with pytest.raises(NotYetFinalized):
275 encryptor.tag
276 with pytest.raises(AlreadyUpdated):
277 encryptor.authenticate_additional_data(b"b" * 16)
278 encryptor.finalize()
279 with pytest.raises(AlreadyFinalized):
280 encryptor.authenticate_additional_data(b"b" * 16)
281 with pytest.raises(AlreadyFinalized):
282 encryptor.update(b"b" * 16)
283 with pytest.raises(AlreadyFinalized):
284 encryptor.finalize()
285 cipher = Cipher(
286 cipher_factory(binascii.unhexlify(b"0" * 32)),
287 mode_factory(binascii.unhexlify(b"0" * 24), b"0" * 16),
288 backend
289 )
290 decryptor = cipher.decryptor()
291 decryptor.update(b"a" * 16)
292 with pytest.raises(AttributeError):
293 decryptor.tag
Paul Kehrerb91221d2013-12-04 17:56:40 -0600294
Alex Gaynor21919e22013-12-13 08:56:32 -0800295
Paul Kehrer783479c2013-12-26 21:08:45 -0600296def generate_aead_tag_exception_test(cipher_factory, mode_factory):
Alex Gaynor21919e22013-12-13 08:56:32 -0800297 def test_aead_tag_exception(self, backend):
Paul Kehrer783479c2013-12-26 21:08:45 -0600298 aead_tag_exception_test(backend, cipher_factory, mode_factory)
Paul Kehrerb91221d2013-12-04 17:56:40 -0600299 return test_aead_tag_exception
Alex Gaynor21919e22013-12-13 08:56:32 -0800300
301
Paul Kehrer783479c2013-12-26 21:08:45 -0600302def aead_tag_exception_test(backend, cipher_factory, mode_factory):
Alex Gaynor21919e22013-12-13 08:56:32 -0800303 cipher = Cipher(
304 cipher_factory(binascii.unhexlify(b"0" * 32)),
305 mode_factory(binascii.unhexlify(b"0" * 24)),
306 backend
307 )
308 with pytest.raises(ValueError):
309 cipher.decryptor()
Alex Gaynor516b1ad2014-01-01 12:28:37 -0800310
Paul Kehrerf7b4ede2013-12-21 17:25:19 -0600311 with pytest.raises(ValueError):
Alex Gaynor516b1ad2014-01-01 12:28:37 -0800312 mode_factory(binascii.unhexlify(b"0" * 24), b"000")
313
Paul Kehrerf7b4ede2013-12-21 17:25:19 -0600314 cipher = Cipher(
315 cipher_factory(binascii.unhexlify(b"0" * 32)),
Alex Gaynor21919e22013-12-13 08:56:32 -0800316 mode_factory(binascii.unhexlify(b"0" * 24), b"0" * 16),
317 backend
318 )
319 with pytest.raises(ValueError):
320 cipher.encryptor()
David Reid66c9cd92014-01-20 16:05:53 -0800321
322
David Reid5443e9d2014-01-22 17:18:49 -0800323def hkdf_derive_test(backend, algorithm, params):
David Reid0d492db2014-01-27 17:05:49 -0800324 hkdf = HKDF(
David Reid66c9cd92014-01-20 16:05:53 -0800325 algorithm,
David Reid0d492db2014-01-27 17:05:49 -0800326 int(params["l"]),
327 salt=binascii.unhexlify(params["salt"]) or None,
328 info=binascii.unhexlify(params["info"]) or None,
David Reid66c9cd92014-01-20 16:05:53 -0800329 backend=backend
330 )
331
David Reid0d492db2014-01-27 17:05:49 -0800332 okm = hkdf.derive(binascii.unhexlify(params["ikm"]))
333
David Reid14367302014-01-27 16:33:31 -0800334 assert okm == binascii.unhexlify(params["okm"])
David Reid66c9cd92014-01-20 16:05:53 -0800335
336
David Reid5443e9d2014-01-22 17:18:49 -0800337def hkdf_extract_test(backend, algorithm, params):
David Reid0d492db2014-01-27 17:05:49 -0800338 hkdf = HKDF(
David Reid5443e9d2014-01-22 17:18:49 -0800339 algorithm,
David Reid0d492db2014-01-27 17:05:49 -0800340 int(params["l"]),
341 salt=binascii.unhexlify(params["salt"]) or None,
342 info=binascii.unhexlify(params["info"]) or None,
David Reid5443e9d2014-01-22 17:18:49 -0800343 backend=backend
344 )
345
David Reid15fd6432014-01-30 15:28:09 -0800346 prk = hkdf._extract(binascii.unhexlify(params["ikm"]))
David Reid0d492db2014-01-27 17:05:49 -0800347
David Reid14367302014-01-27 16:33:31 -0800348 assert prk == binascii.unhexlify(params["prk"])
David Reid5443e9d2014-01-22 17:18:49 -0800349
350
351def hkdf_expand_test(backend, algorithm, params):
Ayrxac1a0792014-05-07 17:02:21 +0800352 hkdf = HKDFExpand(
David Reid5443e9d2014-01-22 17:18:49 -0800353 algorithm,
David Reid14367302014-01-27 16:33:31 -0800354 int(params["l"]),
David Reid0d492db2014-01-27 17:05:49 -0800355 info=binascii.unhexlify(params["info"]) or None,
David Reid5443e9d2014-01-22 17:18:49 -0800356 backend=backend
357 )
358
Ayrx2d6cd442014-05-09 14:48:06 +0800359 okm = hkdf.derive(binascii.unhexlify(params["prk"]))
David Reid0d492db2014-01-27 17:05:49 -0800360
David Reid14367302014-01-27 16:33:31 -0800361 assert okm == binascii.unhexlify(params["okm"])
David Reid5443e9d2014-01-22 17:18:49 -0800362
363
David Reid66c9cd92014-01-20 16:05:53 -0800364def generate_hkdf_test(param_loader, path, file_names, algorithm):
365 all_params = _load_all_params(path, file_names, param_loader)
366
David Reid5443e9d2014-01-22 17:18:49 -0800367 all_tests = [hkdf_extract_test, hkdf_expand_test, hkdf_derive_test]
368
369 @pytest.mark.parametrize(
370 ("params", "hkdf_test"),
371 itertools.product(all_params, all_tests)
372 )
373 def test_hkdf(self, backend, params, hkdf_test):
David Reid66c9cd92014-01-20 16:05:53 -0800374 hkdf_test(backend, algorithm, params)
375
376 return test_hkdf
Paul Kehrerb5936a72014-03-13 21:03:00 -0400377
378
Paul Kehrerf29c3c52014-03-19 09:35:40 -0400379def generate_rsa_verification_test(param_loader, path, file_names, hash_alg,
Paul Kehrerc85f1792014-03-19 09:45:42 -0400380 pad_factory):
Paul Kehrerb5936a72014-03-13 21:03:00 -0400381 all_params = _load_all_params(path, file_names, param_loader)
Paul Kehrer1cfdca22014-03-16 17:57:43 -0400382 all_params = [i for i in all_params
383 if i["algorithm"] == hash_alg.name.upper()]
Paul Kehrerb5936a72014-03-13 21:03:00 -0400384
385 @pytest.mark.parametrize("params", all_params)
Paul Kehrerf29c3c52014-03-19 09:35:40 -0400386 def test_rsa_verification(self, backend, params):
Paul Kehrerc85f1792014-03-19 09:45:42 -0400387 rsa_verification_test(backend, params, hash_alg, pad_factory)
Paul Kehrerb5936a72014-03-13 21:03:00 -0400388
Paul Kehrerf29c3c52014-03-19 09:35:40 -0400389 return test_rsa_verification
Paul Kehrerb5936a72014-03-13 21:03:00 -0400390
391
Paul Kehrerc85f1792014-03-19 09:45:42 -0400392def rsa_verification_test(backend, params, hash_alg, pad_factory):
Paul Kehrer6af3c6e2014-06-06 21:05:23 -0500393 public_numbers = rsa.RSAPublicNumbers(
394 e=params["public_exponent"],
395 n=params["modulus"]
Paul Kehrer762014e2014-03-16 17:47:22 -0400396 )
Paul Kehrer144a4152014-06-20 11:52:37 -0600397 public_key = public_numbers.public_key(backend)
Paul Kehrerc85f1792014-03-19 09:45:42 -0400398 pad = pad_factory(params, hash_alg)
Paul Kehrer49c8e212014-03-18 07:54:34 -0400399 verifier = public_key.verifier(
400 binascii.unhexlify(params["s"]),
401 pad,
Paul Kehrer6af3c6e2014-06-06 21:05:23 -0500402 hash_alg
Paul Kehrer762014e2014-03-16 17:47:22 -0400403 )
404 verifier.update(binascii.unhexlify(params["msg"]))
Paul Kehrer49c8e212014-03-18 07:54:34 -0400405 if params["fail"]:
406 with pytest.raises(InvalidSignature):
407 verifier.verify()
408 else:
409 verifier.verify()
Alex Stapleton458c09b2014-04-23 20:58:37 +0100410
411
Paul Kehrer6af3c6e2014-06-06 21:05:23 -0500412def _check_rsa_private_numbers(skey):
Alex Stapleton458c09b2014-04-23 20:58:37 +0100413 assert skey
Paul Kehrer6af3c6e2014-06-06 21:05:23 -0500414 pkey = skey.public_numbers
415 assert pkey
416 assert pkey.e
417 assert pkey.n
418 assert skey.d
419 assert skey.p * skey.q == pkey.n
Alex Stapleton458c09b2014-04-23 20:58:37 +0100420 assert skey.dmp1 == rsa.rsa_crt_dmp1(skey.d, skey.p)
421 assert skey.dmq1 == rsa.rsa_crt_dmq1(skey.d, skey.q)
422 assert skey.iqmp == rsa.rsa_crt_iqmp(skey.p, skey.q)