Simplify BN_rand_range
[openssl.git] / crypto / dsa / dsa_ossl.c
index 06575bdc98c31f24ccbba7db03eaacd33615a4e3..734681733703217a38fc1b14fbe3b195f708b345 100644 (file)
 #include <openssl/dsa.h>
 #include <openssl/rand.h>
 #include <openssl/asn1.h>
 #include <openssl/dsa.h>
 #include <openssl/rand.h>
 #include <openssl/asn1.h>
+#include <openssl/engine.h>
 
 static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa);
 
 static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa);
-int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp);
+static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp);
 static int dsa_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig,
                  DSA *dsa);
 static int dsa_init(DSA *dsa);
 static int dsa_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig,
                  DSA *dsa);
 static int dsa_init(DSA *dsa);
@@ -91,7 +92,7 @@ dsa_finish,
 NULL
 };
 
 NULL
 };
 
-DSA_METHOD *DSA_OpenSSL(void)
+const DSA_METHOD *DSA_OpenSSL(void)
 {
        return &openssl_dsa_meth;
 }
 {
        return &openssl_dsa_meth;
 }
@@ -161,7 +162,7 @@ err:
        return(ret);
        }
 
        return(ret);
        }
 
-int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp)
+static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp)
        {
        BN_CTX *ctx;
        BIGNUM k,*kinv=NULL,*r=NULL;
        {
        BN_CTX *ctx;
        BIGNUM k,*kinv=NULL,*r=NULL;
@@ -179,13 +180,9 @@ int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp)
        kinv=NULL;
 
        /* Get random k */
        kinv=NULL;
 
        /* Get random k */
-       for (;;)
-               {
-               if (!BN_rand(&k, BN_num_bits(dsa->q), 1, 0)) goto err;
-               if (BN_cmp(&k,dsa->q) >= 0)
-                       BN_sub(&k,&k,dsa->q);
-               if (!BN_is_zero(&k)) break;
-               }
+       do
+               if (!BN_rand_range(&k, dsa->q)) goto err;
+       while (BN_is_zero(&k));
 
        if ((dsa->method_mont_p == NULL) && (dsa->flags & DSA_FLAG_CACHE_MONT_P))
                {
 
        if ((dsa->method_mont_p == NULL) && (dsa->flags & DSA_FLAG_CACHE_MONT_P))
                {
@@ -195,7 +192,7 @@ int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp)
                }
 
        /* Compute r = (g^k mod p) mod q */
                }
 
        /* Compute r = (g^k mod p) mod q */
-       if (!dsa->meth->bn_mod_exp(dsa, r,dsa->g,&k,dsa->p,ctx,
+       if (!ENGINE_get_DSA(dsa->engine)->bn_mod_exp(dsa, r,dsa->g,&k,dsa->p,ctx,
                (BN_MONT_CTX *)dsa->method_mont_p)) goto err;
        if (!BN_mod(r,r,dsa->q,ctx)) goto err;
 
                (BN_MONT_CTX *)dsa->method_mont_p)) goto err;
        if (!BN_mod(r,r,dsa->q,ctx)) goto err;
 
@@ -273,7 +270,7 @@ static int dsa_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig,
        if (!BN_mod(&u1,&u1,dsa->q,ctx)) goto err;
 #else
        {
        if (!BN_mod(&u1,&u1,dsa->q,ctx)) goto err;
 #else
        {
-       if (!dsa->meth->dsa_mod_exp(dsa, &t1,dsa->g,&u1,dsa->pub_key,&u2,
+       if (!ENGINE_get_DSA(dsa->engine)->dsa_mod_exp(dsa, &t1,dsa->g,&u1,dsa->pub_key,&u2,
                                                dsa->p,ctx,mont)) goto err;
        /* BN_copy(&u1,&t1); */
        /* let u1 = u1 mod q */
                                                dsa->p,ctx,mont)) goto err;
        /* BN_copy(&u1,&t1); */
        /* let u1 = u1 mod q */
@@ -295,12 +292,14 @@ static int dsa_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig,
 
 static int dsa_init(DSA *dsa)
 {
 
 static int dsa_init(DSA *dsa)
 {
-       dsa->flags=DSA_FLAG_CACHE_MONT_P;
+       dsa->flags|=DSA_FLAG_CACHE_MONT_P;
        return(1);
 }
 
 static int dsa_finish(DSA *dsa)
 {
        return(1);
 }
 
 static int dsa_finish(DSA *dsa)
 {
+       if(dsa->method_mont_p)
+               BN_MONT_CTX_free((BN_MONT_CTX *)dsa->method_mont_p);
        return(1);
 }
 
        return(1);
 }