blob: 963838eb309f3acd6dd52d875c015512ad8c7552 [file] [log] [blame]
Alex Gaynorbd458ae2013-10-16 11:59:30 -07001import binascii
2import os
3
David Reid5443e9d2014-01-22 17:18:49 -08004import itertools
5
Alex Gaynorbd458ae2013-10-16 11:59:30 -07006import pytest
7
Paul Kehrer22e80cb2013-11-20 21:27:00 -06008from cryptography.hazmat.primitives import hashes, hmac
Paul Kehrer1277bc72014-01-28 17:09:59 -06009from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
Paul Kehrer21dde562013-11-06 12:22:09 +080010from cryptography.hazmat.primitives.ciphers import Cipher
David Reid5443e9d2014-01-22 17:18:49 -080011from cryptography.hazmat.primitives.kdf.hkdf import (
12 hkdf_derive, hkdf_extract, hkdf_expand
13)
David Reid66c9cd92014-01-20 16:05:53 -080014
Paul Kehrer22e80cb2013-11-20 21:27:00 -060015from cryptography.exceptions import (
Paul Kehrera4bfc082013-11-22 19:57:37 -060016 AlreadyFinalized, NotYetFinalized, AlreadyUpdated, InvalidTag,
Paul Kehrer22e80cb2013-11-20 21:27:00 -060017)
Alex Gaynorbd458ae2013-10-16 11:59:30 -070018
Paul Kehrerf7f6a9f2013-11-11 20:43:52 -060019from ...utils import load_vectors_from_file
20
Alex Gaynorbd458ae2013-10-16 11:59:30 -070021
Alex Gaynore5c5eec2013-12-13 08:10:20 -080022def _load_all_params(path, file_names, param_loader):
23 all_params = []
24 for file_name in file_names:
25 all_params.extend(
26 load_vectors_from_file(os.path.join(path, file_name), param_loader)
27 )
28 return all_params
29
Alex Gaynor4eec0bb2013-12-13 09:12:19 -080030
Alex Gaynor016eed12013-10-16 14:16:04 -070031def generate_encrypt_test(param_loader, path, file_names, cipher_factory,
Paul Kehrer783479c2013-12-26 21:08:45 -060032 mode_factory):
Alex Gaynore5c5eec2013-12-13 08:10:20 -080033 all_params = _load_all_params(path, file_names, param_loader)
34
35 @pytest.mark.parametrize("params", all_params)
36 def test_encryption(self, backend, params):
Paul Kehrer783479c2013-12-26 21:08:45 -060037 encrypt_test(backend, cipher_factory, mode_factory, params)
Alex Gaynore5c5eec2013-12-13 08:10:20 -080038
Alex Gaynorbd458ae2013-10-16 11:59:30 -070039 return test_encryption
40
41
Paul Kehrer783479c2013-12-26 21:08:45 -060042def encrypt_test(backend, cipher_factory, mode_factory, params):
Paul Kehrera620b7d2013-12-20 22:59:02 -060043 plaintext = params["plaintext"]
44 ciphertext = params["ciphertext"]
Alex Gaynor21919e22013-12-13 08:56:32 -080045 cipher = Cipher(
46 cipher_factory(**params),
47 mode_factory(**params),
48 backend=backend
49 )
50 encryptor = cipher.encryptor()
51 actual_ciphertext = encryptor.update(binascii.unhexlify(plaintext))
52 actual_ciphertext += encryptor.finalize()
53 assert actual_ciphertext == binascii.unhexlify(ciphertext)
54 decryptor = cipher.decryptor()
55 actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext))
56 actual_plaintext += decryptor.finalize()
57 assert actual_plaintext == binascii.unhexlify(plaintext)
58
59
Paul Kehrer22e80cb2013-11-20 21:27:00 -060060def generate_aead_test(param_loader, path, file_names, cipher_factory,
Paul Kehrer783479c2013-12-26 21:08:45 -060061 mode_factory):
Alex Gaynore5c5eec2013-12-13 08:10:20 -080062 all_params = _load_all_params(path, file_names, param_loader)
63
64 @pytest.mark.parametrize("params", all_params)
65 def test_aead(self, backend, params):
Paul Kehrer783479c2013-12-26 21:08:45 -060066 aead_test(backend, cipher_factory, mode_factory, params)
Alex Gaynore5c5eec2013-12-13 08:10:20 -080067
Paul Kehrer22e80cb2013-11-20 21:27:00 -060068 return test_aead
69
70
Paul Kehrer783479c2013-12-26 21:08:45 -060071def aead_test(backend, cipher_factory, mode_factory, params):
Alex Gaynor21919e22013-12-13 08:56:32 -080072 if params.get("pt") is not None:
Paul Kehrera620b7d2013-12-20 22:59:02 -060073 plaintext = params["pt"]
74 ciphertext = params["ct"]
75 aad = params["aad"]
Alex Gaynor21919e22013-12-13 08:56:32 -080076 if params.get("fail") is True:
77 cipher = Cipher(
78 cipher_factory(binascii.unhexlify(params["key"])),
79 mode_factory(binascii.unhexlify(params["iv"]),
80 binascii.unhexlify(params["tag"])),
81 backend
82 )
83 decryptor = cipher.decryptor()
84 decryptor.authenticate_additional_data(binascii.unhexlify(aad))
85 actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext))
86 with pytest.raises(InvalidTag):
87 decryptor.finalize()
88 else:
89 cipher = Cipher(
90 cipher_factory(binascii.unhexlify(params["key"])),
91 mode_factory(binascii.unhexlify(params["iv"]), None),
92 backend
93 )
94 encryptor = cipher.encryptor()
95 encryptor.authenticate_additional_data(binascii.unhexlify(aad))
96 actual_ciphertext = encryptor.update(binascii.unhexlify(plaintext))
97 actual_ciphertext += encryptor.finalize()
98 tag_len = len(params["tag"])
99 assert binascii.hexlify(encryptor.tag)[:tag_len] == params["tag"]
100 cipher = Cipher(
101 cipher_factory(binascii.unhexlify(params["key"])),
102 mode_factory(binascii.unhexlify(params["iv"]),
103 binascii.unhexlify(params["tag"])),
104 backend
105 )
106 decryptor = cipher.decryptor()
107 decryptor.authenticate_additional_data(binascii.unhexlify(aad))
108 actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext))
109 actual_plaintext += decryptor.finalize()
110 assert actual_plaintext == binascii.unhexlify(plaintext)
111
112
Paul Kehrer4da28c32013-11-07 07:50:17 +0800113def generate_stream_encryption_test(param_loader, path, file_names,
Paul Kehrer783479c2013-12-26 21:08:45 -0600114 cipher_factory):
Alex Gaynore5c5eec2013-12-13 08:10:20 -0800115 all_params = _load_all_params(path, file_names, param_loader)
116
117 @pytest.mark.parametrize("params", all_params)
118 def test_stream_encryption(self, backend, params):
Paul Kehrer783479c2013-12-26 21:08:45 -0600119 stream_encryption_test(backend, cipher_factory, params)
Paul Kehrer4da28c32013-11-07 07:50:17 +0800120 return test_stream_encryption
121
122
Paul Kehrer783479c2013-12-26 21:08:45 -0600123def stream_encryption_test(backend, cipher_factory, params):
Paul Kehrera620b7d2013-12-20 22:59:02 -0600124 plaintext = params["plaintext"]
125 ciphertext = params["ciphertext"]
126 offset = params["offset"]
Alex Gaynor21919e22013-12-13 08:56:32 -0800127 cipher = Cipher(cipher_factory(**params), None, backend=backend)
128 encryptor = cipher.encryptor()
129 # throw away offset bytes
130 encryptor.update(b"\x00" * int(offset))
131 actual_ciphertext = encryptor.update(binascii.unhexlify(plaintext))
132 actual_ciphertext += encryptor.finalize()
133 assert actual_ciphertext == binascii.unhexlify(ciphertext)
134 decryptor = cipher.decryptor()
135 decryptor.update(b"\x00" * int(offset))
136 actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext))
137 actual_plaintext += decryptor.finalize()
138 assert actual_plaintext == binascii.unhexlify(plaintext)
139
140
Paul Kehrer783479c2013-12-26 21:08:45 -0600141def generate_hash_test(param_loader, path, file_names, hash_cls):
Alex Gaynore5c5eec2013-12-13 08:10:20 -0800142 all_params = _load_all_params(path, file_names, param_loader)
Paul Kehrer4da28c32013-11-07 07:50:17 +0800143
Alex Gaynore5c5eec2013-12-13 08:10:20 -0800144 @pytest.mark.parametrize("params", all_params)
145 def test_hash(self, backend, params):
Paul Kehrer783479c2013-12-26 21:08:45 -0600146 hash_test(backend, hash_cls, params)
Paul Kehrerbde6fb52013-10-18 18:08:49 -0500147 return test_hash
148
149
Paul Kehrer783479c2013-12-26 21:08:45 -0600150def hash_test(backend, algorithm, params):
Alex Gaynor36e651c2014-01-27 10:08:35 -0800151 msg, md = params
Alex Gaynor21919e22013-12-13 08:56:32 -0800152 m = hashes.Hash(algorithm, backend=backend)
153 m.update(binascii.unhexlify(msg))
154 expected_md = md.replace(" ", "").lower().encode("ascii")
155 assert m.finalize() == binascii.unhexlify(expected_md)
156
157
Paul Kehrerb078d8e2013-12-27 16:33:14 -0600158def generate_base_hash_test(algorithm, digest_size, block_size):
159 def test_base_hash(self, backend):
160 base_hash_test(backend, algorithm, digest_size, block_size)
161 return test_base_hash
162
163
Paul Kehrer783479c2013-12-26 21:08:45 -0600164def base_hash_test(backend, algorithm, digest_size, block_size):
Alex Gaynor21919e22013-12-13 08:56:32 -0800165 m = hashes.Hash(algorithm, backend=backend)
166 assert m.algorithm.digest_size == digest_size
167 assert m.algorithm.block_size == block_size
168 m_copy = m.copy()
169 assert m != m_copy
170 assert m._ctx != m_copy._ctx
171
172 m.update(b"abc")
173 copy = m.copy()
174 copy.update(b"123")
175 m.update(b"123")
176 assert copy.finalize() == m.finalize()
177
178
Paul Kehrer783479c2013-12-26 21:08:45 -0600179def generate_long_string_hash_test(hash_factory, md):
Alex Gaynore5c5eec2013-12-13 08:10:20 -0800180 def test_long_string_hash(self, backend):
Paul Kehrer783479c2013-12-26 21:08:45 -0600181 long_string_hash_test(backend, hash_factory, md)
Paul Kehrerc1794072013-10-18 21:42:57 -0500182 return test_long_string_hash
183
184
Paul Kehrer783479c2013-12-26 21:08:45 -0600185def long_string_hash_test(backend, algorithm, md):
Alex Gaynor21919e22013-12-13 08:56:32 -0800186 m = hashes.Hash(algorithm, backend=backend)
187 m.update(b"a" * 1000000)
188 assert m.finalize() == binascii.unhexlify(md.lower().encode("ascii"))
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 )
293 with pytest.raises(ValueError):
294 cipher.decryptor()
Alex Gaynor516b1ad2014-01-01 12:28:37 -0800295
Paul Kehrerf7b4ede2013-12-21 17:25:19 -0600296 with pytest.raises(ValueError):
Alex Gaynor516b1ad2014-01-01 12:28:37 -0800297 mode_factory(binascii.unhexlify(b"0" * 24), b"000")
298
Paul Kehrerf7b4ede2013-12-21 17:25:19 -0600299 cipher = Cipher(
300 cipher_factory(binascii.unhexlify(b"0" * 32)),
Alex Gaynor21919e22013-12-13 08:56:32 -0800301 mode_factory(binascii.unhexlify(b"0" * 24), b"0" * 16),
302 backend
303 )
304 with pytest.raises(ValueError):
305 cipher.encryptor()
David Reid66c9cd92014-01-20 16:05:53 -0800306
307
David Reid5443e9d2014-01-22 17:18:49 -0800308def hkdf_derive_test(backend, algorithm, params):
309 ikm, salt, info, length, prk, expected_okm = params
David Reid66c9cd92014-01-20 16:05:53 -0800310
311 okm = hkdf_derive(
312 binascii.unhexlify(ikm),
313 length,
314 binascii.unhexlify(salt),
315 binascii.unhexlify(info),
316 algorithm,
317 backend=backend
318 )
319
320 assert binascii.hexlify(okm) == expected_okm
321
322
David Reid5443e9d2014-01-22 17:18:49 -0800323def hkdf_extract_test(backend, algorithm, params):
324 ikm, salt, info, length, expected_prk, okm = params
325
326 prk = hkdf_extract(
327 algorithm,
328 binascii.unhexlify(ikm),
329 binascii.unhexlify(salt),
330 backend=backend
331 )
332
333 assert prk == binascii.unhexlify(expected_prk)
334
335
336def hkdf_expand_test(backend, algorithm, params):
337 ikm, salt, info, length, prk, expected_okm = params
338
339 okm = hkdf_expand(
340 algorithm,
341 binascii.unhexlify(prk),
342 binascii.unhexlify(info),
343 length,
344 backend=backend
345 )
346
347 assert okm == binascii.unhexlify(expected_okm)
348
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