Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6053439iog; Thu, 23 Jun 2022 10:20:12 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sOaCYIROFDfN462p7la6RnU105l3jP/lw3yvPP7KCBj4AHKFPQMAslsnSGXfPbAT6/2YZn X-Received: by 2002:a17:907:1620:b0:711:da21:d836 with SMTP id hb32-20020a170907162000b00711da21d836mr9396085ejc.427.1656004812567; Thu, 23 Jun 2022 10:20:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656004812; cv=none; d=google.com; s=arc-20160816; b=Exg7tN5sJY7BNcW+oNRr4GC+CMKxn6sXmgvUelDAbIDQj/J7nYqS9l5Dbk/MV4C2kx Cr0bRQwmQQAp3QBxbAz5qkhjz987vj+RbgmeSYUb1Zybzdp4h62HtVaD8NZihZGUQvk1 ttngi2lGvZq3W+cyVDrEB/sYGoJzpFgaWTqOCA7xofrRM+m/LLRmBCwqYZXGey4qk/c2 X25aVHnTUXueVTCaGjJ19AYXMTohkZl9e0SkALOww/QFlxaxltd67rnjOZi9SD5HEOu+ 0ENUcmBMxRnt87V/S6hOeQ+Ndd/35OL13fcGI2KQwMq8WzjfINL1HQ4Aya9N/9b+Oey1 FvFA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=XLG2aJ9/u8Skfhemqe9148cxBtRvAeOuTD3RIiMvxj0=; b=Dn8r7hJikjkDU6xzGkAh29J8YUesfD1T4eXTULxZqPN5qS/lbiCBtHHQLQ4OQxNhTT 4y/MfyfiUDkUIFo2vE+kkii96uC4zOq+RZ0+f7CA756wlnndsJNa/m3YciQeJ390Eh2G VT4DIaTxwmrRZAdkiae3VyI5mmOmKMuBze4CqgTdyj65Wg1Okdaj0TFhTg6M3X88GKNt 6oEF5H29S6/ClEnBu2SLETn1noAWgHJ18ct/g1+GDbpIwrVgD+QFUjGgYmKnxkav62c6 DIT+7A+lBO8U2blM/MSq0JFFEYeLTaJQt1tnJ0xogXMTyjaVQDTm7e4AYU80cqjIujgW ZiSg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=kzhUrAsB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f21-20020a0564021e9500b004357dec0f7csi108070edf.571.2022.06.23.10.19.46; Thu, 23 Jun 2022 10:20:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=kzhUrAsB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231244AbiFWRPZ (ORCPT + 99 others); Thu, 23 Jun 2022 13:15:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42830 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233163AbiFWRMh (ORCPT ); Thu, 23 Jun 2022 13:12:37 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B28C4248D2; Thu, 23 Jun 2022 09:58:09 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 9E087B82497; Thu, 23 Jun 2022 16:58:06 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E3963C3411B; Thu, 23 Jun 2022 16:58:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656003485; bh=URmXZGWfp4mYOPqbgeXf3TKeUzk0jpETKvNZ90kaMgg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kzhUrAsBqkB2m1nD0asaC+wokBKq6mgxyrd16VKT3iRZA1mj4D+C+GH1pvFdBxLLI AawTIomaJm9XmsxgL1FJTTHoNK06qRjbEw3U/L5JnTyZmMx7XNJ4Qh/VOQh1bvzBe1 WYWOZjHhfnBFwvyj2lLUgHNtsKpTiNDuFzKn1b2o= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Nicolai Stange , =?UTF-8?q?Stephan=20M=C3=BCller?= , Herbert Xu , "Jason A. Donenfeld" Subject: [PATCH 4.9 217/264] crypto: drbg - prepare for more fine-grained tracking of seeding state Date: Thu, 23 Jun 2022 18:43:30 +0200 Message-Id: <20220623164350.216558710@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220623164344.053938039@linuxfoundation.org> References: <20220623164344.053938039@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Nicolai Stange commit ce8ce31b2c5c8b18667784b8c515650c65d57b4e upstream. There are two different randomness sources the DRBGs are getting seeded from, namely the jitterentropy source (if enabled) and get_random_bytes(). At initial DRBG seeding time during boot, the latter might not have collected sufficient entropy for seeding itself yet and thus, the DRBG implementation schedules a reseed work from a random_ready_callback once that has happened. This is particularly important for the !->pr DRBG instances, for which (almost) no further reseeds are getting triggered during their lifetime. Because collecting data from the jitterentropy source is a rather expensive operation, the aforementioned asynchronously scheduled reseed work restricts itself to get_random_bytes() only. That is, it in some sense amends the initial DRBG seed derived from jitterentropy output at full (estimated) entropy with fresh randomness obtained from get_random_bytes() once that has been seeded with sufficient entropy itself. With the advent of rng_is_initialized(), there is no real need for doing the reseed operation from an asynchronously scheduled work anymore and a subsequent patch will make it synchronous by moving it next to related logic already present in drbg_generate(). However, for tracking whether a full reseed including the jitterentropy source is required or a "partial" reseed involving only get_random_bytes() would be sufficient already, the boolean struct drbg_state's ->seeded member must become a tristate value. Prepare for this by introducing the new enum drbg_seed_state and change struct drbg_state's ->seeded member's type from bool to that type. For facilitating review, enum drbg_seed_state is made to only contain two members corresponding to the former ->seeded values of false and true resp. at this point: DRBG_SEED_STATE_UNSEEDED and DRBG_SEED_STATE_FULL. A third one for tracking the intermediate state of "seeded from jitterentropy only" will be introduced with a subsequent patch. There is no change in behaviour at this point. Signed-off-by: Nicolai Stange Reviewed-by: Stephan Müller Signed-off-by: Herbert Xu Signed-off-by: Jason A. Donenfeld Signed-off-by: Greg Kroah-Hartman Signed-off-by: Greg Kroah-Hartman --- crypto/drbg.c | 19 ++++++++++--------- include/crypto/drbg.h | 7 ++++++- 2 files changed, 16 insertions(+), 10 deletions(-) --- a/crypto/drbg.c +++ b/crypto/drbg.c @@ -1044,7 +1044,7 @@ static inline int __drbg_seed(struct drb if (ret) return ret; - drbg->seeded = true; + drbg->seeded = DRBG_SEED_STATE_FULL; /* 10.1.1.2 / 10.1.1.3 step 5 */ drbg->reseed_ctr = 1; @@ -1089,14 +1089,14 @@ static void drbg_async_seed(struct work_ if (ret) goto unlock; - /* Set seeded to false so that if __drbg_seed fails the - * next generate call will trigger a reseed. + /* Reset ->seeded so that if __drbg_seed fails the next + * generate call will trigger a reseed. */ - drbg->seeded = false; + drbg->seeded = DRBG_SEED_STATE_UNSEEDED; __drbg_seed(drbg, &seedlist, true); - if (drbg->seeded) + if (drbg->seeded == DRBG_SEED_STATE_FULL) drbg->reseed_threshold = drbg_max_requests(drbg); unlock: @@ -1385,13 +1385,14 @@ static int drbg_generate(struct drbg_sta * here. The spec is a bit convoluted here, we make it simpler. */ if (drbg->reseed_threshold < drbg->reseed_ctr) - drbg->seeded = false; + drbg->seeded = DRBG_SEED_STATE_UNSEEDED; - if (drbg->pr || !drbg->seeded) { + if (drbg->pr || drbg->seeded == DRBG_SEED_STATE_UNSEEDED) { pr_devel("DRBG: reseeding before generation (prediction " "resistance: %s, state %s)\n", drbg->pr ? "true" : "false", - drbg->seeded ? "seeded" : "unseeded"); + (drbg->seeded == DRBG_SEED_STATE_FULL ? + "seeded" : "unseeded")); /* 9.3.1 steps 7.1 through 7.3 */ len = drbg_seed(drbg, addtl, true); if (len) @@ -1576,7 +1577,7 @@ static int drbg_instantiate(struct drbg_ if (!drbg->core) { drbg->core = &drbg_cores[coreref]; drbg->pr = pr; - drbg->seeded = false; + drbg->seeded = DRBG_SEED_STATE_UNSEEDED; drbg->reseed_threshold = drbg_max_requests(drbg); ret = drbg_alloc_state(drbg); --- a/include/crypto/drbg.h +++ b/include/crypto/drbg.h @@ -105,6 +105,11 @@ struct drbg_test_data { struct drbg_string *testentropy; /* TEST PARAMETER: test entropy */ }; +enum drbg_seed_state { + DRBG_SEED_STATE_UNSEEDED, + DRBG_SEED_STATE_FULL, +}; + struct drbg_state { struct mutex drbg_mutex; /* lock around DRBG */ unsigned char *V; /* internal state 10.1.1.1 1a) */ @@ -129,7 +134,7 @@ struct drbg_state { struct completion ctr_completion; /* CTR mode async handler */ int ctr_async_err; /* CTR mode async error */ - bool seeded; /* DRBG fully seeded? */ + enum drbg_seed_state seeded; /* DRBG fully seeded? */ bool pr; /* Prediction resistance enabled? */ bool fips_primed; /* Continuous test primed? */ unsigned char *prev; /* FIPS 140-2 continuous test value */