Skip to content

Commit 5fa08e7

Browse files
committed
DH: honor explicit prime in wc_DhCheckPrivKey_ex (Fenrir F5317)
1 parent 18c9684 commit 5fa08e7

2 files changed

Lines changed: 22 additions & 6 deletions

File tree

wolfcrypt/src/dh.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1808,12 +1808,9 @@ int wc_DhCheckPrivKey_ex(DhKey* key, const byte* priv, word32 privSz,
18081808
if (ret == 0) {
18091809
if (mp_iszero(q) == MP_NO) {
18101810
/* priv (x) shouldn't be greater than q - 1 */
1811-
if (mp_copy(&key->q, q) != MP_OKAY)
1812-
ret = MP_INIT_E;
1813-
if (ret == 0) {
1814-
if (mp_sub_d(q, 1, q) != MP_OKAY)
1815-
ret = MP_SUB_E;
1816-
}
1811+
/* q already holds the supplied prime or key->q; do not clobber it */
1812+
if (mp_sub_d(q, 1, q) != MP_OKAY)
1813+
ret = MP_SUB_E;
18171814
if (ret == 0) {
18181815
if (mp_cmp(x, q) == MP_GT)
18191816
ret = DH_CHECK_PRIV_E;

wolfcrypt/test/test.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30504,6 +30504,9 @@ static wc_test_ret_t dh_check_priv_key_test(DhKey* key, const byte* priv,
3050430504
{
3050530505
wc_test_ret_t ret;
3050630506
word32 pSz, qSz, gSz;
30507+
byte pBuf[DH_TEST_BUF_SIZE];
30508+
byte qBuf[DH_TEST_BUF_SIZE];
30509+
byte gBuf[DH_TEST_BUF_SIZE];
3050730510

3050830511
ret = wc_DhCheckPrivKey(NULL, NULL, 0);
3050930512
if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG))
@@ -30521,6 +30524,22 @@ static wc_test_ret_t dh_check_priv_key_test(DhKey* key, const byte* priv,
3052130524
if (ret != WC_NO_ERR_TRACE(LENGTH_ONLY_E))
3052230525
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_dh_check_priv);
3052330526

30527+
/* Regression: the explicit prime argument to wc_DhCheckPrivKey_ex must be
30528+
* honored and not overwritten by key->q. This key is set with p and g
30529+
* only, so key->q is empty; supplying the modulus p as a generous bound
30530+
* must accept any valid priv (priv < p). The prior defect clobbered the
30531+
* loaded bound with the empty key->q and rejected a valid key. */
30532+
if (pSz <= (word32)sizeof(pBuf) && qSz <= (word32)sizeof(qBuf) &&
30533+
gSz <= (word32)sizeof(gBuf)) {
30534+
ret = wc_DhExportParamsRaw(key, pBuf, &pSz, qBuf, &qSz, gBuf, &gSz);
30535+
if (ret != 0)
30536+
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_dh_check_priv);
30537+
30538+
ret = wc_DhCheckPrivKey_ex(key, priv, privSz, pBuf, pSz);
30539+
if (ret != 0)
30540+
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_dh_check_priv);
30541+
}
30542+
3052430543
ret = 0;
3052530544

3052630545
exit_dh_check_priv:

0 commit comments

Comments
 (0)