MCPcopy Index your code
hub / github.com/Bitmessage/PyBitmessage / sign

Method sign

src/pyelliptic/ecc.py:305–372  ·  view source on GitHub ↗

Sign the input with ECDSA method and returns the signature

(self, inputb, digest_alg=OpenSSL.digest_ecdsa_sha1)

Source from the content-addressed store, hash-verified

303 OpenSSL.BN_free(priv_key)
304
305 def sign(self, inputb, digest_alg=OpenSSL.digest_ecdsa_sha1):
306 """
307 Sign the input with ECDSA method and returns the signature
308 """
309 try:
310 size = len(inputb)
311 buff = OpenSSL.malloc(inputb, size)
312 digest = OpenSSL.malloc(0, 64)
313 if OpenSSL._hexversion > 0x10100000 and not OpenSSL._libreSSL:
314 md_ctx = OpenSSL.EVP_MD_CTX_new()
315 else:
316 md_ctx = OpenSSL.EVP_MD_CTX_create()
317 dgst_len = OpenSSL.pointer(OpenSSL.c_int(0))
318 siglen = OpenSSL.pointer(OpenSSL.c_int(0))
319 sig = OpenSSL.malloc(0, 151)
320
321 key = OpenSSL.EC_KEY_new_by_curve_name(self.curve)
322 if key == 0:
323 raise Exception("[OpenSSL] EC_KEY_new_by_curve_name FAIL ...")
324
325 priv_key = OpenSSL.BN_bin2bn(self.privkey, len(self.privkey), 0)
326 pub_key_x = OpenSSL.BN_bin2bn(self.pubkey_x, len(self.pubkey_x), 0)
327 pub_key_y = OpenSSL.BN_bin2bn(self.pubkey_y, len(self.pubkey_y), 0)
328
329 if (OpenSSL.EC_KEY_set_private_key(key, priv_key)) == 0:
330 raise Exception("[OpenSSL] EC_KEY_set_private_key FAIL ...")
331
332 group = OpenSSL.EC_KEY_get0_group(key)
333 pub_key = OpenSSL.EC_POINT_new(group)
334
335 if (OpenSSL.EC_POINT_set_affine_coordinates_GFp(group, pub_key,
336 pub_key_x,
337 pub_key_y,
338 0)) == 0:
339 raise Exception(
340 "[OpenSSL] EC_POINT_set_affine_coordinates_GFp FAIL ...")
341 if (OpenSSL.EC_KEY_set_public_key(key, pub_key)) == 0:
342 raise Exception("[OpenSSL] EC_KEY_set_public_key FAIL ...")
343 if (OpenSSL.EC_KEY_check_key(key)) == 0:
344 raise Exception("[OpenSSL] EC_KEY_check_key FAIL ...")
345
346 if OpenSSL._hexversion > 0x10100000 and not OpenSSL._libreSSL:
347 OpenSSL.EVP_MD_CTX_new(md_ctx)
348 else:
349 OpenSSL.EVP_MD_CTX_init(md_ctx)
350 OpenSSL.EVP_DigestInit_ex(md_ctx, digest_alg(), None)
351
352 if (OpenSSL.EVP_DigestUpdate(md_ctx, buff, size)) == 0:
353 raise Exception("[OpenSSL] EVP_DigestUpdate FAIL ...")
354 OpenSSL.EVP_DigestFinal_ex(md_ctx, digest, dgst_len)
355 OpenSSL.ECDSA_sign(0, digest, dgst_len.contents, sig, siglen, key)
356 if (OpenSSL.ECDSA_verify(0, digest, dgst_len.contents, sig,
357 siglen.contents, key)) != 1:
358 raise Exception("[OpenSSL] ECDSA_verify FAIL ...")
359
360 return sig.raw[:siglen.contents.value]
361
362 finally:

Callers 5

sendBroadcastMethod · 0.80
sendMsgMethod · 0.80
signFunction · 0.80

Calls 1

mallocMethod · 0.80

Tested by

no test coverage detected