2121import hashlib
2222import sys
2323
24- ssl = ctypes .cdll .LoadLibrary (ctypes .util .find_library ('ssl' ) or 'libeay32' )
24+ _ssl = ctypes .cdll .LoadLibrary (ctypes .util .find_library ('ssl' ) or 'libeay32' )
2525
2626# this specifies the curve used with ECDSA.
27- NID_secp256k1 = 714 # from openssl/obj_mac.h
27+ _NID_secp256k1 = 714 # from openssl/obj_mac.h
2828
2929# Thx to Sam Devlin for the ctypes magic 64-bit fix.
3030def _check_result (val , func , args ):
@@ -33,8 +33,8 @@ def _check_result (val, func, args):
3333 else :
3434 return ctypes .c_void_p (val )
3535
36- ssl .EC_KEY_new_by_curve_name .restype = ctypes .c_void_p
37- ssl .EC_KEY_new_by_curve_name .errcheck = _check_result
36+ _ssl .EC_KEY_new_by_curve_name .restype = ctypes .c_void_p
37+ _ssl .EC_KEY_new_by_curve_name .errcheck = _check_result
3838
3939class CECKey :
4040 """Wrapper around OpenSSL's EC_KEY"""
@@ -43,51 +43,51 @@ class CECKey:
4343 POINT_CONVERSION_UNCOMPRESSED = 4
4444
4545 def __init__ (self ):
46- self .k = ssl .EC_KEY_new_by_curve_name (NID_secp256k1 )
46+ self .k = _ssl .EC_KEY_new_by_curve_name (_NID_secp256k1 )
4747
4848 def __del__ (self ):
49- if ssl :
50- ssl .EC_KEY_free (self .k )
49+ if _ssl :
50+ _ssl .EC_KEY_free (self .k )
5151 self .k = None
5252
5353 def set_secretbytes (self , secret ):
54- priv_key = ssl .BN_bin2bn (secret , 32 , ssl .BN_new ())
55- group = ssl .EC_KEY_get0_group (self .k )
56- pub_key = ssl .EC_POINT_new (group )
57- ctx = ssl .BN_CTX_new ()
58- if not ssl .EC_POINT_mul (group , pub_key , priv_key , None , None , ctx ):
54+ priv_key = _ssl .BN_bin2bn (secret , 32 , _ssl .BN_new ())
55+ group = _ssl .EC_KEY_get0_group (self .k )
56+ pub_key = _ssl .EC_POINT_new (group )
57+ ctx = _ssl .BN_CTX_new ()
58+ if not _ssl .EC_POINT_mul (group , pub_key , priv_key , None , None , ctx ):
5959 raise ValueError ("Could not derive public key from the supplied secret." )
60- ssl .EC_POINT_mul (group , pub_key , priv_key , None , None , ctx )
61- ssl .EC_KEY_set_private_key (self .k , priv_key )
62- ssl .EC_KEY_set_public_key (self .k , pub_key )
63- ssl .EC_POINT_free (pub_key )
64- ssl .BN_CTX_free (ctx )
60+ _ssl .EC_POINT_mul (group , pub_key , priv_key , None , None , ctx )
61+ _ssl .EC_KEY_set_private_key (self .k , priv_key )
62+ _ssl .EC_KEY_set_public_key (self .k , pub_key )
63+ _ssl .EC_POINT_free (pub_key )
64+ _ssl .BN_CTX_free (ctx )
6565 return self .k
6666
6767 def set_privkey (self , key ):
6868 self .mb = ctypes .create_string_buffer (key )
69- return ssl .d2i_ECPrivateKey (ctypes .byref (self .k ), ctypes .byref (ctypes .pointer (self .mb )), len (key ))
69+ return _ssl .d2i_ECPrivateKey (ctypes .byref (self .k ), ctypes .byref (ctypes .pointer (self .mb )), len (key ))
7070
7171 def set_pubkey (self , key ):
7272 self .mb = ctypes .create_string_buffer (key )
73- return ssl .o2i_ECPublicKey (ctypes .byref (self .k ), ctypes .byref (ctypes .pointer (self .mb )), len (key ))
73+ return _ssl .o2i_ECPublicKey (ctypes .byref (self .k ), ctypes .byref (ctypes .pointer (self .mb )), len (key ))
7474
7575 def get_privkey (self ):
76- size = ssl .i2d_ECPrivateKey (self .k , 0 )
76+ size = _ssl .i2d_ECPrivateKey (self .k , 0 )
7777 mb_pri = ctypes .create_string_buffer (size )
78- ssl .i2d_ECPrivateKey (self .k , ctypes .byref (ctypes .pointer (mb_pri )))
78+ _ssl .i2d_ECPrivateKey (self .k , ctypes .byref (ctypes .pointer (mb_pri )))
7979 return mb_pri .raw
8080
8181 def get_pubkey (self ):
82- size = ssl .i2o_ECPublicKey (self .k , 0 )
82+ size = _ssl .i2o_ECPublicKey (self .k , 0 )
8383 mb = ctypes .create_string_buffer (size )
84- ssl .i2o_ECPublicKey (self .k , ctypes .byref (ctypes .pointer (mb )))
84+ _ssl .i2o_ECPublicKey (self .k , ctypes .byref (ctypes .pointer (mb )))
8585 return mb .raw
8686
8787 def get_raw_ecdh_key (self , other_pubkey ):
8888 ecdh_keybuffer = ctypes .create_string_buffer (32 )
89- r = ssl .ECDH_compute_key (ctypes .pointer (ecdh_keybuffer ), 32 ,
90- ssl .EC_KEY_get0_public_key (other_pubkey .k ),
89+ r = _ssl .ECDH_compute_key (ctypes .pointer (ecdh_keybuffer ), 32 ,
90+ _ssl .EC_KEY_get0_public_key (other_pubkey .k ),
9191 self .k , 0 )
9292 if r != 32 :
9393 raise Exception ('CKey.get_ecdh_key(): ECDH_compute_key() failed' )
@@ -106,22 +106,22 @@ def sign(self, hash):
106106 raise ValueError ('Hash must be exactly 32 bytes long' )
107107
108108 sig_size0 = ctypes .c_uint32 ()
109- sig_size0 .value = ssl .ECDSA_size (self .k )
109+ sig_size0 .value = _ssl .ECDSA_size (self .k )
110110 mb_sig = ctypes .create_string_buffer (sig_size0 .value )
111- result = ssl .ECDSA_sign (0 , hash , len (hash ), mb_sig , ctypes .byref (sig_size0 ), self .k )
111+ result = _ssl .ECDSA_sign (0 , hash , len (hash ), mb_sig , ctypes .byref (sig_size0 ), self .k )
112112 assert 1 == result
113113 return mb_sig .raw [:sig_size0 .value ]
114114
115115 def verify (self , hash , sig ):
116116 """Verify a DER signature"""
117- return ssl .ECDSA_verify (0 , hash , len (hash ), sig , len (sig ), self .k ) == 1
117+ return _ssl .ECDSA_verify (0 , hash , len (hash ), sig , len (sig ), self .k ) == 1
118118
119119 def set_compressed (self , compressed ):
120120 if compressed :
121121 form = self .POINT_CONVERSION_COMPRESSED
122122 else :
123123 form = self .POINT_CONVERSION_UNCOMPRESSED
124- ssl .EC_KEY_set_conv_form (self .k , form )
124+ _ssl .EC_KEY_set_conv_form (self .k , form )
125125
126126
127127class CPubKey (bytes ):
0 commit comments