498 views|0 replies

501

Posts

4

Resources
The OP
 

【NUCLEO H533RE】Random number generator test [Copy link]

Complete project attachment, replace \STM32Cube_FW_H5_V1.2.0\Projects\NUCLEO-H533RE\Applications\ThreadX\Tx_Thread_Creation

Tx_Thread_Creation.zip (577.69 KB, downloads: 1)

  • Preface

This article continues to experience RNG random number generation. Random numbers can be used for AES to generate IV, etc.

2. Hardware Driver

Add driver file

stm32h5xx_hal_rng.c

Uncomment in stm32h5xx_hal_conf.h

#define HAL_RNG_MODULE_ENABLED

MSP Initialize RNG

/**

* @brief RNG MSP Initialization

* This function configures the hardware resources used in this example

* @param hrng: RNG handle pointer

* @retval None

*/

void HAL_RNG_MspInit(RNG_HandleTypeDef* hrng)

{

RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};

if(hrng->Instance==RNG)

{

/* USER CODE BEGIN RNG_MspInit 0 */

/* USER CODE END RNG_MspInit 0 */

/** Initializes the peripherals clock

*/

PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RNG;

PeriphClkInitStruct.RngClockSelection = RCC_RNGCLKSOURCE_HSI48;

if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)

{

Error_Handler();

}

/* Peripheral clock enable */

__HAL_RCC_RNG_CLK_ENABLE();

/* RNG interrupt Init */

HAL_NVIC_SetPriority(RNG_IRQn, 0, 0);

HAL_NVIC_EnableIRQ(RNG_IRQn);

/* USER CODE BEGIN RNG_MspInit 1 */

/* USER CODE END RNG_MspInit 1 */

}

}

/**

* @brief RNG MSP De-Initialization

* This function freeze the hardware resources used in this example

* @param hrng: RNG handle pointer

* @retval None

*/

void HAL_RNG_MspDeInit(RNG_HandleTypeDef* hrng)

{

if(hrng->Instance==RNG)

{

/* USER CODE BEGIN RNG_MspDeInit 0 */

/* USER CODE END RNG_MspDeInit 0 */

/* Peripheral clock disable */

__HAL_RCC_RNG_CLK_DISABLE();

/* RNG interrupt DeInit */

HAL_NVIC_DisableIRQ(RNG_IRQn);

/* USER CODE BEGIN RNG_MspDeInit 1 */

/* USER CODE END RNG_MspDeInit 1 */

}

}

RNG_HandleTypeDef hrng;

RNG Initialization

hrng.Instance = RNG;

hrng.Init.ClockErrorDetection = RNG_CED_ENABLE;

if (HAL_RNG_Init(&hrng) != HAL_OK)

{

Error_Handler();

}

Interrupt handling

extern RNG_HandleTypeDef hrng;

/**

* @brief This function handles RNG global interrupt.

*/

void RNG_IRQHandler(void)

{

/* USER CODE BEGIN RNG_IRQn 0 */

/* USER CODE END RNG_IRQn 0 */

HAL_RNG_IRQHandler(&hrng);

/* USER CODE BEGIN RNG_IRQn 1 */

/* USER CODE END RNG_IRQn 1 */

}

Generate random numbers

HAL_RNG_GenerateRandomNumber

3. Test

We add commands to generate random numbers and then send them to the PC. The PC uses a visualization tool to draw a curve to see if the curve is random.

shell_func.c

Declare the implementation function

static void rng_func(uint8_t* param);

New commands for calculation

{ (uint8_t*)"rng", rng_func, (uint8_t*)"rng testtimes"}, "},

The implementation is as follows

static void rng_func(uint8_t* param)

{

uint32_t test_times;

if(sscanf((const char*)param,"%*s %d",&test_times) == 1)

{

hrng.Instance = RNG;

hrng.Init.ClockErrorDetection = RNG_CED_ENABLE;

if (HAL_RNG_Init(&hrng) != HAL_OK)

{

}

for(uint32_t i=0; i<test_times; i++)

{

uint32_t randnum;

HAL_RNG_GenerateRandomNumber(&hrng,&randomnum);

tx_thread_sleep(10);

xprintf("/*%d*/\r\n",randnum);

}

if(HAL_RNG_DeInit(&hrng) != HAL_OK)

{

}

}

}

The test is as follows

Input rng 1000

Check the curve as follows

  • Summarize

Using a random number generator to generate random numbers makes the encryption result different each time, increasing the difficulty of cracking. This is the most commonly used method. This article experiences the random number generator of STM32H5, and the code is very simple.

Note that the HSI48 clock must be enabled.

This post is from stm32/stm8
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list