blob: 3c9627522a011a5a9aed90a1549aee028b2ad753 [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 Kehrer783479c2013-12-26 21:08:45 -0600152def base_hash_test(backend, algorithm, digest_size, block_size):
Alex Gaynor21919e22013-12-13 08:56:32 -0800153 m = hashes.Hash(algorithm, backend=backend)
154 assert m.algorithm.digest_size == digest_size
155 assert m.algorithm.block_size == block_size
156 m_copy = m.copy()
157 assert m != m_copy
158 assert m._ctx != m_copy._ctx
159
160 m.update(b"abc")
161 copy = m.copy()
162 copy.update(b"123")
163 m.update(b"123")
164 assert copy.finalize() == m.finalize()
165
166
Paul Kehrer783479c2013-12-26 21:08:45 -0600167def generate_long_string_hash_test(hash_factory, md):
Alex Gaynore5c5eec2013-12-13 08:10:20 -0800168 def test_long_string_hash(self, backend):
Paul Kehrer783479c2013-12-26 21:08:45 -0600169 long_string_hash_test(backend, hash_factory, md)
Paul Kehrerc1794072013-10-18 21:42:57 -0500170 return test_long_string_hash
171
172
Paul Kehrer783479c2013-12-26 21:08:45 -0600173def long_string_hash_test(backend, algorithm, md):
Alex Gaynor21919e22013-12-13 08:56:32 -0800174 m = hashes.Hash(algorithm, backend=backend)
175 m.update(b"a" * 1000000)
176 assert m.finalize() == binascii.unhexlify(md.lower().encode("ascii"))
177
178
Paul Kehrer783479c2013-12-26 21:08:45 -0600179def generate_hmac_test(param_loader, path, file_names, algorithm):
Alex Gaynore5c5eec2013-12-13 08:10:20 -0800180 all_params = _load_all_params(path, file_names, param_loader)
Paul Kehrer0317b042013-10-28 17:34:27 -0500181
Alex Gaynore5c5eec2013-12-13 08:10:20 -0800182 @pytest.mark.parametrize("params", all_params)
183 def test_hmac(self, backend, params):
Paul Kehrer783479c2013-12-26 21:08:45 -0600184 hmac_test(backend, algorithm, params)
Paul Kehrer0317b042013-10-28 17:34:27 -0500185 return test_hmac
186
187
Paul Kehrer783479c2013-12-26 21:08:45 -0600188def hmac_test(backend, algorithm, params):
Alex Gaynor21919e22013-12-13 08:56:32 -0800189 msg = params[0]
190 md = params[1]
191 key = params[2]
192 h = hmac.HMAC(binascii.unhexlify(key), algorithm, backend=backend)
193 h.update(binascii.unhexlify(msg))
194 assert h.finalize() == binascii.unhexlify(md.encode("ascii"))
Paul Kehrer0317b042013-10-28 17:34:27 -0500195
Alex Gaynor21919e22013-12-13 08:56:32 -0800196
Paul Kehrer783479c2013-12-26 21:08:45 -0600197def generate_aead_exception_test(cipher_factory, mode_factory):
Alex Gaynor21919e22013-12-13 08:56:32 -0800198 def test_aead_exception(self, backend):
Paul Kehrer783479c2013-12-26 21:08:45 -0600199 aead_exception_test(backend, cipher_factory, mode_factory)
Paul Kehrerce9c6112013-11-22 14:10:59 -0600200 return test_aead_exception
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600201
202
Paul Kehrer783479c2013-12-26 21:08:45 -0600203def aead_exception_test(backend, cipher_factory, mode_factory):
Alex Gaynor21919e22013-12-13 08:56:32 -0800204 cipher = Cipher(
205 cipher_factory(binascii.unhexlify(b"0" * 32)),
206 mode_factory(binascii.unhexlify(b"0" * 24)),
207 backend
208 )
209 encryptor = cipher.encryptor()
210 encryptor.update(b"a" * 16)
211 with pytest.raises(NotYetFinalized):
212 encryptor.tag
213 with pytest.raises(AlreadyUpdated):
214 encryptor.authenticate_additional_data(b"b" * 16)
215 encryptor.finalize()
216 with pytest.raises(AlreadyFinalized):
217 encryptor.authenticate_additional_data(b"b" * 16)
218 with pytest.raises(AlreadyFinalized):
219 encryptor.update(b"b" * 16)
220 with pytest.raises(AlreadyFinalized):
221 encryptor.finalize()
222 cipher = Cipher(
223 cipher_factory(binascii.unhexlify(b"0" * 32)),
224 mode_factory(binascii.unhexlify(b"0" * 24), b"0" * 16),
225 backend
226 )
227 decryptor = cipher.decryptor()
228 decryptor.update(b"a" * 16)
229 with pytest.raises(AttributeError):
230 decryptor.tag
Paul Kehrerb91221d2013-12-04 17:56:40 -0600231
Alex Gaynor21919e22013-12-13 08:56:32 -0800232
Paul Kehrer783479c2013-12-26 21:08:45 -0600233def generate_aead_tag_exception_test(cipher_factory, mode_factory):
Alex Gaynor21919e22013-12-13 08:56:32 -0800234 def test_aead_tag_exception(self, backend):
Paul Kehrer783479c2013-12-26 21:08:45 -0600235 aead_tag_exception_test(backend, cipher_factory, mode_factory)
Paul Kehrerb91221d2013-12-04 17:56:40 -0600236 return test_aead_tag_exception
Alex Gaynor21919e22013-12-13 08:56:32 -0800237
238
Paul Kehrer783479c2013-12-26 21:08:45 -0600239def aead_tag_exception_test(backend, cipher_factory, mode_factory):
Alex Gaynor21919e22013-12-13 08:56:32 -0800240 cipher = Cipher(
241 cipher_factory(binascii.unhexlify(b"0" * 32)),
242 mode_factory(binascii.unhexlify(b"0" * 24)),
243 backend
244 )
245 with pytest.raises(ValueError):
246 cipher.decryptor()
247 cipher = Cipher(
248 cipher_factory(binascii.unhexlify(b"0" * 32)),
Paul Kehrerf7b4ede2013-12-21 17:25:19 -0600249 mode_factory(binascii.unhexlify(b"0" * 24), b"000"),
250 backend
251 )
252 with pytest.raises(ValueError):
253 cipher.decryptor()
254 cipher = Cipher(
255 cipher_factory(binascii.unhexlify(b"0" * 32)),
Alex Gaynor21919e22013-12-13 08:56:32 -0800256 mode_factory(binascii.unhexlify(b"0" * 24), b"0" * 16),
257 backend
258 )
259 with pytest.raises(ValueError):
260 cipher.encryptor()