From a01a299c8b1903f99bf46f08d4f91a99ffe614fc Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sat, 18 Apr 2026 23:10:15 +0300 Subject: [PATCH] crypto.ecdsa: fixes --- vlib/crypto/ecdsa/ecdsa.c.v | 12 ++++++------ vlib/crypto/ecdsa/ecdsa.v | 18 +++++++++--------- vlib/crypto/ecdsa/util.v | 10 +++++----- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/vlib/crypto/ecdsa/ecdsa.c.v b/vlib/crypto/ecdsa/ecdsa.c.v index 23e3697aa..b37674807 100644 --- a/vlib/crypto/ecdsa/ecdsa.c.v +++ b/vlib/crypto/ecdsa/ecdsa.c.v @@ -65,8 +65,8 @@ 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) i32 fn C.EVP_PKEY_get_group_name(pkey &C.EVP_PKEY, gname &u8, gname_sz u32, gname_len &usize) i32 -fn C.EVP_PKEY_get1_encoded_public_key(pkey &C.EVP_PKEY, ppub &&u8) i32 -fn C.EVP_PKEY_get_bn_param(pkey &C.EVP_PKEY, key_name &u8, bn &&C.BIGNUM) i32 +fn C.EVP_PKEY_get1_encoded_public_key(pkey &C.EVP_PKEY, ppub &&u8) usize +fn C.EVP_PKEY_get_bn_param(pkey &C.EVP_PKEY, key_name &char, bn &&C.BIGNUM) i32 fn C.EVP_PKEY_fromdata_init(ctx &C.EVP_PKEY_CTX) i32 fn C.EVP_PKEY_fromdata(ctx &C.EVP_PKEY_CTX, ppkey &&C.EVP_PKEY, selection i32, params &C.OSSL_PARAM) i32 @@ -131,7 +131,7 @@ struct C.EC_POINT {} fn C.EC_POINT_new(group &C.EC_GROUP) &C.EC_POINT fn C.EC_POINT_mul(group &C.EC_GROUP, r &C.EC_POINT, n &C.BIGNUM, q &C.EC_POINT, m &C.BIGNUM, ctx &C.BN_CTX) i32 -fn C.EC_POINT_point2buf(group &C.EC_GROUP, point &C.EC_POINT, form i32, pbuf &&u8, ctx &C.BN_CTX) i32 +fn C.EC_POINT_point2buf(group &C.EC_GROUP, point &C.EC_POINT, form i32, pbuf &&u8, ctx &C.BN_CTX) usize fn C.EC_POINT_free(point &C.EC_POINT) // Elliptic group (curve) related declarations. @@ -186,7 +186,7 @@ struct C.OSSL_PARAM_BLD {} fn C.OSSL_PARAM_free(params &C.OSSL_PARAM) fn C.OSSL_PARAM_BLD_free(param_bld &C.OSSL_PARAM_BLD) fn C.OSSL_PARAM_BLD_new() &C.OSSL_PARAM_BLD -fn C.OSSL_PARAM_BLD_push_utf8_string(bld &C.OSSL_PARAM_BLD, key &char, buf &char, bsize i32) i32 -fn C.OSSL_PARAM_BLD_push_BN(bld &C.OSSL_PARAM_BLD, key &u8, bn &C.BIGNUM) i32 -fn C.OSSL_PARAM_BLD_push_octet_string(bld &C.OSSL_PARAM_BLD, key &u8, buf voidptr, bsize i32) i32 +fn C.OSSL_PARAM_BLD_push_utf8_string(bld &C.OSSL_PARAM_BLD, key &char, buf &char, bsize usize) i32 +fn C.OSSL_PARAM_BLD_push_BN(bld &C.OSSL_PARAM_BLD, key &char, bn &C.BIGNUM) i32 +fn C.OSSL_PARAM_BLD_push_octet_string(bld &C.OSSL_PARAM_BLD, key &char, buf voidptr, bsize usize) i32 fn C.OSSL_PARAM_BLD_to_param(bld &C.OSSL_PARAM_BLD) &C.OSSL_PARAM diff --git a/vlib/crypto/ecdsa/ecdsa.v b/vlib/crypto/ecdsa/ecdsa.v index 0de8dc7cc..e55adf2b9 100644 --- a/vlib/crypto/ecdsa/ecdsa.v +++ b/vlib/crypto/ecdsa/ecdsa.v @@ -228,7 +228,7 @@ pub fn (pv PrivateKey) sign_with_options(message []u8, opt SignerOpts) ![]u8 { // bytes represent private key as bytes. pub fn (pv PrivateKey) bytes() ![]u8 { - bn := C.BN_new() + mut bn := &C.BIGNUM(unsafe { nil }) // retrieves a BIGNUM value associated with a 'priv' key name n := C.EVP_PKEY_get_bn_param(pv.evpkey, c'priv', &bn) if n <= 0 { @@ -244,8 +244,8 @@ pub fn (pv PrivateKey) bytes() ![]u8 { num_bytes } mut buf := []u8{len: int(size)} - res := C.BN_bn2binpad(bn, buf.data, size) - if res == 0 { + res := C.BN_bn2binpad(bn, buf.data, i32(size)) + if res <= 0 { C.BN_free(bn) return error('Failed to convert BIGNUM to bytes') } @@ -486,7 +486,7 @@ fn evpkey_from_seed(seed []u8, opt CurveOptions) !&C.EVP_PKEY { // push the group, private and public key bytes infos into the builder n := C.OSSL_PARAM_BLD_push_utf8_string(param_bld, c'group', voidptr(opt.nid.str().str), 0) m := C.OSSL_PARAM_BLD_push_BN(param_bld, c'priv', bn) - o := C.OSSL_PARAM_BLD_push_octet_string(param_bld, c'pub', pub_bytes.data, pub_bytes.len) + o := C.OSSL_PARAM_BLD_push_octet_string(param_bld, c'pub', pub_bytes.data, usize(pub_bytes.len)) if n <= 0 || m <= 0 || o <= 0 { C.EC_POINT_free(point) C.BN_free(bn) @@ -569,23 +569,23 @@ const default_point_bufsize = 160 // 2 * 64 + 1 + extra // point_2_buf gets bytes representation of the EC_POINT fn point_2_buf(group &C.EC_GROUP, point &C.EC_POINT, fmt int) ![]u8 { ctx := C.BN_CTX_new() - pbuf := []u8{len: default_point_bufsize} + mut pbuf := &u8(unsafe { nil }) // Notes from the docs: // EC_POINT_point2buf() allocates a buffer of suitable length and writes an EC_POINT to it in octet format. // The allocated buffer is written to *pbuf and its length is returned. // The caller must free up the allocated buffer with a call to OPENSSL_free(). // Since the allocated buffer value is written to *pbuf the pbuf parameter MUST NOT be NULL. // So, we explicitly call `.OPENSSL_free` on the allocated buffer. - n := C.EC_POINT_point2buf(group, point, fmt, voidptr(&pbuf.data), ctx) + n := C.EC_POINT_point2buf(group, point, fmt, &pbuf, ctx) if n <= 0 { C.BN_CTX_free(ctx) - C.OPENSSL_free(voidptr(&pbuf.data)) + C.OPENSSL_free(voidptr(pbuf)) return error('Get null length of buf') } // Gets the copy of the result with the correct length - result := pbuf[..n].clone() + result := unsafe { pbuf.vbytes(int(n)).clone() } - C.OPENSSL_free(voidptr(pbuf.data)) + C.OPENSSL_free(voidptr(pbuf)) C.BN_CTX_free(ctx) return result diff --git a/vlib/crypto/ecdsa/util.v b/vlib/crypto/ecdsa/util.v index 89e5f8bfb..180ef8873 100644 --- a/vlib/crypto/ecdsa/util.v +++ b/vlib/crypto/ecdsa/util.v @@ -70,15 +70,15 @@ pub fn pubkey_from_bytes(bytes []u8) !PublicKey { // bytes gets the bytes of public key. pub fn (pbk PublicKey) bytes() ![]u8 { - ppub := []u8{len: default_point_bufsize} - n := C.EVP_PKEY_get1_encoded_public_key(pbk.evpkey, voidptr(&ppub.data)) + mut ppub := &u8(unsafe { nil }) + n := C.EVP_PKEY_get1_encoded_public_key(pbk.evpkey, &ppub) if n <= 0 { - C.OPENSSL_free(voidptr(ppub.data)) + C.OPENSSL_free(voidptr(ppub)) return error('EVP_PKEY_get1_encoded_public_key failed') } - out := ppub[..n].clone() + out := unsafe { ppub.vbytes(int(n)).clone() } // ppub should be freed by calling `OPENSSL_free` or memleak happens. - C.OPENSSL_free(voidptr(ppub.data)) + C.OPENSSL_free(voidptr(ppub)) return out } -- 2.39.5