From d706515791ad42a527b39e899b89e4f669dff430 Mon Sep 17 00:00:00 2001 From: David Reid Date: Mon, 11 Nov 2013 11:48:39 -0800 Subject: Move register so it can be used by cryptography.hazmat.bindings.interfaces. --- cryptography/hazmat/bindings/interfaces.py | 57 +++++++++++++++++++++++++ cryptography/hazmat/bindings/openssl/backend.py | 10 ++++- cryptography/hazmat/primitives/ciphers/base.py | 3 +- cryptography/hazmat/primitives/ciphers/modes.py | 19 +++++---- cryptography/hazmat/primitives/hashes.py | 19 +++++---- cryptography/hazmat/primitives/hmac.py | 3 +- cryptography/hazmat/primitives/interfaces.py | 7 --- cryptography/hazmat/primitives/padding.py | 5 ++- cryptography/utils.py | 21 +++++++++ 9 files changed, 114 insertions(+), 30 deletions(-) create mode 100644 cryptography/hazmat/bindings/interfaces.py create mode 100644 cryptography/utils.py diff --git a/cryptography/hazmat/bindings/interfaces.py b/cryptography/hazmat/bindings/interfaces.py new file mode 100644 index 00000000..e9c8fef3 --- /dev/null +++ b/cryptography/hazmat/bindings/interfaces.py @@ -0,0 +1,57 @@ +# 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 __future__ import absolute_import, division, print_function + +import abc + +import six + + +class CiphersProviderBackend(six.with_metaclass(abc.ABCMeta)): + @abc.abstractproperty + def ciphers(self): + """ + """ + + +class CiphersProvider(six.with_metaclass(abc.ABCMeta)): + """ + wat + """ + + +class HashesProviderBackend(six.with_metaclass(abc.ABCMeta)): + @abc.abstractproperty + def hashes(self): + """ + """ + + +class HashesProvider(six.with_metaclass(abc.ABCMeta)): + """ + wat + """ + + +class HMACsProviderBackend(six.with_metaclass(abc.ABCMeta)): + @abc.abstractproperty + def hmacs(self): + """ + """ + + +class HMACsProvider(six.with_metaclass(abc.ABCMeta)): + """ + wat + """ diff --git a/cryptography/hazmat/bindings/openssl/backend.py b/cryptography/hazmat/bindings/openssl/backend.py index 844e175f..a7c0741c 100644 --- a/cryptography/hazmat/bindings/openssl/backend.py +++ b/cryptography/hazmat/bindings/openssl/backend.py @@ -18,7 +18,12 @@ import sys import cffi +from cryptography import utils from cryptography.exceptions import UnsupportedAlgorithm +from cryptography.hazmat.bindings.interfaces import ( + CiphersProviderBackend, CiphersProvider, HashesProviderBackend, + HashesProvider, HMACsProviderBackend, HMACsProvider +) from cryptography.hazmat.primitives import interfaces from cryptography.hazmat.primitives.ciphers.algorithms import ( AES, Blowfish, Camellia, CAST5, TripleDES, ARC4, @@ -28,6 +33,9 @@ from cryptography.hazmat.primitives.ciphers.modes import ( ) +@utils.register_interface(CiphersProviderBackend) +@utils.register_interface(HashesProviderBackend) +@utils.register_interface(HMACsProviderBackend) class Backend(object): """ OpenSSL API wrapper. @@ -196,7 +204,7 @@ class GetCipherByName(object): return backend.lib.EVP_get_cipherbyname(cipher_name.encode("ascii")) -@interfaces.register(interfaces.CipherContext) +@utils.register_interface(interfaces.CipherContext) class _CipherContext(object): _ENCRYPT = 1 _DECRYPT = 0 diff --git a/cryptography/hazmat/primitives/ciphers/base.py b/cryptography/hazmat/primitives/ciphers/base.py index 78bf7e0c..3d733afc 100644 --- a/cryptography/hazmat/primitives/ciphers/base.py +++ b/cryptography/hazmat/primitives/ciphers/base.py @@ -13,6 +13,7 @@ from __future__ import absolute_import, division, print_function +from cryptography import utils from cryptography.exceptions import AlreadyFinalized from cryptography.hazmat.primitives import interfaces @@ -42,7 +43,7 @@ class Cipher(object): )) -@interfaces.register(interfaces.CipherContext) +@utils.register_interface(interfaces.CipherContext) class _CipherContext(object): def __init__(self, ctx): self._ctx = ctx diff --git a/cryptography/hazmat/primitives/ciphers/modes.py b/cryptography/hazmat/primitives/ciphers/modes.py index 915fd83d..1d0de689 100644 --- a/cryptography/hazmat/primitives/ciphers/modes.py +++ b/cryptography/hazmat/primitives/ciphers/modes.py @@ -13,11 +13,12 @@ from __future__ import absolute_import, division, print_function +from cryptography import utils from cryptography.hazmat.primitives import interfaces -@interfaces.register(interfaces.Mode) -@interfaces.register(interfaces.ModeWithInitializationVector) +@utils.register_interface(interfaces.Mode) +@utils.register_interface(interfaces.ModeWithInitializationVector) class CBC(object): name = "CBC" @@ -25,13 +26,13 @@ class CBC(object): self.initialization_vector = initialization_vector -@interfaces.register(interfaces.Mode) +@utils.register_interface(interfaces.Mode) class ECB(object): name = "ECB" -@interfaces.register(interfaces.Mode) -@interfaces.register(interfaces.ModeWithInitializationVector) +@utils.register_interface(interfaces.Mode) +@utils.register_interface(interfaces.ModeWithInitializationVector) class OFB(object): name = "OFB" @@ -39,8 +40,8 @@ class OFB(object): self.initialization_vector = initialization_vector -@interfaces.register(interfaces.Mode) -@interfaces.register(interfaces.ModeWithInitializationVector) +@utils.register_interface(interfaces.Mode) +@utils.register_interface(interfaces.ModeWithInitializationVector) class CFB(object): name = "CFB" @@ -48,8 +49,8 @@ class CFB(object): self.initialization_vector = initialization_vector -@interfaces.register(interfaces.Mode) -@interfaces.register(interfaces.ModeWithNonce) +@utils.register_interface(interfaces.Mode) +@utils.register_interface(interfaces.ModeWithNonce) class CTR(object): name = "CTR" diff --git a/cryptography/hazmat/primitives/hashes.py b/cryptography/hazmat/primitives/hashes.py index 86c9fe2e..93fc8c42 100644 --- a/cryptography/hazmat/primitives/hashes.py +++ b/cryptography/hazmat/primitives/hashes.py @@ -15,11 +15,12 @@ from __future__ import absolute_import, division, print_function import six +from cryptography import utils from cryptography.exceptions import AlreadyFinalized from cryptography.hazmat.primitives import interfaces -@interfaces.register(interfaces.HashContext) +@utils.register_interface(interfaces.HashContext) class Hash(object): def __init__(self, algorithm, backend=None, ctx=None): if not isinstance(algorithm, interfaces.HashAlgorithm): @@ -59,56 +60,56 @@ class Hash(object): return digest -@interfaces.register(interfaces.HashAlgorithm) +@utils.register_interface(interfaces.HashAlgorithm) class SHA1(object): name = "sha1" digest_size = 20 block_size = 64 -@interfaces.register(interfaces.HashAlgorithm) +@utils.register_interface(interfaces.HashAlgorithm) class SHA224(object): name = "sha224" digest_size = 28 block_size = 64 -@interfaces.register(interfaces.HashAlgorithm) +@utils.register_interface(interfaces.HashAlgorithm) class SHA256(object): name = "sha256" digest_size = 32 block_size = 64 -@interfaces.register(interfaces.HashAlgorithm) +@utils.register_interface(interfaces.HashAlgorithm) class SHA384(object): name = "sha384" digest_size = 48 block_size = 128 -@interfaces.register(interfaces.HashAlgorithm) +@utils.register_interface(interfaces.HashAlgorithm) class SHA512(object): name = "sha512" digest_size = 64 block_size = 128 -@interfaces.register(interfaces.HashAlgorithm) +@utils.register_interface(interfaces.HashAlgorithm) class RIPEMD160(object): name = "ripemd160" digest_size = 20 block_size = 64 -@interfaces.register(interfaces.HashAlgorithm) +@utils.register_interface(interfaces.HashAlgorithm) class Whirlpool(object): name = "whirlpool" digest_size = 64 block_size = 64 -@interfaces.register(interfaces.HashAlgorithm) +@utils.register_interface(interfaces.HashAlgorithm) class MD5(object): name = "md5" digest_size = 16 diff --git a/cryptography/hazmat/primitives/hmac.py b/cryptography/hazmat/primitives/hmac.py index 1bbe39c7..08dfae01 100644 --- a/cryptography/hazmat/primitives/hmac.py +++ b/cryptography/hazmat/primitives/hmac.py @@ -15,11 +15,12 @@ from __future__ import absolute_import, division, print_function import six +from cryptography import utils from cryptography.exceptions import AlreadyFinalized from cryptography.hazmat.primitives import interfaces -@interfaces.register(interfaces.HashContext) +@utils.register_interface(interfaces.HashContext) class HMAC(object): def __init__(self, key, algorithm, ctx=None, backend=None): if not isinstance(algorithm, interfaces.HashAlgorithm): diff --git a/cryptography/hazmat/primitives/interfaces.py b/cryptography/hazmat/primitives/interfaces.py index bbbb266c..8cc9d42c 100644 --- a/cryptography/hazmat/primitives/interfaces.py +++ b/cryptography/hazmat/primitives/interfaces.py @@ -18,13 +18,6 @@ import abc import six -def register(iface): - def register_decorator(klass): - iface.register(klass) - return klass - return register_decorator - - class CipherAlgorithm(six.with_metaclass(abc.ABCMeta)): @abc.abstractproperty def name(self): diff --git a/cryptography/hazmat/primitives/padding.py b/cryptography/hazmat/primitives/padding.py index f41c62c3..2dbac752 100644 --- a/cryptography/hazmat/primitives/padding.py +++ b/cryptography/hazmat/primitives/padding.py @@ -13,6 +13,7 @@ import six +from cryptography import utils from cryptography.hazmat.primitives import interfaces @@ -33,7 +34,7 @@ class PKCS7(object): return _PKCS7UnpaddingContext(self.block_size) -@interfaces.register(interfaces.PaddingContext) +@utils.register_interface(interfaces.PaddingContext) class _PKCS7PaddingContext(object): def __init__(self, block_size): self.block_size = block_size @@ -67,7 +68,7 @@ class _PKCS7PaddingContext(object): return result -@interfaces.register(interfaces.PaddingContext) +@utils.register_interface(interfaces.PaddingContext) class _PKCS7UnpaddingContext(object): def __init__(self, block_size): self.block_size = block_size diff --git a/cryptography/utils.py b/cryptography/utils.py new file mode 100644 index 00000000..e697d515 --- /dev/null +++ b/cryptography/utils.py @@ -0,0 +1,21 @@ +# 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 __future__ import absolute_import, division, print_function + + +def register_interface(iface): + def register_decorator(klass): + iface.register(klass) + return klass + return register_decorator -- cgit v1.2.3 From 92ec09ec0b127b67b794c250aa4d32c063ab66aa Mon Sep 17 00:00:00 2001 From: David Reid Date: Mon, 11 Nov 2013 15:53:52 -0800 Subject: flesh out method definitions. --- cryptography/hazmat/bindings/interfaces.py | 74 ++++++++++++++++++++++++++---- 1 file changed, 65 insertions(+), 9 deletions(-) diff --git a/cryptography/hazmat/bindings/interfaces.py b/cryptography/hazmat/bindings/interfaces.py index e9c8fef3..43563d13 100644 --- a/cryptography/hazmat/bindings/interfaces.py +++ b/cryptography/hazmat/bindings/interfaces.py @@ -22,36 +22,92 @@ class CiphersProviderBackend(six.with_metaclass(abc.ABCMeta)): @abc.abstractproperty def ciphers(self): """ + An instance of CiphersProvider """ class CiphersProvider(six.with_metaclass(abc.ABCMeta)): - """ - wat - """ + @abc.abstractmethod + def supported(self, cipher, mode): + """ + """ + + @abc.abstractmethod + def register_cipher_adapter(self, cipher, mode): + """ + """ + + @abc.abstractmethod + def create_encrypt_ctx(self, cipher, mode): + """ + """ + + @abc.abstractmethod + def create_decrypt_ctx(self, cipher, mode): + """ + """ class HashesProviderBackend(six.with_metaclass(abc.ABCMeta)): @abc.abstractproperty def hashes(self): """ + An instance of HashesProvider """ class HashesProvider(six.with_metaclass(abc.ABCMeta)): - """ - wat - """ + @abc.abstractmethod + def supported(self, algorithm): + """ + """ + + @abc.abstractmethod + def create_ctx(self, algorithm): + """ + """ + + @abc.abstractmethod + def update_ctx(self, ctx, data): + """ + """ + + @abc.abstractmethod + def finalize_ctx(self, ctx, digest_size): + """ + """ + + @abc.abstractmethod + def copy_ctx(self, ctx): + """ + """ class HMACsProviderBackend(six.with_metaclass(abc.ABCMeta)): @abc.abstractproperty def hmacs(self): """ + An instance of HMACsProvider """ class HMACsProvider(six.with_metaclass(abc.ABCMeta)): - """ - wat - """ + @abc.abstractmethod + def create_ctx(self, key, algorithm): + """ + """ + + @abc.abstractmethod + def update_ctx(self, ctx, data): + """ + """ + + @abc.abstractmethod + def finalize_ctx(self, ctx, digest_size): + """ + """ + + @abc.abstractmethod + def copy_ctx(self, ctx): + """ + """ -- cgit v1.2.3 From df3a462da843955f658d5ab3174799368d57f16b Mon Sep 17 00:00:00 2001 From: David Reid Date: Fri, 15 Nov 2013 15:30:20 -0800 Subject: Change interface names to fit in the new smaller interface surface and correct method names on interfaces. --- cryptography/hazmat/bindings/interfaces.py | 72 ++++--------------------- cryptography/hazmat/bindings/openssl/backend.py | 13 +++-- 2 files changed, 15 insertions(+), 70 deletions(-) diff --git a/cryptography/hazmat/bindings/interfaces.py b/cryptography/hazmat/bindings/interfaces.py index 43563d13..ffcd5f14 100644 --- a/cryptography/hazmat/bindings/interfaces.py +++ b/cryptography/hazmat/bindings/interfaces.py @@ -18,17 +18,9 @@ import abc import six -class CiphersProviderBackend(six.with_metaclass(abc.ABCMeta)): - @abc.abstractproperty - def ciphers(self): - """ - An instance of CiphersProvider - """ - - -class CiphersProvider(six.with_metaclass(abc.ABCMeta)): +class CipherBackend(six.with_metaclass(abc.ABCMeta)): @abc.abstractmethod - def supported(self, cipher, mode): + def cipher_supported(self, cipher, mode): """ """ @@ -38,76 +30,30 @@ class CiphersProvider(six.with_metaclass(abc.ABCMeta)): """ @abc.abstractmethod - def create_encrypt_ctx(self, cipher, mode): - """ - """ - - @abc.abstractmethod - def create_decrypt_ctx(self, cipher, mode): - """ - """ - - -class HashesProviderBackend(six.with_metaclass(abc.ABCMeta)): - @abc.abstractproperty - def hashes(self): - """ - An instance of HashesProvider - """ - - -class HashesProvider(six.with_metaclass(abc.ABCMeta)): - @abc.abstractmethod - def supported(self, algorithm): - """ - """ - - @abc.abstractmethod - def create_ctx(self, algorithm): - """ - """ - - @abc.abstractmethod - def update_ctx(self, ctx, data): - """ - """ - - @abc.abstractmethod - def finalize_ctx(self, ctx, digest_size): + def create_symmetric_encryption_ctx(self, cipher, mode): """ """ @abc.abstractmethod - def copy_ctx(self, ctx): + def create_symmetric_decryption_ctx(self, cipher, mode): """ """ -class HMACsProviderBackend(six.with_metaclass(abc.ABCMeta)): - @abc.abstractproperty - def hmacs(self): - """ - An instance of HMACsProvider - """ - - -class HMACsProvider(six.with_metaclass(abc.ABCMeta)): +class HashBackend(six.with_metaclass(abc.ABCMeta)): @abc.abstractmethod - def create_ctx(self, key, algorithm): + def hash_supported(self, algorithm): """ """ @abc.abstractmethod - def update_ctx(self, ctx, data): + def create_hash_ctx(self, algorithm): """ """ - @abc.abstractmethod - def finalize_ctx(self, ctx, digest_size): - """ - """ +class HMACBackend(six.with_metaclass(abc.ABCMeta)): @abc.abstractmethod - def copy_ctx(self, ctx): + def create_hmac_ctx(self, key, algorithm): """ """ diff --git a/cryptography/hazmat/bindings/openssl/backend.py b/cryptography/hazmat/bindings/openssl/backend.py index a7c0741c..db4d18e7 100644 --- a/cryptography/hazmat/bindings/openssl/backend.py +++ b/cryptography/hazmat/bindings/openssl/backend.py @@ -21,8 +21,7 @@ import cffi from cryptography import utils from cryptography.exceptions import UnsupportedAlgorithm from cryptography.hazmat.bindings.interfaces import ( - CiphersProviderBackend, CiphersProvider, HashesProviderBackend, - HashesProvider, HMACsProviderBackend, HMACsProvider + CipherBackend, HashBackend, HMACBackend ) from cryptography.hazmat.primitives import interfaces from cryptography.hazmat.primitives.ciphers.algorithms import ( @@ -33,9 +32,9 @@ from cryptography.hazmat.primitives.ciphers.modes import ( ) -@utils.register_interface(CiphersProviderBackend) -@utils.register_interface(HashesProviderBackend) -@utils.register_interface(HMACsProviderBackend) +@utils.register_interface(CipherBackend) +@utils.register_interface(HashBackend) +@utils.register_interface(HMACBackend) class Backend(object): """ OpenSSL API wrapper. @@ -275,7 +274,7 @@ class _CipherContext(object): return self._backend.ffi.buffer(buf)[:outlen[0]] -@interfaces.register(interfaces.HashContext) +@utils.register_interface(interfaces.HashContext) class _HashContext(object): def __init__(self, backend, algorithm, ctx=None): self.algorithm = algorithm @@ -318,7 +317,7 @@ class _HashContext(object): return self._backend.ffi.buffer(buf)[:] -@interfaces.register(interfaces.HashContext) +@utils.register_interface(interfaces.HashContext) class _HMACContext(object): def __init__(self, backend, key, algorithm, ctx=None): self.algorithm = algorithm -- cgit v1.2.3 From 2a746ce5a4e4b69c9c8fa3ab0e90fb87b053a186 Mon Sep 17 00:00:00 2001 From: David Reid Date: Fri, 15 Nov 2013 15:32:14 -0800 Subject: Start documenting the backend interfaces. --- docs/hazmat/bindings/index.rst | 1 + docs/hazmat/bindings/interfaces.rst | 43 +++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 docs/hazmat/bindings/interfaces.rst diff --git a/docs/hazmat/bindings/index.rst b/docs/hazmat/bindings/index.rst index 19e03999..11355bfa 100644 --- a/docs/hazmat/bindings/index.rst +++ b/docs/hazmat/bindings/index.rst @@ -7,3 +7,4 @@ Bindings :maxdepth: 1 openssl + interfaces diff --git a/docs/hazmat/bindings/interfaces.rst b/docs/hazmat/bindings/interfaces.rst new file mode 100644 index 00000000..2f163267 --- /dev/null +++ b/docs/hazmat/bindings/interfaces.rst @@ -0,0 +1,43 @@ +.. hazmat:: + +Backend Interfaces +================== + +.. currentmodule:: cryptography.hazmat.bindings.interfaces + + +.. class:: CipherBackend + + .. method:: cipher_supported(cipher, mode) + + pass + + .. method:: register_cipher_adapter(cipher_cls, mode_cls, adapter) + + pass + + .. method:: create_symmetric_encryption_ctx(cipher, mode) + + pass + + .. method:: create_symmetric_decryption_ctx(cipher, mode) + + pass + + +.. class:: HashBackend + + .. method:: hash_supported(algorithm) + + pass + + .. method:: create_hash_ctx(algorithm) + + pass + + +.. class:: HMACBackend + + .. method:: create_hmac_ctx(algorithm) + + pass -- cgit v1.2.3 From 6c2e176d0fc77e5d43b86984f15dc350a7376c6e Mon Sep 17 00:00:00 2001 From: David Reid Date: Mon, 18 Nov 2013 11:29:14 -0800 Subject: Fix usages of interfaces.register --- cryptography/hazmat/primitives/ciphers/algorithms.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/cryptography/hazmat/primitives/ciphers/algorithms.py b/cryptography/hazmat/primitives/ciphers/algorithms.py index 1ed487b6..75a87265 100644 --- a/cryptography/hazmat/primitives/ciphers/algorithms.py +++ b/cryptography/hazmat/primitives/ciphers/algorithms.py @@ -13,10 +13,11 @@ from __future__ import absolute_import, division, print_function +from cryptography import utils from cryptography.hazmat.primitives import interfaces -@interfaces.register(interfaces.CipherAlgorithm) +@utils.register_interface(interfaces.CipherAlgorithm) class AES(object): name = "AES" block_size = 128 @@ -36,7 +37,7 @@ class AES(object): return len(self.key) * 8 -@interfaces.register(interfaces.CipherAlgorithm) +@utils.register_interface(interfaces.CipherAlgorithm) class Camellia(object): name = "camellia" block_size = 128 @@ -56,7 +57,7 @@ class Camellia(object): return len(self.key) * 8 -@interfaces.register(interfaces.CipherAlgorithm) +@utils.register_interface(interfaces.CipherAlgorithm) class TripleDES(object): name = "3DES" block_size = 64 @@ -80,7 +81,7 @@ class TripleDES(object): return len(self.key) * 8 -@interfaces.register(interfaces.CipherAlgorithm) +@utils.register_interface(interfaces.CipherAlgorithm) class Blowfish(object): name = "Blowfish" block_size = 64 @@ -100,7 +101,7 @@ class Blowfish(object): return len(self.key) * 8 -@interfaces.register(interfaces.CipherAlgorithm) +@utils.register_interface(interfaces.CipherAlgorithm) class CAST5(object): name = "CAST5" block_size = 64 @@ -120,7 +121,7 @@ class CAST5(object): return len(self.key) * 8 -@interfaces.register(interfaces.CipherAlgorithm) +@utils.register_interface(interfaces.CipherAlgorithm) class ARC4(object): name = "RC4" block_size = 1 -- cgit v1.2.3 From 5973f4cc78a5afeab010d44fc7962660158f1d30 Mon Sep 17 00:00:00 2001 From: David Reid Date: Mon, 18 Nov 2013 11:29:44 -0800 Subject: Document backend interfaces. --- docs/hazmat/bindings/interfaces.rst | 89 ++++++++++++++++++++++++++++++++++--- 1 file changed, 82 insertions(+), 7 deletions(-) diff --git a/docs/hazmat/bindings/interfaces.rst b/docs/hazmat/bindings/interfaces.rst index 2f163267..851b31a9 100644 --- a/docs/hazmat/bindings/interfaces.rst +++ b/docs/hazmat/bindings/interfaces.rst @@ -8,36 +8,111 @@ Backend Interfaces .. class:: CipherBackend + A backend which provides methods for using ciphers for encryption + and decryption. + .. method:: cipher_supported(cipher, mode) - pass + Check if a ``cipher`` and ``mode`` combination is supported by + this backend. + + :param cipher: An instance of a + :class:`~cryptography.hazmat.primitives.interfaces.CipherAlgorithm` + provider. + :param mode: An instance of a + :class:`~cryptography.hazmat.primitives.interfaces.Mode` provider. + + :returns: ``True`` if the specified ``cipher`` and ``mode`` combination + is supported by this backend, otherwise ``False`` .. method:: register_cipher_adapter(cipher_cls, mode_cls, adapter) - pass + Register an adapter which can be used to create a backend specific + object from instances of the + :class:`~cryptography.hazmat.primitives.interfaces.CipherAlgorithm` and + the :class:`~cryptography.hazmat.primitives.interfaces.Mode` primitives. + + :param cipher_cls: A class whose instances provide + :class:`~cryptography.hazmat.primitives.interfaces.CipherAlgorithm` + :param mode_cls: A class whose instances provide: + :class:`~cryptography.hazmat.primitives.interfaces.Mode` + :param adapter: A ``function`` that takes 3 arguments, ``backend`` (a + :class:`CipherBackend` provider), ``cipher`` (a + :class:`~cryptography.hazmat.primitives.interfaces.CipherAlgorithm` + provider ), and ``mode`` (a + :class:`~cryptography.hazmat.primitives.interfaces.Mode` provider). + It returns a backend specific object which may be used to construct + a :class:`~cryptogrpahy.hazmat.primitives.interfaces.CipherContext`. + .. method:: create_symmetric_encryption_ctx(cipher, mode) - pass + Create a + :class:`~cryptogrpahy.hazmat.primitives.interfaces.CipherContext` that + can be used for encrypting data with the symmetric ``cipher`` using + the given ``mode``. + + :param cipher: An instance of a + :class:`~cryptography.hazmat.primitives.interfaces.CipherAlgorithm` + provider. + :param mode: An instance of a + :class:`~cryptography.hazmat.primitives.interfaces.Mode` provider. + + :returns: + :class:`~cryptography.hazmat.primitives.interfaces.CipherContext` + .. method:: create_symmetric_decryption_ctx(cipher, mode) - pass + Create a + :class:`~cryptogrpahy.hazmat.primitives.interfaces.CipherContext` that + can be used for decrypting data with the symmetric ``cipher`` using + the given ``mode``. + + :param cipher: An instance of a + :class:`~cryptography.hazmat.primitives.interfaces.CipherAlgorithm` + provider. + :param mode: An instance of a + :class:`~cryptography.hazmat.primitives.interfaces.Mode` provider. + + :returns: + :class:`~cryptography.hazmat.primitives.interfaces.CipherContext` .. class:: HashBackend + A backend with methods for using cryptographic hash functions. + .. method:: hash_supported(algorithm) - pass + :param algorithm: An instance of a + :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm` + provider. + + :returns: ``True`` if the specified ``algorithm`` is supported by this + backend, otherwise ``False``. + .. method:: create_hash_ctx(algorithm) - pass + :param algorithm: An instance of a + :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm` + provider. + + :returns: + :class:`~cryptography.hazmat.primitives.interfaces.HashContext` .. class:: HMACBackend + A backend with methods for using cryptographic hash functions as message + authentication codes. + .. method:: create_hmac_ctx(algorithm) - pass + :param algorithm: An instance of a + :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm` + provider. + + :returns: + :class:`~cryptography.hazmat.primitives.interfaces.HashContext` -- cgit v1.2.3 From 6624a444c665ff6655cb0f2b9cece131afab6e49 Mon Sep 17 00:00:00 2001 From: David Reid Date: Mon, 18 Nov 2013 12:44:30 -0800 Subject: Hashes and HMACs. --- docs/hazmat/bindings/interfaces.rst | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/docs/hazmat/bindings/interfaces.rst b/docs/hazmat/bindings/interfaces.rst index 851b31a9..98c099bb 100644 --- a/docs/hazmat/bindings/interfaces.rst +++ b/docs/hazmat/bindings/interfaces.rst @@ -85,6 +85,8 @@ Backend Interfaces .. method:: hash_supported(algorithm) + Check if the specified ``algorithm`` is supported by this backend. + :param algorithm: An instance of a :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm` provider. @@ -95,6 +97,10 @@ Backend Interfaces .. method:: create_hash_ctx(algorithm) + Create a + :class:`~cryptogrpahy.hazmat.primitives.interfaces.HashContext` that + uses the specified ``algorithm`` to calculate a message digest. + :param algorithm: An instance of a :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm` provider. @@ -110,6 +116,11 @@ Backend Interfaces .. method:: create_hmac_ctx(algorithm) + Create a + :class:`~cryptogrpahy.hazmat.primitives.interfaces.HashContext` that + uses the specified ``algorithm`` to calculate a hash-based message + authentication code. + :param algorithm: An instance of a :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm` provider. -- cgit v1.2.3 From 3daa8b45151be755a29f708832c49e901c6e9fdc Mon Sep 17 00:00:00 2001 From: David Reid Date: Mon, 18 Nov 2013 12:48:31 -0800 Subject: Move more uses of interfaces.register... --- tests/hazmat/bindings/test_openssl.py | 3 ++- tests/hazmat/primitives/test_block.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/hazmat/bindings/test_openssl.py b/tests/hazmat/bindings/test_openssl.py index e4f8dd8b..9f27aab7 100644 --- a/tests/hazmat/bindings/test_openssl.py +++ b/tests/hazmat/bindings/test_openssl.py @@ -13,6 +13,7 @@ import pytest +from cryptography import utils from cryptography.exceptions import UnsupportedAlgorithm from cryptography.hazmat.bindings.openssl.backend import backend, Backend from cryptography.hazmat.primitives import interfaces @@ -25,7 +26,7 @@ class DummyMode(object): pass -@interfaces.register(interfaces.CipherAlgorithm) +@utils.register_interface(interfaces.CipherAlgorithm) class DummyCipher(object): pass diff --git a/tests/hazmat/primitives/test_block.py b/tests/hazmat/primitives/test_block.py index 963136b9..9460c53d 100644 --- a/tests/hazmat/primitives/test_block.py +++ b/tests/hazmat/primitives/test_block.py @@ -17,6 +17,7 @@ import binascii import pytest +from cryptography import utils from cryptography.exceptions import UnsupportedAlgorithm, AlreadyFinalized from cryptography.hazmat.primitives import interfaces from cryptography.hazmat.primitives.ciphers import ( @@ -24,7 +25,7 @@ from cryptography.hazmat.primitives.ciphers import ( ) -@interfaces.register(interfaces.CipherAlgorithm) +@utils.register_interface(interfaces.CipherAlgorithm) class DummyCipher(object): pass -- cgit v1.2.3 From 327076a17670f35e8f660a62d6c633025f600e28 Mon Sep 17 00:00:00 2001 From: David Reid Date: Mon, 18 Nov 2013 14:03:03 -0800 Subject: Some docstrings. --- cryptography/hazmat/bindings/interfaces.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/cryptography/hazmat/bindings/interfaces.py b/cryptography/hazmat/bindings/interfaces.py index ffcd5f14..912476bb 100644 --- a/cryptography/hazmat/bindings/interfaces.py +++ b/cryptography/hazmat/bindings/interfaces.py @@ -22,21 +22,25 @@ class CipherBackend(six.with_metaclass(abc.ABCMeta)): @abc.abstractmethod def cipher_supported(self, cipher, mode): """ + Return True if the given cipher and mode are supported. """ @abc.abstractmethod - def register_cipher_adapter(self, cipher, mode): + def register_cipher_adapter(self, cipher, mode, adapter): """ + Register an adapter for a cipher and mode to a backend specific object. """ @abc.abstractmethod def create_symmetric_encryption_ctx(self, cipher, mode): """ + Get a CipherContext that can be used for encryption. """ @abc.abstractmethod def create_symmetric_decryption_ctx(self, cipher, mode): """ + Get a CipherContext that can be used for decryption. """ @@ -44,11 +48,13 @@ class HashBackend(six.with_metaclass(abc.ABCMeta)): @abc.abstractmethod def hash_supported(self, algorithm): """ + Return True if the hash algorithm is supported by this backend. """ @abc.abstractmethod def create_hash_ctx(self, algorithm): """ + Create a HashContext for calculating a message digest. """ @@ -56,4 +62,5 @@ class HMACBackend(six.with_metaclass(abc.ABCMeta)): @abc.abstractmethod def create_hmac_ctx(self, key, algorithm): """ + Create a HashContext for calculating a message authentication code. """ -- cgit v1.2.3 From 6b9df81232514bd36c14a07ef3beb901ddb2af7a Mon Sep 17 00:00:00 2001 From: David Reid Date: Mon, 18 Nov 2013 14:13:02 -0800 Subject: Describe what backends provide via these interfaces and that not all backends must provide all interfaces. --- docs/hazmat/bindings/interfaces.rst | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docs/hazmat/bindings/interfaces.rst b/docs/hazmat/bindings/interfaces.rst index 98c099bb..c55d86dc 100644 --- a/docs/hazmat/bindings/interfaces.rst +++ b/docs/hazmat/bindings/interfaces.rst @@ -6,6 +6,14 @@ Backend Interfaces .. currentmodule:: cryptography.hazmat.bindings.interfaces +Backend implementations may provide a number of interfaces to support operations +such as :doc:`/hazmat/primitives/symmetric-encryption`, +:doc:`/hazmat/primitives/cryptographic-hashes`, and +:doc:`/hazmat/primitives/hmac`. + +A specific ``backend`` may provide one or more of these interfaces. + + .. class:: CipherBackend A backend which provides methods for using ciphers for encryption -- cgit v1.2.3 From 69f5ee4a703a52d09799b0a9978cb35a05ab18c6 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Tue, 19 Nov 2013 14:06:18 -0800 Subject: Fix latex compilation (needed for pdf on read the docs) --- docs/cryptography-docs.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/docs/cryptography-docs.py b/docs/cryptography-docs.py index 4ed5526a..ea7e8eef 100644 --- a/docs/cryptography-docs.py +++ b/docs/cryptography-docs.py @@ -31,10 +31,14 @@ class Hazmat(nodes.Admonition, nodes.Element): pass -def visit_hazmat_node(self, node): +def html_visit_hazmat_node(self, node): return self.visit_admonition(node, "danger") +def latex_visit_hazmat_node(self, node): + return self.visit_admonition(node) + + def depart_hazmat_node(self, node): return self.depart_admonition(node) @@ -42,6 +46,7 @@ def depart_hazmat_node(self, node): def setup(app): app.add_node( Hazmat, - html=(visit_hazmat_node, depart_hazmat_node) + html=(html_visit_hazmat_node, depart_hazmat_node), + latex=(latex_visit_hazmat_node, depart_hazmat_node), ) app.add_directive("hazmat", HazmatDirective) -- cgit v1.2.3 From 5a4aa42542c952905fbf83582bff4bb2b328c4e1 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Wed, 20 Nov 2013 11:05:39 -0800 Subject: Travis now has an up to date pypy --- .travis/install.sh | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/.travis/install.sh b/.travis/install.sh index fdd71907..4aa39799 100755 --- a/.travis/install.sh +++ b/.travis/install.sh @@ -5,24 +5,8 @@ set -x if [[ "${OPENSSL}" == "0.9.8" ]]; then sudo add-apt-repository "deb http://archive.ubuntu.com/ubuntu/ lucid main" -fi - -if [[ "${TOX_ENV}" == "pypy" ]]; then - sudo add-apt-repository -y ppa:pypy/ppa -fi - -sudo apt-get -y update - -if [[ "${OPENSSL}" == "0.9.8" ]]; then + sudo apt-get -y update sudo apt-get install -y --force-yes libssl-dev/lucid fi -if [[ "${TOX_ENV}" == "pypy" ]]; then - sudo apt-get install -y pypy - - # This is required because we need to get rid of the Travis installed PyPy - # or it'll take precedence over the PPA installed one. - sudo rm -rf /usr/local/pypy/bin -fi - pip install tox coveralls -- cgit v1.2.3