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 | |
| 11 | |
Alex Gaynor | 016eed1 | 2013-10-16 14:16:04 -0700 | [diff] [blame] | 12 | def generate_encrypt_test(param_loader, path, file_names, cipher_factory, |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 13 | mode_factory, only_if=lambda backend: True, |
Alex Gaynor | 512dd69 | 2013-10-16 14:27:52 -0700 | [diff] [blame] | 14 | skip_message=None): |
Alex Gaynor | bd458ae | 2013-10-16 11:59:30 -0700 | [diff] [blame] | 15 | def test_encryption(self): |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 16 | for backend in _ALL_BACKENDS: |
Alex Gaynor | 512dd69 | 2013-10-16 14:27:52 -0700 | [diff] [blame] | 17 | for file_name in file_names: |
| 18 | for params in param_loader(os.path.join(path, file_name)): |
| 19 | yield ( |
| 20 | encrypt_test, |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 21 | backend, |
Alex Gaynor | 512dd69 | 2013-10-16 14:27:52 -0700 | [diff] [blame] | 22 | cipher_factory, |
| 23 | mode_factory, |
| 24 | params, |
| 25 | only_if, |
| 26 | skip_message |
| 27 | ) |
Alex Gaynor | bd458ae | 2013-10-16 11:59:30 -0700 | [diff] [blame] | 28 | return test_encryption |
| 29 | |
| 30 | |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 31 | def encrypt_test(backend, cipher_factory, mode_factory, params, only_if, |
Alex Gaynor | 512dd69 | 2013-10-16 14:27:52 -0700 | [diff] [blame] | 32 | skip_message): |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 33 | if not only_if(backend): |
Alex Gaynor | 512dd69 | 2013-10-16 14:27:52 -0700 | [diff] [blame] | 34 | pytest.skip(skip_message) |
Alex Gaynor | bd458ae | 2013-10-16 11:59:30 -0700 | [diff] [blame] | 35 | plaintext = params.pop("plaintext") |
| 36 | ciphertext = params.pop("ciphertext") |
Paul Kehrer | 21dde56 | 2013-11-06 12:22:09 +0800 | [diff] [blame] | 37 | cipher = Cipher( |
Alex Gaynor | bd458ae | 2013-10-16 11:59:30 -0700 | [diff] [blame] | 38 | cipher_factory(**params), |
| 39 | mode_factory(**params), |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 40 | backend |
Alex Gaynor | bd458ae | 2013-10-16 11:59:30 -0700 | [diff] [blame] | 41 | ) |
Paul Kehrer | 620c2ae | 2013-10-19 14:12:04 -0500 | [diff] [blame] | 42 | encryptor = cipher.encryptor() |
| 43 | actual_ciphertext = encryptor.update(binascii.unhexlify(plaintext)) |
| 44 | actual_ciphertext += encryptor.finalize() |
Alex Gaynor | fb39b3f | 2013-10-16 14:30:59 -0700 | [diff] [blame] | 45 | assert actual_ciphertext == binascii.unhexlify(ciphertext) |
Paul Kehrer | 620c2ae | 2013-10-19 14:12:04 -0500 | [diff] [blame] | 46 | decryptor = cipher.decryptor() |
| 47 | actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext)) |
| 48 | actual_plaintext += decryptor.finalize() |
| 49 | assert actual_plaintext == binascii.unhexlify(plaintext) |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 50 | |
| 51 | |
Paul Kehrer | bb069c2 | 2013-10-18 19:51:01 -0500 | [diff] [blame] | 52 | def generate_hash_test(param_loader, path, file_names, hash_cls, |
Donald Stufft | 2acd77a | 2013-10-19 19:49:30 -0400 | [diff] [blame] | 53 | only_if=None, skip_message=None): |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 54 | def test_hash(self): |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 55 | for backend in _ALL_BACKENDS: |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 56 | for file_name in file_names: |
| 57 | for params in param_loader(os.path.join(path, file_name)): |
| 58 | yield ( |
| 59 | hash_test, |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 60 | backend, |
Paul Kehrer | bb069c2 | 2013-10-18 19:51:01 -0500 | [diff] [blame] | 61 | hash_cls, |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 62 | params, |
| 63 | only_if, |
| 64 | skip_message |
| 65 | ) |
| 66 | return test_hash |
| 67 | |
| 68 | |
David Reid | bb0d3f0 | 2013-10-31 15:22:49 -0700 | [diff] [blame] | 69 | def hash_test(backend, algorithm, params, only_if, skip_message): |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 70 | if only_if is not None and not only_if(backend): |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 71 | pytest.skip(skip_message) |
| 72 | msg = params[0] |
| 73 | md = params[1] |
David Reid | bb0d3f0 | 2013-10-31 15:22:49 -0700 | [diff] [blame] | 74 | m = hashes.Hash(algorithm, backend=backend) |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 75 | m.update(binascii.unhexlify(msg)) |
David Reid | c3d029f | 2013-10-31 14:06:14 -0700 | [diff] [blame] | 76 | expected_md = md.replace(" ", "").lower().encode("ascii") |
| 77 | assert m.finalize() == binascii.unhexlify(expected_md) |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 78 | |
| 79 | |
David Reid | 69aeb49 | 2013-10-30 11:35:37 -0700 | [diff] [blame] | 80 | def generate_base_hash_test(algorithm, digest_size, block_size, |
Donald Stufft | 2acd77a | 2013-10-19 19:49:30 -0400 | [diff] [blame] | 81 | only_if=None, skip_message=None): |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 82 | def test_base_hash(self): |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 83 | for backend in _ALL_BACKENDS: |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 84 | yield ( |
| 85 | base_hash_test, |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 86 | backend, |
David Reid | 69aeb49 | 2013-10-30 11:35:37 -0700 | [diff] [blame] | 87 | algorithm, |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 88 | digest_size, |
| 89 | block_size, |
| 90 | only_if, |
| 91 | skip_message, |
| 92 | ) |
| 93 | return test_base_hash |
| 94 | |
| 95 | |
David Reid | 69aeb49 | 2013-10-30 11:35:37 -0700 | [diff] [blame] | 96 | def base_hash_test(backend, algorithm, digest_size, block_size, only_if, |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 97 | skip_message): |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 98 | if only_if is not None and not only_if(backend): |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 99 | pytest.skip(skip_message) |
David Reid | 69aeb49 | 2013-10-30 11:35:37 -0700 | [diff] [blame] | 100 | |
| 101 | m = hashes.Hash(algorithm, backend=backend) |
| 102 | assert m.algorithm.digest_size == digest_size |
| 103 | assert m.algorithm.block_size == block_size |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 104 | m_copy = m.copy() |
| 105 | assert m != m_copy |
| 106 | assert m._ctx != m_copy._ctx |
Paul Kehrer | c179407 | 2013-10-18 21:42:57 -0500 | [diff] [blame] | 107 | |
| 108 | |
Donald Stufft | 2acd77a | 2013-10-19 19:49:30 -0400 | [diff] [blame] | 109 | def generate_long_string_hash_test(hash_factory, md, only_if=None, |
Paul Kehrer | c179407 | 2013-10-18 21:42:57 -0500 | [diff] [blame] | 110 | skip_message=None): |
| 111 | def test_long_string_hash(self): |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 112 | for backend in _ALL_BACKENDS: |
Paul Kehrer | c179407 | 2013-10-18 21:42:57 -0500 | [diff] [blame] | 113 | yield( |
| 114 | long_string_hash_test, |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 115 | backend, |
Paul Kehrer | c179407 | 2013-10-18 21:42:57 -0500 | [diff] [blame] | 116 | hash_factory, |
| 117 | md, |
| 118 | only_if, |
| 119 | skip_message |
| 120 | ) |
| 121 | return test_long_string_hash |
| 122 | |
| 123 | |
David Reid | 69aeb49 | 2013-10-30 11:35:37 -0700 | [diff] [blame] | 124 | def long_string_hash_test(backend, algorithm, md, only_if, skip_message): |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 125 | if only_if is not None and not only_if(backend): |
Paul Kehrer | c179407 | 2013-10-18 21:42:57 -0500 | [diff] [blame] | 126 | pytest.skip(skip_message) |
David Reid | 69aeb49 | 2013-10-30 11:35:37 -0700 | [diff] [blame] | 127 | m = hashes.Hash(algorithm, backend=backend) |
Paul Kehrer | c179407 | 2013-10-18 21:42:57 -0500 | [diff] [blame] | 128 | m.update(b"a" * 1000000) |
David Reid | c3d029f | 2013-10-31 14:06:14 -0700 | [diff] [blame] | 129 | assert m.finalize() == binascii.unhexlify(md.lower().encode("ascii")) |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 130 | |
| 131 | |
David Reid | e3960f6 | 2013-11-01 14:52:16 -0700 | [diff] [blame] | 132 | def generate_hmac_test(param_loader, path, file_names, algorithm, |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 133 | 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 Reid | e3960f6 | 2013-11-01 14:52:16 -0700 | [diff] [blame] | 141 | algorithm, |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 142 | params, |
| 143 | only_if, |
| 144 | skip_message |
| 145 | ) |
| 146 | return test_hmac |
| 147 | |
| 148 | |
David Reid | e3960f6 | 2013-11-01 14:52:16 -0700 | [diff] [blame] | 149 | def hmac_test(backend, algorithm, params, only_if, skip_message): |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 150 | 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 Reid | e3960f6 | 2013-11-01 14:52:16 -0700 | [diff] [blame] | 155 | h = hmac.HMAC(binascii.unhexlify(key), algorithm) |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 156 | h.update(binascii.unhexlify(msg)) |
David Reid | 753ae19 | 2013-11-01 16:28:41 -0700 | [diff] [blame] | 157 | assert h.finalize() == binascii.unhexlify(md.encode("ascii")) |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 158 | |
| 159 | |
| 160 | def 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 Reid | e3960f6 | 2013-11-01 14:52:16 -0700 | [diff] [blame] | 173 | def base_hmac_test(backend, algorithm, only_if, skip_message): |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 174 | if only_if is not None and not only_if(backend): |
| 175 | pytest.skip(skip_message) |
| 176 | key = b"ab" |
David Reid | e3960f6 | 2013-11-01 14:52:16 -0700 | [diff] [blame] | 177 | h = hmac.HMAC(binascii.unhexlify(key), algorithm) |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 178 | h_copy = h.copy() |
| 179 | assert h != h_copy |
| 180 | assert h._ctx != h_copy._ctx |