cancel
Showing results for 
Search instead for 
Did you mean: 

STM32H563 Trustzone cmox hardfault

DBara.1
Associate II

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?

9 REPLIES 9
DBara.1
Associate II

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. 

lobna
ST Employee

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.

  • Device / Software:
    - STM32 part number:
    - Package / revision:
    - STM32Cube version:
    - CMOX version:
    - Compiler / toolchain:
    - IDE version:
  • Execution Context:
    - Secure or Non-Secure execution:
    - TrustZone enabled:
    - SAU configuration:
    - GTZC configuration:
    - MPU configuration:
  • CRC Setup:
    - Is the CRC clock enabled before the first crypto call?
    - Is the CRC peripheral reset to a known state?
    - Does any other code access CRC before CMOX?
    - Is CRC assigned to the correct security domain?
    - CRC initialization code:
  • Crypto Parameters:
    - Algorithm:
    - Mode:
    - Key size:
    - IV size:
    - Padding mode:
    - Input length:
  • Test Vector:
    - Exact key:
    - Exact IV:
    - Plaintext:
    - Expected ciphertext:
    - Actual ciphertext:
  • Memory Placement:
    - Input buffer address:
    - Output buffer address:
    - Are the buffers located in Secure RAM?
    - Are the buffers properly aligned?
    - Linker section used:
  • Reproduction Behavior:
    - Is the first run different from the subsequent runs?
    - Are all runs identical?
    - Does it match the online calculator?
    - Does it match the NIST vector?
    - Number of iterations tested:
  • Fault Information:
    - HardFault occurs:
    - SecureFault occurs:
    - CFSR:
    - HFSR:
    - BFAR:
    - MMFAR:
    - Stacked PC:
    - Stacked LR:
    - Call stack:

Best regards 

Lobna

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. 

  • Device / Software:
    - STM32H563ZIT6
    - LQFP-144
    - STM32CubeMX version: 6.17.0
    - CMOX version: No idea where to find this, but generally libraries 
    - Compiler / toolchain: arm-none-eabi-gcc
    - IDE version: 2.1.0

  • Execution Context:
    - Secure or Non-Secure execution: Executing encryption function in Secure zone
    - TrustZone enabled: Yes
    - SAU configuration: Default?
    - GTZC configuration: Default?
    - MPU configuration: Default?
  • CRC Setup:
    - Is the CRC clock enabled before the first crypto call? No
    - Is the CRC peripheral reset to a known state? Should be in its default state after initialization
    - Does any other code access CRC before CMOX? No
    - Is CRC assigned to the correct security domain? Yes
    - CRC initialization code:
     
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 */
}
  • Crypto Parameters:
    - Algorithm: AES-256
    - Mode: CBC
    - Key size: 32 bytes
    - IV size: 16 bytes
    - Padding mode: None
    - Input length: 16 bytes
  • Test Vector: 
    - Exact key: 0x603DEB1015CA71BE2B73AEF0857D77811F352C073B6108D72D9810A30914DFF4
    - Exact IV: 0x000102030405060708090A0B0C0D0E0F
    - Plaintext: 0x6BC1BEE22E409F96E93D7E117393172A
    - Expected ciphertext: 0xF58C4C04D6E5F1BA779EABFB5F7BFBD6
    - Actual ciphertext:Hardfault
  • Memory Placement:
    Would be good to know how to check all of theese. Buffers are generally declared as uint8_t key[32] and so before calling all of the cmox functions.
    - Input buffer address:
    - Output buffer address:
    - Are the buffers located in Secure RAM?
    - Are the buffers properly aligned?
    - Linker section used:
  • Reproduction Behavior:
    - Is the first run different from the subsequent runs? No
    - Are all runs identical? Yes
    - Does it match the online calculator? Hardfault
    - Does it match the NIST vector? Hardfault
    - Number of iterations tested: a lot
  • Fault Information:
    - HardFault occurs: Yes
    - SecureFault occurs: No
    - CFSR: 1048576
    - HFSR: 1073741824
    - BFAR: 805633116
    - MMFAR: 805633116
    - Stacked PC: 0xc0019d8
    - Stacked LR: -23
    - Call stack: Corrupted.

    For the second part of the problem, when running same exact code in non-secure area:
    Getting different values for the first run compared to all other runs seems to have been some other function using CRC engine before the encryption, that got sorted out with the fresh project and only encryption function tests. Besides that, return value still does not match the expected value. 
    Expected ciphertext from NIST: 0xF58C4C04D6E5F1BA779EABFB5F7BFBD6 
    Actual ciphertext from STM: 0x17A6495D11D8BEF42610AD89AFBD5633
    Returned ciphertext does not match neither NIST nor online calculator (online calculator matches NIST).
    Code snippet for actual encryption:
     
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

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

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

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)

 

lobna_1-1776091445847.png

 

The hard fault will disappear

Best regards 

Lobna

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

Dear @DBara.1 

Have you enabled TZEN?

BR

Lobna

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