blob: e35c915ae3bf8a64db88d981c000753f2e17752e [file] [log] [blame]
Alex Gaynorbd458ae2013-10-16 11:59:30 -07001import binascii
2import os
3
4import pytest
5
6from cryptography.bindings import openssl
7from cryptography.primitives.block import BlockCipher
8
9
10def 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
26def 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
39def encrypt_skipped():
40 pytest.skip("because reasons")