3 * $Id: dh.h,v 1.9 2004/04/08 01:36:15 mdw Exp $
5 * Diffie-Hellman and related public-key systems
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,
37 /*----- Header files ------------------------------------------------------*/
39 #ifndef CATACOMB_GROUP_H
43 #ifndef CATACOMB_GRAND_H
47 #ifndef CATACOMB_KEY_H
51 #ifndef CATACOMB_KEYCHECK_H
52 # include "keycheck.h"
55 #ifndef CATACOMB_PGEN_H
59 #ifndef CATACOMB_QDPARSE_H
63 /*----- Data structures ---------------------------------------------------*/
65 typedef gprime_param dh_param
; /* Group parameters */
67 typedef struct dh_pub
{
68 dh_param dp
; /* Shared parameters */
69 mp
*y
; /* Public key */
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}$% */
78 /*----- Key fetching ------------------------------------------------------*/
80 extern const key_fetchdef dh_paramfetch
[];
81 #define DH_PARAMFETCHSZ 5
83 extern const key_fetchdef dh_pubfetch
[];
84 #define DH_PUBFETCHSZ 6
86 extern const key_fetchdef dh_privfetch
[];
87 #define DH_PRIVFETCHSZ 9
89 /* --- @dh_paramfree@, @dh_pubfree@, @dh_privfree@ --- *
91 * Arguments: @dh_param *dp@, @dh_pub *dp@, @dh_priv *dp@ = pointer to
96 * Use: Frees a Diffie-Hellman key block.
99 extern void dh_paramfree(dh_param */
*dp*/
);
100 extern void dh_pubfree(dh_pub */
*dp*/
);
101 extern void dh_privfree(dh_priv */
*dp*/
);
103 /*----- Functions provided ------------------------------------------------*/
105 /* --- @dh_gen@ --- *
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
115 * Returns: @PGEN_DONE@ if it worked, @PGEN_ABORT@ if it didn't.
117 * Use: Generates Diffie-Hellman parameters.
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
129 extern int dh_gen(dh_param */
*dp*/
, unsigned /*ql*/, unsigned /*pl*/,
130 unsigned /*steps*/, grand */
*r*/
, pgen_proc */
*event*/
,
133 /* --- @dh_limlee@ --- *
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
147 * Returns: @PGEN_DONE@ if it worked, @PGEN_ABORT@ if it didn't.
149 * Use: Generates Diffie-Hellman parameters based on a Lim-Lee prime.
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.
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
162 #define DH_SUBGROUP 1u
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*/
);
169 /* --- @dh_checkparam@ --- *
171 * Arguments: @keycheck *kc@ = keycheck state
172 * @const dh_param *dp@ = pointer to the parameter set
173 * @mp **v@ = optional vector of factors
174 * @size_t n@ = size of vector
176 * Returns: Zero if all OK, or return status from function.
178 * Use: Checks a set of Diffie-Hellman parameters for consistency and
182 extern int dh_checkparam(keycheck */
*kc*/
, const dh_param */
*dp*/
,
183 mp
**/
*v*/
, size_t /*n*/);
185 /* --- @dh_parse@ --- *
187 * Arguments: @qd_parse *qd@ = parser context
188 * @dh_param *dp@ = parameters to fill in
190 * Returns: Zero if OK, nonzero on error.
192 * Use: Parses a prime group string. This is either one of the
193 * standard group strings, or a %$p$%, %$q$%, %$g$% triple
194 * separated by commas.
197 extern int dh_parse(qd_parse */
*qd*/
, dh_param */
*dp*/
);
199 /*----- That's all, folks -------------------------------------------------*/