blob: d0e87a787718561d7ef78fb941589ce00cd79d4b [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 Kehrerdbb64bd2016-07-11 02:13:40 +000050 if not backend.cipher_supported(
51 cipher_factory(**params), mode_factory(**params)
52 ):
53 pytest.skip("cipher/mode combo is unsupported by this backend")
54
Paul Kehrera620b7d2013-12-20 22:59:02 -060055 plaintext = params["plaintext"]
56 ciphertext = params["ciphertext"]
Alex Gaynor21919e22013-12-13 08:56:32 -080057 cipher = Cipher(
58 cipher_factory(**params),
59 mode_factory(**params),
60 backend=backend
61 )
62 encryptor = cipher.encryptor()
63 actual_ciphertext = encryptor.update(binascii.unhexlify(plaintext))
64 actual_ciphertext += encryptor.finalize()
65 assert actual_ciphertext == binascii.unhexlify(ciphertext)
66 decryptor = cipher.decryptor()
67 actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext))
68 actual_plaintext += decryptor.finalize()
69 assert actual_plaintext == binascii.unhexlify(plaintext)
70
71
Paul Kehrer22e80cb2013-11-20 21:27:00 -060072def generate_aead_test(param_loader, path, file_names, cipher_factory,
Paul Kehrer783479c2013-12-26 21:08:45 -060073 mode_factory):
Alex Gaynore5c5eec2013-12-13 08:10:20 -080074 all_params = _load_all_params(path, file_names, param_loader)
75
76 @pytest.mark.parametrize("params", all_params)
77 def test_aead(self, backend, params):
Paul Kehrer783479c2013-12-26 21:08:45 -060078 aead_test(backend, cipher_factory, mode_factory, params)
Alex Gaynore5c5eec2013-12-13 08:10:20 -080079
Paul Kehrer22e80cb2013-11-20 21:27:00 -060080 return test_aead
81
82
Paul Kehrer783479c2013-12-26 21:08:45 -060083def aead_test(backend, cipher_factory, mode_factory, params):
Alex Gaynor21919e22013-12-13 08:56:32 -080084 if params.get("pt") is not None:
Paul Kehrera620b7d2013-12-20 22:59:02 -060085 plaintext = params["pt"]
86 ciphertext = params["ct"]
87 aad = params["aad"]
Alex Gaynor21919e22013-12-13 08:56:32 -080088 if params.get("fail") is True:
89 cipher = Cipher(
90 cipher_factory(binascii.unhexlify(params["key"])),
91 mode_factory(binascii.unhexlify(params["iv"]),
Alex Gaynor8f1b8e82014-06-29 20:43:29 -070092 binascii.unhexlify(params["tag"]),
93 len(binascii.unhexlify(params["tag"]))),
Alex Gaynor21919e22013-12-13 08:56:32 -080094 backend
95 )
96 decryptor = cipher.decryptor()
97 decryptor.authenticate_additional_data(binascii.unhexlify(aad))
98 actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext))
99 with pytest.raises(InvalidTag):
100 decryptor.finalize()
101 else:
102 cipher = Cipher(
103 cipher_factory(binascii.unhexlify(params["key"])),
104 mode_factory(binascii.unhexlify(params["iv"]), None),
105 backend
106 )
107 encryptor = cipher.encryptor()
108 encryptor.authenticate_additional_data(binascii.unhexlify(aad))
109 actual_ciphertext = encryptor.update(binascii.unhexlify(plaintext))
110 actual_ciphertext += encryptor.finalize()
Alex Gaynor8f1b8e82014-06-29 20:43:29 -0700111 tag_len = len(binascii.unhexlify(params["tag"]))
112 assert binascii.hexlify(encryptor.tag[:tag_len]) == params["tag"]
Alex Gaynor21919e22013-12-13 08:56:32 -0800113 cipher = Cipher(
114 cipher_factory(binascii.unhexlify(params["key"])),
115 mode_factory(binascii.unhexlify(params["iv"]),
Alex Gaynor8f1b8e82014-06-29 20:43:29 -0700116 binascii.unhexlify(params["tag"]),
117 min_tag_length=tag_len),
Alex Gaynor21919e22013-12-13 08:56:32 -0800118 backend
119 )
120 decryptor = cipher.decryptor()
121 decryptor.authenticate_additional_data(binascii.unhexlify(aad))
122 actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext))
123 actual_plaintext += decryptor.finalize()
124 assert actual_plaintext == binascii.unhexlify(plaintext)
125
126
Paul Kehrer4da28c32013-11-07 07:50:17 +0800127def generate_stream_encryption_test(param_loader, path, file_names,
Paul Kehrer783479c2013-12-26 21:08:45 -0600128 cipher_factory):
Alex Gaynore5c5eec2013-12-13 08:10:20 -0800129 all_params = _load_all_params(path, file_names, param_loader)
130
131 @pytest.mark.parametrize("params", all_params)
132 def test_stream_encryption(self, backend, params):
Paul Kehrer783479c2013-12-26 21:08:45 -0600133 stream_encryption_test(backend, cipher_factory, params)
Paul Kehrer4da28c32013-11-07 07:50:17 +0800134 return test_stream_encryption
135
136
Paul Kehrer783479c2013-12-26 21:08:45 -0600137def stream_encryption_test(backend, cipher_factory, params):
Paul Kehrera620b7d2013-12-20 22:59:02 -0600138 plaintext = params["plaintext"]
139 ciphertext = params["ciphertext"]
140 offset = params["offset"]
Alex Gaynor21919e22013-12-13 08:56:32 -0800141 cipher = Cipher(cipher_factory(**params), None, backend=backend)
142 encryptor = cipher.encryptor()
143 # throw away offset bytes
144 encryptor.update(b"\x00" * int(offset))
145 actual_ciphertext = encryptor.update(binascii.unhexlify(plaintext))
146 actual_ciphertext += encryptor.finalize()
147 assert actual_ciphertext == binascii.unhexlify(ciphertext)
148 decryptor = cipher.decryptor()
149 decryptor.update(b"\x00" * int(offset))
150 actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext))
151 actual_plaintext += decryptor.finalize()
152 assert actual_plaintext == binascii.unhexlify(plaintext)
153
154
Paul Kehrer783479c2013-12-26 21:08:45 -0600155def generate_hash_test(param_loader, path, file_names, hash_cls):
Alex Gaynore5c5eec2013-12-13 08:10:20 -0800156 all_params = _load_all_params(path, file_names, param_loader)
Paul Kehrer4da28c32013-11-07 07:50:17 +0800157
Alex Gaynore5c5eec2013-12-13 08:10:20 -0800158 @pytest.mark.parametrize("params", all_params)
159 def test_hash(self, backend, params):
Paul Kehrer783479c2013-12-26 21:08:45 -0600160 hash_test(backend, hash_cls, params)
Paul Kehrerbde6fb52013-10-18 18:08:49 -0500161 return test_hash
162
163
Paul Kehrer783479c2013-12-26 21:08:45 -0600164def hash_test(backend, algorithm, params):
Alex Gaynor36e651c2014-01-27 10:08:35 -0800165 msg, md = params
Alex Gaynor21919e22013-12-13 08:56:32 -0800166 m = hashes.Hash(algorithm, backend=backend)
167 m.update(binascii.unhexlify(msg))
168 expected_md = md.replace(" ", "").lower().encode("ascii")
169 assert m.finalize() == binascii.unhexlify(expected_md)
170
171
Paul Kehrerb078d8e2013-12-27 16:33:14 -0600172def generate_base_hash_test(algorithm, digest_size, block_size):
173 def test_base_hash(self, backend):
174 base_hash_test(backend, algorithm, digest_size, block_size)
175 return test_base_hash
176
177
Paul Kehrer783479c2013-12-26 21:08:45 -0600178def base_hash_test(backend, algorithm, digest_size, block_size):
Alex Gaynor21919e22013-12-13 08:56:32 -0800179 m = hashes.Hash(algorithm, backend=backend)
180 assert m.algorithm.digest_size == digest_size
181 assert m.algorithm.block_size == block_size
182 m_copy = m.copy()
183 assert m != m_copy
184 assert m._ctx != m_copy._ctx
185
186 m.update(b"abc")
187 copy = m.copy()
188 copy.update(b"123")
189 m.update(b"123")
190 assert copy.finalize() == m.finalize()
191
192
Paul Kehrer783479c2013-12-26 21:08:45 -0600193def generate_long_string_hash_test(hash_factory, md):
Alex Gaynore5c5eec2013-12-13 08:10:20 -0800194 def test_long_string_hash(self, backend):
Paul Kehrer783479c2013-12-26 21:08:45 -0600195 long_string_hash_test(backend, hash_factory, md)
Paul Kehrerc1794072013-10-18 21:42:57 -0500196 return test_long_string_hash
197
198
Paul Kehrer783479c2013-12-26 21:08:45 -0600199def long_string_hash_test(backend, algorithm, md):
Alex Gaynor21919e22013-12-13 08:56:32 -0800200 m = hashes.Hash(algorithm, backend=backend)
201 m.update(b"a" * 1000000)
202 assert m.finalize() == binascii.unhexlify(md.lower().encode("ascii"))
203
204
Paul Kehrerb078d8e2013-12-27 16:33:14 -0600205def generate_base_hmac_test(hash_cls):
206 def test_base_hmac(self, backend):
207 base_hmac_test(backend, hash_cls)
208 return test_base_hmac
209
210
211def base_hmac_test(backend, algorithm):
212 key = b"ab"
213 h = hmac.HMAC(binascii.unhexlify(key), algorithm, backend=backend)
214 h_copy = h.copy()
215 assert h != h_copy
216 assert h._ctx != h_copy._ctx
217
218
Paul Kehrer783479c2013-12-26 21:08:45 -0600219def generate_hmac_test(param_loader, path, file_names, algorithm):
Alex Gaynore5c5eec2013-12-13 08:10:20 -0800220 all_params = _load_all_params(path, file_names, param_loader)
Paul Kehrer0317b042013-10-28 17:34:27 -0500221
Alex Gaynore5c5eec2013-12-13 08:10:20 -0800222 @pytest.mark.parametrize("params", all_params)
223 def test_hmac(self, backend, params):
Paul Kehrer783479c2013-12-26 21:08:45 -0600224 hmac_test(backend, algorithm, params)
Paul Kehrer0317b042013-10-28 17:34:27 -0500225 return test_hmac
226
227
Paul Kehrer783479c2013-12-26 21:08:45 -0600228def hmac_test(backend, algorithm, params):
Alex Gaynor36e651c2014-01-27 10:08:35 -0800229 msg, md, key = params
Alex Gaynor21919e22013-12-13 08:56:32 -0800230 h = hmac.HMAC(binascii.unhexlify(key), algorithm, backend=backend)
231 h.update(binascii.unhexlify(msg))
232 assert h.finalize() == binascii.unhexlify(md.encode("ascii"))
Paul Kehrer0317b042013-10-28 17:34:27 -0500233
Alex Gaynor21919e22013-12-13 08:56:32 -0800234
Paul Kehrer1050ddf2014-01-27 21:04:03 -0600235def generate_pbkdf2_test(param_loader, path, file_names, algorithm):
236 all_params = _load_all_params(path, file_names, param_loader)
237
238 @pytest.mark.parametrize("params", all_params)
239 def test_pbkdf2(self, backend, params):
240 pbkdf2_test(backend, algorithm, params)
241 return test_pbkdf2
242
243
244def pbkdf2_test(backend, algorithm, params):
245 # Password and salt can contain \0, which should be loaded as a null char.
246 # The NIST loader loads them as literal strings so we replace with the
247 # proper value.
Paul Kehrer1277bc72014-01-28 17:09:59 -0600248 kdf = PBKDF2HMAC(
Paul Kehrer1050ddf2014-01-27 21:04:03 -0600249 algorithm,
250 int(params["length"]),
251 params["salt"],
252 int(params["iterations"]),
253 backend
254 )
255 derived_key = kdf.derive(params["password"])
256 assert binascii.hexlify(derived_key) == params["derived_key"]
257
258
Paul Kehrer783479c2013-12-26 21:08:45 -0600259def generate_aead_exception_test(cipher_factory, mode_factory):
Alex Gaynor21919e22013-12-13 08:56:32 -0800260 def test_aead_exception(self, backend):
Paul Kehrer783479c2013-12-26 21:08:45 -0600261 aead_exception_test(backend, cipher_factory, mode_factory)
Paul Kehrerce9c6112013-11-22 14:10:59 -0600262 return test_aead_exception
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600263
264
Paul Kehrer783479c2013-12-26 21:08:45 -0600265def aead_exception_test(backend, cipher_factory, mode_factory):
Alex Gaynor21919e22013-12-13 08:56:32 -0800266 cipher = Cipher(
267 cipher_factory(binascii.unhexlify(b"0" * 32)),
268 mode_factory(binascii.unhexlify(b"0" * 24)),
269 backend
270 )
271 encryptor = cipher.encryptor()
272 encryptor.update(b"a" * 16)
273 with pytest.raises(NotYetFinalized):
274 encryptor.tag
275 with pytest.raises(AlreadyUpdated):
276 encryptor.authenticate_additional_data(b"b" * 16)
277 encryptor.finalize()
278 with pytest.raises(AlreadyFinalized):
279 encryptor.authenticate_additional_data(b"b" * 16)
280 with pytest.raises(AlreadyFinalized):
281 encryptor.update(b"b" * 16)
282 with pytest.raises(AlreadyFinalized):
283 encryptor.finalize()
284 cipher = Cipher(
285 cipher_factory(binascii.unhexlify(b"0" * 32)),
286 mode_factory(binascii.unhexlify(b"0" * 24), b"0" * 16),
287 backend
288 )
289 decryptor = cipher.decryptor()
290 decryptor.update(b"a" * 16)
291 with pytest.raises(AttributeError):
292 decryptor.tag
Paul Kehrerb91221d2013-12-04 17:56:40 -0600293
Alex Gaynor21919e22013-12-13 08:56:32 -0800294
Paul Kehrer783479c2013-12-26 21:08:45 -0600295def generate_aead_tag_exception_test(cipher_factory, mode_factory):
Alex Gaynor21919e22013-12-13 08:56:32 -0800296 def test_aead_tag_exception(self, backend):
Paul Kehrer783479c2013-12-26 21:08:45 -0600297 aead_tag_exception_test(backend, cipher_factory, mode_factory)
Paul Kehrerb91221d2013-12-04 17:56:40 -0600298 return test_aead_tag_exception
Alex Gaynor21919e22013-12-13 08:56:32 -0800299
300
Paul Kehrer783479c2013-12-26 21:08:45 -0600301def aead_tag_exception_test(backend, cipher_factory, mode_factory):
Alex Gaynor21919e22013-12-13 08:56:32 -0800302 cipher = Cipher(
303 cipher_factory(binascii.unhexlify(b"0" * 32)),
304 mode_factory(binascii.unhexlify(b"0" * 24)),
305 backend
306 )
307 with pytest.raises(ValueError):
308 cipher.decryptor()
Alex Gaynor516b1ad2014-01-01 12:28:37 -0800309
Paul Kehrerf7b4ede2013-12-21 17:25:19 -0600310 with pytest.raises(ValueError):
Alex Gaynor516b1ad2014-01-01 12:28:37 -0800311 mode_factory(binascii.unhexlify(b"0" * 24), b"000")
312
Alex Gaynora9477592014-06-30 10:03:22 -0700313 with pytest.raises(ValueError):
314 mode_factory(binascii.unhexlify(b"0" * 24), b"000000", 2)
315
Paul Kehrerf7b4ede2013-12-21 17:25:19 -0600316 cipher = Cipher(
317 cipher_factory(binascii.unhexlify(b"0" * 32)),
Alex Gaynor21919e22013-12-13 08:56:32 -0800318 mode_factory(binascii.unhexlify(b"0" * 24), b"0" * 16),
319 backend
320 )
321 with pytest.raises(ValueError):
322 cipher.encryptor()
David Reid66c9cd92014-01-20 16:05:53 -0800323
324
David Reid5443e9d2014-01-22 17:18:49 -0800325def hkdf_derive_test(backend, algorithm, params):
David Reid0d492db2014-01-27 17:05:49 -0800326 hkdf = HKDF(
David Reid66c9cd92014-01-20 16:05:53 -0800327 algorithm,
David Reid0d492db2014-01-27 17:05:49 -0800328 int(params["l"]),
329 salt=binascii.unhexlify(params["salt"]) or None,
330 info=binascii.unhexlify(params["info"]) or None,
David Reid66c9cd92014-01-20 16:05:53 -0800331 backend=backend
332 )
333
David Reid0d492db2014-01-27 17:05:49 -0800334 okm = hkdf.derive(binascii.unhexlify(params["ikm"]))
335
David Reid14367302014-01-27 16:33:31 -0800336 assert okm == binascii.unhexlify(params["okm"])
David Reid66c9cd92014-01-20 16:05:53 -0800337
338
David Reid5443e9d2014-01-22 17:18:49 -0800339def hkdf_extract_test(backend, algorithm, params):
David Reid0d492db2014-01-27 17:05:49 -0800340 hkdf = HKDF(
David Reid5443e9d2014-01-22 17:18:49 -0800341 algorithm,
David Reid0d492db2014-01-27 17:05:49 -0800342 int(params["l"]),
343 salt=binascii.unhexlify(params["salt"]) or None,
344 info=binascii.unhexlify(params["info"]) or None,
David Reid5443e9d2014-01-22 17:18:49 -0800345 backend=backend
346 )
347
David Reid15fd6432014-01-30 15:28:09 -0800348 prk = hkdf._extract(binascii.unhexlify(params["ikm"]))
David Reid0d492db2014-01-27 17:05:49 -0800349
David Reid14367302014-01-27 16:33:31 -0800350 assert prk == binascii.unhexlify(params["prk"])
David Reid5443e9d2014-01-22 17:18:49 -0800351
352
353def hkdf_expand_test(backend, algorithm, params):
Ayrxac1a0792014-05-07 17:02:21 +0800354 hkdf = HKDFExpand(
David Reid5443e9d2014-01-22 17:18:49 -0800355 algorithm,
David Reid14367302014-01-27 16:33:31 -0800356 int(params["l"]),
David Reid0d492db2014-01-27 17:05:49 -0800357 info=binascii.unhexlify(params["info"]) or None,
David Reid5443e9d2014-01-22 17:18:49 -0800358 backend=backend
359 )
360
Ayrx2d6cd442014-05-09 14:48:06 +0800361 okm = hkdf.derive(binascii.unhexlify(params["prk"]))
David Reid0d492db2014-01-27 17:05:49 -0800362
David Reid14367302014-01-27 16:33:31 -0800363 assert okm == binascii.unhexlify(params["okm"])
David Reid5443e9d2014-01-22 17:18:49 -0800364
365
David Reid66c9cd92014-01-20 16:05:53 -0800366def generate_hkdf_test(param_loader, path, file_names, algorithm):
367 all_params = _load_all_params(path, file_names, param_loader)
368
David Reid5443e9d2014-01-22 17:18:49 -0800369 all_tests = [hkdf_extract_test, hkdf_expand_test, hkdf_derive_test]
370
371 @pytest.mark.parametrize(
372 ("params", "hkdf_test"),
373 itertools.product(all_params, all_tests)
374 )
375 def test_hkdf(self, backend, params, hkdf_test):
David Reid66c9cd92014-01-20 16:05:53 -0800376 hkdf_test(backend, algorithm, params)
377
378 return test_hkdf
Paul Kehrerb5936a72014-03-13 21:03:00 -0400379
380
Jared6d7fe002016-05-29 17:32:37 -0700381def generate_kbkdf_counter_mode_test(param_loader, path, file_names):
382 all_params = _load_all_params(path, file_names, param_loader)
383
384 @pytest.mark.parametrize("params", all_params)
385 def test_kbkdf(self, backend, params):
386 kbkdf_counter_mode_test(backend, params)
387 return test_kbkdf
388
389
390def kbkdf_counter_mode_test(backend, params):
391 supported_algorithms = {
392 'hmac_sha1': hashes.SHA1,
393 'hmac_sha224': hashes.SHA224,
394 'hmac_sha256': hashes.SHA256,
395 'hmac_sha384': hashes.SHA384,
396 'hmac_sha512': hashes.SHA512,
397 }
398
Paul Kehrercb0fa2e2016-05-29 22:37:33 -0500399 supported_counter_locations = {
Jared6d7fe002016-05-29 17:32:37 -0700400 "before_fixed": CounterLocation.BeforeFixed,
401 "after_fixed": CounterLocation.AfterFixed,
402 }
403
404 algorithm = supported_algorithms.get(params.get('prf'))
405 if algorithm is None or not backend.hmac_supported(algorithm()):
Paul Kehrercb0fa2e2016-05-29 22:37:33 -0500406 pytest.skip("KBKDF does not support algorithm: {0}".format(
407 params.get('prf')
408 ))
Jared6d7fe002016-05-29 17:32:37 -0700409
Paul Kehrercb0fa2e2016-05-29 22:37:33 -0500410 ctr_loc = supported_counter_locations.get(params.get("ctrlocation"))
Jared6d7fe002016-05-29 17:32:37 -0700411 if ctr_loc is None or not isinstance(ctr_loc, CounterLocation):
Paul Kehrercb0fa2e2016-05-29 22:37:33 -0500412 pytest.skip("Does not support counter location: {0}".format(
413 params.get('ctrlocation')
Jared6d7fe002016-05-29 17:32:37 -0700414 ))
415
416 ctrkdf = KBKDFHMAC(
417 algorithm(),
418 Mode.CounterMode,
419 params['l'] // 8,
420 params['rlen'] // 8,
421 None,
422 ctr_loc,
423 None,
424 None,
425 binascii.unhexlify(params['fixedinputdata']),
426 backend=backend)
427
428 ko = ctrkdf.derive(binascii.unhexlify(params['ki']))
429 assert binascii.hexlify(ko) == params["ko"]
430
431
Paul Kehrerf29c3c52014-03-19 09:35:40 -0400432def generate_rsa_verification_test(param_loader, path, file_names, hash_alg,
Paul Kehrerc85f1792014-03-19 09:45:42 -0400433 pad_factory):
Paul Kehrerb5936a72014-03-13 21:03:00 -0400434 all_params = _load_all_params(path, file_names, param_loader)
Paul Kehrer1cfdca22014-03-16 17:57:43 -0400435 all_params = [i for i in all_params
436 if i["algorithm"] == hash_alg.name.upper()]
Paul Kehrerb5936a72014-03-13 21:03:00 -0400437
438 @pytest.mark.parametrize("params", all_params)
Paul Kehrerf29c3c52014-03-19 09:35:40 -0400439 def test_rsa_verification(self, backend, params):
Paul Kehrerc85f1792014-03-19 09:45:42 -0400440 rsa_verification_test(backend, params, hash_alg, pad_factory)
Paul Kehrerb5936a72014-03-13 21:03:00 -0400441
Paul Kehrerf29c3c52014-03-19 09:35:40 -0400442 return test_rsa_verification
Paul Kehrerb5936a72014-03-13 21:03:00 -0400443
444
Paul Kehrerc85f1792014-03-19 09:45:42 -0400445def rsa_verification_test(backend, params, hash_alg, pad_factory):
Paul Kehrer6af3c6e2014-06-06 21:05:23 -0500446 public_numbers = rsa.RSAPublicNumbers(
447 e=params["public_exponent"],
448 n=params["modulus"]
Paul Kehrer762014e2014-03-16 17:47:22 -0400449 )
Paul Kehrer144a4152014-06-20 11:52:37 -0600450 public_key = public_numbers.public_key(backend)
Paul Kehrerc85f1792014-03-19 09:45:42 -0400451 pad = pad_factory(params, hash_alg)
Paul Kehrer49c8e212014-03-18 07:54:34 -0400452 verifier = public_key.verifier(
453 binascii.unhexlify(params["s"]),
454 pad,
Paul Kehrer6af3c6e2014-06-06 21:05:23 -0500455 hash_alg
Paul Kehrer762014e2014-03-16 17:47:22 -0400456 )
457 verifier.update(binascii.unhexlify(params["msg"]))
Paul Kehrer49c8e212014-03-18 07:54:34 -0400458 if params["fail"]:
459 with pytest.raises(InvalidSignature):
460 verifier.verify()
461 else:
462 verifier.verify()
Alex Stapleton458c09b2014-04-23 20:58:37 +0100463
464
Paul Kehrer6af3c6e2014-06-06 21:05:23 -0500465def _check_rsa_private_numbers(skey):
Alex Stapleton458c09b2014-04-23 20:58:37 +0100466 assert skey
Paul Kehrer6af3c6e2014-06-06 21:05:23 -0500467 pkey = skey.public_numbers
468 assert pkey
469 assert pkey.e
470 assert pkey.n
471 assert skey.d
472 assert skey.p * skey.q == pkey.n
Alex Stapleton458c09b2014-04-23 20:58:37 +0100473 assert skey.dmp1 == rsa.rsa_crt_dmp1(skey.d, skey.p)
474 assert skey.dmq1 == rsa.rsa_crt_dmq1(skey.d, skey.q)
475 assert skey.iqmp == rsa.rsa_crt_iqmp(skey.p, skey.q)
Paul Kehrer85c11062014-12-22 07:56:05 -0600476
477
478def _check_dsa_private_numbers(skey):
479 assert skey
480 pkey = skey.public_numbers
481 params = pkey.parameter_numbers
482 assert pow(params.g, skey.x, params.p) == pkey.y