Make default_method mostly compile-time
[openssl.git] / crypto / dsa / dsa_ossl.c
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 /* Original version from Steven Schoch <schoch@sheba.arc.nasa.gov> */
11
12 #include <stdio.h>
13 #include "internal/cryptlib.h"
14 #include <openssl/bn.h>
15 #include <openssl/sha.h>
16 #include "dsa_locl.h"
17 #include <openssl/asn1.h>
18
19 static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa);
20 static int dsa_sign_setup_no_digest(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp,
21                                     BIGNUM **rp);
22 static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp,
23                           BIGNUM **rp, const unsigned char *dgst, int dlen);
24 static int dsa_do_verify(const unsigned char *dgst, int dgst_len,
25                          DSA_SIG *sig, DSA *dsa);
26 static int dsa_init(DSA *dsa);
27 static int dsa_finish(DSA *dsa);
28
29 static DSA_METHOD openssl_dsa_meth = {
30     "OpenSSL DSA method",
31     dsa_do_sign,
32     dsa_sign_setup_no_digest,
33     dsa_do_verify,
34     NULL,                       /* dsa_mod_exp, */
35     NULL,                       /* dsa_bn_mod_exp, */
36     dsa_init,
37     dsa_finish,
38     DSA_FLAG_FIPS_METHOD,
39     NULL,
40     NULL,
41     NULL
42 };
43
44 static const DSA_METHOD *default_DSA_method = &openssl_dsa_meth;
45
46 void DSA_set_default_method(const DSA_METHOD *meth)
47 {
48     default_DSA_method = meth;
49 }
50
51 const DSA_METHOD *DSA_get_default_method(void)
52 {
53     return default_DSA_method;
54 }
55
56 const DSA_METHOD *DSA_OpenSSL(void)
57 {
58     return &openssl_dsa_meth;
59 }
60
61 static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
62 {
63     BIGNUM *kinv = NULL;
64     BIGNUM *m;
65     BIGNUM *xr;
66     BN_CTX *ctx = NULL;
67     int reason = ERR_R_BN_LIB;
68     DSA_SIG *ret = NULL;
69     int rv = 0;
70
71     m = BN_new();
72     xr = BN_new();
73     if (m == NULL || xr == NULL)
74         goto err;
75
76     if (!dsa->p || !dsa->q || !dsa->g) {
77         reason = DSA_R_MISSING_PARAMETERS;
78         goto err;
79     }
80
81     ret = DSA_SIG_new();
82     if (ret == NULL)
83         goto err;
84     ret->r = BN_new();
85     ret->s = BN_new();
86     if (ret->r == NULL || ret->s == NULL)
87         goto err;
88
89     ctx = BN_CTX_new();
90     if (ctx == NULL)
91         goto err;
92  redo:
93     if (!dsa_sign_setup(dsa, ctx, &kinv, &ret->r, dgst, dlen))
94         goto err;
95
96     if (dlen > BN_num_bytes(dsa->q))
97         /*
98          * if the digest length is greater than the size of q use the
99          * BN_num_bits(dsa->q) leftmost bits of the digest, see fips 186-3,
100          * 4.2
101          */
102         dlen = BN_num_bytes(dsa->q);
103     if (BN_bin2bn(dgst, dlen, m) == NULL)
104         goto err;
105
106     /* Compute  s = inv(k) (m + xr) mod q */
107     if (!BN_mod_mul(xr, dsa->priv_key, ret->r, dsa->q, ctx))
108         goto err;               /* s = xr */
109     if (!BN_add(ret->s, xr, m))
110         goto err;               /* s = m + xr */
111     if (BN_cmp(ret->s, dsa->q) > 0)
112         if (!BN_sub(ret->s, ret->s, dsa->q))
113             goto err;
114     if (!BN_mod_mul(ret->s, ret->s, kinv, dsa->q, ctx))
115         goto err;
116
117     /*
118      * Redo if r or s is zero as required by FIPS 186-3: this is very
119      * unlikely.
120      */
121     if (BN_is_zero(ret->r) || BN_is_zero(ret->s))
122         goto redo;
123
124     rv = 1;
125
126  err:
127     if (rv == 0) {
128         DSAerr(DSA_F_DSA_DO_SIGN, reason);
129         DSA_SIG_free(ret);
130         ret = NULL;
131     }
132     BN_CTX_free(ctx);
133     BN_clear_free(m);
134     BN_clear_free(xr);
135     BN_clear_free(kinv);
136     return ret;
137 }
138
139 static int dsa_sign_setup_no_digest(DSA *dsa, BN_CTX *ctx_in,
140                                     BIGNUM **kinvp, BIGNUM **rp)
141 {
142     return dsa_sign_setup(dsa, ctx_in, kinvp, rp, NULL, 0);
143 }
144
145 static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in,
146                           BIGNUM **kinvp, BIGNUM **rp,
147                           const unsigned char *dgst, int dlen)
148 {
149     BN_CTX *ctx = NULL;
150     BIGNUM *k, *kinv = NULL, *r = *rp;
151     int ret = 0;
152
153     if (!dsa->p || !dsa->q || !dsa->g) {
154         DSAerr(DSA_F_DSA_SIGN_SETUP, DSA_R_MISSING_PARAMETERS);
155         return 0;
156     }
157
158     k = BN_new();
159     if (k == NULL)
160         goto err;
161
162     if (ctx_in == NULL) {
163         if ((ctx = BN_CTX_new()) == NULL)
164             goto err;
165     } else
166         ctx = ctx_in;
167
168     /* Get random k */
169     do {
170         if (dgst != NULL) {
171             /*
172              * We calculate k from SHA512(private_key + H(message) + random).
173              * This protects the private key from a weak PRNG.
174              */
175             if (!BN_generate_dsa_nonce(k, dsa->q, dsa->priv_key, dgst,
176                                        dlen, ctx))
177                 goto err;
178         } else if (!BN_rand_range(k, dsa->q))
179             goto err;
180     } while (BN_is_zero(k));
181
182     BN_set_flags(k, BN_FLG_CONSTTIME);
183
184     if (dsa->flags & DSA_FLAG_CACHE_MONT_P) {
185         if (!BN_MONT_CTX_set_locked(&dsa->method_mont_p,
186                                     dsa->lock, dsa->p, ctx))
187             goto err;
188     }
189
190     /* Compute r = (g^k mod p) mod q */
191
192     /*
193      * We do not want timing information to leak the length of k, so we
194      * compute g^k using an equivalent exponent of fixed length. (This
195      * is a kludge that we need because the BN_mod_exp_mont() does not
196      * let us specify the desired timing behaviour.)
197      */
198
199     if (!BN_add(k, k, dsa->q))
200         goto err;
201     if (BN_num_bits(k) <= BN_num_bits(dsa->q)) {
202         if (!BN_add(k, k, dsa->q))
203             goto err;
204     }
205
206     if ((dsa)->meth->bn_mod_exp != NULL) {
207             if (!dsa->meth->bn_mod_exp(dsa, r, dsa->g, k, dsa->p, ctx,
208                                        dsa->method_mont_p))
209                 goto err;
210     } else {
211             if (!BN_mod_exp_mont(r, dsa->g, k, dsa->p, ctx, dsa->method_mont_p))
212                 goto err;
213     }
214
215     if (!BN_mod(r, r, dsa->q, ctx))
216         goto err;
217
218     /* Compute  part of 's = inv(k) (m + xr) mod q' */
219     if ((kinv = BN_mod_inverse(NULL, k, dsa->q, ctx)) == NULL)
220         goto err;
221
222     BN_clear_free(*kinvp);
223     *kinvp = kinv;
224     kinv = NULL;
225     ret = 1;
226  err:
227     if (!ret)
228         DSAerr(DSA_F_DSA_SIGN_SETUP, ERR_R_BN_LIB);
229     if (ctx != ctx_in)
230         BN_CTX_free(ctx);
231     BN_clear_free(k);
232     return ret;
233 }
234
235 static int dsa_do_verify(const unsigned char *dgst, int dgst_len,
236                          DSA_SIG *sig, DSA *dsa)
237 {
238     BN_CTX *ctx;
239     BIGNUM *u1, *u2, *t1;
240     BN_MONT_CTX *mont = NULL;
241     const BIGNUM *r, *s;
242     int ret = -1, i;
243     if (!dsa->p || !dsa->q || !dsa->g) {
244         DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_MISSING_PARAMETERS);
245         return -1;
246     }
247
248     i = BN_num_bits(dsa->q);
249     /* fips 186-3 allows only different sizes for q */
250     if (i != 160 && i != 224 && i != 256) {
251         DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_BAD_Q_VALUE);
252         return -1;
253     }
254
255     if (BN_num_bits(dsa->p) > OPENSSL_DSA_MAX_MODULUS_BITS) {
256         DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_MODULUS_TOO_LARGE);
257         return -1;
258     }
259     u1 = BN_new();
260     u2 = BN_new();
261     t1 = BN_new();
262     ctx = BN_CTX_new();
263     if (u1 == NULL || u2 == NULL || t1 == NULL || ctx == NULL)
264         goto err;
265
266     DSA_SIG_get0(sig, &r, &s);
267
268     if (BN_is_zero(r) || BN_is_negative(r) ||
269         BN_ucmp(r, dsa->q) >= 0) {
270         ret = 0;
271         goto err;
272     }
273     if (BN_is_zero(s) || BN_is_negative(s) ||
274         BN_ucmp(s, dsa->q) >= 0) {
275         ret = 0;
276         goto err;
277     }
278
279     /*
280      * Calculate W = inv(S) mod Q save W in u2
281      */
282     if ((BN_mod_inverse(u2, s, dsa->q, ctx)) == NULL)
283         goto err;
284
285     /* save M in u1 */
286     if (dgst_len > (i >> 3))
287         /*
288          * if the digest length is greater than the size of q use the
289          * BN_num_bits(dsa->q) leftmost bits of the digest, see fips 186-3,
290          * 4.2
291          */
292         dgst_len = (i >> 3);
293     if (BN_bin2bn(dgst, dgst_len, u1) == NULL)
294         goto err;
295
296     /* u1 = M * w mod q */
297     if (!BN_mod_mul(u1, u1, u2, dsa->q, ctx))
298         goto err;
299
300     /* u2 = r * w mod q */
301     if (!BN_mod_mul(u2, r, u2, dsa->q, ctx))
302         goto err;
303
304     if (dsa->flags & DSA_FLAG_CACHE_MONT_P) {
305         mont = BN_MONT_CTX_set_locked(&dsa->method_mont_p,
306                                       dsa->lock, dsa->p, ctx);
307         if (!mont)
308             goto err;
309     }
310
311     if (dsa->meth->dsa_mod_exp != NULL) {
312         if (!dsa->meth->dsa_mod_exp(dsa, t1, dsa->g, u1, dsa->pub_key, u2,
313                                     dsa->p, ctx, mont))
314             goto err;
315     } else {
316         if (!BN_mod_exp2_mont(t1, dsa->g, u1, dsa->pub_key, u2, dsa->p, ctx,
317                               mont))
318             goto err;
319     }
320
321     /* let u1 = u1 mod q */
322     if (!BN_mod(u1, t1, dsa->q, ctx))
323         goto err;
324
325     /*
326      * V is now in u1.  If the signature is correct, it will be equal to R.
327      */
328     ret = (BN_ucmp(u1, r) == 0);
329
330  err:
331     if (ret < 0)
332         DSAerr(DSA_F_DSA_DO_VERIFY, ERR_R_BN_LIB);
333     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     BN_MONT_CTX_free(dsa->method_mont_p);
349     return (1);
350 }