1 (edited by rahmanikivi724 2021-10-19 08:47:44)

Topic: Implementation of ECC on STM32f446

Dear friends
I am going to implement ECC on launchpad stm32 Nucleo f446re + with FreeRTOS

First. because I am a new guy in this field what configuration I have to do before programming.

I did these configurations
1- I compiled the library for STM32 cortex m4 and linked it to my IDE
2- I defined these Items based of WOLFSSL STM32 SUPPORT
https://www.wolfssl.com/docs/stm32

WOLFSSL_STM32F4
FREERTOS
HAVE_ECC_SIGN
HAVE_ECC_VERIFY
STM32_RNG
STM32_CRYPTO
STM32

but the program gets stuck in the rng initiation function?

 wc_InitRng(&rng);

I removed all other function to find the problem you can see the code here, it is Freertos template with two tasks that I put this function in task 2

#include <stdio.h>
#include <string.h>
#include <wolfssl/wolfcrypt/random.h>
#include <wolfssl/options.h>
#include <wolfssl/wolfcrypt/settings.h>
#include <wolfssl/wolfcrypt/ecc.h>
#include <wolfssl/wolfcrypt/sha256.h>
#include <wolfssl/wolfcrypt/hash.h>
#include "stm32f4xx.h"
#include "FreeRTOS.h"  //provides TaskHandle_t
#include "task.h"  //to create tasks
/**define some macro for making condition in tasks***************************************/

/**define global variable **************************************************************/
TaskHandle_t xTaskHandle1=NULL;
TaskHandle_t xTaskHandle2=NULL;

WC_RNG rng;  // Data structure to keep random number
int ret = 0; // defining variable to keep status
/**prototype of functions****************************************************************/
extern void initialise_monitor_handles();  //0-to enable Semihosting -

void vTask1_handler(void *params);
void vTask2_handler(void *params);
/**main()********************************************************************************/
int main(void)
{
    //0-to enable Semihosting  exclude syscalls.c printf()
    initialise_monitor_handles();
    //project would work fine even without these two steps
    //1.Resets the RCC clock configuration to the default reset state.HSI= ON, PLL OFF, system clock = 16 MHz, cpu_clock= 16MHz
    RCC_DeInit();
    //2.update the SystemCoreClock variable
    SystemCoreClockUpdate();

    //3. lets create 2 tasks, task-1 and task-2
    xTaskCreate( vTask1_handler, "Hello_Task-1", configMINIMAL_STACK_SIZE, NULL, 2, &xTaskHandle1);
    xTaskCreate( vTask2_handler, "Hello_Task-2", configMINIMAL_STACK_SIZE, NULL, 2, &xTaskHandle2);

    printf("Start Program \n");

    //4. enable scheduler to schedule tasks to run
     vTaskStartScheduler();

    for(;;);
}



/**Task 1********************************************************************************/
void vTask1_handler(void *params)
{

    while(1)
    {
        printf("Start Task1\n");


        printf("ret is =%d",ret);
        printf("Finish Task 1 \n");
    }
}
/**Task 2********************************************************************************/
void vTask2_handler(void *params)
{
    while(1)
        {
        printf("Start Task2\n");
        ret = wc_InitRng(&rng); //Gets the seed (from OS) and key cipher for rng.
        printf("ret is =%d",ret);
        printf("Finish Task 2 \n");

        }
}

second, do you have any suggestions?

Share

Re: Implementation of ECC on STM32f446

Hi rahmanikivi724,

A couple of things to try.
First, you should always call wolfSSL_Init() before any other wolfSSL functions.
Second, try defining WOLFSSL_STM32F427_RNG to use our STM32F4xx codepath for your RNG.

If this doesn't help, please enable debugging by building with DEBUG_WOLFSSL and running wolfSSL_Debugging_ON() before your code, and attach debug logs.  The return code of wc_InitRng would also be helpful.

Thanks,
Kareem

Share

3 (edited by rahmanikivi724 2021-10-20 10:28:25)

Re: Implementation of ECC on STM32f446

thanks for your help

when I tried to put wolfSSL_Init() I am needed to include ssl.h so when i include ssl.h I
received this error

C:/00-libs/wolfssl-m4-flag-hard2/include/wolfssl/wolfio.h:157:22: fatal error: sys/socket.h: No such file or directory
             #include <sys/socket.h>
                      ^~~~~~~~~~~~~~
compilation terminated.

also when defined WOLFSSL_STM32F427_RNG it asked for #include "stm32f427xx.h" which is not in my include

