/* -*-c-*-
*
- * $Id: bbs-gen.c,v 1.3 2000/02/12 18:21:02 mdw Exp $
+ * $Id: bbs-gen.c,v 1.6 2004/04/08 01:36:15 mdw Exp $
*
* Generate Blum integers
*
* (c) 1999 Straylight/Edgeware
*/
-/*----- Licensing notice --------------------------------------------------*
+/*----- Licensing notice --------------------------------------------------*
*
* This file is part of Catacomb.
*
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
- *
+ *
* Catacomb is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
- *
+ *
* You should have received a copy of the GNU Library General Public
* License along with Catacomb; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
* MA 02111-1307, USA.
*/
-/*----- Revision history --------------------------------------------------*
- *
- * $Log: bbs-gen.c,v $
- * Revision 1.3 2000/02/12 18:21:02 mdw
- * Overhaul of key management (again).
- *
- * Revision 1.2 1999/12/22 15:52:28 mdw
- * Reworking for new prime-search system.
- *
- * Revision 1.1 1999/12/10 23:14:59 mdw
- * Blum-Blum-Shub generator, and Blum-Goldwasser encryption.
- *
- */
-
/*----- Header files ------------------------------------------------------*/
#include <stdio.h>
#include "pgen.h"
#include "strongprime.h"
-/*----- Data structures ---------------------------------------------------*/
-
-typedef struct gcdctx {
- mp *q, *jq;
- pfilt p, jp;
- mp *r;
-} gcdctx;
-
-/*----- Custom stepper ----------------------------------------------------*/
-
-static int gcdstep(int rq, pgen_event *ev, void *p)
-{
- gcdctx *g = p;
- int rc = PGEN_ABORT;
- mp *z = MP_NEW;
-
- switch (rq) {
-
- /* --- Set everything up --- */
-
- case PGEN_BEGIN: {
- mp *p = ev->m;
- if ((p->v[0] & 3) != 3)
- p = mp_add(p, p, g->jp.m);
- rc = pfilt_create(&g->p, p);
- g->q = mp_lsr(MP_NEW, p, 1);
- g->jq = MP_COPY(g->jp.m);
- pfilt_muladd(&g->jp, &g->jp, 2, 0);
- g->jq = mp_lsr(MP_NEW, p, 1);
- mp_drop(p);
- } break;
-
- /* --- Grind through another iteration --- */
-
- case PGEN_TRY:
- mp_drop(ev->m);
- rc = pfilt_jump(&g->p, &g->jp);
- g->q = mp_add(g->q, g->q, g->jq);
- break;
-
- /* --- Finished --- */
-
- case PGEN_DONE:
- pfilt_destroy(&g->p);
- mp_drop(g->q);
- mp_drop(g->jq);
- return (PGEN_DONE);
- }
-
- /* --- Step on until everything is OK --- */
-
- for (;;) {
- if (rc != PGEN_FAIL) {
- mp_gcd(&z, 0, 0, g->r, g->q);
- if (MP_CMP(z, !=, MP_ONE))
- rc = PGEN_FAIL;
- }
- if (rc != PGEN_FAIL)
- break;
- rc = pfilt_jump(&g->p, &g->jp);
- g->q = mp_add(g->q, g->q, g->jq);
- }
-
- mp_drop(z);
- ev->m = MP_COPY(g->p.m);
- return (rc);
-}
-
/*----- Main code ---------------------------------------------------------*/
/* --- @bbs_gen@ --- *
*
- * Arguments: @bbs_param *bp@ = pointer to parameter block
+ * Arguments: @bbs_priv *bp@ = pointer to parameter block
* @unsigned nbits@ = number of bits in the modulus
* @grand *r@ = pointer to random number source
* @unsigned n@ = number of attempts to make
* Returns: If it worked OK, @PGEN_DONE@, otherwise @PGEN_ABORT@.
*
* Use: Finds two prime numbers %$p'$% and %$q'$% such that both are
- * congruent to %$3 \bmod 4$%, and $(p - 1)/2$% and
+ * congruent to %$3 \bmod 4$%, and $(p - 1)/2$% and
* %$(q - 1)/2$% have no common factors. The product %$n = pq$%
* is eminently suitable for use as a modulus in a Blum-Blum-
* Shub pseudorandom bit generator.
*/
-int bbs_gen(bbs_param *bp, unsigned nbits, grand *r, unsigned n,
+int bbs_gen(bbs_priv *bp, unsigned nbits, grand *r, unsigned n,
pgen_proc *event, void *ectx)
{
rabin rb;
- pgen_safejumpctx j;
- gcdctx g;
+ pfilt jp;
+ pgen_jumpctx j;
+ pgen_gcdstepctx g;
unsigned nb = nbits/2;
mp *x = MP_NEW;
/* --- Generate @p@ --- */
- if ((x = strongprime_setup("p", x, &j.jq, nb, r, n, event, ectx)) == 0)
+again:
+ if ((x = strongprime_setup("p", x, &jp, nb, r, n, event, ectx)) == 0)
goto fail_x;
- bp->p = pgen("p", MP_NEW, x, event, ectx, n, pgen_safejump, &j,
+ j.j = &jp;
+ bp->p = pgen("p", MP_NEW, x, event, ectx, n, pgen_jump, &j,
rabin_iters(nb), pgen_test, &rb);
- pfilt_destroy(&j.jq);
- if (!bp->p)
- goto fail_p;
+ pfilt_destroy(&jp);
+ if (!bp->p) {
+ if (n)
+ goto fail_p;
+ goto again;
+ }
/* --- Generate @q@ --- */
nb = nbits - nb;
if ((x = strongprime_setup("q", x, &g.jp, nb, r, n, event, ectx)) == 0)
goto fail_q;
+ if ((x->v[0] & 3) != 3)
+ x = mp_add(x, x, g.jp.m);
+ pfilt_muladd(&g.jp, &g.jp, 2, 0);
g.r = mp_lsr(MP_NEW, bp->p, 1);
- bp->q = pgen("q", MP_NEW, x, event, ectx, n, gcdstep, &g,
+ g.g = MP_NEW;
+ g.max = MP_ONE;
+ bp->q = pgen("q", MP_NEW, x, event, ectx, n, pgen_gcdstep, &g,
rabin_iters(nb), pgen_test, &rb);
pfilt_destroy(&g.jp);
mp_drop(g.r);
- if (!bp->q)
- goto fail_q;
+ mp_drop(g.g);
+ if (!bp->q) {
+ if (n)
+ goto fail_q;
+ mp_drop(bp->p);
+ goto again;
+ }
/* --- Compute @n@ --- */