chore: checkpoint before Python removal

This commit is contained in:
2026-03-26 22:33:59 +00:00
parent 683cec9307
commit e568ddf82a
29972 changed files with 11269302 additions and 2 deletions

View File

@@ -0,0 +1,976 @@
// Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
// The DSS routines are based on patches supplied by Steven Schoch <schoch@sheba.arc.nasa.gov>.
// SPDX-License-Identifier: Apache-2.0
#include <openssl/dsa.h>
#include <string.h>
#include <openssl/bio.h>
#include <openssl/bn.h>
#include <openssl/dh.h>
#include <openssl/digest.h>
#include <openssl/engine.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/ex_data.h>
#include <openssl/mem.h>
#include <openssl/rand.h>
#include <openssl/sha.h>
#include <openssl/thread.h>
#include "internal.h"
#include "../fipsmodule/bn/internal.h"
#include "../fipsmodule/dh/internal.h"
#include "../internal.h"
// Primality test according to FIPS PUB 186[-1], Appendix 2.1: 50 rounds of
// Miller-Rabin.
#define DSS_prime_checks 50
static int dsa_sign_setup(const DSA *dsa, BN_CTX *ctx_in, BIGNUM **out_kinv,
BIGNUM **out_r);
static CRYPTO_EX_DATA_CLASS g_ex_data_class = CRYPTO_EX_DATA_CLASS_INIT;
DSA *DSA_new(void) {
DSA *dsa = OPENSSL_zalloc(sizeof(DSA));
if (dsa == NULL) {
return NULL;
}
dsa->references = 1;
CRYPTO_MUTEX_init(&dsa->method_mont_lock);
CRYPTO_new_ex_data(&dsa->ex_data);
return dsa;
}
void DSA_free(DSA *dsa) {
if (dsa == NULL) {
return;
}
if (!CRYPTO_refcount_dec_and_test_zero(&dsa->references)) {
return;
}
CRYPTO_free_ex_data(&g_ex_data_class, dsa, &dsa->ex_data);
BN_clear_free(dsa->p);
BN_clear_free(dsa->q);
BN_clear_free(dsa->g);
BN_clear_free(dsa->pub_key);
BN_clear_free(dsa->priv_key);
BN_MONT_CTX_free(dsa->method_mont_p);
BN_MONT_CTX_free(dsa->method_mont_q);
CRYPTO_MUTEX_cleanup(&dsa->method_mont_lock);
OPENSSL_free(dsa);
}
int DSA_print(BIO *bio, const DSA *dsa, int indent) {
EVP_PKEY *pkey = EVP_PKEY_new();
int ret = pkey != NULL &&
EVP_PKEY_set1_DSA(pkey, (DSA *)dsa) &&
EVP_PKEY_print_private(bio, pkey, indent, NULL);
EVP_PKEY_free(pkey);
return ret;
}
int DSA_print_fp(FILE *fp, const DSA *dsa, int indent) {
BIO *bio = BIO_new(BIO_s_file());
if (bio == NULL) {
OPENSSL_PUT_ERROR(RSA, ERR_R_BUF_LIB);
return 0;
}
BIO_set_fp(bio, fp, BIO_NOCLOSE);
int ret = DSA_print(bio, dsa, indent);
BIO_free(bio);
return ret;
}
int DSA_up_ref(DSA *dsa) {
CRYPTO_refcount_inc(&dsa->references);
return 1;
}
unsigned DSA_bits(const DSA *dsa) { return BN_num_bits(dsa->p); }
const BIGNUM *DSA_get0_pub_key(const DSA *dsa) { return dsa->pub_key; }
const BIGNUM *DSA_get0_priv_key(const DSA *dsa) { return dsa->priv_key; }
const BIGNUM *DSA_get0_p(const DSA *dsa) { return dsa->p; }
const BIGNUM *DSA_get0_q(const DSA *dsa) { return dsa->q; }
const BIGNUM *DSA_get0_g(const DSA *dsa) { return dsa->g; }
void DSA_get0_key(const DSA *dsa, const BIGNUM **out_pub_key,
const BIGNUM **out_priv_key) {
if (out_pub_key != NULL) {
*out_pub_key = dsa->pub_key;
}
if (out_priv_key != NULL) {
*out_priv_key = dsa->priv_key;
}
}
void DSA_get0_pqg(const DSA *dsa, const BIGNUM **out_p, const BIGNUM **out_q,
const BIGNUM **out_g) {
if (out_p != NULL) {
*out_p = dsa->p;
}
if (out_q != NULL) {
*out_q = dsa->q;
}
if (out_g != NULL) {
*out_g = dsa->g;
}
}
int DSA_set0_key(DSA *dsa, BIGNUM *pub_key, BIGNUM *priv_key) {
if (dsa->pub_key == NULL && pub_key == NULL) {
return 0;
}
if (pub_key != NULL) {
BN_free(dsa->pub_key);
dsa->pub_key = pub_key;
}
if (priv_key != NULL) {
BN_free(dsa->priv_key);
dsa->priv_key = priv_key;
}
return 1;
}
int DSA_set0_pqg(DSA *dsa, BIGNUM *p, BIGNUM *q, BIGNUM *g) {
if ((dsa->p == NULL && p == NULL) ||
(dsa->q == NULL && q == NULL) ||
(dsa->g == NULL && g == NULL)) {
return 0;
}
if (p != NULL) {
BN_free(dsa->p);
dsa->p = p;
}
if (q != NULL) {
BN_free(dsa->q);
dsa->q = q;
}
if (g != NULL) {
BN_free(dsa->g);
dsa->g = g;
}
BN_MONT_CTX_free(dsa->method_mont_p);
dsa->method_mont_p = NULL;
BN_MONT_CTX_free(dsa->method_mont_q);
dsa->method_mont_q = NULL;
return 1;
}
int DSA_generate_parameters_ex(DSA *dsa, unsigned bits, const uint8_t *seed_in,
size_t seed_len, int *out_counter,
unsigned long *out_h, BN_GENCB *cb) {
const EVP_MD *evpmd = (bits >= 2048) ? EVP_sha256() : EVP_sha1();
return dsa_internal_paramgen(dsa, bits, evpmd, seed_in, seed_len, out_counter, out_h, cb);
}
int dsa_internal_paramgen(DSA *dsa, size_t bits, const EVP_MD *evpmd,
const unsigned char *seed_in, size_t seed_len,
int *out_counter, unsigned long *out_h,
BN_GENCB *cb)
{
if (bits > OPENSSL_DSA_MAX_MODULUS_BITS) {
OPENSSL_PUT_ERROR(DSA, DSA_R_INVALID_PARAMETERS);
return 0;
}
int ok = 0;
unsigned char seed[SHA256_DIGEST_LENGTH];
unsigned char md[SHA256_DIGEST_LENGTH];
unsigned char buf[SHA256_DIGEST_LENGTH], buf2[SHA256_DIGEST_LENGTH];
BIGNUM *r0, *W, *X, *c, *test;
BIGNUM *g = NULL, *q = NULL, *p = NULL;
BN_MONT_CTX *mont = NULL;
int k, n = 0, m = 0;
int counter = 0;
int r = 0;
BN_CTX *ctx = NULL;
unsigned int h = 2;
const size_t qsize = EVP_MD_size(evpmd);
if (bits < 512) {
bits = 512;
}
bits = (bits + 63) / 64 * 64;
if (seed_in != NULL) {
if (seed_len < qsize) {
return 0;
}
if (seed_len > qsize) {
// Only consume as much seed as is expected.
seed_len = qsize;
}
OPENSSL_memcpy(seed, seed_in, seed_len);
}
ctx = BN_CTX_new();
if (ctx == NULL) {
goto err;
}
BN_CTX_start(ctx);
r0 = BN_CTX_get(ctx);
g = BN_CTX_get(ctx);
W = BN_CTX_get(ctx);
q = BN_CTX_get(ctx);
X = BN_CTX_get(ctx);
c = BN_CTX_get(ctx);
p = BN_CTX_get(ctx);
test = BN_CTX_get(ctx);
if (test == NULL || !BN_lshift(test, BN_value_one(), bits - 1)) {
goto err;
}
for (;;) {
// Find q.
for (;;) {
// step 1
if (!BN_GENCB_call(cb, BN_GENCB_GENERATED, m++)) {
goto err;
}
int use_random_seed = (seed_in == NULL);
if (use_random_seed) {
AWSLC_ABORT_IF_NOT_ONE(RAND_bytes(seed, qsize));
// DSA parameters are public.
CONSTTIME_DECLASSIFY(seed, qsize);
} else {
// If we come back through, use random seed next time.
seed_in = NULL;
}
OPENSSL_memcpy(buf, seed, qsize);
OPENSSL_memcpy(buf2, seed, qsize);
// precompute "SEED + 1" for step 7:
for (size_t i = qsize - 1; i < qsize; i--) {
buf[i]++;
if (buf[i] != 0) {
break;
}
}
// step 2
if (!EVP_Digest(seed, qsize, md, NULL, evpmd, NULL) ||
!EVP_Digest(buf, qsize, buf2, NULL, evpmd, NULL)) {
goto err;
}
for (size_t i = 0; i < qsize; i++) {
md[i] ^= buf2[i];
}
// step 3
md[0] |= 0x80;
md[qsize - 1] |= 0x01;
if (!BN_bin2bn(md, qsize, q)) {
goto err;
}
// step 4
r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx, use_random_seed, cb);
if (r > 0) {
break;
}
if (r != 0) {
goto err;
}
// do a callback call
// step 5
}
if (!BN_GENCB_call(cb, 2, 0) || !BN_GENCB_call(cb, 3, 0)) {
goto err;
}
// step 6
counter = 0;
// "offset = 2"
n = (bits - 1) / 160;
for (;;) {
if ((counter != 0) && !BN_GENCB_call(cb, BN_GENCB_GENERATED, counter)) {
goto err;
}
// step 7
BN_zero(W);
// now 'buf' contains "SEED + offset - 1"
for (k = 0; k <= n; k++) {
// obtain "SEED + offset + k" by incrementing:
for (size_t i = qsize - 1; i < qsize; i--) {
buf[i]++;
if (buf[i] != 0) {
break;
}
}
if (!EVP_Digest(buf, qsize, md, NULL, evpmd, NULL)) {
goto err;
}
// step 8
if (!BN_bin2bn(md, qsize, r0) ||
!BN_lshift(r0, r0, (qsize << 3) * k) ||
!BN_add(W, W, r0)) {
goto err;
}
}
// more of step 8
if (!BN_mask_bits(W, bits - 1) ||
!BN_copy(X, W) ||
!BN_add(X, X, test)) {
goto err;
}
// step 9
if (!BN_lshift1(r0, q) ||
!BN_mod(c, X, r0, ctx) ||
!BN_sub(r0, c, BN_value_one()) ||
!BN_sub(p, X, r0)) {
goto err;
}
// step 10
if (BN_cmp(p, test) >= 0) {
// step 11
r = BN_is_prime_fasttest_ex(p, DSS_prime_checks, ctx, 1, cb);
if (r > 0) {
goto end; // found it
}
if (r != 0) {
goto err;
}
}
// step 13
counter++;
// "offset = offset + n + 1"
// step 14
if (counter >= 4096) {
break;
}
}
}
end:
if (!BN_GENCB_call(cb, 2, 1)) {
goto err;
}
// We now need to generate g
// Set r0=(p-1)/q
if (!BN_sub(test, p, BN_value_one()) ||
!BN_div(r0, NULL, test, q, ctx)) {
goto err;
}
mont = BN_MONT_CTX_new_for_modulus(p, ctx);
if (mont == NULL ||
!BN_set_word(test, h)) {
goto err;
}
for (;;) {
// g=test^r0%p
if (!BN_mod_exp_mont(g, test, r0, p, ctx, mont)) {
goto err;
}
if (!BN_is_one(g)) {
break;
}
if (!BN_add(test, test, BN_value_one())) {
goto err;
}
h++;
}
if (!BN_GENCB_call(cb, 3, 1)) {
goto err;
}
ok = 1;
err:
if (ok) {
BN_free(dsa->p);
BN_free(dsa->q);
BN_free(dsa->g);
dsa->p = BN_dup(p);
dsa->q = BN_dup(q);
dsa->g = BN_dup(g);
if (dsa->p == NULL || dsa->q == NULL || dsa->g == NULL) {
ok = 0;
goto err;
}
if (out_counter != NULL) {
*out_counter = counter;
}
if (out_h != NULL) {
*out_h = h;
}
}
if (ctx) {
BN_CTX_end(ctx);
BN_CTX_free(ctx);
}
BN_MONT_CTX_free(mont);
OPENSSL_cleanse(seed, SHA256_DIGEST_LENGTH);
return ok;
}
DSA *DSAparams_dup(const DSA *dsa) {
DSA *ret = DSA_new();
if (ret == NULL) {
return NULL;
}
ret->p = BN_dup(dsa->p);
ret->q = BN_dup(dsa->q);
ret->g = BN_dup(dsa->g);
if (ret->p == NULL || ret->q == NULL || ret->g == NULL) {
DSA_free(ret);
return NULL;
}
return ret;
}
int DSA_generate_key(DSA *dsa) {
if (!dsa_check_key(dsa)) {
return 0;
}
int ok = 0;
BIGNUM *pub_key = NULL, *priv_key = NULL;
BN_CTX *ctx = BN_CTX_new();
if (ctx == NULL) {
goto err;
}
priv_key = dsa->priv_key;
if (priv_key == NULL) {
priv_key = BN_new();
if (priv_key == NULL) {
goto err;
}
}
if (!BN_rand_range_ex(priv_key, 1, dsa->q)) {
goto err;
}
pub_key = dsa->pub_key;
if (pub_key == NULL) {
pub_key = BN_new();
if (pub_key == NULL) {
goto err;
}
}
if (!BN_MONT_CTX_set_locked(&dsa->method_mont_p, &dsa->method_mont_lock,
dsa->p, ctx) ||
!BN_mod_exp_mont_consttime(pub_key, dsa->g, priv_key, dsa->p, ctx,
dsa->method_mont_p)) {
goto err;
}
// The public key is computed from the private key, but is public.
bn_declassify(pub_key);
dsa->priv_key = priv_key;
dsa->pub_key = pub_key;
ok = 1;
err:
if (dsa->pub_key == NULL) {
BN_free(pub_key);
}
if (dsa->priv_key == NULL) {
BN_free(priv_key);
}
BN_CTX_free(ctx);
return ok;
}
DSA_SIG *DSA_SIG_new(void) { return OPENSSL_zalloc(sizeof(DSA_SIG)); }
void DSA_SIG_free(DSA_SIG *sig) {
if (!sig) {
return;
}
BN_free(sig->r);
BN_free(sig->s);
OPENSSL_free(sig);
}
void DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **out_r,
const BIGNUM **out_s) {
if (out_r != NULL) {
*out_r = sig->r;
}
if (out_s != NULL) {
*out_s = sig->s;
}
}
int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s) {
if (r == NULL || s == NULL) {
return 0;
}
BN_free(sig->r);
BN_free(sig->s);
sig->r = r;
sig->s = s;
return 1;
}
// mod_mul_consttime sets |r| to |a| * |b| modulo |mont->N|, treating |a| and
// |b| as secret. This function internally uses Montgomery reduction, but
// neither inputs nor outputs are in Montgomery form.
static int mod_mul_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
const BN_MONT_CTX *mont, BN_CTX *ctx) {
BN_CTX_start(ctx);
BIGNUM *tmp = BN_CTX_get(ctx);
// |BN_mod_mul_montgomery| removes a factor of R, so we cancel it with a
// single |BN_to_montgomery| which adds one factor of R.
int ok = tmp != NULL &&
BN_to_montgomery(tmp, a, mont, ctx) &&
BN_mod_mul_montgomery(r, tmp, b, mont, ctx);
BN_CTX_end(ctx);
return ok;
}
DSA_SIG *DSA_do_sign(const uint8_t *digest, size_t digest_len, const DSA *dsa) {
if (!dsa_check_key(dsa)) {
return NULL;
}
if (dsa->priv_key == NULL) {
OPENSSL_PUT_ERROR(DSA, DSA_R_MISSING_PARAMETERS);
return NULL;
}
BIGNUM *kinv = NULL, *r = NULL, *s = NULL;
BIGNUM m;
BIGNUM xr;
BN_CTX *ctx = NULL;
DSA_SIG *ret = NULL;
BN_init(&m);
BN_init(&xr);
s = BN_new();
if (s == NULL) {
goto err;
}
ctx = BN_CTX_new();
if (ctx == NULL) {
goto err;
}
// Cap iterations so that invalid parameters do not infinite loop. This does
// not impact valid parameters because the probability of requiring even one
// retry is negligible, let alone 32. Unfortunately, DSA was mis-specified, so
// invalid parameters are reachable from most callers handling untrusted
// private keys. (The |dsa_check_key| call above is not sufficient. Checking
// whether arbitrary paremeters form a valid DSA group is expensive.)
static const int kMaxIterations = 32;
int iters = 0;
redo:
if (!dsa_sign_setup(dsa, ctx, &kinv, &r)) {
goto err;
}
if (digest_len > BN_num_bytes(dsa->q)) {
// If the digest length is greater than the size of |dsa->q| use the
// BN_num_bits(dsa->q) leftmost bits of the digest, see FIPS 186-3, 4.2.
// Note the above check that |dsa->q| is a multiple of 8 bits.
digest_len = BN_num_bytes(dsa->q);
}
if (BN_bin2bn(digest, digest_len, &m) == NULL) {
goto err;
}
// |m| is bounded by 2^(num_bits(q)), which is slightly looser than q. This
// violates |bn_mod_add_consttime| and |mod_mul_consttime|'s preconditions.
// (The underlying algorithms could accept looser bounds, but we reduce for
// simplicity.)
size_t q_width = bn_minimal_width(dsa->q);
if (!bn_resize_words(&m, q_width) ||
!bn_resize_words(&xr, q_width)) {
goto err;
}
bn_reduce_once_in_place(m.d, 0 /* no carry word */, dsa->q->d,
xr.d /* scratch space */, q_width);
// Compute s = inv(k) (m + xr) mod q. Note |dsa->method_mont_q| is
// initialized by |dsa_sign_setup|.
if (!mod_mul_consttime(&xr, dsa->priv_key, r, dsa->method_mont_q, ctx) ||
!bn_mod_add_consttime(s, &xr, &m, dsa->q, ctx) ||
!mod_mul_consttime(s, s, kinv, dsa->method_mont_q, ctx)) {
goto err;
}
// The signature is computed from the private key, but is public.
bn_declassify(r);
bn_declassify(s);
// Redo if r or s is zero as required by FIPS 186-3: this is
// very unlikely.
if (BN_is_zero(r) || BN_is_zero(s)) {
iters++;
if (iters > kMaxIterations) {
OPENSSL_PUT_ERROR(DSA, DSA_R_TOO_MANY_ITERATIONS);
goto err;
}
goto redo;
}
ret = DSA_SIG_new();
if (ret == NULL) {
goto err;
}
ret->r = r;
ret->s = s;
err:
if (ret == NULL) {
OPENSSL_PUT_ERROR(DSA, ERR_R_BN_LIB);
BN_free(r);
BN_free(s);
}
BN_CTX_free(ctx);
BN_clear_free(&m);
BN_clear_free(&xr);
BN_clear_free(kinv);
return ret;
}
int DSA_do_verify(const uint8_t *digest, size_t digest_len, const DSA_SIG *sig,
const DSA *dsa) {
int valid;
if (!DSA_do_check_signature(&valid, digest, digest_len, sig, dsa)) {
return -1;
}
return valid;
}
int DSA_do_check_signature(int *out_valid, const uint8_t *digest,
size_t digest_len, const DSA_SIG *sig,
const DSA *dsa) {
*out_valid = 0;
if (!dsa_check_key(dsa)) {
return 0;
}
if (dsa->pub_key == NULL) {
OPENSSL_PUT_ERROR(DSA, DSA_R_MISSING_PARAMETERS);
return 0;
}
int ret = 0;
BIGNUM u1, u2, t1;
BN_init(&u1);
BN_init(&u2);
BN_init(&t1);
BN_CTX *ctx = BN_CTX_new();
if (ctx == NULL) {
goto err;
}
if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
BN_ucmp(sig->r, dsa->q) >= 0) {
ret = 1;
goto err;
}
if (BN_is_zero(sig->s) || BN_is_negative(sig->s) ||
BN_ucmp(sig->s, dsa->q) >= 0) {
ret = 1;
goto err;
}
// Calculate W = inv(S) mod Q
// save W in u2
if (BN_mod_inverse(&u2, sig->s, dsa->q, ctx) == NULL) {
goto err;
}
// save M in u1
unsigned q_bits = BN_num_bits(dsa->q);
if (digest_len > (q_bits >> 3)) {
// if the digest length is greater than the size of q use the
// BN_num_bits(dsa->q) leftmost bits of the digest, see
// fips 186-3, 4.2
digest_len = (q_bits >> 3);
}
if (BN_bin2bn(digest, digest_len, &u1) == NULL) {
goto err;
}
// u1 = M * w mod q
if (!BN_mod_mul(&u1, &u1, &u2, dsa->q, ctx)) {
goto err;
}
// u2 = r * w mod q
if (!BN_mod_mul(&u2, sig->r, &u2, dsa->q, ctx)) {
goto err;
}
if (!BN_MONT_CTX_set_locked((BN_MONT_CTX **)&dsa->method_mont_p,
(CRYPTO_MUTEX *)&dsa->method_mont_lock, dsa->p,
ctx)) {
goto err;
}
if (!BN_mod_exp2_mont(&t1, dsa->g, &u1, dsa->pub_key, &u2, dsa->p, ctx,
dsa->method_mont_p)) {
goto err;
}
// BN_copy(&u1,&t1);
// let u1 = u1 mod q
if (!BN_mod(&u1, &t1, dsa->q, ctx)) {
goto err;
}
// V is now in u1. If the signature is correct, it will be
// equal to R.
*out_valid = BN_ucmp(&u1, sig->r) == 0;
ret = 1;
err:
if (ret != 1) {
OPENSSL_PUT_ERROR(DSA, ERR_R_BN_LIB);
}
BN_CTX_free(ctx);
BN_free(&u1);
BN_free(&u2);
BN_free(&t1);
return ret;
}
int DSA_sign(int type, const uint8_t *digest, size_t digest_len,
uint8_t *out_sig, unsigned int *out_siglen, const DSA *dsa) {
DSA_SIG *s;
s = DSA_do_sign(digest, digest_len, dsa);
if (s == NULL) {
*out_siglen = 0;
return 0;
}
*out_siglen = i2d_DSA_SIG(s, &out_sig);
DSA_SIG_free(s);
return 1;
}
int DSA_verify(int type, const uint8_t *digest, size_t digest_len,
const uint8_t *sig, size_t sig_len, const DSA *dsa) {
int valid;
if (!DSA_check_signature(&valid, digest, digest_len, sig, sig_len, dsa)) {
return -1;
}
return valid;
}
int DSA_check_signature(int *out_valid, const uint8_t *digest,
size_t digest_len, const uint8_t *sig, size_t sig_len,
const DSA *dsa) {
DSA_SIG *s = NULL;
int ret = 0;
uint8_t *der = NULL;
s = DSA_SIG_new();
if (s == NULL) {
goto err;
}
const uint8_t *sigp = sig;
if (d2i_DSA_SIG(&s, &sigp, sig_len) == NULL || sigp != sig + sig_len) {
goto err;
}
// Ensure that the signature uses DER and doesn't have trailing garbage.
int der_len = i2d_DSA_SIG(s, &der);
if (der_len < 0 || (size_t)der_len != sig_len ||
OPENSSL_memcmp(sig, der, sig_len)) {
goto err;
}
ret = DSA_do_check_signature(out_valid, digest, digest_len, s, dsa);
err:
OPENSSL_free(der);
DSA_SIG_free(s);
return ret;
}
// der_len_len returns the number of bytes needed to represent a length of |len|
// in DER.
static size_t der_len_len(size_t len) {
if (len < 0x80) {
return 1;
}
size_t ret = 1;
while (len > 0) {
ret++;
len >>= 8;
}
return ret;
}
int DSA_size(const DSA *dsa) {
if (dsa->q == NULL) {
return 0;
}
size_t order_len = BN_num_bytes(dsa->q);
// Compute the maximum length of an |order_len| byte integer. Defensively
// assume that the leading 0x00 is included.
size_t integer_len = 1 /* tag */ + der_len_len(order_len + 1) + 1 + order_len;
if (integer_len < order_len) {
return 0;
}
// A DSA signature is two INTEGERs.
size_t value_len = 2 * integer_len;
if (value_len < integer_len) {
return 0;
}
// Add the header.
size_t ret = 1 /* tag */ + der_len_len(value_len) + value_len;
if (ret < value_len) {
return 0;
}
return ret;
}
static int dsa_sign_setup(const DSA *dsa, BN_CTX *ctx, BIGNUM **out_kinv,
BIGNUM **out_r) {
int ret = 0;
BIGNUM k;
BN_init(&k);
BIGNUM *r = BN_new();
BIGNUM *kinv = BN_new();
if (r == NULL || kinv == NULL ||
// Get random k
!BN_rand_range_ex(&k, 1, dsa->q) ||
!BN_MONT_CTX_set_locked((BN_MONT_CTX **)&dsa->method_mont_p,
(CRYPTO_MUTEX *)&dsa->method_mont_lock, dsa->p,
ctx) ||
!BN_MONT_CTX_set_locked((BN_MONT_CTX **)&dsa->method_mont_q,
(CRYPTO_MUTEX *)&dsa->method_mont_lock, dsa->q,
ctx) ||
// Compute r = (g^k mod p) mod q
!BN_mod_exp_mont_consttime(r, dsa->g, &k, dsa->p, ctx,
dsa->method_mont_p)) {
OPENSSL_PUT_ERROR(DSA, ERR_R_BN_LIB);
goto err;
}
// Note |BN_mod| below is not constant-time and may leak information about
// |r|. |dsa->p| may be significantly larger than |dsa->q|, so this is not
// easily performed in constant-time with Montgomery reduction.
//
// However, |r| at this point is g^k (mod p). It is almost the value of |r|
// revealed in the signature anyway (g^k (mod p) (mod q)), going from it to
// |k| would require computing a discrete log.
bn_declassify(r);
if (!BN_mod(r, r, dsa->q, ctx) ||
// Compute part of 's = inv(k) (m + xr) mod q' using Fermat's Little
// Theorem.
!bn_mod_inverse_prime(kinv, &k, dsa->q, ctx, dsa->method_mont_q)) {
OPENSSL_PUT_ERROR(DSA, ERR_R_BN_LIB);
goto err;
}
BN_clear_free(*out_kinv);
*out_kinv = kinv;
kinv = NULL;
BN_clear_free(*out_r);
*out_r = r;
r = NULL;
ret = 1;
err:
BN_clear_free(&k);
BN_clear_free(r);
BN_clear_free(kinv);
return ret;
}
int DSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
CRYPTO_EX_dup *dup_unused, CRYPTO_EX_free *free_func) {
int index;
if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp,
free_func)) {
return -1;
}
return index;
}
int DSA_set_ex_data(DSA *dsa, int idx, void *arg) {
return CRYPTO_set_ex_data(&dsa->ex_data, idx, arg);
}
void *DSA_get_ex_data(const DSA *dsa, int idx) {
return CRYPTO_get_ex_data(&dsa->ex_data, idx);
}
DH *DSA_dup_DH(const DSA *dsa) {
if (dsa == NULL) {
return NULL;
}
DH *ret = DH_new();
if (ret == NULL) {
goto err;
}
if (dsa->q != NULL) {
ret->priv_length = BN_num_bits(dsa->q);
if ((ret->q = BN_dup(dsa->q)) == NULL) {
goto err;
}
}
if ((dsa->p != NULL && (ret->p = BN_dup(dsa->p)) == NULL) ||
(dsa->g != NULL && (ret->g = BN_dup(dsa->g)) == NULL) ||
(dsa->pub_key != NULL && (ret->pub_key = BN_dup(dsa->pub_key)) == NULL) ||
(dsa->priv_key != NULL &&
(ret->priv_key = BN_dup(dsa->priv_key)) == NULL)) {
goto err;
}
return ret;
err:
DH_free(ret);
return NULL;
}

