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 |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 7 | from cryptography.hazmat.primitives import hmac |
Donald Stufft | f04317a | 2013-10-27 16:44:30 -0400 | [diff] [blame] | 8 | from cryptography.hazmat.primitives.block import BlockCipher |
Alex Gaynor | bd458ae | 2013-10-16 11:59:30 -0700 | [diff] [blame] | 9 | |
| 10 | |
Alex Gaynor | 016eed1 | 2013-10-16 14:16:04 -0700 | [diff] [blame] | 11 | def generate_encrypt_test(param_loader, path, file_names, cipher_factory, |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 12 | mode_factory, only_if=lambda backend: True, |
Alex Gaynor | 512dd69 | 2013-10-16 14:27:52 -0700 | [diff] [blame] | 13 | skip_message=None): |
Alex Gaynor | bd458ae | 2013-10-16 11:59:30 -0700 | [diff] [blame] | 14 | def test_encryption(self): |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 15 | for backend in _ALL_BACKENDS: |
Alex Gaynor | 512dd69 | 2013-10-16 14:27:52 -0700 | [diff] [blame] | 16 | for file_name in file_names: |
| 17 | for params in param_loader(os.path.join(path, file_name)): |
| 18 | yield ( |
| 19 | encrypt_test, |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 20 | backend, |
Alex Gaynor | 512dd69 | 2013-10-16 14:27:52 -0700 | [diff] [blame] | 21 | cipher_factory, |
| 22 | mode_factory, |
| 23 | params, |
| 24 | only_if, |
| 25 | skip_message |
| 26 | ) |
Alex Gaynor | bd458ae | 2013-10-16 11:59:30 -0700 | [diff] [blame] | 27 | return test_encryption |
| 28 | |
| 29 | |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 30 | def encrypt_test(backend, cipher_factory, mode_factory, params, only_if, |
Alex Gaynor | 512dd69 | 2013-10-16 14:27:52 -0700 | [diff] [blame] | 31 | skip_message): |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 32 | if not only_if(backend): |
Alex Gaynor | 512dd69 | 2013-10-16 14:27:52 -0700 | [diff] [blame] | 33 | pytest.skip(skip_message) |
Alex Gaynor | bd458ae | 2013-10-16 11:59:30 -0700 | [diff] [blame] | 34 | plaintext = params.pop("plaintext") |
| 35 | ciphertext = params.pop("ciphertext") |
| 36 | cipher = BlockCipher( |
| 37 | cipher_factory(**params), |
| 38 | mode_factory(**params), |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 39 | backend |
Alex Gaynor | bd458ae | 2013-10-16 11:59:30 -0700 | [diff] [blame] | 40 | ) |
Paul Kehrer | 620c2ae | 2013-10-19 14:12:04 -0500 | [diff] [blame] | 41 | encryptor = cipher.encryptor() |
| 42 | actual_ciphertext = encryptor.update(binascii.unhexlify(plaintext)) |
| 43 | actual_ciphertext += encryptor.finalize() |
Alex Gaynor | fb39b3f | 2013-10-16 14:30:59 -0700 | [diff] [blame] | 44 | assert actual_ciphertext == binascii.unhexlify(ciphertext) |
Paul Kehrer | 620c2ae | 2013-10-19 14:12:04 -0500 | [diff] [blame] | 45 | decryptor = cipher.decryptor() |
| 46 | actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext)) |
| 47 | actual_plaintext += decryptor.finalize() |
| 48 | assert actual_plaintext == binascii.unhexlify(plaintext) |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 49 | |
| 50 | |
Paul Kehrer | bb069c2 | 2013-10-18 19:51:01 -0500 | [diff] [blame] | 51 | def generate_hash_test(param_loader, path, file_names, hash_cls, |
Donald Stufft | 2acd77a | 2013-10-19 19:49:30 -0400 | [diff] [blame] | 52 | only_if=None, skip_message=None): |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 53 | def test_hash(self): |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 54 | for backend in _ALL_BACKENDS: |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 55 | for file_name in file_names: |
| 56 | for params in param_loader(os.path.join(path, file_name)): |
| 57 | yield ( |
| 58 | hash_test, |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 59 | backend, |
Paul Kehrer | bb069c2 | 2013-10-18 19:51:01 -0500 | [diff] [blame] | 60 | hash_cls, |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 61 | params, |
| 62 | only_if, |
| 63 | skip_message |
| 64 | ) |
| 65 | return test_hash |
| 66 | |
| 67 | |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 68 | def hash_test(backend, hash_cls, params, only_if, skip_message): |
| 69 | if only_if is not None and not only_if(backend): |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 70 | pytest.skip(skip_message) |
| 71 | msg = params[0] |
| 72 | md = params[1] |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 73 | m = hash_cls(backend=backend) |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 74 | m.update(binascii.unhexlify(msg)) |
| 75 | assert m.hexdigest() == md.replace(" ", "").lower() |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 76 | digst = hash_cls(backend=backend, data=binascii.unhexlify(msg)).hexdigest() |
| 77 | assert digst == md.replace(" ", "").lower() |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 78 | |
| 79 | |
Paul Kehrer | bb069c2 | 2013-10-18 19:51:01 -0500 | [diff] [blame] | 80 | def generate_base_hash_test(hash_cls, 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, |
Paul Kehrer | bb069c2 | 2013-10-18 19:51:01 -0500 | [diff] [blame] | 87 | hash_cls, |
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 | |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 96 | def base_hash_test(backend, hash_cls, 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) |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 100 | m = hash_cls(backend=backend) |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 101 | 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 Kehrer | c179407 | 2013-10-18 21:42:57 -0500 | [diff] [blame] | 106 | |
| 107 | |
Donald Stufft | 2acd77a | 2013-10-19 19:49:30 -0400 | [diff] [blame] | 108 | def generate_long_string_hash_test(hash_factory, md, only_if=None, |
Paul Kehrer | c179407 | 2013-10-18 21:42:57 -0500 | [diff] [blame] | 109 | skip_message=None): |
| 110 | def test_long_string_hash(self): |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 111 | for backend in _ALL_BACKENDS: |
Paul Kehrer | c179407 | 2013-10-18 21:42:57 -0500 | [diff] [blame] | 112 | yield( |
| 113 | long_string_hash_test, |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 114 | backend, |
Paul Kehrer | c179407 | 2013-10-18 21:42:57 -0500 | [diff] [blame] | 115 | hash_factory, |
| 116 | md, |
| 117 | only_if, |
| 118 | skip_message |
| 119 | ) |
| 120 | return test_long_string_hash |
| 121 | |
| 122 | |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 123 | def 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 Kehrer | c179407 | 2013-10-18 21:42:57 -0500 | [diff] [blame] | 125 | pytest.skip(skip_message) |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 126 | m = hash_factory(backend=backend) |
Paul Kehrer | c179407 | 2013-10-18 21:42:57 -0500 | [diff] [blame] | 127 | m.update(b"a" * 1000000) |
| 128 | assert m.hexdigest() == md.lower() |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 129 | |
| 130 | |
| 131 | def 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 | |
| 148 | def 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 | |
| 162 | def 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 | |
| 175 | def 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 |