From c967dd2a30a81722fbb8a8994413fbd8d5313974 Mon Sep 17 00:00:00 2001 From: Daniel Pouzzner Date: Sat, 10 May 2025 01:28:17 -0500 Subject: [PATCH 1/3] wolfcrypt/src/sp_int.c and wolfssl/wolfcrypt/sp_int.h: add MP_INT_SIZEOF_DIGITS() macro, and use it for stack allocations in DECL_SP_INT() and DECL_SP_INT_ARRAY(); refactor _sp_submod() to use DECL_SP_INT() rather than DECL_SP_INT_ARRAY() to work around apparent optimizer bug in gcc-15. --- wolfcrypt/src/sp_int.c | 29 ++++++++++++++++------------- wolfssl/wolfcrypt/sp_int.h | 3 +++ 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/wolfcrypt/src/sp_int.c b/wolfcrypt/src/sp_int.c index 1769840e7..e3a003cf1 100644 --- a/wolfcrypt/src/sp_int.c +++ b/wolfcrypt/src/sp_int.c @@ -130,8 +130,8 @@ PRAGMA_GCC("GCC diagnostic ignored \"-Warray-bounds\"") #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \ !defined(WOLFSSL_SP_NO_DYN_STACK) /* Declare a variable on the stack with the required data size. */ - #define DECL_SP_INT(n, s) \ - byte n##d[MP_INT_SIZEOF(s)]; \ + #define DECL_SP_INT(n, s) \ + sp_int_digit n##d[MP_INT_SIZEOF_DIGITS(s)]; \ sp_int* (n) = (sp_int*)n##d #else /* Declare a variable on the stack. */ @@ -221,8 +221,8 @@ PRAGMA_GCC("GCC diagnostic ignored \"-Warray-bounds\"") #elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \ !defined(WOLFSSL_SP_NO_DYN_STACK) /* Declare a variable on the stack with the required data size. */ - #define DECL_SP_INT_ARRAY(n, s, c) \ - byte n##d[MP_INT_SIZEOF(s) * (c)]; \ + #define DECL_SP_INT_ARRAY(n, s, c) \ + sp_int_digit n##d[MP_INT_SIZEOF_DIGITS(s) * (c)]; \ sp_int* (n)[c] = { NULL, } #else /* Declare a variable on the stack. */ @@ -7909,28 +7909,30 @@ static int _sp_submod(const sp_int* a, const sp_int* b, const sp_int* m, unsigned int used = ((a->used >= m->used) ? ((a->used >= b->used) ? (a->used + 1U) : (b->used + 1U)) : ((b->used >= m->used)) ? (b->used + 1U) : (m->used + 1U)); - DECL_SP_INT_ARRAY(t, used, 2); + DECL_SP_INT(t0, used); + DECL_SP_INT(t1, used); - ALLOC_SP_INT_ARRAY(t, used, 2, err, NULL); + ALLOC_SP_INT_SIZE(t0, used, err, NULL); + ALLOC_SP_INT_SIZE(t1, used, err, NULL); if (err == MP_OKAY) { /* Reduce a to less than m. */ if (_sp_cmp(a, m) != MP_LT) { - err = sp_mod(a, m, t[0]); - a = t[0]; + err = sp_mod(a, m, t0); + a = t0; } } if (err == MP_OKAY) { /* Reduce b to less than m. */ if (_sp_cmp(b, m) != MP_LT) { - err = sp_mod(b, m, t[1]); - b = t[1]; + err = sp_mod(b, m, t1); + b = t1; } } if (err == MP_OKAY) { /* Add m to a if a smaller than b. */ if (_sp_cmp(a, b) == MP_LT) { - err = sp_add(a, m, t[0]); - a = t[0]; + err = sp_add(a, m, t0); + a = t0; } } if (err == MP_OKAY) { @@ -7938,7 +7940,8 @@ static int _sp_submod(const sp_int* a, const sp_int* b, const sp_int* m, err = sp_sub(a, b, r); } - FREE_SP_INT_ARRAY(t, NULL); + FREE_SP_INT(t0, NULL); + FREE_SP_INT(t1, NULL); #else /* WOLFSSL_SP_INT_NEGATIVE */ sp_size_t used = ((a->used >= b->used) ? a->used + 1 : b->used + 1); DECL_SP_INT(t, used); diff --git a/wolfssl/wolfcrypt/sp_int.h b/wolfssl/wolfcrypt/sp_int.h index 7385e6800..658eb82e0 100644 --- a/wolfssl/wolfcrypt/sp_int.h +++ b/wolfssl/wolfcrypt/sp_int.h @@ -930,6 +930,9 @@ typedef struct sp_int_minimal { sp_int_digit dp[1]; } sp_int_minimal; +static_assert(sizeof(struct sp_int_minimal) % sizeof(sp_int_digit) == 0); +#define MP_INT_SIZEOF_DIGITS(cnt) (MP_INT_SIZEOF(cnt) / sizeof(sp_int_digit)) + /* Multi-precision integer type is SP integer type. */ typedef sp_int mp_int; /* Multi-precision integer digit type is SP integer digit type. From 8410d922db4e67751c4b0e94a20b52a045ded0ea Mon Sep 17 00:00:00 2001 From: Daniel Pouzzner Date: Sat, 10 May 2025 01:31:45 -0500 Subject: [PATCH 2/3] .wolfssl_known_macro_extras: remove WOLFSSL_CURVE25519_BLINDING (defined in settings.h since aa840f9c94). --- .wolfssl_known_macro_extras | 1 - 1 file changed, 1 deletion(-) diff --git a/.wolfssl_known_macro_extras b/.wolfssl_known_macro_extras index 9ca281786..5099e72cd 100644 --- a/.wolfssl_known_macro_extras +++ b/.wolfssl_known_macro_extras @@ -641,7 +641,6 @@ WOLFSSL_CLIENT_EXAMPLE WOLFSSL_COMMERCIAL_LICENSE WOLFSSL_CONTIKI WOLFSSL_CRL_ALLOW_MISSING_CDP -WOLFSSL_CURVE25519_BLINDING WOLFSSL_CUSTOM_CONFIG WOLFSSL_DILITHIUM_ASSIGN_KEY WOLFSSL_DILITHIUM_MAKE_KEY_SMALL_MEM From 5a911f6af083196604c7136076dd30c0e8b843f6 Mon Sep 17 00:00:00 2001 From: Daniel Pouzzner Date: Sat, 10 May 2025 02:16:48 -0500 Subject: [PATCH 3/3] wolfssl/wolfcrypt/sp_int.h: wc_static_assert(), not static_assert(). --- wolfssl/wolfcrypt/sp_int.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wolfssl/wolfcrypt/sp_int.h b/wolfssl/wolfcrypt/sp_int.h index 658eb82e0..e9eacf42e 100644 --- a/wolfssl/wolfcrypt/sp_int.h +++ b/wolfssl/wolfcrypt/sp_int.h @@ -930,7 +930,7 @@ typedef struct sp_int_minimal { sp_int_digit dp[1]; } sp_int_minimal; -static_assert(sizeof(struct sp_int_minimal) % sizeof(sp_int_digit) == 0); +wc_static_assert(sizeof(struct sp_int_minimal) % sizeof(sp_int_digit) == 0); #define MP_INT_SIZEOF_DIGITS(cnt) (MP_INT_SIZEOF(cnt) / sizeof(sp_int_digit)) /* Multi-precision integer type is SP integer type. */