1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2026-01-01 01:41:18 +01:00

Merge pull request #4757 from haukepetersen/opt_periph_random

drivers/periph: renamed random and more
This commit is contained in:
Joakim Nohlgård 2016-02-14 17:56:57 +01:00
commit 9db8d671ab
55 changed files with 261 additions and 547 deletions

View File

@ -2,7 +2,7 @@
FEATURES_PROVIDED += periph_adc
FEATURES_PROVIDED += periph_cpuid
FEATURES_PROVIDED += periph_gpio
FEATURES_PROVIDED += periph_random
FEATURES_PROVIDED += periph_hwrng
FEATURES_PROVIDED += periph_rtt
FEATURES_PROVIDED += periph_spi
FEATURES_PROVIDED += periph_timer

View File

@ -98,13 +98,6 @@
#define RTT_PRESCALER (3275U) /* run with 10 Hz */
/** @} */
/**
* @name Random Number Generator configuration
* @{
*/
#define RANDOM_NUMOF (1U)
/** @} */
/**
* @name SPI configuration
* @{

View File

@ -1,8 +1,8 @@
# Put defined MCU peripherals here (in alphabetical order)
FEATURES_PROVIDED += periph_cpuid
FEATURES_PROVIDED += periph_gpio
FEATURES_PROVIDED += periph_hwrng
FEATURES_PROVIDED += periph_pwm
FEATURES_PROVIDED += periph_random
FEATURES_PROVIDED += periph_spi
FEATURES_PROVIDED += periph_timer
FEATURES_PROVIDED += periph_uart

View File

@ -86,13 +86,6 @@ static const uart_conf_t uart_config[] = {
#define UART_NUMOF (sizeof(uart_config) / sizeof(uart_config[0]))
/** @} */
/**
* @name Random Number Generator configuration
* @{
*/
#define RANDOM_NUMOF (1U)
/** @} */
/**
* @name SPI configuration
* @{

View File

@ -1,7 +1,7 @@
# Put defined MCU peripherals here (in alphabetical order)
FEATURES_PROVIDED += periph_cpuid
FEATURES_PROVIDED += periph_gpio
FEATURES_PROVIDED += periph_random
FEATURES_PROVIDED += periph_hwrng
FEATURES_PROVIDED += periph_timer
FEATURES_PROVIDED += periph_uart

View File

@ -105,13 +105,6 @@ extern "C" {
#define UART_1_CTS_PIN GPIO_PB0
/** @} */
/**
* @name Random Number Generator configuration
* @{
*/
#define RANDOM_NUMOF 1
/** @} */
/**
* @name GPIO configuration
* @{

View File

@ -2,9 +2,9 @@
FEATURES_PROVIDED += periph_adc
FEATURES_PROVIDED += periph_cpuid
FEATURES_PROVIDED += periph_gpio
FEATURES_PROVIDED += periph_hwrng
FEATURES_PROVIDED += periph_i2c
FEATURES_PROVIDED += periph_pwm
FEATURES_PROVIDED += periph_random
FEATURES_PROVIDED += periph_rtc
FEATURES_PROVIDED += periph_rtt
FEATURES_PROVIDED += periph_spi

View File

@ -280,10 +280,9 @@ extern "C"
* @name Random Number Generator configuration
* @{
*/
#define RANDOM_NUMOF (1U)
#define KINETIS_RNGA RNG
#define RANDOM_CLKEN() (SIM->SCGC6 |= (1 << 9))
#define RANDOM_CLKDIS() (SIM->SCGC6 &= ~(1 << 9))
#define KINETIS_RNGA RNG
#define HWRNG_CLKEN() (SIM->SCGC6 |= (1 << 9))
#define HWRNG_CLKDIS() (SIM->SCGC6 &= ~(1 << 9))
/** @} */
#ifdef __cplusplus

View File

@ -2,9 +2,9 @@
FEATURES_PROVIDED += periph_adc
FEATURES_PROVIDED += periph_dac
FEATURES_PROVIDED += periph_gpio
FEATURES_PROVIDED += periph_hwrng
FEATURES_PROVIDED += periph_i2c
FEATURES_PROVIDED += periph_pwm
FEATURES_PROVIDED += periph_random
FEATURES_PROVIDED += periph_spi
FEATURES_PROVIDED += periph_timer
FEATURES_PROVIDED += periph_uart

View File

