blob: 73a2469aa73ae2522d40db355d9199b5caff938a [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
Paul Kehrer0317b042013-10-28 17:34:27 -05007from cryptography.hazmat.primitives import hmac
Donald Stufftf04317a2013-10-27 16:44:30 -04008from cryptography.hazmat.primitives.block import BlockCipher
Alex Gaynorbd458ae2013-10-16 11:59:30 -07009
10
Alex Gaynor016eed12013-10-16 14:16:04 -070011def generate_encrypt_test(param_loader, path, file_names, cipher_factory,
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050012 mode_factory, only_if=lambda backend: True,
Alex Gaynor512dd692013-10-16 14:27:52 -070013 skip_message=None):
Alex Gaynorbd458ae2013-10-16 11:59:30 -070014 def test_encryption(self):
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050015 for backend in _ALL_BACKENDS:
Alex Gaynor512dd692013-10-16 14:27:52 -070016 for file_name in file_names:
17 for params in param_loader(os.path.join(path, file_name)):
18 yield (
19 encrypt_test,
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050020 backend,
Alex Gaynor512dd692013-10-16 14:27:52 -070021 cipher_factory,
22 mode_factory,
23 params,
24 only_if,
25 skip_message
26 )
Alex Gaynorbd458ae2013-10-16 11:59:30 -070027 return test_encryption
28
29
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050030def encrypt_test(backend, cipher_factory, mode_factory, params, only_if,
Alex Gaynor512dd692013-10-16 14:27:52 -070031 skip_message):
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050032 if not only_if(backend):
Alex Gaynor512dd692013-10-16 14:27:52 -070033 pytest.skip(skip_message)
Alex Gaynorbd458ae2013-10-16 11:59:30 -070034 plaintext = params.pop("plaintext")
35 ciphertext = params.pop("ciphertext")
36 cipher = BlockCipher(
37 cipher_factory(**params),
38 mode_factory(**params),
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050039 backend
Alex Gaynorbd458ae2013-10-16 11:59:30 -070040 )
Paul Kehrer620c2ae2013-10-19 14:12:04 -050041 encryptor = cipher.encryptor()
42 actual_ciphertext = encryptor.update(binascii.unhexlify(plaintext))
43 actual_ciphertext += encryptor.finalize()
Alex Gaynorfb39b3f2013-10-16 14:30:59 -070044 assert actual_ciphertext == binascii.unhexlify(ciphertext)
Paul Kehrer620c2ae2013-10-19 14:12:04 -050045 decryptor = cipher.decryptor()
46 actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext))
47 actual_plaintext += decryptor.finalize()
48 assert actual_plaintext == binascii.unhexlify(plaintext)
Paul Kehrerbde6fb52013-10-18 18:08:49 -050049
50
Paul Kehrerbb069c22013-10-18 19:51:01 -050051def generate_hash_test(param_loader, path, file_names, hash_cls,
Donald Stufft2acd77a2013-10-19 19:49:30 -040052 only_if=None, skip_message=None):
Paul Kehrerbde6fb52013-10-18 18:08:49 -050053 def test_hash(self):
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050054 for backend in _ALL_BACKENDS:
Paul Kehrerbde6fb52013-10-18 18:08:49 -050055 for file_name in file_names:
56 for params in param_loader(os.path.join(path, file_name)):
57 yield (
58 hash_test,
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050059 backend,
Paul Kehrerbb069c22013-10-18 19:51:01 -050060 hash_cls,
Paul Kehrerbde6fb52013-10-18 18:08:49 -050061 params,
62 only_if,
63 skip_message
64 )
65 return test_hash
66
67
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050068def hash_test(backend, hash_cls, params, only_if, skip_message):
69 if only_if is not None and not only_if(backend):
Paul Kehrerbde6fb52013-10-18 18:08:49 -050070 pytest.skip(skip_message)
71 msg = params[0]
72 md = params[1]
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050073 m = hash_cls(backend=backend)
Paul Kehrerbde6fb52013-10-18 18:08:49 -050074 m.update(binascii.unhexlify(msg))
75 assert m.hexdigest() == md.replace(" ", "").lower()
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050076 digst = hash_cls(backend=backend, data=binascii.unhexlify(msg)).hexdigest()
77 assert digst == md.replace(" ", "").lower()
Paul Kehrerbde6fb52013-10-18 18:08:49 -050078
79
Paul Kehrerbb069c22013-10-18 19:51:01 -050080def generate_base_hash_test(hash_cls, 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,
Paul Kehrerbb069c22013-10-18 19:51:01 -050087 hash_cls,
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
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050096def base_hash_test(backend, hash_cls, 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)
Paul Kehrerdb37d0e2013-10-22 20:13:06 -0500100 m = hash_cls(backend=backend)
Paul Kehrerbde6fb52013-10-18 18:08:49 -0500101 assert m.digest_size == digest_size
102 assert m.block_size == block_size
103 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
Paul Kehrerdb37d0e2013-10-22 20:13:06 -0500123def long_string_hash_test(backend, hash_factory, md, only_if, skip_message):
124 if only_if is not None and not only_if(backend):
Paul Kehrerc1794072013-10-18 21:42:57 -0500125 pytest.skip(skip_message)
Paul Kehrerdb37d0e2013-10-22 20:13:06 -0500126 m = hash_factory(backend=backend)
Paul Kehrerc1794072013-10-18 21:42:57 -0500127 m.update(b"a" * 1000000)
128 assert m.hexdigest() == md.lower()
Paul Kehrer0317b042013-10-28 17:34:27 -0500129
130
131def generate_hmac_test(param_loader, path, file_names, hash_cls,
132 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,
140 hash_cls,
141 params,
142 only_if,
143 skip_message
144 )
145 return test_hmac
146
147
148def hmac_test(backend, hash_cls, params, only_if, skip_message):
149 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]
154 h = hmac.HMAC(binascii.unhexlify(key), hash_cls)
155 h.update(binascii.unhexlify(msg))
156 assert h.hexdigest() == md
157 digest = hmac.HMAC(binascii.unhexlify(key), hash_cls,
158 data=binascii.unhexlify(msg)).hexdigest()
159 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
175def base_hmac_test(backend, hash_cls, only_if, skip_message):
176 if only_if is not None and not only_if(backend):
177 pytest.skip(skip_message)
178 key = b"ab"
179 h = hmac.HMAC(binascii.unhexlify(key), hash_cls)
180 h_copy = h.copy()
181 assert h != h_copy
182 assert h._ctx != h_copy._ctx