Alex Gaynor | bd458ae | 2013-10-16 11:59:30 -0700 | [diff] [blame] | 1 | import binascii |
| 2 | import os |
| 3 | |
| 4 | import pytest |
| 5 | |
Paul Kehrer | 22e80cb | 2013-11-20 21:27:00 -0600 | [diff] [blame] | 6 | from cryptography.hazmat.primitives import hashes, hmac |
Paul Kehrer | 21dde56 | 2013-11-06 12:22:09 +0800 | [diff] [blame] | 7 | from cryptography.hazmat.primitives.ciphers import Cipher |
Paul Kehrer | 22e80cb | 2013-11-20 21:27:00 -0600 | [diff] [blame] | 8 | from cryptography.exceptions import ( |
Paul Kehrer | a4bfc08 | 2013-11-22 19:57:37 -0600 | [diff] [blame] | 9 | AlreadyFinalized, NotYetFinalized, AlreadyUpdated, InvalidTag, |
Paul Kehrer | 22e80cb | 2013-11-20 21:27:00 -0600 | [diff] [blame] | 10 | ) |
Alex Gaynor | bd458ae | 2013-10-16 11:59:30 -0700 | [diff] [blame] | 11 | |
Paul Kehrer | f7f6a9f | 2013-11-11 20:43:52 -0600 | [diff] [blame] | 12 | from ...utils import load_vectors_from_file |
| 13 | |
Alex Gaynor | bd458ae | 2013-10-16 11:59:30 -0700 | [diff] [blame] | 14 | |
Alex Gaynor | e5c5eec | 2013-12-13 08:10:20 -0800 | [diff] [blame] | 15 | def _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 Gaynor | 4eec0bb | 2013-12-13 09:12:19 -0800 | [diff] [blame] | 23 | |
Alex Gaynor | 016eed1 | 2013-10-16 14:16:04 -0700 | [diff] [blame] | 24 | def generate_encrypt_test(param_loader, path, file_names, cipher_factory, |
Paul Kehrer | 783479c | 2013-12-26 21:08:45 -0600 | [diff] [blame] | 25 | mode_factory): |
Alex Gaynor | e5c5eec | 2013-12-13 08:10:20 -0800 | [diff] [blame] | 26 | 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 Kehrer | 783479c | 2013-12-26 21:08:45 -0600 | [diff] [blame] | 30 | encrypt_test(backend, cipher_factory, mode_factory, params) |
Alex Gaynor | e5c5eec | 2013-12-13 08:10:20 -0800 | [diff] [blame] | 31 | |
Alex Gaynor | bd458ae | 2013-10-16 11:59:30 -0700 | [diff] [blame] | 32 | return test_encryption |
| 33 | |
| 34 | |
Paul Kehrer | 783479c | 2013-12-26 21:08:45 -0600 | [diff] [blame] | 35 | def encrypt_test(backend, cipher_factory, mode_factory, params): |
Paul Kehrer | a620b7d | 2013-12-20 22:59:02 -0600 | [diff] [blame] | 36 | plaintext = params["plaintext"] |
| 37 | ciphertext = params["ciphertext"] |
Alex Gaynor | 21919e2 | 2013-12-13 08:56:32 -0800 | [diff] [blame] | 38 | 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 Kehrer | 22e80cb | 2013-11-20 21:27:00 -0600 | [diff] [blame] | 53 | def generate_aead_test(param_loader, path, file_names, cipher_factory, |
Paul Kehrer | 783479c | 2013-12-26 21:08:45 -0600 | [diff] [blame] | 54 | mode_factory): |
Alex Gaynor | e5c5eec | 2013-12-13 08:10:20 -0800 | [diff] [blame] | 55 | 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 Kehrer | 783479c | 2013-12-26 21:08:45 -0600 | [diff] [blame] | 59 | aead_test(backend, cipher_factory, mode_factory, params) |
Alex Gaynor | e5c5eec | 2013-12-13 08:10:20 -0800 | [diff] [blame] | 60 | |
Paul Kehrer | 22e80cb | 2013-11-20 21:27:00 -0600 | [diff] [blame] | 61 | return test_aead |
| 62 | |
| 63 | |
Paul Kehrer | 783479c | 2013-12-26 21:08:45 -0600 | [diff] [blame] | 64 | def aead_test(backend, cipher_factory, mode_factory, params): |
Alex Gaynor | 21919e2 | 2013-12-13 08:56:32 -0800 | [diff] [blame] | 65 | if params.get("pt") is not None: |
Paul Kehrer | a620b7d | 2013-12-20 22:59:02 -0600 | [diff] [blame] | 66 | plaintext = params["pt"] |
| 67 | ciphertext = params["ct"] |
| 68 | aad = params["aad"] |
Alex Gaynor | 21919e2 | 2013-12-13 08:56:32 -0800 | [diff] [blame] | 69 | 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 Kehrer | 4da28c3 | 2013-11-07 07:50:17 +0800 | [diff] [blame] | 106 | def generate_stream_encryption_test(param_loader, path, file_names, |
Paul Kehrer | 783479c | 2013-12-26 21:08:45 -0600 | [diff] [blame] | 107 | cipher_factory): |
Alex Gaynor | e5c5eec | 2013-12-13 08:10:20 -0800 | [diff] [blame] | 108 | 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 Kehrer | 783479c | 2013-12-26 21:08:45 -0600 | [diff] [blame] | 112 | stream_encryption_test(backend, cipher_factory, params) |
Paul Kehrer | 4da28c3 | 2013-11-07 07:50:17 +0800 | [diff] [blame] | 113 | return test_stream_encryption |
| 114 | |
| 115 | |
Paul Kehrer | 783479c | 2013-12-26 21:08:45 -0600 | [diff] [blame] | 116 | def stream_encryption_test(backend, cipher_factory, params): |
Paul Kehrer | a620b7d | 2013-12-20 22:59:02 -0600 | [diff] [blame] | 117 | plaintext = params["plaintext"] |
| 118 | ciphertext = params["ciphertext"] |
| 119 | offset = params["offset"] |
Alex Gaynor | 21919e2 | 2013-12-13 08:56:32 -0800 | [diff] [blame] | 120 | 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 Kehrer | 783479c | 2013-12-26 21:08:45 -0600 | [diff] [blame] | 134 | def generate_hash_test(param_loader, path, file_names, hash_cls): |
Alex Gaynor | e5c5eec | 2013-12-13 08:10:20 -0800 | [diff] [blame] | 135 | all_params = _load_all_params(path, file_names, param_loader) |
Paul Kehrer | 4da28c3 | 2013-11-07 07:50:17 +0800 | [diff] [blame] | 136 | |
Alex Gaynor | e5c5eec | 2013-12-13 08:10:20 -0800 | [diff] [blame] | 137 | @pytest.mark.parametrize("params", all_params) |
| 138 | def test_hash(self, backend, params): |
Paul Kehrer | 783479c | 2013-12-26 21:08:45 -0600 | [diff] [blame] | 139 | hash_test(backend, hash_cls, params) |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 140 | return test_hash |
| 141 | |
| 142 | |
Paul Kehrer | 783479c | 2013-12-26 21:08:45 -0600 | [diff] [blame] | 143 | def hash_test(backend, algorithm, params): |
Alex Gaynor | 21919e2 | 2013-12-13 08:56:32 -0800 | [diff] [blame] | 144 | 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 Kehrer | b078d8e | 2013-12-27 16:33:14 -0600 | [diff] [blame] | 152 | def generate_base_hash_test(algorithm, digest_size, block_size): |
| 153 | def test_base_hash(self, backend): |
| 154 | base_hash_test(backend, algorithm, digest_size, block_size) |
| 155 | return test_base_hash |
| 156 | |
| 157 | |
Paul Kehrer | 783479c | 2013-12-26 21:08:45 -0600 | [diff] [blame] | 158 | def base_hash_test(backend, algorithm, digest_size, block_size): |
Alex Gaynor | 21919e2 | 2013-12-13 08:56:32 -0800 | [diff] [blame] | 159 | m = hashes.Hash(algorithm, backend=backend) |
| 160 | assert m.algorithm.digest_size == digest_size |
| 161 | assert m.algorithm.block_size == block_size |
| 162 | m_copy = m.copy() |
| 163 | assert m != m_copy |
| 164 | assert m._ctx != m_copy._ctx |
| 165 | |
| 166 | m.update(b"abc") |
| 167 | copy = m.copy() |
| 168 | copy.update(b"123") |
| 169 | m.update(b"123") |
| 170 | assert copy.finalize() == m.finalize() |
| 171 | |
| 172 | |
Paul Kehrer | 783479c | 2013-12-26 21:08:45 -0600 | [diff] [blame] | 173 | def generate_long_string_hash_test(hash_factory, md): |
Alex Gaynor | e5c5eec | 2013-12-13 08:10:20 -0800 | [diff] [blame] | 174 | def test_long_string_hash(self, backend): |
Paul Kehrer | 783479c | 2013-12-26 21:08:45 -0600 | [diff] [blame] | 175 | long_string_hash_test(backend, hash_factory, md) |
Paul Kehrer | c179407 | 2013-10-18 21:42:57 -0500 | [diff] [blame] | 176 | return test_long_string_hash |
| 177 | |
| 178 | |
Paul Kehrer | 783479c | 2013-12-26 21:08:45 -0600 | [diff] [blame] | 179 | def long_string_hash_test(backend, algorithm, md): |
Alex Gaynor | 21919e2 | 2013-12-13 08:56:32 -0800 | [diff] [blame] | 180 | m = hashes.Hash(algorithm, backend=backend) |
| 181 | m.update(b"a" * 1000000) |
| 182 | assert m.finalize() == binascii.unhexlify(md.lower().encode("ascii")) |
| 183 | |
| 184 | |
Paul Kehrer | b078d8e | 2013-12-27 16:33:14 -0600 | [diff] [blame] | 185 | def generate_base_hmac_test(hash_cls): |
| 186 | def test_base_hmac(self, backend): |
| 187 | base_hmac_test(backend, hash_cls) |
| 188 | return test_base_hmac |
| 189 | |
| 190 | |
| 191 | def base_hmac_test(backend, algorithm): |
| 192 | key = b"ab" |
| 193 | h = hmac.HMAC(binascii.unhexlify(key), algorithm, backend=backend) |
| 194 | h_copy = h.copy() |
| 195 | assert h != h_copy |
| 196 | assert h._ctx != h_copy._ctx |
| 197 | |
| 198 | |
Paul Kehrer | 783479c | 2013-12-26 21:08:45 -0600 | [diff] [blame] | 199 | def generate_hmac_test(param_loader, path, file_names, algorithm): |
Alex Gaynor | e5c5eec | 2013-12-13 08:10:20 -0800 | [diff] [blame] | 200 | all_params = _load_all_params(path, file_names, param_loader) |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 201 | |
Alex Gaynor | e5c5eec | 2013-12-13 08:10:20 -0800 | [diff] [blame] | 202 | @pytest.mark.parametrize("params", all_params) |
| 203 | def test_hmac(self, backend, params): |
Paul Kehrer | 783479c | 2013-12-26 21:08:45 -0600 | [diff] [blame] | 204 | hmac_test(backend, algorithm, params) |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 205 | return test_hmac |
| 206 | |
| 207 | |
Paul Kehrer | 783479c | 2013-12-26 21:08:45 -0600 | [diff] [blame] | 208 | def hmac_test(backend, algorithm, params): |
Alex Gaynor | 21919e2 | 2013-12-13 08:56:32 -0800 | [diff] [blame] | 209 | msg = params[0] |
| 210 | md = params[1] |
| 211 | key = params[2] |
| 212 | h = hmac.HMAC(binascii.unhexlify(key), algorithm, backend=backend) |
| 213 | h.update(binascii.unhexlify(msg)) |
| 214 | assert h.finalize() == binascii.unhexlify(md.encode("ascii")) |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 215 | |
Alex Gaynor | 21919e2 | 2013-12-13 08:56:32 -0800 | [diff] [blame] | 216 | |
Paul Kehrer | 783479c | 2013-12-26 21:08:45 -0600 | [diff] [blame] | 217 | def generate_aead_exception_test(cipher_factory, mode_factory): |
Alex Gaynor | 21919e2 | 2013-12-13 08:56:32 -0800 | [diff] [blame] | 218 | def test_aead_exception(self, backend): |
Paul Kehrer | 783479c | 2013-12-26 21:08:45 -0600 | [diff] [blame] | 219 | aead_exception_test(backend, cipher_factory, mode_factory) |
Paul Kehrer | ce9c611 | 2013-11-22 14:10:59 -0600 | [diff] [blame] | 220 | return test_aead_exception |
Paul Kehrer | 22e80cb | 2013-11-20 21:27:00 -0600 | [diff] [blame] | 221 | |
| 222 | |
Paul Kehrer | 783479c | 2013-12-26 21:08:45 -0600 | [diff] [blame] | 223 | def aead_exception_test(backend, cipher_factory, mode_factory): |
Alex Gaynor | 21919e2 | 2013-12-13 08:56:32 -0800 | [diff] [blame] | 224 | cipher = Cipher( |
| 225 | cipher_factory(binascii.unhexlify(b"0" * 32)), |
| 226 | mode_factory(binascii.unhexlify(b"0" * 24)), |
| 227 | backend |
| 228 | ) |
| 229 | encryptor = cipher.encryptor() |
| 230 | encryptor.update(b"a" * 16) |
| 231 | with pytest.raises(NotYetFinalized): |
| 232 | encryptor.tag |
| 233 | with pytest.raises(AlreadyUpdated): |
| 234 | encryptor.authenticate_additional_data(b"b" * 16) |
| 235 | encryptor.finalize() |
| 236 | with pytest.raises(AlreadyFinalized): |
| 237 | encryptor.authenticate_additional_data(b"b" * 16) |
| 238 | with pytest.raises(AlreadyFinalized): |
| 239 | encryptor.update(b"b" * 16) |
| 240 | with pytest.raises(AlreadyFinalized): |
| 241 | encryptor.finalize() |
| 242 | cipher = Cipher( |
| 243 | cipher_factory(binascii.unhexlify(b"0" * 32)), |
| 244 | mode_factory(binascii.unhexlify(b"0" * 24), b"0" * 16), |
| 245 | backend |
| 246 | ) |
| 247 | decryptor = cipher.decryptor() |
| 248 | decryptor.update(b"a" * 16) |
| 249 | with pytest.raises(AttributeError): |
| 250 | decryptor.tag |
Paul Kehrer | b91221d | 2013-12-04 17:56:40 -0600 | [diff] [blame] | 251 | |
Alex Gaynor | 21919e2 | 2013-12-13 08:56:32 -0800 | [diff] [blame] | 252 | |
Paul Kehrer | 783479c | 2013-12-26 21:08:45 -0600 | [diff] [blame] | 253 | def generate_aead_tag_exception_test(cipher_factory, mode_factory): |
Alex Gaynor | 21919e2 | 2013-12-13 08:56:32 -0800 | [diff] [blame] | 254 | def test_aead_tag_exception(self, backend): |
Paul Kehrer | 783479c | 2013-12-26 21:08:45 -0600 | [diff] [blame] | 255 | aead_tag_exception_test(backend, cipher_factory, mode_factory) |
Paul Kehrer | b91221d | 2013-12-04 17:56:40 -0600 | [diff] [blame] | 256 | return test_aead_tag_exception |
Alex Gaynor | 21919e2 | 2013-12-13 08:56:32 -0800 | [diff] [blame] | 257 | |
| 258 | |
Paul Kehrer | 783479c | 2013-12-26 21:08:45 -0600 | [diff] [blame] | 259 | def aead_tag_exception_test(backend, cipher_factory, mode_factory): |
Alex Gaynor | 21919e2 | 2013-12-13 08:56:32 -0800 | [diff] [blame] | 260 | cipher = Cipher( |
| 261 | cipher_factory(binascii.unhexlify(b"0" * 32)), |
| 262 | mode_factory(binascii.unhexlify(b"0" * 24)), |
| 263 | backend |
| 264 | ) |
| 265 | with pytest.raises(ValueError): |
| 266 | cipher.decryptor() |
| 267 | cipher = Cipher( |
| 268 | cipher_factory(binascii.unhexlify(b"0" * 32)), |
Paul Kehrer | f7b4ede | 2013-12-21 17:25:19 -0600 | [diff] [blame] | 269 | mode_factory(binascii.unhexlify(b"0" * 24), b"000"), |
| 270 | backend |
| 271 | ) |
| 272 | with pytest.raises(ValueError): |
| 273 | cipher.decryptor() |
| 274 | cipher = Cipher( |
| 275 | cipher_factory(binascii.unhexlify(b"0" * 32)), |
Alex Gaynor | 21919e2 | 2013-12-13 08:56:32 -0800 | [diff] [blame] | 276 | mode_factory(binascii.unhexlify(b"0" * 24), b"0" * 16), |
| 277 | backend |
| 278 | ) |
| 279 | with pytest.raises(ValueError): |
| 280 | cipher.encryptor() |