|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
|
|
Packit Service |
084de1 |
*
|
|
Packit Service |
084de1 |
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
Packit Service |
084de1 |
* this file except in compliance with the License. You can obtain a copy
|
|
Packit Service |
084de1 |
* in the file LICENSE in the source distribution or at
|
|
Packit Service |
084de1 |
* https://www.openssl.org/source/license.html
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
#include <stdio.h>
|
|
Packit Service |
084de1 |
#include "internal/cryptlib.h"
|
|
Packit Service |
084de1 |
#include "crypto/bn.h"
|
|
Packit Service |
084de1 |
#include <openssl/bn.h>
|
|
Packit Service |
084de1 |
#include <openssl/sha.h>
|
|
Packit Service |
084de1 |
#include "dsa_local.h"
|
|
Packit Service |
084de1 |
#include <openssl/asn1.h>
|
|
Packit Service |
084de1 |
#ifdef OPENSSL_FIPS
|
|
Packit Service |
084de1 |
# include <openssl/fips.h>
|
|
Packit Service |
084de1 |
#endif
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa);
|
|
Packit Service |
084de1 |
static int dsa_sign_setup_no_digest(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp,
|
|
Packit Service |
084de1 |
BIGNUM **rp);
|
|
Packit Service |
084de1 |
static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp,
|
|
Packit Service |
084de1 |
BIGNUM **rp, const unsigned char *dgst, int dlen);
|
|
Packit Service |
084de1 |
static int dsa_do_verify(const unsigned char *dgst, int dgst_len,
|
|
Packit Service |
084de1 |
DSA_SIG *sig, DSA *dsa);
|
|
Packit Service |
084de1 |
static int dsa_init(DSA *dsa);
|
|
Packit Service |
084de1 |
static int dsa_finish(DSA *dsa);
|
|
Packit Service |
084de1 |
static BIGNUM *dsa_mod_inverse_fermat(const BIGNUM *k, const BIGNUM *q,
|
|
Packit Service |
084de1 |
BN_CTX *ctx);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
static DSA_METHOD openssl_dsa_meth = {
|
|
Packit Service |
084de1 |
"OpenSSL DSA method",
|
|
Packit Service |
084de1 |
dsa_do_sign,
|
|
Packit Service |
084de1 |
dsa_sign_setup_no_digest,
|
|
Packit Service |
084de1 |
dsa_do_verify,
|
|
Packit Service |
084de1 |
NULL, /* dsa_mod_exp, */
|
|
Packit Service |
084de1 |
NULL, /* dsa_bn_mod_exp, */
|
|
Packit Service |
084de1 |
dsa_init,
|
|
Packit Service |
084de1 |
dsa_finish,
|
|
Packit Service |
084de1 |
DSA_FLAG_FIPS_METHOD,
|
|
Packit Service |
084de1 |
NULL,
|
|
Packit Service |
084de1 |
NULL,
|
|
Packit Service |
084de1 |
NULL
|
|
Packit Service |
084de1 |
};
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
static const DSA_METHOD *default_DSA_method = &openssl_dsa_meth;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
void DSA_set_default_method(const DSA_METHOD *meth)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
default_DSA_method = meth;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
const DSA_METHOD *DSA_get_default_method(void)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
return default_DSA_method;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
const DSA_METHOD *DSA_OpenSSL(void)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
return &openssl_dsa_meth;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
BIGNUM *kinv = NULL;
|
|
Packit Service |
084de1 |
BIGNUM *m, *blind, *blindm, *tmp;
|
|
Packit Service |
084de1 |
BN_CTX *ctx = NULL;
|
|
Packit Service |
084de1 |
int reason = ERR_R_BN_LIB;
|
|
Packit Service |
084de1 |
DSA_SIG *ret = NULL;
|
|
Packit Service |
084de1 |
int rv = 0;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (dsa->p == NULL || dsa->q == NULL || dsa->g == NULL) {
|
|
Packit Service |
084de1 |
reason = DSA_R_MISSING_PARAMETERS;
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
if (dsa->priv_key == NULL) {
|
|
Packit Service |
084de1 |
reason = DSA_R_MISSING_PRIVATE_KEY;
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
#ifdef OPENSSL_FIPS
|
|
Packit Service |
084de1 |
if (FIPS_selftest_failed()) {
|
|
Packit Service |
084de1 |
FIPSerr(FIPS_F_DSA_DO_SIGN, FIPS_R_FIPS_SELFTEST_FAILED);
|
|
Packit Service |
084de1 |
return NULL;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (FIPS_mode() && !(dsa->flags & DSA_FLAG_NON_FIPS_ALLOW)
|
|
Packit Service |
084de1 |
&& (BN_num_bits(dsa->p) < OPENSSL_DSA_FIPS_MIN_MODULUS_BITS)) {
|
|
Packit Service |
084de1 |
DSAerr(DSA_F_DSA_DO_SIGN, DSA_R_KEY_SIZE_TOO_SMALL);
|
|
Packit Service |
084de1 |
return NULL;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
#endif
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
ret = DSA_SIG_new();
|
|
Packit Service |
084de1 |
if (ret == NULL)
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
ret->r = BN_new();
|
|
Packit Service |
084de1 |
ret->s = BN_new();
|
|
Packit Service |
084de1 |
if (ret->r == NULL || ret->s == NULL)
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
ctx = BN_CTX_new();
|
|
Packit Service |
084de1 |
if (ctx == NULL)
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
m = BN_CTX_get(ctx);
|
|
Packit Service |
084de1 |
blind = BN_CTX_get(ctx);
|
|
Packit Service |
084de1 |
blindm = BN_CTX_get(ctx);
|
|
Packit Service |
084de1 |
tmp = BN_CTX_get(ctx);
|
|
Packit Service |
084de1 |
if (tmp == NULL)
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
redo:
|
|
Packit Service |
084de1 |
if (!dsa_sign_setup(dsa, ctx, &kinv, &ret->r, dgst, dlen))
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (dlen > BN_num_bytes(dsa->q))
|
|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* if the digest length is greater than the size of q use the
|
|
Packit Service |
084de1 |
* BN_num_bits(dsa->q) leftmost bits of the digest, see fips 186-3,
|
|
Packit Service |
084de1 |
* 4.2
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
dlen = BN_num_bytes(dsa->q);
|
|
Packit Service |
084de1 |
if (BN_bin2bn(dgst, dlen, m) == NULL)
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* The normal signature calculation is:
|
|
Packit Service |
084de1 |
*
|
|
Packit Service |
084de1 |
* s := k^-1 * (m + r * priv_key) mod q
|
|
Packit Service |
084de1 |
*
|
|
Packit Service |
084de1 |
* We will blind this to protect against side channel attacks
|
|
Packit Service |
084de1 |
*
|
|
Packit Service |
084de1 |
* s := blind^-1 * k^-1 * (blind * m + blind * r * priv_key) mod q
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* Generate a blinding value */
|
|
Packit Service |
084de1 |
do {
|
|
Packit Service |
084de1 |
if (!BN_priv_rand(blind, BN_num_bits(dsa->q) - 1,
|
|
Packit Service |
084de1 |
BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY))
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
} while (BN_is_zero(blind));
|
|
Packit Service |
084de1 |
BN_set_flags(blind, BN_FLG_CONSTTIME);
|
|
Packit Service |
084de1 |
BN_set_flags(blindm, BN_FLG_CONSTTIME);
|
|
Packit Service |
084de1 |
BN_set_flags(tmp, BN_FLG_CONSTTIME);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* tmp := blind * priv_key * r mod q */
|
|
Packit Service |
084de1 |
if (!BN_mod_mul(tmp, blind, dsa->priv_key, dsa->q, ctx))
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
if (!BN_mod_mul(tmp, tmp, ret->r, dsa->q, ctx))
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* blindm := blind * m mod q */
|
|
Packit Service |
084de1 |
if (!BN_mod_mul(blindm, blind, m, dsa->q, ctx))
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* s : = (blind * priv_key * r) + (blind * m) mod q */
|
|
Packit Service |
084de1 |
if (!BN_mod_add_quick(ret->s, tmp, blindm, dsa->q))
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* s := s * k^-1 mod q */
|
|
Packit Service |
084de1 |
if (!BN_mod_mul(ret->s, ret->s, kinv, dsa->q, ctx))
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* s:= s * blind^-1 mod q */
|
|
Packit Service |
084de1 |
if (BN_mod_inverse(blind, blind, dsa->q, ctx) == NULL)
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
if (!BN_mod_mul(ret->s, ret->s, blind, dsa->q, ctx))
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* Redo if r or s is zero as required by FIPS 186-3: this is very
|
|
Packit Service |
084de1 |
* unlikely.
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
if (BN_is_zero(ret->r) || BN_is_zero(ret->s))
|
|
Packit Service |
084de1 |
goto redo;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
rv = 1;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
err:
|
|
Packit Service |
084de1 |
if (rv == 0) {
|
|
Packit Service |
084de1 |
DSAerr(DSA_F_DSA_DO_SIGN, reason);
|
|
Packit Service |
084de1 |
DSA_SIG_free(ret);
|
|
Packit Service |
084de1 |
ret = NULL;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
BN_CTX_free(ctx);
|
|
Packit Service |
084de1 |
BN_clear_free(kinv);
|
|
Packit Service |
084de1 |
return ret;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
static int dsa_sign_setup_no_digest(DSA *dsa, BN_CTX *ctx_in,
|
|
Packit Service |
084de1 |
BIGNUM **kinvp, BIGNUM **rp)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
return dsa_sign_setup(dsa, ctx_in, kinvp, rp, NULL, 0);
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in,
|
|
Packit Service |
084de1 |
BIGNUM **kinvp, BIGNUM **rp,
|
|
Packit Service |
084de1 |
const unsigned char *dgst, int dlen)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
BN_CTX *ctx = NULL;
|
|
Packit Service |
084de1 |
BIGNUM *k, *kinv = NULL, *r = *rp;
|
|
Packit Service |
084de1 |
BIGNUM *l;
|
|
Packit Service |
084de1 |
int ret = 0;
|
|
Packit Service |
084de1 |
int q_bits, q_words;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (!dsa->p || !dsa->q || !dsa->g) {
|
|
Packit Service |
084de1 |
DSAerr(DSA_F_DSA_SIGN_SETUP, DSA_R_MISSING_PARAMETERS);
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* Reject obviously invalid parameters */
|
|
Packit Service |
084de1 |
if (BN_is_zero(dsa->p) || BN_is_zero(dsa->q) || BN_is_zero(dsa->g)) {
|
|
Packit Service |
084de1 |
DSAerr(DSA_F_DSA_SIGN_SETUP, DSA_R_INVALID_PARAMETERS);
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
if (dsa->priv_key == NULL) {
|
|
Packit Service |
084de1 |
DSAerr(DSA_F_DSA_SIGN_SETUP, DSA_R_MISSING_PRIVATE_KEY);
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
k = BN_new();
|
|
Packit Service |
084de1 |
l = BN_new();
|
|
Packit Service |
084de1 |
if (k == NULL || l == NULL)
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (ctx_in == NULL) {
|
|
Packit Service |
084de1 |
if ((ctx = BN_CTX_new()) == NULL)
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
} else
|
|
Packit Service |
084de1 |
ctx = ctx_in;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* Preallocate space */
|
|
Packit Service |
084de1 |
q_bits = BN_num_bits(dsa->q);
|
|
Packit Service |
084de1 |
q_words = bn_get_top(dsa->q);
|
|
Packit Service |
084de1 |
if (!bn_wexpand(k, q_words + 2)
|
|
Packit Service |
084de1 |
|| !bn_wexpand(l, q_words + 2))
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* Get random k */
|
|
Packit Service |
084de1 |
do {
|
|
Packit Service |
084de1 |
if (dgst != NULL) {
|
|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* We calculate k from SHA512(private_key + H(message) + random).
|
|
Packit Service |
084de1 |
* This protects the private key from a weak PRNG.
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
if (!BN_generate_dsa_nonce(k, dsa->q, dsa->priv_key, dgst,
|
|
Packit Service |
084de1 |
dlen, ctx))
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
} else if (!BN_priv_rand_range(k, dsa->q))
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
} while (BN_is_zero(k));
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
BN_set_flags(k, BN_FLG_CONSTTIME);
|
|
Packit Service |
084de1 |
BN_set_flags(l, BN_FLG_CONSTTIME);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (dsa->flags & DSA_FLAG_CACHE_MONT_P) {
|
|
Packit Service |
084de1 |
if (!BN_MONT_CTX_set_locked(&dsa->method_mont_p,
|
|
Packit Service |
084de1 |
dsa->lock, dsa->p, ctx))
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* Compute r = (g^k mod p) mod q */
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* We do not want timing information to leak the length of k, so we
|
|
Packit Service |
084de1 |
* compute G^k using an equivalent scalar of fixed bit-length.
|
|
Packit Service |
084de1 |
*
|
|
Packit Service |
084de1 |
* We unconditionally perform both of these additions to prevent a
|
|
Packit Service |
084de1 |
* small timing information leakage. We then choose the sum that is
|
|
Packit Service |
084de1 |
* one bit longer than the modulus.
|
|
Packit Service |
084de1 |
*
|
|
Packit Service |
084de1 |
* There are some concerns about the efficacy of doing this. More
|
|
Packit Service |
084de1 |
* specifically refer to the discussion starting with:
|
|
Packit Service |
084de1 |
* https://github.com/openssl/openssl/pull/7486#discussion_r228323705
|
|
Packit Service |
084de1 |
* The fix is to rework BN so these gymnastics aren't required.
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
if (!BN_add(l, k, dsa->q)
|
|
Packit Service |
084de1 |
|| !BN_add(k, l, dsa->q))
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
BN_consttime_swap(BN_is_bit_set(l, q_bits), k, l, q_words + 2);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if ((dsa)->meth->bn_mod_exp != NULL) {
|
|
Packit Service |
084de1 |
if (!dsa->meth->bn_mod_exp(dsa, r, dsa->g, k, dsa->p, ctx,
|
|
Packit Service |
084de1 |
dsa->method_mont_p))
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
} else {
|
|
Packit Service |
084de1 |
if (!BN_mod_exp_mont(r, dsa->g, k, dsa->p, ctx, dsa->method_mont_p))
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (!BN_mod(r, r, dsa->q, ctx))
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* Compute part of 's = inv(k) (m + xr) mod q' */
|
|
Packit Service |
084de1 |
if ((kinv = dsa_mod_inverse_fermat(k, dsa->q, ctx)) == NULL)
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
BN_clear_free(*kinvp);
|
|
Packit Service |
084de1 |
*kinvp = kinv;
|
|
Packit Service |
084de1 |
kinv = NULL;
|
|
Packit Service |
084de1 |
ret = 1;
|
|
Packit Service |
084de1 |
err:
|
|
Packit Service |
084de1 |
if (!ret)
|
|
Packit Service |
084de1 |
DSAerr(DSA_F_DSA_SIGN_SETUP, ERR_R_BN_LIB);
|
|
Packit Service |
084de1 |
if (ctx != ctx_in)
|
|
Packit Service |
084de1 |
BN_CTX_free(ctx);
|
|
Packit Service |
084de1 |
BN_clear_free(k);
|
|
Packit Service |
084de1 |
BN_clear_free(l);
|
|
Packit Service |
084de1 |
return ret;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
static int dsa_do_verify(const unsigned char *dgst, int dgst_len,
|
|
Packit Service |
084de1 |
DSA_SIG *sig, DSA *dsa)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
BN_CTX *ctx;
|
|
Packit Service |
084de1 |
BIGNUM *u1, *u2, *t1;
|
|
Packit Service |
084de1 |
BN_MONT_CTX *mont = NULL;
|
|
Packit Service |
084de1 |
const BIGNUM *r, *s;
|
|
Packit Service |
084de1 |
int ret = -1, i;
|
|
Packit Service |
084de1 |
if (!dsa->p || !dsa->q || !dsa->g) {
|
|
Packit Service |
084de1 |
DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_MISSING_PARAMETERS);
|
|
Packit Service |
084de1 |
return -1;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
i = BN_num_bits(dsa->q);
|
|
Packit Service |
084de1 |
/* fips 186-3 allows only different sizes for q */
|
|
Packit Service |
084de1 |
if (i != 160 && i != 224 && i != 256) {
|
|
Packit Service |
084de1 |
DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_BAD_Q_VALUE);
|
|
Packit Service |
084de1 |
return -1;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
#ifdef OPENSSL_FIPS
|
|
Packit Service |
084de1 |
if (FIPS_selftest_failed()) {
|
|
Packit Service |
084de1 |
FIPSerr(FIPS_F_DSA_DO_VERIFY, FIPS_R_FIPS_SELFTEST_FAILED);
|
|
Packit Service |
084de1 |
return -1;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (FIPS_mode() && !(dsa->flags & DSA_FLAG_NON_FIPS_ALLOW)
|
|
Packit Service |
084de1 |
&& (BN_num_bits(dsa->p) < OPENSSL_DSA_FIPS_MIN_MODULUS_BITS)) {
|
|
Packit Service |
084de1 |
DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_KEY_SIZE_TOO_SMALL);
|
|
Packit Service |
084de1 |
return -1;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
#endif
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (BN_num_bits(dsa->p) > OPENSSL_DSA_MAX_MODULUS_BITS) {
|
|
Packit Service |
084de1 |
DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_MODULUS_TOO_LARGE);
|
|
Packit Service |
084de1 |
return -1;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
u1 = BN_new();
|
|
Packit Service |
084de1 |
u2 = BN_new();
|
|
Packit Service |
084de1 |
t1 = BN_new();
|
|
Packit Service |
084de1 |
ctx = BN_CTX_new();
|
|
Packit Service |
084de1 |
if (u1 == NULL || u2 == NULL || t1 == NULL || ctx == NULL)
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
DSA_SIG_get0(sig, &r, &s);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (BN_is_zero(r) || BN_is_negative(r) ||
|
|
Packit Service |
084de1 |
BN_ucmp(r, dsa->q) >= 0) {
|
|
Packit Service |
084de1 |
ret = 0;
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
if (BN_is_zero(s) || BN_is_negative(s) ||
|
|
Packit Service |
084de1 |
BN_ucmp(s, dsa->q) >= 0) {
|
|
Packit Service |
084de1 |
ret = 0;
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* Calculate W = inv(S) mod Q save W in u2
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
if ((BN_mod_inverse(u2, s, dsa->q, ctx)) == NULL)
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* save M in u1 */
|
|
Packit Service |
084de1 |
if (dgst_len > (i >> 3))
|
|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* if the digest length is greater than the size of q use the
|
|
Packit Service |
084de1 |
* BN_num_bits(dsa->q) leftmost bits of the digest, see fips 186-3,
|
|
Packit Service |
084de1 |
* 4.2
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
dgst_len = (i >> 3);
|
|
Packit Service |
084de1 |
if (BN_bin2bn(dgst, dgst_len, u1) == NULL)
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* u1 = M * w mod q */
|
|
Packit Service |
084de1 |
if (!BN_mod_mul(u1, u1, u2, dsa->q, ctx))
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* u2 = r * w mod q */
|
|
Packit Service |
084de1 |
if (!BN_mod_mul(u2, r, u2, dsa->q, ctx))
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (dsa->flags & DSA_FLAG_CACHE_MONT_P) {
|
|
Packit Service |
084de1 |
mont = BN_MONT_CTX_set_locked(&dsa->method_mont_p,
|
|
Packit Service |
084de1 |
dsa->lock, dsa->p, ctx);
|
|
Packit Service |
084de1 |
if (!mont)
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (dsa->meth->dsa_mod_exp != NULL) {
|
|
Packit Service |
084de1 |
if (!dsa->meth->dsa_mod_exp(dsa, t1, dsa->g, u1, dsa->pub_key, u2,
|
|
Packit Service |
084de1 |
dsa->p, ctx, mont))
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
} else {
|
|
Packit Service |
084de1 |
if (!BN_mod_exp2_mont(t1, dsa->g, u1, dsa->pub_key, u2, dsa->p, ctx,
|
|
Packit Service |
084de1 |
mont))
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* let u1 = u1 mod q */
|
|
Packit Service |
084de1 |
if (!BN_mod(u1, t1, dsa->q, ctx))
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* V is now in u1. If the signature is correct, it will be equal to R.
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
ret = (BN_ucmp(u1, r) == 0);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
err:
|
|
Packit Service |
084de1 |
if (ret < 0)
|
|
Packit Service |
084de1 |
DSAerr(DSA_F_DSA_DO_VERIFY, ERR_R_BN_LIB);
|
|
Packit Service |
084de1 |
BN_CTX_free(ctx);
|
|
Packit Service |
084de1 |
BN_free(u1);
|
|
Packit Service |
084de1 |
BN_free(u2);
|
|
Packit Service |
084de1 |
BN_free(t1);
|
|
Packit Service |
084de1 |
return ret;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
static int dsa_init(DSA *dsa)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
#ifdef OPENSSL_FIPS
|
|
Packit Service |
084de1 |
FIPS_selftest_check();
|
|
Packit Service |
084de1 |
#endif
|
|
Packit Service |
084de1 |
dsa->flags |= DSA_FLAG_CACHE_MONT_P;
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
static int dsa_finish(DSA *dsa)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
BN_MONT_CTX_free(dsa->method_mont_p);
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* Compute the inverse of k modulo q.
|
|
Packit Service |
084de1 |
* Since q is prime, Fermat's Little Theorem applies, which reduces this to
|
|
Packit Service |
084de1 |
* mod-exp operation. Both the exponent and modulus are public information
|
|
Packit Service |
084de1 |
* so a mod-exp that doesn't leak the base is sufficient. A newly allocated
|
|
Packit Service |
084de1 |
* BIGNUM is returned which the caller must free.
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
static BIGNUM *dsa_mod_inverse_fermat(const BIGNUM *k, const BIGNUM *q,
|
|
Packit Service |
084de1 |
BN_CTX *ctx)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
BIGNUM *res = NULL;
|
|
Packit Service |
084de1 |
BIGNUM *r, *e;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if ((r = BN_new()) == NULL)
|
|
Packit Service |
084de1 |
return NULL;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
BN_CTX_start(ctx);
|
|
Packit Service |
084de1 |
if ((e = BN_CTX_get(ctx)) != NULL
|
|
Packit Service |
084de1 |
&& BN_set_word(r, 2)
|
|
Packit Service |
084de1 |
&& BN_sub(e, q, r)
|
|
Packit Service |
084de1 |
&& BN_mod_exp_mont(r, k, e, q, ctx, NULL))
|
|
Packit Service |
084de1 |
res = r;
|
|
Packit Service |
084de1 |
else
|
|
Packit Service |
084de1 |
BN_free(r);
|
|
Packit Service |
084de1 |
BN_CTX_end(ctx);
|
|
Packit Service |
084de1 |
return res;
|
|
Packit Service |
084de1 |
}
|