C:/00-libs/wolfssl-m4-flag-hard2/include/wolfssl/wolfcrypt/settings.h:1266:22: fatal error: stm32f427xx.h: No such file or directory
             #include "stm32f427xx.h"
                      ^~~~~~~~~~~~~~
compilation terminated.

do you think that I missed something?
can you give a  rng example data structure to put instead of generating and passing this function It is just a test

rng=XXXXXXXdatastructure

Share

Re: Implementation of ECC on STM32f446

Happy to help.

You need to define WOLFSSL_NO_SOCK as well, also make sure you are including options.h and settings.h before any other wolfSSL headers.  I would recommend you to use our STM32 user_settings.h template, found here: https://github.com/wolfSSL/wolfssl/blob … gs_stm32.h
Yes, you would need to modify this include to use your platform's header instead, most likely "stm32f446xx.h".  You can find this include in wolfssl/wolfcrypt/settings.h around line 1266.  However I would first try running wolfSSL_Init() and confirm that doesn't fix the issue.
You are using the correct data structure WC_RNG in your code.  After initializing the RNG with wc_InitRng, call wc_RNG_GenerateBlock or wc_RNG_GenerateByte to generate random data.

Share

5 (edited by rahmanikivi724 2021-10-21 03:05:56)

Re: Implementation of ECC on STM32f446

now I realized something after several tries that is this data

wolfSSL_Init() return 1
wc_InitRng() return -199
wolfSSL_Debugging_ON() return -174

there is a problem with wc_InitRng() which would be impossible

I tried to use the user setting configuration which you suggest and my MCU and board (STM32F446RE  or STM32F446XX) is not in the list

/* ------------------------------------------------------------------------- */
/* Hardware platform */
/* ------------------------------------------------------------------------- */
#define NO_STM32_HASH
#define NO_STM32_CRYPTO

#if defined(STM32WB55xx)
    #define WOLFSSL_STM32WB
    #define WOLFSSL_STM32_PKA
    #undef  NO_STM32_CRYPTO
    #define HAL_CONSOLE_UART huart1
#elif defined(STM32F407xx)
    #define WOLFSSL_STM32F4
    #define HAL_CONSOLE_UART huart2
#elif defined(STM32F437xx)
    #define WOLFSSL_STM32F4
    #undef  NO_STM32_HASH
    #undef  NO_STM32_CRYPTO
    #define STM32_HAL_V2
    #define HAL_CONSOLE_UART huart4
#elif defined(STM32F777xx)
    #define WOLFSSL_STM32F7
    #undef  NO_STM32_HASH
    #undef  NO_STM32_CRYPTO
    #define STM32_HAL_V2
    #define HAL_CONSOLE_UART huart2
#elif defined(STM32H753xx)
    #define WOLFSSL_STM32H7
    #undef  NO_STM32_HASH
    #undef  NO_STM32_CRYPTO
    #define HAL_CONSOLE_UART huart3
#elif defined(STM32L4A6xx)
    #define WOLFSSL_STM32L4
    #undef  NO_STM32_HASH
    #undef  NO_STM32_CRYPTO
    #define HAL_CONSOLE_UART hlpuart1
#elif defined(STM32L475xx)
    #define WOLFSSL_STM32L4
    #define HAL_CONSOLE_UART huart1
#elif defined(STM32L562xx)
    #define WOLFSSL_STM32L5
    #define WOLFSSL_STM32_PKA
    #undef  NO_STM32_HASH
    #undef  NO_STM32_CRYPTO
    #define HAL_CONSOLE_UART huart1
#elif defined(STM32L552xx)
    #define WOLFSSL_STM32L5
    #undef  NO_STM32_HASH
    #define HAL_CONSOLE_UART hlpuart1
#elif defined(STM32F207xx)
    #define WOLFSSL_STM32F2
    #define HAL_CONSOLE_UART huart3
#elif defined(STM32F107xC)
    #define WOLFSSL_STM32F1
    #define HAL_CONSOLE_UART huart4
    #define NO_STM32_RNG
#elif defined(STM32F401xE)
    #define WOLFSSL_STM32F4
    #define HAL_CONSOLE_UART huart2
    #define NO_STM32_RNG
    #define WOLFSSL_GENSEED_FORTEST
#elif defined(STM32G071xx)
    #define WOLFSSL_STM32G0
    #define HAL_CONSOLE_UART huart2
    #define NO_STM32_RNG
    #define WOLFSSL_GENSEED_FORTEST
