Skip to content

Commit

Permalink
Make DSA opaque
Browse files Browse the repository at this point in the history
Update-Note: Accessing the DSA struct directly is no longer supported.
Use accessors instead.

Bug: 325
Change-Id: I73dc20f2e275d48648ca84d2db7806fe155c567d
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/60425
Reviewed-by: Adam Langley <agl@google.com>
Auto-Submit: David Benjamin <davidben@google.com>
Commit-Queue: David Benjamin <davidben@google.com>
  • Loading branch information
davidben authored and Boringssl LUCI CQ committed Jun 6, 2023
1 parent 43f8891 commit f4a4e27
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 29 deletions.
10 changes: 8 additions & 2 deletions crypto/dsa/dsa_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,10 @@ TEST(DSATest, Verify) {
TEST(DSATest, InvalidGroup) {
bssl::UniquePtr<DSA> dsa = GetFIPSDSA();
ASSERT_TRUE(dsa);
BN_zero(dsa->g);
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;
Expand Down Expand Up @@ -305,7 +308,10 @@ TEST(DSATest, MissingPrivate) {
TEST(DSATest, ZeroPrivateKey) {
bssl::UniquePtr<DSA> dsa = GetFIPSDSA();
ASSERT_TRUE(dsa);
BN_zero(dsa->priv_key);
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()));
Expand Down
18 changes: 18 additions & 0 deletions crypto/dsa/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,29 @@

#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);
Expand Down
13 changes: 7 additions & 6 deletions crypto/evp/print.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,12 @@ static int rsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent) {
static int do_dsa_print(BIO *bp, const DSA *x, int off, int ptype) {
const BIGNUM *priv_key = NULL;
if (ptype == 2) {
priv_key = x->priv_key;
priv_key = DSA_get0_priv_key(x);
}

const BIGNUM *pub_key = NULL;
if (ptype > 0) {
pub_key = x->pub_key;
pub_key = DSA_get0_pub_key(x);
}

const char *ktype = "DSA-Parameters";
Expand All @@ -210,14 +210,15 @@ static int do_dsa_print(BIO *bp, const DSA *x, int off, int ptype) {
}

if (!BIO_indent(bp, off, 128) ||
BIO_printf(bp, "%s: (%u bit)\n", ktype, BN_num_bits(x->p)) <= 0 ||
BIO_printf(bp, "%s: (%u bit)\n", ktype, BN_num_bits(DSA_get0_p(x))) <=
0 ||
// |priv_key| and |pub_key| may be NULL, in which case |bn_print| will
// silently skip them.
!bn_print(bp, "priv:", priv_key, off) ||
!bn_print(bp, "pub:", pub_key, off) ||
!bn_print(bp, "P:", x->p, off) ||
!bn_print(bp, "Q:", x->q, off) ||
!bn_print(bp, "G:", x->g, off)) {
!bn_print(bp, "P:", DSA_get0_p(x), off) ||
!bn_print(bp, "Q:", DSA_get0_q(x), off) ||
!bn_print(bp, "G:", DSA_get0_g(x), off)) {
return 0;
}

Expand Down
21 changes: 0 additions & 21 deletions include/openssl/dsa.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@

#include <openssl/base.h>

#include <openssl/engine.h>
#include <openssl/ex_data.h>
#include <openssl/thread.h>

#if defined(__cplusplus)
extern "C" {
Expand Down Expand Up @@ -398,25 +396,6 @@ OPENSSL_EXPORT DSA *DSA_generate_parameters(int bits, unsigned char *seed,
void *cb_arg);


struct dsa_st {
long version;
BIGNUM *p;
BIGNUM *q; // == 20
BIGNUM *g;

BIGNUM *pub_key; // y public key
BIGNUM *priv_key; // x private key

int flags;
// 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;
};


#if defined(__cplusplus)
} // extern C

Expand Down

0 comments on commit f4a4e27

Please sign in to comment.