cancel
Showing results for 
Search instead for 
Did you mean: 

ECC Project for STM32G4 not working using STM Cryptographic library and importing cryptographic library to existing project

mickey
Associate II

The board connects but when I build and run the project, the light does not blink as it says it should.

Also, I'm using mac, how do I import cryptolib into an existing project? I followed this instructions and this wiki but it's not working.

1 ACCEPTED SOLUTION

Accepted Solutions
Jocelyn RICARD
ST Employee

Hello Mickey,

I understand you want to use the software library X-CUBE-CRYPTOLIB.

In this package you have examples like

STM32CubeExpansion_Crypto_V4.1.0\Projects\NUCLEO-G474RE\Applications\ECC\ECDSA_SignVerify\

As you are talking about a LED that should blink, yes in the example we use the LED to show that it worked fine. Now, this works only if you use the target Nucleo (STM32G474RE).

You seem to be using the Nucleo STM32G431 which uses a different GPIO to control the LED.

Anyway, the use of the LED to show the result is really not important.

I would just suggest launching your favorite debugger and see how it goes in your project.

If you followed the steps described in the Wiki, it should work fine.

Best regards

Jocelyn

View solution in original post

6 REPLIES 6

Which board specifically are you using?

STM32Cube_FW_G4_V1.5.1\Projects\STM32G474E-EVAL\Examples\CRYP\CRYP_DMA\readme.txt

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

I’m using STM32G431KBT6

Jocelyn RICARD
ST Employee

Hello Mickey,

I understand you want to use the software library X-CUBE-CRYPTOLIB.

In this package you have examples like

STM32CubeExpansion_Crypto_V4.1.0\Projects\NUCLEO-G474RE\Applications\ECC\ECDSA_SignVerify\

As you are talking about a LED that should blink, yes in the example we use the LED to show that it worked fine. Now, this works only if you use the target Nucleo (STM32G474RE).

You seem to be using the Nucleo STM32G431 which uses a different GPIO to control the LED.

Anyway, the use of the LED to show the result is really not important.

I would just suggest launching your favorite debugger and see how it goes in your project.

If you followed the steps described in the Wiki, it should work fine.

Best regards

Jocelyn

That makes sense, Jocelyn. Thank you!

I am looking at ECDSA_SharedSecretGeneration example and I get this error:

../Core/Src/main.c:30:6: error: unknown type name 'TestStatus'

../Core/Src/main.c:30:31: error: 'FAILED' undeclared here (not in a function)

../Core/Src/main.c:71:13: error: static declaration of 'Error_Handler' follows non-static declaration

../Core/Src/main.c:190:18: error: 'PASSED' undeclared (first use in this function)

make: *** [Core/Src/subdir.mk:37: Core/Src/main.o] Error 1

#include "main.h"
#include "cmox_crypto.h"
#include <string.h>
 
/* Global variables ----------------------------------------------------------*/
/* ECC context */
cmox_ecc_handle_t Ecc_Ctx;
/* ECC working buffer */
uint8_t Working_Buffer[2000];
 
__IO TestStatus glob_status = FAILED;
 
const uint8_t Private_Key[] =
{
  0x7d, 0x7d, 0xc5, 0xf7, 0x1e, 0xb2, 0x9d, 0xda, 0xf8, 0x0d, 0x62, 0x14, 0x63, 0x2e, 0xea, 0xe0,
  0x3d, 0x90, 0x58, 0xaf, 0x1f, 0xb6, 0xd2, 0x2e, 0xd8, 0x0b, 0xad, 0xb6, 0x2b, 0xc1, 0xa5, 0x34
};
const uint8_t Remote_Public_Key[] =
{
  0x70, 0x0c, 0x48, 0xf7, 0x7f, 0x56, 0x58, 0x4c, 0x5c, 0xc6, 0x32, 0xca, 0x65, 0x64, 0x0d, 0xb9,
  0x1b, 0x6b, 0xac, 0xce, 0x3a, 0x4d, 0xf6, 0xb4, 0x2c, 0xe7, 0xcc, 0x83, 0x88, 0x33, 0xd2, 0x87,
  0xdb, 0x71, 0xe5, 0x09, 0xe3, 0xfd, 0x9b, 0x06, 0x0d, 0xdb, 0x20, 0xba, 0x5c, 0x51, 0xdc, 0xc5,
  0x94, 0x8d, 0x46, 0xfb, 0xf6, 0x40, 0xdf, 0xe0, 0x44, 0x17, 0x82, 0xca, 0xb8, 0x5f, 0xa4, 0xac
};
const uint8_t Expected_SecretX[] =
{
  0x46, 0xfc, 0x62, 0x10, 0x64, 0x20, 0xff, 0x01, 0x2e, 0x54, 0xa4, 0x34, 0xfb, 0xdd, 0x2d, 0x25,
  0xcc, 0xc5, 0x85, 0x20, 0x60, 0x56, 0x1e, 0x68, 0x04, 0x0d, 0xd7, 0x77, 0x89, 0x97, 0xbd, 0x7b
};
 