View File

@@ -0,0 +1,370 @@
// Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project 2000
// Copyright (c) 2000-2005 The OpenSSL Project. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
#include <openssl/dsa.h>
#include <assert.h>
#include <openssl/bn.h>
#include <openssl/bytestring.h>
#include <openssl/err.h>
#include <openssl/mem.h>
#include "internal.h"
#include "../bytestring/internal.h"
#include "../crypto/internal.h"
// This function is in dsa_asn1.c rather than dsa.c because it is reachable from
// |EVP_PKEY| parsers. This makes it easier for the static linker to drop most
// of the DSA implementation.
int dsa_check_key(const DSA *dsa) {
if (!dsa->p || !dsa->q || !dsa->g) {
OPENSSL_PUT_ERROR(DSA, DSA_R_MISSING_PARAMETERS);
return 0;
}
// Fully checking for invalid DSA groups is expensive, so security and
// correctness of the signature scheme depend on how |dsa| was computed. I.e.
// we leave "assurance of domain parameter validity" from FIPS 186-4 to the
// caller. However, we check bounds on all values to avoid DoS vectors even
// when domain parameters are invalid. In particular, signing will infinite
// loop if |g| is zero.
if (BN_is_negative(dsa->p) || BN_is_negative(dsa->q) || BN_is_zero(dsa->p) ||
BN_is_zero(dsa->q) || !BN_is_odd(dsa->p) || !BN_is_odd(dsa->q) ||
// |q| must be a prime divisor of |p - 1|, which implies |q < p|.
BN_cmp(dsa->q, dsa->p) >= 0 ||
// |g| is in the multiplicative group of |p|.
BN_is_negative(dsa->g) || BN_is_zero(dsa->g) ||
BN_cmp(dsa->g, dsa->p) >= 0) {
OPENSSL_PUT_ERROR(DSA, DSA_R_INVALID_PARAMETERS);
return 0;
}
// FIPS 186-4 allows only three different sizes for q.
unsigned q_bits = BN_num_bits(dsa->q);
if (q_bits != 160 && q_bits != 224 && q_bits != 256) {
OPENSSL_PUT_ERROR(DSA, DSA_R_BAD_Q_VALUE);
return 0;
}
// Bound |dsa->p| to avoid a DoS vector. Note this limit is much larger than
// the one in FIPS 186-4, which only allows L = 1024, 2048, and 3072.
if (BN_num_bits(dsa->p) > OPENSSL_DSA_MAX_MODULUS_BITS) {
OPENSSL_PUT_ERROR(DSA, DSA_R_MODULUS_TOO_LARGE);
return 0;
}
if (dsa->pub_key != NULL) {
// The public key is also in the multiplicative group of |p|.
if (BN_is_negative(dsa->pub_key) || BN_is_zero(dsa->pub_key) ||
BN_cmp(dsa->pub_key, dsa->p) >= 0) {
OPENSSL_PUT_ERROR(DSA, DSA_R_INVALID_PARAMETERS);
return 0;
}
}
if (dsa->priv_key != NULL) {
// The private key is a non-zero element of the scalar field, determined by
// |q|.
if (BN_is_negative(dsa->priv_key) ||
constant_time_declassify_int(BN_is_zero(dsa->priv_key)) ||
constant_time_declassify_int(BN_cmp(dsa->priv_key, dsa->q) >= 0)) {
OPENSSL_PUT_ERROR(DSA, DSA_R_INVALID_PARAMETERS);
return 0;
}
}
return 1;
}
static int parse_integer(CBS *cbs, BIGNUM **out) {
assert(*out == NULL);
*out = BN_new();
if (*out == NULL) {
return 0;
}
return BN_parse_asn1_unsigned(cbs, *out);
}
static int marshal_integer(CBB *cbb, BIGNUM *bn) {
if (bn == NULL) {
// A DSA object may be missing some components.
OPENSSL_PUT_ERROR(DSA, ERR_R_PASSED_NULL_PARAMETER);
return 0;
}
return BN_marshal_asn1(cbb, bn);
}
DSA_SIG *DSA_SIG_parse(CBS *cbs) {
DSA_SIG *ret = DSA_SIG_new();
if (ret == NULL) {
return NULL;
}
CBS child;
if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) ||
!parse_integer(&child, &ret->r) ||
!parse_integer(&child, &ret->s) ||
CBS_len(&child) != 0) {
OPENSSL_PUT_ERROR(DSA, DSA_R_DECODE_ERROR);
DSA_SIG_free(ret);
return NULL;
}
return ret;
}
int DSA_SIG_marshal(CBB *cbb, const DSA_SIG *sig) {
CBB child;
if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) ||
!marshal_integer(&child, sig->r) ||
!marshal_integer(&child, sig->s) ||
!CBB_flush(cbb)) {
OPENSSL_PUT_ERROR(DSA, DSA_R_ENCODE_ERROR);
return 0;
}
return 1;
}
DSA *DSA_parse_public_key(CBS *cbs) {
DSA *ret = DSA_new();
if (ret == NULL) {
return NULL;
}
CBS child;
if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) ||
!parse_integer(&child, &ret->pub_key) ||
!parse_integer(&child, &ret->p) ||
!parse_integer(&child, &ret->q) ||
!parse_integer(&child, &ret->g) ||
CBS_len(&child) != 0) {
OPENSSL_PUT_ERROR(DSA, DSA_R_DECODE_ERROR);
goto err;
}
if (!dsa_check_key(ret)) {
goto err;
}
return ret;
err:
DSA_free(ret);
return NULL;
}
int DSA_marshal_public_key(CBB *cbb, const DSA *dsa) {
CBB child;
if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) ||
!marshal_integer(&child, dsa->pub_key) ||
!marshal_integer(&child, dsa->p) ||
!marshal_integer(&child, dsa->q) ||
!marshal_integer(&child, dsa->g) ||
!CBB_flush(cbb)) {
OPENSSL_PUT_ERROR(DSA, DSA_R_ENCODE_ERROR);
return 0;
}
return 1;
}
DSA *DSA_parse_parameters(CBS *cbs) {
DSA *ret = DSA_new();
if (ret == NULL) {
return NULL;
}
CBS child;
if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) ||
!parse_integer(&child, &ret->p) ||
!parse_integer(&child, &ret->q) ||
!parse_integer(&child, &ret->g) ||
CBS_len(&child) != 0) {
OPENSSL_PUT_ERROR(DSA, DSA_R_DECODE_ERROR);
goto err;
}
if (!dsa_check_key(ret)) {
goto err;
}
return ret;
err:
DSA_free(ret);
return NULL;
}
int DSA_marshal_parameters(CBB *cbb, const DSA *dsa) {
CBB child;
if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) ||
!marshal_integer(&child, dsa->p) ||
!marshal_integer(&child, dsa->q) ||
!marshal_integer(&child, dsa->g) ||
!CBB_flush(cbb)) {
OPENSSL_PUT_ERROR(DSA, DSA_R_ENCODE_ERROR);
return 0;
}
return 1;
}
DSA *DSA_parse_private_key(CBS *cbs) {
DSA *ret = DSA_new();
if (ret == NULL) {
return NULL;
}
CBS child;
uint64_t version;
if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) ||
!CBS_get_asn1_uint64(&child, &version)) {
OPENSSL_PUT_ERROR(DSA, DSA_R_DECODE_ERROR);
goto err;
}
if (version != 0) {
OPENSSL_PUT_ERROR(DSA, DSA_R_BAD_VERSION);
goto err;
}
if (!parse_integer(&child, &ret->p) ||
!parse_integer(&child, &ret->q) ||
!parse_integer(&child, &ret->g) ||
!parse_integer(&child, &ret->pub_key) ||
!parse_integer(&child, &ret->priv_key) ||
CBS_len(&child) != 0) {
OPENSSL_PUT_ERROR(DSA, DSA_R_DECODE_ERROR);
goto err;
}
if (!dsa_check_key(ret)) {
goto err;
}
return ret;
err:
DSA_free(ret);
return NULL;
}
int DSA_marshal_private_key(CBB *cbb, const DSA *dsa) {
CBB child;
if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) ||
!CBB_add_asn1_uint64(&child, 0 /* version */) ||
!marshal_integer(&child, dsa->p) ||
!marshal_integer(&child, dsa->q) ||
!marshal_integer(&child, dsa->g) ||
!marshal_integer(&child, dsa->pub_key) ||
!marshal_integer(&child, dsa->priv_key) ||
!CBB_flush(cbb)) {
OPENSSL_PUT_ERROR(DSA, DSA_R_ENCODE_ERROR);
return 0;
}
return 1;
}
DSA_SIG *d2i_DSA_SIG(DSA_SIG **out_sig, const uint8_t **inp, long len) {
if (len < 0) {
return NULL;
}
CBS cbs;
CBS_init(&cbs, *inp, (size_t)len);
DSA_SIG *ret = DSA_SIG_parse(&cbs);
if (ret == NULL) {
return NULL;
}
if (out_sig != NULL) {
DSA_SIG_free(*out_sig);
*out_sig = ret;
}
*inp = CBS_data(&cbs);
return ret;
}
int i2d_DSA_SIG(const DSA_SIG *in, uint8_t **outp) {
CBB cbb;
if (!CBB_init(&cbb, 0) ||
!DSA_SIG_marshal(&cbb, in)) {
CBB_cleanup(&cbb);
return -1;
}
return CBB_finish_i2d(&cbb, outp);
}
DSA *d2i_DSAPublicKey(DSA **out, const uint8_t **inp, long len) {
if (len < 0) {
return NULL;
}
CBS cbs;
CBS_init(&cbs, *inp, (size_t)len);
DSA *ret = DSA_parse_public_key(&cbs);
if (ret == NULL) {
return NULL;
}
if (out != NULL) {
DSA_free(*out);
*out = ret;
}
*inp = CBS_data(&cbs);
return ret;
}
int i2d_DSAPublicKey(const DSA *in, uint8_t **outp) {
CBB cbb;
if (!CBB_init(&cbb, 0) ||
!DSA_marshal_public_key(&cbb, in)) {
CBB_cleanup(&cbb);
return -1;
}
return CBB_finish_i2d(&cbb, outp);
}
DSA *d2i_DSAPrivateKey(DSA **out, const uint8_t **inp, long len) {
if (len < 0) {
return NULL;
}
CBS cbs;
CBS_init(&cbs, *inp, (size_t)len);
DSA *ret = DSA_parse_private_key(&cbs);
if (ret == NULL) {
return NULL;
}
if (out != NULL) {
DSA_free(*out);
*out = ret;
}
*inp = CBS_data(&cbs);
return ret;
}
int i2d_DSAPrivateKey(const DSA *in, uint8_t **outp) {
CBB cbb;
if (!CBB_init(&cbb, 0) ||
!DSA_marshal_private_key(&cbb, in)) {
CBB_cleanup(&cbb);
return -1;
}
return CBB_finish_i2d(&cbb, outp);
}
DSA *d2i_DSAparams(DSA **out, const uint8_t **inp, long len) {
if (len < 0) {
return NULL;
}
CBS cbs;
CBS_init(&cbs, *inp, (size_t)len);
DSA *ret = DSA_parse_parameters(&cbs);
if (ret == NULL) {
return NULL;
}
if (out != NULL) {
DSA_free(*out);
*out = ret;
}
*inp = CBS_data(&cbs);
return ret;
}
int i2d_DSAparams(const DSA *in, uint8_t **outp) {
CBB cbb;
if (!CBB_init(&cbb, 0) ||
!DSA_marshal_parameters(&cbb, in)) {
CBB_cleanup(&cbb);
return -1;
}
return CBB_finish_i2d(&cbb, outp);
}

