chiark / gitweb /
Minor changes for key fetching. Typesetting fixes.
[catacomb] / bbs.h
1 /* -*-c-*-
2  *
3  * $Id: bbs.h,v 1.4 2000/06/17 10:45:48 mdw Exp $
4  *
5  * The Blum-Blum-Shub random bit generator
6  *
7  * (c) 1999 Straylight/Edgeware
8  */
9
10 /*----- Licensing notice --------------------------------------------------* 
11  *
12  * This file is part of Catacomb.
13  *
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.
18  * 
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.
23  * 
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,
27  * MA 02111-1307, USA.
28  */
29
30 /*----- Revision history --------------------------------------------------* 
31  *
32  * $Log: bbs.h,v $
33  * Revision 1.4  2000/06/17 10:45:48  mdw
34  * Minor changes for key fetching.  Typesetting fixes.
35  *
36  * Revision 1.3  2000/02/12 18:21:02  mdw
37  * Overhaul of key management (again).
38  *
39  * Revision 1.2  1999/12/22 15:52:08  mdw
40  * Rename `bbs_params' to `bbs_param' for consistency.
41  *
42  * Revision 1.1  1999/12/10 23:14:59  mdw
43  * Blum-Blum-Shub generator, and Blum-Goldwasser encryption.
44  *
45  */
46
47 /*----- Notes on the BBS generator ----------------------------------------*
48  *
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.
61  */
62
63 #ifndef CATACOMB_BBS_H
64 #define CATACOMB_BBS_H
65
66 #ifdef __cplusplus
67   extern "C" {
68 #endif
69
70 /*----- Header files ------------------------------------------------------*/
71
72 #include <mLib/bits.h>
73
74 #ifndef CATACOMB_GRAND_H
75 #  include "grand.h"
76 #endif
77
78 #ifndef CATACOMB_KEY_H
79 #  include "key.h"
80 #endif
81
82 #ifndef CATACOMB_MP_H
83 #  include "mp.h"
84 #endif
85
86 #ifndef CATACOMB_MPBARRETT_H
87 #  include "mpbarrett.h"
88 #endif
89
90 #ifndef CATACOMB_PGEN_H
91 #  include "pgen.h"
92 #endif
93
94 /*----- Data structures ---------------------------------------------------*/
95
96 /* --- Basic generator state --- */
97
98 typedef struct bbs {
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 */
104 } bbs;
105
106 /* --- Parameters --- */
107
108 typedef struct bbs_pub {
109   mp *n;
110 } bbs_pub;
111
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;
116
117 /*----- Key fetching ------------------------------------------------------*/
118
119 extern const key_fetchdef bbs_pubfetch[];
120 #define BBS_PUBFETCHSZ 3
121
122 extern const key_fetchdef bbs_privfetch[];
123 #define BBS_PRIVFETCHSZ 7
124
125 /*----- The basic generator -----------------------------------------------*/
126
127 /* --- @bbs_create@ --- *
128  *
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
132  *
133  * Returns:     ---
134  *
135  * Use:         Initializes a BBS generator.  The generator is stepped once
136  *              after initialization, as for @bbs_seed@.
137  */
138
139 extern void bbs_create(bbs */*b*/, mp */*m*/, mp */*x*/);
140
141 /* --- @bbs_destroy@ --- *
142  *
143  * Arguments:   @bbs *b@ = pointer to BBS generator state
144  *
145  * Returns:     ---
146  *
147  * Use:         Destroys a generator state when it's no longer wanted.
148  */
149
150 extern void bbs_destroy(bbs */*b*/);
151
152 /* --- @bbs_step@ --- *
153  *
154  * Arguments:   @bbs *b@ = pointer to BBS generator state
155  *
156  * Returns:     ---
157  *
158  * Use:         Steps the generator once.  This isn't too useful in client
159  *              code.
160  */
161
162 extern void bbs_step(bbs */*b*/);
163
164 /* --- @bbs_set@ --- *
165  *
166  * Arguments:   @bbs *b@ = pointer to BBS generator state
167  *              @mp *x@ = new residue to set
168  *
169  * Returns:     ---
170  *
171  * Use:         Sets a new quadratic residue.  The generator is stepped once.
172  */
173
174 extern void bbs_set(bbs */*b*/, mp */*x*/);
175
176 /* --- @bbs_seed@ --- *
177  *
178  * Arguments:   @bbs *b@ = pointer to BBS generator state
179  *              @mp *x@ = new seed to set
180  *
181  * Returns      ---
182  *
183  * Use:         Sets a new seed.  The generator is stepped until the residue
184  *              has clearly wrapped around.
185  */
186
187 extern void bbs_seed(bbs */*b*/, mp */*x*/);
188
189 /* --- @bbs_bits@ --- *
190  *
191  * Arguments:   @bbs *b@ = pointer to BBS generator state
192  *              @unsigned bits@ = number of bits wanted
193  *
194  * Returns:     Bits extracted from the BBS generator.
195  *
196  * Use:         Extracts a requested number of bits from the BBS generator.
197  */
198
199 extern uint32 bbs_bits(bbs */*b*/, unsigned /*bits*/);
200
201 /* --- @bbs_wrap@ --- *
202  *
203  * Arguments:   @bbs *b@ = pointer to BBS generator state
204  *
205  * Returns:     ---
206  *
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.
211  *
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.
215  */
216
217 extern void bbs_wrap(bbs */*b*/);
218
219 /*----- Large forwards and backwards jumps --------------------------------*/
220
221 /* --- @bbs_ff@ --- *
222  *
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
226  *
227  * Returns:     ---
228  *
229  * Use:         `Fast-forwards' a Blum-Blum-Shub generator by @n@ steps.
230  *              Requires the factorization of the Blum modulus to do this
231  *              efficiently.
232  */
233
234 extern void bbs_ff(bbs */*b*/, bbs_param */*bp*/, unsigned long /*n*/);
235
236 /* --- @bbs_rew@ --- *
237  *
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
241  *
242  * Returns:     ---
243  *
244  * Use:         `Rewinds' a Blum-Blum-Shub generator by @n@ steps.
245  *              Requires the factorization of the Blum modulus to do this
246  *              at all.
247  */
248
249 extern void bbs_rew(bbs */*b*/, bbs_param */*bp*/, unsigned long /*n*/);
250
251 /*----- Parameter generation ----------------------------------------------*/
252
253 /* --- @bbs_gen@ --- *
254  *
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
261  *
262  * Returns:     If it worked OK, @PGEN_DONE@, otherwise @PGEN_ABORT@.
263  *
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.
269  */
270
271 extern int bbs_gen(bbs_param */*bp*/, unsigned /*nbits*/, grand */*r*/,
272                    unsigned /*n*/, pgen_proc */*event*/, void */*ectx*/);
273
274 /*----- Generic random number generator interface -------------------------*/
275
276 /* --- @bbs_rand@ --- *
277  *
278  * Arguments:   @mp *m@ = modulus
279  *              @mp *x@ = initial seed
280  *
281  * Returns:     Pointer to a generic generator.
282  *
283  * Use:         Constructs a generic generator interface over a
284  *              Blum-Blum-Shub generator.
285  */
286
287 extern grand *bbs_rand(mp */*m*/, mp */*x*/);
288
289 /* --- Blum-Blum-Shub-specific misc op codes --- */
290
291 enum {
292   BBS_SET = GRAND_SPECIFIC              /* @mp *x@ */
293 };
294
295 /*----- That's all, folks -------------------------------------------------*/
296
297 #ifdef __cplusplus
298   }
299 #endif
300
301 #endif