Skip to content

Commit d6931b9

Browse files
authored
Merge pull request #10272 from The-Capable-Hub/wbeasley/meta-cheri-fixes
Fix support on CHERI RISC-V architecture
2 parents 12070eb + ba01226 commit d6931b9

8 files changed

Lines changed: 106 additions & 9 deletions

File tree

wolfcrypt/src/aes.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2158,14 +2158,14 @@ static void XorTable_Multi(const word32* t, word32* t0, byte o0,
21582158
word32 e1 = 0;
21592159
word32 e2 = 0;
21602160
word32 e3 = 0;
2161-
byte hi0 = o0 & 0xf0;
2162-
byte lo0 = o0 & 0x0f;
2163-
byte hi1 = o1 & 0xf0;
2164-
byte lo1 = o1 & 0x0f;
2165-
byte hi2 = o2 & 0xf0;
2166-
byte lo2 = o2 & 0x0f;
2167-
byte hi3 = o3 & 0xf0;
2168-
byte lo3 = o3 & 0x0f;
2161+
byte hi0 = o0 & WC_CACHE_LINE_MASK_HI;
2162+
byte lo0 = o0 & WC_CACHE_LINE_MASK_LO;
2163+
byte hi1 = o1 & WC_CACHE_LINE_MASK_HI;
2164+
byte lo1 = o1 & WC_CACHE_LINE_MASK_LO;
2165+
byte hi2 = o2 & WC_CACHE_LINE_MASK_HI;
2166+
byte lo2 = o2 & WC_CACHE_LINE_MASK_LO;
2167+
byte hi3 = o3 & WC_CACHE_LINE_MASK_HI;
2168+
byte lo3 = o3 & WC_CACHE_LINE_MASK_LO;
21692169
int i;
21702170

21712171
for (i = 0; i < 256; i += (1 << WC_CACHE_LINE_BITS)) {

wolfcrypt/src/sakke.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2649,12 +2649,19 @@ static int sakke_modexp_loop(SakkeKey* key, mp_int* b, mp_int* e, mp_proj* r,
26492649
err = sakke_proj_mul_qx1(c[0], by, prime, mp, c[j^1], t1, t2);
26502650
#else
26512651
err = sakke_proj_mul_qx1(c[0], by, prime, mp, c[2], t1, t2);
2652+
#ifdef WC_NO_PTR_INT_CAST
2653+
err = mp_cond_copy(c[2]->x, j, c[0]->x);
2654+
err = mp_cond_copy(c[2]->x, j^1, c[1]->x);
2655+
err = mp_cond_copy(c[2]->y, j, c[0]->y);
2656+
err = mp_cond_copy(c[2]->y, j^1, c[1]->y);
2657+
#else
26522658
mp_copy(c[2]->x,
26532659
(mp_int*) ( ((wc_ptr_t)c[0]->x & wc_off_on_addr[j]) +
26542660
((wc_ptr_t)c[1]->x & wc_off_on_addr[j^1]) ) );
26552661
mp_copy(c[2]->y,
26562662
(mp_int*) ( ((wc_ptr_t)c[0]->y & wc_off_on_addr[j]) +
26572663
((wc_ptr_t)c[1]->y & wc_off_on_addr[j^1]) ) );
2664+
#endif
26582665
#endif
26592666
}
26602667
}

wolfcrypt/src/sp_int.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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) {

wolfssl/internal.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5831,15 +5831,19 @@ typedef struct BuildMsgArgs {
58315831
#endif
58325832

58335833
#ifdef WOLFSSL_ASYNC_IO
5834-
#define MAX_ASYNC_ARGS 18
5834+
#define MAX_ASYNC_ARGS 24
58355835
typedef void (*FreeArgsCb)(struct WOLFSSL* ssl, void* pArgs);
58365836

58375837
struct WOLFSSL_ASYNC {
58385838
#if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WOLFSSL_NO_TLS12)
58395839
BuildMsgArgs buildArgs; /* holder for current BuildMessage args */
58405840
#endif
58415841
FreeArgsCb freeArgs; /* function pointer to cleanup args */
5842+
#ifdef WC_NO_PTR_INT_CAST
5843+
max_align_t args[MAX_ASYNC_ARGS * sizeof(word32) / sizeof(max_align_t)]; /* holder for current args */
5844+
#else
58425845
word32 args[MAX_ASYNC_ARGS]; /* holder for current args */
5846+
#endif
58435847
};
58445848
#endif
58455849

wolfssl/openssl/md5.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,17 @@
4343
typedef struct WOLFSSL_MD5_CTX {
4444
/* big enough to hold wolfcrypt md5, but check on init */
4545
#ifdef STM32_HASH
46+
# ifdef WC_NO_PTR_INT_CAST
47+
void* holder[(128 + WC_ASYNC_DEV_SIZE + sizeof(STM32_HASH_Context)) / sizeof(void*)];
48+
# else
4649
void* holder[(112 + WC_ASYNC_DEV_SIZE + sizeof(STM32_HASH_Context)) / sizeof(void*)];
50+
# endif
4751
#else
52+
# ifdef WC_NO_PTR_INT_CAST
53+
void* holder[(128 + WC_ASYNC_DEV_SIZE) / sizeof(void*)];
54+
# else
4855
void* holder[(112 + WC_ASYNC_DEV_SIZE) / sizeof(void*)];
56+
# endif
4957
#endif
5058
} WOLFSSL_MD5_CTX;
5159

wolfssl/openssl/rc4.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@
3939
* the size of RC4_KEY structures. */
4040
typedef struct WOLFSSL_RC4_KEY {
4141
/* big enough for Arc4 from wolfssl/wolfcrypt/arc4.h */
42+
#ifdef WC_NO_PTR_INT_CAST
43+
void* holder[(288 + WC_ASYNC_DEV_SIZE) / sizeof(void*)];
44+
#else
4245
void* holder[(272 + WC_ASYNC_DEV_SIZE) / sizeof(void*)];
46+
#endif
4347
} WOLFSSL_RC4_KEY;
4448

4549
WOLFSSL_API void wolfSSL_RC4_set_key(WOLFSSL_RC4_KEY* key, int len,

wolfssl/openssl/sha.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@
5252
#ifndef NO_SHA
5353
typedef struct WOLFSSL_SHA_CTX {
5454
/* big enough to hold wolfcrypt Sha, but check on init */
55+
#ifdef WC_NO_PTR_INT_CAST
56+
void* holder[(160 + WC_ASYNC_DEV_SIZE + CTX_SHA_HW_ADDER) / sizeof(void*)];
57+
#else
5558
void* holder[(112 + WC_ASYNC_DEV_SIZE + CTX_SHA_HW_ADDER) / sizeof(void*)];
59+
#endif
5660
#if defined(WOLFSSL_DEVCRYPTO_HASH) || defined(WOLFSSL_HASH_KEEP)
5761
void* keephash_holder[sizeof(void*) + (2 * sizeof(unsigned int))];
5862
#endif

wolfssl/wolfcrypt/settings.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5178,6 +5178,10 @@ extern void uITRON4_free(void *p) ;
51785178
#endif
51795179
#endif
51805180

5181+
#ifdef __CHERI_PURE_CAPABILITY__
5182+
#define WC_NO_PTR_INT_CAST
5183+
#endif
5184+
51815185
#ifdef __cplusplus
51825186
} /* extern "C" */
51835187
#endif

0 commit comments

Comments
 (0)