#else
    #warning Please define a hardware platform!
    /* This means there is not a pre-defined platform for your board/CPU */
    /* You need to define a CPU type, HW crypto and debug UART */
    /* CPU Type: WOLFSSL_STM32F1, WOLFSSL_STM32F2, WOLFSSL_STM32F4,
        WOLFSSL_STM32F7, WOLFSSL_STM32H7, WOLFSSL_STM32L4 and WOLFSSL_STM32L5 */
    #define WOLFSSL_STM32F4

    /* Debug UART used for printf */
    /* The UART interface number varies for each board/CPU */
    /* Typically this is the UART attached to the ST-Link USB CDC UART port */
    #define HAL_CONSOLE_UART huart4

    /* Hardware Crypto - uncomment as available on hardware */
    //#define WOLFSSL_STM32_PKA
    //#define NO_STM32_RNG
    //#undef  NO_STM32_HASH
    //#undef  NO_STM32_CRYPTO
    //#define WOLFSSL_GENSEED_FORTEST
    //#define STM32_HAL_V2
#endif

so I just define WOLFSSL_STM32F4 then I always receive the error of defining hardware platform and a missing file "stm32f4xx_hal.h"

C:/02-RtosWorkplace/STM32_HelloWorld/Config/user_settings.h:181:6: warning: #warning Please define a hardware platform! [-Wcpp]
     #warning Please define a hardware platform!
      ^~~~~~~
C:/02-RtosWorkplace/STM32_HelloWorld/Config/user_settings.h:186:0: warning: "WOLFSSL_STM32F4" redefined
     #define WOLFSSL_STM32F4
 
<command-line>:0:0: note: this is the location of the previous definition
In file included from ../src/bareMetal.c:4:0:
C:/00-libs/wolfssl-m4-flag-hard2/include/wolfssl/wolfcrypt/settings.h:1296:22: fatal error: stm32f4xx_hal.h: No such file or directory
             #include "stm32f4xx_hal.h"


around line 1266 of the setting file if I define WOLFSSL_STM32F427_RNG it asks for the "stm32f427xx.h" but this is not the file that I have this is "stm32f4xx.h"

        #ifdef WOLFSSL_STM32F427_RNG
            #include "stm32f427xx.h"
        #endif

finally, is there a manual that shows how to config settings? or how to config this library for this board, or which definitions I need to use ?

Share

Re: Implementation of ECC on STM32f446

You should have stm32f4xx_hal.h, it needs to be generated by STM32CubeIde.  You may be running into a path issue, make sure your include paths include stm32f4xx_hal.h.
You need to add your platform to the user_settings.h template, see our documentation: https://github.com/wolfSSL/wolfssl/tree … /STM32Cube and user_settings.h template under "#warning Please define a hardware platform!".  You should define NO_STM32_HASH/CRYPTO if applicable for your platform.  You do not need WOLFSSL_STM32F427_RNG defined.
Also see our video here: https://www.youtube.com/watch?v=pUd2HEfBp3w
For debugging, you need to build with DEBUG_WOLFSSL and you need to call wolfSSL_Debugging_ON() before any other code.

Share

Re: Implementation of ECC on STM32f446

hello again

in this implementation I used System Workbench for STM32 as IDE almost all guides are related to STM32Cube, so I Installed STM32Cube and start again from zero,

1- in this IDE I didn't need to link wolfssl library, so based on the video and the documentation that you mentioned
- I created a new project for STM32F446re
- added wolfssl pack to the project

2-to keep everything simple I didn't use FreeRTOS so I let wolfssl configuration be unchanged "Single Threaded" also I did not enable FreeRTOS too,

3-I sat the configurations like the guide in the documentation

4- I opened the main.c file and did not edit or put anything just I click on debug

5-  2 errors appeared 
I-

#warning Please define a hardware platform!

for this error
because my board is not listed so I think it doesn't have any hardware to accelerate crypto functions so based on the guide

#else
    #warning Please define a hardware platform!
    /* This means there is not a pre-defined platform for your board/CPU */
    /* You need to define a CPU type, HW crypto and debug UART */
    /* CPU Type: WOLFSSL_STM32F1, WOLFSSL_STM32F2, WOLFSSL_STM32F4,
        WOLFSSL_STM32F7, WOLFSSL_STM32H7, WOLFSSL_STM32L4 and WOLFSSL_STM32L5 */
    #define WOLFSSL_STM32F4

    /* Debug UART used for printf */
    /* The UART interface number varies for each board/CPU */
    /* Typically this is the UART attached to the ST-Link USB CDC UART port */
    #define HAL_CONSOLE_UART huart4

    /* Hardware Crypto - uncomment as available on hardware */
    //#define WOLFSSL_STM32_PKA
    //#define NO_STM32_RNG
    //#undef  NO_STM32_HASH
    //#undef  NO_STM32_CRYPTO
    //#define WOLFSSL_GENSEED_FORTEST
    //#define STM32_HAL_V2
