Skip to contentSkip to conversation

crypto: qat - add improvements to FW loader

PATCH In review Giovanni Cabiddu 2 participants 5 messages Mar 14, 2025 – Mar 21, 2025

Subsystems (2)
lore
Share & tools
lore

Giovanni Cabiddu

author maintainer
#0 lore raw

cover crypto: qat - add improvements to FW loader

This small set of patches adds some minor improvements to the QAT firmware loader.

- Patch #1 removes some unused members in a structure that are not used for doing the firmware authentication.
- Patch #2 removes a redundant check on the size of the firmware image.
- Patch #3 simplifies the allocations for the firmware authentication an introduces an additional check to ensure that the allocated memory meets the requirements of the authentication firmware.

Jack Xu (3):
crypto: qat - remove unused members in suof structure
crypto: qat - remove redundant FW image size check
crypto: qat - optimize allocations for fw authentication

 .../intel/qat/qat_common/icp_qat_uclo.h       | 10 -----
 .../crypto/intel/qat/qat_common/qat_uclo.c    | 38 ++++++++++---------
 2 files changed, 21 insertions(+), 27 deletions(-)
Signature · 1 line
2.48.1
author maintainer
#1 lore raw

PATCH 1/3 crypto: qat - remove unused members in suof structure

From: Jack Xu <[email protected]>

Remove the members css_key and css_signature which are not used for doing the firmware authentication.

The signed image pointer can be calculated using the pointer to the CSS header and the length of the CSS header, making these members redundant.

---
 drivers/crypto/intel/qat/qat_common/icp_qat_uclo.h | 2 --
 drivers/crypto/intel/qat/qat_common/qat_uclo.c     | 9 ++-------
 2 files changed, 2 insertions(+), 9 deletions(-)
