Skip to content

Commit c2b7dbf

Browse files
authored
crypto.ecdsa: improve safety checking, unify signing (and verifying) api to accept options (#23463)
1 parent 3c48780 commit c2b7dbf

8 files changed

Lines changed: 748 additions & 105 deletions

File tree

‎cmd/tools/modules/testing/common.v‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ pub fn new_test_session(_vargs string, will_compile bool) TestSession {
252252
skip_files << 'vlib/v/tests/websocket_logger_interface_should_compile_test.v' // requires OpenSSL
253253
skip_files << 'vlib/crypto/ecdsa/ecdsa_test.v' // requires OpenSSL
254254
skip_files << 'vlib/crypto/ecdsa/util_test.v' // requires OpenSSL
255+
skip_files << 'vlib/crypto/ecdsa/example/ecdsa_seed_test.v' // requires OpenSSL
255256
$if tinyc {
256257
skip_files << 'examples/database/orm.v' // try fix it
257258
}
@@ -284,13 +285,15 @@ pub fn new_test_session(_vargs string, will_compile bool) TestSession {
284285
skip_files << 'vlib/net/openssl/openssl_compiles_test.c.v'
285286
skip_files << 'vlib/crypto/ecdsa/ecdsa_test.v' // requires OpenSSL
286287
skip_files << 'vlib/crypto/ecdsa/util_test.v' // requires OpenSSL
288+
skip_files << 'vlib/crypto/ecdsa/example/ecdsa_seed_test.v' // requires OpenSSL
287289
skip_files << 'vlib/x/ttf/ttf_test.v'
288290
skip_files << 'vlib/encoding/iconv/iconv_test.v' // needs libiconv to be installed
289291
}
290292
if github_job == 'tests-sanitize-memory-clang' {
291293
skip_files << 'vlib/net/openssl/openssl_compiles_test.c.v'
292294
skip_files << 'vlib/crypto/ecdsa/ecdsa_test.v' // requires OpenSSL
293295
skip_files << 'vlib/crypto/ecdsa/util_test.v' // requires OpenSSL
296+
skip_files << 'vlib/crypto/ecdsa/example/ecdsa_seed_test.v' // requires OpenSSL
294297
// Fails compilation with: `/usr/bin/ld: /lib/x86_64-linux-gnu/libpthread.so.0: error adding symbols: DSO missing from command line`
295298
skip_files << 'examples/sokol/sounds/simple_sin_tones.v'
296299
}

‎vlib/crypto/ecdsa/README.md‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,38 @@
22

33
`ecdsa` module for V language. Its a wrapper on top of openssl ecdsa functionality.
44
Its currently (expanded) to support the following curves:
5+
56
- NIST P-256 curve, commonly referred as prime256v1 or secp256r1
67
- NIST P-384 curve, commonly referred as secp384r1
78
- NIST P-521 curve, commonly referred as secp521r1
8-
- A famous Bitcoin curve, commonly referred as secp256k1
9+
- A famous Bitcoin curve, commonly referred as secp256k1
10+
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+
18+
# Example
19+
```v
20+
import crypto.ecdsa
21+
22+
fn main() {
23+
// create default NIST P-256 secp256r1 curve key pair. If you wish to generate another curve,
24+
// use: `pbkey, pvkey := ecdsa.generate_key(nid: .secp521r1)!` instead.
25+
pbkey, pvkey := ecdsa.generate_key()!
26+
27+
message_tobe_signed := 'Hello ecdsa'.bytes()
28+
// create a signature with the recommended hash
29+
signature := pvkey.sign(message_tobe_signed)!
30+
31+
// verify the message with the signature
32+
verified := pbkey.verify(message_tobe_signed, signature)!
33+
dump(verified) // should be true
34+
35+
// free allocated keys when you have done with your work.
36+
pbkey.free()
37+
pvkey.free()
38+
}
39+
```

0 commit comments

Comments
 (0)