Skip to content

Commit 35f6910

Browse files
authored
Merge pull request #9649 from douzzer/20260112-fixes
20260112-fixes
2 parents 6f48e06 + 1d247b7 commit 35f6910

8 files changed

Lines changed: 80 additions & 39 deletions

File tree

configure.ac

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10921,6 +10921,11 @@ fi
1092110921
LIB_SOCKET_NSL
1092210922
AX_HARDEN_CC_COMPILER_FLAGS
1092310923
10924+
# -Wdeprecated-enum-enum-conversion is on by default in C++20, but conflicts with
10925+
# our use of enum constructs to define fungible constants.
10926+
AX_CHECK_COMPILE_FLAG([-Werror -Wno-deprecated-enum-enum-conversion],
10927+
[AX_APPEND_FLAG([-Wno-deprecated-enum-enum-conversion], [AM_CFLAGS])])
10928+
1092410929
case $host_os in
1092510930
mingw*)
1092610931
# if mingw then link to ws2_32 for sockets, and crypt32

src/ssl.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,7 +1119,12 @@ static int wolfSSL_parse_cipher_list(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
11191119
#endif
11201120

11211121
/* prevent multiple mutex initializations */
1122+
1123+
/* note, initRefCount is not used for thread synchronization, only for
1124+
* bookkeeping while inits_count_mutex is held.
1125+
*/
11221126
static volatile WC_THREADSHARED int initRefCount = 0;
1127+
11231128
/* init ref count mutex */
11241129
static WC_THREADSHARED wolfSSL_Mutex inits_count_mutex
11251130
WOLFSSL_MUTEX_INITIALIZER_CLAUSE(inits_count_mutex);
@@ -6527,7 +6532,7 @@ int wolfSSL_Init(void)
65276532
#endif /* WOLFSSL_SYS_CRYPTO_POLICY */
65286533

65296534
if (ret == WOLFSSL_SUCCESS) {
6530-
initRefCount++;
6535+
initRefCount = initRefCount + 1;
65316536
}
65326537
else {
65336538
initRefCount = 1; /* Force cleanup */
@@ -11401,7 +11406,7 @@ int wolfSSL_Cleanup(void)
1140111406
#endif
1140211407

1140311408
if (initRefCount > 0) {
11404-
--initRefCount;
11409+
initRefCount = initRefCount - 1;
1140511410
if (initRefCount == 0)
1140611411
release = 1;
1140711412
}

wolfcrypt/src/asn.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27660,8 +27660,8 @@ static int KeyPemToDerPassCb(char* passwd, int sz, int rw, void* userdata)
2766027660
if (userdata == NULL)
2766127661
return 0;
2766227662

27663-
XSTRNCPY(passwd, (char*)userdata, (size_t)sz);
27664-
return (int)min((word32)sz, (word32)XSTRLEN((char*)userdata));
27663+
XSTRLCPY(passwd, (char*)userdata, (size_t)sz);
27664+
return (int)min((word32)(sz - 1), (word32)XSTRLEN((char*)userdata));
2766527665
}
2766627666
#endif
2766727667

wolfcrypt/src/sha512.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,7 @@ static int InitSha512_Family(wc_Sha512* sha512, void* heap, int devId,
867867
return BAD_FUNC_ARG;
868868
}
869869

870+
XMEMSET(sha512, 0, sizeof(*sha512));
870871

871872
sha512->heap = heap;
872873
#ifdef WOLFSSL_SMALL_STACK_CACHE
@@ -884,26 +885,33 @@ static int InitSha512_Family(wc_Sha512* sha512, void* heap, int devId,
884885
sha512->devCtx = NULL;
885886
#endif
886887

887-
/* call the initialization function pointed to by initfp */
888-
ret = initfp(sha512);
889-
if (ret != 0)
890-
return ret;
891-
892888
#ifdef WOLFSSL_HASH_KEEP
893889
sha512->msg = NULL;
894-
sha512->len = 0;
895-
sha512->used = 0;
896890
#endif
897891

892+
/* call the initialization function pointed to by initfp */
893+
ret = initfp(sha512);
894+
898895
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA512)
899-
ret = wolfAsync_DevCtxInit(&sha512->asyncDev,
896+
if (ret == 0) {
897+
ret = wolfAsync_DevCtxInit(&sha512->asyncDev,
900898
WOLFSSL_ASYNC_MARKER_SHA512, sha512->heap, devId);
899+
}
901900
#else
902901
(void)devId;
903902
#endif /* WOLFSSL_ASYNC_CRYPT */
904903
#ifdef WOLFSSL_IMXRT1170_CAAM
905-
ret = wc_CAAM_HashInit(&sha512->hndl, &sha512->ctx, WC_HASH_TYPE_SHA512);
904+
if (ret == 0)
905+
ret = wc_CAAM_HashInit(&sha512->hndl, &sha512->ctx, WC_HASH_TYPE_SHA512);
906+
#endif
907+
908+
#ifdef WOLFSSL_SMALL_STACK_CACHE
909+
if (ret != 0) {
910+
XFREE(sha512->W, sha512->heap, DYNAMIC_TYPE_DIGEST);
911+
sha512->W = NULL;
912+
}
906913
#endif
914+
907915
return ret;
908916
} /* InitSha512_Family */
909917

wolfcrypt/src/wc_port.c

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,11 @@
151151
#endif
152152

153153
/* prevent multiple mutex initializations */
154-
static volatile int initRefCount = 0;
154+
#ifdef WOLFSSL_ATOMIC_OPS
155+
wolfSSL_Atomic_Int initRefCount = WOLFSSL_ATOMIC_INITIALIZER(0);
156+
#else
157+
static int initRefCount = 0;
158+
#endif
155159

156160
#if defined(__aarch64__) && defined(WOLFSSL_ARMASM_BARRIER_DETECT)
157161
int aarch64_use_sb = 0;
@@ -164,7 +168,8 @@ WOLFSSL_ABI
164168
int wolfCrypt_Init(void)
165169
{
166170
int ret = 0;
167-
if (initRefCount == 0) {
171+
int my_initRefCount = wolfSSL_Atomic_Int_FetchAdd(&initRefCount, 1);
172+
if (my_initRefCount == 0) {
168173
WOLFSSL_ENTER("wolfCrypt_Init");
169174

170175
#if defined(__aarch64__) && defined(WOLFSSL_ARMASM_BARRIER_DETECT)
@@ -444,8 +449,16 @@ int wolfCrypt_Init(void)
444449
return ret;
445450
}
446451
#endif
452+
453+
/* increment to 2, to signify successful initialization: */
454+
(void)wolfSSL_Atomic_Int_FetchAdd(&initRefCount, 1);
455+
}
456+
else {
457+
if (my_initRefCount < 2) {
458+
(void)wolfSSL_Atomic_Int_FetchSub(&initRefCount, 1);
459+
ret = BUSY_E;
460+
}
447461
}
448-
initRefCount++;
449462

450463
return ret;
451464
}
@@ -469,12 +482,9 @@ WOLFSSL_ABI
469482
int wolfCrypt_Cleanup(void)
470483
{
471484
int ret = 0;
485+
int my_initRefCount = wolfSSL_Atomic_Int_SubFetch(&initRefCount, 1);
472486

473-
initRefCount--;
474-
if (initRefCount < 0)
475-
initRefCount = 0;
476-
477-
if (initRefCount == 0) {
487+
if (my_initRefCount == 1) {
478488
WOLFSSL_ENTER("wolfCrypt_Cleanup");
479489

480490
#ifdef HAVE_ECC
@@ -564,11 +574,18 @@ int wolfCrypt_Cleanup(void)
564574
* must be freed. */
565575
wc_MemZero_Free();
566576
#endif
567-
}
577+
578+
(void)wolfSSL_Atomic_Int_SubFetch(&initRefCount, 1);
568579

569580
#if defined(HAVE_LIBOQS)
570-
wolfSSL_liboqsClose();
581+
wolfSSL_liboqsClose();
571582
#endif
583+
}
584+
else if (my_initRefCount < 0) {
585+
(void)wolfSSL_Atomic_Int_AddFetch(&initRefCount, 1);
586+
WOLFSSL_MSG("wolfCrypt_Cleanup() called with initRefCount <= 0.");
587+
ret = ALREADY_E;
588+
}
572589

573590
return ret;
574591
}
@@ -1462,9 +1479,17 @@ int wolfSSL_Atomic_Ptr_CompareExchange(
14621479
* atomic_compare_exchange_strong_explicit(), to sidestep _Atomic type
14631480
* requirements.
14641481
*/
1465-
return __atomic_compare_exchange_n(
1466-
c, expected_ptr, new_ptr, 0 /* weak */,
1467-
__ATOMIC_SEQ_CST, __ATOMIC_ACQUIRE);
1482+
if (__atomic_compare_exchange_n(
1483+
c, expected_ptr, new_ptr,
1484+
#ifdef WOLF_C89
1485+
0 /* weak */,
1486+
#else
1487+
(_Bool)0 /* weak */,
1488+
#endif
1489+
__ATOMIC_SEQ_CST, __ATOMIC_ACQUIRE))
1490+
return 1;
1491+
else
1492+
return 0;
14681493
}
14691494

14701495
#elif defined(__GNUC__) && defined(__ATOMIC_RELAXED)

wolfcrypt/test/test.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20195,7 +20195,9 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t random_bank_test(void)
2019520195
WC_ALLOC_VAR_EX(rng, WC_RNG, 1, HEAP_HINT,
2019620196
DYNAMIC_TYPE_TMP_BUFFER,
2019720197
return WC_TEST_RET_ENC_EC(MEMORY_E));
20198+
#ifdef WC_DRBG_BANKREF
2019820199
XMEMSET(rng, 0, sizeof(*rng));
20200+
#endif
2019920201

2020020202
ret = wc_rng_bank_init(NULL, WC_RNG_BANK_STATIC_SIZE, WC_RNG_BANK_FLAG_CAN_WAIT, 10, HEAP_HINT, INVALID_DEVID);
2020120203
if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG))
@@ -20207,7 +20209,10 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t random_bank_test(void)
2020720209
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
2020820210
#endif
2020920211

