Skip to contentSkip to conversation

Add Versal TRNG driver

PATCH Superseded Harsh Jain 4 participants 16 messages Jun 9, 2025 – Jun 11, 2025

Subsystems (3)

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Harsh Jain

author
#0 lore raw

v2 cover Add Versal TRNG driver

Versal TRNG module consist of array of Ring Oscillators as entropy source and a deterministic CTR_DRBG random bit generator (DRBG). Add driver to registers entropy source to hwrng and CTR_DRBG generator to crypto subsystem. Derivation Function (DF) defined in NIST SP-800-90A for CTR_DRBG is not supported in current TRNG IP. For DF processing, Update drbg module to export CTR_DRBG derivation function.

Changes in v2
- Fixed signoff chain
- Added 3 patch to fix Kernel test robot bugs

Harsh Jain (5):
crypto: xilinx: Add TRNG driver for Versal
crypto: drbg: Export CTR DRBG DF functions
crypto: xilinx: Select dependant Kconfig option for CRYPTO_DRBG_CTR
crypto: xilinx: Fix missing goto in probe
crypto: xilinx: Use min_t macro to compare value

Mounika Botcha (1):
dt-bindings: crypto: Add node for True Random Number Generator

 .../bindings/crypto/xlnx,versal-trng.yaml     |  36 ++
 MAINTAINERS                                   |   6 +
 crypto/drbg.c                                 | 108 +++--
 drivers/crypto/Kconfig                        |  14 +
 drivers/crypto/xilinx/Makefile                |   1 +
 drivers/crypto/xilinx/xilinx-trng.c           | 434 ++++++++++++++++++
 include/crypto/drbg.h                         |  15 +
 7 files changed, 563 insertions(+), 51 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/crypto/xlnx,versal-trng.yaml
 create mode 100644 drivers/crypto/xilinx/xilinx-trng.c
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v2 1/6 dt-bindings: crypto: Add node for True Random Number Generator

From: Mounika Botcha <[email protected]>

Add TRNG node compatible string and reg properities.

---
 .../bindings/crypto/xlnx,versal-trng.yaml     | 36 +++++++++++++++++++
 1 file changed, 36 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/crypto/xlnx,versal-trng.yaml
Documentation/devicetree/bindings/crypto/xlnx,versal-trng.yaml new file +36 -0
new file mode 100644
index 000000000000..b6424eeb5966
@@ -0,0 +1,36 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/crypto/xlnx,versal-rng.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Xilinx Versal True Random Number Generator Hardware Accelerator
+
+maintainers:
+  - Harsh Jain <[email protected]>
+  - Mounika Botcha <[email protected]>
+
+description:
+  The Versal True Random Number Generator consists of Ring Oscillators as
+  entropy source and a deterministic CTR_DRBG random bit generator (DRBG).
+
+properties:
+  compatible:
+    const: xlnx,versal-rng
+
+  reg:
+    maxItems: 1
+
+required:
+  - reg
+
+additionalProperties: false
+
+examples:
+  - |
+    rng@f1230000 {
+        compatible = "xlnx,versal-rng";
+        reg = <0xf1230000 0x1000>;
+    };
+...
+
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v2 1/6] dt-bindings: crypto: Add node for True Random Number Generator

Quoted 11 lines · Harsh Jain

From: Mounika Botcha <[email protected]>

Add TRNG node compatible string and reg properities.

---
 .../bindings/crypto/xlnx,versal-trng.yaml     | 36 +++++++++++++++++++
 1 file changed, 36 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/crypto/xlnx,versal-trng.yaml

My bot found errors running 'make dt_binding_check' on your patch:

yamllint warnings/errors:

dtschema/dtc warnings/errors:
/builds/robherring/dt-review-ci/linux/Documentation/devicetree/bindings/crypto/xlnx,versal-trng.yaml: $id: Cannot determine base path from $id, relative path/filename doesn't match actual path or filename
$id: http://devicetree.org/schemas/crypto/xlnx,versal-rng.yaml
file: /builds/robherring/dt-review-ci/linux/Documentation/devicetree/bindings/crypto/xlnx,versal-trng.yaml

doc reference errors (make refcheckdocs):

See https://patchwork.ozlabs.org/project/devicetree-bindings/patch/[email protected]

The base for the series is generally the latest rc1. A different dependency should be noted in *this* patch.

If you already ran 'make dt_binding_check' and didn't see the above error(s), then make sure 'yamllint' is installed and dt-schema is up to date:

pip3 install dtschema --upgrade

Please check and re-submit after running the above command yourself. Note that DT_SCHEMA_FILES can be set to your schema file to speed up checking your schema. However, it must be unset to test all examples with your schema.

maintainer
#3 lore raw

Re: [PATCH v2 1/6] dt-bindings: crypto: Add node for True Random Number Generator

In reply to Harsh Jain
Quoted 26 lines · Harsh Jain