View File

@@ -0,0 +1,442 @@
// Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
// The DSS routines are based on patches supplied by Steven Schoch <schoch@sheba.arc.nasa.gov>.
// SPDX-License-Identifier: Apache-2.0
#include <openssl/dsa.h>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <gtest/gtest.h>
#include <openssl/bn.h>
#include <openssl/crypto.h>
#include <openssl/err.h>
#include <openssl/pem.h>
#include <openssl/span.h>
#include "../test/test_util.h"
// The following values are taken from the updated Appendix 5 to FIPS PUB 186
// and also appear in Appendix 5 to FIPS PUB 186-1.
static const uint8_t seed[20] = {
0xd5, 0x01, 0x4e, 0x4b, 0x60, 0xef, 0x2b, 0xa8, 0xb6, 0x21, 0x1b,
0x40, 0x62, 0xba, 0x32, 0x24, 0xe0, 0x42, 0x7d, 0xd3,
};
static const uint8_t fips_p[] = {
0x8d, 0xf2, 0xa4, 0x94, 0x49, 0x22, 0x76, 0xaa, 0x3d, 0x25, 0x75,
0x9b, 0xb0, 0x68, 0x69, 0xcb, 0xea, 0xc0, 0xd8, 0x3a, 0xfb, 0x8d,
0x0c, 0xf7, 0xcb, 0xb8, 0x32, 0x4f, 0x0d, 0x78, 0x82, 0xe5, 0xd0,
0x76, 0x2f, 0xc5, 0xb7, 0x21, 0x0e, 0xaf, 0xc2, 0xe9, 0xad, 0xac,
0x32, 0xab, 0x7a, 0xac, 0x49, 0x69, 0x3d, 0xfb, 0xf8, 0x37, 0x24,
0xc2, 0xec, 0x07, 0x36, 0xee, 0x31, 0xc8, 0x02, 0x91,
};
static const uint8_t fips_q[] = {
0xc7, 0x73, 0x21, 0x8c, 0x73, 0x7e, 0xc8, 0xee, 0x99, 0x3b, 0x4f,
0x2d, 0xed, 0x30, 0xf4, 0x8e, 0xda, 0xce, 0x91, 0x5f,
};
static const uint8_t fips_g[] = {
0x62, 0x6d, 0x02, 0x78, 0x39, 0xea, 0x0a, 0x13, 0x41, 0x31, 0x63,
0xa5, 0x5b, 0x4c, 0xb5, 0x00, 0x29, 0x9d, 0x55, 0x22, 0x95, 0x6c,
0xef, 0xcb, 0x3b, 0xff, 0x10, 0xf3, 0x99, 0xce, 0x2c, 0x2e, 0x71,
0xcb, 0x9d, 0xe5, 0xfa, 0x24, 0xba, 0xbf, 0x58, 0xe5, 0xb7, 0x95,
0x21, 0x92, 0x5c, 0x9c, 0xc4, 0x2e, 0x9f, 0x6f, 0x46, 0x4b, 0x08,
0x8c, 0xc5, 0x72, 0xaf, 0x53, 0xe6, 0xd7, 0x88, 0x02,
};
static const uint8_t fips_x[] = {
0x20, 0x70, 0xb3, 0x22, 0x3d, 0xba, 0x37, 0x2f, 0xde, 0x1c, 0x0f,
0xfc, 0x7b, 0x2e, 0x3b, 0x49, 0x8b, 0x26, 0x06, 0x14,
};
static const uint8_t fips_y[] = {
0x19, 0x13, 0x18, 0x71, 0xd7, 0x5b, 0x16, 0x12, 0xa8, 0x19, 0xf2,
0x9d, 0x78, 0xd1, 0xb0, 0xd7, 0x34, 0x6f, 0x7a, 0xa7, 0x7b, 0xb6,
0x2a, 0x85, 0x9b, 0xfd, 0x6c, 0x56, 0x75, 0xda, 0x9d, 0x21, 0x2d,
0x3a, 0x36, 0xef, 0x16, 0x72, 0xef, 0x66, 0x0b, 0x8c, 0x7c, 0x25,
0x5c, 0xc0, 0xec, 0x74, 0x85, 0x8f, 0xba, 0x33, 0xf4, 0x4c, 0x06,
0x69, 0x96, 0x30, 0xa7, 0x6b, 0x03, 0x0e, 0xe3, 0x33,
};
static const uint8_t fips_digest[] = {
0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a, 0xba, 0x3e, 0x25,
0x71, 0x78, 0x50, 0xc2, 0x6c, 0x9c, 0xd0, 0xd8, 0x9d,
};
// fips_sig is a DER-encoded version of the r and s values in FIPS PUB 186-1.
static const uint8_t fips_sig[] = {
0x30, 0x2d, 0x02, 0x15, 0x00, 0x8b, 0xac, 0x1a, 0xb6, 0x64, 0x10,
0x43, 0x5c, 0xb7, 0x18, 0x1f, 0x95, 0xb1, 0x6a, 0xb9, 0x7c, 0x92,
0xb3, 0x41, 0xc0, 0x02, 0x14, 0x41, 0xe2, 0x34, 0x5f, 0x1f, 0x56,
0xdf, 0x24, 0x58, 0xf4, 0x26, 0xd1, 0x55, 0xb4, 0xba, 0x2d, 0xb6,
0xdc, 0xd8, 0xc8,
};
// fips_sig_negative is fips_sig with r encoded as a negative number.
static const uint8_t fips_sig_negative[] = {
0x30, 0x2c, 0x02, 0x14, 0x8b, 0xac, 0x1a, 0xb6, 0x64, 0x10, 0x43,
0x5c, 0xb7, 0x18, 0x1f, 0x95, 0xb1, 0x6a, 0xb9, 0x7c, 0x92, 0xb3,
0x41, 0xc0, 0x02, 0x14, 0x41, 0xe2, 0x34, 0x5f, 0x1f, 0x56, 0xdf,
0x24, 0x58, 0xf4, 0x26, 0xd1, 0x55, 0xb4, 0xba, 0x2d, 0xb6, 0xdc,
0xd8, 0xc8,
};
// fip_sig_extra is fips_sig with trailing data.
static const uint8_t fips_sig_extra[] = {
0x30, 0x2d, 0x02, 0x15, 0x00, 0x8b, 0xac, 0x1a, 0xb6, 0x64, 0x10,
0x43, 0x5c, 0xb7, 0x18, 0x1f, 0x95, 0xb1, 0x6a, 0xb9, 0x7c, 0x92,
0xb3, 0x41, 0xc0, 0x02, 0x14, 0x41, 0xe2, 0x34, 0x5f, 0x1f, 0x56,
0xdf, 0x24, 0x58, 0xf4, 0x26, 0xd1, 0x55, 0xb4, 0xba, 0x2d, 0xb6,
0xdc, 0xd8, 0xc8, 0x00,
};
// fips_sig_lengths is fips_sig with a non-minimally encoded length.
static const uint8_t fips_sig_bad_length[] = {
0x30, 0x81, 0x2d, 0x02, 0x15, 0x00, 0x8b, 0xac, 0x1a, 0xb6, 0x64,
0x10, 0x43, 0x5c, 0xb7, 0x18, 0x1f, 0x95, 0xb1, 0x6a, 0xb9, 0x7c,
0x92, 0xb3, 0x41, 0xc0, 0x02, 0x14, 0x41, 0xe2, 0x34, 0x5f, 0x1f,
0x56, 0xdf, 0x24, 0x58, 0xf4, 0x26, 0xd1, 0x55, 0xb4, 0xba, 0x2d,
0xb6, 0xdc, 0xd8, 0xc8, 0x00,
};
// fips_sig_bad_r is fips_sig with a bad r value.
static const uint8_t fips_sig_bad_r[] = {
0x30, 0x2d, 0x02, 0x15, 0x00, 0x8c, 0xac, 0x1a, 0xb6, 0x64, 0x10,
0x43, 0x5c, 0xb7, 0x18, 0x1f, 0x95, 0xb1, 0x6a, 0xb9, 0x7c, 0x92,
0xb3, 0x41, 0xc0, 0x02, 0x14, 0x41, 0xe2, 0x34, 0x5f, 0x1f, 0x56,
0xdf, 0x24, 0x58, 0xf4, 0x26, 0xd1, 0x55, 0xb4, 0xba, 0x2d, 0xb6,
0xdc, 0xd8, 0xc8,
};
static bssl::UniquePtr<DSA> GetFIPSDSAGroup(void) {
bssl::UniquePtr<DSA> dsa(DSA_new());
if (!dsa) {
return nullptr;
}
bssl::UniquePtr<BIGNUM> p(BN_bin2bn(fips_p, sizeof(fips_p), nullptr));
bssl::UniquePtr<BIGNUM> q(BN_bin2bn(fips_q, sizeof(fips_q), nullptr));
bssl::UniquePtr<BIGNUM> g(BN_bin2bn(fips_g, sizeof(fips_g), nullptr));
if (!p || !q || !g || !DSA_set0_pqg(dsa.get(), p.get(), q.get(), g.get())) {
return nullptr;
}
// |DSA_set0_pqg| takes ownership.
p.release();
q.release();
g.release();
return dsa;
}
static bssl::UniquePtr<DSA> GetFIPSDSA(void) {
bssl::UniquePtr<DSA> dsa = GetFIPSDSAGroup();
if (!dsa) {
return nullptr;
}
bssl::UniquePtr<BIGNUM> pub_key(BN_bin2bn(fips_y, sizeof(fips_y), nullptr));
bssl::UniquePtr<BIGNUM> priv_key(BN_bin2bn(fips_x, sizeof(fips_x), nullptr));
if (!pub_key || !priv_key ||
!DSA_set0_key(dsa.get(), pub_key.get(), priv_key.get())) {
return nullptr;
}
// |DSA_set0_key| takes ownership.
pub_key.release();
priv_key.release();
return dsa;
}
TEST(DSATest, Generate) {
bssl::UniquePtr<DSA> dsa(DSA_new());
ASSERT_TRUE(dsa);
int counter;
unsigned long h;
ASSERT_TRUE(DSA_generate_parameters_ex(dsa.get(), 512, seed, 20, &counter, &h,
nullptr));
EXPECT_EQ(counter, 105);
EXPECT_EQ(h, 2u);
auto expect_bn_bytes = [](const char *msg, const BIGNUM *bn,
bssl::Span<const uint8_t> bytes) {
std::vector<uint8_t> buf(BN_num_bytes(bn));
BN_bn2bin(bn, buf.data());
EXPECT_EQ(Bytes(buf), Bytes(bytes)) << msg;
};
expect_bn_bytes("q value is wrong", DSA_get0_q(dsa.get()), fips_q);
expect_bn_bytes("p value is wrong", DSA_get0_p(dsa.get()), fips_p);
expect_bn_bytes("g value is wrong", DSA_get0_g(dsa.get()), fips_g);
ASSERT_TRUE(DSA_generate_key(dsa.get()));
std::vector<uint8_t> sig(DSA_size(dsa.get()));
unsigned sig_len;
ASSERT_TRUE(DSA_sign(0, fips_digest, sizeof(fips_digest), sig.data(),
&sig_len, dsa.get()));
EXPECT_EQ(1, DSA_verify(0, fips_digest, sizeof(fips_digest), sig.data(),
sig_len, dsa.get()));
}
TEST(DSATest, GenerateParamsTooLarge) {
bssl::UniquePtr<DSA> dsa(DSA_new());
ASSERT_TRUE(dsa);
EXPECT_FALSE(DSA_generate_parameters_ex(
dsa.get(), 10001, /*seed=*/nullptr, /*seed_len=*/0,
/*out_counter=*/nullptr, /*out_h=*/nullptr,
/*cb=*/nullptr));
}
TEST(DSATest, GenerateKeyTooLarge) {
bssl::UniquePtr<DSA> dsa = GetFIPSDSA();
ASSERT_TRUE(dsa);
bssl::UniquePtr<BIGNUM> large_p(BN_new());
ASSERT_TRUE(large_p);
ASSERT_TRUE(BN_set_bit(large_p.get(), 10001));
ASSERT_TRUE(BN_set_bit(large_p.get(), 0));
ASSERT_TRUE(DSA_set0_pqg(dsa.get(), /*p=*/large_p.get(), /*q=*/nullptr,
/*g=*/nullptr));
large_p.release(); // |DSA_set0_pqg| takes ownership on success.
// Don't generate DSA keys if the group is too large.
EXPECT_FALSE(DSA_generate_key(dsa.get()));
}
TEST(DSATest, Verify) {
bssl::UniquePtr<DSA> dsa = GetFIPSDSA();
ASSERT_TRUE(dsa);
EXPECT_EQ(1, DSA_verify(0, fips_digest, sizeof(fips_digest), fips_sig,
sizeof(fips_sig), dsa.get()));
EXPECT_EQ(-1,
DSA_verify(0, fips_digest, sizeof(fips_digest), fips_sig_negative,
sizeof(fips_sig_negative), dsa.get()));
EXPECT_EQ(-1, DSA_verify(0, fips_digest, sizeof(fips_digest), fips_sig_extra,
sizeof(fips_sig_extra), dsa.get()));
EXPECT_EQ(-1,
DSA_verify(0, fips_digest, sizeof(fips_digest), fips_sig_bad_length,
sizeof(fips_sig_bad_length), dsa.get()));
EXPECT_EQ(0, DSA_verify(0, fips_digest, sizeof(fips_digest), fips_sig_bad_r,
sizeof(fips_sig_bad_r), dsa.get()));
}
TEST(DSATest, InvalidGroup) {
bssl::UniquePtr<DSA> dsa = GetFIPSDSA();
ASSERT_TRUE(dsa);
bssl::UniquePtr<BIGNUM> zero(BN_new());
ASSERT_TRUE(zero);
ASSERT_TRUE(DSA_set0_pqg(dsa.get(), /*p=*/nullptr, /*q=*/nullptr,
/*g=*/zero.release()));
std::vector<uint8_t> sig(DSA_size(dsa.get()));
unsigned sig_len;
static const uint8_t kDigest[32] = {0};
EXPECT_FALSE(
DSA_sign(0, kDigest, sizeof(kDigest), sig.data(), &sig_len, dsa.get()));
EXPECT_TRUE(
ErrorEquals(ERR_get_error(), ERR_LIB_DSA, DSA_R_INVALID_PARAMETERS));
}
// Signing and verifying should cleanly fail when the DSA object is empty.
TEST(DSATest, MissingParameters) {
bssl::UniquePtr<DSA> dsa(DSA_new());
ASSERT_TRUE(dsa);
EXPECT_EQ(-1, DSA_verify(0, fips_digest, sizeof(fips_digest), fips_sig,
sizeof(fips_sig), dsa.get()));
std::vector<uint8_t> sig(DSA_size(dsa.get()));
unsigned sig_len;
EXPECT_FALSE(DSA_sign(0, fips_digest, sizeof(fips_digest), sig.data(),
&sig_len, dsa.get()));
}
// Verifying should cleanly fail when the public key is missing.
TEST(DSATest, MissingPublic) {
bssl::UniquePtr<DSA> dsa = GetFIPSDSAGroup();
ASSERT_TRUE(dsa);
EXPECT_EQ(-1, DSA_verify(0, fips_digest, sizeof(fips_digest), fips_sig,
sizeof(fips_sig), dsa.get()));
}
// Signing should cleanly fail when the private key is missing.
TEST(DSATest, MissingPrivate) {
bssl::UniquePtr<DSA> dsa = GetFIPSDSAGroup();
ASSERT_TRUE(dsa);
std::vector<uint8_t> sig(DSA_size(dsa.get()));
unsigned sig_len;
EXPECT_FALSE(DSA_sign(0, fips_digest, sizeof(fips_digest), sig.data(),
&sig_len, dsa.get()));
}
// A zero private key is invalid and can cause signing to loop forever.
TEST(DSATest, ZeroPrivateKey) {
bssl::UniquePtr<DSA> dsa = GetFIPSDSA();
ASSERT_TRUE(dsa);
bssl::UniquePtr<BIGNUM> zero(BN_new());
ASSERT_TRUE(zero);
ASSERT_TRUE(DSA_set0_key(dsa.get(), /*pub_key=*/nullptr,
/*priv_key=*/zero.release()));
static const uint8_t kZeroDigest[32] = {0};
std::vector<uint8_t> sig(DSA_size(dsa.get()));
unsigned sig_len;
EXPECT_FALSE(DSA_sign(0, kZeroDigest, sizeof(kZeroDigest), sig.data(),
&sig_len, dsa.get()));
}
// If the "field" is actually a ring and the "generator" of the multiplicative
// subgroup is actually nilpotent with low degree, DSA signing never completes.
// Test that we give up in the infinite loop.
TEST(DSATest, NilpotentGenerator) {
static const char kPEM[] = R"(
-----BEGIN DSA PRIVATE KEY-----
MGECAQACFQHH+MnFXh4NNlZiV/zUVb5a5ib3kwIVAOP8ZOKvDwabKzEr/moq3y1z
E3vJAhUAl/2Ylx9fWbzHdh1URsc/c6IM/TECAQECFCsjU4AZRcuks45g1NMOUeCB
Epvg
-----END DSA PRIVATE KEY-----
)";
bssl::UniquePtr<BIO> bio(BIO_new_mem_buf(kPEM, sizeof(kPEM)));
ASSERT_TRUE(bio);
bssl::UniquePtr<DSA> dsa(
PEM_read_bio_DSAPrivateKey(bio.get(), nullptr, nullptr, nullptr));
ASSERT_TRUE(dsa);
std::vector<uint8_t> sig(DSA_size(dsa.get()));
unsigned sig_len;
EXPECT_FALSE(DSA_sign(0, fips_digest, sizeof(fips_digest), sig.data(),
&sig_len, dsa.get()));
}
TEST(DSATest, Overwrite) {
// Load an arbitrary DSA private key and use it.
static const char kPEM[] = R"(
-----BEGIN DSA PRIVATE KEY-----
MIIDTgIBAAKCAQEAyH68EuravtF+7PTFBtWJkwjmp0YJmh8e2Cdpu8ci3dZf87rk
GwXzfqYkAEkW5H4Hp0cxdICKFiqfxjSaiEauOrNV+nXWZS634hZ9H47I8HnAVS0p
5MmSmPJ7NNUowymMpyB6M6hfqHl/1pZd7avbTmnzb2SZ0kw0WLWJo6vMekepYWv9
3o1Xove4ci00hnkr7Qo9Bh/+z84jgeT2/MTdsCVtbuMv/mbcYLhCKVWPBozDZr/D
qwhGTlomsTRvP3WIbem3b5eYhQaPuMsKiAzntcinoxQXWrIoZB+xJyF/sI013uBI
i9ePSxY3704U4QGxVM0aR/6fzORz5kh8ZjhhywIdAI9YBUR6eoGevUaLq++qXiYW
TgXBXlyqE32ESbkCggEBAL/c5GerO5g25D0QsfgVIJtlZHQOwYauuWoUudaQiyf6
VhWLBNNTAGldkFGdtxsA42uqqZSXCki25LvN6PscGGvFy8oPWaa9TGt+l9Z5ZZiV
ShNpg71V9YuImsPB3BrQ4L6nZLfhBt6InzJ6KqjDNdg7u6lgnFKue7l6khzqNxbM
RgxHWMq7PkhMcl+RzpqbiGcxSHqraxldutqCWsnZzhKh4d4GdunuRY8GiFo0Axkb
Kn0Il3zm81ewv08F/ocu+IZQEzxTyR8YRQ99MLVbnwhVxndEdLjjetCX82l+/uEY
5fdUy0thR8odcDsvUc/tT57I+yhnno80HbpUUNw2+/sCggEAdh1wp/9CifYIp6T8
P/rIus6KberZ2Pv/n0bl+Gv8AoToA0zhZXIfY2l0TtanKmdLqPIvjqkN0v6zGSs+
+ahR1QzMQnK718mcsQmB4X6iP5LKgJ/t0g8LrDOxc/cNycmHq76MmF9RN5NEBz4+
PAnRIftm/b0UQflP6uy3gRQP2X7P8ZebCytOPKTZC4oLyCtvPevSkCiiauq/RGjL
k6xqRgLxMtmuyhT+dcVbtllV1p1xd9Bppnk17/kR5VCefo/e/7DHu163izRDW8tx
SrEmiVyVkRijY3bVZii7LPfMz5eEAWEDJRuFwyNv3i6j7CKeZw2d/hzu370Ua28F
s2lmkAIcLIFUDFrbC2nViaB5ATM9ARKk6F2QwnCfGCyZ6A==
-----END DSA PRIVATE KEY-----
)";
bssl::UniquePtr<BIO> bio(BIO_new_mem_buf(kPEM, sizeof(kPEM)));
ASSERT_TRUE(bio);
bssl::UniquePtr<DSA> dsa(
PEM_read_bio_DSAPrivateKey(bio.get(), nullptr, nullptr, nullptr));
ASSERT_TRUE(dsa);
std::vector<uint8_t> sig(DSA_size(dsa.get()));
unsigned sig_len;
ASSERT_TRUE(DSA_sign(0, fips_digest, sizeof(fips_digest), sig.data(),
&sig_len, dsa.get()));
sig.resize(sig_len);
EXPECT_EQ(1, DSA_verify(0, fips_digest, sizeof(fips_digest), sig.data(),
sig.size(), dsa.get()));
// Overwrite it with the sample key.
bssl::UniquePtr<BIGNUM> p(BN_bin2bn(fips_p, sizeof(fips_p), nullptr));
ASSERT_TRUE(p);
bssl::UniquePtr<BIGNUM> q(BN_bin2bn(fips_q, sizeof(fips_q), nullptr));
ASSERT_TRUE(q);
bssl::UniquePtr<BIGNUM> g(BN_bin2bn(fips_g, sizeof(fips_g), nullptr));
ASSERT_TRUE(g);
ASSERT_TRUE(DSA_set0_pqg(dsa.get(), p.get(), q.get(), g.get()));
// |DSA_set0_pqg| takes ownership on success.
p.release();
q.release();
g.release();
bssl::UniquePtr<BIGNUM> pub_key(BN_bin2bn(fips_y, sizeof(fips_y), nullptr));
ASSERT_TRUE(pub_key);
bssl::UniquePtr<BIGNUM> priv_key(BN_bin2bn(fips_x, sizeof(fips_x), nullptr));
ASSERT_TRUE(priv_key);
ASSERT_TRUE(DSA_set0_key(dsa.get(), pub_key.get(), priv_key.get()));
// |DSA_set0_key| takes ownership on success.
pub_key.release();
priv_key.release();
// The key should now work correctly for the new parameters.
EXPECT_EQ(1, DSA_verify(0, fips_digest, sizeof(fips_digest), fips_sig,
sizeof(fips_sig), dsa.get()));
// Test signing by verifying it round-trips through the real key.
sig.resize(DSA_size(dsa.get()));
ASSERT_TRUE(DSA_sign(0, fips_digest, sizeof(fips_digest), sig.data(),
&sig_len, dsa.get()));
sig.resize(sig_len);
dsa = GetFIPSDSA();
ASSERT_TRUE(dsa);
EXPECT_EQ(1, DSA_verify(0, fips_digest, sizeof(fips_digest), sig.data(),
sig.size(), dsa.get()));
}
TEST(DSATest, DSAPrint) {
bssl::UniquePtr<DSA> dsa = GetFIPSDSA();
ASSERT_TRUE(dsa);
bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem()));
ASSERT_TRUE(bio);
DSA_print(bio.get(), dsa.get(), 4);
const uint8_t *data;
size_t len;
BIO_mem_contents(bio.get(), &data, &len);
const char *expected = ""
" Private-Key: (512 bit)\n"
" priv:\n"
" 20:70:b3:22:3d:ba:37:2f:de:1c:0f:fc:7b:2e:3b:\n"
" 49:8b:26:06:14\n"
" pub:\n"
" 19:13:18:71:d7:5b:16:12:a8:19:f2:9d:78:d1:b0:\n"
" d7:34:6f:7a:a7:7b:b6:2a:85:9b:fd:6c:56:75:da:\n"
" 9d:21:2d:3a:36:ef:16:72:ef:66:0b:8c:7c:25:5c:\n"
" c0:ec:74:85:8f:ba:33:f4:4c:06:69:96:30:a7:6b:\n"
" 03:0e:e3:33\n"
" P:\n"
" 00:8d:f2:a4:94:49:22:76:aa:3d:25:75:9b:b0:68:\n"
" 69:cb:ea:c0:d8:3a:fb:8d:0c:f7:cb:b8:32:4f:0d:\n"
" 78:82:e5:d0:76:2f:c5:b7:21:0e:af:c2:e9:ad:ac:\n"
" 32:ab:7a:ac:49:69:3d:fb:f8:37:24:c2:ec:07:36:\n"
" ee:31:c8:02:91\n"
" Q:\n"
" 00:c7:73:21:8c:73:7e:c8:ee:99:3b:4f:2d:ed:30:\n"
" f4:8e:da:ce:91:5f\n"
" G:\n"
" 62:6d:02:78:39:ea:0a:13:41:31:63:a5:5b:4c:b5:\n"
" 00:29:9d:55:22:95:6c:ef:cb:3b:ff:10:f3:99:ce:\n"
" 2c:2e:71:cb:9d:e5:fa:24:ba:bf:58:e5:b7:95:21:\n"
" 92:5c:9c:c4:2e:9f:6f:46:4b:08:8c:c5:72:af:53:\n"
" e6:d7:88:02\n";
ASSERT_EQ(Bytes(expected), Bytes(data, len));
#if !defined(OPENSSL_ANDROID)
// On Android, when running from an APK, |tmpfile| does not work. See
// b/36991167#comment8.
TempFILE tmp = createTempFILE();
ASSERT_TRUE(tmp);
ASSERT_TRUE(DSA_print_fp(tmp.get(), dsa.get(), 4));
fseek(tmp.get(), 0, SEEK_END);
long fileSize = ftell(tmp.get());
ASSERT_GT(fileSize, 0);
rewind(tmp.get());
std::unique_ptr<uint8_t[]> buf(new uint8_t[fileSize]);
size_t bytesRead = fread(buf.get(), 1, fileSize, tmp.get());
ASSERT_EQ(bytesRead, (size_t)fileSize);
ASSERT_EQ(Bytes(expected), Bytes(buf.get(), fileSize));
#endif
}

