MCPcopy Create free account
hub / github.com/Bitmessage/PyBitmessage / raw_get_ecdh_key

Method raw_get_ecdh_key

src/pyelliptic/ecc.py:191–244  ·  view source on GitHub ↗
(self, pubkey_x, pubkey_y)

Source from the content-addressed store, hash-verified

189 return sha512(self.raw_get_ecdh_key(pubkey_x, pubkey_y)).digest()
190
191 def raw_get_ecdh_key(self, pubkey_x, pubkey_y):
192 try:
193 ecdh_keybuffer = OpenSSL.malloc(0, 32)
194
195 other_key = OpenSSL.EC_KEY_new_by_curve_name(self.curve)
196 if other_key == 0:
197 raise Exception("[OpenSSL] EC_KEY_new_by_curve_name FAIL ...")
198
199 other_pub_key_x = OpenSSL.BN_bin2bn(pubkey_x, len(pubkey_x), 0)
200 other_pub_key_y = OpenSSL.BN_bin2bn(pubkey_y, len(pubkey_y), 0)
201
202 other_group = OpenSSL.EC_KEY_get0_group(other_key)
203 other_pub_key = OpenSSL.EC_POINT_new(other_group)
204
205 if (OpenSSL.EC_POINT_set_affine_coordinates_GFp(other_group,
206 other_pub_key,
207 other_pub_key_x,
208 other_pub_key_y,
209 0)) == 0:
210 raise Exception(
211 "[OpenSSL] EC_POINT_set_affine_coordinates_GFp FAIL ...")
212 if (OpenSSL.EC_KEY_set_public_key(other_key, other_pub_key)) == 0:
213 raise Exception("[OpenSSL] EC_KEY_set_public_key FAIL ...")
214 if (OpenSSL.EC_KEY_check_key(other_key)) == 0:
215 raise Exception("[OpenSSL] EC_KEY_check_key FAIL ...")
216
217 own_key = OpenSSL.EC_KEY_new_by_curve_name(self.curve)
218 if own_key == 0:
219 raise Exception("[OpenSSL] EC_KEY_new_by_curve_name FAIL ...")
220 own_priv_key = OpenSSL.BN_bin2bn(
221 self.privkey, len(self.privkey), 0)
222
223 if (OpenSSL.EC_KEY_set_private_key(own_key, own_priv_key)) == 0:
224 raise Exception("[OpenSSL] EC_KEY_set_private_key FAIL ...")
225
226 if OpenSSL._hexversion > 0x10100000 and not OpenSSL._libreSSL:
227 OpenSSL.EC_KEY_set_method(own_key, OpenSSL.EC_KEY_OpenSSL())
228 else:
229 OpenSSL.ECDH_set_method(own_key, OpenSSL.ECDH_OpenSSL())
230 ecdh_keylen = OpenSSL.ECDH_compute_key(
231 ecdh_keybuffer, 32, other_pub_key, own_key, 0)
232
233 if ecdh_keylen != 32:
234 raise Exception("[OpenSSL] ECDH keylen FAIL ...")
235
236 return ecdh_keybuffer.raw
237
238 finally:
239 OpenSSL.EC_KEY_free(other_key)
240 OpenSSL.BN_free(other_pub_key_x)
241 OpenSSL.BN_free(other_pub_key_y)
242 OpenSSL.EC_POINT_free(other_pub_key)
243 OpenSSL.EC_KEY_free(own_key)
244 OpenSSL.BN_free(own_priv_key)
245
246 def check_key(self, privkey, pubkey):
247 """

Callers 3

get_ecdh_keyMethod · 0.95
raw_encryptMethod · 0.95
decryptMethod · 0.95

Calls 1

mallocMethod · 0.80

Tested by

no test coverage detected