summaryrefslogtreecommitdiffstats
path: root/crypto/dsa/dsa_gen.c
diff options
context:
space:
mode:
authorGeoff Thorpe <geoff@openssl.org>2002-12-08 05:24:31 +0000
committerGeoff Thorpe <geoff@openssl.org>2002-12-08 05:24:31 +0000
commite9224c717711eefb30038c9b37c69795dda93c9a (patch)
treecdb7a95f6ef21a6434008c494c38d530b629def0 /crypto/dsa/dsa_gen.c
parente90e7197398ce87786e92468e946d50f3c6728b7 (diff)
This is a first-cut at improving the callback mechanisms used in
key-generation and prime-checking functions. Rather than explicitly passing callback functions and caller-defined context data for the callbacks, a new structure BN_GENCB is defined that encapsulates this; a pointer to the structure is passed to all such functions instead. This wrapper structure allows the encapsulation of "old" and "new" style callbacks - "new" callbacks return a boolean result on the understanding that returning FALSE should terminate keygen/primality processing. The BN_GENCB abstraction will allow future callback modifications without needing to break binary compatibility nor change the API function prototypes. The new API functions have been given names ending in "_ex" and the old functions are implemented as wrappers to the new ones. The OPENSSL_NO_DEPRECATED symbol has been introduced so that, if defined, declaration of the older functions will be skipped. NB: Some openssl-internal code will stick with the older callbacks for now, so appropriate "#undef" logic will be put in place - this is in case the user is *building* openssl (rather than *including* its headers) with this symbol defined. There is another change in the new _ex functions; the key-generation functions do not return key structures but operate on structures passed by the caller, the return value is a boolean. This will allow for a smoother transition to having key-generation as "virtual function" in the various ***_METHOD tables.
Diffstat (limited to 'crypto/dsa/dsa_gen.c')
-rw-r--r--crypto/dsa/dsa_gen.c42
1 files changed, 21 insertions, 21 deletions
diff --git a/crypto/dsa/dsa_gen.c b/crypto/dsa/dsa_gen.c
index dc9c249310..ca2c867089 100644
--- a/crypto/dsa/dsa_gen.c
+++ b/crypto/dsa/dsa_gen.c
@@ -80,11 +80,9 @@
#include <openssl/rand.h>
#include <openssl/sha.h>
-DSA *DSA_generate_parameters(int bits,
+int DSA_generate_parameters_ex(DSA *ret, 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 *counter_ret, unsigned long *h_ret, BN_GENCB *cb)
{
int ok=0;
unsigned char seed[SHA_DIGEST_LENGTH];
@@ -98,7 +96,6 @@ DSA *DSA_generate_parameters(int bits,
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;
@@ -114,7 +111,6 @@ DSA *DSA_generate_parameters(int bits,
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;
@@ -137,7 +133,8 @@ DSA *DSA_generate_parameters(int bits,
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)
{
@@ -170,7 +167,8 @@ DSA *DSA_generate_parameters(int bits,
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);
+ r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx3,
+ seed_is_random, cb);
if (r > 0)
break;
if (r != 0)
@@ -180,8 +178,8 @@ DSA *DSA_generate_parameters(int bits,
/* 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;
@@ -192,8 +190,8 @@ DSA *DSA_generate_parameters(int bits,
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);
@@ -231,7 +229,8 @@ DSA *DSA_generate_parameters(int bits,
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)
@@ -247,7 +246,8 @@ DSA *DSA_generate_parameters(int bits,
}
}
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 */
@@ -266,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);
@@ -291,6 +291,6 @@ err:
}
if (ctx3 != NULL) BN_CTX_free(ctx3);
if (mont != NULL) BN_MONT_CTX_free(mont);
- return(ok?ret:NULL);
+ return ok;
}
#endif