General utilities cleanup. Add signature support to catcrypt. Throw in
[u/mdw/catacomb] / bbs-gen.c
1 /* -*-c-*-
2 *
3 * $Id: bbs-gen.c,v 1.6 2004/04/08 01:36:15 mdw Exp $
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 /*----- Header files ------------------------------------------------------*/
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include "bbs.h"
37 #include "mp.h"
38 #include "mprand.h"
39 #include "pgen.h"
40 #include "strongprime.h"
41
42 /*----- Main code ---------------------------------------------------------*/
43
44 /* --- @bbs_gen@ --- *
45 *
46 * Arguments: @bbs_priv *bp@ = pointer to parameter block
47 * @unsigned nbits@ = number of bits in the modulus
48 * @grand *r@ = pointer to random number source
49 * @unsigned n@ = number of attempts to make
50 * @pgen_proc *event@ = event handler function
51 * @void *ectx@ = argument for event handler
52 *
53 * Returns: If it worked OK, @PGEN_DONE@, otherwise @PGEN_ABORT@.
54 *
55 * Use: Finds two prime numbers %$p'$% and %$q'$% such that both are
56 * congruent to %$3 \bmod 4$%, and $(p - 1)/2$% and
57 * %$(q - 1)/2$% have no common factors. The product %$n = pq$%
58 * is eminently suitable for use as a modulus in a Blum-Blum-
59 * Shub pseudorandom bit generator.
60 */
61
62 int bbs_gen(bbs_priv *bp, unsigned nbits, grand *r, unsigned n,
63 pgen_proc *event, void *ectx)
64 {
65 rabin rb;
66 pgen_safejumpctx j;
67 pgen_gcdstepctx g;
68 unsigned nb = nbits/2;
69 mp *x = MP_NEW;
70
71 /* --- Generate @p@ --- */
72
73 again:
74 if ((x = strongprime_setup("p", x, &j.jq, nb, r, n, event, ectx)) == 0)
75 goto fail_x;
76 bp->p = pgen("p", MP_NEW, x, event, ectx, n, pgen_safejump, &j,
77 rabin_iters(nb), pgen_test, &rb);
78 pfilt_destroy(&j.jq);
79 if (!bp->p) {
80 if (n)
81 goto fail_p;
82 goto again;
83 }
84
85 /* --- Generate @q@ --- */
86
87 nb = nbits - nb;
88 if ((x = strongprime_setup("q", x, &g.jp, nb, r, n, event, ectx)) == 0)
89 goto fail_q;
90 if ((x->v[0] & 3) != 3)
91 x = mp_add(x, x, g.jp.m);
92 pfilt_muladd(&g.jp, &g.jp, 2, 0);
93 g.r = mp_lsr(MP_NEW, bp->p, 1);
94 g.g = MP_NEW;
95 g.max = MP_ONE;
96 bp->q = pgen("q", MP_NEW, x, event, ectx, n, pgen_gcdstep, &g,
97 rabin_iters(nb), pgen_test, &rb);
98 pfilt_destroy(&g.jp);
99 mp_drop(g.r);
100 mp_drop(g.g);
101 if (!bp->q) {
102 if (n)
103 goto fail_q;
104 mp_drop(bp->p);
105 goto again;
106 }
107
108 /* --- Compute @n@ --- */
109
110 bp->n = mp_mul(MP_NEW, bp->p, bp->q);
111 mp_drop(x);
112 return (PGEN_DONE);
113
114 /* --- Tidy up if things went wrong --- */
115
116 fail_q:
117 mp_drop(bp->p);
118 fail_p:
119 mp_drop(x);
120 fail_x:
121 return (PGEN_ABORT);
122 }
123
124 /*----- That's all, folks -------------------------------------------------*/