3 * $Id: bbs.h,v 1.2 1999/12/22 15:52:08 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 /*----- Revision history --------------------------------------------------*
33 * Revision 1.2 1999/12/22 15:52:08 mdw
34 * Rename `bbs_params' to `bbs_param' for consistency.
36 * Revision 1.1 1999/12/10 23:14:59 mdw
37 * Blum-Blum-Shub generator, and Blum-Goldwasser encryption.
41 /*----- Notes on the BBS generator ----------------------------------------*
43 * The Blum-Blum-Shub generator takes the least significant bits from the
44 * sequence %$x_i = x_{i - 1}^2 \bmod n$%, where %$n = pq$% is the product of
45 * two primes %$p$% and %$q$%, each of which are congruent to %$3 \bmod 4$%.
46 * For maximum period of the generator, %$(p - 1)/2$% and %$(q - 1)/1$%
47 * should be coprime. It is safe to use the least significant %$\log \log
48 * n$% bits of each step in the sequence -- an adversary must factor the
49 * modulus before being able to work forwards or backwards. The output of
50 * the generator cannot be distinguished from a (uniform, independent) random
51 * sequence of bits using any polynomial-time test. This is by far the
52 * strongest pseudorandom number generator provided in Catacomb, and by far
53 * the slowest too. For normal use, the standard Catacomb @rand@ generator
54 * should be more than adequate.
57 #ifndef CATACOMB_BBS_H
58 #define CATACOMB_BBS_H
64 /*----- Header files ------------------------------------------------------*/
66 #include <mLib/bits.h>
68 #ifndef CATACOMB_GRAND_H
76 #ifndef CATACOMB_MPBARRETT_H
77 # include "mpbarrett.h"
80 #ifndef CATACOMB_PGEN_H
84 /*----- Data structures ---------------------------------------------------*/
86 /* --- Basic generator state --- */
89 mpbarrett mb
; /* Barrett reduction context */
90 mp
*x
; /* Current quadratic residue */
91 unsigned k
; /* Number of bits from each step */
92 unsigned b
; /* Number of bits in reservoir */
93 mpw r
; /* Reservoir of output bits */
96 /* --- Parameters --- */
98 typedef struct bbs_param
{
99 mp
*p
, *q
; /* Prime factors (3 mod 4) */
100 mp
*n
; /* Product @pq@ -- a Blum integer */
103 /*----- The basic generator -----------------------------------------------*/
105 /* --- @bbs_create@ --- *
107 * Arguments: @bbs *b@ = pointer to BBS generator state to initialize
108 * @mp *m@ = modulus (must be a Blum integer)
109 * @mp *x@ = initial seed for generator
113 * Use: Initializes a BBS generator. The generator is stepped once
114 * after initialization, as for @bbs_seed@.
117 extern void bbs_create(bbs */
*b*/
, mp */
*m*/
, mp */
*x*/
);
119 /* --- @bbs_destroy@ --- *
121 * Arguments: @bbs *b@ = pointer to BBS generator state
125 * Use: Destroys a generator state when it's no longer wanted.
128 extern void bbs_destroy(bbs */
*b*/
);
130 /* --- @bbs_step@ --- *
132 * Arguments: @bbs *b@ = pointer to BBS generator state
136 * Use: Steps the generator once. This isn't too useful in client
140 extern void bbs_step(bbs */
*b*/
);
142 /* --- @bbs_set@ --- *
144 * Arguments: @bbs *b@ = pointer to BBS generator state
145 * @mp *x@ = new residue to set
149 * Use: Sets a new quadratic residue. The generator is stepped once.
152 extern void bbs_set(bbs */
*b*/
, mp */
*x*/
);
154 /* --- @bbs_seed@ --- *
156 * Arguments: @bbs *b@ = pointer to BBS generator state
157 * @mp *x@ = new seed to set
161 * Use: Sets a new seed. The generator is stepped until the residue
162 * has clearly wrapped around.
165 extern void bbs_seed(bbs */
*b*/
, mp */
*x*/
);
167 /* --- @bbs_bits@ --- *
169 * Arguments: @bbs *b@ = pointer to BBS generator state
170 * @unsigned bits@ = number of bits wanted
172 * Returns: Bits extracted from the BBS generator.
174 * Use: Extracts a requested number of bits from the BBS generator.
177 extern uint32
bbs_bits(bbs */
*b*/
, unsigned /*bits*/);
179 /* --- @bbs_wrap@ --- *
181 * Arguments: @bbs *b@ = pointer to BBS generator state
185 * Use: Steps the generator if any of the reservoir bits are used.
186 * This can be used to `wrap up' after a Blum-Goldwasser
187 * encryption, for example, producing the final value to be sent
188 * along with the ciphertext.
190 * If a generator is seeded, %$b$% bits are extracted, and then
191 * @bbs_wrap@ is called, the generator will have been stepped
192 * %$\lceil b/k \rceil% times.
195 extern void bbs_wrap(bbs */
*b*/
);
197 /*----- Large forwards and backwards jumps --------------------------------*/
199 /* --- @bbs_ff@ --- *
201 * Arguments: @bbs *b@ = pointer to a BBS generator state
202 * @bbs_param *bp@ = pointer to BBS modulus factors
203 * @unsigned long n@ = number of steps to make
207 * Use: `Fast-forwards' a Blum-Blum-Shub generator by @n@ steps.
208 * Requires the factorization of the Blum modulus to do this
212 extern void bbs_ff(bbs */
*b*/
, bbs_param */
*bp*/
, unsigned long /*n*/);
214 /* --- @bbs_rew@ --- *
216 * Arguments: @bbs *b@ = pointer to a BBS generator state
217 * @bbs_param *bp@ = pointer to BBS modulus factors
218 * @unsigned long n@ = number of steps to make
222 * Use: `Rewinds' a Blum-Blum-Shub generator by @n@ steps.
223 * Requires the factorization of the Blum modulus to do this
227 extern void bbs_rew(bbs */
*b*/
, bbs_param */
*bp*/
, unsigned long /*n*/);
229 /*----- Parameter generation ----------------------------------------------*/
231 /* --- @bbs_gen@ --- *
233 * Arguments: @bbs_param *bp@ = pointer to parameter block
234 * @mp *p, *q@ = initial numbers to search from
235 * @size_t n@ = number of attempts to make
236 * @pgen_proc *event@ = event handler function
237 * @void *ectx@ = argument for event handler
239 * Returns: If it worked OK, @PGEN_DONE@, otherwise @PGEN_ABORT@.
241 * Use: Finds two prime numbers %$p'$% and %$q'$% such that both are
242 * congruent to %$3 \bmod 4$%, and $(p - 1)/2$% and
243 * %$(q - 1)/2$% have no common factors. The product %$n = pq$%
244 * is eminently suitable for use as a modulus in a Blum-Blum-
245 * Shub pseudorandom bit generator.
248 extern int bbs_gen(bbs_param */
*bp*/
, mp */
*p*/
, mp */
*q*/
, size_t /*n*/,
249 pgen_proc */
*event*/
, void */
*ectx*/
);
251 /*----- Generic random number generator interface -------------------------*/
253 /* --- @bbs_rand@ --- *
255 * Arguments: @mp *m@ = modulus
256 * @mp *x@ = initial seed
258 * Returns: Pointer to a generic generator.
260 * Use: Constructs a generic generator interface over a
261 * Blum-Blum-Shub generator.
264 extern grand
*bbs_rand(mp */
*m*/
, mp */
*x*/
);
266 /* --- Blum-Blum-Shub-specific misc op codes --- */
269 BBS_SET
= GRAND_SPECIFIC
/* @mp *x@ */
272 /*----- That's all, folks -------------------------------------------------*/