3 * Generate Diffie-Hellman parameters from Lim-Lee primes
5 * (c) 2000 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
35 /*----- Main code ---------------------------------------------------------*/
37 /* --- @dh_limlee@ --- *
39 * Arguments: @dh_param *dp@ = pointer to output parameter block
40 * @unsigned ql@ = length of smallest factor of %$(p - 1)/2$%
41 * @unsigned pl@ = length of %$p$% in bits
42 * @unsigned flags@ = other generation flags
43 * @unsigned steps@ = number of steps to go
44 * @grand *r@ = random number source
45 * @pgen_proc *oev@ = outer event handler function
46 * @void *oec@ = argument for the outer event handler
47 * @pgen_proc *iev@ = inner event handler function
48 * @void *iec@ = argument for the inner event handler
49 * @size_t *nf@, @mp ***f@ = output array for factors
51 * Returns: @PGEN_DONE@ if it worked, @PGEN_ABORT@ if it didn't.
53 * Use: Generates Diffie-Hellman parameters based on a Lim-Lee prime.
55 * The modulus is a large prime %$p = 2 \prod q_i + 1$%, @pl@
56 * bits long, where the %$q_i$% are smaller primes each at least
57 * @ql@ bits long. It is safe to set @nf@ and @f@ to zero if
58 * you're not interested in the factor values.
60 * The returned %$g$% generates a subgroup of order %$q_0$% (the
61 * first factor, returned as @f[0]@), if the flag @DH_SUBGROUP@
62 * is set on entry; otherwise %$g$% will have order
66 int dh_limlee(dh_param *dp, unsigned ql, unsigned pl,
67 unsigned flags, unsigned steps, grand *r,
68 pgen_proc *oev, void *oec, pgen_proc *iev,
69 void *iec, size_t *nf, mp ***f)
78 /* --- Generate the Lim-Lee prime --- */
80 if ((dp->p = limlee("p", MP_NEW, MP_NEW, ql, pl,
81 r, steps, oev, oec, iev, iec, &nff, &ff)) == 0)
84 /* --- Now find a primitive element --- */
86 mpmont_create(&pc.mm, dp->p);
87 pp = mp_sub(MP_NEW, dp->p, MP_ONE);
88 if (flags & DH_SUBGROUP) {
89 dp->q = mp_copy(ff[0]);
91 mp_div(&pc.exp, 0, pp, dp->q);
95 dp->q = mp_lsr(MP_NEW, dp->p, 1);
98 pc.f = xmalloc(nff * sizeof(mp *));
99 for (i = 0; i < nff; i++) {
101 mp_div(&pc.f[i], 0, pp, ff[i]);
106 dp->g = pgen("g", MP_NEW, MP_NEW, oev, oec,
107 0, prim_step, &j, 1, prim_test, &pc);
111 for (i = 0; i < pc.n; i++)
115 mpmont_destroy(&pc.mm);
117 /* --- Do something sensible with the list of primes --- */
123 for (i = 0; i < nff; i++)
128 /* --- Tidy up and return --- */
138 /*----- That's all, folks -------------------------------------------------*/