Skip to contentSkip to conversation

Add FCS Random Number Generator (RNG) driver

PATCH New [email protected] 1 participant 3 messages Nov 20

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

cover Add FCS Random Number Generator (RNG) driver

From: Hang Suan Wang <[email protected]>

This series adds a hw_random front-end for the random-number service of the Altera SoCFPGA Crypto Service (FCS).

The driver owns no session or service-layer state: every request goes through the FCS core, which serializes access to the SDM crypto session and forwards it to the SDM through ATF.

The SDM is the hardware security controller in Altera SoCFPGA devices. Host software reaches it through the existing stratix10-svc mailbox, which uses Arm Trusted Firmware SIP SMC calls.

The Agilex FPGA series generator is an SP 800-90A compliant DRNG hardware block in the SDM, in particular, Agilex 7 is SP 800-90A compliant DRBG generation flow and is seeded with the TRNG hardware block.

Unlike existing hw_random drivers, this RNG produces data only while an SDM crypto session is open (for example, via FCS_IOC_OPEN_SESSION on /dev/socfpga-fcs). The session belongs to the file that opened it, not to this driver. With no session open, socfpga_fcs_core_engine() returns -ENOTCONN and the error is passed through unchanged: read() on /dev/hwrng fails with that error, and the hw_random entropy thread treats it as a transient failure and retries. The open and close session ioctl was added in the previous series:
https://lore.kernel.org/all/[email protected]/

The ATF accepts only a non-zero, 4-byte-aligned length, from 4 bytes up to 4080 bytes per request.

hwrng.quality is left at 0. The hw_random core documents 0 as the maximum (1024 bits of entropy per 1024 bits of input), and with the default quality of 1024 that is the value applied at registration. That matches a conditioned SP 800-90A DRNG, so the driver does not publish a separate estimate.

The series is organized as follows:
Patch 1 (prerequisite) extends the stratix10-svc service layer with the FCS command codes and matching SIP SMC function IDs.

Patch 2 adds the RNG driver: socfpga-fcs-rng hw_random driver. Add FCS_DEV_RANDOM_NUMBER_GEN_EXT in the FCS core. The socfpga-fcs-rng child device registered by the FCS driver. Also, add MAINTAINERS entry for this new driver.

Testing:
Built for arm64 (defconfig + CONFIG_HW_RANDOM_SOCFPGA_FCS=m). Tested on an Agilex 5 SoC FPGA board

root@testsocfpga# ./fcs_client -R dbg.bin -j 64 && hexdump -C dbg.bin
00000000 15 fc 77 a9 86 17 0b b5 91 b3 db ce a9 39 48 f1 |..w..........9H.|
00000010 65 00 80 9a 63 b8 1e 99 df 56 6f b4 ef 66 73 f9 |e...c....Vo..fs.|
00000020 91 66 c6 94 94 38 f1 9f cd 3a c7 6e b1 63 1d e2 |.f...8...:.n.c..|
00000030 03 ec 29 01 4f 27 4a de e7 7e 67 95 c2 44 e7 04 |..).O'J..~g..D..|
00000040

Set timeout to 1000s for operation: rngtest
cat /dev/hwrng | rngtest -c 2000 2>&1
rngtest 6.17
Copyright (c) 2004 by Henrique de Moraes Holschuh
This is free software; see the source for copying conditions. There is NO warranty;
not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

	rngtest: starting FIPS tests...
	rngtest: bits received from input: 40000032
	rngtest: FIPS 140-2 successes: 1998
	rngtest: FIPS 140-2 failures: 2
	rngtest: FIPS 140-2(2001-10-10) Monobit: 0
	rngtest: FIPS 140-2(2001-10-10) Poker: 0
	rngtest: FIPS 140-2(2001-10-10) Runs: 1
	rngtest: FIPS 140-2(2001-10-10) Long run: 1
	rngtest: FIPS 140-2(2001-10-10) Continuous run: 0
	rngtest: input channel speed: (min=495.429; avg=52178.721; 
								   max=10000000000.000)bits/s
	rngtest: FIPS tests speed: (min=47.329; avg=70.926; max=91.699)Mibits/s
	rngtest: Program run time: 807497698 microseconds