@ -141,13 +141,6 @@ extern "C" {
#define DAC_0_CH1_PIN 5
/** @} */
/**
* @name Random Number Generator configuration
* @{
*/
#define RANDOM_NUMOF (1U)
/** @} */
/**
* @name UART configuration
* @{

View File

@ -2,9 +2,9 @@
FEATURES_PROVIDED += periph_adc
FEATURES_PROVIDED += periph_cpuid
FEATURES_PROVIDED += periph_gpio
FEATURES_PROVIDED += periph_hwrng
FEATURES_PROVIDED += periph_i2c
FEATURES_PROVIDED += periph_pwm
FEATURES_PROVIDED += periph_random
FEATURES_PROVIDED += periph_rtc
FEATURES_PROVIDED += periph_rtt
FEATURES_PROVIDED += periph_spi

View File

@ -505,9 +505,8 @@ extern "C"
* @name Random Number Generator configuration
* @{
*/
#define RANDOM_NUMOF (1U)
#define RANDOM_CLKEN() (BITBAND_REG32(SIM->SCGC3, SIM_SCGC3_RNGA_SHIFT) = 1)
#define RANDOM_CLKDIS() (BITBAND_REG32(SIM->SCGC3, SIM_SCGC3_RNGA_SHIFT) = 0)
#define HWRNG_CLKEN() (BITBAND_REG32(SIM->SCGC3, SIM_SCGC3_RNGA_SHIFT) = 1)
#define HWRNG_CLKDIS() (BITBAND_REG32(SIM->SCGC3, SIM_SCGC3_RNGA_SHIFT) = 0)
/** @} */
#ifdef __cplusplus

View File

@ -1,6 +1,6 @@
# Put defined MCU peripherals here (in alphabetical order)
FEATURES_PROVIDED += periph_cpuid
FEATURES_PROVIDED += periph_random
FEATURES_PROVIDED += periph_hwrng
FEATURES_PROVIDED += periph_rtc
FEATURES_PROVIDED += periph_timer

View File

@ -1,7 +1,7 @@
# Put defined MCU peripherals here (in alphabetical order)
FEATURES_PROVIDED += periph_cpuid
FEATURES_PROVIDED += periph_gpio
FEATURES_PROVIDED += periph_random
FEATURES_PROVIDED += periph_hwrng
FEATURES_PROVIDED += periph_rtt
FEATURES_PROVIDED += periph_timer
FEATURES_PROVIDED += periph_uart

View File

@ -99,13 +99,6 @@ extern "C" {
#define UART_PIN_CTS 10
/** @} */
/**
* @name Random Number Generator configuration
* @{
*/
#define RANDOM_NUMOF (1U)
/** @} */
/**
* @name Radio device configuration
*

View File

@ -1,7 +1,7 @@
# Put defined MCU peripherals here (in alphabetical order)
FEATURES_PROVIDED += periph_cpuid
FEATURES_PROVIDED += periph_gpio
FEATURES_PROVIDED += periph_random
FEATURES_PROVIDED += periph_hwrng
FEATURES_PROVIDED += periph_rtt
FEATURES_PROVIDED += periph_timer
FEATURES_PROVIDED += periph_uart

View File

@ -74,13 +74,6 @@ extern "C" {
#define UART_PIN_TX 6
/** @} */
/**
* @name Random Number Generator configuration
* @{
*/
#define RANDOM_NUMOF (1U)
/** @} */
#ifdef __cplusplus
}
#endif

View File

@ -1,7 +1,7 @@
# Put defined MCU peripherals here (in alphabetical order)
FEATURES_PROVIDED += periph_cpuid
FEATURES_PROVIDED += periph_gpio
FEATURES_PROVIDED += periph_random
FEATURES_PROVIDED += periph_hwrng
FEATURES_PROVIDED += periph_rtt
FEATURES_PROVIDED += periph_spi
FEATURES_PROVIDED += periph_timer

View File

@ -105,13 +105,6 @@ extern "C" {
#define UART_PIN_CTS 18
/** @} */
/**
* @name Random Number Generator configuration
* @{
*/
#define RANDOM_NUMOF (1U)
/** @} */
/**
* @name Radio device configuration
*

View File

@ -1,7 +1,7 @@
# Put defined MCU peripherals here (in alphabetical order)
FEATURES_PROVIDED += periph_cpuid
FEATURES_PROVIDED += periph_gpio
FEATURES_PROVIDED += periph_random
FEATURES_PROVIDED += periph_hwrng
FEATURES_PROVIDED += periph_timer
FEATURES_PROVIDED += periph_uart

View File

@ -95,13 +95,6 @@
#define UART_0_RX_PIN GPIO_PA0
/** @} */
/**
* @name Random Number Generator configuration
* @{
*/
#define RANDOM_NUMOF 1
/** @} */
/**
* @name GPIO configuration
* @{

View File

@ -2,9 +2,9 @@
FEATURES_PROVIDED += periph_adc
FEATURES_PROVIDED += periph_cpuid
FEATURES_PROVIDED += periph_gpio
FEATURES_PROVIDED += periph_hwrng
FEATURES_PROVIDED += periph_i2c
FEATURES_PROVIDED += periph_pwm
FEATURES_PROVIDED += periph_random
FEATURES_PROVIDED += periph_rtc
FEATURES_PROVIDED += periph_rtt
FEATURES_PROVIDED += periph_spi

View File

@ -325,10 +325,9 @@ extern "C"
* @name Random Number Generator configuration
* @{
*/
#define RANDOM_NUMOF (1U)
#define KINETIS_RNGA RNG
#define RANDOM_CLKEN() (SIM->SCGC6 |= (1 << 9))
#define RANDOM_CLKDIS() (SIM->SCGC6 &= ~(1 << 9))
#define KINETIS_RNGA RNG
#define HWRNG_CLKEN() (SIM->SCGC6 |= (1 << 9))
#define HWRNG_CLKDIS() (SIM->SCGC6 &= ~(1 << 9))
/** @} */

View File

@ -1,7 +1,7 @@
# Put defined MCU peripherals here (in alphabetical order)
FEATURES_PROVIDED += periph_cpuid
FEATURES_PROVIDED += periph_gpio
FEATURES_PROVIDED += periph_random
FEATURES_PROVIDED += periph_hwrng
FEATURES_PROVIDED += periph_rtt
FEATURES_PROVIDED += periph_timer
FEATURES_PROVIDED += periph_uart

View File

@ -101,13 +101,6 @@ extern "C" {
#define UART_PIN_CTS 10
/** @} */
/**
* @name Random Number Generator configuration
* @{
*/
#define RANDOM_NUMOF (1U)
/** @} */
/**
* @name Radio device configuration
*

View File

@ -2,7 +2,7 @@
FEATURES_PROVIDED += periph_adc
FEATURES_PROVIDED += periph_cpuid
FEATURES_PROVIDED += periph_gpio
FEATURES_PROVIDED += periph_random
FEATURES_PROVIDED += periph_hwrng
FEATURES_PROVIDED += periph_rtt
FEATURES_PROVIDED += periph_spi
FEATURES_PROVIDED += periph_timer

View File

@ -98,13 +98,6 @@ extern "C" {
#define RTT_PRESCALER (3275U) /* run with 10 Hz */
/** @} */
/**
* @name Random Number Generator configuration
* @{
*/
#define RANDOM_NUMOF (1U)
/** @} */
/**
* @name SPI configuration
* @{

View File

@ -1,7 +1,7 @@
# Put defined MCU peripherals here (in alphabetical order)
FEATURES_PROVIDED += periph_cpuid
FEATURES_PROVIDED += periph_gpio
FEATURES_PROVIDED += periph_random
FEATURES_PROVIDED += periph_hwrng
FEATURES_PROVIDED += periph_timer
FEATURES_PROVIDED += periph_uart

View File

@ -98,13 +98,6 @@
#define UART_0_RX_PIN GPIO_PA0
/** @} */
/**
* @name Random Number Generator configuration
* @{
*/
#define RANDOM_NUMOF 1
/** @} */
/**
* @name GPIO configuration
* @{

View File

@ -82,13 +82,6 @@ extern "C" {
#define I2C_NUMOF (0)
/** @} */
/**
* @name Random Number Generator configuration
* @{
*/
#define RANDOM_NUMOF (0U)
/** @} */
/**
* @name RTC configuration
* @{

View File

@ -208,13 +208,6 @@ static const pwm_conf_t pwm_config[] = {
#define I2C_SCL PIN_PA17
#define I2C_0_PINS (PORT_PA16 | PORT_PA17)
/**
* @name Random Number Generator configuration
* @{
*/
#define RANDOM_NUMOF (0U)
/** @} */
/**
* @name RTC configuration
* @{

View File

@ -3,9 +3,9 @@ FEATURES_PROVIDED += periph_adc
FEATURES_PROVIDED += periph_cpuid
FEATURES_PROVIDED += periph_dac
FEATURES_PROVIDED += periph_gpio
FEATURES_PROVIDED += periph_hwrng
FEATURES_PROVIDED += periph_i2c
FEATURES_PROVIDED += periph_pwm
FEATURES_PROVIDED += periph_random
FEATURES_PROVIDED += periph_spi
FEATURES_PROVIDED += periph_timer
FEATURES_PROVIDED += periph_uart

View File

@ -213,13 +213,6 @@ static const uart_conf_t uart_config[] = {
#define PWM_1_PIN_AF 2
/** @} */
/**
* @name Random Number Generator configuration
* @{
*/
#define RANDOM_NUMOF (1U)
/** @} */
/**
* @name SPI configuration
* @{

View File

@ -1,7 +1,7 @@
# Put defined MCU peripherals here (in alphabetical order)
FEATURES_PROVIDED += periph_cpuid
FEATURES_PROVIDED += periph_gpio
FEATURES_PROVIDED += periph_random
FEATURES_PROVIDED += periph_hwrng
FEATURES_PROVIDED += periph_spi
FEATURES_PROVIDED += periph_timer
FEATURES_PROVIDED += periph_uart

View File

@ -84,13 +84,6 @@ static const uart_conf_t uart_config[] = {
#define UART_NUMOF (sizeof(uart_config) / sizeof(uart_config[0]))
/** @} */
/**
* @name Random Number Generator configuration
* @{
*/
#define RANDOM_NUMOF (1U)
/** @} */
/**
* @name SPI configuration
* @{

View File

@ -2,7 +2,7 @@
FEATURES_PROVIDED += periph_adc
FEATURES_PROVIDED += periph_cpuid
FEATURES_PROVIDED += periph_gpio
FEATURES_PROVIDED += periph_random
FEATURES_PROVIDED += periph_hwrng
FEATURES_PROVIDED += periph_rtt
FEATURES_PROVIDED += periph_spi
FEATURES_PROVIDED += periph_timer

View File

@ -96,13 +96,6 @@ extern "C" {
#define RTT_PRESCALER (3275U) /* run with 10 Hz */
/** @} */
/**
* @name Random Number Generator configuration
* @{
*/
#define RANDOM_NUMOF (1U)
/** @} */
/**
* @name SPI configuration
* @{

View File

@ -1,5 +1,6 @@
/*
* Copyright (C) 2014 Loci Controls Inc.
* 2016 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
@ -7,27 +8,22 @@
*/
/**
* @ingroup driver_periph
* @ingroup cpu_cc2538
* @{
*
* @file
* @brief Low-level random number generator driver implementation
* @brief HWRNG peripheral driver interface implementation
*
* @author Ian Martin <ian@locicontrols.com>
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
*
* @}
*/
#include <stdint.h>
#include "cpu.h"
#include "periph_conf.h"
#include "periph/random.h"
#include "periph/hwrng.h"
/* only compile this driver if enabled in the board's periph_conf.h */
#if RANDOM_NUMOF
void random_init(void)
void hwrng_init(void)
{
uint16_t seed = 0;
int i;
@ -71,32 +67,18 @@ void random_init(void)
/* Turn RF off: */
RFCORE_SFR_RFST = ISRFOFF;
random_poweron();
}
int random_read(char *buf, unsigned int num)
void hwrng_read(uint8_t *buf, unsigned int num)
{
unsigned count;
for (count = 0; count < num; ) {
/* Clock the RNG LSFR once: */
SOC_ADC->cc2538_adc_adccon1.ADCCON1bits.RCTRL = 1;
/* Read up to 2 bytes of random data: */
/* Read up to 2 bytes of hwrng data: */
buf[count++] = SOC_ADC_RNDL;
if (count >= num) break;
buf[count++] = SOC_ADC_RNDH;
}
return count;
}
void random_poweron(void)
{
}
void random_poweroff(void)
{
}
#endif /* RANDOM_NUMOF */

View File

@ -1,10 +1,10 @@
/*
* Copyright (C) 2014 Freie Universität Berlin
* Copyright (C) 2014-2016 Freie Universität Berlin
* Copyright (C) 2014 PHYTEC Messtechnik GmbH
*
* This file is subject to the terms and conditions of the GNU Lesser General
* Public License v2.1. See the file LICENSE in the top level directory for more
* details.
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
@ -13,7 +13,7 @@
* @{
*
* @file
* @brief Low-level random number generator driver implementation.
* @brief HWRNG interface implementation
*
* @author Johann Fischer <j.fischer@phytec.de> (adaption for Freescale's RNGA)
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
@ -22,21 +22,24 @@
*/
#include "cpu.h"
#include "periph/random.h"
#include "periph/hwrng.h"
#include "periph_conf.h"
#if RANDOM_NUMOF
#ifdef KINETIS_RNGA
void random_init(void)
void hwrng_init(void)
{
random_poweron();
/* nothing to do here */
}
int random_read(char *buf, unsigned int num)
void hwrng_read(uint8_t *buf, unsigned int num)
{
unsigned int count = 0;
/* power on and enable the device */
HWRNG_CLKEN();
KINETIS_RNGA->CR = RNG_CR_INTM_MASK | RNG_CR_HA_MASK | RNG_CR_GO_MASK;
/* self-seeding */
while (!(KINETIS_RNGA->SR & RNG_SR_OREG_LVL_MASK));
@ -50,31 +53,14 @@ int random_read(char *buf, unsigned int num)
/* copy data into result vector */
for (int i = 0; i < 4 && count < num; i++) {
buf[count++] = (char)tmp;
buf[count++] = (uint8_t)tmp;
tmp = tmp >> 8;
}
}
return (int)count;
}
void random_poweron(void)
{
RANDOM_CLKEN();
KINETIS_RNGA->CR = RNG_CR_INTM_MASK | RNG_CR_HA_MASK | RNG_CR_GO_MASK;
}
void random_poweroff(void)
{
/* power of the device */
KINETIS_RNGA->CR = 0;
RANDOM_CLKDIS();
HWRNG_CLKDIS();
}
/*
void isr_rng(void)
{
}
*/
#endif /* KINETIS_RNGA */
#endif /* RANDOM_NUMOF */

View File

@ -1,11 +1,11 @@
/*
* Copyright (C) 2014 Freie Universität Berlin
* Copyright (C) 2014-2016 Freie Universität Berlin
* Copyright (C) 2014 PHYTEC Messtechnik GmbH
* Copyright (C) 2015 Eistec AB
*
* This file is subject to the terms and conditions of the GNU Lesser General
* Public License v2.1. See the file LICENSE in the top level directory for more
* details.
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
@ -13,7 +13,7 @@
* @{
*
* @file
* @brief Low-level random number generator driver implementation.
* @brief HWRNG interface implementation
*
* @author Joakim Nohlgård <joakim.nohlgard@eistec.se> (adaption for Freescale's RNGB)
* @author Johann Fischer <j.fischer@phytec.de> (adaption for Freescale's RNGA)
@ -23,22 +23,32 @@
*/
#include "cpu.h"
#include "periph/random.h"
#include "periph/hwrng.h"
#include "periph_conf.h"
#if RANDOM_NUMOF
#ifdef KINETIS_RNGB
void random_init(void)
void hwrng_init(void)
{
random_poweron();
/* nothing to be done here */
}
int random_read(char *buf, unsigned int num)
void hwrng_read(uint8_t *buf, unsigned int num)
{
unsigned int count = 0;
HWRNG_CLKEN();
if ((KINETIS_RNGB->VER & RNG_VER_TYPE_MASK) != 0b0001) {
/* Wrong type of RNG */
/* TODO: Handle */
}
/* Software reset, bit is self-clearing */
BITBAND_REG32(KINETIS_RNGB->CMD, RNG_CMD_SR_SHIFT) = 1;
/* Set up automatic reseed */
KINETIS_RNGB->CR = RNG_CR_AR_MASK | RNG_CR_MASKERR_MASK | RNG_CR_MASKDONE_MASK;
while (count < num) {
uint32_t tmp;
@ -49,40 +59,13 @@ int random_read(char *buf, unsigned int num)
/* copy data into result vector */
for (int i = 0; i < 4 && count < num; i++) {
buf[count++] = (char)tmp;
buf[count++] = (uint8_t)tmp;
tmp = tmp >> 8;
}
}
return (int)count;
}
void random_poweron(void)
{
RANDOM_CLKEN();
if ((KINETIS_RNGB->VER & RNG_VER_TYPE_MASK) != 0b0001) {
/* Wrong type of RNG */
/* TODO: Handle */
}
/* Software reset, bit is self-clearing */
BITBAND_REG32(KINETIS_RNGB->CMD, RNG_CMD_SR_SHIFT) = 1;
/* Set up automatic reseed */
KINETIS_RNGB->CR = RNG_CR_AR_MASK | RNG_CR_MASKERR_MASK | RNG_CR_MASKDONE_MASK;
}
void random_poweroff(void)
{
KINETIS_RNGB->CR = 0;
RANDOM_CLKDIS();
HWRNG_CLKDIS();
}
/*
void isr_rng(void)
{
}
*/
#endif /* KINETIS_RNGB */
#endif /* RANDOM_NUMOF */

View File

@ -1,5 +1,6 @@
/*
* Copyright (C) 2014 Ludwig Knüpfer <ludwig.knuepfer@fu-berlin.de>
* 2016 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
@ -11,6 +12,9 @@
* @defgroup native_rng
* @{
*
* @file
* @brief HWRNG interface implementation
*
* @author Ludwig Knüpfer <ludwig.knuepfer@fu-berlin.de>
*/
@ -20,16 +24,14 @@
#include <sys/stat.h>
#include <fcntl.h>
#include "cpu_conf.h"
#include "native_internal.h"
#include "periph/random.h"
#include "periph/hwrng.h"
#define ENABLE_DEBUG (0)
#include "debug.h"
static int powered = 0;
static int initialized = 0;
static int dev_random = -1;
@ -42,16 +44,16 @@ static int dev_random = -1;
*/
void _native_rng_init_det(void);
void _native_rng_init_hq(void);
unsigned _native_rng_read_det(char *buf, unsigned num);
unsigned _native_rng_read_hq(char *buf, unsigned num);
unsigned _native_rng_read_det(uint8_t *buf, unsigned num);
unsigned _native_rng_read_hq(uint8_t *buf, unsigned num);
/**********************************************************************
* public API implementation
**********************************************************************/
void random_init(void)
void hwrng_init(void)
{
DEBUG("random_init: initializing\n");
DEBUG("hwrng_init: initializing\n");
switch (_native_rng_mode) {
case 0:
_native_rng_init_hq();
@ -60,65 +62,34 @@ void random_init(void)
_native_rng_init_det();
break;
default:
err(EXIT_FAILURE, "random_init: _native_rng_mode is in invalid state %i\n",
err(EXIT_FAILURE, "hwrng_init: _native_rng_mode is in invalid state %i\n",
_native_rng_mode);
break;
}
initialized = 1;
DEBUG("random_init: powering on\n");
random_poweron();
}
int random_read(char *buf, unsigned int num)
void hwrng_read(uint8_t *buf, unsigned int num)
{
if (!initialized) {
warnx("random_read: random device not initialized, failing\n");
return 0;
warnx("hwrng_read: random device not initialized, failing\n");
return;
}
if (!powered) {
warnx("random_read: random device not powered, failing\n");
return 0;
}
DEBUG("random_read: writing %u bytes\n", num);
DEBUG("hwrng_read: writing %u bytes\n", num);
switch (_native_rng_mode) {
case 0:
num = _native_rng_read_hq(buf, num);
_native_rng_read_hq(buf, num);
break;
case 1:
num = _native_rng_read_det(buf, num);
_native_rng_read_det(buf, num);
break;
default:
err(EXIT_FAILURE, "random_read: _native_rng_mode is in invalid state %i\n",
err(EXIT_FAILURE, "hwrng_read: _native_rng_mode is in invalid state %i\n",
_native_rng_mode);
break;
}
return num;
}
void random_poweron(void)
{
DEBUG("random_poweron: power on\n");
if (!initialized) {
warnx("random_poweron: not initialized.");
}
powered = 1;
}
void random_poweroff(void)
{
DEBUG("random_poweroff: power off\n");
if (!initialized) {
warnx("random_poweroff: not initialized.");
}
powered = 0;
}
/**********************************************************************
@ -147,19 +118,19 @@ void _native_rng_init_hq(void)
_native_syscall_leave();
}
unsigned _native_rng_read_det(char *buf, unsigned num)
unsigned _native_rng_read_det(uint8_t *buf, unsigned num)
{
DEBUG("_native_rng_read_det\n");
for (unsigned i = 0; i < num; i++) {
_native_syscall_enter();
buf[i] = (char)real_random();
buf[i] = (uint8_t)real_random();
_native_syscall_leave();
}
return num;
}
unsigned _native_rng_read_hq(char *buf, unsigned num)
unsigned _native_rng_read_hq(uint8_t *buf, unsigned num)
{
DEBUG("_native_rng_read_hq\n");
unsigned offset = 0;

View File

@ -1,13 +1,13 @@
/*
* Copyright (C) 2014 Freie Universität Berlin
* Copyright (C) 2014-2016 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser General
* Public License v2.1. See the file LICENSE in the top level directory for more
* details.
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup cpu_nrf51822
* @ingroup cpu_nrf51
* @{
*
* @file
@ -20,20 +20,16 @@
*/
#include "cpu.h"
#include "periph_conf.h"
#include "periph/random.h"
#include "periph/hwrng.h"
/* guard file in case no random device was specified */
#if RANDOM_NUMOF
void random_init(void)
void hwrng_init(void)
{
/* The RNG is initiated every time when RNG read is called
* This reduces power consumption when RNG is not needed
*/
}
int random_read(char *buf, unsigned int num)
void hwrng_read(uint8_t *buf, unsigned int num)
{
unsigned int count = 0;
@ -57,7 +53,7 @@ int random_read(char *buf, unsigned int num)
__WFE();
}
buf[count++] = (char)NRF_RNG->VALUE;
buf[count++] = (uint8_t)NRF_RNG->VALUE;
/* NRF51 PAN #21 */
NRF_RNG->EVENTS_VALRDY = 0;
/* clear interrupt state */
@ -66,18 +62,4 @@ int random_read(char *buf, unsigned int num)
/* power off RNG */
NRF_RNG->POWER = 0;
return count;
}
void random_poweron(void)
{
/* RNG is powered on when needed */
}
void random_poweroff(void)
{
/* RNG is powered off automaticly */
}
#endif /* RANDOM_NUMOF */

View File

@ -12,7 +12,7 @@
* @{
*
* @file
* @brief Implementation of the random number generator interface
* @brief Implementation of the hardware random number generator interface
*
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
* @author Jan Wagner <mail@jwagner.eu>
@ -21,15 +21,14 @@
*/
#include "cpu.h"
#include "periph_conf.h"
#include "periph/random.h"
#include "periph/hwrng.h"
void random_init(void)
void hwrng_init(void)
{
/* nothing to do here */
}
int random_read(char *buf, unsigned int num)
void hwrng_read(uint8_t *buf, unsigned int num)
{
unsigned int count = 0;
@ -39,20 +38,8 @@ int random_read(char *buf, unsigned int num)
while (NRF_RNG->EVENTS_VALRDY == 0);
NRF_RNG->EVENTS_VALRDY = 0;
buf[count++] = (char)NRF_RNG->VALUE;
buf[count++] = (uint8_t)NRF_RNG->VALUE;
}
NRF_RNG->TASKS_STOP = 1;
return count;
}
void random_poweron(void)
{
/* nothing to do here */
}
void random_poweroff(void)
{
/* nothing to do here */
}

View File

@ -1,13 +1,13 @@
/*
* Copyright (C) 2014 Freie Universität Berlin
* Copyright (C) 2014-2016 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser General
* Public License v2.1. See the file LICENSE in the top level directory for more
* details.
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup driver_periph
* @ingroup cpu_sam3
* @{
*
* @file
@ -19,11 +19,7 @@
*/
#include "cpu.h"
#include "periph_conf.h"
#include "periph/random.h"
/* only compile this driver if enabled in the board's periph_conf.h */
#if RANDOM_NUMOF
#include "periph/hwrng.h"
/**
* @name KEY needs to be written to the config register when en|disabling
@ -31,46 +27,34 @@
*/
#define KEY (0x524e4700)
void random_init(void)
void hwrng_init(void)
{
random_poweron();
/* no need for initialization */
}
int random_read(char *buf, unsigned int num)
void hwrng_read(uint8_t *buf, unsigned int num)
{
/* cppcheck-suppress variableScope */
uint32_t tmp;
unsigned count = 0;
/* enable clock signal for TRNG module */
PMC->PMC_PCER1 |= PMC_PCER1_PID41;
/* enable the generation of random numbers */
TRNG->TRNG_CR |= (KEY | TRNG_CR_ENABLE);
while (count < num) {
/* wait until new value is generated -> takes up to 84 cycles */
while (!(TRNG->TRNG_ISR & TRNG_ISR_DATRDY));
/* read 4 byte of random data */
tmp = TRNG->TRNG_ODATA;
uint32_t tmp = TRNG->TRNG_ODATA;
/* extract copy bytes to result */
for (int i = 0; i < 4 && count < num; i++) {
buf[count++] = (char)tmp;
buf[count++] = (uint8_t)tmp;
tmp = tmp >> 8;
}
}
return count;
}
void random_poweron(void)
{
/* enable clock signal for TRNG module */
PMC->PMC_PCER1 |= PMC_PCER1_PID41;
/* enable the generation of random numbers */
TRNG->TRNG_CR |= (KEY | TRNG_CR_ENABLE);
}
void random_poweroff(void)
{
/* disable the generation of random numbers */
TRNG->TRNG_CR &= ~(KEY | TRNG_CR_ENABLE);
/* disable clock signal for TRNG module */
PMC->PMC_PCER1 &= ~(PMC_PCER1_PID41);
}
#endif /* RANDOM_NUMOF */

View File

@ -1,9 +1,9 @@
/*
* Copyright (C) 2014 Freie Universität Berlin
* Copyright (C) 2014-2016 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser General
* Public License v2.1. See the file LICENSE in the top level directory for more
* details.
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
@ -19,48 +19,40 @@
*/
#include "cpu.h"
#include "periph/random.h"
#include "periph_conf.h"
#include "periph/hwrng.h"
/* ignore file in case no RNG device is defined */
#if RANDOM_NUMOF
/* only build if the CPU actually provides a RNG peripheral */
#ifdef RNG
void random_init(void)
void hwrng_init(void)
{
random_poweron();
/* no need for initialization */
}
int random_read(char *buf, unsigned int num)
void hwrng_read(uint8_t *buf, unsigned int num)
{
/* cppcheck-suppress variableScope */
uint32_t tmp;
unsigned int count = 0;
/* power on and enable the device */
RCC->AHB2ENR |= RCC_AHB2ENR_RNGEN;
RNG->CR = RNG_CR_RNGEN;
/* get random data */
while (count < num) {
/* wait for random data to be ready to read */
while (!(RNG->SR & RNG_SR_DRDY));
/* read next 4 bytes */
tmp = RNG->DR;
uint32_t tmp = RNG->DR;
/* copy data into result vector */
for (int i = 0; i < 4 && count < num; i++) {
buf[count++] = (char)tmp;
buf[count++] = (uint8_t)tmp;
tmp = tmp >> 8;
}
}
return (int)count;
}
void random_poweron(void)
{
RCC->AHB2ENR |= RCC_AHB2ENR_RNGEN;
RNG->CR = RNG_CR_RNGEN;
}
void random_poweroff(void)
{
/* finally disable the device again */
RNG->CR = 0;
RCC->AHB2ENR &= ~RCC_AHB2ENR_RNGEN;
}
#endif /* RANDOM_NUMOF */
#endif /* CPUID_LEN */

View File

@ -0,0 +1,67 @@
/*
* Copyright (C) 2014-2016 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @defgroup drivers_periph_hwrng HWRNG Abstraction
* @ingroup drivers_periph
* @brief Peripheral hardware random number generator interface
*
* The HWRNG interface abstracts means provided by MCU peripherals to create
* random number. On most platforms, these peripherals are called something like
* 'hardware random number generator' or 'pseudo random number generator'. The
* quality of the created random data does differ quite a bit between different
* MCUs, so please refer to your MCUs reference manual for information on this!
*
* @note Refer to your platforms MCU reference manual for information on the
* quality of the used (pseudo) random number generator!
*
* @{
* @file
* @brief Hardware random number generator driver interface
*
* @author Christian Mehlis <mehlis@inf.fu-berlin.de>
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
*/
#ifndef PERIPH_HWRNG_H
#define PERIPH_HWRNG_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Initialize the hardware random number generator
*
* On some platforms, the random number generator needs some global
* initialization before it can be used. This should happen in this function
* if it would impose too much overhead to do this everytime the hwrng_read
* function is called. The device should however be put into power of mode
* after initialization and will be powered on and of when hwrng_read is called.
*/
void hwrng_init(void);
/**
* @brief Read N bytes of random data from the hardware device
*
* The read function should power on the HWRNG MCU peripheral, read the given
* number of random bytes and than put the peripheral back to sleep.
*
* @param[in] buf destination buffer to write the bytes to
* @param[in] num number of bytes to get from device
*/
void hwrng_read(uint8_t *buf, unsigned int num);
#ifdef __cplusplus
}
#endif
#endif /* PERIPH_HWRNG_H */
/** @} */

View File

@ -1,81 +0,0 @@
/*
* Copyright (C) 2014 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser General
* Public License v2.1. See the file LICENSE in the top level directory for more
* details.
*/
/**
* @defgroup drivers_periph_random Random
* @ingroup drivers_periph
* @brief Low-level (pseudo) random number generator driver
*
* The quality of the random data read from this interface is highly
* dependent on hardware dependent implementation. Most platforms utilize a
* hardware (Pseudo) Random Number Generator. The quality of the generated
* random data can be however very different.
*
* @note REFER TO YOUR PLATFORMS IMPLEMENTATION ABOUT INFORMATION ABOUT THE
* QUALITY OF RANDOMNES!
*
* @{
* @file
* @brief Low-level random peripheral driver interface definitions
*
* @author Christian Mehlis <mehlis@inf.fu-berlin.de>
*/
#ifndef RANDOM_H
#define RANDOM_H
#include "periph_conf.h"
#ifdef __cplusplus
extern "C" {
#endif
/* only include this file if a random number generator is defined */
#if RANDOM_NUMOF
/**
* @brief Initializes the source of randomness
*
* In case of a hardware random number generator, this peripheral
* is initialized and powered on. If such a device is not present,
* it depends on the implementation how a source for randomness
* is created and initialized.
*/
void random_init(void);
/**
* @brief Reads num or less bytes of randomness from the source, will
* block until random data is available
*
* @param[in] buf destination buffer to write the bytes to
* @param[in] num number of bytes to get from device,
* only values >0 are valid
*
* @return the number of bytes written to buf
* @return 0 on error
*/
int random_read(char *buf, unsigned int num);
/**
* @brief Power on the random number generator
*/
void random_poweron(void);
/**
* @brief Power off the random number generator
*/
void random_poweroff(void);
#endif /* RANDOM_NUMOF */
#ifdef __cplusplus
}
#endif
#endif /* RANDOM_H */
/** @} */

View File

@ -1,4 +1,4 @@
From d6c1cb4244c72a8fa7d5a8550c8193700cd0de03 Mon Sep 17 00:00:00 2001
From 146edb3bb6d23c8d3571bac0b12fd4d1ddc16639 Mon Sep 17 00:00:00 2001
From: Frank Holtz <frank-riot2015@holtznet.de>
Date: Sat, 27 Dec 2014 19:57:01 +0100
Subject: [PATCH 1/2] Add RIOT Makefile
@ -19,5 +19,4 @@ index 0000000..33c8152
+
+include $(RIOTBASE)/Makefile.base
--
1.8.3.1
2.7.1

View File

@ -1,15 +1,15 @@
From 60ac2261d89d1a483bef4676c1e9b16fec8830d1 Mon Sep 17 00:00:00 2001
From 2ab8fb085a02fec23ca817bd6de8848df160970b Mon Sep 17 00:00:00 2001
From: Frank Holtz <frank-riot2015@holtznet.de>
Date: Sat, 17 Jan 2015 18:41:14 +0100
Subject: [PATCH 2/2] Include RIOT Hardware RNG interface
---
uECC.c | 99 +++++++++++-------------------------------------------------------
uECC.h | 28 +------------------
2 files changed, 17 insertions(+), 110 deletions(-)
uECC.c | 93 +++++++-----------------------------------------------------------
uECC.h | 28 +-------------------
2 files changed, 11 insertions(+), 110 deletions(-)
diff --git a/uECC.c b/uECC.c
index aded242..5fe3389 100644
index aded242..8b355a4 100644
--- a/uECC.c
+++ b/uECC.c
@@ -322,85 +322,6 @@ static void vli_square(uECC_word_t *p_result, uECC_word_t *p_left);
@ -98,86 +98,67 @@ index aded242..5fe3389 100644
#ifdef __GNUC__ /* Only support GCC inline asm for now */
#if (uECC_ASM && (uECC_PLATFORM == uECC_avr))
#include "asm_avr.inc"
@@ -1779,11 +1700,15 @@ int uECC_make_key(uint8_t p_publicKey[uECC_BYTES*2], uint8_t p_privateKey[uECC_B
uECC_word_t l_private[uECC_WORDS];
uECC_word_t l_tries = 0;
+ /* power on rng */
+ random_poweron();
+
@@ -1782,10 +1703,13 @@ int uECC_make_key(uint8_t p_publicKey[uECC_BYTES*2], uint8_t p_privateKey[uECC_B
do
{
repeat:
- if(!g_rng((uint8_t *)l_private, sizeof(l_private)) || (l_tries++ >= MAX_TRIES))
+ if(random_read((char *)l_private, sizeof(l_private))!=sizeof(l_private) || (l_tries++ >= MAX_TRIES))
+ if(l_tries++ >= MAX_TRIES)
{
+ random_poweroff();
return 0;
}
+
+ hwrng_read((uint8_t *)l_private, sizeof(l_private));
+
if(vli_isZero(l_private))
@@ -1805,6 +1730,7 @@ int uECC_make_key(uint8_t p_publicKey[uECC_BYTES*2], uint8_t p_privateKey[uECC_B
vli_nativeToBytes(p_privateKey, l_private);
vli_nativeToBytes(p_publicKey, l_public.x);
vli_nativeToBytes(p_publicKey + uECC_BYTES, l_public.y);
+ random_poweroff();
return 1;
}
@@ -1814,7 +1740,9 @@ int uECC_shared_secret(const uint8_t p_publicKey[uECC_BYTES*2], const uint8_t p_
{
goto repeat;
@@ -1814,7 +1738,7 @@ int uECC_shared_secret(const uint8_t p_publicKey[uECC_BYTES*2], const uint8_t p_
uECC_word_t l_private[uECC_WORDS];
uECC_word_t l_random[uECC_WORDS];
- g_rng((uint8_t *)l_random, sizeof(l_random));
+ random_poweron();
+ random_read((char *)l_random, sizeof(l_random));
+ random_poweroff();
+ hwrng_read((uint8_t *)l_random, sizeof(l_random));
vli_bytesToNative(l_private, p_privateKey);
vli_bytesToNative(l_public.x, p_publicKey);
@@ -2152,11 +2080,14 @@ int uECC_sign(const uint8_t p_privateKey[uECC_BYTES], const uint8_t p_hash[uECC_
EccPoint p;
uECC_word_t l_tries = 0;
+ random_poweron();
+
@@ -2155,11 +2079,13 @@ int uECC_sign(const uint8_t p_privateKey[uECC_BYTES], const uint8_t p_hash[uECC_
do
{
repeat:
- if(!g_rng((uint8_t *)k, sizeof(k)) || (l_tries++ >= MAX_TRIES))
+ if(random_read((char *)k, sizeof(k))!=sizeof(k) || (l_tries++ >= MAX_TRIES))
+ if(l_tries++ >= MAX_TRIES)
{
+ random_poweroff();
return 0;
}
@@ -2203,8 +2134,9 @@ int uECC_sign(const uint8_t p_privateKey[uECC_BYTES], const uint8_t p_hash[uECC_
+ hwrng_read((uint8_t *)k, sizeof(k));
+
if(vli_isZero(k))
{
goto repeat;
@@ -2203,10 +2129,11 @@ int uECC_sign(const uint8_t p_privateKey[uECC_BYTES], const uint8_t p_hash[uECC_
l_tries = 0;
do
{
- if(!g_rng((uint8_t *)l_tmp, sizeof(l_tmp)) || (l_tries++ >= MAX_TRIES))
+ if(random_read((char *)l_tmp, sizeof(l_tmp))!=sizeof(l_tmp) || (l_tries++ >= MAX_TRIES))
+ if(l_tries++ >= MAX_TRIES)
{
+ random_poweroff();
return 0;
}
+ hwrng_read((uint8_t *)l_tmp, sizeof(l_tmp));
} while(vli_isZero(l_tmp));
@@ -2234,6 +2166,7 @@ int uECC_sign(const uint8_t p_privateKey[uECC_BYTES], const uint8_t p_hash[uECC_
#endif
vli_nativeToBytes(p_signature + uECC_BYTES, s);
+ random_poweroff();
return 1;
}
/* Prevent side channel analysis of vli_modInv() to determine
diff --git a/uECC.h b/uECC.h
index 2c9927b..27a2e47 100644
index 2c9927b..02e2f22 100644
--- a/uECC.h
+++ b/uECC.h
@@ -4,6 +4,7 @@
#define _MICRO_ECC_H_
#include <stdint.h>
+#include "periph/random.h"
+#include "periph/hwrng.h"
/* Platform selection options.
If uECC_PLATFORM is not defined, the code will try to guess it based on compiler macros.
@ -216,5 +197,5 @@ index 2c9927b..27a2e47 100644
Create a public/private key pair.
--
1.8.3.1
2.7.1

View File

@ -1,7 +1,7 @@
export APPLICATION = periph_random
export APPLICATION = periph_hwrng
include ../Makefile.tests_common
FEATURES_REQUIRED = periph_random
FEATURES_REQUIRED = periph_hwrng
USEMODULE += xtimer

View File

@ -0,0 +1,9 @@
Expected result
===============
This test outputs a sequence of random bytes, starting with one, then two and so
on, until 20 random bytes are printed. Then the application sleeps for a second
and starts over.
Background
==========
Test the functionality of a platforms HWRNG implementation.

View File

@ -22,19 +22,20 @@
#include <string.h>
#include "xtimer.h"
#include "periph/random.h"
#include "periph/hwrng.h"
#define LIMIT (20U)
int main(void)
{
char buf[LIMIT];
uint8_t buf[LIMIT];
puts("\nRandom number generator low-level driver test\n");
printf("This test will print from 1 to %i random bytes about every second\n\n", LIMIT);
puts("\nHWRNG peripheral driver test\n");
printf("This test will print from 1 to %i random bytes about every"
"second\n\n", LIMIT);
puts("Initializing Random Number Generator driver.\n");
random_init();
puts("Initializing the HWRNG driver.\n");
hwrng_init();
while (1) {
/* zero out buffer */
@ -43,12 +44,7 @@ int main(void)
/* create random numbers */
for (unsigned i = 1; i <= LIMIT; i++) {
printf("generating %u random byte(s)\n", i);
unsigned count = random_read(buf, i);
if (count != i) {
printf("Error generating random bytes, got %u instead of %u", count, i);
return 0;
}
hwrng_read(buf, i);
printf("Got:");
for (unsigned j = 0; j < i; j++) {

View File

@ -1,7 +0,0 @@
Expected result
===============
This test outputs a sequence of random bytes, starting with one, then two and so on, until 20 random bytes are printed. Then the application sleeps for a second and starts over.
Background
==========
This test was introduced to test the implementation of the low-level random number generator driver. For most platforms the implementation is based on hardware CPU peripherals.

View File

@ -1,7 +1,7 @@
APPLICATION = micro-ecc
include ../Makefile.tests_common
FEATURES_REQUIRED = periph_random
FEATURES_REQUIRED = periph_hwrng
USEPKG += micro-ecc
include $(RIOTBASE)/Makefile.include

View File

@ -37,7 +37,7 @@
#include <stdio.h>
#include <string.h>
#include "uECC.h"
#include "periph/random.h"
#include "periph/hwrng.h"
#define TESTROUNDS 16
@ -61,9 +61,7 @@ int main(void)
uint8_t l_sig[uECC_BYTES * 2];
/* initialize hardware random number generator */
random_init();
/* power off RNG to save energy */
random_poweroff();
hwrng_init();
printf("Testing %d random private key pairs and signature\n", TESTROUNDS);