3 * $Id: bbs.h,v 1.3 2000/02/12 18:21:02 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.3 2000/02/12 18:21:02 mdw
34 * Overhaul of key management (again).
36 * Revision 1.2 1999/12/22 15:52:08 mdw
37 * Rename `bbs_params' to `bbs_param' for consistency.
39 * Revision 1.1 1999/12/10 23:14:59 mdw
40 * Blum-Blum-Shub generator, and Blum-Goldwasser encryption.
44 /*----- Notes on the BBS generator ----------------------------------------*
46 * The Blum-Blum-Shub generator takes the least significant bits from the
47 * sequence %$x_i = x_{i - 1}^2 \bmod n$%, where %$n = pq$% is the product of
48 * two primes %$p$% and %$q$%, each of which are congruent to %$3 \bmod 4$%.
49 * For maximum period of the generator, %$(p - 1)/2$% and %$(q - 1)/1$%
50 * should be coprime. It is safe to use the least significant %$\log \log
51 * n$% bits of each step in the sequence -- an adversary must factor the
52 * modulus before being able to work forwards or backwards. The output of
53 * the generator cannot be distinguished from a (uniform, independent) random
54 * sequence of bits using any polynomial-time test. This is by far the
55 * strongest pseudorandom number generator provided in Catacomb, and by far
56 * the slowest too. For normal use, the standard Catacomb @rand@ generator
57 * should be more than adequate.
60 #ifndef CATACOMB_BBS_H
61 #define CATACOMB_BBS_H
67 /*----- Header files ------------------------------------------------------*/
69 #include <mLib/bits.h>
71 #ifndef CATACOMB_GRAND_H
79 #ifndef CATACOMB_MPBARRETT_H
80 # include "mpbarrett.h"
83 #ifndef CATACOMB_PGEN_H
87 /*----- Data structures ---------------------------------------------------*/
89 /* --- Basic generator state --- */
92 mpbarrett mb
; /* Barrett reduction context */
93 mp
*x
; /* Current quadratic residue */
94 unsigned k
; /* Number of bits from each step */
95 unsigned b
; /* Number of bits in reservoir */
96 mpw r
; /* Reservoir of output bits */
99 /* --- Parameters --- */
101 typedef struct bbs_param
{
102 mp
*p
, *q
; /* Prime factors (3 mod 4) */
103 mp
*n
; /* Product @pq@ -- a Blum integer */
106 /*----- The basic generator -----------------------------------------------*/
108 /* --- @bbs_create@ --- *
110 * Arguments: @bbs *b@ = pointer to BBS generator state to initialize
111 * @mp *m@ = modulus (must be a Blum integer)
112 * @mp *x@ = initial seed for generator
116 * Use: Initializes a BBS generator. The generator is stepped once
117 * after initialization, as for @bbs_seed@.
120 extern void bbs_create(bbs */
*b*/
, mp */
*m*/
, mp */
*x*/
);
122 /* --- @bbs_destroy@ --- *
124 * Arguments: @bbs *b@ = pointer to BBS generator state
128 * Use: Destroys a generator state when it's no longer wanted.
131 extern void bbs_destroy(bbs */
*b*/
);
133 /* --- @bbs_step@ --- *
135 * Arguments: @bbs *b@ = pointer to BBS generator state
139 * Use: Steps the generator once. This isn't too useful in client
143 extern void bbs_step(bbs */
*b*/
);
145 /* --- @bbs_set@ --- *
147 * Arguments: @bbs *b@ = pointer to BBS generator state
148 * @mp *x@ = new residue to set
152 * Use: Sets a new quadratic residue. The generator is stepped once.
155 extern void bbs_set(bbs */
*b*/
, mp */
*x*/
);
157 /* --- @bbs_seed@ --- *
159 * Arguments: @bbs *b@ = pointer to BBS generator state
160 * @mp *x@ = new seed to set
164 * Use: Sets a new seed. The generator is stepped until the residue
165 * has clearly wrapped around.
168 extern void bbs_seed(bbs */
*b*/
, mp */
*x*/
);
170 /* --- @bbs_bits@ --- *
172 * Arguments: @bbs *b@ = pointer to BBS generator state
173 * @unsigned bits@ = number of bits wanted
175 * Returns: Bits extracted from the BBS generator.
177 * Use: Extracts a requested number of bits from the BBS generator.
180 extern uint32
bbs_bits(bbs */
*b*/
, unsigned /*bits*/);
182 /* --- @bbs_wrap@ --- *
184 * Arguments: @bbs *b@ = pointer to BBS generator state
188 * Use: Steps the generator if any of the reservoir bits are used.
189 * This can be used to `wrap up' after a Blum-Goldwasser
190 * encryption, for example, producing the final value to be sent
191 * along with the ciphertext.
193 * If a generator is seeded, %$b$% bits are extracted, and then
194 * @bbs_wrap@ is called, the generator will have been stepped
195 * %$\lceil b/k \rceil% times.
198 extern void bbs_wrap(bbs */
*b*/
);
200 /*----- Large forwards and backwards jumps --------------------------------*/
202 /* --- @bbs_ff@ --- *
204 * Arguments: @bbs *b@ = pointer to a BBS generator state
205 * @bbs_param *bp@ = pointer to BBS modulus factors
206 * @unsigned long n@ = number of steps to make
210 * Use: `Fast-forwards' a Blum-Blum-Shub generator by @n@ steps.
211 * Requires the factorization of the Blum modulus to do this
215 extern void bbs_ff(bbs */
*b*/
, bbs_param */
*bp*/
, unsigned long /*n*/);
217 /* --- @bbs_rew@ --- *
219 * Arguments: @bbs *b@ = pointer to a BBS generator state
220 * @bbs_param *bp@ = pointer to BBS modulus factors
221 * @unsigned long n@ = number of steps to make
225 * Use: `Rewinds' a Blum-Blum-Shub generator by @n@ steps.
226 * Requires the factorization of the Blum modulus to do this
230 extern void bbs_rew(bbs */
*b*/
, bbs_param */
*bp*/
, unsigned long /*n*/);
232 /*----- Parameter generation ----------------------------------------------*/
234 /* --- @bbs_gen@ --- *
236 * Arguments: @bbs_param *bp@ = pointer to parameter block
237 * @unsigned nbits@ = number of bits in the modulus
238 * @grand *r@ = pointer to random number source
239 * @unsigned n@ = number of attempts to make
240 * @pgen_proc *event@ = event handler function
241 * @void *ectx@ = argument for event handler
243 * Returns: If it worked OK, @PGEN_DONE@, otherwise @PGEN_ABORT@.
245 * Use: Finds two prime numbers %$p'$% and %$q'$% such that both are
246 * congruent to %$3 \bmod 4$%, and $(p - 1)/2$% and
247 * %$(q - 1)/2$% have no common factors. The product %$n = pq$%
248 * is eminently suitable for use as a modulus in a Blum-Blum-
249 * Shub pseudorandom bit generator.
252 extern int bbs_gen(bbs_param */
*bp*/
, unsigned /*nbits*/, grand */
*r*/
,
253 unsigned /*n*/, pgen_proc */
*event*/
, void */
*ectx*/
);
255 /*----- Generic random number generator interface -------------------------*/
257 /* --- @bbs_rand@ --- *
259 * Arguments: @mp *m@ = modulus
260 * @mp *x@ = initial seed
262 * Returns: Pointer to a generic generator.
264 * Use: Constructs a generic generator interface over a
265 * Blum-Blum-Shub generator.
268 extern grand
*bbs_rand(mp */
*m*/
, mp */
*x*/
);
270 /* --- Blum-Blum-Shub-specific misc op codes --- */
273 BBS_SET
= GRAND_SPECIFIC
/* @mp *x@ */
276 /*----- That's all, folks -------------------------------------------------*/