Skip to content

Commit b881d61

Browse files
committed
Adds new STM32 Bare support for Hash, SAES/AES and PKA
1 parent 02dfd12 commit b881d61

7 files changed

Lines changed: 1821 additions & 20 deletions

File tree

.wolfssl_known_macro_extras

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
AES_CR_CCFC
12
AES_GCM_GMULT_NCT
3+
AES_ICR_CCF
24
AFX_RESOURCE_DLL
35
AFX_TARG_ENU
46
ALLOW_BINARY_MISMATCH_INTROSPECTION
@@ -265,7 +267,11 @@ HARDWARE_CACHE_COHERENCY
265267
HASH_AlgoMode_HASH
266268
HASH_AlgoMode_HMAC
267269
HASH_BYTE_SWAP
270+
HASH_CR_ALGO_1
271+
HASH_CR_DATATYPE_0
272+
HASH_CR_DATATYPE_1
268273
HASH_CR_LKEY
274+
HASH_CR_MODE
269275
HASH_DIGEST
270276
HASH_DataType_8b
271277
HASH_IMR_DCIE
@@ -495,6 +501,12 @@ PTHREAD_STACK_MIN
495501
QAT_ENABLE_HASH
496502
QAT_ENABLE_RNG
497503
QAT_USE_POLLING_CHECK
504+
RCC_AHB1ENR_PKAEN
505+
RCC_AHB2ENR1_AESEN
506+
RCC_AHB2ENR1_HASHEN
507+
RCC_AHB2ENR1_PKAEN
508+
RCC_AHB2ENR_HASHEN
509+
RCC_AHB2ENR_PKAEN
498510
RC_NO_RNG
499511
REDIRECTION_IN3_KEYELMID
500512
REDIRECTION_IN3_KEYID
@@ -917,6 +929,7 @@ WOLFSSL_SP_INT_SQR_VOLATILE
917929
WOLFSSL_STACK_CHECK
918930
WOLFSSL_STM32F427_RNG
919931
WOLFSSL_STM32U5_DHUK
932+
WOLFSSL_STM32_BARE
920933
WOLFSSL_STRONGEST_HASH_SIG
921934
WOLFSSL_STSAFE_TAKES_SLOT
922935
WOLFSSL_TELIT_M2MB

wolfcrypt/src/aes.c

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,10 @@ 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+
return wc_Stm32_Aes_Ecb(aes, outBlock, inBlock, WC_AES_BLOCK_SIZE, 1);
233+
#else
230234
int ret = 0;
231235
#ifdef WOLFSSL_STM32_CUBEMX
232236
CRYP_HandleTypeDef hcryp;
@@ -367,6 +371,7 @@ block cipher mechanism that uses n-bit binary string parameter key with 128-bits
367371
wc_Stm32_Aes_Cleanup();
368372

369373
return ret;
374+
#endif /* !WOLFSSL_STM32_BARE */
370375
}
371376
#endif /* WOLFSSL_AES_DIRECT || HAVE_AESGCM || HAVE_AESCCM */
372377

@@ -375,6 +380,9 @@ block cipher mechanism that uses n-bit binary string parameter key with 128-bits
375380
static WARN_UNUSED_RESULT int wc_AesDecrypt(
376381
Aes* aes, const byte* inBlock, byte* outBlock)
377382
{
383+
#ifdef WOLFSSL_STM32_BARE
384+
return wc_Stm32_Aes_Ecb(aes, outBlock, inBlock, WC_AES_BLOCK_SIZE, 0);
385+
#else
378386
int ret = 0;
379387
#ifdef WOLFSSL_STM32_CUBEMX
380388
CRYP_HandleTypeDef hcryp;
@@ -521,6 +529,7 @@ block cipher mechanism that uses n-bit binary string parameter key with 128-bits
521529
wc_Stm32_Aes_Cleanup();
522530

523531
return ret;
532+
#endif /* !WOLFSSL_STM32_BARE */
524533
}
525534
#endif /* WOLFSSL_AES_DIRECT */
526535
#endif /* HAVE_AES_DECRYPT */
@@ -5575,7 +5584,34 @@ int wc_AesSetIV(Aes* aes, const byte* iv)
55755584
#ifdef HAVE_AES_CBC
55765585
#if defined(STM32_CRYPTO)
55775586

