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

From
Stephan Mueller <hidden>
Date
2020-01-16 07:23:30
Also in
linux-crypto, lkml
Reply to
Randy Dunlap, 2020-01-16
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:
quoted
CC: "Eric W. Biederman" <redacted>
CC: "Alexander E. Patrakov" <redacted>
CC: "Ahmed S. Darwish" <redacted>
CC: "Theodore Y. Ts'o" <redacted>
CC: Willy Tarreau <w@1wt.eu>
CC: Matthew Garrett <redacted>
CC: Vito Caputo <redacted>
CC: Andreas Dilger <redacted>
CC: Jan Kara <redacted>
CC: Ray Strode <redacted>
CC: William Jon McCann <redacted>
CC: zhangjs <redacted>
CC: Andy Lutomirski <redacted>
CC: Florian Weimer <redacted>
CC: Lennart Poettering <redacted>
CC: Nicolai Stange <redacted>
Mathematical aspects Reviewed-by: "Peter, Matthias"
[off-list ref] Reviewed-by: Marcelo Henrique Cerri
[off-list ref]
Reviewed-by: Roman Drahtmueller <redacted>
Tested-by: Roman Drahtmüller <redacted>
Tested-by: Marcelo Henrique Cerri <redacted>
Tested-by: Neil Horman <redacted>
Signed-off-by: Stephan Mueller <redacted>
---

 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.
quoted
+	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-T9tCv8IpfcWELgA04lAiVw@public.gmane.org>
+ */
+
+#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
Thread (154 messages) 154 messages, 11 authors, last reply 2020-01-20
Keyboard shortcuts
hback out one level
jnext message in thread / next series-diff position
kprevious message in thread / previous series-diff position
ldrill in
Escclose help / fold thread tree
?toggle this help