diff --git a/crypto/evp_extra/evp_extra_test.cc b/crypto/evp_extra/evp_extra_test.cc index 633220b42b..29c5e4ce61 100644 --- a/crypto/evp_extra/evp_extra_test.cc +++ b/crypto/evp_extra/evp_extra_test.cc @@ -1815,6 +1815,29 @@ TEST(EVPExtraTest, DHKeygen) { } } +TEST(EVPExtraTest, DHParamgen) { + std::vector> test_data({ {1024, 3}, {512, 2}}); + + for (std::pair plgen : test_data) { + const int prime_len = plgen.first; + const int generator = plgen.second; + // Construct a EVP_PKEY_CTX + bssl::UniquePtr 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 pctx( diff --git a/crypto/evp_extra/p_dh.c b/crypto/evp_extra/p_dh.c index 59a42dfc55..593fc77c5f 100644 --- a/crypto/evp_extra/p_dh.c +++ b/crypto/evp_extra/p_dh.c @@ -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; diff --git a/include/openssl/evp.h b/include/openssl/evp.h index 656f961146..c569822984 100644 --- a/include/openssl/evp.h +++ b/include/openssl/evp.h @@ -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