Skip to content

Commit c430cc7

Browse files
committed
src/ssl.c and wolfssl/ssl.h: fix signature on wolfSSL_CTX_get0_privatekey() -- ctx is not const;
wolfcrypt/src/wc_port.c and wolfssl/wolfcrypt/wc_port.h: tweak gates on atomic implementations to maximize availability within currently supported targets; fix some whitespace.
1 parent 26ba634 commit c430cc7

File tree

5 files changed

+86
-90
lines changed

5 files changed

+86
-90
lines changed

.wolfssl_known_macro_extras

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,7 @@ __ARCH_STRSTR_NO_REDIRECT
969969
__ARM_ARCH_7M__
970970
__ARM_FEATURE_CRYPTO
971971
__ASSEMBLER__
972+
__ATOMIC_CONSUME
972973
__ATOMIC_RELAXED
973974
__AVR_ARCH__
974975
__AVR__

src/ssl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7559,7 +7559,7 @@ int wolfSSL_CTX_check_private_key(const WOLFSSL_CTX* ctx)
75597559
* Return the private key of the WOLFSSL_CTX struct
75607560
* @return WOLFSSL_EVP_PKEY* The caller doesn *NOT*` free the returned object.
75617561
*/
7562-
WOLFSSL_EVP_PKEY* wolfSSL_CTX_get0_privatekey(const WOLFSSL_CTX* ctx)
7562+
WOLFSSL_EVP_PKEY* wolfSSL_CTX_get0_privatekey(WOLFSSL_CTX* ctx)
75637563
{
75647564
WOLFSSL_EVP_PKEY* res;
75657565
const unsigned char *key;

wolfcrypt/src/wc_port.c

Lines changed: 62 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,75 +1273,80 @@ char* wc_strdup_ex(const char *src, int memType) {
12731273

12741274
#if defined(WOLFSSL_ATOMIC_OPS) && !defined(SINGLE_THREADED)
12751275

1276-
#ifdef HAVE_C___ATOMIC
1277-
/* Atomic ops using standard C lib */
1278-
#ifdef __cplusplus
1279-
/* C++ using direct calls to compiler built-in functions */
1276+
#if defined(WOLFSSL_USER_DEFINED_ATOMICS)
1277+
1278+
#elif defined(HAVE_C___ATOMIC) && defined(WOLFSSL_HAVE_ATOMIC_H)
1279+
1280+
/* Default C Implementation */
12801281
void wolfSSL_Atomic_Int_Init(wolfSSL_Atomic_Int* c, int i)
12811282
{
1282-
*c = i;
1283+
atomic_init(c, i);
12831284
}
12841285

12851286
void wolfSSL_Atomic_Uint_Init(wolfSSL_Atomic_Uint* c, unsigned int i)
12861287
{
1287-
*c = i;
1288+
atomic_init(c, i);
12881289
}
12891290

12901291
int wolfSSL_Atomic_Int_FetchAdd(wolfSSL_Atomic_Int* c, int i)
12911292
{
1292-
return __atomic_fetch_add(c, i, __ATOMIC_RELAXED);
1293+
return atomic_fetch_add_explicit(c, i, memory_order_relaxed);
12931294
}
12941295

12951296
int wolfSSL_Atomic_Int_FetchSub(wolfSSL_Atomic_Int* c, int i)
12961297
{
1297-
return __atomic_fetch_sub(c, i, __ATOMIC_RELAXED);
1298+
return atomic_fetch_sub_explicit(c, i, memory_order_relaxed);
12981299
}
12991300

13001301
int wolfSSL_Atomic_Int_AddFetch(wolfSSL_Atomic_Int* c, int i)
13011302
{
1302-
return __atomic_add_fetch(c, i, __ATOMIC_RELAXED);
1303+
int ret = atomic_fetch_add_explicit(c, i, memory_order_relaxed);
1304+
return ret + i;
13031305
}
13041306

13051307
int wolfSSL_Atomic_Int_SubFetch(wolfSSL_Atomic_Int* c, int i)
13061308
{
1307-
return __atomic_sub_fetch(c, i, __ATOMIC_RELAXED);
1309+
int ret = atomic_fetch_sub_explicit(c, i, memory_order_relaxed);
1310+
return ret - i;
13081311
}
13091312

1310-
int wolfSSL_Atomic_Int_CompareExchange(wolfSSL_Atomic_Int* c, int *expected_i,
1311-
int new_i)
1313+
int wolfSSL_Atomic_Int_CompareExchange(
1314+
wolfSSL_Atomic_Int* c, int *expected_i, int new_i)
13121315
{
13131316
/* For the success path, use full synchronization with barriers --
13141317
* "Sequentially-consistent ordering" -- so that all threads see the same
13151318
* "single total modification order of all atomic operations" -- but on
13161319
* failure we just need to be sure we acquire the value that changed out
13171320
* from under us.
13181321
*/
1319-
return __atomic_compare_exchange_n(c, expected_i, new_i, 0 /* weak */,
1320-
__ATOMIC_SEQ_CST, __ATOMIC_ACQUIRE);
1322+
return atomic_compare_exchange_strong_explicit(
1323+
c, expected_i, new_i, memory_order_seq_cst, memory_order_acquire);
13211324
}
13221325

13231326
unsigned int wolfSSL_Atomic_Uint_FetchAdd(wolfSSL_Atomic_Uint* c,
13241327
unsigned int i)
13251328
{
1326-
return __atomic_fetch_add(c, i, __ATOMIC_RELAXED);
1329+
return atomic_fetch_add_explicit(c, i, memory_order_relaxed);
13271330
}
13281331

13291332
unsigned int wolfSSL_Atomic_Uint_FetchSub(wolfSSL_Atomic_Uint* c,
13301333
unsigned int i)
13311334
{
1332-
return __atomic_fetch_sub(c, i, __ATOMIC_RELAXED);
1335+
return atomic_fetch_sub_explicit(c, i, memory_order_relaxed);
13331336
}
13341337

13351338
unsigned int wolfSSL_Atomic_Uint_AddFetch(wolfSSL_Atomic_Uint* c,
13361339
unsigned int i)
13371340
{
1338-
return __atomic_add_fetch(c, i, __ATOMIC_RELAXED);
1341+
unsigned int ret = atomic_fetch_add_explicit(c, i, memory_order_relaxed);
1342+
return ret + i;
13391343
}
13401344

13411345
unsigned int wolfSSL_Atomic_Uint_SubFetch(wolfSSL_Atomic_Uint* c,
13421346
unsigned int i)
13431347
{
1344-
return __atomic_sub_fetch(c, i, __ATOMIC_RELAXED);
1348+
unsigned int ret = atomic_fetch_sub_explicit(c, i, memory_order_relaxed);
1349+
return ret - i;
13451350
}
13461351

13471352
int wolfSSL_Atomic_Uint_CompareExchange(
@@ -1353,89 +1358,90 @@ int wolfSSL_Atomic_Uint_CompareExchange(
13531358
* failure we just need to be sure we acquire the value that changed out
13541359
* from under us.
13551360
*/
1356-
return __atomic_compare_exchange_n(
1357-
c, expected_i, new_i, 0 /* weak */, __ATOMIC_SEQ_CST, __ATOMIC_ACQUIRE);
1361+
return atomic_compare_exchange_strong_explicit(
1362+
c, expected_i, new_i, memory_order_seq_cst, memory_order_acquire);
13581363
}
13591364

13601365
int wolfSSL_Atomic_Ptr_CompareExchange(
13611366
void **c, void **expected_ptr, void *new_ptr)
13621367
{
1368+
/* use gcc-built-in __atomic_compare_exchange_n(), not
1369+
* atomic_compare_exchange_strong_explicit(), to sidestep _Atomic type
1370+
* requirements.
1371+
*/
13631372
return __atomic_compare_exchange_n(
1364-
c, expected_ptr, new_ptr, 0 /* weak */, __ATOMIC_SEQ_CST, __ATOMIC_ACQUIRE);
1373+
c, expected_ptr, new_ptr, 0 /* weak */,
1374+
__ATOMIC_SEQ_CST, __ATOMIC_ACQUIRE);
13651375
}
13661376

1367-
#else
1377+
#elif defined(__GNUC__) && defined(__ATOMIC_RELAXED)
1378+
/* direct calls using gcc-style compiler built-ins */
13681379

1369-
/* Default C Implementation */
13701380
void wolfSSL_Atomic_Int_Init(wolfSSL_Atomic_Int* c, int i)
13711381
{
1372-
atomic_init(c, i);
1382+
*c = i;
13731383
}
13741384

13751385
void wolfSSL_Atomic_Uint_Init(wolfSSL_Atomic_Uint* c, unsigned int i)
13761386
{
1377-
atomic_init(c, i);
1387+
*c = i;
13781388
}
13791389

13801390
int wolfSSL_Atomic_Int_FetchAdd(wolfSSL_Atomic_Int* c, int i)
13811391
{
1382-
return atomic_fetch_add_explicit(c, i, memory_order_relaxed);
1392+
return __atomic_fetch_add(c, i, __ATOMIC_RELAXED);
13831393
}
13841394

13851395
int wolfSSL_Atomic_Int_FetchSub(wolfSSL_Atomic_Int* c, int i)
13861396
{
1387-
return atomic_fetch_sub_explicit(c, i, memory_order_relaxed);
1397+
return __atomic_fetch_sub(c, i, __ATOMIC_RELAXED);
13881398
}
13891399

13901400
int wolfSSL_Atomic_Int_AddFetch(wolfSSL_Atomic_Int* c, int i)
13911401
{
1392-
int ret = atomic_fetch_add_explicit(c, i, memory_order_relaxed);
1393-
return ret + i;
1402+
return __atomic_add_fetch(c, i, __ATOMIC_RELAXED);
13941403
}
13951404

13961405
int wolfSSL_Atomic_Int_SubFetch(wolfSSL_Atomic_Int* c, int i)
13971406
{
1398-
int ret = atomic_fetch_sub_explicit(c, i, memory_order_relaxed);
1399-
return ret - i;
1407+
return __atomic_sub_fetch(c, i, __ATOMIC_RELAXED);
14001408
}
14011409

1402-
int wolfSSL_Atomic_Int_CompareExchange(
1403-
wolfSSL_Atomic_Int* c, int *expected_i, int new_i)
1410+
int wolfSSL_Atomic_Int_CompareExchange(wolfSSL_Atomic_Int* c, int *expected_i,
1411+
int new_i)
14041412
{
14051413
/* For the success path, use full synchronization with barriers --
14061414
* "Sequentially-consistent ordering" -- so that all threads see the same
14071415
* "single total modification order of all atomic operations" -- but on
14081416
* failure we just need to be sure we acquire the value that changed out
14091417
* from under us.
14101418
*/
1411-
return atomic_compare_exchange_strong_explicit(
1412-
c, expected_i, new_i, memory_order_seq_cst, memory_order_acquire);
1419+
return __atomic_compare_exchange_n(c, expected_i, new_i, 0 /* weak */,
1420+
__ATOMIC_SEQ_CST, __ATOMIC_ACQUIRE);
14131421
}
14141422

14151423
unsigned int wolfSSL_Atomic_Uint_FetchAdd(wolfSSL_Atomic_Uint* c,
14161424
unsigned int i)
14171425
{
1418-
return atomic_fetch_add_explicit(c, i, memory_order_relaxed);
1426+
return __atomic_fetch_add(c, i, __ATOMIC_RELAXED);
14191427
}
14201428

14211429
unsigned int wolfSSL_Atomic_Uint_FetchSub(wolfSSL_Atomic_Uint* c,
14221430
unsigned int i)
14231431
{
1424-
return atomic_fetch_sub_explicit(c, i, memory_order_relaxed);
1432+
return __atomic_fetch_sub(c, i, __ATOMIC_RELAXED);
14251433
}
14261434

14271435
unsigned int wolfSSL_Atomic_Uint_AddFetch(wolfSSL_Atomic_Uint* c,
14281436
unsigned int i)
14291437
{
1430-
unsigned int ret = atomic_fetch_add_explicit(c, i, memory_order_relaxed);
1431-
return ret + i;
1438+
return __atomic_add_fetch(c, i, __ATOMIC_RELAXED);
14321439
}
14331440

14341441
unsigned int wolfSSL_Atomic_Uint_SubFetch(wolfSSL_Atomic_Uint* c,
14351442
unsigned int i)
14361443
{
1437-
unsigned int ret = atomic_fetch_sub_explicit(c, i, memory_order_relaxed);
1438-
return ret - i;
1444+
return __atomic_sub_fetch(c, i, __ATOMIC_RELAXED);
14391445
}
14401446

14411447
int wolfSSL_Atomic_Uint_CompareExchange(
@@ -1447,24 +1453,19 @@ int wolfSSL_Atomic_Uint_CompareExchange(
14471453
* failure we just need to be sure we acquire the value that changed out
14481454
* from under us.
14491455
*/
1450-
return atomic_compare_exchange_strong_explicit(
1451-
c, expected_i, new_i, memory_order_seq_cst, memory_order_acquire);
1456+
return __atomic_compare_exchange_n(
1457+
c, expected_i, new_i, 0 /* weak */, __ATOMIC_SEQ_CST, __ATOMIC_ACQUIRE);
14521458
}
14531459

14541460
int wolfSSL_Atomic_Ptr_CompareExchange(
14551461
void **c, void **expected_ptr, void *new_ptr)
14561462
{
1457-
/* use gcc-built-in __atomic_compare_exchange_n(), not
1458-
* atomic_compare_exchange_strong_explicit(), to sidestep _Atomic type
1459-
* requirements.
1460-
*/
14611463
return __atomic_compare_exchange_n(
1462-
c, expected_ptr, new_ptr, 0 /* weak */, __ATOMIC_SEQ_CST, __ATOMIC_ACQUIRE);
1464+
c, expected_ptr, new_ptr, 0 /* weak */,
1465+
__ATOMIC_SEQ_CST, __ATOMIC_ACQUIRE);
14631466
}
14641467

1465-
#endif /* __cplusplus */
1466-
1467-
#elif defined(_MSC_VER)
1468+
#elif defined(_MSC_VER) && !defined(WOLFSSL_NOT_WINDOWS_API)
14681469

14691470
void wolfSSL_Atomic_Int_Init(wolfSSL_Atomic_Int* c, int i)
14701471
{
@@ -1545,8 +1546,8 @@ unsigned int wolfSSL_Atomic_Uint_SubFetch(wolfSSL_Atomic_Uint* c,
15451546
int wolfSSL_Atomic_Uint_CompareExchange(
15461547
wolfSSL_Atomic_Uint* c, unsigned int *expected_i, unsigned int new_i)
15471548
{
1548-
long actual_i = InterlockedCompareExchange
1549-
((wolfSSL_Atomic_Int *)c, (long)new_i, (long)*expected_i);
1549+
long actual_i = InterlockedCompareExchange(
1550+
(wolfSSL_Atomic_Int *)c, (long)new_i, (long)*expected_i);
15501551
if (actual_i == (long)*expected_i) {
15511552
return 1;
15521553
}
@@ -1560,23 +1561,23 @@ int wolfSSL_Atomic_Uint_CompareExchange(
15601561
void ** c, void **expected_ptr, void *new_ptr)
15611562
{
15621563
#ifdef _WIN64
1563-
LONG64 actual_ptr = InterlockedCompareExchange64
1564-
((LONG64 *)c, (LONG64)new_i, (LONG64)*expected_i);
1565-
if (actual_ptr == (LONG64)*expected_i) {
1564+
LONG64 actual_ptr = InterlockedCompareExchange64(
1565+
(LONG64 *)c, (LONG64)new_ptr, (LONG64)*expected_ptr);
1566+
if (actual_ptr == (LONG64)*expected_ptr) {
15661567
return 1;
15671568
}
15681569
else {
1569-
*expected_i = (void *)actual_ptr;
1570+
*expected_ptr = (void *)actual_ptr;
15701571
return 0;
15711572
}
15721573
#else /* !_WIN64 */
1573-
LONG actual_ptr = InterlockedCompareExchange
1574-
((LONG *)c, (LONG)new_i, (LONG)*expected_i);
1575-
if (actual_ptr == (LONG)*expected_i) {
1574+
LONG actual_ptr = InterlockedCompareExchange(
1575+
(LONG *)c, (LONG)new_ptr, (LONG)*expected_ptr);
1576+
if (actual_ptr == (LONG)*expected_ptr) {
15761577
return 1;
15771578
}
15781579
else {
1579-
*expected_i = (void *)actual_ptr;
1580+
*expected_ptr = (void *)actual_ptr;
15801581
return 0;
15811582
}
15821583
#endif /* !_WIN64 */

wolfssl/ssl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3228,7 +3228,7 @@ WOLFSSL_API int wolfSSL_want_write(WOLFSSL* ssl);
32283228
#ifdef OPENSSL_EXTRA
32293229
WOLFSSL_API int wolfSSL_want(WOLFSSL* ssl);
32303230

3231-
WOLFSSL_API WOLFSSL_EVP_PKEY* wolfSSL_CTX_get0_privatekey(const WOLFSSL_CTX* ctx);
3231+
WOLFSSL_API WOLFSSL_EVP_PKEY* wolfSSL_CTX_get0_privatekey(WOLFSSL_CTX* ctx);
32323232

32333233
#include <stdarg.h> /* var_arg */
32343234
WOLFSSL_API int wolfSSL_BIO_vprintf(WOLFSSL_BIO* bio, const char* format,

wolfssl/wolfcrypt/wc_port.h

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -494,31 +494,25 @@
494494
#define WOLFSSL_ATOMIC_LOAD(x) (x)
495495
#define WOLFSSL_ATOMIC_STORE(x, val) (x) = (val)
496496
#define WOLFSSL_ATOMIC_OPS
497-
#elif defined(HAVE_C___ATOMIC)
498-
#ifdef __cplusplus
499-
#if defined(__GNUC__) && defined(__ATOMIC_RELAXED)
500-
/* C++ using direct calls to compiler built-in functions */
501-
typedef volatile int wolfSSL_Atomic_Int;
502-
typedef volatile unsigned int wolfSSL_Atomic_Uint;
503-
#define WOLFSSL_ATOMIC_INITIALIZER(x) (x)
504-
#define WOLFSSL_ATOMIC_LOAD(x) __atomic_load_n(&(x), \
505-
__ATOMIC_CONSUME)
506-
#define WOLFSSL_ATOMIC_STORE(x, val) __atomic_store_n(&(x), \
507-
val, __ATOMIC_RELEASE)
508-
#define WOLFSSL_ATOMIC_OPS
509-
#endif
510-
#else
511-
#ifdef WOLFSSL_HAVE_ATOMIC_H
512-
/* Default C Implementation */
513-
#include <stdatomic.h>
514-
typedef atomic_int wolfSSL_Atomic_Int;
515-
typedef atomic_uint wolfSSL_Atomic_Uint;
516-
#define WOLFSSL_ATOMIC_INITIALIZER(x) (x)
517-
#define WOLFSSL_ATOMIC_LOAD(x) atomic_load(&(x))
518-
#define WOLFSSL_ATOMIC_STORE(x, val) atomic_store(&(x), val)
519-
#define WOLFSSL_ATOMIC_OPS
520-
#endif /* WOLFSSL_HAVE_ATOMIC_H */
521-
#endif
497+
#elif defined(HAVE_C___ATOMIC) && defined(WOLFSSL_HAVE_ATOMIC_H)
498+
/* Default C Implementation */
499+
#include <stdatomic.h>
500+
typedef atomic_int wolfSSL_Atomic_Int;
501+
typedef atomic_uint wolfSSL_Atomic_Uint;
502+
#define WOLFSSL_ATOMIC_INITIALIZER(x) (x)
503+
#define WOLFSSL_ATOMIC_LOAD(x) atomic_load(&(x))
504+
#define WOLFSSL_ATOMIC_STORE(x, val) atomic_store(&(x), val)
505+
#define WOLFSSL_ATOMIC_OPS
506+
#elif defined(__GNUC__) && defined(__ATOMIC_CONSUME)
507+
/* direct calls using gcc-style compiler built-ins */
508+
typedef volatile int wolfSSL_Atomic_Int;
509+
typedef volatile unsigned int wolfSSL_Atomic_Uint;
510+
#define WOLFSSL_ATOMIC_INITIALIZER(x) (x)
511+
#define WOLFSSL_ATOMIC_LOAD(x) __atomic_load_n(&(x), \
512+
__ATOMIC_CONSUME)
513+
#define WOLFSSL_ATOMIC_STORE(x, val) __atomic_store_n(&(x), \
514+
val, __ATOMIC_RELEASE)
515+
#define WOLFSSL_ATOMIC_OPS
522516
#elif defined(_MSC_VER) && !defined(WOLFSSL_NOT_WINDOWS_API)
523517
/* Use MSVC compiler intrinsics for atomic ops */
524518
#ifdef _WIN32_WCE
@@ -534,8 +528,8 @@
534528
#define WOLFSSL_ATOMIC_OPS
535529
#endif
536530

537-
#ifndef WOLFSSL_ATOMIC_INITIALIZER
538-
/* If we weren't able to implement atomics above, disable them here. */
531+
/* If we weren't able to implement atomics above, disable them here. */
532+
#ifndef WOLFSSL_ATOMIC_OPS
539533
#define WOLFSSL_NO_ATOMICS
540534
#endif
541535
#endif

0 commit comments

Comments
 (0)