Skip to content

Commit 2fd3b7e

Browse files
authored
crypto.ecdsa: improve the performance of the .public_key method of PrivateKey (#23920)
1 parent 0321c3f commit 2fd3b7e

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

vlib/crypto/ecdsa/ecdsa.c.v

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ fn C.EVP_PKEY_size(key &C.EVP_PKEY) int
4444
fn C.EVP_PKEY_eq(a &C.EVP_PKEY, b &C.EVP_PKEY) int
4545
fn C.EVP_PKEY_check(ctx &C.EVP_PKEY_CTX) int
4646
fn C.EVP_PKEY_public_check(ctx &C.EVP_PKEY_CTX) int
47+
fn C.EVP_PKEY_dup(key &C.EVP_PKEY) &C.EVP_PKEY
48+
fn C.EVP_PKEY_set_bn_param(pkey &C.EVP_PKEY, key_name &char, bn &C.BIGNUM) int
4749

4850
fn C.EVP_PKEY_get_group_name(pkey &C.EVP_PKEY, gname &u8, gname_sz u32, gname_len &usize) int
4951
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
101103
fn C.BIO_write(b &C.BIO, buf &u8, length int) int
102104
fn C.PEM_read_bio_PrivateKey(bp &C.BIO, x &&C.EVP_PKEY, cb int, u &voidptr) &C.EVP_PKEY
103105
fn C.PEM_read_bio_PUBKEY(bp &C.BIO, x &&C.EVP_PKEY, cb int, u &voidptr) &C.EVP_PKEY
106+
fn C.PEM_write_bio_PUBKEY(bp &C.BIO, x &C.EVP_PKEY) int
104107
fn C.d2i_PUBKEY(k &&C.EVP_PKEY, pp &&u8, length u32) &C.EVP_PKEY
105108
fn C.i2d_PUBKEY_bio(bo &C.BIO, pkey &C.EVP_PKEY) int
106109
fn C.d2i_PUBKEY_bio(bo &C.BIO, key &&C.EVP_PKEY) &C.EVP_PKEY

vlib/crypto/ecdsa/ecdsa.v

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -262,14 +262,13 @@ pub fn (pv PrivateKey) seed() ![]u8 {
262262

263263
// public_key gets the PublicKey from private key.
264264
pub fn (pv PrivateKey) public_key() !PublicKey {
265-
bo := C.BIO_new(C.BIO_s_mem())
266-
n := C.i2d_PUBKEY_bio(bo, pv.evpkey)
267-
assert n != 0
268-
// stores this bio as another key
269-
pbkey := C.d2i_PUBKEY_bio(bo, 0)
270-
271-
C.BIO_free_all(bo)
272-
265+
// Using duplicate key and removes (clears out) priv key
266+
pbkey := C.EVP_PKEY_dup(pv.evpkey)
267+
bn := C.BN_new()
268+
n := C.EVP_PKEY_set_bn_param(pbkey, c'priv', bn)
269+
assert n == 1
270+
// cleansup
271+
C.BN_free(bn)
273272
return PublicKey{
274273
evpkey: pbkey
275274
}

0 commit comments

Comments
 (0)