aboutsummaryrefslogtreecommitdiffstats
path: root/tests/primitives/utils.py
blob: a3759b03dc20e34a286324dfbb491e2bc4c7a535 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import binascii
import os

import pytest

from cryptography.bindings import _ALL_APIS
from cryptography.primitives.block import BlockCipher


def generate_encrypt_test(param_loader, path, file_names, cipher_factory,
                          mode_factory, only_if=lambda api: True,
                          skip_message=None):
    def test_encryption(self):
        for api in _ALL_APIS:
            for file_name in file_names:
                for params in param_loader(os.path.join(path, file_name)):
                    yield (
                        encrypt_test,
                        api,
                        cipher_factory,
                        mode_factory,
                        params,
                        only_if,
                        skip_message
                    )
    return test_encryption


def encrypt_test(api, cipher_factory, mode_factory, params, only_if,
                 skip_message):
    if not only_if(api):
        pytest.skip(skip_message)
    plaintext = params.pop("plaintext")
    ciphertext = params.pop("ciphertext")
    cipher = BlockCipher(
        cipher_factory(**params),
        mode_factory(**params),
        api
    )
    actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext))
    actual_ciphertext += cipher.finalize()
    assert actual_ciphertext == binascii.unhexlify(ciphertext)


def generate_hash_test(param_loader, path, file_names, hash_cls,
                       only_if=None, skip_message=None):
    def test_hash(self):
        for api in _ALL_APIS:
            for file_name in file_names:
                for params in param_loader(os.path.join(path, file_name)):
                    yield (
                        hash_test,
                        api,
                        hash_cls,
                        params,
                        only_if,
                        skip_message
                    )
    return test_hash


def hash_test(api, hash_cls, params, only_if, skip_message):
    if only_if is not None and not only_if(api):
        pytest.skip(skip_message)
    msg = params[0]
    md = params[1]
    m = hash_cls(api=api)
    m.update(binascii.unhexlify(msg))
    assert m.hexdigest() == md.replace(" ", "").lower()


def generate_base_hash_test(hash_cls, digest_size, block_size,
                            only_if=None, skip_message=None):
    def test_base_hash(self):
        for api in _ALL_APIS:
            yield (
                base_hash_test,
                api,
                hash_cls,
                digest_size,
                block_size,
                only_if,
                skip_message,
            )
    return test_base_hash


def base_hash_test(api, hash_cls, digest_size, block_size, only_if,
                   skip_message):
    if only_if is not None and not only_if(api):
        pytest.skip(skip_message)
    m = hash_cls(api=api)
    assert m.digest_size == digest_size
    assert m.block_size == block_size
    m_copy = m.copy()
    assert m != m_copy
    assert m._ctx != m_copy._ctx


def generate_long_string_hash_test(hash_factory, md, only_if=None,
                                   skip_message=None):
    def test_long_string_hash(self):
        for api in _ALL_APIS:
            yield(
                long_string_hash_test,
                api,
                hash_factory,
                md,
                only_if,
                skip_message
            )
    return test_long_string_hash


def long_string_hash_test(api, hash_factory, md, only_if, skip_message):
    if only_if is not None and not only_if(api):
        pytest.skip(skip_message)
    m = hash_factory(api)
    m.update(b"a" * 1000000)
    assert m.hexdigest() == md.lower()