blob: 3a1d6d884f66440aef11a0b721487738a015a377 [file] [log] [blame]
Alex Gaynorbd458ae2013-10-16 11:59:30 -07001import binascii
2import os
3
4import pytest
5
Paul Kehrer22e80cb2013-11-20 21:27:00 -06006from cryptography.hazmat.primitives import hashes, hmac
Paul Kehrer1050ddf2014-01-27 21:04:03 -06007from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2
Paul Kehrer21dde562013-11-06 12:22:09 +08008from cryptography.hazmat.primitives.ciphers import Cipher
Paul Kehrer22e80cb2013-11-20 21:27:00 -06009from cryptography.exceptions import (
Paul Kehrera4bfc082013-11-22 19:57:37 -060010 AlreadyFinalized, NotYetFinalized, AlreadyUpdated, InvalidTag,
Paul Kehrer22e80cb2013-11-20 21:27:00 -060011)
Alex Gaynorbd458ae2013-10-16 11:59:30 -070012
Paul Kehrerf7f6a9f2013-11-11 20:43:52 -060013from ...utils import load_vectors_from_file
14
Alex Gaynorbd458ae2013-10-16 11:59:30 -070015
Alex Gaynore5c5eec2013-12-13 08:10:20 -080016def _load_all_params(path, file_names, param_loader):
17 all_params = []
18 for file_name in file_names:
19 all_params.extend(
20 load_vectors_from_file(os.path.join(path, file_name), param_loader)
21 )
22 return all_params
23
Alex Gaynor4eec0bb2013-12-13 09:12:19 -080024
Alex Gaynor016eed12013-10-16 14:16:04 -070025def generate_encrypt_test(param_loader, path, file_names, cipher_factory,
Paul Kehrer783479c2013-12-26 21:08:45 -060026 mode_factory):
Alex Gaynore5c5eec2013-12-13 08:10:20 -080027 all_params = _load_all_params(path, file_names, param_loader)
28
29 @pytest.mark.parametrize("params", all_params)
30 def test_encryption(self, backend, params):
Paul Kehrer783479c2013-12-26 21:08:45 -060031 encrypt_test(backend, cipher_factory, mode_factory, params)
Alex Gaynore5c5eec2013-12-13 08:10:20 -080032
Alex Gaynorbd458ae2013-10-16 11:59:30 -070033 return test_encryption
34
35
Paul Kehrer783479c2013-12-26 21:08:45 -060036def encrypt_test(backend, cipher_factory, mode_factory, params):
Paul Kehrera620b7d2013-12-20 22:59:02 -060037 plaintext = params["plaintext"]
38 ciphertext = params["ciphertext"]
Alex Gaynor21919e22013-12-13 08:56:32 -080039 cipher = Cipher(
40 cipher_factory(**params),
41 mode_factory(**params),
42 backend=backend
43 )
44 encryptor = cipher.encryptor()
45 actual_ciphertext = encryptor.update(binascii.unhexlify(plaintext))
46 actual_ciphertext += encryptor.finalize()
47 assert actual_ciphertext == binascii.unhexlify(ciphertext)
48 decryptor = cipher.decryptor()
49 actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext))
50 actual_plaintext += decryptor.finalize()
51 assert actual_plaintext == binascii.unhexlify(plaintext)
52
53
Paul Kehrer22e80cb2013-11-20 21:27:00 -060054def generate_aead_test(param_loader, path, file_names, cipher_factory,
Paul Kehrer783479c2013-12-26 21:08:45 -060055 mode_factory):
Alex Gaynore5c5eec2013-12-13 08:10:20 -080056 all_params = _load_all_params(path, file_names, param_loader)
57
58 @pytest.mark.parametrize("params", all_params)
59 def test_aead(self, backend, params):
Paul Kehrer783479c2013-12-26 21:08:45 -060060 aead_test(backend, cipher_factory, mode_factory, params)
Alex Gaynore5c5eec2013-12-13 08:10:20 -080061
Paul Kehrer22e80cb2013-11-20 21:27:00 -060062 return test_aead
63
64
Paul Kehrer783479c2013-12-26 21:08:45 -060065def aead_test(backend, cipher_factory, mode_factory, params):
Alex Gaynor21919e22013-12-13 08:56:32 -080066 if params.get("pt") is not None:
Paul Kehrera620b7d2013-12-20 22:59:02 -060067 plaintext = params["pt"]
68 ciphertext = params["ct"]
69 aad = params["aad"]
Alex Gaynor21919e22013-12-13 08:56:32 -080070 if params.get("fail") is True:
71 cipher = Cipher(
72 cipher_factory(binascii.unhexlify(params["key"])),
73 mode_factory(binascii.unhexlify(params["iv"]),
74 binascii.unhexlify(params["tag"])),
75 backend
76 )
77 decryptor = cipher.decryptor()
78 decryptor.authenticate_additional_data(binascii.unhexlify(aad))
79 actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext))
80 with pytest.raises(InvalidTag):
81 decryptor.finalize()
82 else:
83 cipher = Cipher(
84 cipher_factory(binascii.unhexlify(params["key"])),
85 mode_factory(binascii.unhexlify(params["iv"]), None),
86 backend
87 )
88 encryptor = cipher.encryptor()
89 encryptor.authenticate_additional_data(binascii.unhexlify(aad))
90 actual_ciphertext = encryptor.update(binascii.unhexlify(plaintext))
91 actual_ciphertext += encryptor.finalize()
92 tag_len = len(params["tag"])
93 assert binascii.hexlify(encryptor.tag)[:tag_len] == params["tag"]
94 cipher = Cipher(
95 cipher_factory(binascii.unhexlify(params["key"])),
96 mode_factory(binascii.unhexlify(params["iv"]),
97 binascii.unhexlify(params["tag"])),
98 backend
99 )
100 decryptor = cipher.decryptor()
101 decryptor.authenticate_additional_data(binascii.unhexlify(aad))
102 actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext))
103 actual_plaintext += decryptor.finalize()
104 assert actual_plaintext == binascii.unhexlify(plaintext)
105
106
Paul Kehrer4da28c32013-11-07 07:50:17 +0800107def generate_stream_encryption_test(param_loader, path, file_names,
Paul Kehrer783479c2013-12-26 21:08:45 -0600108 cipher_factory):
Alex Gaynore5c5eec2013-12-13 08:10:20 -0800109 all_params = _load_all_params(path, file_names, param_loader)
110
111 @pytest.mark.parametrize("params", all_params)
112 def test_stream_encryption(self, backend, params):
Paul Kehrer783479c2013-12-26 21:08:45 -0600113 stream_encryption_test(backend, cipher_factory, params)
Paul Kehrer4da28c32013-11-07 07:50:17 +0800114 return test_stream_encryption
115
116
Paul Kehrer783479c2013-12-26 21:08:45 -0600117def stream_encryption_test(backend, cipher_factory, params):
Paul Kehrera620b7d2013-12-20 22:59:02 -0600118 plaintext = params["plaintext"]
119 ciphertext = params["ciphertext"]
120 offset = params["offset"]
Alex Gaynor21919e22013-12-13 08:56:32 -0800121 cipher = Cipher(cipher_factory(**params), None, backend=backend)
122 encryptor = cipher.encryptor()
123 # throw away offset bytes
124 encryptor.update(b"\x00" * int(offset))
125 actual_ciphertext = encryptor.update(binascii.unhexlify(plaintext))
126 actual_ciphertext += encryptor.finalize()
127 assert actual_ciphertext == binascii.unhexlify(ciphertext)
128 decryptor = cipher.decryptor()
129 decryptor.update(b"\x00" * int(offset))
130 actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext))
131 actual_plaintext += decryptor.finalize()
132 assert actual_plaintext == binascii.unhexlify(plaintext)
133
134
Paul Kehrer783479c2013-12-26 21:08:45 -0600135def generate_hash_test(param_loader, path, file_names, hash_cls):
Alex Gaynore5c5eec2013-12-13 08:10:20 -0800136 all_params = _load_all_params(path, file_names, param_loader)
Paul Kehrer4da28c32013-11-07 07:50:17 +0800137
Alex Gaynore5c5eec2013-12-13 08:10:20 -0800138 @pytest.mark.parametrize("params", all_params)
139 def test_hash(self, backend, params):
Paul Kehrer783479c2013-12-26 21:08:45 -0600140 hash_test(backend, hash_cls, params)
Paul Kehrerbde6fb52013-10-18 18:08:49 -0500141 return test_hash
142
143
Paul Kehrer783479c2013-12-26 21:08:45 -0600144def hash_test(backend, algorithm, params):
Alex Gaynor36e651c2014-01-27 10:08:35 -0800145 msg, md = params
Alex Gaynor21919e22013-12-13 08:56:32 -0800146 m = hashes.Hash(algorithm, backend=backend)
147 m.update(binascii.unhexlify(msg))
148 expected_md = md.replace(" ", "").lower().encode("ascii")
149 assert m.finalize() == binascii.unhexlify(expected_md)
150
151
Paul Kehrerb078d8e2013-12-27 16:33:14 -0600152def generate_base_hash_test(algorithm, digest_size, block_size):
153 def test_base_hash(self, backend):
154 base_hash_test(backend, algorithm, digest_size, block_size)
155 return test_base_hash
156
157
Paul Kehrer783479c2013-12-26 21:08:45 -0600158def base_hash_test(backend, algorithm, digest_size, block_size):
Alex Gaynor21919e22013-12-13 08:56:32 -0800159 m = hashes.Hash(algorithm, backend=backend)
160 assert m.algorithm.digest_size == digest_size
161 assert m.algorithm.block_size == block_size
162 m_copy = m.copy()
163 assert m != m_copy
164 assert m._ctx != m_copy._ctx
165
166 m.update(b"abc")
167 copy = m.copy()
168 copy.update(b"123")
169 m.update(b"123")
170 assert copy.finalize() == m.finalize()
171
172
Paul Kehrer783479c2013-12-26 21:08:45 -0600173def generate_long_string_hash_test(hash_factory, md):
Alex Gaynore5c5eec2013-12-13 08:10:20 -0800174 def test_long_string_hash(self, backend):
Paul Kehrer783479c2013-12-26 21:08:45 -0600175 long_string_hash_test(backend, hash_factory, md)
Paul Kehrerc1794072013-10-18 21:42:57 -0500176 return test_long_string_hash
177
178
Paul Kehrer783479c2013-12-26 21:08:45 -0600179def long_string_hash_test(backend, algorithm, md):
Alex Gaynor21919e22013-12-13 08:56:32 -0800180 m = hashes.Hash(algorithm, backend=backend)
181 m.update(b"a" * 1000000)
182 assert m.finalize() == binascii.unhexlify(md.lower().encode("ascii"))
183
184
Paul Kehrerb078d8e2013-12-27 16:33:14 -0600185def generate_base_hmac_test(hash_cls):
186 def test_base_hmac(self, backend):
187 base_hmac_test(backend, hash_cls)
188 return test_base_hmac
189
190
191def base_hmac_test(backend, algorithm):
192 key = b"ab"
193 h = hmac.HMAC(binascii.unhexlify(key), algorithm, backend=backend)
194 h_copy = h.copy()
195 assert h != h_copy
196 assert h._ctx != h_copy._ctx
197
198
Paul Kehrer783479c2013-12-26 21:08:45 -0600199def generate_hmac_test(param_loader, path, file_names, algorithm):
Alex Gaynore5c5eec2013-12-13 08:10:20 -0800200 all_params = _load_all_params(path, file_names, param_loader)
Paul Kehrer0317b042013-10-28 17:34:27 -0500201
Alex Gaynore5c5eec2013-12-13 08:10:20 -0800202 @pytest.mark.parametrize("params", all_params)
203 def test_hmac(self, backend, params):
Paul Kehrer783479c2013-12-26 21:08:45 -0600204 hmac_test(backend, algorithm, params)
Paul Kehrer0317b042013-10-28 17:34:27 -0500205 return test_hmac
206
207
Paul Kehrer783479c2013-12-26 21:08:45 -0600208def hmac_test(backend, algorithm, params):
Alex Gaynor36e651c2014-01-27 10:08:35 -0800209 msg, md, key = params
Alex Gaynor21919e22013-12-13 08:56:32 -0800210 h = hmac.HMAC(binascii.unhexlify(key), algorithm, backend=backend)
211 h.update(binascii.unhexlify(msg))
212 assert h.finalize() == binascii.unhexlify(md.encode("ascii"))
Paul Kehrer0317b042013-10-28 17:34:27 -0500213
Alex Gaynor21919e22013-12-13 08:56:32 -0800214
Paul Kehrer1050ddf2014-01-27 21:04:03 -0600215def generate_pbkdf2_test(param_loader, path, file_names, algorithm):
216 all_params = _load_all_params(path, file_names, param_loader)
217
218 @pytest.mark.parametrize("params", all_params)
219 def test_pbkdf2(self, backend, params):
220 pbkdf2_test(backend, algorithm, params)
221 return test_pbkdf2
222
223
224def pbkdf2_test(backend, algorithm, params):
225 # Password and salt can contain \0, which should be loaded as a null char.
226 # The NIST loader loads them as literal strings so we replace with the
227 # proper value.
228 kdf = PBKDF2(
229 algorithm,
230 int(params["length"]),
231 params["salt"],
232 int(params["iterations"]),
233 backend
234 )
235 derived_key = kdf.derive(params["password"])
236 assert binascii.hexlify(derived_key) == params["derived_key"]
237
238
Paul Kehrer783479c2013-12-26 21:08:45 -0600239def generate_aead_exception_test(cipher_factory, mode_factory):
Alex Gaynor21919e22013-12-13 08:56:32 -0800240 def test_aead_exception(self, backend):
Paul Kehrer783479c2013-12-26 21:08:45 -0600241 aead_exception_test(backend, cipher_factory, mode_factory)
Paul Kehrerce9c6112013-11-22 14:10:59 -0600242 return test_aead_exception
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600243
244
Paul Kehrer783479c2013-12-26 21:08:45 -0600245def aead_exception_test(backend, cipher_factory, mode_factory):
Alex Gaynor21919e22013-12-13 08:56:32 -0800246 cipher = Cipher(
247 cipher_factory(binascii.unhexlify(b"0" * 32)),
248 mode_factory(binascii.unhexlify(b"0" * 24)),
249 backend
250 )
251 encryptor = cipher.encryptor()
252 encryptor.update(b"a" * 16)
253 with pytest.raises(NotYetFinalized):
254 encryptor.tag
255 with pytest.raises(AlreadyUpdated):
256 encryptor.authenticate_additional_data(b"b" * 16)
257 encryptor.finalize()
258 with pytest.raises(AlreadyFinalized):
259 encryptor.authenticate_additional_data(b"b" * 16)
260 with pytest.raises(AlreadyFinalized):
261 encryptor.update(b"b" * 16)
262 with pytest.raises(AlreadyFinalized):
263 encryptor.finalize()
264 cipher = Cipher(
265 cipher_factory(binascii.unhexlify(b"0" * 32)),
266 mode_factory(binascii.unhexlify(b"0" * 24), b"0" * 16),
267 backend
268 )
269 decryptor = cipher.decryptor()
270 decryptor.update(b"a" * 16)
271 with pytest.raises(AttributeError):
272 decryptor.tag
Paul Kehrerb91221d2013-12-04 17:56:40 -0600273
Alex Gaynor21919e22013-12-13 08:56:32 -0800274
Paul Kehrer783479c2013-12-26 21:08:45 -0600275def generate_aead_tag_exception_test(cipher_factory, mode_factory):
Alex Gaynor21919e22013-12-13 08:56:32 -0800276 def test_aead_tag_exception(self, backend):
Paul Kehrer783479c2013-12-26 21:08:45 -0600277 aead_tag_exception_test(backend, cipher_factory, mode_factory)
Paul Kehrerb91221d2013-12-04 17:56:40 -0600278 return test_aead_tag_exception
Alex Gaynor21919e22013-12-13 08:56:32 -0800279
280
Paul Kehrer783479c2013-12-26 21:08:45 -0600281def aead_tag_exception_test(backend, cipher_factory, mode_factory):
Alex Gaynor21919e22013-12-13 08:56:32 -0800282 cipher = Cipher(
283 cipher_factory(binascii.unhexlify(b"0" * 32)),
284 mode_factory(binascii.unhexlify(b"0" * 24)),
285 backend
286 )
287 with pytest.raises(ValueError):
288 cipher.decryptor()
Alex Gaynor516b1ad2014-01-01 12:28:37 -0800289
Paul Kehrerf7b4ede2013-12-21 17:25:19 -0600290 with pytest.raises(ValueError):
Alex Gaynor516b1ad2014-01-01 12:28:37 -0800291 mode_factory(binascii.unhexlify(b"0" * 24), b"000")
292
Paul Kehrerf7b4ede2013-12-21 17:25:19 -0600293 cipher = Cipher(
294 cipher_factory(binascii.unhexlify(b"0" * 32)),
Alex Gaynor21919e22013-12-13 08:56:32 -0800295 mode_factory(binascii.unhexlify(b"0" * 24), b"0" * 16),
296 backend
297 )
298 with pytest.raises(ValueError):
299 cipher.encryptor()