chiark / gitweb /
ec-bin (ec_binproj): Make curve setup faster.
[catacomb] / dh.h
1 /* -*-c-*-
2  *
3  * $Id$
4  *
5  * Diffie-Hellman and related public-key systems
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 #ifndef CATACOMB_DH_H
31 #define CATACOMB_DH_H
32
33 #ifdef __cplusplus
34   extern "C" {
35 #endif
36
37 /*----- Header files ------------------------------------------------------*/
38
39 #ifndef CATACOMB_GROUP_H
40 #  include "group.h"
41 #endif
42
43 #ifndef CATACOMB_GRAND_H
44 #  include "grand.h"
45 #endif
46
47 #ifndef CATACOMB_KEY_H
48 #  include "key.h"
49 #endif
50
51 #ifndef CATACOMB_KEYCHECK_H
52 #  include "keycheck.h"
53 #endif
54
55 #ifndef CATACOMB_PGEN_H
56 #  include "pgen.h"
57 #endif
58
59 #ifndef CATACOMB_QDPARSE_H
60 #  include "qdparse.h"
61 #endif
62
63 /*----- Data structures ---------------------------------------------------*/
64
65 typedef gprime_param dh_param;          /* Group parameters */
66
67 typedef struct dh_pub {
68   dh_param dp;                          /* Shared parameters */         
69   mp *y;                                /* Public key */
70 } dh_pub;
71
72 typedef struct dh_priv {
73   dh_param dp;                          /* Shared parameters */
74   mp *x;                                /* Private key */
75   mp *y;                                /* %$y \equiv g^x \pmod{p}$% */
76 } dh_priv;
77
78 /*----- Key fetching ------------------------------------------------------*/
79
80 extern const key_fetchdef dh_paramfetch[];
81 #define DH_PARAMFETCHSZ 5
82
83 extern const key_fetchdef dh_pubfetch[];
84 #define DH_PUBFETCHSZ 6
85
86 extern const key_fetchdef dh_privfetch[];
87 #define DH_PRIVFETCHSZ 9
88
89 /* --- @dh_paramfree@, @dh_pubfree@, @dh_privfree@ --- *
90  *
91  * Arguments:   @dh_param *dp@, @dh_pub *dp@, @dh_priv *dp@ = pointer to
92  *                      key block to free
93  *
94  * Returns:     ---
95  *
96  * Use:         Frees a Diffie-Hellman key block.
97  */
98
99 extern void dh_paramfree(dh_param */*dp*/);
100 extern void dh_pubfree(dh_pub */*dp*/);
101 extern void dh_privfree(dh_priv */*dp*/);
102
103 /*----- Functions provided ------------------------------------------------*/
104
105 /* --- @dh_gen@ --- *
106  *
107  * Arguments:   @dh_param *dp@ = pointer to output parameter block
108  *              @unsigned ql@ = length of %$q$% in bits, or zero
109  *              @unsigned pl@ = length of %$p$% in bits
110  *              @unsigned steps@ = number of steps to go
111  *              @grand *r@ = random number source
112  *              @pgen_proc *event@ = event handler function
113  *              @void *ectx@ = argument for the event handler
114  *
115  * Returns:     @PGEN_DONE@ if it worked, @PGEN_ABORT@ if it didn't.
116  *
117  * Use:         Generates Diffie-Hellman parameters.
118  *
119  *              The parameters are a prime %$q$%, relatively small, and a
120  *              large prime %$p = kq + 1$% for some %$k$%, together with a
121  *              generator %$g$% of the cyclic subgroup of order %$q$%.  These
122  *              are actually the same as the DSA parameter set, but the
123  *              generation algorithm is different.  Also, if @ql@ is zero,
124  *              this algorithm forces %$k = 2$%, and chooses %$g = 4$%.  Make
125  *              sure you have something interesting to do if you choose this
126  *              option.
127  */
128
129 extern int dh_gen(dh_param */*dp*/, unsigned /*ql*/, unsigned /*pl*/,
130                   unsigned /*steps*/, grand */*r*/, pgen_proc */*event*/,
131                   void */*ectx*/);
132
133 /* --- @dh_limlee@ --- *
134  *
135  * Arguments:   @dh_param *dp@ = pointer to output parameter block
136  *              @unsigned ql@ = length of smallest factor of %$(p - 1)/2$%
137  *              @unsigned pl@ = length of %$p$% in bits
138  *              @unsigned flags@ = other generation flags
139  *              @unsigned steps@ = number of steps to go
140  *              @grand *r@ = random number source
141  *              @pgen_proc *oev@ = outer event handler function
142  *              @void *oec@ = argument for the outer event handler
143  *              @pgen_proc *iev@ = inner event handler function
144  *              @void *iec@ = argument for the inner event handler
145  *              @size_t *nf@, @mp ***f@ = output array for factors
146  *
147  * Returns:     @PGEN_DONE@ if it worked, @PGEN_ABORT@ if it didn't.
148  *
149  * Use:         Generates Diffie-Hellman parameters based on a Lim-Lee prime.
150  *
151  *              The modulus is a large prime %$p = 2 \prod q_i + 1$%, @pl@
152  *              bits long, where the %$q_i$% are smaller primes each at least
153  *              @ql@ bits long.  It is safe to set @nf@ and @f@ to zero if
154  *              you're not interested in the factor values.
155  *
156  *              The returned %$g$% generates a subgroup of order %$q_0$% (the
157  *              first factor, returned as @f[0]@), if the flag @DH_SUBGROUP@
158  *              is set on entry; otherwise %$g$% will have order
159  *              %$(p - 1)/2$%.
160  */
161
162 #define DH_SUBGROUP 1u
163
164 extern int dh_limlee(dh_param */*dp*/, unsigned /*ql*/, unsigned /*pl*/,
165                      unsigned /*flags*/, unsigned /*steps*/, grand */*r*/,
166                      pgen_proc */*oev*/, void */*oec*/, pgen_proc */*iev*/,
167                      void */*iec*/, size_t */*nf*/, mp ***/*f*/);
168
169 /* --- @dh_kcdsagen@ --- *
170  *
171  * Arguments:   @dh_param *dp@ = pointer to output parameter block
172  *              @unsigned ql@ = size of small factor of %$(p - 1)/2$%
173  *              @unsigned pl@ = size of %$p$% in bits
174  *              @unsigned flags@ = other generation flags (none defined)
175  *              @unsigned steps@ = number of steps to go
176  *              @grand *r@ = random number source
177  *              @pgen_proc *ev@ = event handler function
178  *              @void *ec@ = context for the event handler
179  *
180  * Returns:     @PGEN_DONE@ if it worked, @PGEN_ABORT@ if it failed.
181  *
182  * Use:         Generates a KCDSA prime group.  That is, it chooses a prime
183  *              %$p$%, such that $%p = 2 q v + 1$%, for primes %$q$% and
184  *              %$v$%.  The actual group of interest is the subgroup of order
185  *              %$q$%.
186  */
187
188 extern int dh_kcdsagen(dh_param */*dp*/, unsigned /*ql*/, unsigned /*pl*/,
189                        unsigned /*flags*/, unsigned /*steps*/, grand */*r*/,
190                        pgen_proc */*ev*/, void */*ec*/);
191
192 /* --- @dh_checkparam@ --- *
193  *
194  * Arguments:   @keycheck *kc@ = keycheck state
195  *              @const dh_param *dp@ = pointer to the parameter set
196  *              @mp **v@ = optional vector of factors
197  *              @size_t n@ = size of vector
198  *
199  * Returns:     Zero if all OK, or return status from function.
200  *
201  * Use:         Checks a set of Diffie-Hellman parameters for consistency and
202  *              security.
203  */
204
205 extern int dh_checkparam(keycheck */*kc*/, const dh_param */*dp*/,
206                          mp **/*v*/, size_t /*n*/);
207
208 /* ---- @dh_infofromdata@ --- *
209  *
210  * Arguments:   @dh_param *dp@ = parameters to fill in
211  *              @pdata *pd@ = packed data structure
212  *
213  * Returns:     ---
214  *
215  * Use:         Fills in a parameters structure from a packed data block.
216  */
217
218 struct pdata;
219 extern void dh_infofromdata(dh_param */*dp*/, struct pdata */*pd*/);
220
221 /* --- @dh_parse@, @dhbin_parse@ --- *
222  *
223  * Arguments:   @qd_parse *qd@ = parser context
224  *              @dh_param *dp@ = parameters to fill in
225  *
226  * Returns:     Zero if OK, nonzero on error.
227  *
228  * Use:         Parses a prime/binary group string.  This is either one of
229  *              the standard group strings, or a %$p$%, %$q$%, %$g$% triple
230  *              separated by commas.
231  */
232
233 extern int dh_parse(qd_parse */*qd*/, dh_param */*dp*/);
234 extern int dhbin_parse(qd_parse */*qd*/, gbin_param */*gb*/);
235
236 /*----- That's all, folks -------------------------------------------------*/
237
238 #ifdef __cplusplus
239   }
240 #endif
241
242 #endif