def load_pycryptodome_raw_lib(name, cdecl): for ext in _raw_api.extension_suffixes: try: # On OSX, Pyinstaller copies the module into the root as # eg Crypto.Cipher._Salsa20.so. Unfortunately this may # clash with other modules of the same name. For example, # Crypto.Hash._SHA256.so clashes with the standard # _sha256.so (OSX is case insensitive). # So we try for the wider case first and then fallback to # the original search algorithm. return _raw_api.load_lib(name + ext, cdecl) except OSError: pass return _original_load_pycryptodome_raw_lib(name, cdecl)
def load_pycryptodome_raw_lib(name, cdecl): for ext in _raw_api.extension_suffixes: try: # On OSX, Pyinstaller copies the module into the root as # eg Crypto.Cipher._Salsa20.so. Unfortunately this may # clash with other modules of the same name. For example, # Crypto.Hash._SHA256.so clashes with the standard # _sha256.so (OSX is case insensitive). # So we try for the wider case first and then fallback to # the original search algorithm. return _raw_api.load_lib(name + ext, cdecl) except OSError: pass return _original_load_pycryptodome_raw_lib(name, cdecl)
void __gmpz_tdiv_q_2exp (mpz_t q, const mpz_t n, mp_bitcnt_t b); void __gmpz_fdiv_q (mpz_t q, const mpz_t n, const mpz_t d); void __gmpz_mul_2exp (mpz_t rop, const mpz_t op1, mp_bitcnt_t op2); int __gmpz_tstbit (const mpz_t op, mp_bitcnt_t bit_index); int __gmpz_perfect_square_p (const mpz_t op); int __gmpz_jacobi (const mpz_t a, const mpz_t b); void __gmpz_gcd (mpz_t rop, const mpz_t op1, const mpz_t op2); UNIX_ULONG __gmpz_gcd_ui (mpz_t rop, const mpz_t op1, UNIX_ULONG op2); void __gmpz_lcm (mpz_t rop, const mpz_t op1, const mpz_t op2); int __gmpz_invert (mpz_t rop, const mpz_t op1, const mpz_t op2); int __gmpz_divisible_p (const mpz_t n, const mpz_t d); int __gmpz_divisible_ui_p (const mpz_t n, UNIX_ULONG d); """ lib = load_lib("gmp", gmp_defs) implementation = {"library": "gmp", "api": backend} # In order to create a function that returns a pointer to # a new MPZ structure, we need to break the abstraction # and know exactly what ffi backend we have if implementation["api"] == "ctypes": from ctypes import Structure, c_int, c_void_p, byref class _MPZ(Structure): _fields_ = [('_mp_alloc', c_int), ('_mp_size', c_int), ('_mp_d', c_void_p)] def new_mpz(): return byref(_MPZ())
void __gmpz_mul_2exp (mpz_t rop, const mpz_t op1, mp_bitcnt_t op2); int __gmpz_tstbit (const mpz_t op, mp_bitcnt_t bit_index); int __gmpz_perfect_square_p (const mpz_t op); int __gmpz_jacobi (const mpz_t a, const mpz_t b); void __gmpz_gcd (mpz_t rop, const mpz_t op1, const mpz_t op2); UNIX_ULONG __gmpz_gcd_ui (mpz_t rop, const mpz_t op1, UNIX_ULONG op2); void __gmpz_lcm (mpz_t rop, const mpz_t op1, const mpz_t op2); int __gmpz_invert (mpz_t rop, const mpz_t op1, const mpz_t op2); int __gmpz_divisible_p (const mpz_t n, const mpz_t d); int __gmpz_divisible_ui_p (const mpz_t n, UNIX_ULONG d); """ try: gmp_defs = "typedef unsigned long UNIX_ULONG;" + gmp_defs_common lib = load_lib("gmp", gmp_defs) implementation = { "library":"gmp", "api":backend } except OSError: import platform bits, linkage = platform.architecture() if bits.startswith("64") and linkage.startswith("Win"): # MPIR uses unsigned long long where GMP uses unsigned long # (LLP64 vs LP64) gmp_defs = "typedef unsigned long long UNIX_ULONG;" + gmp_defs_common c_ulong = c_ulonglong # Try to load private MPIR lib first (wheel) try: from Crypto.Util._file_system import pycryptodome_filename mpir_dll = pycryptodome_filename(("Crypto", "Math"), "mpir.dll") lib = load_lib(mpir_dll, gmp_defs)
void __gmpz_mul_2exp (mpz_t rop, const mpz_t op1, mp_bitcnt_t op2); int __gmpz_tstbit (const mpz_t op, mp_bitcnt_t bit_index); int __gmpz_perfect_square_p (const mpz_t op); int __gmpz_jacobi (const mpz_t a, const mpz_t b); void __gmpz_gcd (mpz_t rop, const mpz_t op1, const mpz_t op2); UNIX_ULONG __gmpz_gcd_ui (mpz_t rop, const mpz_t op1, UNIX_ULONG op2); void __gmpz_lcm (mpz_t rop, const mpz_t op1, const mpz_t op2); int __gmpz_invert (mpz_t rop, const mpz_t op1, const mpz_t op2); int __gmpz_divisible_p (const mpz_t n, const mpz_t d); int __gmpz_divisible_ui_p (const mpz_t n, UNIX_ULONG d); """ try: gmp_defs = "typedef unsigned long UNIX_ULONG;" + gmp_defs_common lib = load_lib("gmp", gmp_defs) implementation = {"library": "gmp", "api": backend} except OSError: import platform bits, linkage = platform.architecture() if bits.startswith("64") and linkage.startswith("Win"): # MPIR uses unsigned long long where GMP uses unsigned long # (LLP64 vs LP64) gmp_defs = "typedef unsigned long long UNIX_ULONG;" + gmp_defs_common c_ulong = c_ulonglong lib = load_lib("mpir", gmp_defs) implementation = {"library": "mpir", "api": backend} # In order to create a function that returns a pointer to # a new MPZ structure, we need to break the abstraction # and know exactly what ffi backend we have
void __gmpz_tdiv_q_2exp (mpz_t q, const mpz_t n, mp_bitcnt_t b); void __gmpz_fdiv_q (mpz_t q, const mpz_t n, const mpz_t d); void __gmpz_mul_2exp (mpz_t rop, const mpz_t op1, mp_bitcnt_t op2); int __gmpz_tstbit (const mpz_t op, mp_bitcnt_t bit_index); int __gmpz_perfect_square_p (const mpz_t op); int __gmpz_jacobi (const mpz_t a, const mpz_t b); void __gmpz_gcd (mpz_t rop, const mpz_t op1, const mpz_t op2); UNIX_ULONG __gmpz_gcd_ui (mpz_t rop, const mpz_t op1, UNIX_ULONG op2); void __gmpz_lcm (mpz_t rop, const mpz_t op1, const mpz_t op2); int __gmpz_invert (mpz_t rop, const mpz_t op1, const mpz_t op2); int __gmpz_divisible_p (const mpz_t n, const mpz_t d); int __gmpz_divisible_ui_p (const mpz_t n, UNIX_ULONG d); """ lib = load_lib("gmp", gmp_defs) implementation = { "library":"gmp", "api":backend } if hasattr(lib, "__mpir_version"): raise ImportError("MPIR library detected") if sys.platform == "win32": raise ImportError("Not using GMP on Windows") # In order to create a function that returns a pointer to # a new MPZ structure, we need to break the abstraction # and know exactly what ffi backend we have if implementation["api"] == "ctypes": from ctypes import Structure, c_int, c_void_p, byref class _MPZ(Structure):