From a124429fb01ebde3f82cc338e20579d614b12f16 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Thu, 9 Jan 2014 22:51:34 -0600 Subject: commoncrypto initial backend --- cryptography/hazmat/backends/__init__.py | 13 ++- .../hazmat/backends/commoncrypto/__init__.py | 17 +++ .../hazmat/backends/commoncrypto/backend.py | 128 +++++++++++++++++++++ 3 files changed, 155 insertions(+), 3 deletions(-) create mode 100644 cryptography/hazmat/backends/commoncrypto/__init__.py create mode 100644 cryptography/hazmat/backends/commoncrypto/backend.py diff --git a/cryptography/hazmat/backends/__init__.py b/cryptography/hazmat/backends/__init__.py index 215aa4d3..54a227b7 100644 --- a/cryptography/hazmat/backends/__init__.py +++ b/cryptography/hazmat/backends/__init__.py @@ -13,10 +13,17 @@ from cryptography.hazmat.backends import openssl +_POTENTIAL_BACKENDS = ["openssl", "commoncrypto"] -_ALL_BACKENDS = [ - openssl.backend -] +_ALL_BACKENDS = [] + +for b in _POTENTIAL_BACKENDS: + binding = __import__("cryptography.hazmat.bindings.{0}.binding".format(b), + fromlist=["binding"]) + if binding.Binding.is_available(): + backend = __import__("cryptography.hazmat.backends.{0}".format(b), + fromlist=["backend"]) + _ALL_BACKENDS.append(backend.backend) def default_backend(): diff --git a/cryptography/hazmat/backends/commoncrypto/__init__.py b/cryptography/hazmat/backends/commoncrypto/__init__.py new file mode 100644 index 00000000..64a1c01c --- /dev/null +++ b/cryptography/hazmat/backends/commoncrypto/__init__.py @@ -0,0 +1,17 @@ +# 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.commoncrypto.backend import backend + + +__all__ = ["backend"] diff --git a/cryptography/hazmat/backends/commoncrypto/backend.py b/cryptography/hazmat/backends/commoncrypto/backend.py new file mode 100644 index 00000000..efbd6bab --- /dev/null +++ b/cryptography/hazmat/backends/commoncrypto/backend.py @@ -0,0 +1,128 @@ +# 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 + +from collections import namedtuple + +from cryptography import utils +from cryptography.exceptions import UnsupportedAlgorithm +from cryptography.hazmat.backends.interfaces import ( + HashBackend, +) +from cryptography.hazmat.bindings.commoncrypto.binding import Binding +from cryptography.hazmat.primitives import interfaces + + +@utils.register_interface(HashBackend) +class Backend(object): + """ + CommonCrypto API wrapper. + """ + hashtuple = namedtuple("HashClass", ["struct", "init", "update", "final"]) + + def __init__(self): + self._binding = Binding() + self._ffi = self._binding.ffi + self._lib = self._binding.lib + + self.hash_methods = { + b"md5": self.hashtuple( + "CC_MD5_CTX *", self._lib.CC_MD5_Init, + self._lib.CC_MD5_Update, self._lib.CC_MD5_Final + ), + b"sha1": self.hashtuple( + "CC_SHA1_CTX *", self._lib.CC_SHA1_Init, + self._lib.CC_SHA1_Update, self._lib.CC_SHA1_Final + ), + b"sha224": self.hashtuple( + "CC_SHA256_CTX *", self._lib.CC_SHA224_Init, + self._lib.CC_SHA224_Update, self._lib.CC_SHA224_Final + ), + b"sha256": self.hashtuple( + "CC_SHA256_CTX *", self._lib.CC_SHA256_Init, + self._lib.CC_SHA256_Update, self._lib.CC_SHA256_Final + ), + b"sha384": self.hashtuple( + "CC_SHA512_CTX *", self._lib.CC_SHA384_Init, + self._lib.CC_SHA384_Update, self._lib.CC_SHA384_Final + ), + b"sha512": self.hashtuple( + "CC_SHA512_CTX *", self._lib.CC_SHA512_Init, + self._lib.CC_SHA512_Update, self._lib.CC_SHA512_Final + ), + } + + def hash_supported(self, algorithm): + try: + self.hash_methods[algorithm.name.encode("ascii")] + return True + except KeyError: + return False + + def create_hash_ctx(self, algorithm): + return _HashContext(self, algorithm) + + +@utils.register_interface(interfaces.HashContext) +class _HashContext(object): + def __init__(self, backend, algorithm, ctx=None): + self.algorithm = algorithm + self._backend = backend + + if ctx is None: + try: + methods = self._backend.hash_methods[ + self.algorithm.name.encode("ascii") + ] + except KeyError: + raise UnsupportedAlgorithm( + "{0} is not a supported hash on this backend".format( + algorithm.name) + ) + ctx = self._backend._ffi.new(methods.struct) + res = methods.init(ctx) + assert res == 1 + + self._ctx = ctx + + def copy(self): + methods = self._backend.hash_methods[ + self.algorithm.name.encode("ascii") + ] + new_ctx = self._backend._ffi.new(methods.struct) + # CommonCrypto has no APIs for copying hashes, so we have to copy the + # underlying struct. + new_ctx[0] = self._ctx[0] + + return _HashContext(self._backend, self.algorithm, ctx=new_ctx) + + def update(self, data): + methods = self._backend.hash_methods[ + self.algorithm.name.encode("ascii") + ] + res = methods.update(self._ctx, data, len(data)) + assert res == 1 + + def finalize(self): + methods = self._backend.hash_methods[ + self.algorithm.name.encode("ascii") + ] + buf = self._backend._ffi.new("unsigned char[]", + self.algorithm.digest_size) + res = methods.final(buf, self._ctx) + assert res == 1 + return self._backend._ffi.buffer(buf)[:] + + +backend = Backend() -- cgit v1.2.3 From a9e31b35cb1cfd4489294b6f93eb8ea0f07641fb Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Fri, 10 Jan 2014 00:03:03 -0600 Subject: commoncrypto backend docs --- docs/hazmat/backends/common-crypto.rst | 28 ++++++++++++++++++++++++++++ docs/hazmat/backends/index.rst | 1 + 2 files changed, 29 insertions(+) create mode 100644 docs/hazmat/backends/common-crypto.rst diff --git a/docs/hazmat/backends/common-crypto.rst b/docs/hazmat/backends/common-crypto.rst new file mode 100644 index 00000000..edd45b6b --- /dev/null +++ b/docs/hazmat/backends/common-crypto.rst @@ -0,0 +1,28 @@ +.. hazmat:: + +CommonCrypto Backend +==================== + +These are `CFFI`_ bindings to the `CommonCrypto`_ C library provided by Apple +on OS X and iOS. + +.. currentmodule:: cryptography.hazmat.backends.commoncrypto.backend + +.. data:: cryptography.hazmat.backends.commoncrypto.backend + + This is the exposed API for the CommonCrypto bindings. It has two public + attributes: + + .. attribute:: ffi + + This is a :class:`cffi.FFI` instance. It can be used to allocate and + otherwise manipulate CommonCrypto structures. + + .. attribute:: lib + + This is a ``cffi`` library. It can be used to call CommonCrypto + functions, and access constants. + + +.. _`CFFI`: https://cffi.readthedocs.org/ +.. _`CommonCrypto`: https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/Common%20Crypto.3cc.html diff --git a/docs/hazmat/backends/index.rst b/docs/hazmat/backends/index.rst index 06951281..22354f69 100644 --- a/docs/hazmat/backends/index.rst +++ b/docs/hazmat/backends/index.rst @@ -31,4 +31,5 @@ Individual Backends :maxdepth: 1 openssl + common-crypto interfaces -- cgit v1.2.3 From b53e5a05f199b48de9f2f52afd29be4b9c876595 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sat, 11 Jan 2014 19:47:46 -0600 Subject: simpler method of importing CommonCrypto backend into _ALL_BACKENDS --- cryptography/hazmat/backends/__init__.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/cryptography/hazmat/backends/__init__.py b/cryptography/hazmat/backends/__init__.py index 54a227b7..cb1fee90 100644 --- a/cryptography/hazmat/backends/__init__.py +++ b/cryptography/hazmat/backends/__init__.py @@ -12,18 +12,15 @@ # limitations under the License. from cryptography.hazmat.backends import openssl +from cryptography.hazmat.bindings.commoncrypto.binding import ( + Binding as CCBinding +) -_POTENTIAL_BACKENDS = ["openssl", "commoncrypto"] +_ALL_BACKENDS = [openssl.backend] -_ALL_BACKENDS = [] - -for b in _POTENTIAL_BACKENDS: - binding = __import__("cryptography.hazmat.bindings.{0}.binding".format(b), - fromlist=["binding"]) - if binding.Binding.is_available(): - backend = __import__("cryptography.hazmat.backends.{0}".format(b), - fromlist=["backend"]) - _ALL_BACKENDS.append(backend.backend) +if CCBinding.is_available(): + from cryptography.hazmat.backends import commoncrypto + _ALL_BACKENDS.append(commoncrypto.backend) def default_backend(): -- cgit v1.2.3 From 602d4005b856eb638e59aa1d2a3359e144bb0196 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sat, 11 Jan 2014 20:09:38 -0600 Subject: add iOS to wordlist --- docs/spelling_wordlist.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/spelling_wordlist.txt b/docs/spelling_wordlist.txt index 97356c24..b17bcde0 100644 --- a/docs/spelling_wordlist.txt +++ b/docs/spelling_wordlist.txt @@ -26,3 +26,4 @@ Changelog Docstrings Fernet Schneier +iOS -- cgit v1.2.3 From 12656e53baf6d66a2a0a83aed9711407a4cf7818 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sun, 19 Jan 2014 12:15:54 -0600 Subject: update docs, add name attribute to backend --- cryptography/hazmat/backends/commoncrypto/backend.py | 1 + docs/hazmat/backends/common-crypto.rst | 18 ++++-------------- 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/cryptography/hazmat/backends/commoncrypto/backend.py b/cryptography/hazmat/backends/commoncrypto/backend.py index efbd6bab..6953b1f3 100644 --- a/cryptography/hazmat/backends/commoncrypto/backend.py +++ b/cryptography/hazmat/backends/commoncrypto/backend.py @@ -29,6 +29,7 @@ class Backend(object): """ CommonCrypto API wrapper. """ + name = "commoncrypto" hashtuple = namedtuple("HashClass", ["struct", "init", "update", "final"]) def __init__(self): diff --git a/docs/hazmat/backends/common-crypto.rst b/docs/hazmat/backends/common-crypto.rst index edd45b6b..ad4cc210 100644 --- a/docs/hazmat/backends/common-crypto.rst +++ b/docs/hazmat/backends/common-crypto.rst @@ -3,26 +3,16 @@ CommonCrypto Backend ==================== -These are `CFFI`_ bindings to the `CommonCrypto`_ C library provided by Apple -on OS X and iOS. +The `CommonCrypto`_ C library provided by Apple on OS X and iOS. .. currentmodule:: cryptography.hazmat.backends.commoncrypto.backend .. data:: cryptography.hazmat.backends.commoncrypto.backend - This is the exposed API for the CommonCrypto bindings. It has two public - attributes: + This is the exposed API for the OpenSSL backend. It has one public attribute. - .. attribute:: ffi + .. attribute:: name - This is a :class:`cffi.FFI` instance. It can be used to allocate and - otherwise manipulate CommonCrypto structures. + The string name of the backend. - .. attribute:: lib - - This is a ``cffi`` library. It can be used to call CommonCrypto - functions, and access constants. - - -.. _`CFFI`: https://cffi.readthedocs.org/ .. _`CommonCrypto`: https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/Common%20Crypto.3cc.html -- cgit v1.2.3 From 70b6cf82795b7069c504b5700afb671e8a585716 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sun, 19 Jan 2014 12:24:53 -0600 Subject: stop using byte keys for no reason --- .../hazmat/backends/commoncrypto/backend.py | 30 ++++++++-------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/cryptography/hazmat/backends/commoncrypto/backend.py b/cryptography/hazmat/backends/commoncrypto/backend.py index 6953b1f3..6ca1bfa7 100644 --- a/cryptography/hazmat/backends/commoncrypto/backend.py +++ b/cryptography/hazmat/backends/commoncrypto/backend.py @@ -38,27 +38,27 @@ class Backend(object): self._lib = self._binding.lib self.hash_methods = { - b"md5": self.hashtuple( + "md5": self.hashtuple( "CC_MD5_CTX *", self._lib.CC_MD5_Init, self._lib.CC_MD5_Update, self._lib.CC_MD5_Final ), - b"sha1": self.hashtuple( + "sha1": self.hashtuple( "CC_SHA1_CTX *", self._lib.CC_SHA1_Init, self._lib.CC_SHA1_Update, self._lib.CC_SHA1_Final ), - b"sha224": self.hashtuple( + "sha224": self.hashtuple( "CC_SHA256_CTX *", self._lib.CC_SHA224_Init, self._lib.CC_SHA224_Update, self._lib.CC_SHA224_Final ), - b"sha256": self.hashtuple( + "sha256": self.hashtuple( "CC_SHA256_CTX *", self._lib.CC_SHA256_Init, self._lib.CC_SHA256_Update, self._lib.CC_SHA256_Final ), - b"sha384": self.hashtuple( + "sha384": self.hashtuple( "CC_SHA512_CTX *", self._lib.CC_SHA384_Init, self._lib.CC_SHA384_Update, self._lib.CC_SHA384_Final ), - b"sha512": self.hashtuple( + "sha512": self.hashtuple( "CC_SHA512_CTX *", self._lib.CC_SHA512_Init, self._lib.CC_SHA512_Update, self._lib.CC_SHA512_Final ), @@ -66,7 +66,7 @@ class Backend(object): def hash_supported(self, algorithm): try: - self.hash_methods[algorithm.name.encode("ascii")] + self.hash_methods[algorithm.name] return True except KeyError: return False @@ -83,9 +83,7 @@ class _HashContext(object): if ctx is None: try: - methods = self._backend.hash_methods[ - self.algorithm.name.encode("ascii") - ] + methods = self._backend.hash_methods[self.algorithm.name] except KeyError: raise UnsupportedAlgorithm( "{0} is not a supported hash on this backend".format( @@ -98,9 +96,7 @@ class _HashContext(object): self._ctx = ctx def copy(self): - methods = self._backend.hash_methods[ - self.algorithm.name.encode("ascii") - ] + methods = self._backend.hash_methods[self.algorithm.name] new_ctx = self._backend._ffi.new(methods.struct) # CommonCrypto has no APIs for copying hashes, so we have to copy the # underlying struct. @@ -109,16 +105,12 @@ class _HashContext(object): return _HashContext(self._backend, self.algorithm, ctx=new_ctx) def update(self, data): - methods = self._backend.hash_methods[ - self.algorithm.name.encode("ascii") - ] + methods = self._backend.hash_methods[self.algorithm.name] res = methods.update(self._ctx, data, len(data)) assert res == 1 def finalize(self): - methods = self._backend.hash_methods[ - self.algorithm.name.encode("ascii") - ] + methods = self._backend.hash_methods[self.algorithm.name] buf = self._backend._ffi.new("unsigned char[]", self.algorithm.digest_size) res = methods.final(buf, self._ctx) -- cgit v1.2.3 From 16bf40c85c381c7ae84c3b69e0e4824bc6693624 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sun, 19 Jan 2014 12:44:04 -0600 Subject: rename a few things in an attempt to improve clarity --- .../hazmat/backends/commoncrypto/backend.py | 38 ++++++++++++---------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/cryptography/hazmat/backends/commoncrypto/backend.py b/cryptography/hazmat/backends/commoncrypto/backend.py index 6ca1bfa7..10c6596f 100644 --- a/cryptography/hazmat/backends/commoncrypto/backend.py +++ b/cryptography/hazmat/backends/commoncrypto/backend.py @@ -30,35 +30,37 @@ class Backend(object): CommonCrypto API wrapper. """ name = "commoncrypto" - hashtuple = namedtuple("HashClass", ["struct", "init", "update", "final"]) + HashMethods = namedtuple( + "HashMethods", ["ctx", "hash_init", "hash_update", "hash_final"] + ) def __init__(self): self._binding = Binding() self._ffi = self._binding.ffi self._lib = self._binding.lib - self.hash_methods = { - "md5": self.hashtuple( + self.hash_mapping = { + "md5": self.HashMethods( "CC_MD5_CTX *", self._lib.CC_MD5_Init, self._lib.CC_MD5_Update, self._lib.CC_MD5_Final ), - "sha1": self.hashtuple( + "sha1": self.HashMethods( "CC_SHA1_CTX *", self._lib.CC_SHA1_Init, self._lib.CC_SHA1_Update, self._lib.CC_SHA1_Final ), - "sha224": self.hashtuple( + "sha224": self.HashMethods( "CC_SHA256_CTX *", self._lib.CC_SHA224_Init, self._lib.CC_SHA224_Update, self._lib.CC_SHA224_Final ), - "sha256": self.hashtuple( + "sha256": self.HashMethods( "CC_SHA256_CTX *", self._lib.CC_SHA256_Init, self._lib.CC_SHA256_Update, self._lib.CC_SHA256_Final ), - "sha384": self.hashtuple( + "sha384": self.HashMethods( "CC_SHA512_CTX *", self._lib.CC_SHA384_Init, self._lib.CC_SHA384_Update, self._lib.CC_SHA384_Final ), - "sha512": self.hashtuple( + "sha512": self.HashMethods( "CC_SHA512_CTX *", self._lib.CC_SHA512_Init, self._lib.CC_SHA512_Update, self._lib.CC_SHA512_Final ), @@ -66,7 +68,7 @@ class Backend(object): def hash_supported(self, algorithm): try: - self.hash_methods[algorithm.name] + self.hash_mapping[algorithm.name] return True except KeyError: return False @@ -83,21 +85,21 @@ class _HashContext(object): if ctx is None: try: - methods = self._backend.hash_methods[self.algorithm.name] + methods = self._backend.hash_mapping[self.algorithm.name] except KeyError: raise UnsupportedAlgorithm( "{0} is not a supported hash on this backend".format( algorithm.name) ) - ctx = self._backend._ffi.new(methods.struct) - res = methods.init(ctx) + ctx = self._backend._ffi.new(methods.ctx) + res = methods.hash_init(ctx) assert res == 1 self._ctx = ctx def copy(self): - methods = self._backend.hash_methods[self.algorithm.name] - new_ctx = self._backend._ffi.new(methods.struct) + methods = self._backend.hash_mapping[self.algorithm.name] + new_ctx = self._backend._ffi.new(methods.ctx) # CommonCrypto has no APIs for copying hashes, so we have to copy the # underlying struct. new_ctx[0] = self._ctx[0] @@ -105,15 +107,15 @@ class _HashContext(object): return _HashContext(self._backend, self.algorithm, ctx=new_ctx) def update(self, data): - methods = self._backend.hash_methods[self.algorithm.name] - res = methods.update(self._ctx, data, len(data)) + methods = self._backend.hash_mapping[self.algorithm.name] + res = methods.hash_update(self._ctx, data, len(data)) assert res == 1 def finalize(self): - methods = self._backend.hash_methods[self.algorithm.name] + methods = self._backend.hash_mapping[self.algorithm.name] buf = self._backend._ffi.new("unsigned char[]", self.algorithm.digest_size) - res = methods.final(buf, self._ctx) + res = methods.hash_final(buf, self._ctx) assert res == 1 return self._backend._ffi.buffer(buf)[:] -- cgit v1.2.3 From 33cd92ddcf10f976eda510fdad73b0c06a4bf75b Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sun, 19 Jan 2014 13:18:49 -0600 Subject: privatize hash_mapping --- cryptography/hazmat/backends/commoncrypto/backend.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cryptography/hazmat/backends/commoncrypto/backend.py b/cryptography/hazmat/backends/commoncrypto/backend.py index 10c6596f..a7a81ce6 100644 --- a/cryptography/hazmat/backends/commoncrypto/backend.py +++ b/cryptography/hazmat/backends/commoncrypto/backend.py @@ -39,7 +39,7 @@ class Backend(object): self._ffi = self._binding.ffi self._lib = self._binding.lib - self.hash_mapping = { + self._hash_mapping = { "md5": self.HashMethods( "CC_MD5_CTX *", self._lib.CC_MD5_Init, self._lib.CC_MD5_Update, self._lib.CC_MD5_Final @@ -68,7 +68,7 @@ class Backend(object): def hash_supported(self, algorithm): try: - self.hash_mapping[algorithm.name] + self._hash_mapping[algorithm.name] return True except KeyError: return False @@ -85,7 +85,7 @@ class _HashContext(object): if ctx is None: try: - methods = self._backend.hash_mapping[self.algorithm.name] + methods = self._backend._hash_mapping[self.algorithm.name] except KeyError: raise UnsupportedAlgorithm( "{0} is not a supported hash on this backend".format( @@ -98,7 +98,7 @@ class _HashContext(object): self._ctx = ctx def copy(self): - methods = self._backend.hash_mapping[self.algorithm.name] + methods = self._backend._hash_mapping[self.algorithm.name] new_ctx = self._backend._ffi.new(methods.ctx) # CommonCrypto has no APIs for copying hashes, so we have to copy the # underlying struct. @@ -107,12 +107,12 @@ class _HashContext(object): return _HashContext(self._backend, self.algorithm, ctx=new_ctx) def update(self, data): - methods = self._backend.hash_mapping[self.algorithm.name] + methods = self._backend._hash_mapping[self.algorithm.name] res = methods.hash_update(self._ctx, data, len(data)) assert res == 1 def finalize(self): - methods = self._backend.hash_mapping[self.algorithm.name] + methods = self._backend._hash_mapping[self.algorithm.name] buf = self._backend._ffi.new("unsigned char[]", self.algorithm.digest_size) res = methods.hash_final(buf, self._ctx) -- cgit v1.2.3 From b545e9e56fe1d2bb2c827cf61c4beb45826709e5 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sun, 19 Jan 2014 13:20:42 -0600 Subject: move HashMethods to top level --- cryptography/hazmat/backends/commoncrypto/backend.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/cryptography/hazmat/backends/commoncrypto/backend.py b/cryptography/hazmat/backends/commoncrypto/backend.py index a7a81ce6..3d98bf6b 100644 --- a/cryptography/hazmat/backends/commoncrypto/backend.py +++ b/cryptography/hazmat/backends/commoncrypto/backend.py @@ -24,15 +24,17 @@ from cryptography.hazmat.bindings.commoncrypto.binding import Binding from cryptography.hazmat.primitives import interfaces +HashMethods = namedtuple( + "HashMethods", ["ctx", "hash_init", "hash_update", "hash_final"] +) + + @utils.register_interface(HashBackend) class Backend(object): """ CommonCrypto API wrapper. """ name = "commoncrypto" - HashMethods = namedtuple( - "HashMethods", ["ctx", "hash_init", "hash_update", "hash_final"] - ) def __init__(self): self._binding = Binding() @@ -40,27 +42,27 @@ class Backend(object): self._lib = self._binding.lib self._hash_mapping = { - "md5": self.HashMethods( + "md5": HashMethods( "CC_MD5_CTX *", self._lib.CC_MD5_Init, self._lib.CC_MD5_Update, self._lib.CC_MD5_Final ), - "sha1": self.HashMethods( + "sha1": HashMethods( "CC_SHA1_CTX *", self._lib.CC_SHA1_Init, self._lib.CC_SHA1_Update, self._lib.CC_SHA1_Final ), - "sha224": self.HashMethods( + "sha224": HashMethods( "CC_SHA256_CTX *", self._lib.CC_SHA224_Init, self._lib.CC_SHA224_Update, self._lib.CC_SHA224_Final ), - "sha256": self.HashMethods( + "sha256": HashMethods( "CC_SHA256_CTX *", self._lib.CC_SHA256_Init, self._lib.CC_SHA256_Update, self._lib.CC_SHA256_Final ), - "sha384": self.HashMethods( + "sha384": HashMethods( "CC_SHA512_CTX *", self._lib.CC_SHA384_Init, self._lib.CC_SHA384_Update, self._lib.CC_SHA384_Final ), - "sha512": self.HashMethods( + "sha512": HashMethods( "CC_SHA512_CTX *", self._lib.CC_SHA512_Init, self._lib.CC_SHA512_Update, self._lib.CC_SHA512_Final ), -- cgit v1.2.3 From d6b8dfab17705f2fa2a49395f2b689b0b7d51b38 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sun, 19 Jan 2014 13:26:21 -0600 Subject: increase indent and note the value of the attribute in the docs --- docs/hazmat/backends/common-crypto.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/hazmat/backends/common-crypto.rst b/docs/hazmat/backends/common-crypto.rst index ad4cc210..afff9a16 100644 --- a/docs/hazmat/backends/common-crypto.rst +++ b/docs/hazmat/backends/common-crypto.rst @@ -11,8 +11,8 @@ The `CommonCrypto`_ C library provided by Apple on OS X and iOS. This is the exposed API for the OpenSSL backend. It has one public attribute. - .. attribute:: name + .. attribute:: name - The string name of the backend. + Returns ``commoncrypto``, the string name of this backend. .. _`CommonCrypto`: https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/Common%20Crypto.3cc.html -- cgit v1.2.3 From 4925575af6f949aaf83f2504c00cb979cc547162 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sun, 19 Jan 2014 13:29:06 -0600 Subject: fix copy mistake in docs --- docs/hazmat/backends/common-crypto.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/hazmat/backends/common-crypto.rst b/docs/hazmat/backends/common-crypto.rst index afff9a16..64e95ccb 100644 --- a/docs/hazmat/backends/common-crypto.rst +++ b/docs/hazmat/backends/common-crypto.rst @@ -9,7 +9,7 @@ The `CommonCrypto`_ C library provided by Apple on OS X and iOS. .. data:: cryptography.hazmat.backends.commoncrypto.backend - This is the exposed API for the OpenSSL backend. It has one public attribute. + This is the exposed API for the CommonCrypto backend. It has one public attribute. .. attribute:: name -- cgit v1.2.3 From ab6fa0a7f56fbfe3e5c13844969f1ac276ba79e2 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sun, 19 Jan 2014 13:57:15 -0600 Subject: doc updates --- docs/hazmat/backends/common-crypto.rst | 4 ++-- docs/spelling_wordlist.txt | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/hazmat/backends/common-crypto.rst b/docs/hazmat/backends/common-crypto.rst index 64e95ccb..6127116b 100644 --- a/docs/hazmat/backends/common-crypto.rst +++ b/docs/hazmat/backends/common-crypto.rst @@ -11,8 +11,8 @@ The `CommonCrypto`_ C library provided by Apple on OS X and iOS. This is the exposed API for the CommonCrypto backend. It has one public attribute. - .. attribute:: name + .. attribute:: name - Returns ``commoncrypto``, the string name of this backend. + The string name of this backend: ``"commoncrypto"`` .. _`CommonCrypto`: https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/Common%20Crypto.3cc.html diff --git a/docs/spelling_wordlist.txt b/docs/spelling_wordlist.txt index b17bcde0..75628ba5 100644 --- a/docs/spelling_wordlist.txt +++ b/docs/spelling_wordlist.txt @@ -14,6 +14,7 @@ hazmat indistinguishability introspectability invariants +iOS pickleable plaintext testability @@ -26,4 +27,3 @@ Changelog Docstrings Fernet Schneier -iOS -- cgit v1.2.3 From 6d913a6a8ebd063c0d8f630ef8e805906e94ef45 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sun, 19 Jan 2014 14:04:12 -0600 Subject: changelog to note addition of commoncrypto backend with hash support --- docs/changelog.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/changelog.rst b/docs/changelog.rst index 289992f4..b0baf912 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -6,6 +6,7 @@ Changelog **In development** +* Added CommonCrypto backend with hash support. * Added initial CommonCrypto bindings. 0.1 - 2014-01-08 -- cgit v1.2.3 From 6ce4bb78e7d4c597b1594479d99caa73e9c5c73a Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sun, 19 Jan 2014 14:29:28 -0600 Subject: added versionadded --- docs/hazmat/backends/common-crypto.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/hazmat/backends/common-crypto.rst b/docs/hazmat/backends/common-crypto.rst index 6127116b..af2032b6 100644 --- a/docs/hazmat/backends/common-crypto.rst +++ b/docs/hazmat/backends/common-crypto.rst @@ -7,6 +7,8 @@ The `CommonCrypto`_ C library provided by Apple on OS X and iOS. .. currentmodule:: cryptography.hazmat.backends.commoncrypto.backend +.. versionadded:: 0.2 + .. data:: cryptography.hazmat.backends.commoncrypto.backend This is the exposed API for the CommonCrypto backend. It has one public attribute. -- cgit v1.2.3