3 * Digital Signature Algorithm
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 #ifndef CATACOMB_DSA_H
29 #define CATACOMB_DSA_H
35 /*----- Notes on the Digital Signature Algorithm --------------------------*
37 * The Digital Signature Algorithm was designed by the NSA for US Government
38 * use. It's defined in FIPS 186-1. Whether it's covered by patents is
39 * under dispute, although it looks relatively clear. It produces compact
40 * signatures, and is relatively easy to compute. It seems strong, if
41 * appropriate parameters are chosen.
44 /*----- Header files ------------------------------------------------------*/
50 #ifndef CATACOMB_GRAND_H
54 #ifndef CATACOMB_KEY_H
58 #ifndef CATACOMB_KEYCHECK_H
59 # include "keycheck.h"
66 #ifndef CATACOMB_PGEN_H
70 /*----- Data structures ---------------------------------------------------*/
72 /* --- The parameters and keys are the same as for Diffie-Hellman --- */
74 typedef dh_param dsa_param
;
75 typedef dh_pub dsa_pub
;
76 typedef dh_priv dsa_priv
;
78 /* --- DSA key seed structure --- */
80 typedef struct dsa_seed
{
81 void *p
; /* Pointer to seed material */
82 size_t sz
; /* Size of seed material */
83 unsigned count
; /* Iterations to find @p@ */
86 /* --- DSA signature structure --- *
88 * This is the recommended structure for a DSA signature. The actual signing
89 * function can cope with arbitrary-sized objects given appropriate
90 * parameters, however.
95 typedef struct dsa_sig
{
96 octet r
[DSA_SIGLEN
]; /* 160-bit @r@ value */
97 octet s
[DSA_SIGLEN
]; /* 160-bit @s@ value */
100 /*----- Key fetching ------------------------------------------------------*/
102 #define dsa_paramfetch dh_paramfetch
103 #define dsa_pubfetch dh_pubfetch
104 #define dsa_privfetch dh_privfetch
106 #define DSA_PARAMFETCHSZ DH_PARAMFETCHSZ
107 #define DSA_PUBFETCHSZ DH_PUBFETCHSZ
108 #define DSA_PRIVFETCHSZ DH_PRIVFETCHSZ
110 #define dsa_paramfree dh_paramfree
111 #define dsa_pubfree dh_pubfree
112 #define dsa_privfree dh_privfree
114 /*----- DSA stepper -------------------------------------------------------*/
116 typedef struct dsa_stepctx
{
118 /* --- To be initialized by the client --- */
120 grand
*r
; /* Random number generator */
121 mp
*q
; /* Force @p@ to be a multiple */
122 size_t bits
; /* Number of bits in the result */
123 unsigned or; /* OR mask for low order bits */
124 unsigned count
; /* Counts the number of steps made */
125 void *seedbuf
; /* Pointer to seed buffer */
128 /* --- @dsa_step@ --- *
130 * The stepper chooses random integers, ensures that they are a multiple of
131 * @q@ (if specified), sets the low-order bits, and then tests for
132 * divisibility by small primes.
135 extern pgen_proc dsa_step
;
137 /*----- Functions provided ------------------------------------------------*/
139 /* --- @dsa_gen@ --- *
141 * Arguments: @dsa_param *dp@ = where to store parameters
142 * @unsigned ql@ = length of @q@ in bits
143 * @unsigned pl@ = length of @p@ in bits
144 * @unsigned steps@ = number of steps to find @q@
145 * @const void *k@ = pointer to key material
146 * @size_t sz@ = size of key material
147 * @dsa_seed *sd@ = optional pointer for output seed information
148 * @pgen_proc *event@ = event handler function
149 * @void *ectx@ = argument for event handler
151 * Returns: @PGEN_DONE@ if everything worked ok; @PGEN_ABORT@ otherwise.
153 * Use: Generates the DSA shared parameters from a given seed value.
154 * This can take quite a long time.
156 * The algorithm used is a compatible extension of the method
157 * described in the DSA standard, FIPS 186. The standard
158 * requires that %$q$% be 160 bits in size (i.e., @ql == 160@)
159 * and that the length of %$p$% be %$L = 512 + 64l$% for some
160 * %$l$%. Neither limitation applies to this implementation.
163 extern int dsa_gen(dsa_param */
*dp*/
, unsigned /*ql*/, unsigned /*pl*/,
164 unsigned /*steps*/, const void */
*k*/
, size_t /*sz*/,
165 dsa_seed */
*sd*/
, pgen_proc */
*event*/
, void */
*ectx*/
);
167 /* --- @dsa_checkparam@ --- *
169 * Arguments: @keycheck *kc@ = keycheck state
170 * @const dsa_param *dp@ = pointer to the parameter set
171 * @const dsa_seed *ds@ = pointer to seed information
173 * Returns: Zero if all OK, or return status from function.
175 * Use: Checks a set of DSA parameters for consistency and security.
178 extern int dsa_checkparam(keycheck */
*kc*/
, const dsa_param */
*dp*/
,
179 const dsa_seed */
*ds*/
);
181 /* --- @dsa_h2n@ --- *
183 * Arguments: @mp *d@ = destination integer
184 * @mp *r@ = order of the DSA group
185 * @const void *h@ = pointer to message hash
186 * @size_t hsz@ = size (in bytes) of the hash output
188 * Returns: Resulting integer.
190 * Use: Converts a hash to an integer in the demented way necessary
191 * for DSA/ECDSA. This is, of course, completely insane, but
195 extern mp
*dsa_h2n(mp */
*d*/
, mp */
*r*/
, const void */
*h*/
, size_t /*hsz*/);
197 /* --- @dsa_nonce@ --- *
199 * Arguments: @mp *d@ = destination integer
200 * @mp *q@ = order of the DSA group
201 * @mp *x@ = secret key
202 * @const octet *m@ = message hash
203 * @const gchash *ch@ = hash class
204 * @grand *r@ = random bit source, or null
208 * Use: Generates a nonce for use in DSA (or another Fiat--Shamir
212 extern mp
*dsa_nonce(mp */
*d*/
, mp */
*q*/
, mp */
*x*/
, const octet */
*m*/
,
213 const gchash */
*ch*/
, grand */
*r*/
);
215 /* --- @dsa_mksig@ --- *
217 * Arguments: @const dsa_param *dp@ = pointer to DSA parameters
218 * @mp *a@ = secret signing key
219 * @mp *m@ = message to be signed
220 * @mp *k@ = random data
221 * @mp **rr, **ss@ = where to put output parameters
225 * Use: Computes a DSA signature of a message.
228 extern void dsa_mksig(const dsa_param */
*dp*/
, mp */
*a*/
,
229 mp */
*m*/
, mp */
*k*/
,
230 mp
**/
*rr*/
, mp
**/
*ss*/
);
232 /* --- @dsa_sign@ --- *
234 * Arguments: @dsa_param *dp@ = pointer to DSA parameters
235 * @mp *a@ = pointer to secret signing key
236 * @const void *m@ = pointer to message
237 * @size_t msz@ = size of the message
238 * @const void *k@ = secret random data for securing signature
239 * @size_t ksz@ = size of secret data
240 * @void *r@ = pointer to output space for @r@
241 * @size_t rsz@ = size of output space for @r@
242 * @void *s@ = pointer to output space for @s@
243 * @size_t ssz@ = size of output space for @s@
247 * Use: Signs a message, storing the results in a big-endian binary
251 extern void dsa_sign(dsa_param */
*dp*/
, mp */
*a*/
,
252 const void */
*m*/
, size_t /*msz*/,
253 const void */
*k*/
, size_t /*ksz*/,
254 void */
*r*/
, size_t /*rsz*/,
255 void */
*s*/
, size_t /*ssz*/);
257 /* --- @dsa_vrfy@ --- *
259 * Arguments: @const dsa_param *dp@ = pointer to DSA parameters
260 * @mp *y@ = public verification key
261 * @mp *m@ = message which was signed
262 * @mp *r, *s@ = the signature
264 * Returns: Zero if the signature is a forgery, nonzero if it's valid.
266 * Use: Verifies a DSA digital signature.
269 extern int dsa_vrfy(const dsa_param */
*dp*/
, mp */
*y*/
,
270 mp */
*m*/
, mp */
*r*/
, mp */
*s*/
);
272 /* --- @dsa_verify@ --- *
274 * Arguments: @const dsa_param *dp@ = pointer to DSA parameters
275 * @mp *y@ = public verification key
276 * @const void *m@ = pointer to message block
277 * @size_t msz@ = size of message block
278 * @const void *r@ = pointer to @r@ signature half
279 * @size_t rsz@ = size of @r@
280 * @const void *s@ = pointer to @s@ signature half
281 * @size_t ssz@ = size of @s@
283 * Returns: Zero if the signature is a forgery, nonzero if it's valid.
285 * Use: Verifies a DSA digital signature.
288 extern int dsa_verify(const dsa_param */
*dp*/
, mp */
*y*/
,
289 const void */
*m*/
, size_t /*msz*/,
290 const void */
*r*/
, size_t /*rsz*/,
291 const void */
*s*/
, size_t /*ssz*/);
293 /*----- That's all, folks -------------------------------------------------*/