3 * $Id: bbs.h,v 1.6 2001/02/03 16:07:33 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.6 2001/02/03 16:07:33 mdw
34 * Give generic random objects separate namespaces for their supported misc
37 * Revision 1.5 2000/07/01 11:20:24 mdw
38 * New functions for freeing public and private keys. Remove bad type name
41 * Revision 1.4 2000/06/17 10:45:48 mdw
42 * Minor changes for key fetching. Typesetting fixes.
44 * Revision 1.3 2000/02/12 18:21:02 mdw
45 * Overhaul of key management (again).
47 * Revision 1.2 1999/12/22 15:52:08 mdw
48 * Rename `bbs_params' to `bbs_param' for consistency.
50 * Revision 1.1 1999/12/10 23:14:59 mdw
51 * Blum-Blum-Shub generator, and Blum-Goldwasser encryption.
55 /*----- Notes on the BBS generator ----------------------------------------*
57 * The Blum-Blum-Shub generator takes the least significant bits from the
58 * sequence %$x_i = x_{i - 1}^2 \bmod n$%, where %$n = pq$% is the product of
59 * two primes %$p$% and %$q$%, each of which are congruent to %$3 \bmod 4$%.
60 * For maximum period of the generator, %$(p - 1)/2$% and %$(q - 1)/1$%
61 * should be coprime. It is safe to use the least significant
62 * %$\log \log n$% bits of each step in the sequence -- an adversary must
63 * factor the modulus before being able to work forwards or backwards. The
64 * output of the generator cannot be distinguished from a (uniform,
65 * independent) random sequence of bits using any polynomial-time test. This
66 * is by far the strongest pseudorandom number generator provided in
67 * Catacomb, and by far the slowest too. For normal use, the standard
68 * Catacomb @rand@ generator should be more than adequate.
71 #ifndef CATACOMB_BBS_H
72 #define CATACOMB_BBS_H
78 /*----- Header files ------------------------------------------------------*/
80 #include <mLib/bits.h>
82 #ifndef CATACOMB_GRAND_H
86 #ifndef CATACOMB_KEY_H
94 #ifndef CATACOMB_MPBARRETT_H
95 # include "mpbarrett.h"
98 #ifndef CATACOMB_PGEN_H
102 /*----- Data structures ---------------------------------------------------*/
104 /* --- Basic generator state --- */
107 mpbarrett mb
; /* Barrett reduction context */
108 mp
*x
; /* Current quadratic residue */
109 unsigned k
; /* Number of bits from each step */
110 unsigned b
; /* Number of bits in reservoir */
111 mpw r
; /* Reservoir of output bits */
114 /* --- Parameters --- */
116 typedef struct bbs_pub
{
120 typedef struct bbs_priv
{
121 mp
*p
, *q
; /* Prime factors (3 mod 4) */
122 mp
*n
; /* Product @pq@ -- a Blum integer */
125 /*----- Key fetching ------------------------------------------------------*/
127 extern const key_fetchdef bbs_pubfetch
[];
128 #define BBS_PUBFETCHSZ 3
130 extern const key_fetchdef bbs_privfetch
[];
131 #define BBS_PRIVFETCHSZ 7
133 /* --- @bbs_pubfree@, @bbs_privfree@ --- *
135 * Arguments: @bbs_pub *bp@, @bbs_priv *bp@ = pointer to key block
139 * Use: Frees a BBS key block.
142 extern void bbs_pubfree(bbs_pub */
*bp*/
);
143 extern void bbs_privfree(bbs_priv */
*bp*/
);
145 /*----- The basic generator -----------------------------------------------*/
147 /* --- @bbs_create@ --- *
149 * Arguments: @bbs *b@ = pointer to BBS generator state to initialize
150 * @mp *m@ = modulus (must be a Blum integer)
151 * @mp *x@ = initial seed for generator
155 * Use: Initializes a BBS generator. The generator is stepped once
156 * after initialization, as for @bbs_seed@.
159 extern void bbs_create(bbs */
*b*/
, mp */
*m*/
, mp */
*x*/
);
161 /* --- @bbs_destroy@ --- *
163 * Arguments: @bbs *b@ = pointer to BBS generator state
167 * Use: Destroys a generator state when it's no longer wanted.
170 extern void bbs_destroy(bbs */
*b*/
);
172 /* --- @bbs_step@ --- *
174 * Arguments: @bbs *b@ = pointer to BBS generator state
178 * Use: Steps the generator once. This isn't too useful in client
182 extern void bbs_step(bbs */
*b*/
);
184 /* --- @bbs_set@ --- *
186 * Arguments: @bbs *b@ = pointer to BBS generator state
187 * @mp *x@ = new residue to set
191 * Use: Sets a new quadratic residue. The generator is stepped once.
194 extern void bbs_set(bbs */
*b*/
, mp */
*x*/
);
196 /* --- @bbs_seed@ --- *
198 * Arguments: @bbs *b@ = pointer to BBS generator state
199 * @mp *x@ = new seed to set
203 * Use: Sets a new seed. The generator is stepped until the residue
204 * has clearly wrapped around.
207 extern void bbs_seed(bbs */
*b*/
, mp */
*x*/
);
209 /* --- @bbs_bits@ --- *
211 * Arguments: @bbs *b@ = pointer to BBS generator state
212 * @unsigned bits@ = number of bits wanted
214 * Returns: Bits extracted from the BBS generator.
216 * Use: Extracts a requested number of bits from the BBS generator.
219 extern uint32
bbs_bits(bbs */
*b*/
, unsigned /*bits*/);
221 /* --- @bbs_wrap@ --- *
223 * Arguments: @bbs *b@ = pointer to BBS generator state
227 * Use: Steps the generator if any of the reservoir bits are used.
228 * This can be used to `wrap up' after a Blum-Goldwasser
229 * encryption, for example, producing the final value to be sent
230 * along with the ciphertext.
232 * If a generator is seeded, %$b$% bits are extracted, and then
233 * @bbs_wrap@ is called, the generator will have been stepped
234 * %$\lceil b/k \rceil$% times.
237 extern void bbs_wrap(bbs */
*b*/
);
239 /*----- Large forwards and backwards jumps --------------------------------*/
241 /* --- @bbs_ff@ --- *
243 * Arguments: @bbs *b@ = pointer to a BBS generator state
244 * @bbs_priv *bp@ = pointer to BBS modulus factors
245 * @unsigned long n@ = number of steps to make
249 * Use: `Fast-forwards' a Blum-Blum-Shub generator by @n@ steps.
250 * Requires the factorization of the Blum modulus to do this
254 extern void bbs_ff(bbs */
*b*/
, bbs_priv */
*bp*/
, unsigned long /*n*/);
256 /* --- @bbs_rew@ --- *
258 * Arguments: @bbs *b@ = pointer to a BBS generator state
259 * @bbs_priv *bp@ = pointer to BBS modulus factors
260 * @unsigned long n@ = number of steps to make
264 * Use: `Rewinds' a Blum-Blum-Shub generator by @n@ steps.
265 * Requires the factorization of the Blum modulus to do this
269 extern void bbs_rew(bbs */
*b*/
, bbs_priv */
*bp*/
, unsigned long /*n*/);
271 /*----- Parameter generation ----------------------------------------------*/
273 /* --- @bbs_gen@ --- *
275 * Arguments: @bbs_priv *bp@ = pointer to parameter block
276 * @unsigned nbits@ = number of bits in the modulus
277 * @grand *r@ = pointer to random number source
278 * @unsigned n@ = number of attempts to make
279 * @pgen_proc *event@ = event handler function
280 * @void *ectx@ = argument for event handler
282 * Returns: If it worked OK, @PGEN_DONE@, otherwise @PGEN_ABORT@.
284 * Use: Finds two prime numbers %$p'$% and %$q'$% such that both are
285 * congruent to %$3 \bmod 4$%, and $(p - 1)/2$% and
286 * %$(q - 1)/2$% have no common factors. The product %$n = pq$%
287 * is eminently suitable for use as a modulus in a Blum-Blum-
288 * Shub pseudorandom bit generator.
291 extern int bbs_gen(bbs_priv */
*bp*/
, unsigned /*nbits*/, grand */
*r*/
,
292 unsigned /*n*/, pgen_proc */
*event*/
, void */
*ectx*/
);
294 /*----- Generic random number generator interface -------------------------*/
296 /* --- @bbs_rand@ --- *
298 * Arguments: @mp *m@ = modulus
299 * @mp *x@ = initial seed
301 * Returns: Pointer to a generic generator.
303 * Use: Constructs a generic generator interface over a
304 * Blum-Blum-Shub generator.
307 extern grand
*bbs_rand(mp */
*m*/
, mp */
*x*/
);
309 /* --- Blum-Blum-Shub-specific misc op codes --- */
312 BBS_SET
= GRAND_SPECIFIC('B') /* @mp *x@ */
315 /*----- That's all, folks -------------------------------------------------*/