Skip to content

Commit f0d4c10

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 d13c0e0 commit f0d4c10

File tree

4 files changed

+83
-21
lines changed

4 files changed

+83
-21
lines changed

openssl/goopenssl.h

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -575,9 +575,6 @@ int _goboringcrypto_ECDSA_verify(EVP_MD *md, const uint8_t *arg1, size_t arg2, c
575575

576576
#include <openssl/rsa.h>
577577

578-
// Note: order of struct fields here is unchecked.
579-
typedef BN_GENCB GO_BN_GENCB;
580-
581578
int _goboringcrypto_RSA_sign(EVP_MD* md, const uint8_t *msg, unsigned int msgLen, uint8_t *sig, size_t *slen, RSA *rsa);
582579
int _goboringcrypto_RSA_verify(EVP_MD* md, const uint8_t *msg, unsigned int msgLen, const uint8_t *sig, unsigned int slen, GO_RSA *rsa);
583580

@@ -590,9 +587,6 @@ int _goboringcrypto_RSA_verify_raw(EVP_MD *md, const uint8_t *msg, size_t msgLen
590587

591588
DEFINEFUNC(GO_RSA *, RSA_new, (void), ())
592589
DEFINEFUNC(void, RSA_free, (GO_RSA * arg0), (arg0))
593-
DEFINEFUNC(int, RSA_generate_key_ex,
594-
(GO_RSA * arg0, int arg1, GO_BIGNUM *arg2, GO_BN_GENCB *arg3),
595-
(arg0, arg1, arg2, arg3))
596590

597591
DEFINEFUNCINTERNAL(int, RSA_set0_factors,
598592
(GO_RSA * rsa, GO_BIGNUM *p, GO_BIGNUM *q),
@@ -740,7 +734,8 @@ _goboringcrypto_RSA_get0_key(const GO_RSA *rsa, const GO_BIGNUM **n, const GO_BI
740734
#endif
741735
}
742736

743-
int _goboringcrypto_RSA_generate_key_fips(GO_RSA *, int, GO_BN_GENCB *);
737+
GO_RSA *_goboringcrypto_RSA_generate_key_fips(int bits);
738+
744739
enum
745740
{
746741
GO_RSA_PKCS1_PADDING = 1,
@@ -759,7 +754,6 @@ int _goboringcrypto_RSA_sign_pss_mgf1(GO_RSA *, unsigned int *out_len, uint8_t *
759754
int _goboringcrypto_RSA_verify_pss_mgf1(GO_RSA *, const uint8_t *msg, unsigned int msg_len, GO_EVP_MD *md, const GO_EVP_MD *mgf1_md, int salt_len, const uint8_t *sig, unsigned int sig_len);
760755

761756
DEFINEFUNC(unsigned int, RSA_size, (const GO_RSA *arg0), (arg0))
762-
DEFINEFUNC(int, RSA_check_key, (const GO_RSA *arg0), (arg0))
763757

764758
DEFINEFUNC(int, EVP_EncryptInit_ex,
765759
(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type, ENGINE *impl, const unsigned char *key, const unsigned char *iv),
@@ -814,6 +808,7 @@ typedef EVP_PKEY GO_EVP_PKEY;
814808

815809
DEFINEFUNC(GO_EVP_PKEY *, EVP_PKEY_new, (void), ())
816810
DEFINEFUNC(void, EVP_PKEY_free, (GO_EVP_PKEY * arg0), (arg0))
811+
DEFINEFUNC(GO_RSA *, EVP_PKEY_get1_RSA, (GO_EVP_PKEY * arg0), (arg0))
817812
DEFINEFUNC(int, EVP_PKEY_set1_RSA, (GO_EVP_PKEY * arg0, GO_RSA *arg1), (arg0, arg1))
818813
DEFINEFUNC(int, EVP_PKEY_set1_EC_KEY, (GO_EVP_PKEY * arg0, GO_EC_KEY *arg1), (arg0, arg1))
819814
DEFINEFUNC(int, EVP_PKEY_verify,
@@ -879,6 +874,22 @@ _goboringcrypto_EVP_PKEY_CTX_set_rsa_mgf1_md(GO_EVP_PKEY_CTX * ctx, const GO_EVP
879874
EVP_PKEY_CTRL_RSA_MGF1_MD, 0, (void *)md);
880875
}
881876

877+
static inline int
878+
_goboringcrypto_EVP_PKEY_CTX_set_rsa_keygen_bits(GO_EVP_PKEY_CTX *ctx, int mbits) {
879+
return _goboringcrypto_EVP_PKEY_CTX_ctrl(ctx, -1,
880+
EVP_PKEY_OP_KEYGEN,
881+
EVP_PKEY_CTRL_RSA_KEYGEN_BITS,
882+
mbits, NULL);
883+
}
884+
885+
static inline int
886+
_goboringcrypto_EVP_PKEY_CTX_set_rsa_keygen_pubexp(GO_EVP_PKEY_CTX *ctx, GO_BIGNUM *pubexp) {
887+
return _goboringcrypto_EVP_PKEY_CTX_ctrl(ctx, -1,
888+
EVP_PKEY_OP_KEYGEN,
889+
EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP,
890+
0, pubexp);
891+
}
892+
882893
DEFINEFUNC(int, EVP_PKEY_decrypt,
883894
(GO_EVP_PKEY_CTX * arg0, uint8_t *arg1, size_t *arg2, const uint8_t *arg3, size_t arg4),
884895
(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)