@@ -1259,6 +1259,12 @@ static int GeneratePrivateDh(DhKey* key, WC_RNG* rng, byte* priv,
12591259 int ret = 0 ;
12601260 word32 sz = 0 ;
12611261
1262+ /* reject primes below the minimum allowed size */
1263+ if (mp_count_bits (& key -> p ) < DH_MIN_SIZE ) {
1264+ WOLFSSL_MSG ("DH prime smaller than DH_MIN_SIZE" );
1265+ return WC_KEY_SIZE_E ;
1266+ }
1267+
12621268 if (mp_iseven (& key -> p ) == MP_YES ) {
12631269 ret = MP_VAL ;
12641270 }
@@ -1344,6 +1350,12 @@ static int GeneratePublicDh(DhKey* key, byte* priv, word32 privSz,
13441350 return WC_KEY_SIZE_E ;
13451351 }
13461352
1353+ /* reject primes below the minimum allowed size */
1354+ if (mp_count_bits (& key -> p ) < DH_MIN_SIZE ) {
1355+ WOLFSSL_MSG ("DH prime smaller than DH_MIN_SIZE" );
1356+ return WC_KEY_SIZE_E ;
1357+ }
1358+
13471359#ifdef WOLFSSL_HAVE_SP_DH
13481360#ifndef WOLFSSL_SP_NO_2048
13491361 if (mp_count_bits (& key -> p ) == 2048 )
@@ -2366,6 +2378,12 @@ int wc_DhAgree(DhKey* key, byte* agree, word32* agreeSz, const byte* priv,
23662378 (void )privSz ;
23672379 ret = KcapiDh_SharedSecret (key , otherPub , pubSz , agree , agreeSz );
23682380#else
2381+ /* reject primes below the minimum allowed size */
2382+ if (mp_count_bits (& key -> p ) < DH_MIN_SIZE ) {
2383+ WOLFSSL_MSG ("DH prime smaller than DH_MIN_SIZE" );
2384+ return WC_KEY_SIZE_E ;
2385+ }
2386+
23692387#if defined(WOLFSSL_ASYNC_CRYPT ) && defined(WC_ASYNC_ENABLE_DH )
23702388 /* Async marker takes precedence: when wolfAsync_DoSw (wolfcrypt/src/
23712389 * async.c) re-enters the compute path, wc_DhAgree_Async dispatches
0 commit comments