linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] Allow hwrng to initialize crng.
@ 2018-12-13  9:18 Louis Collard
  2018-12-13  9:48 ` Ard Biesheuvel
  2018-12-14  7:56 ` Jarkko Sakkinen
  0 siblings, 2 replies; 5+ messages in thread
From: Louis Collard @ 2018-12-13  9:18 UTC (permalink / raw)
  To: linux-crypto
  Cc: Matt Mackall, Herbert Xu, Arnd Bergmann, Greg Kroah-Hartman,
	Gary R Hook, Michael Buesch, PrasannaKumar Muralidharan,
	Louis Collard, Michael S . Tsirkin, linux-kernel, apronin,
	jarkko.sakkinen, linux, david.bild, tytso

Some systems, for example embedded systems, do not generate
enough entropy on boot through interrupts, and boot may be blocked for
several minutes waiting for a call to getrandom to complete.

Currently, random data is read from a hwrng when it is registered,
and is loaded into primary_crng. This data is treated in the same
way as data that is device-specific but otherwise unchanging, and
so primary_crng cannot become initialized with the data from the
hwrng.

This change causes the data initially read from the hwrng to be
treated the same as subsequent data that is read from the hwrng if
it's quality score is non-zero.

The implications of this are:

The data read from hwrng can cause primary_crng to become
initialized, therefore avoiding problems of getrandom blocking
on boot.

Calls to getrandom (with GRND_RANDOM) may be using entropy
exclusively (or in practise, almost exclusively) from the hwrng.

Regarding the latter point; this behavior is the same as if a
user specified a quality score of 1 (bit of entropy per 1024 bits)
so hopefully this is not too scary a change to make.

This change is the result of the discussion here:
https://patchwork.kernel.org/patch/10453893/

Signed-off-by: Louis Collard <louiscollard@chromium.org>
Acked-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
---
 drivers/char/hw_random/core.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/drivers/char/hw_random/core.c b/drivers/char/hw_random/core.c
index 95be7228f327..7736e1a96321 100644
--- a/drivers/char/hw_random/core.c
+++ b/drivers/char/hw_random/core.c
@@ -24,6 +24,7 @@
 #include <linux/sched.h>
 #include <linux/slab.h>
 #include <linux/uaccess.h>
+#include <crypto/chacha20.h>
 
 #define RNG_MODULE_NAME		"hw_random"
 
@@ -64,13 +65,19 @@ static size_t rng_buffer_size(void)
 static void add_early_randomness(struct hwrng *rng)
 {
 	int bytes_read;
-	size_t size = min_t(size_t, 16, rng_buffer_size());
+	/* Read enough to initialize crng. */
+	size_t size = min_t(size_t,
+			    2*CHACHA20_KEY_SIZE,
+			    rng_buffer_size());
 
 	mutex_lock(&reading_mutex);
 	bytes_read = rng_get_data(rng, rng_buffer, size, 1);
 	mutex_unlock(&reading_mutex);
 	if (bytes_read > 0)
-		add_device_randomness(rng_buffer, bytes_read);
+		/* Allow crng to become initialized, but do not add
+		 * entropy to the pool.
+		 */
+		add_hwgenerator_randomness(rng_buffer, bytes_read, 0);
 }
 
 static inline void cleanup_rng(struct kref *kref)
-- 
2.13.5


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] Allow hwrng to initialize crng.
  2018-12-13  9:18 [PATCH v2] Allow hwrng to initialize crng Louis Collard
@ 2018-12-13  9:48 ` Ard Biesheuvel
  2018-12-14  3:12   ` Louis Collard
  2018-12-14  3:58   ` Theodore Y. Ts'o
  2018-12-14  7:56 ` Jarkko Sakkinen
  1 sibling, 2 replies; 5+ messages in thread