drivers/crypto/intel/qat/qat_common/icp_qat_uclo.h +0 -2
index e28241bdd0f4..4b5e7dcd11d1 100644
@@ -404,8 +404,6 @@ struct icp_qat_suof_img_hdr {
 	char          *simg_buf;
 	unsigned long simg_len;
 	char          *css_header;
-	char          *css_key;
-	char          *css_signature;
 	char          *css_simg;
 	unsigned long simg_size;
 	unsigned int  ae_num;
drivers/crypto/intel/qat/qat_common/qat_uclo.c +2 -7
index 7ea40b4f6e5b..5ace036addec 100644
@@ -1064,6 +1064,7 @@ static void qat_uclo_map_simg(struct icp_qat_fw_loader_handle *handle,
 			      struct icp_qat_suof_chunk_hdr *suof_chunk_hdr)
 {
 	struct icp_qat_suof_handle *suof_handle = handle->sobj_handle;
+	unsigned int offset = ICP_QAT_AE_IMG_OFFSET(handle);
 	struct icp_qat_simg_ae_mode *ae_mode;
 	struct icp_qat_suof_objhdr *suof_objhdr;
 
@@ -1075,13 +1076,7 @@ static void qat_uclo_map_simg(struct icp_qat_fw_loader_handle *handle,
 				   suof_chunk_hdr->offset))->img_length;
 
 	suof_img_hdr->css_header = suof_img_hdr->simg_buf;
-	suof_img_hdr->css_key = (suof_img_hdr->css_header +
-				 sizeof(struct icp_qat_css_hdr));
-	suof_img_hdr->css_signature = suof_img_hdr->css_key +
-				      ICP_QAT_CSS_FWSK_MODULUS_LEN(handle) +
-				      ICP_QAT_CSS_FWSK_EXPONENT_LEN(handle);
-	suof_img_hdr->css_simg = suof_img_hdr->css_signature +
-				 ICP_QAT_CSS_SIGNATURE_LEN(handle);
+	suof_img_hdr->css_simg = suof_img_hdr->css_header + offset;
 
 	ae_mode = (struct icp_qat_simg_ae_mode *)(suof_img_hdr->css_simg);
 	suof_img_hdr->ae_mask = ae_mode->ae_mask;
Signature · 1 line
2.48.1
author maintainer
#2 lore raw

PATCH 2/3 crypto: qat - remove redundant FW image size check

In reply to Giovanni Cabiddu

From: Jack Xu <[email protected]>

The FW image size check is already performed in the function qat_uclo_check_image() before calling qat_uclo_map_auth_fw(). Therefore, the additional check in qat_uclo_map_auth_fw() is redundant and can be safely removed.

---
 drivers/crypto/intel/qat/qat_common/qat_uclo.c | 4 ----
 1 file changed, 4 deletions(-)
drivers/crypto/intel/qat/qat_common/qat_uclo.c +0 -4
index 5ace036addec..61be6df50684 100644
@@ -1418,10 +1418,6 @@ static int qat_uclo_map_auth_fw(struct icp_qat_fw_loader_handle *handle,
 	struct icp_qat_simg_ae_mode *simg_ae_mode;
 	struct icp_firml_dram_desc img_desc;
 
-	if (size > (ICP_QAT_AE_IMG_OFFSET(handle) + ICP_QAT_CSS_RSA4K_MAX_IMAGE_LEN)) {
-		pr_err("QAT: error, input image size overflow %d\n", size);
-		return -EINVAL;
-	}
 	length = (css_hdr->fw_type == CSS_AE_FIRMWARE) ?
 		 ICP_QAT_CSS_AE_SIMG_LEN(handle) + simg_offset :
 		 size + ICP_QAT_CSS_FWSK_PAD_LEN(handle) + simg_offset;
Signature · 1 line
2.48.1
author maintainer
#3 lore raw

PATCH 3/3 crypto: qat - optimize allocations for fw authentication

In reply to Giovanni Cabiddu

From: Jack Xu <[email protected]>

The memory requested to hold the image data for authentication will never exceed ICP_QAT_CSS_RSA4K_MAX_IMAGE_LEN. Therefore, we can simplify the allocation by always requesting the maximum size needed for any image.

Also introduce the following checks:
* Ensure the allocated memory is 8-byte aligned to meet the requirements of the authentication firmware.
* Prevent overflow when constructing the authentication descriptor.

---
 .../intel/qat/qat_common/icp_qat_uclo.h       |  8 ------
 .../crypto/intel/qat/qat_common/qat_uclo.c    | 25 ++++++++++++++-----
 2 files changed, 19 insertions(+), 14 deletions(-)
drivers/crypto/intel/qat/qat_common/icp_qat_uclo.h +0 -8
index 4b5e7dcd11d1..1c7bcd8e4055 100644
@@ -43,7 +43,6 @@
 #define ICP_QAT_SUOF_OBJS "SUF_OBJS"
 #define ICP_QAT_SUOF_IMAG "SUF_IMAG"
 #define ICP_QAT_SIMG_AE_INIT_SEQ_LEN    (50 * sizeof(unsigned long long))
-#define ICP_QAT_SIMG_AE_INSTS_LEN       (0x4000 * sizeof(unsigned long long))
 
 #define DSS_FWSK_MODULUS_LEN    384 /* RSA3K */
 #define DSS_FWSK_EXPONENT_LEN   4
@@ -75,13 +74,6 @@
 						DSS_SIGNATURE_LEN : \
 						CSS_SIGNATURE_LEN)
 
-#define ICP_QAT_CSS_AE_IMG_LEN     (sizeof(struct icp_qat_simg_ae_mode) + \
-				    ICP_QAT_SIMG_AE_INIT_SEQ_LEN +         \
-				    ICP_QAT_SIMG_AE_INSTS_LEN)
-#define ICP_QAT_CSS_AE_SIMG_LEN(handle) (sizeof(struct icp_qat_css_hdr) + \
-					ICP_QAT_CSS_FWSK_PUB_LEN(handle) + \
-					ICP_QAT_CSS_SIGNATURE_LEN(handle) + \
-					ICP_QAT_CSS_AE_IMG_LEN)
 #define ICP_QAT_AE_IMG_OFFSET(handle) (sizeof(struct icp_qat_css_hdr) + \
 					ICP_QAT_CSS_FWSK_MODULUS_LEN(handle) + \
 					ICP_QAT_CSS_FWSK_EXPONENT_LEN(handle) + \
