3 * The Blum-Blum-Shub random bit generator
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
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.
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.
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,
28 /*----- Notes on the BBS generator ----------------------------------------*
30 * The Blum-Blum-Shub generator takes the least significant bits from the
31 * sequence %$x_i = x_{i - 1}^2 \bmod n$%, where %$n = pq$% is the product of
32 * two primes %$p$% and %$q$%, each of which are congruent to %$3 \bmod 4$%.
33 * For maximum period of the generator, %$(p - 1)/2$% and %$(q - 1)/1$%
34 * should be coprime. It is safe to use the least significant
35 * %$\log \log n$% bits of each step in the sequence -- an adversary must
36 * factor the modulus before being able to work forwards or backwards. The
37 * output of the generator cannot be distinguished from a (uniform,
38 * independent) random sequence of bits using any polynomial-time test. This
39 * is by far the strongest pseudorandom number generator provided in
40 * Catacomb, and by far the slowest too. For normal use, the standard
41 * Catacomb @rand@ generator should be more than adequate.
44 #ifndef CATACOMB_BBS_H
45 #define CATACOMB_BBS_H
51 /*----- Header files ------------------------------------------------------*/
53 #include <mLib/bits.h>
55 #ifndef CATACOMB_GRAND_H
59 #ifndef CATACOMB_KEY_H
67 #ifndef CATACOMB_MPBARRETT_H
68 # include "mpbarrett.h"
71 #ifndef CATACOMB_PGEN_H
75 /*----- Data structures ---------------------------------------------------*/
77 /* --- Basic generator state --- */
80 mpbarrett mb; /* Barrett reduction context */
81 mp *x; /* Current quadratic residue */
82 unsigned k; /* Number of bits from each step */
83 unsigned b; /* Number of bits in reservoir */
84 mpw r; /* Reservoir of output bits */
87 /* --- Parameters --- */
89 typedef struct bbs_pub {
93 typedef struct bbs_priv {
94 mp *p, *q; /* Prime factors (3 mod 4) */
95 mp *n; /* Product @pq@ -- a Blum integer */
98 /*----- Key fetching ------------------------------------------------------*/
100 extern const key_fetchdef bbs_pubfetch[];
101 #define BBS_PUBFETCHSZ 3
103 extern const key_fetchdef bbs_privfetch[];
104 #define BBS_PRIVFETCHSZ 7
106 /* --- @bbs_pubfree@, @bbs_privfree@ --- *
108 * Arguments: @bbs_pub *bp@, @bbs_priv *bp@ = pointer to key block
112 * Use: Frees a BBS key block.
115 extern void bbs_pubfree(bbs_pub */*bp*/);
116 extern void bbs_privfree(bbs_priv */*bp*/);
118 /*----- The basic generator -----------------------------------------------*/
120 /* --- @bbs_create@ --- *
122 * Arguments: @bbs *b@ = pointer to BBS generator state to initialize
123 * @mp *m@ = modulus (must be a Blum integer)
124 * @mp *x@ = initial seed for generator
128 * Use: Initializes a BBS generator. The generator is stepped once
129 * after initialization, as for @bbs_seed@.
132 extern void bbs_create(bbs */*b*/, mp */*m*/, mp */*x*/);
134 /* --- @bbs_destroy@ --- *
136 * Arguments: @bbs *b@ = pointer to BBS generator state
140 * Use: Destroys a generator state when it's no longer wanted.
143 extern void bbs_destroy(bbs */*b*/);
145 /* --- @bbs_step@ --- *
147 * Arguments: @bbs *b@ = pointer to BBS generator state
151 * Use: Steps the generator once. This isn't too useful in client
155 extern void bbs_step(bbs */*b*/);
157 /* --- @bbs_set@ --- *
159 * Arguments: @bbs *b@ = pointer to BBS generator state
160 * @mp *x@ = new residue to set
164 * Use: Sets a new quadratic residue. The generator is stepped once.
167 extern void bbs_set(bbs */*b*/, mp */*x*/);
169 /* --- @bbs_seed@ --- *
171 * Arguments: @bbs *b@ = pointer to BBS generator state
172 * @mp *x@ = new seed to set
176 * Use: Sets a new seed. The generator is stepped until the residue
177 * has clearly wrapped around.
180 extern void bbs_seed(bbs */*b*/, mp */*x*/);
182 /* --- @bbs_bits@ --- *
184 * Arguments: @bbs *b@ = pointer to BBS generator state
185 * @unsigned bits@ = number of bits wanted
187 * Returns: Bits extracted from the BBS generator.
189 * Use: Extracts a requested number of bits from the BBS generator.
192 extern uint32 bbs_bits(bbs */*b*/, unsigned /*bits*/);
194 /* --- @bbs_wrap@ --- *
196 * Arguments: @bbs *b@ = pointer to BBS generator state
200 * Use: Steps the generator if any of the reservoir bits are used.
201 * This can be used to `wrap up' after a Blum-Goldwasser
202 * encryption, for example, producing the final value to be sent
203 * along with the ciphertext.
205 * If a generator is seeded, %$b$% bits are extracted, and then
206 * @bbs_wrap@ is called, the generator will have been stepped
207 * %$\lceil b/k \rceil$% times.
210 extern void bbs_wrap(bbs */*b*/);
212 /*----- Large forwards and backwards jumps --------------------------------*/
214 /* --- @bbs_{ff,rew}{,n}@ --- *
216 * Arguments: @bbs *b@ = pointer to a BBS generator state
217 * @const bbs_priv *bp@ = pointer to BBS modulus factors
218 * @mp *n@, @unsigned long n@ = number of steps to make
222 * Use: `Fast-forwards' or rewinds a Blum-Blum-Shub generator by @n@
223 * steps. The @...n@ versions take an @unsigned long@ argument;
224 * the non-@...n@ versions a multiprecision integer. If @n@ is
225 * negative then the generator is stepped in the reverse
229 extern void bbs_ff(bbs */*b*/, const bbs_priv */*bp*/, mp */*n*/);
230 extern void bbs_ffn(bbs */*b*/, const bbs_priv */*bp*/, unsigned long /*n*/);
231 extern void bbs_rew(bbs */*b*/, const bbs_priv */*bp*/, mp */*n*/);
232 extern void bbs_rewn(bbs */*b*/, const bbs_priv */*bp*/, unsigned long /*n*/);
234 /*----- Parameter generation ----------------------------------------------*/
236 /* --- @bbs_gen@ --- *
238 * Arguments: @bbs_priv *bp@ = pointer to parameter block
239 * @unsigned nbits@ = number of bits in the modulus
240 * @grand *r@ = pointer to random number source
241 * @unsigned n@ = number of attempts to make
242 * @pgen_proc *event@ = event handler function
243 * @void *ectx@ = argument for event handler
245 * Returns: If it worked OK, @PGEN_DONE@, otherwise @PGEN_ABORT@.
247 * Use: Finds two prime numbers %$p'$% and %$q'$% such that both are
248 * congruent to %$3 \bmod 4$%, and $(p - 1)/2$% and
249 * %$(q - 1)/2$% have no common factors. The product %$n = pq$%
250 * is eminently suitable for use as a modulus in a Blum-Blum-
251 * Shub pseudorandom bit generator.
254 extern int bbs_gen(bbs_priv */*bp*/, unsigned /*nbits*/, grand */*r*/,
255 unsigned /*n*/, pgen_proc */*event*/, void */*ectx*/);
257 /*----- Generic random number generator interface -------------------------*/
259 /* --- @bbs_rand@ --- *
261 * Arguments: @mp *m@ = modulus
262 * @mp *x@ = initial seed
264 * Returns: Pointer to a generic generator.
266 * Use: Constructs a generic generator interface over a
267 * Blum-Blum-Shub generator.
270 extern grand *bbs_rand(mp */*m*/, mp */*x*/);
272 /* --- Blum-Blum-Shub-specific misc op codes --- */
275 BBS_SET = GRAND_SPECIFIC('B'), /* @mp *x@ */
276 BBS_STEP, /* @void@ */
277 BBS_STEPSZ, /* @void@ */
278 BBS_BITS, /* @unsigned bits, uint32 *w@ */
279 BBS_WRAP, /* @void@ */
280 BBS_FF, /* @bbs_priv *p, mp *n@ */
281 BBS_FFN, /* @bbs_priv *p, unsigned long n@ */
282 BBS_REW, /* @bbs_priv *p, mp *n@ */
283 BBS_REWN, /* @bbs_priv *p, unsigned long n@ */
284 BBS_MOD, /* @mp **n@ */
285 BBS_STATE /* @mp **x@ */
288 /*----- That's all, folks -------------------------------------------------*/