Use BN_rand_range().
[openssl.git] / crypto / dsa / dsa_key.c
index ab7f38fc7c6c0d211af12600bb2989743e02360c..bd5d0ce5673c1c514805798427e62d4ba58b0d2b 100644 (file)
@@ -56,7 +56,7 @@
  * [including the GNU Public Licence.]
  */
 
-#ifndef NO_SHA
+#ifndef OPENSSL_NO_SHA
 #include <stdio.h>
 #include <time.h>
 #include "cryptlib.h"
@@ -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,14 +80,9 @@ int DSA_generate_key(DSA *dsa)
        else
                priv_key=dsa->priv_key;
 
-       i=BN_num_bits(dsa->q);
-       for (;;)
-               {
-               BN_rand(priv_key,i,1,0);
-               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)
                {