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, */
45 const DSA_METHOD
*DSA_OpenSSL(void)
47 return &openssl_dsa_meth
;
50 static DSA_SIG
*dsa_do_sign(const unsigned char *dgst
, int dlen
, DSA
*dsa
)
57 int reason
= ERR_R_BN_LIB
;
63 if (m
== NULL
|| xr
== NULL
)
66 if (!dsa
->p
|| !dsa
->q
|| !dsa
->g
) {
67 reason
= DSA_R_MISSING_PARAMETERS
;
75 DSA_SIG_get0(&r
, &s
, ret
);
81 if (!dsa_sign_setup(dsa
, ctx
, &kinv
, &r
, dgst
, dlen
))
84 if (dlen
> BN_num_bytes(dsa
->q
))
86 * if the digest length is greater than the size of q use the
87 * BN_num_bits(dsa->q) leftmost bits of the digest, see fips 186-3,
90 dlen
= BN_num_bytes(dsa
->q
);
91 if (BN_bin2bn(dgst
, dlen
, m
) == NULL
)
94 /* Compute s = inv(k) (m + xr) mod q */
95 if (!BN_mod_mul(xr
, dsa
->priv_key
, r
, dsa
->q
, ctx
))
96 goto err
; /* s = xr */
97 if (!BN_add(s
, xr
, m
))
98 goto err
; /* s = m + xr */
99 if (BN_cmp(s
, dsa
->q
) > 0)
100 if (!BN_sub(s
, s
, dsa
->q
))
102 if (!BN_mod_mul(s
, s
, kinv
, dsa
->q
, ctx
))
106 * Redo if r or s is zero as required by FIPS 186-3: this is very
109 if (BN_is_zero(r
) || BN_is_zero(s
))
116 DSAerr(DSA_F_DSA_DO_SIGN
, reason
);
127 static int dsa_sign_setup_no_digest(DSA
*dsa
, BN_CTX
*ctx_in
,
128 BIGNUM
**kinvp
, BIGNUM
**rp
)
130 return dsa_sign_setup(dsa
, ctx_in
, kinvp
, rp
, NULL
, 0);
133 static int dsa_sign_setup(DSA
*dsa
, BN_CTX
*ctx_in
,
134 BIGNUM
**kinvp
, BIGNUM
**rp
,
135 const unsigned char *dgst
, int dlen
)
138 BIGNUM
*k
, *kinv
= NULL
, *r
= *rp
;
141 if (!dsa
->p
|| !dsa
->q
|| !dsa
->g
) {
142 DSAerr(DSA_F_DSA_SIGN_SETUP
, DSA_R_MISSING_PARAMETERS
);
150 if (ctx_in
== NULL
) {
151 if ((ctx
= BN_CTX_new()) == NULL
)
160 * We calculate k from SHA512(private_key + H(message) + random).
161 * This protects the private key from a weak PRNG.
163 if (!BN_generate_dsa_nonce(k
, dsa
->q
, dsa
->priv_key
, dgst
,
166 } else if (!BN_rand_range(k
, dsa
->q
))
168 } while (BN_is_zero(k
));
170 BN_set_flags(k
, BN_FLG_CONSTTIME
);
172 if (dsa
->flags
& DSA_FLAG_CACHE_MONT_P
) {
173 if (!BN_MONT_CTX_set_locked(&dsa
->method_mont_p
,
174 dsa
->lock
, dsa
->p
, ctx
))
178 /* Compute r = (g^k mod p) mod q */
181 * We do not want timing information to leak the length of k, so we
182 * compute g^k using an equivalent exponent of fixed length. (This
183 * is a kludge that we need because the BN_mod_exp_mont() does not
184 * let us specify the desired timing behaviour.)
187 if (!BN_add(k
, k
, dsa
->q
))
189 if (BN_num_bits(k
) <= BN_num_bits(dsa
->q
)) {
190 if (!BN_add(k
, k
, dsa
->q
))
194 if ((dsa
)->meth
->bn_mod_exp
!= NULL
) {
195 if (!dsa
->meth
->bn_mod_exp(dsa
, r
, dsa
->g
, k
, dsa
->p
, ctx
,
199 if (!BN_mod_exp_mont(r
, dsa
->g
, k
, dsa
->p
, ctx
, dsa
->method_mont_p
))
203 if (!BN_mod(r
, r
, dsa
->q
, ctx
))
206 /* Compute part of 's = inv(k) (m + xr) mod q' */
207 if ((kinv
= BN_mod_inverse(NULL
, k
, dsa
->q
, ctx
)) == NULL
)
210 BN_clear_free(*kinvp
);
216 DSAerr(DSA_F_DSA_SIGN_SETUP
, ERR_R_BN_LIB
);
223 static int dsa_do_verify(const unsigned char *dgst
, int dgst_len
,
224 DSA_SIG
*sig
, DSA
*dsa
)
227 BIGNUM
*u1
, *u2
, *t1
;
228 BN_MONT_CTX
*mont
= NULL
;
231 if (!dsa
->p
|| !dsa
->q
|| !dsa
->g
) {
232 DSAerr(DSA_F_DSA_DO_VERIFY
, DSA_R_MISSING_PARAMETERS
);
236 i
= BN_num_bits(dsa
->q
);
237 /* fips 186-3 allows only different sizes for q */
238 if (i
!= 160 && i
!= 224 && i
!= 256) {
239 DSAerr(DSA_F_DSA_DO_VERIFY
, DSA_R_BAD_Q_VALUE
);
243 if (BN_num_bits(dsa
->p
) > OPENSSL_DSA_MAX_MODULUS_BITS
) {
244 DSAerr(DSA_F_DSA_DO_VERIFY
, DSA_R_MODULUS_TOO_LARGE
);
251 if (u1
== NULL
|| u2
== NULL
|| t1
== NULL
|| ctx
== NULL
)
254 DSA_SIG_get0(&r
, &s
, sig
);
256 if (BN_is_zero(r
) || BN_is_negative(r
) ||
257 BN_ucmp(r
, dsa
->q
) >= 0) {
261 if (BN_is_zero(s
) || BN_is_negative(s
) ||
262 BN_ucmp(s
, dsa
->q
) >= 0) {
268 * Calculate W = inv(S) mod Q save W in u2
270 if ((BN_mod_inverse(u2
, s
, dsa
->q
, ctx
)) == NULL
)
274 if (dgst_len
> (i
>> 3))
276 * if the digest length is greater than the size of q use the
277 * BN_num_bits(dsa->q) leftmost bits of the digest, see fips 186-3,
281 if (BN_bin2bn(dgst
, dgst_len
, u1
) == NULL
)
284 /* u1 = M * w mod q */
285 if (!BN_mod_mul(u1
, u1
, u2
, dsa
->q
, ctx
))
288 /* u2 = r * w mod q */
289 if (!BN_mod_mul(u2
, r
, u2
, dsa
->q
, ctx
))
292 if (dsa
->flags
& DSA_FLAG_CACHE_MONT_P
) {
293 mont
= BN_MONT_CTX_set_locked(&dsa
->method_mont_p
,
294 dsa
->lock
, dsa
->p
, ctx
);
299 if (dsa
->meth
->dsa_mod_exp
!= NULL
) {
300 if (!dsa
->meth
->dsa_mod_exp(dsa
, t1
, dsa
->g
, u1
, dsa
->pub_key
, u2
,
304 if (!BN_mod_exp2_mont(t1
, dsa
->g
, u1
, dsa
->pub_key
, u2
, dsa
->p
, ctx
,
309 /* let u1 = u1 mod q */
310 if (!BN_mod(u1
, t1
, dsa
->q
, ctx
))
314 * V is now in u1. If the signature is correct, it will be equal to R.
316 ret
= (BN_ucmp(u1
, r
) == 0);
320 DSAerr(DSA_F_DSA_DO_VERIFY
, ERR_R_BN_LIB
);
328 static int dsa_init(DSA
*dsa
)
330 dsa
->flags
|= DSA_FLAG_CACHE_MONT_P
;
334 static int dsa_finish(DSA
*dsa
)
336 BN_MONT_CTX_free(dsa
->method_mont_p
);