[PATCH v4 5/8] sandbox: rng: Add a random number generator(rng) driver

Heinrich Schuchardt xypron.glpk at gmx.de
Tue Dec 24 23:09:20 CET 2019


Resending with Cc Tom, Simon, Ilias

On 12/17/19 12:52 PM, Sughosh Ganu wrote:
> Add a sandbox driver for random number generation. Mostly aimed at
> providing a unit test for rng uclass.
>
> Signed-off-by: Sughosh Ganu <sughosh.ganu at linaro.org>
> Reviewed-by: Patrice Chotard <patrice.chotard at st.com>
> ---
> Changes since V3:
>   Handle review comments from Patrick Delaunay.
>
>   arch/sandbox/dts/test.dts |  4 ++++
>   drivers/rng/Kconfig       |  7 +++++++
>   drivers/rng/Makefile      |  1 +
>   drivers/rng/sandbox_rng.c | 39 +++++++++++++++++++++++++++++++++++++++
>   4 files changed, 51 insertions(+)
>   create mode 100644 drivers/rng/sandbox_rng.c
>
> diff --git a/arch/sandbox/dts/test.dts b/arch/sandbox/dts/test.dts
> index fdb08f2..2c85540 100644
> --- a/arch/sandbox/dts/test.dts
> +++ b/arch/sandbox/dts/test.dts
> @@ -599,6 +599,10 @@
>   		reset-names = "other", "test";
>   	};
>
> +	rng at 0 {
> +		compatible = "sandbox,sandbox-rng";
> +	};
> +
>   	rproc_1: rproc at 1 {
>   		compatible = "sandbox,test-processor";
>   		remoteproc-name = "remoteproc-test-dev1";
> diff --git a/drivers/rng/Kconfig b/drivers/rng/Kconfig
> index 5fc11db..3a1d3f0 100644
> --- a/drivers/rng/Kconfig
> +++ b/drivers/rng/Kconfig
> @@ -6,6 +6,13 @@ config DM_RNG
>   	  This interface is used to initialise the rng device and to
>   	  read the random seed from the device.
>
> +config RNG_SANDBOX
> +       bool "Sandbox random number generator"
> +       depends on SANDBOX && DM_RNG
> +       help
> +         Enable random number generator for sandbox. This is an
> +	 emulation of a rng device.
> +
>   config RNG_STM32MP1
>          bool "Enable random number generator for STM32MP1"
>          depends on ARCH_STM32MP && DM_RNG
> diff --git a/drivers/rng/Makefile b/drivers/rng/Makefile
> index 699beb3..3517005 100644
> --- a/drivers/rng/Makefile
> +++ b/drivers/rng/Makefile
> @@ -4,4 +4,5 @@
>   #
>
>   obj-$(CONFIG_DM_RNG) += rng-uclass.o
> +obj-$(CONFIG_RNG_SANDBOX) += sandbox_rng.o
>   obj-$(CONFIG_RNG_STM32MP1) += stm32mp1_rng.o
> diff --git a/drivers/rng/sandbox_rng.c b/drivers/rng/sandbox_rng.c
> new file mode 100644
> index 0000000..8262e0d
> --- /dev/null
> +++ b/drivers/rng/sandbox_rng.c
> @@ -0,0 +1,39 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2019, Linaro Limited
> + */
> +
> +#include <common.h>
> +#include <dm.h>
> +#include <rng.h>
> +
> +static unsigned long random = 0xdeadbeef;
> +
> +static int sandbox_rng_read(struct udevice *dev, void *data, size_t len)
> +{
> +	if (len != sizeof(random))
> +		return -EINVAL;

This will drop on our feet once we use the RNG class for anything real.

Just imagine an EFI_RNG_PROTOCOL test failing on the sandbox because we
cannot read 128 bit.

Can't we use a PRNG here and reinitialize it when len == ~0UL?

Functions rand() and srand() are good enough for the job.

Best regards

Heinrich

> +
> +	random ^= ~0UL;
> +	*(unsigned long *)data = random;
> +
> +	return 0;
> +}
> +
> +static const struct dm_rng_ops sandbox_rng_ops = {
> +	.read = sandbox_rng_read,
> +};
> +
> +static const struct udevice_id sandbox_rng_match[] = {
> +	{
> +		.compatible = "sandbox,sandbox-rng",
> +	},
> +	{},
> +};
> +
> +U_BOOT_DRIVER(sandbox_rng) = {
> +	.name = "sandbox-rng",
> +	.id = UCLASS_RNG,
> +	.of_match = sandbox_rng_match,
> +	.ops = &sandbox_rng_ops,
> +};
>




More information about the U-Boot mailing list