@@ -5115,6 +5115,21 @@ static WC_INLINE sp_int_digit sp_div_word(sp_int_digit hi, sp_int_digit lo,
51155115 (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA) || \
51165116 (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_PUBLIC_ONLY))
51175117#ifndef WC_NO_CACHE_RESISTANT
5118+ #ifdef WC_NO_PTR_INT_CAST
5119+ static void _sp_cond_copy(const sp_int* a, int copy, sp_int* r, sp_size_t used)
5120+ {
5121+ sp_int_digit mask = (sp_int_digit)0 - (sp_int_digit)copy;
5122+ unsigned int i;
5123+
5124+ for (i = 0; i < (unsigned int)used; i++) {
5125+ r->dp[i] ^= (r->dp[i] ^ a->dp[i]) & mask;
5126+ }
5127+ r->used ^= (r->used ^ a->used) & (sp_size_t)mask;
5128+ #ifdef WOLFSSL_SP_INT_NEGATIVE
5129+ r->sign ^= (r->sign ^ a->sign) & (sp_sign_t)mask;
5130+ #endif
5131+ }
5132+ #else
51185133 /* Mask of address for constant time operations. */
51195134 const size_t sp_off_on_addr[2] =
51205135 {
@@ -5123,6 +5138,7 @@ static WC_INLINE sp_int_digit sp_div_word(sp_int_digit hi, sp_int_digit lo,
51235138 };
51245139#endif
51255140#endif
5141+ #endif
51265142
51275143
51285144#if defined(WOLFSSL_HAVE_SP_DH) || defined(WOLFSSL_HAVE_SP_RSA)
@@ -13166,13 +13182,23 @@ static int _sp_exptmod_ex(const sp_int* b, const sp_int* e, int bits,
1316613182 }
1316713183#else
1316813184 /* 4.1. t[s] = t[s] ^ 2 */
13185+ #ifdef WC_NO_PTR_INT_CAST
13186+ _sp_cond_copy(t[0], s^1, t[2], m->used);
13187+ _sp_cond_copy(t[1], s, t[2], m->used);
13188+ #else
1316913189 _sp_copy((sp_int*)(((size_t)t[0] & sp_off_on_addr[s^1]) +
1317013190 ((size_t)t[1] & sp_off_on_addr[s ])),
1317113191 t[2]);
13192+ #endif
1317213193 err = sp_sqrmod(t[2], m, t[2]);
13194+ #ifdef WC_NO_PTR_INT_CAST
13195+ _sp_cond_copy(t[2], s^1, t[0], m->used);
13196+ _sp_cond_copy(t[2], s, t[1], m->used);
13197+ #else
1317313198 _sp_copy(t[2],
1317413199 (sp_int*)(((size_t)t[0] & sp_off_on_addr[s^1]) +
1317513200 ((size_t)t[1] & sp_off_on_addr[s ])));
13201+ #endif
1317613202
1317713203 if (err == MP_OKAY) {
1317813204 /* 4.2. y = e[i] */
@@ -13183,13 +13209,23 @@ static int _sp_exptmod_ex(const sp_int* b, const sp_int* e, int bits,
1318313209 /* 4.4 s = s | y */
1318413210 s |= y;
1318513211 /* 4.5. t[j] = t[j] * b */
13212+ #ifdef WC_NO_PTR_INT_CAST
13213+ _sp_cond_copy(t[0], j^1, t[2], m->used);
13214+ _sp_cond_copy(t[1], j, t[2], m->used);
13215+ #else
1318613216 _sp_copy((sp_int*)(((size_t)t[0] & sp_off_on_addr[j^1]) +
1318713217 ((size_t)t[1] & sp_off_on_addr[j ])),
1318813218 t[2]);
13219+ #endif
1318913220 err = _sp_mulmod(t[2], b, m, t[2]);
13221+ #ifdef WC_NO_PTR_INT_CAST
13222+ _sp_cond_copy(t[2], j^1, t[0], m->used);
13223+ _sp_cond_copy(t[2], j, t[1], m->used);
13224+ #else
1319013225 _sp_copy(t[2],
1319113226 (sp_int*)(((size_t)t[0] & sp_off_on_addr[j^1]) +
1319213227 ((size_t)t[1] & sp_off_on_addr[j ])));
13228+ #endif
1319313229 }
1319413230#endif
1319513231 }
@@ -13279,9 +13315,14 @@ static int _sp_exptmod_ex(const sp_int* b, const sp_int* e, int bits,
1327913315 err = sp_mulmod(t[0], t[1], m, t[2]);
1328013316 /* 3.3. t[3] = t[y] ^ 2 */
1328113317 if (err == MP_OKAY) {
13318+ #ifdef WC_NO_PTR_INT_CAST
13319+ _sp_cond_copy(t[0], y^1, t[3], m->used);
13320+ _sp_cond_copy(t[1], y, t[3], m->used);
13321+ #else
1328213322 _sp_copy((sp_int*)(((size_t)t[0] & sp_off_on_addr[y^1]) +
1328313323 ((size_t)t[1] & sp_off_on_addr[y ])),
1328413324 t[3]);
13325+ #endif
1328513326 err = sp_sqrmod(t[3], m, t[3]);
1328613327 }
1328713328 /* 3.4. t[y] = t[3], t[y^1] = t[2] */
@@ -13403,16 +13444,26 @@ static int _sp_exptmod_mont_ex(const sp_int* b, const sp_int* e, int bits,
1340313444 /* 6. For i in (bits-1)...0 */
1340413445 for (i = bits - 1; (err == MP_OKAY) && (i >= 0); i--) {
1340513446 /* 6.1. t[s] = t[s] ^ 2 */
13447+ #ifdef WC_NO_PTR_INT_CAST
13448+ _sp_cond_copy(t[0], s^1, t[3], m->used);
13449+ _sp_cond_copy(t[1], s, t[3], m->used);
13450+ #else
1340613451 _sp_copy((sp_int*)(((size_t)t[0] & sp_off_on_addr[s^1]) +
1340713452 ((size_t)t[1] & sp_off_on_addr[s ])),
1340813453 t[3]);
13454+ #endif
1340913455 err = sp_sqr(t[3], t[3]);
1341013456 if (err == MP_OKAY) {
1341113457 err = _sp_mont_red(t[3], m, mp, 0);
1341213458 }
13459+ #ifdef WC_NO_PTR_INT_CAST
13460+ _sp_cond_copy(t[3], s^1, t[0], m->used);
13461+ _sp_cond_copy(t[3], s, t[1], m->used);
13462+ #else
1341313463 _sp_copy(t[3],
1341413464 (sp_int*)(((size_t)t[0] & sp_off_on_addr[s^1]) +
1341513465 ((size_t)t[1] & sp_off_on_addr[s ])));
13466+ #endif
1341613467
1341713468 if (err == MP_OKAY) {
1341813469 /* 6.2. y = e[i] */
@@ -13424,16 +13475,26 @@ static int _sp_exptmod_mont_ex(const sp_int* b, const sp_int* e, int bits,
1342413475 s |= y;
1342513476
1342613477 /* 6.5. t[j] = t[j] * bm */
13478+ #ifdef WC_NO_PTR_INT_CAST
13479+ _sp_cond_copy(t[0], j^1, t[3], m->used);
13480+ _sp_cond_copy(t[1], j, t[3], m->used);
13481+ #else
1342713482 _sp_copy((sp_int*)(((size_t)t[0] & sp_off_on_addr[j^1]) +
1342813483 ((size_t)t[1] & sp_off_on_addr[j ])),
1342913484 t[3]);
13485+ #endif
1343013486 err = sp_mul(t[3], t[2], t[3]);
1343113487 if (err == MP_OKAY) {
1343213488 err = _sp_mont_red(t[3], m, mp, 0);
1343313489 }
13490+ #ifdef WC_NO_PTR_INT_CAST
13491+ _sp_cond_copy(t[3], j^1, t[0], m->used);
13492+ _sp_cond_copy(t[3], j, t[1], m->used);
13493+ #else
1343413494 _sp_copy(t[3],
1343513495 (sp_int*)(((size_t)t[0] & sp_off_on_addr[j^1]) +
1343613496 ((size_t)t[1] & sp_off_on_addr[j ])));
13497+ #endif
1343713498 }
1343813499 }
1343913500 if (err == MP_OKAY) {
@@ -13543,9 +13604,14 @@ static int _sp_exptmod_mont_ex(const sp_int* b, const sp_int* e, int bits,
1354313604 }
1354413605 /* 4.3. t[3] = t[y] ^ 2 */
1354513606 if (err == MP_OKAY) {
13607+ #ifdef WC_NO_PTR_INT_CAST
13608+ _sp_cond_copy(t[0], y^1, t[3], m->used);
13609+ _sp_cond_copy(t[1], y, t[3], m->used);
13610+ #else
1354613611 _sp_copy((sp_int*)(((size_t)t[0] & sp_off_on_addr[y^1]) +
1354713612 ((size_t)t[1] & sp_off_on_addr[y ])),
1354813613 t[3]);
13614+ #endif
1354913615 err = sp_sqr(t[3], t[3]);
1355013616 }
1355113617 if (err == MP_OKAY) {
0 commit comments