3 * $Id: bbs-gen.c,v 1.2 1999/12/22 15:52:28 mdw Exp $
5 * Generate Blum integers
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.2 1999/12/22 15:52:28 mdw
34 * Reworking for new prime-search system.
36 * Revision 1.1 1999/12/10 23:14:59 mdw
37 * Blum-Blum-Shub generator, and Blum-Goldwasser encryption.
41 /*----- Header files ------------------------------------------------------*/
52 /*----- Data structures ---------------------------------------------------*/
54 typedef struct gcdctx
{
60 /*----- Custom stepper ----------------------------------------------------*/
62 static int gcdstep(int rq
, pgen_event
*ev
, void *p
)
70 mp
*p
= ev
->m
= mp_split(ev
->m
);
72 g
->q
= mp_lsr(MP_NEW
, p
, 1);
73 rc
= pfilt_create(&g
->p
, p
);
76 g
->q
= mp_add(g
->q
, g
->q
, MP_FOUR
);
77 rc
= pfilt_step(&g
->p
, 4);
85 if (rc
!= PGEN_FAIL
) {
86 mp_gcd(&z
, 0, 0, g
->r
, g
->q
);
87 if (MP_CMP(z
, !=, MP_ONE
))
92 g
->q
= mp_add(g
->q
, g
->q
, MP_FOUR
);
93 rc
= pfilt_step(&g
->p
, 4);
98 ev
->m
= MP_COPY(g
->p
.m
);
102 /*----- Main code ---------------------------------------------------------*/
104 /* --- @bbs_gen@ --- *
106 * Arguments: @bbs_param *bp@ = pointer to parameter block
107 * @mp *p, *q@ = initial numbers to search from
108 * @size_t n@ = number of attempts to make
109 * @pgen_proc *event@ = event handler function
110 * @void *ectx@ = argument for event handler
112 * Returns: If it worked OK, @PGEN_DONE@, otherwise @PGEN_ABORT@.
114 * Use: Finds two prime numbers %$p'$% and %$q'$% such that both are
115 * congruent to %$3 \bmod 4$%, and $(p - 1)/2$% and
116 * %$(q - 1)/2$% have no common factors. The product %$n = pq$%
117 * is eminently suitable for use as a modulus in a Blum-Blum-
118 * Shub pseudorandom bit generator.
121 int bbs_gen(bbs_param
*bp
, mp
*p
, mp
*q
, size_t n
,
122 pgen_proc
*event
, void *ectx
)
128 /* --- Generate @p@ --- */
130 if ((bp
->p
= pgen("p", MP_NEW
, p
, event
, ectx
, n
, pgen_safestep
, &c
,
131 rabin_iters(mp_bits(p
)), pgen_test
, &r
)) == 0)
134 /* --- Generate @q@ --- */
136 g
.r
= mp_lsr(MP_NEW
, bp
->p
, 1);
137 if ((bp
->q
= pgen("q", MP_NEW
, q
, event
, ectx
, n
, gcdstep
, &g
,
138 rabin_iters(mp_bits(q
)), pgen_test
, &r
)) == 0)
141 /* --- Compute @n@ --- */
143 bp
->n
= mp_mul(MP_NEW
, bp
->p
, bp
->q
);
147 /* --- Tidy up if things went wrong --- */
156 /*----- That's all, folks -------------------------------------------------*/