[PATCH 1/3] rng: add OP-TEE based Random Number Generator

Add driver for OP-TEE based Random Number Generator on ARM SoCs where hardware entropy sources are not accessible to normal world and the RNG service is provided by a HWRNG Trusted Application (TA).
This driver is based on the linux driver: char/hw_random/optee-rng.c
Signed-off-by: Patrick Delaunay patrick.delaunay@foss.st.com ---
MAINTAINERS | 1 + drivers/rng/Kconfig | 8 +++ drivers/rng/Makefile | 1 + drivers/rng/optee_rng.c | 156 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 166 insertions(+) create mode 100644 drivers/rng/optee_rng.c
diff --git a/MAINTAINERS b/MAINTAINERS index f25ca7cd00..3356c65dd0 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -481,6 +481,7 @@ F: drivers/power/regulator/stpmic1.c F: drivers/ram/stm32mp1/ F: drivers/remoteproc/stm32_copro.c F: drivers/reset/stm32-reset.c +F: drivers/rng/optee_rng.c F: drivers/rng/stm32mp1_rng.c F: drivers/rtc/stm32_rtc.c F: drivers/serial/serial_stm32.* diff --git a/drivers/rng/Kconfig b/drivers/rng/Kconfig index b1c5ab93d1..a02c585f61 100644 --- a/drivers/rng/Kconfig +++ b/drivers/rng/Kconfig @@ -31,6 +31,14 @@ config RNG_MSM This driver provides support for the Random Number Generator hardware found on Qualcomm SoCs.
+config RNG_OPTEE + bool "OP-TEE based Random Number Generator support" + depends on DM_RNG && OPTEE + help + This driver provides support for OP-TEE based Random Number + Generator on ARM SoCs where hardware entropy sources are not + accessible to normal world. + config RNG_STM32MP1 bool "Enable random number generator for STM32MP1" depends on ARCH_STM32MP diff --git a/drivers/rng/Makefile b/drivers/rng/Makefile index 39f7ee3f03..435b3b965a 100644 --- a/drivers/rng/Makefile +++ b/drivers/rng/Makefile @@ -7,6 +7,7 @@ obj-$(CONFIG_DM_RNG) += rng-uclass.o obj-$(CONFIG_RNG_MESON) += meson-rng.o obj-$(CONFIG_RNG_SANDBOX) += sandbox_rng.o obj-$(CONFIG_RNG_MSM) += msm_rng.o +obj-$(CONFIG_RNG_OPTEE) += optee_rng.o obj-$(CONFIG_RNG_STM32MP1) += stm32mp1_rng.o obj-$(CONFIG_RNG_ROCKCHIP) += rockchip_rng.o obj-$(CONFIG_RNG_IPROC200) += iproc_rng200.o diff --git a/drivers/rng/optee_rng.c b/drivers/rng/optee_rng.c new file mode 100644 index 0000000000..a0833d0862 --- /dev/null +++ b/drivers/rng/optee_rng.c @@ -0,0 +1,156 @@ +// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause +/* + * Copyright (C) 2022, STMicroelectronics - All Rights Reserved + */ +#define LOG_CATEGORY UCLASS_RNG + +#include <common.h> + +#include <rng.h> +#include <tee.h> +#include <dm/device.h> +#include <dm/device_compat.h> +#include <linux/sizes.h> + +#define TEE_ERROR_HEALTH_TEST_FAIL 0x00000001 + +/* + * TA_CMD_GET_ENTROPY - Get Entropy from RNG + * + * param[0] (inout memref) - Entropy buffer memory reference + * param[1] unused + * param[2] unused + * param[3] unused + * + * Result: + * TEE_SUCCESS - Invoke command success + * TEE_ERROR_BAD_PARAMETERS - Incorrect input param + * TEE_ERROR_NOT_SUPPORTED - Requested entropy size greater than size of pool + * TEE_ERROR_HEALTH_TEST_FAIL - Continuous health testing failed + */ +#define TA_CMD_GET_ENTROPY 0x0 + +#define MAX_ENTROPY_REQ_SZ SZ_4K + +#define TA_HWRNG_UUID { 0xab7a617c, 0xb8e7, 0x4d8f, \ + { 0x83, 0x01, 0xd0, 0x9b, 0x61, 0x03, 0x6b, 0x64 } } + +/** + * struct optee_rng_priv - OP-TEE Random Number Generator private data + * @session_id: RNG TA session identifier. + */ +struct optee_rng_priv { + u32 session_id; +}; + +static int get_optee_rng_data(struct udevice *dev, + struct tee_shm *entropy_shm_pool, + void *buf, size_t *size) +{ + struct optee_rng_priv *priv = dev_get_priv(dev); + int ret = 0; + struct tee_invoke_arg arg; + struct tee_param param; + + memset(&arg, 0, sizeof(arg)); + memset(¶m, 0, sizeof(param)); + + /* Invoke TA_CMD_GET_ENTROPY function of Trusted App */ + arg.func = TA_CMD_GET_ENTROPY; + arg.session = priv->session_id; + + /* Fill invoke cmd params */ + param.attr = TEE_PARAM_ATTR_TYPE_MEMREF_INOUT; + param.u.memref.shm = entropy_shm_pool; + param.u.memref.size = *size; + + ret = tee_invoke_func(dev->parent, &arg, 1, ¶m); + if (ret || arg.ret) { + if (!ret) + ret = -EPROTO; + dev_err(dev, "TA_CMD_GET_ENTROPY invoke err: %d 0x%x\n", ret, arg.ret); + *size = 0; + + return ret; + } + + memcpy(buf, param.u.memref.shm->addr, param.u.memref.size); + *size = param.u.memref.size; + + return 0; +} + +static int optee_rng_read(struct udevice *dev, void *buf, size_t len) +{ + size_t read = 0, rng_size = 0; + struct tee_shm *entropy_shm_pool; + u8 *data = buf; + int ret; + + ret = tee_shm_alloc(dev->parent, MAX_ENTROPY_REQ_SZ, 0, &entropy_shm_pool); + if (ret) { + dev_err(dev, "tee_shm_alloc failed: %d\n", ret); + return ret; + } + + while (read < len) { + rng_size = min(len - read, (size_t)MAX_ENTROPY_REQ_SZ); + ret = get_optee_rng_data(dev, entropy_shm_pool, data, &rng_size); + if (ret) + goto shm_free; + data += rng_size; + read += rng_size; + } + +shm_free: + tee_shm_free(entropy_shm_pool); + + return ret; +} + +static int optee_rng_probe(struct udevice *dev) +{ + const struct tee_optee_ta_uuid uuid = TA_HWRNG_UUID; + struct optee_rng_priv *priv = dev_get_priv(dev); + struct tee_open_session_arg sess_arg; + int ret; + + memset(&sess_arg, 0, sizeof(sess_arg)); + + /* Open session with hwrng Trusted App */ + tee_optee_ta_uuid_to_octets(sess_arg.uuid, &uuid); + sess_arg.clnt_login = TEE_LOGIN_PUBLIC; + + ret = tee_open_session(dev->parent, &sess_arg, 0, NULL); + if (ret || sess_arg.ret) { + if (!ret) + ret = -EIO; + dev_err(dev, "can't open session: %d 0x%x\n", ret, sess_arg.ret); + return ret; + } + priv->session_id = sess_arg.session; + + return 0; +} + +static int optee_rng_remove(struct udevice *dev) +{ + struct optee_rng_priv *priv = dev_get_priv(dev); + + tee_close_session(dev->parent, priv->session_id); + + return 0; +} + +static const struct dm_rng_ops optee_rng_ops = { + .read = optee_rng_read, +}; + +U_BOOT_DRIVER(optee_rng) = { + .name = "optee-rng", + .id = UCLASS_RNG, + .ops = &optee_rng_ops, + .probe = optee_rng_probe, + .remove = optee_rng_remove, + .priv_auto = sizeof(struct optee_rng_priv), +};

