Skip to contentSkip to conversation

hwrng: Add Arm SMCCC TRNG based driver

PATCH In review Andre Przywara 4 participants 6 messages Jul 31, 2021 – Aug 6, 2021

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v4 cover hwrng: Add Arm SMCCC TRNG based driver

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 v3 ... v4:
- drop pointless driver loading message
- drop unneeded init() routine

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 | 123 ++++++++++++++++++++++++
 drivers/firmware/smccc/smccc.c          |  17 ++++
 4 files changed, 155 insertions(+)
 create mode 100644 drivers/char/hw_random/arm_smccc_trng.c
Signature · 1 line
2.17.6
#1 lore raw

PATCH v4 1/2 firmware: smccc: Register smccc_trng platform device

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.

---
 drivers/firmware/smccc/smccc.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
drivers/firmware/smccc/smccc.c +17 -0
index 9f937b125ab0..60ccf3e90d7d 100644
@@ -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);
Signature · 1 line
2.17.6
#2 lore raw

PATCH v4 2/2 hwrng: Add Arm SMCCC TRNG based driver

In reply to Andre Przywara

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.

[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 | 123 ++++++++++++++++++++++++
 3 files changed, 138 insertions(+)
 create mode 100644 drivers/char/hw_random/arm_smccc_trng.c
drivers/char/hw_random/Kconfig +14 -0
index 3f166c8a4099..239eca4d6805 100644
@@ -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
drivers/char/hw_random/Makefile +1 -0
index 8933fada74f2..a5a1c765a394 100644
@@ -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
drivers/char/hw_random/arm_smccc_trng.c new file +123 -0

123 lines not on this page · raw message

Signature · 1 line
2.17.6
Reviewed-by on the v4 series
#3 lore raw
In reply to Andre Przywara
Quoted 24 lines · Andre Przywara

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 v3 ... v4:
- drop pointless driver loading message
- drop unneeded init() routine

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

Quoted 9 lines

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

Quoted 10 lines
 drivers/char/hw_random/Kconfig          |  14 +++
 drivers/char/hw_random/Makefile         |   1 +
 drivers/char/hw_random/arm_smccc_trng.c | 123 ++++++++++++++++++++++++
 drivers/firmware/smccc/smccc.c          |  17 ++++
 4 files changed, 155 insertions(+)
 create mode 100644 drivers/char/hw_random/arm_smccc_trng.c

--
2.17.6

Reviewed-by on the v4 series
#4 lore raw
In reply to Andre Przywara
Quoted 6 lines · Andre Przywara

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.

maintainer
#5 lore raw
In reply to Andre Przywara
Quoted 32 lines · Andre Przywara

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 v3 ... v4:
- drop pointless driver loading message
- drop unneeded init() routine

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

Quoted 6 lines
 drivers/char/hw_random/Kconfig          |  14 +++
 drivers/char/hw_random/Makefile         |   1 +
 drivers/char/hw_random/arm_smccc_trng.c | 123 ++++++++++++++++++++++++
 drivers/firmware/smccc/smccc.c          |  17 ++++
 4 files changed, 155 insertions(+)
 create mode 100644 drivers/char/hw_random/arm_smccc_trng.c

All applied. Thanks.

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt