Skip to content

Commit 1c9091e

Browse files
committed
Adds new STM32 Bare support for Hash, SAES/AES and PKA
1 parent 3351eb4 commit 1c9091e

8 files changed

Lines changed: 2505 additions & 46 deletions

File tree

.wolfssl_known_macro_extras

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
AES_CR_CCFC
12
AES_GCM_GMULT_NCT
3+
AES_ICR_CCF
4+
AES_ISR_CCF
5+
AES_SR_CCF
26
AFX_RESOURCE_DLL
37
AFX_TARG_ENU
48
ALLOW_BINARY_MISMATCH_INTROSPECTION
@@ -266,7 +270,11 @@ HARDWARE_CACHE_COHERENCY
266270
HASH_AlgoMode_HASH
267271
HASH_AlgoMode_HMAC
268272
HASH_BYTE_SWAP
273+
HASH_CR_ALGO_1
274+
HASH_CR_DATATYPE_0
275+
HASH_CR_DATATYPE_1
269276
HASH_CR_LKEY
277+
HASH_CR_MODE
270278
HASH_DIGEST
271279
HASH_DataType_8b
272280
HASH_IMR_DCIE
@@ -496,6 +504,14 @@ PTHREAD_STACK_MIN
496504
QAT_ENABLE_HASH
497505
QAT_ENABLE_RNG
498506
QAT_USE_POLLING_CHECK
507+
RCC_AHB1ENR_PKAEN
508+
RCC_AHB2ENR1_AESEN
509+
RCC_AHB2ENR1_HASHEN
510+
RCC_AHB2ENR1_PKAEN
511+
RCC_AHB2ENR_HASHEN
512+
RCC_AHB2ENR_PKAEN
513+
RCC_AHB2ENR_SAESEN
514+
RCC_AHB3ENR_AESEN
499515
RC_NO_RNG
500516
REDIRECTION_IN3_KEYELMID
501517
REDIRECTION_IN3_KEYID
@@ -678,6 +694,11 @@ WC_SLHDSA_KERNEL_ASM
678694
WC_SLHDSA_NO_ASM
679695
WC_SLHDSA_VERBOSE_DEBUG
680696
WC_SSIZE_TYPE
697+
WC_STM32_AES_CLK_ENABLE_INST
698+
WC_STM32_AES_INST
699+
WC_STM32_HAS_DHUK
700+
WC_STM32_SAES_CLK_DISABLE
701+
WC_STM32_SAES_CLK_ENABLE
681702
WC_STRICT_SIG
682703
WC_USE_PIE_FENCEPOSTS_FOR_FIPS
683704
WC_WANT_FLAG_DONT_USE_VECTOR_OPS
@@ -739,6 +760,9 @@ WOLFSSL_CLANG_TIDY
739760
WOLFSSL_CLIENT_EXAMPLE
740761
WOLFSSL_CONTIKI
741762
WOLFSSL_CRL_ALLOW_MISSING_CDP
763+
WOLFSSL_DHUK
764+
WOLFSSL_DHUK_DEVID
765+
WOLFSSL_DHUK_WRAPPED_DEVID
742766
WOLFSSL_DILITHIUM_ASSIGN_KEY
743767
WOLFSSL_DILITHIUM_NO_ASN1
744768
WOLFSSL_DILITHIUM_NO_CHECK_KEY
@@ -895,6 +919,7 @@ WOLFSSL_RNG_USE_FULL_SEED
895919
WOLFSSL_RSA_CHECK_D_ON_DECRYPT
896920
WOLFSSL_RSA_DECRYPT_TO_0_LEN
897921
WOLFSSL_RW_THREADED
922+
WOLFSSL_SAES_DEVID
898923
WOLFSSL_SAKKE_SMALL
899924
WOLFSSL_SAKKE_SMALL_MODEXP
900925
WOLFSSL_SE050_AUTO_ERASE
@@ -920,8 +945,11 @@ WOLFSSL_SP_ARM32_UDIV
920945
WOLFSSL_SP_FAST_NCT_EXPTMOD
921946
WOLFSSL_SP_INT_SQR_VOLATILE
922947
WOLFSSL_STACK_CHECK
948+
WOLFSSL_STM32C5
923949
WOLFSSL_STM32F427_RNG
924950
WOLFSSL_STM32U5_DHUK
951+
WOLFSSL_STM32_BARE
952+
WOLFSSL_STM32_USE_SAES
925953
WOLFSSL_STRONGEST_HASH_SIG
926954
WOLFSSL_STSAFE_TAKES_SLOT
927955
WOLFSSL_TELIT_M2MB

wolfcrypt/src/aes.c