From: Ard Biesheuvel @ 2018-12-13  9:48 UTC (permalink / raw)
  To: louiscollard
  Cc: open list:HARDWARE RANDOM NUMBER GENERATOR CORE, mpm, Herbert Xu,
	Arnd Bergmann, Greg Kroah-Hartman, gary.hook, m,
	PrasannaKumar Muralidharan, Michael S. Tsirkin,
	Linux Kernel Mailing List, apronin, Jarkko Sakkinen, linux,
	david.bild, Theodore Ts'o

On Thu, 13 Dec 2018 at 10:18, Louis Collard <louiscollard@chromium.org> wrote:
>
> Some systems, for example embedded systems, do not generate
> enough entropy on boot through interrupts, and boot may be blocked for
> several minutes waiting for a call to getrandom to complete.
>
> Currently, random data is read from a hwrng when it is registered,
> and is loaded into primary_crng. This data is treated in the same
> way as data that is device-specific but otherwise unchanging, and
> so primary_crng cannot become initialized with the data from the
> hwrng.
>
> This change causes the data initially read from the hwrng to be
> treated the same as subsequent data that is read from the hwrng if
> it's quality score is non-zero.
>
> The implications of this are:
>
> The data read from hwrng can cause primary_crng to become
> initialized, therefore avoiding problems of getrandom blocking
> on boot.
>
> Calls to getrandom (with GRND_RANDOM) may be using entropy
> exclusively (or in practise, almost exclusively) from the hwrng.
>
> Regarding the latter point; this behavior is the same as if a
> user specified a quality score of 1 (bit of entropy per 1024 bits)
> so hopefully this is not too scary a change to make.
>
> This change is the result of the discussion here:
> https://patchwork.kernel.org/patch/10453893/
>
> Signed-off-by: Louis Collard <louiscollard@chromium.org>
> Acked-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
> ---
>  drivers/char/hw_random/core.c | 11 +++++++++--
>  1 file changed, 9 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/char/hw_random/core.c b/drivers/char/hw_random/core.c
> index 95be7228f327..7736e1a96321 100644
> --- a/drivers/char/hw_random/core.c
> +++ b/drivers/char/hw_random/core.c
> @@ -24,6 +24,7 @@
>  #include <linux/sched.h>
>  #include <linux/slab.h>
>  #include <linux/uaccess.h>
> +#include <crypto/chacha20.h>
>
>  #define RNG_MODULE_NAME                "hw_random"
>
> @@ -64,13 +65,19 @@ static size_t rng_buffer_size(void)
>  static void add_early_randomness(struct hwrng *rng)
>  {
>         int bytes_read;
> -       size_t size = min_t(size_t, 16, rng_buffer_size());
> +       /* Read enough to initialize crng. */
> +       size_t size = min_t(size_t,
> +                           2*CHACHA20_KEY_SIZE,

This should be as symbolic constant that retains its meaning even if
we move away from ChaCha20 or modify the current implementation

> +                           rng_buffer_size());
>
>         mutex_lock(&reading_mutex);
>         bytes_read = rng_get_data(rng, rng_buffer, size, 1);
>         mutex_unlock(&reading_mutex);
>         if (bytes_read > 0)
> -               add_device_randomness(rng_buffer, bytes_read);
> +               /* Allow crng to become initialized, but do not add
> +                * entropy to the pool.
> +                */
> +               add_hwgenerator_randomness(rng_buffer, bytes_read, 0);
>  }
>
>  static inline void cleanup_rng(struct kref *kref)
> --
> 2.13.5
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] Allow hwrng to initialize crng.
  2018-12-13  9:48 ` Ard Biesheuvel
@ 2018-12-14  3:12   ` Louis Collard
  2018-12-14  3:58   ` Theodore Y. Ts'o
  1 sibling, 0 replies; 5+ messages in thread
From: Louis Collard @ 2018-12-14  3:12 UTC (permalink / raw)
  To: ard.biesheuvel
  Cc: linux-crypto, mpm, Herbert Xu, Arnd Bergmann, Greg Kroah-Hartman,
	Gary R Hook, Michael Buesch, PrasannaKumar Muralidharan, mst,
	linux-kernel, Andrey Pronin, Jarkko Sakkinen, linux,
	David R. Bild, tytso

On Thu, Dec 13, 2018 at 5:48 PM Ard Biesheuvel
<ard.biesheuvel@linaro.org> wrote:
>
> On Thu, 13 Dec 2018 at 10:18, Louis Collard <louiscollard@chromium.org> wrote:
> >
> > Some systems, for example embedded systems, do not generate
> > enough entropy on boot through interrupts, and boot may be blocked for
> > several minutes waiting for a call to getrandom to complete.
> >
> > Currently, random data is read from a hwrng when it is registered,
> > and is loaded into primary_crng. This data is treated in the same
> > way as data that is device-specific but otherwise unchanging, and
> > so primary_crng cannot become initialized with the data from the
> > hwrng.
> >
> > This change causes the data initially read from the hwrng to be
> > treated the same as subsequent data that is read from the hwrng if
> > it's quality score is non-zero.
> >
> > The implications of this are:
> >
> > The data read from hwrng can cause primary_crng to become
> > initialized, therefore avoiding problems of getrandom blocking
> > on boot.
> >
> > Calls to getrandom (with GRND_RANDOM) may be using entropy
> > exclusively (or in practise, almost exclusively) from the hwrng.
> >
> > Regarding the latter point; this behavior is the same as if a
> > user specified a quality score of 1 (bit of entropy per 1024 bits)
> > so hopefully this is not too scary a change to make.
> >
> > This change is the result of the discussion here:
> > https://patchwork.kernel.org/patch/10453893/
> >
> > Signed-off-by: Louis Collard <louiscollard@chromium.org>
> > Acked-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
> > ---
> >  drivers/char/hw_random/core.c | 11 +++++++++--
> >  1 file changed, 9 insertions(+), 2 deletions(-)
> >
> > diff --git a/drivers/char/hw_random/core.c b/drivers/char/hw_random/core.c
> > index 95be7228f327..7736e1a96321 100644
> > --- a/drivers/char/hw_random/core.c
> > +++ b/drivers/char/hw_random/core.c
> > @@ -24,6 +24,7 @@
> >  #include <linux/sched.h>
> >  #include <linux/slab.h>
> >  #include <linux/uaccess.h>
> > +#include <crypto/chacha20.h>
> >
> >  #define RNG_MODULE_NAME                "hw_random"
> >
> > @@ -64,13 +65,19 @@ static size_t rng_buffer_size(void)
> >  static void add_early_randomness(struct hwrng *rng)
> >  {
> >         int bytes_read;
> > -       size_t size = min_t(size_t, 16, rng_buffer_size());
> > +       /* Read enough to initialize crng. */
> > +       size_t size = min_t(size_t,
> > +                           2*CHACHA20_KEY_SIZE,
>
> This should be as symbolic constant that retains its meaning even if
> we move away from ChaCha20 or modify the current implementation
>
> > +                           rng_buffer_size());
> >
> >         mutex_lock(&reading_mutex);
> >         bytes_read = rng_get_data(rng, rng_buffer, size, 1);
> >         mutex_unlock(&reading_mutex);
> >         if (bytes_read > 0)
> > -               add_device_randomness(rng_buffer, bytes_read);
> > +               /* Allow crng to become initialized, but do not add
> > +                * entropy to the pool.
> > +                */
> > +               add_hwgenerator_randomness(rng_buffer, bytes_read, 0);
> >  }
> >
> >  static inline void cleanup_rng(struct kref *kref)
> > --
> > 2.13.5
> >

Right, this should be [equal to] CRNG_INIT_CNT_THRESH from random.c -
I wasn't sure where/how to pull this out to though..

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] Allow hwrng to initialize crng.
  2018-12-13  9:48 ` Ard Biesheuvel
  2018-12-14  3:12   ` Louis Collard
@ 2018-12-14  3:58   ` Theodore Y. Ts'o
  1 sibling, 0 replies; 5+ messages in thread
