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