summaryrefslogtreecommitdiffstats
path: root/crypto/rsa/rsa_gen.c
diff options
context:
space:
mode:
authorRich Salz <rsalz@openssl.org>2017-03-06 09:54:17 -0500
committerRich Salz <rsalz@openssl.org>2017-03-06 10:02:42 -0500
commit0d41526f4f76b2f9384540503c21e8ef41e1fbbb (patch)
treee8d312aaa52c4dfd8abf28d237f9d23ccdd9b92d /crypto/rsa/rsa_gen.c
parent0b6b011fc6d6f5034dfc197750057a8b1977f526 (diff)
Fix an endless loop in rsa_builtin_keygen.
And add a test case. Reviewed-by: Richard Levitte <levitte@openssl.org> Reviewed-by: Rich Salz <rsalz@openssl.org> (Merged from https://github.com/openssl/openssl/pull/2757) (cherry picked from commit 697958313ba48c8ebc832ab8f9f2b845fb7acfd4)
Diffstat (limited to 'crypto/rsa/rsa_gen.c')
-rw-r--r--crypto/rsa/rsa_gen.c23
1 files changed, 11 insertions, 12 deletions
diff --git a/crypto/rsa/rsa_gen.c b/crypto/rsa/rsa_gen.c
index 0d1d56bfe7..4ced965519 100644
--- a/crypto/rsa/rsa_gen.c
+++ b/crypto/rsa/rsa_gen.c
@@ -43,6 +43,16 @@ static int rsa_builtin_keygen(RSA *rsa, int bits, BIGNUM *e_value,
int bitsp, bitsq, ok = -1, n = 0;
BN_CTX *ctx = NULL;
+ /*
+ * When generating ridiculously small keys, we can get stuck
+ * continually regenerating the same prime values.
+ */
+ if (bits < 16) {
+ ok = 0; /* we set our own err */
+ RSAerr(RSA_F_RSA_BUILTIN_KEYGEN, RSA_R_KEY_SIZE_TOO_SMALL);
+ goto err;
+ }
+
ctx = BN_CTX_new();
if (ctx == NULL)
goto err;
@@ -94,21 +104,10 @@ static int rsa_builtin_keygen(RSA *rsa, int bits, BIGNUM *e_value,
if (!BN_GENCB_call(cb, 3, 0))
goto err;
for (;;) {
- /*
- * When generating ridiculously small keys, we can get stuck
- * continually regenerating the same prime values. Check for this and
- * bail if it happens 3 times.
- */
- unsigned int degenerate = 0;
do {
if (!BN_generate_prime_ex(rsa->q, bitsq, 0, NULL, NULL, cb))
goto err;
- } while ((BN_cmp(rsa->p, rsa->q) == 0) && (++degenerate < 3));
- if (degenerate == 3) {
- ok = 0; /* we set our own err */
- RSAerr(RSA_F_RSA_BUILTIN_KEYGEN, RSA_R_KEY_SIZE_TOO_SMALL);
- goto err;
- }
+ } while (BN_cmp(rsa->p, rsa->q) == 0);
if (!BN_sub(r2, rsa->q, BN_value_one()))
goto err;
if (!BN_gcd(r1, r2, rsa->e, ctx))