HPRE hardware only validates 1 < pub_key < p-1 but does not verify pub_key^q == 1 mod p. For DHX / FFDHE groups carrying q, add a small subgroup check on the engine path. On the provider path, validate the derived secret z satisfies 1 < z < p-1 (FIPS 186-4 section 6.2.2) after hardware returns. Signed-off-by: Weili Qian <qianweili@huawei.com> --- src/uadk_dh.c | 39 +++++++++++++++++++++++++++++++++++++++ src/uadk_prov_dh.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/src/uadk_dh.c b/src/uadk_dh.c index d138805..416b900 100644 --- a/src/uadk_dh.c +++ b/src/uadk_dh.c @@ -905,6 +905,35 @@ exe_soft: return uadk_e_dh_soft_generate_key(dh); } +static bool dh_check_small_subgroup(const BIGNUM *pub_key, const BIGNUM *q, + const BIGNUM *p) +{ + int valid = 0; + BIGNUM *tmp; + BN_CTX *ctx; + + ctx = BN_CTX_new(); + if (!ctx) { + fprintf(stderr, "failed to alloc BN_CTX for pub check\n"); + return false; + } + + BN_CTX_start(ctx); + tmp = BN_CTX_get(ctx); + if (tmp && BN_mod_exp(tmp, pub_key, q, p, ctx) && BN_is_one(tmp)) + valid = 1; + + BN_CTX_end(ctx); + BN_CTX_free(ctx); + + if (!valid) { + fprintf(stderr, "invalid peer pubkey: small subgroup check failed\n"); + return false; + } + + return true; +} + static int uadk_e_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh) { @@ -927,6 +956,16 @@ static int uadk_e_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, if (!p || !g) return UADK_E_FAIL; + /* + * Hardware validates 1 < pub_key < p-1 (N>M + odd check on prime p), + * but does NOT verify pub_key^q == 1 mod p. For DHX / FFDHE groups + * carrying q, perform small subgroup check in software to defend + * against small subgroup attacks. Traditional safe prime DH (q==NULL) + * relies on hardware validation only. + */ + if (q && !dh_check_small_subgroup(pub_key, q, p)) + return UADK_E_FAIL; + ret = dh_prepare_data(g, dh, &dh_sess, &priv_key); if (!ret) { fprintf(stderr, "failed to prepare dh data\n"); diff --git a/src/uadk_prov_dh.c b/src/uadk_prov_dh.c index 2e4a832..33e5536 100644 --- a/src/uadk_prov_dh.c +++ b/src/uadk_prov_dh.c @@ -1656,6 +1656,33 @@ static void uadk_prov_dh_free_compkey_req(struct uadk_dh_sess *dh_sess) } } +static int dh_validate_derived_secret(const unsigned char *data, __u16 dlen, + const BIGNUM *p) +{ + int ret = UADK_P_FAIL; + BIGNUM *pminus1; + BIGNUM *z; + + z = BN_bin2bn(data, dlen, NULL); + pminus1 = BN_new(); + if (!z || !pminus1 || !BN_copy(pminus1, p) || !BN_sub_word(pminus1, 1)) { + UADK_ERR("failed to validate derived secret\n"); + goto end; + } + + if (BN_cmp(z, BN_value_one()) <= 0 || BN_cmp(z, pminus1) == 0) { + UADK_ERR("derived secret out of valid range\n"); + goto end; + } + + ret = UADK_P_SUCCESS; + +end: + BN_free(z); + BN_free(pminus1); + return ret; +} + static int uadk_dh_compute_key(unsigned char *key, const BIGNUM *pubkey, DH *dh) { struct uadk_dh_sess *dh_sess = NULL; @@ -1701,6 +1728,11 @@ static int uadk_dh_compute_key(unsigned char *key, const BIGNUM *pubkey, DH *dh) } memcpy(key, dh_sess->req.pri, dh_sess->req.pri_bytes); + + /* Validate derived secret: 1 < z < p-1 (FIPS 186-4 section 6.2.2) */ + ret = dh_validate_derived_secret(key, dh_sess->req.pri_bytes, p); + if (ret != UADK_P_SUCCESS) + goto free_req; ret = dh_sess->req.pri_bytes; uadk_prov_dh_free_compkey_req(dh_sess); /* key will be used by user, do not free it here */ -- 2.43.0