Renamed from `rsa-decrypt', since the name was no longer appropriate.
[u/mdw/catacomb] / bbs.h
CommitLineData
2c52abe6 1/* -*-c-*-
2 *
9083376e 3 * $Id: bbs.h,v 1.5 2000/07/01 11:20:24 mdw Exp $
2c52abe6 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 $
9083376e 33 * Revision 1.5 2000/07/01 11:20:24 mdw
34 * New functions for freeing public and private keys. Remove bad type name
35 * `bbs_param'.
36 *
05cac080 37 * Revision 1.4 2000/06/17 10:45:48 mdw
38 * Minor changes for key fetching. Typesetting fixes.
39 *
052b36d0 40 * Revision 1.3 2000/02/12 18:21:02 mdw
41 * Overhaul of key management (again).
42 *
4f743df5 43 * Revision 1.2 1999/12/22 15:52:08 mdw
44 * Rename `bbs_params' to `bbs_param' for consistency.
45 *
2c52abe6 46 * Revision 1.1 1999/12/10 23:14:59 mdw
47 * Blum-Blum-Shub generator, and Blum-Goldwasser encryption.
48 *
49 */
50
51/*----- Notes on the BBS generator ----------------------------------------*
52 *
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$%
05cac080 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.
2c52abe6 65 */
66
67#ifndef CATACOMB_BBS_H
68#define CATACOMB_BBS_H
69
70#ifdef __cplusplus
71 extern "C" {
72#endif
73
74/*----- Header files ------------------------------------------------------*/
75
76#include <mLib/bits.h>
77
78#ifndef CATACOMB_GRAND_H
79# include "grand.h"
80#endif
81
05cac080 82#ifndef CATACOMB_KEY_H
83# include "key.h"
84#endif
85
2c52abe6 86#ifndef CATACOMB_MP_H
87# include "mp.h"
88#endif
89
90#ifndef CATACOMB_MPBARRETT_H
91# include "mpbarrett.h"
92#endif
93
4f743df5 94#ifndef CATACOMB_PGEN_H
95# include "pgen.h"
96#endif
97
2c52abe6 98/*----- Data structures ---------------------------------------------------*/
99
100/* --- Basic generator state --- */
101
102typedef struct bbs {
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 */
108} bbs;
109
110/* --- Parameters --- */
111
05cac080 112typedef struct bbs_pub {
113 mp *n;
114} bbs_pub;
115
9083376e 116typedef struct bbs_priv {
2c52abe6 117 mp *p, *q; /* Prime factors (3 mod 4) */
118 mp *n; /* Product @pq@ -- a Blum integer */
9083376e 119} bbs_priv;
05cac080 120
121/*----- Key fetching ------------------------------------------------------*/
122
123extern const key_fetchdef bbs_pubfetch[];
124#define BBS_PUBFETCHSZ 3
125
126extern const key_fetchdef bbs_privfetch[];
127#define BBS_PRIVFETCHSZ 7
2c52abe6 128
9083376e 129/* --- @bbs_pubfree@, @bbs_privfree@ --- *
130 *
131 * Arguments: @bbs_pub *bp@, @bbs_priv *bp@ = pointer to key block
132 *
133 * Returns: ---
134 *
135 * Use: Frees a BBS key block.
136 */
137
138extern void bbs_pubfree(bbs_pub */*bp*/);
139extern void bbs_privfree(bbs_priv */*bp*/);
140
2c52abe6 141/*----- The basic generator -----------------------------------------------*/
142
143/* --- @bbs_create@ --- *
144 *
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
148 *
149 * Returns: ---
150 *
151 * Use: Initializes a BBS generator. The generator is stepped once
152 * after initialization, as for @bbs_seed@.
153 */
154
155extern void bbs_create(bbs */*b*/, mp */*m*/, mp */*x*/);
156
157/* --- @bbs_destroy@ --- *
158 *
159 * Arguments: @bbs *b@ = pointer to BBS generator state
160 *
161 * Returns: ---
162 *
163 * Use: Destroys a generator state when it's no longer wanted.
164 */
165
166extern void bbs_destroy(bbs */*b*/);
167
168/* --- @bbs_step@ --- *
169 *
170 * Arguments: @bbs *b@ = pointer to BBS generator state
171 *
172 * Returns: ---
173 *
174 * Use: Steps the generator once. This isn't too useful in client
175 * code.
176 */
177
178extern void bbs_step(bbs */*b*/);
179
180/* --- @bbs_set@ --- *
181 *
182 * Arguments: @bbs *b@ = pointer to BBS generator state
183 * @mp *x@ = new residue to set
184 *
185 * Returns: ---
186 *
187 * Use: Sets a new quadratic residue. The generator is stepped once.
188 */
189
190extern void bbs_set(bbs */*b*/, mp */*x*/);
191
192/* --- @bbs_seed@ --- *
193 *
194 * Arguments: @bbs *b@ = pointer to BBS generator state
195 * @mp *x@ = new seed to set
196 *
197 * Returns ---
198 *
199 * Use: Sets a new seed. The generator is stepped until the residue
200 * has clearly wrapped around.
201 */
202
203extern void bbs_seed(bbs */*b*/, mp */*x*/);
204
205/* --- @bbs_bits@ --- *
206 *
207 * Arguments: @bbs *b@ = pointer to BBS generator state
208 * @unsigned bits@ = number of bits wanted
209 *
210 * Returns: Bits extracted from the BBS generator.
211 *
212 * Use: Extracts a requested number of bits from the BBS generator.
213 */
214
215extern uint32 bbs_bits(bbs */*b*/, unsigned /*bits*/);
216
217/* --- @bbs_wrap@ --- *
218 *
219 * Arguments: @bbs *b@ = pointer to BBS generator state
220 *
221 * Returns: ---
222 *
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.
227 *
228 * If a generator is seeded, %$b$% bits are extracted, and then
229 * @bbs_wrap@ is called, the generator will have been stepped
05cac080 230 * %$\lceil b/k \rceil$% times.
2c52abe6 231 */
232
233extern void bbs_wrap(bbs */*b*/);
234
235/*----- Large forwards and backwards jumps --------------------------------*/
236
237/* --- @bbs_ff@ --- *
238 *
239 * Arguments: @bbs *b@ = pointer to a BBS generator state
9083376e 240 * @bbs_priv *bp@ = pointer to BBS modulus factors
2c52abe6 241 * @unsigned long n@ = number of steps to make
242 *
243 * Returns: ---
244 *
245 * Use: `Fast-forwards' a Blum-Blum-Shub generator by @n@ steps.
246 * Requires the factorization of the Blum modulus to do this
247 * efficiently.
248 */
249
9083376e 250extern void bbs_ff(bbs */*b*/, bbs_priv */*bp*/, unsigned long /*n*/);
2c52abe6 251
252/* --- @bbs_rew@ --- *
253 *
254 * Arguments: @bbs *b@ = pointer to a BBS generator state
9083376e 255 * @bbs_priv *bp@ = pointer to BBS modulus factors
2c52abe6 256 * @unsigned long n@ = number of steps to make
257 *
258 * Returns: ---
259 *
260 * Use: `Rewinds' a Blum-Blum-Shub generator by @n@ steps.
261 * Requires the factorization of the Blum modulus to do this
262 * at all.
263 */
264
9083376e 265extern void bbs_rew(bbs */*b*/, bbs_priv */*bp*/, unsigned long /*n*/);
2c52abe6 266
267/*----- Parameter generation ----------------------------------------------*/
268
269/* --- @bbs_gen@ --- *
270 *
9083376e 271 * Arguments: @bbs_priv *bp@ = pointer to parameter block
052b36d0 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
4f743df5 275 * @pgen_proc *event@ = event handler function
276 * @void *ectx@ = argument for event handler
2c52abe6 277 *
4f743df5 278 * Returns: If it worked OK, @PGEN_DONE@, otherwise @PGEN_ABORT@.
2c52abe6 279 *
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.
285 */
286
9083376e 287extern int bbs_gen(bbs_priv */*bp*/, unsigned /*nbits*/, grand */*r*/,
052b36d0 288 unsigned /*n*/, pgen_proc */*event*/, void */*ectx*/);
2c52abe6 289
290/*----- Generic random number generator interface -------------------------*/
291
292/* --- @bbs_rand@ --- *
293 *
294 * Arguments: @mp *m@ = modulus
295 * @mp *x@ = initial seed
296 *
297 * Returns: Pointer to a generic generator.
298 *
299 * Use: Constructs a generic generator interface over a
300 * Blum-Blum-Shub generator.
301 */
302
303extern grand *bbs_rand(mp */*m*/, mp */*x*/);
304
305/* --- Blum-Blum-Shub-specific misc op codes --- */
306
307enum {
308 BBS_SET = GRAND_SPECIFIC /* @mp *x@ */
309};
310
311/*----- That's all, folks -------------------------------------------------*/
312
313#ifdef __cplusplus
314 }
315#endif
316
317#endif