Use the new @mp_odd@ function.
[u/mdw/catacomb] / bbs.h
CommitLineData
2c52abe6 1/* -*-c-*-
2 *
05cac080 3 * $Id: bbs.h,v 1.4 2000/06/17 10:45:48 mdw Exp $
2c52abe6 4 *
5 * The Blum-Blum-Shub random bit generator
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.h,v $
05cac080 33 * Revision 1.4 2000/06/17 10:45:48 mdw
34 * Minor changes for key fetching. Typesetting fixes.
35 *
052b36d0 36 * Revision 1.3 2000/02/12 18:21:02 mdw
37 * Overhaul of key management (again).
38 *
4f743df5 39 * Revision 1.2 1999/12/22 15:52:08 mdw
40 * Rename `bbs_params' to `bbs_param' for consistency.
41 *
2c52abe6 42 * Revision 1.1 1999/12/10 23:14:59 mdw
43 * Blum-Blum-Shub generator, and Blum-Goldwasser encryption.
44 *
45 */
46
47/*----- Notes on the BBS generator ----------------------------------------*
48 *
49 * The Blum-Blum-Shub generator takes the least significant bits from the
50 * sequence %$x_i = x_{i - 1}^2 \bmod n$%, where %$n = pq$% is the product of
51 * two primes %$p$% and %$q$%, each of which are congruent to %$3 \bmod 4$%.
52 * For maximum period of the generator, %$(p - 1)/2$% and %$(q - 1)/1$%
05cac080 53 * should be coprime. It is safe to use the least significant
54 * %$\log \log n$% bits of each step in the sequence -- an adversary must
55 * factor the modulus before being able to work forwards or backwards. The
56 * output of the generator cannot be distinguished from a (uniform,
57 * independent) random sequence of bits using any polynomial-time test. This
58 * is by far the strongest pseudorandom number generator provided in
59 * Catacomb, and by far the slowest too. For normal use, the standard
60 * Catacomb @rand@ generator should be more than adequate.
2c52abe6 61 */
62
63#ifndef CATACOMB_BBS_H
64#define CATACOMB_BBS_H
65
66#ifdef __cplusplus
67 extern "C" {
68#endif
69
70/*----- Header files ------------------------------------------------------*/
71
72#include <mLib/bits.h>
73
74#ifndef CATACOMB_GRAND_H
75# include "grand.h"
76#endif
77
05cac080 78#ifndef CATACOMB_KEY_H
79# include "key.h"
80#endif
81
2c52abe6 82#ifndef CATACOMB_MP_H
83# include "mp.h"
84#endif
85
86#ifndef CATACOMB_MPBARRETT_H
87# include "mpbarrett.h"
88#endif
89
4f743df5 90#ifndef CATACOMB_PGEN_H
91# include "pgen.h"
92#endif
93
2c52abe6 94/*----- Data structures ---------------------------------------------------*/
95
96/* --- Basic generator state --- */
97
98typedef struct bbs {
99 mpbarrett mb; /* Barrett reduction context */
100 mp *x; /* Current quadratic residue */
101 unsigned k; /* Number of bits from each step */
102 unsigned b; /* Number of bits in reservoir */
103 mpw r; /* Reservoir of output bits */
104} bbs;
105
106/* --- Parameters --- */
107
05cac080 108typedef struct bbs_pub {
109 mp *n;
110} bbs_pub;
111
4f743df5 112typedef struct bbs_param {
2c52abe6 113 mp *p, *q; /* Prime factors (3 mod 4) */
114 mp *n; /* Product @pq@ -- a Blum integer */
05cac080 115} bbs_param, bbs_priv;
116
117/*----- Key fetching ------------------------------------------------------*/
118
119extern const key_fetchdef bbs_pubfetch[];
120#define BBS_PUBFETCHSZ 3
121
122extern const key_fetchdef bbs_privfetch[];
123#define BBS_PRIVFETCHSZ 7
2c52abe6 124
125/*----- The basic generator -----------------------------------------------*/
126
127/* --- @bbs_create@ --- *
128 *
129 * Arguments: @bbs *b@ = pointer to BBS generator state to initialize
130 * @mp *m@ = modulus (must be a Blum integer)
131 * @mp *x@ = initial seed for generator
132 *
133 * Returns: ---
134 *
135 * Use: Initializes a BBS generator. The generator is stepped once
136 * after initialization, as for @bbs_seed@.
137 */
138
139extern void bbs_create(bbs */*b*/, mp */*m*/, mp */*x*/);
140
141/* --- @bbs_destroy@ --- *
142 *
143 * Arguments: @bbs *b@ = pointer to BBS generator state
144 *
145 * Returns: ---
146 *
147 * Use: Destroys a generator state when it's no longer wanted.
148 */
149
150extern void bbs_destroy(bbs */*b*/);
151
152/* --- @bbs_step@ --- *
153 *
154 * Arguments: @bbs *b@ = pointer to BBS generator state
155 *
156 * Returns: ---
157 *
158 * Use: Steps the generator once. This isn't too useful in client
159 * code.
160 */
161
162extern void bbs_step(bbs */*b*/);
163
164/* --- @bbs_set@ --- *
165 *
166 * Arguments: @bbs *b@ = pointer to BBS generator state
167 * @mp *x@ = new residue to set
168 *
169 * Returns: ---
170 *
171 * Use: Sets a new quadratic residue. The generator is stepped once.
172 */
173
174extern void bbs_set(bbs */*b*/, mp */*x*/);
175
176/* --- @bbs_seed@ --- *
177 *
178 * Arguments: @bbs *b@ = pointer to BBS generator state
179 * @mp *x@ = new seed to set
180 *
181 * Returns ---
182 *
183 * Use: Sets a new seed. The generator is stepped until the residue
184 * has clearly wrapped around.
185 */
186
187extern void bbs_seed(bbs */*b*/, mp */*x*/);
188
189/* --- @bbs_bits@ --- *
190 *
191 * Arguments: @bbs *b@ = pointer to BBS generator state
192 * @unsigned bits@ = number of bits wanted
193 *
194 * Returns: Bits extracted from the BBS generator.
195 *
196 * Use: Extracts a requested number of bits from the BBS generator.
197 */
198
199extern uint32 bbs_bits(bbs */*b*/, unsigned /*bits*/);
200
201/* --- @bbs_wrap@ --- *
202 *
203 * Arguments: @bbs *b@ = pointer to BBS generator state
204 *
205 * Returns: ---
206 *
207 * Use: Steps the generator if any of the reservoir bits are used.
208 * This can be used to `wrap up' after a Blum-Goldwasser
209 * encryption, for example, producing the final value to be sent
210 * along with the ciphertext.
211 *
212 * If a generator is seeded, %$b$% bits are extracted, and then
213 * @bbs_wrap@ is called, the generator will have been stepped
05cac080 214 * %$\lceil b/k \rceil$% times.
2c52abe6 215 */
216
217extern void bbs_wrap(bbs */*b*/);
218
219/*----- Large forwards and backwards jumps --------------------------------*/
220
221/* --- @bbs_ff@ --- *
222 *
223 * Arguments: @bbs *b@ = pointer to a BBS generator state
4f743df5 224 * @bbs_param *bp@ = pointer to BBS modulus factors
2c52abe6 225 * @unsigned long n@ = number of steps to make
226 *
227 * Returns: ---
228 *
229 * Use: `Fast-forwards' a Blum-Blum-Shub generator by @n@ steps.
230 * Requires the factorization of the Blum modulus to do this
231 * efficiently.
232 */
233
4f743df5 234extern void bbs_ff(bbs */*b*/, bbs_param */*bp*/, unsigned long /*n*/);
2c52abe6 235
236/* --- @bbs_rew@ --- *
237 *
238 * Arguments: @bbs *b@ = pointer to a BBS generator state
4f743df5 239 * @bbs_param *bp@ = pointer to BBS modulus factors
2c52abe6 240 * @unsigned long n@ = number of steps to make
241 *
242 * Returns: ---
243 *
244 * Use: `Rewinds' a Blum-Blum-Shub generator by @n@ steps.
245 * Requires the factorization of the Blum modulus to do this
246 * at all.
247 */
248
4f743df5 249extern void bbs_rew(bbs */*b*/, bbs_param */*bp*/, unsigned long /*n*/);
2c52abe6 250
251/*----- Parameter generation ----------------------------------------------*/
252
253/* --- @bbs_gen@ --- *
254 *
4f743df5 255 * Arguments: @bbs_param *bp@ = pointer to parameter block
052b36d0 256 * @unsigned nbits@ = number of bits in the modulus
257 * @grand *r@ = pointer to random number source
258 * @unsigned n@ = number of attempts to make
4f743df5 259 * @pgen_proc *event@ = event handler function
260 * @void *ectx@ = argument for event handler
2c52abe6 261 *
4f743df5 262 * Returns: If it worked OK, @PGEN_DONE@, otherwise @PGEN_ABORT@.
2c52abe6 263 *
264 * Use: Finds two prime numbers %$p'$% and %$q'$% such that both are
265 * congruent to %$3 \bmod 4$%, and $(p - 1)/2$% and
266 * %$(q - 1)/2$% have no common factors. The product %$n = pq$%
267 * is eminently suitable for use as a modulus in a Blum-Blum-
268 * Shub pseudorandom bit generator.
269 */
270
052b36d0 271extern int bbs_gen(bbs_param */*bp*/, unsigned /*nbits*/, grand */*r*/,
272 unsigned /*n*/, pgen_proc */*event*/, void */*ectx*/);
2c52abe6 273
274/*----- Generic random number generator interface -------------------------*/
275
276/* --- @bbs_rand@ --- *
277 *
278 * Arguments: @mp *m@ = modulus
279 * @mp *x@ = initial seed
280 *
281 * Returns: Pointer to a generic generator.
282 *
283 * Use: Constructs a generic generator interface over a
284 * Blum-Blum-Shub generator.
285 */
286
287extern grand *bbs_rand(mp */*m*/, mp */*x*/);
288
289/* --- Blum-Blum-Shub-specific misc op codes --- */
290
291enum {
292 BBS_SET = GRAND_SPECIFIC /* @mp *x@ */
293};
294
295/*----- That's all, folks -------------------------------------------------*/
296
297#ifdef __cplusplus
298 }
299#endif
300
301#endif