2026-04-07 2:58 PM
Hello. I am trying to encrypt/decrypt data in secure area of stm32h563 (Trustzone application), using code from one of the examples (using cmox_cipher_encrypt function). Running the function in non-secure area, everything works fine. Trying to run that in secure area immediatly hardfaults. It seems like problem might be that the library tries to access something in the non secure area. Initial thought was CRC engine, but in cubemx i specificly initialized that to secure area because it will be used in there. Is there some known workaround or updated libraries to use cmox_cipher_encrypt in secure area?
2026-04-08 1:17 PM
To follow-up on this: Tried running cmox_cipher_encrypt functions in non secure area. Those works fine, but there are two key things:
1) First encrypted output is completely different from the all others (Running 10 loops of same code, first run gives values which differ from other 9 runs). Based on previous talks in the forum, that seems to be some kind of problem with how cmox uses CRC engine, CRC might have some default init values.
2) Even when getting same values on the remaining 9 runs, nothing matches online AES256-CBC calculator. Tried running the code using NIST test vectors and result does not match.
2026-04-09 2:06 AM
Dear @DBara.1
Please provide the following information so we can analyze the issue further. If possible, include a minimal reproducible example and the exact test vector used.
Best regards
Lobna
2026-04-10 3:20 AM - edited 2026-04-10 3:25 AM
Hello,
Im not that advanced yet to understand like half of the things you asked for, but i will try :)
If it would be easier i can just upload whole project somewhere, but generally: Opened CubeMX, Select STM32H563ZIT (MCU on Nucleo-H563ZI board).Activate CRC to Cortex-M33 secure, generate code. Add ST Cryptography libraries to project, write minimal cmox_ciper_encrypt test code in secure area, write linker function to call this from non secure area, make the call. Code reaches cmox_cipher_encrypt() function and then hardfaults.
static void MX_CRC_Init(void)
{
/* USER CODE BEGIN CRC_Init 0 */
/* USER CODE END CRC_Init 0 */
/* USER CODE BEGIN CRC_Init 1 */
/* USER CODE END CRC_Init 1 */
hcrc.Instance = CRC;
hcrc.Init.DefaultPolynomialUse = DEFAULT_POLYNOMIAL_ENABLE;
hcrc.Init.DefaultInitValueUse = DEFAULT_INIT_VALUE_ENABLE;
hcrc.Init.InputDataInversionMode = CRC_INPUTDATA_INVERSION_NONE;
hcrc.Init.OutputDataInversionMode = CRC_OUTPUTDATA_INVERSION_DISABLE;
hcrc.InputDataFormat = CRC_INPUTDATA_FORMAT_BYTES;
if (HAL_CRC_Init(&hcrc) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN CRC_Init 2 */
/* USER CODE END CRC_Init 2 */
}cmox_cbc_handle_t Cbc_Ctx;
uint8_t key[32] = {0x60,0x3D,0xEB,0x10,0x15,0xCA,0x71,0xBE,0x2B,0x73,0xAE,0xF0,0x85,0x7D,0x77,0x81,0x1F,0x35,0x2C,0x07,0x3B,0x61,0x08,0xD7,0x2D,0x98,0x10,0xA3,0x09,0x14,0xDF,0xF4};
uint8_t iv[16] = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F};
uint8_t input[16] = {0x6B,0xC1,0xBE,0xE2,0x2E,0x40,0x9F,0x96,0xE9,0x3D,0x7E,0x11,0x73,0x93,0x17,0x2A};
uint8_t output[16] = {0};
cmox_cipher_retval_t retval;
size_t computed_size;
cmox_cipher_handle_t *cipher_ctx;
uint32_t index;
cmox_init_arg_t init_target = {CMOX_INIT_TARGET_AUTO, NULL};
if (cmox_initialize(&init_target) != CMOX_INIT_SUCCESS)
{
Error_Handler();
}
retval = cmox_cipher_encrypt(CMOX_AES_CBC_ENC_ALGO, input, sizeof(input), key, sizeof(key),iv, sizeof(iv), output, &computed_size);
HAL_Delay(10);Best regards,
Dainius
2026-04-10 3:25 AM
Dear @DBara.1
Thank you for your prompt response. Would it be possible to upload a ZIP file containing the entire project?
Best regards
Lobna
2026-04-10 3:38 AM
Sure. Another question ive raised while writing the response is: if cmox uses CRC engine for some internal calculations, then why:
Secure zone code hardfaults, even though CRC is assigned to M33-Secure
Non secure zone code somewhat works (even though value is wrong), when it shouldnt even have access to the CRC engine.
Best regards,
Dainius
2026-04-13 7:45 AM
dear @DBara.1
Have you made the following step before running your code extracted from the readme file from the H5 Cube Firmware (project template Trust zone enabled)
The hard fault will disappear
Best regards
Lobna
2026-04-13 10:32 AM
Hello,
I do have the internal flash configuration set to bank1 secure, bank2 non secure.
My configuration was
-SECWM1_STRT=0x00 SECWM1_END=0x7F
-SECWM2_STRT=0x7F SECWM2_END=0x00
Just in case i now swapped it to whats shown in the picture (
-SECWM1_STRT=0x00 SECWM1_END=0x7F
-SECWM2_STRT=0x01 SECWM2_END=0x00 ),
but i am still getting the same hardfault when trying to run cmox_encrypt() in Secure Area.
Best regards,
Dainius
2026-04-14 12:08 AM
2026-04-14 12:46 AM
Hello,
Yes i did. TZEN is set to B4 using CubeProgrammer.
By the way, in regards of yesterdays question about internal flash and ram splitting - if second region is not set as non secure ( if SECWM2_STRT=0x01 SECWM2_END=0x00 is not set up properly), STM hardfaults when trying to run nonsecure_init(), it doesnt fully run at all. Thats just something for future refference in case someone has such problems :)
Best regards,
Dainius