Topic: STM32 Hard Faults

I have been struggling for the last few days to get wolfCrypt to work on a STM32L432 microcontroller.  I am trying to first generate a Diffie Hellman keypair among other things.  No matter what I've tried, I still get hard fault errors.

Here are the exact steps to replicate the problem:

  • Install STM32CubeIDE (version 1.12.0 at time of writing)

  • Create a new STM32 project for STM32L432KCU6

  • Install the STM32Cube pack for STM32CubeIDE under Manage Software Packs

  • Select wolfCrypt Core from Select Components

  • Enable the RNG in Security

  • Enable wolfSSL in Middleware and Software Packs

  • Automatically resolve any clock issues

  • Generate Project Code

  • Change the hardware platform to WOLFSSL_STM32L4 under wolfSSL.I-CUBE-wolfSSL_conf.h and comment out the warning

  • Build and debug the code to make sure it runs without issues (it does)

  • In main.c add #include <wolfssl/wolfcrypt/dh.h>

  • In main.c add wolfCrypt_Init();

  • Copy/paste the example code under wc_DhGenerateKeyPair() https://www.wolfssl.com/doxygen/group__ … 6fe9dc3b72

  • Build the project (builds without errors)

  • Debug the project

This is where the problems occur.  The project immediately hard faults.  The CFSR register sometimes shows UNDEFINSTR, while other times it shows NOCP.  If I comment out the line with wc_DhGenerateKeyPair it runs fine, so I believe this problem is related to wolfCrypt in some way.

I've tried changing tons of options in CubeMX as well as the wolfSSL settings file.  I have tried older versions of STM32CubeIDE as well as older versions of wolfSSL.  Nothing seems to help.

Any suggestions?

Share

Re: STM32 Hard Faults

Hi Shammon,

Thanks for joining the wolfSSL Forums. Please try including the wolfSSL config header before any other wolfSSL includes:

#ifndef WOLFSSL_USER_SETTINGS
    #include <wolfssl/options.h>
#endif
#include <wolfssl/wolfcrypt/settings.h>

Thanks,
Eric - wolfSSL Support

Re: STM32 Hard Faults

Hi Shammon,

This sounds like a stack issue. Please try increasing your stack memory for the thread or adjust in the linker script if using bare-metal. What size DH key are you trying to generate? Have you tried running the wolfCrypt tests first? Make sure you have the right DH parameters enabled via `HAVE_FFDHE_2048`, `HAVE_FFDHE_3072`, `HAVE_FFDHE_4096`, etc.

Thanks,
David Garske, wolfSSL

Share

Re: STM32 Hard Faults

Thank you both for your responses.

I tried including the wolfSSL config header before other includes but it made no difference.

I also tried increasing the minimum stack size with no changes.  I am using HAVE_FFDHE_2048.

I cannot run the wolfCrypt tests because my microcontroller is not large enough to run wolfCryptDemo if that is what you're referring to.

Share

Re: STM32 Hard Faults

Hi Shammon,

I still suspect you are running out of memory because DH uses a large amount from the stack. Have you considered using ECDHE?

You might also consider trying a different math library option. We have several (see https://github.com/wolfSSL/wolfssl/blob … .ftl#L202)

I recommend you try with a small math implementation like `3=Single Precision C (only common curves/key sizes)`.

Thanks,
David Garske, wolfSSL

Share

Re: STM32 Hard Faults

Thank you David!  You were indeed correct.  I switched to ECDH with wc_ecc_shared_secret using "6=Single Precision C all small" and my code is finally able to run.  Any other math option causes the hard fault to occur.

Do you have any other suggestions to minimize wolfCrypt memory usage as I begin to add in all my other code?

Share

Re: STM32 Hard Faults

Hi Shammon,

Each asymmetric algorithm has different total memory requirements. We have several build options for altering where this memory comes from. For example you can put on heap, stack or static memory.

By default the memory comes mostly from the stack. If you'd prefer to use the heap then I would should consider updating to the latest wolfSSL in GitHub and using the `WOLFSSL_SP_SMALL_STACK` option, which is new. Or you could just try `WOLFSSL_SMALL_STACK` with your current release.

The RSA/DH algorithms have a much lager key size and generally use more memory. The ECC (ECDHE/ECDSA) and ED25519 algorithms use much less memory and can also be used with TLS v1.2 and v1.3.

Perhaps you can tell me more about your configuration such as RTOS/bare-metal, available heap/stack and TLS requirements?

Thanks,
David Garske, wolfSSL

Share