"!Cname surname" has now become redundant ...
[oweals/openssl.git] / crypto / dsa / dsa_gen.c
index 248a70be4a3424f885d8510d1dba0982f900ad4d..ca2c8670897079e526a9029eef97acb00d575995 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
+#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>
 
-DSA *DSA_generate_parameters(int bits, unsigned char *seed_in, int seed_len,
-               int *counter_ret, unsigned long *h_ret, void (*callback)(),
-               void *cb_arg)
+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)
        {
        int ok=0;
        unsigned char seed[SHA_DIGEST_LENGTH];
@@ -95,7 +96,6 @@ DSA *DSA_generate_parameters(int bits, unsigned char *seed_in, int seed_len,
        int r=0;
        BN_CTX *ctx=NULL,*ctx2=NULL,*ctx3=NULL;
        unsigned int h=2;
-       DSA *ret=NULL;
 
        if (bits < 512) bits=512;
        bits=(bits+63)/64*64;
@@ -111,18 +111,18 @@ DSA *DSA_generate_parameters(int bits, unsigned char *seed_in, int 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(ctx2);
+       r0 = BN_CTX_get(ctx2);
+       g = BN_CTX_get(ctx2);
+       W = BN_CTX_get(ctx2);
+       q = BN_CTX_get(ctx2);
+       X = BN_CTX_get(ctx2);
+       c = BN_CTX_get(ctx2);
+       p = BN_CTX_get(ctx2);
+       test = BN_CTX_get(ctx2);
 
        BN_lshift(test,BN_value_one(),bits-1);
 
@@ -133,7 +133,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)
                                {
@@ -155,8 +156,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];
 
@@ -166,9 +167,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, ctx3,
+                                       seed_is_random, cb);
                        if (r > 0)
                                break;
                        if (r != 0)
@@ -178,8 +178,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;
@@ -190,8 +190,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);
@@ -205,7 +205,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))
@@ -229,7 +229,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,
+                                               ctx3, 1, cb);
                                if (r > 0)
                                                goto end; /* found it */
                                if (r != 0)
@@ -245,7 +246,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 */
@@ -264,16 +266,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,9 +284,13 @@ err:
                if (h_ret != NULL) *h_ret=h;
                }
        if (ctx != NULL) BN_CTX_free(ctx);
-       if (ctx2 != NULL) BN_CTX_free(ctx2);
+       if (ctx2 != NULL)
+               {
+               BN_CTX_end(ctx2);
+               BN_CTX_free(ctx2);
+               }
        if (ctx3 != NULL) BN_CTX_free(ctx3);
        if (mont != NULL) BN_MONT_CTX_free(mont);
-       return(ok?ret:NULL);
+       return ok;
        }
 #endif