chiark / gitweb /
Overhaul of key management (again).
[catacomb] / pgen.h
1 /* -*-c-*-
2  *
3  * $Id: pgen.h,v 1.5 2000/02/12 18:21:03 mdw Exp $
4  *
5  * Prime generation glue
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: pgen.h,v $
33  * Revision 1.5  2000/02/12 18:21:03  mdw
34  * Overhaul of key management (again).
35  *
36  * Revision 1.4  1999/12/22 16:01:11  mdw
37  * Same file, completely different code.  Main interface for new prime-
38  * search system.
39  *
40  */
41
42 #ifndef CATACOMB_PGEN_H
43 #define CATACOMB_PGEN_H
44
45 #ifdef __cplusplus
46   extern "C" {
47 #endif
48
49 /*----- Header files ------------------------------------------------------*/
50
51 #ifndef CATACOMB_GRAND_H
52 #  include "grand.h"
53 #endif
54
55 #ifndef CATACOMB_MP_H
56 #  include "mp.h"
57 #endif
58
59 #ifndef CATACOMB_PFILT_H
60 #  include "pfilt.h"
61 #endif
62
63 #ifndef CATACOMB_RABIN_H
64 #  include "rabin.h"
65 #endif
66
67 /*----- Event handling ----------------------------------------------------*
68  *
69  * Different programs and architectures will want to show progress of prime
70  * searches and similar processes in different ways.  Of course, for simple
71  * searches, it's possible to use the @pfilt@ and @rabin@ functions and
72  * maintain control over the general control flow throughout the search.
73  *
74  * For more complex cases, this sort of control is undesirable.  It's
75  * possible to specify an event handler which is informed in abstract about
76  * the search.  The event handler can also request the search be aborted.
77  */
78
79 /* --- Event code constants --- *
80  *
81  * You're allowed to rely on the values of @PGEN_DONE@ and @PGEN_ABORT@.
82  */
83
84 enum {
85   PGEN_BEGIN = 1,                       /* Search for value begins */
86   PGEN_TRY,                             /* A new candidate has appeared */
87   PGEN_FAIL,                            /* The candidate failed the test */
88   PGEN_PASS,                            /* The candidate passed a test */
89   PGEN_DONE = 0,                        /* A good value has been found */
90   PGEN_ABORT = -1                       /* The search has been aborted */
91 };
92
93 /* --- Event information --- *
94  *
95  * Note that the pseudorandom number generator supplied is not
96  * cryptographically strong.
97  */
98
99 typedef struct pgen_event {
100   const char *name;                     /* Which quantity is being found */
101   mp *m;                                /* Current value under test */
102   unsigned steps;                       /* Number of candidates left */
103   unsigned tests;                       /* Tests left before passing */
104   grand *r;                             /* Source of random numbers */
105 } pgen_event;
106
107 /*----- Prime search parameters -------------------------------------------*
108  *
109  * The prime search is parameterized in a large number of ways, although this
110  * isn't so much of a surprise given the different sorts of properties
111  * required from prime numbers in cryptographic applications.
112  *
113  * There are two main things which need to be configured: stepping, and
114  * testing.  (Filtering is done as part of stepping.)
115  *
116  * The functions here provide a toolkit for constructing stepping and testing
117  * routines.  In a lot of cases, the functions can be used directly; in
118  * others, simple bits of glue need be written.
119  *
120  * Two types of functions are defined: steppers and testers, but their
121  * interfaces are substantially similar.  Each is given a request code, a
122  * context block and an event block.  It is meant to update its context and
123  * the event block and return an event code.
124  *
125  * A call with a request of @PGEN_BEGIN@ asks the stepper or tester to
126  * initialize itself using the information in its event block and context.  A
127  * return of @PGEN_FAIL@ reports an immediate failure; @PGEN_ABORT@ reports a
128  * fatal problem; @PGEN_DONE@ reports immediate success.  @PGEN_TRY@ reports
129  * successful initialization and requests test iterations.
130  *
131  * A call to a stepper with a request of @PGEN_TRY@ asks it to step to the
132  * next possible candidate, replacing the value @m@ in the event block with
133  * the new candidate.  A call to a tester with a request of @PGEN_TRY@
134  * runs one pass of the test.  It should return @PGEN_FAIL@ to report a
135  * failure, @PGEN_PASS@ to report a success and request another iteration,
136  * @PGEN_DONE@ to report final acceptance and @PGEN_ABORT@ to terminate the
137  * search unsuccessfully.  Note that even if the search is aborted, a
138  * shutdown request is still made.
139  *
140  * A call with a request of @PGEN_DONE@ closes down the stepper or tester.
141  * After a successful initialization (i.e., a return of something other than
142  * @PGEN_ABORT@), a shutdown call is guaranteed.  The return code is ignored.
143  */
144
145 typedef int pgen_proc(int /*rq*/, pgen_event */*ev*/, void */*p*/);
146
147 /*----- Simple handler functions ------------------------------------------*/
148
149 /* --- @pgen_filter@ --- *
150  *
151  * A prime generation context contains the information required for the
152  * simple prime filter and tester presented here.
153  */
154
155 typedef struct pgen_filterctx {
156   unsigned step;                        /* Step size (set by client) */
157   pfilt f;                              /* The rapid prime filter */
158 } pgen_filterctx;
159
160 extern int pgen_filter(int /*rq*/, pgen_event */*ev*/, void */*p*/);
161
162 /* --- @pgen_jump@ --- *
163  *
164  * Similar to the standard @pgen_filter@, but jumps in large steps rather
165  * than small ones.
166  */
167
168 typedef struct pgen_jumpctx {
169   const pfilt *j;
170   pfilt f;
171 } pgen_jumpctx;
172
173 extern int pgen_jump(int /*rq*/, pgen_event */*ev*/, void */*p*/);
174
175 /* --- @pgen_test@ --- *
176  *
177  * Runs the Rabin-Miller primality test.  The context block is simply a
178  * @rabin@ context.
179  */
180
181 extern int pgen_test(int /*rq*/, pgen_event */*ev*/, void */*p*/);
182
183 /*----- Safe prime functions ----------------------------------------------*/
184
185 /* --- @pgen_safestep@ --- *
186  *
187  * Steps two numbers, %$q$% and %$p = 2q + 1$%, such that neither has any
188  * small factors.  %$p$% is put in the event block.
189  */
190
191 typedef struct pgen_safestepctx {
192   pfilt q, p;
193 } pgen_safestepctx;
194
195 extern int pgen_safestep(int /*rq*/, pgen_event */*ev*/, void */*p*/);
196
197 /* --- @pgen_safejump@ --- *
198  *
199  * Jumps two numbers, %$q$% and %$p = 2q + 1$% such that neither has any
200  * small factors.
201  */
202
203 typedef struct pgen_safejumpctx {
204   pfilt q, jq;
205   pfilt p, jp;
206 } pgen_safejumpctx;
207
208 extern int pgen_safejump(int /*rq*/, pgen_event */*ev*/, void */*p*/);
209
210 /* --- @pgen_safetest@ --- *
211  *
212  * Applies Rabin-Miller tests to %$p$% and %$(p - 1)/2$%.
213  */
214
215 typedef struct pgen_safetestctx {
216   pgen_safestepctx c;
217   rabin q, p;
218 } pgen_safetestctx;
219
220 extern int pgen_safetest(int /*rq*/, pgen_event */*ev*/, void */*p*/);
221
222 /*----- Standard event handlers -------------------------------------------*/
223
224 /* --- @pgen_evspin@ --- *
225  *
226  * Displays a spinning baton to show progress.
227  */
228
229 extern int pgen_evspin(int /*rq*/, pgen_event */*ev*/, void */*p*/);
230
231 /* --- @pgen_ev@ --- *
232  *
233  * Traditional event handler, shows dots for each test.
234  */
235
236 extern int pgen_ev(int /*rq*/, pgen_event */*ev*/, void */*p*/);
237
238 /*----- The main driver ---------------------------------------------------*/
239
240 /* --- @pgen@ --- *
241  *
242  * Arguments:   @const char *name@ = name of the value being searched for
243  *              @mp *d@ = destination for resulting integer
244  *              @mp *m@ = start value to pass to stepper
245  *              @pgen_proc *event@ = event handler function
246  *              @void *ectx@ = context argument for event andler
247  *              @unsigned steps@ = number of steps to take in search
248  *              @pgen_proc *step@ = stepper function to use
249  *              @void *sctx@ = context argument for stepper
250  *              @unsigned tests@ = number of tests to make
251  *              @pgen_proc *test@ = tester function to use
252  *              @void *tctx@ = context argument for tester
253  *
254  * Returns:     If successful, @PGEN_DONE@; otherwise @PGEN_ABORT@.
255  *
256  * Use:         A generalized prime-number search skeleton.  Yes, that's a
257  *              scary number of arguments.
258  */
259
260 extern mp *pgen(const char */*name*/, mp */*d*/, mp */*m*/,
261                 pgen_proc */*event*/, void */*ectx*/,
262                 unsigned /*steps*/, pgen_proc */*step*/, void */*sctx*/,
263                 unsigned /*tests*/, pgen_proc */*test*/, void */*tctx*/);
264
265 /*----- That's all, folks -------------------------------------------------*/
266
267 #ifdef __cplusplus
268   }
269 #endif
270
271 #endif