20210-
ret = wc_rng_bank_init(bank, WC_RNG_BANK_STATIC_SIZE, WC_RNG_BANK_FLAG_CAN_WAIT, 10, HEAP_HINT, INVALID_DEVID);
20212+
ret = wc_rng_bank_init(bank, WC_RNG_BANK_STATIC_SIZE,
20213+
WC_RNG_BANK_FLAG_NO_VECTOR_OPS |
20214+
WC_RNG_BANK_FLAG_CAN_WAIT,
20215+
10, HEAP_HINT, INVALID_DEVID);
2021120216
if (ret != 0)
2021220217
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
2021320218

@@ -20417,7 +20422,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t random_bank_test(void)
2041720422

2041820423
#else /* !WC_RNG_BANK_STATIC */
2041920424

20420-
ret = wc_rng_bank_new(&bank2, WC_RNG_BANK_STATIC_SIZE + 1, WC_RNG_BANK_FLAG_NO_VECTOR_OPS, 10, HEAP_HINT, INVALID_DEVID);
20425+
ret = wc_rng_bank_new(&bank2, WC_RNG_BANK_STATIC_SIZE + 1, WC_RNG_BANK_FLAG_NONE, 10, HEAP_HINT, INVALID_DEVID);
2042120426
if (ret != 0)
2042220427
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
2042320428

