Alex Gaynor | bd458ae | 2013-10-16 11:59:30 -0700 | [diff] [blame] | 1 | import binascii |
| 2 | import os |
| 3 | |
| 4 | import pytest |
| 5 | |
Donald Stufft | ce0d781 | 2013-10-27 16:52:33 -0400 | [diff] [blame] | 6 | from cryptography.hazmat.bindings import _ALL_BACKENDS |
David Reid | 69aeb49 | 2013-10-30 11:35:37 -0700 | [diff] [blame] | 7 | from cryptography.hazmat.primitives import hashes |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 8 | from cryptography.hazmat.primitives import hmac |
Paul Kehrer | 21dde56 | 2013-11-06 12:22:09 +0800 | [diff] [blame] | 9 | from cryptography.hazmat.primitives.ciphers import Cipher |
Alex Gaynor | bd458ae | 2013-10-16 11:59:30 -0700 | [diff] [blame] | 10 | |
Paul Kehrer | f7f6a9f | 2013-11-11 20:43:52 -0600 | [diff] [blame] | 11 | from ...utils import load_vectors_from_file |
| 12 | |
Alex Gaynor | bd458ae | 2013-10-16 11:59:30 -0700 | [diff] [blame] | 13 | |
Alex Gaynor | 016eed1 | 2013-10-16 14:16:04 -0700 | [diff] [blame] | 14 | def generate_encrypt_test(param_loader, path, file_names, cipher_factory, |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 15 | mode_factory, only_if=lambda backend: True, |
Alex Gaynor | 512dd69 | 2013-10-16 14:27:52 -0700 | [diff] [blame] | 16 | skip_message=None): |
Alex Gaynor | bd458ae | 2013-10-16 11:59:30 -0700 | [diff] [blame] | 17 | def test_encryption(self): |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 18 | for backend in _ALL_BACKENDS: |
Alex Gaynor | 512dd69 | 2013-10-16 14:27:52 -0700 | [diff] [blame] | 19 | for file_name in file_names: |
Paul Kehrer | f7f6a9f | 2013-11-11 20:43:52 -0600 | [diff] [blame] | 20 | for params in load_vectors_from_file( |
| 21 | os.path.join(path, file_name), |
| 22 | param_loader |
| 23 | ): |
Alex Gaynor | 512dd69 | 2013-10-16 14:27:52 -0700 | [diff] [blame] | 24 | yield ( |
| 25 | encrypt_test, |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 26 | backend, |
Alex Gaynor | 512dd69 | 2013-10-16 14:27:52 -0700 | [diff] [blame] | 27 | cipher_factory, |
| 28 | mode_factory, |
| 29 | params, |
| 30 | only_if, |
| 31 | skip_message |
| 32 | ) |
Alex Gaynor | bd458ae | 2013-10-16 11:59:30 -0700 | [diff] [blame] | 33 | return test_encryption |
| 34 | |
| 35 | |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 36 | def encrypt_test(backend, cipher_factory, mode_factory, params, only_if, |
Alex Gaynor | 512dd69 | 2013-10-16 14:27:52 -0700 | [diff] [blame] | 37 | skip_message): |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 38 | if not only_if(backend): |
Alex Gaynor | 512dd69 | 2013-10-16 14:27:52 -0700 | [diff] [blame] | 39 | pytest.skip(skip_message) |
Alex Gaynor | bd458ae | 2013-10-16 11:59:30 -0700 | [diff] [blame] | 40 | plaintext = params.pop("plaintext") |
| 41 | ciphertext = params.pop("ciphertext") |
Paul Kehrer | 21dde56 | 2013-11-06 12:22:09 +0800 | [diff] [blame] | 42 | cipher = Cipher( |
Alex Gaynor | bd458ae | 2013-10-16 11:59:30 -0700 | [diff] [blame] | 43 | cipher_factory(**params), |
| 44 | mode_factory(**params), |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 45 | backend |
Alex Gaynor | bd458ae | 2013-10-16 11:59:30 -0700 | [diff] [blame] | 46 | ) |
Paul Kehrer | 620c2ae | 2013-10-19 14:12:04 -0500 | [diff] [blame] | 47 | encryptor = cipher.encryptor() |
| 48 | actual_ciphertext = encryptor.update(binascii.unhexlify(plaintext)) |
| 49 | actual_ciphertext += encryptor.finalize() |
Alex Gaynor | fb39b3f | 2013-10-16 14:30:59 -0700 | [diff] [blame] | 50 | assert actual_ciphertext == binascii.unhexlify(ciphertext) |
Paul Kehrer | 620c2ae | 2013-10-19 14:12:04 -0500 | [diff] [blame] | 51 | decryptor = cipher.decryptor() |
| 52 | actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext)) |
| 53 | actual_plaintext += decryptor.finalize() |
| 54 | assert actual_plaintext == binascii.unhexlify(plaintext) |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 55 | |
| 56 | |
Paul Kehrer | 4da28c3 | 2013-11-07 07:50:17 +0800 | [diff] [blame] | 57 | def 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 Kehrer | f7f6a9f | 2013-11-11 20:43:52 -0600 | [diff] [blame] | 63 | for params in load_vectors_from_file( |
| 64 | os.path.join(path, file_name), |
| 65 | param_loader |
| 66 | ): |
Paul Kehrer | 4da28c3 | 2013-11-07 07:50:17 +0800 | [diff] [blame] | 67 | 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 | |
| 78 | def 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 Kehrer | bb069c2 | 2013-10-18 19:51:01 -0500 | [diff] [blame] | 99 | def generate_hash_test(param_loader, path, file_names, hash_cls, |
Donald Stufft | 2acd77a | 2013-10-19 19:49:30 -0400 | [diff] [blame] | 100 | only_if=None, skip_message=None): |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 101 | def test_hash(self): |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 102 | for backend in _ALL_BACKENDS: |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 103 | for file_name in file_names: |
Paul Kehrer | f7f6a9f | 2013-11-11 20:43:52 -0600 | [diff] [blame] | 104 | for params in load_vectors_from_file( |
| 105 | os.path.join(path, file_name), |
| 106 | param_loader |
| 107 | ): |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 108 | yield ( |
| 109 | hash_test, |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 110 | backend, |
Paul Kehrer | bb069c2 | 2013-10-18 19:51:01 -0500 | [diff] [blame] | 111 | hash_cls, |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 112 | params, |
| 113 | only_if, |
| 114 | skip_message |
| 115 | ) |
| 116 | return test_hash |
| 117 | |
| 118 | |
David Reid | bb0d3f0 | 2013-10-31 15:22:49 -0700 | [diff] [blame] | 119 | def hash_test(backend, algorithm, params, only_if, skip_message): |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 120 | if only_if is not None and not only_if(backend): |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 121 | pytest.skip(skip_message) |
| 122 | msg = params[0] |
| 123 | md = params[1] |
David Reid | 077e76e | 2013-11-05 12:59:37 -0800 | [diff] [blame^] | 124 | m = hashes.Hash(algorithm, backend) |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 125 | m.update(binascii.unhexlify(msg)) |
David Reid | c3d029f | 2013-10-31 14:06:14 -0700 | [diff] [blame] | 126 | expected_md = md.replace(" ", "").lower().encode("ascii") |
| 127 | assert m.finalize() == binascii.unhexlify(expected_md) |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 128 | |
| 129 | |
David Reid | 69aeb49 | 2013-10-30 11:35:37 -0700 | [diff] [blame] | 130 | def generate_base_hash_test(algorithm, digest_size, block_size, |
Donald Stufft | 2acd77a | 2013-10-19 19:49:30 -0400 | [diff] [blame] | 131 | only_if=None, skip_message=None): |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 132 | def test_base_hash(self): |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 133 | for backend in _ALL_BACKENDS: |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 134 | yield ( |
| 135 | base_hash_test, |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 136 | backend, |
David Reid | 69aeb49 | 2013-10-30 11:35:37 -0700 | [diff] [blame] | 137 | algorithm, |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 138 | digest_size, |
| 139 | block_size, |
| 140 | only_if, |
| 141 | skip_message, |
| 142 | ) |
| 143 | return test_base_hash |
| 144 | |
| 145 | |
David Reid | 69aeb49 | 2013-10-30 11:35:37 -0700 | [diff] [blame] | 146 | def base_hash_test(backend, algorithm, digest_size, block_size, only_if, |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 147 | skip_message): |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 148 | if only_if is not None and not only_if(backend): |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 149 | pytest.skip(skip_message) |
David Reid | 69aeb49 | 2013-10-30 11:35:37 -0700 | [diff] [blame] | 150 | |
David Reid | 077e76e | 2013-11-05 12:59:37 -0800 | [diff] [blame^] | 151 | m = hashes.Hash(algorithm, backend) |
David Reid | 69aeb49 | 2013-10-30 11:35:37 -0700 | [diff] [blame] | 152 | assert m.algorithm.digest_size == digest_size |
| 153 | assert m.algorithm.block_size == block_size |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 154 | m_copy = m.copy() |
| 155 | assert m != m_copy |
| 156 | assert m._ctx != m_copy._ctx |
Paul Kehrer | c179407 | 2013-10-18 21:42:57 -0500 | [diff] [blame] | 157 | |
Alex Gaynor | 217e3b5 | 2013-11-12 10:27:49 -0800 | [diff] [blame] | 158 | m.update(b"abc") |
| 159 | copy = m.copy() |
| 160 | copy.update(b"123") |
| 161 | m.update(b"123") |
| 162 | assert copy.finalize() == m.finalize() |
| 163 | |
Paul Kehrer | c179407 | 2013-10-18 21:42:57 -0500 | [diff] [blame] | 164 | |
Donald Stufft | 2acd77a | 2013-10-19 19:49:30 -0400 | [diff] [blame] | 165 | def generate_long_string_hash_test(hash_factory, md, only_if=None, |
Paul Kehrer | c179407 | 2013-10-18 21:42:57 -0500 | [diff] [blame] | 166 | skip_message=None): |
| 167 | def test_long_string_hash(self): |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 168 | for backend in _ALL_BACKENDS: |
Paul Kehrer | c179407 | 2013-10-18 21:42:57 -0500 | [diff] [blame] | 169 | yield( |
| 170 | long_string_hash_test, |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 171 | backend, |
Paul Kehrer | c179407 | 2013-10-18 21:42:57 -0500 | [diff] [blame] | 172 | hash_factory, |
| 173 | md, |
| 174 | only_if, |
| 175 | skip_message |
| 176 | ) |
| 177 | return test_long_string_hash |
| 178 | |
| 179 | |
David Reid | 69aeb49 | 2013-10-30 11:35:37 -0700 | [diff] [blame] | 180 | def long_string_hash_test(backend, algorithm, md, only_if, skip_message): |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 181 | if only_if is not None and not only_if(backend): |
Paul Kehrer | c179407 | 2013-10-18 21:42:57 -0500 | [diff] [blame] | 182 | pytest.skip(skip_message) |
David Reid | 077e76e | 2013-11-05 12:59:37 -0800 | [diff] [blame^] | 183 | m = hashes.Hash(algorithm, backend) |
Paul Kehrer | c179407 | 2013-10-18 21:42:57 -0500 | [diff] [blame] | 184 | m.update(b"a" * 1000000) |
David Reid | c3d029f | 2013-10-31 14:06:14 -0700 | [diff] [blame] | 185 | assert m.finalize() == binascii.unhexlify(md.lower().encode("ascii")) |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 186 | |
| 187 | |
David Reid | e3960f6 | 2013-11-01 14:52:16 -0700 | [diff] [blame] | 188 | def generate_hmac_test(param_loader, path, file_names, algorithm, |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 189 | only_if=None, skip_message=None): |
| 190 | def test_hmac(self): |
| 191 | for backend in _ALL_BACKENDS: |
| 192 | for file_name in file_names: |
Paul Kehrer | f7f6a9f | 2013-11-11 20:43:52 -0600 | [diff] [blame] | 193 | for params in load_vectors_from_file( |
| 194 | os.path.join(path, file_name), |
| 195 | param_loader |
| 196 | ): |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 197 | yield ( |
| 198 | hmac_test, |
| 199 | backend, |
David Reid | e3960f6 | 2013-11-01 14:52:16 -0700 | [diff] [blame] | 200 | algorithm, |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 201 | params, |
| 202 | only_if, |
| 203 | skip_message |
| 204 | ) |
| 205 | return test_hmac |
| 206 | |
| 207 | |
David Reid | e3960f6 | 2013-11-01 14:52:16 -0700 | [diff] [blame] | 208 | def hmac_test(backend, algorithm, params, only_if, skip_message): |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 209 | if only_if is not None and not only_if(backend): |
| 210 | pytest.skip(skip_message) |
| 211 | msg = params[0] |
| 212 | md = params[1] |
| 213 | key = params[2] |
David Reid | 077e76e | 2013-11-05 12:59:37 -0800 | [diff] [blame^] | 214 | h = hmac.HMAC(binascii.unhexlify(key), algorithm, backend) |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 215 | h.update(binascii.unhexlify(msg)) |
David Reid | 753ae19 | 2013-11-01 16:28:41 -0700 | [diff] [blame] | 216 | assert h.finalize() == binascii.unhexlify(md.encode("ascii")) |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 217 | |
| 218 | |
| 219 | def generate_base_hmac_test(hash_cls, only_if=None, skip_message=None): |
| 220 | def test_base_hmac(self): |
| 221 | for backend in _ALL_BACKENDS: |
| 222 | yield ( |
| 223 | base_hmac_test, |
| 224 | backend, |
| 225 | hash_cls, |
| 226 | only_if, |
| 227 | skip_message, |
| 228 | ) |
| 229 | return test_base_hmac |
| 230 | |
| 231 | |
David Reid | e3960f6 | 2013-11-01 14:52:16 -0700 | [diff] [blame] | 232 | def base_hmac_test(backend, algorithm, only_if, skip_message): |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 233 | if only_if is not None and not only_if(backend): |
| 234 | pytest.skip(skip_message) |
| 235 | key = b"ab" |
David Reid | 077e76e | 2013-11-05 12:59:37 -0800 | [diff] [blame^] | 236 | h = hmac.HMAC(binascii.unhexlify(key), algorithm, backend) |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 237 | h_copy = h.copy() |
| 238 | assert h != h_copy |
| 239 | assert h._ctx != h_copy._ctx |