blob: 0c9af504f04f9926a07aee79d2e49381bcc99f5f [file] [log] [blame]
Alex Gaynor5951f462014-11-16 09:08:42 -08001# This file is dual licensed under the terms of the Apache License, Version
2# 2.0, and the BSD License. See the LICENSE file in the root of this repository
3# for complete details.
Alex Gaynorc37feed2014-03-08 08:32:56 -08004
5from __future__ import absolute_import, division, print_function
6
Alex Gaynorbd458ae2013-10-16 11:59:30 -07007import binascii
David Reid5443e9d2014-01-22 17:18:49 -08008import itertools
Paul Kehrerafc1ccd2014-03-19 11:49:32 -04009import os
David Reid5443e9d2014-01-22 17:18:49 -080010
Alex Gaynorbd458ae2013-10-16 11:59:30 -070011import pytest
12
Paul Kehrerafc1ccd2014-03-19 11:49:32 -040013from cryptography.exceptions import (
Paul Kehrer49c8e212014-03-18 07:54:34 -040014 AlreadyFinalized, AlreadyUpdated, InvalidSignature, InvalidTag,
15 NotYetFinalized
Paul Kehrerafc1ccd2014-03-19 11:49:32 -040016)
Paul Kehrer22e80cb2013-11-20 21:27:00 -060017from cryptography.hazmat.primitives import hashes, hmac
Paul Kehrerc85f1792014-03-19 09:45:42 -040018from cryptography.hazmat.primitives.asymmetric import rsa
Paul Kehrer21dde562013-11-06 12:22:09 +080019from cryptography.hazmat.primitives.ciphers import Cipher
Ayrxac1a0792014-05-07 17:02:21 +080020from cryptography.hazmat.primitives.kdf.hkdf import HKDF, HKDFExpand
Paul Kehrerafc1ccd2014-03-19 11:49:32 -040021from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
Alex Gaynorbd458ae2013-10-16 11:59:30 -070022
Paul Kehrerf7f6a9f2013-11-11 20:43:52 -060023from ...utils import load_vectors_from_file
24
Alex Gaynorbd458ae2013-10-16 11:59:30 -070025
Alex Gaynore5c5eec2013-12-13 08:10:20 -080026def _load_all_params(path, file_names, param_loader):
27 all_params = []
28 for file_name in file_names:
29 all_params.extend(
30 load_vectors_from_file(os.path.join(path, file_name), param_loader)
31 )
32 return all_params
33
Alex Gaynor4eec0bb2013-12-13 09:12:19 -080034
Alex Gaynor016eed12013-10-16 14:16:04 -070035def generate_encrypt_test(param_loader, path, file_names, cipher_factory,
Paul Kehrer783479c2013-12-26 21:08:45 -060036 mode_factory):
Alex Gaynore5c5eec2013-12-13 08:10:20 -080037 all_params = _load_all_params(path, file_names, param_loader)
38
39 @pytest.mark.parametrize("params", all_params)
40 def test_encryption(self, backend, params):
Paul Kehrer783479c2013-12-26 21:08:45 -060041 encrypt_test(backend, cipher_factory, mode_factory, params)
Alex Gaynore5c5eec2013-12-13 08:10:20 -080042
Alex Gaynorbd458ae2013-10-16 11:59:30 -070043 return test_encryption
44
45
Paul Kehrer783479c2013-12-26 21:08:45 -060046def encrypt_test(backend, cipher_factory, mode_factory, params):
Paul Kehrera620b7d2013-12-20 22:59:02 -060047 plaintext = params["plaintext"]
48 ciphertext = params["ciphertext"]
Alex Gaynor21919e22013-12-13 08:56:32 -080049 cipher = Cipher(
50 cipher_factory(**params),
51 mode_factory(**params),
52 backend=backend
53 )
54 encryptor = cipher.encryptor()
55 actual_ciphertext = encryptor.update(binascii.unhexlify(plaintext))
56 actual_ciphertext += encryptor.finalize()
57 assert actual_ciphertext == binascii.unhexlify(ciphertext)
58 decryptor = cipher.decryptor()
59 actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext))
60 actual_plaintext += decryptor.finalize()
61 assert actual_plaintext == binascii.unhexlify(plaintext)
62
63
Paul Kehrer22e80cb2013-11-20 21:27:00 -060064def generate_aead_test(param_loader, path, file_names, cipher_factory,
Paul Kehrer783479c2013-12-26 21:08:45 -060065 mode_factory):
Alex Gaynore5c5eec2013-12-13 08:10:20 -080066 all_params = _load_all_params(path, file_names, param_loader)
67
68 @pytest.mark.parametrize("params", all_params)
69 def test_aead(self, backend, params):
Paul Kehrer783479c2013-12-26 21:08:45 -060070 aead_test(backend, cipher_factory, mode_factory, params)
Alex Gaynore5c5eec2013-12-13 08:10:20 -080071
Paul Kehrer22e80cb2013-11-20 21:27:00 -060072 return test_aead
73
74
Paul Kehrer783479c2013-12-26 21:08:45 -060075def aead_test(backend, cipher_factory, mode_factory, params):
Alex Gaynor21919e22013-12-13 08:56:32 -080076 if params.get("pt") is not None:
Paul Kehrera620b7d2013-12-20 22:59:02 -060077 plaintext = params["pt"]
78 ciphertext = params["ct"]
79 aad = params["aad"]
Alex Gaynor21919e22013-12-13 08:56:32 -080080 if params.get("fail") is True:
81 cipher = Cipher(
82 cipher_factory(binascii.unhexlify(params["key"])),
83 mode_factory(binascii.unhexlify(params["iv"]),
Alex Gaynor8f1b8e82014-06-29 20:43:29 -070084 binascii.unhexlify(params["tag"]),
85 len(binascii.unhexlify(params["tag"]))),
Alex Gaynor21919e22013-12-13 08:56:32 -080086 backend
87 )
88 decryptor = cipher.decryptor()
89 decryptor.authenticate_additional_data(binascii.unhexlify(aad))
90 actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext))
91 with pytest.raises(InvalidTag):
92 decryptor.finalize()
93 else:
94 cipher = Cipher(
95 cipher_factory(binascii.unhexlify(params["key"])),
96 mode_factory(binascii.unhexlify(params["iv"]), None),
97 backend
98 )
99 encryptor = cipher.encryptor()
100 encryptor.authenticate_additional_data(binascii.unhexlify(aad))
101 actual_ciphertext = encryptor.update(binascii.unhexlify(plaintext))
102 actual_ciphertext += encryptor.finalize()
Alex Gaynor8f1b8e82014-06-29 20:43:29 -0700103 tag_len = len(binascii.unhexlify(params["tag"]))
104 assert binascii.hexlify(encryptor.tag[:tag_len]) == params["tag"]
Alex Gaynor21919e22013-12-13 08:56:32 -0800105 cipher = Cipher(
106 cipher_factory(binascii.unhexlify(params["key"])),
107 mode_factory(binascii.unhexlify(params["iv"]),
Alex Gaynor8f1b8e82014-06-29 20:43:29 -0700108 binascii.unhexlify(params["tag"]),
109 min_tag_length=tag_len),
Alex Gaynor21919e22013-12-13 08:56:32 -0800110 backend
111 )
112 decryptor = cipher.decryptor()
113 decryptor.authenticate_additional_data(binascii.unhexlify(aad))
114 actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext))
115 actual_plaintext += decryptor.finalize()
116 assert actual_plaintext == binascii.unhexlify(plaintext)
117
118
Paul Kehrer4da28c32013-11-07 07:50:17 +0800119def generate_stream_encryption_test(param_loader, path, file_names,
Paul Kehrer783479c2013-12-26 21:08:45 -0600120 cipher_factory):
Alex Gaynore5c5eec2013-12-13 08:10:20 -0800121 all_params = _load_all_params(path, file_names, param_loader)
122
123 @pytest.mark.parametrize("params", all_params)
124 def test_stream_encryption(self, backend, params):
Paul Kehrer783479c2013-12-26 21:08:45 -0600125 stream_encryption_test(backend, cipher_factory, params)
Paul Kehrer4da28c32013-11-07 07:50:17 +0800126 return test_stream_encryption
127
128
Paul Kehrer783479c2013-12-26 21:08:45 -0600129def stream_encryption_test(backend, cipher_factory, params):
Paul Kehrera620b7d2013-12-20 22:59:02 -0600130 plaintext = params["plaintext"]
131 ciphertext = params["ciphertext"]
132 offset = params["offset"]
Alex Gaynor21919e22013-12-13 08:56:32 -0800133 cipher = Cipher(cipher_factory(**params), None, backend=backend)
134 encryptor = cipher.encryptor()
135 # throw away offset bytes
136 encryptor.update(b"\x00" * int(offset))
137 actual_ciphertext = encryptor.update(binascii.unhexlify(plaintext))
138 actual_ciphertext += encryptor.finalize()
139 assert actual_ciphertext == binascii.unhexlify(ciphertext)
140 decryptor = cipher.decryptor()
141 decryptor.update(b"\x00" * int(offset))
142 actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext))
143 actual_plaintext += decryptor.finalize()
144 assert actual_plaintext == binascii.unhexlify(plaintext)
145
146
Paul Kehrer783479c2013-12-26 21:08:45 -0600147def generate_hash_test(param_loader, path, file_names, hash_cls):
Alex Gaynore5c5eec2013-12-13 08:10:20 -0800148 all_params = _load_all_params(path, file_names, param_loader)
Paul Kehrer4da28c32013-11-07 07:50:17 +0800149
Alex Gaynore5c5eec2013-12-13 08:10:20 -0800150 @pytest.mark.parametrize("params", all_params)
151 def test_hash(self, backend, params):
Paul Kehrer783479c2013-12-26 21:08:45 -0600152 hash_test(backend, hash_cls, params)
Paul Kehrerbde6fb52013-10-18 18:08:49 -0500153 return test_hash
154
155
Paul Kehrer783479c2013-12-26 21:08:45 -0600156def hash_test(backend, algorithm, params):
Alex Gaynor36e651c2014-01-27 10:08:35 -0800157 msg, md = params
Alex Gaynor21919e22013-12-13 08:56:32 -0800158 m = hashes.Hash(algorithm, backend=backend)
159 m.update(binascii.unhexlify(msg))
160 expected_md = md.replace(" ", "").lower().encode("ascii")
161 assert m.finalize() == binascii.unhexlify(expected_md)
162
163
Paul Kehrerb078d8e2013-12-27 16:33:14 -0600164def generate_base_hash_test(algorithm, digest_size, block_size):
165 def test_base_hash(self, backend):
166 base_hash_test(backend, algorithm, digest_size, block_size)
167 return test_base_hash
168
169
Paul Kehrer783479c2013-12-26 21:08:45 -0600170def base_hash_test(backend, algorithm, digest_size, block_size):
Alex Gaynor21919e22013-12-13 08:56:32 -0800171 m = hashes.Hash(algorithm, backend=backend)
172 assert m.algorithm.digest_size == digest_size
173 assert m.algorithm.block_size == block_size
174 m_copy = m.copy()
175 assert m != m_copy
176 assert m._ctx != m_copy._ctx
177
178 m.update(b"abc")
179 copy = m.copy()
180 copy.update(b"123")
181 m.update(b"123")
182 assert copy.finalize() == m.finalize()
183
184
Paul Kehrer783479c2013-12-26 21:08:45 -0600185def generate_long_string_hash_test(hash_factory, md):
Alex Gaynore5c5eec2013-12-13 08:10:20 -0800186 def test_long_string_hash(self, backend):
Paul Kehrer783479c2013-12-26 21:08:45 -0600187 long_string_hash_test(backend, hash_factory, md)
Paul Kehrerc1794072013-10-18 21:42:57 -0500188 return test_long_string_hash
189
190
Paul Kehrer783479c2013-12-26 21:08:45 -0600191def long_string_hash_test(backend, algorithm, md):
Alex Gaynor21919e22013-12-13 08:56:32 -0800192 m = hashes.Hash(algorithm, backend=backend)
193 m.update(b"a" * 1000000)
194 assert m.finalize() == binascii.unhexlify(md.lower().encode("ascii"))
195
196
Paul Kehrerb078d8e2013-12-27 16:33:14 -0600197def generate_base_hmac_test(hash_cls):
198 def test_base_hmac(self, backend):
199 base_hmac_test(backend, hash_cls)
200 return test_base_hmac
201
202
203def base_hmac_test(backend, algorithm):
204 key = b"ab"
205 h = hmac.HMAC(binascii.unhexlify(key), algorithm, backend=backend)
206 h_copy = h.copy()
207 assert h != h_copy
208 assert h._ctx != h_copy._ctx
209
210
Paul Kehrer783479c2013-12-26 21:08:45 -0600211def generate_hmac_test(param_loader, path, file_names, algorithm):
Alex Gaynore5c5eec2013-12-13 08:10:20 -0800212 all_params = _load_all_params(path, file_names, param_loader)
Paul Kehrer0317b042013-10-28 17:34:27 -0500213
Alex Gaynore5c5eec2013-12-13 08:10:20 -0800214 @pytest.mark.parametrize("params", all_params)
215 def test_hmac(self, backend, params):
Paul Kehrer783479c2013-12-26 21:08:45 -0600216 hmac_test(backend, algorithm, params)
Paul Kehrer0317b042013-10-28 17:34:27 -0500217 return test_hmac
218
219
Paul Kehrer783479c2013-12-26 21:08:45 -0600220def hmac_test(backend, algorithm, params):
Alex Gaynor36e651c2014-01-27 10:08:35 -0800221 msg, md, key = params
Alex Gaynor21919e22013-12-13 08:56:32 -0800222 h = hmac.HMAC(binascii.unhexlify(key), algorithm, backend=backend)
223 h.update(binascii.unhexlify(msg))
224 assert h.finalize() == binascii.unhexlify(md.encode("ascii"))
Paul Kehrer0317b042013-10-28 17:34:27 -0500225
Alex Gaynor21919e22013-12-13 08:56:32 -0800226
Paul Kehrer1050ddf2014-01-27 21:04:03 -0600227def generate_pbkdf2_test(param_loader, path, file_names, algorithm):
228 all_params = _load_all_params(path, file_names, param_loader)
229
230 @pytest.mark.parametrize("params", all_params)
231 def test_pbkdf2(self, backend, params):
232 pbkdf2_test(backend, algorithm, params)
233 return test_pbkdf2
234
235
236def pbkdf2_test(backend, algorithm, params):
237 # Password and salt can contain \0, which should be loaded as a null char.
238 # The NIST loader loads them as literal strings so we replace with the
239 # proper value.
Paul Kehrer1277bc72014-01-28 17:09:59 -0600240 kdf = PBKDF2HMAC(
Paul Kehrer1050ddf2014-01-27 21:04:03 -0600241 algorithm,
242 int(params["length"]),
243 params["salt"],
244 int(params["iterations"]),
245 backend
246 )
247 derived_key = kdf.derive(params["password"])
248 assert binascii.hexlify(derived_key) == params["derived_key"]
249
250
Paul Kehrer783479c2013-12-26 21:08:45 -0600251def generate_aead_exception_test(cipher_factory, mode_factory):
Alex Gaynor21919e22013-12-13 08:56:32 -0800252 def test_aead_exception(self, backend):
Paul Kehrer783479c2013-12-26 21:08:45 -0600253 aead_exception_test(backend, cipher_factory, mode_factory)
Paul Kehrerce9c6112013-11-22 14:10:59 -0600254 return test_aead_exception
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600255
256
Paul Kehrer783479c2013-12-26 21:08:45 -0600257def aead_exception_test(backend, cipher_factory, mode_factory):
Alex Gaynor21919e22013-12-13 08:56:32 -0800258 cipher = Cipher(
259 cipher_factory(binascii.unhexlify(b"0" * 32)),
260 mode_factory(binascii.unhexlify(b"0" * 24)),
261 backend
262 )
263 encryptor = cipher.encryptor()
264 encryptor.update(b"a" * 16)
265 with pytest.raises(NotYetFinalized):
266 encryptor.tag
267 with pytest.raises(AlreadyUpdated):
268 encryptor.authenticate_additional_data(b"b" * 16)
269 encryptor.finalize()
270 with pytest.raises(AlreadyFinalized):
271 encryptor.authenticate_additional_data(b"b" * 16)
272 with pytest.raises(AlreadyFinalized):
273 encryptor.update(b"b" * 16)
274 with pytest.raises(AlreadyFinalized):
275 encryptor.finalize()
276 cipher = Cipher(
277 cipher_factory(binascii.unhexlify(b"0" * 32)),
278 mode_factory(binascii.unhexlify(b"0" * 24), b"0" * 16),
279 backend
280 )
281 decryptor = cipher.decryptor()
282 decryptor.update(b"a" * 16)
283 with pytest.raises(AttributeError):
284 decryptor.tag
Paul Kehrerb91221d2013-12-04 17:56:40 -0600285
Alex Gaynor21919e22013-12-13 08:56:32 -0800286
Paul Kehrer783479c2013-12-26 21:08:45 -0600287def generate_aead_tag_exception_test(cipher_factory, mode_factory):
Alex Gaynor21919e22013-12-13 08:56:32 -0800288 def test_aead_tag_exception(self, backend):
Paul Kehrer783479c2013-12-26 21:08:45 -0600289 aead_tag_exception_test(backend, cipher_factory, mode_factory)
Paul Kehrerb91221d2013-12-04 17:56:40 -0600290 return test_aead_tag_exception
Alex Gaynor21919e22013-12-13 08:56:32 -0800291
292
Paul Kehrer783479c2013-12-26 21:08:45 -0600293def aead_tag_exception_test(backend, cipher_factory, mode_factory):
Alex Gaynor21919e22013-12-13 08:56:32 -0800294 cipher = Cipher(
295 cipher_factory(binascii.unhexlify(b"0" * 32)),
296 mode_factory(binascii.unhexlify(b"0" * 24)),
297 backend
298 )
299 with pytest.raises(ValueError):
300 cipher.decryptor()
Alex Gaynor516b1ad2014-01-01 12:28:37 -0800301
Paul Kehrerf7b4ede2013-12-21 17:25:19 -0600302 with pytest.raises(ValueError):
Alex Gaynor516b1ad2014-01-01 12:28:37 -0800303 mode_factory(binascii.unhexlify(b"0" * 24), b"000")
304
Alex Gaynora9477592014-06-30 10:03:22 -0700305 with pytest.raises(ValueError):
306 mode_factory(binascii.unhexlify(b"0" * 24), b"000000", 2)
307
Paul Kehrerf7b4ede2013-12-21 17:25:19 -0600308 cipher = Cipher(
309 cipher_factory(binascii.unhexlify(b"0" * 32)),
Alex Gaynor21919e22013-12-13 08:56:32 -0800310 mode_factory(binascii.unhexlify(b"0" * 24), b"0" * 16),
311 backend
312 )
313 with pytest.raises(ValueError):
314 cipher.encryptor()
David Reid66c9cd92014-01-20 16:05:53 -0800315
316
David Reid5443e9d2014-01-22 17:18:49 -0800317def hkdf_derive_test(backend, algorithm, params):
David Reid0d492db2014-01-27 17:05:49 -0800318 hkdf = HKDF(
David Reid66c9cd92014-01-20 16:05:53 -0800319 algorithm,
David Reid0d492db2014-01-27 17:05:49 -0800320 int(params["l"]),
321 salt=binascii.unhexlify(params["salt"]) or None,
322 info=binascii.unhexlify(params["info"]) or None,
David Reid66c9cd92014-01-20 16:05:53 -0800323 backend=backend
324 )
325
David Reid0d492db2014-01-27 17:05:49 -0800326 okm = hkdf.derive(binascii.unhexlify(params["ikm"]))
327
David Reid14367302014-01-27 16:33:31 -0800328 assert okm == binascii.unhexlify(params["okm"])
David Reid66c9cd92014-01-20 16:05:53 -0800329
330
David Reid5443e9d2014-01-22 17:18:49 -0800331def hkdf_extract_test(backend, algorithm, params):
David Reid0d492db2014-01-27 17:05:49 -0800332 hkdf = HKDF(
David Reid5443e9d2014-01-22 17:18:49 -0800333 algorithm,
David Reid0d492db2014-01-27 17:05:49 -0800334 int(params["l"]),
335 salt=binascii.unhexlify(params["salt"]) or None,
336 info=binascii.unhexlify(params["info"]) or None,
David Reid5443e9d2014-01-22 17:18:49 -0800337 backend=backend
338 )
339
David Reid15fd6432014-01-30 15:28:09 -0800340 prk = hkdf._extract(binascii.unhexlify(params["ikm"]))
David Reid0d492db2014-01-27 17:05:49 -0800341
David Reid14367302014-01-27 16:33:31 -0800342 assert prk == binascii.unhexlify(params["prk"])
David Reid5443e9d2014-01-22 17:18:49 -0800343
344
345def hkdf_expand_test(backend, algorithm, params):
Ayrxac1a0792014-05-07 17:02:21 +0800346 hkdf = HKDFExpand(
David Reid5443e9d2014-01-22 17:18:49 -0800347 algorithm,
David Reid14367302014-01-27 16:33:31 -0800348 int(params["l"]),
David Reid0d492db2014-01-27 17:05:49 -0800349 info=binascii.unhexlify(params["info"]) or None,
David Reid5443e9d2014-01-22 17:18:49 -0800350 backend=backend
351 )
352
Ayrx2d6cd442014-05-09 14:48:06 +0800353 okm = hkdf.derive(binascii.unhexlify(params["prk"]))
David Reid0d492db2014-01-27 17:05:49 -0800354
David Reid14367302014-01-27 16:33:31 -0800355 assert okm == binascii.unhexlify(params["okm"])
David Reid5443e9d2014-01-22 17:18:49 -0800356
357
David Reid66c9cd92014-01-20 16:05:53 -0800358def generate_hkdf_test(param_loader, path, file_names, algorithm):
359 all_params = _load_all_params(path, file_names, param_loader)
360
David Reid5443e9d2014-01-22 17:18:49 -0800361 all_tests = [hkdf_extract_test, hkdf_expand_test, hkdf_derive_test]
362
363 @pytest.mark.parametrize(
364 ("params", "hkdf_test"),
365 itertools.product(all_params, all_tests)
366 )
367 def test_hkdf(self, backend, params, hkdf_test):
David Reid66c9cd92014-01-20 16:05:53 -0800368 hkdf_test(backend, algorithm, params)
369
370 return test_hkdf
Paul Kehrerb5936a72014-03-13 21:03:00 -0400371
372
Paul Kehrerf29c3c52014-03-19 09:35:40 -0400373def generate_rsa_verification_test(param_loader, path, file_names, hash_alg,
Paul Kehrerc85f1792014-03-19 09:45:42 -0400374 pad_factory):
Paul Kehrerb5936a72014-03-13 21:03:00 -0400375 all_params = _load_all_params(path, file_names, param_loader)
Paul Kehrer1cfdca22014-03-16 17:57:43 -0400376 all_params = [i for i in all_params
377 if i["algorithm"] == hash_alg.name.upper()]
Paul Kehrerb5936a72014-03-13 21:03:00 -0400378
379 @pytest.mark.parametrize("params", all_params)
Paul Kehrerf29c3c52014-03-19 09:35:40 -0400380 def test_rsa_verification(self, backend, params):
Paul Kehrerc85f1792014-03-19 09:45:42 -0400381 rsa_verification_test(backend, params, hash_alg, pad_factory)
Paul Kehrerb5936a72014-03-13 21:03:00 -0400382
Paul Kehrerf29c3c52014-03-19 09:35:40 -0400383 return test_rsa_verification
Paul Kehrerb5936a72014-03-13 21:03:00 -0400384
385
Paul Kehrerc85f1792014-03-19 09:45:42 -0400386def rsa_verification_test(backend, params, hash_alg, pad_factory):
Paul Kehrer6af3c6e2014-06-06 21:05:23 -0500387 public_numbers = rsa.RSAPublicNumbers(
388 e=params["public_exponent"],
389 n=params["modulus"]
Paul Kehrer762014e2014-03-16 17:47:22 -0400390 )
Paul Kehrer144a4152014-06-20 11:52:37 -0600391 public_key = public_numbers.public_key(backend)
Paul Kehrerc85f1792014-03-19 09:45:42 -0400392 pad = pad_factory(params, hash_alg)
Paul Kehrer49c8e212014-03-18 07:54:34 -0400393 verifier = public_key.verifier(
394 binascii.unhexlify(params["s"]),
395 pad,
Paul Kehrer6af3c6e2014-06-06 21:05:23 -0500396 hash_alg
Paul Kehrer762014e2014-03-16 17:47:22 -0400397 )
398 verifier.update(binascii.unhexlify(params["msg"]))
Paul Kehrer49c8e212014-03-18 07:54:34 -0400399 if params["fail"]:
400 with pytest.raises(InvalidSignature):
401 verifier.verify()
402 else:
403 verifier.verify()
Alex Stapleton458c09b2014-04-23 20:58:37 +0100404
405
Paul Kehrer6af3c6e2014-06-06 21:05:23 -0500406def _check_rsa_private_numbers(skey):
Alex Stapleton458c09b2014-04-23 20:58:37 +0100407 assert skey
Paul Kehrer6af3c6e2014-06-06 21:05:23 -0500408 pkey = skey.public_numbers
409 assert pkey
410 assert pkey.e
411 assert pkey.n
412 assert skey.d
413 assert skey.p * skey.q == pkey.n
Alex Stapleton458c09b2014-04-23 20:58:37 +0100414 assert skey.dmp1 == rsa.rsa_crt_dmp1(skey.d, skey.p)
415 assert skey.dmq1 == rsa.rsa_crt_dmq1(skey.d, skey.q)
416 assert skey.iqmp == rsa.rsa_crt_iqmp(skey.p, skey.q)