cpu/native: adapted HWRNG implementation

pr/gpio
Hauke Petersen 8 years ago
parent 216a4cb432
commit c9e9fcf662

@ -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;
}
if (!powered) {
warnx("random_read: random device not powered, failing\n");
return 0;
warnx("hwrng_read: random device not initialized, failing\n");
return;
}
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;
Loading…
Cancel
Save