progs/perftest.c: Use from Glibc syscall numbers.
[catacomb] / pub / bbs-gen.c
1 /* -*-c-*-
2 *
3 * Generate Blum integers
4 *
5 * (c) 1999 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of Catacomb.
11 *
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.
16 *
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.
21 *
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,
25 * MA 02111-1307, USA.
26 */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include "bbs.h"
35 #include "mp.h"
36 #include "mprand.h"
37 #include "pgen.h"
38 #include "strongprime.h"
39
40 /*----- Main code ---------------------------------------------------------*/
41
42 /* --- @bbs_gen@ --- *
43 *
44 * Arguments: @bbs_priv *bp@ = pointer to parameter block
45 * @unsigned nbits@ = number of bits in the modulus
46 * @grand *r@ = pointer to random number source
47 * @unsigned n@ = number of attempts to make
48 * @pgen_proc *event@ = event handler function
49 * @void *ectx@ = argument for event handler
50 *
51 * Returns: If it worked OK, @PGEN_DONE@, otherwise @PGEN_ABORT@.
52 *
53 * Use: Finds two prime numbers %$p'$% and %$q'$% such that both are
54 * congruent to %$3 \bmod 4$%, and $(p - 1)/2$% and
55 * %$(q - 1)/2$% have no common factors. The product %$n = pq$%
56 * is eminently suitable for use as a modulus in a Blum-Blum-
57 * Shub pseudorandom bit generator.
58 */
59
60 int bbs_gen(bbs_priv *bp, unsigned nbits, grand *r, unsigned n,
61 pgen_proc *event, void *ectx)
62 {
63 rabin rb;
64 pfilt jp;
65 pgen_jumpctx j;
66 pgen_gcdstepctx g;
67 unsigned nb = nbits/2;
68 mp *x = MP_NEWSEC;
69
70 /* --- Generate @p@ --- */
71
72 if ((x = strongprime_setup("p", x, &jp, nb, r, n, event, ectx)) == 0)
73 goto fail_x;
74 j.j = &jp;
75 bp->p = pgen("p", MP_NEWSEC, x, event, ectx, n, pgen_jump, &j,
76 rabin_iters(nb), pgen_test, &rb);
77 pfilt_destroy(&jp);
78 if (!bp->p) goto fail_p;
79
80 /* --- Generate @q@ --- */
81
82 nb = nbits - nb;
83 if ((x = strongprime_setup("q", x, &g.jp, nb, r, n, event, ectx)) == 0)
84 goto fail_q;
85 if ((x->v[0] & 3) != 3)
86 x = mp_add(x, x, g.jp.m);
87 pfilt_muladd(&g.jp, &g.jp, 2, 0);
88 g.r = mp_lsr(MP_NEW, bp->p, 1);
89 g.g = MP_NEW;
90 g.max = MP_ONE;
91 bp->q = pgen("q", MP_NEWSEC, x, event, ectx, n, pgen_gcdstep, &g,
92 rabin_iters(nb), pgen_test, &rb);
93 pfilt_destroy(&g.jp);
94 mp_drop(g.r);
95 mp_drop(g.g);
96 if (!bp->q) goto fail_q;
97
98 /* --- Compute @n@ --- */
99
100 bp->n = mp_mul(MP_NEW, bp->p, bp->q);
101 if (mp_bits(bp->n) != nbits) goto fail_n;
102 mp_drop(x);
103 return (PGEN_DONE);
104
105 /* --- Tidy up if things went wrong --- */
106
107 fail_n:
108 mp_drop(bp->n);
109 fail_q:
110 mp_drop(bp->p);
111 fail_p:
112 mp_drop(x);
113 fail_x:
114 return (PGEN_ABORT);
115 }
116
117 /*----- That's all, folks -------------------------------------------------*/