From: Mounika Botcha <[email protected]>

Add TRNG node compatible string and reg properities.

---
 .../bindings/crypto/xlnx,versal-trng.yaml     | 36 +++++++++++++++++++
 1 file changed, 36 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/crypto/xlnx,versal-trng.yaml
Documentation/devicetree/bindings/crypto/xlnx,versal-trng.yaml new file +9 -0
new file mode 100644
index 000000000000..b6424eeb5966
@@ -0,0 +1,36 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/crypto/xlnx,versal-rng.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Xilinx Versal True Random Number Generator Hardware Accelerator
+
+maintainers:

Documentation/devicetree/bindings/crypto/xlnx,versal-trng.yaml line 19

Quoted 10 lines
Documentation/devicetree/bindings/crypto/xlnx,versal-trng.yaml new file +10 -0
+  - Harsh Jain <[email protected]>
+  - Mounika Botcha <[email protected]>
+
+description:
+  The Versal True Random Number Generator consists of Ring Oscillators as
+  entropy source and a deterministic CTR_DRBG random bit generator (DRBG).
+
+properties:
+  compatible:
+    const: xlnx,versal-rng

I believe the prior comment was only about the node name. If the block is called 'trng' then you should call it that.

And please test your bindings before sending.

Rob

#4 lore raw

RE: [PATCH v2 1/6] dt-bindings: crypto: Add node for True Random Number Generator

[AMD Official Use Only - AMD Internal Distribution Only]

Quoted 59 lines

-----Original Message-----
From: Rob Herring <[email protected]>
Sent: Monday, June 9, 2025 6:45 PM
To: Jain, Harsh (AECG-SSW) <[email protected]>
Cc: [email protected]; [email protected]; linux- [email protected]; [email protected]; Botcha, Mounika <[email protected]>; Savitala, Sarat Chand <[email protected]>; Dhanawade, Mohan <[email protected]>; Simek, Michal <[email protected]>
Subject: Re: [PATCH v2 1/6] dt-bindings: crypto: Add node for True Random Number Generator

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

Quoted 10 lines · Harsh Jain

From: Mounika Botcha <[email protected]>

Add TRNG node compatible string and reg properities.

---
 .../bindings/crypto/xlnx,versal-trng.yaml     | 36 +++++++++++++++++++
 1 file changed, 36 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/crypto/xlnx,versal-

trng.yaml

Review

Quoted 2 lines
(quoted diff) +0 -0

b/Documentation/devicetree/bindings/crypto/xlnx,versal-trng.yaml

Quoted 14 lines

new file mode 100644
index 000000000000..b6424eeb5966

Documentation/devicetree/bindings/crypto/xlnx,versal-trng.yaml new file +9 -0
@@ -0,0 +1,36 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/crypto/xlnx,versal-rng.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Xilinx Versal True Random Number Generator Hardware Accelerator
+
+maintainers:

Documentation/devicetree/bindings/crypto/xlnx,versal-trng.yaml line 19

Quoted 10 lines
Documentation/devicetree/bindings/crypto/xlnx,versal-trng.yaml new file +10 -0
+  - Harsh Jain <[email protected]>
+  - Mounika Botcha <[email protected]>
+
+description:
+  The Versal True Random Number Generator consists of Ring Oscillators as
+  entropy source and a deterministic CTR_DRBG random bit generator (DRBG).
+
+properties:
+  compatible:
+    const: xlnx,versal-rng

I believe the prior comment was only about the node name. If the block is called 'trng' then you should call it that.

Quoted 1 line

And please test your bindings before sending.

Thanks Rob, Will fix and test.

Quoted 2 lines

Rob

author
#5 lore raw

PATCH v2 2/6 crypto: xilinx: Add TRNG driver for Versal

In reply to Harsh Jain

Add True Random Number Generator(TRNG) driver for Versal platform.

---
 MAINTAINERS                         |   6 +
 drivers/crypto/Kconfig              |  12 +
 drivers/crypto/xilinx/Makefile      |   1 +
 drivers/crypto/xilinx/xilinx-trng.c | 408 ++++++++++++++++++++++++++++
 4 files changed, 427 insertions(+)
 create mode 100644 drivers/crypto/xilinx/xilinx-trng.c
MAINTAINERS +6 -0
index 85f7619e06b6..6995a0017a35 100644
@@ -26578,6 +26578,12 @@ F:	drivers/misc/Makefile
 F:	drivers/misc/xilinx_sdfec.c
 F:	include/uapi/misc/xilinx_sdfec.h
 
+XILINX TRNG DRIVER
+M:	Mounika Botcha <[email protected]>
+M:	Harsh Jain <[email protected]>
+S:	Maintained
+F:	drivers/crypto/xilinx/xilinx-trng.c
+
 XILINX UARTLITE SERIAL DRIVER
 M:	Peter Korsgaard <[email protected]>
 L:	[email protected]
