Major memory management overhaul. Added arena support. Use the secure
[u/mdw/catacomb] / bbs-gen.c
CommitLineData
2c52abe6 1/* -*-c-*-
2 *
02b1cf93 3 * $Id: bbs-gen.c,v 1.4 2000/06/17 10:43:57 mdw Exp $
2c52abe6 4 *
5 * Generate Blum integers
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
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.
18 *
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.
23 *
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,
27 * MA 02111-1307, USA.
28 */
29
30/*----- Revision history --------------------------------------------------*
31 *
32 * $Log: bbs-gen.c,v $
02b1cf93 33 * Revision 1.4 2000/06/17 10:43:57 mdw
34 * Move GCD filter to separate file. Handle failures from pgen_jump.
35 *
052b36d0 36 * Revision 1.3 2000/02/12 18:21:02 mdw
37 * Overhaul of key management (again).
38 *
b04a7659 39 * Revision 1.2 1999/12/22 15:52:28 mdw
40 * Reworking for new prime-search system.
41 *
2c52abe6 42 * Revision 1.1 1999/12/10 23:14:59 mdw
43 * Blum-Blum-Shub generator, and Blum-Goldwasser encryption.
44 *
45 */
46
47/*----- Header files ------------------------------------------------------*/
48
49#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
52
53#include "bbs.h"
2c52abe6 54#include "mp.h"
55#include "mprand.h"
56#include "pgen.h"
052b36d0 57#include "strongprime.h"
b04a7659 58
2c52abe6 59/*----- Main code ---------------------------------------------------------*/
60
61/* --- @bbs_gen@ --- *
62 *
b04a7659 63 * Arguments: @bbs_param *bp@ = pointer to parameter block
052b36d0 64 * @unsigned nbits@ = number of bits in the modulus
65 * @grand *r@ = pointer to random number source
66 * @unsigned n@ = number of attempts to make
b04a7659 67 * @pgen_proc *event@ = event handler function
68 * @void *ectx@ = argument for event handler
2c52abe6 69 *
b04a7659 70 * Returns: If it worked OK, @PGEN_DONE@, otherwise @PGEN_ABORT@.
2c52abe6 71 *
72 * Use: Finds two prime numbers %$p'$% and %$q'$% such that both are
73 * congruent to %$3 \bmod 4$%, and $(p - 1)/2$% and
74 * %$(q - 1)/2$% have no common factors. The product %$n = pq$%
75 * is eminently suitable for use as a modulus in a Blum-Blum-
76 * Shub pseudorandom bit generator.
77 */
78
052b36d0 79int bbs_gen(bbs_param *bp, unsigned nbits, grand *r, unsigned n,
b04a7659 80 pgen_proc *event, void *ectx)
2c52abe6 81{
052b36d0 82 rabin rb;
83 pgen_safejumpctx j;
02b1cf93 84 pgen_gcdstepctx g;
052b36d0 85 unsigned nb = nbits/2;
86 mp *x = MP_NEW;
2c52abe6 87
b04a7659 88 /* --- Generate @p@ --- */
2c52abe6 89
02b1cf93 90again:
052b36d0 91 if ((x = strongprime_setup("p", x, &j.jq, nb, r, n, event, ectx)) == 0)
92 goto fail_x;
93 bp->p = pgen("p", MP_NEW, x, event, ectx, n, pgen_safejump, &j,
94 rabin_iters(nb), pgen_test, &rb);
95 pfilt_destroy(&j.jq);
02b1cf93 96 if (!bp->p) {
97 if (n)
98 goto fail_p;
99 goto again;
100 }
2c52abe6 101
b04a7659 102 /* --- Generate @q@ --- */
2c52abe6 103
052b36d0 104 nb = nbits - nb;
105 if ((x = strongprime_setup("q", x, &g.jp, nb, r, n, event, ectx)) == 0)
106 goto fail_q;
02b1cf93 107 if ((x->v[0] & 3) != 3)
108 x = mp_add(x, x, g.jp.m);
109 pfilt_muladd(&g.jp, &g.jp, 2, 0);
b04a7659 110 g.r = mp_lsr(MP_NEW, bp->p, 1);
02b1cf93 111 g.g = MP_NEW;
112 g.max = MP_ONE;
113 bp->q = pgen("q", MP_NEW, x, event, ectx, n, pgen_gcdstep, &g,
052b36d0 114 rabin_iters(nb), pgen_test, &rb);
115 pfilt_destroy(&g.jp);
116 mp_drop(g.r);
02b1cf93 117 mp_drop(g.g);
118 if (!bp->q) {
119 if (n)
120 goto fail_q;
121 mp_drop(bp->p);
122 goto again;
123 }
2c52abe6 124
b04a7659 125 /* --- Compute @n@ --- */
2c52abe6 126
b04a7659 127 bp->n = mp_mul(MP_NEW, bp->p, bp->q);
052b36d0 128 mp_drop(x);
b04a7659 129 return (PGEN_DONE);
2c52abe6 130
b04a7659 131 /* --- Tidy up if things went wrong --- */
2c52abe6 132
b04a7659 133fail_q:
b04a7659 134 mp_drop(bp->p);
135fail_p:
052b36d0 136 mp_drop(x);
137fail_x:
b04a7659 138 return (PGEN_ABORT);
2c52abe6 139}
140
141/*----- That's all, folks -------------------------------------------------*/