Exemple #1
0
    def finalize_options(self):
        from cryptography.hazmat.bindings.commoncrypto.binding import Binding as CommonCryptoBinding
        from cryptography.hazmat.bindings.openssl.binding import Binding as OpenSSLBinding
        from cryptography.hazmat.primitives import constant_time, padding

        self.distribution.ext_modules = [
            OpenSSLBinding().ffi.verifier.get_extension(),
            constant_time._ffi.verifier.get_extension(),
            padding._ffi.verifier.get_extension(),
        ]
        if CommonCryptoBinding.is_available():
            self.distribution.ext_modules.append(CommonCryptoBinding().ffi.verifier.get_extension())

        build.finalize_options(self)
Exemple #2
0
    def __init__(self):
        self._binding = Binding()
        self._ffi = self._binding.ffi
        self._lib = self._binding.lib

        self._cipher_registry = {}
        self._register_default_ciphers()
        self._hash_mapping = {
            "md5": HashMethods(
                "CC_MD5_CTX *", self._lib.CC_MD5_Init,
                self._lib.CC_MD5_Update, self._lib.CC_MD5_Final
            ),
            "sha1": HashMethods(
                "CC_SHA1_CTX *", self._lib.CC_SHA1_Init,
                self._lib.CC_SHA1_Update, self._lib.CC_SHA1_Final
            ),
            "sha224": HashMethods(
                "CC_SHA256_CTX *", self._lib.CC_SHA224_Init,
                self._lib.CC_SHA224_Update, self._lib.CC_SHA224_Final
            ),
            "sha256": HashMethods(
                "CC_SHA256_CTX *", self._lib.CC_SHA256_Init,
                self._lib.CC_SHA256_Update, self._lib.CC_SHA256_Final
            ),
            "sha384": HashMethods(
                "CC_SHA512_CTX *", self._lib.CC_SHA384_Init,
                self._lib.CC_SHA384_Update, self._lib.CC_SHA384_Final
            ),
            "sha512": HashMethods(
                "CC_SHA512_CTX *", self._lib.CC_SHA512_Init,
                self._lib.CC_SHA512_Update, self._lib.CC_SHA512_Final
            ),
        }

        self._supported_hmac_algorithms = {
            "md5": self._lib.kCCHmacAlgMD5,
            "sha1": self._lib.kCCHmacAlgSHA1,
            "sha224": self._lib.kCCHmacAlgSHA224,
            "sha256": self._lib.kCCHmacAlgSHA256,
            "sha384": self._lib.kCCHmacAlgSHA384,
            "sha512": self._lib.kCCHmacAlgSHA512,
        }

        self._supported_pbkdf2_hmac_algorithms = {
            "sha1": self._lib.kCCPRFHmacAlgSHA1,
            "sha224": self._lib.kCCPRFHmacAlgSHA224,
            "sha256": self._lib.kCCPRFHmacAlgSHA256,
            "sha384": self._lib.kCCPRFHmacAlgSHA384,
            "sha512": self._lib.kCCPRFHmacAlgSHA512,
        }
Exemple #3
0
def get_ext_modules():
    from cryptography.hazmat.bindings.commoncrypto.binding import (
        Binding as CommonCryptoBinding)
    from cryptography.hazmat.bindings.openssl.binding import (Binding as
                                                              OpenSSLBinding)
    from cryptography.hazmat.primitives import constant_time, padding

    ext_modules = [
        OpenSSLBinding().ffi.verifier.get_extension(),
        constant_time._ffi.verifier.get_extension(),
        padding._ffi.verifier.get_extension()
    ]
    if CommonCryptoBinding.is_available():
        ext_modules.append(CommonCryptoBinding().ffi.verifier.get_extension())
    return ext_modules
Exemple #4
0
def _available_backends():
    global _available_backends_list

    if _available_backends_list is None:
        _available_backends_list = []

        if CommonCryptoBinding.is_available():
            from cryptography.hazmat.backends import commoncrypto
            _available_backends_list.append(commoncrypto.backend)

        if OpenSSLBinding.is_available():
            from cryptography.hazmat.backends import openssl
            _available_backends_list.append(openssl.backend)

    return _available_backends_list
Exemple #5
0
def _available_backends():
    global _available_backends_list

    if _available_backends_list is None:
        _available_backends_list = []

        if CommonCryptoBinding.is_available():
            from cryptography.hazmat.backends import commoncrypto
            _available_backends_list.append(commoncrypto.backend)

        if OpenSSLBinding.is_available():
            from cryptography.hazmat.backends import openssl
            _available_backends_list.append(openssl.backend)

    return _available_backends_list
def get_ext_modules():
    from cryptography.hazmat.bindings.commoncrypto.binding import (
        Binding as CommonCryptoBinding
    )
    from cryptography.hazmat.bindings.openssl.binding import (
        Binding as OpenSSLBinding
    )
    from cryptography.hazmat.primitives import constant_time, padding

    ext_modules = [
        OpenSSLBinding().ffi.verifier.get_extension(),
        constant_time._ffi.verifier.get_extension(),
        padding._ffi.verifier.get_extension()
    ]
    if CommonCryptoBinding.is_available():
        ext_modules.append(CommonCryptoBinding().ffi.verifier.get_extension())
    return ext_modules
