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 |
Donald Stufft | f04317a | 2013-10-27 16:44:30 -0400 | [diff] [blame] | 9 | from cryptography.hazmat.primitives.block import BlockCipher |
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") |
| 37 | cipher = BlockCipher( |
| 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 | |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 69 | def hash_test(backend, hash_cls, params, only_if, skip_message): |
| 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 | 69aeb49 | 2013-10-30 11:35:37 -0700 | [diff] [blame^] | 74 | m = hashes.Hash(hash_cls, backend=backend) |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 75 | m.update(binascii.unhexlify(msg)) |
David Reid | 69aeb49 | 2013-10-30 11:35:37 -0700 | [diff] [blame^] | 76 | assert m.finalize() == binascii.unhexlify(md.replace(" ", "").lower()) |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 77 | |
| 78 | |
David Reid | 69aeb49 | 2013-10-30 11:35:37 -0700 | [diff] [blame^] | 79 | def generate_base_hash_test(algorithm, digest_size, block_size, |
Donald Stufft | 2acd77a | 2013-10-19 19:49:30 -0400 | [diff] [blame] | 80 | only_if=None, skip_message=None): |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 81 | def test_base_hash(self): |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 82 | for backend in _ALL_BACKENDS: |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 83 | yield ( |
| 84 | base_hash_test, |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 85 | backend, |
David Reid | 69aeb49 | 2013-10-30 11:35:37 -0700 | [diff] [blame^] | 86 | algorithm, |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 87 | digest_size, |
| 88 | block_size, |
| 89 | only_if, |
| 90 | skip_message, |
| 91 | ) |
| 92 | return test_base_hash |
| 93 | |
| 94 | |
David Reid | 69aeb49 | 2013-10-30 11:35:37 -0700 | [diff] [blame^] | 95 | def base_hash_test(backend, algorithm, digest_size, block_size, only_if, |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 96 | skip_message): |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 97 | if only_if is not None and not only_if(backend): |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 98 | pytest.skip(skip_message) |
David Reid | 69aeb49 | 2013-10-30 11:35:37 -0700 | [diff] [blame^] | 99 | |
| 100 | m = hashes.Hash(algorithm, backend=backend) |
| 101 | assert m.algorithm.digest_size == digest_size |
| 102 | assert m.algorithm.block_size == block_size |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 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 | |
David Reid | 69aeb49 | 2013-10-30 11:35:37 -0700 | [diff] [blame^] | 123 | def long_string_hash_test(backend, algorithm, md, only_if, skip_message): |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 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) |
David Reid | 69aeb49 | 2013-10-30 11:35:37 -0700 | [diff] [blame^] | 126 | m = hashes.Hash(algorithm, backend=backend) |
Paul Kehrer | c179407 | 2013-10-18 21:42:57 -0500 | [diff] [blame] | 127 | m.update(b"a" * 1000000) |
David Reid | 69aeb49 | 2013-10-30 11:35:37 -0700 | [diff] [blame^] | 128 | assert m.finalize() == binascii.unhexlify(md.lower()) |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 129 | |
| 130 | |
Paul Kehrer | 2824ab7 | 2013-10-28 11:06:55 -0500 | [diff] [blame] | 131 | def generate_hmac_test(param_loader, path, file_names, digestmod, |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 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, |
Paul Kehrer | 2824ab7 | 2013-10-28 11:06:55 -0500 | [diff] [blame] | 140 | digestmod, |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 141 | params, |
| 142 | only_if, |
| 143 | skip_message |
| 144 | ) |
| 145 | return test_hmac |
| 146 | |
| 147 | |
Paul Kehrer | 2824ab7 | 2013-10-28 11:06:55 -0500 | [diff] [blame] | 148 | def hmac_test(backend, digestmod, params, only_if, skip_message): |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 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] |
Paul Kehrer | 2824ab7 | 2013-10-28 11:06:55 -0500 | [diff] [blame] | 154 | h = hmac.HMAC(binascii.unhexlify(key), digestmod=digestmod) |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 155 | h.update(binascii.unhexlify(msg)) |
| 156 | assert h.hexdigest() == md |
Paul Kehrer | 2824ab7 | 2013-10-28 11:06:55 -0500 | [diff] [blame] | 157 | digest = hmac.HMAC(binascii.unhexlify(key), digestmod=digestmod, |
| 158 | msg=binascii.unhexlify(msg)).hexdigest() |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 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 | |
Paul Kehrer | 2824ab7 | 2013-10-28 11:06:55 -0500 | [diff] [blame] | 175 | def base_hmac_test(backend, digestmod, only_if, skip_message): |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 176 | if only_if is not None and not only_if(backend): |
| 177 | pytest.skip(skip_message) |
| 178 | key = b"ab" |
Paul Kehrer | 2824ab7 | 2013-10-28 11:06:55 -0500 | [diff] [blame] | 179 | h = hmac.HMAC(binascii.unhexlify(key), digestmod=digestmod) |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 180 | h_copy = h.copy() |
| 181 | assert h != h_copy |
| 182 | assert h._ctx != h_copy._ctx |