Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp2710567pxa; Tue, 25 Aug 2020 00:37:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwyy4PdrvVK1+8Y1fyaF+VRAIWIGVr345D6qQHgoUWgdV4T8X7plR/V+gmfTbSq2cUsYY/i X-Received: by 2002:a17:906:1483:: with SMTP id x3mr9678876ejc.318.1598341042352; Tue, 25 Aug 2020 00:37:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598341042; cv=none; d=google.com; s=arc-20160816; b=IpZKxAh6Sa79PQCcxY4cveMIqsNGvhqYdPyNrbasmxnBK9HqRbsIUkgNkcjWAVH+ed lZtI0fSpC8H/t1B/pFcpFcJyy1uHRY1IcEMVBhIzFqEUOL4oNZk8z1yrOp9mheFtovlL Dj7KajwKTgdtRxRfzmfyfQCa5YDNU7p9WAetd1h6KItILpx5GUyy0Tq90ahS3K1PE7vY xjZbtAorxrXx+TgqZdsBsHhnyJjPMHXPQWPjyrLV+GLHzSIx56Oi0GSGn2Imid0EDT8j o/EPXg0LWoIf7m5Q3CLhauDDHcXmA/44hvan+TwiD1gA0QBUYOaYMNcVGFTsbt3elMVp S4lw== 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=a1y2MHM7+JQ4vHLeArMWqCKw5iD/QQ88TlBxPLKMjW4=; b=tWqxdJaStDDwV53HrCMlTzM1xkJHsvqH8ubvE32YcpWRfiPHWhqJUFpfsshYYrtXBw qTfUzeoyZbDcgTIfaBYK3L8KOBtNLcGh6NnZQpNn+uXa3yURoUQWR04wTZ3kl4MCAPN/ gFN8oqDngFRUAk+q7WCGNNqK0bZyHsHHyf94LyVXrKyYAjrtZc0N7nEiwxv3GJvm3Gk5 rDTMnBUyYi+ghJskrw2R1IRoJLeQRHTyXKVbjJsmq7KGt9WI+Nw3mAqB0n+aO4l3g/uf SiSpP58hxmhwnAaBOOf/n2pHFNKcV361e0SahvM4JW+miCqnOBiQoM1UIIN87Gpo3CvT F1aA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@chronox.de header.s=strato-dkim-0002 header.b=UtLw0ayi; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id df1si8214532edb.78.2020.08.25.00.36.58; Tue, 25 Aug 2020 00:37:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=fail header.i=@chronox.de header.s=strato-dkim-0002 header.b=UtLw0ayi; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729531AbgHYHgh (ORCPT + 99 others); Tue, 25 Aug 2020 03:36:37 -0400 Received: from mo4-p04-ob.smtp.rzone.de ([85.215.255.123]:27342 "EHLO mo4-p04-ob.smtp.rzone.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729485AbgHYHgc (ORCPT ); Tue, 25 Aug 2020 03:36:32 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1598340987; 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=a1y2MHM7+JQ4vHLeArMWqCKw5iD/QQ88TlBxPLKMjW4=; b=UtLw0ayiTpzG5BjRMoVKKkOxsQHFoPsTA9Dr7m+a7LRBGBsqyjeXZKIHe5BmaKhCwQ DCy2j8XtY9hxho4TWmpX9q4MX2WN90n3R0M1i7V4n3JJXfD7cENqCoGHozNmGOtGLoY6 nZn3ZaAqSOD3jZj/mnvhD0B42qTw3Nfcn4/TEpGqTzLrsnYmT235cJ/sy6+D31EW6epu 9KvNmPshWo5sQ7PI4FAIDy0mkDCED8+F9v+T3HJ3i3feA2TSNzPy1C6T7Ec3KrerBN8j 3nIvH+nw8D08hXUgWUKNjbB7wssqj0sOiAFiCakL35mxyeSF9xvuXQx8lpbTeO7fcd0P 3CGg== X-RZG-AUTH: ":P2ERcEykfu11Y98lp/T7+hdri+uKZK8TKWEqNyiHySGSa9k9xmwdNnzHHXDaIvSXRbo=" X-RZG-CLASS-ID: mo00 Received: from positron.chronox.de by smtp.strato.de (RZmta 46.10.7 DYNA|AUTH) with ESMTPSA id 002e9aw7P7ZLZGT (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits)) (Client did not present a certificate); Tue, 25 Aug 2020 09:35:21 +0200 (CEST) From: Stephan =?ISO-8859-1?Q?M=FCller?= To: Arnd Bergmann Cc: Greg Kroah-Hartman , linux-crypto@vger.kernel.org, LKML , linux-api@vger.kernel.org, "Eric W. Biederman" , "Alexander E. Patrakov" , "Ahmed S. Darwish" , "Theodore Y. Ts'o" , Willy Tarreau , Matthew Garrett , Vito Caputo , Andreas Dilger , Jan Kara , Ray Strode , William Jon McCann , zhangjs , Andy Lutomirski , Florian Weimer , Lennart Poettering , Nicolai Stange , "Peter, Matthias" , Marcelo Henrique Cerri , Roman Drahtmueller , Neil Horman , Randy Dunlap , Julia Lawall , Dan Carpenter , Andy Lavr Subject: [PATCH v34 04/12] LRNG - add switchable DRNG support Date: Tue, 25 Aug 2020 09:24:11 +0200 Message-ID: <2098366.irdbgypaU6@positron.chronox.de> In-Reply-To: <11649613.O9o76ZdvQC@positron.chronox.de> References: <2544450.mvXUDI8C0e@positron.chronox.de> <5532247.MhkbZ0Pkbq@positron.chronox.de> <11649613.O9o76ZdvQC@positron.chronox.de> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="iso-8859-1" Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org The DRNG switch support allows replacing the DRNG mechanism of the LRNG. The switching support rests on the interface definition of include/linux/lrng.h. A new DRNG is implemented by filling in the interface defined in this header file. In addition to the DRNG, the extension also has to provide a hash implementation that is used to hash the entropy pool for random number extraction. Note: It is permissible to implement a DRNG whose operations may sleep. However, the hash function must not sleep. The switchable DRNG support allows replacing the DRNG at runtime. However, only one DRNG extension is allowed to be loaded at any given time. Before replacing it with another DRNG implementation, the possibly existing DRNG extension must be unloaded. The switchable DRNG extension activates the new DRNG during load time. It is expected, however, that such a DRNG switch would be done only once by an administrator to load the intended DRNG implementation. It is permissible to compile DRNG extensions either as kernel modules or statically. The initialization of the DRNG extension should be performed with a late_initcall to ensure the extension is available when user space starts but after all other initialization completed. The initialization is performed by registering the function call data structure with the lrng_set_drng_cb function. In order to unload the DRNG extension, lrng_set_drng_cb must be invoked with the NULL parameter. The DRNG extension should always provide a security strength that is at least as strong as LRNG_DRNG_SECURITY_STRENGTH_BITS. CC: "Eric W. Biederman" CC: "Alexander E. Patrakov" CC: "Ahmed S. Darwish" CC: "Theodore Y. Ts'o" CC: Willy Tarreau CC: Matthew Garrett CC: Vito Caputo CC: Andreas Dilger CC: Jan Kara CC: Ray Strode CC: William Jon McCann CC: zhangjs CC: Andy Lutomirski CC: Florian Weimer CC: Lennart Poettering CC: Nicolai Stange Reviewed-by: Marcelo Henrique Cerri Reviewed-by: Roman Drahtmueller Tested-by: Roman Drahtm=FCller Tested-by: Marcelo Henrique Cerri Tested-by: Neil Horman Signed-off-by: Stephan Mueller =2D-- drivers/char/lrng/Kconfig | 7 ++ drivers/char/lrng/Makefile | 1 + drivers/char/lrng/lrng_switch.c | 193 ++++++++++++++++++++++++++++++++ 3 files changed, 201 insertions(+) create mode 100644 drivers/char/lrng/lrng_switch.c diff --git a/drivers/char/lrng/Kconfig b/drivers/char/lrng/Kconfig index 059b21d9728c..d3138458fd8b 100644 =2D-- a/drivers/char/lrng/Kconfig +++ b/drivers/char/lrng/Kconfig @@ -65,4 +65,11 @@ config LRNG_POOL_SIZE default 7 if LRNG_POOL_SIZE_65536 default 8 if LRNG_POOL_SIZE_131072 =20 +menuconfig LRNG_DRNG_SWITCH + bool "Support DRNG runtime switching" + help + The Linux RNG per default uses a ChaCha20 DRNG that is + accessible via the external interfaces. With this configuration + option other DRNGs can be selected and loaded at runtime. + endif # LRNG diff --git a/drivers/char/lrng/Makefile b/drivers/char/lrng/Makefile index e69c176f0161..31cfe87c999e 100644 =2D-- a/drivers/char/lrng/Makefile +++ b/drivers/char/lrng/Makefile @@ -10,3 +10,4 @@ obj-y +=3D lrng_pool.o lrng_aux.o \ =20 obj-$(CONFIG_NUMA) +=3D lrng_numa.o obj-$(CONFIG_SYSCTL) +=3D lrng_proc.o +obj-$(CONFIG_LRNG_DRNG_SWITCH) +=3D lrng_switch.o diff --git a/drivers/char/lrng/lrng_switch.c b/drivers/char/lrng/lrng_switc= h.c new file mode 100644 index 000000000000..9b40671c6375 =2D-- /dev/null +++ b/drivers/char/lrng/lrng_switch.c @@ -0,0 +1,193 @@ +// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause +/* + * LRNG DRNG switching support + * + * Copyright (C) 2016 - 2020, Stephan Mueller + */ + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include + +#include "lrng_internal.h" + +static int lrng_drng_switch(struct lrng_drng *drng_store, + const struct lrng_crypto_cb *cb, int node) +{ + const struct lrng_crypto_cb *old_cb; + unsigned long flags =3D 0; + int ret; + u8 seed[LRNG_DRNG_SECURITY_STRENGTH_BYTES] __latent_entropy; + void *new_drng =3D cb->lrng_drng_alloc(LRNG_DRNG_SECURITY_STRENGTH_BYTES); + void *old_drng, *new_hash, *old_hash; + bool sl =3D false, reset_drng =3D !lrng_get_available(); + + if (IS_ERR(new_drng)) { + pr_warn("could not allocate new DRNG for NUMA node %d (%ld)\n", + node, PTR_ERR(new_drng)); + return PTR_ERR(new_drng); + } + + /* + * The seed potentially used as MAC key is undefined to add some + * variation. Yet, the security of the MAC does not rely on the key + * being secret. The key is only there to turn a MAC into a hash. + * The intention is to allow the specification of CMAC(AES) as "hash" + * to limit the dependency to AES when using the CTR DRBG. + */ + new_hash =3D cb->lrng_hash_alloc(seed, sizeof(seed)); + if (IS_ERR(new_hash)) { + pr_warn("could not allocate new LRNG pool hash (%ld)\n", + PTR_ERR(new_hash)); + cb->lrng_drng_dealloc(new_drng); + return PTR_ERR(new_hash); + } + + lrng_drng_lock(drng_store, &flags); + + /* + * Pull from existing DRNG to seed new DRNG regardless of seed status + * of old DRNG -- the entropy state for the DRNG is left unchanged which + * implies that als the new DRNG is reseeded when deemed necessary. This + * seeding of the new DRNG shall only ensure that the new DRNG has the + * same entropy as the old DRNG. + */ + ret =3D drng_store->crypto_cb->lrng_drng_generate_helper( + drng_store->drng, seed, sizeof(seed)); + lrng_drng_unlock(drng_store, &flags); + + if (ret < 0) { + reset_drng =3D true; + pr_warn("getting random data from DRNG failed for NUMA node %d (%d)\n", + node, ret); + } else { + /* seed new DRNG with data */ + ret =3D cb->lrng_drng_seed_helper(new_drng, seed, ret); + if (ret < 0) { + reset_drng =3D true; + pr_warn("seeding of new DRNG failed for NUMA node %d (%d)\n", + node, ret); + } else { + pr_debug("seeded new DRNG of NUMA node %d instance from old DRNG instan= ce\n", + node); + } + } + + mutex_lock(&drng_store->lock); + /* + * If we switch the DRNG from the initial ChaCha20 DRNG to something + * else, there is a lock transition from spin lock to mutex (see + * lrng_drng_is_atomic and how the lock is taken in lrng_drng_lock). + * Thus, we need to take both locks during the transition phase. + */ + if (lrng_drng_is_atomic(drng_store)) { + spin_lock_irqsave(&drng_store->spin_lock, flags); + sl =3D true; + } else { + __acquire(&drng_store->spin_lock); + } + + if (reset_drng) + lrng_drng_reset(drng_store); + + old_drng =3D drng_store->drng; + old_cb =3D drng_store->crypto_cb; + drng_store->drng =3D new_drng; + drng_store->crypto_cb =3D cb; + + old_hash =3D drng_store->hash; + drng_store->hash =3D new_hash; + pr_info("Entropy pool read-hash allocated for DRNG for NUMA node %d\n", + node); + + if (sl) + spin_unlock_irqrestore(&drng_store->spin_lock, flags); + else + __release(&drng_store->spin_lock); + mutex_unlock(&drng_store->lock); + + /* ChaCha20 serves as atomic instance left untouched. */ + if (old_drng !=3D &chacha20) { + old_cb->lrng_drng_dealloc(old_drng); + old_cb->lrng_hash_dealloc(old_hash); + } + + pr_info("DRNG of NUMA node %d switched\n", node); + + return 0; +} + +/* + * Switch the existing DRNG instances with new using the new crypto callba= cks. + * The caller must hold the lrng_crypto_cb_update lock. + */ +static int lrng_drngs_switch(const struct lrng_crypto_cb *cb) +{ + struct lrng_drng **lrng_drng =3D lrng_drng_instances(); + struct lrng_drng *lrng_drng_init =3D lrng_drng_init_instance(); + int ret =3D 0; + + /* Update DRNG */ + if (lrng_drng) { + u32 node; + + for_each_online_node(node) { + if (lrng_drng[node]) + ret =3D lrng_drng_switch(lrng_drng[node], cb, + node); + } + } else { + ret =3D lrng_drng_switch(lrng_drng_init, cb, 0); + } + + if (!ret) + lrng_set_available(); + + return 0; +} + +/** + * lrng_set_drng_cb - Register new cryptographic callback functions for DR= NG + * The registering implies that all old DRNG states are replaced with new + * DRNG states. + * + * @cb: Callback functions to be registered -- if NULL, use the default + * callbacks pointing to the ChaCha20 DRNG. + * + * Return: + * * 0 on success + * * < 0 on error + */ +int lrng_set_drng_cb(const struct lrng_crypto_cb *cb) +{ + struct lrng_drng *lrng_drng_init =3D lrng_drng_init_instance(); + int ret; + + if (!cb) + cb =3D &lrng_cc20_crypto_cb; + + mutex_lock(&lrng_crypto_cb_update); + + /* + * If a callback other than the default is set, allow it only to be + * set back to the default callback. This ensures that multiple + * different callbacks can be registered at the same time. If a + * callback different from the current callback and the default + * callback shall be set, the current callback must be deregistered + * (e.g. the kernel module providing it must be unloaded) and the new + * implementation can be registered. + */ + if ((cb !=3D &lrng_cc20_crypto_cb) && + (lrng_drng_init->crypto_cb !=3D &lrng_cc20_crypto_cb)) { + pr_warn("disallow setting new cipher callbacks, unload the old callbacks= first!\n"); + ret =3D -EINVAL; + goto out; + } + + ret =3D lrng_drngs_switch(cb); + +out: + mutex_unlock(&lrng_crypto_cb_update); + return ret; +} +EXPORT_SYMBOL(lrng_set_drng_cb); =2D-=20 2.26.2