2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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 <openssl/bn.h>
13 #include <openssl/sha.h>
15 #include <openssl/asn1.h>
17 static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa);
18 static int dsa_sign_setup_no_digest(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp,
20 static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp,
21 BIGNUM **rp, const unsigned char *dgst, int dlen);
22 static int dsa_do_verify(const unsigned char *dgst, int dgst_len,
23 DSA_SIG *sig, DSA *dsa);
24 static int dsa_init(DSA *dsa);
25 static int dsa_finish(DSA *dsa);
27 static DSA_METHOD openssl_dsa_meth = {
30 dsa_sign_setup_no_digest,
32 NULL, /* dsa_mod_exp, */
33 NULL, /* dsa_bn_mod_exp, */
42 static const DSA_METHOD *default_DSA_method = &openssl_dsa_meth;
44 void DSA_set_default_method(const DSA_METHOD *meth)
46 default_DSA_method = meth;
49 const DSA_METHOD *DSA_get_default_method(void)
51 return default_DSA_method;
54 const DSA_METHOD *DSA_OpenSSL(void)
56 return &openssl_dsa_meth;
59 static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
65 int reason = ERR_R_BN_LIB;
71 if (m == NULL || xr == NULL)
74 if (!dsa->p || !dsa->q || !dsa->g) {
75 reason = DSA_R_MISSING_PARAMETERS;
84 if (ret->r == NULL || ret->s == NULL)
91 if (!dsa_sign_setup(dsa, ctx, &kinv, &ret->r, dgst, dlen))
94 if (dlen > BN_num_bytes(dsa->q))
96 * if the digest length is greater than the size of q use the
97 * BN_num_bits(dsa->q) leftmost bits of the digest, see fips 186-3,
100 dlen = BN_num_bytes(dsa->q);
101 if (BN_bin2bn(dgst, dlen, m) == NULL)
104 /* Compute s = inv(k) (m + xr) mod q */
105 if (!BN_mod_mul(xr, dsa->priv_key, ret->r, dsa->q, ctx))
106 goto err; /* s = xr */
107 if (!BN_add(ret->s, xr, m))
108 goto err; /* s = m + xr */
109 if (BN_cmp(ret->s, dsa->q) > 0)
110 if (!BN_sub(ret->s, ret->s, dsa->q))
112 if (!BN_mod_mul(ret->s, ret->s, kinv, dsa->q, ctx))
116 * Redo if r or s is zero as required by FIPS 186-3: this is very
119 if (BN_is_zero(ret->r) || BN_is_zero(ret->s))
126 DSAerr(DSA_F_DSA_DO_SIGN, reason);
137 static int dsa_sign_setup_no_digest(DSA *dsa, BN_CTX *ctx_in,
138 BIGNUM **kinvp, BIGNUM **rp)
140 return dsa_sign_setup(dsa, ctx_in, kinvp, rp, NULL, 0);
143 static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in,
144 BIGNUM **kinvp, BIGNUM **rp,
145 const unsigned char *dgst, int dlen)
148 BIGNUM *k, *kinv = NULL, *r = *rp;
151 if (!dsa->p || !dsa->q || !dsa->g) {
152 DSAerr(DSA_F_DSA_SIGN_SETUP, DSA_R_MISSING_PARAMETERS);
160 if (ctx_in == NULL) {
161 if ((ctx = BN_CTX_new()) == NULL)
170 * We calculate k from SHA512(private_key + H(message) + random).
171 * This protects the private key from a weak PRNG.
173 if (!BN_generate_dsa_nonce(k, dsa->q, dsa->priv_key, dgst,
176 } else if (!BN_priv_rand_range(k, dsa->q))
178 } while (BN_is_zero(k));
180 BN_set_flags(k, BN_FLG_CONSTTIME);
182 if (dsa->flags & DSA_FLAG_CACHE_MONT_P) {
183 if (!BN_MONT_CTX_set_locked(&dsa->method_mont_p,
184 dsa->lock, dsa->p, ctx))
188 /* Compute r = (g^k mod p) mod q */
191 * We do not want timing information to leak the length of k, so we
192 * compute g^k using an equivalent exponent of fixed length. (This
193 * is a kludge that we need because the BN_mod_exp_mont() does not
194 * let us specify the desired timing behaviour.)
197 if (!BN_add(k, k, dsa->q))
199 if (BN_num_bits(k) <= BN_num_bits(dsa->q)) {
200 if (!BN_add(k, k, dsa->q))
204 if ((dsa)->meth->bn_mod_exp != NULL) {
205 if (!dsa->meth->bn_mod_exp(dsa, r, dsa->g, k, dsa->p, ctx,
209 if (!BN_mod_exp_mont(r, dsa->g, k, dsa->p, ctx, dsa->method_mont_p))
213 if (!BN_mod(r, r, dsa->q, ctx))
216 /* Compute part of 's = inv(k) (m + xr) mod q' */
217 if ((kinv = BN_mod_inverse(NULL, k, dsa->q, ctx)) == NULL)
220 BN_clear_free(*kinvp);
226 DSAerr(DSA_F_DSA_SIGN_SETUP, ERR_R_BN_LIB);
233 static int dsa_do_verify(const unsigned char *dgst, int dgst_len,
234 DSA_SIG *sig, DSA *dsa)
237 BIGNUM *u1, *u2, *t1;
238 BN_MONT_CTX *mont = NULL;
241 if (!dsa->p || !dsa->q || !dsa->g) {
242 DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_MISSING_PARAMETERS);
246 i = BN_num_bits(dsa->q);
247 /* fips 186-3 allows only different sizes for q */
248 if (i != 160 && i != 224 && i != 256) {
249 DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_BAD_Q_VALUE);
253 if (BN_num_bits(dsa->p) > OPENSSL_DSA_MAX_MODULUS_BITS) {
254 DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_MODULUS_TOO_LARGE);
261 if (u1 == NULL || u2 == NULL || t1 == NULL || ctx == NULL)
264 DSA_SIG_get0(sig, &r, &s);
266 if (BN_is_zero(r) || BN_is_negative(r) ||
267 BN_ucmp(r, dsa->q) >= 0) {
271 if (BN_is_zero(s) || BN_is_negative(s) ||
272 BN_ucmp(s, dsa->q) >= 0) {
278 * Calculate W = inv(S) mod Q save W in u2
280 if ((BN_mod_inverse(u2, s, dsa->q, ctx)) == NULL)
284 if (dgst_len > (i >> 3))
286 * if the digest length is greater than the size of q use the
287 * BN_num_bits(dsa->q) leftmost bits of the digest, see fips 186-3,
291 if (BN_bin2bn(dgst, dgst_len, u1) == NULL)
294 /* u1 = M * w mod q */
295 if (!BN_mod_mul(u1, u1, u2, dsa->q, ctx))
298 /* u2 = r * w mod q */
299 if (!BN_mod_mul(u2, r, u2, dsa->q, ctx))
302 if (dsa->flags & DSA_FLAG_CACHE_MONT_P) {
303 mont = BN_MONT_CTX_set_locked(&dsa->method_mont_p,
304 dsa->lock, dsa->p, ctx);
309 if (dsa->meth->dsa_mod_exp != NULL) {
310 if (!dsa->meth->dsa_mod_exp(dsa, t1, dsa->g, u1, dsa->pub_key, u2,
314 if (!BN_mod_exp2_mont(t1, dsa->g, u1, dsa->pub_key, u2, dsa->p, ctx,
319 /* let u1 = u1 mod q */
320 if (!BN_mod(u1, t1, dsa->q, ctx))
324 * V is now in u1. If the signature is correct, it will be equal to R.
326 ret = (BN_ucmp(u1, r) == 0);
330 DSAerr(DSA_F_DSA_DO_VERIFY, ERR_R_BN_LIB);
338 static int dsa_init(DSA *dsa)
340 dsa->flags |= DSA_FLAG_CACHE_MONT_P;
344 static int dsa_finish(DSA *dsa)
346 BN_MONT_CTX_free(dsa->method_mont_p);