Skip to content

Commit 2c8100c

Browse files
committed
Adds new STM32 Bare support for Hash, SAES/AES and PKA
1 parent 90359f9 commit 2c8100c

8 files changed

Lines changed: 2875 additions & 55 deletions

File tree

.wolfssl_known_macro_extras

Lines changed: 33 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
@@ -269,7 +273,11 @@ HARDWARE_CACHE_COHERENCY
269273
HASH_AlgoMode_HASH
270274
HASH_AlgoMode_HMAC
271275
HASH_BYTE_SWAP
276+
HASH_CR_ALGO_1
277+
HASH_CR_DATATYPE_0
278+
HASH_CR_DATATYPE_1
272279
HASH_CR_LKEY
280+
HASH_CR_MODE
273281
HASH_DIGEST
274282
HASH_DataType_8b
275283
HASH_IMR_DCIE
@@ -498,6 +506,19 @@ PTHREAD_STACK_MIN
498506
QAT_ENABLE_HASH
499507
QAT_ENABLE_RNG
500508
QAT_USE_POLLING_CHECK
509+
RCC_AHB1ENR_PKAEN
510+
RCC_AHB2ENR1_AESEN
511+
RCC_AHB2ENR1_HASHEN
512+
RCC_AHB2ENR1_PKAEN
513+
RCC_AHB2ENR_HASHEN
514+
RCC_AHB2ENR_PKAEN
515+
RCC_AHB2ENR_SAESEN
516+
RCC_AHB3ENR_AESEN
517+
RCC_AHB3ENR_CRYPEN
518+
RCC_AHB3ENR_HASHEN
519+
RCC_AHB3ENR_PKAEN
520+
RCC_AHB3ENR_RNGEN
521+
RCC_AHB3ENR_SAESEN
501522
RC_NO_RNG
502523
REDIRECTION_IN3_KEYELMID
503524
REDIRECTION_IN3_KEYID
@@ -677,6 +698,11 @@ WC_SLHDSA_KERNEL_ASM
677698
WC_SLHDSA_NO_ASM
678699
WC_SLHDSA_VERBOSE_DEBUG
679700
WC_SSIZE_TYPE
701+
WC_STM32_AES_CLK_ENABLE_INST
702+
WC_STM32_AES_INST
703+
WC_STM32_HAS_DHUK
704+
WC_STM32_SAES_CLK_DISABLE
705+
WC_STM32_SAES_CLK_ENABLE
680706
WC_STRICT_SIG
681707
WC_USE_PIE_FENCEPOSTS_FOR_FIPS
682708
WC_WANT_FLAG_DONT_USE_VECTOR_OPS
@@ -736,6 +762,9 @@ WOLFSSL_CLANG_TIDY
736762
WOLFSSL_CLIENT_EXAMPLE
737763
WOLFSSL_CONTIKI
738764
WOLFSSL_CRL_ALLOW_MISSING_CDP
765+
WOLFSSL_DHUK
766+
WOLFSSL_DHUK_DEVID
767+
WOLFSSL_DHUK_WRAPPED_DEVID
739768
WOLFSSL_DILITHIUM_ASSIGN_KEY
740769
WOLFSSL_DILITHIUM_NO_CHECK_KEY
741770
WOLFSSL_DILITHIUM_NO_MAKE
@@ -892,6 +921,7 @@ WOLFSSL_RNG_USE_FULL_SEED
892921
WOLFSSL_RSA_CHECK_D_ON_DECRYPT
893922
WOLFSSL_RSA_DECRYPT_TO_0_LEN
894923
WOLFSSL_RW_THREADED
924+
WOLFSSL_SAES_DEVID
895925
WOLFSSL_SAKKE_SMALL
896926
WOLFSSL_SAKKE_SMALL_MODEXP
897927
WOLFSSL_SE050_AUTO_ERASE
@@ -917,9 +947,12 @@ WOLFSSL_SP_ARM32_UDIV
917947
WOLFSSL_SP_FAST_NCT_EXPTMOD
918948
WOLFSSL_SP_INT_SQR_VOLATILE
919949
WOLFSSL_STACK_CHECK
950+
WOLFSSL_STM32C5
920951
WOLFSSL_STM32F427_RNG
921952
WOLFSSL_STM32U5_DHUK
953+
WOLFSSL_STM32_BARE
922954
WOLFSSL_STM32_RNG_NOLIB
955+
WOLFSSL_STM32_USE_SAES
923956
WOLFSSL_STRONGEST_HASH_SIG
924957
WOLFSSL_STSAFE_TAKES_SLOT
925958
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: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,12 @@ 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) && !defined(WOLFSSL_STM32_BARE)) && \
292+
!defined(WOLFSSL_PSOC6_CRYPTO)
293+
/* WOLFSSL_STM32_BARE+PKA still uses the SW ECDSA helper paths
294+
* (sign/verify) since the bare-metal driver only implements ECCMul
295+
* HW; the SP-less SW ECDSA fallback then drives that HW. */
292296
#undef HAVE_ECC_VERIFY_HELPER
293297
#define HAVE_ECC_VERIFY_HELPER
294298
#endif
@@ -7018,7 +7022,12 @@ static int deterministic_sign_helper(const byte* in, word32 inlen, ecc_key* key)
70187022
#endif /* WOLFSSL_ECDSA_DETERMINISTIC_K ||
70197023
WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT */
70207024