drivers/crypto/Kconfig +12 -0
index 5686369779be..209720b42ec6 100644
@@ -711,6 +711,18 @@ config CRYPTO_DEV_TEGRA
 	  Select this to enable Tegra Security Engine which accelerates various
 	  AES encryption/decryption and HASH algorithms.
 
+config CRYPTO_DEV_XILINX_TRNG
+	tristate "Support for Xilinx True Random Generator"
+	depends on ZYNQMP_FIRMWARE || COMPILE_TEST
+	select CRYPTO_RNG
+	select HW_RANDOM
+	help
+	  Xilinx Versal SoC driver provides kernel-side support for True Random Number
+	  Generator and Pseudo random Number in CTR_DRBG mode as defined in NIST SP800-90A.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called xilinx-trng.
+
 config CRYPTO_DEV_ZYNQMP_AES
 	tristate "Support for Xilinx ZynqMP AES hw accelerator"
 	depends on ZYNQMP_FIRMWARE || COMPILE_TEST
drivers/crypto/xilinx/Makefile +1 -0
index 730feff5b5f2..9b51636ef75e 100644
@@ -1,3 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0-only
+obj-$(CONFIG_CRYPTO_DEV_XILINX_TRNG) += xilinx-trng.o
 obj-$(CONFIG_CRYPTO_DEV_ZYNQMP_AES) += zynqmp-aes-gcm.o
 obj-$(CONFIG_CRYPTO_DEV_ZYNQMP_SHA3) += zynqmp-sha.o
drivers/crypto/xilinx/xilinx-trng.c new file +408 -0

408 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#6 lore raw

PATCH v2 3/6 crypto: drbg: Export CTR DRBG DF functions

In reply to Harsh Jain

Export drbg_ctr_df() derivative function to re-use it in xilinx trng driver. Changes has been tested by enabling CONFIG_CRYPTO_USER_API_RNG_CAVP

---
 crypto/drbg.c                       | 108 +++++++++++++++-------------
 drivers/crypto/Kconfig              |   1 +
 drivers/crypto/xilinx/xilinx-trng.c |  30 +++++++-
 include/crypto/drbg.h               |  15 ++++
 4 files changed, 101 insertions(+), 53 deletions(-)
crypto/drbg.c +57 -51
index dbe4c8bb5ceb..322c630c54b8 100644
@@ -294,10 +294,11 @@ MODULE_ALIAS_CRYPTO("drbg_nopr_ctr_aes192");
 MODULE_ALIAS_CRYPTO("drbg_pr_ctr_aes128");
 MODULE_ALIAS_CRYPTO("drbg_nopr_ctr_aes128");
 
