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