2c52abe6 |
1 | /* -*-c-*- |
2 | * |
052b36d0 |
3 | * $Id: bbs-gen.c,v 1.3 2000/02/12 18:21:02 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 $ |
052b36d0 |
33 | * Revision 1.3 2000/02/12 18:21:02 mdw |
34 | * Overhaul of key management (again). |
35 | * |
b04a7659 |
36 | * Revision 1.2 1999/12/22 15:52:28 mdw |
37 | * Reworking for new prime-search system. |
38 | * |
2c52abe6 |
39 | * Revision 1.1 1999/12/10 23:14:59 mdw |
40 | * Blum-Blum-Shub generator, and Blum-Goldwasser encryption. |
41 | * |
42 | */ |
43 | |
44 | /*----- Header files ------------------------------------------------------*/ |
45 | |
46 | #include <stdio.h> |
47 | #include <stdlib.h> |
48 | #include <string.h> |
49 | |
50 | #include "bbs.h" |
2c52abe6 |
51 | #include "mp.h" |
52 | #include "mprand.h" |
53 | #include "pgen.h" |
052b36d0 |
54 | #include "strongprime.h" |
b04a7659 |
55 | |
56 | /*----- Data structures ---------------------------------------------------*/ |
57 | |
58 | typedef struct gcdctx { |
052b36d0 |
59 | mp *q, *jq; |
60 | pfilt p, jp; |
b04a7659 |
61 | mp *r; |
b04a7659 |
62 | } gcdctx; |
63 | |
64 | /*----- Custom stepper ----------------------------------------------------*/ |
65 | |
66 | static int gcdstep(int rq, pgen_event *ev, void *p) |
67 | { |
68 | gcdctx *g = p; |
69 | int rc = PGEN_ABORT; |
70 | mp *z = MP_NEW; |
71 | |
72 | switch (rq) { |
052b36d0 |
73 | |
74 | /* --- Set everything up --- */ |
75 | |
b04a7659 |
76 | case PGEN_BEGIN: { |
052b36d0 |
77 | mp *p = ev->m; |
78 | if ((p->v[0] & 3) != 3) |
79 | p = mp_add(p, p, g->jp.m); |
b04a7659 |
80 | rc = pfilt_create(&g->p, p); |
052b36d0 |
81 | g->q = mp_lsr(MP_NEW, p, 1); |
82 | g->jq = MP_COPY(g->jp.m); |
83 | pfilt_muladd(&g->jp, &g->jp, 2, 0); |
84 | g->jq = mp_lsr(MP_NEW, p, 1); |
85 | mp_drop(p); |
b04a7659 |
86 | } break; |
052b36d0 |
87 | |
88 | /* --- Grind through another iteration --- */ |
89 | |
b04a7659 |
90 | case PGEN_TRY: |
052b36d0 |
91 | mp_drop(ev->m); |
92 | rc = pfilt_jump(&g->p, &g->jp); |
93 | g->q = mp_add(g->q, g->q, g->jq); |
b04a7659 |
94 | break; |
052b36d0 |
95 | |
96 | /* --- Finished --- */ |
97 | |
b04a7659 |
98 | case PGEN_DONE: |
99 | pfilt_destroy(&g->p); |
100 | mp_drop(g->q); |
052b36d0 |
101 | mp_drop(g->jq); |
b04a7659 |
102 | return (PGEN_DONE); |
103 | } |
052b36d0 |
104 | |
105 | /* --- Step on until everything is OK --- */ |
106 | |
b04a7659 |
107 | for (;;) { |
108 | if (rc != PGEN_FAIL) { |
109 | mp_gcd(&z, 0, 0, g->r, g->q); |
110 | if (MP_CMP(z, !=, MP_ONE)) |
111 | rc = PGEN_FAIL; |
112 | } |
113 | if (rc != PGEN_FAIL) |
114 | break; |
052b36d0 |
115 | rc = pfilt_jump(&g->p, &g->jp); |
116 | g->q = mp_add(g->q, g->q, g->jq); |
b04a7659 |
117 | } |
118 | |
119 | mp_drop(z); |
b04a7659 |
120 | ev->m = MP_COPY(g->p.m); |
121 | return (rc); |
122 | } |
2c52abe6 |
123 | |
124 | /*----- Main code ---------------------------------------------------------*/ |
125 | |
126 | /* --- @bbs_gen@ --- * |
127 | * |
b04a7659 |
128 | * Arguments: @bbs_param *bp@ = pointer to parameter block |
052b36d0 |
129 | * @unsigned nbits@ = number of bits in the modulus |
130 | * @grand *r@ = pointer to random number source |
131 | * @unsigned n@ = number of attempts to make |
b04a7659 |
132 | * @pgen_proc *event@ = event handler function |
133 | * @void *ectx@ = argument for event handler |
2c52abe6 |
134 | * |
b04a7659 |
135 | * Returns: If it worked OK, @PGEN_DONE@, otherwise @PGEN_ABORT@. |
2c52abe6 |
136 | * |
137 | * Use: Finds two prime numbers %$p'$% and %$q'$% such that both are |
138 | * congruent to %$3 \bmod 4$%, and $(p - 1)/2$% and |
139 | * %$(q - 1)/2$% have no common factors. The product %$n = pq$% |
140 | * is eminently suitable for use as a modulus in a Blum-Blum- |
141 | * Shub pseudorandom bit generator. |
142 | */ |
143 | |
052b36d0 |
144 | int bbs_gen(bbs_param *bp, unsigned nbits, grand *r, unsigned n, |
b04a7659 |
145 | pgen_proc *event, void *ectx) |
2c52abe6 |
146 | { |
052b36d0 |
147 | rabin rb; |
148 | pgen_safejumpctx j; |
b04a7659 |
149 | gcdctx g; |
052b36d0 |
150 | unsigned nb = nbits/2; |
151 | mp *x = MP_NEW; |
2c52abe6 |
152 | |
b04a7659 |
153 | /* --- Generate @p@ --- */ |
2c52abe6 |
154 | |
052b36d0 |
155 | if ((x = strongprime_setup("p", x, &j.jq, nb, r, n, event, ectx)) == 0) |
156 | goto fail_x; |
157 | bp->p = pgen("p", MP_NEW, x, event, ectx, n, pgen_safejump, &j, |
158 | rabin_iters(nb), pgen_test, &rb); |
159 | pfilt_destroy(&j.jq); |
160 | if (!bp->p) |
b04a7659 |
161 | goto fail_p; |
2c52abe6 |
162 | |
b04a7659 |
163 | /* --- Generate @q@ --- */ |
2c52abe6 |
164 | |
052b36d0 |
165 | nb = nbits - nb; |
166 | if ((x = strongprime_setup("q", x, &g.jp, nb, r, n, event, ectx)) == 0) |
167 | goto fail_q; |
b04a7659 |
168 | g.r = mp_lsr(MP_NEW, bp->p, 1); |
052b36d0 |
169 | bp->q = pgen("q", MP_NEW, x, event, ectx, n, gcdstep, &g, |
170 | rabin_iters(nb), pgen_test, &rb); |
171 | pfilt_destroy(&g.jp); |
172 | mp_drop(g.r); |
173 | if (!bp->q) |
b04a7659 |
174 | goto fail_q; |
2c52abe6 |
175 | |
b04a7659 |
176 | /* --- Compute @n@ --- */ |
2c52abe6 |
177 | |
b04a7659 |
178 | bp->n = mp_mul(MP_NEW, bp->p, bp->q); |
052b36d0 |
179 | mp_drop(x); |
b04a7659 |
180 | return (PGEN_DONE); |
2c52abe6 |
181 | |
b04a7659 |
182 | /* --- Tidy up if things went wrong --- */ |
2c52abe6 |
183 | |
b04a7659 |
184 | fail_q: |
b04a7659 |
185 | mp_drop(bp->p); |
186 | fail_p: |
052b36d0 |
187 | mp_drop(x); |
188 | fail_x: |
b04a7659 |
189 | return (PGEN_ABORT); |
2c52abe6 |
190 | } |
191 | |
192 | /*----- That's all, folks -------------------------------------------------*/ |