Skip to content

Commit c66cde1

Browse files
committed
Add DH_MIN_SIZE mechanism and default to 2048
1 parent 8961b60 commit c66cde1

5 files changed

Lines changed: 40 additions & 6 deletions

File tree

tests/api/test_dh.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ int test_wc_DhAgree_subgroup_check(void)
8484
{
8585
EXPECT_DECLS;
8686
#if !defined(NO_DH) && !defined(WOLFSSL_SP_MATH) && !defined(HAVE_SELFTEST) && \
87-
(!defined(HAVE_FIPS) || FIPS_VERSION3_GT(7,0,0))
87+
(!defined(HAVE_FIPS) || FIPS_VERSION3_GT(7,0,0)) && DH_MIN_SIZE <= 512
8888
DhKey key;
8989
WC_RNG rng;
9090
byte agree[64];

wolfcrypt/src/dh.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

wolfssl/internal.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,10 +1231,8 @@ enum {
12311231
#elif WOLFSSL_HARDEN_TLS >= 112
12321232
#define WOLFSSL_MIN_DHKEY_BITS 2048
12331233
#endif
1234-
#elif defined(WOLFSSL_MAX_STRENGTH)
1235-
#define WOLFSSL_MIN_DHKEY_BITS 2048
12361234
#else
1237-
#define WOLFSSL_MIN_DHKEY_BITS 1024
1235+
#define WOLFSSL_MIN_DHKEY_BITS DH_MIN_SIZE
12381236
#endif
12391237
#endif
12401238
#if defined(WOLFSSL_HARDEN_TLS) && WOLFSSL_MIN_DHKEY_BITS < 2048 && \
@@ -1252,6 +1250,12 @@ enum {
12521250
#if (WOLFSSL_MIN_DHKEY_BITS > 16000)
12531251
#error DH minimum bit size must not be greater than 16000
12541252
#endif
1253+
#if (WOLFSSL_MIN_DHKEY_BITS < DH_MIN_SIZE)
1254+
/* The TLS-layer minimum must not be looser than the wolfCrypt DH primitive
1255+
* minimum (DH_MIN_SIZE), otherwise a key size accepted during negotiation
1256+
* is later rejected by wc_DhAgree with WC_KEY_SIZE_E. */
1257+
#error "WOLFSSL_MIN_DHKEY_BITS must be >= DH_MIN_SIZE"
1258+
#endif
12551259
#define MIN_DHKEY_SZ (WOLFSSL_MIN_DHKEY_BITS / 8)
12561260
/* set maximum DH key size allowed */
12571261
#ifndef WOLFSSL_MAX_DHKEY_BITS

wolfssl/version.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
extern "C" {
2929
#endif
3030

31-
#define LIBWOLFSSL_VERSION_STRING "5.9.2"
32-
#define LIBWOLFSSL_VERSION_HEX 0x05009002
31+
#define LIBWOLFSSL_VERSION_STRING "5.9.1"
32+
#define LIBWOLFSSL_VERSION_HEX 0x05009001
3333

3434
#ifdef __cplusplus
3535
}

wolfssl/wolfcrypt/settings.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4626,6 +4626,18 @@ blinding by defining WC_BLINDING_NO_RNG_ACKNOWLEDGE_WEAKNESS."
46264626
#endif
46274627
#endif
46284628

4629+
/* Minimum allowed DH prime size in bits. SP 800-56A Rev3 / SP 800-131A
4630+
* disallow FFC below 2048 bits. Override to permit legacy sizes. */
4631+
#ifndef DH_MIN_SIZE
4632+
#ifdef WOLFSSL_MIN_DHKEY_BITS
4633+
/* For existing build settings that use WOLFSSL_MIN_DHKEY_BITS, map
4634+
* DH_MIN_SIZE to the user's desired minimum */
4635+
#define DH_MIN_SIZE WOLFSSL_MIN_DHKEY_BITS
4636+
#else
4637+
#define DH_MIN_SIZE 2048
4638+
#endif
4639+
#endif
4640+
46294641
/* wc_Sha512.devId isn't available before FIPS 5.1 */
46304642
#if defined(HAVE_FIPS) && FIPS_VERSION_LT(5,1)
46314643
#define NO_SHA2_CRYPTO_CB

0 commit comments

Comments
 (0)