HardFault Error in Private Key Parsing with mbedTLS and FreeRTOS

Hi everyone,

I’m currently working on integrating mbedTLS with FreeRTOS in my project, using STM32CubeIDE as my development environment. However, I keep running into a HardFault error when trying to parse a private key using mbedTLS.

Here’s what I’ve observed:

  • When I use mbedTLS without FreeRTOS, everything works perfectly.
  • The issue arises only when FreeRTOS is included in the setup.

I suspect the issue could be related to one of the following:

  1. Task stack size?
  2. Heap management (e.g., memory allocation issues)?
  3. Any specific thread-safety considerations with mbedTLS?

I’ve attached a screenshot of the function call stack to provide more context. Please take a look—any guidance or suggestions would be greatly appreciated!

Thanks in advance!

When you use MbedTLS with FreeRTOS, you need to enable threading support in MbedTLS (by defining MBEDTLS_THREADING_C in mbedtls config file) and supply some platform functions. You can refer to the following -

1 Like

Hi Gaurav,

I have made the changes you suggested, but I am still encountering the same error

Does not seem correct because the call to mbedtls_calloc should not go into _call0oc_r. Can you see why the call is not getting routed to here - FreeRTOS/FreeRTOS-Plus/VisualStudio_StaticProjects/MbedTLS/mbedtls_freertos_port.c at main · FreeRTOS/FreeRTOS · GitHub.

You likely need these lines in your mbedTLS config file.