#endif

I defined NO_STM32_RNG ,also these are defined by default  NO_STM32_HASH and  NO_STM32_CRYPTO
the error persisted the only way to solve it smile is to make the warning line to be a comment

II-

../Middlewares/Third_Party/wolfSSL_wolfSSL_wolfSSL/wolfssl/wolfcrypt/src/port/st/stm32.c:39:14: fatal error: wolfcrypt/src/misc.c: No such file or directory
   39 |     #include <wolfcrypt/src/misc.c>

for this error
I defined NO_INLINE and it solves it smile

then I compiled this empty project I received a new error

../Middlewares/Third_Party/wolfSSL_wolfSSL_wolfSSL/wolfssl/wolfcrypt/src/random.c:2587:6: error: #error "you need to write an os specific wc_GenerateSeed() here"
 2587 |     #error "you need to write an os specific wc_GenerateSeed() here"
      |      ^~~~~

this error comes from random.c line 2585

#elif defined(NO_DEV_RANDOM)

    #error "you need to write an os specific wc_GenerateSeed() here"

    /*
    int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
    {
        return 0;
    }
    */

what is your suggestion why it is empty and there is not any seed generation code? do you have any seed generation code suggestion that works with STM32F446RE ?

also I try to define CUSTOM_RAND_GENERATE_BLOCK it was not successful I received several undeclareds

Share

8 (edited by rahmanikivi724 2021-10-26 10:45:51)

Re: Implementation of ECC on STM32f446

hello again

as you know I started to implement ecc with your library.
I followed the video and as I told in a previous thread I had a problem with wc_GenerateSeed()
so I defined and problem solved

#define WOLFSSL_GENSEED_FORTEST

now I have this problem

../Middlewares/Third_Party/wolfSSL_wolfSSL_wolfSSL/wolfssl/src/ssl.c:149:10: fatal error: wolfcrypt/src/evp.c: No such file or directory
  149 | #include "wolfcrypt/src/evp.c"

in this case, I think it should not ask for this file I reviewed ssl.c I didn't find any define to avoid calling evp.c

Share

Re: Implementation of ECC on STM32f446

Hello rahmanikivi724,

Your platform does have a hardware RNG, you can see the details here: https://www.st.com/en/microcontrollers- … 446re.html
So you should undefine NO_STM32_RNG so we use our STM32 hardware RNG support, which will include a wc_GenerateSeed implementation.  You should also undefine WOLFSSL_GENSEED_FORTEST.

Thanks,
Kareem

Share

10 (edited by rahmanikivi724 2021-11-03 06:37:21)

Re: Implementation of ECC on STM32f446

thanks kareem

yes it says hardware RNG is supported , I am reading the manual to read more about it.

with defining WOLFSSL_GENSEED_FORTEST I managed to run Ecc with this configuration

#define ECC_KEY_SIZE  48
#define ECC_KEY_CURVE ECC_SECP256R1

when I changed it to SECP256K1

#define ECC_KEY_SIZE  48
#define ECC_KEY_CURVE ECC_SECP256K1

I received error 172 from make key function some other function send error based on this problem

make_key_Func= -172
 PrivateKey_Func= -170
 sign_Func= -170
 verify_Func=-171

based on the wolfssl manual 172 means

ECC_CURVE_OID_E    -172    Unsupported ECC OID curve type

then I tried defining these two that I found in ecc.c

#define HAVE_ECC_KOBLITZ  
#define WOLFSSL_CUSTOM_CURVES

then I received an error

error: conflicting types for 'ByteToHex'
16503 | static void ByteToHex(byte n, char* str)
      |             ^~~~~~~~~
In file included from ../Middlewares/Third_Party/wolfSSL_wolfSSL_wolfSSL/wolfssl/wolfcrypt/src/asn.c:86:
../Middlewares/Third_Party/wolfSSL_wolfSSL_wolfSSL/wolfssl/wolfcrypt/src/misc.c:438:26: note: previous definition of 'ByteToHex' was here
  438 | WC_STATIC WC_INLINE char ByteToHex(byte in)

I realized there is a definition of ByteToHex inside two files miscs.c and asn.c 
if I remove misc.c by defining NO_INLINE , I will receive an error like this

undefined reference to `ForceZero'.

Share

Re: Implementation of ECC on STM32f446

We recently released 5.0 which fixes various bugs in 4.8.1, please try updating and let me know if you still see any issues.

Share

Re: Implementation of ECC on STM32f446

thanks for bugs fixing

It works now !! smile

Share