#NEWLINE# def sign( self , inputb ) : #NEWLINE# #INDENT# '\n        Sign the input with ECDSA method and returns the signature\n        ' #NEWLINE# try : #NEWLINE# #INDENT# size = len ( inputb ) #NEWLINE# buff = OpenSSL . malloc ( inputb , size ) #NEWLINE# digest = OpenSSL . malloc ( 0 , 64 ) #NEWLINE# md_ctx = OpenSSL . EVP_MD_CTX_create ( ) #NEWLINE# dgst_len = OpenSSL . pointer ( OpenSSL . c_int ( 0 ) ) #NEWLINE# siglen = OpenSSL . pointer ( OpenSSL . c_int ( 0 ) ) #NEWLINE# sig = OpenSSL . malloc ( 0 , 151 ) #NEWLINE# key = OpenSSL . EC_KEY_new_by_curve_name ( self . curve ) #NEWLINE# if ( key == 0 ) : #NEWLINE# #INDENT# raise Exception ( '[OpenSSL] EC_KEY_new_by_curve_name FAIL ...' ) #NEWLINE# #UNINDENT# priv_key = OpenSSL . BN_bin2bn ( self . privkey , len ( self . privkey ) , 0 ) #NEWLINE# pub_key_x = OpenSSL . BN_bin2bn ( self . pubkey_x , len ( self . pubkey_x ) , 0 ) #NEWLINE# pub_key_y = OpenSSL . BN_bin2bn ( self . pubkey_y , len ( self . pubkey_y ) , 0 ) #NEWLINE# if ( OpenSSL . EC_KEY_set_private_key ( key , inputb ) == 0 ) : #NEWLINE# #INDENT# raise Exception ( '[OpenSSL] EC_KEY_set_private_key FAIL ...' ) #NEWLINE# #UNINDENT# group = OpenSSL . EC_KEY_get0_group ( key ) #NEWLINE# pub_key = OpenSSL . EC_POINT_new ( group ) #NEWLINE# if ( OpenSSL . EC_POINT_set_affine_coordinates_GFp ( group , pub_key , pub_key_x , pub_key_y , 0 ) == 0 ) : #NEWLINE# #INDENT# raise Exception ( '[OpenSSL] EC_POINT_set_affine_coordinates_GFp FAIL ...' ) #NEWLINE# #UNINDENT# if ( OpenSSL . EC_KEY_set_public_key ( key , pub_key ) == 0 ) : #NEWLINE# #INDENT# raise Exception ( '[OpenSSL] EC_KEY_set_public_key FAIL ...' ) #NEWLINE# #UNINDENT# if ( OpenSSL . EC_KEY_check_key ( key ) == 0 ) : #NEWLINE# #INDENT# raise Exception ( '[OpenSSL] EC_KEY_check_key FAIL ...' ) #NEWLINE# #UNINDENT# OpenSSL . EVP_MD_CTX_init ( md_ctx ) #NEWLINE# OpenSSL . EVP_DigestInit ( md_ctx , OpenSSL . EVP_ecdsa ( ) ) #NEWLINE# if ( OpenSSL . EVP_DigestUpdate ( md_ctx , buff , size ) == 0 ) : #NEWLINE# #INDENT# raise Exception ( '[OpenSSL] EVP_DigestUpdate FAIL ...' ) #NEWLINE# #UNINDENT# OpenSSL . EVP_DigestFinal ( md_ctx , digest , dgst_len ) #NEWLINE# OpenSSL . ECDSA_sign ( 0 , digest , dgst_len . contents , sig , siglen , key ) #NEWLINE# if ( OpenSSL . ECDSA_verify ( 0 , digest , dgst_len . contents , sig , siglen . contents , key ) != 1 ) : #NEWLINE# #INDENT# raise Exception ( '[OpenSSL] ECDSA_verify FAIL ...' ) #NEWLINE# #UNINDENT# return sig . raw [ 0 : siglen . contents . value ] #NEWLINE# #UNINDENT# finally : #NEWLINE# #INDENT# OpenSSL . EC_KEY_free ( key ) #NEWLINE# OpenSSL . BN_free ( pub_key_x ) #NEWLINE# OpenSSL . BN_free ( pub_key_y ) #NEWLINE# OpenSSL . BN_free ( priv_key ) #NEWLINE# OpenSSL . EC_POINT_free ( pub_key ) #NEWLINE# OpenSSL . EVP_MD_CTX_destroy ( md_ctx )