Commits

Jason R. Coombs committed b4dc70f

Started work on a EVP-structure aware library, including py.test runnable unittests

Comments (0)

Files changed (4)

ctypescrypto/evp.py

+from ctypes import *
+
+from .support import find_library
+
+class DigestError(Exception): pass
+
+class DigestType(Structure):
+	_fields_ = [
+		('type', c_int),
+		('pkey_type', c_int),
+		('md_size', c_int),
+		('flags', c_ulong),
+		('init', c_void_p),
+		('update', c_void_p),
+		('final', c_void_p),
+		('copy', c_void_p),
+		('cleanup', c_void_p),
+		('sign', c_void_p),
+		('verify', c_void_p),
+		('required_pkey_type', c_int*5),
+		('block size', c_int),
+		('ctx_size', c_int),
+		('md_ctrl', c_void_p),
+	]
+
+	@classmethod
+	def from_name(cls, digest_name):
+		res = lib.EVP_get_digestbyname(digest_name)
+		if not res:
+			raise DigestError("Unknown Digest: %(digest_name)s" % vars())
+		return res.contents
+
+lib = find_library('libeay32')
+lib.EVP_get_digestbyname.argtypes = c_char_p,
+lib.EVP_get_digestbyname.restype = POINTER(DigestType)
+lib.OpenSSL_add_all_digests()
+lib.OpenSSL_add_all_ciphers()

ctypescrypto/support.py

+import ctypes
+import os
+
+def find_library(lib_name):
+	"""
+	Given a name like libeay32, find the best match.
+	"""
+	# todo, allow the target environment to customize this behavior
+	lib_name = r'c:\Program Files\OpenSSL\%(lib_name)s.dll' % vars()
+	assert os.path.exists(lib_name)
+	return ctypes.cdll.LoadLibrary(lib_name)

tests/__init__.py

Empty file added.

tests/test_evp.py

+import py.test
+from ctypescrypto import evp
+
+def test_load_valid_digest_type_by_name():
+	t = evp.DigestType.from_name('SHA256')
+
+def test_load_invalid_digest_type_by_name():
+	# dne is Does Not Exist
+	py.test.raises(evp.DigestError, evp.DigestType.from_name, 'sha-dne')