blob: 0f975950e54e15ad4aacba3ff6c1c72bb2e80ce1 [file] [log] [blame]
Alex Gaynorbd458ae2013-10-16 11:59:30 -07001import binascii
2import os
3
4import pytest
5
Donald Stufftce0d7812013-10-27 16:52:33 -04006from cryptography.hazmat.bindings import _ALL_BACKENDS
David Reid69aeb492013-10-30 11:35:37 -07007from cryptography.hazmat.primitives import hashes
Paul Kehrer0317b042013-10-28 17:34:27 -05008from cryptography.hazmat.primitives import hmac
Paul Kehrer21dde562013-11-06 12:22:09 +08009from cryptography.hazmat.primitives.ciphers import Cipher
Alex Gaynorbd458ae2013-10-16 11:59:30 -070010
Paul Kehrerf7f6a9f2013-11-11 20:43:52 -060011from ...utils import load_vectors_from_file
12
Alex Gaynorbd458ae2013-10-16 11:59:30 -070013
Alex Gaynor016eed12013-10-16 14:16:04 -070014def generate_encrypt_test(param_loader, path, file_names, cipher_factory,
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050015 mode_factory, only_if=lambda backend: True,
Alex Gaynor512dd692013-10-16 14:27:52 -070016 skip_message=None):
Alex Gaynorbd458ae2013-10-16 11:59:30 -070017 def test_encryption(self):
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050018 for backend in _ALL_BACKENDS:
Alex Gaynor512dd692013-10-16 14:27:52 -070019 for file_name in file_names:
Paul Kehrerf7f6a9f2013-11-11 20:43:52 -060020 for params in load_vectors_from_file(
21 os.path.join(path, file_name),
22 param_loader
23 ):
Alex Gaynor512dd692013-10-16 14:27:52 -070024 yield (
25 encrypt_test,
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050026 backend,
Alex Gaynor512dd692013-10-16 14:27:52 -070027 cipher_factory,
28 mode_factory,
29 params,
30 only_if,
31 skip_message
32 )
Alex Gaynorbd458ae2013-10-16 11:59:30 -070033 return test_encryption
34
35
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050036def encrypt_test(backend, cipher_factory, mode_factory, params, only_if,
Alex Gaynor512dd692013-10-16 14:27:52 -070037 skip_message):
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050038 if not only_if(backend):
Alex Gaynor512dd692013-10-16 14:27:52 -070039 pytest.skip(skip_message)
Alex Gaynorbd458ae2013-10-16 11:59:30 -070040 plaintext = params.pop("plaintext")
41 ciphertext = params.pop("ciphertext")
Paul Kehrer21dde562013-11-06 12:22:09 +080042 cipher = Cipher(
Alex Gaynorbd458ae2013-10-16 11:59:30 -070043 cipher_factory(**params),
44 mode_factory(**params),
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050045 backend
Alex Gaynorbd458ae2013-10-16 11:59:30 -070046 )
Paul Kehrer620c2ae2013-10-19 14:12:04 -050047 encryptor = cipher.encryptor()
48 actual_ciphertext = encryptor.update(binascii.unhexlify(plaintext))
49 actual_ciphertext += encryptor.finalize()
Alex Gaynorfb39b3f2013-10-16 14:30:59 -070050 assert actual_ciphertext == binascii.unhexlify(ciphertext)
Paul Kehrer620c2ae2013-10-19 14:12:04 -050051 decryptor = cipher.decryptor()
52 actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext))
53 actual_plaintext += decryptor.finalize()
54 assert actual_plaintext == binascii.unhexlify(plaintext)
Paul Kehrerbde6fb52013-10-18 18:08:49 -050055
56
Paul Kehrer4da28c32013-11-07 07:50:17 +080057def generate_stream_encryption_test(param_loader, path, file_names,
58 cipher_factory, only_if=None,
59 skip_message=None):
60 def test_stream_encryption(self):
61 for backend in _ALL_BACKENDS:
62 for file_name in file_names:
Paul Kehrerf7f6a9f2013-11-11 20:43:52 -060063 for params in load_vectors_from_file(
64 os.path.join(path, file_name),
65 param_loader
66 ):
Paul Kehrer4da28c32013-11-07 07:50:17 +080067 yield (
68 stream_encryption_test,
69 backend,
70 cipher_factory,
71 params,
72 only_if,
73 skip_message
74 )
75 return test_stream_encryption
76
77
78def stream_encryption_test(backend, cipher_factory, params, only_if,
79 skip_message):
80 if not only_if(backend):
81 pytest.skip(skip_message)
82 plaintext = params.pop("plaintext")
83 ciphertext = params.pop("ciphertext")
84 offset = params.pop("offset")
85 cipher = Cipher(cipher_factory(**params), None, backend)
86 encryptor = cipher.encryptor()
87 # throw away offset bytes
88 encryptor.update(b"\x00" * int(offset))
89 actual_ciphertext = encryptor.update(binascii.unhexlify(plaintext))
90 actual_ciphertext += encryptor.finalize()
91 assert actual_ciphertext == binascii.unhexlify(ciphertext)
92 decryptor = cipher.decryptor()
93 decryptor.update(b"\x00" * int(offset))
94 actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext))
95 actual_plaintext += decryptor.finalize()
96 assert actual_plaintext == binascii.unhexlify(plaintext)
97
98
Paul Kehrerbb069c22013-10-18 19:51:01 -050099def generate_hash_test(param_loader, path, file_names, hash_cls,
Donald Stufft2acd77a2013-10-19 19:49:30 -0400100 only_if=None, skip_message=None):
Paul Kehrerbde6fb52013-10-18 18:08:49 -0500101 def test_hash(self):
Paul Kehrerdb37d0e2013-10-22 20:13:06 -0500102 for backend in _ALL_BACKENDS:
Paul Kehrerbde6fb52013-10-18 18:08:49 -0500103 for file_name in file_names:
Paul Kehrerf7f6a9f2013-11-11 20:43:52 -0600104 for params in load_vectors_from_file(
105 os.path.join(path, file_name),
106 param_loader
107 ):
Paul Kehrerbde6fb52013-10-18 18:08:49 -0500108 yield (
109 hash_test,
Paul Kehrerdb37d0e2013-10-22 20:13:06 -0500110 backend,
Paul Kehrerbb069c22013-10-18 19:51:01 -0500111 hash_cls,
Paul Kehrerbde6fb52013-10-18 18:08:49 -0500112 params,
113 only_if,
114 skip_message
115 )
116 return test_hash
117
118
David Reidbb0d3f02013-10-31 15:22:49 -0700119def hash_test(backend, algorithm, params, only_if, skip_message):
Paul Kehrerdb37d0e2013-10-22 20:13:06 -0500120 if only_if is not None and not only_if(backend):
Paul Kehrerbde6fb52013-10-18 18:08:49 -0500121 pytest.skip(skip_message)
122 msg = params[0]
123 md = params[1]
David Reidbb0d3f02013-10-31 15:22:49 -0700124 m = hashes.Hash(algorithm, backend=backend)
Paul Kehrerbde6fb52013-10-18 18:08:49 -0500125 m.update(binascii.unhexlify(msg))
David Reidc3d029f2013-10-31 14:06:14 -0700126 expected_md = md.replace(" ", "").lower().encode("ascii")
127 assert m.finalize() == binascii.unhexlify(expected_md)
Paul Kehrerbde6fb52013-10-18 18:08:49 -0500128
129
David Reid69aeb492013-10-30 11:35:37 -0700130def generate_base_hash_test(algorithm, digest_size, block_size,
Donald Stufft2acd77a2013-10-19 19:49:30 -0400131 only_if=None, skip_message=None):
Paul Kehrerbde6fb52013-10-18 18:08:49 -0500132 def test_base_hash(self):
Paul Kehrerdb37d0e2013-10-22 20:13:06 -0500133 for backend in _ALL_BACKENDS:
Paul Kehrerbde6fb52013-10-18 18:08:49 -0500134 yield (
135 base_hash_test,
Paul Kehrerdb37d0e2013-10-22 20:13:06 -0500136 backend,
David Reid69aeb492013-10-30 11:35:37 -0700137 algorithm,
Paul Kehrerbde6fb52013-10-18 18:08:49 -0500138 digest_size,
139 block_size,
140 only_if,
141 skip_message,
142 )
143 return test_base_hash
144
145
David Reid69aeb492013-10-30 11:35:37 -0700146def base_hash_test(backend, algorithm, digest_size, block_size, only_if,
Paul Kehrerbde6fb52013-10-18 18:08:49 -0500147 skip_message):
Paul Kehrerdb37d0e2013-10-22 20:13:06 -0500148 if only_if is not None and not only_if(backend):
Paul Kehrerbde6fb52013-10-18 18:08:49 -0500149 pytest.skip(skip_message)
David Reid69aeb492013-10-30 11:35:37 -0700150
151 m = hashes.Hash(algorithm, backend=backend)
152 assert m.algorithm.digest_size == digest_size
153 assert m.algorithm.block_size == block_size
Paul Kehrerbde6fb52013-10-18 18:08:49 -0500154 m_copy = m.copy()
155 assert m != m_copy
156 assert m._ctx != m_copy._ctx
Paul Kehrerc1794072013-10-18 21:42:57 -0500157
158
Donald Stufft2acd77a2013-10-19 19:49:30 -0400159def generate_long_string_hash_test(hash_factory, md, only_if=None,
Paul Kehrerc1794072013-10-18 21:42:57 -0500160 skip_message=None):
161 def test_long_string_hash(self):
Paul Kehrerdb37d0e2013-10-22 20:13:06 -0500162 for backend in _ALL_BACKENDS:
Paul Kehrerc1794072013-10-18 21:42:57 -0500163 yield(
164 long_string_hash_test,
Paul Kehrerdb37d0e2013-10-22 20:13:06 -0500165 backend,
Paul Kehrerc1794072013-10-18 21:42:57 -0500166 hash_factory,
167 md,
168 only_if,
169 skip_message
170 )
171 return test_long_string_hash
172
173
David Reid69aeb492013-10-30 11:35:37 -0700174def long_string_hash_test(backend, algorithm, md, only_if, skip_message):
Paul Kehrerdb37d0e2013-10-22 20:13:06 -0500175 if only_if is not None and not only_if(backend):
Paul Kehrerc1794072013-10-18 21:42:57 -0500176 pytest.skip(skip_message)
David Reid69aeb492013-10-30 11:35:37 -0700177 m = hashes.Hash(algorithm, backend=backend)
Paul Kehrerc1794072013-10-18 21:42:57 -0500178 m.update(b"a" * 1000000)
David Reidc3d029f2013-10-31 14:06:14 -0700179 assert m.finalize() == binascii.unhexlify(md.lower().encode("ascii"))
Paul Kehrer0317b042013-10-28 17:34:27 -0500180
181
David Reide3960f62013-11-01 14:52:16 -0700182def generate_hmac_test(param_loader, path, file_names, algorithm,
Paul Kehrer0317b042013-10-28 17:34:27 -0500183 only_if=None, skip_message=None):
184 def test_hmac(self):
185 for backend in _ALL_BACKENDS:
186 for file_name in file_names:
Paul Kehrerf7f6a9f2013-11-11 20:43:52 -0600187 for params in load_vectors_from_file(
188 os.path.join(path, file_name),
189 param_loader
190 ):
Paul Kehrer0317b042013-10-28 17:34:27 -0500191 yield (
192 hmac_test,
193 backend,
David Reide3960f62013-11-01 14:52:16 -0700194 algorithm,
Paul Kehrer0317b042013-10-28 17:34:27 -0500195 params,
196 only_if,
197 skip_message
198 )
199 return test_hmac
200
201
David Reide3960f62013-11-01 14:52:16 -0700202def hmac_test(backend, algorithm, params, only_if, skip_message):
Paul Kehrer0317b042013-10-28 17:34:27 -0500203 if only_if is not None and not only_if(backend):
204 pytest.skip(skip_message)
205 msg = params[0]
206 md = params[1]
207 key = params[2]
David Reide3960f62013-11-01 14:52:16 -0700208 h = hmac.HMAC(binascii.unhexlify(key), algorithm)
Paul Kehrer0317b042013-10-28 17:34:27 -0500209 h.update(binascii.unhexlify(msg))
David Reid753ae192013-11-01 16:28:41 -0700210 assert h.finalize() == binascii.unhexlify(md.encode("ascii"))
Paul Kehrer0317b042013-10-28 17:34:27 -0500211
212
213def generate_base_hmac_test(hash_cls, only_if=None, skip_message=None):
214 def test_base_hmac(self):
215 for backend in _ALL_BACKENDS:
216 yield (
217 base_hmac_test,
218 backend,
219 hash_cls,
220 only_if,
221 skip_message,
222 )
223 return test_base_hmac
224
225
David Reide3960f62013-11-01 14:52:16 -0700226def base_hmac_test(backend, algorithm, only_if, skip_message):
Paul Kehrer0317b042013-10-28 17:34:27 -0500227 if only_if is not None and not only_if(backend):
228 pytest.skip(skip_message)
229 key = b"ab"
David Reide3960f62013-11-01 14:52:16 -0700230 h = hmac.HMAC(binascii.unhexlify(key), algorithm)
Paul Kehrer0317b042013-10-28 17:34:27 -0500231 h_copy = h.copy()
232 assert h != h_copy
233 assert h._ctx != h_copy._ctx