blob: fabdca018c340741d6449b9a1555b92c854e4cc1 [file] [log] [blame]
Alex Gaynorbd458ae2013-10-16 11:59:30 -07001import binascii
2import os
3
4import pytest
5
Donald Stufftce0d7812013-10-27 16:52:33 -04006from cryptography.hazmat.bindings import _ALL_BACKENDS
Donald Stufftf04317a2013-10-27 16:44:30 -04007from cryptography.hazmat.primitives.block import BlockCipher
Alex Gaynorbd458ae2013-10-16 11:59:30 -07008
9
Alex Gaynor016eed12013-10-16 14:16:04 -070010def generate_encrypt_test(param_loader, path, file_names, cipher_factory,
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050011 mode_factory, only_if=lambda backend: True,
Alex Gaynor512dd692013-10-16 14:27:52 -070012 skip_message=None):
Alex Gaynorbd458ae2013-10-16 11:59:30 -070013 def test_encryption(self):
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050014 for backend in _ALL_BACKENDS:
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,
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050019 backend,
Alex Gaynor512dd692013-10-16 14:27:52 -070020 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
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050029def encrypt_test(backend, cipher_factory, mode_factory, params, only_if,
Alex Gaynor512dd692013-10-16 14:27:52 -070030 skip_message):
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050031 if not only_if(backend):
Alex Gaynor512dd692013-10-16 14:27:52 -070032 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),
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050038 backend
Alex Gaynorbd458ae2013-10-16 11:59:30 -070039 )
Paul Kehrer620c2ae2013-10-19 14:12:04 -050040 encryptor = cipher.encryptor()
41 actual_ciphertext = encryptor.update(binascii.unhexlify(plaintext))
42 actual_ciphertext += encryptor.finalize()
Alex Gaynorfb39b3f2013-10-16 14:30:59 -070043 assert actual_ciphertext == binascii.unhexlify(ciphertext)
Paul Kehrer620c2ae2013-10-19 14:12:04 -050044 decryptor = cipher.decryptor()
45 actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext))
46 actual_plaintext += decryptor.finalize()
47 assert actual_plaintext == binascii.unhexlify(plaintext)
Paul Kehrerbde6fb52013-10-18 18:08:49 -050048
49
Paul Kehrerbb069c22013-10-18 19:51:01 -050050def generate_hash_test(param_loader, path, file_names, hash_cls,
Donald Stufft2acd77a2013-10-19 19:49:30 -040051 only_if=None, skip_message=None):
Paul Kehrerbde6fb52013-10-18 18:08:49 -050052 def test_hash(self):
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050053 for backend in _ALL_BACKENDS:
Paul Kehrerbde6fb52013-10-18 18:08:49 -050054 for file_name in file_names:
55 for params in param_loader(os.path.join(path, file_name)):
56 yield (
57 hash_test,
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050058 backend,
Paul Kehrerbb069c22013-10-18 19:51:01 -050059 hash_cls,
Paul Kehrerbde6fb52013-10-18 18:08:49 -050060 params,
61 only_if,
62 skip_message
63 )
64 return test_hash
65
66
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050067def hash_test(backend, hash_cls, params, only_if, skip_message):
68 if only_if is not None and not only_if(backend):
Paul Kehrerbde6fb52013-10-18 18:08:49 -050069 pytest.skip(skip_message)
70 msg = params[0]
71 md = params[1]
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050072 m = hash_cls(backend=backend)
Paul Kehrerbde6fb52013-10-18 18:08:49 -050073 m.update(binascii.unhexlify(msg))
74 assert m.hexdigest() == md.replace(" ", "").lower()
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050075 digst = hash_cls(backend=backend, data=binascii.unhexlify(msg)).hexdigest()
76 assert digst == md.replace(" ", "").lower()
Paul Kehrerbde6fb52013-10-18 18:08:49 -050077
78
Paul Kehrerbb069c22013-10-18 19:51:01 -050079def generate_base_hash_test(hash_cls, digest_size, block_size,
Donald Stufft2acd77a2013-10-19 19:49:30 -040080 only_if=None, skip_message=None):
Paul Kehrerbde6fb52013-10-18 18:08:49 -050081 def test_base_hash(self):
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050082 for backend in _ALL_BACKENDS:
Paul Kehrerbde6fb52013-10-18 18:08:49 -050083 yield (
84 base_hash_test,
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050085 backend,
Paul Kehrerbb069c22013-10-18 19:51:01 -050086 hash_cls,
Paul Kehrerbde6fb52013-10-18 18:08:49 -050087 digest_size,
88 block_size,
89 only_if,
90 skip_message,
91 )
92 return test_base_hash
93
94
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050095def base_hash_test(backend, hash_cls, digest_size, block_size, only_if,
Paul Kehrerbde6fb52013-10-18 18:08:49 -050096 skip_message):
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050097 if only_if is not None and not only_if(backend):
Paul Kehrerbde6fb52013-10-18 18:08:49 -050098 pytest.skip(skip_message)
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050099 m = hash_cls(backend=backend)
Paul Kehrerbde6fb52013-10-18 18:08:49 -0500100 assert m.digest_size == digest_size
101 assert m.block_size == block_size
102 m_copy = m.copy()
103 assert m != m_copy
104 assert m._ctx != m_copy._ctx
Paul Kehrerc1794072013-10-18 21:42:57 -0500105
106
Donald Stufft2acd77a2013-10-19 19:49:30 -0400107def generate_long_string_hash_test(hash_factory, md, only_if=None,
Paul Kehrerc1794072013-10-18 21:42:57 -0500108 skip_message=None):
109 def test_long_string_hash(self):
Paul Kehrerdb37d0e2013-10-22 20:13:06 -0500110 for backend in _ALL_BACKENDS:
Paul Kehrerc1794072013-10-18 21:42:57 -0500111 yield(
112 long_string_hash_test,
Paul Kehrerdb37d0e2013-10-22 20:13:06 -0500113 backend,
Paul Kehrerc1794072013-10-18 21:42:57 -0500114 hash_factory,
115 md,
116 only_if,
117 skip_message
118 )
119 return test_long_string_hash
120
121
Paul Kehrerdb37d0e2013-10-22 20:13:06 -0500122def long_string_hash_test(backend, hash_factory, md, only_if, skip_message):
123 if only_if is not None and not only_if(backend):
Paul Kehrerc1794072013-10-18 21:42:57 -0500124 pytest.skip(skip_message)
Paul Kehrerdb37d0e2013-10-22 20:13:06 -0500125 m = hash_factory(backend=backend)
Paul Kehrerc1794072013-10-18 21:42:57 -0500126 m.update(b"a" * 1000000)
127 assert m.hexdigest() == md.lower()