Lines changed: 75 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,16 @@ block cipher mechanism that uses n-bit binary string parameter key with 128-bits
227227
static WARN_UNUSED_RESULT int wc_AesEncrypt(
228228
Aes* aes, const byte* inBlock, byte* outBlock)
229229
{
230+
#ifdef WOLFSSL_STM32_BARE
231+
/* Bare-metal driver handles mutex, clock and key/IV internally. */
232+
#ifdef WOLFSSL_DHUK
233+
if (aes->devId == WOLFSSL_DHUK_WRAPPED_DEVID) {
234+
return wc_Stm32_Aes_DhukOp(aes, outBlock, inBlock,
235+
WC_AES_BLOCK_SIZE, 1 /* encrypt */);
236+
}
237+
#endif
238+
return wc_Stm32_Aes_Ecb(aes, outBlock, inBlock, WC_AES_BLOCK_SIZE, 1);
239+
#else
230240
int ret = 0;
231241
#ifdef WOLFSSL_STM32_CUBEMX
232242
CRYP_HandleTypeDef hcryp;
@@ -241,13 +251,13 @@ block cipher mechanism that uses n-bit binary string parameter key with 128-bits
241251
return ret;
242252
#endif
243253

244-
#ifdef WOLFSSL_STM32U5_DHUK
254+
#ifdef WOLFSSL_DHUK
245255
ret = wolfSSL_CryptHwMutexLock();
246256
if (ret != 0)
247257
return ret;
248258

249259
/* Handle making use of wrapped key */
250-
if (aes->devId == WOLFSSL_STM32U5_DHUK_WRAPPED_DEVID) {
260+
if (aes->devId == WOLFSSL_DHUK_WRAPPED_DEVID) {
251261
CRYP_ConfigTypeDef Config = {0};
252262

253263
ret = wc_Stm32_Aes_UnWrap(aes, &hcryp, (const byte*)aes->key,
@@ -367,6 +377,7 @@ block cipher mechanism that uses n-bit binary string parameter key with 128-bits
367377
wc_Stm32_Aes_Cleanup();
368378

369379
return ret;
380+
#endif /* !WOLFSSL_STM32_BARE */
370381
}
371382
#endif /* WOLFSSL_AES_DIRECT || HAVE_AESGCM || HAVE_AESCCM */
372383

@@ -375,6 +386,15 @@ block cipher mechanism that uses n-bit binary string parameter key with 128-bits
375386
static WARN_UNUSED_RESULT int wc_AesDecrypt(
376387
Aes* aes, const byte* inBlock, byte* outBlock)
377388
{
389+
#ifdef WOLFSSL_STM32_BARE
390+
#ifdef WOLFSSL_DHUK
391+
if (aes->devId == WOLFSSL_DHUK_WRAPPED_DEVID) {
392+
return wc_Stm32_Aes_DhukOp(aes, outBlock, inBlock,
393+
WC_AES_BLOCK_SIZE, 0 /* decrypt */);
394+
}
395+
#endif
396+
return wc_Stm32_Aes_Ecb(aes, outBlock, inBlock, WC_AES_BLOCK_SIZE, 0);
397+
#else
378398
int ret = 0;
379399
#ifdef WOLFSSL_STM32_CUBEMX
380400
CRYP_HandleTypeDef hcryp;
@@ -389,13 +409,13 @@ block cipher mechanism that uses n-bit binary string parameter key with 128-bits
389409
return ret;
390410
#endif
391411

392-
#ifdef WOLFSSL_STM32U5_DHUK
412+
#ifdef WOLFSSL_DHUK
393413
ret = wolfSSL_CryptHwMutexLock();
394414
if (ret != 0)
395415
return ret;
396416

397417
/* Handle making use of wrapped key */
398-
if (aes->devId == WOLFSSL_STM32U5_DHUK_WRAPPED_DEVID) {
418+
if (aes->devId == WOLFSSL_DHUK_WRAPPED_DEVID) {
399419
CRYP_ConfigTypeDef Config;
400420

401421
XMEMSET(&Config, 0, sizeof(Config));
@@ -521,6 +541,7 @@ block cipher mechanism that uses n-bit binary string parameter key with 128-bits
521541
wc_Stm32_Aes_Cleanup();
522542

523543
return ret;
544+
#endif /* !WOLFSSL_STM32_BARE */
524545
}
525546
#endif /* WOLFSSL_AES_DIRECT */
526547
#endif /* HAVE_AES_DECRYPT */
@@ -5576,7 +5597,34 @@ int wc_AesSetIV(Aes* aes, const byte* iv)
55765597
#ifdef HAVE_AES_CBC
55775598
#if defined(STM32_CRYPTO)
55785599

5579-
#ifdef WOLFSSL_STM32U5_DHUK
5600+
#ifdef WOLFSSL_STM32_BARE
5601+
int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
5602+
{
5603+
#ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
5604+
if (sz % WC_AES_BLOCK_SIZE) {
5605+
return BAD_LENGTH_E;
5606+
}
5607+
#endif
5608+
if (sz == 0) {
5609+
return 0;
5610+
}
5611+
return wc_Stm32_Aes_Cbc(aes, out, in, sz, 1);
5612+
}
5613+
#ifdef HAVE_AES_DECRYPT
5614+
int wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
5615+
{
5616+
#ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
5617+
if (sz % WC_AES_BLOCK_SIZE) {
5618+
return BAD_LENGTH_E;
5619+
}
5620+
#endif
5621+
if (sz == 0) {
5622+
return 0;
5623+
}
5624+
return wc_Stm32_Aes_Cbc(aes, out, in, sz, 0);
5625+
}
5626+
#endif /* HAVE_AES_DECRYPT */
5627+
#elif defined(WOLFSSL_DHUK)
55805628
int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
55815629
{
55825630
int ret = 0;
@@ -5596,7 +5644,7 @@ int wc_AesSetIV(Aes* aes, const byte* iv)
55965644
return ret;
55975645
}
55985646

5599-
if (aes->devId == WOLFSSL_STM32U5_DHUK_WRAPPED_DEVID) {
5647+
if (aes->devId == WOLFSSL_DHUK_WRAPPED_DEVID) {
56005648
CRYP_ConfigTypeDef Config;
56015649

56025650
XMEMSET(&Config, 0, sizeof(Config));
@@ -5662,7 +5710,7 @@ int wc_AesSetIV(Aes* aes, const byte* iv)
56625710
return ret;
56635711
}
56645712

5665-
if (aes->devId == WOLFSSL_STM32U5_DHUK_WRAPPED_DEVID) {
5713+
if (aes->devId == WOLFSSL_DHUK_WRAPPED_DEVID) {
56665714
CRYP_ConfigTypeDef Config;
56675715

56685716
XMEMSET(&Config, 0, sizeof(Config));
@@ -6956,6 +7004,11 @@ int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
69567004

69577005
int wc_AesCtrEncryptBlock(Aes* aes, byte* out, const byte* in)
69587006
{
7007+
#ifdef WOLFSSL_STM32_BARE
7008+
/* CTR per-block transform: ECB-encrypt the counter (passed in
7009+
* 'in'); aes.c handles counter increment and XOR with plaintext. */
7010+
return wc_Stm32_Aes_Ecb(aes, out, in, WC_AES_BLOCK_SIZE, 1);
7011+
#else
69597012
int ret = 0;
69607013
#ifdef WOLFSSL_STM32_CUBEMX
69617014
CRYP_HandleTypeDef hcryp;
@@ -7066,6 +7119,7 @@ int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
70667119
wolfSSL_CryptHwMutexUnLock();
70677120
wc_Stm32_Aes_Cleanup();
70687121
return ret;
7122+
#endif /* !WOLFSSL_STM32_BARE */
70697123
}
70707124

70717125

@@ -10142,6 +10196,15 @@ int wc_AesGcmEncrypt(Aes* aes, byte* out, const byte* in, word32 sz,
1014210196
authIn, authInSz);
1014310197
#endif
1014410198

10199+
#if defined(WOLFSSL_STM32_BARE) && defined(STM32_CRYPTO)
10200+
ret = wc_Stm32_Aes_Gcm(aes, out, in, sz, iv, ivSz,
10201+
authTag, authTagSz,
10202+
authIn, authInSz, 1 /* enc */);
10203+
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
10204+
return ret;
10205+
/* fall through to SW GCM (still uses HW AES via wc_AesEncrypt) */
10206+
#endif /* WOLFSSL_STM32_BARE && STM32_CRYPTO */
10207+
1014510208
#ifdef STM32_CRYPTO_AES_GCM
1014610209
return wc_AesGcmEncrypt_STM32(
1014710210
aes, out, in, sz, iv, ivSz,
@@ -10871,6 +10934,10 @@ int wc_AesGcmDecrypt(Aes* aes, byte* out, const byte* in, word32 sz,
1087110934

1087210935
#endif
1087310936

10937+
/* BARE: GCM decrypt always uses SW path (with HW AES blocks via
10938+
* wc_AesEncrypt). Encrypt is HW-accelerated above; decrypt + tag
10939+
* verification stays in well-tested SW for now. */
10940+
1087410941
#ifdef STM32_CRYPTO_AES_GCM
1087510942
/* The STM standard peripheral library API's doesn't support partial blocks */
1087610943
return wc_AesGcmDecrypt_STM32(
@@ -13695,7 +13762,7 @@ int wc_AesInit(Aes* aes, void* heap, int devId)
1369513762

1369613763
aes->heap = heap;
1369713764

13698-
#if defined(WOLF_CRYPTO_CB) || defined(WOLFSSL_STM32U5_DHUK)
13765+
#if defined(WOLF_CRYPTO_CB) || defined(WOLFSSL_DHUK)
1369913766
aes->devId = devId;
1370013767
aes->devCtx = NULL;
1370113768
#else

wolfcrypt/src/ecc.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,12 @@ ECC Curve Sizes:
286286
#if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
287287
!defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_SILABS_SE_ACCEL) && \
288288
!defined(WOLFSSL_KCAPI_ECC) && !defined(WOLFSSL_SE050) && \
289-
!defined(WOLFSSL_XILINX_CRYPT_VERSAL) && !defined(WOLFSSL_STM32_PKA) && \
289+
!defined(WOLFSSL_XILINX_CRYPT_VERSAL) && \
290+
!(defined(WOLFSSL_STM32_PKA) && !defined(WOLFSSL_STM32_BARE)) && \
290291
!defined(WOLFSSL_PSOC6_CRYPTO)
292+
/* WOLFSSL_STM32_BARE+PKA still uses the SW ECDSA helper paths
293+
* (sign/verify) since the bare-metal driver only implements ECCMul
294+
* HW; the SP-less SW ECDSA fallback then drives that HW. */
291295
#undef HAVE_ECC_VERIFY_HELPER
292296
#define HAVE_ECC_VERIFY_HELPER
293297
#endif
@@ -6947,7 +6951,12 @@ static int deterministic_sign_helper(const byte* in, word32 inlen, ecc_key* key)
69476951
#endif /* WOLFSSL_ECDSA_DETERMINISTIC_K ||
69486952
WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT */
69496953

6950-
#if defined(WOLFSSL_STM32_PKA)
6954+
/* Under WOLFSSL_STM32_BARE the bare-metal PKA driver implements only
6955+
* ECCMul HW (the building block used by ECDH and the SP-less SW ECDSA
6956+
* path). HW ECDSA sign/verify is intentionally not wired up in v1 of
6957+
* the bare driver -- fall back to the standard SW ECDSA which itself
6958+
* calls wc_ecc_mulmod_ex2() (HW-accelerated). */
6959+
#if defined(WOLFSSL_STM32_PKA) && !defined(WOLFSSL_STM32_BARE)
69516960
int wc_ecc_sign_hash_ex(const byte* in, word32 inlen, WC_RNG* rng,
69526961
ecc_key* key, mp_int *r, mp_int *s)
69536962
{
@@ -8763,7 +8772,8 @@ int wc_ecc_verify_hash(const byte* sig, word32 siglen, const byte* hash,
87638772

87648773
#ifndef WOLF_CRYPTO_CB_ONLY_ECC
87658774

8766-
#if !defined(WOLFSSL_STM32_PKA) && !defined(WOLFSSL_PSOC6_CRYPTO) && \
8775+
#if !(defined(WOLFSSL_STM32_PKA) && !defined(WOLFSSL_STM32_BARE)) && \
8776+
!defined(WOLFSSL_PSOC6_CRYPTO) && \
87678777
!defined(WOLF_CRYPTO_CB_ONLY_ECC)
87688778
static int wc_ecc_check_r_s_range(ecc_key* key, mp_int* r, mp_int* s)
87698779
{
@@ -9279,7 +9289,10 @@ static int ecc_verify_hash(mp_int *r, mp_int *s, const byte* hash,
92799289
int wc_ecc_verify_hash_ex(mp_int *r, mp_int *s, const byte* hash,
92809290
word32 hashlen, int* res, ecc_key* key)
92819291
{
9282-
#if defined(WOLFSSL_STM32_PKA)
9292+
#if defined(WOLFSSL_STM32_PKA) && !defined(WOLFSSL_STM32_BARE)
9293+
/* See comment above wc_ecc_sign_hash_ex(): BARE uses SW ECDSA
9294+
* verify which internally accelerates the scalar muls via the
9295+
* bare-metal HW wc_ecc_mulmod_ex2(). */
92839296
return stm32_ecc_verify_hash_ex(r, s, hash, hashlen, res, key);
92849297
#elif defined(WOLFSSL_PSOC6_CRYPTO)
92859298
return psoc6_ecc_verify_hash_ex(r, s, hash, hashlen, res, key);

0 commit comments

Comments
 (0)