Commit | Line | Data |
---|---|---|
8b810a45 | 1 | /* -*-c-*- |
2 | * | |
8b810a45 | 3 | * Digital Signature Algorithm |
4 | * | |
5 | * (c) 1999 Straylight/Edgeware | |
6 | */ | |
7 | ||
45c0fd36 | 8 | /*----- Licensing notice --------------------------------------------------* |
8b810a45 | 9 | * |
10 | * This file is part of Catacomb. | |
11 | * | |
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. | |
45c0fd36 | 16 | * |
8b810a45 | 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. | |
45c0fd36 | 21 | * |
8b810a45 | 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, | |
25 | * MA 02111-1307, USA. | |
26 | */ | |
27 | ||
b3f05084 | 28 | #ifndef CATACOMB_DSA_H |
29 | #define CATACOMB_DSA_H | |
8b810a45 | 30 | |
31 | #ifdef __cplusplus | |
32 | extern "C" { | |
33 | #endif | |
34 | ||
35 | /*----- Notes on the Digital Signature Algorithm --------------------------* | |
36 | * | |
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. | |
42 | */ | |
43 | ||
44 | /*----- Header files ------------------------------------------------------*/ | |
45 | ||
827e6c99 | 46 | #ifndef CATACOMB_DH_H |
47 | # include "dh.h" | |
48 | #endif | |
49 | ||
aa00f6fb MW |
50 | #ifndef CATACOMB_GRAND_H |
51 | # include "grand.h" | |
52 | #endif | |
53 | ||
80a2ff16 | 54 | #ifndef CATACOMB_KEY_H |
55 | # include "key.h" | |
56 | #endif | |
57 | ||
600127f0 | 58 | #ifndef CATACOMB_KEYCHECK_H |
59 | # include "keycheck.h" | |
60 | #endif | |
61 | ||
b3f05084 | 62 | #ifndef CATACOMB_MP_H |
8b810a45 | 63 | # include "mp.h" |
64 | #endif | |
45c0fd36 | 65 | |
b04a7659 | 66 | #ifndef CATACOMB_PGEN_H |
67 | # include "pgen.h" | |
68 | #endif | |
8b810a45 | 69 | |
70 | /*----- Data structures ---------------------------------------------------*/ | |
71 | ||
827e6c99 | 72 | /* --- The parameters and keys are the same as for Diffie-Hellman --- */ |
80a2ff16 | 73 | |
827e6c99 | 74 | typedef dh_param dsa_param; |
75 | typedef dh_pub dsa_pub; | |
76 | typedef dh_priv dsa_priv; | |
80a2ff16 | 77 | |
600127f0 | 78 | /* --- DSA key seed structure --- */ |
79 | ||
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@ */ | |
84 | } dsa_seed; | |
85 | ||
8b810a45 | 86 | /* --- DSA signature structure --- * |
87 | * | |
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. | |
91 | */ | |
92 | ||
93 | #define DSA_SIGLEN 20 | |
94 | ||
95 | typedef struct dsa_sig { | |
96 | octet r[DSA_SIGLEN]; /* 160-bit @r@ value */ | |
97 | octet s[DSA_SIGLEN]; /* 160-bit @s@ value */ | |
98 | } dsa_sig; | |
99 | ||
80a2ff16 | 100 | /*----- Key fetching ------------------------------------------------------*/ |
101 | ||
827e6c99 | 102 | #define dsa_paramfetch dh_paramfetch |
103 | #define dsa_pubfetch dh_pubfetch | |
104 | #define dsa_privfetch dh_privfetch | |
80a2ff16 | 105 | |
827e6c99 | 106 | #define DSA_PARAMFETCHSZ DH_PARAMFETCHSZ |
107 | #define DSA_PUBFETCHSZ DH_PUBFETCHSZ | |
108 | #define DSA_PRIVFETCHSZ DH_PRIVFETCHSZ | |
b92da8eb | 109 | |
827e6c99 | 110 | #define dsa_paramfree dh_paramfree |
111 | #define dsa_pubfree dh_pubfree | |
112 | #define dsa_privfree dh_privfree | |
b92da8eb | 113 | |
b04a7659 | 114 | /*----- DSA stepper -------------------------------------------------------*/ |
115 | ||
116 | typedef struct dsa_stepctx { | |
117 | ||
118 | /* --- To be initialized by the client --- */ | |
119 | ||
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 */ | |
600127f0 | 124 | unsigned count; /* Counts the number of steps made */ |
125 | void *seedbuf; /* Pointer to seed buffer */ | |
b04a7659 | 126 | } dsa_stepctx; |
127 | ||
128 | /* --- @dsa_step@ --- * | |
129 | * | |
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. | |
133 | */ | |
134 | ||
ab6ce636 | 135 | extern pgen_proc dsa_step; |
b04a7659 | 136 | |
8b810a45 | 137 | /*----- Functions provided ------------------------------------------------*/ |
138 | ||
827e6c99 | 139 | /* --- @dsa_gen@ --- * |
8b810a45 | 140 | * |
141 | * Arguments: @dsa_param *dp@ = where to store parameters | |
b04a7659 | 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@ | |
8b810a45 | 145 | * @const void *k@ = pointer to key material |
146 | * @size_t sz@ = size of key material | |
600127f0 | 147 | * @dsa_seed *sd@ = optional pointer for output seed information |
b04a7659 | 148 | * @pgen_proc *event@ = event handler function |
149 | * @void *ectx@ = argument for event handler | |
8b810a45 | 150 | * |
b04a7659 | 151 | * Returns: @PGEN_DONE@ if everything worked ok; @PGEN_ABORT@ otherwise. |
8b810a45 | 152 | * |
153 | * Use: Generates the DSA shared parameters from a given seed value. | |
b04a7659 | 154 | * This can take quite a long time. |
155 | * | |
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. | |
8b810a45 | 161 | */ |
162 | ||
827e6c99 | 163 | extern int dsa_gen(dsa_param */*dp*/, unsigned /*ql*/, unsigned /*pl*/, |
164 | unsigned /*steps*/, const void */*k*/, size_t /*sz*/, | |
600127f0 | 165 | dsa_seed */*sd*/, pgen_proc */*event*/, void */*ectx*/); |
166 | ||
167 | /* --- @dsa_checkparam@ --- * | |
168 | * | |
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 | |
172 | * | |
173 | * Returns: Zero if all OK, or return status from function. | |
174 | * | |
175 | * Use: Checks a set of DSA parameters for consistency and security. | |
176 | */ | |
177 | ||
178 | extern int dsa_checkparam(keycheck */*kc*/, const dsa_param */*dp*/, | |
179 | const dsa_seed */*ds*/); | |
8b810a45 | 180 | |
c97fbcf9 MW |
181 | /* --- @dsa_h2n@ --- * |
182 | * | |
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 | |
187 | * | |
188 | * Returns: Resulting integer. | |
189 | * | |
190 | * Use: Converts a hash to an integer in the demented way necessary | |
191 | * for DSA/ECDSA. This is, of course, completely insane, but | |
192 | * there you go. | |
193 | */ | |
194 | ||
195 | extern mp *dsa_h2n(mp */*d*/, mp */*r*/, const void */*h*/, size_t /*hsz*/); | |
196 | ||
77fdf13a MW |
197 | /* --- @dsa_nonce@ --- * |
198 | * | |
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 | |
49a7bf52 | 203 | * @const gchash *ch@ = hash class |
77fdf13a MW |
204 | * @grand *r@ = random bit source, or null |
205 | * | |
206 | * Returns: A nonce. | |
207 | * | |
208 | * Use: Generates a nonce for use in DSA (or another Fiat--Shamir | |
209 | * signature scheme). | |
210 | */ | |
211 | ||
212 | extern mp *dsa_nonce(mp */*d*/, mp */*q*/, mp */*x*/, const octet */*m*/, | |
213 | const gchash */*ch*/, grand */*r*/); | |
214 | ||
8b810a45 | 215 | /* --- @dsa_mksig@ --- * |
216 | * | |
217 | * Arguments: @const dsa_param *dp@ = pointer to DSA parameters | |
b3f05084 | 218 | * @mp *a@ = secret signing key |
219 | * @mp *m@ = message to be signed | |
220 | * @mp *k@ = random data | |
8b810a45 | 221 | * @mp **rr, **ss@ = where to put output parameters |
222 | * | |
223 | * Returns: --- | |
224 | * | |
225 | * Use: Computes a DSA signature of a message. | |
226 | */ | |
227 | ||
b3f05084 | 228 | extern void dsa_mksig(const dsa_param */*dp*/, mp */*a*/, |
229 | mp */*m*/, mp */*k*/, | |
8b810a45 | 230 | mp **/*rr*/, mp **/*ss*/); |
231 | ||
232 | /* --- @dsa_sign@ --- * | |
233 | * | |
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@ | |
244 | * | |
245 | * Returns: --- | |
246 | * | |
247 | * Use: Signs a message, storing the results in a big-endian binary | |
248 | * form. | |
249 | */ | |
250 | ||
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*/); | |
256 | ||
257 | /* --- @dsa_vrfy@ --- * | |
258 | * | |
259 | * Arguments: @const dsa_param *dp@ = pointer to DSA parameters | |
b3f05084 | 260 | * @mp *y@ = public verification key |
261 | * @mp *m@ = message which was signed | |
262 | * @mp *r, *s@ = the signature | |
8b810a45 | 263 | * |
264 | * Returns: Zero if the signature is a forgery, nonzero if it's valid. | |
265 | * | |
266 | * Use: Verifies a DSA digital signature. | |
267 | */ | |
268 | ||
b3f05084 | 269 | extern int dsa_vrfy(const dsa_param */*dp*/, mp */*y*/, |
270 | mp */*m*/, mp */*r*/, mp */*s*/); | |
8b810a45 | 271 | |
272 | /* --- @dsa_verify@ --- * | |
273 | * | |
274 | * Arguments: @const dsa_param *dp@ = pointer to DSA parameters | |
b3f05084 | 275 | * @mp *y@ = public verification key |
8b810a45 | 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@ | |
282 | * | |
283 | * Returns: Zero if the signature is a forgery, nonzero if it's valid. | |
284 | * | |
285 | * Use: Verifies a DSA digital signature. | |
286 | */ | |
287 | ||
b3f05084 | 288 | extern int dsa_verify(const dsa_param */*dp*/, mp */*y*/, |
b92da8eb | 289 | const void */*m*/, size_t /*msz*/, |
290 | const void */*r*/, size_t /*rsz*/, | |
291 | const void */*s*/, size_t /*ssz*/); | |
8b810a45 | 292 | |
293 | /*----- That's all, folks -------------------------------------------------*/ | |
294 | ||
295 | #ifdef __cplusplus | |
296 | } | |
297 | #endif | |
298 | ||
299 | #endif |