5578-
#ifdef WOLFSSL_STM32U5_DHUK
5587+
#ifdef WOLFSSL_STM32_BARE
5588+
int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
5589+
{
5590+
#ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
5591+
if (sz % WC_AES_BLOCK_SIZE) {
5592+
return BAD_LENGTH_E;
5593+
}
5594+
#endif
5595+
if (sz == 0) {
5596+
return 0;
5597+
}
5598+
return wc_Stm32_Aes_Cbc(aes, out, in, sz, 1);
5599+
}
5600+
#ifdef HAVE_AES_DECRYPT
5601+
int wc_AesCbcDecrypt(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, 0);
5612+
}
5613+
#endif /* HAVE_AES_DECRYPT */
5614+
#elif defined(WOLFSSL_STM32U5_DHUK)
55795615
int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
55805616
{
55815617
int ret = 0;
@@ -6955,6 +6991,11 @@ int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
69556991

69566992
int wc_AesCtrEncryptBlock(Aes* aes, byte* out, const byte* in)
69576993
{
6994+
#ifdef WOLFSSL_STM32_BARE
6995+
/* CTR per-block transform: ECB-encrypt the counter (passed in
6996+
* 'in'); aes.c handles counter increment and XOR with plaintext. */
6997+
return wc_Stm32_Aes_Ecb(aes, out, in, WC_AES_BLOCK_SIZE, 1);
6998+
#else
69586999
int ret = 0;
69597000
#ifdef WOLFSSL_STM32_CUBEMX
69607001
CRYP_HandleTypeDef hcryp;
@@ -7065,6 +7106,7 @@ int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
70657106
wolfSSL_CryptHwMutexUnLock();
70667107
wc_Stm32_Aes_Cleanup();
70677108
return ret;
7109+
#endif /* !WOLFSSL_STM32_BARE */
70687110
}
70697111

70707112

@@ -10141,6 +10183,15 @@ int wc_AesGcmEncrypt(Aes* aes, byte* out, const byte* in, word32 sz,
1014110183
authIn, authInSz);
1014210184
#endif
1014310185

10186+
#if defined(WOLFSSL_STM32_BARE) && defined(STM32_CRYPTO)
10187+
ret = wc_Stm32_Aes_Gcm(aes, out, in, sz, iv, ivSz,
10188+
authTag, authTagSz,
10189+
authIn, authInSz, 1 /* enc */);
10190+
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
10191+
return ret;
10192+
/* fall through to SW GCM (still uses HW AES via wc_AesEncrypt) */
10193+
#endif /* WOLFSSL_STM32_BARE && STM32_CRYPTO */
10194+
1014410195
#ifdef STM32_CRYPTO_AES_GCM
1014510196
return wc_AesGcmEncrypt_STM32(
1014610197
aes, out, in, sz, iv, ivSz,
@@ -10870,6 +10921,10 @@ int wc_AesGcmDecrypt(Aes* aes, byte* out, const byte* in, word32 sz,
1087010921

1087110922
#endif
1087210923

10924+
/* BARE: GCM decrypt always uses SW path (with HW AES blocks via
10925+
* wc_AesEncrypt). Encrypt is HW-accelerated above; decrypt + tag
10926+
* verification stays in well-tested SW for now. */
10927+
1087310928
#ifdef STM32_CRYPTO_AES_GCM
1087410929
/* The STM standard peripheral library API's doesn't support partial blocks */
1087510930
return wc_AesGcmDecrypt_STM32(

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
{
@@ -8751,7 +8760,8 @@ int wc_ecc_verify_hash(const byte* sig, word32 siglen, const byte* hash,
87518760

87528761
#ifndef WOLF_CRYPTO_CB_ONLY_ECC
87538762

8754-
#if !defined(WOLFSSL_STM32_PKA) && !defined(WOLFSSL_PSOC6_CRYPTO) && \
8763+
#if !(defined(WOLFSSL_STM32_PKA) && !defined(WOLFSSL_STM32_BARE)) && \
8764+
!defined(WOLFSSL_PSOC6_CRYPTO) && \
87558765
!defined(WOLF_CRYPTO_CB_ONLY_ECC)
87568766
static int wc_ecc_check_r_s_range(ecc_key* key, mp_int* r, mp_int* s)
87578767
{
@@ -9267,7 +9277,10 @@ static int ecc_verify_hash(mp_int *r, mp_int *s, const byte* hash,
92679277
int wc_ecc_verify_hash_ex(mp_int *r, mp_int *s, const byte* hash,
92689278
word32 hashlen, int* res, ecc_key* key)
92699279
{
9270-
#if defined(WOLFSSL_STM32_PKA)
9280+
#if defined(WOLFSSL_STM32_PKA) && !defined(WOLFSSL_STM32_BARE)
9281+
/* See comment above wc_ecc_sign_hash_ex(): BARE uses SW ECDSA
9282+
* verify which internally accelerates the scalar muls via the
9283+
* bare-metal HW wc_ecc_mulmod_ex2(). */
92719284
return stm32_ecc_verify_hash_ex(r, s, hash, hashlen, res, key);
92729285
#elif defined(WOLFSSL_PSOC6_CRYPTO)
92739286
return psoc6_ecc_verify_hash_ex(r, s, hash, hashlen, res, key);

0 commit comments

Comments
 (0)