Alex Gaynor | bd458ae | 2013-10-16 11:59:30 -0700 | [diff] [blame] | 1 | import binascii |
| 2 | import os |
| 3 | |
| 4 | import pytest |
| 5 | |
Alex Gaynor | a20631d | 2013-10-16 14:17:36 -0700 | [diff] [blame] | 6 | from cryptography.bindings import _ALL_APIS |
Alex Gaynor | bd458ae | 2013-10-16 11:59:30 -0700 | [diff] [blame] | 7 | from cryptography.primitives.block import BlockCipher |
| 8 | |
| 9 | |
Alex Gaynor | 016eed1 | 2013-10-16 14:16:04 -0700 | [diff] [blame] | 10 | def generate_encrypt_test(param_loader, path, file_names, cipher_factory, |
Alex Gaynor | 512dd69 | 2013-10-16 14:27:52 -0700 | [diff] [blame] | 11 | mode_factory, only_if=lambda api: True, |
| 12 | skip_message=None): |
Alex Gaynor | bd458ae | 2013-10-16 11:59:30 -0700 | [diff] [blame] | 13 | def test_encryption(self): |
Alex Gaynor | a20631d | 2013-10-16 14:17:36 -0700 | [diff] [blame] | 14 | for api in _ALL_APIS: |
Alex Gaynor | 512dd69 | 2013-10-16 14:27:52 -0700 | [diff] [blame] | 15 | for file_name in file_names: |
| 16 | for params in param_loader(os.path.join(path, file_name)): |
| 17 | yield ( |
| 18 | encrypt_test, |
| 19 | api, |
| 20 | cipher_factory, |
| 21 | mode_factory, |
| 22 | params, |
| 23 | only_if, |
| 24 | skip_message |
| 25 | ) |
Alex Gaynor | bd458ae | 2013-10-16 11:59:30 -0700 | [diff] [blame] | 26 | return test_encryption |
| 27 | |
| 28 | |
Alex Gaynor | 512dd69 | 2013-10-16 14:27:52 -0700 | [diff] [blame] | 29 | def encrypt_test(api, cipher_factory, mode_factory, params, only_if, |
| 30 | skip_message): |
| 31 | if not only_if(api): |
| 32 | pytest.skip(skip_message) |
Alex Gaynor | bd458ae | 2013-10-16 11:59:30 -0700 | [diff] [blame] | 33 | plaintext = params.pop("plaintext") |
| 34 | ciphertext = params.pop("ciphertext") |
| 35 | cipher = BlockCipher( |
| 36 | cipher_factory(**params), |
| 37 | mode_factory(**params), |
| 38 | api |
| 39 | ) |
| 40 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 41 | actual_ciphertext += cipher.finalize() |
Alex Gaynor | fb39b3f | 2013-10-16 14:30:59 -0700 | [diff] [blame] | 42 | assert actual_ciphertext == binascii.unhexlify(ciphertext) |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame^] | 43 | |
| 44 | |
| 45 | def generate_hash_test(param_loader, path, file_names, hash_factory, |
| 46 | only_if=lambda api: True, skip_message=None): |
| 47 | def test_hash(self): |
| 48 | for api in _ALL_APIS: |
| 49 | for file_name in file_names: |
| 50 | for params in param_loader(os.path.join(path, file_name)): |
| 51 | yield ( |
| 52 | hash_test, |
| 53 | api, |
| 54 | hash_factory, |
| 55 | params, |
| 56 | only_if, |
| 57 | skip_message |
| 58 | ) |
| 59 | return test_hash |
| 60 | |
| 61 | |
| 62 | def hash_test(api, hash_factory, params, only_if, skip_message): |
| 63 | if not only_if(api): |
| 64 | pytest.skip(skip_message) |
| 65 | msg = params[0] |
| 66 | md = params[1] |
| 67 | m = hash_factory(api) |
| 68 | m.update(binascii.unhexlify(msg)) |
| 69 | assert m.hexdigest() == md.replace(" ", "").lower() |
| 70 | |
| 71 | |
| 72 | def generate_base_hash_test(hash_factory, digest_size, block_size, |
| 73 | only_if=lambda api: True, skip_message=None): |
| 74 | def test_base_hash(self): |
| 75 | for api in _ALL_APIS: |
| 76 | yield ( |
| 77 | base_hash_test, |
| 78 | api, |
| 79 | hash_factory, |
| 80 | digest_size, |
| 81 | block_size, |
| 82 | only_if, |
| 83 | skip_message, |
| 84 | ) |
| 85 | return test_base_hash |
| 86 | |
| 87 | |
| 88 | def base_hash_test(api, hash_factory, digest_size, block_size, only_if, |
| 89 | skip_message): |
| 90 | if not only_if(api): |
| 91 | pytest.skip(skip_message) |
| 92 | m = hash_factory(api=api) |
| 93 | assert m.digest_size == digest_size |
| 94 | assert m.block_size == block_size |
| 95 | m_copy = m.copy() |
| 96 | assert m != m_copy |
| 97 | assert m._ctx != m_copy._ctx |