The default implementation of DSA_METHOD has an interdependence on the
[openssl.git] / crypto / dsa / dsa_ossl.c
1 /* crypto/dsa/dsa_ossl.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  * 
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  * 
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  * 
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from 
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  * 
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * 
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58
59 /* Original version from Steven Schoch <schoch@sheba.arc.nasa.gov> */
60
61 #include <stdio.h>
62 #include "cryptlib.h"
63 #include <openssl/bn.h>
64 #include <openssl/dsa.h>
65 #include <openssl/rand.h>
66 #include <openssl/asn1.h>
67 #ifndef OPENSSL_NO_ENGINE
68 #include <openssl/engine.h>
69 #endif
70
71 static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa);
72 static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp);
73 static int dsa_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig,
74                   DSA *dsa);
75 static int dsa_init(DSA *dsa);
76 static int dsa_finish(DSA *dsa);
77
78 static DSA_METHOD openssl_dsa_meth = {
79 "OpenSSL DSA method",
80 dsa_do_sign,
81 dsa_sign_setup,
82 dsa_do_verify,
83 NULL, /* dsa_mod_exp, */
84 NULL, /* dsa_bn_mod_exp, */
85 dsa_init,
86 dsa_finish,
87 0,
88 NULL,
89 NULL,
90 NULL
91 };
92
93 /* These macro wrappers replace attempts to use the dsa_mod_exp() and
94  * bn_mod_exp() handlers in the DSA_METHOD structure. We avoid the problem of
95  * having a the macro work as an expression by bundling an "err_instr". So;
96  * 
97  *     if (!dsa->meth->bn_mod_exp(dsa, r,dsa->g,&k,dsa->p,ctx,
98  *                 dsa->method_mont_p)) goto err;
99  *
100  * can be replaced by;
101  *
102  *     DSA_BN_MOD_EXP(goto err, dsa, r, dsa->g, &k, dsa->p, ctx,
103  *                 dsa->method_mont_p);
104  */
105
106 #define DSA_MOD_EXP(err_instr,dsa,rr,a1,p1,a2,p2,m,ctx,in_mont) \
107         do { \
108         int _tmp_res53; \
109         if((dsa)->meth->dsa_mod_exp) \
110                 _tmp_res53 = (dsa)->meth->dsa_mod_exp((dsa), (rr), (a1), (p1), \
111                                 (a2), (p2), (m), (ctx), (in_mont)); \
112         else \
113                 _tmp_res53 = BN_mod_exp2_mont((rr), (a1), (p1), (a2), (p2), \
114                                 (m), (ctx), (in_mont)); \
115         if(!_tmp_res53) err_instr; \
116         } while(0)
117 #define DSA_BN_MOD_EXP(err_instr,dsa,r,a,p,m,ctx,m_ctx) \
118         do { \
119         int _tmp_res53; \
120         if((dsa)->meth->bn_mod_exp) \
121                 _tmp_res53 = (dsa)->meth->bn_mod_exp((dsa), (r), (a), (p), \
122                                 (m), (ctx), (m_ctx)); \
123         else \
124                 _tmp_res53 = BN_mod_exp_mont((r), (a), (p), (m), (ctx), (m_ctx)); \
125         if(!_tmp_res53) err_instr; \
126         } while(0)
127
128 const DSA_METHOD *DSA_OpenSSL(void)
129 {
130         return &openssl_dsa_meth;
131 }
132
133 static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
134         {
135         BIGNUM *kinv=NULL,*r=NULL,*s=NULL;
136         BIGNUM m;
137         BIGNUM xr;
138         BN_CTX *ctx=NULL;
139         int i,reason=ERR_R_BN_LIB;
140         DSA_SIG *ret=NULL;
141
142         BN_init(&m);
143         BN_init(&xr);
144
145         if (!dsa->p || !dsa->q || !dsa->g)
146                 {
147                 reason=DSA_R_MISSING_PARAMETERS;
148                 goto err;
149                 }
150
151         s=BN_new();
152         if (s == NULL) goto err;
153
154         i=BN_num_bytes(dsa->q); /* should be 20 */
155         if ((dlen > i) || (dlen > 50))
156                 {
157                 reason=DSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE;
158                 goto err;
159                 }
160
161         ctx=BN_CTX_new();
162         if (ctx == NULL) goto err;
163
164         if ((dsa->kinv == NULL) || (dsa->r == NULL))
165                 {
166                 if (!DSA_sign_setup(dsa,ctx,&kinv,&r)) goto err;
167                 }
168         else
169                 {
170                 kinv=dsa->kinv;
171                 dsa->kinv=NULL;
172                 r=dsa->r;
173                 dsa->r=NULL;
174                 }
175
176         if (BN_bin2bn(dgst,dlen,&m) == NULL) goto err;
177
178         /* Compute  s = inv(k) (m + xr) mod q */
179         if (!BN_mod_mul(&xr,dsa->priv_key,r,dsa->q,ctx)) goto err;/* s = xr */
180         if (!BN_add(s, &xr, &m)) goto err;              /* s = m + xr */
181         if (BN_cmp(s,dsa->q) > 0)
182                 BN_sub(s,s,dsa->q);
183         if (!BN_mod_mul(s,s,kinv,dsa->q,ctx)) goto err;
184
185         ret=DSA_SIG_new();
186         if (ret == NULL) goto err;
187         ret->r = r;
188         ret->s = s;
189         
190 err:
191         if (!ret)
192                 {
193                 DSAerr(DSA_F_DSA_DO_SIGN,reason);
194                 BN_free(r);
195                 BN_free(s);
196                 }
197         if (ctx != NULL) BN_CTX_free(ctx);
198         BN_clear_free(&m);
199         BN_clear_free(&xr);
200         if (kinv != NULL) /* dsa->kinv is NULL now if we used it */
201             BN_clear_free(kinv);
202         return(ret);
203         }
204
205 static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp)
206         {
207         BN_CTX *ctx;
208         BIGNUM k,*kinv=NULL,*r=NULL;
209         int ret=0;
210
211         if (!dsa->p || !dsa->q || !dsa->g)
212                 {
213                 DSAerr(DSA_F_DSA_SIGN_SETUP,DSA_R_MISSING_PARAMETERS);
214                 return 0;
215                 }
216
217         BN_init(&k);
218
219         if (ctx_in == NULL)
220                 {
221                 if ((ctx=BN_CTX_new()) == NULL) goto err;
222                 }
223         else
224                 ctx=ctx_in;
225
226         if ((r=BN_new()) == NULL) goto err;
227         kinv=NULL;
228
229         /* Get random k */
230         do
231                 if (!BN_rand_range(&k, dsa->q)) goto err;
232         while (BN_is_zero(&k));
233
234         if ((dsa->method_mont_p == NULL) && (dsa->flags & DSA_FLAG_CACHE_MONT_P))
235                 {
236                 if ((dsa->method_mont_p=(char *)BN_MONT_CTX_new()) != NULL)
237                         if (!BN_MONT_CTX_set((BN_MONT_CTX *)dsa->method_mont_p,
238                                 dsa->p,ctx)) goto err;
239                 }
240
241         /* Compute r = (g^k mod p) mod q */
242         DSA_BN_MOD_EXP(goto err, dsa, r, dsa->g, &k, dsa->p, ctx,
243                         (BN_MONT_CTX *)dsa->method_mont_p);
244         if (!BN_mod(r,r,dsa->q,ctx)) goto err;
245
246         /* Compute  part of 's = inv(k) (m + xr) mod q' */
247         if ((kinv=BN_mod_inverse(NULL,&k,dsa->q,ctx)) == NULL) goto err;
248
249         if (*kinvp != NULL) BN_clear_free(*kinvp);
250         *kinvp=kinv;
251         kinv=NULL;
252         if (*rp != NULL) BN_clear_free(*rp);
253         *rp=r;
254         ret=1;
255 err:
256         if (!ret)
257                 {
258                 DSAerr(DSA_F_DSA_SIGN_SETUP,ERR_R_BN_LIB);
259                 if (kinv != NULL) BN_clear_free(kinv);
260                 if (r != NULL) BN_clear_free(r);
261                 }
262         if (ctx_in == NULL) BN_CTX_free(ctx);
263         if (kinv != NULL) BN_clear_free(kinv);
264         BN_clear_free(&k);
265         return(ret);
266         }
267
268 static int dsa_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig,
269                   DSA *dsa)
270         {
271         BN_CTX *ctx;
272         BIGNUM u1,u2,t1;
273         BN_MONT_CTX *mont=NULL;
274         int ret = -1;
275         if (!dsa->p || !dsa->q || !dsa->g)
276                 {
277                 DSAerr(DSA_F_DSA_DO_VERIFY,DSA_R_MISSING_PARAMETERS);
278                 return -1;
279                 }
280
281         BN_init(&u1);
282         BN_init(&u2);
283         BN_init(&t1);
284
285         if ((ctx=BN_CTX_new()) == NULL) goto err;
286
287         if (BN_is_zero(sig->r) || BN_get_sign(sig->r) ||
288             BN_ucmp(sig->r, dsa->q) >= 0)
289                 {
290                 ret = 0;
291                 goto err;
292                 }
293         if (BN_is_zero(sig->s) || BN_get_sign(sig->s) ||
294             BN_ucmp(sig->s, dsa->q) >= 0)
295                 {
296                 ret = 0;
297                 goto err;
298                 }
299
300         /* Calculate W = inv(S) mod Q
301          * save W in u2 */
302         if ((BN_mod_inverse(&u2,sig->s,dsa->q,ctx)) == NULL) goto err;
303
304         /* save M in u1 */
305         if (BN_bin2bn(dgst,dgst_len,&u1) == NULL) goto err;
306
307         /* u1 = M * w mod q */
308         if (!BN_mod_mul(&u1,&u1,&u2,dsa->q,ctx)) goto err;
309
310         /* u2 = r * w mod q */
311         if (!BN_mod_mul(&u2,sig->r,&u2,dsa->q,ctx)) goto err;
312
313         if ((dsa->method_mont_p == NULL) && (dsa->flags & DSA_FLAG_CACHE_MONT_P))
314                 {
315                 if ((dsa->method_mont_p=(char *)BN_MONT_CTX_new()) != NULL)
316                         if (!BN_MONT_CTX_set((BN_MONT_CTX *)dsa->method_mont_p,
317                                 dsa->p,ctx)) goto err;
318                 }
319         mont=(BN_MONT_CTX *)dsa->method_mont_p;
320
321
322         DSA_MOD_EXP(goto err, dsa, &t1, dsa->g, &u1, dsa->pub_key, &u2, dsa->p, ctx, mont);
323         /* BN_copy(&u1,&t1); */
324         /* let u1 = u1 mod q */
325         if (!BN_mod(&u1,&t1,dsa->q,ctx)) goto err;
326
327         /* V is now in u1.  If the signature is correct, it will be
328          * equal to R. */
329         ret=(BN_ucmp(&u1, sig->r) == 0);
330
331         err:
332         if (ret != 1) DSAerr(DSA_F_DSA_DO_VERIFY,ERR_R_BN_LIB);
333         if (ctx != NULL) BN_CTX_free(ctx);
334         BN_free(&u1);
335         BN_free(&u2);
336         BN_free(&t1);
337         return(ret);
338         }
339
340 static int dsa_init(DSA *dsa)
341 {
342         dsa->flags|=DSA_FLAG_CACHE_MONT_P;
343         return(1);
344 }
345
346 static int dsa_finish(DSA *dsa)
347 {
348         if(dsa->method_mont_p)
349                 BN_MONT_CTX_free((BN_MONT_CTX *)dsa->method_mont_p);
350         return(1);
351 }
352