3 * $Id: bbs.h,v 1.7 2004/04/08 01:36:15 mdw Exp $
5 * The Blum-Blum-Shub random bit generator
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Notes on the BBS generator ----------------------------------------*
32 * The Blum-Blum-Shub generator takes the least significant bits from the
33 * sequence %$x_i = x_{i - 1}^2 \bmod n$%, where %$n = pq$% is the product of
34 * two primes %$p$% and %$q$%, each of which are congruent to %$3 \bmod 4$%.
35 * For maximum period of the generator, %$(p - 1)/2$% and %$(q - 1)/1$%
36 * should be coprime. It is safe to use the least significant
37 * %$\log \log n$% bits of each step in the sequence -- an adversary must
38 * factor the modulus before being able to work forwards or backwards. The
39 * output of the generator cannot be distinguished from a (uniform,
40 * independent) random sequence of bits using any polynomial-time test. This
41 * is by far the strongest pseudorandom number generator provided in
42 * Catacomb, and by far the slowest too. For normal use, the standard
43 * Catacomb @rand@ generator should be more than adequate.
46 #ifndef CATACOMB_BBS_H
47 #define CATACOMB_BBS_H
53 /*----- Header files ------------------------------------------------------*/
55 #include <mLib/bits.h>
57 #ifndef CATACOMB_GRAND_H
61 #ifndef CATACOMB_KEY_H
69 #ifndef CATACOMB_MPBARRETT_H
70 # include "mpbarrett.h"
73 #ifndef CATACOMB_PGEN_H
77 /*----- Data structures ---------------------------------------------------*/
79 /* --- Basic generator state --- */
82 mpbarrett mb
; /* Barrett reduction context */
83 mp
*x
; /* Current quadratic residue */
84 unsigned k
; /* Number of bits from each step */
85 unsigned b
; /* Number of bits in reservoir */
86 mpw r
; /* Reservoir of output bits */
89 /* --- Parameters --- */
91 typedef struct bbs_pub
{
95 typedef struct bbs_priv
{
96 mp
*p
, *q
; /* Prime factors (3 mod 4) */
97 mp
*n
; /* Product @pq@ -- a Blum integer */
100 /*----- Key fetching ------------------------------------------------------*/
102 extern const key_fetchdef bbs_pubfetch
[];
103 #define BBS_PUBFETCHSZ 3
105 extern const key_fetchdef bbs_privfetch
[];
106 #define BBS_PRIVFETCHSZ 7
108 /* --- @bbs_pubfree@, @bbs_privfree@ --- *
110 * Arguments: @bbs_pub *bp@, @bbs_priv *bp@ = pointer to key block
114 * Use: Frees a BBS key block.
117 extern void bbs_pubfree(bbs_pub */
*bp*/
);
118 extern void bbs_privfree(bbs_priv */
*bp*/
);
120 /*----- The basic generator -----------------------------------------------*/
122 /* --- @bbs_create@ --- *
124 * Arguments: @bbs *b@ = pointer to BBS generator state to initialize
125 * @mp *m@ = modulus (must be a Blum integer)
126 * @mp *x@ = initial seed for generator
130 * Use: Initializes a BBS generator. The generator is stepped once
131 * after initialization, as for @bbs_seed@.
134 extern void bbs_create(bbs */
*b*/
, mp */
*m*/
, mp */
*x*/
);
136 /* --- @bbs_destroy@ --- *
138 * Arguments: @bbs *b@ = pointer to BBS generator state
142 * Use: Destroys a generator state when it's no longer wanted.
145 extern void bbs_destroy(bbs */
*b*/
);
147 /* --- @bbs_step@ --- *
149 * Arguments: @bbs *b@ = pointer to BBS generator state
153 * Use: Steps the generator once. This isn't too useful in client
157 extern void bbs_step(bbs */
*b*/
);
159 /* --- @bbs_set@ --- *
161 * Arguments: @bbs *b@ = pointer to BBS generator state
162 * @mp *x@ = new residue to set
166 * Use: Sets a new quadratic residue. The generator is stepped once.
169 extern void bbs_set(bbs */
*b*/
, mp */
*x*/
);
171 /* --- @bbs_seed@ --- *
173 * Arguments: @bbs *b@ = pointer to BBS generator state
174 * @mp *x@ = new seed to set
178 * Use: Sets a new seed. The generator is stepped until the residue
179 * has clearly wrapped around.
182 extern void bbs_seed(bbs */
*b*/
, mp */
*x*/
);
184 /* --- @bbs_bits@ --- *
186 * Arguments: @bbs *b@ = pointer to BBS generator state
187 * @unsigned bits@ = number of bits wanted
189 * Returns: Bits extracted from the BBS generator.
191 * Use: Extracts a requested number of bits from the BBS generator.
194 extern uint32
bbs_bits(bbs */
*b*/
, unsigned /*bits*/);
196 /* --- @bbs_wrap@ --- *
198 * Arguments: @bbs *b@ = pointer to BBS generator state
202 * Use: Steps the generator if any of the reservoir bits are used.
203 * This can be used to `wrap up' after a Blum-Goldwasser
204 * encryption, for example, producing the final value to be sent
205 * along with the ciphertext.
207 * If a generator is seeded, %$b$% bits are extracted, and then
208 * @bbs_wrap@ is called, the generator will have been stepped
209 * %$\lceil b/k \rceil$% times.
212 extern void bbs_wrap(bbs */
*b*/
);
214 /*----- Large forwards and backwards jumps --------------------------------*/
216 /* --- @bbs_ff@ --- *
218 * Arguments: @bbs *b@ = pointer to a BBS generator state
219 * @bbs_priv *bp@ = pointer to BBS modulus factors
220 * @unsigned long n@ = number of steps to make
224 * Use: `Fast-forwards' a Blum-Blum-Shub generator by @n@ steps.
225 * Requires the factorization of the Blum modulus to do this
229 extern void bbs_ff(bbs */
*b*/
, bbs_priv */
*bp*/
, unsigned long /*n*/);
231 /* --- @bbs_rew@ --- *
233 * Arguments: @bbs *b@ = pointer to a BBS generator state
234 * @bbs_priv *bp@ = pointer to BBS modulus factors
235 * @unsigned long n@ = number of steps to make
239 * Use: `Rewinds' a Blum-Blum-Shub generator by @n@ steps.
240 * Requires the factorization of the Blum modulus to do this
244 extern void bbs_rew(bbs */
*b*/
, bbs_priv */
*bp*/
, unsigned long /*n*/);
246 /*----- Parameter generation ----------------------------------------------*/
248 /* --- @bbs_gen@ --- *
250 * Arguments: @bbs_priv *bp@ = pointer to parameter block
251 * @unsigned nbits@ = number of bits in the modulus
252 * @grand *r@ = pointer to random number source
253 * @unsigned n@ = number of attempts to make
254 * @pgen_proc *event@ = event handler function
255 * @void *ectx@ = argument for event handler
257 * Returns: If it worked OK, @PGEN_DONE@, otherwise @PGEN_ABORT@.
259 * Use: Finds two prime numbers %$p'$% and %$q'$% such that both are
260 * congruent to %$3 \bmod 4$%, and $(p - 1)/2$% and
261 * %$(q - 1)/2$% have no common factors. The product %$n = pq$%
262 * is eminently suitable for use as a modulus in a Blum-Blum-
263 * Shub pseudorandom bit generator.
266 extern int bbs_gen(bbs_priv */
*bp*/
, unsigned /*nbits*/, grand */
*r*/
,
267 unsigned /*n*/, pgen_proc */
*event*/
, void */
*ectx*/
);
269 /*----- Generic random number generator interface -------------------------*/
271 /* --- @bbs_rand@ --- *
273 * Arguments: @mp *m@ = modulus
274 * @mp *x@ = initial seed
276 * Returns: Pointer to a generic generator.
278 * Use: Constructs a generic generator interface over a
279 * Blum-Blum-Shub generator.
282 extern grand
*bbs_rand(mp */
*m*/
, mp */
*x*/
);
284 /* --- Blum-Blum-Shub-specific misc op codes --- */
287 BBS_SET
= GRAND_SPECIFIC('B') /* @mp *x@ */
290 /*----- That's all, folks -------------------------------------------------*/