blob: a15e773c3a7a13faf63a2d14e73fe4ffa74e58ff [file] [log] [blame]
Alex Gaynorbd458ae2013-10-16 11:59:30 -07001import binascii
2import os
3
4import pytest
5
Alex Gaynora20631d2013-10-16 14:17:36 -07006from cryptography.bindings import _ALL_APIS
Alex Gaynorbd458ae2013-10-16 11:59:30 -07007from cryptography.primitives.block import BlockCipher
8
9
Alex Gaynor016eed12013-10-16 14:16:04 -070010def generate_encrypt_test(param_loader, path, file_names, cipher_factory,
Alex Gaynor512dd692013-10-16 14:27:52 -070011 mode_factory, only_if=lambda api: True,
12 skip_message=None):
Alex Gaynorbd458ae2013-10-16 11:59:30 -070013 def test_encryption(self):
Alex Gaynora20631d2013-10-16 14:17:36 -070014 for api in _ALL_APIS:
Alex Gaynor512dd692013-10-16 14:27:52 -070015 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 Gaynorbd458ae2013-10-16 11:59:30 -070026 return test_encryption
27
28
Alex Gaynor512dd692013-10-16 14:27:52 -070029def 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 Gaynorbd458ae2013-10-16 11:59:30 -070033 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 Gaynorfb39b3f2013-10-16 14:30:59 -070042 assert actual_ciphertext == binascii.unhexlify(ciphertext)
Paul Kehrerbde6fb52013-10-18 18:08:49 -050043
44
Paul Kehrerbb069c22013-10-18 19:51:01 -050045def generate_hash_test(param_loader, path, file_names, hash_cls,
Donald Stufft2acd77a2013-10-19 19:49:30 -040046 only_if=None, skip_message=None):
Paul Kehrerbde6fb52013-10-18 18:08:49 -050047 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,
Paul Kehrerbb069c22013-10-18 19:51:01 -050054 hash_cls,
Paul Kehrerbde6fb52013-10-18 18:08:49 -050055 params,
56 only_if,
57 skip_message
58 )
59 return test_hash
60
61
Paul Kehrerbb069c22013-10-18 19:51:01 -050062def hash_test(api, hash_cls, params, only_if, skip_message):
Donald Stufft2acd77a2013-10-19 19:49:30 -040063 if only_if is not None and not only_if(api):
Paul Kehrerbde6fb52013-10-18 18:08:49 -050064 pytest.skip(skip_message)
65 msg = params[0]
66 md = params[1]
Paul Kehrerbb069c22013-10-18 19:51:01 -050067 m = hash_cls(api=api)
Paul Kehrerbde6fb52013-10-18 18:08:49 -050068 m.update(binascii.unhexlify(msg))
69 assert m.hexdigest() == md.replace(" ", "").lower()
Paul Kehrerd4cb34d2013-10-19 23:05:12 -050070 digest = hash_cls(api=api, data=binascii.unhexlify(msg)).hexdigest()
71 assert digest == md.replace(" ", "").lower()
Paul Kehrerbde6fb52013-10-18 18:08:49 -050072
73
Paul Kehrerbb069c22013-10-18 19:51:01 -050074def generate_base_hash_test(hash_cls, digest_size, block_size,
Donald Stufft2acd77a2013-10-19 19:49:30 -040075 only_if=None, skip_message=None):
Paul Kehrerbde6fb52013-10-18 18:08:49 -050076 def test_base_hash(self):
77 for api in _ALL_APIS:
78 yield (
79 base_hash_test,
80 api,
Paul Kehrerbb069c22013-10-18 19:51:01 -050081 hash_cls,
Paul Kehrerbde6fb52013-10-18 18:08:49 -050082 digest_size,
83 block_size,
84 only_if,
85 skip_message,
86 )
87 return test_base_hash
88
89
Paul Kehrerbb069c22013-10-18 19:51:01 -050090def base_hash_test(api, hash_cls, digest_size, block_size, only_if,
Paul Kehrerbde6fb52013-10-18 18:08:49 -050091 skip_message):
Donald Stufft2acd77a2013-10-19 19:49:30 -040092 if only_if is not None and not only_if(api):
Paul Kehrerbde6fb52013-10-18 18:08:49 -050093 pytest.skip(skip_message)
Paul Kehrerbb069c22013-10-18 19:51:01 -050094 m = hash_cls(api=api)
Paul Kehrerbde6fb52013-10-18 18:08:49 -050095 assert m.digest_size == digest_size
96 assert m.block_size == block_size
97 m_copy = m.copy()
98 assert m != m_copy
99 assert m._ctx != m_copy._ctx
Paul Kehrerc1794072013-10-18 21:42:57 -0500100
101
Donald Stufft2acd77a2013-10-19 19:49:30 -0400102def generate_long_string_hash_test(hash_factory, md, only_if=None,
Paul Kehrerc1794072013-10-18 21:42:57 -0500103 skip_message=None):
104 def test_long_string_hash(self):
105 for api in _ALL_APIS:
106 yield(
107 long_string_hash_test,
108 api,
109 hash_factory,
110 md,
111 only_if,
112 skip_message
113 )
114 return test_long_string_hash
115
116
117def long_string_hash_test(api, hash_factory, md, only_if, skip_message):
Donald Stufft2acd77a2013-10-19 19:49:30 -0400118 if only_if is not None and not only_if(api):
Paul Kehrerc1794072013-10-18 21:42:57 -0500119 pytest.skip(skip_message)
Paul Kehrerd4cb34d2013-10-19 23:05:12 -0500120 m = hash_factory(api=api)
Paul Kehrerc1794072013-10-18 21:42:57 -0500121 m.update(b"a" * 1000000)
122 assert m.hexdigest() == md.lower()