Update perl asm scripts include paths for perlasm.
[openssl.git] / crypto / dsa / dsa_key.c
index 5aef2d5fcffb6541a62f279c88d7ba1a2b43aab6..c4aa86bc6dce4eb7f2e034fe8486444acd410f81 100644 (file)
  * [including the GNU Public Licence.]
  */
 
-#ifndef NO_SHA
 #include <stdio.h>
 #include <time.h>
 #include "cryptlib.h"
-#include <openssl/sha.h>
+#ifndef OPENSSL_NO_SHA
 #include <openssl/bn.h>
 #include <openssl/dsa.h>
 #include <openssl/rand.h>
 
+static int dsa_builtin_keygen(DSA *dsa);
+
 int DSA_generate_key(DSA *dsa)
+       {
+       if(dsa->meth->dsa_keygen)
+               return dsa->meth->dsa_keygen(dsa);
+       return dsa_builtin_keygen(dsa);
+       }
+
+static int dsa_builtin_keygen(DSA *dsa)
        {
        int ok=0;
-       unsigned int i;
        BN_CTX *ctx=NULL;
        BIGNUM *pub_key=NULL,*priv_key=NULL;
 
@@ -81,15 +88,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,1,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)
                {
@@ -97,8 +98,22 @@ int DSA_generate_key(DSA *dsa)
                }
        else
                pub_key=dsa->pub_key;
+       
+       {
+               BIGNUM local_prk;
+               BIGNUM *prk;
 
-       if (!BN_mod_exp(pub_key,dsa->g,priv_key,dsa->p,ctx)) goto err;
+               if ((dsa->flags & DSA_FLAG_NO_EXP_CONSTTIME) == 0)
+                       {
+                       BN_init(&local_prk);
+                       prk = &local_prk;
+                       BN_with_flags(prk, priv_key, BN_FLG_CONSTTIME);
+                       }
+               else
+                       prk = priv_key;
+
+               if (!BN_mod_exp(pub_key,dsa->g,prk,dsa->p,ctx)) goto err;
+       }
 
        dsa->priv_key=priv_key;
        dsa->pub_key=pub_key;