3 * $Id: bbs.h,v 1.5 2000/07/01 11:20:24 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.5 2000/07/01 11:20:24 mdw
34 * New functions for freeing public and private keys. Remove bad type name
37 * Revision 1.4 2000/06/17 10:45:48 mdw
38 * Minor changes for key fetching. Typesetting fixes.
40 * Revision 1.3 2000/02/12 18:21:02 mdw
41 * Overhaul of key management (again).
43 * Revision 1.2 1999/12/22 15:52:08 mdw
44 * Rename `bbs_params' to `bbs_param' for consistency.
46 * Revision 1.1 1999/12/10 23:14:59 mdw
47 * Blum-Blum-Shub generator, and Blum-Goldwasser encryption.
51 /*----- Notes on the BBS generator ----------------------------------------*
53 * The Blum-Blum-Shub generator takes the least significant bits from the
54 * sequence %$x_i = x_{i - 1}^2 \bmod n$%, where %$n = pq$% is the product of
55 * two primes %$p$% and %$q$%, each of which are congruent to %$3 \bmod 4$%.
56 * For maximum period of the generator, %$(p - 1)/2$% and %$(q - 1)/1$%
57 * should be coprime. It is safe to use the least significant
58 * %$\log \log n$% bits of each step in the sequence -- an adversary must
59 * factor the modulus before being able to work forwards or backwards. The
60 * output of the generator cannot be distinguished from a (uniform,
61 * independent) random sequence of bits using any polynomial-time test. This
62 * is by far the strongest pseudorandom number generator provided in
63 * Catacomb, and by far the slowest too. For normal use, the standard
64 * Catacomb @rand@ generator should be more than adequate.
67 #ifndef CATACOMB_BBS_H
68 #define CATACOMB_BBS_H
74 /*----- Header files ------------------------------------------------------*/
76 #include <mLib/bits.h>
78 #ifndef CATACOMB_GRAND_H
82 #ifndef CATACOMB_KEY_H
90 #ifndef CATACOMB_MPBARRETT_H
91 # include "mpbarrett.h"
94 #ifndef CATACOMB_PGEN_H
98 /*----- Data structures ---------------------------------------------------*/
100 /* --- Basic generator state --- */
103 mpbarrett mb
; /* Barrett reduction context */
104 mp
*x
; /* Current quadratic residue */
105 unsigned k
; /* Number of bits from each step */
106 unsigned b
; /* Number of bits in reservoir */
107 mpw r
; /* Reservoir of output bits */
110 /* --- Parameters --- */
112 typedef struct bbs_pub
{
116 typedef struct bbs_priv
{
117 mp
*p
, *q
; /* Prime factors (3 mod 4) */
118 mp
*n
; /* Product @pq@ -- a Blum integer */
121 /*----- Key fetching ------------------------------------------------------*/
123 extern const key_fetchdef bbs_pubfetch
[];
124 #define BBS_PUBFETCHSZ 3
126 extern const key_fetchdef bbs_privfetch
[];
127 #define BBS_PRIVFETCHSZ 7
129 /* --- @bbs_pubfree@, @bbs_privfree@ --- *
131 * Arguments: @bbs_pub *bp@, @bbs_priv *bp@ = pointer to key block
135 * Use: Frees a BBS key block.
138 extern void bbs_pubfree(bbs_pub */
*bp*/
);
139 extern void bbs_privfree(bbs_priv */
*bp*/
);
141 /*----- The basic generator -----------------------------------------------*/
143 /* --- @bbs_create@ --- *
145 * Arguments: @bbs *b@ = pointer to BBS generator state to initialize
146 * @mp *m@ = modulus (must be a Blum integer)
147 * @mp *x@ = initial seed for generator
151 * Use: Initializes a BBS generator. The generator is stepped once
152 * after initialization, as for @bbs_seed@.
155 extern void bbs_create(bbs */
*b*/
, mp */
*m*/
, mp */
*x*/
);
157 /* --- @bbs_destroy@ --- *
159 * Arguments: @bbs *b@ = pointer to BBS generator state
163 * Use: Destroys a generator state when it's no longer wanted.
166 extern void bbs_destroy(bbs */
*b*/
);
168 /* --- @bbs_step@ --- *
170 * Arguments: @bbs *b@ = pointer to BBS generator state
174 * Use: Steps the generator once. This isn't too useful in client
178 extern void bbs_step(bbs */
*b*/
);
180 /* --- @bbs_set@ --- *
182 * Arguments: @bbs *b@ = pointer to BBS generator state
183 * @mp *x@ = new residue to set
187 * Use: Sets a new quadratic residue. The generator is stepped once.
190 extern void bbs_set(bbs */
*b*/
, mp */
*x*/
);
192 /* --- @bbs_seed@ --- *
194 * Arguments: @bbs *b@ = pointer to BBS generator state
195 * @mp *x@ = new seed to set
199 * Use: Sets a new seed. The generator is stepped until the residue
200 * has clearly wrapped around.
203 extern void bbs_seed(bbs */
*b*/
, mp */
*x*/
);
205 /* --- @bbs_bits@ --- *
207 * Arguments: @bbs *b@ = pointer to BBS generator state
208 * @unsigned bits@ = number of bits wanted
210 * Returns: Bits extracted from the BBS generator.
212 * Use: Extracts a requested number of bits from the BBS generator.
215 extern uint32
bbs_bits(bbs */
*b*/
, unsigned /*bits*/);
217 /* --- @bbs_wrap@ --- *
219 * Arguments: @bbs *b@ = pointer to BBS generator state
223 * Use: Steps the generator if any of the reservoir bits are used.
224 * This can be used to `wrap up' after a Blum-Goldwasser
225 * encryption, for example, producing the final value to be sent
226 * along with the ciphertext.
228 * If a generator is seeded, %$b$% bits are extracted, and then
229 * @bbs_wrap@ is called, the generator will have been stepped
230 * %$\lceil b/k \rceil$% times.
233 extern void bbs_wrap(bbs */
*b*/
);
235 /*----- Large forwards and backwards jumps --------------------------------*/
237 /* --- @bbs_ff@ --- *
239 * Arguments: @bbs *b@ = pointer to a BBS generator state
240 * @bbs_priv *bp@ = pointer to BBS modulus factors
241 * @unsigned long n@ = number of steps to make
245 * Use: `Fast-forwards' a Blum-Blum-Shub generator by @n@ steps.
246 * Requires the factorization of the Blum modulus to do this
250 extern void bbs_ff(bbs */
*b*/
, bbs_priv */
*bp*/
, unsigned long /*n*/);
252 /* --- @bbs_rew@ --- *
254 * Arguments: @bbs *b@ = pointer to a BBS generator state
255 * @bbs_priv *bp@ = pointer to BBS modulus factors
256 * @unsigned long n@ = number of steps to make
260 * Use: `Rewinds' a Blum-Blum-Shub generator by @n@ steps.
261 * Requires the factorization of the Blum modulus to do this
265 extern void bbs_rew(bbs */
*b*/
, bbs_priv */
*bp*/
, unsigned long /*n*/);
267 /*----- Parameter generation ----------------------------------------------*/
269 /* --- @bbs_gen@ --- *
271 * Arguments: @bbs_priv *bp@ = pointer to parameter block
272 * @unsigned nbits@ = number of bits in the modulus
273 * @grand *r@ = pointer to random number source
274 * @unsigned n@ = number of attempts to make
275 * @pgen_proc *event@ = event handler function
276 * @void *ectx@ = argument for event handler
278 * Returns: If it worked OK, @PGEN_DONE@, otherwise @PGEN_ABORT@.
280 * Use: Finds two prime numbers %$p'$% and %$q'$% such that both are
281 * congruent to %$3 \bmod 4$%, and $(p - 1)/2$% and
282 * %$(q - 1)/2$% have no common factors. The product %$n = pq$%
283 * is eminently suitable for use as a modulus in a Blum-Blum-
284 * Shub pseudorandom bit generator.
287 extern int bbs_gen(bbs_priv */
*bp*/
, unsigned /*nbits*/, grand */
*r*/
,
288 unsigned /*n*/, pgen_proc */
*event*/
, void */
*ectx*/
);
290 /*----- Generic random number generator interface -------------------------*/
292 /* --- @bbs_rand@ --- *
294 * Arguments: @mp *m@ = modulus
295 * @mp *x@ = initial seed
297 * Returns: Pointer to a generic generator.
299 * Use: Constructs a generic generator interface over a
300 * Blum-Blum-Shub generator.
303 extern grand
*bbs_rand(mp */
*m*/
, mp */
*x*/
);
305 /* --- Blum-Blum-Shub-specific misc op codes --- */
308 BBS_SET
= GRAND_SPECIFIC
/* @mp *x@ */
311 /*----- That's all, folks -------------------------------------------------*/