aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Gaynor <alex.gaynor@gmail.com>2013-09-13 19:34:33 -0700
committerAlex Gaynor <alex.gaynor@gmail.com>2013-09-13 19:34:33 -0700
commitc7b0f797b512d7c2a369632c1669326666e6004d (patch)
tree91f6aa160fb8a23c99eec76b1866423bcc337126
parentbc1994db98320fb82155df1f1da619177cc59979 (diff)
parente1b2d3cb863e5d157da1c1766dc3f86ac3bb29e9 (diff)
downloadcryptography-c7b0f797b512d7c2a369632c1669326666e6004d.tar.gz
cryptography-c7b0f797b512d7c2a369632c1669326666e6004d.tar.bz2
cryptography-c7b0f797b512d7c2a369632c1669326666e6004d.zip
Merge pull request #67 from reaperhulk/api-refactor
API refactor to separate cffi declarations
-rw-r--r--AUTHORS.rst1
-rw-r--r--cryptography/bindings/openssl/api.py63
-rw-r--r--cryptography/bindings/openssl/evp.py36
-rw-r--r--cryptography/bindings/openssl/opensslv.py23
4 files changed, 93 insertions, 30 deletions
diff --git a/AUTHORS.rst b/AUTHORS.rst
index 9d499dc7..1aa37e48 100644
--- a/AUTHORS.rst
+++ b/AUTHORS.rst
@@ -7,4 +7,5 @@ AUTHORS
* Laurens Van Houtven <_@lvh.io>
* Christian Heimes <christian@python.org>
* Paul Kehrer <paul.l.kehrer@gmail.com>
+* Jarret Raim <jarito@gmail.com>
diff --git a/cryptography/bindings/openssl/api.py b/cryptography/bindings/openssl/api.py
index 917c1846..bc2b4ae4 100644
--- a/cryptography/bindings/openssl/api.py
+++ b/cryptography/bindings/openssl/api.py
@@ -16,46 +16,49 @@ from __future__ import absolute_import, division, print_function
from cryptography.primitives import interfaces
import cffi
+import sys
class API(object):
"""
OpenSSL API wrapper.
"""
+ _modules = [
+ "evp",
+ "opensslv",
+ ]
def __init__(self):
- ffi = cffi.FFI()
- self._populate_ffi(ffi)
- self._ffi = ffi
- self._lib = ffi.verify("""
- #include <openssl/evp.h>
- #include <openssl/opensslv.h>
- """)
+ self._ffi = cffi.FFI()
+ self.includes, self.types, self.functions = [], [], []
+ self._import()
+ self._define()
+ self._verify()
+
self._lib.OpenSSL_add_all_algorithms()
- def _populate_ffi(self, ffi):
- ffi.cdef("""
- typedef struct {
- ...;
- } EVP_CIPHER_CTX;
- typedef ... EVP_CIPHER;
- typedef ... ENGINE;
-
- static char *const OPENSSL_VERSION_TEXT;
-
- void OpenSSL_add_all_algorithms();
-
- const EVP_CIPHER *EVP_get_cipherbyname(const char *);
- int EVP_EncryptInit_ex(EVP_CIPHER_CTX *, const EVP_CIPHER *,
- ENGINE *, unsigned char *, unsigned char *);
- int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *, int);
- int EVP_EncryptUpdate(EVP_CIPHER_CTX *, unsigned char *, int *,
- unsigned char *, int);
- int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *, unsigned char *, int *);
- int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *);
- const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *);
- int EVP_CIPHER_block_size(const EVP_CIPHER *);
- """)
+ def _import(self):
+ """
+ Import all library definitions
+ """
+ for name in self._modules:
+ __import__('cryptography.bindings.openssl.' + name)
+ module = sys.modules['cryptography.bindings.openssl.' + name]
+ self.includes.append(module.INCLUDES)
+ self.types.append(module.TYPES)
+ self.functions.append(module.FUNCTIONS)
+
+ def _define(self):
+ for typedef in self.types:
+ self._ffi.cdef(typedef)
+ for function in self.functions:
+ self._ffi.cdef(function)
+
+ def _verify(self):
+ self._lib = self._ffi.verify(
+ source="\n".join(self.includes),
+ libraries=['crypto']
+ )
def openssl_version_text(self):
"""
diff --git a/cryptography/bindings/openssl/evp.py b/cryptography/bindings/openssl/evp.py
new file mode 100644
index 00000000..7795e935
--- /dev/null
+++ b/cryptography/bindings/openssl/evp.py
@@ -0,0 +1,36 @@
+# 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 <openssl/evp.h>
+"""
+
+TYPES = """
+ typedef struct { ...; } EVP_CIPHER_CTX;
+ typedef ... EVP_CIPHER;
+ typedef ... ENGINE;
+"""
+
+FUNCTIONS = """
+ void OpenSSL_add_all_algorithms();
+ const EVP_CIPHER *EVP_get_cipherbyname(const char *);
+ int EVP_EncryptInit_ex(EVP_CIPHER_CTX *, const EVP_CIPHER *,
+ ENGINE *, unsigned char *, unsigned char *);
+ int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *, int);
+ int EVP_EncryptUpdate(EVP_CIPHER_CTX *, unsigned char *, int *,
+ unsigned char *, int);
+ int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *, unsigned char *, int *);
+ int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *);
+ const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *);
+ int EVP_CIPHER_block_size(const EVP_CIPHER *);
+"""
diff --git a/cryptography/bindings/openssl/opensslv.py b/cryptography/bindings/openssl/opensslv.py
new file mode 100644
index 00000000..a8f0b9b8
--- /dev/null
+++ b/cryptography/bindings/openssl/opensslv.py
@@ -0,0 +1,23 @@
+# 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 <openssl/opensslv.h>
+"""
+
+TYPES = """
+ static char *const OPENSSL_VERSION_TEXT;
+"""
+
+
+FUNCTIONS = ""