View File

@@ -0,0 +1,44 @@
// Copyright (c) 2020, Google Inc.
// SPDX-License-Identifier: ISC
#ifndef OPENSSL_HEADER_DSA_INTERNAL_H
#define OPENSSL_HEADER_DSA_INTERNAL_H
#include <openssl/dsa.h>
#include <openssl/thread.h>
#if defined(__cplusplus)
extern "C" {
#endif
struct dsa_st {
BIGNUM *p;
BIGNUM *q;
BIGNUM *g;
BIGNUM *pub_key;
BIGNUM *priv_key;
// Normally used to cache montgomery values
CRYPTO_MUTEX method_mont_lock;
BN_MONT_CTX *method_mont_p;
BN_MONT_CTX *method_mont_q;
CRYPTO_refcount_t references;
CRYPTO_EX_DATA ex_data;
};
// dsa_check_key performs cheap self-checks on |dsa|, and ensures it is within
// DoS bounds. It returns one on success and zero on error.
int dsa_check_key(const DSA *dsa);
int dsa_internal_paramgen(DSA *dsa, size_t bits, const EVP_MD *evpmd,
const unsigned char *seed_in, size_t seed_len,
int *out_counter, unsigned long *out_h, BN_GENCB *cb);
#if defined(__cplusplus)
} // extern C
#endif
#endif // OPENSSL_HEADER_DSA_INTERNAL_H