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