#include <stdio.h>
#include <openssl/asn1.h>
#include <openssl/bn.h>
#include <openssl/dsa.h>
#include <openssl/sha.h>
#include "bn_local.h"
#include "dsa_local.h"
#include "err_local.h"
#define DSA_MAX_SIGN_ITERATIONS 32
static DSA_SIG *
dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
{
BIGNUM *b = NULL, *bm = NULL, *bxr = NULL, *binv = NULL, *m = NULL;
BIGNUM *kinv = NULL, *r = NULL, *s = NULL;
BN_CTX *ctx = NULL;
int reason = ERR_R_BN_LIB;
DSA_SIG *ret = NULL;
int attempts = 0;
int noredo = 0;
if (!dsa_check_key(dsa)) {
reason = DSA_R_INVALID_PARAMETERS;
goto err;
}
if ((s = BN_new()) == NULL)
goto err;
if ((ctx = BN_CTX_new()) == NULL)
goto err;
BN_CTX_start(ctx);
if ((b = BN_CTX_get(ctx)) == NULL)
goto err;
if ((binv = BN_CTX_get(ctx)) == NULL)
goto err;
if ((bm = BN_CTX_get(ctx)) == NULL)
goto err;
if ((bxr = BN_CTX_get(ctx)) == NULL)
goto err;
if ((m = BN_CTX_get(ctx)) == NULL)
goto err;
if (dlen > BN_num_bytes(dsa->q))
dlen = BN_num_bytes(dsa->q);
if (BN_bin2bn(dgst, dlen, m) == NULL)
goto err;
redo:
if (dsa->kinv == NULL || dsa->r == NULL) {
if (!DSA_sign_setup(dsa, ctx, &kinv, &r))
goto err;
} else {
kinv = dsa->kinv;
dsa->kinv = NULL;
r = dsa->r;
dsa->r = NULL;
noredo = 1;
}
if (!bn_rand_interval(b, 1, dsa->q))
goto err;
if (BN_mod_inverse_ct(binv, b, dsa->q, ctx) == NULL)
goto err;
if (!BN_mod_mul(bxr, b, dsa->priv_key, dsa->q, ctx))
goto err;
if (!BN_mod_mul(bxr, bxr, r, dsa->q, ctx))
goto err;
if (!BN_mod_mul(bm, b, m, dsa->q, ctx))
goto err;
if (!BN_mod_add(s, bxr, bm, dsa->q, ctx))
goto err;
if (!BN_mod_mul(s, s, kinv, dsa->q, ctx))
goto err;
if (!BN_mod_mul(s, s, binv, dsa->q, ctx))
goto err;
if (BN_is_zero(r) || BN_is_zero(s)) {
if (noredo) {
reason = DSA_R_NEED_NEW_SETUP_VALUES;
goto err;
}
if (++attempts > DSA_MAX_SIGN_ITERATIONS) {
reason = DSA_R_INVALID_PARAMETERS;
goto err;
}
goto redo;
}
if ((ret = DSA_SIG_new()) == NULL) {
reason = ERR_R_MALLOC_FAILURE;
goto err;
}
ret->r = r;
ret->s = s;
err:
if (!ret) {
DSAerror(reason);
BN_free(r);
BN_free(s);
}
BN_CTX_end(ctx);
BN_CTX_free(ctx);
BN_free(kinv);
return ret;
}
static int
dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp)
{
BIGNUM *k = NULL, *l = NULL, *m = NULL, *kinv = NULL, *r = NULL;
BN_CTX *ctx = NULL;
int q_bits;
int ret = 0;
if (!dsa_check_key(dsa))
goto err;
if ((r = BN_new()) == NULL)
goto err;
if ((ctx = ctx_in) == NULL)
ctx = BN_CTX_new();
if (ctx == NULL)
goto err;
BN_CTX_start(ctx);
if ((k = BN_CTX_get(ctx)) == NULL)
goto err;
if ((l = BN_CTX_get(ctx)) == NULL)
goto err;
if ((m = BN_CTX_get(ctx)) == NULL)
goto err;
q_bits = BN_num_bits(dsa->q);
if (!BN_set_bit(k, q_bits) ||
!BN_set_bit(l, q_bits) ||
!BN_set_bit(m, q_bits))
goto err;
if (!bn_rand_interval(k, 1, dsa->q))
goto err;
BN_set_flags(k, BN_FLG_CONSTTIME);
if (dsa->flags & DSA_FLAG_CACHE_MONT_P) {
if (!BN_MONT_CTX_set_locked(&dsa->method_mont_p,
CRYPTO_LOCK_DSA, dsa->p, ctx))
goto err;
}
if (!BN_add(l, k, dsa->q) ||
!BN_add(m, l, dsa->q) ||
!bn_copy(k, BN_num_bits(l) > q_bits ? l : m))
goto err;
if (!BN_mod_exp_mont_ct(r, dsa->g, k, dsa->p, ctx, dsa->method_mont_p))
goto err;
if (!BN_mod_ct(r, r, dsa->q, ctx))
goto err;
if ((kinv = BN_mod_inverse_ct(NULL, k, dsa->q, ctx)) == NULL)
goto err;
BN_free(*kinvp);
*kinvp = kinv;
kinv = NULL;
BN_free(*rp);
*rp = r;
ret = 1;
err:
if (!ret) {
DSAerror(ERR_R_BN_LIB);
BN_free(r);
}
BN_CTX_end(ctx);
if (ctx != ctx_in)
BN_CTX_free(ctx);
return ret;
}
static int
dsa_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig, DSA *dsa)
{
BIGNUM *u1 = NULL, *u2 = NULL, *t1 = NULL;
BN_CTX *ctx = NULL;
BN_MONT_CTX *mont = NULL;
int qbits;
int ret = -1;
if (!dsa_check_key(dsa))
goto err;
if ((ctx = BN_CTX_new()) == NULL)
goto err;
BN_CTX_start(ctx);
if ((u1 = BN_CTX_get(ctx)) == NULL)
goto err;
if ((u2 = BN_CTX_get(ctx)) == NULL)
goto err;
if ((t1 = BN_CTX_get(ctx)) == NULL)
goto err;
if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
BN_ucmp(sig->r, dsa->q) >= 0) {
ret = 0;
goto err;
}
if (BN_is_zero(sig->s) || BN_is_negative(sig->s) ||
BN_ucmp(sig->s, dsa->q) >= 0) {
ret = 0;
goto err;
}
if ((BN_mod_inverse_ct(u2, sig->s, dsa->q, ctx)) == NULL)
goto err;
qbits = BN_num_bits(dsa->q);
if (dgst_len > (qbits >> 3))
dgst_len = (qbits >> 3);
if (BN_bin2bn(dgst, dgst_len, u1) == NULL)
goto err;
if (!BN_mod_mul(u1, u1, u2, dsa->q, ctx))
goto err;
if (!BN_mod_mul(u2, sig->r, u2, dsa->q, ctx))
goto err;
if (dsa->flags & DSA_FLAG_CACHE_MONT_P) {
mont = BN_MONT_CTX_set_locked(&dsa->method_mont_p,
CRYPTO_LOCK_DSA, dsa->p, ctx);
if (!mont)
goto err;
}
if (!BN_mod_exp2_mont(t1, dsa->g, u1, dsa->pub_key, u2, dsa->p,
ctx, mont))
goto err;
if (!BN_mod_ct(u1, t1, dsa->q, ctx))
goto err;
ret = BN_ucmp(u1, sig->r) == 0;
err:
if (ret < 0)
DSAerror(ERR_R_BN_LIB);
BN_CTX_end(ctx);
BN_CTX_free(ctx);
return ret;
}
static int
dsa_init(DSA *dsa)
{
dsa->flags |= DSA_FLAG_CACHE_MONT_P;
return 1;
}
static int
dsa_finish(DSA *dsa)
{
BN_MONT_CTX_free(dsa->method_mont_p);
return 1;
}
static const DSA_METHOD openssl_dsa_meth = {
.name = "OpenSSL DSA method",
.dsa_do_sign = dsa_do_sign,
.dsa_sign_setup = dsa_sign_setup,
.dsa_do_verify = dsa_do_verify,
.init = dsa_init,
.finish = dsa_finish,
};
const DSA_METHOD *
DSA_OpenSSL(void)
{
return &openssl_dsa_meth;
}
LCRYPTO_ALIAS(DSA_OpenSSL);
DSA_SIG *
DSA_SIG_new(void)
{
return calloc(1, sizeof(DSA_SIG));
}
LCRYPTO_ALIAS(DSA_SIG_new);
void
DSA_SIG_free(DSA_SIG *sig)
{
if (sig == NULL)
return;
BN_free(sig->r);
BN_free(sig->s);
free(sig);
}
LCRYPTO_ALIAS(DSA_SIG_free);
int
DSA_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp)
{
return dsa->meth->dsa_sign_setup(dsa, ctx_in, kinvp, rp);
}
LCRYPTO_ALIAS(DSA_sign_setup);
DSA_SIG *
DSA_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
{
return dsa->meth->dsa_do_sign(dgst, dlen, dsa);
}
LCRYPTO_ALIAS(DSA_do_sign);
int
DSA_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig, DSA *dsa)
{
return dsa->meth->dsa_do_verify(dgst, dgst_len, sig, dsa);
}
LCRYPTO_ALIAS(DSA_do_verify);