aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Gaynor <alex.gaynor@gmail.com>2014-01-09 18:05:42 -0800
committerAlex Gaynor <alex.gaynor@gmail.com>2014-01-09 18:05:42 -0800
commit0c7b560a8259834a0697f670e7f66c69041b3b60 (patch)
treeedb7131a6f11a5ef525c77a583ceaaa19e2119a6
parent3d5913d223ecac8e130c8b2a8fcb25a715f57f3b (diff)
parent588b0acac807f4aaa6da564be7f0363626415d4b (diff)
downloadcryptography-0c7b560a8259834a0697f670e7f66c69041b3b60.tar.gz
cryptography-0c7b560a8259834a0697f670e7f66c69041b3b60.tar.bz2
cryptography-0c7b560a8259834a0697f670e7f66c69041b3b60.zip
Merge pull request #405 from reaperhulk/common-crypto-binding
CommonCrypto Binding
-rw-r--r--cryptography/hazmat/bindings/commoncrypto/__init__.py12
-rw-r--r--cryptography/hazmat/bindings/commoncrypto/binding.py46
-rw-r--r--cryptography/hazmat/bindings/commoncrypto/common_digest.py67
-rw-r--r--docs/hazmat/bindings/commoncrypto.rst28
-rw-r--r--docs/hazmat/bindings/index.rst1
-rw-r--r--tests/hazmat/bindings/test_commoncrypto.py32
6 files changed, 186 insertions, 0 deletions
diff --git a/cryptography/hazmat/bindings/commoncrypto/__init__.py b/cryptography/hazmat/bindings/commoncrypto/__init__.py
new file mode 100644
index 00000000..55c925c6
--- /dev/null
+++ b/cryptography/hazmat/bindings/commoncrypto/__init__.py
@@ -0,0 +1,12 @@
+# 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.
diff --git a/cryptography/hazmat/bindings/commoncrypto/binding.py b/cryptography/hazmat/bindings/commoncrypto/binding.py
new file mode 100644
index 00000000..e0cd61f7
--- /dev/null
+++ b/cryptography/hazmat/bindings/commoncrypto/binding.py
@@ -0,0 +1,46 @@
+# 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 sys
+
+from cryptography.hazmat.bindings.utils import build_ffi
+
+
+class Binding(object):
+ """
+ CommonCrypto API wrapper.
+ """
+ _module_prefix = "cryptography.hazmat.bindings.commoncrypto."
+ _modules = [
+ "common_digest",
+ ]
+
+ ffi = None
+ lib = None
+
+ def __init__(self):
+ self._ensure_ffi_initialized()
+
+ @classmethod
+ def _ensure_ffi_initialized(cls):
+ if cls.ffi is not None and cls.lib is not None:
+ return
+
+ cls.ffi, cls.lib = build_ffi(cls._module_prefix, cls._modules,
+ "", "", [])
+
+ @classmethod
+ def is_available(cls):
+ return sys.platform == "darwin"
diff --git a/cryptography/hazmat/bindings/commoncrypto/common_digest.py b/cryptography/hazmat/bindings/commoncrypto/common_digest.py
new file mode 100644
index 00000000..ec0fcc92
--- /dev/null
+++ b/cryptography/hazmat/bindings/commoncrypto/common_digest.py
@@ -0,0 +1,67 @@
+# 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.
+
+INCLUDES = """
+#include <CommonCrypto/CommonDigest.h>
+"""
+
+TYPES = """
+typedef uint32_t CC_LONG;
+typedef uint64_t CC_LONG64;
+typedef struct CC_MD5state_st {
+ ...;
+} CC_MD5_CTX;
+typedef struct CC_SHA1state_st {
+ ...;
+} CC_SHA1_CTX;
+typedef struct CC_SHA256state_st {
+ ...;
+} CC_SHA256_CTX;
+typedef struct CC_SHA512state_st {
+ ...;
+} CC_SHA512_CTX;
+"""
+
+FUNCTIONS = """
+int CC_MD5_Init(CC_MD5_CTX *);
+int CC_MD5_Update(CC_MD5_CTX *, const void *, CC_LONG);
+int CC_MD5_Final(unsigned char *, CC_MD5_CTX *);
+
+int CC_SHA1_Init(CC_SHA1_CTX *);
+int CC_SHA1_Update(CC_SHA1_CTX *, const void *, CC_LONG);
+int CC_SHA1_Final(unsigned char *, CC_SHA1_CTX *);
+
+int CC_SHA224_Init(CC_SHA256_CTX *);
+int CC_SHA224_Update(CC_SHA256_CTX *, const void *, CC_LONG);
+int CC_SHA224_Final(unsigned char *, CC_SHA256_CTX *);
+
+int CC_SHA256_Init(CC_SHA256_CTX *);
+int CC_SHA256_Update(CC_SHA256_CTX *, const void *, CC_LONG);
+int CC_SHA256_Final(unsigned char *, CC_SHA256_CTX *);
+
+int CC_SHA384_Init(CC_SHA512_CTX *);
+int CC_SHA384_Update(CC_SHA512_CTX *, const void *, CC_LONG);
+int CC_SHA384_Final(unsigned char *, CC_SHA512_CTX *);
+
+int CC_SHA512_Init(CC_SHA512_CTX *);
+int CC_SHA512_Update(CC_SHA512_CTX *, const void *, CC_LONG);
+int CC_SHA512_Final(unsigned char *, CC_SHA512_CTX *);
+"""
+
+MACROS = """
+"""
+
+CUSTOMIZATIONS = """
+"""
+
+CONDITIONAL_NAMES = {}
diff --git a/docs/hazmat/bindings/commoncrypto.rst b/docs/hazmat/bindings/commoncrypto.rst
new file mode 100644
index 00000000..a4423365
--- /dev/null
+++ b/docs/hazmat/bindings/commoncrypto.rst
@@ -0,0 +1,28 @@
+.. hazmat::
+
+CommonCrypto Binding
+====================
+
+.. currentmodule:: cryptography.hazmat.bindings.commoncrypto.binding
+
+These are `CFFI`_ bindings to the `CommonCrypto`_ C library. It is available on
+Mac OS X.
+
+.. class:: cryptography.hazmat.bindings.commoncrypto.binding.Binding()
+
+ 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 OpenSSL structures.
+
+ .. attribute:: lib
+
+ This is a ``cffi`` library. It can be used to call OpenSSL 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#//apple_ref/doc/man/3cc/CommonCrypto
diff --git a/docs/hazmat/bindings/index.rst b/docs/hazmat/bindings/index.rst
index e2a17591..caab8d6a 100644
--- a/docs/hazmat/bindings/index.rst
+++ b/docs/hazmat/bindings/index.rst
@@ -20,3 +20,4 @@ Individual Bindings
:maxdepth: 1
openssl
+ commoncrypto
diff --git a/tests/hazmat/bindings/test_commoncrypto.py b/tests/hazmat/bindings/test_commoncrypto.py
new file mode 100644
index 00000000..db3d1b74
--- /dev/null
+++ b/tests/hazmat/bindings/test_commoncrypto.py
@@ -0,0 +1,32 @@
+# 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.
+
+import pytest
+
+from cryptography.hazmat.bindings.commoncrypto.binding import Binding
+
+
+@pytest.mark.skipif(not Binding.is_available(),
+ reason="CommonCrypto not available")
+class TestCommonCrypto(object):
+ def test_binding_loads(self):
+ binding = Binding()
+ assert binding
+ assert binding.lib
+ assert binding.ffi
+
+ def test_binding_returns_same_lib(self):
+ binding = Binding()
+ binding2 = Binding()
+ assert binding.lib == binding2.lib
+ assert binding.ffi == binding2.ffi