blob: 811dcf905d7f7b2869354097bb42205fafb70f22 [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
Jared6d7fe002016-05-29 17:32:37 -070021from cryptography.hazmat.primitives.kdf.kbkdf import (
22 CounterLocation, KBKDFHMAC, Mode
23)
Paul Kehrerafc1ccd2014-03-19 11:49:32 -040024from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
Alex Gaynorbd458ae2013-10-16 11:59:30 -070025
Paul Kehrerf7f6a9f2013-11-11 20:43:52 -060026from ...utils import load_vectors_from_file
27
Alex Gaynorbd458ae2013-10-16 11:59:30 -070028
Alex Gaynore5c5eec2013-12-13 08:10:20 -080029def _load_all_params(path, file_names, param_loader):
30 all_params = []
31 for file_name in file_names:
32 all_params.extend(
33 load_vectors_from_file(os.path.join(path, file_name), param_loader)
34 )
35 return all_params
36
Alex Gaynor4eec0bb2013-12-13 09:12:19 -080037
Alex Gaynor016eed12013-10-16 14:16:04 -070038def generate_encrypt_test(param_loader, path, file_names, cipher_factory,
Paul Kehrer783479c2013-12-26 21:08:45 -060039 mode_factory):
Alex Gaynore5c5eec2013-12-13 08:10:20 -080040 all_params = _load_all_params(path, file_names, param_loader)
41
42 @pytest.mark.parametrize("params", all_params)
43 def test_encryption(self, backend, params):
Paul Kehrer783479c2013-12-26 21:08:45 -060044 encrypt_test(backend, cipher_factory, mode_factory, params)
Alex Gaynore5c5eec2013-12-13 08:10:20 -080045
Alex Gaynorbd458ae2013-10-16 11:59:30 -070046 return test_encryption
47
48
Paul Kehrer783479c2013-12-26 21:08:45 -060049def encrypt_test(backend, cipher_factory, mode_factory, params):
Paul Kehrer51032352017-05-20 10:09:02 -070050 assert backend.cipher_supported(
Paul Kehrerdbb64bd2016-07-11 02:13:40 +000051 cipher_factory(**params), mode_factory(**params)
Paul Kehrer51032352017-05-20 10:09:02 -070052 )
Paul Kehrerdbb64bd2016-07-11 02:13:40 +000053
Paul Kehrera620b7d2013-12-20 22:59:02 -060054 plaintext = params["plaintext"]
55 ciphertext = params["ciphertext"]
Alex Gaynor21919e22013-12-13 08:56:32 -080056 cipher = Cipher(
57 cipher_factory(**params),
58 mode_factory(**params),
59 backend=backend
60 )
61 encryptor = cipher.encryptor()
62 actual_ciphertext = encryptor.update(binascii.unhexlify(plaintext))
63 actual_ciphertext += encryptor.finalize()
64 assert actual_ciphertext == binascii.unhexlify(ciphertext)
65 decryptor = cipher.decryptor()
66 actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext))
67 actual_plaintext += decryptor.finalize()
68 assert actual_plaintext == binascii.unhexlify(plaintext)
69
70
Paul Kehrer22e80cb2013-11-20 21:27:00 -060071def generate_aead_test(param_loader, path, file_names, cipher_factory,
Paul Kehrer783479c2013-12-26 21:08:45 -060072 mode_factory):
Alex Gaynore5c5eec2013-12-13 08:10:20 -080073 all_params = _load_all_params(path, file_names, param_loader)
74
75 @pytest.mark.parametrize("params", all_params)
76 def test_aead(self, backend, params):
Paul Kehrer783479c2013-12-26 21:08:45 -060077 aead_test(backend, cipher_factory, mode_factory, params)
Alex Gaynore5c5eec2013-12-13 08:10:20 -080078
Paul Kehrer22e80cb2013-11-20 21:27:00 -060079 return test_aead
80
81
Paul Kehrer783479c2013-12-26 21:08:45 -060082def aead_test(backend, cipher_factory, mode_factory, params):
Alex Gaynor21919e22013-12-13 08:56:32 -080083 if params.get("pt") is not None:
Paul Kehrera620b7d2013-12-20 22:59:02 -060084 plaintext = params["pt"]
85 ciphertext = params["ct"]
86 aad = params["aad"]
Alex Gaynor21919e22013-12-13 08:56:32 -080087 if params.get("fail") is True:
88 cipher = Cipher(
89 cipher_factory(binascii.unhexlify(params["key"])),
90 mode_factory(binascii.unhexlify(params["iv"]),
Alex Gaynor8f1b8e82014-06-29 20:43:29 -070091 binascii.unhexlify(params["tag"]),
92 len(binascii.unhexlify(params["tag"]))),
Alex Gaynor21919e22013-12-13 08:56:32 -080093 backend
94 )
95 decryptor = cipher.decryptor()
96 decryptor.authenticate_additional_data(binascii.unhexlify(aad))
97 actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext))
98 with pytest.raises(InvalidTag):
99 decryptor.finalize()
100 else:
101 cipher = Cipher(
102 cipher_factory(binascii.unhexlify(params["key"])),
103 mode_factory(binascii.unhexlify(params["iv"]), None),
104 backend
105 )
106 encryptor = cipher.encryptor()
107 encryptor.authenticate_additional_data(binascii.unhexlify(aad))
108 actual_ciphertext = encryptor.update(binascii.unhexlify(plaintext))
109 actual_ciphertext += encryptor.finalize()
Alex Gaynor8f1b8e82014-06-29 20:43:29 -0700110 tag_len = len(binascii.unhexlify(params["tag"]))
111 assert binascii.hexlify(encryptor.tag[:tag_len]) == params["tag"]
Alex Gaynor21919e22013-12-13 08:56:32 -0800112 cipher = Cipher(
113 cipher_factory(binascii.unhexlify(params["key"])),
114 mode_factory(binascii.unhexlify(params["iv"]),
Alex Gaynor8f1b8e82014-06-29 20:43:29 -0700115 binascii.unhexlify(params["tag"]),
116 min_tag_length=tag_len),
Alex Gaynor21919e22013-12-13 08:56:32 -0800117 backend
118 )
119 decryptor = cipher.decryptor()
120 decryptor.authenticate_additional_data(binascii.unhexlify(aad))
121 actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext))
122 actual_plaintext += decryptor.finalize()
123 assert actual_plaintext == binascii.unhexlify(plaintext)
124
125
Paul Kehrer4da28c32013-11-07 07:50:17 +0800126def generate_stream_encryption_test(param_loader, path, file_names,
Paul Kehrer783479c2013-12-26 21:08:45 -0600127 cipher_factory):
Alex Gaynore5c5eec2013-12-13 08:10:20 -0800128 all_params = _load_all_params(path, file_names, param_loader)
129
130 @pytest.mark.parametrize("params", all_params)
131 def test_stream_encryption(self, backend, params):
Paul Kehrer783479c2013-12-26 21:08:45 -0600132 stream_encryption_test(backend, cipher_factory, params)
Paul Kehrer4da28c32013-11-07 07:50:17 +0800133 return test_stream_encryption
134
135
Paul Kehrer783479c2013-12-26 21:08:45 -0600136def stream_encryption_test(backend, cipher_factory, params):
Paul Kehrera620b7d2013-12-20 22:59:02 -0600137 plaintext = params["plaintext"]
138 ciphertext = params["ciphertext"]
139 offset = params["offset"]
Alex Gaynor21919e22013-12-13 08:56:32 -0800140 cipher = Cipher(cipher_factory(**params), None, backend=backend)
141 encryptor = cipher.encryptor()
142 # throw away offset bytes
143 encryptor.update(b"\x00" * int(offset))
144 actual_ciphertext = encryptor.update(binascii.unhexlify(plaintext))
145 actual_ciphertext += encryptor.finalize()
146 assert actual_ciphertext == binascii.unhexlify(ciphertext)
147 decryptor = cipher.decryptor()
148 decryptor.update(b"\x00" * int(offset))
149 actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext))
150 actual_plaintext += decryptor.finalize()
151 assert actual_plaintext == binascii.unhexlify(plaintext)
152
153
Paul Kehrer783479c2013-12-26 21:08:45 -0600154def generate_hash_test(param_loader, path, file_names, hash_cls):
Alex Gaynore5c5eec2013-12-13 08:10:20 -0800155 all_params = _load_all_params(path, file_names, param_loader)
Paul Kehrer4da28c32013-11-07 07:50:17 +0800156
Alex Gaynore5c5eec2013-12-13 08:10:20 -0800157 @pytest.mark.parametrize("params", all_params)
158 def test_hash(self, backend, params):
Paul Kehrer783479c2013-12-26 21:08:45 -0600159 hash_test(backend, hash_cls, params)
Paul Kehrerbde6fb52013-10-18 18:08:49 -0500160 return test_hash
161
162
Paul Kehrer783479c2013-12-26 21:08:45 -0600163def hash_test(backend, algorithm, params):
Alex Gaynor36e651c2014-01-27 10:08:35 -0800164 msg, md = params
Alex Gaynor21919e22013-12-13 08:56:32 -0800165 m = hashes.Hash(algorithm, backend=backend)
166 m.update(binascii.unhexlify(msg))
167 expected_md = md.replace(" ", "").lower().encode("ascii")
168 assert m.finalize() == binascii.unhexlify(expected_md)
169
170
Paul Kehrer162a17e2018-07-23 19:55:06 +0800171def generate_base_hash_test(algorithm, digest_size):
Paul Kehrerb078d8e2013-12-27 16:33:14 -0600172 def test_base_hash(self, backend):
Paul Kehrer162a17e2018-07-23 19:55:06 +0800173 base_hash_test(backend, algorithm, digest_size)
Paul Kehrerb078d8e2013-12-27 16:33:14 -0600174 return test_base_hash
175
176
Paul Kehrer162a17e2018-07-23 19:55:06 +0800177def base_hash_test(backend, algorithm, digest_size):
Alex Gaynor21919e22013-12-13 08:56:32 -0800178 m = hashes.Hash(algorithm, backend=backend)
179 assert m.algorithm.digest_size == digest_size
Alex Gaynor21919e22013-12-13 08:56:32 -0800180 m_copy = m.copy()
181 assert m != m_copy
182 assert m._ctx != m_copy._ctx
183
184 m.update(b"abc")
185 copy = m.copy()
186 copy.update(b"123")
187 m.update(b"123")
188 assert copy.finalize() == m.finalize()
189
190
Paul Kehrerb078d8e2013-12-27 16:33:14 -0600191def generate_base_hmac_test(hash_cls):
192 def test_base_hmac(self, backend):
193 base_hmac_test(backend, hash_cls)
194 return test_base_hmac
195
196
197def base_hmac_test(backend, algorithm):
198 key = b"ab"
199 h = hmac.HMAC(binascii.unhexlify(key), algorithm, backend=backend)
200 h_copy = h.copy()
201 assert h != h_copy
202 assert h._ctx != h_copy._ctx
203
204
Paul Kehrer783479c2013-12-26 21:08:45 -0600205def generate_hmac_test(param_loader, path, file_names, algorithm):
Alex Gaynore5c5eec2013-12-13 08:10:20 -0800206 all_params = _load_all_params(path, file_names, param_loader)
Paul Kehrer0317b042013-10-28 17:34:27 -0500207
Alex Gaynore5c5eec2013-12-13 08:10:20 -0800208 @pytest.mark.parametrize("params", all_params)
209 def test_hmac(self, backend, params):
Paul Kehrer783479c2013-12-26 21:08:45 -0600210 hmac_test(backend, algorithm, params)
Paul Kehrer0317b042013-10-28 17:34:27 -0500211 return test_hmac
212
213
Paul Kehrer783479c2013-12-26 21:08:45 -0600214def hmac_test(backend, algorithm, params):
Alex Gaynor36e651c2014-01-27 10:08:35 -0800215 msg, md, key = params
Alex Gaynor21919e22013-12-13 08:56:32 -0800216 h = hmac.HMAC(binascii.unhexlify(key), algorithm, backend=backend)
217 h.update(binascii.unhexlify(msg))
218 assert h.finalize() == binascii.unhexlify(md.encode("ascii"))
Paul Kehrer0317b042013-10-28 17:34:27 -0500219
Alex Gaynor21919e22013-12-13 08:56:32 -0800220
Paul Kehrer1050ddf2014-01-27 21:04:03 -0600221def generate_pbkdf2_test(param_loader, path, file_names, algorithm):
222 all_params = _load_all_params(path, file_names, param_loader)
223
224 @pytest.mark.parametrize("params", all_params)
225 def test_pbkdf2(self, backend, params):
226 pbkdf2_test(backend, algorithm, params)
227 return test_pbkdf2
228
229
230def pbkdf2_test(backend, algorithm, params):
231 # Password and salt can contain \0, which should be loaded as a null char.
232 # The NIST loader loads them as literal strings so we replace with the
233 # proper value.
Paul Kehrer1277bc72014-01-28 17:09:59 -0600234 kdf = PBKDF2HMAC(
Paul Kehrer1050ddf2014-01-27 21:04:03 -0600235 algorithm,
236 int(params["length"]),
237 params["salt"],
238 int(params["iterations"]),
239 backend
240 )
241 derived_key = kdf.derive(params["password"])
242 assert binascii.hexlify(derived_key) == params["derived_key"]
243
244
Paul Kehrer783479c2013-12-26 21:08:45 -0600245def generate_aead_exception_test(cipher_factory, mode_factory):
Alex Gaynor21919e22013-12-13 08:56:32 -0800246 def test_aead_exception(self, backend):
Paul Kehrer783479c2013-12-26 21:08:45 -0600247 aead_exception_test(backend, cipher_factory, mode_factory)
Paul Kehrerce9c6112013-11-22 14:10:59 -0600248 return test_aead_exception
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600249
250
Paul Kehrer783479c2013-12-26 21:08:45 -0600251def aead_exception_test(backend, cipher_factory, mode_factory):
Alex Gaynor21919e22013-12-13 08:56:32 -0800252 cipher = Cipher(
253 cipher_factory(binascii.unhexlify(b"0" * 32)),
254 mode_factory(binascii.unhexlify(b"0" * 24)),
255 backend
256 )
257 encryptor = cipher.encryptor()
258 encryptor.update(b"a" * 16)
259 with pytest.raises(NotYetFinalized):
260 encryptor.tag
261 with pytest.raises(AlreadyUpdated):
262 encryptor.authenticate_additional_data(b"b" * 16)
263 encryptor.finalize()
264 with pytest.raises(AlreadyFinalized):
265 encryptor.authenticate_additional_data(b"b" * 16)
266 with pytest.raises(AlreadyFinalized):
267 encryptor.update(b"b" * 16)
268 with pytest.raises(AlreadyFinalized):
269 encryptor.finalize()
270 cipher = Cipher(
271 cipher_factory(binascii.unhexlify(b"0" * 32)),
272 mode_factory(binascii.unhexlify(b"0" * 24), b"0" * 16),
273 backend
274 )
275 decryptor = cipher.decryptor()
276 decryptor.update(b"a" * 16)
277 with pytest.raises(AttributeError):
278 decryptor.tag
Paul Kehrerb91221d2013-12-04 17:56:40 -0600279
Alex Gaynor21919e22013-12-13 08:56:32 -0800280
Paul Kehrer783479c2013-12-26 21:08:45 -0600281def generate_aead_tag_exception_test(cipher_factory, mode_factory):
Alex Gaynor21919e22013-12-13 08:56:32 -0800282 def test_aead_tag_exception(self, backend):
Paul Kehrer783479c2013-12-26 21:08:45 -0600283 aead_tag_exception_test(backend, cipher_factory, mode_factory)
Paul Kehrerb91221d2013-12-04 17:56:40 -0600284 return test_aead_tag_exception
Alex Gaynor21919e22013-12-13 08:56:32 -0800285
286
Paul Kehrer783479c2013-12-26 21:08:45 -0600287def aead_tag_exception_test(backend, cipher_factory, mode_factory):
Alex Gaynor21919e22013-12-13 08:56:32 -0800288 cipher = Cipher(
289 cipher_factory(binascii.unhexlify(b"0" * 32)),
290 mode_factory(binascii.unhexlify(b"0" * 24)),
291 backend
292 )
Alex Gaynor516b1ad2014-01-01 12:28:37 -0800293
Paul Kehrerf7b4ede2013-12-21 17:25:19 -0600294 with pytest.raises(ValueError):
Alex Gaynor516b1ad2014-01-01 12:28:37 -0800295 mode_factory(binascii.unhexlify(b"0" * 24), b"000")
296
Alex Gaynora9477592014-06-30 10:03:22 -0700297 with pytest.raises(ValueError):
298 mode_factory(binascii.unhexlify(b"0" * 24), b"000000", 2)
299
Paul Kehrerf7b4ede2013-12-21 17:25:19 -0600300 cipher = Cipher(
301 cipher_factory(binascii.unhexlify(b"0" * 32)),
Alex Gaynor21919e22013-12-13 08:56:32 -0800302 mode_factory(binascii.unhexlify(b"0" * 24), b"0" * 16),
303 backend
304 )
305 with pytest.raises(ValueError):
306 cipher.encryptor()
David Reid66c9cd92014-01-20 16:05:53 -0800307
308
David Reid5443e9d2014-01-22 17:18:49 -0800309def hkdf_derive_test(backend, algorithm, params):
David Reid0d492db2014-01-27 17:05:49 -0800310 hkdf = HKDF(
David Reid66c9cd92014-01-20 16:05:53 -0800311 algorithm,
David Reid0d492db2014-01-27 17:05:49 -0800312 int(params["l"]),
313 salt=binascii.unhexlify(params["salt"]) or None,
314 info=binascii.unhexlify(params["info"]) or None,
David Reid66c9cd92014-01-20 16:05:53 -0800315 backend=backend
316 )
317
David Reid0d492db2014-01-27 17:05:49 -0800318 okm = hkdf.derive(binascii.unhexlify(params["ikm"]))
319
David Reid14367302014-01-27 16:33:31 -0800320 assert okm == binascii.unhexlify(params["okm"])
David Reid66c9cd92014-01-20 16:05:53 -0800321
322
David Reid5443e9d2014-01-22 17:18:49 -0800323def hkdf_extract_test(backend, algorithm, params):
David Reid0d492db2014-01-27 17:05:49 -0800324 hkdf = HKDF(
David Reid5443e9d2014-01-22 17:18:49 -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 Reid5443e9d2014-01-22 17:18:49 -0800329 backend=backend
330 )
331
David Reid15fd6432014-01-30 15:28:09 -0800332 prk = hkdf._extract(binascii.unhexlify(params["ikm"]))
David Reid0d492db2014-01-27 17:05:49 -0800333
David Reid14367302014-01-27 16:33:31 -0800334 assert prk == binascii.unhexlify(params["prk"])
David Reid5443e9d2014-01-22 17:18:49 -0800335
336
337def hkdf_expand_test(backend, algorithm, params):
Ayrxac1a0792014-05-07 17:02:21 +0800338 hkdf = HKDFExpand(
David Reid5443e9d2014-01-22 17:18:49 -0800339 algorithm,
David Reid14367302014-01-27 16:33:31 -0800340 int(params["l"]),
David Reid0d492db2014-01-27 17:05:49 -0800341 info=binascii.unhexlify(params["info"]) or None,
David Reid5443e9d2014-01-22 17:18:49 -0800342 backend=backend
343 )
344
Ayrx2d6cd442014-05-09 14:48:06 +0800345 okm = hkdf.derive(binascii.unhexlify(params["prk"]))
David Reid0d492db2014-01-27 17:05:49 -0800346
David Reid14367302014-01-27 16:33:31 -0800347 assert okm == binascii.unhexlify(params["okm"])
David Reid5443e9d2014-01-22 17:18:49 -0800348
349
David Reid66c9cd92014-01-20 16:05:53 -0800350def generate_hkdf_test(param_loader, path, file_names, algorithm):
351 all_params = _load_all_params(path, file_names, param_loader)
352
David Reid5443e9d2014-01-22 17:18:49 -0800353 all_tests = [hkdf_extract_test, hkdf_expand_test, hkdf_derive_test]
354
355 @pytest.mark.parametrize(
356 ("params", "hkdf_test"),
357 itertools.product(all_params, all_tests)
358 )
359 def test_hkdf(self, backend, params, hkdf_test):
David Reid66c9cd92014-01-20 16:05:53 -0800360 hkdf_test(backend, algorithm, params)
361
362 return test_hkdf
Paul Kehrerb5936a72014-03-13 21:03:00 -0400363
364
Jared6d7fe002016-05-29 17:32:37 -0700365def generate_kbkdf_counter_mode_test(param_loader, path, file_names):
366 all_params = _load_all_params(path, file_names, param_loader)
367
368 @pytest.mark.parametrize("params", all_params)
369 def test_kbkdf(self, backend, params):
370 kbkdf_counter_mode_test(backend, params)
371 return test_kbkdf
372
373
374def kbkdf_counter_mode_test(backend, params):
375 supported_algorithms = {
376 'hmac_sha1': hashes.SHA1,
377 'hmac_sha224': hashes.SHA224,
378 'hmac_sha256': hashes.SHA256,
379 'hmac_sha384': hashes.SHA384,
380 'hmac_sha512': hashes.SHA512,
381 }
382
Paul Kehrercb0fa2e2016-05-29 22:37:33 -0500383 supported_counter_locations = {
Jared6d7fe002016-05-29 17:32:37 -0700384 "before_fixed": CounterLocation.BeforeFixed,
385 "after_fixed": CounterLocation.AfterFixed,
386 }
387
388 algorithm = supported_algorithms.get(params.get('prf'))
389 if algorithm is None or not backend.hmac_supported(algorithm()):
Paul Kehrercb0fa2e2016-05-29 22:37:33 -0500390 pytest.skip("KBKDF does not support algorithm: {0}".format(
391 params.get('prf')
392 ))
Jared6d7fe002016-05-29 17:32:37 -0700393
Paul Kehrercb0fa2e2016-05-29 22:37:33 -0500394 ctr_loc = supported_counter_locations.get(params.get("ctrlocation"))
Jared6d7fe002016-05-29 17:32:37 -0700395 if ctr_loc is None or not isinstance(ctr_loc, CounterLocation):
Paul Kehrercb0fa2e2016-05-29 22:37:33 -0500396 pytest.skip("Does not support counter location: {0}".format(
397 params.get('ctrlocation')
Jared6d7fe002016-05-29 17:32:37 -0700398 ))
399
400 ctrkdf = KBKDFHMAC(
401 algorithm(),
402 Mode.CounterMode,
403 params['l'] // 8,
404 params['rlen'] // 8,
405 None,
406 ctr_loc,
407 None,
408 None,
409 binascii.unhexlify(params['fixedinputdata']),
410 backend=backend)
411
412 ko = ctrkdf.derive(binascii.unhexlify(params['ki']))
413 assert binascii.hexlify(ko) == params["ko"]
414
415
Paul Kehrerf29c3c52014-03-19 09:35:40 -0400416def generate_rsa_verification_test(param_loader, path, file_names, hash_alg,
Paul Kehrerc85f1792014-03-19 09:45:42 -0400417 pad_factory):
Paul Kehrerb5936a72014-03-13 21:03:00 -0400418 all_params = _load_all_params(path, file_names, param_loader)
Paul Kehrer1cfdca22014-03-16 17:57:43 -0400419 all_params = [i for i in all_params
420 if i["algorithm"] == hash_alg.name.upper()]
Paul Kehrerb5936a72014-03-13 21:03:00 -0400421
422 @pytest.mark.parametrize("params", all_params)
Paul Kehrerf29c3c52014-03-19 09:35:40 -0400423 def test_rsa_verification(self, backend, params):
Paul Kehrerc85f1792014-03-19 09:45:42 -0400424 rsa_verification_test(backend, params, hash_alg, pad_factory)
Paul Kehrerb5936a72014-03-13 21:03:00 -0400425
Paul Kehrerf29c3c52014-03-19 09:35:40 -0400426 return test_rsa_verification
Paul Kehrerb5936a72014-03-13 21:03:00 -0400427
428
Paul Kehrerc85f1792014-03-19 09:45:42 -0400429def rsa_verification_test(backend, params, hash_alg, pad_factory):
Paul Kehrer6af3c6e2014-06-06 21:05:23 -0500430 public_numbers = rsa.RSAPublicNumbers(
431 e=params["public_exponent"],
432 n=params["modulus"]
Paul Kehrer762014e2014-03-16 17:47:22 -0400433 )
Paul Kehrer144a4152014-06-20 11:52:37 -0600434 public_key = public_numbers.public_key(backend)
Paul Kehrerc85f1792014-03-19 09:45:42 -0400435 pad = pad_factory(params, hash_alg)
Alex Gaynor0242c082017-12-09 20:58:52 -0500436 signature = binascii.unhexlify(params["s"])
437 msg = binascii.unhexlify(params["msg"])
Paul Kehrer49c8e212014-03-18 07:54:34 -0400438 if params["fail"]:
439 with pytest.raises(InvalidSignature):
Alex Gaynor0242c082017-12-09 20:58:52 -0500440 public_key.verify(
441 signature,
442 msg,
443 pad,
444 hash_alg
445 )
Paul Kehrer49c8e212014-03-18 07:54:34 -0400446 else:
Alex Gaynor0242c082017-12-09 20:58:52 -0500447 public_key.verify(
448 signature,
449 msg,
450 pad,
451 hash_alg
452 )
Alex Stapleton458c09b2014-04-23 20:58:37 +0100453
454
Paul Kehrer6af3c6e2014-06-06 21:05:23 -0500455def _check_rsa_private_numbers(skey):
Alex Stapleton458c09b2014-04-23 20:58:37 +0100456 assert skey
Paul Kehrer6af3c6e2014-06-06 21:05:23 -0500457 pkey = skey.public_numbers
458 assert pkey
459 assert pkey.e
460 assert pkey.n
461 assert skey.d
462 assert skey.p * skey.q == pkey.n
Alex Stapleton458c09b2014-04-23 20:58:37 +0100463 assert skey.dmp1 == rsa.rsa_crt_dmp1(skey.d, skey.p)
464 assert skey.dmq1 == rsa.rsa_crt_dmq1(skey.d, skey.q)
465 assert skey.iqmp == rsa.rsa_crt_iqmp(skey.p, skey.q)
Paul Kehrer85c11062014-12-22 07:56:05 -0600466
467
468def _check_dsa_private_numbers(skey):
469 assert skey
470 pkey = skey.public_numbers
471 params = pkey.parameter_numbers
472 assert pow(params.g, skey.x, params.p) == pkey.y