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