Skip to contentSkip to conversation

ARM: implement support for SMCCC TRNG entropy source

PATCH In review Ard Biesheuvel 2 participants 4 messages Oct 6, 2020 – Oct 7, 2020 Arm (32-bit)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ARM: implement support for SMCCC TRNG entropy source

Implement arch_get_random_seed_*() for ARM based on the firmware or hypervisor provided entropy source described in ARM DEN0098.

This will make the kernel's random number generator consume entropy provided by this interface, at early boot, and periodically at runtime when reseeding.

Cc Andre Przywara <[email protected]>
Cc Linus Walleij <[email protected]>
Cc Russell King <[email protected]>

---
Note that this depends on changes to include/linux/arm-smccc.h that are still under review.

 arch/arm/Kconfig                  |  4 ++
 arch/arm/include/asm/archrandom.h | 55 ++++++++++++++++++++
 arch/arm/kernel/Makefile          |  1 +
 arch/arm/kernel/random.c          | 28 ++++++++++
 arch/arm/kernel/setup.c           |  3 ++
 5 files changed, 91 insertions(+)
arch/arm/Kconfig +4 -0
index e00d94b16658..dd67500ca8c9 100644
@@ -1682,6 +1682,10 @@ config STACKPROTECTOR_PER_TASK
 	  Enable this option to switch to a different method that uses a
 	  different canary value for each task.
 
+config ARCH_RANDOM
+	def_bool y
+	depends on HAVE_ARM_SMCCC
+
 endmenu
 
 menu "Boot options"
arch/arm/include/asm/archrandom.h new file +55 -0

55 lines not on this page · raw message

arch/arm/kernel/Makefile +1 -0
index 89e5d864e923..11b13f0e7627 100644
@@ -105,5 +105,6 @@ obj-$(CONFIG_SMP)		+= psci_smp.o
 endif
 
 obj-$(CONFIG_HAVE_ARM_SMCCC)	+= smccc-call.o
+obj-$(CONFIG_ARCH_RANDOM)	+= random.o
 
 extra-y := $(head-y) vmlinux.lds
arch/arm/kernel/random.c new file +28 -0
new file mode 100644
index 000000000000..6f1e6eb57ae6
@@ -0,0 +1,28 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#include <linux/arm-smccc.h>
+#include <linux/kernel.h>
+#include <asm/archrandom.h>
+
+#define ARM_SMCCC_TRNG_MIN_VERSION	0x10000UL
+
+bool __ro_after_init have_arch_random;
+
+void __init init_arch_random(void)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(ARM_SMCCC_TRNG_VERSION, &res);
+
+	if ((s32)res.a0 >= ARM_SMCCC_TRNG_MIN_VERSION) {
+		/* double check that the 32-bit flavor is available */
+		arm_smccc_1_1_invoke(ARM_SMCCC_TRNG_FEATURES,
+				     ARM_SMCCC_TRNG_RND32,
+				     &res);
+		if ((s32)res.a0 >= 0)
+			have_arch_random = true;
+	}
+
+	pr_info("SMCCC TRNG entropy source %sdetected\n",
+		have_arch_random ? "" : "not ");
+}
arch/arm/kernel/setup.c +3 -0
index bdba280a569d..c08574cad748 100644
@@ -31,6 +31,7 @@
 #include <linux/psci.h>
 
 #include <asm/unified.h>
+#include <asm/archrandom.h>
 #include <asm/cp15.h>
 #include <asm/cpu.h>
 #include <asm/cputype.h>
@@ -1148,6 +1149,8 @@ void __init setup_arch(char **cmdline_p)
 
 	arm_dt_init_cpu_maps();
 	psci_dt_init();
+	init_arch_random();
+
 #ifdef CONFIG_SMP
 	if (is_smp()) {
 		if (!mdesc->smp_init || !mdesc->smp_init()) {
Signature · 7 lines
2.17.1


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Ard Biesheuvel

Implement arch_get_random_seed_*() for ARM based on the firmware or hypervisor provided entropy source described in ARM DEN0098.

This will make the kernel's random number generator consume entropy provided by this interface, at early boot, and periodically at runtime when reseeding.

Cc Andre Przywara <[email protected]>
Cc Linus Walleij <[email protected]>
Cc Russell King <[email protected]>

This is a good place to start for sure.

Ideally (if I can dream) I would like to see an interface similar to "earlycon", where some drivers have special callbacks that can be used really early to get some debug prints, something like that but named "earlyrandom", so that any drivers under drivers/char/hw_random.c
could optionally implement these early callbacks and provide random seed right out of some register reads or so before the kernel driver core is up. I do not know how
realistic that is, but for e.g. the nomadik-rng.c it is a simple register read from a physical address which should be simple to achieve.

Yours,
Linus Walleij

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#2 lore raw
Quoted 10 lines · Linus Walleij
Quoted 7 lines · Ard Biesheuvel

Implement arch_get_random_seed_*() for ARM based on the firmware or hypervisor provided entropy source described in ARM DEN0098.

This will make the kernel's random number generator consume entropy provided by this interface, at early boot, and periodically at runtime when reseeding.

Quoted 8 lines
Quoted 4 lines
Cc Andre Przywara <[email protected]>
Cc Linus Walleij <[email protected]>
Cc Russell King <[email protected]>

This is a good place to start for sure.

Thanks,

Quoted 12 lines

Ideally (if I can dream) I would like to see an interface similar to "earlycon", where some drivers have special callbacks that can be used really early to get some debug prints, something like that but named "earlyrandom", so that any drivers under drivers/char/hw_random.c
could optionally implement these early callbacks and provide random seed right out of some register reads or so before the kernel driver core is up. I do not know how
realistic that is, but for e.g. the nomadik-rng.c it is a simple register read from a physical address which should be simple to achieve.

This only works for peripherals that are always clocked, powered, etc, which you cannot rely on in the general case. This means you would need some cooperation from the bootloader/firmware to signal that the RNG is accessible early, and at which address. It might just as well pass you a random seed instead ...

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#3 lore raw
Quoted 14 lines · Ard Biesheuvel

[Me]

Quoted 12 lines

Ideally (if I can dream) I would like to see an interface similar to "earlycon", where some drivers have special callbacks that can be used really early to get some debug prints, something like that but named "earlyrandom", so that any drivers under drivers/char/hw_random.c
could optionally implement these early callbacks and provide random seed right out of some register reads or so before the kernel driver core is up. I do not know how
realistic that is, but for e.g. the nomadik-rng.c it is a simple register read from a physical address which should be simple to achieve.

Quoted 5 lines

This only works for peripherals that are always clocked, powered, etc, which you cannot rely on in the general case. This means you would need some cooperation from the bootloader/firmware to signal that the RNG is accessible early, and at which address. It might just as well pass you a random seed instead ...

Point taken. Maybe we should provide different ways to do that if the system does not have firmware, like an added property in the top of the device tree or even a new (gasp) ATAG
for legacy systems. But people might bikeshed that to pieces so it's good to begin like this.

Yours,
Linus Walleij

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel