|
39 | 39 | #endif |
40 | 40 | #endif |
41 | 41 |
|
| 42 | +/* The CubeMX AES crypto-callback device needs cryptocb.h on any WOLF_CRYPTO_CB |
| 43 | + * build, not only WOLFSSL_DHUK. */ |
| 44 | +#if defined(WOLF_CRYPTO_CB) && !defined(WOLFSSL_DHUK) |
| 45 | + #include <wolfssl/wolfcrypt/cryptocb.h> |
| 46 | +#endif |
| 47 | + |
42 | 48 | #ifdef NO_INLINE |
43 | 49 | #include <wolfssl/wolfcrypt/misc.h> |
44 | 50 | #else |
|
75 | 81 | #elif defined(WOLFSSL_STM32U5) |
76 | 82 | #include <stm32u5xx_hal_conf.h> |
77 | 83 | #include <stm32u5xx_hal_pka.h> |
| 84 | +#elif defined(WOLFSSL_STM32U3) |
| 85 | +#include <stm32u3xx_hal_conf.h> |
| 86 | +#include <stm32u3xx_hal_pka.h> |
78 | 87 | #elif defined(WOLFSSL_STM32WB) |
79 | 88 | #include <stm32wbxx_hal_conf.h> |
80 | 89 | #include <stm32wbxx_hal_pka.h> |
@@ -5046,6 +5055,142 @@ void wc_Stm32_Aes_Cleanup(void) |
5046 | 5055 |
|
5047 | 5056 | #endif /* WOLFSSL_STM32_BARE / WOLFSSL_STM32_CUBEMX / StdPeriph */ |
5048 | 5057 |
|
| 5058 | +/* CubeMX/HAL AES crypto-callback device -- makes WOLF_CRYPTO_CB_ONLY_AES work on |
| 5059 | + * the HAL build. That mode strips the software AES core and routes AES through |
| 5060 | + * the crypto callback; wc_AesGcmSetKey derives the GHASH subkey H via |
| 5061 | + * wc_CryptoCb_AesEcbEncrypt, so an AES-ECB handler is required just to key GCM. |
| 5062 | + * Providing it here lets AES-GCM be keyed, after which wc_AesGcmEncrypt runs on |
| 5063 | + * the native HAL GCM engine (wc_AesGcmEncrypt_STM32) via its CRYPTOCB_UNAVAILABLE |
| 5064 | + * fall-through. Uses the plaintext key on the Aes (aes->key) and the plain CRYP |
| 5065 | + * (AES/TinyAES) instance -- no DHUK/SAES. */ |
| 5066 | +#if defined(WOLFSSL_STM32_CUBEMX) && defined(WOLF_CRYPTO_CB) |
| 5067 | + |
| 5068 | +#if defined(HAVE_AES_ECB) || defined(WOLFSSL_AES_DIRECT) || \ |
| 5069 | + defined(WOLF_CRYPTO_CB_ONLY_AES) |
| 5070 | +/* One or more whole AES blocks through the HAL in ECB mode. enc != 0 selects |
| 5071 | + * encrypt. Mirrors the HAL ECB sequence in wc_AesEncrypt/wc_AesDecrypt (aes.c) |
| 5072 | + * that WOLF_CRYPTO_CB_ONLY_AES compiles out. */ |
| 5073 | +static int Stm32Cube_AesEcb(struct Aes* aes, byte* out, const byte* in, |
| 5074 | + word32 sz, int enc) |
| 5075 | +{ |
| 5076 | + CRYP_HandleTypeDef hcryp; |
| 5077 | + int ret; |
| 5078 | + |
| 5079 | + if (aes == NULL || out == NULL || in == NULL) { |
| 5080 | + return BAD_FUNC_ARG; |
| 5081 | + } |
| 5082 | + if (sz == 0 || (sz % WC_AES_BLOCK_SIZE) != 0) { |
| 5083 | + return BAD_FUNC_ARG; |
| 5084 | + } |
| 5085 | + |
| 5086 | + ret = wc_Stm32_Aes_Init(aes, &hcryp, 0); |
| 5087 | + if (ret != 0) { |
| 5088 | + return ret; |
| 5089 | + } |
| 5090 | + |
| 5091 | + ret = wolfSSL_CryptHwMutexLock(); |
| 5092 | + if (ret != 0) { |
| 5093 | + return ret; |
| 5094 | + } |
| 5095 | + |
| 5096 | +#if defined(STM32_HAL_V2) |
| 5097 | + hcryp.Init.Algorithm = CRYP_AES_ECB; |
| 5098 | +#elif defined(STM32_CRYPTO_AES_ONLY) |
| 5099 | + hcryp.Init.OperatingMode = enc ? |
| 5100 | + CRYP_ALGOMODE_ENCRYPT : CRYP_ALGOMODE_KEYDERIVATION_DECRYPT; |
| 5101 | + hcryp.Init.ChainingMode = CRYP_CHAINMODE_AES_ECB; |
| 5102 | + hcryp.Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE; |
| 5103 | +#endif |
| 5104 | + if (HAL_CRYP_Init(&hcryp) != HAL_OK) { |
| 5105 | + wolfSSL_CryptHwMutexUnLock(); |
| 5106 | + wc_Stm32_Aes_Cleanup(); |
| 5107 | + return WC_HW_E; |
| 5108 | + } |
| 5109 | + |
| 5110 | +#if defined(STM32_HAL_V2) |
| 5111 | + if (enc) { |
| 5112 | + ret = HAL_CRYP_Encrypt(&hcryp, (uint32_t*)in, sz, (uint32_t*)out, |
| 5113 | + STM32_HAL_TIMEOUT); |
| 5114 | + } |
| 5115 | + else { |
| 5116 | + ret = HAL_CRYP_Decrypt(&hcryp, (uint32_t*)in, sz, (uint32_t*)out, |
| 5117 | + STM32_HAL_TIMEOUT); |
| 5118 | + } |
| 5119 | +#elif defined(STM32_CRYPTO_AES_ONLY) |
| 5120 | + ret = HAL_CRYPEx_AES(&hcryp, (uint8_t*)in, sz, out, STM32_HAL_TIMEOUT); |
| 5121 | +#else |
| 5122 | + if (enc) { |
| 5123 | + ret = HAL_CRYP_AESECB_Encrypt(&hcryp, (uint8_t*)in, sz, out, |
| 5124 | + STM32_HAL_TIMEOUT); |
| 5125 | + } |
| 5126 | + else { |
| 5127 | + ret = HAL_CRYP_AESECB_Decrypt(&hcryp, (uint8_t*)in, sz, out, |
| 5128 | + STM32_HAL_TIMEOUT); |
| 5129 | + } |
| 5130 | +#endif |
| 5131 | + if (ret != HAL_OK) { |
| 5132 | + ret = WC_TIMEOUT_E; |
| 5133 | + } |
| 5134 | + |
| 5135 | + HAL_CRYP_DeInit(&hcryp); |
| 5136 | + wolfSSL_CryptHwMutexUnLock(); |
| 5137 | + wc_Stm32_Aes_Cleanup(); |
| 5138 | + return ret; |
| 5139 | +} |
| 5140 | +#endif /* HAVE_AES_ECB || WOLFSSL_AES_DIRECT || WOLF_CRYPTO_CB_ONLY_AES */ |
| 5141 | + |
| 5142 | +/* Cipher dispatch (shared with the CCB device below). Services AES-ECB; other |
| 5143 | + * modes return CRYPTOCB_UNAVAILABLE so the caller falls through to the native |
| 5144 | + * HAL path (e.g. wc_AesGcmEncrypt_STM32) or another provider. */ |
| 5145 | +static int Stm32Cube_Cipher(struct wc_CryptoInfo* info) |
| 5146 | +{ |
| 5147 | + if (info == NULL) { |
| 5148 | + return CRYPTOCB_UNAVAILABLE; |
| 5149 | + } |
| 5150 | + |
| 5151 | + switch (info->cipher.type) { |
| 5152 | +#if defined(HAVE_AES_ECB) || defined(WOLFSSL_AES_DIRECT) || \ |
| 5153 | + defined(WOLF_CRYPTO_CB_ONLY_AES) |
| 5154 | + case WC_CIPHER_AES_ECB: |
| 5155 | + return Stm32Cube_AesEcb(info->cipher.aesecb.aes, |
| 5156 | + info->cipher.aesecb.out, info->cipher.aesecb.in, |
| 5157 | + info->cipher.aesecb.sz, info->cipher.enc); |
| 5158 | +#endif |
| 5159 | + default: |
| 5160 | + return CRYPTOCB_UNAVAILABLE; |
| 5161 | + } |
| 5162 | +} |
| 5163 | + |
| 5164 | +/* Standalone AES-only device for CubeMX builds without the CCB/DHUK ECDSA |
| 5165 | + * device. Register at a devId, then wc_AesInit(aes, heap, devId). (With |
| 5166 | + * WOLFSSL_STM32_CCB the CCB device dispatches ciphers here too.) */ |
| 5167 | +static int Stm32Cube_AesCryptoDevCb(int devId, struct wc_CryptoInfo* info, |
| 5168 | + void* ctx) |
| 5169 | +{ |
| 5170 | + (void)devId; |
| 5171 | + (void)ctx; |
| 5172 | + |
| 5173 | + if (info == NULL) { |
| 5174 | + return CRYPTOCB_UNAVAILABLE; |
| 5175 | + } |
| 5176 | + if (info->algo_type == WC_ALGO_TYPE_CIPHER) { |
| 5177 | + return Stm32Cube_Cipher(info); |
| 5178 | + } |
| 5179 | + return CRYPTOCB_UNAVAILABLE; |
| 5180 | +} |
| 5181 | + |
| 5182 | +int wc_Stm32_CubeAesRegister(int devId) |
| 5183 | +{ |
| 5184 | + return wc_CryptoCb_RegisterDevice(devId, Stm32Cube_AesCryptoDevCb, NULL); |
| 5185 | +} |
| 5186 | + |
| 5187 | +void wc_Stm32_CubeAesUnRegister(int devId) |
| 5188 | +{ |
| 5189 | + wc_CryptoCb_UnRegisterDevice(devId); |
| 5190 | +} |
| 5191 | + |
| 5192 | +#endif /* WOLFSSL_STM32_CUBEMX && WOLF_CRYPTO_CB */ |
| 5193 | + |
5049 | 5194 | /* CubeMX/HAL CCB ECDSA port -- placed after the build-branch structure and |
5050 | 5195 | * guarded on WOLFSSL_STM32_CUBEMX so it compiles only for the HAL build (the |
5051 | 5196 | * BARE build provides its own wc_Stm32_Ccb_* above). */ |
@@ -5234,7 +5379,17 @@ static int Stm32Ccb_CryptoDevCb(int devId, struct wc_CryptoInfo* info, |
5234 | 5379 |
|
5235 | 5380 | (void)devId; |
5236 | 5381 | (void)ctx; |
5237 | | - if (info == NULL || info->algo_type != WC_ALGO_TYPE_PK) { |
| 5382 | + if (info == NULL) { |
| 5383 | + return CRYPTOCB_UNAVAILABLE; |
| 5384 | + } |
| 5385 | + /* Route ciphers to the shared CubeMX AES handler so one devId serves both |
| 5386 | + * CCB ECDSA and HAL AES (lets WOLF_CRYPTO_CB_ONLY_AES work here). */ |
| 5387 | +#ifndef NO_AES |
| 5388 | + if (info->algo_type == WC_ALGO_TYPE_CIPHER) { |
| 5389 | + return Stm32Cube_Cipher(info); |
| 5390 | + } |
| 5391 | +#endif |
| 5392 | + if (info->algo_type != WC_ALGO_TYPE_PK) { |
5238 | 5393 | return CRYPTOCB_UNAVAILABLE; |
5239 | 5394 | } |
5240 | 5395 | /* Transparent provisioning: wc_ecc_make_key() on a WC_DHUK_DEVID key binds |
|
0 commit comments