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
10 /* Original version from Steven Schoch <schoch@sheba.arc.nasa.gov> */
13 #include "internal/cryptlib.h"
14 #include <openssl/bn.h>
15 #include <openssl/sha.h>
17 #include <openssl/rand.h>
18 #include <openssl/asn1.h>
20 static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa);
21 static int dsa_sign_setup_no_digest(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp,
23 static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp,
24 BIGNUM **rp, const unsigned char *dgst, int dlen);
25 static int dsa_do_verify(const unsigned char *dgst, int dgst_len,
26 DSA_SIG *sig, DSA *dsa);
27 static int dsa_init(DSA *dsa);
28 static int dsa_finish(DSA *dsa);
30 static DSA_METHOD openssl_dsa_meth = {
33 dsa_sign_setup_no_digest,
35 NULL, /* dsa_mod_exp, */
36 NULL, /* dsa_bn_mod_exp, */
46 * These macro wrappers replace attempts to use the dsa_mod_exp() and
47 * bn_mod_exp() handlers in the DSA_METHOD structure. We avoid the problem of
48 * having a the macro work as an expression by bundling an "err_instr". So;
50 * if (!dsa->meth->bn_mod_exp(dsa, r,dsa->g,&k,dsa->p,ctx,
51 * dsa->method_mont_p)) goto err;
55 * DSA_BN_MOD_EXP(goto err, dsa, r, dsa->g, &k, dsa->p, ctx,
56 * dsa->method_mont_p);
59 #define DSA_MOD_EXP(err_instr,dsa,rr,a1,p1,a2,p2,m,ctx,in_mont) \
62 if ((dsa)->meth->dsa_mod_exp) \
63 _tmp_res53 = (dsa)->meth->dsa_mod_exp((dsa), (rr), (a1), (p1), \
64 (a2), (p2), (m), (ctx), (in_mont)); \
66 _tmp_res53 = BN_mod_exp2_mont((rr), (a1), (p1), (a2), (p2), \
67 (m), (ctx), (in_mont)); \
68 if (!_tmp_res53) err_instr; \
70 #define DSA_BN_MOD_EXP(err_instr,dsa,r,a,p,m,ctx,m_ctx) \
73 if ((dsa)->meth->bn_mod_exp) \
74 _tmp_res53 = (dsa)->meth->bn_mod_exp((dsa), (r), (a), (p), \
75 (m), (ctx), (m_ctx)); \
77 _tmp_res53 = BN_mod_exp_mont((r), (a), (p), (m), (ctx), (m_ctx)); \
78 if (!_tmp_res53) err_instr; \
81 const DSA_METHOD *DSA_OpenSSL(void)
83 return &openssl_dsa_meth;
86 static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
93 int reason = ERR_R_BN_LIB;
99 if (m == NULL || xr == NULL)
102 if (!dsa->p || !dsa->q || !dsa->g) {
103 reason = DSA_R_MISSING_PARAMETERS;
111 DSA_SIG_get0(&r, &s, ret);
117 if (!dsa_sign_setup(dsa, ctx, &kinv, &r, dgst, dlen))
120 if (dlen > BN_num_bytes(dsa->q))
122 * if the digest length is greater than the size of q use the
123 * BN_num_bits(dsa->q) leftmost bits of the digest, see fips 186-3,
126 dlen = BN_num_bytes(dsa->q);
127 if (BN_bin2bn(dgst, dlen, m) == NULL)
130 /* Compute s = inv(k) (m + xr) mod q */
131 if (!BN_mod_mul(xr, dsa->priv_key, r, dsa->q, ctx))
132 goto err; /* s = xr */
133 if (!BN_add(s, xr, m))
134 goto err; /* s = m + xr */
135 if (BN_cmp(s, dsa->q) > 0)
136 if (!BN_sub(s, s, dsa->q))
138 if (!BN_mod_mul(s, s, kinv, dsa->q, ctx))
142 * Redo if r or s is zero as required by FIPS 186-3: this is very
145 if (BN_is_zero(r) || BN_is_zero(s))
152 DSAerr(DSA_F_DSA_DO_SIGN, reason);
163 static int dsa_sign_setup_no_digest(DSA *dsa, BN_CTX *ctx_in,
164 BIGNUM **kinvp, BIGNUM **rp)
166 return dsa_sign_setup(dsa, ctx_in, kinvp, rp, NULL, 0);
169 static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in,
170 BIGNUM **kinvp, BIGNUM **rp,
171 const unsigned char *dgst, int dlen)
174 BIGNUM *k, *kq, *K, *kinv = NULL, *r = *rp;
177 if (!dsa->p || !dsa->q || !dsa->g) {
178 DSAerr(DSA_F_DSA_SIGN_SETUP, DSA_R_MISSING_PARAMETERS);
184 if (k == NULL || kq == NULL)
187 if (ctx_in == NULL) {
188 if ((ctx = BN_CTX_new()) == NULL)
197 * We calculate k from SHA512(private_key + H(message) + random).
198 * This protects the private key from a weak PRNG.
200 if (!BN_generate_dsa_nonce(k, dsa->q, dsa->priv_key, dgst,
203 } else if (!BN_rand_range(k, dsa->q))
205 } while (BN_is_zero(k));
207 if ((dsa->flags & DSA_FLAG_NO_EXP_CONSTTIME) == 0) {
208 BN_set_flags(k, BN_FLG_CONSTTIME);
211 if (dsa->flags & DSA_FLAG_CACHE_MONT_P) {
212 if (!BN_MONT_CTX_set_locked(&dsa->method_mont_p,
213 dsa->lock, dsa->p, ctx))
217 /* Compute r = (g^k mod p) mod q */
219 if ((dsa->flags & DSA_FLAG_NO_EXP_CONSTTIME) == 0) {
224 * We do not want timing information to leak the length of k, so we
225 * compute g^k using an equivalent exponent of fixed length. (This
226 * is a kludge that we need because the BN_mod_exp_mont() does not
227 * let us specify the desired timing behaviour.)
230 if (!BN_add(kq, kq, dsa->q))
232 if (BN_num_bits(kq) <= BN_num_bits(dsa->q)) {
233 if (!BN_add(kq, kq, dsa->q))
241 DSA_BN_MOD_EXP(goto err, dsa, r, dsa->g, K, dsa->p, ctx,
243 if (!BN_mod(r, r, dsa->q, ctx))
246 /* Compute part of 's = inv(k) (m + xr) mod q' */
247 if ((kinv = BN_mod_inverse(NULL, k, dsa->q, ctx)) == NULL)
250 BN_clear_free(*kinvp);
256 DSAerr(DSA_F_DSA_SIGN_SETUP, ERR_R_BN_LIB);
264 static int dsa_do_verify(const unsigned char *dgst, int dgst_len,
265 DSA_SIG *sig, DSA *dsa)
268 BIGNUM *u1, *u2, *t1;
269 BN_MONT_CTX *mont = NULL;
272 if (!dsa->p || !dsa->q || !dsa->g) {
273 DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_MISSING_PARAMETERS);
277 i = BN_num_bits(dsa->q);
278 /* fips 186-3 allows only different sizes for q */
279 if (i != 160 && i != 224 && i != 256) {
280 DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_BAD_Q_VALUE);
284 if (BN_num_bits(dsa->p) > OPENSSL_DSA_MAX_MODULUS_BITS) {
285 DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_MODULUS_TOO_LARGE);
292 if (u1 == NULL || u2 == NULL || t1 == NULL || ctx == NULL)
295 DSA_SIG_get0(&r, &s, sig);
297 if (BN_is_zero(r) || BN_is_negative(r) ||
298 BN_ucmp(r, dsa->q) >= 0) {
302 if (BN_is_zero(s) || BN_is_negative(s) ||
303 BN_ucmp(s, dsa->q) >= 0) {
309 * Calculate W = inv(S) mod Q save W in u2
311 if ((BN_mod_inverse(u2, s, dsa->q, ctx)) == NULL)
315 if (dgst_len > (i >> 3))
317 * if the digest length is greater than the size of q use the
318 * BN_num_bits(dsa->q) leftmost bits of the digest, see fips 186-3,
322 if (BN_bin2bn(dgst, dgst_len, u1) == NULL)
325 /* u1 = M * w mod q */
326 if (!BN_mod_mul(u1, u1, u2, dsa->q, ctx))
329 /* u2 = r * w mod q */
330 if (!BN_mod_mul(u2, r, u2, dsa->q, ctx))
333 if (dsa->flags & DSA_FLAG_CACHE_MONT_P) {
334 mont = BN_MONT_CTX_set_locked(&dsa->method_mont_p,
335 dsa->lock, dsa->p, ctx);
340 DSA_MOD_EXP(goto err, dsa, t1, dsa->g, u1, dsa->pub_key, u2, dsa->p, ctx,
342 /* BN_copy(&u1,&t1); */
343 /* let u1 = u1 mod q */
344 if (!BN_mod(u1, t1, dsa->q, ctx))
348 * V is now in u1. If the signature is correct, it will be equal to R.
350 ret = (BN_ucmp(u1, r) == 0);
354 DSAerr(DSA_F_DSA_DO_VERIFY, ERR_R_BN_LIB);
362 static int dsa_init(DSA *dsa)
364 dsa->flags |= DSA_FLAG_CACHE_MONT_P;
368 static int dsa_finish(DSA *dsa)
370 BN_MONT_CTX_free(dsa->method_mont_p);