Expunge revision histories in files.
[u/mdw/catacomb] / dh.h
CommitLineData
44c240ee 1/* -*-c-*-
2 *
b817bfc6 3 * $Id: dh.h,v 1.9 2004/04/08 01:36:15 mdw Exp $
052b36d0 4 *
5 * Diffie-Hellman and related public-key systems
44c240ee 6 *
44c240ee 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
b3f05084 30#ifndef CATACOMB_DH_H
31#define CATACOMB_DH_H
44c240ee 32
33#ifdef __cplusplus
34 extern "C" {
35#endif
36
37/*----- Header files ------------------------------------------------------*/
38
34e4f738 39#ifndef CATACOMB_GROUP_H
40# include "group.h"
41#endif
42
052b36d0 43#ifndef CATACOMB_GRAND_H
44# include "grand.h"
44c240ee 45#endif
46
e23471a7 47#ifndef CATACOMB_KEY_H
48# include "key.h"
49#endif
50
600127f0 51#ifndef CATACOMB_KEYCHECK_H
52# include "keycheck.h"
53#endif
54
052b36d0 55#ifndef CATACOMB_PGEN_H
56# include "pgen.h"
57#endif
44c240ee 58
34e4f738 59#ifndef CATACOMB_QDPARSE_H
60# include "qdparse.h"
61#endif
62
052b36d0 63/*----- Data structures ---------------------------------------------------*/
44c240ee 64
34e4f738 65typedef gprime_param dh_param; /* Group parameters */
44c240ee 66
600127f0 67typedef struct dh_pub {
68 dh_param dp; /* Shared parameters */
69 mp *y; /* Public key */
e23471a7 70} dh_pub;
71
600127f0 72typedef struct dh_priv {
73 dh_param dp; /* Shared parameters */
74 mp *x; /* Private key */
75 mp *y; /* %$y \equiv g^x \pmod{p}$% */
e23471a7 76} dh_priv;
77
78/*----- Key fetching ------------------------------------------------------*/
79
80extern const key_fetchdef dh_paramfetch[];
81#define DH_PARAMFETCHSZ 5
82
83extern const key_fetchdef dh_pubfetch[];
84#define DH_PUBFETCHSZ 6
85
86extern const key_fetchdef dh_privfetch[];
87#define DH_PRIVFETCHSZ 9
88
b92da8eb 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
99extern void dh_paramfree(dh_param */*dp*/);
100extern void dh_pubfree(dh_pub */*dp*/);
101extern void dh_privfree(dh_priv */*dp*/);
102
44c240ee 103/*----- Functions provided ------------------------------------------------*/
104
052b36d0 105/* --- @dh_gen@ --- *
44c240ee 106 *
052b36d0 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
44c240ee 114 *
052b36d0 115 * Returns: @PGEN_DONE@ if it worked, @PGEN_ABORT@ if it didn't.
44c240ee 116 *
052b36d0 117 * Use: Generates Diffie-Hellman parameters.
44c240ee 118 *
052b36d0 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.
44c240ee 127 */
128
052b36d0 129extern int dh_gen(dh_param */*dp*/, unsigned /*ql*/, unsigned /*pl*/,
130 unsigned /*steps*/, grand */*r*/, pgen_proc */*event*/,
131 void */*ectx*/);
44c240ee 132
1269467c 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
164extern 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
600127f0 169/* --- @dh_checkparam@ --- *
170 *
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
175 *
176 * Returns: Zero if all OK, or return status from function.
177 *
178 * Use: Checks a set of Diffie-Hellman parameters for consistency and
179 * security.
180 */
181
182extern int dh_checkparam(keycheck */*kc*/, const dh_param */*dp*/,
183 mp **/*v*/, size_t /*n*/);
184
34e4f738 185/* --- @dh_parse@ --- *
186 *
187 * Arguments: @qd_parse *qd@ = parser context
188 * @dh_param *dp@ = parameters to fill in
189 *
190 * Returns: Zero if OK, nonzero on error.
191 *
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.
195 */
196
197extern int dh_parse(qd_parse */*qd*/, dh_param */*dp*/);
198
44c240ee 199/*----- That's all, folks -------------------------------------------------*/
200
201#ifdef __cplusplus
202 }
203#endif
204
205#endif