---
Hang Suan Wang (2):
firmware: stratix10-svc: add FCS random-number command for Agilex 5
hwrng: socfpga-fcs - Add FCS random number generator driver

 MAINTAINERS                                   |   1 +
 drivers/char/hw_random/Kconfig                |  12 ++
 drivers/char/hw_random/Makefile               |   1 +
 drivers/char/hw_random/socfpga-fcs-rng.c      | 133 ++++++++++++++++++
 drivers/firmware/socfpga-fcs-core.c           |  71 +++++++---
 drivers/firmware/socfpga-fcs.c                |  33 +++++
 drivers/firmware/stratix10-svc.c              |   8 ++
 include/linux/firmware/intel/socfpga-fcs.h    |  23 ++-
 include/linux/firmware/intel/stratix10-smc.h  |  46 ++++++
 .../firmware/intel/stratix10-svc-client.h     |   1 +
 10 files changed, 300 insertions(+), 29 deletions(-)
 create mode 100644 drivers/char/hw_random/socfpga-fcs-rng.c
Signature · 1 line
2.43.7
#2 lore raw

PATCH 2/2 hwrng: socfpga-fcs - Add FCS random number generator driver

From: Hang Suan Wang <[email protected]>

Add a hw_random front-end for the SoCFPGA FCS random-number service. The driver owns no session or service-layer state: every request goes through the FCS core, which serializes access to the SDM crypto session and forwards it to the SDM through ATF.

The Agilex FPGA series generator is an SP 800-90A compliant DRNG hardware block in the SDM, in particular, Agilex 7 is SP 800-90A compliant DRBG generation flow and is seeded with the TRNG hardware block.

Unlike existing hw_random drivers, this RNG produces data only if SDM crypto session is open (for example via FCS_IOC_OPEN_SESSION on /dev/socfpga-fcs). The session is held by the FCS service, not by this driver. With no session open, socfpga_fcs_core_engine() returns -ENOTCONN and the error is passed through unchanged: read() on /dev/hwrng fails with it, and the hw_random entropy thread treats it as a transient failure and retries.

The ATF accepts only a non-zero, 4-byte-aligned length, from 4 bytes up to 4080 bytes per request.

The hwrng.quality is left unset. The hw_random core treats 0 as the maximum (1024 bits of entropy per 1024 bits of input), which matches a conditioned SP 800-90A DRNG, so the driver claims no separate estimate.

---
 MAINTAINERS                                |   1 +
 drivers/char/hw_random/Kconfig             |  12 ++
 drivers/char/hw_random/Makefile            |   1 +
 drivers/char/hw_random/socfpga-fcs-rng.c   | 133 +++++++++++++++++++++
 drivers/firmware/socfpga-fcs-core.c        |  71 +++++++----
 drivers/firmware/socfpga-fcs.c             |  33 +++++
 include/linux/firmware/intel/socfpga-fcs.h |  23 ++--
 7 files changed, 245 insertions(+), 29 deletions(-)
 create mode 100644 drivers/char/hw_random/socfpga-fcs-rng.c
MAINTAINERS +1 -0
index 8fed8012195a..52e8650d22eb 100644
@@ -981,6 +981,7 @@ L:	[email protected]
 S:	Maintained
 F:	Documentation/ABI/testing/sysfs-devices-platform-stratix10-fcs
 F:	Documentation/misc-devices/socfpga-fcs.rst
+F:	drivers/char/hw_random/socfpga-fcs-rng.c
 F:	drivers/firmware/socfpga-fcs*
 F:	include/linux/firmware/intel/socfpga-fcs*
 F:	include/uapi/misc/socfpga-fcs*
drivers/char/hw_random/Kconfig +12 -0
index d3108991c251..9ebb52536854 100644
@@ -648,6 +648,18 @@ config HW_RANDOM_QCOM
 	  To compile this driver as a module, choose M here. The
 	  module will be called qcom-rng. If unsure, say N.
 
