Skip to content

Commit 1eaa7c8

Browse files
authored
crypto.ecdsa: split out the C wrapper to a new .c.v file (#23595)
1 parent 3343fb7 commit 1eaa7c8

File tree

3 files changed

+119
-129
lines changed

3 files changed

+119
-129
lines changed

vlib/crypto/ecdsa/ecdsa.c.v

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
2+
// Use of this source code is governed by an MIT license
3+
// that can be found in the LICENSE file.
4+
module ecdsa
5+
6+
// See https://docs.openssl.org/master/man7/openssl_user_macros/#description
7+
// should be 0x30000000L, but a lot of EC_KEY method was deprecated on version 3.0
8+
// #define OPENSSL_API_COMPAT 0x10100000L
9+
10+
#flag darwin -L /opt/homebrew/opt/openssl/lib -I /opt/homebrew/opt/openssl/include
11+
12+
#flag -I/usr/include/openssl
13+
#flag -lcrypto
14+
#flag darwin -I/usr/local/opt/openssl/include
15+
#flag darwin -L/usr/local/opt/openssl/lib
16+
#include <openssl/ecdsa.h>
17+
#include <openssl/obj_mac.h>
18+
#include <openssl/objects.h>
19+
#include <openssl/bn.h>
20+
#include <openssl/evp.h>
21+
#include <openssl/x509.h>
22+
#include <openssl/bio.h>
23+
#include <openssl/pem.h>
24+
25+
// The new opaque of public key pair high level API
26+
@[typedef]
27+
struct C.EVP_PKEY {}
28+
29+
fn C.EVP_PKEY_new() &C.EVP_PKEY
30+
fn C.EVP_PKEY_free(key &C.EVP_PKEY)
31+
fn C.EVP_PKEY_get1_EC_KEY(pkey &C.EVP_PKEY) &C.EC_KEY
32+
fn C.EVP_PKEY_base_id(key &C.EVP_PKEY) int
33+
34+
// Elliptic curve keypair declarations
35+
@[typedef]
36+
struct C.EC_KEY {}
37+
38+
fn C.EC_KEY_new_by_curve_name(nid int) &C.EC_KEY
39+
fn C.EC_KEY_generate_key(key &C.EC_KEY) int
40+
fn C.EC_KEY_dup(src &C.EC_KEY) &C.EC_KEY
41+
fn C.EC_KEY_free(key &C.EC_KEY)
42+
fn C.EC_KEY_set_public_key(key &C.EC_KEY, &C.EC_POINT) int
43+
fn C.EC_KEY_set_private_key(key &C.EC_KEY, prv &C.BIGNUM) int
44+
fn C.EC_KEY_get0_group(key &C.EC_KEY) &C.EC_GROUP
45+
fn C.EC_KEY_get0_private_key(key &C.EC_KEY) &C.BIGNUM
46+
fn C.EC_KEY_get0_public_key(key &C.EC_KEY) &C.EC_POINT
47+
fn C.EC_KEY_get_conv_form(k &C.EC_KEY) int
48+
fn C.EC_KEY_check_key(key &C.EC_KEY) int
49+
fn C.EC_KEY_up_ref(key &C.EC_KEY) int
50+
51+
// BIO input output declarations.
52+
@[typedef]
53+
struct C.BIO_METHOD {}
54+
55+
@[typedef]
56+
pub struct C.BIO {}
57+
58+
fn C.BIO_new(t &C.BIO_METHOD) &C.BIO
59+
fn C.BIO_free_all(a &C.BIO)
60+
fn C.BIO_s_mem() &C.BIO_METHOD
61+
fn C.BIO_write(b &C.BIO, buf &u8, length int) int
62+
fn C.PEM_read_bio_PrivateKey(bp &C.BIO, x &&C.EVP_PKEY, cb int, u &voidptr) &C.EVP_PKEY
63+
fn C.PEM_read_bio_PUBKEY(bp &C.BIO, x &&C.EVP_PKEY, cb int, u &voidptr) &C.EVP_PKEY
64+
fn C.d2i_PUBKEY(k &&C.EVP_PKEY, pp &&u8, length u32) &C.EVP_PKEY
65+
66+
// Elliptic curve point related declarations.
67+
@[typedef]
68+
struct C.EC_POINT {}
69+
70+
fn C.EC_POINT_new(group &C.EC_GROUP) &C.EC_POINT
71+
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) int
72+
fn C.EC_POINT_point2oct(g &C.EC_GROUP, p &C.EC_POINT, form int, buf &u8, max_out int, ctx &C.BN_CTX) int
73+
fn C.EC_POINT_cmp(group &C.EC_GROUP, a &C.EC_POINT, b &C.EC_POINT, ctx &C.BN_CTX) int
74+
fn C.EC_POINT_free(point &C.EC_POINT)
75+
76+
// Elliptic group (curve) related declarations.
77+
@[typedef]
78+
struct C.EC_GROUP {}
79+
80+
fn C.EC_GROUP_free(group &C.EC_GROUP)
81+
fn C.EC_GROUP_get_degree(g &C.EC_GROUP) int
82+
fn C.EC_GROUP_get_curve_name(g &C.EC_GROUP) int
83+
fn C.EC_GROUP_cmp(a &C.EC_GROUP, b &C.EC_GROUP, ctx &C.BN_CTX) int
84+
85+
// Elliptic BIGNUM related declarations.
86+
@[typedef]
87+
struct C.BIGNUM {}
88+
89+
fn C.BN_num_bits(a &C.BIGNUM) int
90+
fn C.BN_bn2bin(a &C.BIGNUM, to &u8) int
91+
fn C.BN_bn2binpad(a &C.BIGNUM, to &u8, tolen int) int
92+
fn C.BN_cmp(a &C.BIGNUM, b &C.BIGNUM) int
93+
fn C.BN_bin2bn(s &u8, len int, ret &C.BIGNUM) &C.BIGNUM
94+
fn C.BN_free(a &C.BIGNUM)
95+
96+
// BIGNUM context
97+
@[typedef]
98+
struct C.BN_CTX {}
99+
100+
fn C.BN_CTX_new() &C.BN_CTX
101+
fn C.BN_CTX_free(ctx &C.BN_CTX)
102+
103+
// ELliptic ECDSA signing and verifying related declarations.
104+
@[typedef]
105+
struct C.ECDSA_SIG {}
106+
107+
fn C.ECDSA_size(key &C.EC_KEY) u32
108+
fn C.ECDSA_sign(type_ int, dgst &u8, dgstlen int, sig &u8, siglen &u32, eckey &C.EC_KEY) int
109+
fn C.ECDSA_verify(type_ int, dgst &u8, dgstlen int, sig &u8, siglen int, eckey &C.EC_KEY) int

