Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp437101imu; Wed, 12 Dec 2018 20:53:25 -0800 (PST) X-Google-Smtp-Source: AFSGD/WMmwp+b4kJjeKFbdrkxj5uK+TaN8T1CBFeI3YKvxXCqF4l1bh+isYoMcrofH2mzy3llPXR X-Received: by 2002:a63:2109:: with SMTP id h9mr2209997pgh.277.1544676805013; Wed, 12 Dec 2018 20:53:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1544676804; cv=none; d=google.com; s=arc-20160816; b=GJEHycV0h4YapNKbZicx1Wg9wMx0uue7/iows7MWHEGf3P6B8w/7pi8O7MyRoU8FXl RjGBdtRHE4Tg3ZaCVthqLU8XYhsA/sq4Tp6pnT3BxOCn6jUMP/MMVHpXaq8fE3h2ZfUs f4o384MLpHGgC499NB+k/eeSkpmQu3hTazVaIoMscmNkRtDob2uyAKkSMue8FXJKzcW3 riPaVzgV5jncVwK+eit/BOiPg4CyhYJ+wP57LgShhp9WF4wozb/etx9JyddMGwcR3kSH 6NwacIxYSMlmIGUpTjIOKIXFL6CjwvXx3xnquQwU1w4nniArAfMQ3i4m1LPII+j9rqxL QXlA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=3Ei4zhiuJTQT4ckP9LIS4RtQhY7la856WNzxCgSqbEE=; b=pfoNIFIYF8l0nE7ucpci04JRmaxCzC0nR3FkxW9/MF50aww7zX9NMgEjJgg2rst8o0 zhXto4fzogDmkiX33mcufNrlWfvhuy/HauhYgYP1hdjG2nlNvff4sozm0CLFkFxJCykJ YKeviFVt458VYg2femaj7X/m+tOfqNZO4uj5BDDGRJyvuGscc2GYIxFhxHC3DulB7l2C kTjej0b4cWJ87CvHzSI+GBcpa9IM3p2y9qHQacJ1n1wEop3tGGOYXH56+A80sgLg8vwh EtXXcRYnVmPhLVSdGaRPqUYXlGQ/I6ZuoFVQ3oioWY/LqwLdhr4ZvDh1O3+HLbRayfdc ldGQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=d+UUT341; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id be11si661052plb.134.2018.12.12.20.53.10; Wed, 12 Dec 2018 20:53:24 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=d+UUT341; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728430AbeLMEu2 (ORCPT + 99 others); Wed, 12 Dec 2018 23:50:28 -0500 Received: from mail-ot1-f68.google.com ([209.85.210.68]:45163 "EHLO mail-ot1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729261AbeLMEu0 (ORCPT ); Wed, 12 Dec 2018 23:50:26 -0500 Received: by mail-ot1-f68.google.com with SMTP id 32so693835ota.12 for ; Wed, 12 Dec 2018 20:50:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=3Ei4zhiuJTQT4ckP9LIS4RtQhY7la856WNzxCgSqbEE=; b=d+UUT341AxkPB/HtWo/SLAt7NR3mfWTWgFKEV5vH1TdNLDo0CUU4DQY4o/lPLnY1CM K+8g97H7gz8aEnTzsGmWOjZLol9YTbZUkg2nzmFZFT58RVj9hY3sn1ts5SXI9EU9wdy8 IX5Kt6gs+mrkbFP8QDfehhjzxsC6wXx9V6MXU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=3Ei4zhiuJTQT4ckP9LIS4RtQhY7la856WNzxCgSqbEE=; b=OBehDlNDLfP7NnHWmT7e+vBIZSC3bnsLGrR8qN1q5GOndQk9jTQZfPJX9kZULJSwee mVuh5q4Nb15jNe4tmpQHSDr2yAlacKhroa3Qt1vPN9+786YvfAWyxdTrpzmbRbwzc5mA pDulzw1Al26qIgWSRPPV5LYCl2/Lxih8AEPZqRjgjIZp9rMhVLjCaFxNqLAa2iKdBOim 3JD9VJFjJ0di3JlOJ7XUzZUOZl3U+GrD+vfZJXDoVSiahJHHb/JrfyFkGc9hxqxcvRyu 2hZyYDkjrftoLyUSus6kMc1eIhyD91XR0Wv/kecyhdaNMoih1PJGkySmto2DJOqfgsez VOkA== X-Gm-Message-State: AA+aEWb4H4S04E/cVMvHCSy9RENpLsrtNC16aSh0wrVnHSETY+FcCZ0W UuoGF9hkuBYAiyclKRw/UhZxGpZtVDjJPF5WBrPBfg== X-Received: by 2002:a9d:5c88:: with SMTP id a8mr15232105oti.64.1544676625837; Wed, 12 Dec 2018 20:50:25 -0800 (PST) MIME-Version: 1.0 References: <20180926032455.224600-1-louiscollard@chromium.org> <568ea3447a8116ef460d191bf3599e2761bca3a5.camel@mniewoehner.de> In-Reply-To: <568ea3447a8116ef460d191bf3599e2761bca3a5.camel@mniewoehner.de> From: Louis Collard Date: Thu, 13 Dec 2018 12:50:15 +0800 Message-ID: Subject: Re: [PATCH] Allow hwrng to initialize crng. To: linux@mniewoehner.de Cc: linux-integrity@vger.kernel.org, Arnd Bergmann , Greg Kroah-Hartman , linux-kernel@vger.kernel.org, Jarkko Sakkinen , Andrey Pronin , Jason Gunthorpe , "David R. Bild" Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, Nov 18, 2018 at 4:15 AM Michael Niew=C3=B6hner wrote: > > Hi Louis, > > On Wed, 2018-09-26 at 11:24 +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/ > > > > Signed-off-by: Louis Collard > > Acked-by: Jarkko Sakkinen > > --- > > drivers/char/hw_random/core.c | 9 +++++++-- > > 1 file changed, 7 insertions(+), 2 deletions(-) > > > > diff --git a/drivers/char/hw_random/core.c b/drivers/char/hw_random/cor= e.c > > index aaf9e5afaad4..47f358aa0c3d 100644 > > --- a/drivers/char/hw_random/core.c > > +++ b/drivers/char/hw_random/core.c > > @@ -24,6 +24,7 @@ > > #include > > #include > > #include > > +#include > > > > #define RNG_MODULE_NAME "hw_random" > > > > @@ -64,13 +65,17 @@ static size_t rng_buffer_size(void) > > static void add_early_randomness(struct hwrng *rng) > > { > > int bytes_read; > > - size_t size =3D min_t(size_t, 16, rng_buffer_size()); > > + /* Read enough to initialize crng. */ > > + size_t size =3D 2*CHACHA20_KEY_SIZE; > > > > mutex_lock(&reading_mutex); > > bytes_read =3D 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) > > I found your patch by chance, searching for a solution for crng init dela= y on my > headless machine. Unfortunately it hardly makes any difference for me. Wi= th the > patch the system hangs for about 80s instead of 120s until the "crng init= done" > message.In contrast, doing a `cat /dev/hwrng >/dev/random` or running rng= d > initializes the crng instantly. > > Isn't that delay the problem this patch tries to fix? Any idea what is wr= ong > here? > > Thanks! > > Best regards > Michael > > Yes that is the problem this is trying to address. My guess would be rng_get_data() is not returning as much data as requested, so the delay is reduced but not eliminated. Looking at implementation of rng_get_data() it appears this could be caused by device support for read() vs data_read(). I don't have a good feel for whether looping to retrieve more data here would be acceptable, it is certainly a bigger change than currently proposed. Thanks, Louis