Skip to content

Commit 833186a

Browse files
authored
Return error in even moduli in sp_exptmod_nct for RSA public-only builds (#11849)
* Handle even moduli in sp_exptmod_nct for RSA public-only builds * Reject even moduli in sp_exptmod_nct when the generic path is not built * Align even modulus test guard and MP_VAL docs with sp_exptmod_nct * Reject even moduli in sp_exptmod_nct for all RSA verify/public builds No RSA, DH or ECC modulus is even, so the guard no longer depends on DH.
1 parent 20fc06f commit 833186a

3 files changed

Lines changed: 40 additions & 8 deletions

File tree

‎.github/configs/os-check-linux.json‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,14 @@
598598
"configure": ["--enable-cryptonly", "--enable-asn=no", "--disable-rsa",
599599
"--disable-dh", "--disable-fpecc", "--with-eccminsz=160",
600600
"--enable-ecccustcurves=all"]},
601+
{"name": "cryptonly-rsapub-no-dh-public-mp", "minutes": 1.0,
602+
"comment": "RSA public only with DH off, so sp_exptmod_ex hands every modulus to sp_exptmod_nct. WOLFSSL_PUBLIC_MP lets wolfcrypt test run mp_test, whose even modulus checks confirm both return MP_VAL instead of a wrong Montgomery result.",
603+
"configure": ["--enable-cryptonly", "--enable-rsapub", "--disable-dh",
604+
"--disable-examples", "CPPFLAGS=-DWOLFSSL_PUBLIC_MP"]},
605+
{"name": "cryptonly-rsapub-public-mp", "minutes": 1.0,
606+
"comment": "RSA public only with DH at its default (on). sp_exptmod_ex keeps its existing route for an even modulus to the generic exponentiation, while sp_exptmod_nct returns MP_VAL. WOLFSSL_PUBLIC_MP lets wolfcrypt test run mp_test's even modulus checks in this shape.",
607+
"configure": ["--enable-cryptonly", "--enable-rsapub",
608+
"--disable-examples", "CPPFLAGS=-DWOLFSSL_PUBLIC_MP"]},
601609
{"name": "pkcs12-pbkdf-mp-api", "minutes": 1.0,
602610
"comment": "WC_PKCS12_PBKDF_USING_MP_API selects a second wc_PKCS12_PBKDF_ex implementation that no configure option reaches.",
603611
"configure": ["--enable-pkcs12", "--enable-des3", "--enable-opensslextra",

‎wolfcrypt/src/sp_int.c‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14629,7 +14629,8 @@ static int _sp_exptmod_base_2(const sp_int* e, int digits, const sp_int* m,
1462914629
*
1463014630
* @return MP_OKAY on success.
1463114631
* @return MP_VAL when b, e, m or r is NULL, digits is negative, or m <= 0 or
14632-
* e is negative.
14632+
* e is negative; or m is even in an RSA verify/public only build
14633+
* without DH.
1463314634
* @return MP_MEM when dynamic memory allocation fails.
1463414635
*/
1463514636
int sp_exptmod_ex(const sp_int* b, const sp_int* e, int digits, const sp_int* m,
@@ -14822,7 +14823,8 @@ int sp_exptmod_ex(const sp_int* b, const sp_int* e, int digits, const sp_int* m,
1482214823
* @param [out] r SP integer to hold result.
1482314824
*
1482414825
* @return MP_OKAY on success.
14825-
* @return MP_VAL when b, e, m or r is NULL; or m <= 0 or e is negative.
14826+
* @return MP_VAL when b, e, m or r is NULL; or m <= 0 or e is negative; or
14827+
* m is even in an RSA verify/public only build without DH.
1482614828
* @return MP_MEM when dynamic memory allocation fails.
1482714829
*/
1482814830
int sp_exptmod(const sp_int* b, const sp_int* e, const sp_int* m, sp_int* r)
@@ -15282,7 +15284,9 @@ static int _sp_exptmod_nct(const sp_int* b, const sp_int* e, const sp_int* m,
1528215284
* @param [out] r SP integer to hold result.
1528315285
*
1528415286
* @return MP_OKAY on success.
15285-
* @return MP_VAL when b, e, m or r is NULL; or m <= 0 or e is negative.
15287+
* @return MP_VAL when b, e, m or r is NULL; or m <= 0 or e is negative; or
15288+
* m is even in an RSA verify/public only build, even where
15289+
* sp_exptmod_ex accepts it (DH builds).
1528615290
* @return MP_MEM when dynamic memory allocation fails.
1528715291
*/
1528815292
int sp_exptmod_nct(const sp_int* b, const sp_int* e, const sp_int* m, sp_int* r)
@@ -15329,11 +15333,14 @@ int sp_exptmod_nct(const sp_int* b, const sp_int* e, const sp_int* m, sp_int* r)
1532915333
else if (m->used * 2 >= r->size) {
1533015334
err = MP_VAL;
1533115335
}
15332-
#if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
1533315336
else if (mp_iseven(m)) {
15337+
#if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
1533415338
err = _sp_exptmod_ex(b, e, (int)(e->used * SP_WORD_SIZE), m, r);
15335-
}
15339+
#else
15340+
/* Montgomery form requires an odd modulus. */
15341+
err = MP_VAL;
1533615342
#endif
15343+
}
1533715344
else {
1533815345
err = _sp_exptmod_nct(b, e, m, r);
1533915346
}

‎wolfcrypt/test/test.c‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98949,9 +98949,11 @@ static wc_test_ret_t mp_test_exptmod(mp_int* b, mp_int* e, mp_int* m, mp_int* r,
9894998949
return WC_TEST_RET_ENC_NC;
9895098950
}
9895198951

98952-
/* Even, multi-word modulus uses the generic exponentiation path for
98953-
* both mp_exptmod_ex and mp_exptmod_nct. Verify b^6 mod m against
98954-
* repeated modular multiplication. */
98952+
/* Even, multi-word modulus. Verify b^6 mod m against repeated modular
98953+
* multiplication. RSA verify/public only builds reject an even modulus
98954+
* in mp_exptmod_nct rather than return a wrong Montgomery result.
98955+
* Without DH, mp_exptmod_ex hands every modulus to mp_exptmod_nct, so it
98956+
* rejects it too; with DH it uses the generic exponentiation. */
9895598957
if ((ret = mp_set(m, 1)) != MP_OKAY ||
9895698958
(ret = mp_mul_2d(m, 521, m)) != MP_OKAY || /* m = 2^521 (even) */
9895798959
(ret = mp_set(b, 7)) != MP_OKAY ||
@@ -98964,18 +98966,33 @@ static wc_test_ret_t mp_test_exptmod(mp_int* b, mp_int* e, mp_int* m, mp_int* r,
9896498966
return WC_TEST_RET_ENC_EC(ret);
9896598967
}
9896698968
}
98969+
#if (defined(WOLFSSL_RSA_VERIFY_ONLY) || defined(WOLFSSL_RSA_PUBLIC_ONLY)) && \
98970+
defined(NO_DH)
98971+
ret = mp_exptmod_ex(b, e, (int)e->used, m, r);
98972+
if (ret != WC_NO_ERR_TRACE(MP_VAL)) {
98973+
return WC_TEST_RET_ENC_EC(ret);
98974+
}
98975+
#else
9896798976
if ((ret = mp_exptmod_ex(b, e, (int)e->used, m, r)) != MP_OKAY) {
9896898977
return WC_TEST_RET_ENC_EC(ret);
9896998978
}
9897098979
if (mp_cmp(r, t) != MP_EQ) {
9897198980
return WC_TEST_RET_ENC_NC;
9897298981
}
98982+
#endif
98983+
#if defined(WOLFSSL_RSA_VERIFY_ONLY) || defined(WOLFSSL_RSA_PUBLIC_ONLY)
98984+
ret = mp_exptmod_nct(b, e, m, r);
98985+
if (ret != WC_NO_ERR_TRACE(MP_VAL)) {
98986+
return WC_TEST_RET_ENC_EC(ret);
98987+
}
98988+
#else
9897398989
if ((ret = mp_exptmod_nct(b, e, m, r)) != MP_OKAY) {
9897498990
return WC_TEST_RET_ENC_EC(ret);
9897598991
}
9897698992
if (mp_cmp(r, t) != MP_EQ) {
9897798993
return WC_TEST_RET_ENC_NC;
9897898994
}
98995+
#endif
9897998996

9898098997
#if !defined(HAVE_FIPS) || FIPS_VERSION3_GE(7,0,0)
9898198998

0 commit comments

Comments
 (0)