2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
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
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>
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,
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,
30 static DSA_METHOD openssl_dsa_meth = {
33 dsa_sign_setup_no_digest,
35 NULL, /* dsa_mod_exp, */
36 NULL, /* dsa_bn_mod_exp, */
45 static const DSA_METHOD *default_DSA_method = &openssl_dsa_meth;
48 void DSA_set_default_method(const DSA_METHOD *meth)
50 default_DSA_method = meth;
52 #endif /* FIPS_MODE */
54 const DSA_METHOD *DSA_get_default_method(void)
56 return default_DSA_method;
59 const DSA_METHOD *DSA_OpenSSL(void)
61 return &openssl_dsa_meth;
64 DSA_SIG *dsa_do_sign_int(OPENSSL_CTX *libctx, const unsigned char *dgst,
68 BIGNUM *m, *blind, *blindm, *tmp;
70 int reason = ERR_R_BN_LIB;
74 if (dsa->p == NULL || dsa->q == NULL || dsa->g == NULL) {
75 reason = DSA_R_MISSING_PARAMETERS;
78 if (dsa->priv_key == NULL) {
79 reason = DSA_R_MISSING_PRIVATE_KEY;
88 if (ret->r == NULL || ret->s == NULL)
91 ctx = BN_CTX_new_ex(libctx);
95 blind = BN_CTX_get(ctx);
96 blindm = BN_CTX_get(ctx);
97 tmp = BN_CTX_get(ctx);
102 if (!dsa_sign_setup(dsa, ctx, &kinv, &ret->r, dgst, dlen))
105 if (dlen > BN_num_bytes(dsa->q))
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,
111 dlen = BN_num_bytes(dsa->q);
112 if (BN_bin2bn(dgst, dlen, m) == NULL)
116 * The normal signature calculation is:
118 * s := k^-1 * (m + r * priv_key) mod q
120 * We will blind this to protect against side channel attacks
122 * s := blind^-1 * k^-1 * (blind * m + blind * r * priv_key) mod q
125 /* Generate a blinding value */
127 if (!BN_priv_rand_ex(blind, BN_num_bits(dsa->q) - 1,
128 BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY, ctx))
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);
135 /* tmp := blind * priv_key * r mod q */
136 if (!BN_mod_mul(tmp, blind, dsa->priv_key, dsa->q, ctx))
138 if (!BN_mod_mul(tmp, tmp, ret->r, dsa->q, ctx))
141 /* blindm := blind * m mod q */
142 if (!BN_mod_mul(blindm, blind, m, dsa->q, ctx))
145 /* s : = (blind * priv_key * r) + (blind * m) mod q */
146 if (!BN_mod_add_quick(ret->s, tmp, blindm, dsa->q))
149 /* s := s * k^-1 mod q */
150 if (!BN_mod_mul(ret->s, ret->s, kinv, dsa->q, ctx))
153 /* s:= s * blind^-1 mod q */
154 if (BN_mod_inverse(blind, blind, dsa->q, ctx) == NULL)
156 if (!BN_mod_mul(ret->s, ret->s, blind, dsa->q, ctx))
160 * Redo if r or s is zero as required by FIPS 186-3: this is very
163 if (BN_is_zero(ret->r) || BN_is_zero(ret->s))
179 static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
181 return dsa_do_sign_int(NULL, dgst, dlen, dsa);
184 static int dsa_sign_setup_no_digest(DSA *dsa, BN_CTX *ctx_in,
185 BIGNUM **kinvp, BIGNUM **rp)
187 return dsa_sign_setup(dsa, ctx_in, kinvp, rp, NULL, 0);
190 static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in,
191 BIGNUM **kinvp, BIGNUM **rp,
192 const unsigned char *dgst, int dlen)
195 BIGNUM *k, *kinv = NULL, *r = *rp;
200 if (!dsa->p || !dsa->q || !dsa->g) {
201 DSAerr(DSA_F_DSA_SIGN_SETUP, DSA_R_MISSING_PARAMETERS);
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);
210 if (dsa->priv_key == NULL) {
211 DSAerr(DSA_F_DSA_SIGN_SETUP, DSA_R_MISSING_PRIVATE_KEY);
217 if (k == NULL || l == NULL)
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)
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))
238 * We calculate k from SHA512(private_key + H(message) + random).
239 * This protects the private key from a weak PRNG.
241 if (!BN_generate_dsa_nonce(k, dsa->q, dsa->priv_key, dgst,
244 } else if (!BN_priv_rand_range_ex(k, dsa->q, ctx))
246 } while (BN_is_zero(k));
248 BN_set_flags(k, BN_FLG_CONSTTIME);
249 BN_set_flags(l, BN_FLG_CONSTTIME);
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))
257 /* Compute r = (g^k mod p) mod q */
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.
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.
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.
272 if (!BN_add(l, k, dsa->q)
273 || !BN_add(k, l, dsa->q))
276 BN_consttime_swap(BN_is_bit_set(l, q_bits), k, l, q_words + 2);
278 if ((dsa)->meth->bn_mod_exp != NULL) {
279 if (!dsa->meth->bn_mod_exp(dsa, r, dsa->g, k, dsa->p, ctx,
283 if (!BN_mod_exp_mont(r, dsa->g, k, dsa->p, ctx, dsa->method_mont_p))
287 if (!BN_mod(r, r, dsa->q, ctx))
290 /* Compute part of 's = inv(k) (m + xr) mod q' */
291 if ((kinv = dsa_mod_inverse_fermat(k, dsa->q, ctx)) == NULL)
294 BN_clear_free(*kinvp);
300 DSAerr(DSA_F_DSA_SIGN_SETUP, ERR_R_BN_LIB);
308 static int dsa_do_verify(const unsigned char *dgst, int dgst_len,
309 DSA_SIG *sig, DSA *dsa)
312 BIGNUM *u1, *u2, *t1;
313 BN_MONT_CTX *mont = NULL;
316 if (!dsa->p || !dsa->q || !dsa->g) {
317 DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_MISSING_PARAMETERS);
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);
328 if (BN_num_bits(dsa->p) > OPENSSL_DSA_MAX_MODULUS_BITS) {
329 DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_MODULUS_TOO_LARGE);
335 ctx = BN_CTX_new_ex(NULL); /* verify does not need a libctx */
336 if (u1 == NULL || u2 == NULL || t1 == NULL || ctx == NULL)
339 DSA_SIG_get0(sig, &r, &s);
341 if (BN_is_zero(r) || BN_is_negative(r) ||
342 BN_ucmp(r, dsa->q) >= 0) {
346 if (BN_is_zero(s) || BN_is_negative(s) ||
347 BN_ucmp(s, dsa->q) >= 0) {
353 * Calculate W = inv(S) mod Q save W in u2
355 if ((BN_mod_inverse(u2, s, dsa->q, ctx)) == NULL)
359 if (dgst_len > (i >> 3))
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,
366 if (BN_bin2bn(dgst, dgst_len, u1) == NULL)
369 /* u1 = M * w mod q */
370 if (!BN_mod_mul(u1, u1, u2, dsa->q, ctx))
373 /* u2 = r * w mod q */
374 if (!BN_mod_mul(u2, r, u2, dsa->q, ctx))
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);
384 if (dsa->meth->dsa_mod_exp != NULL) {
385 if (!dsa->meth->dsa_mod_exp(dsa, t1, dsa->g, u1, dsa->pub_key, u2,
389 if (!BN_mod_exp2_mont(t1, dsa->g, u1, dsa->pub_key, u2, dsa->p, ctx,
394 /* let u1 = u1 mod q */
395 if (!BN_mod(u1, t1, dsa->q, ctx))
399 * V is now in u1. If the signature is correct, it will be equal to R.
401 ret = (BN_ucmp(u1, r) == 0);
405 DSAerr(DSA_F_DSA_DO_VERIFY, ERR_R_BN_LIB);
413 static int dsa_init(DSA *dsa)
415 dsa->flags |= DSA_FLAG_CACHE_MONT_P;
419 static int dsa_finish(DSA *dsa)
421 BN_MONT_CTX_free(dsa->method_mont_p);
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.
432 static BIGNUM *dsa_mod_inverse_fermat(const BIGNUM *k, const BIGNUM *q,
438 if ((r = BN_new()) == NULL)
442 if ((e = BN_CTX_get(ctx)) != NULL
445 && BN_mod_exp_mont(r, k, e, q, ctx, NULL))