blob: c02d1926f0d1b14339613abbcd2be455526a4f24 [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
Donald Stufftf04317a2013-10-27 16:44:30 -04009from cryptography.hazmat.primitives.block import BlockCipher
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")
37 cipher = BlockCipher(
38 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
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050069def hash_test(backend, hash_cls, params, only_if, skip_message):
70 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 Reid69aeb492013-10-30 11:35:37 -070074 m = hashes.Hash(hash_cls, backend=backend)
Paul Kehrerbde6fb52013-10-18 18:08:49 -050075 m.update(binascii.unhexlify(msg))
David Reid69aeb492013-10-30 11:35:37 -070076 assert m.finalize() == binascii.unhexlify(md.replace(" ", "").lower())
Paul Kehrerbde6fb52013-10-18 18:08:49 -050077
78
David Reid69aeb492013-10-30 11:35:37 -070079def generate_base_hash_test(algorithm, digest_size, block_size,
Donald Stufft2acd77a2013-10-19 19:49:30 -040080 only_if=None, skip_message=None):
Paul Kehrerbde6fb52013-10-18 18:08:49 -050081 def test_base_hash(self):
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050082 for backend in _ALL_BACKENDS:
Paul Kehrerbde6fb52013-10-18 18:08:49 -050083 yield (
84 base_hash_test,
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050085 backend,
David Reid69aeb492013-10-30 11:35:37 -070086 algorithm,
Paul Kehrerbde6fb52013-10-18 18:08:49 -050087 digest_size,
88 block_size,
89 only_if,
90 skip_message,
91 )
92 return test_base_hash
93
94
David Reid69aeb492013-10-30 11:35:37 -070095def base_hash_test(backend, algorithm, digest_size, block_size, only_if,
Paul Kehrerbde6fb52013-10-18 18:08:49 -050096 skip_message):
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050097 if only_if is not None and not only_if(backend):
Paul Kehrerbde6fb52013-10-18 18:08:49 -050098 pytest.skip(skip_message)
David Reid69aeb492013-10-30 11:35:37 -070099
100 m = hashes.Hash(algorithm, backend=backend)
101 assert m.algorithm.digest_size == digest_size
102 assert m.algorithm.block_size == block_size
Paul Kehrerbde6fb52013-10-18 18:08:49 -0500103 m_copy = m.copy()
104 assert m != m_copy
105 assert m._ctx != m_copy._ctx
Paul Kehrerc1794072013-10-18 21:42:57 -0500106
107
Donald Stufft2acd77a2013-10-19 19:49:30 -0400108def generate_long_string_hash_test(hash_factory, md, only_if=None,
Paul Kehrerc1794072013-10-18 21:42:57 -0500109 skip_message=None):
110 def test_long_string_hash(self):
Paul Kehrerdb37d0e2013-10-22 20:13:06 -0500111 for backend in _ALL_BACKENDS:
Paul Kehrerc1794072013-10-18 21:42:57 -0500112 yield(
113 long_string_hash_test,
Paul Kehrerdb37d0e2013-10-22 20:13:06 -0500114 backend,
Paul Kehrerc1794072013-10-18 21:42:57 -0500115 hash_factory,
116 md,
117 only_if,
118 skip_message
119 )
120 return test_long_string_hash
121
122
David Reid69aeb492013-10-30 11:35:37 -0700123def long_string_hash_test(backend, algorithm, md, only_if, skip_message):
Paul Kehrerdb37d0e2013-10-22 20:13:06 -0500124 if only_if is not None and not only_if(backend):
Paul Kehrerc1794072013-10-18 21:42:57 -0500125 pytest.skip(skip_message)
David Reid69aeb492013-10-30 11:35:37 -0700126 m = hashes.Hash(algorithm, backend=backend)
Paul Kehrerc1794072013-10-18 21:42:57 -0500127 m.update(b"a" * 1000000)
David Reid69aeb492013-10-30 11:35:37 -0700128 assert m.finalize() == binascii.unhexlify(md.lower())
Paul Kehrer0317b042013-10-28 17:34:27 -0500129
130
Paul Kehrer2824ab72013-10-28 11:06:55 -0500131def generate_hmac_test(param_loader, path, file_names, digestmod,
Paul Kehrer0317b042013-10-28 17:34:27 -0500132 only_if=None, skip_message=None):
133 def test_hmac(self):
134 for backend in _ALL_BACKENDS:
135 for file_name in file_names:
136 for params in param_loader(os.path.join(path, file_name)):
137 yield (
138 hmac_test,
139 backend,
Paul Kehrer2824ab72013-10-28 11:06:55 -0500140 digestmod,
Paul Kehrer0317b042013-10-28 17:34:27 -0500141 params,
142 only_if,
143 skip_message
144 )
145 return test_hmac
146
147
Paul Kehrer2824ab72013-10-28 11:06:55 -0500148def hmac_test(backend, digestmod, params, only_if, skip_message):
Paul Kehrer0317b042013-10-28 17:34:27 -0500149 if only_if is not None and not only_if(backend):
150 pytest.skip(skip_message)
151 msg = params[0]
152 md = params[1]
153 key = params[2]
Paul Kehrer2824ab72013-10-28 11:06:55 -0500154 h = hmac.HMAC(binascii.unhexlify(key), digestmod=digestmod)
Paul Kehrer0317b042013-10-28 17:34:27 -0500155 h.update(binascii.unhexlify(msg))
156 assert h.hexdigest() == md
Paul Kehrer2824ab72013-10-28 11:06:55 -0500157 digest = hmac.HMAC(binascii.unhexlify(key), digestmod=digestmod,
158 msg=binascii.unhexlify(msg)).hexdigest()
Paul Kehrer0317b042013-10-28 17:34:27 -0500159 assert digest == md
160
161
162def generate_base_hmac_test(hash_cls, only_if=None, skip_message=None):
163 def test_base_hmac(self):
164 for backend in _ALL_BACKENDS:
165 yield (
166 base_hmac_test,
167 backend,
168 hash_cls,
169 only_if,
170 skip_message,
171 )
172 return test_base_hmac
173
174
Paul Kehrer2824ab72013-10-28 11:06:55 -0500175def base_hmac_test(backend, digestmod, only_if, skip_message):
Paul Kehrer0317b042013-10-28 17:34:27 -0500176 if only_if is not None and not only_if(backend):
177 pytest.skip(skip_message)
178 key = b"ab"
Paul Kehrer2824ab72013-10-28 11:06:55 -0500179 h = hmac.HMAC(binascii.unhexlify(key), digestmod=digestmod)
Paul Kehrer0317b042013-10-28 17:34:27 -0500180 h_copy = h.copy()
181 assert h != h_copy
182 assert h._ctx != h_copy._ctx