Skip to content

Commit

Permalink
Docs and testing
Browse files Browse the repository at this point in the history
  • Loading branch information
justsmth committed Oct 4, 2024
1 parent ca654f6 commit 3d0cebf
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
23 changes: 23 additions & 0 deletions crypto/evp_extra/evp_extra_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1815,6 +1815,29 @@ TEST(EVPExtraTest, DHKeygen) {
}
}

TEST(EVPExtraTest, DHParamgen) {
std::vector<std::pair<int, int>> test_data({ {1024, 3}, {512, 2}});

for (std::pair<int, int> plgen : test_data) {
const int prime_len = plgen.first;
const int generator = plgen.second;
// Construct a EVP_PKEY_CTX
bssl::UniquePtr<EVP_PKEY_CTX> ctx(EVP_PKEY_CTX_new_id(EVP_PKEY_DH, nullptr));
ASSERT_TRUE(ctx);
// Initialize for paramgen
ASSERT_TRUE(EVP_PKEY_paramgen_init(ctx.get()));
// Set the prime length
ASSERT_TRUE(EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx.get(), prime_len));
// Set the generator
ASSERT_TRUE(EVP_PKEY_CTX_set_dh_paramgen_generator(ctx.get(), generator));

EVP_PKEY *raw_pkey = NULL;
// Generate the parameters
ASSERT_TRUE(EVP_PKEY_paramgen(ctx.get(), &raw_pkey));
EVP_PKEY_free(raw_pkey);
}
}

// Test that |EVP_PKEY_keygen| works for Ed25519.
TEST(EVPExtraTest, Ed25519Keygen) {
bssl::UniquePtr<EVP_PKEY_CTX> pctx(
Expand Down
2 changes: 2 additions & 0 deletions crypto/evp_extra/p_dh.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ static int pkey_dh_init(EVP_PKEY_CTX *ctx) {
if (dctx == NULL) {
return 0;
}
dctx->prime_len = 2048;
dctx->generator = 2;

ctx->data = dctx;
return 1;
Expand Down
7 changes: 7 additions & 0 deletions include/openssl/evp.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,14 @@ OPENSSL_EXPORT int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key);
OPENSSL_EXPORT int EVP_PKEY_assign_DH(EVP_PKEY *pkey, DH *key);
OPENSSL_EXPORT DH *EVP_PKEY_get0_DH(const EVP_PKEY *pkey);
OPENSSL_EXPORT DH *EVP_PKEY_get1_DH(const EVP_PKEY *pkey);

// EVP_PKEY_CTX_set_dh_paramgen_prime_len sets the length of the DH prime
// parameter p for DH parameter generation. If this function is not called,
// the default length of 2048 is used.
OPENSSL_EXPORT int EVP_PKEY_CTX_set_dh_paramgen_prime_len(EVP_PKEY_CTX *ctx, int pbits);

// EVP_PKEY_CTX_set_dh_paramgen_generator sets the DH generator for DH parameter
// generation. If this function is not called, the default value of 2 is used.
OPENSSL_EXPORT int EVP_PKEY_CTX_set_dh_paramgen_generator(EVP_PKEY_CTX *ctx, int gen);

#define EVP_PKEY_NONE NID_undef
Expand Down

0 comments on commit 3d0cebf

Please sign in to comment.