From: Theodore Y. Ts'o @ 2018-12-14  3:58 UTC (permalink / raw)
  To: Ard Biesheuvel
  Cc: louiscollard, open list:HARDWARE RANDOM NUMBER GENERATOR CORE,
	mpm, Herbert Xu, Arnd Bergmann, Greg Kroah-Hartman, gary.hook, m,
	PrasannaKumar Muralidharan, Michael S. Tsirkin,
	Linux Kernel Mailing List, apronin, Jarkko Sakkinen, linux,
	david.bild

On Thu, Dec 13, 2018 at 10:48:07AM +0100, Ard Biesheuvel wrote:
> > @@ -64,13 +65,19 @@ static size_t rng_buffer_size(void)
> >  static void add_early_randomness(struct hwrng *rng)
> >  {
> >         int bytes_read;
> > -       size_t size = min_t(size_t, 16, rng_buffer_size());
> > +       /* Read enough to initialize crng. */
> > +       size_t size = min_t(size_t,
> > +                           2*CHACHA20_KEY_SIZE,
> 
> This should be as symbolic constant that retains its meaning even if
> we move away from ChaCha20 or modify the current implementation

Also, rng_buffer_size() could be less than 2*hCHACHA20_KEY_SIZE, at
which point your goal wouldn't be realized.  What I'd recommend is to
keep the line:

	size_t size = min_t(size_t, 16, rng_buffer_size());

But to loop until rng_is_initialized() returns true or bytes_read is
0.  If you want to be paranoid, you could also break out of the loop
it isn't initialized after, say, 8 times through the loop.

Cheers,

					- Ted

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] Allow hwrng to initialize crng.
  2018-12-13  9:18 [PATCH v2] Allow hwrng to initialize crng Louis Collard
  2018-12-13  9:48 ` Ard Biesheuvel
@ 2018-12-14  7:56 ` Jarkko Sakkinen
  1 sibling, 0 replies; 5+ messages in thread
From: Jarkko Sakkinen @ 2018-12-14  7:56 UTC (permalink / raw)
  To: Louis Collard
  Cc: linux-crypto, Matt Mackall, Herbert Xu, Arnd Bergmann,
	Greg Kroah-Hartman, Gary R Hook, Michael Buesch,
	PrasannaKumar Muralidharan, Michael S . Tsirkin, linux-kernel,
	apronin, linux, david.bild, tytso

On Thu, Dec 13, 2018 at 05:18:48PM +0800, Louis Collard wrote:
> Some systems, for example embedded systems, do not generate
> enough entropy on boot through interrupts, and boot may be blocked for
> several minutes waiting for a call to getrandom to complete.
> 
> Currently, random data is read from a hwrng when it is registered,
> and is loaded into primary_crng. This data is treated in the same
> way as data that is device-specific but otherwise unchanging, and
> so primary_crng cannot become initialized with the data from the
> hwrng.
> 
> This change causes the data initially read from the hwrng to be
> treated the same as subsequent data that is read from the hwrng if
> it's quality score is non-zero.
> 
> The implications of this are:
> 
> The data read from hwrng can cause primary_crng to become
> initialized, therefore avoiding problems of getrandom blocking
> on boot.
> 
> Calls to getrandom (with GRND_RANDOM) may be using entropy
> exclusively (or in practise, almost exclusively) from the hwrng.
> 
> Regarding the latter point; this behavior is the same as if a
> user specified a quality score of 1 (bit of entropy per 1024 bits)
> so hopefully this is not too scary a change to make.
> 
> This change is the result of the discussion here:
> https://patchwork.kernel.org/patch/10453893/

Please remove these two lines.

> Signed-off-by: Louis Collard <louiscollard@chromium.org>
> Acked-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
> ---

The change log seems to be missing before diffstat, after dashes.

/Jarkko

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2018-12-14  7:57 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-12-13  9:18 [PATCH v2] Allow hwrng to initialize crng Louis Collard
2018-12-13  9:48 ` Ard Biesheuvel
2018-12-14  3:12   ` Louis Collard
2018-12-14  3:58   ` Theodore Y. Ts'o
2018-12-14  7:56 ` Jarkko Sakkinen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).