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