The new BN_CTX code makes this sort of abuse unnecessary.
[openssl.git] / crypto / dsa / dsa_gen.c
index 65602dda77a399d314a8b86bde68fb392304c01f..878dc67ad2182b8acf03a9e474f852bbdb418acb 100644 (file)
 #ifdef GENUINE_DSA
 /* Parameter generation follows the original release of FIPS PUB 186,
  * Appendix 2.2 (i.e. use SHA as defined in FIPS PUB 180) */
-#define HASH    SHA
+#define HASH    EVP_sha()
 #else
 /* Parameter generation follows the updated Appendix 2.2 for FIPS PUB 186,
  * also Appendix 2.2 of FIPS PUB 186-1 (i.e. use SHA as defined in
  * FIPS PUB 180-1) */
-#define HASH    SHA1
+#define HASH    EVP_sha1()
 #endif 
 
-#ifndef NO_SHA
+#include <openssl/opensslconf.h> /* To see if OPENSSL_NO_SHA is defined */
+
+#ifndef OPENSSL_NO_SHA
 
 #include <stdio.h>
 #include <time.h>
 #include "cryptlib.h"
-#include <openssl/sha.h>
+#include <openssl/evp.h>
 #include <openssl/bn.h>
 #include <openssl/dsa.h>
 #include <openssl/rand.h>
+#include <openssl/sha.h>
+
+static int dsa_builtin_paramgen(DSA *ret, int bits,
+               unsigned char *seed_in, int seed_len,
+               int *counter_ret, unsigned long *h_ret, BN_GENCB *cb);
+
+int DSA_generate_parameters_ex(DSA *ret, int bits,
+               unsigned char *seed_in, int seed_len,
+               int *counter_ret, unsigned long *h_ret, BN_GENCB *cb)
+       {
+       if(ret->meth->dsa_paramgen)
+               return ret->meth->dsa_paramgen(ret, bits, seed_in, seed_len,
+                               counter_ret, h_ret, cb);
+       return dsa_builtin_paramgen(ret, bits, seed_in, seed_len,
+                       counter_ret, h_ret, cb);
+       }
 
