Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6141014iog; Thu, 23 Jun 2022 12:11:52 -0700 (PDT) X-Google-Smtp-Source: AGRyM1ths7Su91yLHq2dCCTMK/0Ww3weEHlPckOLySVOANdqhQYGq2n3Gdgp2f1HMa9iKA3YDPp5 X-Received: by 2002:a63:36cc:0:b0:40c:5301:7fe2 with SMTP id d195-20020a6336cc000000b0040c53017fe2mr8606440pga.547.1656011511902; Thu, 23 Jun 2022 12:11:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656011511; cv=none; d=google.com; s=arc-20160816; b=Qk3+99+mnWXybIJ9yF3u35LyziR1bqUeoTM8NJp2xmlY47VvBKV2iku/bun/kSgTp8 lq9F1Vgv4317KGQyDbnB8B2liOFxEWqaubLx6tAyzbgCFHY1g4lSmTnI/8j1ZwSB4ZQN X6+Yf23mRq4mJHBeTDwo/xYvQGXk6tZQuDzBTArElkT1EFXql01dO2H/eiieFK/PCoVl NYcusKDgHmwaDTr3kpAvcvMWXKA3jkbAY4zhozORXglfAw64GLGnxYtg3Q2IDQYScUnZ SKVgjFDrDirTUfbzlsenz5R1LPxgPh+KN8GLtNA/YHOAIJJlazH5oty/HKgO/1wNhGkE +VrQ== 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=4GF8kBA5W3+H+YoEltfK8/2F2Zr6IDe45e9wUDNjFzk=; b=KulWdzejB/MS5A2KbE7EbT7as+xQ6dOmZS0UDyaGBUwv5Ke5SYu9LgqPrx1nMG+m0/ 65qHFYd+gy3lzQ513YIkc429erakxz7/jPzDeJH0DoUNvjEn92rj8+Jh11ot5uW+hwl7 T94kVqWu3sbBPYy33ZVupJmV6HvoRH5wZQ0ABqYOwMmoL+LFJ/mN1aicQ4ivTv6N32QL 0V4mPw3ddNSjVCuye9VQVqq3/HY9GKU//CmhdPtKyesxnVwMHxm3tTIyhdRhZlKEPJsG 7j4ZUcWxZRHxOqQ3smVs3lx5HU7+oe9MumwCB+tSKph1RLZ1KK16qrx4LkbtXKZmoH// CWLA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=H71Zb8yE; 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 j23-20020a632317000000b0040d28f1a10asi7988391pgj.810.2022.06.23.12.11.40; Thu, 23 Jun 2022 12:11:51 -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=H71Zb8yE; 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 S236981AbiFWSTh (ORCPT + 99 others); Thu, 23 Jun 2022 14:19:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47360 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236824AbiFWSQo (ORCPT ); Thu, 23 Jun 2022 14:16:44 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 285C2167F5; Thu, 23 Jun 2022 10:23:10 -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 dfw.source.kernel.org (Postfix) with ESMTPS id B51FD61EA7; Thu, 23 Jun 2022 17:23:09 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8FD1DC341C4; Thu, 23 Jun 2022 17:23:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656004989; bh=0H++xgumGyP4vXTmIwUqvpkenDYtXxwRDeRziKpHu3Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=H71Zb8yEdaZh3wYjLnXI233TveaMWf+VIGRL5Gx8C4abOFlUrMRbi/zHo+JrWxJKU kxyWF9XyHXK02igso/9/TlHSC0NooMc7G8GWPEGl5dSvJuBNx8zj4Cw0ZFoCXnJzqj Srhs5Qv4O3xbcaOAxJyUkzu3wjOhPAtlteUCl6wk= 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.19 173/234] crypto: drbg - prepare for more fine-grained tracking of seeding state Date: Thu, 23 Jun 2022 18:44:00 +0200 Message-Id: <20220623164347.947324428@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220623164343.042598055@linuxfoundation.org> References: <20220623164343.042598055@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 @@ -1042,7 +1042,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; @@ -1087,14 +1087,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: @@ -1383,13 +1383,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) @@ -1574,7 +1575,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) */ @@ -127,7 +132,7 @@ struct drbg_state { struct crypto_wait ctr_wait; /* CTR mode async wait obj */ struct scatterlist sg_in, sg_out; /* CTR mode SGLs */ - 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 */