Random Number Generator (RNG)
The Random Number Generator (RNG) generates true non-deterministic random numbers derived from thermal noise that are suitable for cryptographic purposes. The RNG does not require a seed value.
随机数生成器(RNG)从热噪声中生成真实的非确定性随机数,适合于加密目的。 RNG不需要种子值。
/********************************************************************************
* @file bsp_rng.c
* @author jianqiang.xue
* @version V1.0.0
* @date 2021-07-09
* @brief 随机数生成
********************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include
#include
#include "RTE_Components.h"
#include CMSIS_device_header
#include "nrf_rng.h"
#include "nrf_error.h"
#include "bsp_rng.h"
/* Private Function Prototypes -----------------------------------------------*/
/**
* @brief 启动硬件随机数,并返回一个随机数值
* @note NULL
* @retval 0-255 随机数
*/
static uint8_t get_random_vector(void)
{
uint8_t value;
NRF_RNG->CONFIG = 1;
NRF_RNG->TASKS_START = 1;
// 生成新的随机数并写入VALUE寄存器。
NRF_RNG->EVENTS_VALRDY = 0;
while (NRF_RNG->EVENTS_VALRDY == 0)
{
}
value = NRF_RNG->VALUE;
NRF_RNG->TASKS_STOP = 1;
NRF_RNG->INTENCLR = 0;
NRF_RNG->CONFIG = 0;
return value;
}
/**
* @brief 得到一组随机数
* @note NULL
* @param pbuff: 缓存区
* @param size: 数量
* @retval None
*/
void random_vector_generate(uint8_t *pbuff, uint8_t size)
{
uint8_t i;
for (i = 0; i < size; i++)
{
pbuff = get_random_vector();
}
}
/********************************************************************************
* @file bsp_rng.h
* @author jianqiang.xue
* @version V1.0.0
* @date 2021-07-09
* @brief 随机数生成
********************************************************************************/
#ifndef __BSP_RNG_H
#define __BSP_RNG_H
/* Includes ------------------------------------------------------------------*/
#include
/* Public Function Prototypes ------------------------------------------------*/
void random_vector_generate(uint8_t * pbuff, uint8_t size);
#endif
Random Number Generator (RNG)
The Random Number Generator (RNG) generates true non-deterministic random numbers derived from thermal noise that are suitable for cryptographic purposes. The RNG does not require a seed value.
随机数生成器(RNG)从热噪声中生成真实的非确定性随机数,适合于加密目的。 RNG不需要种子值。
/********************************************************************************
* @file bsp_rng.c
* @author jianqiang.xue
* @version V1.0.0
* @date 2021-07-09
* @brief 随机数生成
********************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include
#include
#include "RTE_Components.h"
#include CMSIS_device_header
#include "nrf_rng.h"
#include "nrf_error.h"
#include "bsp_rng.h"
/* Private Function Prototypes -----------------------------------------------*/
/**
* @brief 启动硬件随机数,并返回一个随机数值
* @note NULL
* @retval 0-255 随机数
*/
static uint8_t get_random_vector(void)
{
uint8_t value;
NRF_RNG->CONFIG = 1;
NRF_RNG->TASKS_START = 1;
// 生成新的随机数并写入VALUE寄存器。
NRF_RNG->EVENTS_VALRDY = 0;
while (NRF_RNG->EVENTS_VALRDY == 0)
{
}
value = NRF_RNG->VALUE;
NRF_RNG->TASKS_STOP = 1;
NRF_RNG->INTENCLR = 0;
NRF_RNG->CONFIG = 0;
return value;
}
/**
* @brief 得到一组随机数
* @note NULL
* @param pbuff: 缓存区
* @param size: 数量
* @retval None
*/
void random_vector_generate(uint8_t *pbuff, uint8_t size)
{
uint8_t i;
for (i = 0; i < size; i++)
{
pbuff = get_random_vector();
}
}
/********************************************************************************
* @file bsp_rng.h
* @author jianqiang.xue
* @version V1.0.0
* @date 2021-07-09
* @brief 随机数生成
********************************************************************************/
#ifndef __BSP_RNG_H
#define __BSP_RNG_H
/* Includes ------------------------------------------------------------------*/
#include
/* Public Function Prototypes ------------------------------------------------*/
void random_vector_generate(uint8_t * pbuff, uint8_t size);
#endif
举报