Deprecate the low level DSA functions.
[openssl.git] / crypto / dsa / dsa_ossl.c
1 /*
2  * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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 /*
11  * DSA low level APIs are deprecated for public use, but still ok for
12  * internal use.
13  */
14 #include "internal/deprecated.h"
15
16 #include <stdio.h>
17 #include "internal/cryptlib.h"
18 #include "crypto/bn.h"
19 #include <openssl/bn.h>
20 #include <openssl/sha.h>
21 #include "dsa_local.h"
22 #include <openssl/asn1.h>
23
24 static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa);
25 static int dsa_sign_setup_no_digest(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp,
26                                     BIGNUM **rp);
27 static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp,
28                           BIGNUM **rp, const unsigned char *dgst, int dlen);
29 static int dsa_do_verify(const unsigned char *dgst, int dgst_len,
30                          DSA_SIG *sig, DSA *dsa);
31 static int dsa_init(DSA *dsa);
32 static int dsa_finish(DSA *dsa);
33 static BIGNUM *dsa_mod_inverse_fermat(const BIGNUM *k, const BIGNUM *q,
34                                       BN_CTX *ctx);
35
36 static DSA_METHOD openssl_dsa_meth = {
37     "OpenSSL DSA method",
38     dsa_do_sign,
39     dsa_sign_setup_no_digest,
40     dsa_do_verify,
41     NULL,                       /* dsa_mod_exp, */
42     NULL,                       /* dsa_bn_mod_exp, */
43     dsa_init,
44     dsa_finish,
45     DSA_FLAG_FIPS_METHOD,
46     NULL,
47     NULL,
48     NULL
49 };
50
51 static const DSA_METHOD *default_DSA_method = &openssl_dsa_meth;
52
53 #ifndef FIPS_MODE
54 void DSA_set_default_method(const DSA_METHOD *meth)
55 {
56     default_DSA_method = meth;
57 }
58 #endif /* FIPS_MODE */
59
60 const DSA_METHOD *DSA_get_default_method(void)
61 {
62     return default_DSA_method;
63 }
64
65 const DSA_METHOD *DSA_OpenSSL(void)
66 {
67     return &openssl_dsa_meth;
68 }
69
70 DSA_SIG *dsa_do_sign_int(OPENSSL_CTX *libctx, const unsigned char *dgst,
71                          int dlen, DSA *dsa)
72 {
73     BIGNUM *kinv = NULL;
74     BIGNUM *m, *blind, *blindm, *tmp;
75     BN_CTX *ctx = NULL;
76     int reason = ERR_R_BN_LIB;
77     DSA_SIG *ret = NULL;
78     int rv = 0;
79
80     if (dsa->params.p == NULL
81         || dsa->params.q == NULL
82         || dsa->params.g == NULL) {
83         reason = DSA_R_MISSING_PARAMETERS;
84         goto err;
85     }
86     if (dsa->priv_key == NULL) {
87         reason = DSA_R_MISSING_PRIVATE_KEY;
88         goto err;
89     }
90
91     ret = DSA_SIG_new();
92     if (ret == NULL)
93         goto err;
94     ret->r = BN_new();
95     ret->s = BN_new();
96     if (ret->r == NULL || ret->s == NULL)
97         goto err;
98
99     ctx = BN_CTX_new_ex(libctx);
100     if (ctx == NULL)
101         goto err;
102     m = BN_CTX_get(ctx);
103     blind = BN_CTX_get(ctx);
104     blindm = BN_CTX_get(ctx);
105     tmp = BN_CTX_get(ctx);
106     if (tmp == NULL)
107         goto err;
108
109  redo:
110     if (!dsa_sign_setup(dsa, ctx, &kinv, &ret->r, dgst, dlen))
111         goto err;
112
113     if (dlen > BN_num_bytes(dsa->params.q))
114         /*
115          * if the digest length is greater than the size of q use the
116          * BN_num_bits(dsa->q) leftmost bits of the digest, see fips 186-3,
117          * 4.2
118          */
119         dlen = BN_num_bytes(dsa->params.q);
120     if (BN_bin2bn(dgst, dlen, m) == NULL)
121         goto err;
122
123     /*
124      * The normal signature calculation is:
125      *
126      *   s := k^-1 * (m + r * priv_key) mod q
127      *
128      * We will blind this to protect against side channel attacks
129      *
130      *   s := blind^-1 * k^-1 * (blind * m + blind * r * priv_key) mod q
131      */
132
133     /* Generate a blinding value */
134     do {
135         if (!BN_priv_rand_ex(blind, BN_num_bits(dsa->params.q) - 1,
136                              BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY, ctx))
137             goto err;
138     } while (BN_is_zero(blind));
139     BN_set_flags(blind, BN_FLG_CONSTTIME);
140     BN_set_flags(blindm, BN_FLG_CONSTTIME);
141     BN_set_flags(tmp, BN_FLG_CONSTTIME);
142
143     /* tmp := blind * priv_key * r mod q */
144     if (!BN_mod_mul(tmp, blind, dsa->priv_key, dsa->params.q, ctx))
145         goto err;
146     if (!BN_mod_mul(tmp, tmp, ret->r, dsa->params.q, ctx))
147         goto err;
148
149     /* blindm := blind * m mod q */
150     if (!BN_mod_mul(blindm, blind, m, dsa->params.q, ctx))
151         goto err;
152
153     /* s : = (blind * priv_key * r) + (blind * m) mod q */
154     if (!BN_mod_add_quick(ret->s, tmp, blindm, dsa->params.q))
155         goto err;
156
157     /* s := s * k^-1 mod q */
158     if (!BN_mod_mul(ret->s, ret->s, kinv, dsa->params.q, ctx))
159         goto err;
160
161     /* s:= s * blind^-1 mod q */
162     if (BN_mod_inverse(blind, blind, dsa->params.q, ctx) == NULL)
163         goto err;
164     if (!BN_mod_mul(ret->s, ret->s, blind, dsa->params.q, ctx))
165         goto err;
166
167     /*
168      * Redo if r or s is zero as required by FIPS 186-3: this is very
169      * unlikely.
170      */
171     if (BN_is_zero(ret->r) || BN_is_zero(ret->s))
172         goto redo;
173
174     rv = 1;
175
176  err:
177     if (rv == 0) {
178         DSAerr(0, reason);
179         DSA_SIG_free(ret);
180         ret = NULL;
181     }
182     BN_CTX_free(ctx);
183     BN_clear_free(kinv);
184     return ret;
185 }
186
187 static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
188 {
189     return dsa_do_sign_int(NULL, dgst, dlen, dsa);
190 }
191
192 static int dsa_sign_setup_no_digest(DSA *dsa, BN_CTX *ctx_in,
193                                     BIGNUM **kinvp, BIGNUM **rp)
194 {
195     return dsa_sign_setup(dsa, ctx_in, kinvp, rp, NULL, 0);
196 }
197
198 static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in,
199                           BIGNUM **kinvp, BIGNUM **rp,
200                           const unsigned char *dgst, int dlen)
201 {
202     BN_CTX *ctx = NULL;
203     BIGNUM *k, *kinv = NULL, *r = *rp;
204     BIGNUM *l;
205     int ret = 0;
206     int q_bits, q_words;
207
208     if (!dsa->params.p || !dsa->params.q || !dsa->params.g) {
209         DSAerr(DSA_F_DSA_SIGN_SETUP, DSA_R_MISSING_PARAMETERS);
210         return 0;
211     }
212
213     /* Reject obviously invalid parameters */
214     if (BN_is_zero(dsa->params.p)
215         || BN_is_zero(dsa->params.q)
216         || BN_is_zero(dsa->params.g)) {
217         DSAerr(DSA_F_DSA_SIGN_SETUP, DSA_R_INVALID_PARAMETERS);
218         return 0;
219     }
220     if (dsa->priv_key == NULL) {
221         DSAerr(DSA_F_DSA_SIGN_SETUP, DSA_R_MISSING_PRIVATE_KEY);
222         return 0;
223     }
224
225     k = BN_new();
226     l = BN_new();
227     if (k == NULL || l == NULL)
228         goto err;
229
230     if (ctx_in == NULL) {
231         /* if you don't pass in ctx_in you get a default libctx */
232         if ((ctx = BN_CTX_new_ex(NULL)) == NULL)
233             goto err;
234     } else
235         ctx = ctx_in;
236
237     /* Preallocate space */
238     q_bits = BN_num_bits(dsa->params.q);
239     q_words = bn_get_top(dsa->params.q);
240     if (!bn_wexpand(k, q_words + 2)
241         || !bn_wexpand(l, q_words + 2))
242         goto err;
243
244     /* Get random k */
245     do {
246         if (dgst != NULL) {
247             /*
248              * We calculate k from SHA512(private_key + H(message) + random).
249              * This protects the private key from a weak PRNG.
250              */
251             if (!BN_generate_dsa_nonce(k, dsa->params.q, dsa->priv_key, dgst,
252                                        dlen, ctx))
253                 goto err;
254         } else if (!BN_priv_rand_range_ex(k, dsa->params.q, ctx))
255             goto err;
256     } while (BN_is_zero(k));
257
258     BN_set_flags(k, BN_FLG_CONSTTIME);
259     BN_set_flags(l, BN_FLG_CONSTTIME);
260
261     if (dsa->flags & DSA_FLAG_CACHE_MONT_P) {
262         if (!BN_MONT_CTX_set_locked(&dsa->method_mont_p,
263                                     dsa->lock, dsa->params.p, ctx))
264             goto err;
265     }
266
267     /* Compute r = (g^k mod p) mod q */
268
269     /*
270      * We do not want timing information to leak the length of k, so we
271      * compute G^k using an equivalent scalar of fixed bit-length.
272      *
273      * We unconditionally perform both of these additions to prevent a
274      * small timing information leakage.  We then choose the sum that is
275      * one bit longer than the modulus.
276      *
277      * There are some concerns about the efficacy of doing this.  More
278      * specifically refer to the discussion starting with:
279      *     https://github.com/openssl/openssl/pull/7486#discussion_r228323705
280      * The fix is to rework BN so these gymnastics aren't required.
281      */
282     if (!BN_add(l, k, dsa->params.q)
283         || !BN_add(k, l, dsa->params.q))
284         goto err;
285
286     BN_consttime_swap(BN_is_bit_set(l, q_bits), k, l, q_words + 2);
287
288     if ((dsa)->meth->bn_mod_exp != NULL) {
289             if (!dsa->meth->bn_mod_exp(dsa, r, dsa->params.g, k, dsa->params.p,
290                                        ctx, dsa->method_mont_p))
291                 goto err;
292     } else {
293             if (!BN_mod_exp_mont(r, dsa->params.g, k, dsa->params.p, ctx,
294                                  dsa->method_mont_p))
295                 goto err;
296     }
297
298     if (!BN_mod(r, r, dsa->params.q, ctx))
299         goto err;
300
301     /* Compute part of 's = inv(k) (m + xr) mod q' */
302     if ((kinv = dsa_mod_inverse_fermat(k, dsa->params.q, ctx)) == NULL)
303         goto err;
304
305     BN_clear_free(*kinvp);
306     *kinvp = kinv;
307     kinv = NULL;
308     ret = 1;
309  err:
310     if (!ret)
311         DSAerr(DSA_F_DSA_SIGN_SETUP, ERR_R_BN_LIB);
312     if (ctx != ctx_in)
313         BN_CTX_free(ctx);
314     BN_clear_free(k);
315     BN_clear_free(l);
316     return ret;
317 }
318
319 static int dsa_do_verify(const unsigned char *dgst, int dgst_len,
320                          DSA_SIG *sig, DSA *dsa)
321 {
322     BN_CTX *ctx;
323     BIGNUM *u1, *u2, *t1;
324     BN_MONT_CTX *mont = NULL;
325     const BIGNUM *r, *s;
326     int ret = -1, i;
327
328     if (dsa->params.p == NULL
329         || dsa->params.q == NULL
330         || dsa->params.g == NULL) {
331         DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_MISSING_PARAMETERS);
332         return -1;
333     }
334
335     i = BN_num_bits(dsa->params.q);
336     /* fips 186-3 allows only different sizes for q */
337     if (i != 160 && i != 224 && i != 256) {
338         DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_BAD_Q_VALUE);
339         return -1;
340     }
341
342     if (BN_num_bits(dsa->params.p) > OPENSSL_DSA_MAX_MODULUS_BITS) {
343         DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_MODULUS_TOO_LARGE);
344         return -1;
345     }
346     u1 = BN_new();
347     u2 = BN_new();
348     t1 = BN_new();
349     ctx = BN_CTX_new_ex(NULL); /* verify does not need a libctx */
350     if (u1 == NULL || u2 == NULL || t1 == NULL || ctx == NULL)
351         goto err;
352
353     DSA_SIG_get0(sig, &r, &s);
354
355     if (BN_is_zero(r) || BN_is_negative(r) ||
356         BN_ucmp(r, dsa->params.q) >= 0) {
357         ret = 0;
358         goto err;
359     }
360     if (BN_is_zero(s) || BN_is_negative(s) ||
361         BN_ucmp(s, dsa->params.q) >= 0) {
362         ret = 0;
363         goto err;
364     }
365
366     /*
367      * Calculate W = inv(S) mod Q save W in u2
368      */
369     if ((BN_mod_inverse(u2, s, dsa->params.q, ctx)) == NULL)
370         goto err;
371
372     /* save M in u1 */
373     if (dgst_len > (i >> 3))
374         /*
375          * if the digest length is greater than the size of q use the
376          * BN_num_bits(dsa->q) leftmost bits of the digest, see fips 186-3,
377          * 4.2
378          */
379         dgst_len = (i >> 3);
380     if (BN_bin2bn(dgst, dgst_len, u1) == NULL)
381         goto err;
382
383     /* u1 = M * w mod q */
384     if (!BN_mod_mul(u1, u1, u2, dsa->params.q, ctx))
385         goto err;
386
387     /* u2 = r * w mod q */
388     if (!BN_mod_mul(u2, r, u2, dsa->params.q, ctx))
389         goto err;
390
391     if (dsa->flags & DSA_FLAG_CACHE_MONT_P) {
392         mont = BN_MONT_CTX_set_locked(&dsa->method_mont_p,
393                                       dsa->lock, dsa->params.p, ctx);
394         if (!mont)
395             goto err;
396     }
397
398     if (dsa->meth->dsa_mod_exp != NULL) {
399         if (!dsa->meth->dsa_mod_exp(dsa, t1, dsa->params.g, u1, dsa->pub_key, u2,
400                                     dsa->params.p, ctx, mont))
401             goto err;
402     } else {
403         if (!BN_mod_exp2_mont(t1, dsa->params.g, u1, dsa->pub_key, u2,
404                               dsa->params.p, ctx, mont))
405             goto err;
406     }
407
408     /* let u1 = u1 mod q */
409     if (!BN_mod(u1, t1, dsa->params.q, ctx))
410         goto err;
411
412     /*
413      * V is now in u1.  If the signature is correct, it will be equal to R.
414      */
415     ret = (BN_ucmp(u1, r) == 0);
416
417  err:
418     if (ret < 0)
419         DSAerr(DSA_F_DSA_DO_VERIFY, ERR_R_BN_LIB);
420     BN_CTX_free(ctx);
421     BN_free(u1);
422     BN_free(u2);
423     BN_free(t1);
424     return ret;
425 }
426
427 static int dsa_init(DSA *dsa)
428 {
429     dsa->flags |= DSA_FLAG_CACHE_MONT_P;
430     ffc_params_init(&dsa->params);
431     dsa->dirty_cnt++;
432     return 1;
433 }
434
435 static int dsa_finish(DSA *dsa)
436 {
437     BN_MONT_CTX_free(dsa->method_mont_p);
438     return 1;
439 }
440
441 /*
442  * Compute the inverse of k modulo q.
443  * Since q is prime, Fermat's Little Theorem applies, which reduces this to
444  * mod-exp operation.  Both the exponent and modulus are public information
445  * so a mod-exp that doesn't leak the base is sufficient.  A newly allocated
446  * BIGNUM is returned which the caller must free.
447  */
448 static BIGNUM *dsa_mod_inverse_fermat(const BIGNUM *k, const BIGNUM *q,
449                                       BN_CTX *ctx)
450 {
451     BIGNUM *res = NULL;
452     BIGNUM *r, *e;
453
454     if ((r = BN_new()) == NULL)
455         return NULL;
456
457     BN_CTX_start(ctx);
458     if ((e = BN_CTX_get(ctx)) != NULL
459             && BN_set_word(r, 2)
460             && BN_sub(e, q, r)
461             && BN_mod_exp_mont(r, k, e, q, ctx, NULL))
462         res = r;
463     else
464         BN_free(r);
465     BN_CTX_end(ctx);
466     return res;
467 }