In U-Boot, the discovery of TA based on its UUID on the TEE bus is not supported.
This patch only binds the driver associated to the new supported OP-TEE TA = TA_HWRNG when this driver is enable.
Signed-off-by: Patrick Delaunay patrick.delaunay@foss.st.com ---
drivers/tee/optee/core.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
diff --git a/drivers/tee/optee/core.c b/drivers/tee/optee/core.c index dad46aa388..a89d62aaf0 100644 --- a/drivers/tee/optee/core.c +++ b/drivers/tee/optee/core.c @@ -7,6 +7,7 @@ #include <cpu_func.h> #include <dm.h> #include <dm/device_compat.h> +#include <dm/lists.h> #include <log.h> #include <malloc.h> #include <tee.h> @@ -641,6 +642,8 @@ static int optee_probe(struct udevice *dev) { struct optee_pdata *pdata = dev_get_plat(dev); u32 sec_caps; + struct udevice *child; + int ret;
if (!is_optee_api(pdata->invoke_fn)) { dev_err(dev, "OP-TEE api uid mismatch\n"); @@ -665,6 +668,16 @@ static int optee_probe(struct udevice *dev) return -ENOENT; }
+ /* + * in U-Boot, the discovery of TA on the TEE bus is not supported: + * only bind the drivers associated to the supported OP-TEE TA + */ + if (IS_ENABLED(CONFIG_RNG_OPTEE)) { + ret = device_bind_driver(dev, "optee-rng", "optee-rng", &child); + if (ret) + return ret; + } + return 0; }