Exemple #7
0
    def finalize_options(self):
        from cryptography.hazmat.bindings.commoncrypto.binding import (
            Binding as CommonCryptoBinding)
        from cryptography.hazmat.bindings.openssl.binding import (
            Binding as OpenSSLBinding)
        from cryptography.hazmat.primitives import constant_time, padding

        self.distribution.ext_modules = [
            OpenSSLBinding().ffi.verifier.get_extension(),
            constant_time._ffi.verifier.get_extension(),
            padding._ffi.verifier.get_extension()
        ]
        if CommonCryptoBinding.is_available():
            self.distribution.ext_modules.append(
                CommonCryptoBinding().ffi.verifier.get_extension())

        build.finalize_options(self)
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from cryptography.hazmat.backends import openssl
from cryptography.hazmat.bindings.commoncrypto.binding import (
    Binding as CCBinding
)

_ALL_BACKENDS = [openssl.backend]

if CCBinding.is_available():
    from cryptography.hazmat.backends import commoncrypto
    _ALL_BACKENDS.append(commoncrypto.backend)


def default_backend():
    return openssl.backend
from cryptography import utils
from cryptography.exceptions import UnsupportedAlgorithm, InternalError
from cryptography.hazmat.bindings.commoncrypto.binding import Binding
from cryptography.hazmat.primitives import interfaces
from cryptography.hazmat.primitives.ciphers.algorithms import AES
from cryptography.hazmat.primitives.ciphers.base import Cipher
from cryptography.hazmat.primitives.ciphers.modes import CBC, GCM


@utils.register_interface(interfaces.CipherAlgorithm)
class DummyCipher(object):
    name = "dummy-cipher"
    block_size = 128


@pytest.mark.skipif(not Binding.is_available(),
                    reason="CommonCrypto not available")
class TestCommonCrypto(object):
    def test_supports_cipher(self):
        from cryptography.hazmat.backends.commoncrypto.backend import backend
        assert backend.cipher_supported(None, None) is False

    def test_register_duplicate_cipher_adapter(self):
        from cryptography.hazmat.backends.commoncrypto.backend import backend
        with pytest.raises(ValueError):
            backend._register_cipher_adapter(
                AES, backend._lib.kCCAlgorithmAES128,
                CBC, backend._lib.kCCModeCBC
            )

    def test_handle_response(self):
Exemple #10
0
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from cryptography.hazmat.backends import openssl
from cryptography.hazmat.backends.multibackend import MultiBackend
from cryptography.hazmat.bindings.commoncrypto.binding import (
    Binding as CommonCryptoBinding)

_ALL_BACKENDS = []

if CommonCryptoBinding.is_available():
    from cryptography.hazmat.backends import commoncrypto
    _ALL_BACKENDS.append(commoncrypto.backend)

_ALL_BACKENDS.append(openssl.backend)

_default_backend = MultiBackend(_ALL_BACKENDS)


def default_backend():
    return _default_backend
Exemple #11
0
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from cryptography.hazmat.backends import openssl
from cryptography.hazmat.backends.multibackend import MultiBackend
from cryptography.hazmat.bindings.commoncrypto.binding import (
    Binding as CommonCryptoBinding
)

_ALL_BACKENDS = [openssl.backend]

if CommonCryptoBinding.is_available():
    from cryptography.hazmat.backends import commoncrypto
    _ALL_BACKENDS.append(commoncrypto.backend)


_default_backend = MultiBackend(_ALL_BACKENDS)


def default_backend():
    return _default_backend
Exemple #12
0
from cryptography.hazmat.bindings.commoncrypto.binding import Binding
from cryptography.hazmat.primitives import interfaces
from cryptography.hazmat.primitives.ciphers.algorithms import AES
from cryptography.hazmat.primitives.ciphers.base import Cipher
from cryptography.hazmat.primitives.ciphers.modes import CBC, GCM

from ...utils import raises_unsupported_algorithm


@utils.register_interface(interfaces.CipherAlgorithm)
class DummyCipher(object):
    name = "dummy-cipher"
    block_size = 128


@pytest.mark.skipif(not Binding.is_available(),
                    reason="CommonCrypto not available")
class TestCommonCrypto(object):
    def test_supports_cipher(self):
        from cryptography.hazmat.backends.commoncrypto.backend import backend
        assert backend.cipher_supported(None, None) is False

    def test_register_duplicate_cipher_adapter(self):
        from cryptography.hazmat.backends.commoncrypto.backend import backend
        with pytest.raises(ValueError):
            backend._register_cipher_adapter(
                AES, backend._lib.kCCAlgorithmAES128,
                CBC, backend._lib.kCCModeCBC
            )

    def test_handle_response(self):
Exemple #13
0
 def test_binding_returns_same_lib(self):
     binding = Binding()
     binding2 = Binding()
     assert binding.lib == binding2.lib
     assert binding.ffi == binding2.ffi
Exemple #14
0
 def test_binding_loads(self):
     binding = Binding()
     assert binding
     assert binding.lib
     assert binding.ffi