Alex Gaynor | bd458ae | 2013-10-16 11:59:30 -0700 | [diff] [blame^] | 1 | import binascii |
| 2 | import os |
| 3 | |
| 4 | import pytest |
| 5 | |
| 6 | from cryptography.bindings import openssl |
| 7 | from cryptography.primitives.block import BlockCipher |
| 8 | |
| 9 | |
| 10 | def generate_encrypt_test(param_loader, cipher_name, vector_type, file_names, |
| 11 | cipher_factory, mode_factory, |
| 12 | only_if=lambda api: True): |
| 13 | def test_encryption(self): |
| 14 | for api in [openssl.api]: |
| 15 | if not only_if(api): |
| 16 | yield encrypt_skipped |
| 17 | else: |
| 18 | for file_name in file_names: |
| 19 | for params in param_loader( |
| 20 | os.path.join(cipher_name, vector_type, file_name) |
| 21 | ): |
| 22 | yield encrypt_test, api, cipher_factory, mode_factory, params |
| 23 | return test_encryption |
| 24 | |
| 25 | |
| 26 | def encrypt_test(api, cipher_factory, mode_factory, params): |
| 27 | plaintext = params.pop("plaintext") |
| 28 | ciphertext = params.pop("ciphertext") |
| 29 | cipher = BlockCipher( |
| 30 | cipher_factory(**params), |
| 31 | mode_factory(**params), |
| 32 | api |
| 33 | ) |
| 34 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 35 | actual_ciphertext += cipher.finalize() |
| 36 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 37 | |
| 38 | |
| 39 | def encrypt_skipped(): |
| 40 | pytest.skip("because reasons") |