Really add the EVP and all of the DES changes.
[openssl.git] / crypto / dsa / dsa_gen.c
index 710a4faa66bd996d0d559d48246607b5337644dd..0dc00191ab8d1832428e4ddad6692723a96d4c1e 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)(),
+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)
        {
        int ok=0;
@@ -93,7 +96,7 @@ 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,*ctx4=NULL;
+       BN_CTX *ctx=NULL,*ctx2=NULL,*ctx3=NULL;
        unsigned int h=2;
        DSA *ret=NULL;
 
@@ -111,19 +114,19 @@ 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 ((ctx4=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);
 
@@ -156,8 +159,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);
+                       EVP_Digest(buf,SHA_DIGEST_LENGTH,buf2,NULL,HASH);
                        for (i=0; i<SHA_DIGEST_LENGTH; i++)
                                md[i]^=buf2[i];
 
@@ -167,11 +170,11 @@ 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, ctx4, cb_arg, seed_is_random);
+                       r = BN_is_prime_fasttest(q, DSS_prime_checks, callback, ctx3, cb_arg, seed_is_random);
                        if (r > 0)
-                                       break;
+                               break;
                        if (r != 0)
-                                       goto err;
+                               goto err;
 
                        /* do a callback call */
                        /* step 5 */
@@ -204,7 +207,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);
 
                                /* step 8 */
                                if (!BN_bin2bn(md,SHA_DIGEST_LENGTH,r0))
@@ -228,7 +231,7 @@ 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, ctx4, cb_arg, 1);
+                               r = BN_is_prime_fasttest(p, DSS_prime_checks, callback, ctx3, cb_arg, 1);
                                if (r > 0)
                                                goto end; /* found it */
                                if (r != 0)
@@ -281,9 +284,12 @@ 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 (ctx4 != NULL) BN_CTX_free(ctx4);
        if (mont != NULL) BN_MONT_CTX_free(mont);
        return(ok?ret:NULL);
        }