def test_ciphers(self):
        backend = MultiBackend([
            DummyHashBackend([]),
            DummyCipherBackend([
                (algorithms.AES, modes.CBC),
            ])
        ])
        assert backend.cipher_supported(algorithms.AES(b"\x00" * 16),
                                        modes.CBC(b"\x00" * 16))

        cipher = Cipher(algorithms.AES(b"\x00" * 16),
                        modes.CBC(b"\x00" * 16),
                        backend=backend)
        cipher.encryptor()
        cipher.decryptor()

        cipher = Cipher(algorithms.Camellia(b"\x00" * 16),
                        modes.CBC(b"\x00" * 16),
                        backend=backend)
        with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_CIPHER):
            cipher.encryptor()
        with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_CIPHER):
            cipher.decryptor()
Example #2
0
def get_cipher(key, method, op_, iv_):
    '''get stream cipher'''
    # if method == 'bypass':
    #     return bypass()
    if method == 'rc4-md5':
        md5 = hashlib.md5()
        md5.update(key)
        md5.update(iv_)
        key = md5.digest()
        method = 'rc4'
    cipher = None

    if method in ('rc4', 'chacha20-ietf'):
        pass
    elif method.endswith('ctr'):
        mode = modes.CTR(iv_)
    elif method.endswith('cfb'):
        mode = modes.CFB(iv_)
    elif method.endswith('ofb'):
        mode = modes.OFB(iv_)
    else:
        raise ValueError('operation mode "%s" not supported!' % method.upper())

    if method == 'rc4':
        cipher = Cipher(algorithms.ARC4(key), None, default_backend())
    elif method == 'chacha20-ietf':
        return Chacha20IETF(method, key, iv_)
    elif method.startswith('aes'):
        cipher = Cipher(algorithms.AES(key), mode, default_backend())
    elif method.startswith('camellia'):
        cipher = Cipher(algorithms.Camellia(key), mode, default_backend())
    else:
        raise ValueError('crypto algorithm "%s" not supported!' %
                         method.upper())

    return cipher.encryptor() if op_ else cipher.decryptor()
Example #3
0
import os

import pytest

from cryptography.hazmat.backends.interfaces import CipherBackend
from cryptography.hazmat.primitives.ciphers import algorithms, modes

from .utils import generate_encrypt_test
from ...utils import (
    load_cryptrec_vectors, load_nist_vectors
)


@pytest.mark.supported(
    only_if=lambda backend: backend.cipher_supported(
        algorithms.Camellia("\x00" * 16), modes.ECB()
    ),
    skip_message="Does not support Camellia ECB",
)
@pytest.mark.requires_backend_interface(interface=CipherBackend)
class TestCamelliaModeECB(object):
    test_ECB = generate_encrypt_test(
        load_cryptrec_vectors,
        os.path.join("ciphers", "Camellia"),
        [
            "camellia-128-ecb.txt",
            "camellia-192-ecb.txt",
            "camellia-256-ecb.txt"
        ],
        lambda key, **kwargs: algorithms.Camellia(binascii.unhexlify(key)),
        lambda **kwargs: modes.ECB(),
import binascii
import os

import pytest

from cryptography.hazmat.backends.interfaces import CipherBackend
from cryptography.hazmat.primitives.ciphers import algorithms, modes

from .utils import generate_encrypt_test
from ...utils import load_cryptrec_vectors, load_nist_vectors


@pytest.mark.supported(
    only_if=lambda backend: backend.cipher_supported(
        algorithms.Camellia(b"\x00" * 16), modes.ECB()),
    skip_message="Does not support Camellia ECB",
)
@pytest.mark.requires_backend_interface(interface=CipherBackend)
class TestCamelliaModeECB(object):
    test_ecb = generate_encrypt_test(
        load_cryptrec_vectors,
        os.path.join("ciphers", "Camellia"),
        [
            "camellia-128-ecb.txt",
            "camellia-192-ecb.txt",
            "camellia-256-ecb.txt",
        ],
        lambda key, **kwargs: algorithms.Camellia(binascii.unhexlify(key)),
        lambda **kwargs: modes.ECB(),
    )
def choicesS(algo):
    if (algo == 1): return algorithms.AES(key)  #block size == 128 bits
    elif (algo == 2): return algorithms.TripleDES(key)  #64 bits
    elif (algo == 3): return algorithms.Camellia(key)  #128 bits
    elif (algo == 4): return algorithms.CAST5(key)  #64 bits
    elif (algo == 5): return algorithms.SEED(key)  #128 bits