linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 0/2] hwrng: Add Arm SMCCC TRNG based driver
@ 2021-07-26 17:56 Andre Przywara
  2021-07-26 17:56 ` [PATCH v3 1/2] firmware: smccc: Register smccc_trng platform device Andre Przywara
  2021-07-26 17:56 ` [PATCH v3 2/2] hwrng: Add Arm SMCCC TRNG based driver Andre Przywara
  0 siblings, 2 replies; 6+ messages in thread
From: Andre Przywara @ 2021-07-26 17:56 UTC (permalink / raw)
  To: Matt Mackall, Herbert Xu, Mark Rutland, Lorenzo Pieralisi, Sudeep Holla
  Cc: linux-crypto, linux-arm-kernel, linux-kernel,
	Benjamin Herrenschmidt, Ard Biesheuvel, Mark Brown, Will Deacon,
	Ali Saidi, Jon Nettleton

The "Arm True Random Number Generator Firmware Interface"[1] provides
an SMCCC based interface to a true hardware random number generator.
So far we are using that in arch_get_random_seed(), but it might be
useful to expose the entropy through the /dev/hwrng device as well. This
allows to assess the quality of the implementation, by using "rngtest"
from the rng-tools package, for example.

Patch 1 creates a platform device, triggered by the previous discovery
of the SMCCC TRNG service.
Patch 2 implements a hw_random platform driver, which is instantiated
through this said platform device.

The driver can be loaded as module, or built into the kernel.

[1] https://developer.arm.com/documentation/den0098/latest/

Changelog v2 ... v3:
- split platform device and driver

Changelog v1 ... v2:
- fix building as a module
- de-register device upon exit
- mention module name in Kconfig

Andre Przywara (2):
  firmware: smccc: Register smccc_trng platform device
  hwrng: Add Arm SMCCC TRNG based driver

 drivers/char/hw_random/Kconfig          |  14 +++
 drivers/char/hw_random/Makefile         |   1 +
 drivers/char/hw_random/arm_smccc_trng.c | 134 ++++++++++++++++++++++++
 drivers/firmware/smccc/smccc.c          |  17 +++
 4 files changed, 166 insertions(+)
 create mode 100644 drivers/char/hw_random/arm_smccc_trng.c

-- 
2.17.6


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v3 1/2] firmware: smccc: Register smccc_trng platform device
  2021-07-26 17:56 [PATCH v3 0/2] hwrng: Add Arm SMCCC TRNG based driver Andre Przywara
@ 2021-07-26 17:56 ` Andre Przywara
  2021-07-26 17:56 ` [PATCH v3 2/2] hwrng: Add Arm SMCCC TRNG based driver Andre Przywara
  1 sibling, 0 replies; 6+ messages in thread
From: Andre Przywara @ 2021-07-26 17:56 UTC (permalink / raw)
  To: Matt Mackall, Herbert Xu, Mark Rutland, Lorenzo Pieralisi, Sudeep Holla
  Cc: linux-crypto, linux-arm-kernel, linux-kernel,
	Benjamin Herrenschmidt, Ard Biesheuvel, Mark Brown, Will Deacon,
	Ali Saidi, Jon Nettleton

At the moment we probe for the Random Number Generator SMCCC service,
and use that in the core code (arch_get_random). However the hardware
entropy can also be useful to access from userland, and be it to assess
its quality.

Register a platform device when the SMCCC TRNG service is detected, to
allow a hw_random driver to hook onto this.

The function registering the device is deliberately made in a way which
allows expansion, so other services that could be exposed via a platform
device (or some other interface), can be added here easily.

Signed-off-by: Andre Przywara <andre.przywara@arm.com>
---
 drivers/firmware/smccc/smccc.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/drivers/firmware/smccc/smccc.c b/drivers/firmware/smccc/smccc.c
