Skip to content

Commit 54ada38

Browse files
committed
Adds new STM32 Bare support for Hash, SAES/AES and PKA
1 parent dfe03ff commit 54ada38

8 files changed

Lines changed: 2816 additions & 55 deletions

File tree

.wolfssl_known_macro_extras

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1+
AES
2+
AES1
3+
AES_CR_CCFC
14
AES_GCM_GMULT_NCT
5+
AES_ICR_CCF
6+
AES_ISR_CCF
7+
AES_SR_CCF
28
AFX_RESOURCE_DLL
39
AFX_TARG_ENU
410
ALLOW_BINARY_MISMATCH_INTROSPECTION
@@ -269,7 +275,11 @@ HARDWARE_CACHE_COHERENCY
269275
HASH_AlgoMode_HASH
270276
HASH_AlgoMode_HMAC
271277
HASH_BYTE_SWAP
278+
HASH_CR_ALGO_1
279+
HASH_CR_DATATYPE_0
280+
HASH_CR_DATATYPE_1
272281
HASH_CR_LKEY
282+
HASH_CR_MODE
273283
HASH_DIGEST
274284
HASH_DataType_8b
275285
HASH_IMR_DCIE
@@ -491,14 +501,35 @@ OTHER_BOARD
491501
O_CLOEXEC
492502
PEER_INFO
493503
PERF_FLAG_FD_CLOEXEC
504+
PKA_CLRFR_OPERRFC
505+
PKA_CR_OPERRIE
494506
PKA_ECC_SCALAR_MUL_IN_B_COEFF
507+
PKA_SR_OPERRF
495508
PLATFORMIO
496509
PLUTON_CRYPTO_ECC
497510
PRINT_SESSION_STATS
498511
PTHREAD_STACK_MIN
499512
QAT_ENABLE_HASH
500513
QAT_ENABLE_RNG
501514
QAT_USE_POLLING_CHECK
515+
RCC_AHB1ENR_PKAEN
516+
RCC_AHB2ENR1_AESEN
517+
RCC_AHB2ENR1_HASHEN
518+
RCC_AHB2ENR1_PKAEN
519+
RCC_AHB2ENR1_SAESEN
520+
RCC_AHB2ENR_AESEN
521+
RCC_AHB2ENR_HASHEN
522+
RCC_AHB2ENR_PKAEN
523+
RCC_AHB2ENR_SAESEN
524+
RCC_AHB3ENR_AESEN
525+
RCC_AHB3ENR_CRYPEN
526+
RCC_AHB3ENR_HASHEN
527+
RCC_AHB3ENR_PKAEN
528+
RCC_AHB3ENR_RNGEN
529+
RCC_AHB3ENR_SAESEN
530+
RCC_MP_AHB5ENSETR_CRYP1EN
531+
RCC_MP_AHB5ENSETR_HASH1EN
532+
RCC_MP_AHB5ENSETR_RNG1EN
502533
RC_NO_RNG
503534
REDIRECTION_IN3_KEYELMID
504535
REDIRECTION_IN3_KEYID
@@ -509,11 +540,18 @@ REDIRECTION_OUT2_KEYID
509540
RENESAS_T4_USE
510541
RHEL_MAJOR
511542
RHEL_RELEASE_CODE
543+
RNG_CAND_NIST_CR_VALUE
544+
RNG_CAND_NIST_HTCR_VALUE
545+
RNG_CAND_NIST_NSCR_VALUE
546+
RNG_CR_CONDRST
547+
RNG_SR_BUSY
512548
RTC_ALARMSUBSECONDMASK_ALL
513549
RTE_CMSIS_RTOS_RTX
514550
RTOS_MODULE_NET_AVAIL
515551
RTPLATFORM
516552
SAL_IOMMU_CODE
553+
SAES
554+
SAES_CR_EN
517555
SA_INTERRUPT
518556
SCEKEY_INSTALLED
519557
SHA256_MANY_REGISTERS
@@ -575,6 +613,7 @@ STM32WB55xx
575613
STM32WBA52xx
576614
STM32WL55xx
577615
STM32_AESGCM_PARTIAL
616+
STM32_AES_CLEAR_INST
578617
STM32_HW_CLOCK_AUTO
579618
STM32_NUTTX_RNG
580619
STSAFE_HOST_KEY_CIPHER
@@ -679,6 +718,12 @@ WC_SLHDSA_NO_ASM
679718
WC_SLHDSA_VERBOSE_DEBUG
680719
WC_SSIZE_TYPE
681720
WC_STRICT_SIG
721+
WC_STM32_HASH_INSTANCE_HRA
722+
WC_STM32_PKA_DIAG
723+
WC_STM32_RNG_CED_DISABLE
724+
WC_STM32_RNG_DIAG
725+
WC_STM32_RNG_NO_NIST_INIT
726+
WC_STM32_SAES_DIAG
682727
WC_USE_PIE_FENCEPOSTS_FOR_FIPS
683728
WC_WANT_FLAG_DONT_USE_VECTOR_OPS
684729
WIFIESPAT
@@ -918,9 +963,13 @@ WOLFSSL_SP_ARM32_UDIV
918963
WOLFSSL_SP_FAST_NCT_EXPTMOD
919964
WOLFSSL_SP_INT_SQR_VOLATILE
920965
WOLFSSL_STACK_CHECK
966+
WOLFSSL_STM32C5
967+
WOLFSSL_STM32F3
921968
WOLFSSL_STM32F427_RNG
922-
WOLFSSL_STM32U5_DHUK
923-
WOLFSSL_STM32_RNG_NOLIB
969+
WOLFSSL_STM32U0
970+
WOLFSSL_STM32_BARE
971+
WOLFSSL_STM32_DHUK_UNWRAP
972+
WOLFSSL_STM32_USE_SAES
924973
WOLFSSL_STRONGEST_HASH_SIG
925974
WOLFSSL_STSAFE_TAKES_SLOT
926975
WOLFSSL_TELIT_M2MB

