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 * DSA low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.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>
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,
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,
36 static DSA_METHOD openssl_dsa_meth = {
39 dsa_sign_setup_no_digest,
41 NULL, /* dsa_mod_exp, */
42 NULL, /* dsa_bn_mod_exp, */
51 static const DSA_METHOD *default_DSA_method = &openssl_dsa_meth;
54 void DSA_set_default_method(const DSA_METHOD *meth)
56 default_DSA_method = meth;
58 #endif /* FIPS_MODE */
60 const DSA_METHOD *DSA_get_default_method(void)
62 return default_DSA_method;
65 const DSA_METHOD *DSA_OpenSSL(void)
67 return &openssl_dsa_meth;
70 DSA_SIG *dsa_do_sign_int(OPENSSL_CTX *libctx, const unsigned char *dgst,
74 BIGNUM *m, *blind, *blindm, *tmp;
76 int reason = ERR_R_BN_LIB;
80 if (dsa->params.p == NULL
81 || dsa->params.q == NULL
82 || dsa->params.g == NULL) {
83 reason = DSA_R_MISSING_PARAMETERS;
86 if (dsa->priv_key == NULL) {
87 reason = DSA_R_MISSING_PRIVATE_KEY;
96 if (ret->r == NULL || ret->s == NULL)
99 ctx = BN_CTX_new_ex(libctx);
103 blind = BN_CTX_get(ctx);
104 blindm = BN_CTX_get(ctx);
105 tmp = BN_CTX_get(ctx);
110 if (!dsa_sign_setup(dsa, ctx, &kinv, &ret->r, dgst, dlen))
113 if (dlen > BN_num_bytes(dsa->params.q))
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,
119 dlen = BN_num_bytes(dsa->params.q);
120 if (BN_bin2bn(dgst, dlen, m) == NULL)
124 * The normal signature calculation is:
126 * s := k^-1 * (m + r * priv_key) mod q
128 * We will blind this to protect against side channel attacks
130 * s := blind^-1 * k^-1 * (blind * m + blind * r * priv_key) mod q
133 /* Generate a blinding value */
135 if (!BN_priv_rand_ex(blind, BN_num_bits(dsa->params.q) - 1,
136 BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY, ctx))
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);
143 /* tmp := blind * priv_key * r mod q */
144 if (!BN_mod_mul(tmp, blind, dsa->priv_key, dsa->params.q, ctx))
146 if (!BN_mod_mul(tmp, tmp, ret->r, dsa->params.q, ctx))
149 /* blindm := blind * m mod q */
150 if (!BN_mod_mul(blindm, blind, m, dsa->params.q, ctx))
153 /* s : = (blind * priv_key * r) + (blind * m) mod q */
154 if (!BN_mod_add_quick(ret->s, tmp, blindm, dsa->params.q))
157 /* s := s * k^-1 mod q */
158 if (!BN_mod_mul(ret->s, ret->s, kinv, dsa->params.q, ctx))
161 /* s:= s * blind^-1 mod q */
162 if (BN_mod_inverse(blind, blind, dsa->params.q, ctx) == NULL)
164 if (!BN_mod_mul(ret->s, ret->s, blind, dsa->params.q, ctx))
168 * Redo if r or s is zero as required by FIPS 186-3: this is very
171 if (BN_is_zero(ret->r) || BN_is_zero(ret->s))
187 static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
189 return dsa_do_sign_int(NULL, dgst, dlen, dsa);
192 static int dsa_sign_setup_no_digest(DSA *dsa, BN_CTX *ctx_in,
193 BIGNUM **kinvp, BIGNUM **rp)
195 return dsa_sign_setup(dsa, ctx_in, kinvp, rp, NULL, 0);
198 static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in,
199 BIGNUM **kinvp, BIGNUM **rp,
200 const unsigned char *dgst, int dlen)
203 BIGNUM *k, *kinv = NULL, *r = *rp;
208 if (!dsa->params.p || !dsa->params.q || !dsa->params.g) {
209 DSAerr(DSA_F_DSA_SIGN_SETUP, DSA_R_MISSING_PARAMETERS);
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);
220 if (dsa->priv_key == NULL) {
221 DSAerr(DSA_F_DSA_SIGN_SETUP, DSA_R_MISSING_PRIVATE_KEY);
227 if (k == NULL || l == NULL)
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)
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))
248 * We calculate k from SHA512(private_key + H(message) + random).
249 * This protects the private key from a weak PRNG.
251 if (!BN_generate_dsa_nonce(k, dsa->params.q, dsa->priv_key, dgst,
254 } else if (!BN_priv_rand_range_ex(k, dsa->params.q, ctx))
256 } while (BN_is_zero(k));
258 BN_set_flags(k, BN_FLG_CONSTTIME);
259 BN_set_flags(l, BN_FLG_CONSTTIME);
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))
267 /* Compute r = (g^k mod p) mod q */
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.
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.
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.
282 if (!BN_add(l, k, dsa->params.q)
283 || !BN_add(k, l, dsa->params.q))
286 BN_consttime_swap(BN_is_bit_set(l, q_bits), k, l, q_words + 2);
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))
293 if (!BN_mod_exp_mont(r, dsa->params.g, k, dsa->params.p, ctx,
298 if (!BN_mod(r, r, dsa->params.q, ctx))
301 /* Compute part of 's = inv(k) (m + xr) mod q' */
302 if ((kinv = dsa_mod_inverse_fermat(k, dsa->params.q, ctx)) == NULL)
305 BN_clear_free(*kinvp);
311 DSAerr(DSA_F_DSA_SIGN_SETUP, ERR_R_BN_LIB);
319 static int dsa_do_verify(const unsigned char *dgst, int dgst_len,
320 DSA_SIG *sig, DSA *dsa)
323 BIGNUM *u1, *u2, *t1;
324 BN_MONT_CTX *mont = NULL;
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);
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);
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);
349 ctx = BN_CTX_new_ex(NULL); /* verify does not need a libctx */
350 if (u1 == NULL || u2 == NULL || t1 == NULL || ctx == NULL)
353 DSA_SIG_get0(sig, &r, &s);
355 if (BN_is_zero(r) || BN_is_negative(r) ||
356 BN_ucmp(r, dsa->params.q) >= 0) {
360 if (BN_is_zero(s) || BN_is_negative(s) ||
361 BN_ucmp(s, dsa->params.q) >= 0) {
367 * Calculate W = inv(S) mod Q save W in u2
369 if ((BN_mod_inverse(u2, s, dsa->params.q, ctx)) == NULL)
373 if (dgst_len > (i >> 3))
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,
380 if (BN_bin2bn(dgst, dgst_len, u1) == NULL)
383 /* u1 = M * w mod q */
384 if (!BN_mod_mul(u1, u1, u2, dsa->params.q, ctx))
387 /* u2 = r * w mod q */
388 if (!BN_mod_mul(u2, r, u2, dsa->params.q, ctx))
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);
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))
403 if (!BN_mod_exp2_mont(t1, dsa->params.g, u1, dsa->pub_key, u2,
404 dsa->params.p, ctx, mont))
408 /* let u1 = u1 mod q */
409 if (!BN_mod(u1, t1, dsa->params.q, ctx))
413 * V is now in u1. If the signature is correct, it will be equal to R.
415 ret = (BN_ucmp(u1, r) == 0);
419 DSAerr(DSA_F_DSA_DO_VERIFY, ERR_R_BN_LIB);
427 static int dsa_init(DSA *dsa)
429 dsa->flags |= DSA_FLAG_CACHE_MONT_P;
430 ffc_params_init(&dsa->params);
435 static int dsa_finish(DSA *dsa)
437 BN_MONT_CTX_free(dsa->method_mont_p);
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.
448 static BIGNUM *dsa_mod_inverse_fermat(const BIGNUM *k, const BIGNUM *q,
454 if ((r = BN_new()) == NULL)
458 if ((e = BN_CTX_get(ctx)) != NULL
461 && BN_mod_exp_mont(r, k, e, q, ctx, NULL))