_raw_api.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. # ===================================================================
  2. #
  3. # Copyright (c) 2014, Legrandin <helderijs@gmail.com>
  4. # All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions
  8. # are met:
  9. #
  10. # 1. Redistributions of source code must retain the above copyright
  11. # notice, this list of conditions and the following disclaimer.
  12. # 2. Redistributions in binary form must reproduce the above copyright
  13. # notice, this list of conditions and the following disclaimer in
  14. # the documentation and/or other materials provided with the
  15. # distribution.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  27. # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. # POSSIBILITY OF SUCH DAMAGE.
  29. # ===================================================================
  30. import os
  31. import abc
  32. import sys
  33. from Cryptodome.Util.py3compat import byte_string
  34. from Cryptodome.Util._file_system import pycryptodome_filename
  35. #
  36. # List of file suffixes for Python extensions
  37. #
  38. if sys.version_info[0] < 3:
  39. import imp
  40. extension_suffixes = []
  41. for ext, mod, typ in imp.get_suffixes():
  42. if typ == imp.C_EXTENSION:
  43. extension_suffixes.append(ext)
  44. else:
  45. from importlib import machinery
  46. extension_suffixes = machinery.EXTENSION_SUFFIXES
  47. # Which types with buffer interface we support (apart from byte strings)
  48. _buffer_type = (bytearray, memoryview)
  49. class _VoidPointer(object):
  50. @abc.abstractmethod
  51. def get(self):
  52. """Return the memory location we point to"""
  53. return
  54. @abc.abstractmethod
  55. def address_of(self):
  56. """Return a raw pointer to this pointer"""
  57. return
  58. try:
  59. # Starting from v2.18, pycparser (used by cffi for in-line ABI mode)
  60. # stops working correctly when PYOPTIMIZE==2 or the parameter -OO is
  61. # passed. In that case, we fall back to ctypes.
  62. # Note that PyPy ships with an old version of pycparser so we can keep
  63. # using cffi there.
  64. # See https://github.com/Legrandin/pycryptodome/issues/228
  65. if '__pypy__' not in sys.builtin_module_names and sys.flags.optimize == 2:
  66. raise ImportError("CFFI with optimize=2 fails due to pycparser bug.")
  67. from cffi import FFI
  68. ffi = FFI()
  69. null_pointer = ffi.NULL
  70. uint8_t_type = ffi.typeof(ffi.new("const uint8_t*"))
  71. _Array = ffi.new("uint8_t[1]").__class__.__bases__
  72. def load_lib(name, cdecl):
  73. """Load a shared library and return a handle to it.
  74. @name, either an absolute path or the name of a library
  75. in the system search path.
  76. @cdecl, the C function declarations.
  77. """
  78. if hasattr(ffi, "RTLD_DEEPBIND") and not os.getenv('PYCRYPTODOME_DISABLE_DEEPBIND'):
  79. lib = ffi.dlopen(name, ffi.RTLD_DEEPBIND)
  80. else:
  81. lib = ffi.dlopen(name)
  82. ffi.cdef(cdecl)
  83. return lib
  84. def c_ulong(x):
  85. """Convert a Python integer to unsigned long"""
  86. return x
  87. c_ulonglong = c_ulong
  88. c_uint = c_ulong
  89. c_ubyte = c_ulong
  90. def c_size_t(x):
  91. """Convert a Python integer to size_t"""
  92. return x
  93. def create_string_buffer(init_or_size, size=None):
  94. """Allocate the given amount of bytes (initially set to 0)"""
  95. if isinstance(init_or_size, bytes):
  96. size = max(len(init_or_size) + 1, size)
  97. result = ffi.new("uint8_t[]", size)
  98. result[:] = init_or_size
  99. else:
  100. if size:
  101. raise ValueError("Size must be specified once only")
  102. result = ffi.new("uint8_t[]", init_or_size)
  103. return result
  104. def get_c_string(c_string):
  105. """Convert a C string into a Python byte sequence"""
  106. return ffi.string(c_string)
  107. def get_raw_buffer(buf):
  108. """Convert a C buffer into a Python byte sequence"""
  109. return ffi.buffer(buf)[:]
  110. def c_uint8_ptr(data):
  111. if isinstance(data, _buffer_type):
  112. # This only works for cffi >= 1.7
  113. return ffi.cast(uint8_t_type, ffi.from_buffer(data))
  114. elif byte_string(data) or isinstance(data, _Array):
  115. return data
  116. else:
  117. raise TypeError("Object type %s cannot be passed to C code" % type(data))
  118. class VoidPointer_cffi(_VoidPointer):
  119. """Model a newly allocated pointer to void"""
  120. def __init__(self):
  121. self._pp = ffi.new("void *[1]")
  122. def get(self):
  123. return self._pp[0]
  124. def address_of(self):
  125. return self._pp
  126. def VoidPointer():
  127. return VoidPointer_cffi()
  128. backend = "cffi"
  129. except ImportError:
  130. import ctypes
  131. from ctypes import (CDLL, c_void_p, byref, c_ulong, c_ulonglong, c_size_t,
  132. create_string_buffer, c_ubyte, c_uint)
  133. from ctypes.util import find_library
  134. from ctypes import Array as _Array
  135. null_pointer = None
  136. cached_architecture = []
  137. def c_ubyte(c):
  138. if not (0 <= c < 256):
  139. raise OverflowError()
  140. return ctypes.c_ubyte(c)
  141. def load_lib(name, cdecl):
  142. if not cached_architecture:
  143. # platform.architecture() creates a subprocess, so caching the
  144. # result makes successive imports faster.
  145. import platform
  146. cached_architecture[:] = platform.architecture()
  147. bits, linkage = cached_architecture
  148. if "." not in name and not linkage.startswith("Win"):
  149. full_name = find_library(name)
  150. if full_name is None:
  151. raise OSError("Cannot load library '%s'" % name)
  152. name = full_name
  153. return CDLL(name)
  154. def get_c_string(c_string):
  155. return c_string.value
  156. def get_raw_buffer(buf):
  157. return buf.raw
  158. # ---- Get raw pointer ---
  159. _c_ssize_t = ctypes.c_ssize_t
  160. _PyBUF_SIMPLE = 0
  161. _PyObject_GetBuffer = ctypes.pythonapi.PyObject_GetBuffer
  162. _PyBuffer_Release = ctypes.pythonapi.PyBuffer_Release
  163. _py_object = ctypes.py_object
  164. _c_ssize_p = ctypes.POINTER(_c_ssize_t)
  165. # See Include/object.h for CPython
  166. # and https://github.com/pallets/click/blob/master/src/click/_winconsole.py
  167. class _Py_buffer(ctypes.Structure):
  168. _fields_ = [
  169. ('buf', c_void_p),
  170. ('obj', ctypes.py_object),
  171. ('len', _c_ssize_t),
  172. ('itemsize', _c_ssize_t),
  173. ('readonly', ctypes.c_int),
  174. ('ndim', ctypes.c_int),
  175. ('format', ctypes.c_char_p),
  176. ('shape', _c_ssize_p),
  177. ('strides', _c_ssize_p),
  178. ('suboffsets', _c_ssize_p),
  179. ('internal', c_void_p)
  180. ]
  181. # Extra field for CPython 2.6/2.7
  182. if sys.version_info[0] == 2:
  183. _fields_.insert(-1, ('smalltable', _c_ssize_t * 2))
  184. def c_uint8_ptr(data):
  185. if byte_string(data) or isinstance(data, _Array):
  186. return data
  187. elif isinstance(data, _buffer_type):
  188. obj = _py_object(data)
  189. buf = _Py_buffer()
  190. _PyObject_GetBuffer(obj, byref(buf), _PyBUF_SIMPLE)
  191. try:
  192. buffer_type = ctypes.c_ubyte * buf.len
  193. return buffer_type.from_address(buf.buf)
  194. finally:
  195. _PyBuffer_Release(byref(buf))
  196. else:
  197. raise TypeError("Object type %s cannot be passed to C code" % type(data))
  198. # ---
  199. class VoidPointer_ctypes(_VoidPointer):
  200. """Model a newly allocated pointer to void"""
  201. def __init__(self):
  202. self._p = c_void_p()
  203. def get(self):
  204. return self._p
  205. def address_of(self):
  206. return byref(self._p)
  207. def VoidPointer():
  208. return VoidPointer_ctypes()
  209. backend = "ctypes"
  210. class SmartPointer(object):
  211. """Class to hold a non-managed piece of memory"""
  212. def __init__(self, raw_pointer, destructor):
  213. self._raw_pointer = raw_pointer
  214. self._destructor = destructor
  215. def get(self):
  216. return self._raw_pointer
  217. def release(self):
  218. rp, self._raw_pointer = self._raw_pointer, None
  219. return rp
  220. def __del__(self):
  221. try:
  222. if self._raw_pointer is not None:
  223. self._destructor(self._raw_pointer)
  224. self._raw_pointer = None
  225. except AttributeError:
  226. pass
  227. def load_pycryptodome_raw_lib(name, cdecl):
  228. """Load a shared library and return a handle to it.
  229. @name, the name of the library expressed as a PyCryptodome module,
  230. for instance Cryptodome.Cipher._raw_cbc.
  231. @cdecl, the C function declarations.
  232. """
  233. split = name.split(".")
  234. dir_comps, basename = split[:-1], split[-1]
  235. attempts = []
  236. for ext in extension_suffixes:
  237. try:
  238. filename = basename + ext
  239. full_name = pycryptodome_filename(dir_comps, filename)
  240. if not os.path.isfile(full_name):
  241. attempts.append("Not found '%s'" % filename)
  242. continue
  243. return load_lib(full_name, cdecl)
  244. except OSError as exp:
  245. attempts.append("Cannot load '%s': %s" % (filename, str(exp)))
  246. raise OSError("Cannot load native module '%s': %s" % (name, ", ".join(attempts)))
  247. def is_buffer(x):
  248. """Return True if object x supports the buffer interface"""
  249. return isinstance(x, (bytes, bytearray, memoryview))
  250. def is_writeable_buffer(x):
  251. return (isinstance(x, bytearray) or
  252. (isinstance(x, memoryview) and not x.readonly))