Skip to content

Commit 6dae0c9

Browse files
authored
Merge pull request #10818 from dgarske/dh_checkprivkey_prime
DH: honor explicit prime in wc_DhCheckPrivKey_ex (Fenrir F5317)
2 parents 3fa342a + 5fa08e7 commit 6dae0c9

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
@@ -1810,12 +1810,9 @@ int wc_DhCheckPrivKey_ex(DhKey* key, const byte* priv, word32 privSz,
18101810
if (ret == 0) {
18111811
if (mp_iszero(q) == MP_NO) {
18121812
/* priv (x) shouldn't be greater than q - 1 */
1813-
if (mp_copy(&key->q, q) != MP_OKAY)
1814-
ret = MP_INIT_E;
1815-
if (ret == 0) {
1816-
if (mp_sub_d(q, 1, q) != MP_OKAY)
1817-
ret = MP_SUB_E;
1818-
}
1813+
/* q already holds the supplied prime or key->q; do not clobber it */
1814+
if (mp_sub_d(q, 1, q) != MP_OKAY)
1815+
ret = MP_SUB_E;
18191816
if (ret == 0) {
18201817
if (mp_cmp(x, q) == MP_GT)
18211818
ret = DH_CHECK_PRIV_E;

wolfcrypt/test/test.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30552,6 +30552,9 @@ static wc_test_ret_t dh_check_priv_key_test(DhKey* key, const byte* priv,
3055230552
{
3055330553
wc_test_ret_t ret;
3055430554
word32 pSz, qSz, gSz;
30555+
byte pBuf[DH_TEST_BUF_SIZE];
30556+
byte qBuf[DH_TEST_BUF_SIZE];
30557+
byte gBuf[DH_TEST_BUF_SIZE];
3055530558

3055630559
ret = wc_DhCheckPrivKey(NULL, NULL, 0);
3055730560
if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG))
@@ -30569,6 +30572,22 @@ static wc_test_ret_t dh_check_priv_key_test(DhKey* key, const byte* priv,
3056930572
if (ret != WC_NO_ERR_TRACE(LENGTH_ONLY_E))
3057030573
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_dh_check_priv);
3057130574

30575+
/* Regression: the explicit prime argument to wc_DhCheckPrivKey_ex must be
30576+
* honored and not overwritten by key->q. This key is set with p and g
30577+
* only, so key->q is empty; supplying the modulus p as a generous bound
30578+
* must accept any valid priv (priv < p). The prior defect clobbered the
30579+
* loaded bound with the empty key->q and rejected a valid key. */
30580+
if (pSz <= (word32)sizeof(pBuf) && qSz <= (word32)sizeof(qBuf) &&
30581+
gSz <= (word32)sizeof(gBuf)) {
30582+
ret = wc_DhExportParamsRaw(key, pBuf, &pSz, qBuf, &qSz, gBuf, &gSz);
30583+
if (ret != 0)
30584+
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_dh_check_priv);
30585+
30586+
ret = wc_DhCheckPrivKey_ex(key, priv, privSz, pBuf, pSz);
30587+
if (ret != 0)
30588+
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_dh_check_priv);
30589+
}
30590+
3057230591
ret = 0;
3057330592

3057430593
exit_dh_check_priv:

0 commit comments

Comments
 (0)