summaryrefslogtreecommitdiff
path: root/crypto/rsa/rsa_gen.c
diff options
context:
space:
mode:
authorgeoff <geoff>2004-04-26 15:38:44 +0000
committergeoff <geoff>2004-04-26 15:38:44 +0000
commit62b5fcff3fc75879e2c30a5351c18fe474d1535f (patch)
tree0799c71ab7ecfbdebf11322536fb38e1a07ba741 /crypto/rsa/rsa_gen.c
parent4c5b46fddc04a585c7dfe85d138d8aacbc4fa4c2 (diff)
downloadopenssl-62b5fcff3fc75879e2c30a5351c18fe474d1535f.tar.gz
The problem of rsa key-generation getting stuck in a loop for (pointlessly)
small key sizes seems to result from the code continually regenerating the same prime value once the range is small enough. From my tests, this change fixes the problem by setting an escape velocity of 3 repeats for the second of the two primes. PR: 874
Diffstat (limited to 'crypto/rsa/rsa_gen.c')
-rw-r--r--crypto/rsa/rsa_gen.c17
1 files changed, 15 insertions, 2 deletions
diff --git a/crypto/rsa/rsa_gen.c b/crypto/rsa/rsa_gen.c
index 68a266179..6f4b8db2c 100644
--- a/crypto/rsa/rsa_gen.c
+++ b/crypto/rsa/rsa_gen.c
@@ -129,11 +129,24 @@ static int rsa_builtin_keygen(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb)
goto err;
for (;;)
{
- if(!BN_generate_prime_ex(rsa->q, bitsq, 0, NULL, NULL, cb))
+ /* 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_GENERATE_KEY,RSA_R_KEY_SIZE_TOO_SMALL);
goto err;
+ }
if (!BN_sub(r2,rsa->q,BN_value_one())) goto err;
if (!BN_gcd(r1,r2,rsa->e,ctx)) goto err;
- if (BN_is_one(r1) && (BN_cmp(rsa->p,rsa->q) != 0))
+ if (BN_is_one(r1))
break;
if(!BN_GENCB_call(cb, 2, n++))
goto err;