When the RNG device is secured with OP-TEE, it is only accessible with the HWRNG TA, the CONFIG_RNG_OPTEE is needed for STM32MP15 targets with OP-TEE support.
The probe of this RNG driver fails when the TA is not available in OP-TEE and the previous driver can be used, as CONFIG_RNG_STM32MP1 is activated and when the associated node is activated in the device tree with:
&rng1 { status = "okay"; };
When the RNG is used in OP-TEE, this node should be deactivated in the Linux and U-Boot device tree.
Signed-off-by: Patrick Delaunay patrick.delaunay@foss.st.com ---
configs/stm32mp15_defconfig | 1 + configs/stm32mp15_trusted_defconfig | 1 + 2 files changed, 2 insertions(+)
diff --git a/configs/stm32mp15_defconfig b/configs/stm32mp15_defconfig index 2beb88e81d..3f48c677e6 100644 --- a/configs/stm32mp15_defconfig +++ b/configs/stm32mp15_defconfig @@ -119,6 +119,7 @@ CONFIG_DM_REGULATOR_STPMIC1=y CONFIG_REMOTEPROC_STM32_COPRO=y CONFIG_RESET_SCMI=y CONFIG_DM_RNG=y +CONFIG_RNG_OPTEE=y CONFIG_RNG_STM32MP1=y CONFIG_DM_RTC=y CONFIG_RTC_STM32=y diff --git a/configs/stm32mp15_trusted_defconfig b/configs/stm32mp15_trusted_defconfig index c6857d08ec..9f869aca36 100644 --- a/configs/stm32mp15_trusted_defconfig +++ b/configs/stm32mp15_trusted_defconfig @@ -120,6 +120,7 @@ CONFIG_DM_REGULATOR_STPMIC1=y CONFIG_REMOTEPROC_STM32_COPRO=y CONFIG_RESET_SCMI=y CONFIG_DM_RNG=y +CONFIG_RNG_OPTEE=y CONFIG_RNG_STM32MP1=y CONFIG_DM_RTC=y CONFIG_RTC_STM32=y