+config HW_RANDOM_SOCFPGA_FCS
+	tristate "Altera SoCFPGA FCS Random Number Generator support"
+	depends on ALTERA_SOCFPGA_FCS
+	help
+	  This driver provides support for the random number generator
+	  of the Altera SoCFPGA FPGA Crypto Service (FCS) in the Secure
+	  Device Manager, through the FCS core engine. It can only be
+	  used while an FCS crypto session is open.
+
+	  To compile this driver as a module, choose M here. The
+	  module will be called socfpga-fcs-rng. If unsure, say N.
+
 endif # HW_RANDOM
 
 config UML_RANDOM
drivers/char/hw_random/Makefile +1 -0
index 8fce2aa6cf7d..8496471d568b 100644
@@ -55,3 +55,4 @@ obj-$(CONFIG_HW_RANDOM_ROCKCHIP) += rockchip-rng.o
 obj-$(CONFIG_HW_RANDOM_JH7110) += jh7110-trng.o
 obj-$(CONFIG_HW_RANDOM_XILINX) += xilinx-trng.o
 obj-$(CONFIG_HW_RANDOM_QCOM) += qcom-rng.o
+obj-$(CONFIG_HW_RANDOM_SOCFPGA_FCS) += socfpga-fcs-rng.o
drivers/char/hw_random/socfpga-fcs-rng.c new file +133 -0

133 lines not on this page · raw message

drivers/firmware/socfpga-fcs-core.c +49 -22

143 lines not on this page · raw message

drivers/firmware/socfpga-fcs.c +33 -0
index c4a0c96ccca9..51812d409304 100644
@@ -235,6 +235,30 @@ static int fcs_driver_probe(struct platform_device *pdev)
 		return dev_err_probe(dev, ret, "Failed to register misc device\n");
 	}
 
+	/*
+	 * Spawn the hwrng front-end as a child device. It discovers this
+	 * state through dev_get_drvdata(dev->parent) and only ever calls
+	 * socfpga_fcs_core_engine(); a failure here is non-fatal, SDOS keeps
+	 * working without the RNG.
+	 */
+	if (!IS_ENABLED(CONFIG_HW_RANDOM_SOCFPGA_FCS))
+		return 0;
+
+	priv->rng_pdev = platform_device_alloc("socfpga-fcs-rng",
+					       PLATFORM_DEVID_NONE);
+	if (!priv->rng_pdev) {
+		dev_warn(dev, "failed to allocate socfpga-fcs-rng device\n");
+	} else {
+		priv->rng_pdev->dev.parent = dev;
+		ret = platform_device_add(priv->rng_pdev);
+		if (ret) {
+			dev_warn(dev, "failed to add socfpga-fcs-rng device: %d\n",
+				 ret);
+			platform_device_put(priv->rng_pdev);
+			priv->rng_pdev = NULL;
+		}
+	}
+
 	return 0;
 }
 
