3131 * local one.
3232 */
3333
34- #ifndef EVP_PKEY_CTRL_DH_PAD
35- #define EVP_PKEY_CTRL_DH_PAD (EVP_PKEY_ALG_CTRL + 16)
36- #endif
37-
3834#define DEFAULT_PRIME_LEN 1024
3935
4036/**
@@ -567,6 +563,7 @@ static int we_dh_compute_key_int(we_Dh *engineDh, unsigned char *secret,
567563 unsigned char * privBuf = NULL ;
568564 int privLen = 0 ;
569565 unsigned int secLen = 0 ;
566+ const BIGNUM * privBn ;
570567
571568 WOLFENGINE_ENTER (WE_LOG_KE , "we_dh_compute_key_int" );
572569 WOLFENGINE_MSG_VERBOSE (WE_LOG_KE , "ARGS [engineDh = %p, secret = %p, "
@@ -600,7 +597,15 @@ static int we_dh_compute_key_int(we_Dh *engineDh, unsigned char *secret,
600597
601598 if (ret == 1 ) {
602599 /* Convert our private key to a byte array. */
603- ret = we_dh_bignum_to_bin (DH_get0_priv_key (dh ), & privBuf , & privLen );
600+ privBn = DH_get0_priv_key (dh );
601+ if (privBn == NULL ) {
602+ WOLFENGINE_ERROR_MSG (WE_LOG_KE , "Private key is NULL. Can't create "
603+ "DH shared secret." );
604+ ret = 0 ;
605+ }
606+ else {
607+ ret = we_dh_bignum_to_bin (DH_get0_priv_key (dh ), & privBuf , & privLen );
608+ }
604609 }
605610
606611 if (ret == 1 ) {
@@ -1057,6 +1062,7 @@ static int we_dh_pkey_ctrl(EVP_PKEY_CTX *ctx, int type, int num, void *ptr)
10571062 "setting the generator when generating DH params" );
10581063 /* wolfCrypt doesn't allow setting the generator when generating
10591064 * DH params. */
1065+ ret = 0 ;
10601066 break ;
10611067 case EVP_PKEY_CTRL_DH_PAD :
10621068 dh -> pad = num ;
@@ -1086,7 +1092,8 @@ static int we_dh_pkey_ctrl(EVP_PKEY_CTX *ctx, int type, int num, void *ptr)
10861092 * Extra operations for working with DH.
10871093 * Supported operations include:
10881094 * - "dh_param": set the named parameters.
1089- * - "pad": pad out secret to input length.
1095+ * - "dh_pad": pad out secret to input length.
1096+ * - "dh_paramgen_prime_len": set the length of the prime, "p."
10901097 *
10911098 * @param ctx [in] Public key context of operation.
10921099 * @param type [in] Type of operation to perform.
@@ -1113,7 +1120,7 @@ static int we_dh_pkey_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
11131120
11141121 if (ret == 1 ) {
11151122 /* Set named DH parameters. */
1116- if (XSTRNCMP (type , "dh_param" , 9 ) == 0 ) {
1123+ if (XSTRCMP (type , "dh_param" ) == 0 ) {
11171124 #ifndef HAVE_WC_DHSETNAMEDKEY
11181125 const DhParams * params = NULL ;
11191126 #else
@@ -1122,7 +1129,7 @@ static int we_dh_pkey_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
11221129
11231130 #ifdef HAVE_PUBLIC_FFDHE
11241131 #ifdef HAVE_FFDHE_2048
1125- if (XSTRNCMP (value , "ffdhe2048" , 10 ) == 0 ) {
1132+ if (XSTRCMP (value , "ffdhe2048" ) == 0 ) {
11261133 WOLFENGINE_MSG (WE_LOG_KE ,
11271134 "Setting named parameters: ffdhe2048" );
11281135 #ifndef HAVE_WC_DHSETNAMEDKEY
@@ -1134,7 +1141,7 @@ static int we_dh_pkey_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
11341141 else
11351142 #endif
11361143 #ifdef HAVE_FFDHE_3072
1137- if (XSTRNCMP (value , "ffdhe3072" , 10 ) == 0 ) {
1144+ if (XSTRCMP (value , "ffdhe3072" ) == 0 ) {
11381145 WOLFENGINE_MSG (WE_LOG_KE ,
11391146 "Setting named parameters: ffdhe3072" );
11401147 #ifndef HAVE_WC_DHSETNAMEDKEY
@@ -1146,7 +1153,7 @@ static int we_dh_pkey_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
11461153 else
11471154 #endif
11481155 #ifdef HAVE_FFDHE_4096
1149- if (XSTRNCMP (value , "ffdhe4096" , 10 ) == 0 ) {
1156+ if (XSTRCMP (value , "ffdhe4096" ) == 0 ) {
11501157 WOLFENGINE_MSG (WE_LOG_KE ,
11511158 "Setting named parameters: ffdhe4096" );
11521159 #ifndef HAVE_WC_DHSETNAMEDKEY
@@ -1180,7 +1187,8 @@ static int we_dh_pkey_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
11801187 rc = wc_DhSetNamedKey (& dh -> key , params );
11811188 #endif
11821189 if (rc != 0 ) {
1183- WOLFENGINE_ERROR_MSG (WE_LOG_KE , "Failed set parameters" );
1190+ WOLFENGINE_ERROR_MSG (WE_LOG_KE , "Failed to set "
1191+ "parameters." );
11841192 ret = 0 ;
11851193 }
11861194 }
@@ -1189,9 +1197,13 @@ static int we_dh_pkey_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
11891197 }
11901198 }
11911199 /* Set padding requirement for secret output. */
1192- else if (XSTRNCMP (type , "dh_pad" , 7 ) == 0 ) {
1200+ else if (XSTRCMP (type , "dh_pad" ) == 0 ) {
11931201 dh -> pad = XATOI (value );
11941202 }
1203+ else if (XSTRCMP (type , "dh_paramgen_prime_len" ) == 0 ) {
1204+ ret = we_dh_pkey_ctrl (ctx , EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN ,
1205+ XATOI (value ), NULL );
1206+ }
11951207 else {
11961208 /* Unsupported control type. */
11971209 XSNPRINTF (errBuff , sizeof (errBuff ), "Unsupported ctrl string %s" ,
0 commit comments