From 7323dea41b2eaae2e9c7f045fc8f7ff1e982c72c Mon Sep 17 00:00:00 2001 From: Aidan Keefe Date: Tue, 14 Jul 2026 16:33:38 -0600 Subject: [PATCH] Added new thread yield macro and added new header with CAAM_ADDRESS def and a couple loose fixes --- doc/dox_comments/header_files/sha256.h | 4 +- tests/api/test_aes.c | 7 +- wolfcrypt/src/aes.c | 2 + wolfcrypt/src/des3.c | 4 +- wolfcrypt/src/dsa.c | 2 +- wolfcrypt/src/ecc.c | 4 ++ wolfcrypt/src/hmac.c | 1 + .../Espressif/esp_crt_bundle/esp_crt_bundle.c | 2 +- .../src/port/Renesas/renesas_fspsm_sha.c | 6 +- wolfcrypt/src/port/caam/wolfcaam_ecdsa.c | 2 +- wolfcrypt/src/port/caam/wolfcaam_hmac.c | 2 +- wolfcrypt/src/port/devcrypto/devcrypto_hash.c | 25 +++---- wolfcrypt/src/random.c | 65 ++++++++++++------- wolfcrypt/src/wc_mlkem_poly.c | 4 +- wolfssl/wolfcrypt/ecc.h | 18 ++--- wolfssl/wolfcrypt/include.am | 3 +- wolfssl/wolfcrypt/port/caam/caam_qnx.h | 3 +- wolfssl/wolfcrypt/port/caam/caam_type.h | 48 ++++++++++++++ wolfssl/wolfcrypt/port/caam/wolfcaam.h | 1 + .../wolfcrypt/port/caam/wolfcaam_fsl_nxp.h | 2 +- wolfssl/wolfcrypt/port/caam/wolfcaam_qnx.h | 2 +- wolfssl/wolfcrypt/port/caam/wolfcaam_seco.h | 3 +- .../wolfcrypt/port/devcrypto/wc_devcrypto.h | 2 +- wolfssl/wolfcrypt/settings.h | 2 + wolfssl/wolfcrypt/wc_port.h | 24 +++++++ 25 files changed, 170 insertions(+), 68 deletions(-) create mode 100644 wolfssl/wolfcrypt/port/caam/caam_type.h diff --git a/doc/dox_comments/header_files/sha256.h b/doc/dox_comments/header_files/sha256.h index 3a94b797ba..2fe62b57a0 100644 --- a/doc/dox_comments/header_files/sha256.h +++ b/doc/dox_comments/header_files/sha256.h @@ -366,11 +366,11 @@ int wc_Sha256_Grow(wc_Sha256* sha256, const byte* in, int inSz); \return negative on error \param src Source SHA256 structure - \param dst Destination SHA256 structure + \param dst Destination SHA256 structure; must be zeroed/initialized _Example_ \code - wc_Sha256 src, dst; + wc_Sha256 src, dst = {0}; int ret = wc_Sha256Copy(&src, &dst); \endcode diff --git a/tests/api/test_aes.c b/tests/api/test_aes.c index 0a1a8116b3..874f658517 100644 --- a/tests/api/test_aes.c +++ b/tests/api/test_aes.c @@ -3262,6 +3262,8 @@ int test_wc_AesGcmEncryptDecrypt_Sizes(void) int sz; int i; WC_DECLARE_VAR(plain, byte, GCM_LEN, NULL); + /* enlarged size is to accommodate devcrypto build which assumes + * space in buffer to append auth tag */ WC_DECLARE_VAR(cipher, byte, GCM_LEN+WC_AES_BLOCK_SIZE, NULL); #ifdef HAVE_AES_DECRYPT WC_DECLARE_VAR(decrypted, byte, GCM_LEN, NULL); @@ -3288,7 +3290,7 @@ int test_wc_AesGcmEncryptDecrypt_Sizes(void) ExpectIntEQ(wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte)), 0); for (sz = 0; sz < WC_AES_BLOCK_SIZE; sz++) { - XMEMSET(cipher, 0, GCM_LEN); + XMEMSET(cipher, 0, GCM_LEN + WC_AES_BLOCK_SIZE); ExpectIntEQ(wc_AesGcmEncrypt(&aes, cipher, plain, sz, iv, ivLen, tag, sizeof(tag), NULL, 0), 0); ExpectBufEQ(cipher, expected, sz); @@ -3304,7 +3306,7 @@ int test_wc_AesGcmEncryptDecrypt_Sizes(void) i = 0; for (sz = WC_AES_BLOCK_SIZE; sz <= GCM_LEN; sz *= 2) { - XMEMSET(cipher, 0, GCM_LEN); + XMEMSET(cipher, 0, GCM_LEN + WC_AES_BLOCK_SIZE); ExpectIntEQ(wc_AesGcmEncrypt(&aes, cipher, plain, sz, iv, ivLen, tag, sizeof(tag), NULL, 0), 0); ExpectBufEQ(tag, expTagLong[i], WC_AES_BLOCK_SIZE); @@ -3996,6 +3998,7 @@ int test_wc_AesGcmNonStdNonce(void) !defined(HAVE_FIPS) && \ !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_KCAPI) && \ !defined(WOLFSSL_DEVCRYPTO_AES) + /* DEVCRYPTO does not support Non std Nonce */ /* ------------------------------------------------------------------ * Section 1: 1-byte IV, AES-128 diff --git a/wolfcrypt/src/aes.c b/wolfcrypt/src/aes.c index 8546537682..db16c717c4 100644 --- a/wolfcrypt/src/aes.c +++ b/wolfcrypt/src/aes.c @@ -5972,6 +5972,7 @@ static void AesSetKey_C(Aes* aes, const byte* key, word32 keySz, int dir) #if defined(WOLFSSL_DEVCRYPTO) && \ (defined(WOLFSSL_DEVCRYPTO_AES) || defined(WOLFSSL_DEVCRYPTO_CBC)) aes->ctx.inited = 0; + aes->ctx.cfd = -1; #endif #ifdef WOLFSSL_IMX6_CAAM_BLOB #ifdef WOLFSSL_CHECK_MEM_ZERO @@ -15787,6 +15788,7 @@ int wc_AesInit(Aes* aes, void* heap, int devId) #if defined(WOLFSSL_DEVCRYPTO) && \ (defined(WOLFSSL_DEVCRYPTO_AES) || defined(WOLFSSL_DEVCRYPTO_CBC)) aes->ctx.inited = 0; + aes->ctx.cfd = -1; #endif #if defined(WOLFSSL_IMXRT_DCP) DCPAesInit(aes); diff --git a/wolfcrypt/src/des3.c b/wolfcrypt/src/des3.c index 841fc960f9..10f6288aec 100644 --- a/wolfcrypt/src/des3.c +++ b/wolfcrypt/src/des3.c @@ -1570,13 +1570,13 @@ pc1m[(l = j + totrot[i]) < (j < 28 ? 28 : 56) ? l : l-28]; /* rotate left and right halves independently */ - for (j = 0; j < 48; j++) { /* select bits individually */ + for (j = 0; j < 48; j++) { /* select bits individually */ byte bit; byte mask; bit = (byte)(pcr[pc2[j] - 1]); /* all pcr values are either 0 or 1 */ mask = (byte)(0 - bit); /* mask is either 0xFF or 0x00 */ - /* only set to bytebit value if bit == 1*/ + /* only set to bytebit value if bit == 1 */ ks[j/6] |= (byte)((bytebit[j % 6] >> 2) & mask); } diff --git a/wolfcrypt/src/dsa.c b/wolfcrypt/src/dsa.c index 252e483bbe..c534386788 100644 --- a/wolfcrypt/src/dsa.c +++ b/wolfcrypt/src/dsa.c @@ -1151,7 +1151,7 @@ int wc_DsaVerify_ex(const byte* digest, word32 digestSz, const byte* sig, if (digest == NULL || sig == NULL || key == NULL || answer == NULL) return BAD_FUNC_ARG; - /* assign default value so we return 0 on error */ + /* assign default value so verification is always failed on error */ *answer = 0; /* Note the min allowed digestSz here is WC_SHA_DIGEST_SIZE, not diff --git a/wolfcrypt/src/ecc.c b/wolfcrypt/src/ecc.c index bc5ff67bfc..ca2e9bca15 100644 --- a/wolfcrypt/src/ecc.c +++ b/wolfcrypt/src/ecc.c @@ -270,6 +270,10 @@ ECC Curve Sizes: #include #endif +#if defined(WOLFSSL_CAAM) + #include +#endif + #if defined(WOLFSSL_KCAPI_ECC) #include #endif diff --git a/wolfcrypt/src/hmac.c b/wolfcrypt/src/hmac.c index f73fa9caed..0138fc13cc 100644 --- a/wolfcrypt/src/hmac.c +++ b/wolfcrypt/src/hmac.c @@ -1537,6 +1537,7 @@ int wc_HmacInit(Hmac* hmac, void* heap, int devId) #endif #if defined(WOLFSSL_DEVCRYPTO_HMAC) hmac->ctx.inited = 0; + hmac->ctx.cfd = -1; #endif #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC) diff --git a/wolfcrypt/src/port/Espressif/esp_crt_bundle/esp_crt_bundle.c b/wolfcrypt/src/port/Espressif/esp_crt_bundle/esp_crt_bundle.c index a99387bc88..85fc192370 100644 --- a/wolfcrypt/src/port/Espressif/esp_crt_bundle/esp_crt_bundle.c +++ b/wolfcrypt/src/port/Espressif/esp_crt_bundle/esp_crt_bundle.c @@ -983,7 +983,7 @@ static CB_INLINE int wolfssl_ssl_conf_verify_cb_no_signer(int preverify, /* Clean up and exit */ if ((_crt_found == 0) && (bundle_cert != NULL)) { ESP_LOGW(TAG, "Cert not found, free bundle_cert"); - /* this_subject and this_issuer are apart of bundle_cert and will be + /* this_subject and this_issuer are a part of bundle_cert and will be * freed here*/ wolfSSL_X509_free(bundle_cert); bundle_cert = NULL; diff --git a/wolfcrypt/src/port/Renesas/renesas_fspsm_sha.c b/wolfcrypt/src/port/Renesas/renesas_fspsm_sha.c index 8a263a63fd..976543583d 100644 --- a/wolfcrypt/src/port/Renesas/renesas_fspsm_sha.c +++ b/wolfcrypt/src/port/Renesas/renesas_fspsm_sha.c @@ -418,7 +418,7 @@ static int FSPSM_HashFinal(wolfssl_FSPSM_Hash* hash, byte* out, word32 outSz) #endif wc_fspsm_hw_lock(); - if ((ret = Init(&handle)) == FSP_SUCCESS) { + if (Init(&handle) == FSP_SUCCESS) { ret = Update(&handle, (uint8_t*)hash->msg, hash->used); if (ret == FSP_SUCCESS) { ret = Final(&handle, out, (uint32_t*)&sz); @@ -433,6 +433,10 @@ static int FSPSM_HashFinal(wolfssl_FSPSM_Hash* hash, byte* out, word32 outSz) } } } + + if (ret != FSP_SUCCESS) + ret = WC_HW_E; + wc_fspsm_hw_unlock(); #elif defined(WOLFSSL_RENESAS_RSIP) diff --git a/wolfcrypt/src/port/caam/wolfcaam_ecdsa.c b/wolfcrypt/src/port/caam/wolfcaam_ecdsa.c index 57b3da424b..308cdd5a42 100644 --- a/wolfcrypt/src/port/caam/wolfcaam_ecdsa.c +++ b/wolfcrypt/src/port/caam/wolfcaam_ecdsa.c @@ -339,7 +339,7 @@ int wc_CAAM_EccSign(const byte* in, int inlen, byte* out, word32* outlen, /* private key */ if (key->blackKey == CAAM_BLACK_KEY_SM) { - buf[idx].TheAddress = (CAAM_ADDRESS)key->blackKey; + buf[idx].TheAddress = key->blackKey; args[0] = CAAM_BLACK_KEY_SM; /* is a black key in sm */ buf[idx].Length = keySz; } diff --git a/wolfcrypt/src/port/caam/wolfcaam_hmac.c b/wolfcrypt/src/port/caam/wolfcaam_hmac.c index fb9c149a4d..e6c54286aa 100644 --- a/wolfcrypt/src/port/caam/wolfcaam_hmac.c +++ b/wolfcrypt/src/port/caam/wolfcaam_hmac.c @@ -54,7 +54,7 @@ int wc_CAAM_Hmac(Hmac* hmac, int macType, const byte* msg, int msgSz, { int ret = 0; - if (hmac->ctx.cfd == -1 && hmac->keyLen > 0) { + if (hmac->ctx.inited == 0 && hmac->keyLen > 0) { ret = wc_DevCrypto_HmacSetKey(hmac, macType, hmac->keyRaw, hmac->keyLen); if (ret != 0) { diff --git a/wolfcrypt/src/port/devcrypto/devcrypto_hash.c b/wolfcrypt/src/port/devcrypto/devcrypto_hash.c index 04612dad48..4c573a2a6e 100644 --- a/wolfcrypt/src/port/devcrypto/devcrypto_hash.c +++ b/wolfcrypt/src/port/devcrypto/devcrypto_hash.c @@ -193,12 +193,11 @@ int wc_Sha256GetHash(wc_Sha256* sha, byte* hash) #ifdef WOLFSSL_DEVCRYPTO_HASH_KEEP { int ret; - wc_Sha256 cpy; - XMEMSET(&cpy, 0, sizeof(cpy)); /* ZII */ + wc_Sha256 cpy = {0}; ret = wc_Sha256Copy(sha, &cpy); - if (ret == 0 && - (ret = HashUpdate(&cpy, CRYPTO_SHA2_256, cpy.msg, cpy.used)) == 0) { + if (ret == 0 && (ret = HashUpdate(&cpy, + CRYPTO_SHA2_256, cpy.msg, cpy.used)) == 0) { /* help static analysis tools out */ XMEMSET(hash, 0, WC_SHA256_DIGEST_SIZE); ret = GetDigest(&cpy, CRYPTO_SHA2_256, hash); @@ -226,25 +225,21 @@ int wc_Sha256Copy(wc_Sha256* src, wc_Sha256* dst) #ifdef WOLFSSL_DEVCRYPTO_HASH_KEEP wc_Sha256Free(dst); if ((ret = wc_InitSha256_ex(dst, src->heap, 0)) != 0) { - /* make sure that any attempts to free dst - * dont accidentally close an unopened fd */ - dst->ctx.inited = 0; - dst->ctx.cfd = -1; return ret; } dst->len = src->len; dst->used = src->used; - dst->msg = (byte*)XMALLOC(src->len, dst->heap, DYNAMIC_TYPE_TMP_BUFFER); - if (dst->msg == NULL) { - wc_Sha256Free(dst); - return MEMORY_E; + if (src->len > 0) { + dst->msg = (byte*)XMALLOC(src->len, dst->heap, DYNAMIC_TYPE_TMP_BUFFER); + if (dst->msg == NULL) { + wc_Sha256Free(dst); + return MEMORY_E; + } + XMEMCPY(dst->msg, src->msg, src->len); } - XMEMCPY(dst->msg, src->msg, src->len); return ret; #else - (void)src; - (void)dst; (void)ret; WOLFSSL_MSG("Compile with WOLFSSL_DEVCRYPTO_HASH_KEEP for this feature"); diff --git a/wolfcrypt/src/random.c b/wolfcrypt/src/random.c index 5a31a26459..f1d099e649 100644 --- a/wolfcrypt/src/random.c +++ b/wolfcrypt/src/random.c @@ -386,10 +386,10 @@ static int sha512DrbgDisabled = 0; #endif /* WOLFSSL_DRBG_SHA512 */ enum { - wc_DrbgState_Mutex_Uninited, - wc_DrbgState_Mutex_InitProgress, - wc_DrbgState_Mutex_FreeProgress, - wc_DrbgState_Mutex_Inited + WC_DRBG_MUTEX_UNINITED, + WC_DRBG_MUTEX_INITPROGRESS, + WC_DRBG_MUTEX_FREEPROGRESS, + WC_DRBG_MUTEX_INITED }; #ifndef SINGLE_THREADED @@ -398,9 +398,9 @@ static wolfSSL_Mutex drbgStateMutex #ifndef WOLFSSL_MUTEX_INITIALIZER #ifdef WOLFSSL_ATOMIC_OPS static wolfSSL_Atomic_Int drbgStateMutex_inited = - WOLFSSL_ATOMIC_INITIALIZER(wc_DrbgState_Mutex_Uninited); + WOLFSSL_ATOMIC_INITIALIZER(WC_DRBG_MUTEX_UNINITED); #else -static int drbgStateMutex_inited = 0; +static volatile int drbgStateMutex_inited = 0; #endif #endif #endif /* !SINGLE_THREADED */ @@ -410,34 +410,45 @@ int wc_DrbgState_MutexInit(void) { #ifndef SINGLE_THREADED #ifndef WOLFSSL_MUTEX_INITIALIZER + #if defined(WOLFSSL_ATOMIC_OPS) && defined(WOLFSSL_THREAD_YIELD) /* State machine so the mutex isn't marked ready before it is. The CAS * winner initializes and publishes Inited; losers spin (via their own * 'expected', which the failed CAS updates) until they see Inited. */ for (;;) { - int expected = wc_DrbgState_Mutex_Uninited; + WC_ATOMIC_INT_ARG expected = WC_DRBG_MUTEX_UNINITED; if (wolfSSL_Atomic_Int_CompareExchange(&drbgStateMutex_inited, - &expected, wc_DrbgState_Mutex_InitProgress)) { + &expected, WC_DRBG_MUTEX_INITPROGRESS)) { /* We own initialization (state moved Uninited -> InitProgress). */ int ret = wc_InitMutex(&drbgStateMutex); if (ret != 0) { /* Init failed; release ownership so another thread may retry. */ (void)wolfSSL_Atomic_Int_Exchange(&drbgStateMutex_inited, - wc_DrbgState_Mutex_Uninited); + WC_DRBG_MUTEX_UNINITED); return ret; } /* Publish the fully initialized mutex. */ (void)wolfSSL_Atomic_Int_Exchange(&drbgStateMutex_inited, - wc_DrbgState_Mutex_Inited); + WC_DRBG_MUTEX_INITED); return 0; } /* Spin until drbgStateMutex is inited */ - if (expected == wc_DrbgState_Mutex_Inited) { + if (expected == WC_DRBG_MUTEX_INITED) { /* Mutex is fully initialized. */ return 0; } - continue; + WOLFSSL_THREAD_YIELD(); } + #else + if (drbgStateMutex_inited == WC_DRBG_MUTEX_UNINITED) { + int ret = wc_InitMutex(&drbgStateMutex); + if (ret != 0) { + return ret; + } + drbgStateMutex_inited = WC_DRBG_MUTEX_INITED; + } + #endif + #endif #endif return 0; @@ -447,13 +458,14 @@ int wc_DrbgState_MutexFree(void) { #ifndef SINGLE_THREADED #ifndef WOLFSSL_MUTEX_INITIALIZER + #if defined(WOLFSSL_ATOMIC_OPS) && defined(WOLFSSL_THREAD_YIELD) /* CAS the ready state (Inited -> FreeProgress) so exactly one caller frees. * Losers spin until it settles: Uninited returns success; Inited (a free * that failed and rolled back) lets a spinning thread retry. */ for (;;) { - int expected = wc_DrbgState_Mutex_Inited; + WC_ATOMIC_INT_ARG expected = WC_DRBG_MUTEX_INITED; if (wolfSSL_Atomic_Int_CompareExchange(&drbgStateMutex_inited, - &expected, wc_DrbgState_Mutex_FreeProgress)) { + &expected, WC_DRBG_MUTEX_FREEPROGRESS)) { /* We own teardown (state moved Inited -> FreeProgress). */ int ret = wc_FreeMutex(&drbgStateMutex); if (ret != 0) { @@ -461,25 +473,31 @@ int wc_DrbgState_MutexFree(void) * valid object, so restore the ready state rather than leaving * the flag claiming it is uninitialized. */ (void)wolfSSL_Atomic_Int_Exchange(&drbgStateMutex_inited, - wc_DrbgState_Mutex_Inited); + WC_DRBG_MUTEX_INITED); return ret; } /* Mark the mutex as no longer initialized. */ (void)wolfSSL_Atomic_Int_Exchange(&drbgStateMutex_inited, - wc_DrbgState_Mutex_Uninited); + WC_DRBG_MUTEX_UNINITED); return 0; } /* CAS failed; 'expected' holds the observed state. */ - if (expected == wc_DrbgState_Mutex_Uninited) { + if (expected == WC_DRBG_MUTEX_UNINITED) { /* Already freed or never initialized; nothing to do. */ return 0; } - /* expected == InitProgress or FreeProgress: another thread is busy; - * spin until it settles. */ - continue; - } - return 0; + WOLFSSL_THREAD_YIELD(); + } + #else + if (drbgStateMutex_inited == WC_DRBG_MUTEX_INITED) { + int ret = wc_FreeMutex(&drbgStateMutex); + if (ret != 0) { + return ret; + } + drbgStateMutex_inited = WC_DRBG_MUTEX_UNINITED; + } + #endif #endif #endif return 0; @@ -3905,6 +3923,7 @@ static int wc_GenerateSeed_IntelRD(OS_Seed* os, byte* output, word32 sz) return ret; } writeUnalignedWord64(output, rndTmpLocal); + ForceZero(&rndTmpLocal, sizeof(rndTmpLocal)); } if (sz == 0) return 0; @@ -3992,6 +4011,7 @@ static int wc_GenerateRand_IntelRD(OS_Seed* os, byte* output, word32 sz) return ret; } writeUnalignedWord64(output, rndTmpLocal); + ForceZero(&rndTmpLocal, sizeof(rndTmpLocal)); } if (sz == 0) return 0; @@ -4002,6 +4022,7 @@ static int wc_GenerateRand_IntelRD(OS_Seed* os, byte* output, word32 sz) return ret; XMEMCPY(output, &rndTmp, sz); + ForceZero(&rndTmp, sizeof(rndTmp)); return 0; } diff --git a/wolfcrypt/src/wc_mlkem_poly.c b/wolfcrypt/src/wc_mlkem_poly.c index 611171962d..b939cfe22d 100644 --- a/wolfcrypt/src/wc_mlkem_poly.c +++ b/wolfcrypt/src/wc_mlkem_poly.c @@ -5110,7 +5110,7 @@ static int mlkem_get_noise_k4_avx512(MLKEM_PRF_T* prf, sword16* vec1, */ static void mlkem_get_noise_x3_eta2_aarch64(byte* rand, byte* seed, byte o) { - word64 state[3 * 25]; + word64 state[3 * 25] = {0}; state[0*25 + 4] = 0x1f00 + 0 + o; state[1*25 + 4] = 0x1f00 + 1 + o; @@ -5143,7 +5143,7 @@ static void mlkem_get_noise_x3_eta2_aarch64(byte* rand, byte* seed, byte o) */ static void mlkem_get_noise_x3_eta3_aarch64(byte* rand, byte* seed, byte o) { - word64 state[3 * 25]; + word64 state[3 * 25] = {0}; state[0*25 + 4] = 0x1f00 + 0 + o; state[1*25 + 4] = 0x1f00 + 1 + o; diff --git a/wolfssl/wolfcrypt/ecc.h b/wolfssl/wolfcrypt/ecc.h index a0f8e94438..97c0e0859f 100644 --- a/wolfssl/wolfcrypt/ecc.h +++ b/wolfssl/wolfcrypt/ecc.h @@ -67,6 +67,11 @@ #include #endif +#ifdef WOLFSSL_CAAM + /* for CAAM_ADDRESS, used by struct ecc_key below */ + #include +#endif + #if defined(WOLFSSL_XILINX_CRYPT_VERSAL) #include #endif @@ -76,10 +81,6 @@ #endif -#if defined(WOLFSSL_CAAM) - #include -#endif - #ifdef __cplusplus extern "C" { #endif @@ -532,13 +533,8 @@ struct ecc_key { #endif #ifdef WOLFSSL_CAAM - #ifdef CAAM_ADDRESS - CAAM_ADDRESS blackKey; /* address of key encrypted and in secure memory */ - CAAM_ADDRESS securePubKey; /* address of public key in secure memory */ - #else - word32 blackKey; /* address of key encrypted and in secure memory */ - word32 securePubKey; /* address of public key in secure memory */ - #endif + CAAM_ADDRESS blackKey; /* address of key encrypted and in secure memory */ + CAAM_ADDRESS securePubKey; /* address of public key in secure memory */ int partNum; /* partition number*/ #endif #ifdef WOLFSSL_SE050 diff --git a/wolfssl/wolfcrypt/include.am b/wolfssl/wolfcrypt/include.am index d10b3cb3dd..86018af96e 100644 --- a/wolfssl/wolfcrypt/include.am +++ b/wolfssl/wolfcrypt/include.am @@ -209,7 +209,8 @@ endif endif if BUILD_CAAM -nobase_include_HEADERS+= wolfssl/wolfcrypt/port/caam/wolfcaam.h \ +nobase_include_HEADERS+= wolfssl/wolfcrypt/port/caam/caam_type.h \ + wolfssl/wolfcrypt/port/caam/wolfcaam.h \ wolfssl/wolfcrypt/port/caam/wolfcaam_sha.h \ wolfssl/wolfcrypt/port/caam/wolfcaam_hash.h \ wolfssl/wolfcrypt/port/caam/wolfcaam_rsa.h \ diff --git a/wolfssl/wolfcrypt/port/caam/caam_qnx.h b/wolfssl/wolfcrypt/port/caam/caam_qnx.h index 7e4f5676a1..74117569f2 100644 --- a/wolfssl/wolfcrypt/port/caam/caam_qnx.h +++ b/wolfssl/wolfcrypt/port/caam/caam_qnx.h @@ -41,10 +41,11 @@ #define CAAM_LOCK_MUTEX(x) pthread_mutex_lock((x)) #define CAAM_UNLOCK_MUTEX(x) pthread_mutex_unlock((x)) +#include + #define Error int #define Value int #define Boolean int -#define CAAM_ADDRESS uintptr_t #define Success 1 #define Failure 0 #define INTERRUPT_Panic() do {} while (0) diff --git a/wolfssl/wolfcrypt/port/caam/caam_type.h b/wolfssl/wolfcrypt/port/caam/caam_type.h new file mode 100644 index 0000000000..5b07d266fc --- /dev/null +++ b/wolfssl/wolfcrypt/port/caam/caam_type.h @@ -0,0 +1,48 @@ +/* caam_type.h + * + * Copyright (C) 2006-2026 wolfSSL Inc. + * + * This file is part of wolfSSL. + * + * wolfSSL is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * wolfSSL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ + +/* Single definition of CAAM_ADDRESS, the type used to hold an address handed to + * or returned from the CAAM. + * + * struct ecc_key stores CAAM addresses, so its layout depends on this type. + * Keep the definition here, selected only from build configuration macros, so + * that every translation unit in a build agrees on it no matter which of the + * CAAM headers it happens to include, and in which order. + * + * This header intentionally has no wolfSSL dependencies; the standalone QNX + * driver build includes it without settings.h. Whatever includes it is expected + * to have already pulled in settings.h if it needs the configuration macros. + */ + +#ifndef WOLF_CRYPT_CAAM_TYPE_H +#define WOLF_CRYPT_CAAM_TYPE_H + +#include + +#ifndef CAAM_ADDRESS + #ifdef WOLFSSL_SECO_CAAM + #define CAAM_ADDRESS intptr_t + #else + #define CAAM_ADDRESS uintptr_t + #endif +#endif + +#endif /* WOLF_CRYPT_CAAM_TYPE_H */ diff --git a/wolfssl/wolfcrypt/port/caam/wolfcaam.h b/wolfssl/wolfcrypt/port/caam/wolfcaam.h index d033054249..4d7aa270aa 100644 --- a/wolfssl/wolfcrypt/port/caam/wolfcaam.h +++ b/wolfssl/wolfcrypt/port/caam/wolfcaam.h @@ -24,6 +24,7 @@ #include #include +#include /* include for porting layer */ #ifdef WOLFSSL_QNX_CAAM diff --git a/wolfssl/wolfcrypt/port/caam/wolfcaam_fsl_nxp.h b/wolfssl/wolfcrypt/port/caam/wolfcaam_fsl_nxp.h index 032c0f22f2..39a90e46a8 100644 --- a/wolfssl/wolfcrypt/port/caam/wolfcaam_fsl_nxp.h +++ b/wolfssl/wolfcrypt/port/caam/wolfcaam_fsl_nxp.h @@ -50,7 +50,7 @@ #define NoActivityReady -1 #define MemoryOperationNotPerformed -1 -#define CAAM_ADDRESS uintptr_t +#include #ifndef WOLFSSL_CAAM_BUFFER #define WOLFSSL_CAAM_BUFFER typedef struct CAAM_BUFFER { diff --git a/wolfssl/wolfcrypt/port/caam/wolfcaam_qnx.h b/wolfssl/wolfcrypt/port/caam/wolfcaam_qnx.h index 910d17db12..b1f7b55209 100644 --- a/wolfssl/wolfcrypt/port/caam/wolfcaam_qnx.h +++ b/wolfssl/wolfcrypt/port/caam/wolfcaam_qnx.h @@ -41,7 +41,7 @@ #define NoActivityReady -1 #define MemoryOperationNotPerformed -1 -#define CAAM_ADDRESS uintptr_t +#include #ifndef WOLFSSL_CAAM_BUFFER #define WOLFSSL_CAAM_BUFFER typedef struct CAAM_BUFFER { diff --git a/wolfssl/wolfcrypt/port/caam/wolfcaam_seco.h b/wolfssl/wolfcrypt/port/caam/wolfcaam_seco.h index 6d9004e923..19be34e847 100644 --- a/wolfssl/wolfcrypt/port/caam/wolfcaam_seco.h +++ b/wolfssl/wolfcrypt/port/caam/wolfcaam_seco.h @@ -44,8 +44,7 @@ #define NoActivityReady -1 #define MemoryOperationNotPerformed -1 -#include -#define CAAM_ADDRESS intptr_t +#include #ifndef WOLFSSL_CAAM_BUFFER #define WOLFSSL_CAAM_BUFFER typedef struct CAAM_BUFFER { diff --git a/wolfssl/wolfcrypt/port/devcrypto/wc_devcrypto.h b/wolfssl/wolfcrypt/port/devcrypto/wc_devcrypto.h index def1b3eeb8..c3387d76f7 100644 --- a/wolfssl/wolfcrypt/port/devcrypto/wc_devcrypto.h +++ b/wolfssl/wolfcrypt/port/devcrypto/wc_devcrypto.h @@ -38,7 +38,7 @@ typedef struct WC_CRYPTODEV { int cfd; - word8 inited : 1;/* is this object initialized (1) or not (0) */ + unsigned int inited : 1;/* is this object initialized (1) or not (0) */ struct session_op sess; } WC_CRYPTODEV; diff --git a/wolfssl/wolfcrypt/settings.h b/wolfssl/wolfcrypt/settings.h index 696ece0323..c3eded1b93 100644 --- a/wolfssl/wolfcrypt/settings.h +++ b/wolfssl/wolfcrypt/settings.h @@ -3153,6 +3153,7 @@ #endif #ifdef WOLFSSL_SECO_CAAM + #undef WOLFSSL_CAAM #define WOLFSSL_CAAM #define WOLFSSL_HASH_KEEP @@ -3160,6 +3161,7 @@ #endif #ifdef WOLFSSL_IMXRT1170_CAAM + #undef WOLFSSL_CAAM #define WOLFSSL_CAAM #endif diff --git a/wolfssl/wolfcrypt/wc_port.h b/wolfssl/wolfcrypt/wc_port.h index 3d3faf24ac..0709d162d4 100644 --- a/wolfssl/wolfcrypt/wc_port.h +++ b/wolfssl/wolfcrypt/wc_port.h @@ -773,6 +773,30 @@ } #endif +/* Yield the CPU to another runnable thread. Used by spin-wait loops that are + * waiting on another thread to finish a short critical section. Ports may + * override. */ +#ifndef WOLFSSL_THREAD_YIELD + #if defined(SINGLE_THREADED) + #define WOLFSSL_THREAD_YIELD() WC_DO_NOTHING + #elif defined(WOLFSSL_PTHREADS) + #include + #define WOLFSSL_THREAD_YIELD() (void)sched_yield() + #elif defined(USE_WINDOWS_API) && !defined(WOLFSSL_NOT_WINDOWS_API) + #define WOLFSSL_THREAD_YIELD() (void)SwitchToThread() + #elif defined(FREERTOS) + #define WOLFSSL_THREAD_YIELD() taskYIELD() + #elif defined(THREADX) + #define WOLFSSL_THREAD_YIELD() tx_thread_relinquish() + #elif defined(WOLFSSL_ZEPHYR) + #define WOLFSSL_THREAD_YIELD() k_yield() + #elif defined(WOLFSSL_VXWORKS) + #include + #include + #define WOLFSSL_THREAD_YIELD() (void)taskDelay(0) + #endif +#endif + /* Reference counting. */ typedef struct wolfSSL_RefWithMutex { #if !defined(SINGLE_THREADED)