-static void drbg_kcapi_symsetkey(struct drbg_state *drbg,
-				 const unsigned char *key);
-static int drbg_kcapi_sym(struct drbg_state *drbg, unsigned char *outval,
-			  const struct drbg_string *in);
+static void drbg_kcapi_symsetkey(struct crypto_cipher *tfm,
+				 const unsigned char *key,
+				 u8 keylen);
+static int drbg_kcapi_sym(struct crypto_cipher *tfm, unsigned char *outval,
+			  const struct drbg_string *in, u8 blocklen_bytes);
 static int drbg_init_sym_kernel(struct drbg_state *drbg);
 static int drbg_fini_sym_kernel(struct drbg_state *drbg);
 static int drbg_kcapi_sym_ctr(struct drbg_state *drbg,
@@ -306,28 +307,31 @@ static int drbg_kcapi_sym_ctr(struct drbg_state *drbg,
 #define DRBG_OUTSCRATCHLEN 256
 
 /* BCC function for CTR DRBG as defined in 10.4.3 */
-static int drbg_ctr_bcc(struct drbg_state *drbg,
+
+static int drbg_ctr_bcc(struct crypto_cipher *tfm,
 			unsigned char *out, const unsigned char *key,
-			struct list_head *in)
+			struct list_head *in,
+			u8 blocklen_bytes,
+			u8 keylen)
 {
 	int ret = 0;
 	struct drbg_string *curr = NULL;
 	struct drbg_string data;
 	short cnt = 0;
 
-	drbg_string_fill(&data, out, drbg_blocklen(drbg));
+	drbg_string_fill(&data, out, blocklen_bytes);
 
 	/* 10.4.3 step 2 / 4 */
-	drbg_kcapi_symsetkey(drbg, key);
+	drbg_kcapi_symsetkey(tfm, key, keylen);
 	list_for_each_entry(curr, in, list) {
 		const unsigned char *pos = curr->buf;
 		size_t len = curr->len;
 		/* 10.4.3 step 4.1 */
 		while (len) {
 			/* 10.4.3 step 4.2 */
-			if (drbg_blocklen(drbg) == cnt) {
+			if (blocklen_bytes == cnt) {
 				cnt = 0;
-				ret = drbg_kcapi_sym(drbg, out, &data);
+				ret = drbg_kcapi_sym(tfm, out, &data, blocklen_bytes);
 				if (ret)
 					return ret;
 			}
@@ -339,13 +343,13 @@ static int drbg_ctr_bcc(struct drbg_state *drbg,
 	}
 	/* 10.4.3 step 4.2 for last block */
 	if (cnt)
-		ret = drbg_kcapi_sym(drbg, out, &data);
+		ret = drbg_kcapi_sym(tfm, out, &data, blocklen_bytes);
 
 	return ret;
 }
 
 /*
- * scratchpad usage: drbg_ctr_update is interlinked with drbg_ctr_df
+ * scratchpad usage: drbg_ctr_update is interlinked with crypto_drbg_ctr_df
  * (and drbg_ctr_bcc, but this function does not need any temporary buffers),
  * the scratchpad is used as follows:
  * drbg_ctr_update:
@@ -362,7 +366,7 @@ static int drbg_ctr_bcc(struct drbg_state *drbg,
  *				drbg_statelen(drbg) + drbg_blocklen(drbg)
  *		length: drbg_statelen(drbg)
  *
- * drbg_ctr_df:
+ * crypto_drbg_ctr_df:
  *	pad
  *		start: df_data + drbg_statelen(drbg)
  *		length: drbg_blocklen(drbg)
@@ -381,21 +385,24 @@ static int drbg_ctr_bcc(struct drbg_state *drbg,
  *			the final output of all BCC rounds are truncated).
  *			Therefore, add drbg_blocklen(drbg) to cover all
  *			possibilities.
+ * refer to crypto_drbg_ctr_df_datalen() to get required length
  */
 
 /* Derivation Function for CTR DRBG as defined in 10.4.2 */
-static int drbg_ctr_df(struct drbg_state *drbg,
+int crypto_drbg_ctr_df(struct crypto_cipher *tfm,
 		       unsigned char *df_data, size_t bytes_to_return,
-		       struct list_head *seedlist)
+		       struct list_head *seedlist,
+		       u8 blocklen_bytes,
+		       u8 statelen)
 {
 	int ret = -EFAULT;
 	unsigned char L_N[8];
 	/* S3 is input */
 	struct drbg_string S1, S2, S4, cipherin;
 	LIST_HEAD(bcc_list);
-	unsigned char *pad = df_data + drbg_statelen(drbg);
-	unsigned char *iv = pad + drbg_blocklen(drbg);
-	unsigned char *temp = iv + drbg_blocklen(drbg);
+	unsigned char *pad = df_data + statelen;
+	unsigned char *iv = pad + blocklen_bytes;
+	unsigned char *temp = iv + blocklen_bytes;
 	size_t padlen = 0;
 	unsigned int templen = 0;
 	/* 10.4.2 step 7 */
@@ -410,10 +417,11 @@ static int drbg_ctr_df(struct drbg_state *drbg,
 	size_t generated_len = 0;
 	size_t inputlen = 0;
 	struct drbg_string *seed = NULL;
+	u8 keylen;
 
-	memset(pad, 0, drbg_blocklen(drbg));
-	memset(iv, 0, drbg_blocklen(drbg));
-
+	memset(pad, 0, blocklen_bytes);
+	memset(iv, 0, blocklen_bytes);
+	keylen = statelen - blocklen_bytes;
 	/* 10.4.2 step 1 is implicit as we work byte-wise */
 
 	/* 10.4.2 step 2 */
@@ -429,10 +437,10 @@ static int drbg_ctr_df(struct drbg_state *drbg,
 	drbg_cpu_to_be32(bytes_to_return, &L_N[4]);
 
 	/* 10.4.2 step 5: length is L_N, input_string, one byte, padding */
-	padlen = (inputlen + sizeof(L_N) + 1) % (drbg_blocklen(drbg));
+	padlen = (inputlen + sizeof(L_N) + 1) % (blocklen_bytes);
 	/* wrap the padlen appropriately */
 	if (padlen)
-		padlen = drbg_blocklen(drbg) - padlen;
+		padlen = blocklen_bytes - padlen;
 	/*
 	 * pad / padlen contains the 0x80 byte and the following zero bytes.
 	 * As the calculated padlen value only covers the number of zero
@@ -442,7 +450,7 @@ static int drbg_ctr_df(struct drbg_state *drbg,
 	pad[0] = 0x80;
 
 	/* 10.4.2 step 4 -- first fill the linked list and then order it */
-	drbg_string_fill(&S1, iv, drbg_blocklen(drbg));
+	drbg_string_fill(&S1, iv, blocklen_bytes);
 	list_add_tail(&S1.list, &bcc_list);
 	drbg_string_fill(&S2, L_N, sizeof(L_N));
 	list_add_tail(&S2.list, &bcc_list);
@@ -451,7 +459,7 @@ static int drbg_ctr_df(struct drbg_state *drbg,
 	list_add_tail(&S4.list, &bcc_list);
 
 	/* 10.4.2 step 9 */
-	while (templen < (drbg_keylen(drbg) + (drbg_blocklen(drbg)))) {
+	while (templen < (keylen + (blocklen_bytes))) {
 		/*
 		 * 10.4.2 step 9.1 - the padding is implicit as the buffer
 		 * holds zeros after allocation -- even the increment of i
@@ -459,22 +467,23 @@ static int drbg_ctr_df(struct drbg_state *drbg,
 		 */
 		drbg_cpu_to_be32(i, iv);
 		/* 10.4.2 step 9.2 -- BCC and concatenation with temp */
-		ret = drbg_ctr_bcc(drbg, temp + templen, K, &bcc_list);
+		ret = drbg_ctr_bcc(tfm, temp + templen, K, &bcc_list,
+				   blocklen_bytes, keylen);
 		if (ret)
 			goto out;
 		/* 10.4.2 step 9.3 */
 		i++;
-		templen += drbg_blocklen(drbg);
+		templen += blocklen_bytes;
 	}
 
 	/* 10.4.2 step 11 */
-	X = temp + (drbg_keylen(drbg));
-	drbg_string_fill(&cipherin, X, drbg_blocklen(drbg));
+	X = temp + (keylen);
+	drbg_string_fill(&cipherin, X, blocklen_bytes);
 
 	/* 10.4.2 step 12: overwriting of outval is implemented in next step */
 
 	/* 10.4.2 step 13 */
-	drbg_kcapi_symsetkey(drbg, temp);
+	drbg_kcapi_symsetkey(tfm, temp, keylen);
 	while (generated_len < bytes_to_return) {
 		short blocklen = 0;
 		/*
@@ -482,12 +491,12 @@ static int drbg_ctr_df(struct drbg_state *drbg,
 		 * implicit as the key is only drbg_blocklen in size based on
 		 * the implementation of the cipher function callback
 		 */
-		ret = drbg_kcapi_sym(drbg, X, &cipherin);
+		ret = drbg_kcapi_sym(tfm, X, &cipherin, blocklen_bytes);
 		if (ret)
 			goto out;
-		blocklen = (drbg_blocklen(drbg) <
+		blocklen = (blocklen_bytes <
 				(bytes_to_return - generated_len)) ?
-			    drbg_blocklen(drbg) :
+			    blocklen_bytes :
 				(bytes_to_return - generated_len);
 		/* 10.4.2 step 13.2 and 14 */
 		memcpy(df_data + generated_len, X, blocklen);
@@ -497,11 +506,12 @@ static int drbg_ctr_df(struct drbg_state *drbg,
 	ret = 0;
 
 out:
-	memset(iv, 0, drbg_blocklen(drbg));
-	memset(temp, 0, drbg_statelen(drbg) + drbg_blocklen(drbg));
-	memset(pad, 0, drbg_blocklen(drbg));
+	memset(iv, 0, blocklen_bytes);
+	memset(temp, 0, statelen + blocklen_bytes);
+	memset(pad, 0, blocklen_bytes);
 	return ret;
 }
+EXPORT_SYMBOL_GPL(crypto_drbg_ctr_df);
 
 /*
  * update function of CTR DRBG as defined in 10.2.1.2
@@ -548,7 +558,9 @@ static int drbg_ctr_update(struct drbg_state *drbg, struct list_head *seed,
 
 	/* 10.2.1.3.2 step 2 and 10.2.1.4.2 step 2 */
 	if (seed) {
-		ret = drbg_ctr_df(drbg, df_data, drbg_statelen(drbg), seed);
+		ret = crypto_drbg_ctr_df(drbg->priv_data, df_data, drbg_statelen(drbg), seed,
+					 drbg_blocklen(drbg),
+					 drbg_statelen(drbg));
 		if (ret)
 			goto out;
 	}
@@ -1310,10 +1322,8 @@ static inline int drbg_alloc_state(struct drbg_state *drbg)
 		sb_size = 0;
 	else if (drbg->core->flags & DRBG_CTR)
 		sb_size = drbg_statelen(drbg) + drbg_blocklen(drbg) + /* temp */
-			  drbg_statelen(drbg) +	/* df_data */
-			  drbg_blocklen(drbg) +	/* pad */
-			  drbg_blocklen(drbg) +	/* iv */
-			  drbg_statelen(drbg) + drbg_blocklen(drbg); /* temp */
+			  crypto_drbg_ctr_df_datalen(drbg_statelen(drbg),
+						     drbg_blocklen(drbg));
 	else
 		sb_size = drbg_statelen(drbg) + drbg_blocklen(drbg);
 
@@ -1800,21 +1810,17 @@ static int drbg_init_sym_kernel(struct drbg_state *drbg)
 	return alignmask;
 }
 
-static void drbg_kcapi_symsetkey(struct drbg_state *drbg,
-				 const unsigned char *key)
+static void drbg_kcapi_symsetkey(struct crypto_cipher *tfm,
+				 const unsigned char *key, u8 keylen)
 {
-	struct crypto_cipher *tfm = drbg->priv_data;
-
-	crypto_cipher_setkey(tfm, key, (drbg_keylen(drbg)));
+	crypto_cipher_setkey(tfm, key, keylen);
 }
 
-static int drbg_kcapi_sym(struct drbg_state *drbg, unsigned char *outval,
-			  const struct drbg_string *in)
+static int drbg_kcapi_sym(struct crypto_cipher *tfm, unsigned char *outval,
+			  const struct drbg_string *in, u8 blocklen_bytes)
 {
-	struct crypto_cipher *tfm = drbg->priv_data;
-
 	/* there is only component in *in */
-	BUG_ON(in->len < drbg_blocklen(drbg));
+	BUG_ON(in->len < blocklen_bytes);
 	crypto_cipher_encrypt_one(tfm, outval, in->buf);
 	return 0;
 }
drivers/crypto/Kconfig +1 -0
index 209720b42ec6..1bbac5298ffa 100644
@@ -714,6 +714,7 @@ config CRYPTO_DEV_TEGRA
 config CRYPTO_DEV_XILINX_TRNG
 	tristate "Support for Xilinx True Random Generator"
 	depends on ZYNQMP_FIRMWARE || COMPILE_TEST
+	select CRYPTO_DRBG_CTR
 	select CRYPTO_RNG
 	select HW_RANDOM
 	help
drivers/crypto/xilinx/xilinx-trng.c +28 -2
index 3b861d1a8a8d..adaf69c58647 100644
@@ -57,6 +57,8 @@
 struct xilinx_rng {
 	void __iomem *rng_base;
 	struct device *dev;
+	unsigned char *scratchpadbuf;
+	struct crypto_cipher *tfm;
 	struct mutex lock;	/* Protect access to TRNG device */
 	struct hwrng trng;
 };
@@ -195,9 +197,14 @@ static int xtrng_reseed_internal(struct xilinx_rng *rng)
 	ret = xtrng_collect_random_data(rng, entropy, TRNG_SEED_LEN_BYTES, true);
 	if (ret != TRNG_SEED_LEN_BYTES)
 		return -EINVAL;
+	ret = crypto_drbg_ctr_df(rng->tfm, rng->scratchpadbuf,
+				 TRNG_SEED_LEN_BYTES, &seedlist, AES_BLOCK_SIZE,
+				 TRNG_SEED_LEN_BYTES);
+	if (ret)
+		return ret;
 
 	xtrng_write_multiple_registers(rng->rng_base + TRNG_EXT_SEED_OFFSET,
-				       (u32 *)entropy, TRNG_NUM_INIT_REGS);
+				       (u32 *)rng->scratchpadbuf, TRNG_NUM_INIT_REGS);
 	/* select reseed operation */
 	iowrite32(TRNG_CTRL_PRNGXS_MASK, rng->rng_base + TRNG_CTRL_OFFSET);
 
@@ -327,6 +334,7 @@ static void xtrng_hwrng_unregister(struct hwrng *trng)
 static int xtrng_probe(struct platform_device *pdev)
 {
 	struct xilinx_rng *rng;
+	size_t sb_size;
 	int ret;
 
 	rng = devm_kzalloc(&pdev->dev, sizeof(*rng), GFP_KERNEL);
@@ -340,6 +348,19 @@ static int xtrng_probe(struct platform_device *pdev)
 		return PTR_ERR(rng->rng_base);
 	}
 
+	rng->tfm = crypto_alloc_cipher("aes", 0, 0);
+	if (IS_ERR(rng->tfm)) {
+		pr_info("DRBG: could not allocate cipher TFM handle:\n");
+		return PTR_ERR(rng->tfm);
+	}
+
+	sb_size = crypto_drbg_ctr_df_datalen(TRNG_SEED_LEN_BYTES, AES_BLOCK_SIZE);
+	rng->scratchpadbuf = devm_kzalloc(&pdev->dev, sb_size, GFP_KERNEL);
+	if (!rng->scratchpadbuf) {
+		ret = -ENOMEM;
+		goto cipher_cleanup;
+	}
+
 	xtrng_trng_reset(rng->rng_base);
 	ret = xtrng_reseed_internal(rng);
 	if (ret) {
@@ -352,8 +373,9 @@ static int xtrng_probe(struct platform_device *pdev)
 	ret = crypto_register_rng(&xtrng_trng_alg);
 	if (ret) {
 		dev_err(&pdev->dev, "Crypto Random device registration failed: %d\n", ret);
-		return ret;
+		goto cipher_cleanup;
 	}
+
 	ret = xtrng_hwrng_register(&rng->trng);
 	if (ret) {
 		dev_err(&pdev->dev, "HWRNG device registration failed: %d\n", ret);
@@ -366,6 +388,9 @@ static int xtrng_probe(struct platform_device *pdev)
 crypto_rng_free:
 	crypto_unregister_rng(&xtrng_trng_alg);
 
+cipher_cleanup:
+	crypto_free_cipher(rng->tfm);
+
 	return ret;
 }
 
@@ -377,6 +402,7 @@ static void xtrng_remove(struct platform_device *pdev)
 	rng = platform_get_drvdata(pdev);
 	xtrng_hwrng_unregister(&rng->trng);
 	crypto_unregister_rng(&xtrng_trng_alg);
+	crypto_free_cipher(rng->tfm);
 	xtrng_write_multiple_registers(rng->rng_base + TRNG_EXT_SEED_OFFSET, zero,
 				       TRNG_NUM_INIT_REGS);
 	xtrng_write_multiple_registers(rng->rng_base + TRNG_PER_STRNG_OFFSET, zero,
include/crypto/drbg.h +15 -0
index af5ad51d3eef..978a4a0c34e0 100644
@@ -53,6 +53,7 @@
 #include <linux/mutex.h>
 #include <linux/list.h>
 #include <linux/workqueue.h>
+#include <crypto/internal/cipher.h>
 
 /*
  * Concatenation Helper and string operation helper
@@ -264,6 +265,14 @@ static inline int crypto_drbg_reset_test(struct crypto_rng *drng,
 	return crypto_rng_reset(drng, pers->buf, pers->len);
 }
 
+static inline int crypto_drbg_ctr_df_datalen(u8 statelen, u8 blocklen)
+{
+	return statelen +	/* df_data */
+		blocklen +	/* pad */
+		blocklen +	/* iv */
+		statelen + blocklen;  /* temp */
+}
+
 /* DRBG type flags */
 #define DRBG_CTR	((drbg_flag_t)1<<0)
 #define DRBG_HMAC	((drbg_flag_t)1<<1)
@@ -283,4 +292,10 @@ enum drbg_prefixes {
 	DRBG_PREFIX3
 };
 
+int crypto_drbg_ctr_df(struct crypto_cipher *tfm,
+		       unsigned char *df_data, size_t bytes_to_return,
+		       struct list_head *seedlist,
+		       u8 blocklen_bytes,
+		       u8 statelen);
+
 #endif /* _DRBG_H */
Signature · 1 line
2.34.1
author
#7 lore raw

PATCH v2 4/6 crypto: xilinx: Select dependant Kconfig option for CRYPTO_DRBG_CTR

In reply to Harsh Jain

CRYPTO_DRBG_MENU Kconfig option is dependant on CRYPTO_DRBG_CTR. Select CRYPTO_DRBG_MENU to fix warning reported by kernel test robot.

---
 drivers/crypto/Kconfig | 1 +
 1 file changed, 1 insertion(+)
drivers/crypto/Kconfig +1 -0
index 1bbac5298ffa..cfbc06451fec 100644
@@ -714,6 +714,7 @@ config CRYPTO_DEV_TEGRA
 config CRYPTO_DEV_XILINX_TRNG
 	tristate "Support for Xilinx True Random Generator"
 	depends on ZYNQMP_FIRMWARE || COMPILE_TEST
+	select CRYPTO_DRBG_MENU
 	select CRYPTO_DRBG_CTR
 	select CRYPTO_RNG
 	select HW_RANDOM
Signature · 1 line
2.34.1
#8 lore raw

Re: [PATCH v2 4/6] crypto: xilinx: Select dependant Kconfig option for CRYPTO_DRBG_CTR

Quoted 3 lines · Harsh Jain

CRYPTO_DRBG_MENU Kconfig option is dependant on CRYPTO_DRBG_CTR. Select CRYPTO_DRBG_MENU to fix warning reported by kernel test robot.

Fixes?

If not, why is this a separate commit? Are you adding known bug in the same patchset just to fix it later?

Quoted 2 lines

That's report for your patch!

Quoted 1 line

Best regards,
Krzysztof

#9 lore raw

RE: [PATCH v2 4/6] crypto: xilinx: Select dependant Kconfig option for CRYPTO_DRBG_CTR

[AMD Official Use Only - AMD Internal Distribution Only]

Quoted 17 lines

-----Original Message-----
From: Krzysztof Kozlowski <[email protected]>
Sent: Tuesday, June 10, 2025 11:48 AM
To: Jain, Harsh (AECG-SSW) <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; Botcha, Mounika <[email protected]>; Savitala, Sarat Chand <[email protected]>; Dhanawade, Mohan <[email protected]>; Simek, Michal <[email protected]>
Cc: kernel test robot <[email protected]>
Subject: Re: [PATCH v2 4/6] crypto: xilinx: Select dependant Kconfig option for CRYPTO_DRBG_CTR

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

On 09/06/2025 06:51, Harsh Jain wrote:

Quoted 8 lines
Quoted 3 lines

CRYPTO_DRBG_MENU Kconfig option is dependant on CRYPTO_DRBG_CTR. Select CRYPTO_DRBG_MENU to fix warning reported by kernel test robot.

Fixes?

If not, why is this a separate commit? Are you adding known bug in the same patchset just to fix it later?

I added this fix patches to have Reported-by tags for " kernel test robot". Will squash these fixes to original patch.

Quoted 13 lines
Quoted 3 lines

[email protected]/

That's report for your patch!

Quoted 1 line

Best regards,
Krzysztof

author
#10 lore raw

PATCH v2 5/6 crypto: xilinx: Fix missing goto in probe

In reply to Harsh Jain

Add goto to clean up allocated cipher on reseed failure.

---
 drivers/crypto/xilinx/xilinx-trng.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/crypto/xilinx/xilinx-trng.c +1 -1
index adaf69c58647..8ec0f83b53f1 100644
@@ -365,7 +365,7 @@ static int xtrng_probe(struct platform_device *pdev)
 	ret = xtrng_reseed_internal(rng);
 	if (ret) {
 		dev_err(&pdev->dev, "TRNG Seed fail\n");
-		return ret;
+		goto cipher_cleanup;
 	}
 
 	xilinx_rng_dev = rng;
Signature · 1 line
2.34.1
#12 lore raw

RE: [PATCH v2 5/6] crypto: xilinx: Fix missing goto in probe

[AMD Official Use Only - AMD Internal Distribution Only]

Quoted 15 lines

-----Original Message-----
From: Krzysztof Kozlowski <[email protected]>
Sent: Tuesday, June 10, 2025 11:48 AM
To: Jain, Harsh (AECG-SSW) <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; Botcha, Mounika <[email protected]>; Savitala, Sarat Chand <[email protected]>; Dhanawade, Mohan <[email protected]>; Simek, Michal <[email protected]>
Cc: kernel test robot <[email protected]>; Dan Carpenter <[email protected]>
Subject: Re: [PATCH v2 5/6] crypto: xilinx: Fix missing goto in probe

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

Quoted 8 lines
Quoted 5 lines · Harsh Jain

Add goto to clean up allocated cipher on reseed failure.

Please stop adding bugs and fixing them afterwards. Fix your patch first.

Hi Kozlowski,

After squashing this fix, Do I need to add "Reported-by, Closes" tag in original patch?

Thanks

Quoted 5 lines

Best regards,
Krzysztof

#13 lore raw

Re: [PATCH v2 5/6] crypto: xilinx: Fix missing goto in probe

Quoted 24 lines · Harsh (AECG-SSW)

[AMD Official Use Only - AMD Internal Distribution Only]

Quoted 22 lines

-----Original Message-----
From: Krzysztof Kozlowski <[email protected]>
Sent: Tuesday, June 10, 2025 11:48 AM
To: Jain, Harsh (AECG-SSW) <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; Botcha, Mounika <[email protected]>; Savitala, Sarat Chand <[email protected]>; Dhanawade, Mohan <[email protected]>; Simek, Michal <[email protected]>
Cc: kernel test robot <[email protected]>; Dan Carpenter <[email protected]>
Subject: Re: [PATCH v2 5/6] crypto: xilinx: Fix missing goto in probe

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

Quoted 5 lines · Harsh Jain

Add goto to clean up allocated cipher on reseed failure.

Quoted 6 lines
Quoted 1 line

Please stop adding bugs and fixing them afterwards. Fix your patch first.

Hi Kozlowski,

After squashing this fix, Do I need to add "Reported-by, Closes" tag in original patch?

Nope.

regards,
dan carpenter

author
#14 lore raw

PATCH v2 6/6 crypto: xilinx: Use min_t macro to compare value

In reply to Harsh Jain

Fix signedness error reported by kernel test robot.

---
 drivers/crypto/xilinx/xilinx-trng.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/crypto/xilinx/xilinx-trng.c +2 -2
index 8ec0f83b53f1..4edcaf911475 100644
@@ -304,8 +304,8 @@ static int xtrng_hwrng_trng_read(struct hwrng *hwrng, void *data, size_t max, bo
 		if (ret < 0)
 			break;
 
-		memcpy(data + i, buf, min(ret, (max - i)));
-		i += min(ret, (max - i));
+		memcpy(data + i, buf, min_t(int, ret, (max - i)));
+		i += min_t(int, ret, (max - i));
 	}
 	mutex_unlock(&rng->lock);
 
Signature · 1 line
2.34.1