Skip to content

Commit 22fbaf5

Browse files
cheri: Fix CHERI tag violation on constant time pointer selection
The branchless code in casts sp_int pointers to size_t for bitmask arithmetic, then casts the result back to sp_int*. On CHERI, pointer-to-integer casts strip the hardware capability tag. The reconstructed pointer is tagless and cannot be dereferenced, causing a tag-violation fault. Swap this for SP_CT_ADDR(t, idx) which on CHERI will do a basic array index for now. On non-CHERI targets the behaviour is the same. Signed-off-by: William Beasley (The Capable Hub) <wbeasley@thegoodpenguin.co.uk>
1 parent 5c52734 commit 22fbaf5

1 file changed

Lines changed: 18 additions & 30 deletions

File tree

wolfcrypt/src/sp_int.c

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5121,6 +5121,14 @@ static WC_INLINE sp_int_digit sp_div_word(sp_int_digit hi, sp_int_digit lo,
51215121
(size_t) 0,
51225122
(size_t)-1
51235123
};
5124+
/* Constant time access here will not work on CHERI so fallback to basic for now */
5125+
#ifdef __CHERI__
5126+
#define SP_CT_ADDR(t, idx) ((t)[(idx)])
5127+
#else
5128+
#define SP_CT_ADDR(t, idx) \
5129+
(sp_int*)(((size_t)(t)[0] & sp_off_on_addr[(idx)^1]) + \
5130+
((size_t)(t)[1] & sp_off_on_addr[(idx)]))
5131+
#endif
51245132
#endif
51255133
#endif
51265134

@@ -13166,13 +13174,9 @@ static int _sp_exptmod_ex(const sp_int* b, const sp_int* e, int bits,
1316613174
}
1316713175
#else
1316813176
/* 4.1. t[s] = t[s] ^ 2 */
13169-
_sp_copy((sp_int*)(((size_t)t[0] & sp_off_on_addr[s^1]) +
13170-
((size_t)t[1] & sp_off_on_addr[s ])),
13171-
t[2]);
13177+
_sp_copy(SP_CT_ADDR(t, s), t[2]);
1317213178
err = sp_sqrmod(t[2], m, t[2]);
13173-
_sp_copy(t[2],
13174-
(sp_int*)(((size_t)t[0] & sp_off_on_addr[s^1]) +
13175-
((size_t)t[1] & sp_off_on_addr[s ])));
13179+
_sp_copy(t[2], SP_CT_ADDR(t, s));
1317613180

1317713181
if (err == MP_OKAY) {
1317813182
/* 4.2. y = e[i] */
@@ -13183,13 +13187,9 @@ static int _sp_exptmod_ex(const sp_int* b, const sp_int* e, int bits,
1318313187
/* 4.4 s = s | y */
1318413188
s |= y;
1318513189
/* 4.5. t[j] = t[j] * b */
13186-
_sp_copy((sp_int*)(((size_t)t[0] & sp_off_on_addr[j^1]) +
13187-
((size_t)t[1] & sp_off_on_addr[j ])),
13188-
t[2]);
13190+
_sp_copy(SP_CT_ADDR(t, j), t[2]);
1318913191
err = _sp_mulmod(t[2], b, m, t[2]);
13190-
_sp_copy(t[2],
13191-
(sp_int*)(((size_t)t[0] & sp_off_on_addr[j^1]) +
13192-
((size_t)t[1] & sp_off_on_addr[j ])));
13192+
_sp_copy(t[2], SP_CT_ADDR(t, j));
1319313193
}
1319413194
#endif
1319513195
}
@@ -13279,9 +13279,7 @@ static int _sp_exptmod_ex(const sp_int* b, const sp_int* e, int bits,
1327913279
err = sp_mulmod(t[0], t[1], m, t[2]);
1328013280
/* 3.3. t[3] = t[y] ^ 2 */
1328113281
if (err == MP_OKAY) {
13282-
_sp_copy((sp_int*)(((size_t)t[0] & sp_off_on_addr[y^1]) +
13283-
((size_t)t[1] & sp_off_on_addr[y ])),
13284-
t[3]);
13282+
_sp_copy(SP_CT_ADDR(t, y), t[3]);
1328513283
err = sp_sqrmod(t[3], m, t[3]);
1328613284
}
1328713285
/* 3.4. t[y] = t[3], t[y^1] = t[2] */
@@ -13403,16 +13401,12 @@ static int _sp_exptmod_mont_ex(const sp_int* b, const sp_int* e, int bits,
1340313401
/* 6. For i in (bits-1)...0 */
1340413402
for (i = bits - 1; (err == MP_OKAY) && (i >= 0); i--) {
1340513403
/* 6.1. t[s] = t[s] ^ 2 */
13406-
_sp_copy((sp_int*)(((size_t)t[0] & sp_off_on_addr[s^1]) +
13407-
((size_t)t[1] & sp_off_on_addr[s ])),
13408-
t[3]);
13404+
_sp_copy(SP_CT_ADDR(t, s), t[3]);
1340913405
err = sp_sqr(t[3], t[3]);
1341013406
if (err == MP_OKAY) {
1341113407
err = _sp_mont_red(t[3], m, mp, 0);
1341213408
}
13413-
_sp_copy(t[3],
13414-
(sp_int*)(((size_t)t[0] & sp_off_on_addr[s^1]) +
13415-
((size_t)t[1] & sp_off_on_addr[s ])));
13409+
_sp_copy(t[3], SP_CT_ADDR(t, s));
1341613410

1341713411
if (err == MP_OKAY) {
1341813412
/* 6.2. y = e[i] */
@@ -13424,16 +13418,12 @@ static int _sp_exptmod_mont_ex(const sp_int* b, const sp_int* e, int bits,
1342413418
s |= y;
1342513419

1342613420
/* 6.5. t[j] = t[j] * bm */
13427-
_sp_copy((sp_int*)(((size_t)t[0] & sp_off_on_addr[j^1]) +
13428-
((size_t)t[1] & sp_off_on_addr[j ])),
13429-
t[3]);
13421+
_sp_copy(SP_CT_ADDR(t, j), t[3]);
1343013422
err = sp_mul(t[3], t[2], t[3]);
1343113423
if (err == MP_OKAY) {
1343213424
err = _sp_mont_red(t[3], m, mp, 0);
1343313425
}
13434-
_sp_copy(t[3],
13435-
(sp_int*)(((size_t)t[0] & sp_off_on_addr[j^1]) +
13436-
((size_t)t[1] & sp_off_on_addr[j ])));
13426+
_sp_copy(t[3], SP_CT_ADDR(t, j));
1343713427
}
1343813428
}
1343913429
if (err == MP_OKAY) {
@@ -13543,9 +13533,7 @@ static int _sp_exptmod_mont_ex(const sp_int* b, const sp_int* e, int bits,
1354313533
}
1354413534
/* 4.3. t[3] = t[y] ^ 2 */
1354513535
if (err == MP_OKAY) {
13546-
_sp_copy((sp_int*)(((size_t)t[0] & sp_off_on_addr[y^1]) +
13547-
((size_t)t[1] & sp_off_on_addr[y ])),
13548-
t[3]);
13536+
_sp_copy(SP_CT_ADDR(t, y), t[3]);
1354913537
err = sp_sqr(t[3], t[3]);
1355013538
}
1355113539
if (err == MP_OKAY) {

0 commit comments

Comments
 (0)