Skip to content

Commit fe62f3b

Browse files
committed
Fix STM32 CubeMX AES callback clock cleanup and error mapping
1 parent cafe66c commit fe62f3b

3 files changed

Lines changed: 14 additions & 6 deletions

File tree

wolfcrypt/src/port/st/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ Some newer families (H5/H7S/U3/U5/WBA/C5/N6, plus the L562 sub-variant) expose a
101101
wc_Stm32_DhukRegister(devId); /* once; serves AES + ECDSA (+ CCB) */
102102
```
103103

104-
(For an AES-only build without CCB/ECC, `wc_Stm32_CubeAesRegister(devId)` registers a cipher-only device.) HW PKA on the HAL build requires the application to build/link ST's `stm32XXxx_hal_pka.c` and provide a `PKA_HandleTypeDef hpka;` initialized with `HAL_PKA_Init(&hpka)` (a CubeMX `MX_PKA_Init`); wolfSSL references `hpka` as `extern`. Note: CCB key *provisioning* (`wc_ecc_make_key`) derives the scalar in software, so it needs a build without `WOLF_CRYPTO_CB_ONLY_ECC` -- provision the CCB blob there, after which CCB *sign* runs under the stripped config. Worked examples in [`STM32_Bare_Test`](https://github.com/wolfSSL/wolfssl-examples-stm32): `main_cubeaes.c` (AES) and `main_cubecrypto.c` (ECC + CCB + AES), validated on NUCLEO-U385RG-Q.
104+
(For a build without CCB, `wc_Stm32_CubeAesRegister(devId)` registers the same CubeMX device: AES, plus ECDSA sign/verify on the HW PKA when `WOLFSSL_STM32_PKA` is enabled -- so it also satisfies `WOLF_CRYPTO_CB_ONLY_ECC` without CCB. It falls back to cipher-only when the PKA is not built in.) HW PKA on the HAL build requires the application to build/link ST's `stm32XXxx_hal_pka.c` and provide a `PKA_HandleTypeDef hpka;` initialized with `HAL_PKA_Init(&hpka)` (a CubeMX `MX_PKA_Init`); wolfSSL references `hpka` as `extern`. Note: CCB key *provisioning* (`wc_ecc_make_key`) derives the scalar in software, so it needs a build without `WOLF_CRYPTO_CB_ONLY_ECC` -- provision the CCB blob there, after which CCB *sign* runs under the stripped config. Worked examples in [`STM32_Bare_Test`](https://github.com/wolfSSL/wolfssl-examples-stm32): `main_cubeaes.c` (AES) and `main_cubecrypto.c` (ECC + CCB + AES), validated on NUCLEO-U385RG-Q.
105105

106106
### Coding
107107

wolfcrypt/src/port/st/stm32.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5090,6 +5090,9 @@ static int Stm32Cube_AesEcb(struct Aes* aes, byte* out, const byte* in,
50905090

50915091
ret = wolfSSL_CryptHwMutexLock();
50925092
if (ret != 0) {
5093+
/* wc_Stm32_Aes_Init enabled the CRYP clock (STM32_HW_CLOCK_AUTO);
5094+
* release it before bailing out. */
5095+
wc_Stm32_Aes_Cleanup();
50935096
return ret;
50945097
}
50955098

@@ -5129,7 +5132,9 @@ static int Stm32Cube_AesEcb(struct Aes* aes, byte* out, const byte* in,
51295132
}
51305133
#endif
51315134
if (ret != HAL_OK) {
5132-
ret = WC_TIMEOUT_E;
5135+
/* Keep HAL_TIMEOUT distinct from HAL_ERROR/HAL_BUSY so callers and
5136+
* debugging can tell a real timeout from a HW fault. */
5137+
ret = (ret == HAL_TIMEOUT) ? WC_TIMEOUT_E : WC_HW_E;
51335138
}
51345139

51355140
HAL_CRYP_DeInit(&hcryp);

wolfssl/wolfcrypt/port/st/stm32.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,10 +1000,13 @@ int stm32_ecc_sign_hash_ex(const byte* hash, word32 hashlen, struct WC_RNG* rng,
10001000
void wc_Stm32_DhukUnRegister(int devId);
10011001
#endif
10021002

1003-
/* CubeMX/HAL AES crypto-callback device. Register at a devId, then init an Aes
1004-
* with it (wc_AesInit) to run AES on the HAL through the crypto callback -- this
1005-
* makes WOLF_CRYPTO_CB_ONLY_AES work on the HAL build. With WOLFSSL_STM32_CCB
1006-
* enabled, wc_Stm32_DhukRegister already covers AES too (same devId). */
1003+
/* CubeMX/HAL crypto-callback device. Register at a devId, then init an Aes
1004+
* with it (wc_AesInit) to run AES on the HAL through the crypto callback --
1005+
* this makes WOLF_CRYPTO_CB_ONLY_AES work on the HAL build. When
1006+
* WOLFSSL_STM32_PKA && HAVE_ECC are also enabled it additionally routes ECDSA
1007+
* sign/verify to the HW PKA, so it satisfies WOLF_CRYPTO_CB_ONLY_ECC too (no
1008+
* CCB required). With WOLFSSL_STM32_CCB enabled, wc_Stm32_DhukRegister already
1009+
* covers AES + ECDSA (same devId). */
10071010
#if defined(WOLFSSL_STM32_CUBEMX) && defined(WOLF_CRYPTO_CB)
10081011
int wc_Stm32_CubeAesRegister(int devId);
10091012
void wc_Stm32_CubeAesUnRegister(int devId);

0 commit comments

Comments
 (0)