-DSA *DSA_generate_parameters(int bits, unsigned char *seed_in, int seed_len,
-               int *counter_ret, unsigned long *h_ret,
-               void (*callback)(int, int, void *),
-               void *cb_arg)
+static int dsa_builtin_paramgen(DSA *ret, int bits,
+               unsigned char *seed_in, int seed_len,
+               int *counter_ret, unsigned long *h_ret, BN_GENCB *cb)
        {
        int ok=0;
        unsigned char seed[SHA_DIGEST_LENGTH];
@@ -94,9 +111,8 @@ DSA *DSA_generate_parameters(int bits, unsigned char *seed_in, int seed_len,
        int k,n=0,i,b,m=0;
        int counter=0;
        int r=0;
-       BN_CTX *ctx=NULL,*ctx2=NULL,*ctx3=NULL;
+       BN_CTX *ctx=NULL;
        unsigned int h=2;
-       DSA *ret=NULL;
 
        if (bits < 512) bits=512;
        bits=(bits+63)/64*64;
@@ -110,20 +126,18 @@ DSA *DSA_generate_parameters(int bits, unsigned char *seed_in, int seed_len,
                memcpy(seed,seed_in,seed_len);
 
        if ((ctx=BN_CTX_new()) == NULL) goto err;
-       if ((ctx2=BN_CTX_new()) == NULL) goto err;
-       if ((ctx3=BN_CTX_new()) == NULL) goto err;
-       if ((ret=DSA_new()) == NULL) goto err;
 
        if ((mont=BN_MONT_CTX_new()) == NULL) goto err;
 
-       r0= &(ctx2->bn[0]);
-       g= &(ctx2->bn[1]);
-       W= &(ctx2->bn[2]);
-       q= &(ctx2->bn[3]);
-       X= &(ctx2->bn[4]);
-       c= &(ctx2->bn[5]);
-       p= &(ctx2->bn[6]);
-       test= &(ctx2->bn[7]);
+       BN_CTX_start(ctx);
+       r0 = BN_CTX_get(ctx);
+       g = BN_CTX_get(ctx);
+       W = BN_CTX_get(ctx);
+       q = BN_CTX_get(ctx);
+       X = BN_CTX_get(ctx);
+       c = BN_CTX_get(ctx);
+       p = BN_CTX_get(ctx);
+       test = BN_CTX_get(ctx);
 
        BN_lshift(test,BN_value_one(),bits-1);
 
@@ -134,7 +148,8 @@ DSA *DSA_generate_parameters(int bits, unsigned char *seed_in, int seed_len,
                        int seed_is_random;
 
                        /* step 1 */
-                       if (callback != NULL) callback(0,m++,cb_arg);
+                       if(!BN_GENCB_call(cb, 0, m++))
+                               goto err;
 
                        if (!seed_len)
                                {
@@ -156,8 +171,8 @@ DSA *DSA_generate_parameters(int bits, unsigned char *seed_in, int seed_len,
                                }
 
                        /* step 2 */
-                       HASH(seed,SHA_DIGEST_LENGTH,md);
-                       HASH(buf,SHA_DIGEST_LENGTH,buf2);
+                       EVP_Digest(seed,SHA_DIGEST_LENGTH,md,NULL,HASH, NULL);
+                       EVP_Digest(buf,SHA_DIGEST_LENGTH,buf2,NULL,HASH, NULL);
                        for (i=0; i<SHA_DIGEST_LENGTH; i++)
                                md[i]^=buf2[i];
 
@@ -167,9 +182,8 @@ DSA *DSA_generate_parameters(int bits, unsigned char *seed_in, int seed_len,
                        if (!BN_bin2bn(md,SHA_DIGEST_LENGTH,q)) goto err;
 
                        /* step 4 */
-                       r = BN_is_prime_fasttest(q, DSS_prime_checks, callback, ctx3, cb_arg, seed_is_random);
-                       if (ctx3->tos)
-                               goto err;
+                       r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx,
+                                       seed_is_random, cb);
                        if (r > 0)
                                break;
                        if (r != 0)
@@ -179,8 +193,8 @@ DSA *DSA_generate_parameters(int bits, unsigned char *seed_in, int seed_len,
                        /* step 5 */
                        }
 
-               if (callback != NULL) callback(2,0,cb_arg);
-               if (callback != NULL) callback(3,0,cb_arg);
+               if(!BN_GENCB_call(cb, 2, 0)) goto err;
+               if(!BN_GENCB_call(cb, 3, 0)) goto err;
 
                /* step 6 */
                counter=0;
@@ -191,8 +205,8 @@ DSA *DSA_generate_parameters(int bits, unsigned char *seed_in, int seed_len,
 
                for (;;)
                        {
-                       if (callback != NULL && counter != 0)
-                               callback(0,counter,cb_arg);
+                       if ((counter != 0) && !BN_GENCB_call(cb, 0, counter))
+                               goto err;
 
                        /* step 7 */
                        BN_zero(W);
@@ -206,7 +220,7 @@ DSA *DSA_generate_parameters(int bits, unsigned char *seed_in, int seed_len,
                                        if (buf[i] != 0) break;
                                        }
 
-                               HASH(buf,SHA_DIGEST_LENGTH,md);
+                               EVP_Digest(buf,SHA_DIGEST_LENGTH,md,NULL,HASH, NULL);
 
                                /* step 8 */
                                if (!BN_bin2bn(md,SHA_DIGEST_LENGTH,r0))
@@ -230,7 +244,8 @@ DSA *DSA_generate_parameters(int bits, unsigned char *seed_in, int seed_len,
                        if (BN_cmp(p,test) >= 0)
                                {
                                /* step 11 */
-                               r = BN_is_prime_fasttest(p, DSS_prime_checks, callback, ctx3, cb_arg, 1);
+                               r = BN_is_prime_fasttest_ex(p, DSS_prime_checks,
+                                               ctx, 1, cb);
                                if (r > 0)
                                                goto end; /* found it */
                                if (r != 0)
@@ -246,7 +261,8 @@ DSA *DSA_generate_parameters(int bits, unsigned char *seed_in, int seed_len,
                        }
                }
 end:
-       if (callback != NULL) callback(2,1,cb_arg);
+       if(!BN_GENCB_call(cb, 2, 1))
+               goto err;
 
        /* We now need to generate g */
        /* Set r0=(p-1)/q */
@@ -265,16 +281,16 @@ end:
                h++;
                }
 
-       if (callback != NULL) callback(3,1,cb_arg);
+       if(!BN_GENCB_call(cb, 3, 1))
+               goto err;
 
        ok=1;
 err:
-       if (!ok)
-               {
-               if (ret != NULL) DSA_free(ret);
-               }
-       else
+       if (ok)
                {
+               if(ret->p) BN_free(ret->p);
+               if(ret->q) BN_free(ret->q);
+               if(ret->g) BN_free(ret->g);
                ret->p=BN_dup(p);
                ret->q=BN_dup(q);
                ret->g=BN_dup(g);
@@ -282,10 +298,12 @@ err:
                if (counter_ret != NULL) *counter_ret=counter;
                if (h_ret != NULL) *h_ret=h;
                }
-       if (ctx != NULL) BN_CTX_free(ctx);
-       if (ctx2 != NULL) BN_CTX_free(ctx2);
-       if (ctx3 != NULL) BN_CTX_free(ctx3);
+       if(ctx)
+               {
+               BN_CTX_end(ctx);
+               BN_CTX_free(ctx);
+               }
        if (mont != NULL) BN_MONT_CTX_free(mont);
-       return(ok?ret:NULL);
+       return ok;
        }
 #endif