On 3/22/22 15:06, Patrick Delaunay wrote:
Add driver for OP-TEE based Random Number Generator on ARM SoCs where hardware entropy sources are not accessible to normal world and the RNG service is provided by a HWRNG Trusted Application (TA).
This driver is based on the linux driver: char/hw_random/optee-rng.c
Signed-off-by: Patrick Delaunay patrick.delaunay@foss.st.com
MAINTAINERS | 1 + drivers/rng/Kconfig | 8 +++ drivers/rng/Makefile | 1 + drivers/rng/optee_rng.c | 156 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 166 insertions(+) create mode 100644 drivers/rng/optee_rng.c
diff --git a/MAINTAINERS b/MAINTAINERS index f25ca7cd00..3356c65dd0 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -481,6 +481,7 @@ F: drivers/power/regulator/stpmic1.c F: drivers/ram/stm32mp1/ F: drivers/remoteproc/stm32_copro.c F: drivers/reset/stm32-reset.c +F: drivers/rng/optee_rng.c F: drivers/rng/stm32mp1_rng.c F: drivers/rtc/stm32_rtc.c F: drivers/serial/serial_stm32.* diff --git a/drivers/rng/Kconfig b/drivers/rng/Kconfig index b1c5ab93d1..a02c585f61 100644 --- a/drivers/rng/Kconfig +++ b/drivers/rng/Kconfig @@ -31,6 +31,14 @@ config RNG_MSM This driver provides support for the Random Number Generator hardware found on Qualcomm SoCs.
+config RNG_OPTEE
- bool "OP-TEE based Random Number Generator support"
- depends on DM_RNG && OPTEE
- help
This driver provides support for OP-TEE based Random Number
Generator on ARM SoCs where hardware entropy sources are not
accessible to normal world.
https://optee.readthedocs.io/en/latest/architecture/porting_guidelines.html has:
"By default OP-TEE is configured with a software PRNG. The entropy is added to software PRNG at various places, but unfortunately it is still quite easy to predict the data added as entropy. As a consequence, unless the RNG is based on hardware the generated random will be quite weak."
Having a similiar warning in the long text for the CONFIG_RNG_OPTEE symbol would be helpful.
- config RNG_STM32MP1 bool "Enable random number generator for STM32MP1" depends on ARCH_STM32MP
diff --git a/drivers/rng/Makefile b/drivers/rng/Makefile index 39f7ee3f03..435b3b965a 100644 --- a/drivers/rng/Makefile +++ b/drivers/rng/Makefile @@ -7,6 +7,7 @@ obj-$(CONFIG_DM_RNG) += rng-uclass.o obj-$(CONFIG_RNG_MESON) += meson-rng.o obj-$(CONFIG_RNG_SANDBOX) += sandbox_rng.o obj-$(CONFIG_RNG_MSM) += msm_rng.o +obj-$(CONFIG_RNG_OPTEE) += optee_rng.o obj-$(CONFIG_RNG_STM32MP1) += stm32mp1_rng.o obj-$(CONFIG_RNG_ROCKCHIP) += rockchip_rng.o obj-$(CONFIG_RNG_IPROC200) += iproc_rng200.o diff --git a/drivers/rng/optee_rng.c b/drivers/rng/optee_rng.c new file mode 100644 index 0000000000..a0833d0862 --- /dev/null +++ b/drivers/rng/optee_rng.c @@ -0,0 +1,156 @@ +// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause +/*
- Copyright (C) 2022, STMicroelectronics - All Rights Reserved
- */
+#define LOG_CATEGORY UCLASS_RNG
+#include <common.h>
+#include <rng.h> +#include <tee.h> +#include <dm/device.h> +#include <dm/device_compat.h> +#include <linux/sizes.h>
+#define TEE_ERROR_HEALTH_TEST_FAIL 0x00000001
+/*
- TA_CMD_GET_ENTROPY - Get Entropy from RNG
- param[0] (inout memref) - Entropy buffer memory reference
- param[1] unused
- param[2] unused
- param[3] unused
- Result:
- TEE_SUCCESS - Invoke command success
- TEE_ERROR_BAD_PARAMETERS - Incorrect input param
- TEE_ERROR_NOT_SUPPORTED - Requested entropy size greater than size of pool
- TEE_ERROR_HEALTH_TEST_FAIL - Continuous health testing failed
- */
+#define TA_CMD_GET_ENTROPY 0x0
+#define MAX_ENTROPY_REQ_SZ SZ_4K
+#define TA_HWRNG_UUID { 0xab7a617c, 0xb8e7, 0x4d8f, \
{ 0x83, 0x01, 0xd0, 0x9b, 0x61, 0x03, 0x6b, 0x64 } }
+/**
- struct optee_rng_priv - OP-TEE Random Number Generator private data
- @session_id: RNG TA session identifier.
- */
+struct optee_rng_priv {
- u32 session_id;
+};
Please, provide as Sphinx style function description.
+static int get_optee_rng_data(struct udevice *dev,
struct tee_shm *entropy_shm_pool,
void *buf, size_t *size)
+{
- struct optee_rng_priv *priv = dev_get_priv(dev);
- int ret = 0;
- struct tee_invoke_arg arg;
Wouldn't it be preferable to use
struct tee_invoke_arg arg = { 0 }; ?
This way the compiler can optimize the initialization and set all 0 initialized variables with a single memset() call.
Best regards
Heinrich
- struct tee_param param;
- memset(&arg, 0, sizeof(arg));
- memset(¶m, 0, sizeof(param));
- /* Invoke TA_CMD_GET_ENTROPY function of Trusted App */
- arg.func = TA_CMD_GET_ENTROPY;
- arg.session = priv->session_id;
- /* Fill invoke cmd params */
- param.attr = TEE_PARAM_ATTR_TYPE_MEMREF_INOUT;
- param.u.memref.shm = entropy_shm_pool;
- param.u.memref.size = *size;
- ret = tee_invoke_func(dev->parent, &arg, 1, ¶m);
- if (ret || arg.ret) {
if (!ret)
ret = -EPROTO;
dev_err(dev, "TA_CMD_GET_ENTROPY invoke err: %d 0x%x\n", ret, arg.ret);
*size = 0;
return ret;
- }
- memcpy(buf, param.u.memref.shm->addr, param.u.memref.size);
- *size = param.u.memref.size;
- return 0;
+}
+static int optee_rng_read(struct udevice *dev, void *buf, size_t len) +{
- size_t read = 0, rng_size = 0;
- struct tee_shm *entropy_shm_pool;
- u8 *data = buf;
- int ret;
- ret = tee_shm_alloc(dev->parent, MAX_ENTROPY_REQ_SZ, 0, &entropy_shm_pool);
- if (ret) {
dev_err(dev, "tee_shm_alloc failed: %d\n", ret);
return ret;
- }
- while (read < len) {
rng_size = min(len - read, (size_t)MAX_ENTROPY_REQ_SZ);
ret = get_optee_rng_data(dev, entropy_shm_pool, data, &rng_size);
if (ret)
goto shm_free;
data += rng_size;
read += rng_size;
- }
+shm_free:
- tee_shm_free(entropy_shm_pool);
- return ret;
+}
+static int optee_rng_probe(struct udevice *dev) +{
- const struct tee_optee_ta_uuid uuid = TA_HWRNG_UUID;
- struct optee_rng_priv *priv = dev_get_priv(dev);
- struct tee_open_session_arg sess_arg;
- int ret;
- memset(&sess_arg, 0, sizeof(sess_arg));
- /* Open session with hwrng Trusted App */
- tee_optee_ta_uuid_to_octets(sess_arg.uuid, &uuid);
- sess_arg.clnt_login = TEE_LOGIN_PUBLIC;
- ret = tee_open_session(dev->parent, &sess_arg, 0, NULL);
- if (ret || sess_arg.ret) {
if (!ret)
ret = -EIO;
dev_err(dev, "can't open session: %d 0x%x\n", ret, sess_arg.ret);
return ret;
- }
- priv->session_id = sess_arg.session;
- return 0;
+}
+static int optee_rng_remove(struct udevice *dev) +{
- struct optee_rng_priv *priv = dev_get_priv(dev);
- tee_close_session(dev->parent, priv->session_id);
- return 0;
+}
+static const struct dm_rng_ops optee_rng_ops = {
- .read = optee_rng_read,
+};
+U_BOOT_DRIVER(optee_rng) = {
- .name = "optee-rng",
- .id = UCLASS_RNG,
- .ops = &optee_rng_ops,
- .probe = optee_rng_probe,
- .remove = optee_rng_remove,
- .priv_auto = sizeof(struct optee_rng_priv),
+};

