Skip to content

Commit e944967

Browse files
committed
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().
1 parent a25c024 commit e944967

4 files changed

Lines changed: 19 additions & 27 deletions

File tree

src/ssl_crypto.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2923,8 +2923,7 @@ void wolfSSL_DES_ecb_encrypt(WOLFSSL_DES_cblock* in, WOLFSSL_DES_cblock* out,
29232923
static int wolfssl_aes_set_key(const unsigned char *key, const int bits,
29242924
AES_KEY *aes, int enc)
29252925
{
2926-
typedef char aes_test[sizeof(AES_KEY) >= sizeof(Aes) ? 1 : -1];
2927-
(void)sizeof(aes_test);
2926+
wc_static_assert(sizeof(AES_KEY) >= sizeof(Aes));
29282927

29292928
/* Validate parameters. */
29302929
if ((key == NULL) || (aes == NULL)) {
@@ -3438,8 +3437,7 @@ size_t wolfSSL_CRYPTO_cts128_decrypt(const unsigned char *in,
34383437
void wolfSSL_RC4_set_key(WOLFSSL_RC4_KEY* key, int len,
34393438
const unsigned char* data)
34403439
{
3441-
typedef char rc4_test[sizeof(WOLFSSL_RC4_KEY) >= sizeof(Arc4) ? 1 : -1];
3442-
(void)sizeof(rc4_test);
3440+
wc_static_assert(sizeof(WOLFSSL_RC4_KEY) >= sizeof(Arc4));
34433441

34443442
WOLFSSL_ENTER("wolfSSL_RC4_set_key");
34453443

wolfcrypt/src/evp.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10495,21 +10495,16 @@ int wolfSSL_EVP_MD_type(const WOLFSSL_EVP_MD* type)
1049510495
const WOLFSSL_EVP_MD* md)
1049610496
{
1049710497
int ret = WOLFSSL_SUCCESS;
10498+
#ifdef WOLFSSL_ASYNC_CRYPT
10499+
wc_static_assert(WC_ASYNC_DEV_SIZE >= sizeof(WC_ASYNC_DEV));
10500+
#endif
1049810501

1049910502
WOLFSSL_ENTER("EVP_DigestInit");
1050010503

1050110504
if (ctx == NULL) {
1050210505
return WOLFSSL_FAILURE;
1050310506
}
1050410507

10505-
10506-
#ifdef WOLFSSL_ASYNC_CRYPT
10507-
/* compile-time validation of ASYNC_CTX_SIZE */
10508-
typedef char async_test[WC_ASYNC_DEV_SIZE >= sizeof(WC_ASYNC_DEV) ?
10509-
1 : -1];
10510-
(void)sizeof(async_test);
10511-
#endif
10512-
1051310508
/* Set to 0 if no match */
1051410509
ctx->macType = EvpMd2MacType(md);
1051510510
if (md == NULL) {

wolfssl/internal.h

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2071,18 +2071,9 @@ enum Misc {
20712071

20722072
#define MAX_ENCRYPT_SZ ENCRYPT_LEN
20732073

2074-
/* A static check to assert a relation between x and y */
2075-
#define WOLFSSL_ASSERT_TEST(x, y, op) do { \
2076-
typedef char _args_test_[(x) op (y) ? 1 : -1]; \
2077-
(void)sizeof(_args_test_); \
2078-
} while(0)
2074+
#define WOLFSSL_ASSERT_EQ(x, y) wc_static_assert((x) == (y))
20792075

2080-
#define WOLFSSL_ASSERT_EQ(x, y) WOLFSSL_ASSERT_TEST(x, y, ==)
2081-
2082-
#define WOLFSSL_ASSERT_SIZEOF_TEST(x, y, op) \
2083-
WOLFSSL_ASSERT_TEST(sizeof(x), sizeof(y), op)
2084-
2085-
#define WOLFSSL_ASSERT_SIZEOF_GE(x, y) WOLFSSL_ASSERT_SIZEOF_TEST(x, y, >=)
2076+
#define WOLFSSL_ASSERT_SIZEOF_GE(x, y) wc_static_assert(sizeof(x) >= sizeof(y))
20862077

20872078
/* states. Adding state before HANDSHAKE_DONE will break session importing */
20882079
enum states {

wolfssl/wolfcrypt/types.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1693,11 +1693,16 @@ typedef struct w64wrapper {
16931693
#define PRAGMA_DIAG_POP /* null expansion */
16941694
#endif
16951695

1696-
#ifndef wc_static_assert
1696+
#define WC_CPP_CAT_(a, b) a ## b
1697+
#define WC_CPP_CAT(a, b) WC_CPP_CAT_(a, b)
1698+
#if defined(WC_NO_STATIC_ASSERT)
1699+
#define wc_static_assert(expr) struct wc_static_assert_dummy_struct
1700+
#define wc_static_assert2(expr, msg) wc_static_assert(expr)
1701+
#elif !defined(wc_static_assert)
16971702
#if (defined(__cplusplus) && (__cplusplus >= 201703L)) || \
16981703
(defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 202311L)) || \
16991704
(defined(_MSVC_LANG) && (_MSVC_LANG >= 201103L))
1700-
/* directly usable variadic declaration */
1705+
/* native variadic static_assert() */
17011706
#define wc_static_assert static_assert
17021707
#ifndef wc_static_assert2
17031708
#define wc_static_assert2 static_assert
@@ -1722,8 +1727,11 @@ typedef struct w64wrapper {
17221727
#define wc_static_assert2(expr, msg) _Static_assert(expr, msg)
17231728
#endif
17241729
#else
1725-
/* fallback -- map wc_static_assert*() to do-nothing. */
1726-
#define wc_static_assert(expr) struct wc_static_assert_dummy_struct
1730+
/* C89-compatible fallback */
1731+
#define wc_static_assert(expr) \
1732+
struct WC_CPP_CAT(wc_static_assert_dummy_struct_L, __LINE__) { \
1733+
char t[(expr) ? 1 : -1]; \
1734+
}
17271735
#ifndef wc_static_assert2
17281736
#define wc_static_assert2(expr, msg) wc_static_assert(expr)
17291737
#endif

0 commit comments

Comments
 (0)