1# This file is dual licensed under the terms of the Apache License, Version
2# 2.0, and the BSD License. See the LICENSE file in the root of this repository
3# for complete details.
4
5from __future__ import absolute_import, division, print_function
6
7import binascii
8import os
9
10import pytest
11
12from cryptography.hazmat.backends.interfaces import CipherBackend
13from cryptography.hazmat.primitives.ciphers import algorithms, modes
14
15from .utils import generate_encrypt_test
16from ...utils import load_nist_vectors
17
18
19@pytest.mark.supported(
20    only_if=lambda backend: backend.cipher_supported(
21        algorithms.Blowfish(b"\x00" * 56), modes.ECB()
22    ),
23    skip_message="Does not support Blowfish ECB",
24)
25@pytest.mark.requires_backend_interface(interface=CipherBackend)
26class TestBlowfishModeECB(object):
27    test_ecb = generate_encrypt_test(
28        load_nist_vectors,
29        os.path.join("ciphers", "Blowfish"),
30        ["bf-ecb.txt"],
31        lambda key, **kwargs: algorithms.Blowfish(binascii.unhexlify(key)),
32        lambda **kwargs: modes.ECB(),
33    )
34
35
36@pytest.mark.supported(
37    only_if=lambda backend: backend.cipher_supported(
38        algorithms.Blowfish(b"\x00" * 56), modes.CBC(b"\x00" * 8)
39    ),
40    skip_message="Does not support Blowfish CBC",
41)
42@pytest.mark.requires_backend_interface(interface=CipherBackend)
43class TestBlowfishModeCBC(object):
44    test_cbc = generate_encrypt_test(
45        load_nist_vectors,
46        os.path.join("ciphers", "Blowfish"),
47        ["bf-cbc.txt"],
48        lambda key, **kwargs: algorithms.Blowfish(binascii.unhexlify(key)),
49        lambda iv, **kwargs: modes.CBC(binascii.unhexlify(iv)),
50    )
51
52
53@pytest.mark.supported(
54    only_if=lambda backend: backend.cipher_supported(
55        algorithms.Blowfish(b"\x00" * 56), modes.OFB(b"\x00" * 8)
56    ),
57    skip_message="Does not support Blowfish OFB",
58)
59@pytest.mark.requires_backend_interface(interface=CipherBackend)
60class TestBlowfishModeOFB(object):
61    test_ofb = generate_encrypt_test(
62        load_nist_vectors,
63        os.path.join("ciphers", "Blowfish"),
64        ["bf-ofb.txt"],
65        lambda key, **kwargs: algorithms.Blowfish(binascii.unhexlify(key)),
66        lambda iv, **kwargs: modes.OFB(binascii.unhexlify(iv)),
67    )
68
69
70@pytest.mark.supported(
71    only_if=lambda backend: backend.cipher_supported(
72        algorithms.Blowfish(b"\x00" * 56), modes.CFB(b"\x00" * 8)
73    ),
74    skip_message="Does not support Blowfish CFB",
75)
76@pytest.mark.requires_backend_interface(interface=CipherBackend)
77class TestBlowfishModeCFB(object):
78    test_cfb = generate_encrypt_test(
79        load_nist_vectors,
80        os.path.join("ciphers", "Blowfish"),
81        ["bf-cfb.txt"],
82        lambda key, **kwargs: algorithms.Blowfish(binascii.unhexlify(key)),
83        lambda iv, **kwargs: modes.CFB(binascii.unhexlify(iv)),
84    )
85