Hi,
On 3/22/22 15:38, Heinrich Schuchardt wrote:
On 3/22/22 15:06, Patrick Delaunay wrote:
Add driver for OP-TEE based Random Number Generator on ARM SoCs where hardware entropy sources are not accessible to normal world and the RNG service is provided by a HWRNG Trusted Application (TA).
This driver is based on the linux driver: char/hw_random/optee-rng.c
Signed-off-by: Patrick Delaunay patrick.delaunay@foss.st.com
MAINTAINERS | 1 + drivers/rng/Kconfig | 8 +++ drivers/rng/Makefile | 1 + drivers/rng/optee_rng.c | 156 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 166 insertions(+) create mode 100644 drivers/rng/optee_rng.c
diff --git a/MAINTAINERS b/MAINTAINERS index f25ca7cd00..3356c65dd0 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -481,6 +481,7 @@ F: drivers/power/regulator/stpmic1.c F: drivers/ram/stm32mp1/ F: drivers/remoteproc/stm32_copro.c F: drivers/reset/stm32-reset.c +F: drivers/rng/optee_rng.c F: drivers/rng/stm32mp1_rng.c F: drivers/rtc/stm32_rtc.c F: drivers/serial/serial_stm32.* diff --git a/drivers/rng/Kconfig b/drivers/rng/Kconfig index b1c5ab93d1..a02c585f61 100644 --- a/drivers/rng/Kconfig +++ b/drivers/rng/Kconfig @@ -31,6 +31,14 @@ config RNG_MSM This driver provides support for the Random Number Generator hardware found on Qualcomm SoCs.
+config RNG_OPTEE + bool "OP-TEE based Random Number Generator support" + depends on DM_RNG && OPTEE + help + This driver provides support for OP-TEE based Random Number + Generator on ARM SoCs where hardware entropy sources are not + accessible to normal world.
https://optee.readthedocs.io/en/latest/architecture/porting_guidelines.html
has:
"By default OP-TEE is configured with a software PRNG. The entropy is added to software PRNG at various places, but unfortunately it is still quite easy to predict the data added as entropy. As a consequence, unless the RNG is based on hardware the generated random will be quite weak."
Having a similiar warning in the long text for the CONFIG_RNG_OPTEE symbol would be helpful.
I propose something as :
+config RNG_OPTEE + bool "OP-TEE based Random Number Generator support" + depends on DM_RNG && OPTEE + help + This driver provides support for OP-TEE based Random Number + Generator on ARM SoCs where hardware entropy sources are not + accessible to normal world but reserved and used by the OP-TEE
+ to avoid weakness of the software PRNG.
config RNG_STM32MP1 bool "Enable random number generator for STM32MP1" depends on ARCH_STM32MP diff --git a/drivers/rng/Makefile b/drivers/rng/Makefile index 39f7ee3f03..435b3b965a 100644 --- a/drivers/rng/Makefile +++ b/drivers/rng/Makefile @@ -7,6 +7,7 @@ obj-$(CONFIG_DM_RNG) += rng-uclass.o obj-$(CONFIG_RNG_MESON) += meson-rng.o obj-$(CONFIG_RNG_SANDBOX) += sandbox_rng.o obj-$(CONFIG_RNG_MSM) += msm_rng.o +obj-$(CONFIG_RNG_OPTEE) += optee_rng.o obj-$(CONFIG_RNG_STM32MP1) += stm32mp1_rng.o obj-$(CONFIG_RNG_ROCKCHIP) += rockchip_rng.o obj-$(CONFIG_RNG_IPROC200) += iproc_rng200.o diff --git a/drivers/rng/optee_rng.c b/drivers/rng/optee_rng.c new file mode 100644 index 0000000000..a0833d0862 --- /dev/null +++ b/drivers/rng/optee_rng.c @@ -0,0 +1,156 @@ +// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause +/*
- Copyright (C) 2022, STMicroelectronics - All Rights Reserved
- */
+#define LOG_CATEGORY UCLASS_RNG
+#include <common.h>
+#include <rng.h> +#include <tee.h> +#include <dm/device.h> +#include <dm/device_compat.h> +#include <linux/sizes.h>
+#define TEE_ERROR_HEALTH_TEST_FAIL 0x00000001
+/*
- TA_CMD_GET_ENTROPY - Get Entropy from RNG
- param[0] (inout memref) - Entropy buffer memory reference
- param[1] unused
- param[2] unused
- param[3] unused
- Result:
- TEE_SUCCESS - Invoke command success
- TEE_ERROR_BAD_PARAMETERS - Incorrect input param
- TEE_ERROR_NOT_SUPPORTED - Requested entropy size greater than
size of pool
- TEE_ERROR_HEALTH_TEST_FAIL - Continuous health testing failed
- */
+#define TA_CMD_GET_ENTROPY 0x0
+#define MAX_ENTROPY_REQ_SZ SZ_4K
+#define TA_HWRNG_UUID { 0xab7a617c, 0xb8e7, 0x4d8f, \ + { 0x83, 0x01, 0xd0, 0x9b, 0x61, 0x03, 0x6b, 0x64 } }
+/**
- struct optee_rng_priv - OP-TEE Random Number Generator private data
- @session_id: RNG TA session identifier.
- */
+struct optee_rng_priv { + u32 session_id; +};
Please, provide as Sphinx style function description.
OK
+static int get_optee_rng_data(struct udevice *dev, + struct tee_shm *entropy_shm_pool, + void *buf, size_t *size) +{ + struct optee_rng_priv *priv = dev_get_priv(dev); + int ret = 0; + struct tee_invoke_arg arg;
Wouldn't it be preferable to use
struct tee_invoke_arg arg = { 0 }; ?
This way the compiler can optimize the initialization and set all 0 initialized variables with a single memset() call.
I wasn't sure of the portability for struct zero-initialisation.
But I will change it in V2.
Best regards
Heinrich
+ struct tee_param param;
+ memset(&arg, 0, sizeof(arg)); + memset(¶m, 0, sizeof(param));
+ /* Invoke TA_CMD_GET_ENTROPY function of Trusted App */ + arg.func = TA_CMD_GET_ENTROPY; + arg.session = priv->session_id;
+ /* Fill invoke cmd params */ + param.attr = TEE_PARAM_ATTR_TYPE_MEMREF_INOUT; + param.u.memref.shm = entropy_shm_pool; + param.u.memref.size = *size;
+ ret = tee_invoke_func(dev->parent, &arg, 1, ¶m); + if (ret || arg.ret) { + if (!ret) + ret = -EPROTO; + dev_err(dev, "TA_CMD_GET_ENTROPY invoke err: %d 0x%x\n", ret, arg.ret); + *size = 0;
+ return ret; + }
+ memcpy(buf, param.u.memref.shm->addr, param.u.memref.size); + *size = param.u.memref.size;
+ return 0; +}
(...)
Regards
Patrick

