Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6136492iog; Thu, 23 Jun 2022 12:06:37 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sSdzH58gSbFIp0c1teqeXFBomwRZ5qUtEpKOqlm9tCiTRXo1m0wXuVPzG70r9tnQ/I3i8K X-Received: by 2002:a17:90a:df98:b0:1ec:96e5:b04d with SMTP id p24-20020a17090adf9800b001ec96e5b04dmr5371538pjv.185.1656011197197; Thu, 23 Jun 2022 12:06:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656011197; cv=none; d=google.com; s=arc-20160816; b=PJl4mLBdB08n1G6XLz15aLd6GbbLRhylm8kQRBjpNVpE7UPK4QS4EYeO3xYO0CMvXM Ki+d1D0CixgoEZ7i/eiVp9OFSYg+312jrSrEsUrTnjsrUXnK51s/g9h8kJ4h/G9DRNnj IuUdXT6cS/GQid4DvzS+ijMfz42swm74EClqcflHBWNpH/UIBhsn38RPAsKhKeXpNLzT rGF8AEADw6kpNxRknPOL5fUtBHfJWXreR2WlE1AobAqVydVxUF/D53Jqjhs4RVodSrFX X6EP3wQnihhQOYt39/EZEM3l61K7G2EHQWGpWlvjZ2tv6dm2+RAsuCt25FYnS/FaZpnx lkzA== 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=faoDTVoZnsrHgdzx1eISgMNObXRfgRHsQSCtE1PA0J4=; b=OdTTREdJ8Pa3jsY9sbqTqDMu7cY6Q+qhBEwTlHxqNoADz/pC4z5j9dNSsDOJI3SI7u ere94LEy6xP7Hlh5iOzNAq6ywkoffAVwvrGqYKRkcCbKqdk82/R2JXmBAB2oOhGQYEMw uvgoG3nQADP7sQZhY+bhTH+y7CYbx5nllSJF+4O9S7e5GcDBrEFYwndERU+oreFreEI3 XgbEl4wVnRTVSvo7YZHCoJaC077yvMnGIJdsY0X1J+P+c8eh2dS2DZi54xMF+6ne8Bp0 A6hBRxH420VD5lN5GRHPvlLl9Jwax23It5vEsza+sSajGVIXxaA28JFehMgi5TzDoM/e iSBQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Zn9yjJdn; 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 e6-20020a170902ef4600b0016a52b34991si491178plx.113.2022.06.23.12.06.23; Thu, 23 Jun 2022 12:06:37 -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=Zn9yjJdn; 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 S231890AbiFWRLA (ORCPT + 99 others); Thu, 23 Jun 2022 13:11:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39110 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233934AbiFWRI3 (ORCPT ); Thu, 23 Jun 2022 13:08:29 -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 7E5AF54BFE; Thu, 23 Jun 2022 09:57: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 dfw.source.kernel.org (Postfix) with ESMTPS id 8A91A60AE7; Thu, 23 Jun 2022 16:56:59 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 62C0CC341C5; Thu, 23 Jun 2022 16:56:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656003418; bh=G1HhTB/ni4xta0LtrEap2YX3FQEIHfqQnjNSgsOz9D0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Zn9yjJdnkOyIn+8cwqTkZt0DOMYlX7iKRkNXPGN8DaL454qDczKltST1EqpRHtrbM UH2/3NdEIXtla7T3soBlRFzTG7+btEkO2egfcs3tsjNwjEtmPQuXqAGyqPaCovNJgf O4Xt/fabnm8d6I2BCUHWZ03XHj5xpVoklSXI6Diw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Stephan Mueller , Yann Droneaud , Herbert Xu , "Jason A. Donenfeld" Subject: [PATCH 4.9 215/264] crypto: drbg - add FIPS 140-2 CTRNG for noise source Date: Thu, 23 Jun 2022 18:43:28 +0200 Message-Id: <20220623164350.158407689@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: Stephan Mueller commit db07cd26ac6a418dc2823187958edcfdb415fa83 upstream. FIPS 140-2 section 4.9.2 requires a continuous self test of the noise source. Up to kernel 4.8 drivers/char/random.c provided this continuous self test. Afterwards it was moved to a location that is inconsistent with the FIPS 140-2 requirements. The relevant patch was e192be9d9a30555aae2ca1dc3aad37cba484cd4a . Thus, the FIPS 140-2 CTRNG is added to the DRBG when it obtains the seed. This patch resurrects the function drbg_fips_continous_test that existed some time ago and applies it to the noise sources. The patch that removed the drbg_fips_continous_test was b3614763059b82c26bdd02ffcb1c016c1132aad0 . The Jitter RNG implements its own FIPS 140-2 self test and thus does not need to be subjected to the test in the DRBG. The patch contains a tiny fix to ensure proper zeroization in case of an error during the Jitter RNG data gathering. Signed-off-by: Stephan Mueller Reviewed-by: Yann Droneaud Signed-off-by: Herbert Xu Signed-off-by: Jason A. Donenfeld Signed-off-by: Greg Kroah-Hartman --- crypto/drbg.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++-- include/crypto/drbg.h | 2 + 2 files changed, 93 insertions(+), 3 deletions(-) --- a/crypto/drbg.c +++ b/crypto/drbg.c @@ -220,6 +220,57 @@ static inline unsigned short drbg_sec_st } /* + * FIPS 140-2 continuous self test for the noise source + * The test is performed on the noise source input data. Thus, the function + * implicitly knows the size of the buffer to be equal to the security + * strength. + * + * Note, this function disregards the nonce trailing the entropy data during + * initial seeding. + * + * drbg->drbg_mutex must have been taken. + * + * @drbg DRBG handle + * @entropy buffer of seed data to be checked + * + * return: + * 0 on success + * -EAGAIN on when the CTRNG is not yet primed + * < 0 on error + */ +static int drbg_fips_continuous_test(struct drbg_state *drbg, + const unsigned char *entropy) +{ + unsigned short entropylen = drbg_sec_strength(drbg->core->flags); + int ret = 0; + + if (!IS_ENABLED(CONFIG_CRYPTO_FIPS)) + return 0; + + /* skip test if we test the overall system */ + if (list_empty(&drbg->test_data.list)) + return 0; + /* only perform test in FIPS mode */ + if (!fips_enabled) + return 0; + + if (!drbg->fips_primed) { + /* Priming of FIPS test */ + memcpy(drbg->prev, entropy, entropylen); + drbg->fips_primed = true; + /* priming: another round is needed */ + return -EAGAIN; + } + ret = memcmp(drbg->prev, entropy, entropylen); + if (!ret) + panic("DRBG continuous self test failed\n"); + memcpy(drbg->prev, entropy, entropylen); + + /* the test shall pass when the two values are not equal */ + return 0; +} + +/* * Convert an integer into a byte representation of this integer. * The byte representation is big-endian * @@ -1000,6 +1051,22 @@ static inline int __drbg_seed(struct drb return ret; } +static inline int drbg_get_random_bytes(struct drbg_state *drbg, + unsigned char *entropy, + unsigned int entropylen) +{ + int ret; + + do { + get_random_bytes(entropy, entropylen); + ret = drbg_fips_continuous_test(drbg, entropy); + if (ret && ret != -EAGAIN) + return ret; + } while (ret); + + return 0; +} + static void drbg_async_seed(struct work_struct *work) { struct drbg_string data; @@ -1008,16 +1075,20 @@ static void drbg_async_seed(struct work_ seed_work); unsigned int entropylen = drbg_sec_strength(drbg->core->flags); unsigned char entropy[32]; + int ret; BUG_ON(!entropylen); BUG_ON(entropylen > sizeof(entropy)); - get_random_bytes(entropy, entropylen); drbg_string_fill(&data, entropy, entropylen); list_add_tail(&data.list, &seedlist); mutex_lock(&drbg->drbg_mutex); + ret = drbg_get_random_bytes(drbg, entropy, entropylen); + if (ret) + goto unlock; + /* If nonblocking pool is initialized, deactivate Jitter RNG */ crypto_free_rng(drbg->jent); drbg->jent = NULL; @@ -1032,6 +1103,7 @@ static void drbg_async_seed(struct work_ if (drbg->seeded) drbg->reseed_threshold = drbg_max_requests(drbg); +unlock: mutex_unlock(&drbg->drbg_mutex); memzero_explicit(entropy, entropylen); @@ -1083,7 +1155,9 @@ static int drbg_seed(struct drbg_state * BUG_ON((entropylen * 2) > sizeof(entropy)); /* Get seed from in-kernel /dev/urandom */ - get_random_bytes(entropy, entropylen); + ret = drbg_get_random_bytes(drbg, entropy, entropylen); + if (ret) + goto out; if (!drbg->jent) { drbg_string_fill(&data1, entropy, entropylen); @@ -1096,7 +1170,7 @@ static int drbg_seed(struct drbg_state * entropylen); if (ret) { pr_devel("DRBG: jent failed with %d\n", ret); - return ret; + goto out; } drbg_string_fill(&data1, entropy, entropylen * 2); @@ -1123,6 +1197,7 @@ static int drbg_seed(struct drbg_state * ret = __drbg_seed(drbg, &seedlist, reseed); +out: memzero_explicit(entropy, entropylen * 2); return ret; @@ -1144,6 +1219,11 @@ static inline void drbg_dealloc_state(st drbg->reseed_ctr = 0; drbg->d_ops = NULL; drbg->core = NULL; + if (IS_ENABLED(CONFIG_CRYPTO_FIPS)) { + kzfree(drbg->prev); + drbg->prev = NULL; + drbg->fips_primed = false; + } } /* @@ -1213,6 +1293,14 @@ static inline int drbg_alloc_state(struc drbg->scratchpad = PTR_ALIGN(drbg->scratchpadbuf, ret + 1); } + if (IS_ENABLED(CONFIG_CRYPTO_FIPS)) { + drbg->prev = kzalloc(drbg_sec_strength(drbg->core->flags), + GFP_KERNEL); + if (!drbg->prev) + goto fini; + drbg->fips_primed = false; + } + return 0; fini: --- a/include/crypto/drbg.h +++ b/include/crypto/drbg.h @@ -131,6 +131,8 @@ struct drbg_state { bool seeded; /* DRBG fully seeded? */ bool pr; /* Prediction resistance enabled? */ + bool fips_primed; /* Continuous test primed? */ + unsigned char *prev; /* FIPS 140-2 continuous test value */ struct work_struct seed_work; /* asynchronous seeding support */ struct crypto_rng *jent; const struct drbg_state_ops *d_ops;