X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/44c240eef3706e9b1a0451fb4d584ba7caea0cdf..2685767a6125c1620719c7de6234aedf41857b7e:/dh.h diff --git a/dh.h b/dh.h index 330c258..90fdaa3 100644 --- a/dh.h +++ b/dh.h @@ -1,8 +1,9 @@ /* -*-c-*- * - * $Id: dh.h,v 1.1 1999/11/20 22:24:44 mdw Exp $ + * $Id: dh.h,v 1.7 2001/02/03 16:08:24 mdw Exp $ + * + * Diffie-Hellman and related public-key systems * - * [Diffie-Hellman key negotiation * * (c) 1999 Straylight/Edgeware */ @@ -29,13 +30,26 @@ /*----- Revision history --------------------------------------------------* * * $Log: dh.h,v $ - * Revision 1.1 1999/11/20 22:24:44 mdw - * Add Diffie-Hellman support. + * Revision 1.7 2001/02/03 16:08:24 mdw + * Add consistency checking for public keys. + * + * Revision 1.6 2000/07/29 10:01:16 mdw + * Supply commentry for the Diffie-Hellman parameters. Add Lim-Lee + * parameter generation. + * + * Revision 1.5 2000/07/01 11:20:51 mdw + * New functions for freeing public and private keys. + * + * Revision 1.4 2000/06/17 10:52:47 mdw + * Minor changes for key fetching. + * + * Revision 1.3 2000/02/12 18:21:02 mdw + * Overhaul of key management (again). * */ -#ifndef DH_H -#define DH_H +#ifndef CATACOMB_DH_H +#define CATACOMB_DH_H #ifdef __cplusplus extern "C" { @@ -43,40 +57,146 @@ /*----- Header files ------------------------------------------------------*/ -#ifndef MP_H -# include "mp.h" +#ifndef CATACOMB_GRAND_H +# include "grand.h" #endif -/*----- Event codes -------------------------------------------------------*/ +#ifndef CATACOMB_KEY_H +# include "key.h" +#endif + +#ifndef CATACOMB_KEYCHECK_H +# include "keycheck.h" +#endif + +#ifndef CATACOMB_PGEN_H +# include "pgen.h" +#endif -enum { - DHEV_OK, +/*----- Data structures ---------------------------------------------------*/ - DHEV_FAIL, - DHEV_PASS -}; +typedef struct dh_param { + mp *p, *q; /* Prime numbers %$p$% and %$q$% */ + mp *g; /* Generates order-%$q$% subgroup */ +} dh_param; + +typedef struct dh_pub { + dh_param dp; /* Shared parameters */ + mp *y; /* Public key */ +} dh_pub; + +typedef struct dh_priv { + dh_param dp; /* Shared parameters */ + mp *x; /* Private key */ + mp *y; /* %$y \equiv g^x \pmod{p}$% */ +} dh_priv; + +/*----- Key fetching ------------------------------------------------------*/ + +extern const key_fetchdef dh_paramfetch[]; +#define DH_PARAMFETCHSZ 5 + +extern const key_fetchdef dh_pubfetch[]; +#define DH_PUBFETCHSZ 6 + +extern const key_fetchdef dh_privfetch[]; +#define DH_PRIVFETCHSZ 9 + +/* --- @dh_paramfree@, @dh_pubfree@, @dh_privfree@ --- * + * + * Arguments: @dh_param *dp@, @dh_pub *dp@, @dh_priv *dp@ = pointer to + * key block to free + * + * Returns: --- + * + * Use: Frees a Diffie-Hellman key block. + */ + +extern void dh_paramfree(dh_param */*dp*/); +extern void dh_pubfree(dh_pub */*dp*/); +extern void dh_privfree(dh_priv */*dp*/); /*----- Functions provided ------------------------------------------------*/ -/* --- @dh_prime@ --- * +/* --- @dh_gen@ --- * + * + * Arguments: @dh_param *dp@ = pointer to output parameter block + * @unsigned ql@ = length of %$q$% in bits, or zero + * @unsigned pl@ = length of %$p$% in bits + * @unsigned steps@ = number of steps to go + * @grand *r@ = random number source + * @pgen_proc *event@ = event handler function + * @void *ectx@ = argument for the event handler + * + * Returns: @PGEN_DONE@ if it worked, @PGEN_ABORT@ if it didn't. + * + * Use: Generates Diffie-Hellman parameters. + * + * The parameters are a prime %$q$%, relatively small, and a + * large prime %$p = kq + 1$% for some %$k$%, together with a + * generator %$g$% of the cyclic subgroup of order %$q$%. These + * are actually the same as the DSA parameter set, but the + * generation algorithm is different. Also, if @ql@ is zero, + * this algorithm forces %$k = 2$%, and chooses %$g = 4$%. Make + * sure you have something interesting to do if you choose this + * option. + */ + +extern int dh_gen(dh_param */*dp*/, unsigned /*ql*/, unsigned /*pl*/, + unsigned /*steps*/, grand */*r*/, pgen_proc */*event*/, + void */*ectx*/); + +/* --- @dh_limlee@ --- * + * + * Arguments: @dh_param *dp@ = pointer to output parameter block + * @unsigned ql@ = length of smallest factor of %$(p - 1)/2$% + * @unsigned pl@ = length of %$p$% in bits + * @unsigned flags@ = other generation flags + * @unsigned steps@ = number of steps to go + * @grand *r@ = random number source + * @pgen_proc *oev@ = outer event handler function + * @void *oec@ = argument for the outer event handler + * @pgen_proc *iev@ = inner event handler function + * @void *iec@ = argument for the inner event handler + * @size_t *nf@, @mp ***f@ = output array for factors + * + * Returns: @PGEN_DONE@ if it worked, @PGEN_ABORT@ if it didn't. + * + * Use: Generates Diffie-Hellman parameters based on a Lim-Lee prime. + * + * The modulus is a large prime %$p = 2 \prod q_i + 1$%, @pl@ + * bits long, where the %$q_i$% are smaller primes each at least + * @ql@ bits long. It is safe to set @nf@ and @f@ to zero if + * you're not interested in the factor values. * - * Arguments: @mp *s@ = start point for search (must be odd) - * @size_t n@ = number of concerted attempts to make, or zero - * @void (*proc)(int ev, void *p)@ = event handler - * @void *p@ = argument for event handler + * The returned %$g$% generates a subgroup of order %$q_0$% (the + * first factor, returned as @f[0]@), if the flag @DH_SUBGROUP@ + * is set on entry; otherwise %$g$% will have order + * %$(p - 1)/2$%. + */ + +#define DH_SUBGROUP 1u + +extern int dh_limlee(dh_param */*dp*/, unsigned /*ql*/, unsigned /*pl*/, + unsigned /*flags*/, unsigned /*steps*/, grand */*r*/, + pgen_proc */*oev*/, void */*oec*/, pgen_proc */*iev*/, + void */*iec*/, size_t */*nf*/, mp ***/*f*/); + +/* --- @dh_checkparam@ --- * * - * Returns: A prime number %$p$% where %$p = 2q + 1$% for prime %$q$%. + * Arguments: @keycheck *kc@ = keycheck state + * @const dh_param *dp@ = pointer to the parameter set + * @mp **v@ = optional vector of factors + * @size_t n@ = size of vector * - * Use: Finds a safe prime by sequential search from a given starting - * point. If it fails, a null pointer is returned. + * Returns: Zero if all OK, or return status from function. * - * The event handler is informed of the progress of the search. - * It may abort the search at any time by returning a nonzero - * value. + * Use: Checks a set of Diffie-Hellman parameters for consistency and + * security. */ -extern mp *dh_prime(mp */*s*/, size_t /*n*/, - int (*proc)(int /*ev*/, void */*p*/), void */*p*/); +extern int dh_checkparam(keycheck */*kc*/, const dh_param */*dp*/, + mp **/*v*/, size_t /*n*/); /*----- That's all, folks -------------------------------------------------*/