From ad050a19eeb4f329c3abf5614431bab98fcaa135 Mon Sep 17 00:00:00 2001 From: Aidan Garske Date: Mon, 12 Oct 2026 11:42:38 -0700 Subject: [PATCH 1/8] F-8365 - Preserve absent q in named DH groups --- src/we_dh.c | 2 +- test/test_dh.c | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/we_dh.c b/src/we_dh.c index 98163cd9..2211d656 100644 --- a/src/we_dh.c +++ b/src/we_dh.c @@ -784,7 +784,7 @@ static int we_dh_convert_params(DhKey *wolfDh, DH *osslDh) ret = 0; } } - if (ret == 1) { + if ((ret == 1) && (qLen > 0)) { /* Allocate a big number and set value to be: Group prime. */ qBn = BN_bin2bn(q, qLen, NULL); if (qBn == NULL) { diff --git a/test/test_dh.c b/test/test_dh.c index 8b44931d..6f64bada 100644 --- a/test/test_dh.c +++ b/test/test_dh.c @@ -549,6 +549,7 @@ int test_dh_ctrl(ENGINE* e, void* data) { int err; EVP_PKEY_CTX* ctx = NULL; + EVP_PKEY* key = NULL; int primeLengths[] = {1024, 2048, 3072}; word32 i; @@ -604,6 +605,15 @@ int test_dh_ctrl(ENGINE* e, void* data) if (err == 0) { err = EVP_PKEY_CTX_ctrl_str(ctx, "dh_param", "ffdhe2048") <= 0; } + #ifndef HAVE_FFDHE_Q + if (err == 0) { + PRINT_MSG("Generate a key with named DH parameters"); + err = EVP_PKEY_keygen_init(ctx) != 1; + } + if (err == 0) { + err = EVP_PKEY_keygen(ctx, &key) != 1; + } + #endif #else if (err == 0) { err = EVP_PKEY_CTX_ctrl_str(ctx, "dh_param", "ffdhe2048") > 0; @@ -631,6 +641,7 @@ int test_dh_ctrl(ENGINE* e, void* data) #endif /* HAVE_FFDHE_4096 */ #endif /* HAVE_PUBLIC_FFDHE */ + EVP_PKEY_free(key); EVP_PKEY_CTX_free(ctx); return err; From 8622975556d25c9542454aa6d5fb8fcd8336c024 Mon Sep 17 00:00:00 2001 From: Aidan Garske Date: Mon, 12 Oct 2026 11:56:51 -0700 Subject: [PATCH 2/8] F-10145 - Unlock DH RNG mutex only after locking --- src/we_dh.c | 4 ++-- test/test_dh.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/src/we_dh.c b/src/we_dh.c index 2211d656..a7d457be 100644 --- a/src/we_dh.c +++ b/src/we_dh.c @@ -874,10 +874,10 @@ static int we_dh_paramgen_int(DH *dh, we_Dh *engineDh) WOLFENGINE_ERROR_FUNC(WE_LOG_KE, "wc_DhGenerateParams", rc); ret = 0; } - } #if defined(WE_DH_USE_GLOBAL_RNG) && !defined(WE_SINGLE_THREADED) - wc_UnLockMutex(we_rng_mutex); + wc_UnLockMutex(we_rng_mutex); #endif + } if (ret == 1) { WOLFENGINE_MSG(WE_LOG_KE, "Converting DH params to OpenSSL DH"); diff --git a/test/test_dh.c b/test/test_dh.c index 6f64bada..aca8ab16 100644 --- a/test/test_dh.c +++ b/test/test_dh.c @@ -45,6 +45,64 @@ static const unsigned char dh_g[] = 0x02, }; +#if defined(WE_DH_USE_GLOBAL_RNG) && !defined(WE_SINGLE_THREADED) && \ + !defined(TEST_MULTITHREADED) && !defined(_WIN32) +extern wolfSSL_Mutex* we_rng_mutex; + +static int test_dh_failed_rng_lock(DH* dh) +{ + int err = 0; + int attrInit = 0; + int mutexInit = 0; + int mutexLocked = 0; + int rc; + pthread_mutexattr_t attr; + wolfSSL_Mutex mutex; + wolfSSL_Mutex* savedMutex = we_rng_mutex; + + rc = pthread_mutexattr_init(&attr); + err = rc != 0; + attrInit = rc == 0; + if (err == 0) { + rc = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK); + err = rc != 0; + } + if (err == 0) { + rc = pthread_mutex_init(&mutex, &attr); + err = rc != 0; + mutexInit = rc == 0; + } + if (err == 0) { + rc = pthread_mutex_lock(&mutex); + err = rc != 0; + mutexLocked = rc == 0; + } + if (err == 0) { + we_rng_mutex = &mutex; + rc = DH_generate_parameters_ex(dh, 1024, DH_GENERATOR_5, NULL); + we_rng_mutex = savedMutex; + err = rc != 0; + } + if (err == 0) { + rc = pthread_mutex_trylock(&mutex); + if (rc == 0) { + pthread_mutex_unlock(&mutex); + mutexLocked = 0; + err = 1; + } + } + if (mutexInit) { + if (mutexLocked) + pthread_mutex_unlock(&mutex); + pthread_mutex_destroy(&mutex); + } + if (attrInit) + pthread_mutexattr_destroy(&attr); + + return err; +} +#endif + static int test_dh_keygen(DH *dhOpenSSL, DH *dhWolfEngine) { int err = 0; @@ -126,6 +184,13 @@ int test_dh_pgen(ENGINE *e, void *data) if (err == 0) { DH_set_method(dhWolfEngine, method); } +#if defined(WE_DH_USE_GLOBAL_RNG) && !defined(WE_SINGLE_THREADED) && \ + !defined(TEST_MULTITHREADED) && !defined(_WIN32) + if (err == 0) { + PRINT_MSG("Keep RNG mutex ownership when locking fails"); + err = test_dh_failed_rng_lock(dhWolfEngine); + } +#endif if (err == 0) { /* Generator and callback (last param) ignored by wolfEngine. */ err = DH_generate_parameters_ex(dhWolfEngine, 1024, DH_GENERATOR_5, From 01f549311eff58b7cfe5adde62657d0f27d7fd1d Mon Sep 17 00:00:00 2001 From: Aidan Garske Date: Mon, 12 Oct 2026 12:07:03 -0700 Subject: [PATCH 3/8] F-11318 - Pad DH derives to the modulus size --- src/we_dh.c | 32 +++++++++++++++++++------------ test/test_dh.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 12 deletions(-) diff --git a/src/we_dh.c b/src/we_dh.c index a7d457be..f13183ab 100644 --- a/src/we_dh.c +++ b/src/we_dh.c @@ -1028,7 +1028,7 @@ static void we_dh_pkey_cleanup(EVP_PKEY_CTX *ctx) * - EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR: intended to set the generator, "g", * but doesn't actually do this, as wolfCrypt doesn't allow you to specify * the generator used in DH key creation. - * - EVP_PKEY_CTRL_DH_PAD: pad out secret to input length. + * - EVP_PKEY_CTRL_DH_PAD: pad secret to the DH modulus size. * - EVP_PKEY_CTRL_PEER_KEY: intended to set the peer key, but doesn't actually * do this, as we can get the peer key directly from the ctx when needed. * @@ -1112,7 +1112,7 @@ static int we_dh_pkey_ctrl(EVP_PKEY_CTX *ctx, int type, int num, void *ptr) * Extra operations for working with DH. * Supported operations include: * - "dh_param": set the named parameters. - * - "dh_pad": pad out secret to input length. + * - "dh_pad": pad secret to the DH modulus size. * - "dh_paramgen_prime_len": set the length of the prime, "p." * * @param ctx [in] Public key context of operation. @@ -1403,6 +1403,7 @@ static int we_dh_pkey_derive(EVP_PKEY_CTX *ctx, unsigned char *secret, DH *peerDh = NULL; const BIGNUM *peerPub = NULL; size_t totalLen = *secretLen; + size_t dhLen = 0; WOLFENGINE_ENTER(WE_LOG_KE, "we_dh_pkey_derive"); WOLFENGINE_MSG_VERBOSE(WE_LOG_KE, "ARGS [ctx = %p, secret = %p, " @@ -1433,16 +1434,25 @@ static int we_dh_pkey_derive(EVP_PKEY_CTX *ctx, unsigned char *secret, WOLFENGINE_ERROR_FUNC_NULL(WE_LOG_KE, "EVP_PKEY_get0_DH", ourDh); ret = 0; } + else { + dhLen = (size_t)DH_size(ourDh); + } } if ((ret == 1) && (secret == NULL)) { /* Return the length of the secret only. */ - *secretLen = DH_size(ourDh); + *secretLen = dhLen; } else if (ret == 1) { + if (engineDh->pad && (totalLen < dhLen)) { + WOLFENGINE_ERROR_MSG(WE_LOG_KE, "DH secret buffer too small"); + ret = 0; + } + /* Get the peer's key. */ - peerKey = EVP_PKEY_CTX_get0_peerkey(ctx); - if (peerKey == NULL) { + if (ret == 1) + peerKey = EVP_PKEY_CTX_get0_peerkey(ctx); + if ((ret == 1) && (peerKey == NULL)) { WOLFENGINE_ERROR_FUNC_NULL(WE_LOG_KE, "EVP_PKEY_CTX_get0_peerkey", peerKey); ret = 0; @@ -1480,13 +1490,11 @@ static int we_dh_pkey_derive(EVP_PKEY_CTX *ctx, unsigned char *secret, } } - /* When padding required and total buffer size is bigger than secret - * generated, ensure the buffer is zero padded at the front. */ - if ((ret == 1) && engineDh->pad && (totalLen != *secretLen)) { - XMEMMOVE(secret + totalLen - *secretLen, secret, *secretLen); - XMEMSET(secret, 0, totalLen - *secretLen); - /* Return the original length. */ - *secretLen = (int)totalLen; + /* Pad the generated secret to the DH modulus size. */ + if ((ret == 1) && engineDh->pad && (dhLen > *secretLen)) { + XMEMMOVE(secret + dhLen - *secretLen, secret, *secretLen); + XMEMSET(secret, 0, dhLen - *secretLen); + *secretLen = dhLen; } } diff --git a/test/test_dh.c b/test/test_dh.c index aca8ab16..f745a0de 100644 --- a/test/test_dh.c +++ b/test/test_dh.c @@ -405,6 +405,11 @@ static int test_dh_pkey_keygen(ENGINE *e, EVP_PKEY *params) size_t secretLenOpenSSL = 0; unsigned char *secretWolfEngine = NULL; size_t secretLenWolfEngine = 0; + unsigned char *paddedSecret = NULL; + unsigned char *shortSecret = NULL; + size_t paddedSecretLen = 0; + size_t shortSecretLen = 0; + size_t expectedSecretLen = 0; ctx = EVP_PKEY_CTX_new(params, e); err = ctx == NULL; @@ -485,6 +490,48 @@ static int test_dh_pkey_keygen(ENGINE *e, EVP_PKEY *params) err = memcmp(secretOpenSSL, secretWolfEngine, secretLenOpenSSL) != 0; } + if (err == 0) { + PRINT_MSG("Pad DH secret to the modulus size"); + EVP_PKEY_CTX_free(ctx); + ctx = EVP_PKEY_CTX_new(keyWolfEngine, e); + err = ctx == NULL; + } + if (err == 0) { + err = EVP_PKEY_derive_init(ctx) <= 0; + } + if (err == 0) { + err = EVP_PKEY_derive_set_peer(ctx, keyOpenSSL) <= 0; + } + if (err == 0) { + err = EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_DERIVE, + EVP_PKEY_CTRL_DH_PAD, 1, NULL) <= 0; + } + if (err == 0) { + expectedSecretLen = (size_t)EVP_PKEY_size(keyWolfEngine); + paddedSecretLen = expectedSecretLen + 8; + paddedSecret = (unsigned char*)OPENSSL_malloc(paddedSecretLen); + err = paddedSecret == NULL; + } + if (err == 0) { + err = EVP_PKEY_derive(ctx, paddedSecret, &paddedSecretLen) <= 0; + } + if (err == 0) { + err = paddedSecretLen != expectedSecretLen; + } + if (err == 0) { + shortSecret = (unsigned char*)OPENSSL_malloc(expectedSecretLen + 1); + err = shortSecret == NULL; + } + if (err == 0) { + XMEMSET(shortSecret, 0xa5, expectedSecretLen + 1); + shortSecretLen = expectedSecretLen - 1; + PRINT_MSG("Reject a short padded DH secret buffer"); + err = EVP_PKEY_derive(ctx, shortSecret + 1, &shortSecretLen) > 0; + } + if (err == 0) { + err = (shortSecret[0] != 0xa5) || + (shortSecret[expectedSecretLen] != 0xa5); + } EVP_PKEY_CTX_free(ctx); EVP_PKEY_free(keyOpenSSL); EVP_PKEY_free(keyWolfEngine); @@ -493,6 +540,10 @@ static int test_dh_pkey_keygen(ENGINE *e, EVP_PKEY *params) OPENSSL_free(secretWolfEngine); if (secretOpenSSL != NULL) OPENSSL_free(secretOpenSSL); + if (paddedSecret != NULL) + OPENSSL_free(paddedSecret); + if (shortSecret != NULL) + OPENSSL_free(shortSecret); return err; } From e52cf4fc4e1640b9e689bd79748125278ce38d9b Mon Sep 17 00:00:00 2001 From: Aidan Garske Date: Mon, 12 Oct 2026 12:25:11 -0700 Subject: [PATCH 4/8] F-12253 - Normalize DH padding controls --- src/we_dh.c | 4 +- test/test_dh.c | 110 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+), 2 deletions(-) diff --git a/src/we_dh.c b/src/we_dh.c index f13183ab..92a0f83c 100644 --- a/src/we_dh.c +++ b/src/we_dh.c @@ -1085,7 +1085,7 @@ static int we_dh_pkey_ctrl(EVP_PKEY_CTX *ctx, int type, int num, void *ptr) ret = 0; break; case EVP_PKEY_CTRL_DH_PAD: - dh->pad = num; + dh->pad = num != 0; break; case EVP_PKEY_CTRL_PEER_KEY: WOLFENGINE_MSG(WE_LOG_KE, "EVP_PKEY_CTRL_PEER_KEY"); @@ -1218,7 +1218,7 @@ static int we_dh_pkey_ctrl_str(EVP_PKEY_CTX *ctx, const char *type, } /* Set padding requirement for secret output. */ else if (XSTRCMP(type, "dh_pad") == 0) { - dh->pad = XATOI(value); + dh->pad = XATOI(value) != 0; } else if (XSTRCMP(type, "dh_paramgen_prime_len") == 0) { ret = we_dh_pkey_ctrl(ctx, EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN, diff --git a/test/test_dh.c b/test/test_dh.c index f745a0de..190865b5 100644 --- a/test/test_dh.c +++ b/test/test_dh.c @@ -395,6 +395,109 @@ int test_dh_priv_key_length(ENGINE *e, void *data) #ifdef WE_HAVE_EVP_PKEY +static int test_dh_pkey_nonzero_padding(ENGINE *e, EVP_PKEY *params, + int useStringControl) +{ + static const unsigned char privateKey[] = { + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, + 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, + 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, + 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, + 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, + 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, + 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, + 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x00, 0x64 + }; + int err; + DH *ourDh = NULL; + DH *peerDh = NULL; + BIGNUM *priv = NULL; + BIGNUM *pub = NULL; + EVP_PKEY *ourKey = NULL; + EVP_PKEY *peerKey = NULL; + EVP_PKEY_CTX *ctx = NULL; + unsigned char *secret = NULL; + size_t secretLen = 0; + DH *paramsDh = (DH *)EVP_PKEY_get0_DH(params); + + err = paramsDh == NULL; + if (err == 0) { + ourDh = DHparams_dup(paramsDh); + peerDh = DHparams_dup(paramsDh); + err = (ourDh == NULL) || (peerDh == NULL); + } + if (err == 0) { + priv = BN_bin2bn(privateKey, sizeof(privateKey), NULL); + pub = BN_new(); + err = (priv == NULL) || (pub == NULL); + } + if (err == 0) { + err = BN_set_word(pub, 2) != 1; + } + if (err == 0) { + err = DH_set0_key(ourDh, NULL, priv) != 1; + if (err == 0) + priv = NULL; + } + if (err == 0) { + err = DH_set0_key(peerDh, pub, NULL) != 1; + if (err == 0) + pub = NULL; + } + if (err == 0) { + ourKey = EVP_PKEY_new(); + peerKey = EVP_PKEY_new(); + err = (ourKey == NULL) || (peerKey == NULL); + } + if (err == 0) { + err = EVP_PKEY_assign_DH(ourKey, ourDh) != 1; + if (err == 0) + ourDh = NULL; + } + if (err == 0) { + err = EVP_PKEY_assign_DH(peerKey, peerDh) != 1; + if (err == 0) + peerDh = NULL; + } + if (err == 0) { + ctx = EVP_PKEY_CTX_new(ourKey, e); + err = ctx == NULL; + } + if (err == 0) + err = EVP_PKEY_derive_init(ctx) <= 0; + if (err == 0) + err = EVP_PKEY_derive_set_peer(ctx, peerKey) <= 0; + if ((err == 0) && useStringControl) { + err = EVP_PKEY_CTX_ctrl_str(ctx, "dh_pad", "2") <= 0; + } + else if (err == 0) { + err = EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_DERIVE, + EVP_PKEY_CTRL_DH_PAD, 2, NULL) <= 0; + } + if (err == 0) { + secretLen = (size_t)EVP_PKEY_size(ourKey); + secret = (unsigned char *)OPENSSL_malloc(secretLen); + err = secret == NULL; + } + if (err == 0) + err = EVP_PKEY_derive(ctx, secret, &secretLen) <= 0; + if (err == 0) + err = secretLen != (size_t)EVP_PKEY_size(ourKey); + if (err == 0) + err = secret[0] != 0; + + OPENSSL_free(secret); + EVP_PKEY_CTX_free(ctx); + EVP_PKEY_free(peerKey); + EVP_PKEY_free(ourKey); + BN_free(pub); + BN_clear_free(priv); + DH_free(peerDh); + DH_free(ourDh); + + return err; +} + static int test_dh_pkey_keygen(ENGINE *e, EVP_PKEY *params) { int err; @@ -654,6 +757,13 @@ int test_dh_pkey(ENGINE* e, void* data) if (err == 0) { err = test_dh_pkey_keygen(e, params); } + if (err == 0) { + PRINT_MSG("Treat every nonzero DH padding control value as enabled"); + err = test_dh_pkey_nonzero_padding(e, params, 0); + } + if (err == 0) { + err = test_dh_pkey_nonzero_padding(e, params, 1); + } EVP_PKEY_free(params); DH_free(dh); From 4e08b1b07e09f62de3b0eeb3c6f6cc5973c90017 Mon Sep 17 00:00:00 2001 From: Aidan Garske Date: Mon, 12 Oct 2026 12:52:24 -0700 Subject: [PATCH 5/8] F-12255 - Clear stale q on DH parameter refresh --- src/we_dh.c | 16 +++++++ test/test_dh.c | 116 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+) diff --git a/src/we_dh.c b/src/we_dh.c index 92a0f83c..89bb694c 100644 --- a/src/we_dh.c +++ b/src/we_dh.c @@ -304,6 +304,22 @@ static int we_dh_set_parameters(const DH *dh, we_Dh *engineDh) engineDh->qLen = qBufLen; } } + else if (ret == 1) { + if (engineDh->q != NULL) { + OPENSSL_free(engineDh->q); + engineDh->q = NULL; + } + engineDh->qLen = 0; + + /* Reinitialize the key because setting q to NULL does not clear an + * existing q on every supported wolfCrypt version. */ + wc_FreeDhKey(&engineDh->key); + rc = wc_InitDhKey(&engineDh->key); + if (rc != 0) { + WOLFENGINE_ERROR_FUNC(WE_LOG_KE, "wc_InitDhKey", rc); + ret = 0; + } + } if (ret == 1) { /* Set p, g and q parameters into wolfSSL DH key. */ diff --git a/test/test_dh.c b/test/test_dh.c index 190865b5..113b1c87 100644 --- a/test/test_dh.c +++ b/test/test_dh.c @@ -23,6 +23,10 @@ #ifdef WE_HAVE_DH +#ifndef WE_DH_EX_DATA_IDX +#define WE_DH_EX_DATA_IDX 0 +#endif + /* dh1024 p */ static const unsigned char dh_p[] = { @@ -161,6 +165,115 @@ static int test_dh_keygen(DH *dhOpenSSL, DH *dhWolfEngine) return err; } +static int test_dh_parameter_refresh(ENGINE *e) +{ + int err; + DH *dhWithQ = NULL; + DH *dhWithoutQ = NULL; + const DH_METHOD *method = ENGINE_get_DH(e); + BIGNUM *p = NULL; + BIGNUM *q = NULL; + BIGNUM *g = NULL; + BIGNUM *priv = NULL; + BIGNUM *peerPub = NULL; + void *withQData = NULL; + void *withoutQData = NULL; + unsigned char secret[sizeof(dh_p)]; + unsigned char privateKey[64]; + int i; + + for (i = 0; i < (int)sizeof(privateKey); i++) + privateKey[i] = (unsigned char)(i + 1); + + err = method == NULL; + if (err == 0) { + dhWithQ = DH_new(); + dhWithoutQ = DH_new(); + err = (dhWithQ == NULL) || (dhWithoutQ == NULL); + } + if (err == 0) { + err = (DH_set_method(dhWithQ, method) != 1) || + (DH_set_method(dhWithoutQ, method) != 1); + } + if (err == 0) { + p = BN_bin2bn(dh_p, sizeof(dh_p), NULL); + q = BN_dup(p); + g = BN_new(); + err = (p == NULL) || (q == NULL) || (g == NULL); + } + if (err == 0) { + err = (BN_sub_word(q, 1) != 1) || (BN_rshift1(q, q) != 1) || + (BN_set_word(g, 4) != 1); + } + if (err == 0) { + err = DH_set0_pqg(dhWithQ, p, q, g) != 1; + if (err == 0) { + p = NULL; + q = NULL; + g = NULL; + } + } + if (err == 0) { + priv = BN_bin2bn(privateKey, sizeof(privateKey), NULL); + err = priv == NULL; + } + if (err == 0) { + err = DH_set0_key(dhWithQ, NULL, priv) != 1; + if (err == 0) + priv = NULL; + } + if (err == 0) + err = DH_generate_key(dhWithQ) != 1; + + if (err == 0) { + p = BN_bin2bn(dh_p, sizeof(dh_p), NULL); + g = BN_bin2bn(dh_g, sizeof(dh_g), NULL); + err = (p == NULL) || (g == NULL); + } + if (err == 0) { + peerPub = BN_new(); + err = (peerPub == NULL) || (BN_set_word(peerPub, 2) != 1); + } + if (err == 0) { + err = DH_set0_pqg(dhWithoutQ, p, NULL, g) != 1; + if (err == 0) { + p = NULL; + g = NULL; + } + } + if (err == 0) + err = DH_generate_key(dhWithoutQ) != 1; + + if (err == 0) { + withQData = DH_get_ex_data(dhWithQ, WE_DH_EX_DATA_IDX); + withoutQData = DH_get_ex_data(dhWithoutQ, WE_DH_EX_DATA_IDX); + err = (withQData == NULL) || (withoutQData == NULL); + } + if (err == 0) { + err = DH_set_ex_data(dhWithQ, WE_DH_EX_DATA_IDX, withoutQData) != 1; + } + if (err == 0) { + if (DH_set_ex_data(dhWithoutQ, WE_DH_EX_DATA_IDX, withQData) != 1) { + (void)DH_set_ex_data(dhWithQ, WE_DH_EX_DATA_IDX, withQData); + err = 1; + } + } + if (err == 0) { + PRINT_MSG("Clear cached q when refreshing q-less DH parameters"); + err = DH_compute_key(secret, peerPub, dhWithoutQ) <= 0; + } + + BN_free(peerPub); + BN_clear_free(priv); + BN_free(g); + BN_free(q); + BN_free(p); + DH_free(dhWithoutQ); + DH_free(dhWithQ); + + return err; +} + int test_dh_pgen(ENGINE *e, void *data) { int err; @@ -210,6 +323,9 @@ int test_dh_pgen(ENGINE *e, void *data) if (err == 0) { err = test_dh_keygen(dhOpenSSL, dhWolfEngine); } + if (err == 0) { + err = test_dh_parameter_refresh(e); + } DH_free(dhOpenSSL); DH_free(dhWolfEngine); From ca50a49ab462e1bd28e12fcbdd62a77d0a185838 Mon Sep 17 00:00:00 2001 From: Aidan Garske Date: Mon, 12 Oct 2026 13:08:02 -0700 Subject: [PATCH 6/8] F-12290 - Verify generated DH generator --- README.md | 2 + .../1.0.2h/tests/fips/dhtest_102h.patch | 20 +++ .../1.1.1b/tests/fips/dhtest_111b.patch | 32 ++++ src/we_dh.c | 96 +++++++++-- test/test_dh.c | 152 +++++++++++++++--- 5 files changed, 270 insertions(+), 32 deletions(-) create mode 100644 openssl_patches/1.0.2h/tests/fips/dhtest_102h.patch create mode 100644 openssl_patches/1.1.1b/tests/fips/dhtest_111b.patch diff --git a/README.md b/README.md index 3265a565..8a4c218a 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,8 @@ having to touch the OpenSSL code itself. * DRBG * RSA * DH + * With wolfCrypt FIPS, use named or imported parameters for key generation + and agreement. Legacy `DH_generate_parameters_ex()` returns 0. * ECC * ECDSA * ECDH diff --git a/openssl_patches/1.0.2h/tests/fips/dhtest_102h.patch b/openssl_patches/1.0.2h/tests/fips/dhtest_102h.patch new file mode 100644 index 00000000..a44885ba --- /dev/null +++ b/openssl_patches/1.0.2h/tests/fips/dhtest_102h.patch @@ -0,0 +1,20 @@ +diff --git a/crypto/dh/dhtest.c b/crypto/dh/dhtest.c +--- a/crypto/dh/dhtest.c ++++ b/crypto/dh/dhtest.c +@@ -140,5 +140,13 @@ + BN_GENCB_set(&_cb, &cb, out); +- if (((a = DH_new()) == NULL) || !DH_generate_parameters_ex(a, 1024, +- DH_GENERATOR_5, +- &_cb)) ++ if ((a = DH_new()) == NULL) ++ goto err; ++ if (a->engine != e || ++ DH_generate_parameters_ex(a, 1024, DH_GENERATOR_5, &_cb) != 0 || ++ a->p != NULL || a->q != NULL || a->g != NULL || a->engine != e) ++ goto err; ++ BIO_puts(out, "Legacy DH parameter generation rejected in FIPS mode\n"); ++ ERR_clear_error(); ++ DH_free(a); ++ a = DH_get_2048_256(); ++ if (a == NULL || a->engine != e) + goto err; diff --git a/openssl_patches/1.1.1b/tests/fips/dhtest_111b.patch b/openssl_patches/1.1.1b/tests/fips/dhtest_111b.patch new file mode 100644 index 00000000..1e93a12a --- /dev/null +++ b/openssl_patches/1.1.1b/tests/fips/dhtest_111b.patch @@ -0,0 +1,32 @@ +diff --git a/test/dhtest.c b/test/dhtest.c +--- a/test/dhtest.c ++++ b/test/dhtest.c +@@ -25,4 +25,6 @@ + static int cb(int p, int n, BN_GENCB *arg); +- +- ++ ++extern ENGINE *e; ++ ++ + static const unsigned char dhtest_1024_p[] = +@@ -193,5 +193,17 @@ + BN_GENCB_set(_cb, &cb, NULL); + if (!TEST_ptr(a = DH_new()) +- || !TEST_true(DH_generate_parameters_ex(a, 1024, +- DH_GENERATOR_5, _cb))) ++ || !TEST_ptr_eq(DH_get0_engine(a), e) ++ || !TEST_int_eq(DH_generate_parameters_ex(a, 1024, ++ DH_GENERATOR_5, _cb), 0) ++ || !TEST_ptr_null(DH_get0_p(a)) ++ || !TEST_ptr_null(DH_get0_q(a)) ++ || !TEST_ptr_null(DH_get0_g(a)) ++ || !TEST_ptr_eq(DH_get0_engine(a), e)) ++ goto err3; ++ TEST_info("Legacy DH parameter generation rejected in FIPS mode"); ++ ERR_clear_error(); ++ DH_free(a); ++ a = NULL; ++ if (!TEST_ptr(a = DH_get_2048_256()) ++ || !TEST_ptr_eq(DH_get0_engine(a), e)) + goto err3; diff --git a/src/we_dh.c b/src/we_dh.c index 89bb694c..fc40cdd7 100644 --- a/src/we_dh.c +++ b/src/we_dh.c @@ -911,35 +911,105 @@ static int we_dh_paramgen_int(DH *dh, we_Dh *engineDh) * * @param dh [in,out] OpenSSL DH object. * @param primeLen [in] Length of prime. - * @param g [in] Generator to use. (ignored) - * @param cb [in] Callback to show generation progress. (ignored) + * @param g [in] Generator to use. + * @param cb [in] Callback to show generation progress. * @returns 1 on success and 0 on failure. */ static int we_dh_generate_params(DH *dh, int primeLen, int g, BN_GENCB *cb) { - int ret = 1; - we_Dh *engineDh; + int ret = 0; +#ifndef HAVE_FIPS + we_Dh *engineDh = NULL; + DH *params = NULL; + const BIGNUM *paramP = NULL; + const BIGNUM *paramQ = NULL; + const BIGNUM *paramG = NULL; + BIGNUM *p = NULL; + BIGNUM *q = NULL; + BIGNUM *generator = NULL; +#endif WOLFENGINE_ENTER(WE_LOG_KE, "we_dh_generate_parameters"); WOLFENGINE_MSG_VERBOSE(WE_LOG_KE, "ARGS [dh = %p, primeLen = %d, g = %d, " "cb = %p]", dh, primeLen, g, cb); - /* Cannot set g or callback to wolfSSL parameter generation. */ +#ifdef HAVE_FIPS + (void)dh; + (void)primeLen; (void)g; (void)cb; - - /* Retrieve internal DH object. */ - engineDh = (we_Dh *)DH_get_ex_data(dh, WE_DH_EX_DATA_IDX); - if (engineDh == NULL) { - WOLFENGINE_ERROR_FUNC_NULL(WE_LOG_KE, "DH_get_ex_data", engineDh); + WOLFENGINE_ERROR_MSG(WE_LOG_KE, "Legacy DH parameter generation is not " + "supported in FIPS mode"); +#else + ret = 1; + if ((ret == 1) && (DH_get0_q(dh) != NULL)) { + WOLFENGINE_ERROR_MSG(WE_LOG_KE, "Cannot replace DH parameters with q"); ret = 0; } if (ret == 1) { - engineDh->primeLen = primeLen; - /* Generate the parameters with wolfSSL and copy into OpenSSL object. */ - ret = we_dh_paramgen_int(dh, engineDh); + engineDh = (we_Dh *)DH_get_ex_data(dh, WE_DH_EX_DATA_IDX); + if (engineDh == NULL) { + WOLFENGINE_ERROR_FUNC_NULL(WE_LOG_KE, "DH_get_ex_data", engineDh); + ret = 0; + } + } + + if (ret == 1) { + params = DH_new_method(NULL); + if (params == NULL) { + WOLFENGINE_ERROR_FUNC_NULL(WE_LOG_KE, "DH_new_method", params); + ret = 0; + } + } + if (ret == 1) { + ret = DH_set_method(params, DH_OpenSSL()); + if (ret != 1) { + WOLFENGINE_ERROR_FUNC(WE_LOG_KE, "DH_set_method", ret); + } + } + if (ret == 1) { + ret = DH_generate_parameters_ex(params, primeLen, g, cb); + if (ret != 1) { + WOLFENGINE_ERROR_FUNC(WE_LOG_KE, "DH_generate_parameters_ex", + ret); + } + } + if (ret == 1) { + DH_get0_pqg(params, ¶mP, ¶mQ, ¶mG); + p = BN_dup(paramP); + if (paramQ != NULL) + q = BN_dup(paramQ); + generator = BN_dup(paramG); + if ((p == NULL) || ((paramQ != NULL) && (q == NULL)) || + (generator == NULL)) { + WOLFENGINE_ERROR_MSG(WE_LOG_KE, "Failed to duplicate DH params"); + ret = 0; + } + } + if (ret == 1) { + ret = DH_set0_pqg(dh, p, q, generator); + if (ret != 1) { + WOLFENGINE_ERROR_FUNC(WE_LOG_KE, "DH_set0_pqg", ret); + } + else { + p = NULL; + q = NULL; + generator = NULL; + } } + if (ret == 1) { + ret = we_dh_set_parameters(dh, engineDh); + if (ret != 1) { + WOLFENGINE_ERROR_FUNC(WE_LOG_KE, "we_dh_set_parameters", ret); + } + } + + BN_free(generator); + BN_free(q); + BN_free(p); + DH_free(params); +#endif WOLFENGINE_LEAVE(WE_LOG_KE, "we_dh_generate_parameters", ret); diff --git a/test/test_dh.c b/test/test_dh.c index 113b1c87..0fed6ce0 100644 --- a/test/test_dh.c +++ b/test/test_dh.c @@ -49,11 +49,12 @@ static const unsigned char dh_g[] = 0x02, }; -#if defined(WE_DH_USE_GLOBAL_RNG) && !defined(WE_SINGLE_THREADED) && \ - !defined(TEST_MULTITHREADED) && !defined(_WIN32) +#if defined(WE_DH_USE_GLOBAL_RNG) && defined(WE_HAVE_EVP_PKEY) && \ + !defined(WE_SINGLE_THREADED) && !defined(TEST_MULTITHREADED) && \ + !defined(_WIN32) extern wolfSSL_Mutex* we_rng_mutex; -static int test_dh_failed_rng_lock(DH* dh) +static int test_dh_failed_rng_lock(ENGINE* e) { int err = 0; int attrInit = 0; @@ -63,10 +64,19 @@ static int test_dh_failed_rng_lock(DH* dh) pthread_mutexattr_t attr; wolfSSL_Mutex mutex; wolfSSL_Mutex* savedMutex = we_rng_mutex; + EVP_PKEY_CTX* ctx = NULL; + EVP_PKEY* params = NULL; + + ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_DH, e); + err = ctx == NULL; + if (err == 0) + err = EVP_PKEY_paramgen_init(ctx) != 1; - rc = pthread_mutexattr_init(&attr); - err = rc != 0; - attrInit = rc == 0; + if (err == 0) { + rc = pthread_mutexattr_init(&attr); + err = rc != 0; + attrInit = rc == 0; + } if (err == 0) { rc = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK); err = rc != 0; @@ -83,7 +93,7 @@ static int test_dh_failed_rng_lock(DH* dh) } if (err == 0) { we_rng_mutex = &mutex; - rc = DH_generate_parameters_ex(dh, 1024, DH_GENERATOR_5, NULL); + rc = EVP_PKEY_paramgen(ctx, ¶ms); we_rng_mutex = savedMutex; err = rc != 0; } @@ -102,6 +112,8 @@ static int test_dh_failed_rng_lock(DH* dh) } if (attrInit) pthread_mutexattr_destroy(&attr); + EVP_PKEY_free(params); + EVP_PKEY_CTX_free(ctx); return err; } @@ -274,15 +286,46 @@ static int test_dh_parameter_refresh(ENGINE *e) return err; } +#ifndef HAVE_FIPS +static int test_dh_paramgen_cb(int event, int iteration, BN_GENCB *cb) +{ +#if OPENSSL_VERSION_NUMBER < 0x10100000L + int *count = (int *)cb->arg; +#else + int *count = (int *)BN_GENCB_get_arg(cb); +#endif + + (void)event; + (void)iteration; + (*count)++; + + return 1; +} +#endif + int test_dh_pgen(ENGINE *e, void *data) { int err; - DH *dhWolfEngine; DH *dhOpenSSL = NULL; + DH *dhWolfEngine; const DH_METHOD *method = NULL; +#ifndef HAVE_FIPS const BIGNUM *p = NULL; const BIGNUM *q = NULL; const BIGNUM *g = NULL; + const BIGNUM *oldP = NULL; + const BIGNUM *oldQ = NULL; + const BIGNUM *oldG = NULL; + BIGNUM *copyP = NULL; + BIGNUM *copyQ = NULL; + BIGNUM *copyG = NULL; + BIGNUM *existingQ = NULL; + BN_GENCB *cb = NULL; +#if OPENSSL_VERSION_NUMBER < 0x10100000L + BN_GENCB cbData; +#endif + int callbackCount = 0; +#endif (void)data; @@ -297,36 +340,107 @@ int test_dh_pgen(ENGINE *e, void *data) if (err == 0) { DH_set_method(dhWolfEngine, method); } -#if defined(WE_DH_USE_GLOBAL_RNG) && !defined(WE_SINGLE_THREADED) && \ - !defined(TEST_MULTITHREADED) && !defined(_WIN32) +#if defined(WE_DH_USE_GLOBAL_RNG) && defined(WE_HAVE_EVP_PKEY) && \ + !defined(WE_SINGLE_THREADED) && !defined(TEST_MULTITHREADED) && \ + !defined(_WIN32) if (err == 0) { PRINT_MSG("Keep RNG mutex ownership when locking fails"); - err = test_dh_failed_rng_lock(dhWolfEngine); + err = test_dh_failed_rng_lock(e); } #endif if (err == 0) { - /* Generator and callback (last param) ignored by wolfEngine. */ - err = DH_generate_parameters_ex(dhWolfEngine, 1024, DH_GENERATOR_5, - NULL) != 1; + PRINT_MSG("Reject unsupported DH parameter generators"); + err = DH_generate_parameters_ex(dhWolfEngine, 1024, 1, NULL) > 0; + } +#ifdef HAVE_FIPS + if (err == 0) { + PRINT_MSG("Reject legacy DH parameter generation in FIPS mode"); + err = DH_generate_parameters_ex(dhWolfEngine, 1024, DH_GENERATOR_2, + NULL) > 0; + } +#else + if (err == 0) { +#if OPENSSL_VERSION_NUMBER < 0x10100000L + cb = &cbData; +#else + cb = BN_GENCB_new(); + err = cb == NULL; +#endif + } + if (err == 0) { + BN_GENCB_set(cb, test_dh_paramgen_cb, &callbackCount); + PRINT_MSG("Honor the requested DH generator"); + err = DH_generate_parameters_ex(dhWolfEngine, 1024, DH_GENERATOR_2, + cb) != 1; + } + if (err == 0) { + PRINT_MSG("Forward the DH parameter generation callback"); + err = callbackCount == 0; } - if (err == 0) { DH_get0_pqg(dhWolfEngine, &p, &q, &g); - + PRINT_MSG("Check generated DH parameters"); + err = (p == NULL) || (g == NULL) || + (BN_get_word(g) != DH_GENERATOR_2); + } + if (err == 0) { dhOpenSSL = DH_new(); - err = (dhOpenSSL == NULL); + err = dhOpenSSL == NULL; } if (err == 0) { - err = DH_set0_pqg(dhOpenSSL, BN_dup(p), BN_dup(q), BN_dup(g)) != 1; + PRINT_MSG("Copy generated DH parameters"); + copyP = BN_dup(p); + if (q != NULL) + copyQ = BN_dup(q); + copyG = BN_dup(g); + err = (copyP == NULL) || ((q != NULL) && (copyQ == NULL)) || + (copyG == NULL); + } + if (err == 0) { + err = DH_set0_pqg(dhOpenSSL, copyP, copyQ, copyG) != 1; + if (err == 0) { + copyP = NULL; + copyQ = NULL; + copyG = NULL; + } } - if (err == 0) { + PRINT_MSG("Use generated DH parameters"); err = test_dh_keygen(dhOpenSSL, dhWolfEngine); } + if (err == 0) { + existingQ = BN_new(); + err = (existingQ == NULL) || (BN_set_word(existingQ, 23) != 1); + } + if (err == 0) { + err = DH_set0_pqg(dhWolfEngine, NULL, existingQ, NULL) != 1; + if (err == 0) + existingQ = NULL; + } + if (err == 0) { + DH_get0_pqg(dhWolfEngine, &oldP, &oldQ, &oldG); + PRINT_MSG("Reject replacing DH parameters that contain q"); + err = DH_generate_parameters_ex(dhWolfEngine, 1024, DH_GENERATOR_2, + NULL) > 0; + } + if (err == 0) { + DH_get0_pqg(dhWolfEngine, &p, &q, &g); + err = (p != oldP) || (q != oldQ) || (g != oldG); + } +#endif if (err == 0) { err = test_dh_parameter_refresh(e); } +#ifndef HAVE_FIPS +#if OPENSSL_VERSION_NUMBER >= 0x10100000L + BN_GENCB_free(cb); +#endif + BN_free(existingQ); + BN_free(copyG); + BN_free(copyQ); + BN_free(copyP); +#endif DH_free(dhOpenSSL); DH_free(dhWolfEngine); From e450d880307641577b0627a99d012c8c60850f68 Mon Sep 17 00:00:00 2001 From: Aidan Garske Date: Mon, 12 Oct 2026 13:43:21 -0700 Subject: [PATCH 7/8] F-13043 - Size DH generator allocation correctly --- .gitignore | 3 + src/we_dh.c | 2 +- test-openssl-version.sh | 17 ++++ test/include.am | 7 ++ test/test_dh_alloc.c | 166 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 194 insertions(+), 1 deletion(-) create mode 100644 test/test_dh_alloc.c diff --git a/.gitignore b/.gitignore index 0cf3f344..d297b1b1 100644 --- a/.gitignore +++ b/.gitignore @@ -44,6 +44,9 @@ /test/.deps /test/.libs /test/.dirstamp +/test/dh_alloc.test +/test/dh_alloc.log +/test/dh_alloc.trs /test/unit.test /test/unit.log /test/unit.trs diff --git a/src/we_dh.c b/src/we_dh.c index fc40cdd7..7aa3da94 100644 --- a/src/we_dh.c +++ b/src/we_dh.c @@ -774,7 +774,7 @@ static int we_dh_convert_params(DhKey *wolfDh, DH *osslDh) } if (ret == 1) { /* Allocate buffer for generator. */ - g = (unsigned char*)OPENSSL_malloc(pLen); + g = (unsigned char*)OPENSSL_malloc(gLen == 0 ? 1 : gLen); if (g == NULL) { WOLFENGINE_ERROR_FUNC_NULL(WE_LOG_KE, "OPENSSL_malloc(g)", g); ret = 0; diff --git a/test-openssl-version.sh b/test-openssl-version.sh index b25ad68c..319dc975 100755 --- a/test-openssl-version.sh +++ b/test-openssl-version.sh @@ -135,6 +135,23 @@ do_config() { if [ "$VERBOSE" = "yes" ]; then cat $TMP_FILE fi + + echo -n " dh_alloc.test ... " + LD_LIBRARY_PATH=$OPENSSL_DIR ./test/dh_alloc.test >$TMP_FILE 2>&1 + DH_ALLOC_RESULT=$? + case $DH_ALLOC_RESULT in + 0) echo "PASS" ;; + 77) echo "SKIP" ;; + *) + cat $TMP_FILE + echo "DH allocation test failed for wolfSSL engine" + do_cleanup + exit 1 + ;; + esac + if [ "$VERBOSE" = "yes" ]; then + cat $TMP_FILE + fi } while [ $# -gt 0 ] diff --git a/test/include.am b/test/include.am index 4ab850c9..88af641e 100644 --- a/test/include.am +++ b/test/include.am @@ -27,3 +27,10 @@ test_unit_test_SOURCES = \ test/unit.c test_unit_test_LDADD = libwolfengine.la noinst_HEADERS += test/unit.h + +check_PROGRAMS += test/dh_alloc.test +noinst_PROGRAMS += test/dh_alloc.test +DISTCLEANFILES += test/.libs/dh_alloc.test + +test_dh_alloc_test_SOURCES = test/test_dh_alloc.c +test_dh_alloc_test_LDADD = libwolfengine.la diff --git a/test/test_dh_alloc.c b/test/test_dh_alloc.c new file mode 100644 index 00000000..c8d86149 --- /dev/null +++ b/test/test_dh_alloc.c @@ -0,0 +1,166 @@ +/* test_dh_alloc.c + * + * Copyright (C) 2026 wolfSSL Inc. + * + * This file is part of wolfengine. + * + * wolfengine is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * wolfengine is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ + +#include +#include +#include + +#include +#ifdef WOLFENGINE_USER_SETTINGS +#include +#else +#include +#endif +#include +#include +#include +#include +#include +#include + +#if defined(WE_HAVE_DH) && defined(WE_HAVE_EVP_PKEY) && \ + defined(HAVE_PUBLIC_FFDHE) && defined(HAVE_FFDHE_2048) && \ + !defined(HAVE_FFDHE_Q) && !defined(TEST_MULTITHREADED) && \ + !defined(WE_NO_OPENSSL_MALLOC) && !defined(OPENSSL_NO_FILENAMES) + +static int dhAllocTracking = 0; +static size_t dhAllocSizes[32]; +static int dhAllocCount = 0; + +static void* dh_alloc_malloc(size_t len, const char* file, int line) +{ + (void)line; + + if (dhAllocTracking && (file != NULL) && + (strstr(file, "we_dh.c") != NULL) && + (dhAllocCount < (int)(sizeof(dhAllocSizes) / sizeof(*dhAllocSizes)))) { + dhAllocSizes[dhAllocCount++] = len; + } + + return malloc(len); +} + +static void* dh_alloc_realloc(void* ptr, size_t len, const char* file, int line) +{ + (void)file; + (void)line; + + return realloc(ptr, len); +} + +#if OPENSSL_VERSION_NUMBER >= 0x10100000L +static void dh_alloc_free(void* ptr, const char* file, int line) +{ + (void)file; + (void)line; + + free(ptr); +} +#else +static void dh_alloc_free_legacy(void* ptr) +{ + free(ptr); +} +#endif + +int main(void) +{ + int err = 0; + int matched = 0; + int i; + ENGINE* e = NULL; + EVP_PKEY_CTX* ctx = NULL; + EVP_PKEY* key = NULL; + const DH* keyDh = NULL; + const BIGNUM* keyG = NULL; + + /* Memory hooks must precede all OpenSSL initialization in this process. */ +#if OPENSSL_VERSION_NUMBER < 0x10100000L + err = CRYPTO_set_mem_ex_functions(dh_alloc_malloc, dh_alloc_realloc, + dh_alloc_free_legacy) != 1; +#else + err = CRYPTO_set_mem_functions(dh_alloc_malloc, dh_alloc_realloc, + dh_alloc_free) != 1; +#endif + if (err == 0) { + ENGINE_load_wolfengine(); + e = ENGINE_by_id(wolfengine_id); + err = e == NULL; + } + if (err == 0) { + ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_DH, e); + err = ctx == NULL; + } + if (err == 0) { + err = EVP_PKEY_paramgen_init(ctx) != 1; + } + if (err == 0) { + err = EVP_PKEY_CTX_ctrl_str(ctx, "dh_param", "ffdhe2048") != 1; + } + if (err == 0) { + err = EVP_PKEY_keygen_init(ctx) != 1; + } + if (err == 0) { + dhAllocTracking = 1; + err = EVP_PKEY_keygen(ctx, &key) != 1; + dhAllocTracking = 0; + } + if (err == 0) { + keyDh = EVP_PKEY_get0_DH(key); + if (keyDh != NULL) + keyG = DH_get0_g(keyDh); + err = keyG == NULL; + } + if (err == 0) { + for (i = 0; !matched && (i + 2 < dhAllocCount); i++) { + matched = (dhAllocSizes[i] == (size_t)DH_size(keyDh)) && + (dhAllocSizes[i + 1] == 1) && + (dhAllocSizes[i + 2] == (size_t)BN_num_bytes(keyG)); + } + err = !matched; + } + + EVP_PKEY_free(key); + EVP_PKEY_CTX_free(ctx); + ENGINE_free(e); +#if OPENSSL_VERSION_NUMBER < 0x10100000L + EVP_cleanup(); + ENGINE_cleanup(); + ERR_free_strings(); +#endif + + if (err != 0) + fprintf(stderr, "DH encoded allocation lengths failed\n"); + else + printf("DH encoded allocation lengths passed\n"); + + return err; +} + +#else + +int main(void) +{ + printf("DH allocation tracking unavailable in this configuration\n"); + return 77; +} + +#endif From fe6e53153ded7696918aa25ff54b5986bd8ab5cf Mon Sep 17 00:00:00 2001 From: Aidan Garske Date: Mon, 12 Oct 2026 13:54:14 -0700 Subject: [PATCH 8/8] F-13068 - Validate legacy DH prime lengths --- src/we_dh.c | 11 ++++++++++- test/test_dh.c | 9 +++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/we_dh.c b/src/we_dh.c index 7aa3da94..55671989 100644 --- a/src/we_dh.c +++ b/src/we_dh.c @@ -33,6 +33,11 @@ #define DEFAULT_PRIME_LEN 1024 +static int we_dh_prime_len_valid(int primeLen) +{ + return (primeLen == 1024) || (primeLen == 2048) || (primeLen == 3072); +} + /** * Data required to complete DH operations. */ @@ -942,6 +947,10 @@ static int we_dh_generate_params(DH *dh, int primeLen, int g, BN_GENCB *cb) "supported in FIPS mode"); #else ret = 1; + if (!we_dh_prime_len_valid(primeLen)) { + WOLFENGINE_ERROR_MSG(WE_LOG_KE, "Invalid DH prime bit length"); + ret = 0; + } if ((ret == 1) && (DH_get0_q(dh) != NULL)) { WOLFENGINE_ERROR_MSG(WE_LOG_KE, "Cannot replace DH parameters with q"); ret = 0; @@ -1150,7 +1159,7 @@ static int we_dh_pkey_ctrl(EVP_PKEY_CTX *ctx, int type, int num, void *ptr) WOLFENGINE_MSG(WE_LOG_KE, "EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN"); /* These are the sizes allowed by wolfCrypt. */ - if (num != 1024 && num != 2048 && num != 3072) { + if (!we_dh_prime_len_valid(num)) { WOLFENGINE_ERROR_MSG(WE_LOG_KE, "Invalid DH prime bit length."); ret = 0; diff --git a/test/test_dh.c b/test/test_dh.c index 0fed6ce0..60c98acd 100644 --- a/test/test_dh.c +++ b/test/test_dh.c @@ -352,6 +352,15 @@ int test_dh_pgen(ENGINE *e, void *data) PRINT_MSG("Reject unsupported DH parameter generators"); err = DH_generate_parameters_ex(dhWolfEngine, 1024, 1, NULL) > 0; } + if (err == 0) { + PRINT_MSG("Reject unsupported DH prime lengths before generation"); + err = DH_generate_parameters_ex(dhWolfEngine, 512, DH_GENERATOR_2, + NULL) > 0; + } + if (err == 0) { + PRINT_MSG("Check rejected DH parameter generation left no prime"); + err = DH_get0_p(dhWolfEngine) != NULL; + } #ifdef HAVE_FIPS if (err == 0) { PRINT_MSG("Reject legacy DH parameter generation in FIPS mode");