From 052973cd38d010ed00b4e605dde210d214d1ac60 Mon Sep 17 00:00:00 2001 From: Alex Lanzano Date: Sun, 30 Aug 2026 15:07:38 -0400 Subject: [PATCH] Ensure the casper and hashcrypt accelerator functions are using mutexes --- wolfcrypt/src/port/nxp/README.md | 10 +- wolfcrypt/src/port/nxp/casper_port.c | 131 +++++++++----- wolfcrypt/src/port/nxp/hashcrypt_port.c | 222 ++++++++++++++++-------- wolfssl/wolfcrypt/wc_port.h | 12 +- 4 files changed, 261 insertions(+), 114 deletions(-) diff --git a/wolfcrypt/src/port/nxp/README.md b/wolfcrypt/src/port/nxp/README.md index f9145a2b659..25615b9f635 100644 --- a/wolfcrypt/src/port/nxp/README.md +++ b/wolfcrypt/src/port/nxp/README.md @@ -27,7 +27,15 @@ ensure this never happens, by properly managing the iv/counter in use. will use a fully software implementation. - When the HashCrypt engine is in use for SHA-1 or SHA-256, it must not be interrupted with another hash request or an AES request. The hash must be -completed before another operation is requested. +completed before another operation is requested. Only one SHA stream exists +at a time, so `NO_WOLFSSL_SHA256_INTERLEAVE` is set and a `wc_Sha256Update()` +after a `wc_Sha256GetHash()` restarts the stream. +- SHA-224 is unavailable when HashCrypt SHA is enabled. +- Hardware access is serialized with the wolfCrypt crypto HW mutex, enabled +automatically. A threaded build setting `WOLFSSL_CRYPT_HW_MUTEX` to 0 is +rejected at compile time; use `SINGLE_THREADED` instead. Being a software +mutex it does not arbitrate between the two cores -- do not drive either +accelerator from both. ### wolfSSL LPC55S69 Hardware Acceleration Enable diff --git a/wolfcrypt/src/port/nxp/casper_port.c b/wolfcrypt/src/port/nxp/casper_port.c index 1b35082f7f4..993bd86024e 100644 --- a/wolfcrypt/src/port/nxp/casper_port.c +++ b/wolfcrypt/src/port/nxp/casper_port.c @@ -23,16 +23,17 @@ #ifdef WOLFSSL_NXP_CASPER -#if defined(WOLFSSL_CRYPT_HW_MUTEX) && WOLFSSL_CRYPT_HW_MUTEX > 0 - #error WOLFSSL_CRYPT_HW_MUTEX=1 not supported yet -#endif - #include #include #include "fsl_casper.h" int wc_casper_init(void) { + int ret; + + if ((ret = wolfSSL_HwPkMutexInit()) != 0) + return ret; + CASPER_Init(CASPER); return 0; @@ -63,19 +64,22 @@ int casper_rsa_public_exptmod( if (exp_sz <= 0 || exp_sz > (int)sizeof(exp_buf)) return BAD_FUNC_ARG; + if ((res = wolfSSL_HwPkMutexLock()) != 0) + return res; + /* casper requires little endian format for inputs/outputs */ XMEMCPY(sig_buf, in, sig_sz); mp_reverse(sig_buf, sig_sz); if ((res = mp_to_unsigned_bin(&key->n, key_buf)) != MP_OKAY) - return res; + goto unlock; mp_reverse(key_buf, key_sz); XMEMSET(exp_buf, 0, sizeof(exp_buf)); if ((res = mp_to_unsigned_bin(&key->e, exp_buf + sizeof(exp_buf) - exp_sz)) != MP_OKAY) - return res; + goto unlock; exp = ((uint32_t)exp_buf[0] << 24) | ((uint32_t)exp_buf[1] << 16) | ((uint32_t)exp_buf[2] << 8) | ((uint32_t)exp_buf[3]); @@ -86,8 +90,12 @@ int casper_rsa_public_exptmod( XMEMCPY(out, out_buf, sig_sz); *outLen = inLen; + res = 0; - return 0; +unlock: + wolfSSL_HwPkMutexUnLock(); + + return res; } #endif /* !NO_RSA && WOLFSSL_NXP_CASPER_RSA_PUB_EXPTMOD */ @@ -105,64 +113,81 @@ int casper_ecc_mulmod( uint32_t X[CASPER_MAX_ECC_SIZE_BYTES / sizeof(uint32_t)] = { 0 }; uint32_t Y[CASPER_MAX_ECC_SIZE_BYTES / sizeof(uint32_t)] = { 0 }; int size; + int ret; if (!m || !P || !R) return BAD_FUNC_ARG; if (curve_id == ECC_SECP256R1) - { size = 32; - CASPER_ecc_init(kCASPER_ECC_P256); - } else if (curve_id == ECC_SECP384R1) - { size = 48; - CASPER_ecc_init(kCASPER_ECC_P384); - } else if (curve_id == ECC_SECP521R1) - { size = 66; - CASPER_ecc_init(kCASPER_ECC_P521); - } else return BAD_FUNC_ARG; + if ((ret = wolfSSL_HwPkMutexLock()) != 0) + return ret; + /* scalar */ if (mp_to_unsigned_bin(m, (unsigned char *)&M[0]) != MP_OKAY) - return MP_TO_E; + { + ret = MP_TO_E; + goto unlock; + } mp_reverse((unsigned char *)&M[0], size); /* point */ if (mp_to_unsigned_bin(P->x, (unsigned char *)&X[0]) != MP_OKAY) - return MP_TO_E; + { + ret = MP_TO_E; + goto unlock; + } mp_reverse((unsigned char *)&X[0], size); if (mp_to_unsigned_bin(P->y, (unsigned char *)&Y[0]) != MP_OKAY) - return MP_TO_E; + { + ret = MP_TO_E; + goto unlock; + } mp_reverse((unsigned char *)&Y[0], size); if (curve_id == ECC_SECP256R1) { + CASPER_ecc_init(kCASPER_ECC_P256); CASPER_ECC_SECP256R1_Mul(CASPER, X, Y, X, Y, (void *)M); } else if (curve_id == ECC_SECP384R1) { + CASPER_ecc_init(kCASPER_ECC_P384); CASPER_ECC_SECP384R1_Mul(CASPER, X, Y, X, Y, (void *)M); } else if (curve_id == ECC_SECP521R1) { + CASPER_ecc_init(kCASPER_ECC_P521); CASPER_ECC_SECP521R1_Mul(CASPER, X, Y, X, Y, (void *)M); } /* result */ mp_reverse((unsigned char *)&X[0], size); if (mp_read_unsigned_bin(R->x, (unsigned char *)&X[0], size) != MP_OKAY) - return MP_READ_E; + { + ret = MP_READ_E; + goto unlock; + } mp_reverse((unsigned char *)&Y[0], size); if (mp_read_unsigned_bin(R->y, (unsigned char *)&Y[0], size) != MP_OKAY) - return MP_READ_E; + { + ret = MP_READ_E; + goto unlock; + } mp_set(R->z, 1); + ret = 0; - return 0; +unlock: + wolfSSL_HwPkMutexUnLock(); + + return ret; } #endif /* HAVE_ECC && WOLFSSL_NXP_CASPER_ECC_MULMOD */ @@ -180,66 +205,82 @@ int casper_ecc_mul2add( uint32_t X2[CASPER_MAX_ECC_SIZE_BYTES / sizeof(uint32_t)] = { 0 }; uint32_t Y2[CASPER_MAX_ECC_SIZE_BYTES / sizeof(uint32_t)] = { 0 }; int size; + int ret; if (!m || !P || !n || !Q || !R) return BAD_FUNC_ARG; if (curve_id == ECC_SECP256R1) - { size = 32; - CASPER_ecc_init(kCASPER_ECC_P256); - } else if (curve_id == ECC_SECP384R1) - { size = 48; - CASPER_ecc_init(kCASPER_ECC_P384); - } else if (curve_id == ECC_SECP521R1) - { size = 66; - CASPER_ecc_init(kCASPER_ECC_P521); - } else return BAD_FUNC_ARG; + if ((ret = wolfSSL_HwPkMutexLock()) != 0) + return ret; + /* first scalar */ if (mp_to_unsigned_bin(m, (unsigned char *)&M[0]) != MP_OKAY) - return MP_TO_E; + { + ret = MP_TO_E; + goto unlock; + } mp_reverse((unsigned char *)&M[0], size); /* first point */ if (mp_to_unsigned_bin(P->x, (unsigned char *)&X1[0]) != MP_OKAY) - return MP_TO_E; + { + ret = MP_TO_E; + goto unlock; + } mp_reverse((unsigned char *)&X1[0], size); if (mp_to_unsigned_bin(P->y, (unsigned char *)&Y1[0]) != MP_OKAY) - return MP_TO_E; + { + ret = MP_TO_E; + goto unlock; + } mp_reverse((unsigned char *)&Y1[0], size); /* second scalar */ if (mp_to_unsigned_bin(n, (unsigned char *)&N[0]) != MP_OKAY) - return MP_TO_E; + { + ret = MP_TO_E; + goto unlock; + } mp_reverse((unsigned char *)&N[0], size); /* second point */ if (mp_to_unsigned_bin(Q->x, (unsigned char *)&X2[0]) != MP_OKAY) - return MP_TO_E; + { + ret = MP_TO_E; + goto unlock; + } mp_reverse((unsigned char *)&X2[0], size); if (mp_to_unsigned_bin(Q->y, (unsigned char *)&Y2[0]) != MP_OKAY) - return MP_TO_E; + { + ret = MP_TO_E; + goto unlock; + } mp_reverse((unsigned char *)&Y2[0], size); if (curve_id == ECC_SECP256R1) { + CASPER_ecc_init(kCASPER_ECC_P256); CASPER_ECC_SECP256R1_MulAdd(CASPER, &X1[0], &Y1[0], &X1[0], &Y1[0], (void *)M, &X2[0], &Y2[0], (void *)N); } else if (curve_id == ECC_SECP384R1) { + CASPER_ecc_init(kCASPER_ECC_P384); CASPER_ECC_SECP384R1_MulAdd(CASPER, &X1[0], &Y1[0], &X1[0], &Y1[0], (void *)M, &X2[0], &Y2[0], (void *)N); } else if (curve_id == ECC_SECP521R1) { + CASPER_ecc_init(kCASPER_ECC_P521); CASPER_ECC_SECP521R1_MulAdd(CASPER, &X1[0], &Y1[0], &X1[0], &Y1[0], (void *)M, &X2[0], &Y2[0], (void *)N); } @@ -247,13 +288,23 @@ int casper_ecc_mul2add( /* result */ mp_reverse((unsigned char *)&X1[0], size); if (mp_read_unsigned_bin(R->x, (unsigned char *)&X1[0], size) != MP_OKAY) - return MP_READ_E; + { + ret = MP_READ_E; + goto unlock; + } mp_reverse((unsigned char *)&Y1[0], size); if (mp_read_unsigned_bin(R->y, (unsigned char *)&Y1[0], size) != MP_OKAY) - return MP_READ_E; + { + ret = MP_READ_E; + goto unlock; + } mp_set(R->z, 1); + ret = 0; - return 0; +unlock: + wolfSSL_HwPkMutexUnLock(); + + return ret; } #endif /* HAVE_ECC && WOLFSSL_NXP_CASPER_ECC_MUL2ADD */ diff --git a/wolfcrypt/src/port/nxp/hashcrypt_port.c b/wolfcrypt/src/port/nxp/hashcrypt_port.c index 0b145f538f5..d1a21f47082 100644 --- a/wolfcrypt/src/port/nxp/hashcrypt_port.c +++ b/wolfcrypt/src/port/nxp/hashcrypt_port.c @@ -23,9 +23,7 @@ #ifdef WOLFSSL_NXP_HASHCRYPT -#if defined(WOLFSSL_CRYPT_HW_MUTEX) && WOLFSSL_CRYPT_HW_MUTEX > 0 - #error WOLFSSL_CRYPT_HW_MUTEX=1 not supported yet -#endif +/* AES and SHA share one engine, so both take the global crypt HW mutex */ #include #include @@ -48,6 +46,11 @@ int wc_hashcrypt_init(void) #if ((!defined(NO_SHA) || !defined(NO_SHA256)) && \ defined(WOLFSSL_NXP_HASHCRYPT_SHA)) || \ (!defined(NO_AES) && defined(WOLFSSL_NXP_HASHCRYPT_AES)) + int ret; + + if ((ret = wolfSSL_CryptHwMutexInit()) != 0) + return ret; + HASHCRYPT_Init(HASHCRYPT); #endif return 0; @@ -56,6 +59,8 @@ int wc_hashcrypt_init(void) #if !defined(NO_SHA256) && defined(WOLFSSL_NXP_HASHCRYPT_SHA) int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId) { + int ret; + (void)heap; (void)devId; @@ -63,44 +68,65 @@ int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId) return BAD_FUNC_ARG; XMEMSET(sha256, 0, sizeof(wc_Sha256)); + + if ((ret = wolfSSL_CryptHwMutexLock()) != 0) + return ret; + if (HASHCRYPT_SHA_Init(HASHCRYPT, &hash_ctx, kHASHCRYPT_Sha256) != kStatus_Success) - return WC_HW_E; + ret = WC_HW_E; + else + finish_called = 0; - finish_called = 0; + wolfSSL_CryptHwMutexUnLock(); - return 0; + return ret; } int wc_Sha256Update(wc_Sha256* sha256, const byte* data, word32 len) { + int ret = 0; + if (sha256 == NULL || (data == NULL && len != 0)) return BAD_FUNC_ARG; + if ((ret = wolfSSL_CryptHwMutexLock()) != 0) + return ret; + if (finish_called) { - HASHCRYPT_SHA_Init(HASHCRYPT, &hash_ctx, kHASHCRYPT_Sha256); - finish_called = 0; + if (HASHCRYPT_SHA_Init(HASHCRYPT, &hash_ctx, kHASHCRYPT_Sha256) + != kStatus_Success) + ret = WC_HW_E; + else + finish_called = 0; } - if (HASHCRYPT_SHA_Update(HASHCRYPT, &hash_ctx, data, len) + if (ret == 0 && + HASHCRYPT_SHA_Update(HASHCRYPT, &hash_ctx, data, len) != kStatus_Success) - return WC_HW_E; + ret = WC_HW_E; - return 0; + wolfSSL_CryptHwMutexUnLock(); + + return ret; } int wc_Sha256Final(wc_Sha256* sha256, byte* hash) { size_t outlen = WC_SHA256_DIGEST_SIZE; static byte previous_sha256_hash[WC_SHA256_DIGEST_SIZE]; + int ret = 0; if (sha256 == NULL || hash == NULL) return BAD_FUNC_ARG; + if ((ret = wolfSSL_CryptHwMutexLock()) != 0) + return ret; + if (finish_called) { memcpy(hash, previous_sha256_hash, WC_SHA256_DIGEST_SIZE); - return 0; + goto unlock; } if ( @@ -109,12 +135,16 @@ int wc_Sha256Final(wc_Sha256* sha256, byte* hash) || outlen != WC_SHA256_DIGEST_SIZE ) { - return WC_HW_E; + ret = WC_HW_E; + goto unlock; } memcpy(previous_sha256_hash, hash, WC_SHA256_DIGEST_SIZE); finish_called = 1; - return 0; +unlock: + wolfSSL_CryptHwMutexUnLock(); + + return ret; } #endif /* !defined(NO_SHA256) && defined(WOLFSSL_NXP_HASHCRYPT_SHA) */ @@ -122,6 +152,8 @@ int wc_Sha256Final(wc_Sha256* sha256, byte* hash) #if !defined(NO_SHA) && defined(WOLFSSL_NXP_HASHCRYPT_SHA) int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId) { + int ret; + (void)heap; (void)devId; @@ -129,44 +161,65 @@ int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId) return BAD_FUNC_ARG; XMEMSET(sha, 0, sizeof(wc_Sha)); + + if ((ret = wolfSSL_CryptHwMutexLock()) != 0) + return ret; + if (HASHCRYPT_SHA_Init(HASHCRYPT, &hash_ctx, kHASHCRYPT_Sha1) != kStatus_Success) - return WC_HW_E; + ret = WC_HW_E; + else + finish_called = 0; - finish_called = 0; + wolfSSL_CryptHwMutexUnLock(); - return 0; + return ret; } int wc_ShaUpdate(wc_Sha* sha, const byte* data, word32 len) { + int ret = 0; + if (sha == NULL || (data == NULL && len != 0)) return BAD_FUNC_ARG; + if ((ret = wolfSSL_CryptHwMutexLock()) != 0) + return ret; + if (finish_called) { - HASHCRYPT_SHA_Init(HASHCRYPT, &hash_ctx, kHASHCRYPT_Sha1); - finish_called = 0; + if (HASHCRYPT_SHA_Init(HASHCRYPT, &hash_ctx, kHASHCRYPT_Sha1) + != kStatus_Success) + ret = WC_HW_E; + else + finish_called = 0; } - if (HASHCRYPT_SHA_Update(HASHCRYPT, &hash_ctx, data, len) + if (ret == 0 && + HASHCRYPT_SHA_Update(HASHCRYPT, &hash_ctx, data, len) != kStatus_Success) - return WC_HW_E; + ret = WC_HW_E; - return 0; + wolfSSL_CryptHwMutexUnLock(); + + return ret; } int wc_ShaFinal(wc_Sha* sha, byte* hash) { size_t outlen = WC_SHA_DIGEST_SIZE; static byte previous_sha_hash[WC_SHA_DIGEST_SIZE]; + int ret = 0; if (sha == NULL || hash == NULL) return BAD_FUNC_ARG; + if ((ret = wolfSSL_CryptHwMutexLock()) != 0) + return ret; + if (finish_called) { memcpy(hash, previous_sha_hash, WC_SHA_DIGEST_SIZE); - return 0; + goto unlock; } if ( @@ -175,12 +228,16 @@ int wc_ShaFinal(wc_Sha* sha, byte* hash) || outlen != WC_SHA_DIGEST_SIZE ) { - return WC_HW_E; + ret = WC_HW_E; + goto unlock; } memcpy(previous_sha_hash, hash, WC_SHA_DIGEST_SIZE); finish_called = 1; - return 0; +unlock: + wolfSSL_CryptHwMutexUnLock(); + + return ret; } #endif /* !defined(NO_SHA) && defined(WOLFSSL_NXP_HASHCRYPT_SHA) */ @@ -219,15 +276,18 @@ int wc_AesEcbEncrypt(Aes* aes, byte* out, const byte* in, word32 sz) if (sz == 0) return 0; - ret = _hashcrypt_set_key(aes); - if (ret) + if ((ret = wolfSSL_CryptHwMutexLock()) != 0) return ret; - if (HASHCRYPT_AES_EncryptEcb(HASHCRYPT, &aes_handle, in, out, sz) + ret = _hashcrypt_set_key(aes); + if (ret == 0 && + HASHCRYPT_AES_EncryptEcb(HASHCRYPT, &aes_handle, in, out, sz) != kStatus_Success) - return WC_HW_E; + ret = WC_HW_E; - return 0; + wolfSSL_CryptHwMutexUnLock(); + + return ret; } #ifdef HAVE_AES_DECRYPT @@ -241,15 +301,18 @@ int wc_AesEcbDecrypt(Aes* aes, byte* out, const byte* in, word32 sz) if (sz == 0) return 0; - ret = _hashcrypt_set_key(aes); - if (ret) + if ((ret = wolfSSL_CryptHwMutexLock()) != 0) return ret; - if (HASHCRYPT_AES_DecryptEcb(HASHCRYPT, &aes_handle, in, out, sz) + ret = _hashcrypt_set_key(aes); + if (ret == 0 && + HASHCRYPT_AES_DecryptEcb(HASHCRYPT, &aes_handle, in, out, sz) != kStatus_Success) - return WC_HW_E; + ret = WC_HW_E; - return 0; + wolfSSL_CryptHwMutexUnLock(); + + return ret; } #endif #endif /* HAVE_AES_ECB */ @@ -265,18 +328,22 @@ int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz) if (sz == 0) return 0; - ret = _hashcrypt_set_key(aes); - if (ret) + if ((ret = wolfSSL_CryptHwMutexLock()) != 0) return ret; - if (HASHCRYPT_AES_EncryptCbc( - HASHCRYPT, &aes_handle, in, out, sz, (const uint8_t *)aes->reg) - != kStatus_Success) - return WC_HW_E; + ret = _hashcrypt_set_key(aes); + if (ret == 0) { + if (HASHCRYPT_AES_EncryptCbc( + HASHCRYPT, &aes_handle, in, out, sz, (const uint8_t *)aes->reg) + != kStatus_Success) + ret = WC_HW_E; + else + XMEMCPY(aes->reg, out + sz - 16, 16); + } - XMEMCPY(aes->reg, out + sz - 16, 16); + wolfSSL_CryptHwMutexUnLock(); - return 0; + return ret; } #ifdef HAVE_AES_DECRYPT @@ -291,20 +358,24 @@ int wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz) if (sz == 0) return 0; - ret = _hashcrypt_set_key(aes); - if (ret) + if ((ret = wolfSSL_CryptHwMutexLock()) != 0) return ret; - XMEMCPY(tmp_iv, in + sz - 16, 16); + ret = _hashcrypt_set_key(aes); + if (ret == 0) { + XMEMCPY(tmp_iv, in + sz - 16, 16); - if (HASHCRYPT_AES_DecryptCbc( - HASHCRYPT, &aes_handle, in, out, sz, (const uint8_t *)aes->reg) - != kStatus_Success) - return WC_HW_E; + if (HASHCRYPT_AES_DecryptCbc( + HASHCRYPT, &aes_handle, in, out, sz, (const uint8_t *)aes->reg) + != kStatus_Success) + ret = WC_HW_E; + else + XMEMCPY(aes->reg, tmp_iv, 16); + } - XMEMCPY(aes->reg, tmp_iv, 16); + wolfSSL_CryptHwMutexUnLock(); - return 0; + return ret; } #endif #endif /* HAVE_AES_CBC */ @@ -326,17 +397,19 @@ static int _hashcrypt_aes_ofb(Aes* aes, byte* out, const byte* in, word32 sz) if (sz == 0) return 0; - ret = _hashcrypt_set_key(aes); - if (ret) + if ((ret = wolfSSL_CryptHwMutexLock()) != 0) return ret; - while (sz > 0) { + ret = _hashcrypt_set_key(aes); + while (ret == 0 && sz > 0) { if (aes->left == 0) { /* keystream block O = E(reg); OFB feeds O back as the next reg */ if (HASHCRYPT_AES_EncryptEcb(HASHCRYPT, &aes_handle, (const uint8_t *)aes->reg, (uint8_t *)aes->tmp, - WC_AES_BLOCK_SIZE) != kStatus_Success) - return WC_HW_E; + WC_AES_BLOCK_SIZE) != kStatus_Success) { + ret = WC_HW_E; + break; + } XMEMCPY(aes->reg, aes->tmp, WC_AES_BLOCK_SIZE); aes->left = WC_AES_BLOCK_SIZE; } @@ -348,7 +421,9 @@ static int _hashcrypt_aes_ofb(Aes* aes, byte* out, const byte* in, word32 sz) } } - return 0; + wolfSSL_CryptHwMutexUnLock(); + + return ret; } int wc_AesOfbEncrypt(Aes* aes, byte* out, const byte* in, word32 sz) @@ -382,16 +457,18 @@ static int _hashcrypt_aes_cfb(Aes* aes, byte* out, const byte* in, word32 sz, if (sz == 0) return 0; - ret = _hashcrypt_set_key(aes); - if (ret) + if ((ret = wolfSSL_CryptHwMutexLock()) != 0) return ret; - while (sz > 0) { + ret = _hashcrypt_set_key(aes); + while (ret == 0 && sz > 0) { if (aes->left == 0) { if (HASHCRYPT_AES_EncryptEcb(HASHCRYPT, &aes_handle, (const uint8_t *)aes->reg, (uint8_t *)aes->tmp, - WC_AES_BLOCK_SIZE) != kStatus_Success) - return WC_HW_E; + WC_AES_BLOCK_SIZE) != kStatus_Success) { + ret = WC_HW_E; + break; + } aes->left = WC_AES_BLOCK_SIZE; } idx = WC_AES_BLOCK_SIZE - aes->left; @@ -406,7 +483,9 @@ static int _hashcrypt_aes_cfb(Aes* aes, byte* out, const byte* in, word32 sz, aes->left--; } - return 0; + wolfSSL_CryptHwMutexUnLock(); + + return ret; } int wc_AesCfbEncrypt(Aes* aes, byte* out, const byte* in, word32 sz) @@ -425,7 +504,7 @@ int wc_AesCfbDecrypt(Aes* aes, byte* out, const byte* in, word32 sz) #ifdef WOLFSSL_AES_COUNTER int wc_AesCtrEncrypt(Aes* aes, byte* out, const byte* in, word32 sz) { - int ret; + int ret = 0; byte* tmp; if (aes == NULL || out == NULL || in == NULL) { @@ -441,18 +520,21 @@ int wc_AesCtrEncrypt(Aes* aes, byte* out, const byte* in, word32 sz) } if (sz) { - ret = _hashcrypt_set_key(aes); - if (ret) + if ((ret = wolfSSL_CryptHwMutexLock()) != 0) return ret; - if (HASHCRYPT_AES_CryptCtr( + ret = _hashcrypt_set_key(aes); + if (ret == 0 && + HASHCRYPT_AES_CryptCtr( HASHCRYPT, &aes_handle, in, out, sz, (byte *)aes->reg, (byte *)aes->tmp, (word32 *)&aes->left) != kStatus_Success) - return WC_HW_E; + ret = WC_HW_E; + + wolfSSL_CryptHwMutexUnLock(); } - return 0; + return ret; } #endif /* WOLFSSL_AES_COUNTER */ diff --git a/wolfssl/wolfcrypt/wc_port.h b/wolfssl/wolfcrypt/wc_port.h index 90359200e60..0813aa0c7e1 100644 --- a/wolfssl/wolfcrypt/wc_port.h +++ b/wolfssl/wolfcrypt/wc_port.h @@ -1017,12 +1017,13 @@ WOLFSSL_LOCAL void wolfSSL_RefWithMutexDec_IfEquals(wolfSSL_RefWithMutex* ref, #endif -/* Enable crypt HW mutex for Freescale MMCAU, PIC32MZ, STM32, MAX3266X or - * RTL8735B */ +/* Enable crypt HW mutex for Freescale MMCAU, PIC32MZ, STM32, MAX3266X, + * RTL8735B, NXP CASPER or NXP HashCrypt */ #if defined(FREESCALE_MMCAU) || defined(WOLFSSL_MICROCHIP_PIC32MZ) || \ defined(STM32_CRYPTO) || defined(STM32_HASH) || defined(STM32_RNG) || \ defined(WOLFSSL_MAX3266X) || defined(WOLFSSL_MAX3266X_OLD) || \ - defined(WOLFSSL_RTL8735B_HUK) || defined(WOLFSSL_SEC_QORIQ) + defined(WOLFSSL_RTL8735B_HUK) || defined(WOLFSSL_SEC_QORIQ) || \ + defined(WOLFSSL_NXP_CASPER) || defined(WOLFSSL_NXP_HASHCRYPT) #ifndef WOLFSSL_CRYPT_HW_MUTEX #define WOLFSSL_CRYPT_HW_MUTEX 1 #endif @@ -1032,6 +1033,11 @@ WOLFSSL_LOCAL void wolfSSL_RefWithMutexDec_IfEquals(wolfSSL_RefWithMutex* ref, #define WOLFSSL_CRYPT_HW_MUTEX 0 #endif +#if (defined(WOLFSSL_NXP_CASPER) || defined(WOLFSSL_NXP_HASHCRYPT)) && \ + !defined(SINGLE_THREADED) && (WOLFSSL_CRYPT_HW_MUTEX == 0) + #error "NXP CASPER/HashCrypt require WOLFSSL_CRYPT_HW_MUTEX unless SINGLE_THREADED" +#endif + #if WOLFSSL_CRYPT_HW_MUTEX /* wolfSSL_CryptHwMutexInit is called on first wolfSSL_CryptHwMutexLock, however it's recommended to call this directly on Hw init to avoid possible