blob: e6e97d1d59acc3755d6ac7f1820136a1c2601b34 [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
11
Alex Gaynor016eed12013-10-16 14:16:04 -070012def generate_encrypt_test(param_loader, path, file_names, cipher_factory,
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050013 mode_factory, only_if=lambda backend: True,
Alex Gaynor512dd692013-10-16 14:27:52 -070014 skip_message=None):
Alex Gaynorbd458ae2013-10-16 11:59:30 -070015 def test_encryption(self):
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050016 for backend in _ALL_BACKENDS:
Alex Gaynor512dd692013-10-16 14:27:52 -070017 for file_name in file_names:
18 for params in param_loader(os.path.join(path, file_name)):
19 yield (
20 encrypt_test,
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050021 backend,
Alex Gaynor512dd692013-10-16 14:27:52 -070022 cipher_factory,
23 mode_factory,
24 params,
25 only_if,
26 skip_message
27 )
Alex Gaynorbd458ae2013-10-16 11:59:30 -070028 return test_encryption
29
30
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050031def encrypt_test(backend, cipher_factory, mode_factory, params, only_if,
Alex Gaynor512dd692013-10-16 14:27:52 -070032 skip_message):
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050033 if not only_if(backend):
Alex Gaynor512dd692013-10-16 14:27:52 -070034 pytest.skip(skip_message)
Alex Gaynorbd458ae2013-10-16 11:59:30 -070035 plaintext = params.pop("plaintext")
36 ciphertext = params.pop("ciphertext")
Paul Kehrer21dde562013-11-06 12:22:09 +080037 cipher = Cipher(
Alex Gaynorbd458ae2013-10-16 11:59:30 -070038 cipher_factory(**params),
39 mode_factory(**params),
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050040 backend
Alex Gaynorbd458ae2013-10-16 11:59:30 -070041 )
Paul Kehrer620c2ae2013-10-19 14:12:04 -050042 encryptor = cipher.encryptor()
43 actual_ciphertext = encryptor.update(binascii.unhexlify(plaintext))
44 actual_ciphertext += encryptor.finalize()
Alex Gaynorfb39b3f2013-10-16 14:30:59 -070045 assert actual_ciphertext == binascii.unhexlify(ciphertext)
Paul Kehrer620c2ae2013-10-19 14:12:04 -050046 decryptor = cipher.decryptor()
47 actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext))
48 actual_plaintext += decryptor.finalize()
49 assert actual_plaintext == binascii.unhexlify(plaintext)
Paul Kehrerbde6fb52013-10-18 18:08:49 -050050
51
Paul Kehrerbb069c22013-10-18 19:51:01 -050052def generate_hash_test(param_loader, path, file_names, hash_cls,
Donald Stufft2acd77a2013-10-19 19:49:30 -040053 only_if=None, skip_message=None):
Paul Kehrerbde6fb52013-10-18 18:08:49 -050054 def test_hash(self):
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050055 for backend in _ALL_BACKENDS:
Paul Kehrerbde6fb52013-10-18 18:08:49 -050056 for file_name in file_names:
57 for params in param_loader(os.path.join(path, file_name)):
58 yield (
59 hash_test,
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050060 backend,
Paul Kehrerbb069c22013-10-18 19:51:01 -050061 hash_cls,
Paul Kehrerbde6fb52013-10-18 18:08:49 -050062 params,
63 only_if,
64 skip_message
65 )
66 return test_hash
67
68
David Reidbb0d3f02013-10-31 15:22:49 -070069def hash_test(backend, algorithm, params, only_if, skip_message):
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050070 if only_if is not None and not only_if(backend):
Paul Kehrerbde6fb52013-10-18 18:08:49 -050071 pytest.skip(skip_message)
72 msg = params[0]
73 md = params[1]
David Reidbb0d3f02013-10-31 15:22:49 -070074 m = hashes.Hash(algorithm, backend=backend)
Paul Kehrerbde6fb52013-10-18 18:08:49 -050075 m.update(binascii.unhexlify(msg))
David Reidc3d029f2013-10-31 14:06:14 -070076 expected_md = md.replace(" ", "").lower().encode("ascii")
77 assert m.finalize() == binascii.unhexlify(expected_md)
Paul Kehrerbde6fb52013-10-18 18:08:49 -050078
79
David Reid69aeb492013-10-30 11:35:37 -070080def generate_base_hash_test(algorithm, digest_size, block_size,
Donald Stufft2acd77a2013-10-19 19:49:30 -040081 only_if=None, skip_message=None):
Paul Kehrerbde6fb52013-10-18 18:08:49 -050082 def test_base_hash(self):
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050083 for backend in _ALL_BACKENDS:
Paul Kehrerbde6fb52013-10-18 18:08:49 -050084 yield (
85 base_hash_test,
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050086 backend,
David Reid69aeb492013-10-30 11:35:37 -070087 algorithm,
Paul Kehrerbde6fb52013-10-18 18:08:49 -050088 digest_size,
89 block_size,
90 only_if,
91 skip_message,
92 )
93 return test_base_hash
94
95
David Reid69aeb492013-10-30 11:35:37 -070096def base_hash_test(backend, algorithm, digest_size, block_size, only_if,
Paul Kehrerbde6fb52013-10-18 18:08:49 -050097 skip_message):
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050098 if only_if is not None and not only_if(backend):
Paul Kehrerbde6fb52013-10-18 18:08:49 -050099 pytest.skip(skip_message)
David Reid69aeb492013-10-30 11:35:37 -0700100
101 m = hashes.Hash(algorithm, backend=backend)
102 assert m.algorithm.digest_size == digest_size
103 assert m.algorithm.block_size == block_size
Paul Kehrerbde6fb52013-10-18 18:08:49 -0500104 m_copy = m.copy()
105 assert m != m_copy
106 assert m._ctx != m_copy._ctx
Paul Kehrerc1794072013-10-18 21:42:57 -0500107
108
Donald Stufft2acd77a2013-10-19 19:49:30 -0400109def generate_long_string_hash_test(hash_factory, md, only_if=None,
Paul Kehrerc1794072013-10-18 21:42:57 -0500110 skip_message=None):
111 def test_long_string_hash(self):
Paul Kehrerdb37d0e2013-10-22 20:13:06 -0500112 for backend in _ALL_BACKENDS:
Paul Kehrerc1794072013-10-18 21:42:57 -0500113 yield(
114 long_string_hash_test,
Paul Kehrerdb37d0e2013-10-22 20:13:06 -0500115 backend,
Paul Kehrerc1794072013-10-18 21:42:57 -0500116 hash_factory,
117 md,
118 only_if,
119 skip_message
120 )
121 return test_long_string_hash
122
123
David Reid69aeb492013-10-30 11:35:37 -0700124def long_string_hash_test(backend, algorithm, md, only_if, skip_message):
Paul Kehrerdb37d0e2013-10-22 20:13:06 -0500125 if only_if is not None and not only_if(backend):
Paul Kehrerc1794072013-10-18 21:42:57 -0500126 pytest.skip(skip_message)
David Reid69aeb492013-10-30 11:35:37 -0700127 m = hashes.Hash(algorithm, backend=backend)
Paul Kehrerc1794072013-10-18 21:42:57 -0500128 m.update(b"a" * 1000000)
David Reidc3d029f2013-10-31 14:06:14 -0700129 assert m.finalize() == binascii.unhexlify(md.lower().encode("ascii"))
Paul Kehrer0317b042013-10-28 17:34:27 -0500130
131
David Reide3960f62013-11-01 14:52:16 -0700132def generate_hmac_test(param_loader, path, file_names, algorithm,
Paul Kehrer0317b042013-10-28 17:34:27 -0500133 only_if=None, skip_message=None):
134 def test_hmac(self):
135 for backend in _ALL_BACKENDS:
136 for file_name in file_names:
137 for params in param_loader(os.path.join(path, file_name)):
138 yield (
139 hmac_test,
140 backend,
David Reide3960f62013-11-01 14:52:16 -0700141 algorithm,
Paul Kehrer0317b042013-10-28 17:34:27 -0500142 params,
143 only_if,
144 skip_message
145 )
146 return test_hmac
147
148
David Reide3960f62013-11-01 14:52:16 -0700149def hmac_test(backend, algorithm, params, only_if, skip_message):
Paul Kehrer0317b042013-10-28 17:34:27 -0500150 if only_if is not None and not only_if(backend):
151 pytest.skip(skip_message)
152 msg = params[0]
153 md = params[1]
154 key = params[2]
David Reide3960f62013-11-01 14:52:16 -0700155 h = hmac.HMAC(binascii.unhexlify(key), algorithm)
Paul Kehrer0317b042013-10-28 17:34:27 -0500156 h.update(binascii.unhexlify(msg))
David Reid753ae192013-11-01 16:28:41 -0700157 assert h.finalize() == binascii.unhexlify(md.encode("ascii"))
Paul Kehrer0317b042013-10-28 17:34:27 -0500158
159
160def generate_base_hmac_test(hash_cls, only_if=None, skip_message=None):
161 def test_base_hmac(self):
162 for backend in _ALL_BACKENDS:
163 yield (
164 base_hmac_test,
165 backend,
166 hash_cls,
167 only_if,
168 skip_message,
169 )
170 return test_base_hmac
171
172
David Reide3960f62013-11-01 14:52:16 -0700173def base_hmac_test(backend, algorithm, only_if, skip_message):
Paul Kehrer0317b042013-10-28 17:34:27 -0500174 if only_if is not None and not only_if(backend):
175 pytest.skip(skip_message)
176 key = b"ab"
David Reide3960f62013-11-01 14:52:16 -0700177 h = hmac.HMAC(binascii.unhexlify(key), algorithm)
Paul Kehrer0317b042013-10-28 17:34:27 -0500178 h_copy = h.copy()
179 assert h != h_copy
180 assert h._ctx != h_copy._ctx