7021-
#if defined(WOLFSSL_STM32_PKA)
7025+
/* Under WOLFSSL_STM32_BARE the bare-metal PKA driver implements only
7026+
* ECCMul HW (the building block used by ECDH and the SP-less SW ECDSA
7027+
* path). HW ECDSA sign/verify is intentionally not wired up in v1 of
7028+
* the bare driver -- fall back to the standard SW ECDSA which itself
7029+
* calls wc_ecc_mulmod_ex2() (HW-accelerated). */
7030+
#if defined(WOLFSSL_STM32_PKA) && !defined(WOLFSSL_STM32_BARE)
70227031
int wc_ecc_sign_hash_ex(const byte* in, word32 inlen, WC_RNG* rng,
70237032
ecc_key* key, mp_int *r, mp_int *s)
70247033
{
@@ -8836,7 +8845,8 @@ int wc_ecc_verify_hash(const byte* sig, word32 siglen, const byte* hash,
88368845

88378846
#ifndef WOLF_CRYPTO_CB_ONLY_ECC
88388847

8839-
#if !defined(WOLFSSL_STM32_PKA) && !defined(WOLFSSL_PSOC6_CRYPTO) && \
8848+
#if !(defined(WOLFSSL_STM32_PKA) && !defined(WOLFSSL_STM32_BARE)) && \
8849+
!defined(WOLFSSL_PSOC6_CRYPTO) && \
88408850
!defined(WOLF_CRYPTO_CB_ONLY_ECC)
88418851
static int wc_ecc_check_r_s_range(ecc_key* key, mp_int* r, mp_int* s)
88428852
{
@@ -9353,7 +9363,10 @@ static int ecc_verify_hash(mp_int *r, mp_int *s, const byte* hash,
93539363
int wc_ecc_verify_hash_ex(mp_int *r, mp_int *s, const byte* hash,
93549364
word32 hashlen, int* res, ecc_key* key)
93559365
{
9356-
#if defined(WOLFSSL_STM32_PKA)
9366+
#if defined(WOLFSSL_STM32_PKA) && !defined(WOLFSSL_STM32_BARE)
9367+
/* See comment above wc_ecc_sign_hash_ex(): BARE uses SW ECDSA
9368+
* verify which internally accelerates the scalar muls via the
9369+
* bare-metal HW wc_ecc_mulmod_ex2(). */
93579370
return stm32_ecc_verify_hash_ex(r, s, hash, hashlen, res, key);
93589371
#elif defined(WOLFSSL_PSOC6_CRYPTO)
93599372
return psoc6_ecc_verify_hash_ex(r, s, hash, hashlen, res, key);

0 commit comments

Comments
 (0)