On 3/24/22 12:03, Patrick DELAUNAY wrote:
Hi,
On 3/22/22 15:38, Heinrich Schuchardt wrote:
On 3/22/22 15:06, Patrick Delaunay wrote:
Add driver for OP-TEE based Random Number Generator on ARM SoCs where hardware entropy sources are not accessible to normal world and the RNG service is provided by a HWRNG Trusted Application (TA).
This driver is based on the linux driver: char/hw_random/optee-rng.c
Signed-off-by: Patrick Delaunay patrick.delaunay@foss.st.com
MAINTAINERS | 1 + drivers/rng/Kconfig | 8 +++ drivers/rng/Makefile | 1 + drivers/rng/optee_rng.c | 156 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 166 insertions(+) create mode 100644 drivers/rng/optee_rng.c
diff --git a/MAINTAINERS b/MAINTAINERS index f25ca7cd00..3356c65dd0 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -481,6 +481,7 @@ F: drivers/power/regulator/stpmic1.c F: drivers/ram/stm32mp1/ F: drivers/remoteproc/stm32_copro.c F: drivers/reset/stm32-reset.c +F: drivers/rng/optee_rng.c F: drivers/rng/stm32mp1_rng.c F: drivers/rtc/stm32_rtc.c F: drivers/serial/serial_stm32.* diff --git a/drivers/rng/Kconfig b/drivers/rng/Kconfig index b1c5ab93d1..a02c585f61 100644 --- a/drivers/rng/Kconfig +++ b/drivers/rng/Kconfig @@ -31,6 +31,14 @@ config RNG_MSM This driver provides support for the Random Number Generator hardware found on Qualcomm SoCs.
+config RNG_OPTEE + bool "OP-TEE based Random Number Generator support" + depends on DM_RNG && OPTEE + help + This driver provides support for OP-TEE based Random Number + Generator on ARM SoCs where hardware entropy sources are not + accessible to normal world.
https://optee.readthedocs.io/en/latest/architecture/porting_guidelines.html
has:
"By default OP-TEE is configured with a software PRNG. The entropy is added to software PRNG at various places, but unfortunately it is still quite easy to predict the data added as entropy. As a consequence, unless the RNG is based on hardware the generated random will be quite weak."
Having a similiar warning in the long text for the CONFIG_RNG_OPTEE symbol would be helpful.
I propose something as :
+config RNG_OPTEE + bool "OP-TEE based Random Number Generator support" + depends on DM_RNG && OPTEE + help + This driver provides support for OP-TEE based Random Number
%s/for/for the/
+ Generator on ARM SoCs where hardware entropy sources are not + accessible to normal world but reserved and used by the OP-TEE
%s/\n//
+ to avoid weakness of the software PRNG.
%s/avoid weakness of the/avoid the weakness of a/
Looks ok to me.
Best regards
Heinrich
config RNG_STM32MP1 bool "Enable random number generator for STM32MP1" depends on ARCH_STM32MP diff --git a/drivers/rng/Makefile b/drivers/rng/Makefile index 39f7ee3f03..435b3b965a 100644 --- a/drivers/rng/Makefile +++ b/drivers/rng/Makefile @@ -7,6 +7,7 @@ obj-$(CONFIG_DM_RNG) += rng-uclass.o obj-$(CONFIG_RNG_MESON) += meson-rng.o obj-$(CONFIG_RNG_SANDBOX) += sandbox_rng.o obj-$(CONFIG_RNG_MSM) += msm_rng.o +obj-$(CONFIG_RNG_OPTEE) += optee_rng.o obj-$(CONFIG_RNG_STM32MP1) += stm32mp1_rng.o obj-$(CONFIG_RNG_ROCKCHIP) += rockchip_rng.o obj-$(CONFIG_RNG_IPROC200) += iproc_rng200.o diff --git a/drivers/rng/optee_rng.c b/drivers/rng/optee_rng.c new file mode 100644 index 0000000000..a0833d0862 --- /dev/null +++ b/drivers/rng/optee_rng.c @@ -0,0 +1,156 @@ +// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause +/*
- Copyright (C) 2022, STMicroelectronics - All Rights Reserved
- */
+#define LOG_CATEGORY UCLASS_RNG
+#include <common.h>
+#include <rng.h> +#include <tee.h> +#include <dm/device.h> +#include <dm/device_compat.h> +#include <linux/sizes.h>
+#define TEE_ERROR_HEALTH_TEST_FAIL 0x00000001
+/*
- TA_CMD_GET_ENTROPY - Get Entropy from RNG
- param[0] (inout memref) - Entropy buffer memory reference
- param[1] unused
- param[2] unused
- param[3] unused
- Result:
- TEE_SUCCESS - Invoke command success
- TEE_ERROR_BAD_PARAMETERS - Incorrect input param
- TEE_ERROR_NOT_SUPPORTED - Requested entropy size greater than
size of pool
- TEE_ERROR_HEALTH_TEST_FAIL - Continuous health testing failed
- */
+#define TA_CMD_GET_ENTROPY 0x0
+#define MAX_ENTROPY_REQ_SZ SZ_4K
+#define TA_HWRNG_UUID { 0xab7a617c, 0xb8e7, 0x4d8f, \ + { 0x83, 0x01, 0xd0, 0x9b, 0x61, 0x03, 0x6b, 0x64 } }
+/**
- struct optee_rng_priv - OP-TEE Random Number Generator private data
- @session_id: RNG TA session identifier.
- */
+struct optee_rng_priv { + u32 session_id; +};
Please, provide as Sphinx style function description.
OK
+static int get_optee_rng_data(struct udevice *dev, + struct tee_shm *entropy_shm_pool, + void *buf, size_t *size) +{ + struct optee_rng_priv *priv = dev_get_priv(dev); + int ret = 0; + struct tee_invoke_arg arg;
Wouldn't it be preferable to use
struct tee_invoke_arg arg = { 0 }; ?
This way the compiler can optimize the initialization and set all 0 initialized variables with a single memset() call.
I wasn't sure of the portability for struct zero-initialisation.
But I will change it in V2.
Best regards
Heinrich
+ struct tee_param param;
+ memset(&arg, 0, sizeof(arg)); + memset(¶m, 0, sizeof(param));
+ /* Invoke TA_CMD_GET_ENTROPY function of Trusted App */ + arg.func = TA_CMD_GET_ENTROPY; + arg.session = priv->session_id;
+ /* Fill invoke cmd params */ + param.attr = TEE_PARAM_ATTR_TYPE_MEMREF_INOUT; + param.u.memref.shm = entropy_shm_pool; + param.u.memref.size = *size;
+ ret = tee_invoke_func(dev->parent, &arg, 1, ¶m); + if (ret || arg.ret) { + if (!ret) + ret = -EPROTO; + dev_err(dev, "TA_CMD_GET_ENTROPY invoke err: %d 0x%x\n", ret, arg.ret); + *size = 0;
+ return ret; + }
+ memcpy(buf, param.u.memref.shm->addr, param.u.memref.size); + *size = param.u.memref.size;
+ return 0; +}
(...)
Regards
Patrick
participants (3)
-
Heinrich Schuchardt
-
Patrick DELAUNAY
-
Patrick Delaunay