X-Git-Url: http://wagner.pp.ru/gitweb/?a=blobdiff_plain;f=ctypescrypto%2Fpkey.py;h=50dccd059a349b54e07a64c6bf1f59254e92e7b0;hb=8afcf4f352ef2f967adbe84837d4abc5e9b6ee0c;hp=676d324fb1cda61eb885f677189da3f010f3a9f0;hpb=5e79220ede7efe3bf4703344c7963049a6e71545;p=oss%2Fctypescrypto.git diff --git a/ctypescrypto/pkey.py b/ctypescrypto/pkey.py index 676d324..50dccd0 100644 --- a/ctypescrypto/pkey.py +++ b/ctypescrypto/pkey.py @@ -1,13 +1,13 @@ -from ctypes import byref,c_int,c_long, c_longlong, create_string_buffer +from ctypes import c_char_p,c_void_p,byref,c_int,c_long, c_longlong, create_string_buffer,CFUNCTYPE,POINTER from ctypescrypto import libcrypto -from ctypescrypto.exception import LibCryptoErrors,clear_err_stack +from ctypescrypto.exception import LibCryptoError,clear_err_stack from ctypescrypto.bio import Membio class PKeyError(LibCryptoError): pass CALLBACK_FUNC=CFUNCTYPE(c_int,c_char_p,c_int,c_int,c_char_p) -def password_callback(buf,length,rwflag,u) +def password_callback(buf,length,rwflag,u): cnt=len(u) if length0 + def derive(self,peerkey,**kwargs): + """ + Derives shared key (DH,ECDH,VKO 34.10). Requires + private key available + + @param peerkey - other key (may be public only) + + Keyword parameters are algorithm-specific + """ + ctx=libcrypto.EVP_PKEY_CTX_new(self.key,None) + if ctx is None: + raise PKeyError("Initailizing derive context") + if libcrypto.EVP_PKEY_derive_init(ctx)<1: + raise PKeyError("derive_init") + for oper in kwargs: + rv=libcrypto.EVP_PKEY_CTX_ctrl_str(ctx,oper,kwargs[oper]) + if rv==-2: + raise PKeyError("Parameter %s is not supported by key"%(oper)) + if rv<1: + raise PKeyError("Error setting parameter %s"(oper)) + if libcrypto.EVP_PKEY_derive_set_peer(ctx,peerkey.key)<=0: + raise PKeyError("Cannot set peer key") + keylen=c_long(0) + if libcrypto.EVP_PKEY_derive(ctx,None,byref(keylen))<=0: + raise PKeyError("computing shared key length") + buf=create_string_buffer(keylen) + if libcrypto.EVP_PKEY_derive(ctx,buf,byref(keylen))<=0: + raise PKeyError("computing actual shared key") + libcrypto.EVP_PKEY_CTX_free(ctx) + return buf.raw[:keylen] + @staticmethod def generate(algorithm,**kwargs): """ Generates new private-public key pair for given algorithm @@ -119,7 +161,7 @@ class PKey: raise PKeyError("keygen_init") for oper in kwargs: rv=libcrypto.EVP_PKEY_CTX_ctrl_str(ctx,oper,kwargs[oper]) - if rw=-2: + if rw==-2: raise PKeyError("Parameter %s is not supported by key"%(oper)) if rv<1: raise PKeyError("Error setting parameter %s"(oper)) @@ -127,5 +169,17 @@ class PKey: if libcrypto.EVP_PKEY_keygen(ctx,byref(key))<=0: raise PKeyError("Error generating key") libcrypto.EVP_PKEY_CTX_free(ctx) - return PKey(key,True) - + return PKey(ptr=key,cansign=True) + +# Declare function prototypes +libcrypto.EVP_PKEY_cmp.argtypes=(c_void_p,c_void_p) +libcrypto.PEM_read_bio_PrivateKey.restype=c_void_p +libcrypto.PEM_read_bio_PrivateKey.argtypes=(c_void_p,POINTER(c_void_p),CALLBACK_FUNC,c_char_p) +libcrypto.PEM_read_bio_PUBKEY.restype=c_void_p +libcrypto.PEM_read_bio_PUBKEY.argtypes=(c_void_p,POINTER(c_void_p),CALLBACK_FUNC,c_char_p) +libcrypto.d2i_PUBKEY_bio.restype=c_void_p +libcrypto.d2i_PUBKEY_bio.argtypes=(c_void_p,c_void_p) +libcrypto.d2i_PrivateKey_bio.restype=c_void_p +libcrypto.d2i_PrivateKey_bio.argtypes=(c_void_p,c_void_p) +libcrypto.EVP_PKEY_print_public.argtypes=(c_void_p,c_void_p,c_int,c_void_p) +