wrapper/rust/wolfssl-wolfcrypt/src/lib.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ pub mod sha;
5050
/// ```rust
5151
/// use wolfssl_wolfcrypt::*;
5252
/// wolfcrypt_init().expect("Error with wolfcrypt_init()");
53+
/// // ... use the library ...
54+
/// wolfcrypt_cleanup().expect("wolfCrypt_Cleanup failed");
5355
/// ```
5456
pub fn wolfcrypt_init() -> Result<(), i32> {
5557
let rc = unsafe { sys::wolfCrypt_Init() };
@@ -66,12 +68,7 @@ pub fn wolfcrypt_init() -> Result<(), i32> {
6668
/// Returns either Ok(()) on success or Err(e) containing the wolfSSL
6769
/// library error code value.
6870
///
69-
/// # Example
70-
///
71-
/// ```rust
72-
/// use wolfssl_wolfcrypt::*;
73-
/// wolfcrypt_cleanup().expect("Error with wolfcrypt_cleanup()");
74-
/// ```
71+
/// See also: [`wolfcrypt_init`]
7572
pub fn wolfcrypt_cleanup() -> Result<(), i32> {
7673
let rc = unsafe { sys::wolfCrypt_Cleanup() };
7774
if rc != 0 {
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
use wolfssl_wolfcrypt::*;
22

33
#[test]
4-
fn test_wolfcrypt_init() {
4+
fn test_wolfcrypt_init_and_cleanup() {
55
wolfcrypt_init().expect("Error with wolfcrypt_init()");
6-
}
7-
8-
#[test]
9-
fn test_wolfcrypt_cleanup() {
106
wolfcrypt_cleanup().expect("Error with wolfcrypt_cleanup()");
117
}

0 commit comments

Comments
 (0)