diff options
-rw-r--r-- | cryptography/hazmat/bindings/utils.py | 16 | ||||
-rw-r--r-- | cryptography/hazmat/primitives/constant_time.py | 4 | ||||
-rw-r--r-- | cryptography/hazmat/primitives/padding.py | 4 |
3 files changed, 20 insertions, 4 deletions
diff --git a/cryptography/hazmat/bindings/utils.py b/cryptography/hazmat/bindings/utils.py index f741aada..0fceac28 100644 --- a/cryptography/hazmat/bindings/utils.py +++ b/cryptography/hazmat/bindings/utils.py @@ -14,6 +14,7 @@ from __future__ import absolute_import, division, print_function import binascii +import threading import sys @@ -21,6 +22,21 @@ from cffi import FFI from cffi.verifier import Verifier +class LazyLibrary(object): + def __init__(self, ffi): + self._ffi = ffi + self._lib = None + self._lock = threading.Lock() + + def __getattr__(self, name): + if self._lib is None: + with self._lock: + if self._lib is None: + self._lib = self._ffi.verifier.load_library() + + return getattr(self._lib, name) + + def load_library_for_binding(ffi, module_prefix, modules): lib = ffi.verifier.load_library() diff --git a/cryptography/hazmat/primitives/constant_time.py b/cryptography/hazmat/primitives/constant_time.py index 8ba045aa..a14eda85 100644 --- a/cryptography/hazmat/primitives/constant_time.py +++ b/cryptography/hazmat/primitives/constant_time.py @@ -16,7 +16,7 @@ from __future__ import absolute_import, division, print_function import hmac import os -from cryptography.hazmat.bindings.utils import build_ffi +from cryptography.hazmat.bindings.utils import LazyLibrary, build_ffi with open(os.path.join(os.path.dirname(__file__), "src/constant_time.h")) as f: @@ -27,7 +27,7 @@ with open(os.path.join(os.path.dirname(__file__), "src/constant_time.c")) as f: _ffi = build_ffi(cdef_source=TYPES, verify_source=FUNCTIONS) -_lib = _ffi.verifier.load_library() +_lib = LazyLibrary(_ffi) if hasattr(hmac, "compare_digest"): diff --git a/cryptography/hazmat/primitives/padding.py b/cryptography/hazmat/primitives/padding.py index 6bb51ab9..7aeeff7c 100644 --- a/cryptography/hazmat/primitives/padding.py +++ b/cryptography/hazmat/primitives/padding.py @@ -17,7 +17,7 @@ import six from cryptography import utils from cryptography.exceptions import AlreadyFinalized -from cryptography.hazmat.bindings.utils import build_ffi +from cryptography.hazmat.bindings.utils import LazyLibrary, build_ffi from cryptography.hazmat.primitives import interfaces @@ -65,7 +65,7 @@ uint8_t Cryptography_check_pkcs7_padding(const uint8_t *data, _ffi = build_ffi(cdef_source=TYPES, verify_source=FUNCTIONS) -_lib = _ffi.verifier.load_library() +_lib = LazyLibrary(_ffi) class PKCS7(object): |