vlib/crypto/ecdsa/ecdsa.v

Lines changed: 10 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -8,50 +8,6 @@ import crypto
88
import crypto.sha256
99
import crypto.sha512
1010

11-
// See https://docs.openssl.org/master/man7/openssl_user_macros/#description
12-
// should be 0x30000000L, but a lot of EC_KEY method was deprecated on version 3.0
13-
// #define OPENSSL_API_COMPAT 0x10100000L
14-
15-
#flag darwin -L /opt/homebrew/opt/openssl/lib -I /opt/homebrew/opt/openssl/include
16-
17-
#flag -I/usr/include/openssl
18-
#flag -lcrypto
19-
#flag darwin -I/usr/local/opt/openssl/include
20-
#flag darwin -L/usr/local/opt/openssl/lib
21-
#include <openssl/ecdsa.h>
22-
#include <openssl/obj_mac.h>
23-
#include <openssl/objects.h>
24-
#include <openssl/bn.h>
25-
26-
// C function declarations
27-
fn C.EC_KEY_new_by_curve_name(nid int) &C.EC_KEY
28-
fn C.EC_KEY_dup(src &C.EC_KEY) &C.EC_KEY
29-
fn C.EC_KEY_generate_key(key &C.EC_KEY) int
30-
fn C.EC_KEY_free(key &C.EC_KEY)
31-
fn C.EC_KEY_set_public_key(key &C.EC_KEY, &C.EC_POINT) int
32-
fn C.EC_KEY_set_private_key(key &C.EC_KEY, prv &C.BIGNUM) int
33-
fn C.EC_KEY_get0_group(key &C.EC_KEY) &C.EC_GROUP
34-
fn C.EC_KEY_get0_private_key(key &C.EC_KEY) &C.BIGNUM
35-
fn C.EC_KEY_get0_public_key(key &C.EC_KEY) &C.EC_POINT
36-
fn C.EC_KEY_check_key(key &C.EC_KEY) int
37-
fn C.EC_KEY_up_ref(key &C.EC_KEY) int
38-
fn C.EC_POINT_new(group &C.EC_GROUP) &C.EC_POINT
39-
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) int
40-
fn C.EC_POINT_cmp(group &C.EC_GROUP, a &C.EC_POINT, b &C.EC_POINT, ctx &C.BN_CTX) int
41-
fn C.EC_POINT_free(point &C.EC_POINT)
42-
fn C.EC_GROUP_cmp(a &C.EC_GROUP, b &C.EC_GROUP, ctx &C.BN_CTX) int
43-
fn C.BN_num_bits(a &C.BIGNUM) int
44-
fn C.BN_bn2bin(a &C.BIGNUM, to &u8) int
45-
fn C.BN_bn2binpad(a &C.BIGNUM, to &u8, tolen int) int
46-
fn C.BN_cmp(a &C.BIGNUM, b &C.BIGNUM) int
47-
fn C.BN_CTX_new() &C.BN_CTX
48-
fn C.BN_CTX_free(ctx &C.BN_CTX)
49-
fn C.BN_bin2bn(s &u8, len int, ret &C.BIGNUM) &C.BIGNUM
50-
fn C.BN_free(a &C.BIGNUM)
51-
fn C.ECDSA_size(key &C.EC_KEY) u32
52-
fn C.ECDSA_sign(type_ int, dgst &u8, dgstlen int, sig &u8, siglen &u32, eckey &C.EC_KEY) int
53-
fn C.ECDSA_verify(type_ int, dgst &u8, dgstlen int, sig &u8, siglen int, eckey &C.EC_KEY) int
54-
5511
// NID constants
5612
//
5713
// NIST P-256 is refered to as secp256r1 and prime256v1, defined as #define NID_X9_62_prime256v1 415
@@ -68,6 +24,9 @@ const nid_secp521r1 = C.NID_secp521r1
6824
// Bitcoin curve, defined as #define NID_secp256k1 714
6925
const nid_secp256k1 = C.NID_secp256k1
7026

