Skip to content

Commit 6623ac2

Browse files
authored
crypto.ecdsa: update and cleanup the documentation (#23897)
1 parent 50b716b commit 6623ac2

File tree

3 files changed

+10
-24
lines changed

3 files changed

+10
-24
lines changed

vlib/crypto/ecdsa/README.md

-7
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,6 @@ Its currently (expanded) to support the following curves:
88
- NIST P-521 curve, commonly referred as secp521r1
99
- A famous Bitcoin curve, commonly referred as secp256k1
1010

11-
> [!CAUTION]
12-
> This module using low level OpenSSL opaque methods that mostly has been deprecated
13-
> in OpenSSL 3.0.
14-
> Please be aware, likely it would not compile with `-cstrict` options until
15-
> its migrated into supported higher level API.
16-
17-
1811
# Example
1912
```v
2013
import crypto.ecdsa

vlib/crypto/ecdsa/ecdsa.v

+7-14
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ fn (nid Nid) str() string {
5454
}
5555
}
5656

57+
// CurveOptions represents configuration options to drive keypair generation.
5758
@[params]
5859
pub struct CurveOptions {
5960
pub mut:
@@ -94,7 +95,7 @@ enum KeyFlag {
9495
}
9596

9697
// generate_key generates a new key pair. If opt was not provided, its default to prime256v1 curve.
97-
// If you want another curve, use in the following manner: `pubkey, pivkey := ecdsa.generate_key(nid: .secp384r1)!`
98+
// If you want another curve, use `pubkey, pivkey := ecdsa.generate_key(nid: .secp384r1)!` instead.
9899
pub fn generate_key(opt CurveOptions) !(PublicKey, PrivateKey) {
99100
// This can be simplified to just more simpler one
100101
pv := PrivateKey.new(opt)!
@@ -111,7 +112,7 @@ pub fn generate_key(opt CurveOptions) !(PublicKey, PrivateKey) {
111112
// You should make sure, the seed bytes come from a cryptographically secure random generator,
112113
// likes the `crypto.rand` or other trusted sources.
113114
// Internally, the seed size's would be checked to not exceed the key size of underlying curve,
114-
// ie, 32 bytes length for p-256 and secp256k1, 48 bytes length for p-384 and 64 bytes length for p-521.
115+
// ie, 32 bytes length for p-256 and secp256k1, 48 bytes length for p-384 and 66 bytes length for p-521.
115116
// Its recommended to use seed with bytes length matching with underlying curve key size.
116117
pub fn new_key_from_seed(seed []u8, opt CurveOptions) !PrivateKey {
117118
// Early exit check
@@ -276,9 +277,6 @@ pub fn (pv PrivateKey) seed() ![]u8 {
276277

277278
// public_key gets the PublicKey from private key.
278279
pub fn (pv PrivateKey) public_key() !PublicKey {
279-
// Check if EVP_PKEY opaque was availables or not.
280-
// TODO: removes this check when its ready
281-
282280
bo := C.BIO_new(C.BIO_s_mem())
283281
n := C.i2d_PUBKEY_bio(bo, pv.evpkey)
284282
assert n != 0
@@ -292,17 +290,13 @@ pub fn (pv PrivateKey) public_key() !PublicKey {
292290
}
293291
}
294292

295-
// equal compares two private keys was equal. Its checks for two things, ie:
296-
//
297-
// - whether both of private keys lives under the same group (curve),
298-
// - compares if two private key bytes was equal.
293+
// equal compares two private keys was equal.
299294
pub fn (priv_key PrivateKey) equal(other PrivateKey) bool {
300295
eq := C.EVP_PKEY_eq(voidptr(priv_key.evpkey), voidptr(other.evpkey))
301296
return eq == 1
302297
}
303298

304-
// free clears out allocated memory for PrivateKey
305-
// Dont use PrivateKey after calling `.free()`
299+
// free clears out allocated memory for PrivateKey. Dont use PrivateKey after calling `.free()`
306300
pub fn (pv &PrivateKey) free() {
307301
C.EVP_PKEY_free(pv.evpkey)
308302
}
@@ -321,14 +315,13 @@ pub fn (pb PublicKey) verify(message []u8, sig []u8, opt SignerOpts) !bool {
321315
return verify_signature(pb.evpkey, sig, digest)
322316
}
323317

324-
// Compare two public keys
318+
// equal compares two public keys was equal.
325319
pub fn (pub_key PublicKey) equal(other PublicKey) bool {
326320
eq := C.EVP_PKEY_eq(voidptr(pub_key.evpkey), voidptr(other.evpkey))
327321
return eq == 1
328322
}
329323

330-
// free clears out allocated memory for PublicKey.
331-
// Dont use PublicKey after calling `.free()`
324+
// free clears out allocated memory for PublicKey. Dont use PublicKey after calling `.free()`
332325
pub fn (pb &PublicKey) free() {
333326
C.EVP_PKEY_free(pb.evpkey)
334327
}

vlib/crypto/ecdsa/util.v

+3-3
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ pub fn pubkey_from_string(s string) !PublicKey {
151151
}
152152

153153
// privkey_from_string loads a PrivateKey from valid PEM-formatted string in s.
154-
// Underlying wrapper support for old secg and pkcs8 private key format, but this was not heavily tested.
155-
// This routine does not support for the pkcs8 EncryptedPrivateKeyInfo format.
156-
// See [usage_test.v](https://github.com/vlang/v/blob/master/vlib/crypto/ecdsa/example/ecdsa_seed_test.v) file
154+
// Underlying wrapper support for old SECG and PKCS8 private key format, but this was not heavily tested.
155+
// This routine does not support for the PKCS8 EncryptedPrivateKeyInfo format.
156+
// See [ecdsa_seed_test.v](https://github.com/vlang/v/blob/master/vlib/crypto/ecdsa/example/ecdsa_seed_test.v) file
157157
// for example of usage.
158158
pub fn privkey_from_string(s string) !PrivateKey {
159159
if s.len == 0 {

0 commit comments

Comments
 (0)