3 * $Id: bbs.h,v 1.4 2000/06/17 10:45:48 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.4 2000/06/17 10:45:48 mdw
34 * Minor changes for key fetching. Typesetting fixes.
36 * Revision 1.3 2000/02/12 18:21:02 mdw
37 * Overhaul of key management (again).
39 * Revision 1.2 1999/12/22 15:52:08 mdw
40 * Rename `bbs_params' to `bbs_param' for consistency.
42 * Revision 1.1 1999/12/10 23:14:59 mdw
43 * Blum-Blum-Shub generator, and Blum-Goldwasser encryption.
47 /*----- Notes on the BBS generator ----------------------------------------*
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$%
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.
63 #ifndef CATACOMB_BBS_H
64 #define CATACOMB_BBS_H
70 /*----- Header files ------------------------------------------------------*/
72 #include <mLib/bits.h>
74 #ifndef CATACOMB_GRAND_H
78 #ifndef CATACOMB_KEY_H
86 #ifndef CATACOMB_MPBARRETT_H
87 # include "mpbarrett.h"
90 #ifndef CATACOMB_PGEN_H
94 /*----- Data structures ---------------------------------------------------*/
96 /* --- Basic generator state --- */
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 */
106 /* --- Parameters --- */
108 typedef struct bbs_pub
{
112 typedef struct bbs_param
{
113 mp
*p
, *q
; /* Prime factors (3 mod 4) */
114 mp
*n
; /* Product @pq@ -- a Blum integer */
115 } bbs_param
, bbs_priv
;
117 /*----- Key fetching ------------------------------------------------------*/
119 extern const key_fetchdef bbs_pubfetch
[];
120 #define BBS_PUBFETCHSZ 3
122 extern const key_fetchdef bbs_privfetch
[];
123 #define BBS_PRIVFETCHSZ 7
125 /*----- The basic generator -----------------------------------------------*/
127 /* --- @bbs_create@ --- *
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
135 * Use: Initializes a BBS generator. The generator is stepped once
136 * after initialization, as for @bbs_seed@.
139 extern void bbs_create(bbs */
*b*/
, mp */
*m*/
, mp */
*x*/
);
141 /* --- @bbs_destroy@ --- *
143 * Arguments: @bbs *b@ = pointer to BBS generator state
147 * Use: Destroys a generator state when it's no longer wanted.
150 extern void bbs_destroy(bbs */
*b*/
);
152 /* --- @bbs_step@ --- *
154 * Arguments: @bbs *b@ = pointer to BBS generator state
158 * Use: Steps the generator once. This isn't too useful in client
162 extern void bbs_step(bbs */
*b*/
);
164 /* --- @bbs_set@ --- *
166 * Arguments: @bbs *b@ = pointer to BBS generator state
167 * @mp *x@ = new residue to set
171 * Use: Sets a new quadratic residue. The generator is stepped once.
174 extern void bbs_set(bbs */
*b*/
, mp */
*x*/
);
176 /* --- @bbs_seed@ --- *
178 * Arguments: @bbs *b@ = pointer to BBS generator state
179 * @mp *x@ = new seed to set
183 * Use: Sets a new seed. The generator is stepped until the residue
184 * has clearly wrapped around.
187 extern void bbs_seed(bbs */
*b*/
, mp */
*x*/
);
189 /* --- @bbs_bits@ --- *
191 * Arguments: @bbs *b@ = pointer to BBS generator state
192 * @unsigned bits@ = number of bits wanted
194 * Returns: Bits extracted from the BBS generator.
196 * Use: Extracts a requested number of bits from the BBS generator.
199 extern uint32
bbs_bits(bbs */
*b*/
, unsigned /*bits*/);
201 /* --- @bbs_wrap@ --- *
203 * Arguments: @bbs *b@ = pointer to BBS generator state
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.
212 * If a generator is seeded, %$b$% bits are extracted, and then
213 * @bbs_wrap@ is called, the generator will have been stepped
214 * %$\lceil b/k \rceil$% times.
217 extern void bbs_wrap(bbs */
*b*/
);
219 /*----- Large forwards and backwards jumps --------------------------------*/
221 /* --- @bbs_ff@ --- *
223 * Arguments: @bbs *b@ = pointer to a BBS generator state
224 * @bbs_param *bp@ = pointer to BBS modulus factors
225 * @unsigned long n@ = number of steps to make
229 * Use: `Fast-forwards' a Blum-Blum-Shub generator by @n@ steps.
230 * Requires the factorization of the Blum modulus to do this
234 extern void bbs_ff(bbs */
*b*/
, bbs_param */
*bp*/
, unsigned long /*n*/);
236 /* --- @bbs_rew@ --- *
238 * Arguments: @bbs *b@ = pointer to a BBS generator state
239 * @bbs_param *bp@ = pointer to BBS modulus factors
240 * @unsigned long n@ = number of steps to make
244 * Use: `Rewinds' a Blum-Blum-Shub generator by @n@ steps.
245 * Requires the factorization of the Blum modulus to do this
249 extern void bbs_rew(bbs */
*b*/
, bbs_param */
*bp*/
, unsigned long /*n*/);
251 /*----- Parameter generation ----------------------------------------------*/
253 /* --- @bbs_gen@ --- *
255 * Arguments: @bbs_param *bp@ = pointer to parameter block
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
259 * @pgen_proc *event@ = event handler function
260 * @void *ectx@ = argument for event handler
262 * Returns: If it worked OK, @PGEN_DONE@, otherwise @PGEN_ABORT@.
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.
271 extern int bbs_gen(bbs_param */
*bp*/
, unsigned /*nbits*/, grand */
*r*/
,
272 unsigned /*n*/, pgen_proc */
*event*/
, void */
*ectx*/
);
274 /*----- Generic random number generator interface -------------------------*/
276 /* --- @bbs_rand@ --- *
278 * Arguments: @mp *m@ = modulus
279 * @mp *x@ = initial seed
281 * Returns: Pointer to a generic generator.
283 * Use: Constructs a generic generator interface over a
284 * Blum-Blum-Shub generator.
287 extern grand
*bbs_rand(mp */
*m*/
, mp */
*x*/
);
289 /* --- Blum-Blum-Shub-specific misc op codes --- */
292 BBS_SET
= GRAND_SPECIFIC
/* @mp *x@ */
295 /*----- That's all, folks -------------------------------------------------*/