@@ -246,6 +270,12 @@ static void fcs_driver_remove(struct platform_device *pdev)
 {
 	struct socfpga_fcs_priv *priv = platform_get_drvdata(pdev);
 
+	/* Unregister the hwrng child first so it stops calling the engine. */
+	if (priv->rng_pdev) {
+		platform_device_unregister(priv->rng_pdev);
+		priv->rng_pdev = NULL;
+	}
+
 	/*
 	 * misc_deregister() does not wait for open files. Tear the svc
 	 * channel down here (parent may free it as soon as remove returns);
@@ -295,3 +325,6 @@ MODULE_LICENSE("GPL");
 MODULE_DESCRIPTION("Altera SoCFPGA FCS SDOS encrypt/decrypt driver");
 MODULE_AUTHOR("Altera Corporation");
 MODULE_ALIAS("platform:stratix10-fcs");
+#if IS_ENABLED(CONFIG_HW_RANDOM_SOCFPGA_FCS)
+MODULE_SOFTDEP("post: socfpga-fcs-rng");
+#endif
include/linux/firmware/intel/socfpga-fcs.h +16 -7
index 4d2d9f538b16..8ddc5d434e3c 100644
@@ -57,8 +57,10 @@ struct fcs_sdos_req {
  *                         socfpga_fcs_core_engine()
  * @src: kernel input buffer, or NULL for output-only commands
  * @src_len: bytes in @src
- * @dst: kernel output buffer, or NULL for input-only commands; only
- *       FCS_DEV_SDOS_DATA_EXT supports one in this series
+ * @dst: kernel output buffer, or NULL for input-only commands.
+ *       FCS_DEV_SDOS_DATA_EXT and FCS_DEV_RANDOM_NUMBER_GEN_EXT use one;
+ *       the engine treats the firmware response value as the number of
+ *       bytes produced.
  * @dst_len: on entry the capacity of @dst, on return the bytes produced
  * @context_id: SDM context ID, generated by the caller
  * @arg: per-command scalars, passed as message arg[2]/arg[3]
@@ -85,6 +87,8 @@ struct socfpga_fcs_priv {
 	struct stratix10_svc_chan *chan;
 	struct stratix10_svc_client client;
 	struct miscdevice miscdev;
+	/* Child device for the hwrng front-end, owned by socfpga-fcs.c. */
+	struct platform_device *rng_pdev;
 	/*
 	 * Held by the driver and by every open file. An fd may outlive
 	 * driver detach, so this state is not devm-managed: the firmware
@@ -114,8 +118,9 @@ struct socfpga_fcs_priv {
 	bool session_opened;
 	/*
 	 * Opaque token of whoever opened the session (the struct file for
-	 * the ioctl front end); guarded by @lock. Only the owner may run
-	 * commands on the session or close it. Compared, never dereferenced.
+	 * the ioctl front end); guarded by @lock. Only that owner may run
+	 * ioctl commands on the session or close it. A NULL owner is
+	 * accepted only for the RNG command. Compared, never dereferenced.
 	 */
 	const void *session_owner;
 	u32 atf_version[3];
@@ -126,6 +131,7 @@ enum fcs_command_code {
 	FCS_DEV_CRYPTO_OPEN_SESSION,
 	FCS_DEV_CRYPTO_CLOSE_SESSION,
 	FCS_DEV_SDOS_DATA_EXT,
+	FCS_DEV_RANDOM_NUMBER_GEN_EXT,
 	FCS_DEV_ATF_VERSION,
 };
 
@@ -144,15 +150,18 @@ void fcs_mark_removed(struct socfpga_fcs_priv *priv);
 int fcs_get_atf_version(struct socfpga_fcs_priv *priv, u32 *version);
 
 /*
- * Open/close a session. Only one session may be open, and only its
- * owner may use or close it. Each takes @priv->lock itself.
+ * Open/close a session. Only one session may be open, and only the same
+ * owner that opened the session can close the session. Each takes @priv->lock
+ * itself.
  */
 int fcs_ctx_begin(struct socfpga_fcs_priv *priv, const void *owner);
 int fcs_ctx_end(struct socfpga_fcs_priv *priv, const void *owner);
 
 /*
  * Run one FCS command on the core engine. @owner must be the session
- * owner; see the kernel-doc in socfpga-fcs-core.c.
+ * owner, except for FCS_DEV_RANDOM_NUMBER_GEN_EXT, which accepts NULL.
+ * The session must already be open. See the kernel-doc in
+ * socfpga-fcs-core.c.
  */
 int socfpga_fcs_core_engine(struct socfpga_fcs_priv *priv, const void *owner,
 			    enum fcs_command_code command,
Signature · 1 line
2.43.7
#1 lore raw

PATCH 1/2 firmware: stratix10-svc: add FCS random-number command for Agilex 5

In reply to [email protected]

From: Hang Suan Wang <[email protected]>

The Agilex 5 Secure Device Manager (SDM 1.5) exposes an FPGA Crypto Service (FCS) random-number primitive over the existing SIP SMC
mailbox: a session-based interface, alongside SDOS encrypt/decrypt. The service layer has no command to drive it yet.

Configure stratix10-svc about this interface so an in-kernel FCS client can use it:

- add the asynchronous client command code
COMMAND_FCS_RANDOM_NUMBER_GEN_EXT

- add the matching SIP SMC function IDs INTEL_SIP_SMC_FCS_RANDOM_NUMBER_EXT and INTEL_SIP_SMC_ASYNC_FCS_RANDOM_NUMBER_EXT, with their register-usage documentation

The transport is unchanged: Agilex 5 reuses the SIP SMC calling convention and async mailbox ABI the driver already implements, so no new transport mechanism is required.

COMMAND_FCS_RANDOM_NUMBER_GEN_EXT is dispatched as INTEL_SIP_SMC_ASYNC_FCS_RANDOM_NUMBER_EXT. The session ID and context ID are taken from the client message. The destination buffer is the physical address of the service-layer payload, and the payload length is the number of random bytes requested (4080 bytes at most). On completion the value returned in a2 is passed back to the client, as for the other asynchronous FCS commands.

This is a prerequisite for the SoCFPGA FCS hw_random driver, the in-tree consumer of this command.

---
 drivers/firmware/stratix10-svc.c              |  8 ++++
 include/linux/firmware/intel/stratix10-smc.h  | 46 +++++++++++++++++++
 .../firmware/intel/stratix10-svc-client.h     |  1 +
 3 files changed, 55 insertions(+)
drivers/firmware/stratix10-svc.c +8 -0
index 386673bc093f..d6d1cf19d5a5 100644
@@ -1643,6 +1643,13 @@ int stratix10_svc_async_send(struct stratix10_svc_chan *chan, void *msg,
 		args.a0 = INTEL_SIP_SMC_ASYNC_FCS_CLOSE_CS_SESSION;
 		args.a2 = p_msg->arg[0];
 		break;
+	case COMMAND_FCS_RANDOM_NUMBER_GEN_EXT:
+		args.a0 = INTEL_SIP_SMC_ASYNC_FCS_RANDOM_NUMBER_EXT;
+		args.a2 = p_msg->arg[0];
+		args.a3 = p_msg->arg[1];
+		args.a4 = svc_va_to_pa(ctrl, p_msg->payload);
+		args.a5 = p_msg->payload_length;
+		break;
 	case COMMAND_FCS_SDOS_DATA_EXT:
 		args.a0 = INTEL_SIP_SMC_ASYNC_FCS_CRYPTION_EXT;
 		args.a2 = p_msg->arg[0];
@@ -1757,6 +1764,7 @@ static int stratix10_svc_async_prepare_response(struct stratix10_svc_chan *chan,
 	case COMMAND_RSU_NOTIFY:
 		break;
 	case COMMAND_FCS_CRYPTO_OPEN_SESSION:
+	case COMMAND_FCS_RANDOM_NUMBER_GEN_EXT:
 	case COMMAND_FCS_SDOS_DATA_EXT:
 		data->kaddr1 = (void *)&handle->res.a2;
 		break;
include/linux/firmware/intel/stratix10-smc.h +46 -0

52 lines not on this page · raw message

include/linux/firmware/intel/stratix10-svc-client.h +1 -0
index edd7953d5d08..c924dc9a6ef2 100644
@@ -202,6 +202,7 @@ enum stratix10_svc_command_code {
 	/* for crypto service */
 	COMMAND_FCS_CRYPTO_OPEN_SESSION = 50,
 	COMMAND_FCS_CRYPTO_CLOSE_SESSION,
+	COMMAND_FCS_RANDOM_NUMBER_GEN_EXT = 81,
 	/* for extended SDOS encrypt/decrypt */
 	COMMAND_FCS_SDOS_DATA_EXT = 82,
 	/* for generic mailbox send command */
Signature · 1 line
2.43.7