crypto.ecdsa: improve the performance of the .public_key method of PrivateKey (#23920)

This commit is contained in:
blackshirt 2025-03-13 15:21:36 +07:00 committed by GitHub
parent 0321c3f544
commit 2fd3b7e881
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 8 deletions

View File

@ -44,6 +44,8 @@ fn C.EVP_PKEY_size(key &C.EVP_PKEY) int
fn C.EVP_PKEY_eq(a &C.EVP_PKEY, b &C.EVP_PKEY) int
fn C.EVP_PKEY_check(ctx &C.EVP_PKEY_CTX) int
fn C.EVP_PKEY_public_check(ctx &C.EVP_PKEY_CTX) int
fn C.EVP_PKEY_dup(key &C.EVP_PKEY) &C.EVP_PKEY
fn C.EVP_PKEY_set_bn_param(pkey &C.EVP_PKEY, key_name &char, bn &C.BIGNUM) int
fn C.EVP_PKEY_get_group_name(pkey &C.EVP_PKEY, gname &u8, gname_sz u32, gname_len &usize) int
fn C.EVP_PKEY_get1_encoded_public_key(pkey &C.EVP_PKEY, ppub &&u8) int
@ -101,6 +103,7 @@ fn C.BIO_s_mem() &C.BIO_METHOD
fn C.BIO_write(b &C.BIO, buf &u8, length int) int
fn C.PEM_read_bio_PrivateKey(bp &C.BIO, x &&C.EVP_PKEY, cb int, u &voidptr) &C.EVP_PKEY
fn C.PEM_read_bio_PUBKEY(bp &C.BIO, x &&C.EVP_PKEY, cb int, u &voidptr) &C.EVP_PKEY
fn C.PEM_write_bio_PUBKEY(bp &C.BIO, x &C.EVP_PKEY) int
fn C.d2i_PUBKEY(k &&C.EVP_PKEY, pp &&u8, length u32) &C.EVP_PKEY
fn C.i2d_PUBKEY_bio(bo &C.BIO, pkey &C.EVP_PKEY) int
fn C.d2i_PUBKEY_bio(bo &C.BIO, key &&C.EVP_PKEY) &C.EVP_PKEY

View File

@ -262,14 +262,13 @@ pub fn (pv PrivateKey) seed() ![]u8 {
// public_key gets the PublicKey from private key.
pub fn (pv PrivateKey) public_key() !PublicKey {
bo := C.BIO_new(C.BIO_s_mem())
n := C.i2d_PUBKEY_bio(bo, pv.evpkey)
assert n != 0
// stores this bio as another key
pbkey := C.d2i_PUBKEY_bio(bo, 0)
C.BIO_free_all(bo)
// Using duplicate key and removes (clears out) priv key
pbkey := C.EVP_PKEY_dup(pv.evpkey)
bn := C.BN_new()
n := C.EVP_PKEY_set_bn_param(pbkey, c'priv', bn)
assert n == 1
// cleansup
C.BN_free(bn)
return PublicKey{
evpkey: pbkey
}