BN_rand_range() as in main branch.
authorUlf Möller <ulf@openssl.org>
Wed, 21 Feb 2001 15:54:31 +0000 (15:54 +0000)
committerUlf Möller <ulf@openssl.org>
Wed, 21 Feb 2001 15:54:31 +0000 (15:54 +0000)
crypto/dh/dh_key.c
crypto/dsa/dsa_key.c
crypto/rsa/rsa_lib.c

index 6f9426dd6fc276129100eba63f9d1abdcadb322d..a6469cad2c5b9c9b949f4e96857a08ed02941dab 100644 (file)
@@ -99,7 +99,6 @@ DH_METHOD *DH_OpenSSL(void)
 static int generate_key(DH *dh)
        {
        int ok=0;
-       unsigned int i;
        BN_CTX ctx;
        BN_MONT_CTX *mont;
        BIGNUM *pub_key=NULL,*priv_key=NULL;
@@ -108,15 +107,11 @@ static int generate_key(DH *dh)
 
        if (dh->priv_key == NULL)
                {
-               i=dh->length;
-               if (i == 0)
-                       {
-                       /* Make the number p-1 bits long */
-                       i=BN_num_bits(dh->p)-1;
-                       }
                priv_key=BN_new();
                if (priv_key == NULL) goto err;
-               if (!BN_rand(priv_key,i,0,0)) goto err;
+               do
+                       if (!BN_rand_range(priv_key, dh->p)) goto err;
+               while (BN_is_zero(priv_key));
                }
        else
                priv_key=dh->priv_key;
index af3c56d770ecb2480b996875df3b5624dd205f26..86cacfb3b9d7f0c5c60309b71da339546efdc8be 100644 (file)
@@ -68,7 +68,6 @@
 int DSA_generate_key(DSA *dsa)
        {
        int ok=0;
-       unsigned int i;
        BN_CTX *ctx=NULL;
        BIGNUM *pub_key=NULL,*priv_key=NULL;
 
@@ -81,15 +80,9 @@ int DSA_generate_key(DSA *dsa)
        else
                priv_key=dsa->priv_key;
 
-       i=BN_num_bits(dsa->q);
-       for (;;)
-               {
-               if (!BN_rand(priv_key,i,0,0))
-                       goto err;
-               if (BN_cmp(priv_key,dsa->q) >= 0)
-                       BN_sub(priv_key,priv_key,dsa->q);
-               if (!BN_is_zero(priv_key)) break;
-               }
+       do
+               if (!BN_rand_range(priv_key,dsa->q)) goto err;
+       while (BN_is_zero(priv_key));
 
        if (dsa->pub_key == NULL)
                {
index bbddd3f0f0bff74f8aa809aa67e7c135dfe96adb..b5b420da97a4cc3baa6d0274e5008f87630cd2ed 100644 (file)
@@ -272,7 +272,7 @@ int RSA_blinding_on(RSA *rsa, BN_CTX *p_ctx)
 
        BN_CTX_start(ctx);
        A = BN_CTX_get(ctx);
-       if (!BN_rand(A,BN_num_bits(rsa->n)-1,1,0)) goto err;
+       if (!BN_rand_range(A,rsa->n)) goto err;
        if ((Ai=BN_mod_inverse(NULL,A,rsa->n,ctx)) == NULL) goto err;
 
        if (!rsa->meth->bn_mod_exp(A,A,rsa->e,rsa->n,ctx,rsa->_method_mod_n))