2020import hashlib
2121import sys
2222
23- ssl = ctypes .cdll .LoadLibrary (ctypes .util .find_library ('ssl' ) or 'libeay32' )
23+ _ssl = ctypes .cdll .LoadLibrary (ctypes .util .find_library ('ssl' ) or 'libeay32' )
2424
2525# this specifies the curve used with ECDSA.
26- NID_secp256k1 = 714 # from openssl/obj_mac.h
26+ _NID_secp256k1 = 714 # from openssl/obj_mac.h
2727
2828
2929# Thx to Sam Devlin for the ctypes magic 64-bit fix.
@@ -34,8 +34,8 @@ def _check_result(val, func, args):
3434 return ctypes .c_void_p (val )
3535
3636
37- ssl .EC_KEY_new_by_curve_name .restype = ctypes .c_void_p
38- ssl .EC_KEY_new_by_curve_name .errcheck = _check_result
37+ _ssl .EC_KEY_new_by_curve_name .restype = ctypes .c_void_p
38+ _ssl .EC_KEY_new_by_curve_name .errcheck = _check_result
3939
4040
4141class CECKey :
@@ -45,57 +45,57 @@ class CECKey:
4545 POINT_CONVERSION_UNCOMPRESSED = 4
4646
4747 def __init__ (self ):
48- self .k = ssl .EC_KEY_new_by_curve_name (NID_secp256k1 )
48+ self .k = _ssl .EC_KEY_new_by_curve_name (_NID_secp256k1 )
4949
5050 def __del__ (self ):
51- if ssl :
52- ssl .EC_KEY_free (self .k )
51+ if _ssl :
52+ _ssl .EC_KEY_free (self .k )
5353 self .k = None
5454
5555 def set_secretbytes (self , secret ):
56- priv_key = ssl .BN_bin2bn (secret , 32 , ssl .BN_new ())
57- group = ssl .EC_KEY_get0_group (self .k )
58- pub_key = ssl .EC_POINT_new (group )
59- ctx = ssl .BN_CTX_new ()
60- if not ssl .EC_POINT_mul (group , pub_key , priv_key , None , None , ctx ):
56+ priv_key = _ssl .BN_bin2bn (secret , 32 , _ssl .BN_new ())
57+ group = _ssl .EC_KEY_get0_group (self .k )
58+ pub_key = _ssl .EC_POINT_new (group )
59+ ctx = _ssl .BN_CTX_new ()
60+ if not _ssl .EC_POINT_mul (group , pub_key , priv_key , None , None , ctx ):
6161 raise ValueError (
6262 "Could not derive public key from the supplied secret." )
63- ssl .EC_POINT_mul (group , pub_key , priv_key , None , None , ctx )
64- ssl .EC_KEY_set_private_key (self .k , priv_key )
65- ssl .EC_KEY_set_public_key (self .k , pub_key )
66- ssl .EC_POINT_free (pub_key )
67- ssl .BN_CTX_free (ctx )
63+ _ssl .EC_POINT_mul (group , pub_key , priv_key , None , None , ctx )
64+ _ssl .EC_KEY_set_private_key (self .k , priv_key )
65+ _ssl .EC_KEY_set_public_key (self .k , pub_key )
66+ _ssl .EC_POINT_free (pub_key )
67+ _ssl .BN_CTX_free (ctx )
6868 return self .k
6969
7070 def set_privkey (self , key ):
7171 self .mb = ctypes .create_string_buffer (key )
72- return ssl .d2i_ECPrivateKey (
72+ return _ssl .d2i_ECPrivateKey (
7373 ctypes .byref (self .k ), ctypes .byref (ctypes .pointer (self .mb )),
7474 len (key ))
7575
7676 def set_pubkey (self , key ):
7777 self .mb = ctypes .create_string_buffer (key )
78- return ssl .o2i_ECPublicKey (
78+ return _ssl .o2i_ECPublicKey (
7979 ctypes .byref (self .k ), ctypes .byref (ctypes .pointer (self .mb )),
8080 len (key ))
8181
8282 def get_privkey (self ):
83- size = ssl .i2d_ECPrivateKey (self .k , 0 )
83+ size = _ssl .i2d_ECPrivateKey (self .k , 0 )
8484 mb_pri = ctypes .create_string_buffer (size )
85- ssl .i2d_ECPrivateKey (self .k , ctypes .byref (ctypes .pointer (mb_pri )))
85+ _ssl .i2d_ECPrivateKey (self .k , ctypes .byref (ctypes .pointer (mb_pri )))
8686 return mb_pri .raw
8787
8888 def get_pubkey (self ):
89- size = ssl .i2o_ECPublicKey (self .k , 0 )
89+ size = _ssl .i2o_ECPublicKey (self .k , 0 )
9090 mb = ctypes .create_string_buffer (size )
91- ssl .i2o_ECPublicKey (self .k , ctypes .byref (ctypes .pointer (mb )))
91+ _ssl .i2o_ECPublicKey (self .k , ctypes .byref (ctypes .pointer (mb )))
9292 return mb .raw
9393
9494 def get_raw_ecdh_key (self , other_pubkey ):
9595 ecdh_keybuffer = ctypes .create_string_buffer (32 )
96- r = ssl .ECDH_compute_key (
96+ r = _ssl .ECDH_compute_key (
9797 ctypes .pointer (ecdh_keybuffer ), 32 ,
98- ssl .EC_KEY_get0_public_key (other_pubkey .k ), self .k , 0 )
98+ _ssl .EC_KEY_get0_public_key (other_pubkey .k ), self .k , 0 )
9999 if r != 32 :
100100 raise Exception ('CKey.get_ecdh_key(): ECDH_compute_key() failed' )
101101 return ecdh_keybuffer .raw
@@ -116,23 +116,23 @@ def sign(self, hash):
116116 raise ValueError ('Hash must be exactly 32 bytes long' )
117117
118118 sig_size0 = ctypes .c_uint32 ()
119- sig_size0 .value = ssl .ECDSA_size (self .k )
119+ sig_size0 .value = _ssl .ECDSA_size (self .k )
120120 mb_sig = ctypes .create_string_buffer (sig_size0 .value )
121- result = ssl .ECDSA_sign (0 , hash , len (hash ), mb_sig ,
122- ctypes .byref (sig_size0 ), self .k )
121+ result = _ssl .ECDSA_sign (0 , hash , len (hash ), mb_sig ,
122+ ctypes .byref (sig_size0 ), self .k )
123123 assert 1 == result
124124 return mb_sig .raw [:sig_size0 .value ]
125125
126126 def verify (self , hash , sig ):
127127 """Verify a DER signature"""
128- return ssl .ECDSA_verify (0 , hash , len (hash ), sig , len (sig ), self .k ) == 1
128+ return _ssl .ECDSA_verify (0 , hash , len (hash ), sig , len (sig ), self .k ) == 1
129129
130130 def set_compressed (self , compressed ):
131131 if compressed :
132132 form = self .POINT_CONVERSION_COMPRESSED
133133 else :
134134 form = self .POINT_CONVERSION_UNCOMPRESSED
135- ssl .EC_KEY_set_conv_form (self .k , form )
135+ _ssl .EC_KEY_set_conv_form (self .k , form )
136136
137137
138138class CPubKey (bytes ):
0 commit comments