Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp7376632yba; Thu, 2 May 2019 08:50:36 -0700 (PDT) X-Google-Smtp-Source: APXvYqw/NsHGP1gALkSvp59CpiWdojjIiiqKlzXUTpiS9nBYi8vB9XGsMjfpSt0Y/mvgCQ+6cJlO X-Received: by 2002:a17:902:aa5:: with SMTP id 34mr4501640plp.263.1556812236257; Thu, 02 May 2019 08:50:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1556812236; cv=none; d=google.com; s=arc-20160816; b=MdHivCTZ2wZQDq1pAolpF3neQdpj5mhL11dz+pH5I4bWN8nkQ9cP1RsQILKqJlUhsK spICJLk9nV71crxIufbx95E+Y2mJiiJHXlPCxojQ6YpJydXee+LPykKO0Q3fwpq6Adgx SSnlRPD2IhClVjcM+qwJepcd+iwc4xo0yP3ngQsZgteF5bu+2LpjmMb7Fuib9qty3XWq QiQxbO6d5O8PJSIjSuwPGL+bk0pLSkMKpcnf/ei4QKF32NvWSK98BShUxY0zFZT+VGBc rxJ8Ahit54cFaCkOHhcssEoF/XGtOSHCNHFCb5mFbT3Vd1z16nk/AOekj/z7Rcfn9NOR dXPQ== 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:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=LWj3It0eJBKXcu+QCUNuQLe3Q1OJuYx1z7uAwZwJerg=; b=zH56skFMSg8Y/A6oRWxrHMiB7YHqzHg97r7//Ho4tKLsk8+XUOPaelwKIlupki8zHe c+p/CRjLNspVcYWgVdBTzUtKztPhnjNfb0tlo6FfTMyMH51J60ZJrW/4mlKX6HQI1l0s lO+p5/qbmD/cx2yJRNJ569+J729cfBh9qbWbot/TpUZbvC7s+/pjuoqugEYOlRPvbvBh UBWYZjH+DPDfdvj/c5xsVDeRpPmQkijAwWts0oJ3j7x8+Wj/Zl3ttREUZuDWwukFFk9W 44TRF/lWldHRYkYCmPg8GbcVf0ASCy8ffQh8TSq88NjhdEd+4yG7L+xGEj/y5PLFAKsQ ufXw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@chronox.de header.s=strato-dkim-0002 header.b=e1Q8ZWAI; spf=pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f7si24668372plo.204.2019.05.02.08.50.16; Thu, 02 May 2019 08:50:36 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@chronox.de header.s=strato-dkim-0002 header.b=e1Q8ZWAI; spf=pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726302AbfEBPtk (ORCPT + 99 others); Thu, 2 May 2019 11:49:40 -0400 Received: from mo4-p00-ob.smtp.rzone.de ([81.169.146.218]:33118 "EHLO mo4-p00-ob.smtp.rzone.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726525AbfEBPtj (ORCPT ); Thu, 2 May 2019 11:49:39 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1556812176; s=strato-dkim-0002; d=chronox.de; h=References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: X-RZG-CLASS-ID:X-RZG-AUTH:From:Subject:Sender; bh=LWj3It0eJBKXcu+QCUNuQLe3Q1OJuYx1z7uAwZwJerg=; b=e1Q8ZWAIsbfNoN8KysjO/JUrVqAbti6m6XzvToyvSbKfv29YKE+uAypHaRR24BLV1k p70AP6dSYsJgAu88w1NMe77UP2tqRrnUdn+Q/VoCwUi2FAvmvU5M/TqQRwCI05PNJJ3c vqQBjX6zibUQHxCi01RDVyqV/8DyKwoZlg2vHsignvglam+Puo2iQ50GQ67x9B4gFu8P 1K9/MrZ+ZGmQBwi4Q9LMTLW/S6hnR7PblKOTbYuykMn9lXs/onjbIKjB6tqnS7w5A7hU 56hVceXPaZQQexlb0HyHzbxqIX4rEAYxFzUdPtlRMh6JoB629Ela2nR/692Tkecj/Wlk ul4g== X-RZG-AUTH: ":P2ERcEykfu11Y98lp/T7+hdri+uKZK8TKWEqNyiHySGSa9k9xmwdNnzGHXPaJfSd/cc=" X-RZG-CLASS-ID: mo00 Received: from positron.chronox.de by smtp.strato.de (RZmta 44.18 DYNA|AUTH) with ESMTPSA id R0373fv42FhMjjq (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (curve secp521r1 with 521 ECDH bits, eq. 15360 bits RSA)) (Client did not present a certificate); Thu, 2 May 2019 17:43:22 +0200 (CEST) From: Stephan =?ISO-8859-1?Q?M=FCller?= To: Herbert Xu Cc: linux-crypto@vger.kernel.org Subject: [PATCH v2] crypto: DRBG - add FIPS 140-2 CTRNG for noise source Date: Thu, 02 May 2019 17:43:22 +0200 Message-ID: <10683686.8DmOGYbKhJ@positron.chronox.de> In-Reply-To: <20190502124811.l4yozv4llqtdvozx@gondor.apana.org.au> References: <1852500.fyBc0DU23F@positron.chronox.de> <20190502124811.l4yozv4llqtdvozx@gondor.apana.org.au> MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org Changes v2: * use IS_ENABLED macro * small fix in comment ---8<--- 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. 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 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 --- crypto/drbg.c | 84 +++++++++++++++++++++++++++++++++++++++++-- include/crypto/drbg.h | 4 +++ 2 files changed, 85 insertions(+), 3 deletions(-) diff --git a/crypto/drbg.c b/crypto/drbg.c index 2a5b16bb000c..d6261262fda4 100644 --- a/crypto/drbg.c +++ b/crypto/drbg.c @@ -219,6 +219,57 @@ static inline unsigned short drbg_sec_strength(drbg_flag_t flags) } } +/* + * 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) +{ +#if IS_ENABLED(CONFIG_CRYPTO_FIPS) + unsigned short entropylen = drbg_sec_strength(drbg->core->flags); + int ret = 0; + + /* skip test if we test the overall system */ + if (list_empty(&drbg->test_data.list)) + return true; + /* only perform test in FIPS mode */ + if (!fips_enabled) + return true; + + 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 (ret != 0) ? 0 : -EFAULT; +#else + return true; +#endif /* CONFIG_CRYPTO_FIPS */ +} + /* * Convert an integer into a byte representation of this integer. * The byte representation is big-endian @@ -1006,16 +1057,23 @@ static void drbg_async_seed(struct work_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); + do { + get_random_bytes(entropy, entropylen); + ret = drbg_fips_continuous_test(drbg, entropy); + if (ret && ret != -EAGAIN) + goto unlock; + } while (ret); + /* If nonblocking pool is initialized, deactivate Jitter RNG */ crypto_free_rng(drbg->jent); drbg->jent = NULL; @@ -1030,6 +1088,7 @@ static void drbg_async_seed(struct work_struct *work) if (drbg->seeded) drbg->reseed_threshold = drbg_max_requests(drbg); +unlock: mutex_unlock(&drbg->drbg_mutex); memzero_explicit(entropy, entropylen); @@ -1081,7 +1140,12 @@ static int drbg_seed(struct drbg_state *drbg, struct drbg_string *pers, BUG_ON((entropylen * 2) > sizeof(entropy)); /* Get seed from in-kernel /dev/urandom */ - get_random_bytes(entropy, entropylen); + do { + get_random_bytes(entropy, entropylen); + ret = drbg_fips_continuous_test(drbg, entropy); + if (ret && ret != -EAGAIN) + goto out; + } while (ret); if (!drbg->jent) { drbg_string_fill(&data1, entropy, entropylen); @@ -1094,7 +1158,7 @@ static int drbg_seed(struct drbg_state *drbg, struct drbg_string *pers, entropylen); if (ret) { pr_devel("DRBG: jent failed with %d\n", ret); - return ret; + goto out; } drbg_string_fill(&data1, entropy, entropylen * 2); @@ -1121,6 +1185,7 @@ static int drbg_seed(struct drbg_state *drbg, struct drbg_string *pers, ret = __drbg_seed(drbg, &seedlist, reseed); +out: memzero_explicit(entropy, entropylen * 2); return ret; @@ -1142,6 +1207,11 @@ static inline void drbg_dealloc_state(struct drbg_state *drbg) 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; +#endif } /* @@ -1211,6 +1281,14 @@ static inline int drbg_alloc_state(struct drbg_state *drbg) 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; +#endif + return 0; fini: diff --git a/include/crypto/drbg.h b/include/crypto/drbg.h index 3fb581bf3b87..939051480c83 100644 --- a/include/crypto/drbg.h +++ b/include/crypto/drbg.h @@ -129,6 +129,10 @@ struct drbg_state { bool seeded; /* DRBG fully seeded? */ bool pr; /* Prediction resistance enabled? */ +#if IS_ENABLED(CONFIG_CRYPTO_FIPS) + bool fips_primed; /* Continuous test primed? */ + unsigned char *prev; /* FIPS 140-2 continuous test value */ +#endif struct work_struct seed_work; /* asynchronous seeding support */ struct crypto_rng *jent; const struct drbg_state_ops *d_ops; -- 2.21.0