blob: cdcf84cb98f382f7909f7a39e05dbb40cf705b5c [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 Kehrer21dde562013-11-06 12:22:09 +08007from cryptography.hazmat.primitives.ciphers import Cipher
Paul Kehrer22e80cb2013-11-20 21:27:00 -06008from cryptography.exceptions import (
Paul Kehrera4bfc082013-11-22 19:57:37 -06009 AlreadyFinalized, NotYetFinalized, AlreadyUpdated, InvalidTag,
Paul Kehrer22e80cb2013-11-20 21:27:00 -060010)
Alex Gaynorbd458ae2013-10-16 11:59:30 -070011
Paul Kehrerf7f6a9f2013-11-11 20:43:52 -060012from ...utils import load_vectors_from_file
13
Alex Gaynorbd458ae2013-10-16 11:59:30 -070014
Alex Gaynore5c5eec2013-12-13 08:10:20 -080015def _load_all_params(path, file_names, param_loader):
16 all_params = []
17 for file_name in file_names:
18 all_params.extend(
19 load_vectors_from_file(os.path.join(path, file_name), param_loader)
20 )
21 return all_params
22
Alex Gaynor4eec0bb2013-12-13 09:12:19 -080023
Alex Gaynor016eed12013-10-16 14:16:04 -070024def generate_encrypt_test(param_loader, path, file_names, cipher_factory,
Paul Kehrer783479c2013-12-26 21:08:45 -060025 mode_factory):
Alex Gaynore5c5eec2013-12-13 08:10:20 -080026 all_params = _load_all_params(path, file_names, param_loader)
27
28 @pytest.mark.parametrize("params", all_params)
29 def test_encryption(self, backend, params):
Paul Kehrer783479c2013-12-26 21:08:45 -060030 encrypt_test(backend, cipher_factory, mode_factory, params)
Alex Gaynore5c5eec2013-12-13 08:10:20 -080031
Alex Gaynorbd458ae2013-10-16 11:59:30 -070032 return test_encryption
33
34
Paul Kehrer783479c2013-12-26 21:08:45 -060035def encrypt_test(backend, cipher_factory, mode_factory, params):
Paul Kehrera620b7d2013-12-20 22:59:02 -060036 plaintext = params["plaintext"]
37 ciphertext = params["ciphertext"]
Alex Gaynor21919e22013-12-13 08:56:32 -080038 cipher = Cipher(
39 cipher_factory(**params),
40 mode_factory(**params),
41 backend=backend
42 )
43 encryptor = cipher.encryptor()
44 actual_ciphertext = encryptor.update(binascii.unhexlify(plaintext))
45 actual_ciphertext += encryptor.finalize()
46 assert actual_ciphertext == binascii.unhexlify(ciphertext)
47 decryptor = cipher.decryptor()
48 actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext))
49 actual_plaintext += decryptor.finalize()
50 assert actual_plaintext == binascii.unhexlify(plaintext)
51
52
Paul Kehrer22e80cb2013-11-20 21:27:00 -060053def generate_aead_test(param_loader, path, file_names, cipher_factory,
Paul Kehrer783479c2013-12-26 21:08:45 -060054 mode_factory):
Alex Gaynore5c5eec2013-12-13 08:10:20 -080055 all_params = _load_all_params(path, file_names, param_loader)
56
57 @pytest.mark.parametrize("params", all_params)
58 def test_aead(self, backend, params):
Paul Kehrer783479c2013-12-26 21:08:45 -060059 aead_test(backend, cipher_factory, mode_factory, params)
Alex Gaynore5c5eec2013-12-13 08:10:20 -080060
Paul Kehrer22e80cb2013-11-20 21:27:00 -060061 return test_aead
62
63
Paul Kehrer783479c2013-12-26 21:08:45 -060064def aead_test(backend, cipher_factory, mode_factory, params):
Alex Gaynor21919e22013-12-13 08:56:32 -080065 if params.get("pt") is not None:
Paul Kehrera620b7d2013-12-20 22:59:02 -060066 plaintext = params["pt"]
67 ciphertext = params["ct"]
68 aad = params["aad"]
Alex Gaynor21919e22013-12-13 08:56:32 -080069 if params.get("fail") is True:
70 cipher = Cipher(
71 cipher_factory(binascii.unhexlify(params["key"])),
72 mode_factory(binascii.unhexlify(params["iv"]),
73 binascii.unhexlify(params["tag"])),
74 backend
75 )
76 decryptor = cipher.decryptor()
77 decryptor.authenticate_additional_data(binascii.unhexlify(aad))
78 actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext))
79 with pytest.raises(InvalidTag):
80 decryptor.finalize()
81 else:
82 cipher = Cipher(
83 cipher_factory(binascii.unhexlify(params["key"])),
84 mode_factory(binascii.unhexlify(params["iv"]), None),
85 backend
86 )
87 encryptor = cipher.encryptor()
88 encryptor.authenticate_additional_data(binascii.unhexlify(aad))
89 actual_ciphertext = encryptor.update(binascii.unhexlify(plaintext))
90 actual_ciphertext += encryptor.finalize()
91 tag_len = len(params["tag"])
92 assert binascii.hexlify(encryptor.tag)[:tag_len] == params["tag"]
93 cipher = Cipher(
94 cipher_factory(binascii.unhexlify(params["key"])),
95 mode_factory(binascii.unhexlify(params["iv"]),
96 binascii.unhexlify(params["tag"])),
97 backend
98 )
99 decryptor = cipher.decryptor()
100 decryptor.authenticate_additional_data(binascii.unhexlify(aad))
101 actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext))
102 actual_plaintext += decryptor.finalize()
103 assert actual_plaintext == binascii.unhexlify(plaintext)
104
105
Paul Kehrer4da28c32013-11-07 07:50:17 +0800106def generate_stream_encryption_test(param_loader, path, file_names,
Paul Kehrer783479c2013-12-26 21:08:45 -0600107 cipher_factory):
Alex Gaynore5c5eec2013-12-13 08:10:20 -0800108 all_params = _load_all_params(path, file_names, param_loader)
109
110 @pytest.mark.parametrize("params", all_params)
111 def test_stream_encryption(self, backend, params):
Paul Kehrer783479c2013-12-26 21:08:45 -0600112 stream_encryption_test(backend, cipher_factory, params)
Paul Kehrer4da28c32013-11-07 07:50:17 +0800113 return test_stream_encryption
114
115
Paul Kehrer783479c2013-12-26 21:08:45 -0600116def stream_encryption_test(backend, cipher_factory, params):
Paul Kehrera620b7d2013-12-20 22:59:02 -0600117 plaintext = params["plaintext"]
118 ciphertext = params["ciphertext"]
119 offset = params["offset"]
Alex Gaynor21919e22013-12-13 08:56:32 -0800120 cipher = Cipher(cipher_factory(**params), None, backend=backend)
121 encryptor = cipher.encryptor()
122 # throw away offset bytes
123 encryptor.update(b"\x00" * int(offset))
124 actual_ciphertext = encryptor.update(binascii.unhexlify(plaintext))
125 actual_ciphertext += encryptor.finalize()
126 assert actual_ciphertext == binascii.unhexlify(ciphertext)
127 decryptor = cipher.decryptor()
128 decryptor.update(b"\x00" * int(offset))
129 actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext))
130 actual_plaintext += decryptor.finalize()
131 assert actual_plaintext == binascii.unhexlify(plaintext)
132
133
Paul Kehrer783479c2013-12-26 21:08:45 -0600134def generate_hash_test(param_loader, path, file_names, hash_cls):
Alex Gaynore5c5eec2013-12-13 08:10:20 -0800135 all_params = _load_all_params(path, file_names, param_loader)
Paul Kehrer4da28c32013-11-07 07:50:17 +0800136
Alex Gaynore5c5eec2013-12-13 08:10:20 -0800137 @pytest.mark.parametrize("params", all_params)
138 def test_hash(self, backend, params):
Paul Kehrer783479c2013-12-26 21:08:45 -0600139 hash_test(backend, hash_cls, params)
Paul Kehrerbde6fb52013-10-18 18:08:49 -0500140 return test_hash
141
142
Paul Kehrer783479c2013-12-26 21:08:45 -0600143def hash_test(backend, algorithm, params):
Alex Gaynor21919e22013-12-13 08:56:32 -0800144 msg = params[0]
145 md = params[1]
146 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 Gaynor21919e22013-12-13 08:56:32 -0800209 msg = params[0]
210 md = params[1]
211 key = params[2]
212 h = hmac.HMAC(binascii.unhexlify(key), algorithm, backend=backend)
213 h.update(binascii.unhexlify(msg))
214 assert h.finalize() == binascii.unhexlify(md.encode("ascii"))
Paul Kehrer0317b042013-10-28 17:34:27 -0500215
Alex Gaynor21919e22013-12-13 08:56:32 -0800216
Paul Kehrer783479c2013-12-26 21:08:45 -0600217def generate_aead_exception_test(cipher_factory, mode_factory):
Alex Gaynor21919e22013-12-13 08:56:32 -0800218 def test_aead_exception(self, backend):
Paul Kehrer783479c2013-12-26 21:08:45 -0600219 aead_exception_test(backend, cipher_factory, mode_factory)
Paul Kehrerce9c6112013-11-22 14:10:59 -0600220 return test_aead_exception
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600221
222
Paul Kehrer783479c2013-12-26 21:08:45 -0600223def aead_exception_test(backend, cipher_factory, mode_factory):
Alex Gaynor21919e22013-12-13 08:56:32 -0800224 cipher = Cipher(
225 cipher_factory(binascii.unhexlify(b"0" * 32)),
226 mode_factory(binascii.unhexlify(b"0" * 24)),
227 backend
228 )
229 encryptor = cipher.encryptor()
230 encryptor.update(b"a" * 16)
231 with pytest.raises(NotYetFinalized):
232 encryptor.tag
233 with pytest.raises(AlreadyUpdated):
234 encryptor.authenticate_additional_data(b"b" * 16)
235 encryptor.finalize()
236 with pytest.raises(AlreadyFinalized):
237 encryptor.authenticate_additional_data(b"b" * 16)
238 with pytest.raises(AlreadyFinalized):
239 encryptor.update(b"b" * 16)
240 with pytest.raises(AlreadyFinalized):
241 encryptor.finalize()
242 cipher = Cipher(
243 cipher_factory(binascii.unhexlify(b"0" * 32)),
244 mode_factory(binascii.unhexlify(b"0" * 24), b"0" * 16),
245 backend
246 )
247 decryptor = cipher.decryptor()
248 decryptor.update(b"a" * 16)
249 with pytest.raises(AttributeError):
250 decryptor.tag
Paul Kehrerb91221d2013-12-04 17:56:40 -0600251
Alex Gaynor21919e22013-12-13 08:56:32 -0800252
Paul Kehrer783479c2013-12-26 21:08:45 -0600253def generate_aead_tag_exception_test(cipher_factory, mode_factory):
Alex Gaynor21919e22013-12-13 08:56:32 -0800254 def test_aead_tag_exception(self, backend):
Paul Kehrer783479c2013-12-26 21:08:45 -0600255 aead_tag_exception_test(backend, cipher_factory, mode_factory)
Paul Kehrerb91221d2013-12-04 17:56:40 -0600256 return test_aead_tag_exception
Alex Gaynor21919e22013-12-13 08:56:32 -0800257
258
Paul Kehrer783479c2013-12-26 21:08:45 -0600259def aead_tag_exception_test(backend, cipher_factory, mode_factory):
Alex Gaynor21919e22013-12-13 08:56:32 -0800260 cipher = Cipher(
261 cipher_factory(binascii.unhexlify(b"0" * 32)),
262 mode_factory(binascii.unhexlify(b"0" * 24)),
263 backend
264 )
265 with pytest.raises(ValueError):
266 cipher.decryptor()
267 cipher = Cipher(
268 cipher_factory(binascii.unhexlify(b"0" * 32)),
Paul Kehrerf7b4ede2013-12-21 17:25:19 -0600269 mode_factory(binascii.unhexlify(b"0" * 24), b"000"),
270 backend
271 )
272 with pytest.raises(ValueError):
273 cipher.decryptor()
274 cipher = Cipher(
275 cipher_factory(binascii.unhexlify(b"0" * 32)),
Alex Gaynor21919e22013-12-13 08:56:32 -0800276 mode_factory(binascii.unhexlify(b"0" * 24), b"0" * 16),
277 backend
278 )
279 with pytest.raises(ValueError):
280 cipher.encryptor()