wolfssl/wolfcrypt/types.h: add WC_NO_STATIC_ASSERT path, and add C89-compatible live fallback definition for wc_static_assert().

wolfssl/internal.h: refactor WOLFSSL_ASSERT_EQ() and WOLFSSL_ASSERT_SIZEOF_GE() to use wc_static_assert(), and drop unused WOLFSSL_ASSERT_TEST() and WOLFSSL_ASSERT_SIZEOF_TEST().

src/ssl_crypto.c and wolfcrypt/src/evp.c: refactor ad hoc asserts in wolfSSL_DES_ecb_encrypt(), wolfSSL_CRYPTO_cts128_decrypt(), and wolfSSL_EVP_DigestInit(), to use wc_static_assert().
This commit is contained in:
Daniel Pouzzner 2024-10-04 21:11:25 -05:00
parent a25c0244a7
commit e944967731
4 changed files with 19 additions and 27 deletions

View File

@ -2923,8 +2923,7 @@ void wolfSSL_DES_ecb_encrypt(WOLFSSL_DES_cblock* in, WOLFSSL_DES_cblock* out,
static int wolfssl_aes_set_key(const unsigned char *key, const int bits,
AES_KEY *aes, int enc)
{
typedef char aes_test[sizeof(AES_KEY) >= sizeof(Aes) ? 1 : -1];
(void)sizeof(aes_test);
wc_static_assert(sizeof(AES_KEY) >= sizeof(Aes));
/* Validate parameters. */
if ((key == NULL) || (aes == NULL)) {
@ -3438,8 +3437,7 @@ size_t wolfSSL_CRYPTO_cts128_decrypt(const unsigned char *in,
void wolfSSL_RC4_set_key(WOLFSSL_RC4_KEY* key, int len,
const unsigned char* data)
{
typedef char rc4_test[sizeof(WOLFSSL_RC4_KEY) >= sizeof(Arc4) ? 1 : -1];
(void)sizeof(rc4_test);
wc_static_assert(sizeof(WOLFSSL_RC4_KEY) >= sizeof(Arc4));
WOLFSSL_ENTER("wolfSSL_RC4_set_key");

View File

@ -10495,6 +10495,9 @@ int wolfSSL_EVP_MD_type(const WOLFSSL_EVP_MD* type)
const WOLFSSL_EVP_MD* md)
{
int ret = WOLFSSL_SUCCESS;
#ifdef WOLFSSL_ASYNC_CRYPT
wc_static_assert(WC_ASYNC_DEV_SIZE >= sizeof(WC_ASYNC_DEV));
#endif
WOLFSSL_ENTER("EVP_DigestInit");
@ -10502,14 +10505,6 @@ int wolfSSL_EVP_MD_type(const WOLFSSL_EVP_MD* type)
return WOLFSSL_FAILURE;
}
#ifdef WOLFSSL_ASYNC_CRYPT
/* compile-time validation of ASYNC_CTX_SIZE */
typedef char async_test[WC_ASYNC_DEV_SIZE >= sizeof(WC_ASYNC_DEV) ?
1 : -1];
(void)sizeof(async_test);
#endif
/* Set to 0 if no match */
ctx->macType = EvpMd2MacType(md);
if (md == NULL) {

View File

@ -2071,18 +2071,9 @@ enum Misc {
#define MAX_ENCRYPT_SZ ENCRYPT_LEN
/* A static check to assert a relation between x and y */
#define WOLFSSL_ASSERT_TEST(x, y, op) do { \
typedef char _args_test_[(x) op (y) ? 1 : -1]; \
(void)sizeof(_args_test_); \
} while(0)
#define WOLFSSL_ASSERT_EQ(x, y) wc_static_assert((x) == (y))
#define WOLFSSL_ASSERT_EQ(x, y) WOLFSSL_ASSERT_TEST(x, y, ==)
#define WOLFSSL_ASSERT_SIZEOF_TEST(x, y, op) \
WOLFSSL_ASSERT_TEST(sizeof(x), sizeof(y), op)
#define WOLFSSL_ASSERT_SIZEOF_GE(x, y) WOLFSSL_ASSERT_SIZEOF_TEST(x, y, >=)
#define WOLFSSL_ASSERT_SIZEOF_GE(x, y) wc_static_assert(sizeof(x) >= sizeof(y))
/* states. Adding state before HANDSHAKE_DONE will break session importing */
enum states {

View File

@ -1693,11 +1693,16 @@ typedef struct w64wrapper {
#define PRAGMA_DIAG_POP /* null expansion */
#endif
#ifndef wc_static_assert
#define WC_CPP_CAT_(a, b) a ## b
#define WC_CPP_CAT(a, b) WC_CPP_CAT_(a, b)
#if defined(WC_NO_STATIC_ASSERT)
#define wc_static_assert(expr) struct wc_static_assert_dummy_struct
#define wc_static_assert2(expr, msg) wc_static_assert(expr)
#elif !defined(wc_static_assert)
#if (defined(__cplusplus) && (__cplusplus >= 201703L)) || \
(defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 202311L)) || \
(defined(_MSVC_LANG) && (_MSVC_LANG >= 201103L))
/* directly usable variadic declaration */
/* native variadic static_assert() */
#define wc_static_assert static_assert
#ifndef wc_static_assert2
#define wc_static_assert2 static_assert
@ -1722,8 +1727,11 @@ typedef struct w64wrapper {
#define wc_static_assert2(expr, msg) _Static_assert(expr, msg)
#endif
#else
/* fallback -- map wc_static_assert*() to do-nothing. */
#define wc_static_assert(expr) struct wc_static_assert_dummy_struct
/* C89-compatible fallback */
#define wc_static_assert(expr) \
struct WC_CPP_CAT(wc_static_assert_dummy_struct_L, __LINE__) { \
char t[(expr) ? 1 : -1]; \
}
#ifndef wc_static_assert2
#define wc_static_assert2(expr, msg) wc_static_assert(expr)
#endif