27+
// #define NID_X9_62_id_ecPublicKey 408
28+
const nid_ec_publickey = C.NID_X9_62_id_ecPublicKey
29+
7130
// The list of supported curve(s)
7231
pub enum Nid {
7332
prime256v1
@@ -87,24 +46,6 @@ pub mut:
8746
fixed_size bool
8847
}
8948

90-
@[typedef]
91-
struct C.EC_KEY {}
92-
93-
@[typedef]
94-
struct C.EC_GROUP {}
95-
96-
@[typedef]
97-
struct C.BIGNUM {}
98-
99-
@[typedef]
100-
struct C.EC_POINT {}
101-
102-
@[typedef]
103-
struct C.ECDSA_SIG {}
104-
105-
@[typedef]
106-
struct C.BN_CTX {}
107-
10849
// enum flag to allow flexible PrivateKey size
10950
enum KeyFlag {
11051
// flexible flag to allow flexible-size of seed bytes
@@ -116,6 +57,10 @@ enum KeyFlag {
11657
// PrivateKey represents ECDSA private key. Actually its a key pair,
11758
// contains private key and public key parts.
11859
pub struct PrivateKey {
60+
// The new high level of keypair opaque, set to nil now.
61+
evpkey &C.EVP_PKEY = unsafe { nil }
62+
// TODO: when all has been migrated to the new one,
63+
// removes this low level declarations.
11964
key &C.EC_KEY
12065
mut:
12166
// ks_flag with .flexible value allowing
@@ -129,6 +74,9 @@ mut:
12974

13075
// PublicKey represents ECDSA public key for verifying message.
13176
pub struct PublicKey {
77+
// The new high level of keypair opaque, set to nil now.
78+
evpkey &C.EVP_PKEY = unsafe { nil }
79+
// Remove this when its fully obsoleted by the new one.
13280
key &C.EC_KEY
13381
}
13482

vlib/crypto/ecdsa/util.v

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,5 @@
11
module ecdsa
22

3-
#include <openssl/evp.h>
4-
#include <openssl/err.h>
5-
#include <openssl/x509.h>
6-
#include <openssl/bio.h>
7-
#include <openssl/pem.h>
8-
9-
// #define NID_X9_62_id_ecPublicKey 408
10-
const nid_ec_publickey = C.NID_X9_62_id_ecPublicKey
11-
12-
@[typedef]
13-
struct C.EVP_PKEY {}
14-
15-
@[typedef]
16-
struct C.BIO_METHOD {}
17-
18-
@[typedef]
19-
pub struct C.BIO {}
20-
21-
// EVP_PKEY *EVP_PKEY_new(void);
22-
fn C.EVP_PKEY_new() &C.EVP_PKEY
23-
24-
// EVP_PKEY_free(EVP_PKEY *key);
25-
fn C.EVP_PKEY_free(key &C.EVP_PKEY)
26-
27-
// EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey);
28-
fn C.EVP_PKEY_get1_EC_KEY(pkey &C.EVP_PKEY) &C.EC_KEY
29-
30-
// EVP_PKEY *d2i_PUBKEY(EVP_PKEY **a, const unsigned char **pp, long length);
31-
fn C.d2i_PUBKEY(k &&C.EVP_PKEY, pp &&u8, length u32) &C.EVP_PKEY
32-
33-
// point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key);
34-
fn C.EC_KEY_get_conv_form(k &C.EC_KEY) int
35-
36-
// EC_GROUP_get_degree
37-
fn C.EC_GROUP_get_degree(g &C.EC_GROUP) int
38-
39-
// const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key);
40-
fn C.EC_KEY_get0_public_key(key &C.EC_KEY) &C.EC_POINT
41-
42-
// size_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *point, point_conversion_form_t form, uint8_t *buf, size_t max_out, BN_CTX *ctx);
43-
fn C.EC_POINT_point2oct(g &C.EC_GROUP, p &C.EC_POINT, form int, buf &u8, max_out int, ctx &C.BN_CTX) int
44-
45-
// int EVP_PKEY_get_base_id(const EVP_PKEY *pkey);
46-
fn C.EVP_PKEY_base_id(key &C.EVP_PKEY) int
47-
48-
// int EC_GROUP_get_curve_name(const EC_GROUP *group);
49-
fn C.EC_GROUP_get_curve_name(g &C.EC_GROUP) int
50-
fn C.EC_GROUP_free(group &C.EC_GROUP)
51-
52-
// BIO * BIO_new(BIO_METHOD *type);
53-
fn C.BIO_new(t &C.BIO_METHOD) &C.BIO
54-
55-
// void BIO_free_all(BIO *a);
56-
fn C.BIO_free_all(a &C.BIO)
57-
58-
// BIO_METHOD * BIO_s_mem(void);
59-
fn C.BIO_s_mem() &C.BIO_METHOD
60-
61-
// int BIO_write(BIO *b, const void *buf, int len);
62-
fn C.BIO_write(b &C.BIO, buf &u8, length int) int
63-
64-
// EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb, void *u);
65-
fn C.PEM_read_bio_PrivateKey(bp &C.BIO, x &&C.EVP_PKEY, cb int, u &voidptr) &C.EVP_PKEY
66-
67-
// EVP_PKEY *PEM_read_bio_PUBKEY(BIO *bp, EVP_PKEY **x, pem_password_cb *cb, void *u);
68-
fn C.PEM_read_bio_PUBKEY(bp &C.BIO, x &&C.EVP_PKEY, cb int, u &voidptr) &C.EVP_PKEY
69-
703
// pubkey_from_bytes loads ECDSA Public Key from bytes array.
714
// The bytes of data should be a valid of ASN.1 DER serialized SubjectPublicKeyInfo structrue of RFC 5480.
725
// Otherwise, its should an error.

0 commit comments

Comments
 (0)