index 9f937b125ab0..60ccf3e90d7d 100644
--- a/drivers/firmware/smccc/smccc.c
+++ b/drivers/firmware/smccc/smccc.c
@@ -9,6 +9,7 @@
 #include <linux/init.h>
 #include <linux/arm-smccc.h>
 #include <linux/kernel.h>
+#include <linux/platform_device.h>
 #include <asm/archrandom.h>
 
 static u32 smccc_version = ARM_SMCCC_VERSION_1_0;
@@ -42,3 +43,19 @@ u32 arm_smccc_get_version(void)
 	return smccc_version;
 }
 EXPORT_SYMBOL_GPL(arm_smccc_get_version);
+
+static int __init smccc_devices_init(void)
+{
+	struct platform_device *pdev;
+
+	if (smccc_trng_available) {
+		pdev = platform_device_register_simple("smccc_trng", -1,
+						       NULL, 0);
+		if (IS_ERR(pdev))
+			pr_err("smccc_trng: could not register device: %ld\n",
+			       PTR_ERR(pdev));
+	}
+
+	return 0;
+}
+device_initcall(smccc_devices_init);
-- 
2.17.6


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v3 2/2] hwrng: Add Arm SMCCC TRNG based driver
  2021-07-26 17:56 [PATCH v3 0/2] hwrng: Add Arm SMCCC TRNG based driver Andre Przywara
  2021-07-26 17:56 ` [PATCH v3 1/2] firmware: smccc: Register smccc_trng platform device Andre Przywara
@ 2021-07-26 17:56 ` Andre Przywara
  2021-07-26 22:37   ` Mark Brown
  1 sibling, 1 reply; 6+ messages in thread
From: Andre Przywara @ 2021-07-26 17:56 UTC (permalink / raw)
  To: Matt Mackall, Herbert Xu, Mark Rutland, Lorenzo Pieralisi, Sudeep Holla
  Cc: linux-crypto, linux-arm-kernel, linux-kernel,
	Benjamin Herrenschmidt, Ard Biesheuvel, Mark Brown, Will Deacon,
	Ali Saidi, Jon Nettleton

The "Arm True Random Number Generator Firmware Interface"[1] provides
an SMCCC based interface to a true hardware random number generator.
So far we are using that in arch_get_random_seed(), but it might be
useful to expose the entropy through the /dev/hwrng device as well. This
allows to assess the quality of the implementation, by using "rngtest"
from the rng-tools package, for example.

Add a simple platform driver implementing the hw_random interface.
The corresponding platform device is created by the SMCCC core code,
we just match it here by name and provide a module alias.

Since the firmware takes care about serialisation, this can happily
coexist with the arch_get_random_seed() bits.

Signed-off-by: Andre Przywara <andre.przywara@arm.com>

[1] https://developer.arm.com/documentation/den0098/latest/
---
 drivers/char/hw_random/Kconfig          |  14 +++
 drivers/char/hw_random/Makefile         |   1 +
 drivers/char/hw_random/arm_smccc_trng.c | 134 ++++++++++++++++++++++++
 3 files changed, 149 insertions(+)
 create mode 100644 drivers/char/hw_random/arm_smccc_trng.c

diff --git a/drivers/char/hw_random/Kconfig b/drivers/char/hw_random/Kconfig
index 3f166c8a4099..239eca4d6805 100644
--- a/drivers/char/hw_random/Kconfig
+++ b/drivers/char/hw_random/Kconfig
@@ -524,6 +524,20 @@ config HW_RANDOM_XIPHERA
 	  To compile this driver as a module, choose M here: the
 	  module will be called xiphera-trng.
 
+config HW_RANDOM_ARM_SMCCC_TRNG
+	tristate "Arm SMCCC TRNG firmware interface support"
+	depends on HAVE_ARM_SMCCC_DISCOVERY
+	default HW_RANDOM
+	help
+	  Say 'Y' to enable the True Random Number Generator driver using
+	  the Arm SMCCC TRNG firmware interface. This reads entropy from
+	  higher exception levels (firmware, hypervisor). Uses SMCCC for
+	  communicating with the firmware:
+	  https://developer.arm.com/documentation/den0098/latest/
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called arm_smccc_trng.
+
 endif # HW_RANDOM
 
 config UML_RANDOM
diff --git a/drivers/char/hw_random/Makefile b/drivers/char/hw_random/Makefile
index 8933fada74f2..a5a1c765a394 100644
--- a/drivers/char/hw_random/Makefile
+++ b/drivers/char/hw_random/Makefile
@@ -45,3 +45,4 @@ obj-$(CONFIG_HW_RANDOM_OPTEE) += optee-rng.o
 obj-$(CONFIG_HW_RANDOM_NPCM) += npcm-rng.o
 obj-$(CONFIG_HW_RANDOM_CCTRNG) += cctrng.o
 obj-$(CONFIG_HW_RANDOM_XIPHERA) += xiphera-trng.o
+obj-$(CONFIG_HW_RANDOM_ARM_SMCCC_TRNG) += arm_smccc_trng.o
diff --git a/drivers/char/hw_random/arm_smccc_trng.c b/drivers/char/hw_random/arm_smccc_trng.c
new file mode 100644
index 000000000000..a04f9f9db1fe
--- /dev/null
+++ b/drivers/char/hw_random/arm_smccc_trng.c
@@ -0,0 +1,134 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Randomness driver for the ARM SMCCC TRNG Firmware Interface
+ * https://developer.arm.com/documentation/den0098/latest/
+ *
+ *  Copyright (C) 2020 Arm Ltd.
+ *
+ * The ARM TRNG firmware interface specifies a protocol to read entropy
+ * from a higher exception level, to abstract from any machine specific
+ * implemenations and allow easier use in hypervisors.
+ *
+ * The firmware interface is realised using the SMCCC specification.
+ */
+
+#include <linux/bits.h>
+#include <linux/device.h>
+#include <linux/hw_random.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/arm-smccc.h>
+
+#ifdef CONFIG_ARM64
+#define ARM_SMCCC_TRNG_RND	ARM_SMCCC_TRNG_RND64
+#define MAX_BITS_PER_CALL	(3 * 64UL)
+#else
+#define ARM_SMCCC_TRNG_RND	ARM_SMCCC_TRNG_RND32
+#define MAX_BITS_PER_CALL	(3 * 32UL)
+#endif
+
+/* We don't want to allow the firmware to stall us forever. */
+#define SMCCC_TRNG_MAX_TRIES	20
+
+#define SMCCC_RET_TRNG_INVALID_PARAMETER	-2
+#define SMCCC_RET_TRNG_NO_ENTROPY		-3
+
+static int smccc_trng_init(struct hwrng *rng)
+{
+	return 0;
+}
+
+static int copy_from_registers(char *buf, struct arm_smccc_res *res,
+			       size_t bytes)
+{
+	unsigned int chunk, copied;
+
+	if (bytes == 0)
+		return 0;
+
+	chunk = min(bytes, sizeof(long));
+	memcpy(buf, &res->a3, chunk);
+	copied = chunk;
+	if (copied >= bytes)
+		return copied;
+
+	chunk = min((bytes - copied), sizeof(long));
+	memcpy(&buf[copied], &res->a2, chunk);
+	copied += chunk;
+	if (copied >= bytes)
+		return copied;
+
+	chunk = min((bytes - copied), sizeof(long));
+	memcpy(&buf[copied], &res->a1, chunk);
+
+	return copied + chunk;
+}
+
+static int smccc_trng_read(struct hwrng *rng, void *data, size_t max, bool wait)
+{
+	struct arm_smccc_res res;
+	u8 *buf = data;
+	unsigned int copied = 0;
+	int tries = 0;
+
+	while (copied < max) {
+		size_t bits = min_t(size_t, (max - copied) * BITS_PER_BYTE,
+				  MAX_BITS_PER_CALL);
+
+		arm_smccc_1_1_invoke(ARM_SMCCC_TRNG_RND, bits, &res);
+		if ((int)res.a0 < 0)
+			return (int)res.a0;
+
+		switch ((int)res.a0) {
+		case SMCCC_RET_SUCCESS:
+			copied += copy_from_registers(buf + copied, &res,
+						      bits / BITS_PER_BYTE);
+			tries = 0;
+			break;
+		case SMCCC_RET_TRNG_NO_ENTROPY:
+			if (!wait)
+				return copied;
+			tries++;
+			if (tries >= SMCCC_TRNG_MAX_TRIES)
+				return copied;
+			cond_resched();
+			break;
+		}
+	}
+
+	return copied;
+}
+
+static int smccc_trng_probe(struct platform_device *pdev)
+{
+	struct hwrng *trng;
+	int ret;
+
+	trng = devm_kzalloc(&pdev->dev, sizeof(*trng), GFP_KERNEL);
+	if (!trng)
+		return -ENOMEM;
+
+	trng->name = "smccc_trng";
+	trng->init = smccc_trng_init;
+	trng->read = smccc_trng_read;
+
+	platform_set_drvdata(pdev, trng);
+	ret = devm_hwrng_register(&pdev->dev, trng);
+	if (!ret)
+		dev_info(&pdev->dev,
+			 "ARM SMCCC TRNG firmware random number generator\n");
+
+	return ret;
+}
+
+static struct platform_driver smccc_trng_driver = {
+	.driver = {
+		.name		= "smccc_trng",
+	},
+	.probe		= smccc_trng_probe,
+};
+module_platform_driver(smccc_trng_driver);
+
+MODULE_ALIAS("platform:smccc_trng");
+MODULE_AUTHOR("Andre Przywara");
+MODULE_LICENSE("GPL");
-- 
2.17.6


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v3 2/2] hwrng: Add Arm SMCCC TRNG based driver
  2021-07-26 17:56 ` [PATCH v3 2/2] hwrng: Add Arm SMCCC TRNG based driver Andre Przywara