/* Computed data buffer */
uint8_t Computed_Secret[CMOX_ECC_SECP256R1_SECRET_LEN];
 
/* Private function prototypes -----------------------------------------------*/
static void SystemClock_Config(void);
static void Error_Handler(void);
/* Functions Definition ------------------------------------------------------*/
 
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
 
 
/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
	  cmox_ecc_retval_t retval;
	  size_t computed_size;
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* USER CODE BEGIN Init */
 
  /* USER CODE END Init */
 
  /* Configure the system clock */
  SystemClock_Config();
 
  /* USER CODE BEGIN SysInit */
  /* Configure LED2 */
  //BSP_LED_Init(LED2);
  /* USER CODE END SysInit */
 
  /* Initialize all configured peripherals */
  //MX_CRC_Init();
  /* USER CODE BEGIN 2 */
  /* Initialize cryptographic library */
   if (cmox_initialize(NULL) != CMOX_INIT_SUCCESS)
   {
     Error_Handler();
   }
 
   cmox_ecc_construct(&Ecc_Ctx, CMOX_ECC256_MATH_FUNCS, Working_Buffer, sizeof(Working_Buffer));
 
 
   retval = cmox_ecdh(&Ecc_Ctx,                                         /* ECC context */
                      CMOX_ECC_CURVE_SECP256R1,                         /* SECP256R1 ECC curve selected */
                      Private_Key, sizeof(Private_Key),                 /* Local Private key */
                      Remote_Public_Key, sizeof(Remote_Public_Key),     /* Remote Public key */
                      Computed_Secret, &computed_size);                 /* Data buffer to receive shared secret */
 
   /* Verify API returned value */
   if (retval != CMOX_ECC_SUCCESS)
   {
     Error_Handler();
   }
 
   /* Verify generated data size is the expected one */
   if (computed_size != sizeof(Computed_Secret))
   {
     Error_Handler();
   }
 
   /* Verify generated data are the expected ones */
   if (memcmp(Computed_Secret, Expected_SecretX, sizeof(Expected_SecretX)) != 0)
   {
     Error_Handler();
   }
 
   /* Cleanup context */
   cmox_ecc_cleanup(&Ecc_Ctx);
 
   /* No more need of cryptographic services, finalize cryptographic library */
   if (cmox_finalize(NULL) != CMOX_INIT_SUCCESS)
   {
     Error_Handler();
   }
 
   /* Turn on LED2 in an infinite loop in case of ECC ECDH operations are successful */
   //BSP_LED_On(LED2);
   //HAL_GPIO_TogglePin(GPIOC, LD4);
   glob_status = PASSED;
   while (1)
   {}
 }
 
 
  /* USER CODE END 2 */
 
/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
 
  /** Configure the main internal regulator output voltage
  */
  HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
 
  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
 
  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
 
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  {
    Error_Handler();
  }
}
 
/**
  * @brief CRC Initialization Function
  * @param None
  * @retval None
  */
/*
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 */
 
//}
 
/* USER CODE BEGIN 4 */
 
/* USER CODE END 4 */
 
/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}
 
#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

Hello Mickey,

the errors you get are related to the content of main.h which basically contains:

...
#include "stm32g4xx_hal.h"
#include "stm32g4xx_nucleo.h"
 
 
/* Exported types ------------------------------------------------------------*/
#define FAILED  ((TestStatus)0)
#define PASSED  ((TestStatus)1)
typedef uint32_t TestStatus;
...

So, I suspect an issue related to this main.h file located in

STM32CubeExpansion_Crypto_V4.1.0\Projects\NUCLEO-G474RE\Applications\ECC\ECDH_SharedSecretGeneration\Inc\main.h

Best regards

Jocelyn

mickey
Associate II

that was it, thanks!