Skip to content

Commit 2339d63

Browse files
committed
wolfssl/wolfcrypt/wc_port.h, wolfssl/wolfcrypt/types.h: move macros and prototypes for wc_init_state_t facility to types.h, to allow the width of the .count bitfield to pivot on UINT_MAX, for 16 bit (e.g. Arduino) compatibility.
1 parent 90b5011 commit 2339d63

3 files changed

Lines changed: 50 additions & 41 deletions

File tree

wolfcrypt/src/wc_port.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ int wc_local_InitUp(wc_init_state_t *s, int doWait)
252252

253253
/* mitigate races on init/shutdown by looping. */
254254
for (;;) {
255-
if (exp_wc_init_state.c.count == 0x1fffffff)
255+
if (exp_wc_init_state.c.count == WC_INIT_STATE_MAX_COUNT)
256256
return SEQ_OVERFLOW_E;
257257
new_wc_init_state = exp_wc_init_state;
258258
if (exp_wc_init_state.c.state == WC_INIT_STATE_UNINITED) {

wolfssl/wolfcrypt/types.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,55 @@ enum {
629629
((out) = (in1) - (in2), \
630630
/* coverity[INTEGER_OVERFLOW] */ 1))))
631631

632+
633+
/* Internal APIs for counting initialization depth, with initialization/cleanup
634+
* races fully mitigated
635+
*/
636+
#ifdef WOLFSSL_ATOMIC_OPS
637+
typedef wolfSSL_Atomic_Uint wc_init_state_t;
638+
#define WC_INIT_STATE_INITIALIZER WOLFSSL_ATOMIC_INITIALIZER(0)
639+
#else
640+
typedef unsigned int wc_init_state_t;
641+
#define WC_INIT_STATE_INITIALIZER 0
642+
#endif
643+
#define WC_DECLARE_INIT_STATE(x) wc_init_state_t x = WC_INIT_STATE_INITIALIZER
644+
#define WC_INIT_STATE_UNINITED 0U
645+
#define WC_INIT_STATE_INITING 1U
646+
#define WC_INIT_STATE_INITED 2U
647+
#define WC_INIT_STATE_CLEANING_UP 3U
648+
#define WC_INIT_STATE_BAD_STATE 4U
649+
union wc_init_state_bitfields {
650+
unsigned int u;
651+
struct {
652+
unsigned int state:3;
653+
#if UINT_MAX == 0xffff
654+
unsigned int count:13;
655+
#define WC_INIT_STATE_MAX_COUNT 0x1fff
656+
#else
657+
unsigned int count:29;
658+
#define WC_INIT_STATE_MAX_COUNT 0x1fffffff
659+
#endif
660+
} c;
661+
};
662+
/* Modules with no provisions for cleanup after a partially successful init need
663+
* to enter a degraded state, returning BAD_STATE_E to the caller, signaling
664+
* that restart is needed. This macro should only be called while
665+
* _STATE_INITING (after wc_local_InitUp() returns _STATE_INITING and before
666+
* wc_local_InitUpDone()), to assure the store is uncontended.
667+
*/
668+
#define WC_INIT_STATE_RAISE_BAD_STATE(x) do { \
669+
union wc_init_state_bitfields _x; \
670+
_x.u = WOLFSSL_ATOMIC_LOAD(x); \
671+
_x.c.state = WC_INIT_STATE_BAD_STATE; \
672+
WOLFSSL_ATOMIC_STORE(x, _x.u); \
673+
} while (0)
674+
WOLFSSL_LOCAL int wc_local_InitUp(wc_init_state_t *s, int doWait);
675+
WOLFSSL_LOCAL int wc_local_InitUpDone(wc_init_state_t *s);
676+
WOLFSSL_LOCAL int wc_local_InitDown(wc_init_state_t *s, int doWait);
677+
WOLFSSL_LOCAL int wc_local_InitDownDone(wc_init_state_t *s);
678+
679+
680+
/* Bindings for heap operations */
632681
#if defined(HAVE_IO_POOL)
633682
WOLFSSL_API void* XMALLOC(size_t n, void* heap, int type);
634683
WOLFSSL_API void* XREALLOC(void *p, size_t n, void* heap, int type);

wolfssl/wolfcrypt/wc_port.h

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -941,46 +941,6 @@ WOLFSSL_API int wc_SetMutexCb(mutex_cb* cb);
941941
WOLFSSL_API mutex_cb* wc_GetMutexCb(void);
942942
#endif
943943

944-
/* Internal APIs for counting initialization depth, with initialization/cleanup
945-
* races fully mitigated
946-
*/
947-
#ifdef WOLFSSL_ATOMIC_OPS
948-
typedef wolfSSL_Atomic_Uint wc_init_state_t;
949-
#define WC_INIT_STATE_INITIALIZER WOLFSSL_ATOMIC_INITIALIZER(0)
950-
#else
951-
typedef unsigned int wc_init_state_t;
952-
#define WC_INIT_STATE_INITIALIZER 0
953-
#endif
954-
#define WC_DECLARE_INIT_STATE(x) wc_init_state_t x = WC_INIT_STATE_INITIALIZER
955-
#define WC_INIT_STATE_UNINITED 0U
956-
#define WC_INIT_STATE_INITING 1U
957-
#define WC_INIT_STATE_INITED 2U
958-
#define WC_INIT_STATE_CLEANING_UP 3U
959-
#define WC_INIT_STATE_BAD_STATE 4U
960-
union wc_init_state_bitfields {
961-
unsigned int u;
962-
struct {
963-
unsigned int state:3;
964-
unsigned int count:29;
965-
} c;
966-
};
967-
/* Modules with no provisions for cleanup after a partially successful init need
968-
* to enter a degraded state, returning BAD_STATE_E to the caller, signaling
969-
* that restart is needed. This macro should only be called while
970-
* _STATE_INITING (after wc_local_InitUp() returns _STATE_INITING and before
971-
* wc_local_InitUpDone()), to assure the store is uncontended.
972-
*/
973-
#define WC_INIT_STATE_RAISE_BAD_STATE(x) do { \
974-
union wc_init_state_bitfields _x; \
975-
_x.u = WOLFSSL_ATOMIC_LOAD(x); \
976-
_x.c.state = WC_INIT_STATE_BAD_STATE; \
977-
WOLFSSL_ATOMIC_STORE(x, _x.u); \
978-
} while (0)
979-
WOLFSSL_LOCAL int wc_local_InitUp(wc_init_state_t *s, int doWait);
980-
WOLFSSL_LOCAL int wc_local_InitUpDone(wc_init_state_t *s);
981-
WOLFSSL_LOCAL int wc_local_InitDown(wc_init_state_t *s, int doWait);
982-
WOLFSSL_LOCAL int wc_local_InitDownDone(wc_init_state_t *s);
983-
984944
/* main crypto initialization function */
985945
WOLFSSL_ABI WOLFSSL_API int wolfCrypt_Init(void);
986946
WOLFSSL_ABI WOLFSSL_API int wolfCrypt_Cleanup(void);

0 commit comments

Comments
 (0)