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_cryptrec_vectors, load_nist_vectors
17
18
19@pytest.mark.supported(
20    only_if=lambda backend: backend.cipher_supported(
21        algorithms.Camellia(b"\x00" * 16), modes.ECB()
22    ),
23    skip_message="Does not support Camellia ECB",
24)
25@pytest.mark.requires_backend_interface(interface=CipherBackend)
26class TestCamelliaModeECB(object):
27    test_ecb = generate_encrypt_test(
28        load_cryptrec_vectors,
29        os.path.join("ciphers", "Camellia"),
30        [
31            "camellia-128-ecb.txt",
32            "camellia-192-ecb.txt",
33            "camellia-256-ecb.txt",
34        ],
35        lambda key, **kwargs: algorithms.Camellia(binascii.unhexlify(key)),
36        lambda **kwargs: modes.ECB(),
37    )
38
39
40@pytest.mark.supported(
41    only_if=lambda backend: backend.cipher_supported(
42        algorithms.Camellia(b"\x00" * 16), modes.CBC(b"\x00" * 16)
43    ),
44    skip_message="Does not support Camellia CBC",
45)
46@pytest.mark.requires_backend_interface(interface=CipherBackend)
47class TestCamelliaModeCBC(object):
48    test_cbc = generate_encrypt_test(
49        load_nist_vectors,
50        os.path.join("ciphers", "Camellia"),
51        ["camellia-cbc.txt"],
52        lambda key, **kwargs: algorithms.Camellia(binascii.unhexlify(key)),
53        lambda iv, **kwargs: modes.CBC(binascii.unhexlify(iv)),
54    )
55
56
57@pytest.mark.supported(
58    only_if=lambda backend: backend.cipher_supported(
59        algorithms.Camellia(b"\x00" * 16), modes.OFB(b"\x00" * 16)
60    ),
61    skip_message="Does not support Camellia OFB",
62)
63@pytest.mark.requires_backend_interface(interface=CipherBackend)
64class TestCamelliaModeOFB(object):
65    test_ofb = generate_encrypt_test(
66        load_nist_vectors,
67        os.path.join("ciphers", "Camellia"),
68        ["camellia-ofb.txt"],
69        lambda key, **kwargs: algorithms.Camellia(binascii.unhexlify(key)),
70        lambda iv, **kwargs: modes.OFB(binascii.unhexlify(iv)),
71    )
72
73
74@pytest.mark.supported(
75    only_if=lambda backend: backend.cipher_supported(
76        algorithms.Camellia(b"\x00" * 16), modes.CFB(b"\x00" * 16)
77    ),
78    skip_message="Does not support Camellia CFB",
79)
80@pytest.mark.requires_backend_interface(interface=CipherBackend)
81class TestCamelliaModeCFB(object):
82    test_cfb = generate_encrypt_test(
83        load_nist_vectors,
84        os.path.join("ciphers", "Camellia"),
85        ["camellia-cfb.txt"],
86        lambda key, **kwargs: algorithms.Camellia(binascii.unhexlify(key)),
87        lambda iv, **kwargs: modes.CFB(binascii.unhexlify(iv)),
88    )
89