From a4215e7b7253f004bf1925a10c4ec169a3e716ef Mon Sep 17 00:00:00 2001 From: Thomas Cook Date: Mon, 10 Aug 2026 11:31:05 -0400 Subject: [PATCH 1/3] Support for ti am64x hw acceleration based on ti mcu plus sdk. --- wolfcrypt/benchmark/benchmark.c | 22 +- wolfcrypt/src/aes.c | 14 +- wolfcrypt/src/ecc.c | 4 + wolfcrypt/src/port/ti/ti-sa2ul_port.c | 927 ++++++++++++++++++++++ wolfcrypt/src/random.c | 3 + wolfcrypt/src/sha256.c | 4 + wolfcrypt/src/sha512.c | 4 + wolfcrypt/src/wc_port.c | 12 + wolfcrypt/test/test.c | 9 + wolfssl/wolfcrypt/aes.h | 7 + wolfssl/wolfcrypt/port/ti/ti-sa2ul_port.h | 33 + wolfssl/wolfcrypt/settings.h | 14 + wolfssl/wolfcrypt/sha256.h | 7 + wolfssl/wolfcrypt/sha512.h | 7 + wolfssl/wolfcrypt/types.h | 4 +- 15 files changed, 1056 insertions(+), 15 deletions(-) create mode 100644 wolfcrypt/src/port/ti/ti-sa2ul_port.c create mode 100644 wolfssl/wolfcrypt/port/ti/ti-sa2ul_port.h diff --git a/wolfcrypt/benchmark/benchmark.c b/wolfcrypt/benchmark/benchmark.c index dae44bf5b69..e1e63797588 100644 --- a/wolfcrypt/benchmark/benchmark.c +++ b/wolfcrypt/benchmark/benchmark.c @@ -220,6 +220,10 @@ #endif #endif +#ifdef WOLFSSL_TI_AM64X + #include +#endif + #ifdef WOLFSSL_ASYNC_CRYPT #include #endif @@ -721,6 +725,10 @@ static WC_INLINE void bench_append_memory_info(char* buffer, size_t size, #define fprintf(fp, ...) \ __android_log_print(ANDROID_LOG_DEBUG, "[WOLFCRYPT]", __VA_ARGS__) +#elif defined(TI_MCU_PLUS_SDK) + #include "kernel/nortos/dpl/common/printf.h" + #define printf printf_ + #else #if defined(XMALLOC_USER) || defined(FREESCALE_MQX) /* MQX classic needs for EXIT_FAILURE */ @@ -2536,7 +2544,11 @@ static void benchmark_static_init(int force) bench_pq_asym_algs = 0; bench_other_algs = 0; bench_pq_hash_sig_algs = 0; + #ifdef WOLFSSL_BENCHMARK_FIXED_CSV + csv_format = 1; + #else csv_format = 0; + #endif } } @@ -8179,13 +8191,15 @@ void bench_sha256(int useDeviceID) printf("InitSha256_ex failed, ret = %d\n", ret); goto exit; } - #ifdef WOLFSSL_PIC32MZ_HASH - wc_Sha256SizeSet(hash[i], numBlocks * bench_size); - #endif } bench_stats_start(&count, &start); do { + #ifdef WOLFSSL_PIC32MZ_HASH + for (i = 0; i < BENCH_MAX_PENDING; i++) { + wc_Sha256SizeSet(hash[i], numBlocks * bench_size); + } + #endif for (times = 0; times < numBlocks || pending > 0; ) { bench_async_poll(&pending); @@ -18309,7 +18323,7 @@ int wolfcrypt_benchmark_main(int argc, char** argv) argc--; argv++; } -#endif /* MAIN_NO_ARGS */ +#endif /* !MAIN_NO_ARGS */ #if defined(WOLFSSL_BENCHMARK_FIXED_CSV) /* when defined, we'll always output CSV regardless of params. diff --git a/wolfcrypt/src/aes.c b/wolfcrypt/src/aes.c index 61f8e9e311f..482d02b753b 100644 --- a/wolfcrypt/src/aes.c +++ b/wolfcrypt/src/aes.c @@ -3449,9 +3449,6 @@ WC_ALL_ARGS_NOT_NULL static WARN_UNUSED_RESULT int wc_AesEncrypt( { #if defined(MAX3266X_AES) word32 keySize; -#endif -#if defined(MAX3266X_CB) - int ret_cb; #endif word32 r; @@ -3584,12 +3581,12 @@ WC_ALL_ARGS_NOT_NULL static WARN_UNUSED_RESULT int wc_AesEncrypt( outBlock, (unsigned int)keySize); } #endif -#if defined(MAX3266X_CB) && defined(HAVE_AES_ECB) /* Can do a basic ECB block */ +#if (defined(WOLFSSL_TI_AM64X) || defined(MAX3266X_CB)) && defined(HAVE_AES_ECB) #ifndef WOLF_CRYPTO_CB_FIND if (aes->devId != INVALID_DEVID) #endif { - ret_cb = wc_CryptoCb_AesEcbEncrypt(aes, outBlock, inBlock, + int ret_cb = wc_CryptoCb_AesEcbEncrypt(aes, outBlock, inBlock, WC_AES_BLOCK_SIZE); if (ret_cb != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) return ret_cb; @@ -4301,9 +4298,6 @@ WC_ALL_ARGS_NOT_NULL static WARN_UNUSED_RESULT int wc_AesDecrypt( { #if defined(MAX3266X_AES) word32 keySize; -#endif -#if defined(MAX3266X_CB) - int ret_cb; #endif word32 r; @@ -4410,12 +4404,12 @@ WC_ALL_ARGS_NOT_NULL static WARN_UNUSED_RESULT int wc_AesDecrypt( } #endif -#if defined(MAX3266X_CB) && defined(HAVE_AES_ECB) /* Can do a basic ECB block */ +#if (defined(WOLFSSL_TI_AM64X) || defined(MAX3266X_CB)) && defined(HAVE_AES_ECB) #ifndef WOLF_CRYPTO_CB_FIND if (aes->devId != INVALID_DEVID) #endif { - ret_cb = wc_CryptoCb_AesEcbDecrypt(aes, outBlock, inBlock, + int ret_cb = wc_CryptoCb_AesEcbDecrypt(aes, outBlock, inBlock, WC_AES_BLOCK_SIZE); if (ret_cb != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) return ret_cb; diff --git a/wolfcrypt/src/ecc.c b/wolfcrypt/src/ecc.c index 34acd96ca87..60707b7977e 100644 --- a/wolfcrypt/src/ecc.c +++ b/wolfcrypt/src/ecc.c @@ -4373,6 +4373,10 @@ int wc_ecc_get_curve_size_from_id(int curve_id) return ecc_sets[curve_idx].size; } +#ifndef strcasecmp +int strcasecmp(const char *s1, const char *s2); +#endif + /* Returns the curve index that corresponds to a given curve name in * ecc_sets[] of ecc.c * diff --git a/wolfcrypt/src/port/ti/ti-sa2ul_port.c b/wolfcrypt/src/port/ti/ti-sa2ul_port.c new file mode 100644 index 00000000000..23e7e09a87f --- /dev/null +++ b/wolfcrypt/src/port/ti/ti-sa2ul_port.c @@ -0,0 +1,927 @@ +/* ti-sa2ul_port.c + * + * 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 + */ + + +#include + +#if defined(WOLFSSL_TI_AM64X) + +#ifndef WOLF_CRYPTO_CB + #error WOLFSSL_TI_SA2UL support requires ./configure --enable-cryptocb or WOLF_CRYPTO_CB to be defined +#endif + +#include +#include +#include +#include + +#ifdef NO_INLINE + #include +#else + #define WOLFSSL_MISC_INCLUDED + #include +#endif + +/* from ti mcu plus sdk... */ +#include "kernel/dpl/CacheP.h" +#include "security/security_common/drivers/crypto/crypto.h" +#include "security/security_common/drivers/crypto/rng/rng.h" +#include "security/security_common/drivers/crypto/sa2ul/sa2ul.h" + +static Crypto_Handle handle; +static Crypto_Context cryptoCtx XALIGNED(SA2UL_CACHELINE_ALIGNMENT); + +#ifndef WC_NO_RNG +#define RNG_NUM_DWORDS (4u) +static RNG_Handle rngHandle = NULL; +static void ti_sa2ul_trng_init(void) +{ + RNG_Handle handle = NULL; + if (gRngConfig[0].attrs->isOpen == 0) { + SA2UL_engineEnable(CSL_CP_ACE_CMD_STATUS_TRNG_EN_MASK); + handle = RNG_open(0); + if (handle != NULL) { + if (RNG_setup(handle) == RNG_RETURN_SUCCESS) { + rngHandle = handle; + } + else { + RNG_close(handle); + } + } + } + else { + /* already opened -- use existing handle */ + rngHandle = (RNG_Handle)&gRngConfig[0]; + } +} + +static int ti_sa2ul_trng_get(OS_Seed* os, byte* output, word32 sz) +{ + if (output == NULL && sz != 0) + return -1; + + while (sz) { + uint32_t random[RNG_NUM_DWORDS]; + uint8_t *ptr = (uint8_t *)random; + int copy_len; + if (RNG_read(rngHandle, random) != RNG_RETURN_SUCCESS) + return -1; + copy_len = RNG_NUM_DWORDS * 4; + if (sz < copy_len) + copy_len = sz; + XMEMCPY(output, ptr, copy_len); + output += copy_len; + sz -= copy_len; + } + + return 0; +} +#endif /* WC_NO_RNG */ + +static void _u8LeToU32(uint32_t *dest, uint8_t *src, uint32_t len) +{ + uint32_t i, t = 0; + + for (i=0; ikeylen == AES_128_KEY_SIZE) { + scParams.encKeySize = SA2UL_ENC_KEYSIZE_128; + } + else { + scParams.encKeySize = SA2UL_ENC_KEYSIZE_256; + } + XMEMCPY(&scParams.key[0], aes->devKey, aes->keylen); + XMEMCPY(&scParams.iv[0], aes->reg, AES_IV_SIZE); + scParams.inputLen = sz; + aes->scObj.totalLengthInBytes = sz; + + if (SA2UL_contextAlloc(cryptoCtx.drvHandle, + &aes->scObj, &scParams) != SystemP_SUCCESS) + { + return WC_HW_E; + } + + CacheP_wbInv((void *)in, sz, CacheP_TYPE_ALLD); + + if (SA2UL_contextProcess(&aes->scObj, in, sz, out) != SystemP_SUCCESS) + ret = WC_HW_E; + + (void)SA2UL_contextFree(&aes->scObj); + + XMEMCPY(aes->reg, out + sz - 16, 16); + + return ret; +} + +#ifdef HAVE_AES_DECRYPT +static int ti_sa2ul_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz) +{ + int ret = 0; + SA2UL_ContextParams scParams; + byte tmp_iv[16]; + + SA2UL_ContextParams_init(&scParams); + + scParams.opType = SA2UL_OP_ENC; + scParams.encAlg = SA2UL_ENC_ALG_AES; + scParams.encMode = SA2UL_ENC_MODE_CBC; + scParams.encDirection = SA2UL_ENC_DIR_DECRYPT; + if (aes->keylen == AES_128_KEY_SIZE) { + scParams.encKeySize = SA2UL_ENC_KEYSIZE_128; + } + else { + scParams.encKeySize = SA2UL_ENC_KEYSIZE_256; + } + XMEMCPY(&scParams.key[0], aes->devKey, aes->keylen); + XMEMCPY(&scParams.iv[0], aes->reg, AES_IV_SIZE); + scParams.inputLen = sz; + aes->scObj.totalLengthInBytes = sz; + + if (SA2UL_contextAlloc(cryptoCtx.drvHandle, + &aes->scObj, &scParams) != SystemP_SUCCESS) + { + return WC_HW_E; + } + + XMEMCPY(tmp_iv, in + sz - 16, 16); + + CacheP_wbInv((void *)in, sz, CacheP_TYPE_ALLD); + + if (SA2UL_contextProcess(&aes->scObj, in, sz, out) != SystemP_SUCCESS) + ret = WC_HW_E; + + (void)SA2UL_contextFree(&aes->scObj); + + XMEMCPY(aes->reg, tmp_iv, 16); + + return ret; +} +#endif /* HAVE_AES_DECRYPT */ +#endif /* HAVE_AES_CBC */ + +#ifdef HAVE_AES_ECB +static int ti_sa2ul_AesEcbEncrypt(Aes* aes, byte* out, const byte* in, word32 sz) +{ + int ret = 0; + SA2UL_ContextParams scParams; + + SA2UL_ContextParams_init(&scParams); + + scParams.opType = SA2UL_OP_ENC; + scParams.encAlg = SA2UL_ENC_ALG_AES; + scParams.encMode = SA2UL_ENC_MODE_ECB; + scParams.encDirection = SA2UL_ENC_DIR_ENCRYPT; + if (aes->keylen == AES_128_KEY_SIZE) { + scParams.encKeySize = SA2UL_ENC_KEYSIZE_128; + } + else { + scParams.encKeySize = SA2UL_ENC_KEYSIZE_256; + } + XMEMCPY(&scParams.key[0], aes->devKey, aes->keylen); + XMEMCPY(&scParams.iv[0], aes->reg, AES_IV_SIZE); + scParams.inputLen = sz; + aes->scObj.totalLengthInBytes = sz; + + if (SA2UL_contextAlloc(cryptoCtx.drvHandle, + &aes->scObj, &scParams) != SystemP_SUCCESS) + { + return WC_HW_E; + } + + CacheP_wbInv((void *)in, sz, CacheP_TYPE_ALLD); + + if (SA2UL_contextProcess(&aes->scObj, in, sz, out) != SystemP_SUCCESS) + ret = WC_HW_E; + + (void)SA2UL_contextFree(&aes->scObj); + + return ret; +} + +#ifdef HAVE_AES_DECRYPT +static int ti_sa2ul_AesEcbDecrypt(Aes* aes, byte* out, const byte* in, word32 sz) +{ + int ret = 0; + SA2UL_ContextParams scParams; + + SA2UL_ContextParams_init(&scParams); + + scParams.opType = SA2UL_OP_ENC; + scParams.encAlg = SA2UL_ENC_ALG_AES; + scParams.encMode = SA2UL_ENC_MODE_ECB; + scParams.encDirection = SA2UL_ENC_DIR_DECRYPT; + if (aes->keylen == AES_128_KEY_SIZE) { + scParams.encKeySize = SA2UL_ENC_KEYSIZE_128; + } + else { + scParams.encKeySize = SA2UL_ENC_KEYSIZE_256; + } + XMEMCPY(&scParams.key[0], aes->devKey, aes->keylen); + XMEMCPY(&scParams.iv[0], aes->reg, AES_IV_SIZE); + scParams.inputLen = sz; + aes->scObj.totalLengthInBytes = sz; + + if (SA2UL_contextAlloc(cryptoCtx.drvHandle, + &aes->scObj, &scParams) != SystemP_SUCCESS) + { + return WC_HW_E; + } + + CacheP_wbInv((void *)in, sz, CacheP_TYPE_ALLD); + + if (SA2UL_contextProcess(&aes->scObj, in, sz, out) != SystemP_SUCCESS) + ret = WC_HW_E; + + (void)SA2UL_contextFree(&aes->scObj); + + return ret; +} +#endif /* HAVE_AES_DECRYPT */ +#endif /* HAVE_AES_ECB */ + +#ifdef HAVE_AESGCM +static void _override_iv_with_ghash(Aes* aes, const byte* iv, word32 ivSz) +{ + SA2UL_SecCtx sc; + byte ivtmp[WC_AES_BLOCK_SIZE]; + + GHASH(&aes->gcm, NULL, 0, iv, ivSz, ivtmp, WC_AES_BLOCK_SIZE); + XMEMCPY(aes->scObj.ctxPrms.iv, ivtmp, WC_AES_BLOCK_SIZE); + _64byteReverseWords((uint32_t*)&sc, (uint32_t*)&aes->scObj.secCtx, sizeof(sc)); + _u8LeToU32(sc.u.enc.encAux3, ivtmp, WC_AES_BLOCK_SIZE); + _64byteReverseWords((uint32_t*)&aes->scObj.secCtx, (uint32_t*)&sc, sizeof(sc)); + CacheP_wbInv(&aes->scObj.secCtx, sizeof(sc), CacheP_TYPE_ALLD); +} + +static int ti_sa2ul_AesGcmEncrypt(Aes* aes, byte* out, + const byte* in, word32 sz, + const byte* iv, word32 ivSz, + byte* authTag, word32 authTagSz, + const byte* authIn, word32 authInSz) +{ + int ret = 0; + SA2UL_ContextParams scParams; + + SA2UL_ContextParams_init(&scParams); + + scParams.opType = SA2UL_OP_ENC; + scParams.encAlg = SA2UL_ENC_ALG_AES; + scParams.encMode = SA2UL_ENC_MODE_GCM; + scParams.encDirection = SA2UL_ENC_DIR_ENCRYPT; + if (aes->keylen == AES_128_KEY_SIZE) { + scParams.encKeySize = SA2UL_ENC_KEYSIZE_128; + } + else { + scParams.encKeySize = SA2UL_ENC_KEYSIZE_256; + } + XMEMCPY(scParams.key, aes->devKey, aes->keylen); + XMEMCPY(scParams.iv, iv, ivSz); + XMEMCPY(scParams.ghash, aes->gcm.H, WC_AES_BLOCK_SIZE); + if (authInSz <= sizeof(scParams.aad)) { + XMEMCPY(scParams.aad, authIn, authInSz); + scParams.aadLen = authInSz; + } + else { + scParams.aadLen = 0; + } + scParams.inputLen = sz; + aes->scObj.totalLengthInBytes = sz; + + if (SA2UL_contextAlloc(cryptoCtx.drvHandle, + &aes->scObj, &scParams) != SystemP_SUCCESS) + { + return WC_HW_E; + } + + if (ivSz != GCM_NONCE_MID_SZ) { + _override_iv_with_ghash(aes, iv, ivSz); + } + + CacheP_wbInv((void *)in, sz, CacheP_TYPE_ALLD); + + if (SA2UL_contextProcess(&aes->scObj, in, sz, out) != SystemP_SUCCESS) + ret = WC_HW_E; + + (void)SA2UL_contextFree(&aes->scObj); + + if (authTag) { + if (authInSz <= sizeof(scParams.aad)) { + XMEMCPY(authTag, aes->scObj.computedHash, authTagSz); + } + else { + ALIGN16 byte initialCounter[WC_AES_BLOCK_SIZE]; + ALIGN16 byte scratch[WC_AES_BLOCK_SIZE]; + GHASH(&aes->gcm, authIn, authInSz, out, sz, authTag, authTagSz); + if (ivSz == GCM_NONCE_MID_SZ) { + XMEMCPY(initialCounter, iv, ivSz); + initialCounter[WC_AES_BLOCK_SIZE-4] = 0; + initialCounter[WC_AES_BLOCK_SIZE-3] = 0; + initialCounter[WC_AES_BLOCK_SIZE-2] = 0; + initialCounter[WC_AES_BLOCK_SIZE-1] = 1; + } + else { + XMEMCPY(initialCounter, aes->scObj.ctxPrms.iv, WC_AES_BLOCK_SIZE); + } + ret = wc_AesEncryptDirect(aes, scratch, initialCounter); + xorbuf(authTag, scratch, authTagSz); + } + } + + return ret; +} + +#ifdef HAVE_AES_DECRYPT +static int ti_sa2ul_AesGcmDecrypt(Aes* aes, byte* out, + const byte* in, word32 sz, + const byte* iv, word32 ivSz, + const byte* authTag, word32 authTagSz, + const byte* authIn, word32 authInSz) +{ + int ret = 0; + SA2UL_ContextParams scParams; + + SA2UL_ContextParams_init(&scParams); + + scParams.opType = SA2UL_OP_ENC; + scParams.encAlg = SA2UL_ENC_ALG_AES; + scParams.encMode = SA2UL_ENC_MODE_GCM; + scParams.encDirection = SA2UL_ENC_DIR_DECRYPT; + if (aes->keylen == AES_128_KEY_SIZE) { + scParams.encKeySize = SA2UL_ENC_KEYSIZE_128; + } + else { + scParams.encKeySize = SA2UL_ENC_KEYSIZE_256; + } + XMEMCPY(scParams.key, aes->devKey, aes->keylen); + XMEMCPY(scParams.iv, iv, ivSz); + XMEMCPY(scParams.ghash, aes->gcm.H, WC_AES_BLOCK_SIZE); + XMEMCPY(scParams.aad, authIn, authInSz); + scParams.aadLen = authInSz; + scParams.inputLen = sz; + aes->scObj.totalLengthInBytes = sz; + + if (SA2UL_contextAlloc(cryptoCtx.drvHandle, + &aes->scObj, &scParams) != SystemP_SUCCESS) + { + return WC_HW_E; + } + + if (ivSz != GCM_NONCE_MID_SZ) { + _override_iv_with_ghash(aes, iv, ivSz); + } + + CacheP_wbInv((void *)in, sz, CacheP_TYPE_ALLD); + + if (SA2UL_contextProcess(&aes->scObj, in, sz, out) != SystemP_SUCCESS) + ret = WC_HW_E; + + (void)SA2UL_contextFree(&aes->scObj); + + if (authTag) { + if (authInSz <= sizeof(scParams.aad)) { + if (XMEMCMP(authTag, aes->scObj.computedHash, authTagSz) != 0) + ret = WC_NO_ERR_TRACE(AES_GCM_AUTH_E); + } + else { + ALIGN16 byte initialCounter[WC_AES_BLOCK_SIZE]; + ALIGN16 byte scratch[WC_AES_BLOCK_SIZE]; + ALIGN16 byte Tprime[WC_AES_BLOCK_SIZE]; + GHASH(&aes->gcm, authIn, authInSz, in, sz, Tprime, sizeof(Tprime)); + if (ivSz == GCM_NONCE_MID_SZ) { + XMEMCPY(initialCounter, iv, ivSz); + initialCounter[WC_AES_BLOCK_SIZE-4] = 0; + initialCounter[WC_AES_BLOCK_SIZE-3] = 0; + initialCounter[WC_AES_BLOCK_SIZE-2] = 0; + initialCounter[WC_AES_BLOCK_SIZE-1] = 1; + } + else { + XMEMCPY(initialCounter, aes->scObj.ctxPrms.iv, WC_AES_BLOCK_SIZE); + } + ret = wc_AesEncryptDirect(aes, scratch, initialCounter); + xorbuf(Tprime, scratch, sizeof(Tprime)); + if (XMEMCMP(authTag, Tprime, authTagSz) != 0) + ret = WC_NO_ERR_TRACE(AES_GCM_AUTH_E); + } + } + + return ret; +} +#endif /* HAVE_AES_DECRYPT */ +#endif /* HAVE_AESGCM */ +#endif /* !NO_AES */ + + +#if !defined(NO_SHA256) || defined(WOLFSSL_SHA512) +/* The ti mcu plus sdk sa2ul driver requires an output buffer of at least + * the size of the input buffer, and it will write data to it, though we don't + * use the data. So, we consider this a scratch buffer, but it also limits + * the amount of data we can hash at one time. */ +#define HASH_SCRATCH_SIZE 0x2000u +static byte hash_scratch[HASH_SCRATCH_SIZE] XALIGNED(SA2UL_CACHELINE_ALIGNMENT); +static volatile int sa2ul_hash_in_use = 0; + +#ifndef NO_SHA256 +static int ti_sa2ul_InitSha256_ctx(wc_Sha256* sha256) +{ + SA2UL_ContextParams scParams; + + SA2UL_ContextParams_init(&scParams); + + scParams.opType = SA2UL_OP_AUTH; + scParams.hashAlg = SA2UL_HASH_ALG_SHA2_256; + /* default length to all ff's, final will override when known */ + scParams.inputLen = 0xffffffffUL; + sha256->scObj.totalLengthInBytes = 0xffffffffUL; + + if (SA2UL_contextAlloc(cryptoCtx.drvHandle, + &sha256->scObj, &scParams) != SystemP_SUCCESS) + { + return WC_HW_E; + } + + sa2ul_hash_in_use = 1; + + return 0; +} + +static int ti_sa2ul_Sha256Free_ctx(wc_Sha256* sha256) +{ + (void)SA2UL_contextFree(&sha256->scObj); + XMEMSET(&sha256->scObj, 0, sizeof(sha256->scObj)); + + sa2ul_hash_in_use = 0; + + return 0; +} + +static int ti_sa2ul_Sha256Hash(wc_Sha256* sha256, const byte* in, + word32 inSz, byte* digest) +{ + int ret = 0; + byte* buffer = (byte*)sha256->buffer; + word32 blocksLen; + word32 partialLen; + + if (in == NULL && digest == NULL) + return WC_HW_E; + + if (sha256->scObj.txBytesCnt == 0 && sa2ul_hash_in_use == 1) { + sha256->flags |= WC_HASH_FLAG_ISCOPY; + return CRYPTOCB_UNAVAILABLE; + } + + if (in != NULL) { + /* update... */ + sha256->loLen += inSz; + + /* handle leftovers first */ + if (sha256->buffLen > 0) { + partialLen = min(inSz, WC_SHA256_BLOCK_SIZE - sha256->buffLen); + XMEMCPY(&buffer[sha256->buffLen], in, partialLen); + sha256->buffLen += partialLen; + in += partialLen; + inSz -= partialLen; + if (sha256->buffLen == WC_SHA256_BLOCK_SIZE) { + CacheP_wbInv((void *)buffer, WC_SHA256_BLOCK_SIZE, + CacheP_TYPE_ALLD); + if (sha256->scObj.txBytesCnt == 0) { + if (ti_sa2ul_InitSha256_ctx(sha256) != 0) + return WC_HW_E; + } + if (SA2UL_contextProcess(&sha256->scObj, buffer, + WC_SHA256_BLOCK_SIZE, hash_scratch) != SystemP_SUCCESS) + { + return WC_HW_E; + } + XMEMCPY(sha256->digest, &sha256->scObj.computedHash, + WC_SHA256_DIGEST_SIZE); + /* final will fall back to sw, and sw needs bytes reversed */ + ByteReverseWords(sha256->digest, sha256->digest, + WC_SHA256_DIGEST_SIZE); + sha256->buffLen = 0; + } + } + /* chunks of full blocks */ + while (inSz >= WC_SHA256_BLOCK_SIZE) { + blocksLen = min(sizeof(hash_scratch), + inSz & ~((word32)WC_SHA256_BLOCK_SIZE-1)); + CacheP_wbInv((void *)in, blocksLen, CacheP_TYPE_ALLD); + if (sha256->scObj.txBytesCnt == 0) { + if (ti_sa2ul_InitSha256_ctx(sha256) != 0) + return WC_HW_E; + } + if (SA2UL_contextProcess(&sha256->scObj, in, blocksLen, + hash_scratch) != SystemP_SUCCESS) { + return WC_HW_E; + } + XMEMCPY(sha256->digest, &sha256->scObj.computedHash, + WC_SHA256_DIGEST_SIZE); + ByteReverseWords(sha256->digest, sha256->digest, + WC_SHA256_DIGEST_SIZE); + in += blocksLen; + inSz -= blocksLen; + } + /* save leftovers */ + if (inSz > 0) { + XMEMCPY(&buffer[0], in, inSz); + sha256->buffLen = inSz; + } + } + else if (digest != NULL) { + /* final... */ + /* hash will be finalized in sw via fallback, but we need the driver + * to tear down the context in hw. To do that, we update the context + * length and push some final arbitrary data. It will not affect + * the hash */ + if (sha256->scObj.txBytesCnt != 0) { + sha256->scObj.ctxPrms.inputLen = sha256->scObj.txBytesCnt + + WC_SHA256_DIGEST_SIZE; + sha256->scObj.totalLengthInBytes = sha256->scObj.txBytesCnt + + WC_SHA256_DIGEST_SIZE; + CacheP_wbInv((void *)buffer, WC_SHA256_DIGEST_SIZE, CacheP_TYPE_ALLD); + if (SA2UL_contextProcess(&sha256->scObj, buffer, + WC_SHA256_DIGEST_SIZE, hash_scratch) != SystemP_SUCCESS) + { + ret = WC_HW_E; + } + (void)ti_sa2ul_Sha256Free_ctx(sha256); + } + ret = CRYPTOCB_UNAVAILABLE; /* fall back to sw */ + } + + return ret; +} +#endif /* !NO_SHA256 */ + +#ifdef WOLFSSL_SHA512 +static int ti_sa2ul_InitSha512_ctx(wc_Sha512* sha512) +{ + SA2UL_ContextParams scParams; + + SA2UL_ContextParams_init(&scParams); + + scParams.opType = SA2UL_OP_AUTH; + scParams.hashAlg = SA2UL_HASH_ALG_SHA2_512; + /* default length to all ff's, final will override when known */ + scParams.inputLen = 0xffffffffUL; + sha512->scObj.totalLengthInBytes = 0xffffffffUL; + + if (SA2UL_contextAlloc(cryptoCtx.drvHandle, + &sha512->scObj, &scParams) != SystemP_SUCCESS) + { + return WC_HW_E; + } + return 0; +} + +static int ti_sa2ul_Sha512Free_ctx(wc_Sha512* sha512) +{ + (void)SA2UL_contextFree(&sha512->scObj); + XMEMSET(&sha512->scObj, 0, sizeof(sha512->scObj)); + + return 0; +} + +static int ti_sa2ul_Sha512Hash(wc_Sha512* sha512, const byte* in, + word32 inSz, byte* digest) +{ + int ret = 0; + byte* buffer = (byte*)sha512->buffer; + word32 blocksLen; + word32 partialLen; + + if (in == NULL && digest == NULL) + return WC_HW_E; + + if (sha512->scObj.txBytesCnt == 0 && sa2ul_hash_in_use == 1) { + sha512->flags |= WC_HASH_FLAG_ISCOPY; + return CRYPTOCB_UNAVAILABLE; + } + + if (in != NULL) { + /* update... */ + sha512->loLen += inSz; + + /* handle leftovers first */ + if (sha512->buffLen > 0) { + partialLen = min(inSz, WC_SHA512_BLOCK_SIZE - sha512->buffLen); + XMEMCPY(&buffer[sha512->buffLen], in, partialLen); + sha512->buffLen += partialLen; + in += partialLen; + inSz -= partialLen; + if (sha512->buffLen == WC_SHA512_BLOCK_SIZE) { + CacheP_wbInv((void *)buffer, WC_SHA512_BLOCK_SIZE, CacheP_TYPE_ALLD); + if (sha512->scObj.txBytesCnt == 0) { + if (ti_sa2ul_InitSha512_ctx(sha512) != 0) + return WC_HW_E; + } + if (SA2UL_contextProcess(&sha512->scObj, buffer, + WC_SHA512_BLOCK_SIZE, hash_scratch) != SystemP_SUCCESS) + { + return WC_HW_E; + } + XMEMCPY(sha512->digest, &sha512->scObj.computedHash, + WC_SHA512_DIGEST_SIZE); + /* final will fall back to sw, and sw needs bytes reversed */ + ByteReverseWords64(sha512->digest, sha512->digest, + WC_SHA512_DIGEST_SIZE); + sha512->buffLen = 0; + } + } + /* chunks of full blocks */ + while (inSz >= WC_SHA512_BLOCK_SIZE) { + blocksLen = min(sizeof(hash_scratch), + inSz & ~((word32)WC_SHA512_BLOCK_SIZE-1)); + CacheP_wbInv((void *)in, blocksLen, CacheP_TYPE_ALLD); + if (sha512->scObj.txBytesCnt == 0) { + if (ti_sa2ul_InitSha512_ctx(sha512) != 0) + return WC_HW_E; + } + if (SA2UL_contextProcess(&sha512->scObj, in, blocksLen, + hash_scratch) != SystemP_SUCCESS) { + return WC_HW_E; + } + XMEMCPY(sha512->digest, &sha512->scObj.computedHash, + WC_SHA512_DIGEST_SIZE); + ByteReverseWords64(sha512->digest, sha512->digest, + WC_SHA512_DIGEST_SIZE); + in += blocksLen; + inSz -= blocksLen; + } + /* save leftovers */ + if (inSz > 0) { + XMEMCPY(&buffer[0], in, inSz); + sha512->buffLen = inSz; + } + } + else if (digest != NULL) { + /* final... */ + /* hash will be finalized in sw via fallback, but we need the driver + * to tear down the context in hw. To do that, we update the context + * length and push some final arbitrary data. It will not affect + * the hash */ + if (sha512->scObj.txBytesCnt != 0) { + sha512->scObj.ctxPrms.inputLen = sha512->scObj.txBytesCnt + + WC_SHA512_DIGEST_SIZE; + sha512->scObj.totalLengthInBytes = sha512->scObj.txBytesCnt + + WC_SHA512_DIGEST_SIZE; + CacheP_wbInv((void *)buffer, WC_SHA512_DIGEST_SIZE, CacheP_TYPE_ALLD); + if (SA2UL_contextProcess(&sha512->scObj, buffer, + WC_SHA512_DIGEST_SIZE, hash_scratch) != SystemP_SUCCESS) { + ret = WC_HW_E; + } + (void)ti_sa2ul_Sha512Free_ctx(sha512); + } + ret = CRYPTOCB_UNAVAILABLE; /* fall back to sw */ + } + + return ret; +} +#endif /* WOLFSSL_SHA512 */ +#endif /* !NO_SHA256 || WOLFSSL_SHA512 */ + +static int ti_sa2ul_CryptoDevCb(int devId, wc_CryptoInfo* info, void* devCtx) +{ + int ret = CRYPTOCB_UNAVAILABLE; + + WOLFSSL_ENTER("ti_sa2ul_CryptoDevCb"); + + (void)devCtx; + + if (info == NULL) + return BAD_FUNC_ARG; + if (devId == INVALID_DEVID) + return CRYPTOCB_UNAVAILABLE; + +#ifdef DEBUG_CRYPTOCB + wc_CryptoCb_InfoString(info); +#endif + + if (info->algo_type == WC_ALGO_TYPE_CIPHER) + { +#if !defined(NO_AES) + if (0) { + /* nothing */ + } +# if defined(HAVE_AES_CBC) + else if (info->cipher.type == WC_CIPHER_AES_CBC) { + Aes* aes = info->cipher.aescbc.aes; + if (aes == NULL) + return BAD_FUNC_ARG; + if (check_aes_keylength(aes->keylen) != 0) + return CRYPTOCB_UNAVAILABLE; /* fall back to sw */ + if (info->cipher.enc) { + ret = ti_sa2ul_AesCbcEncrypt(info->cipher.aescbc.aes, + info->cipher.aescbc.out, + info->cipher.aescbc.in, + info->cipher.aescbc.sz); + } +# ifdef HAVE_AES_DECRYPT + else { + ret = ti_sa2ul_AesCbcDecrypt(info->cipher.aescbc.aes, + info->cipher.aescbc.out, + info->cipher.aescbc.in, + info->cipher.aescbc.sz); + } +# endif /* HAVE_AES_DECRYPT */ + } +# endif /* HAVE_AES_CBC */ +# if defined(HAVE_AES_ECB) + else if (info->cipher.type == WC_CIPHER_AES_ECB) { + Aes* aes = info->cipher.aesecb.aes; + if (aes == NULL) + return BAD_FUNC_ARG; + if (check_aes_keylength(aes->keylen) != 0) + return CRYPTOCB_UNAVAILABLE; /* fall back to sw */ + if (info->cipher.enc) { + ret = ti_sa2ul_AesEcbEncrypt(info->cipher.aesecb.aes, + info->cipher.aesecb.out, + info->cipher.aesecb.in, + info->cipher.aesecb.sz); + } +# ifdef HAVE_AES_DECRYPT + else { + ret = ti_sa2ul_AesEcbDecrypt(info->cipher.aesecb.aes, + info->cipher.aesecb.out, + info->cipher.aesecb.in, + info->cipher.aesecb.sz); + } +# endif /* HAVE_AES_DECRYPT */ + } +# endif /* HAVE_AES_ECB */ +# if defined(HAVE_AESGCM) + else if (info->cipher.type == WC_CIPHER_AES_GCM) { + if (info->cipher.enc) { + Aes* aes = info->cipher.aesgcm_enc.aes; + if (aes == NULL) + return BAD_FUNC_ARG; + if (check_aes_keylength(aes->keylen) != 0 || + info->cipher.aesgcm_enc.sz == 0) { + return CRYPTOCB_UNAVAILABLE; /* fall back to sw */ + } + ret = ti_sa2ul_AesGcmEncrypt(aes, + info->cipher.aesgcm_enc.out, + info->cipher.aesgcm_enc.in, + info->cipher.aesgcm_enc.sz, + info->cipher.aesgcm_enc.iv, + info->cipher.aesgcm_enc.ivSz, + info->cipher.aesgcm_enc.authTag, + info->cipher.aesgcm_enc.authTagSz, + info->cipher.aesgcm_enc.authIn, + info->cipher.aesgcm_enc.authInSz); + } +# ifdef HAVE_AES_DECRYPT + else { + Aes* aes = info->cipher.aesgcm_dec.aes; + if (aes == NULL) + return BAD_FUNC_ARG; + if (check_aes_keylength(aes->keylen) != 0 || + info->cipher.aesgcm_dec.sz == 0) { + return CRYPTOCB_UNAVAILABLE; /* fall back to sw */ + } + ret = ti_sa2ul_AesGcmDecrypt(aes, + info->cipher.aesgcm_dec.out, + info->cipher.aesgcm_dec.in, + info->cipher.aesgcm_dec.sz, + info->cipher.aesgcm_dec.iv, + info->cipher.aesgcm_dec.ivSz, + info->cipher.aesgcm_dec.authTag, + info->cipher.aesgcm_dec.authTagSz, + info->cipher.aesgcm_dec.authIn, + info->cipher.aesgcm_dec.authInSz); + } +# endif /* HAVE_AES_DECRYPT */ + } +# endif /* HAVE_AESGCM */ +#endif /* !NO_AES */ + } + else if (info->algo_type == WC_ALGO_TYPE_HASH) + { +#if !defined(NO_SHA256) || defined(WOLFSSL_SHA512) + if (0) { + /* nothing */ + } +# ifndef NO_SHA256 + else if (info->hash.type == WC_HASH_TYPE_SHA256) { + if ((info->hash.sha256->flags & WC_HASH_FLAG_ISCOPY) == 0) { + ret = ti_sa2ul_Sha256Hash(info->hash.sha256, + info->hash.in, + info->hash.inSz, + info->hash.digest); + } + } +# endif /* !NO_SHA256 */ +# ifdef WOLFSSL_SHA512 + else if (info->hash.type == WC_HASH_TYPE_SHA512) { + if (info->hash.sha512->hashType == WC_HASH_TYPE_SHA512 && + (info->hash.sha512->flags & WC_HASH_FLAG_ISCOPY) == 0) { + ret = ti_sa2ul_Sha512Hash(info->hash.sha512, + info->hash.in, + info->hash.inSz, + info->hash.digest); + } + } +# endif /* WOLFSSL_SHA512 */ +#endif /* !NO_SHA256 || WOLFSSL_SHA512 */ + } +#ifndef WC_NO_RNG + else if (info->algo_type == WC_ALGO_TYPE_SEED) + { + ret = ti_sa2ul_trng_get(info->seed.os, + info->seed.seed, + info->seed.sz); + } +#endif /* WC_NO_RNG */ + + return ret; +} + +int ti_sa2ul_port_init(void) +{ + int ret = WC_HW_E; + +#ifndef WC_NO_RNG + ti_sa2ul_trng_init(); +#endif /* WC_NO_RNG */ + + handle = Crypto_open(&cryptoCtx); + if (handle != NULL) { + ret = wc_CryptoCb_RegisterDevice(WOLFSSL_TI_SA2UL_DEVID, + ti_sa2ul_CryptoDevCb, NULL); + } + return ret; +} + +#endif /* WOLFSSL_TI_AM64X */ \ No newline at end of file diff --git a/wolfcrypt/src/random.c b/wolfcrypt/src/random.c index e65182dffc0..d686e1db020 100644 --- a/wolfcrypt/src/random.c +++ b/wolfcrypt/src/random.c @@ -216,6 +216,9 @@ This library contains implementation for the random number generator. #include "cyhal_trng.h" /* Infineon/Cypress HAL RNG implementation */ #elif defined(WOLFSSL_MAX3266X) || defined(WOLFSSL_MAX3266X_OLD) #include "wolfssl/wolfcrypt/port/maxim/max3266x.h" +#elif defined(WOLFSSL_TI_AM64X) + #include +#elif defined(__ti__) /* ti clang toolchain */ #else #include #if defined(WOLFSSL_GETRANDOM) || defined(HAVE_GETRANDOM) diff --git a/wolfcrypt/src/sha256.c b/wolfcrypt/src/sha256.c index 4c3d4fd2ebb..b0db793cc65 100644 --- a/wolfcrypt/src/sha256.c +++ b/wolfcrypt/src/sha256.c @@ -334,6 +334,10 @@ static int InitSha256(wc_Sha256* sha256) sha256->hSession = NULL; #endif +#ifdef WOLFSSL_TI_AM64X + XMEMSET(&sha256->scObj, 0, sizeof(sha256->scObj)); +#endif + return 0; } #endif diff --git a/wolfcrypt/src/sha512.c b/wolfcrypt/src/sha512.c index 311ff37e436..d0f35b5955c 100644 --- a/wolfcrypt/src/sha512.c +++ b/wolfcrypt/src/sha512.c @@ -962,6 +962,10 @@ static int InitSha512(wc_Sha512* sha512) #if defined(WOLFSSL_SHA512_HASHTYPE) sha512->hashType = WC_HASH_TYPE_SHA512; #endif /* WOLFSSL_SHA512_HASHTYPE */ + +#ifdef WOLFSSL_TI_AM64X + XMEMSET(&sha512->scObj, 0, sizeof(sha512->scObj)); +#endif return 0; } diff --git a/wolfcrypt/src/wc_port.c b/wolfcrypt/src/wc_port.c index 99be1cb3d96..479eefc8322 100644 --- a/wolfcrypt/src/wc_port.c +++ b/wolfcrypt/src/wc_port.c @@ -190,6 +190,10 @@ Threading/Mutex options: #include #endif +#ifdef WOLFSSL_TI_AM64X + #include +#endif + #ifdef WOLF_CRYPTO_CB #include #endif @@ -592,6 +596,14 @@ int wolfCrypt_Init(void) } #endif + #if defined(WOLFSSL_TI_AM64X) + ret = ti_sa2ul_port_init(); + if (ret != 0) { + WOLFSSL_MSG("TI AM64x Init Failed"); + WOLFCRYPT_INIT_RAISE_BAD_STATE(); + } + #endif + #if defined(WOLFSSL_ATMEL) || defined(WOLFSSL_ATECC508A) || \ defined(WOLFSSL_ATECC608A) || defined(WOLFSSL_MICROCHIP_TA100) ret = atmel_init(); diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index 5f7047ac792..3fb3cccc376 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -326,6 +326,11 @@ static const byte const_byte_array[] = "A+Gd\0\0\0"; if ((_i & 7) != 0) WOLFSSL_DEBUG_PRINTF("\n"); \ } while(0) +#ifdef TI_MCU_PLUS_SDK +# include "kernel/nortos/dpl/common/printf.h" +# define printf printf_ +#endif + #include #include #include @@ -486,6 +491,10 @@ static const byte const_byte_array[] = "A+Gd\0\0\0"; #endif #endif +#ifdef WOLFSSL_TI_AM64X + #include +#endif + #ifdef _MSC_VER /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */ #pragma warning(disable: 4996) diff --git a/wolfssl/wolfcrypt/aes.h b/wolfssl/wolfcrypt/aes.h index 183aad072f8..502725b3e9b 100644 --- a/wolfssl/wolfcrypt/aes.h +++ b/wolfssl/wolfcrypt/aes.h @@ -167,6 +167,10 @@ WOLFSSL_LOCAL void WC_ARG_NOT_NULL(1) GHASH(Gcm* gcm, const byte* a, #include "cy_crypto_common.h" #endif /* WOLFSSL_PSOC6_CRYPTO */ +#ifdef WOLFSSL_TI_AM64X + #include "security/security_common/drivers/crypto/crypto.h" +#endif + #ifdef __cplusplus extern "C" { #endif @@ -454,6 +458,9 @@ struct Aes { cy_stc_crypto_aes_gcm_state_t aes_gcm_state; #endif #endif /* WOLFSSL_PSOC6_CRYPTO */ +#ifdef WOLFSSL_TI_AM64X + XALIGNED(16) SA2UL_ContextObject scObj; +#endif }; #ifndef WC_AES_TYPE_DEFINED diff --git a/wolfssl/wolfcrypt/port/ti/ti-sa2ul_port.h b/wolfssl/wolfcrypt/port/ti/ti-sa2ul_port.h new file mode 100644 index 00000000000..e8b1c4cc527 --- /dev/null +++ b/wolfssl/wolfcrypt/port/ti/ti-sa2ul_port.h @@ -0,0 +1,33 @@ +/* ti-sa2ul_port.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 + */ +#ifndef _TI_SA2UL_PORT_H_ +#define _TI_SA2UL_PORT_H_ + +#if defined(WOLFSSL_TI_AM64X) + +#define WOLFSSL_TI_SA2UL_DEVID 8888 +#define WC_USE_DEVID WOLFSSL_TI_SA2UL_DEVID + +int ti_sa2ul_port_init(void); + +#endif /* WOLFSSL_TI_AM64X */ + +#endif /* _TI_SA2UL_PORT_H_ */ \ No newline at end of file diff --git a/wolfssl/wolfcrypt/settings.h b/wolfssl/wolfcrypt/settings.h index 696ece03230..afe76a73927 100644 --- a/wolfssl/wolfcrypt/settings.h +++ b/wolfssl/wolfcrypt/settings.h @@ -2458,6 +2458,20 @@ #define NO_WOLFSSL_SHA256_INTERLEAVE #endif +#ifdef WOLFSSL_TI_AM64X + #define HAVE_AES_ECB + #define WOLFSSL_AES_128 + #define NO_AES_192 + #define WOLFSSL_AES_256 + #define WOLFSSL_AES_DIRECT + #define WOLFSSL_CMAC + #define WOLFSSL_SHA512 + #ifndef WOLF_CRYPTO_CB + #define WOLF_CRYPTO_CB + #endif + #define WOLFSSL_SHA512_HASHTYPE +#endif + #ifdef FREESCALE_LTC_TFM_RSA_4096_ENABLE #undef USE_CERT_BUFFERS_4096 #define USE_CERT_BUFFERS_4096 diff --git a/wolfssl/wolfcrypt/sha256.h b/wolfssl/wolfcrypt/sha256.h index 4305d3078ca..b2a961d1fe3 100644 --- a/wolfssl/wolfcrypt/sha256.h +++ b/wolfssl/wolfcrypt/sha256.h @@ -167,6 +167,10 @@ #include "mcapi_error.h" #endif +#ifdef WOLFSSL_TI_AM64X + #include "security/security_common/drivers/crypto/sa2ul/sa2ul.h" +#endif + /* wc_Sha256 digest */ struct wc_Sha256 { @@ -251,6 +255,9 @@ struct wc_Sha256 { #ifdef WOLFSSL_HASH_FLAGS word32 flags; /* enum wc_HashFlags in hash.h */ #endif +#ifdef WOLFSSL_TI_AM64X + XALIGNED(16) SA2UL_ContextObject scObj; +#endif }; #ifndef WC_SHA256_TYPE_DEFINED diff --git a/wolfssl/wolfcrypt/sha512.h b/wolfssl/wolfcrypt/sha512.h index 5ee250a32be..069fd3ca8d7 100644 --- a/wolfssl/wolfcrypt/sha512.h +++ b/wolfssl/wolfcrypt/sha512.h @@ -147,6 +147,10 @@ #if defined(WOLFSSL_MAX3266X) || defined(WOLFSSL_MAX3266X_OLD) #include "wolfssl/wolfcrypt/port/maxim/max3266x.h" #endif +#ifdef WOLFSSL_TI_AM64X + #include "security/security_common/drivers/crypto/sa2ul/sa2ul.h" +#endif + /* wc_Sha512 digest */ struct wc_Sha512 { #if defined(PSOC6_HASH_SHA2) @@ -214,6 +218,9 @@ struct wc_Sha512 { int hashType; /* used to determine which SHA512 is used */ #endif /* WOLFSSL_SHA512_HASHTYPE */ #endif /* WOLFSSL_PSOC6_CRYPTO */ +#ifdef WOLFSSL_TI_AM64X + XALIGNED(16) SA2UL_ContextObject scObj; +#endif }; diff --git a/wolfssl/wolfcrypt/types.h b/wolfssl/wolfcrypt/types.h index 8ae2f274429..0f47023a0cd 100644 --- a/wolfssl/wolfcrypt/types.h +++ b/wolfssl/wolfcrypt/types.h @@ -2218,7 +2218,7 @@ WOLFSSL_API word32 CheckRunTimeSettings(void); #define PRAGMA_GCC_DIAG_POP /* null expansion */ #endif -#ifdef __clang__ +#if defined(__clang__) && !defined(__ti__) #define PRAGMA_CLANG_DIAG_PUSH _Pragma("clang diagnostic push") #define PRAGMA_CLANG(str) _Pragma(str) #define PRAGMA_CLANG_DIAG_POP _Pragma("clang diagnostic pop") @@ -2231,6 +2231,7 @@ WOLFSSL_API word32 CheckRunTimeSettings(void); #define PRAGMA_CLANG_DIAG_POP /* null expansion */ #endif +#ifndef __ti__ #ifndef PRAGMA_DIAG_PUSH #define PRAGMA_DIAG_PUSH /* null expansion */ #endif @@ -2240,6 +2241,7 @@ WOLFSSL_API word32 CheckRunTimeSettings(void); #ifndef PRAGMA_DIAG_POP #define PRAGMA_DIAG_POP /* null expansion */ #endif +#endif /* !__ti__ */ #define WC_CPP_CAT4_(a, b, c, d) a ## b ## c ## d #define WC_CPP_CAT4(a, b, c, d) WC_CPP_CAT4_(a, b, c, d) From 44a968dce336b61b796995b7d990f0f4eb60d261 Mon Sep 17 00:00:00 2001 From: Thomas Cook Date: Mon, 10 Aug 2026 11:46:17 -0400 Subject: [PATCH 2/3] macros and files --- .wolfssl_known_macro_extras | 1 + wolfcrypt/src/include.am | 1 + wolfssl/wolfcrypt/include.am | 1 + 3 files changed, 3 insertions(+) diff --git a/.wolfssl_known_macro_extras b/.wolfssl_known_macro_extras index 814001c990c..cf04e9dc790 100644 --- a/.wolfssl_known_macro_extras +++ b/.wolfssl_known_macro_extras @@ -1028,6 +1028,7 @@ WOLFSSL_STSAFE_TAKES_SLOT WOLFSSL_TELIT_M2MB WOLFSSL_TEMPLATE_EXAMPLE WOLFSSL_THREADED_CRYPT +WOLFSSL_TI_AM64X WOLFSSL_TICKET_DECRYPT_NO_CREATE WOLFSSL_TICKET_ENC_AES128_GCM WOLFSSL_TICKET_ENC_AES256_CBC diff --git a/wolfcrypt/src/include.am b/wolfcrypt/src/include.am index fdd356e9aed..18c89a0e42e 100644 --- a/wolfcrypt/src/include.am +++ b/wolfcrypt/src/include.am @@ -80,6 +80,7 @@ EXTRA_DIST += wolfcrypt/src/port/ti/ti-aes.c \ wolfcrypt/src/port/ti/ti-des3.c \ wolfcrypt/src/port/ti/ti-hash.c \ wolfcrypt/src/port/ti/ti-ccm.c \ + wolfcrypt/src/port/ti/ti-sa2ul_port.c \ wolfcrypt/src/port/pic32/pic32mz-crypt.c \ wolfcrypt/src/port/nrf51.c \ wolfcrypt/src/port/aria/aria-crypt.c \ diff --git a/wolfssl/wolfcrypt/include.am b/wolfssl/wolfcrypt/include.am index d10b3cb3dd4..86a78357aa7 100644 --- a/wolfssl/wolfcrypt/include.am +++ b/wolfssl/wolfcrypt/include.am @@ -97,6 +97,7 @@ noinst_HEADERS+= \ wolfssl/wolfcrypt/port/pic32/pic32mz-crypt.h \ wolfssl/wolfcrypt/port/ti/ti-hash.h \ wolfssl/wolfcrypt/port/ti/ti-ccm.h \ + wolfssl/wolfcrypt/port/ti/ti-sa2ul_port.h \ wolfssl/wolfcrypt/port/nrf51.h \ wolfssl/wolfcrypt/port/nxp/ksdk_port.h \ wolfssl/wolfcrypt/port/nxp/dcp_port.h \ From f3a458b8cef7d3db5e637b40545bcf42f3dbd687 Mon Sep 17 00:00:00 2001 From: Thomas Cook Date: Mon, 10 Aug 2026 12:34:18 -0400 Subject: [PATCH 3/3] Fix check-source whitespace issues --- wolfcrypt/src/port/ti/ti-sa2ul_port.c | 22 +++++++++++----------- wolfssl/wolfcrypt/port/ti/ti-sa2ul_port.h | 2 +- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/wolfcrypt/src/port/ti/ti-sa2ul_port.c b/wolfcrypt/src/port/ti/ti-sa2ul_port.c index 23e7e09a87f..fcfadc28579 100644 --- a/wolfcrypt/src/port/ti/ti-sa2ul_port.c +++ b/wolfcrypt/src/port/ti/ti-sa2ul_port.c @@ -193,7 +193,7 @@ static int ti_sa2ul_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz byte tmp_iv[16]; SA2UL_ContextParams_init(&scParams); - + scParams.opType = SA2UL_OP_ENC; scParams.encAlg = SA2UL_ENC_ALG_AES; scParams.encMode = SA2UL_ENC_MODE_CBC; @@ -277,7 +277,7 @@ static int ti_sa2ul_AesEcbDecrypt(Aes* aes, byte* out, const byte* in, word32 sz SA2UL_ContextParams scParams; SA2UL_ContextParams_init(&scParams); - + scParams.opType = SA2UL_OP_ENC; scParams.encAlg = SA2UL_ENC_ALG_AES; scParams.encMode = SA2UL_ENC_MODE_ECB; @@ -398,7 +398,7 @@ static int ti_sa2ul_AesGcmEncrypt(Aes* aes, byte* out, xorbuf(authTag, scratch, authTagSz); } } - + return ret; } @@ -475,7 +475,7 @@ static int ti_sa2ul_AesGcmDecrypt(Aes* aes, byte* out, ret = WC_NO_ERR_TRACE(AES_GCM_AUTH_E); } } - + return ret; } #endif /* HAVE_AES_DECRYPT */ @@ -483,7 +483,7 @@ static int ti_sa2ul_AesGcmDecrypt(Aes* aes, byte* out, #endif /* !NO_AES */ -#if !defined(NO_SHA256) || defined(WOLFSSL_SHA512) +#if !defined(NO_SHA256) || defined(WOLFSSL_SHA512) /* The ti mcu plus sdk sa2ul driver requires an output buffer of at least * the size of the input buffer, and it will write data to it, though we don't * use the data. So, we consider this a scratch buffer, but it also limits @@ -504,7 +504,7 @@ static int ti_sa2ul_InitSha256_ctx(wc_Sha256* sha256) /* default length to all ff's, final will override when known */ scParams.inputLen = 0xffffffffUL; sha256->scObj.totalLengthInBytes = 0xffffffffUL; - + if (SA2UL_contextAlloc(cryptoCtx.drvHandle, &sha256->scObj, &scParams) != SystemP_SUCCESS) { @@ -520,9 +520,9 @@ static int ti_sa2ul_Sha256Free_ctx(wc_Sha256* sha256) { (void)SA2UL_contextFree(&sha256->scObj); XMEMSET(&sha256->scObj, 0, sizeof(sha256->scObj)); - + sa2ul_hash_in_use = 0; - + return 0; } @@ -637,7 +637,7 @@ static int ti_sa2ul_InitSha512_ctx(wc_Sha512* sha512) /* default length to all ff's, final will override when known */ scParams.inputLen = 0xffffffffUL; sha512->scObj.totalLengthInBytes = 0xffffffffUL; - + if (SA2UL_contextAlloc(cryptoCtx.drvHandle, &sha512->scObj, &scParams) != SystemP_SUCCESS) { @@ -650,7 +650,7 @@ static int ti_sa2ul_Sha512Free_ctx(wc_Sha512* sha512) { (void)SA2UL_contextFree(&sha512->scObj); XMEMSET(&sha512->scObj, 0, sizeof(sha512->scObj)); - + return 0; } @@ -924,4 +924,4 @@ int ti_sa2ul_port_init(void) return ret; } -#endif /* WOLFSSL_TI_AM64X */ \ No newline at end of file +#endif /* WOLFSSL_TI_AM64X */ diff --git a/wolfssl/wolfcrypt/port/ti/ti-sa2ul_port.h b/wolfssl/wolfcrypt/port/ti/ti-sa2ul_port.h index e8b1c4cc527..a0fc9d7662d 100644 --- a/wolfssl/wolfcrypt/port/ti/ti-sa2ul_port.h +++ b/wolfssl/wolfcrypt/port/ti/ti-sa2ul_port.h @@ -30,4 +30,4 @@ int ti_sa2ul_port_init(void); #endif /* WOLFSSL_TI_AM64X */ -#endif /* _TI_SA2UL_PORT_H_ */ \ No newline at end of file +#endif /* _TI_SA2UL_PORT_H_ */