2018-02-11 17:34:33 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Oticon A/S
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Pseudo-random entropy generator for the ARCH_POSIX architecture:
|
|
|
|
* Following the principle of reproducibility of the native_posix board
|
|
|
|
* this entropy device will always generate the same random sequence when
|
|
|
|
* initialized with the same seed
|
2018-05-16 23:25:13 +08:00
|
|
|
*
|
|
|
|
* This entropy source should only be used for testing.
|
2018-02-11 17:34:33 +08:00
|
|
|
*/
|
|
|
|
|
2020-04-01 23:31:43 +08:00
|
|
|
#define DT_DRV_COMPAT zephyr_native_posix_rng
|
|
|
|
|
2018-02-11 17:34:33 +08:00
|
|
|
#include "device.h"
|
2019-06-26 03:53:49 +08:00
|
|
|
#include <drivers/entropy.h>
|
2018-02-11 17:34:33 +08:00
|
|
|
#include "init.h"
|
2019-06-26 22:33:55 +08:00
|
|
|
#include <sys/util.h>
|
2018-02-11 17:34:33 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2019-10-24 23:08:21 +08:00
|
|
|
#include <arch/posix/posix_trace.h>
|
2018-07-29 21:26:50 +08:00
|
|
|
#include "soc.h"
|
|
|
|
#include "cmdline.h" /* native_posix command line options header */
|
2018-02-11 17:34:33 +08:00
|
|
|
|
|
|
|
static unsigned int seed = 0x5678;
|
|
|
|
|
2020-05-28 00:26:57 +08:00
|
|
|
static int entropy_native_posix_get_entropy(struct device *dev, uint8_t *buffer,
|
|
|
|
uint16_t length)
|
2018-02-11 17:34:33 +08:00
|
|
|
{
|
|
|
|
ARG_UNUSED(dev);
|
|
|
|
|
|
|
|
while (length) {
|
|
|
|
/*
|
|
|
|
* Note that only 1 thread (Zephyr thread or HW models), runs at
|
|
|
|
* a time, therefore there is no need to use random_r()
|
|
|
|
*/
|
|
|
|
long int value = random();
|
|
|
|
|
2019-02-12 01:14:19 +08:00
|
|
|
size_t to_copy = MIN(length, sizeof(long int));
|
2018-02-11 17:34:33 +08:00
|
|
|
|
|
|
|
memcpy(buffer, &value, to_copy);
|
|
|
|
buffer += to_copy;
|
|
|
|
length -= to_copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-28 00:26:57 +08:00
|
|
|
static int entropy_native_posix_get_entropy_isr(struct device *dev, uint8_t *buf,
|
|
|
|
uint16_t len, uint32_t flags)
|
2018-06-22 15:55:50 +08:00
|
|
|
{
|
|
|
|
ARG_UNUSED(flags);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* entropy_native_posix_get_entropy() is also safe for ISRs
|
|
|
|
* and always produces data.
|
|
|
|
*/
|
|
|
|
return entropy_native_posix_get_entropy(dev, buf, len);
|
|
|
|
}
|
|
|
|
|
2018-02-11 17:34:33 +08:00
|
|
|
static int entropy_native_posix_init(struct device *dev)
|
|
|
|
{
|
|
|
|
ARG_UNUSED(dev);
|
|
|
|
srandom(seed);
|
2018-05-16 23:25:13 +08:00
|
|
|
posix_print_warning("WARNING: "
|
|
|
|
"Using a test - not safe - entropy source\n");
|
2018-02-11 17:34:33 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct entropy_driver_api entropy_native_posix_api_funcs = {
|
2018-06-22 15:55:50 +08:00
|
|
|
.get_entropy = entropy_native_posix_get_entropy,
|
|
|
|
.get_entropy_isr = entropy_native_posix_get_entropy_isr
|
2018-02-11 17:34:33 +08:00
|
|
|
};
|
|
|
|
|
2020-04-01 23:31:43 +08:00
|
|
|
DEVICE_AND_API_INIT(entropy_native_posix, DT_INST_LABEL(0),
|
2018-02-11 17:34:33 +08:00
|
|
|
entropy_native_posix_init, NULL, NULL,
|
2018-05-23 06:08:38 +08:00
|
|
|
PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEVICE,
|
2018-02-11 17:34:33 +08:00
|
|
|
&entropy_native_posix_api_funcs);
|
|
|
|
|
2018-07-29 21:26:50 +08:00
|
|
|
static void add_fake_entropy_option(void)
|
|
|
|
{
|
|
|
|
static struct args_struct_t entropy_options[] = {
|
|
|
|
/*
|
|
|
|
* Fields:
|
|
|
|
* manual, mandatory, switch,
|
|
|
|
* option_name, var_name ,type,
|
|
|
|
* destination, callback,
|
|
|
|
* description
|
|
|
|
*/
|
|
|
|
{false, false, false,
|
|
|
|
"seed", "r_seed", 'u',
|
|
|
|
(void *)&seed, NULL,
|
|
|
|
"A 32-bit integer seed value for the entropy device, such as "
|
|
|
|
"97229 (decimal), 0x17BCD (hex), or 0275715 (octal)"},
|
|
|
|
ARG_TABLE_ENDMARKER
|
|
|
|
};
|
|
|
|
|
|
|
|
native_add_command_line_opts(entropy_options);
|
|
|
|
}
|
|
|
|
|
|
|
|
NATIVE_TASK(add_fake_entropy_option, PRE_BOOT_1, 10);
|