@ 2021-07-26 22:37   ` Mark Brown
  2021-07-27  0:30     ` Andre Przywara
  0 siblings, 1 reply; 6+ messages in thread
From: Mark Brown @ 2021-07-26 22:37 UTC (permalink / raw)
  To: Andre Przywara
  Cc: Matt Mackall, Herbert Xu, Mark Rutland, Lorenzo Pieralisi,
	Sudeep Holla, linux-crypto, linux-arm-kernel, linux-kernel,
	Benjamin Herrenschmidt, Ard Biesheuvel, Will Deacon, Ali Saidi,
	Jon Nettleton

[-- Attachment #1: Type: text/plain, Size: 608 bytes --]

On Mon, Jul 26, 2021 at 06:56:10PM +0100, Andre Przywara wrote:

> +static int smccc_trng_init(struct hwrng *rng)
> +{
> +	return 0;
> +}

If this can be empty (looking at the core it seems like it can) then
best just remove it.

> +	platform_set_drvdata(pdev, trng);
> +	ret = devm_hwrng_register(&pdev->dev, trng);
> +	if (!ret)
> +		dev_info(&pdev->dev,
> +			 "ARM SMCCC TRNG firmware random number generator\n");

Is the log message needed given that we're not announcing any version
information here or anything?  A brief sampling of other drivers
suggests it's not a standard thing for the subsystem.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v3 2/2] hwrng: Add Arm SMCCC TRNG based driver
  2021-07-26 22:37   ` Mark Brown
@ 2021-07-27  0:30     ` Andre Przywara
  2021-07-27  0:59       ` Mark Brown
  0 siblings, 1 reply; 6+ messages in thread
From: Andre Przywara @ 2021-07-27  0:30 UTC (permalink / raw)
  To: Mark Brown
  Cc: Matt Mackall, Herbert Xu, Mark Rutland, Lorenzo Pieralisi,
	Sudeep Holla, linux-crypto, linux-arm-kernel, linux-kernel,
	Benjamin Herrenschmidt, Ard Biesheuvel, Will Deacon, Ali Saidi,
	Jon Nettleton

On Mon, 26 Jul 2021 23:37:38 +0100
Mark Brown <broonie@kernel.org> wrote:

> On Mon, Jul 26, 2021 at 06:56:10PM +0100, Andre Przywara wrote:
> 
> > +static int smccc_trng_init(struct hwrng *rng)
> > +{
> > +	return 0;
> > +}  
> 
> If this can be empty (looking at the core it seems like it can) then
> best just remove it.

Ah, you are right! Actually this is explicitly mentioned in the
struct hwrng comments. Thanks for the heads up.

> 
> > +	platform_set_drvdata(pdev, trng);
> > +	ret = devm_hwrng_register(&pdev->dev, trng);
> > +	if (!ret)
> > +		dev_info(&pdev->dev,
> > +			 "ARM SMCCC TRNG firmware random number generator\n");  
> 
> Is the log message needed given that we're not announcing any version
> information here or anything?  A brief sampling of other drivers
> suggests it's not a standard thing for the subsystem.

Yeah, that was indeed more a leftover of the version print. I
thought about querying the version again explicitly, but this would
have brought back the SMCCC calls that I could so nicely delete. Plus,
the hwrng driver is just a (secondary) user of this interface, I think
announcing the version should be done in smccc.c. Which is probably
beyond the scope of this patch.

Now thinking about this, there would probably be some value in making
the TRNG UUID somehow available, as this can be used to identify flawed
implementations (general problems in the hardware or backend bugs). But
this should be some query-able interface, rather than some line in
dmesg. Any ideas? Might be beyond the scope of this series, though...

Cheers,
Andre

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v3 2/2] hwrng: Add Arm SMCCC TRNG based driver
  2021-07-27  0:30     ` Andre Przywara
@ 2021-07-27  0:59       ` Mark Brown
  0 siblings, 0 replies; 6+ messages in thread
From: Mark Brown @ 2021-07-27  0:59 UTC (permalink / raw)
  To: Andre Przywara
  Cc: Matt Mackall, Herbert Xu, Mark Rutland, Lorenzo Pieralisi,
	Sudeep Holla, linux-crypto, linux-arm-kernel, linux-kernel,
	Benjamin Herrenschmidt, Ard Biesheuvel, Will Deacon, Ali Saidi,
	Jon Nettleton

[-- Attachment #1: Type: text/plain, Size: 797 bytes --]

On Tue, Jul 27, 2021 at 01:30:04AM +0100, Andre Przywara wrote:

> Now thinking about this, there would probably be some value in making
> the TRNG UUID somehow available, as this can be used to identify flawed
> implementations (general problems in the hardware or backend bugs). But
> this should be some query-able interface, rather than some line in
> dmesg. Any ideas? Might be beyond the scope of this series, though...

I guess you could append it to the name (eg, "SMCCC TRNG ${UUID}")
though it'd be a bit of an eyesore if anyone displays that in UIs much?

A separate version string queryable in parallel with name would be more
work but possibly a bit more sensible, some other hardware entropy
sources will have firmware version numbers or similar they could
usefully report I expect.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-07-27  0:59 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-26 17:56 [PATCH v3 0/2] hwrng: Add Arm SMCCC TRNG based driver Andre Przywara
2021-07-26 17:56 ` [PATCH v3 1/2] firmware: smccc: Register smccc_trng platform device Andre Przywara
2021-07-26 17:56 ` [PATCH v3 2/2] hwrng: Add Arm SMCCC TRNG based driver Andre Przywara
2021-07-26 22:37   ` Mark Brown
2021-07-27  0:30     ` Andre Przywara
2021-07-27  0:59       ` Mark Brown

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).