2c52abe6 |
1 | /* -*-c-*- |
2 | * |
b04a7659 |
3 | * $Id: bbs-gen.c,v 1.2 1999/12/22 15:52:28 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 $ |
b04a7659 |
33 | * Revision 1.2 1999/12/22 15:52:28 mdw |
34 | * Reworking for new prime-search system. |
35 | * |
2c52abe6 |
36 | * Revision 1.1 1999/12/10 23:14:59 mdw |
37 | * Blum-Blum-Shub generator, and Blum-Goldwasser encryption. |
38 | * |
39 | */ |
40 | |
41 | /*----- Header files ------------------------------------------------------*/ |
42 | |
43 | #include <stdio.h> |
44 | #include <stdlib.h> |
45 | #include <string.h> |
46 | |
47 | #include "bbs.h" |
2c52abe6 |
48 | #include "mp.h" |
49 | #include "mprand.h" |
50 | #include "pgen.h" |
b04a7659 |
51 | |
52 | /*----- Data structures ---------------------------------------------------*/ |
53 | |
54 | typedef struct gcdctx { |
55 | mp *q; |
56 | mp *r; |
57 | pfilt p; |
58 | } gcdctx; |
59 | |
60 | /*----- Custom stepper ----------------------------------------------------*/ |
61 | |
62 | static int gcdstep(int rq, pgen_event *ev, void *p) |
63 | { |
64 | gcdctx *g = p; |
65 | int rc = PGEN_ABORT; |
66 | mp *z = MP_NEW; |
67 | |
68 | switch (rq) { |
69 | case PGEN_BEGIN: { |
70 | mp *p = ev->m = mp_split(ev->m); |
71 | p->v[0] |= 3; |
72 | g->q = mp_lsr(MP_NEW, p, 1); |
73 | rc = pfilt_create(&g->p, p); |
74 | } break; |
75 | case PGEN_TRY: |
76 | g->q = mp_add(g->q, g->q, MP_FOUR); |
77 | rc = pfilt_step(&g->p, 4); |
78 | break; |
79 | case PGEN_DONE: |
80 | pfilt_destroy(&g->p); |
81 | mp_drop(g->q); |
82 | return (PGEN_DONE); |
83 | } |
84 | for (;;) { |
85 | if (rc != PGEN_FAIL) { |
86 | mp_gcd(&z, 0, 0, g->r, g->q); |
87 | if (MP_CMP(z, !=, MP_ONE)) |
88 | rc = PGEN_FAIL; |
89 | } |
90 | if (rc != PGEN_FAIL) |
91 | break; |
92 | g->q = mp_add(g->q, g->q, MP_FOUR); |
93 | rc = pfilt_step(&g->p, 4); |
94 | } |
95 | |
96 | mp_drop(z); |
97 | mp_drop(ev->m); |
98 | ev->m = MP_COPY(g->p.m); |
99 | return (rc); |
100 | } |
2c52abe6 |
101 | |
102 | /*----- Main code ---------------------------------------------------------*/ |
103 | |
104 | /* --- @bbs_gen@ --- * |
105 | * |
b04a7659 |
106 | * Arguments: @bbs_param *bp@ = pointer to parameter block |
2c52abe6 |
107 | * @mp *p, *q@ = initial numbers to search from |
108 | * @size_t n@ = number of attempts to make |
b04a7659 |
109 | * @pgen_proc *event@ = event handler function |
110 | * @void *ectx@ = argument for event handler |
2c52abe6 |
111 | * |
b04a7659 |
112 | * Returns: If it worked OK, @PGEN_DONE@, otherwise @PGEN_ABORT@. |
2c52abe6 |
113 | * |
114 | * Use: Finds two prime numbers %$p'$% and %$q'$% such that both are |
115 | * congruent to %$3 \bmod 4$%, and $(p - 1)/2$% and |
116 | * %$(q - 1)/2$% have no common factors. The product %$n = pq$% |
117 | * is eminently suitable for use as a modulus in a Blum-Blum- |
118 | * Shub pseudorandom bit generator. |
119 | */ |
120 | |
b04a7659 |
121 | int bbs_gen(bbs_param *bp, mp *p, mp *q, size_t n, |
122 | pgen_proc *event, void *ectx) |
2c52abe6 |
123 | { |
b04a7659 |
124 | rabin r; |
125 | pgen_safestepctx c; |
126 | gcdctx g; |
2c52abe6 |
127 | |
b04a7659 |
128 | /* --- Generate @p@ --- */ |
2c52abe6 |
129 | |
b04a7659 |
130 | if ((bp->p = pgen("p", MP_NEW, p, event, ectx, n, pgen_safestep, &c, |
131 | rabin_iters(mp_bits(p)), pgen_test, &r)) == 0) |
132 | goto fail_p; |
2c52abe6 |
133 | |
b04a7659 |
134 | /* --- Generate @q@ --- */ |
2c52abe6 |
135 | |
b04a7659 |
136 | g.r = mp_lsr(MP_NEW, bp->p, 1); |
137 | if ((bp->q = pgen("q", MP_NEW, q, event, ectx, n, gcdstep, &g, |
138 | rabin_iters(mp_bits(q)), pgen_test, &r)) == 0) |
139 | goto fail_q; |
2c52abe6 |
140 | |
b04a7659 |
141 | /* --- Compute @n@ --- */ |
2c52abe6 |
142 | |
b04a7659 |
143 | bp->n = mp_mul(MP_NEW, bp->p, bp->q); |
144 | mp_drop(g.r); |
145 | return (PGEN_DONE); |
2c52abe6 |
146 | |
b04a7659 |
147 | /* --- Tidy up if things went wrong --- */ |
2c52abe6 |
148 | |
b04a7659 |
149 | fail_q: |
150 | mp_drop(g.r); |
151 | mp_drop(bp->p); |
152 | fail_p: |
153 | return (PGEN_ABORT); |
2c52abe6 |
154 | } |
155 | |
156 | /*----- That's all, folks -------------------------------------------------*/ |