ct.c, ct.h: New constant-time operations.
[u/mdw/catacomb] / dsa.h
CommitLineData
8b810a45 1/* -*-c-*-
2 *
b817bfc6 3 * $Id: dsa.h,v 1.9 2004/04/08 01:36:15 mdw Exp $
8b810a45 4 *
5 * Digital Signature Algorithm
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
45c0fd36 10/*----- Licensing notice --------------------------------------------------*
8b810a45 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.
45c0fd36 18 *
8b810a45 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.
45c0fd36 23 *
8b810a45 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_DSA_H
31#define CATACOMB_DSA_H
8b810a45 32
33#ifdef __cplusplus
34 extern "C" {
35#endif
36
37/*----- Notes on the Digital Signature Algorithm --------------------------*
38 *
39 * The Digital Signature Algorithm was designed by the NSA for US Government
40 * use. It's defined in FIPS 186-1. Whether it's covered by patents is
41 * under dispute, although it looks relatively clear. It produces compact
42 * signatures, and is relatively easy to compute. It seems strong, if
43 * appropriate parameters are chosen.
44 */
45
46/*----- Header files ------------------------------------------------------*/
47
827e6c99 48#ifndef CATACOMB_DH_H
49# include "dh.h"
50#endif
51
80a2ff16 52#ifndef CATACOMB_KEY_H
53# include "key.h"
54#endif
55
600127f0 56#ifndef CATACOMB_KEYCHECK_H
57# include "keycheck.h"
58#endif
59
b3f05084 60#ifndef CATACOMB_MP_H
8b810a45 61# include "mp.h"
62#endif
45c0fd36 63
b04a7659 64#ifndef CATACOMB_PGEN_H
65# include "pgen.h"
66#endif
8b810a45 67
68/*----- Data structures ---------------------------------------------------*/
69
827e6c99 70/* --- The parameters and keys are the same as for Diffie-Hellman --- */
80a2ff16 71
827e6c99 72typedef dh_param dsa_param;
73typedef dh_pub dsa_pub;
74typedef dh_priv dsa_priv;
80a2ff16 75
600127f0 76/* --- DSA key seed structure --- */
77
78typedef struct dsa_seed {
79 void *p; /* Pointer to seed material */
80 size_t sz; /* Size of seed material */
81 unsigned count; /* Iterations to find @p@ */
82} dsa_seed;
83
8b810a45 84/* --- DSA signature structure --- *
85 *
86 * This is the recommended structure for a DSA signature. The actual signing
87 * function can cope with arbitrary-sized objects given appropriate
88 * parameters, however.
89 */
90
91#define DSA_SIGLEN 20
92
93typedef struct dsa_sig {
94 octet r[DSA_SIGLEN]; /* 160-bit @r@ value */
95 octet s[DSA_SIGLEN]; /* 160-bit @s@ value */
96} dsa_sig;
97
80a2ff16 98/*----- Key fetching ------------------------------------------------------*/
99
827e6c99 100#define dsa_paramfetch dh_paramfetch
101#define dsa_pubfetch dh_pubfetch
102#define dsa_privfetch dh_privfetch
80a2ff16 103
827e6c99 104#define DSA_PARAMFETCHSZ DH_PARAMFETCHSZ
105#define DSA_PUBFETCHSZ DH_PUBFETCHSZ
106#define DSA_PRIVFETCHSZ DH_PRIVFETCHSZ
b92da8eb 107
827e6c99 108#define dsa_paramfree dh_paramfree
109#define dsa_pubfree dh_pubfree
110#define dsa_privfree dh_privfree
b92da8eb 111
b04a7659 112/*----- DSA stepper -------------------------------------------------------*/
113
114typedef struct dsa_stepctx {
115
116 /* --- To be initialized by the client --- */
117
118 grand *r; /* Random number generator */
119 mp *q; /* Force @p@ to be a multiple */
120 size_t bits; /* Number of bits in the result */
121 unsigned or; /* OR mask for low order bits */
600127f0 122 unsigned count; /* Counts the number of steps made */
123 void *seedbuf; /* Pointer to seed buffer */
b04a7659 124} dsa_stepctx;
125
126/* --- @dsa_step@ --- *
127 *
128 * The stepper chooses random integers, ensures that they are a multiple of
129 * @q@ (if specified), sets the low-order bits, and then tests for
130 * divisibility by small primes.
131 */
132
ab6ce636 133extern pgen_proc dsa_step;
b04a7659 134
8b810a45 135/*----- Functions provided ------------------------------------------------*/
136
827e6c99 137/* --- @dsa_gen@ --- *
8b810a45 138 *
139 * Arguments: @dsa_param *dp@ = where to store parameters
b04a7659 140 * @unsigned ql@ = length of @q@ in bits
141 * @unsigned pl@ = length of @p@ in bits
142 * @unsigned steps@ = number of steps to find @q@
8b810a45 143 * @const void *k@ = pointer to key material
144 * @size_t sz@ = size of key material
600127f0 145 * @dsa_seed *sd@ = optional pointer for output seed information
b04a7659 146 * @pgen_proc *event@ = event handler function
147 * @void *ectx@ = argument for event handler
8b810a45 148 *
b04a7659 149 * Returns: @PGEN_DONE@ if everything worked ok; @PGEN_ABORT@ otherwise.
8b810a45 150 *
151 * Use: Generates the DSA shared parameters from a given seed value.
b04a7659 152 * This can take quite a long time.
153 *
154 * The algorithm used is a compatible extension of the method
155 * described in the DSA standard, FIPS 186. The standard
156 * requires that %$q$% be 160 bits in size (i.e., @ql == 160@)
157 * and that the length of %$p$% be %$L = 512 + 64l$% for some
158 * %$l$%. Neither limitation applies to this implementation.
8b810a45 159 */
160
827e6c99 161extern int dsa_gen(dsa_param */*dp*/, unsigned /*ql*/, unsigned /*pl*/,
162 unsigned /*steps*/, const void */*k*/, size_t /*sz*/,
600127f0 163 dsa_seed */*sd*/, pgen_proc */*event*/, void */*ectx*/);
164
165/* --- @dsa_checkparam@ --- *
166 *
167 * Arguments: @keycheck *kc@ = keycheck state
168 * @const dsa_param *dp@ = pointer to the parameter set
169 * @const dsa_seed *ds@ = pointer to seed information
170 *
171 * Returns: Zero if all OK, or return status from function.
172 *
173 * Use: Checks a set of DSA parameters for consistency and security.
174 */
175
176extern int dsa_checkparam(keycheck */*kc*/, const dsa_param */*dp*/,
177 const dsa_seed */*ds*/);
8b810a45 178
c97fbcf9
MW
179/* --- @dsa_h2n@ --- *
180 *
181 * Arguments: @mp *d@ = destination integer
182 * @mp *r@ = order of the DSA group
183 * @const void *h@ = pointer to message hash
184 * @size_t hsz@ = size (in bytes) of the hash output
185 *
186 * Returns: Resulting integer.
187 *
188 * Use: Converts a hash to an integer in the demented way necessary
189 * for DSA/ECDSA. This is, of course, completely insane, but
190 * there you go.
191 */
192
193extern mp *dsa_h2n(mp */*d*/, mp */*r*/, const void */*h*/, size_t /*hsz*/);
194
8b810a45 195/* --- @dsa_mksig@ --- *
196 *
197 * Arguments: @const dsa_param *dp@ = pointer to DSA parameters
b3f05084 198 * @mp *a@ = secret signing key
199 * @mp *m@ = message to be signed
200 * @mp *k@ = random data
8b810a45 201 * @mp **rr, **ss@ = where to put output parameters
202 *
203 * Returns: ---
204 *
205 * Use: Computes a DSA signature of a message.
206 */
207
b3f05084 208extern void dsa_mksig(const dsa_param */*dp*/, mp */*a*/,
209 mp */*m*/, mp */*k*/,
8b810a45 210 mp **/*rr*/, mp **/*ss*/);
211
212/* --- @dsa_sign@ --- *
213 *
214 * Arguments: @dsa_param *dp@ = pointer to DSA parameters
215 * @mp *a@ = pointer to secret signing key
216 * @const void *m@ = pointer to message
217 * @size_t msz@ = size of the message
218 * @const void *k@ = secret random data for securing signature
219 * @size_t ksz@ = size of secret data
220 * @void *r@ = pointer to output space for @r@
221 * @size_t rsz@ = size of output space for @r@
222 * @void *s@ = pointer to output space for @s@
223 * @size_t ssz@ = size of output space for @s@
224 *
225 * Returns: ---
226 *
227 * Use: Signs a message, storing the results in a big-endian binary
228 * form.
229 */
230
231extern void dsa_sign(dsa_param */*dp*/, mp */*a*/,
232 const void */*m*/, size_t /*msz*/,
233 const void */*k*/, size_t /*ksz*/,
234 void */*r*/, size_t /*rsz*/,
235 void */*s*/, size_t /*ssz*/);
236
237/* --- @dsa_vrfy@ --- *
238 *
239 * Arguments: @const dsa_param *dp@ = pointer to DSA parameters
b3f05084 240 * @mp *y@ = public verification key
241 * @mp *m@ = message which was signed
242 * @mp *r, *s@ = the signature
8b810a45 243 *
244 * Returns: Zero if the signature is a forgery, nonzero if it's valid.
245 *
246 * Use: Verifies a DSA digital signature.
247 */
248
b3f05084 249extern int dsa_vrfy(const dsa_param */*dp*/, mp */*y*/,
250 mp */*m*/, mp */*r*/, mp */*s*/);
8b810a45 251
252/* --- @dsa_verify@ --- *
253 *
254 * Arguments: @const dsa_param *dp@ = pointer to DSA parameters
b3f05084 255 * @mp *y@ = public verification key
8b810a45 256 * @const void *m@ = pointer to message block
257 * @size_t msz@ = size of message block
258 * @const void *r@ = pointer to @r@ signature half
259 * @size_t rsz@ = size of @r@
260 * @const void *s@ = pointer to @s@ signature half
261 * @size_t ssz@ = size of @s@
262 *
263 * Returns: Zero if the signature is a forgery, nonzero if it's valid.
264 *
265 * Use: Verifies a DSA digital signature.
266 */
267
b3f05084 268extern int dsa_verify(const dsa_param */*dp*/, mp */*y*/,
b92da8eb 269 const void */*m*/, size_t /*msz*/,
270 const void */*r*/, size_t /*rsz*/,
271 const void */*s*/, size_t /*ssz*/);
8b810a45 272
273/*----- That's all, folks -------------------------------------------------*/
274
275#ifdef __cplusplus
276 }
277#endif
278
279#endif