wolfcrypt/src/aes.c

Lines changed: 78 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,16 @@ block cipher mechanism that uses n-bit binary string parameter key with 128-bits
233233
static WARN_UNUSED_RESULT int wc_AesEncrypt(
234234
Aes* aes, const byte* inBlock, byte* outBlock)
235235
{
236+
#ifdef WOLFSSL_STM32_BARE
237+
/* Bare-metal driver handles mutex, clock and key/IV internally. */
238+
#ifdef WOLFSSL_DHUK
239+
if (aes->devId == WOLFSSL_DHUK_WRAPPED_DEVID) {
240+
return wc_Stm32_Aes_DhukOp(aes, outBlock, inBlock,
241+
WC_AES_BLOCK_SIZE, 1 /* encrypt */);
242+
}
243+
#endif
244+
return wc_Stm32_Aes_Ecb(aes, outBlock, inBlock, WC_AES_BLOCK_SIZE, 1);
245+
#else
236246
int ret = 0;
237247
#ifdef WOLFSSL_STM32_CUBEMX
238248
CRYP_HandleTypeDef hcryp;
@@ -247,13 +257,13 @@ block cipher mechanism that uses n-bit binary string parameter key with 128-bits
247257
return ret;
248258
#endif
249259

250-
#ifdef WOLFSSL_STM32U5_DHUK
260+
#ifdef WOLFSSL_DHUK
251261
ret = wolfSSL_CryptHwMutexLock();
252262
if (ret != 0)
253263
return ret;
254264

255265
/* Handle making use of wrapped key */
256-
if (aes->devId == WOLFSSL_STM32U5_DHUK_WRAPPED_DEVID) {
266+
if (aes->devId == WOLFSSL_DHUK_WRAPPED_DEVID) {
257267
CRYP_ConfigTypeDef Config = {0};
258268

259269
ret = wc_Stm32_Aes_UnWrap(aes, &hcryp, (const byte*)aes->key,
@@ -373,6 +383,7 @@ block cipher mechanism that uses n-bit binary string parameter key with 128-bits
373383
wc_Stm32_Aes_Cleanup();
374384

375385
return ret;
386+
#endif /* !WOLFSSL_STM32_BARE */
376387
}
377388
#endif /* WOLFSSL_AES_DIRECT || HAVE_AESGCM || HAVE_AESCCM */
378389

@@ -381,6 +392,15 @@ block cipher mechanism that uses n-bit binary string parameter key with 128-bits
381392
static WARN_UNUSED_RESULT int wc_AesDecrypt(
382393
Aes* aes, const byte* inBlock, byte* outBlock)
383394
{
395+
#ifdef WOLFSSL_STM32_BARE
396+
#ifdef WOLFSSL_DHUK
397+
if (aes->devId == WOLFSSL_DHUK_WRAPPED_DEVID) {
398+
return wc_Stm32_Aes_DhukOp(aes, outBlock, inBlock,
399+
WC_AES_BLOCK_SIZE, 0 /* decrypt */);
400+
}
401+
#endif
402+
return wc_Stm32_Aes_Ecb(aes, outBlock, inBlock, WC_AES_BLOCK_SIZE, 0);
403+
#else
384404
int ret = 0;
385405
#ifdef WOLFSSL_STM32_CUBEMX
386406
CRYP_HandleTypeDef hcryp;
@@ -395,13 +415,13 @@ block cipher mechanism that uses n-bit binary string parameter key with 128-bits
395415
return ret;
396416
#endif
397417

398-
#ifdef WOLFSSL_STM32U5_DHUK
418+
#ifdef WOLFSSL_DHUK
399419
ret = wolfSSL_CryptHwMutexLock();
400420
if (ret != 0)
401421
return ret;
402422

403423
/* Handle making use of wrapped key */
404-
if (aes->devId == WOLFSSL_STM32U5_DHUK_WRAPPED_DEVID) {
424+
if (aes->devId == WOLFSSL_DHUK_WRAPPED_DEVID) {
405425
CRYP_ConfigTypeDef Config;
406426

407427
XMEMSET(&Config, 0, sizeof(Config));
@@ -527,6 +547,7 @@ block cipher mechanism that uses n-bit binary string parameter key with 128-bits
527547
wc_Stm32_Aes_Cleanup();
528548

529549
return ret;
550+
#endif /* !WOLFSSL_STM32_BARE */
530551
}
531552
#endif /* WOLFSSL_AES_DIRECT */
532553
#endif /* HAVE_AES_DECRYPT */
@@ -5594,7 +5615,34 @@ int wc_AesSetIV(Aes* aes, const byte* iv)
55945615
#ifdef HAVE_AES_CBC
55955616
#if defined(STM32_CRYPTO)
55965617

5597-
#ifdef WOLFSSL_STM32U5_DHUK
5618+
#ifdef WOLFSSL_STM32_BARE
5619+
int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
5620+
{
5621+
#ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
5622+
if (sz % WC_AES_BLOCK_SIZE) {
5623+
return BAD_LENGTH_E;
5624+
}
5625+
#endif
5626+
if (sz == 0) {
5627+
return 0;
5628+
}
5629+
return wc_Stm32_Aes_Cbc(aes, out, in, sz, 1);
5630+
}
5631+
#ifdef HAVE_AES_DECRYPT
5632+
int wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
5633+
{
5634+
#ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
5635+
if (sz % WC_AES_BLOCK_SIZE) {
5636+
return BAD_LENGTH_E;
5637+
}
5638+
#endif
5639+
if (sz == 0) {
5640+
return 0;
5641+
}
5642+
return wc_Stm32_Aes_Cbc(aes, out, in, sz, 0);
5643+
}
5644+
#endif /* HAVE_AES_DECRYPT */
5645+
#elif defined(WOLFSSL_DHUK)
55985646
int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
55995647
{
56005648
int ret = 0;
@@ -5614,7 +5662,7 @@ int wc_AesSetIV(Aes* aes, const byte* iv)
56145662
return ret;
56155663
}
56165664

5617-
if (aes->devId == WOLFSSL_STM32U5_DHUK_WRAPPED_DEVID) {
5665+
if (aes->devId == WOLFSSL_DHUK_WRAPPED_DEVID) {
56185666
CRYP_ConfigTypeDef Config;
56195667

56205668
XMEMSET(&Config, 0, sizeof(Config));
@@ -5680,7 +5728,7 @@ int wc_AesSetIV(Aes* aes, const byte* iv)
56805728
return ret;
56815729
}
56825730

5683-
if (aes->devId == WOLFSSL_STM32U5_DHUK_WRAPPED_DEVID) {
5731+
if (aes->devId == WOLFSSL_DHUK_WRAPPED_DEVID) {
56845732
CRYP_ConfigTypeDef Config;
56855733

56865734
XMEMSET(&Config, 0, sizeof(Config));
@@ -6977,6 +7025,11 @@ int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
69777025

69787026
int wc_AesCtrEncryptBlock(Aes* aes, byte* out, const byte* in)
69797027
{
7028+
#ifdef WOLFSSL_STM32_BARE
7029+
/* CTR per-block transform: ECB-encrypt the counter (passed in
7030+
* 'in'); aes.c handles counter increment and XOR with plaintext. */
7031+
return wc_Stm32_Aes_Ecb(aes, out, in, WC_AES_BLOCK_SIZE, 1);
7032+
#else
69807033
int ret = 0;
69817034
#ifdef WOLFSSL_STM32_CUBEMX
69827035
CRYP_HandleTypeDef hcryp;
@@ -7087,6 +7140,7 @@ int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
70877140
wolfSSL_CryptHwMutexUnLock();
70887141
wc_Stm32_Aes_Cleanup();
70897142
return ret;
7143+
#endif /* !WOLFSSL_STM32_BARE */
70907144
}
70917145

70927146

@@ -10166,6 +10220,7 @@ int wc_AesGcmEncrypt(Aes* aes, byte* out, const byte* in, word32 sz,
1016610220
authTag, authTagSz,
1016710221
authIn, authInSz);
1016810222
#endif
10223+
1016910224
#if defined(WOLFSSL_MICROCHIP_TA100) && defined(WOLFSSL_MICROCHIP_AESGCM)
1017010225
#ifndef TA_AES_GCM_MAX_DATA_SIZE
1017110226
#define TA_AES_GCM_MAX_DATA_SIZE 996u
@@ -10183,6 +10238,17 @@ int wc_AesGcmEncrypt(Aes* aes, byte* out, const byte* in, word32 sz,
1018310238
authIn, authInSz);
1018410239
}
1018510240
#endif
10241+
10242+
#if defined(WOLFSSL_STM32_BARE) && defined(STM32_CRYPTO)
10243+
ret = wc_Stm32_Aes_Gcm(aes, out, in, sz, iv, ivSz,
10244+
authTag, authTagSz,
10245+
authIn, authInSz, 1 /* enc */);
10246+
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
10247+
return ret;
10248+
/* fall through to SW GCM (still uses HW AES via wc_AesEncrypt) */
10249+
#endif /* WOLFSSL_STM32_BARE && STM32_CRYPTO */
10250+
10251+
1018610252
#ifdef STM32_CRYPTO_AES_GCM
1018710253
return wc_AesGcmEncrypt_STM32(
1018810254
aes, out, in, sz, iv, ivSz,
@@ -10927,6 +10993,10 @@ int wc_AesGcmDecrypt(Aes* aes, byte* out, const byte* in, word32 sz,
1092710993
}
1092810994
#endif
1092910995

10996+
/* BARE: GCM decrypt always uses SW path (with HW AES blocks via
10997+
* wc_AesEncrypt). Encrypt is HW-accelerated above; decrypt + tag
10998+
* verification stays in well-tested SW for now. */
10999+
1093011000
#ifdef STM32_CRYPTO_AES_GCM
1093111001
/* The STM standard peripheral library API's doesn't support partial blocks */
1093211002
return wc_AesGcmDecrypt_STM32(
@@ -13751,7 +13821,7 @@ int wc_AesInit(Aes* aes, void* heap, int devId)
1375113821

1375213822
aes->heap = heap;
1375313823

13754-
#if defined(WOLF_CRYPTO_CB) || defined(WOLFSSL_STM32U5_DHUK)
13824+
#if defined(WOLF_CRYPTO_CB) || defined(WOLFSSL_DHUK)
1375513825
aes->devId = devId;
1375613826
aes->devCtx = NULL;
1375713827
#else

wolfcrypt/src/ecc.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,9 @@ ECC Curve Sizes:
287287
!defined(WOLFSSL_MICROCHIP_TA100) && \
288288
!defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_SILABS_SE_ACCEL) && \
289289
!defined(WOLFSSL_KCAPI_ECC) && !defined(WOLFSSL_SE050) && \
290-
!defined(WOLFSSL_STM32_PKA) && !defined(WOLFSSL_PSOC6_CRYPTO) && \
291-
!defined(WOLFSSL_XILINX_CRYPT_VERSAL)
290+
!defined(WOLFSSL_XILINX_CRYPT_VERSAL) && \
291+
!defined(WOLFSSL_STM32_PKA) && \
292+
!defined(WOLFSSL_PSOC6_CRYPTO)
292293
#undef HAVE_ECC_VERIFY_HELPER
293294
#define HAVE_ECC_VERIFY_HELPER
294295
#endif
@@ -7018,6 +7019,10 @@ static int deterministic_sign_helper(const byte* in, word32 inlen, ecc_key* key)
70187019
#endif /* WOLFSSL_ECDSA_DETERMINISTIC_K ||
70197020
WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT */
70207021

7022+
/* WOLFSSL_STM32_PKA routes HW ECDSA sign/verify through the STM32 PKA
7023+
* (HAL_PKA_ECDSASign / Verify). Works under both the CubeMX-HAL path
7024+
* and the bare-metal direct-register path (WOLFSSL_STM32_BARE) -- the
7025+
* bare-metal driver implements the same HAL_PKA_ECDSA* surface. */
70217026
#if defined(WOLFSSL_STM32_PKA)
70227027
int wc_ecc_sign_hash_ex(const byte* in, word32 inlen, WC_RNG* rng,
70237028
ecc_key* key, mp_int *r, mp_int *s)
@@ -8836,7 +8841,8 @@ int wc_ecc_verify_hash(const byte* sig, word32 siglen, const byte* hash,
88368841

88378842
#ifndef WOLF_CRYPTO_CB_ONLY_ECC
88388843

8839-
#if !defined(WOLFSSL_STM32_PKA) && !defined(WOLFSSL_PSOC6_CRYPTO) && \
8844+
#if !defined(WOLFSSL_STM32_PKA) && \
8845+
!defined(WOLFSSL_PSOC6_CRYPTO) && \
88408846
!defined(WOLF_CRYPTO_CB_ONLY_ECC)
88418847
static int wc_ecc_check_r_s_range(ecc_key* key, mp_int* r, mp_int* s)
88428848
{
@@ -9354,6 +9360,8 @@ int wc_ecc_verify_hash_ex(mp_int *r, mp_int *s, const byte* hash,
93549360
word32 hashlen, int* res, ecc_key* key)
93559361
{
93569362
#if defined(WOLFSSL_STM32_PKA)
9363+
/* HW ECDSA verify via STM32 PKA. Works under both the CubeMX-HAL
9364+
* and the bare-metal direct-register paths. */
93579365
return stm32_ecc_verify_hash_ex(r, s, hash, hashlen, res, key);
93589366
#elif defined(WOLFSSL_PSOC6_CRYPTO)
93599367
return psoc6_ecc_verify_hash_ex(r, s, hash, hashlen, res, key);

0 commit comments

Comments
 (0)