Re: [PATCH v28 01/12] Linux Random Number Generator

From: Stephan Mueller
Date: Thu Jan 16 2020 - 02:23:32 EST


Am Donnerstag, 16. Januar 2020, 01:11:40 CET schrieb Randy Dunlap:

Hi Randy,

> Hi,
>
> On 1/15/20 2:31 AM, Stephan MÃller wrote:
> > CC: "Eric W. Biederman" <ebiederm@xxxxxxxxxxxx>
> > CC: "Alexander E. Patrakov" <patrakov@xxxxxxxxx>
> > CC: "Ahmed S. Darwish" <darwish.07@xxxxxxxxx>
> > CC: "Theodore Y. Ts'o" <tytso@xxxxxxx>
> > CC: Willy Tarreau <w@xxxxxx>
> > CC: Matthew Garrett <mjg59@xxxxxxxxxxxxx>
> > CC: Vito Caputo <vcaputo@xxxxxxxxxxx>
> > CC: Andreas Dilger <adilger.kernel@xxxxxxxxx>
> > CC: Jan Kara <jack@xxxxxxx>
> > CC: Ray Strode <rstrode@xxxxxxxxxx>
> > CC: William Jon McCann <mccann@xxxxxxx>
> > CC: zhangjs <zachary@xxxxxxxxxxxxxxxx>
> > CC: Andy Lutomirski <luto@xxxxxxxxxx>
> > CC: Florian Weimer <fweimer@xxxxxxxxxx>
> > CC: Lennart Poettering <mzxreary@xxxxxxxxxxx>
> > CC: Nicolai Stange <nstange@xxxxxxx>
> > Mathematical aspects Reviewed-by: "Peter, Matthias"
> > <matthias.peter@xxxxxxxxxxx> Reviewed-by: Marcelo Henrique Cerri
> > <marcelo.cerri@xxxxxxxxxxxxx>
> > Reviewed-by: Roman Drahtmueller <draht@xxxxxxxxxxxxxx>
> > Tested-by: Roman DrahtmÃller <draht@xxxxxxxxxxxxxx>
> > Tested-by: Marcelo Henrique Cerri <marcelo.cerri@xxxxxxxxxxxxx>
> > Tested-by: Neil Horman <nhorman@xxxxxxxxxx>
> > Signed-off-by: Stephan Mueller <smueller@xxxxxxxxxx>
> > ---
> >
> > MAINTAINERS | 7 +
> > drivers/char/Kconfig | 2 +
> > drivers/char/Makefile | 9 +-
> > drivers/char/lrng/Kconfig | 67 +++
> > drivers/char/lrng/Makefile | 9 +
> > drivers/char/lrng/lrng_archrandom.c | 94 ++++
> > drivers/char/lrng/lrng_aux.c | 148 +++++++
> > drivers/char/lrng/lrng_chacha20.c | 265 ++++++++++++
> > drivers/char/lrng/lrng_chacha20.h | 25 ++
> > drivers/char/lrng/lrng_drng.c | 400 +++++++++++++++++
> > drivers/char/lrng/lrng_interfaces.c | 638 ++++++++++++++++++++++++++++
> > drivers/char/lrng/lrng_internal.h | 296 +++++++++++++
> > drivers/char/lrng/lrng_lfsr.h | 152 +++++++
> > drivers/char/lrng/lrng_pool.c | 588 +++++++++++++++++++++++++
> > drivers/char/lrng/lrng_sw_noise.c | 102 +++++
> > drivers/char/lrng/lrng_sw_noise.h | 57 +++
> > include/linux/lrng.h | 63 +++
> > 17 files changed, 2921 insertions(+), 1 deletion(-)
> > create mode 100644 drivers/char/lrng/Kconfig
> > create mode 100644 drivers/char/lrng/Makefile
> > create mode 100644 drivers/char/lrng/lrng_archrandom.c
> > create mode 100644 drivers/char/lrng/lrng_aux.c
> > create mode 100644 drivers/char/lrng/lrng_chacha20.c
> > create mode 100644 drivers/char/lrng/lrng_chacha20.h
> > create mode 100644 drivers/char/lrng/lrng_drng.c
> > create mode 100644 drivers/char/lrng/lrng_interfaces.c
> > create mode 100644 drivers/char/lrng/lrng_internal.h
> > create mode 100644 drivers/char/lrng/lrng_lfsr.h
> > create mode 100644 drivers/char/lrng/lrng_pool.c
> > create mode 100644 drivers/char/lrng/lrng_sw_noise.c
> > create mode 100644 drivers/char/lrng/lrng_sw_noise.h
> > create mode 100644 include/linux/lrng.h
> >
> > diff --git a/drivers/char/lrng/Kconfig b/drivers/char/lrng/Kconfig
> > new file mode 100644
> > index 000000000000..56f13efd3592
> > --- /dev/null
> > +++ b/drivers/char/lrng/Kconfig
> > @@ -0,0 +1,67 @@
> > +# SPDX-License-Identifier: GPL-2.0
> > +#
> > +# Linux Random Number Generator configuration
> > +#
> > +
> > +menuconfig LRNG
> > + bool "Linux Random Number Generator"
>
> This should probably depend on CRYPTO and/or some other CRYPTO_xxx symbols.
> Or (worst case) select them. :(

It does not - all CRYPTO related code is limited into the lrng_drbg.c,
lrng_kcapi.c and lrng_jent.c which are all guarded by a Kconfig option.
>
> This message (when CONFIG_CRYPTO is disabled and no crypto facilities are
> enabled) should be avoidable when the correct Kconfig entries are used:
>
> ../drivers/char/lrng/lrng_drbg.c:38:2: error: #error "Unknown DRBG in use"
> #error "Unknown DRBG in use"

Right - this should now be fixed with a depends on CRYPTO for the
aforementioned 3 C files which have an equal entry in Kconfig.
>
> > + help
> > + The Linux Random Number Generator (LRNG) is the replacement
> > + of the existing /dev/random provided with drivers/char/random.c.
> > + It generates entropy from different noise sources and
> > + delivers significant entropy during boot.
> > +
> > +if LRNG
> > +
> > +choice
> > + prompt "LRNG Entropy Pool Size"
> > + default LRNG_POOL_SIZE_4096
> > + help
> > + Select the size of the LRNG entropy pool. The size of the
> > + entropy pool is relevant for the amount of entropy that
> > + the LRNG can maintain as a maximum. The larger the size
> > + of the entropy pool is the more entropy can be maintained
> > + but the less often older entropic values are overwritten
> > + with new entropy.
> > +
> > + config LRNG_POOL_SIZE_512
> > + bool "512 bits"
> > +
> > + config LRNG_POOL_SIZE_1024
> > + bool "1024 bits"
> > +
> > + config LRNG_POOL_SIZE_2048
> > + bool "2048 bits"
> > +
> > + config LRNG_POOL_SIZE_4096
> > + bool "4096 bits (default)"
> > +
> > + config LRNG_POOL_SIZE_8192
> > + bool "8192 bits"
> > +
> > + config LRNG_POOL_SIZE_16384
> > + bool "16384 bits"
> > +
> > + config LRNG_POOL_SIZE_32768
> > + bool "32768 bits"
> > +
> > + config LRNG_POOL_SIZE_65536
> > + bool "65536 bits"
> > +
> > + config LRNG_POOL_SIZE_131072
> > + bool "131072 bits"
> > +endchoice
> > +
> > +config LRNG_POOL_SIZE
> > + int
> > + default 0 if LRNG_POOL_SIZE_512
> > + default 1 if LRNG_POOL_SIZE_1024
> > + default 2 if LRNG_POOL_SIZE_2048
> > + default 3 if LRNG_POOL_SIZE_4096
> > + default 4 if LRNG_POOL_SIZE_8192
> > + default 5 if LRNG_POOL_SIZE_16384
> > + default 6 if LRNG_POOL_SIZE_32768
> > + default 7 if LRNG_POOL_SIZE_65536
> > + default 8 if LRNG_POOL_SIZE_131072
> > +
> > +endif # LRNG
> >
> > diff --git a/drivers/char/lrng/lrng_archrandom.c
> > b/drivers/char/lrng/lrng_archrandom.c new file mode 100644
> > index 000000000000..eeba708d025f
> > --- /dev/null
> > +++ b/drivers/char/lrng/lrng_archrandom.c
> > @@ -0,0 +1,94 @@
> > +// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
> > +/*
> > + * LRNG Fast Noise Source: CPU-based noise source
> > + *
> > + * Copyright (C) 2016 - 2020, Stephan Mueller <smueller@xxxxxxxxxx>
> > + */
> > +
> > +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
> > +
> > +#include <linux/random.h>
> > +
> > +#include "lrng_internal.h"
> > +
> > +/*
> > + * Estimated entropy of data is a 32th of
> > LRNG_DRNG_SECURITY_STRENGTH_BITS. + * As we have no ability to review the
> > implementation of those noise sources, + * it is prudent to have a
> > conservative estimate here.
> > + */
> > +#define LRNG_ARCHRANDOM_DEFAULT_STRENGTH
> > (LRNG_DRNG_SECURITY_STRENGTH_BITS>>5) +#define
> > LRNG_ARCHRANDOM_TRUST_CPU_STRENGTH LRNG_DRNG_SECURITY_STRENGTH_BITS
> > +#ifdef CONFIG_RANDOM_TRUST_CPU
> > +static u32 archrandom = LRNG_ARCHRANDOM_TRUST_CPU_STRENGTH;
> > +#else
> > +static u32 archrandom = LRNG_ARCHRANDOM_DEFAULT_STRENGTH;
> > +#endif
> > +module_param(archrandom, uint, 0644);
> > +MODULE_PARM_DESC(archrandom, "Entropy in bits of 256 data bits from CPU
> > noise " + "source (e.g. RDRAND)");
>
> Please put the string on one line like several other MODULE_PARM_DESC() are
> done:
>
> +MODULE_PARM_DESC(archrandom,
> + "Entropy in bits of 256 data bits from CPU noise source (e.g.
RDRAND)");

Done.
>
>
> With CONFIG_CRYPTO disabled, these warnings happen:
>
> WARNING: unmet direct dependencies detected for CRYPTO_DRBG_MENU
> Depends on [n]: CRYPTO [=n]
> Selected by [m]:
> - LRNG_DRBG [=m] && LRNG [=y] && LRNG_DRNG_SWITCH [=y]
>
> WARNING: unmet direct dependencies detected for CRYPTO_RNG
> Depends on [n]: CRYPTO [=n]
> Selected by [m]:
> - LRNG_KCAPI [=m] && LRNG [=y] && LRNG_DRNG_SWITCH [=y]
>
> ../drivers/char/lrng/lrng_drbg.c: In function âlrng_hash_nameâ:
> ../drivers/char/lrng/lrng_drbg.c:225:1: warning: control reaches end of
> non-void function [-Wreturn-type] }
> ^
> ../drivers/char/lrng/lrng_drbg.c: In function âlrng_drbg_nameâ:
> ../drivers/char/lrng/lrng_drbg.c:220:1: warning: control reaches end of
> non-void function [-Wreturn-type] }
> ^
>
> and build errors happen also, which can be prevented with Kconfig fixes.

With the changes to the Kconfig file as explained in the other emails, I can
successfully compile the LRNG without the crypto API being enabled.

Thank you for pointing that one out.

Ciao
Stephan