Skip to content

Commit 8d955ba

Browse files
committed
Stop using RSA_generate_key_ex
This switches _goboringcrypto_RSA_generate_key_fips to using EVP_PKEY_keygen function instead of RSA_generate_key_ex. The accessors functions around the RSA * type, such as RSA_get0_crt_params, are still used, though they are not a cryptographic operation so this patch leaves it as they are for now. Signed-off-by: Daiki Ueno <[email protected]>
1 parent e91c71b commit 8d955ba

File tree

4 files changed

+83
-17
lines changed

4 files changed

+83
-17
lines changed

openssl/goopenssl.h

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -585,9 +585,6 @@ int _goboringcrypto_EVP_RSA_verify(EVP_MD* md, const uint8_t *msg, unsigned int
585585

586586
DEFINEFUNC(GO_RSA *, RSA_new, (void), ())
587587
DEFINEFUNC(void, RSA_free, (GO_RSA * arg0), (arg0))
588-
DEFINEFUNC(int, RSA_generate_key_ex,
589-
(GO_RSA * arg0, int arg1, GO_BIGNUM *arg2, GO_BN_GENCB *arg3),
590-
(arg0, arg1, arg2, arg3))
591588

592589
DEFINEFUNCINTERNAL(int, RSA_set0_factors,
593590
(GO_RSA * rsa, GO_BIGNUM *p, GO_BIGNUM *q),
@@ -735,7 +732,8 @@ _goboringcrypto_RSA_get0_key(const GO_RSA *rsa, const GO_BIGNUM **n, const GO_BI
735732
#endif
736733
}
737734

738-
int _goboringcrypto_RSA_generate_key_fips(GO_RSA *, int, GO_BN_GENCB *);
735+
GO_RSA *_goboringcrypto_RSA_generate_key_fips(int bits);
736+
739737
enum
740738
{
741739
GO_RSA_PKCS1_PADDING = 1,
@@ -804,6 +802,7 @@ typedef EVP_PKEY GO_EVP_PKEY;
804802

805803
DEFINEFUNC(GO_EVP_PKEY *, EVP_PKEY_new, (void), ())
806804
DEFINEFUNC(void, EVP_PKEY_free, (GO_EVP_PKEY * arg0), (arg0))
805+
DEFINEFUNC(GO_RSA *, EVP_PKEY_get1_RSA, (GO_EVP_PKEY * arg0), (arg0))
807806
DEFINEFUNC(int, EVP_PKEY_set1_RSA, (GO_EVP_PKEY * arg0, GO_RSA *arg1), (arg0, arg1))
808807
DEFINEFUNC(int, EVP_PKEY_set1_EC_KEY, (GO_EVP_PKEY * arg0, GO_EC_KEY *arg1), (arg0, arg1))
809808
DEFINEFUNC(int, EVP_PKEY_verify,
@@ -869,6 +868,22 @@ _goboringcrypto_EVP_PKEY_CTX_set_rsa_mgf1_md(GO_EVP_PKEY_CTX * ctx, const GO_EVP
869868
EVP_PKEY_CTRL_RSA_MGF1_MD, 0, (void *)md);
870869
}
871870

871+
static inline int
872+
_goboringcrypto_EVP_PKEY_CTX_set_rsa_keygen_bits(GO_EVP_PKEY_CTX *ctx, int mbits) {
873+
return _goboringcrypto_EVP_PKEY_CTX_ctrl(ctx, -1,
874+
EVP_PKEY_OP_KEYGEN,
875+
EVP_PKEY_CTRL_RSA_KEYGEN_BITS,
876+
mbits, NULL);
877+
}
878+
879+
static inline int
880+
_goboringcrypto_EVP_PKEY_CTX_set_rsa_keygen_pubexp(GO_EVP_PKEY_CTX *ctx, GO_BIGNUM *pubexp) {
881+
return _goboringcrypto_EVP_PKEY_CTX_ctrl(ctx, -1,
882+
EVP_PKEY_OP_KEYGEN,
883+
EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP,
884+
0, pubexp);
885+
}
886+
872887
DEFINEFUNC(int, EVP_PKEY_decrypt,
873888
(GO_EVP_PKEY_CTX * arg0, uint8_t *arg1, size_t *arg2, const uint8_t *arg3, size_t arg4),
874889
(arg0, arg1, arg2, arg3, arg4))

openssl/openssl_port_rsa.c

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,42 @@
88
#include "goopenssl.h"
99

1010
// Only in BoringSSL.
11-
int _goboringcrypto_RSA_generate_key_fips(GO_RSA *rsa, int size,
12-
GO_BN_GENCB *cb) {
11+
GO_RSA *_goboringcrypto_RSA_generate_key_fips(int bits) {
12+
GO_EVP_PKEY_CTX *ctx = NULL;
13+
GO_EVP_PKEY *pkey = NULL;
14+
GO_BIGNUM *e = NULL;
15+
GO_RSA *ret = NULL;
16+
17+
ctx = _goboringcrypto_EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
18+
if (!ctx)
19+
return NULL;
20+
21+
if (_goboringcrypto_EVP_PKEY_keygen_init(ctx) <= 0)
22+
goto err;
23+
24+
if (_goboringcrypto_EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits) <= 0)
25+
goto err;
26+
1327
// BoringSSL's RSA_generate_key_fips hard-codes e to 65537.
14-
BIGNUM *e = _goboringcrypto_BN_new();
15-
if (e == NULL)
16-
return 0;
17-
int ret = _goboringcrypto_BN_set_word(e, RSA_F4) &&
18-
_goboringcrypto_RSA_generate_key_ex(rsa, size, e, cb);
28+
e = _goboringcrypto_BN_new();
29+
if (!e)
30+
goto err;
31+
32+
if (_goboringcrypto_BN_set_word(e, RSA_F4) <= 0)
33+
goto err;
34+
35+
if (_goboringcrypto_EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, e) <= 0)
36+
goto err;
37+
38+
if (_goboringcrypto_EVP_PKEY_keygen(ctx, &pkey) <= 0)
39+
goto err;
40+
41+
ret = _goboringcrypto_EVP_PKEY_get1_RSA(pkey);
42+
43+
err:
1944
_goboringcrypto_BN_free(e);
45+
_goboringcrypto_EVP_PKEY_free(pkey);
46+
_goboringcrypto_EVP_PKEY_CTX_free(ctx);
2047
return ret;
2148
}
2249

openssl/rsa.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,11 @@ func GenerateKeyRSA(bits int) (N, E, D, P, Q, Dp, Dq, Qinv BigInt, err error) {
2323
return nil, nil, nil, nil, nil, nil, nil, nil, e
2424
}
2525

26-
key := C._goboringcrypto_RSA_new()
26+
key := C._goboringcrypto_RSA_generate_key_fips(C.int(bits))
2727
if key == nil {
28-
return bad(NewOpenSSLError("RSA_new failed"))
29-
}
30-
defer C._goboringcrypto_RSA_free(key)
31-
32-
if C._goboringcrypto_RSA_generate_key_fips(key, C.int(bits), nil) == 0 {
3328
return bad(NewOpenSSLError("RSA_generate_key_fips failed"))
3429
}
30+
defer C._goboringcrypto_RSA_free(key)
3531

3632
var n, e, d, p, q, dp, dq, qinv *C.GO_BIGNUM
3733
C._goboringcrypto_RSA_get0_key(key, &n, &e, &d)

openssl/rsa_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,31 @@ func TestPKCS1v15(t *testing.T) {
139139
}
140140
}
141141
}
142+
143+
func TestKeyGeneration(t *testing.T) {
144+
for _, size := range []int{128, 1024, 2048, 3072} {
145+
n, e, _, _, _, _, _, _, err := openssl.GenerateKeyRSA(size)
146+
if size < 1024 {
147+
if err == nil {
148+
t.Errorf("GenerateKeyRSA(%d): unexpectedly succeeded", size)
149+
}
150+
continue
151+
} else {
152+
if err != nil {
153+
t.Errorf("GenerateKeyRSA(%d): %v", size, err)
154+
}
155+
}
156+
157+
if bbig.Dec(n).BitLen() != size {
158+
t.Errorf("GenerateKeyRSA(%d): bit size doesn't match: %v",
159+
size, bbig.Dec(n).BitLen())
160+
}
161+
162+
// BoringSSL's RSA_generate_key_fips hard-codes e to 65537.
163+
f4 := big.NewInt(65537)
164+
if bbig.Dec(e).Cmp(f4) != 0 {
165+
t.Errorf("GenerateKeyRSA(%d): pubexp doesn't match: %v != %v",
166+
size, bbig.Dec(e), f4)
167+
}
168+
}
169+
}

0 commit comments

Comments
 (0)