8 """ECC secp256k1 crypto routines 10 WARNING: This module does not mlock() secrets; your private keys may end up on 11 disk in swap! Use with caution! 19 ssl = ctypes.cdll.LoadLibrary(ctypes.util.find_library (
'ssl')
or 'libeay32')
21 ssl.BN_new.restype = ctypes.c_void_p
22 ssl.BN_new.argtypes = []
24 ssl.BN_bin2bn.restype = ctypes.c_void_p
25 ssl.BN_bin2bn.argtypes = [ctypes.c_char_p, ctypes.c_int, ctypes.c_void_p]
27 ssl.BN_CTX_free.restype =
None 28 ssl.BN_CTX_free.argtypes = [ctypes.c_void_p]
30 ssl.BN_CTX_new.restype = ctypes.c_void_p
31 ssl.BN_CTX_new.argtypes = []
33 ssl.ECDH_compute_key.restype = ctypes.c_int
34 ssl.ECDH_compute_key.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p, ctypes.c_void_p]
36 ssl.ECDSA_sign.restype = ctypes.c_int
37 ssl.ECDSA_sign.argtypes = [ctypes.c_int, ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p]
39 ssl.ECDSA_verify.restype = ctypes.c_int
40 ssl.ECDSA_verify.argtypes = [ctypes.c_int, ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p]
42 ssl.EC_KEY_free.restype =
None 43 ssl.EC_KEY_free.argtypes = [ctypes.c_void_p]
45 ssl.EC_KEY_new_by_curve_name.restype = ctypes.c_void_p
46 ssl.EC_KEY_new_by_curve_name.argtypes = [ctypes.c_int]
48 ssl.EC_KEY_get0_group.restype = ctypes.c_void_p
49 ssl.EC_KEY_get0_group.argtypes = [ctypes.c_void_p]
51 ssl.EC_KEY_get0_public_key.restype = ctypes.c_void_p
52 ssl.EC_KEY_get0_public_key.argtypes = [ctypes.c_void_p]
54 ssl.EC_KEY_set_private_key.restype = ctypes.c_int
55 ssl.EC_KEY_set_private_key.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
57 ssl.EC_KEY_set_conv_form.restype =
None 58 ssl.EC_KEY_set_conv_form.argtypes = [ctypes.c_void_p, ctypes.c_int]
60 ssl.EC_KEY_set_public_key.restype = ctypes.c_int
61 ssl.EC_KEY_set_public_key.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
63 ssl.i2o_ECPublicKey.restype = ctypes.c_void_p
64 ssl.i2o_ECPublicKey.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
66 ssl.EC_POINT_new.restype = ctypes.c_void_p
67 ssl.EC_POINT_new.argtypes = [ctypes.c_void_p]
69 ssl.EC_POINT_free.restype =
None 70 ssl.EC_POINT_free.argtypes = [ctypes.c_void_p]
72 ssl.EC_POINT_mul.restype = ctypes.c_int
73 ssl.EC_POINT_mul.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p]
83 return ctypes.c_void_p (val)
85 ssl.EC_KEY_new_by_curve_name.restype = ctypes.c_void_p
86 ssl.EC_KEY_new_by_curve_name.errcheck = _check_result
89 """Wrapper around OpenSSL's EC_KEY""" 91 POINT_CONVERSION_COMPRESSED = 2
92 POINT_CONVERSION_UNCOMPRESSED = 4
95 self.
k = ssl.EC_KEY_new_by_curve_name(NID_secp256k1)
99 ssl.EC_KEY_free(self.
k)
103 priv_key = ssl.BN_bin2bn(secret, 32, ssl.BN_new())
104 group = ssl.EC_KEY_get0_group(self.
k)
105 pub_key = ssl.EC_POINT_new(group)
106 ctx = ssl.BN_CTX_new()
107 if not ssl.EC_POINT_mul(group, pub_key, priv_key,
None,
None, ctx):
108 raise ValueError(
"Could not derive public key from the supplied secret.")
109 ssl.EC_POINT_mul(group, pub_key, priv_key,
None,
None, ctx)
110 ssl.EC_KEY_set_private_key(self.
k, priv_key)
111 ssl.EC_KEY_set_public_key(self.
k, pub_key)
112 ssl.EC_POINT_free(pub_key)
117 self.
mb = ctypes.create_string_buffer(key)
118 return ssl.d2i_ECPrivateKey(ctypes.byref(self.
k), ctypes.byref(ctypes.pointer(self.
mb)), len(key))
121 self.
mb = ctypes.create_string_buffer(key)
122 return ssl.o2i_ECPublicKey(ctypes.byref(self.
k), ctypes.byref(ctypes.pointer(self.
mb)), len(key))
125 size = ssl.i2d_ECPrivateKey(self.
k, 0)
126 mb_pri = ctypes.create_string_buffer(size)
127 ssl.i2d_ECPrivateKey(self.
k, ctypes.byref(ctypes.pointer(mb_pri)))
131 size = ssl.i2o_ECPublicKey(self.
k, 0)
132 mb = ctypes.create_string_buffer(size)
133 ssl.i2o_ECPublicKey(self.
k, ctypes.byref(ctypes.pointer(mb)))
137 ecdh_keybuffer = ctypes.create_string_buffer(32)
138 r = ssl.ECDH_compute_key(ctypes.pointer(ecdh_keybuffer), 32,
139 ssl.EC_KEY_get0_public_key(other_pubkey.k),
142 raise Exception(
'CKey.get_ecdh_key(): ECDH_compute_key() failed')
143 return ecdh_keybuffer.raw
152 if not isinstance(hash, bytes):
153 raise TypeError(
'Hash must be bytes instance; got %r' % hash.__class__)
155 raise ValueError(
'Hash must be exactly 32 bytes long')
157 sig_size0 = ctypes.c_uint32()
158 sig_size0.value = ssl.ECDSA_size(self.
k)
159 mb_sig = ctypes.create_string_buffer(sig_size0.value)
160 result = ssl.ECDSA_sign(0, hash, len(hash), mb_sig, ctypes.byref(sig_size0), self.
k)
162 return mb_sig.raw[:sig_size0.value]
165 """Verify a DER signature""" 166 return ssl.ECDSA_verify(0, hash, len(hash), sig, len(sig), self.
k) == 1
173 ssl.EC_KEY_set_conv_form(self.
k, form)
177 """An encapsulated public key 181 is_valid - Corresponds to CPubKey.IsValid() 182 is_fullyvalid - Corresponds to CPubKey.IsFullyValid() 183 is_compressed - Corresponds to CPubKey.IsCompressed() 187 self = super(CPubKey, cls).
__new__(cls, buf)
200 return len(self) == 33
211 if sys.version >
'3':
212 return '%s(%s)' % (self.__class__.__name__, super(CPubKey, self).
__repr__())
214 return '%s(b%s)' % (self.__class__.__name__, super(CPubKey, self).
__repr__())
def get_ecdh_key(self, other_pubkey, kdf=lambda k:hashlib.sha256(k).digest())
def set_pubkey(self, key)
def set_compressed(self, compressed)
def get_raw_ecdh_key(self, other_pubkey)
def verify(self, hash, sig)
def __new__(cls, buf, _cec_key=None)
int POINT_CONVERSION_COMPRESSED
def set_secretbytes(self, secret)
int POINT_CONVERSION_UNCOMPRESSED
Internal SHA-256 implementation.
def verify(self, hash, sig)
def set_privkey(self, key)
def _check_result(val, func, args)