blob: 8705cdc42975c916c39da112b03af759d72d16a2 [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 Kehrera620b7d2013-12-20 22:59:02 -060050 plaintext = params["plaintext"]
51 ciphertext = params["ciphertext"]
Alex Gaynor21919e22013-12-13 08:56:32 -080052 cipher = Cipher(
53 cipher_factory(**params),
54 mode_factory(**params),
55 backend=backend
56 )
57 encryptor = cipher.encryptor()
58 actual_ciphertext = encryptor.update(binascii.unhexlify(plaintext))
59 actual_ciphertext += encryptor.finalize()
60 assert actual_ciphertext == binascii.unhexlify(ciphertext)
61 decryptor = cipher.decryptor()
62 actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext))
63 actual_plaintext += decryptor.finalize()
64 assert actual_plaintext == binascii.unhexlify(plaintext)
65
66
Paul Kehrer22e80cb2013-11-20 21:27:00 -060067def generate_aead_test(param_loader, path, file_names, cipher_factory,
Paul Kehrer783479c2013-12-26 21:08:45 -060068 mode_factory):
Alex Gaynore5c5eec2013-12-13 08:10:20 -080069 all_params = _load_all_params(path, file_names, param_loader)
70
71 @pytest.mark.parametrize("params", all_params)
72 def test_aead(self, backend, params):
Paul Kehrer783479c2013-12-26 21:08:45 -060073 aead_test(backend, cipher_factory, mode_factory, params)
Alex Gaynore5c5eec2013-12-13 08:10:20 -080074
Paul Kehrer22e80cb2013-11-20 21:27:00 -060075 return test_aead
76
77
Paul Kehrer783479c2013-12-26 21:08:45 -060078def aead_test(backend, cipher_factory, mode_factory, params):
Alex Gaynor21919e22013-12-13 08:56:32 -080079 if params.get("pt") is not None:
Paul Kehrera620b7d2013-12-20 22:59:02 -060080 plaintext = params["pt"]
81 ciphertext = params["ct"]
82 aad = params["aad"]
Alex Gaynor21919e22013-12-13 08:56:32 -080083 if params.get("fail") is True:
84 cipher = Cipher(
85 cipher_factory(binascii.unhexlify(params["key"])),
86 mode_factory(binascii.unhexlify(params["iv"]),
Alex Gaynor8f1b8e82014-06-29 20:43:29 -070087 binascii.unhexlify(params["tag"]),
88 len(binascii.unhexlify(params["tag"]))),
Alex Gaynor21919e22013-12-13 08:56:32 -080089 backend
90 )
91 decryptor = cipher.decryptor()
92 decryptor.authenticate_additional_data(binascii.unhexlify(aad))
93 actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext))
94 with pytest.raises(InvalidTag):
95 decryptor.finalize()
96 else:
97 cipher = Cipher(
98 cipher_factory(binascii.unhexlify(params["key"])),
99 mode_factory(binascii.unhexlify(params["iv"]), None),
100 backend
101 )
102 encryptor = cipher.encryptor()
103 encryptor.authenticate_additional_data(binascii.unhexlify(aad))
104 actual_ciphertext = encryptor.update(binascii.unhexlify(plaintext))
105 actual_ciphertext += encryptor.finalize()
Alex Gaynor8f1b8e82014-06-29 20:43:29 -0700106 tag_len = len(binascii.unhexlify(params["tag"]))
107 assert binascii.hexlify(encryptor.tag[:tag_len]) == params["tag"]
Alex Gaynor21919e22013-12-13 08:56:32 -0800108 cipher = Cipher(
109 cipher_factory(binascii.unhexlify(params["key"])),
110 mode_factory(binascii.unhexlify(params["iv"]),
Alex Gaynor8f1b8e82014-06-29 20:43:29 -0700111 binascii.unhexlify(params["tag"]),
112 min_tag_length=tag_len),
Alex Gaynor21919e22013-12-13 08:56:32 -0800113 backend
114 )
115 decryptor = cipher.decryptor()
116 decryptor.authenticate_additional_data(binascii.unhexlify(aad))
117 actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext))
118 actual_plaintext += decryptor.finalize()
119 assert actual_plaintext == binascii.unhexlify(plaintext)
120
121
Paul Kehrer4da28c32013-11-07 07:50:17 +0800122def generate_stream_encryption_test(param_loader, path, file_names,
Paul Kehrer783479c2013-12-26 21:08:45 -0600123 cipher_factory):
Alex Gaynore5c5eec2013-12-13 08:10:20 -0800124 all_params = _load_all_params(path, file_names, param_loader)
125
126 @pytest.mark.parametrize("params", all_params)
127 def test_stream_encryption(self, backend, params):
Paul Kehrer783479c2013-12-26 21:08:45 -0600128 stream_encryption_test(backend, cipher_factory, params)
Paul Kehrer4da28c32013-11-07 07:50:17 +0800129 return test_stream_encryption
130
131
Paul Kehrer783479c2013-12-26 21:08:45 -0600132def stream_encryption_test(backend, cipher_factory, params):
Paul Kehrera620b7d2013-12-20 22:59:02 -0600133 plaintext = params["plaintext"]
134 ciphertext = params["ciphertext"]
135 offset = params["offset"]
Alex Gaynor21919e22013-12-13 08:56:32 -0800136 cipher = Cipher(cipher_factory(**params), None, backend=backend)
137 encryptor = cipher.encryptor()
138 # throw away offset bytes
139 encryptor.update(b"\x00" * int(offset))
140 actual_ciphertext = encryptor.update(binascii.unhexlify(plaintext))
141 actual_ciphertext += encryptor.finalize()
142 assert actual_ciphertext == binascii.unhexlify(ciphertext)
143 decryptor = cipher.decryptor()
144 decryptor.update(b"\x00" * int(offset))
145 actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext))
146 actual_plaintext += decryptor.finalize()
147 assert actual_plaintext == binascii.unhexlify(plaintext)
148
149
Paul Kehrer783479c2013-12-26 21:08:45 -0600150def generate_hash_test(param_loader, path, file_names, hash_cls):
Alex Gaynore5c5eec2013-12-13 08:10:20 -0800151 all_params = _load_all_params(path, file_names, param_loader)
Paul Kehrer4da28c32013-11-07 07:50:17 +0800152
Alex Gaynore5c5eec2013-12-13 08:10:20 -0800153 @pytest.mark.parametrize("params", all_params)
154 def test_hash(self, backend, params):
Paul Kehrer783479c2013-12-26 21:08:45 -0600155 hash_test(backend, hash_cls, params)
Paul Kehrerbde6fb52013-10-18 18:08:49 -0500156 return test_hash
157
158
Paul Kehrer783479c2013-12-26 21:08:45 -0600159def hash_test(backend, algorithm, params):
Alex Gaynor36e651c2014-01-27 10:08:35 -0800160 msg, md = params
Alex Gaynor21919e22013-12-13 08:56:32 -0800161 m = hashes.Hash(algorithm, backend=backend)
162 m.update(binascii.unhexlify(msg))
163 expected_md = md.replace(" ", "").lower().encode("ascii")
164 assert m.finalize() == binascii.unhexlify(expected_md)
165
166
Paul Kehrerb078d8e2013-12-27 16:33:14 -0600167def generate_base_hash_test(algorithm, digest_size, block_size):
168 def test_base_hash(self, backend):
169 base_hash_test(backend, algorithm, digest_size, block_size)
170 return test_base_hash
171
172
Paul Kehrer783479c2013-12-26 21:08:45 -0600173def base_hash_test(backend, algorithm, digest_size, block_size):
Alex Gaynor21919e22013-12-13 08:56:32 -0800174 m = hashes.Hash(algorithm, backend=backend)
175 assert m.algorithm.digest_size == digest_size
176 assert m.algorithm.block_size == block_size
177 m_copy = m.copy()
178 assert m != m_copy
179 assert m._ctx != m_copy._ctx
180
181 m.update(b"abc")
182 copy = m.copy()
183 copy.update(b"123")
184 m.update(b"123")
185 assert copy.finalize() == m.finalize()
186
187
Paul Kehrer783479c2013-12-26 21:08:45 -0600188def generate_long_string_hash_test(hash_factory, md):
Alex Gaynore5c5eec2013-12-13 08:10:20 -0800189 def test_long_string_hash(self, backend):
Paul Kehrer783479c2013-12-26 21:08:45 -0600190 long_string_hash_test(backend, hash_factory, md)
Paul Kehrerc1794072013-10-18 21:42:57 -0500191 return test_long_string_hash
192
193
Paul Kehrer783479c2013-12-26 21:08:45 -0600194def long_string_hash_test(backend, algorithm, md):
Alex Gaynor21919e22013-12-13 08:56:32 -0800195 m = hashes.Hash(algorithm, backend=backend)
196 m.update(b"a" * 1000000)
197 assert m.finalize() == binascii.unhexlify(md.lower().encode("ascii"))
198
199
Paul Kehrerb078d8e2013-12-27 16:33:14 -0600200def generate_base_hmac_test(hash_cls):
201 def test_base_hmac(self, backend):
202 base_hmac_test(backend, hash_cls)
203 return test_base_hmac
204
205
206def base_hmac_test(backend, algorithm):
207 key = b"ab"
208 h = hmac.HMAC(binascii.unhexlify(key), algorithm, backend=backend)
209 h_copy = h.copy()
210 assert h != h_copy
211 assert h._ctx != h_copy._ctx
212
213
Paul Kehrer783479c2013-12-26 21:08:45 -0600214def generate_hmac_test(param_loader, path, file_names, algorithm):
Alex Gaynore5c5eec2013-12-13 08:10:20 -0800215 all_params = _load_all_params(path, file_names, param_loader)
Paul Kehrer0317b042013-10-28 17:34:27 -0500216
Alex Gaynore5c5eec2013-12-13 08:10:20 -0800217 @pytest.mark.parametrize("params", all_params)
218 def test_hmac(self, backend, params):
Paul Kehrer783479c2013-12-26 21:08:45 -0600219 hmac_test(backend, algorithm, params)
Paul Kehrer0317b042013-10-28 17:34:27 -0500220 return test_hmac
221
222
Paul Kehrer783479c2013-12-26 21:08:45 -0600223def hmac_test(backend, algorithm, params):
Alex Gaynor36e651c2014-01-27 10:08:35 -0800224 msg, md, key = params
Alex Gaynor21919e22013-12-13 08:56:32 -0800225 h = hmac.HMAC(binascii.unhexlify(key), algorithm, backend=backend)
226 h.update(binascii.unhexlify(msg))
227 assert h.finalize() == binascii.unhexlify(md.encode("ascii"))
Paul Kehrer0317b042013-10-28 17:34:27 -0500228
Alex Gaynor21919e22013-12-13 08:56:32 -0800229
Paul Kehrer1050ddf2014-01-27 21:04:03 -0600230def generate_pbkdf2_test(param_loader, path, file_names, algorithm):
231 all_params = _load_all_params(path, file_names, param_loader)
232
233 @pytest.mark.parametrize("params", all_params)
234 def test_pbkdf2(self, backend, params):
235 pbkdf2_test(backend, algorithm, params)
236 return test_pbkdf2
237
238
239def pbkdf2_test(backend, algorithm, params):
240 # Password and salt can contain \0, which should be loaded as a null char.
241 # The NIST loader loads them as literal strings so we replace with the
242 # proper value.
Paul Kehrer1277bc72014-01-28 17:09:59 -0600243 kdf = PBKDF2HMAC(
Paul Kehrer1050ddf2014-01-27 21:04:03 -0600244 algorithm,
245 int(params["length"]),
246 params["salt"],
247 int(params["iterations"]),
248 backend
249 )
250 derived_key = kdf.derive(params["password"])
251 assert binascii.hexlify(derived_key) == params["derived_key"]
252
253
Paul Kehrer783479c2013-12-26 21:08:45 -0600254def generate_aead_exception_test(cipher_factory, mode_factory):
Alex Gaynor21919e22013-12-13 08:56:32 -0800255 def test_aead_exception(self, backend):
Paul Kehrer783479c2013-12-26 21:08:45 -0600256 aead_exception_test(backend, cipher_factory, mode_factory)
Paul Kehrerce9c6112013-11-22 14:10:59 -0600257 return test_aead_exception
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600258
259
Paul Kehrer783479c2013-12-26 21:08:45 -0600260def aead_exception_test(backend, cipher_factory, mode_factory):
Alex Gaynor21919e22013-12-13 08:56:32 -0800261 cipher = Cipher(
262 cipher_factory(binascii.unhexlify(b"0" * 32)),
263 mode_factory(binascii.unhexlify(b"0" * 24)),
264 backend
265 )
266 encryptor = cipher.encryptor()
267 encryptor.update(b"a" * 16)
268 with pytest.raises(NotYetFinalized):
269 encryptor.tag
270 with pytest.raises(AlreadyUpdated):
271 encryptor.authenticate_additional_data(b"b" * 16)
272 encryptor.finalize()
273 with pytest.raises(AlreadyFinalized):
274 encryptor.authenticate_additional_data(b"b" * 16)
275 with pytest.raises(AlreadyFinalized):
276 encryptor.update(b"b" * 16)
277 with pytest.raises(AlreadyFinalized):
278 encryptor.finalize()
279 cipher = Cipher(
280 cipher_factory(binascii.unhexlify(b"0" * 32)),
281 mode_factory(binascii.unhexlify(b"0" * 24), b"0" * 16),
282 backend
283 )
284 decryptor = cipher.decryptor()
285 decryptor.update(b"a" * 16)
286 with pytest.raises(AttributeError):
287 decryptor.tag
Paul Kehrerb91221d2013-12-04 17:56:40 -0600288
Alex Gaynor21919e22013-12-13 08:56:32 -0800289
Paul Kehrer783479c2013-12-26 21:08:45 -0600290def generate_aead_tag_exception_test(cipher_factory, mode_factory):
Alex Gaynor21919e22013-12-13 08:56:32 -0800291 def test_aead_tag_exception(self, backend):
Paul Kehrer783479c2013-12-26 21:08:45 -0600292 aead_tag_exception_test(backend, cipher_factory, mode_factory)
Paul Kehrerb91221d2013-12-04 17:56:40 -0600293 return test_aead_tag_exception
Alex Gaynor21919e22013-12-13 08:56:32 -0800294
295
Paul Kehrer783479c2013-12-26 21:08:45 -0600296def aead_tag_exception_test(backend, cipher_factory, mode_factory):
Alex Gaynor21919e22013-12-13 08:56:32 -0800297 cipher = Cipher(
298 cipher_factory(binascii.unhexlify(b"0" * 32)),
299 mode_factory(binascii.unhexlify(b"0" * 24)),
300 backend
301 )
302 with pytest.raises(ValueError):
303 cipher.decryptor()
Alex Gaynor516b1ad2014-01-01 12:28:37 -0800304
Paul Kehrerf7b4ede2013-12-21 17:25:19 -0600305 with pytest.raises(ValueError):
Alex Gaynor516b1ad2014-01-01 12:28:37 -0800306 mode_factory(binascii.unhexlify(b"0" * 24), b"000")
307
Alex Gaynora9477592014-06-30 10:03:22 -0700308 with pytest.raises(ValueError):
309 mode_factory(binascii.unhexlify(b"0" * 24), b"000000", 2)
310
Paul Kehrerf7b4ede2013-12-21 17:25:19 -0600311 cipher = Cipher(
312 cipher_factory(binascii.unhexlify(b"0" * 32)),
Alex Gaynor21919e22013-12-13 08:56:32 -0800313 mode_factory(binascii.unhexlify(b"0" * 24), b"0" * 16),
314 backend
315 )
316 with pytest.raises(ValueError):
317 cipher.encryptor()
David Reid66c9cd92014-01-20 16:05:53 -0800318
319
David Reid5443e9d2014-01-22 17:18:49 -0800320def hkdf_derive_test(backend, algorithm, params):
David Reid0d492db2014-01-27 17:05:49 -0800321 hkdf = HKDF(
David Reid66c9cd92014-01-20 16:05:53 -0800322 algorithm,
David Reid0d492db2014-01-27 17:05:49 -0800323 int(params["l"]),
324 salt=binascii.unhexlify(params["salt"]) or None,
325 info=binascii.unhexlify(params["info"]) or None,
David Reid66c9cd92014-01-20 16:05:53 -0800326 backend=backend
327 )
328
David Reid0d492db2014-01-27 17:05:49 -0800329 okm = hkdf.derive(binascii.unhexlify(params["ikm"]))
330
David Reid14367302014-01-27 16:33:31 -0800331 assert okm == binascii.unhexlify(params["okm"])
David Reid66c9cd92014-01-20 16:05:53 -0800332
333
David Reid5443e9d2014-01-22 17:18:49 -0800334def hkdf_extract_test(backend, algorithm, params):
David Reid0d492db2014-01-27 17:05:49 -0800335 hkdf = HKDF(
David Reid5443e9d2014-01-22 17:18:49 -0800336 algorithm,
David Reid0d492db2014-01-27 17:05:49 -0800337 int(params["l"]),
338 salt=binascii.unhexlify(params["salt"]) or None,
339 info=binascii.unhexlify(params["info"]) or None,
David Reid5443e9d2014-01-22 17:18:49 -0800340 backend=backend
341 )
342
David Reid15fd6432014-01-30 15:28:09 -0800343 prk = hkdf._extract(binascii.unhexlify(params["ikm"]))
David Reid0d492db2014-01-27 17:05:49 -0800344
David Reid14367302014-01-27 16:33:31 -0800345 assert prk == binascii.unhexlify(params["prk"])
David Reid5443e9d2014-01-22 17:18:49 -0800346
347
348def hkdf_expand_test(backend, algorithm, params):
Ayrxac1a0792014-05-07 17:02:21 +0800349 hkdf = HKDFExpand(
David Reid5443e9d2014-01-22 17:18:49 -0800350 algorithm,
David Reid14367302014-01-27 16:33:31 -0800351 int(params["l"]),
David Reid0d492db2014-01-27 17:05:49 -0800352 info=binascii.unhexlify(params["info"]) or None,
David Reid5443e9d2014-01-22 17:18:49 -0800353 backend=backend
354 )
355
Ayrx2d6cd442014-05-09 14:48:06 +0800356 okm = hkdf.derive(binascii.unhexlify(params["prk"]))
David Reid0d492db2014-01-27 17:05:49 -0800357
David Reid14367302014-01-27 16:33:31 -0800358 assert okm == binascii.unhexlify(params["okm"])
David Reid5443e9d2014-01-22 17:18:49 -0800359
360
David Reid66c9cd92014-01-20 16:05:53 -0800361def generate_hkdf_test(param_loader, path, file_names, algorithm):
362 all_params = _load_all_params(path, file_names, param_loader)
363
David Reid5443e9d2014-01-22 17:18:49 -0800364 all_tests = [hkdf_extract_test, hkdf_expand_test, hkdf_derive_test]
365
366 @pytest.mark.parametrize(
367 ("params", "hkdf_test"),
368 itertools.product(all_params, all_tests)
369 )
370 def test_hkdf(self, backend, params, hkdf_test):
David Reid66c9cd92014-01-20 16:05:53 -0800371 hkdf_test(backend, algorithm, params)
372
373 return test_hkdf
Paul Kehrerb5936a72014-03-13 21:03:00 -0400374
375
Jared6d7fe002016-05-29 17:32:37 -0700376def generate_kbkdf_counter_mode_test(param_loader, path, file_names):
377 all_params = _load_all_params(path, file_names, param_loader)
378
379 @pytest.mark.parametrize("params", all_params)
380 def test_kbkdf(self, backend, params):
381 kbkdf_counter_mode_test(backend, params)
382 return test_kbkdf
383
384
385def kbkdf_counter_mode_test(backend, params):
386 supported_algorithms = {
387 'hmac_sha1': hashes.SHA1,
388 'hmac_sha224': hashes.SHA224,
389 'hmac_sha256': hashes.SHA256,
390 'hmac_sha384': hashes.SHA384,
391 'hmac_sha512': hashes.SHA512,
392 }
393
Paul Kehrercb0fa2e2016-05-29 22:37:33 -0500394 supported_counter_locations = {
Jared6d7fe002016-05-29 17:32:37 -0700395 "before_fixed": CounterLocation.BeforeFixed,
396 "after_fixed": CounterLocation.AfterFixed,
397 }
398
399 algorithm = supported_algorithms.get(params.get('prf'))
400 if algorithm is None or not backend.hmac_supported(algorithm()):
Paul Kehrercb0fa2e2016-05-29 22:37:33 -0500401 pytest.skip("KBKDF does not support algorithm: {0}".format(
402 params.get('prf')
403 ))
Jared6d7fe002016-05-29 17:32:37 -0700404
Paul Kehrercb0fa2e2016-05-29 22:37:33 -0500405 ctr_loc = supported_counter_locations.get(params.get("ctrlocation"))
Jared6d7fe002016-05-29 17:32:37 -0700406 if ctr_loc is None or not isinstance(ctr_loc, CounterLocation):
Paul Kehrercb0fa2e2016-05-29 22:37:33 -0500407 pytest.skip("Does not support counter location: {0}".format(
408 params.get('ctrlocation')
Jared6d7fe002016-05-29 17:32:37 -0700409 ))
410
411 ctrkdf = KBKDFHMAC(
412 algorithm(),
413 Mode.CounterMode,
414 params['l'] // 8,
415 params['rlen'] // 8,
416 None,
417 ctr_loc,
418 None,
419 None,
420 binascii.unhexlify(params['fixedinputdata']),
421 backend=backend)
422
423 ko = ctrkdf.derive(binascii.unhexlify(params['ki']))
424 assert binascii.hexlify(ko) == params["ko"]
425
426
Paul Kehrerf29c3c52014-03-19 09:35:40 -0400427def generate_rsa_verification_test(param_loader, path, file_names, hash_alg,
Paul Kehrerc85f1792014-03-19 09:45:42 -0400428 pad_factory):
Paul Kehrerb5936a72014-03-13 21:03:00 -0400429 all_params = _load_all_params(path, file_names, param_loader)
Paul Kehrer1cfdca22014-03-16 17:57:43 -0400430 all_params = [i for i in all_params
431 if i["algorithm"] == hash_alg.name.upper()]
Paul Kehrerb5936a72014-03-13 21:03:00 -0400432
433 @pytest.mark.parametrize("params", all_params)
Paul Kehrerf29c3c52014-03-19 09:35:40 -0400434 def test_rsa_verification(self, backend, params):
Paul Kehrerc85f1792014-03-19 09:45:42 -0400435 rsa_verification_test(backend, params, hash_alg, pad_factory)
Paul Kehrerb5936a72014-03-13 21:03:00 -0400436
Paul Kehrerf29c3c52014-03-19 09:35:40 -0400437 return test_rsa_verification
Paul Kehrerb5936a72014-03-13 21:03:00 -0400438
439
Paul Kehrerc85f1792014-03-19 09:45:42 -0400440def rsa_verification_test(backend, params, hash_alg, pad_factory):
Paul Kehrer6af3c6e2014-06-06 21:05:23 -0500441 public_numbers = rsa.RSAPublicNumbers(
442 e=params["public_exponent"],
443 n=params["modulus"]
Paul Kehrer762014e2014-03-16 17:47:22 -0400444 )
Paul Kehrer144a4152014-06-20 11:52:37 -0600445 public_key = public_numbers.public_key(backend)
Paul Kehrerc85f1792014-03-19 09:45:42 -0400446 pad = pad_factory(params, hash_alg)
Paul Kehrer49c8e212014-03-18 07:54:34 -0400447 verifier = public_key.verifier(
448 binascii.unhexlify(params["s"]),
449 pad,
Paul Kehrer6af3c6e2014-06-06 21:05:23 -0500450 hash_alg
Paul Kehrer762014e2014-03-16 17:47:22 -0400451 )
452 verifier.update(binascii.unhexlify(params["msg"]))
Paul Kehrer49c8e212014-03-18 07:54:34 -0400453 if params["fail"]:
454 with pytest.raises(InvalidSignature):
455 verifier.verify()
456 else:
457 verifier.verify()
Alex Stapleton458c09b2014-04-23 20:58:37 +0100458
459
Paul Kehrer6af3c6e2014-06-06 21:05:23 -0500460def _check_rsa_private_numbers(skey):
Alex Stapleton458c09b2014-04-23 20:58:37 +0100461 assert skey
Paul Kehrer6af3c6e2014-06-06 21:05:23 -0500462 pkey = skey.public_numbers
463 assert pkey
464 assert pkey.e
465 assert pkey.n
466 assert skey.d
467 assert skey.p * skey.q == pkey.n
Alex Stapleton458c09b2014-04-23 20:58:37 +0100468 assert skey.dmp1 == rsa.rsa_crt_dmp1(skey.d, skey.p)
469 assert skey.dmq1 == rsa.rsa_crt_dmq1(skey.d, skey.q)
470 assert skey.iqmp == rsa.rsa_crt_iqmp(skey.p, skey.q)
Paul Kehrer85c11062014-12-22 07:56:05 -0600471
472
473def _check_dsa_private_numbers(skey):
474 assert skey
475 pkey = skey.public_numbers
476 params = pkey.parameter_numbers
477 assert pow(params.g, skey.x, params.p) == pkey.y