打造一个通用性MCU架构,支持CX32/AT32/NRF51/NRF52等。 OS支持RTX4/RTX5/FreeRtos。 采用VsCode+GCC组合,VsCode+KEIL5,超强开发方式。 QQ群:524408033

LiSun

打造一个通用性MCU架构,支持CX32/AT32/NRF51/NRF52等。 OS支持RTX4/RTX5/FreeRtos。 采用VsCode+GCC组合,VsCode+KEIL5,超强开发方式。 QQ群:524408033

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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 <stdint.h>
#include <stdbool.h>

#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[i] = 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 <stdint.h>

/* Public Function Prototypes ------------------------------------------------*/

void random_vector_generate(uint8_t * pbuff, uint8_t size);
#endif

posted on 2022-08-13 11:00  xuejianqiang  阅读(19)  评论(0编辑  收藏  举报
打造一个通用性MCU架构,支持CX32/AT32/NRF51/NRF52等。 OS支持RTX4/RTX5/FreeRtos。 采用VsCode+GCC组合,VsCode+KEIL5,超强开发方式。 QQ群:524408033