drivers/crypto/intel/qat/qat_common/qat_uclo.c +19 -6
index 61be6df50684..7678a93c6853 100644
@@ -1,5 +1,6 @@
 // SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0-only)
 /* Copyright(c) 2014 - 2020 Intel Corporation */
+#include <linux/align.h>
 #include <linux/slab.h>
 #include <linux/ctype.h>
 #include <linux/kernel.h>
@@ -1414,16 +1415,21 @@ static int qat_uclo_map_auth_fw(struct icp_qat_fw_loader_handle *handle,
 	struct icp_qat_fw_auth_desc *auth_desc;
 	struct icp_qat_auth_chunk *auth_chunk;
 	u64 virt_addr,  bus_addr, virt_base;
-	unsigned int length, simg_offset = sizeof(*auth_chunk);
+	unsigned int simg_offset = sizeof(*auth_chunk);
 	struct icp_qat_simg_ae_mode *simg_ae_mode;
 	struct icp_firml_dram_desc img_desc;
+	int ret;
 
-	length = (css_hdr->fw_type == CSS_AE_FIRMWARE) ?
-		 ICP_QAT_CSS_AE_SIMG_LEN(handle) + simg_offset :
-		 size + ICP_QAT_CSS_FWSK_PAD_LEN(handle) + simg_offset;
-	if (qat_uclo_simg_alloc(handle, &img_desc, length)) {
+	ret = qat_uclo_simg_alloc(handle, &img_desc, ICP_QAT_CSS_RSA4K_MAX_IMAGE_LEN);
+	if (ret) {
 		pr_err("QAT: error, allocate continuous dram fail\n");
-		return -ENOMEM;
+		return ret;
+	}
+
+	if (!IS_ALIGNED(img_desc.dram_size, 8) || !img_desc.dram_bus_addr) {
+		pr_debug("QAT: invalid address\n");
+		qat_uclo_simg_free(handle, &img_desc);
+		return -EINVAL;
 	}
 
 	auth_chunk = img_desc.dram_base_addr_v;
@@ -1481,6 +1487,13 @@ static int qat_uclo_map_auth_fw(struct icp_qat_fw_loader_handle *handle,
 	auth_desc->img_high = (unsigned int)(bus_addr >> BITS_IN_DWORD);
 	auth_desc->img_low = (unsigned int)bus_addr;
 	auth_desc->img_len = size - ICP_QAT_AE_IMG_OFFSET(handle);
+	if (bus_addr + auth_desc->img_len > img_desc.dram_bus_addr +
+					    ICP_QAT_CSS_RSA4K_MAX_IMAGE_LEN) {
+		pr_err("QAT: insufficient memory size for authentication data\n");
+		qat_uclo_simg_free(handle, &img_desc);
+		return -ENOMEM;
+	}
+
 	memcpy((void *)(uintptr_t)virt_addr,
 	       (void *)(image + ICP_QAT_AE_IMG_OFFSET(handle)),
 	       auth_desc->img_len);
Signature · 1 line
2.48.1
maintainer
#4 lore raw
In reply to Giovanni Cabiddu
Quoted 10 lines · Giovanni Cabiddu

This small set of patches adds some minor improvements to the QAT firmware loader.

- Patch #1 removes some unused members in a structure that are not used for doing the firmware authentication.
- Patch #2 removes a redundant check on the size of the firmware image.
- Patch #3 simplifies the allocations for the firmware authentication an introduces an additional check to ensure that the allocated memory meets the requirements of the authentication firmware.

Quoted 11 lines

Jack Xu (3):
crypto: qat - remove unused members in suof structure
crypto: qat - remove redundant FW image size check
crypto: qat - optimize allocations for fw authentication

 .../intel/qat/qat_common/icp_qat_uclo.h       | 10 -----
 .../crypto/intel/qat/qat_common/qat_uclo.c    | 38 ++++++++++---------
 2 files changed, 21 insertions(+), 27 deletions(-)
Signature · 1 line
2.48.1

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