Skip to content

Commit 1c69b77

Browse files
committed
Stop using EC_KEY_generate_key for key generation
Signed-off-by: Daiki Ueno <[email protected]>
1 parent 26551da commit 1c69b77

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

openssl/ecdsa.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,14 +188,11 @@ func GenerateKeyECDSA(curve string) (X, Y, D BigInt, err error) {
188188
if err != nil {
189189
return nil, nil, nil, err
190190
}
191-
key := C._goboringcrypto_EC_KEY_new_by_curve_name(nid)
191+
key := C._goboringcrypto_EC_KEY_generate_key_fips(nid)
192192
if key == nil {
193-
return nil, nil, nil, NewOpenSSLError("EC_KEY_new_by_curve_name failed")
193+
return nil, nil, nil, NewOpenSSLError("EC_KEY_generate_key_fips failed")
194194
}
195195
defer C._goboringcrypto_EC_KEY_free(key)
196-
if C._goboringcrypto_EC_KEY_generate_key(key) == 0 {
197-
return nil, nil, nil, NewOpenSSLError("EC_KEY_generate_key failed")
198-
}
199196
group := C._goboringcrypto_EC_KEY_get0_group(key)
200197
pt := C._goboringcrypto_EC_KEY_get0_public_key(key)
201198
bd := C._goboringcrypto_EC_KEY_get0_private_key(key)

openssl/goopenssl.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,12 +459,12 @@ DEFINEFUNC(int, EC_POINT_set_affine_coordinates_GFp,
459459

460460
typedef EC_KEY GO_EC_KEY;
461461

462+
GO_EC_KEY *_goboringcrypto_EC_KEY_generate_key_fips(int nid);
463+
462464
DEFINEFUNC(GO_EC_KEY *, EC_KEY_new, (void), ())
463465
DEFINEFUNC(GO_EC_KEY *, EC_KEY_new_by_curve_name, (int arg0), (arg0))
464466
DEFINEFUNC(void, EC_KEY_free, (GO_EC_KEY * arg0), (arg0))
465467
DEFINEFUNC(const GO_EC_GROUP *, EC_KEY_get0_group, (const GO_EC_KEY *arg0), (arg0))
466-
DEFINEFUNC(int, EC_KEY_set_group, (GO_EC_KEY *arg0, const EC_GROUP *arg1), (arg0, arg1))
467-
DEFINEFUNC(int, EC_KEY_generate_key, (GO_EC_KEY * arg0), (arg0))
468468
DEFINEFUNC(int, EC_KEY_set_private_key, (GO_EC_KEY * arg0, const GO_BIGNUM *arg1), (arg0, arg1))
469469
DEFINEFUNC(int, EC_KEY_set_public_key, (GO_EC_KEY * arg0, const GO_EC_POINT *arg1), (arg0, arg1))
470470
DEFINEFUNC(const GO_BIGNUM *, EC_KEY_get0_private_key, (const GO_EC_KEY *arg0), (arg0))
@@ -803,6 +803,7 @@ DEFINEFUNC(GO_EVP_PKEY *, EVP_PKEY_new, (void), ())
803803
DEFINEFUNC(void, EVP_PKEY_free, (GO_EVP_PKEY * arg0), (arg0))
804804
DEFINEFUNC(GO_RSA *, EVP_PKEY_get1_RSA, (GO_EVP_PKEY * arg0), (arg0))
805805
DEFINEFUNC(int, EVP_PKEY_set1_RSA, (GO_EVP_PKEY * arg0, GO_RSA *arg1), (arg0, arg1))
806+
DEFINEFUNC(GO_EC_KEY *, EVP_PKEY_get1_EC_KEY, (GO_EVP_PKEY * arg0), (arg0))
806807
DEFINEFUNC(int, EVP_PKEY_set1_EC_KEY, (GO_EVP_PKEY * arg0, GO_EC_KEY *arg1), (arg0, arg1))
807808
DEFINEFUNC(int, EVP_PKEY_verify,
808809
(EVP_PKEY_CTX *ctx, const unsigned char *sig, unsigned int siglen, const unsigned char *tbs, size_t tbslen),
@@ -883,6 +884,13 @@ _goboringcrypto_EVP_PKEY_CTX_set_rsa_keygen_pubexp(GO_EVP_PKEY_CTX *ctx, GO_BIGN
883884
0, pubexp);
884885
}
885886

887+
static inline int
888+
_goboringcrypto_EVP_PKEY_CTX_set_ec_paramgen_curve_nid(GO_EVP_PKEY_CTX *ctx, int nid) {
889+
return _goboringcrypto_EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC,
890+
EVP_PKEY_OP_PARAMGEN|EVP_PKEY_OP_KEYGEN,
891+
EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID, nid, NULL);
892+
}
893+
886894
DEFINEFUNC(int, EVP_PKEY_decrypt,
887895
(GO_EVP_PKEY_CTX * arg0, uint8_t *arg1, size_t *arg2, const uint8_t *arg3, size_t arg4),
888896
(arg0, arg1, arg2, arg3, arg4))

openssl/openssl_ecdsa_signature.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,34 @@
66

77
#include "goopenssl.h"
88

9+
// Only in BoringSSL.
10+
GO_EC_KEY *_goboringcrypto_EC_KEY_generate_key_fips(int nid) {
11+
GO_EVP_PKEY_CTX *ctx = NULL;
12+
GO_EVP_PKEY *pkey = NULL;
13+
GO_BIGNUM *e = NULL;
14+
GO_EC_KEY *ret = NULL;
15+
16+
ctx = _goboringcrypto_EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL);
17+
if (!ctx)
18+
return NULL;
19+
20+
if (_goboringcrypto_EVP_PKEY_keygen_init(ctx) <= 0)
21+
goto err;
22+
23+
if (_goboringcrypto_EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid) <= 0)
24+
goto err;
25+
26+
if (_goboringcrypto_EVP_PKEY_keygen(ctx, &pkey) <= 0)
27+
goto err;
28+
29+
ret = _goboringcrypto_EVP_PKEY_get1_EC_KEY(pkey);
30+
31+
err:
32+
_goboringcrypto_EVP_PKEY_free(pkey);
33+
_goboringcrypto_EVP_PKEY_CTX_free(ctx);
34+
return ret;
35+
}
36+
937
int _goboringcrypto_ECDSA_sign(EVP_MD *md, const uint8_t *msg, size_t msgLen,
1038
uint8_t *sig, size_t *slen,
1139
GO_EC_KEY *eckey) {

0 commit comments

Comments
 (0)