diff options
author | Simon Tatham <anakin@pobox.com> | 2020-02-29 12:10:47 +0300 |
---|---|---|
committer | Simon Tatham <anakin@pobox.com> | 2020-03-01 23:09:01 +0300 |
commit | ece788240c3fed2a77d42a7783907fb8a29640e1 (patch) | |
tree | 79b4e03d24528bdcf09a9e48acaecc8a9b08e689 /sshkeygen.h | |
parent | 08a3547bc54051e455ac450ee536612befcb9a5a (diff) |
Introduce a vtable system for prime generation.
The functions primegen() and primegen_add_progress_phase() are gone.
In their place is a small vtable system with two methods corresponding
to them, plus the usual admin of allocating and freeing contexts.
This API change is the starting point for being able to drop in
different prime generation algorithms at run time in response to user
configuration.
Diffstat (limited to 'sshkeygen.h')
-rw-r--r-- | sshkeygen.h | 44 |
1 files changed, 37 insertions, 7 deletions
diff --git a/sshkeygen.h b/sshkeygen.h index f82d0dc9..2565302a 100644 --- a/sshkeygen.h +++ b/sshkeygen.h @@ -148,18 +148,48 @@ double estimate_modexp_cost(unsigned bits); * The top-level API for generating primes. */ -/* This function consumes and frees the PrimeCandidateSource you give it */ -mp_int *primegen(PrimeCandidateSource *pcs, ProgressReceiver *prog); +typedef struct PrimeGenerationPolicy PrimeGenerationPolicy; +typedef struct PrimeGenerationContext PrimeGenerationContext; -/* Estimate how long it will take, and add a phase to a ProgressReceiver */ -ProgressPhase primegen_add_progress_phase(ProgressReceiver *prog, - unsigned bits); +struct PrimeGenerationContext { + const PrimeGenerationPolicy *vt; +}; + +struct PrimeGenerationPolicy { + ProgressPhase (*add_progress_phase)(const PrimeGenerationPolicy *policy, + ProgressReceiver *prog, unsigned bits); + PrimeGenerationContext *(*new_context)( + const PrimeGenerationPolicy *policy); + void (*free_context)(PrimeGenerationContext *ctx); + mp_int *(*generate)( + PrimeGenerationContext *ctx, + PrimeCandidateSource *pcs, ProgressReceiver *prog); + + const void *extra; /* additional data a particular impl might need */ +}; + +static inline ProgressPhase primegen_add_progress_phase( + PrimeGenerationContext *ctx, ProgressReceiver *prog, unsigned bits) +{ return ctx->vt->add_progress_phase(ctx->vt, prog, bits); } +static inline PrimeGenerationContext *primegen_new_context( + const PrimeGenerationPolicy *policy) +{ return policy->new_context(policy); } +static inline void primegen_free_context(PrimeGenerationContext *ctx) +{ ctx->vt->free_context(ctx); } +static inline mp_int *primegen_generate( + PrimeGenerationContext *ctx, + PrimeCandidateSource *pcs, ProgressReceiver *prog) +{ return ctx->vt->generate(ctx, pcs, prog); } + +extern const PrimeGenerationPolicy primegen_probabilistic; /* ---------------------------------------------------------------------- * The overall top-level API for generating entire key pairs. */ -int rsa_generate(RSAKey *key, int bits, ProgressReceiver *prog); -int dsa_generate(struct dss_key *key, int bits, ProgressReceiver *prog); +int rsa_generate(RSAKey *key, int bits, PrimeGenerationContext *pgc, + ProgressReceiver *prog); +int dsa_generate(struct dss_key *key, int bits, PrimeGenerationContext *pgc, + ProgressReceiver *prog); int ecdsa_generate(struct ecdsa_key *key, int bits); int eddsa_generate(struct eddsa_key *key, int bits); |