crypto: ccp - add Hygon SM3/SM4 support
cover crypto: ccp - add Hygon SM3/SM4 support
This series adds SM3 and SM4 offload for Hygon CCP devices, which use the v5 programming model, and fixes two pre-existing problems in the queue handling that were found while validating it.
Patches 1-2 are generic fixes that apply to the AMD devices as well:
1: correct the LSB region mask, which tested the wrong region 2: acknowledge the queue interrupt before publishing the completion
Patches 3-11 prepare and enable the Hygon support on top of a per-device capability set instead of unconditional registration:
3-4: add struct ccp_caps and gate algorithm registration on the
engines the device implements. AMD v5 devices keep their current set of algorithms; the v3 CCP does not implement DES3 and no longer advertises it.
5: split descriptor submission from running the queue, so that the
SM engines can submit a whole batch and run the queue once.
6-7: add the SM3/SM4 command ABI, the Hygon PCI IDs and vdata.
8: ignore the empty-queue interrupt status bit, which the Hygon
CCP reports and the v5 handler must not treat as a completion. AMD hardware has not been observed to report it; for the status values AMD produces the check makes no difference.
9: read the implemented engines from the Hygon version register.
10-11: add the SM4 skcipher and SM3 ahash front ends, registering
ecb/cbc/ctr(sm4), sm3 and hmac(sm3).
Every patch was built separately with W=1 and passes checkpatch. The driver has been tested on Hygon CCP hardware: the crypto API self-tests pass, and an out-of-tree test module passes all 17 of its cases. The cases cover known-answer vectors, cross-checks against the generic software implementations, streaming and export state, multi-segment scatterlists, repeated requests and concurrent requests.
AI assistance: the code and changelogs were produced with AI coding assistants under human review. The assistance is recorded here, as the patches do not carry per-patch Assisted-by tags:
The test module is not part of this series.
Depei Yang (11):
crypto: ccp - fix LSB region mask calculation
crypto: ccp - acknowledge queue interrupt before waking waiter
crypto: ccp - add engine capability and version information
crypto: ccp - register algorithms only for supported engines
crypto: ccp - split v5 command submission from queue run
crypto: ccp - add SM3/SM4 command definitions
crypto: ccp - add Hygon CCP device IDs and vdata
crypto: ccp - handle only supported interrupts in v5 irq bh
crypto: ccp - detect the Hygon CCP engines from the version register
crypto: ccp - add SM4 engine and skcipher algorithms
crypto: ccp - add SM3 engine and ahash algorithms
drivers/crypto/ccp/Kconfig | 1 + drivers/crypto/ccp/Makefile | 4 +- drivers/crypto/ccp/ccp-crypto-main.c | 36 +- drivers/crypto/ccp/ccp-crypto-sm3.c | 468 +++++++++++++++++++++++++ drivers/crypto/ccp/ccp-crypto-sm4.c | 254 ++++++++++++++ drivers/crypto/ccp/ccp-crypto.h | 67 ++++ drivers/crypto/ccp/ccp-dev-v3.c | 21 +- drivers/crypto/ccp/ccp-dev-v5.c | 260 +++++++++++++- drivers/crypto/ccp/ccp-dev.c | 37 ++ drivers/crypto/ccp/ccp-dev.h | 31 ++ drivers/crypto/ccp/ccp-ops.c | 487 +++++++++++++++++++++++++++ drivers/crypto/ccp/sp-dev.h | 12 + drivers/crypto/ccp/sp-pci.c | 35 ++ include/linux/ccp.h | 187 ++++++++++ 14 files changed, 1878 insertions(+), 22 deletions(-) create mode 100644 drivers/crypto/ccp/ccp-crypto-sm3.c create mode 100644 drivers/crypto/ccp/ccp-crypto-sm4.c
base-commit: 05441d697f7f9a927264f4058cbbc6a03dfcac9d
Signature · 1 line
2.34.1
PATCH 1/11 crypto: ccp - fix LSB region mask calculation
ccp_find_lsb_regions() tests the queue bit of status before shifting the register by the region width, so region 0 is examined for region 1 and the last region is never checked. Shift the status before the loop so that each iteration tests the region it records.
--- drivers/crypto/ccp/ccp-dev-v5.c | 1 + 1 file changed, 1 insertion(+)
drivers/crypto/ccp/ccp-dev-v5.c +1 -0
@@ -593,6 +593,7 @@ static int ccp_find_lsb_regions(struct ccp_cmd_queue *cmd_q, u64 status)/* Build a bit mask to know which LSBs this queue has access to. * Don't bother with segment 0 as it has special privileges. */ + status >>=LSB_REGION_WIDTH ;for (j =1 ; j <MAX_LSB_CNT ; j++) {if (status & q_mask)bitmap_set (cmd_q->lsbmask, j,1 );
Signature · 1 line
2.34.1
PATCH 2/11 crypto: ccp - acknowledge queue interrupt before waking waiter
In reply to Depei Yangccp5_irq_bh() and ccp_irq_bh() set int_rcvd before writing the interrupt status back to the device. A waiting queue thread can then start the next command before the previous completion has been acknowledged; the stale write-back clears the new completion bit as well, and the thread waits for an interrupt that will never be raised again. The wait has no timeout, so the queue stays stuck.
Acknowledge the interrupt before making int_rcvd visible. The v5 handler was observed to wedge a queue thread under concurrent crypto load; the v3 handler uses the same ordering and gets the same treatment.
--- drivers/crypto/ccp/ccp-dev-v3.c | 9 ++++++--- drivers/crypto/ccp/ccp-dev-v5.c | 9 ++++++--- 2 files changed, 12 insertions(+), 6 deletions(-)
drivers/crypto/ccp/ccp-dev-v3.c +6 -3
@@ -343,10 +343,13 @@ static void ccp_irq_bh(unsigned long data)if ((q_int & cmd_q->int_err) && !cmd_q->cmd_error) cmd_q->cmd_error =CMD_Q_ERROR (cmd_q->q_status); - cmd_q->int_rcvd =1 ; - -/* Acknowledge the interrupt and wake the kthread */ +/* +* Acknowledge the interrupt before waking the kthread, +* otherwise a fast waiter can start a new command while +* the old interrupt is still pending. +*/ iowrite32 (q_int, ccp->io_regs +IRQ_STATUS_REG ); + cmd_q->int_rcvd =1 ;wake_up_interruptible (&cmd_q->int_queue); } }
drivers/crypto/ccp/ccp-dev-v5.c +6 -3
@@ -754,10 +754,13 @@ static void ccp5_irq_bh(unsigned long data)if ((status &INT_ERROR ) && !cmd_q->cmd_error) cmd_q->cmd_error =CMD_Q_ERROR (cmd_q->q_status); - cmd_q->int_rcvd =1 ; - -/* Acknowledge the interrupt and wake the kthread */ +/* +* Acknowledge the interrupt before waking the kthread, +* otherwise a fast waiter can start a new command while +* the old interrupt is still pending. +*/ iowrite32 (status, cmd_q->reg_interrupt_status); + cmd_q->int_rcvd =1 ;wake_up_interruptible (&cmd_q->int_queue); } }
Signature · 1 line
2.34.1
PATCH 3/11 crypto: ccp - add engine capability and version information
In reply to Depei YangAdd a struct ccp_caps to struct ccp_device to record which engines a device implements, together with the CCP and engine versions. The versions are not used yet; they are needed to tell features of the same engine apart, for example whether an SM4 engine can run XTS.
A get_caps callback fills the set at probe time, after the register mapping and device setup, so a device can derive it from a register instead of a table. Capabilities are BIT(enum ccp_engine) behind the CCP_CAPS_* helpers, so the engine id in a command descriptor and its capability bit cannot drift apart; ccp_has_cap() requires a single device to provide every bit in the mask.
No algorithm registration is gated on this yet.
--- drivers/crypto/ccp/ccp-dev-v3.c | 12 +++++++++++ drivers/crypto/ccp/ccp-dev-v5.c | 16 ++++++++++++++ drivers/crypto/ccp/ccp-dev.c | 37 +++++++++++++++++++++++++++++++++ drivers/crypto/ccp/ccp-dev.h | 2 ++ drivers/crypto/ccp/sp-dev.h | 12 +++++++++++ include/linux/ccp.h | 22 ++++++++++++++++++++ 6 files changed, 101 insertions(+)
drivers/crypto/ccp/ccp-dev-v3.c +12 -0
@@ -587,12 +587,23 @@ static const struct ccp_actions ccp3_actions = { .irqhandler = ccp_irq_handler, }; +/* Engines implemented for the v3 CCP. 3DES is not wired up for it. */ +#define CCP3_BASE_ENGINES (CCP_CAPS_AES | CCP_CAPS_AES_XTS | \ + CCP_CAPS_SHA | CCP_CAPS_RSA | \ + CCP_CAPS_PASSTHRU | CCP_CAPS_ECC) + +static void ccp3_get_caps (struct ccp_device *ccp) +{ + ccp->caps.engines =CCP3_BASE_ENGINES ; +} +const struct ccp_vdata ccpv3_platform = { .version =CCP_VERSION (3 ,0 ), .setup =NULL , .perform = &ccp3_actions, .offset =0 , .rsamax =CCP_RSA_MAX_WIDTH , + .get_caps = ccp3_get_caps, };const struct ccp_vdata ccpv3 = {
@@ -601,4 +612,5 @@ const struct ccp_vdata ccpv3 = { .perform = &ccp3_actions, .offset = 0x20000, .rsamax = CCP_RSA_MAX_WIDTH, + .get_caps = ccp3_get_caps, };
drivers/crypto/ccp/ccp-dev-v5.c +16 -0
@@ -1118,12 +1118,27 @@ static const struct ccp_actions ccp5_actions = { .get_free_slots = ccp5_get_free_slots, }; +/* +* Engines implemented by the AMD v5 CCP variants. They are described +* statically, there is no engine presence register to read. +*/ +#define CCP5_BASE_ENGINES (CCP_CAPS_AES | CCP_CAPS_AES_XTS | \ + CCP_CAPS_DES3 | CCP_CAPS_SHA | \ + CCP_CAPS_RSA | CCP_CAPS_PASSTHRU | \ + CCP_CAPS_ECC) + +static void ccp5_get_caps (struct ccp_device *ccp) +{ + ccp->caps.engines =CCP5_BASE_ENGINES ; +} +const struct ccp_vdata ccpv5a = { .version =CCP_VERSION (5 ,0 ), .setup = ccp5_config, .perform = &ccp5_actions, .offset =0x0 , .rsamax =CCP5_RSA_MAX_WIDTH , + .get_caps = ccp5_get_caps, };const struct ccp_vdata ccpv5b = {
@@ -1133,4 +1148,5 @@ const struct ccp_vdata ccpv5b = { .perform = &ccp5_actions, .offset = 0x0, .rsamax = CCP5_RSA_MAX_WIDTH, + .get_caps = ccp5_get_caps, };
drivers/crypto/ccp/ccp-dev.c +37 -0
@@ -242,6 +242,40 @@ int ccp_present(void) }EXPORT_SYMBOL_GPL (ccp_present); +/** +* ccp_has_cap - check whether any CCP device provides a capability +* +* @cap: mask of BIT(enum ccp_engine) values +* +* Returns true if at least one registered CCP device provides all the +* engines in @cap. +* +* Commands are dispatched to the devices by round-robin without checking +* their capabilities, so a caller may only rely on this result when all +* the CCP devices of the system provide the same engines, as they do when +* they are instances of the same hardware. A system mixing devices with +* different engines would need per-command capability filtering in the +* dispatch path. +*/ +bool ccp_has_cap (unsigned int cap) +{ +struct ccp_device *dp; +unsigned long flags; +bool ret = false; + +read_lock_irqsave (&ccp_unit_lock, flags); +list_for_each_entry (dp, &ccp_units, entry) { +if ((dp->caps.engines & cap) == cap) { + ret = true; +break ; + } + } +read_unlock_irqrestore (&ccp_unit_lock, flags); + +return ret; +} +EXPORT_SYMBOL_GPL (ccp_has_cap); +/** * ccp_version - get the version of the CCP device *
@@ -634,6 +668,9 @@ int ccp_dev_init(struct sp_device *sp)if (ccp->vdata->setup) ccp->vdata->setup (ccp); +if (ccp->vdata->get_caps) + ccp->vdata->get_caps (ccp); + ret = ccp->vdata->perform->init (ccp);if (ret) {/* A positive number means that the device cannot be initialized,
drivers/crypto/ccp/ccp-dev.h +2 -0
@@ -23,6 +23,7 @@#include <linux/interrupt.h> #include <linux/irqreturn.h> #include <linux/dmaengine.h> +#include <linux/ccp.h> #include "sp-dev.h"
@@ -340,6 +341,7 @@ struct ccp_device {struct list_head entry;struct ccp_vdata *vdata; +struct ccp_caps caps;unsigned int ord;char name[MAX_CCP_NAME_LEN ];char rngname[MAX_CCP_NAME_LEN ];
drivers/crypto/ccp/sp-dev.h +12 -0
@@ -35,6 +35,17 @@/* Structure to hold CCP device data */ struct ccp_device ; + +/* +* Engines implemented by a CCP device, plus the CCP and engine +* versions. A version of 0 means that it is not known. +*/ +struct ccp_caps { +unsigned int engines;/* BIT(enum ccp_engine) */ +unsigned int ccp_version; +unsigned int engine_version; +}; +struct ccp_vdata {const unsigned int version;const unsigned int dma_chan_attr;
@@ -42,6 +53,7 @@ struct ccp_vdata {const struct ccp_actions *perform;const unsigned int offset;const unsigned int rsamax; +void (*get_caps)(struct ccp_device *ccp); };struct sev_vdata {
include/linux/ccp.h +22 -0
@@ -11,6 +11,7 @@#ifndef __CCP_H__#define __CCP_H__ +#include <linux/bits.h> #include <linux/scatterlist.h> #include <linux/workqueue.h> #include <linux/list.h>
@@ -30,6 +31,13 @@ struct ccp_cmd;*/ int ccp_present (void ); +/** +* ccp_has_cap - check whether any CCP device provides a capability +* +* Returns: true if at least one registered CCP device provides it. +*/ +bool ccp_has_cap (unsigned int cap); +#define CCP_VSIZE 16#define CCP_VMASK ((unsigned int)((1 << CCP_VSIZE) - 1))#define CCP_VERSION (v, r) ((unsigned int)((v << CCP_VSIZE) \
@@ -79,6 +87,11 @@ static inline unsigned int ccp_version(void)return 0 ; } +static inline bool ccp_has_cap (unsigned int cap) +{ +return false; +} +static inline int ccp_enqueue_cmd (struct ccp_cmd *cmd) {return -ENODEV ;
@@ -630,6 +643,15 @@ enum ccp_engine { CCP_ENGINE__LAST, }; +/* Engine capability bits, indexed by enum ccp_engine */ +#define CCP_CAPS_AES BIT(CCP_ENGINE_AES) +#define CCP_CAPS_AES_XTS BIT(CCP_ENGINE_XTS_AES_128) +#define CCP_CAPS_DES3 BIT(CCP_ENGINE_DES3) +#define CCP_CAPS_SHA BIT(CCP_ENGINE_SHA) +#define CCP_CAPS_RSA BIT(CCP_ENGINE_RSA) +#define CCP_CAPS_PASSTHRU BIT(CCP_ENGINE_PASSTHRU) +#define CCP_CAPS_ECC BIT(CCP_ENGINE_ECC) +/* Flag values for flags member of ccp_cmd */ #define CCP_CMD_MAY_BACKLOG 0x00000001#define CCP_CMD_PASSTHRU_NO_DMA_MAP 0x00000002
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH 03/11] crypto: ccp - add engine capability and version information
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] crypto: ccp - missing @cap kernel-doc description for ccp_has_cap() in ccp.h --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] crypto: ccp - missing @cap kernel-doc description for ccp_has_cap() in ccp.h
--
commit a6f9105aa384767e740aed303e637b97511d7aa6
Author: Depei Yang <[email protected]>
crypto: ccp - add engine capability and version information
This commit adds struct ccp_caps, a get_caps callback for CCP v3 and v5 devices, and a ccp_has_cap() helper to query engine capabilities.
Quoted 13 lines
include/linux/ccp.h +1
-0@@ -11,6 +11,7 @@#ifndef __CCP_H__#define __CCP_H__ +#include <linux/bits.h> #include <linux/scatterlist.h> #include <linux/workqueue.h> #include <linux/list.h>
include/linux/ccp.h struct ccp_cmd; · line 39
Quoted 9 lines
include/linux/ccp.h +6
-0@@ -30,6 +31,13 @@ struct ccp_cmd;*/ int ccp_present (void ); +/** +* ccp_has_cap - check whether any CCP device provides a capability +* +* Returns: true if at least one registered CCP device provides it. +*/ +bool ccp_has_cap (unsigned int cap);
[Severity: Low]
This kernel-doc comment on ccp_has_cap() omits the @cap parameter description (unlike the definition in drivers/crypto/ccp/ccp-dev.c), which causes kernel-doc to emit a warning when parsing include/linux/ccp.h.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
PATCH 4/11 crypto: ccp - register algorithms only for supported engines
In reply to Depei Yangccp_register_algs() registers every algorithm family the driver knows about. A device that does not implement the matching engine still gets the algorithms advertised, and requests for them end up on a queue that cannot execute them.
Gate each family on the engine it is built on, using the capability information from probe time. The AMD v5 variants advertise all of their engines and keep the current set of algorithms; the v3 CCP does not implement DES3 and no longer advertises des3.
--- drivers/crypto/ccp/ccp-crypto-main.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)
drivers/crypto/ccp/ccp-crypto-main.c +8 -6
@@ -325,7 +325,7 @@ static int ccp_register_algs(void) {int ret; -if (!aes_disable) { +if (!aes_disable &&ccp_has_cap (CCP_CAPS_AES )) { ret =ccp_register_aes_algs (&skcipher_algs);if (ret)return ret;
@@ -334,28 +334,30 @@ static int ccp_register_algs(void)if (ret)return ret; - ret =ccp_register_aes_xts_algs (&skcipher_algs); + ret =ccp_register_aes_aeads (&aead_algs);if (ret)return ret; + } - ret =ccp_register_aes_aeads (&aead_algs); +if (!aes_disable &&ccp_has_cap (CCP_CAPS_AES_XTS )) { + ret =ccp_register_aes_xts_algs (&skcipher_algs);if (ret)return ret; } -if (!des3_disable) { +if (!des3_disable &&ccp_has_cap (CCP_CAPS_DES3 )) { ret =ccp_register_des3_algs (&skcipher_algs);if (ret)return ret; } -if (!sha_disable) { +if (!sha_disable &&ccp_has_cap (CCP_CAPS_SHA )) { ret =ccp_register_sha_algs (&hash_algs);if (ret)return ret; } -if (!rsa_disable) { +if (!rsa_disable &&ccp_has_cap (CCP_CAPS_RSA )) { ret =ccp_register_rsa_algs (&akcipher_algs);if (ret)return ret;
Signature · 1 line
2.34.1
PATCH 5/11 crypto: ccp - split v5 command submission from queue run
In reply to Depei Yangccp5_do_cmd() copies one descriptor into the ring and immediately runs the queue and waits for the completion. Some engines prepare several descriptors for a single operation and only need the queue run once.
Split the descriptor copy from running the queue. ccp5_do_cmd() keeps its behaviour by calling both helpers back to back; the SM engines will run the queue through the new ->run action once a whole batch has been submitted.
--- drivers/crypto/ccp/ccp-dev-v5.c | 38 ++++++++++++++++++++++++++------- drivers/crypto/ccp/ccp-dev.h | 1 + 2 files changed, 31 insertions(+), 8 deletions(-)
drivers/crypto/ccp/ccp-dev-v5.c +30 -8
@@ -218,14 +218,12 @@ static unsigned int ccp5_get_free_slots(struct ccp_cmd_queue *cmd_q)return n %COMMANDS_PER_QUEUE ;/* Always one unused spot */ } -static int ccp5_do_cmd (struct ccp5_desc *desc, -struct ccp_cmd_queue *cmd_q) +static void ccp5_submit_cmd (struct ccp5_desc *desc, +struct ccp_cmd_queue *cmd_q) {__le32 *mP;u32 *dP; -u32 tail; -int i; -int ret =0 ; +int i; cmd_q->total_ops++;
@@ -233,6 +231,7 @@ static int ccp5_do_cmd(struct ccp5_desc *desc,CCP5_CMD_IOC (desc) =1 ;CCP5_CMD_SOC (desc) =0 ; } +mutex_lock (&cmd_q->q_mutex); mP = (__le32 *)&cmd_q->qbase[cmd_q->qidx];
@@ -242,6 +241,16 @@ static int ccp5_do_cmd(struct ccp5_desc *desc, cmd_q->qidx = (cmd_q->qidx +1 ) %COMMANDS_PER_QUEUE ; +mutex_unlock (&cmd_q->q_mutex); +} + +static int ccp5_run_queue (struct ccp_cmd_queue *cmd_q,bool wait) +{ +u32 tail; +int ret =0 ; + +mutex_lock (&cmd_q->q_mutex); +/* The data used by this command must be flushed to memory */ wmb ();
@@ -253,7 +262,7 @@ static int ccp5_do_cmd(struct ccp5_desc *desc,iowrite32 (cmd_q->qcontrol |CMD5_Q_RUN , cmd_q->reg_control);mutex_unlock (&cmd_q->q_mutex); -if (CCP5_CMD_IOC (desc)) { +if (wait) {/* Wait for the job to complete */ ret =wait_event_interruptible (cmd_q->int_queue, cmd_q->int_rcvd);
@@ -262,8 +271,7 @@ static int ccp5_do_cmd(struct ccp5_desc *desc,* moving the head pointer */ if (cmd_q->cmd_error) -ccp_log_error (cmd_q->ccp, - cmd_q->cmd_error); +ccp_log_error (cmd_q->ccp, cmd_q->cmd_error);iowrite32 (tail, cmd_q->reg_head_lo);if (!ret) ret = -EIO ;
@@ -274,6 +282,19 @@ static int ccp5_do_cmd(struct ccp5_desc *desc,return ret; } +static int ccp5_run_cmd (struct ccp_op *op) +{ +return ccp5_run_queue (op->cmd_q, op->ioc); +} + +static int ccp5_do_cmd (struct ccp5_desc *desc, +struct ccp_cmd_queue *cmd_q) +{ +ccp5_submit_cmd (desc, cmd_q); + +return ccp5_run_queue (cmd_q,CCP5_CMD_IOC (desc)); +} +static int ccp5_perform_aes (struct ccp_op *op) {struct ccp5_desc desc;
@@ -1111,6 +1132,7 @@ static const struct ccp_actions ccp5_actions = { .rsa = ccp5_perform_rsa, .passthru = ccp5_perform_passthru, .ecc = ccp5_perform_ecc, + .run = ccp5_run_cmd, .sballoc = ccp_lsb_alloc, .sbfree = ccp_lsb_free, .init = ccp5_init,
drivers/crypto/ccp/ccp-dev.h +1 -0
@@ -659,6 +659,7 @@ struct ccp_actions {int (*rsa)(struct ccp_op *);int (*passthru)(struct ccp_op *);int (*ecc)(struct ccp_op *); +int (*run)(struct ccp_op *op);u32 (*sballoc)(struct ccp_cmd_queue *,unsigned int );void (*sbfree)(struct ccp_cmd_queue *,unsigned int ,unsigned int );unsigned int (*get_free_slots)(struct ccp_cmd_queue *);
Signature · 1 line
2.34.1
PATCH 6/11 crypto: ccp - add SM3/SM4 command definitions
In reply to Depei YangAdd the public SM3/SM4 command structures, the engine identifiers and the v5 descriptor fields. No engine or algorithm is wired up yet; this only describes the interface the SM engine patches will use.
The new engine identifiers come with the matching CCP_CAPS_* bits. The SM2 engine number (8) is left reserved.
--- drivers/crypto/ccp/ccp-crypto.h | 67 +++++++++++++ drivers/crypto/ccp/ccp-dev-v5.c | 26 +++++ drivers/crypto/ccp/ccp-dev.h | 22 +++++ include/linux/ccp.h | 165 ++++++++++++++++++++++++++++++++ 4 files changed, 280 insertions(+)
drivers/crypto/ccp/ccp-crypto.h +67 -0
@@ -21,6 +21,8 @@#include <crypto/hash.h> #include <crypto/sha1.h> #include <crypto/sha2.h> +#include <crypto/sm3.h> +#include <crypto/sm4.h> #include <crypto/akcipher.h> #include <crypto/skcipher.h> #include <crypto/internal/rsa.h>
@@ -258,6 +260,67 @@ struct ccp_rsa_req_ctx {#define CCP_RSA_MAXMOD (4 * 1024 / 8)#define CCP5_RSA_MAXMOD (16 * 1024 / 8) +/***** SM3 related defines *****/ +struct ccp_sm3_ctx { +u32 key_len; +u8 key[SM3_BLOCK_SIZE ]; + +u8 ipad[SM3_BLOCK_SIZE ]; + +u8 opad[SM3_BLOCK_SIZE ]; +struct scatterlist opad_sg; + +struct crypto_shash *hmac_tfm; +}; + +struct ccp_sm3_req_ctx { +u64 msg_bits; + +unsigned int first; +unsigned int final; + +struct scatterlist *src; +u32 nbytes; + +u64 hash_cnt; +u32 hash_rem; + +struct sg_table data_sg; + +struct scatterlist ctx_sg; +u8 ctx[SM3_DIGEST_SIZE ]; + +struct scatterlist buf_sg; +u32 buf_count; +u8 buf[SM3_BLOCK_SIZE ]; + +struct ccp_cmd cmd; +}; + +struct ccp_sm3_exp_ctx { +u64 msg_bits; + +u8 ctx[SM3_DIGEST_SIZE ]; + +u32 buf_count; +u8 buf[SM3_BLOCK_SIZE ]; +}; + +/***** SM4 related defines *****/ +struct ccp_sm4_ctx { +struct scatterlist key_sg; +u8 key[SM4_KEY_SIZE ]; +u32 key_len; +u32 mode; +}; + +struct ccp_sm4_req_ctx { +struct scatterlist iv_sg; +u8 iv[SM4_BLOCK_SIZE ]; + +struct ccp_cmd cmd; +}; +/***** Common Context Structure *****/ struct ccp_ctx {int (*complete)(struct crypto_async_request *req,int ret);
@@ -267,6 +330,8 @@ struct ccp_ctx {struct ccp_rsa_ctx rsa;struct ccp_sha_ctx sha;struct ccp_des3_ctx des3; +struct ccp_sm3_ctx sm3; +struct ccp_sm4_ctx sm4; } u; };
@@ -282,5 +347,7 @@ int ccp_register_aes_aeads(struct list_head *head);int ccp_register_sha_algs (struct list_head *head);int ccp_register_des3_algs (struct list_head *head);int ccp_register_rsa_algs (struct list_head *head); +int ccp_register_sm3_algs (struct list_head *head); +int ccp_register_sm4_algs (struct list_head *head);#endif
drivers/crypto/ccp/ccp-dev-v5.c +26 -0
@@ -131,6 +131,23 @@ union ccp_function { u16 type:2; u16 mode:3; } ecc; +struct { +u16 rsvd:10 ; +u16 type:4 ; +u16 rsvd2:1 ; + } sm3; +struct { +u16 rsvd:7 ; +u16 encrypt:1 ; +u16 mode:4 ; +u16 select:1 ; +u16 rsvd2:2 ; + } sm4; +struct { +u16 size:7 ; +u16 encrypt:1 ; +u16 step:7 ; + } sm4_ctr;u16 raw; };
@@ -151,6 +168,13 @@ union ccp_function {#define CCP_PT_BITWISE (p) ((p)->pt.bitwise)#define CCP_ECC_MODE (p) ((p)->ecc.mode)#define CCP_ECC_AFFINE (p) ((p)->ecc.one) +#define CCP_SM3_TYPE (p) ((p)->sm3.type) +#define CCP_SM4_ENCRYPT (p) ((p)->sm4.encrypt) +#define CCP_SM4_MODE (p) ((p)->sm4.mode) +#define CCP_SM4_SELECT (p) ((p)->sm4.select) +#define CCP_SM4_CTR_ENCRYPT (p) ((p)->sm4_ctr.encrypt) +#define CCP_SM4_CTR_STEP (p) ((p)->sm4_ctr.step) +#define CCP_SM4_CTR_SIZE (p) ((p)->sm4_ctr.size)/* Word 0 */ #define CCP5_CMD_DW0 (p) ((p)->dw0)
@@ -186,6 +210,8 @@ union ccp_function {#define CCP5_CMD_FIX_DST (p) ((p)->dw5.fields.fixed)#define CCP5_CMD_SHA_LO (p) ((p)->dw4.sha_len_lo)#define CCP5_CMD_SHA_HI (p) ((p)->dw5.sha_len_hi) +#define CCP5_CMD_SM3_LO (p) ((p)->dw4.sm3_len_lo) +#define CCP5_CMD_SM3_HI (p) ((p)->dw5.sm3_len_hi)/* Word 6/7 */ #define CCP5_CMD_DW6 (p) ((p)->key_lo)
drivers/crypto/ccp/ccp-dev.h +22 -0
@@ -530,6 +530,23 @@ struct ccp_ecc_op {enum ccp_ecc_function function; }; +struct ccp_sm3_op { +enum ccp_sm3_type type; +u64 msg_bits; +}; + +struct ccp_sm4_op { +enum ccp_sm4_action action; +enum ccp_sm4_mode mode; +u32 select; +}; + +struct ccp_sm4_ctr_op { +u32 size; +enum ccp_sm4_action action; +u32 step; +}; +struct ccp_op {struct ccp_cmd_queue *cmd_q;
@@ -553,6 +570,9 @@ struct ccp_op {struct ccp_rsa_op rsa;struct ccp_passthru_op passthru;struct ccp_ecc_op ecc; +struct ccp_sm3_op sm3; +struct ccp_sm4_op sm4; +struct ccp_sm4_ctr_op sm4_ctr; } u; };
@@ -601,6 +621,7 @@ struct dword3 {union dword4 {u32 dst_lo;/* NON-SHA */ u32 sha_len_lo;/* SHA */ +u32 sm3_len_lo;/* SM3 */ };union dword5 {
@@ -611,6 +632,7 @@ union dword5 { unsigned int fixed:1; } fields;u32 sha_len_hi; +u32 sm3_len_hi; };struct dword7 {
include/linux/ccp.h +165 -0
195 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH 7/11 crypto: ccp - add Hygon CCP device IDs and vdata
In reply to Depei YangHygon CCP devices use the v5 programming model. Add the PCI IDs and a minimal vdata table. The get_caps callback is a placeholder that advertises only the SM engines; a later patch replaces it with register-based detection.
--- drivers/crypto/ccp/ccp-dev-v5.c | 32 ++++++++++++++++++++++++++++++ drivers/crypto/ccp/ccp-dev.h | 2 ++ drivers/crypto/ccp/sp-pci.c | 35 +++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+)
drivers/crypto/ccp/ccp-dev-v5.c +32 -0
@@ -1198,3 +1198,35 @@ const struct ccp_vdata ccpv5b = { .rsamax = CCP5_RSA_MAX_WIDTH, .get_caps = ccp5_get_caps, }; + +/* +* Hygon CCP is a v5 derivative with the SM3/SM4 engines added. It +* uses the same queue, LSB and interrupt model as the AMD v5 CCP. +* +* TODO: derive the legacy engines from the Hygon engine register once +* its layout is confirmed. Until then only the engines that cannot be +* detected from a register are advertised. +*/ +static void ccp5_hygon_get_caps (struct ccp_device *ccp) +{ + ccp->caps.engines =CCP_CAPS_SM3 |CCP_CAPS_SM4 |CCP_CAPS_SM4_CTR ; +} + +const struct ccp_vdata ccpv5a_hygon = { + .version =CCP_VERSION (5 ,0 ), + .setup = ccp5_config, + .perform = &ccp5_actions, + .offset =0x0 , + .rsamax =CCP5_RSA_MAX_WIDTH , + .get_caps = ccp5_hygon_get_caps, +}; + +const struct ccp_vdata ccpv5b_hygon = { + .version =CCP_VERSION (5 ,0 ), + .dma_chan_attr =DMA_PRIVATE , + .setup = ccp5other_config, + .perform = &ccp5_actions, + .offset =0x0 , + .rsamax =CCP5_RSA_MAX_WIDTH , + .get_caps = ccp5_hygon_get_caps, +};
drivers/crypto/ccp/ccp-dev.h +2 -0
@@ -694,5 +694,7 @@ extern const struct ccp_vdata ccpv3_platform;extern const struct ccp_vdata ccpv3;extern const struct ccp_vdata ccpv5a;extern const struct ccp_vdata ccpv5b; +extern const struct ccp_vdata ccpv5a_hygon; +extern const struct ccp_vdata ccpv5b_hygon;#endif
drivers/crypto/ccp/sp-pci.c +35 -0
@@ -551,6 +551,33 @@ static const struct sp_dev_vdata dev_vdata[] = { }, }; + +/* +* Hygon secure processor devices. They are CCP v5 derivatives, so +* they reuse the v5 CCP programming model. The PSP/CSV side is not +* described here. +*/ +static const struct sp_dev_vdata hygon_dev_vdata[] = { + {/* 0 */ + .bar =2 , +#ifdef CONFIG_CRYPTO_DEV_SP_CCP + .ccp_vdata = &ccpv5a_hygon, +#endif + }, + {/* 1 */ + .bar =2 , +#ifdef CONFIG_CRYPTO_DEV_SP_CCP + .ccp_vdata = &ccpv5b_hygon, +#endif + }, + {/* 2 */ + .bar =2 , +#ifdef CONFIG_CRYPTO_DEV_SP_CCP + .ccp_vdata = &ccpv5a_hygon, +#endif + }, +}; +static const struct pci_device_id sp_pci_table[] = { {PCI_VDEVICE (AMD ,0x1537 ), .driver_data = (kernel_ulong_t)&dev_vdata[0 ] }, {PCI_VDEVICE (AMD ,0x1456 ), .driver_data = (kernel_ulong_t)&dev_vdata[1 ] },
@@ -565,6 +592,14 @@ static const struct pci_device_id sp_pci_table[] = { { PCI_VDEVICE(AMD, 0x156E), .driver_data = (kernel_ulong_t)&dev_vdata[8] }, { PCI_VDEVICE(AMD, 0x17D8), .driver_data = (kernel_ulong_t)&dev_vdata[8] }, { PCI_VDEVICE(AMD, 0x115A), .driver_data = (kernel_ulong_t)&dev_vdata[9] }, + { PCI_VDEVICE(HYGON, 0x1456), .driver_data = (kernel_ulong_t)&hygon_dev_vdata[0] }, + { PCI_VDEVICE(HYGON, 0x1468), .driver_data = (kernel_ulong_t)&hygon_dev_vdata[1] }, + { PCI_VDEVICE(HYGON, 0x1486), .driver_data = (kernel_ulong_t)&hygon_dev_vdata[2] }, + { PCI_VDEVICE(HYGON, 0x14b8), .driver_data = (kernel_ulong_t)&hygon_dev_vdata[1] }, + { PCI_VDEVICE(HYGON, 0x14a6), .driver_data = (kernel_ulong_t)&hygon_dev_vdata[2] }, + { PCI_VDEVICE(HYGON, 0x14d8), .driver_data = (kernel_ulong_t)&hygon_dev_vdata[1] }, + { PCI_VDEVICE(HYGON, 0x14c6), .driver_data = (kernel_ulong_t)&hygon_dev_vdata[2] }, + { PCI_VDEVICE(HYGON, 0x2006), .driver_data = (kernel_ulong_t)&hygon_dev_vdata[2] },/* Last entry must be zero */ { } };
Signature · 1 line
2.34.1
PATCH 8/11 crypto: ccp - handle only supported interrupts in v5 irq bh
In reply to Depei YangThe Hygon CCP reports INT_EMPTY_QUEUE in the interrupt status register, which SUPPORTED_INTERRUPTS does not include. Treating it as a completion makes the command queue thread observe a spurious completion. Only process the bits that the driver enables.
AMD hardware has not been observed to report this bit; for the status values it produces the new check is a no-op.
--- drivers/crypto/ccp/ccp-dev-v5.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/crypto/ccp/ccp-dev-v5.c +1 -1
@@ -792,7 +792,7 @@ static void ccp5_irq_bh(unsigned long data) status =ioread32 (cmd_q->reg_interrupt_status); -if (status) { +if (status &SUPPORTED_INTERRUPTS ) { cmd_q->int_status = status; cmd_q->q_status =ioread32 (cmd_q->reg_status); cmd_q->q_int_status =ioread32 (cmd_q->reg_int_status);
Signature · 1 line
2.34.1
PATCH 9/11 crypto: ccp - detect the Hygon CCP engines from the version register
In reply to Depei YangThe Hygon CCP reports the engines it implements in the upper bits of the CCP version register, starting at bit 6: SM2, SM3, SM4, SM4-CTR, ECC, AES, SHA2, TRNG and SM4-GCM.
Translate the presence bits into engine capabilities in ccp5_hygon_get_caps(). The same function stores the raw register value and the engine version read from the new CMD5_PSP_CCP_ENG_VERSION register; neither is used yet. Only the SM engines are advertised for now; the other presence bits are defined but not translated until those engines are enabled.
--- drivers/crypto/ccp/ccp-dev-v5.c | 42 +++++++++++++++++++++++++++++---- drivers/crypto/ccp/ccp-dev.h | 1 + 2 files changed, 39 insertions(+), 4 deletions(-)
drivers/crypto/ccp/ccp-dev-v5.c +38 -4
@@ -1203,13 +1203,47 @@ const struct ccp_vdata ccpv5b = {* Hygon CCP is a v5 derivative with the SM3/SM4 engines added. It * uses the same queue, LSB and interrupt model as the AMD v5 CCP. * -* TODO: derive the legacy engines from the Hygon engine register once -* its layout is confirmed. Until then only the engines that cannot be -* detected from a register are advertised. +* Engine presence is reported by the bits 6 and above of the CCP +* version register; the layout differs from the AMD one. */ +#define HYGON_RI_SM2 BIT(6) +#define HYGON_RI_SM3 BIT(7) +#define HYGON_RI_SM4 BIT(8) +#define HYGON_RI_SM4_CTR BIT(9) +#define HYGON_RI_ECC BIT(10) +#define HYGON_RI_AES BIT(11) +#define HYGON_RI_SHA2 BIT(12) +#define HYGON_RI_TRNG BIT(13) +#define HYGON_RI_SM4_GCM BIT(14) +static void ccp5_hygon_get_caps (struct ccp_device *ccp) { - ccp->caps.engines =CCP_CAPS_SM3 |CCP_CAPS_SM4 |CCP_CAPS_SM4_CTR ; +/* +* Only the SM engines are advertised for now. SM2, ECC, AES, +* SHA2, TRNG and SM4-GCM are not enabled for the Hygon CCP yet; +* they will be added to the table once the driver supports them +* on this hardware. +*/ +static const struct { +u32 presence; +u32 cap; + } engines[] = { + {HYGON_RI_SM3 ,CCP_CAPS_SM3 }, + {HYGON_RI_SM4 ,CCP_CAPS_SM4 }, + {HYGON_RI_SM4_CTR ,CCP_CAPS_SM4_CTR }, + }; +u32 version; +unsigned int i; + + version =ioread32 (ccp->io_regs +CMD5_PSP_CCP_VERSION ); + ccp->caps.ccp_version = version; + ccp->caps.engine_version = +ioread32 (ccp->io_regs +CMD5_PSP_CCP_ENG_VERSION ); + ccp->caps.engines =0 ; + +for (i =0 ; i <ARRAY_SIZE (engines); i++) +if (version & engines[i].presence) + ccp->caps.engines |= engines[i].cap; }const struct ccp_vdata ccpv5a_hygon = {
drivers/crypto/ccp/ccp-dev.h +1 -0
@@ -71,6 +71,7 @@#define LSB_PRIVATE_MASK_LO_OFFSET 0x20#define LSB_PRIVATE_MASK_HI_OFFSET 0x24#define CMD5_PSP_CCP_VERSION 0x100 +#define CMD5_PSP_CCP_ENG_VERSION 0x104#define CMD5_Q_CONTROL_BASE 0x0000#define CMD5_Q_TAIL_LO_BASE 0x0004
Signature · 1 line
2.34.1
PATCH 10/11 crypto: ccp - add SM4 engine and skcipher algorithms
In reply to Depei YangAdd the SM4 and SM4-CTR command builders, the ccp-ops request handling and the skcipher front end. ecb(sm4) and cbc(sm4) are registered when a device advertises the SM4 engine; ctr(sm4) needs the separately reported SM4-CTR engine.
--- drivers/crypto/ccp/Makefile | 3 +- drivers/crypto/ccp/ccp-crypto-main.c | 16 ++ drivers/crypto/ccp/ccp-crypto-sm4.c | 254 +++++++++++++++++++++++ drivers/crypto/ccp/ccp-crypto.h | 1 + drivers/crypto/ccp/ccp-dev-v5.c | 65 ++++++ drivers/crypto/ccp/ccp-dev.h | 2 + drivers/crypto/ccp/ccp-ops.c | 296 +++++++++++++++++++++++++++ 7 files changed, 636 insertions(+), 1 deletion(-) create mode 100644 drivers/crypto/ccp/ccp-crypto-sm4.c
drivers/crypto/ccp/Makefile +2 -1
@@ -28,4 +28,5 @@ ccp-crypto-objs := ccp-crypto-main.o \ ccp-crypto-aes-galois.o \ ccp-crypto-des3.o \ ccp-crypto-rsa.o \ - ccp-crypto-sha.o + ccp-crypto-sha.o \ + ccp-crypto-sm4.o
drivers/crypto/ccp/ccp-crypto-main.c +16 -0
@@ -42,6 +42,10 @@ static unsigned int rsa_disable;module_param (rsa_disable, uint,0444 );MODULE_PARM_DESC (rsa_disable,"Disable use of RSA - any non-zero value" ); +static unsigned int sm_disable; +module_param (sm_disable, uint,0444 ); +MODULE_PARM_DESC (sm_disable,"Disable use of SM3/SM4 - any non-zero value" ); +/* List heads for the supported algorithms */ static LIST_HEAD (hash_algs);static LIST_HEAD (skcipher_algs);
@@ -363,6 +367,18 @@ static int ccp_register_algs(void)return ret; } +if (!sm_disable &&ccp_has_cap (CCP_CAPS_SM4 )) { + ret =ccp_register_sm4_algs (&skcipher_algs); +if (ret) +return ret; + } + +if (!sm_disable &&ccp_has_cap (CCP_CAPS_SM4_CTR )) { + ret =ccp_register_sm4_ctr_algs (&skcipher_algs); +if (ret) +return ret; + } +return 0 ; }
drivers/crypto/ccp/ccp-crypto-sm4.c new file +254 -0
254 lines not on this page · raw message
drivers/crypto/ccp/ccp-crypto.h +1 -0
@@ -349,5 +349,6 @@ int ccp_register_des3_algs(struct list_head *head);int ccp_register_rsa_algs (struct list_head *head);int ccp_register_sm3_algs (struct list_head *head);int ccp_register_sm4_algs (struct list_head *head); +int ccp_register_sm4_ctr_algs (struct list_head *head);#endif
drivers/crypto/ccp/ccp-dev-v5.c +65 -0
83 lines not on this page · raw message
drivers/crypto/ccp/ccp-dev.h +2 -0
@@ -683,6 +683,8 @@ struct ccp_actions {int (*passthru)(struct ccp_op *);int (*ecc)(struct ccp_op *);int (*run)(struct ccp_op *op); +int (*sm4)(struct ccp_op *op); +int (*sm4_ctr)(struct ccp_op *op);u32 (*sballoc)(struct ccp_cmd_queue *,unsigned int );void (*sbfree)(struct ccp_cmd_queue *,unsigned int ,unsigned int );unsigned int (*get_free_slots)(struct ccp_cmd_queue *);
drivers/crypto/ccp/ccp-ops.c +296 -0
314 lines not on this page · raw message
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH 10/11] crypto: ccp - add SM4 engine and skcipher algorithms
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [Medium] crypto: ccp - missing chunksize for ctr(sm4) in ccp_register_sm4_alg() - [Medium] crypto: ccp…
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [Medium] crypto: ccp - missing chunksize for ctr(sm4) in ccp_register_sm4_alg()
- [Medium] crypto: ccp - 64-bit counter size in ccp_sm4_crypt() breaks 128-bit ctr(sm4)
- [Low] crypto: ccp - missing noinline_for_stack on SM4 ccp_run_*_cmd() handlers
--
commit dd1e070e32af2e2910159d7e8f6ecb0aabde8163
Author: Depei Yang <[email protected]>
crypto: ccp - add SM4 engine and skcipher algorithms
This commit adds v5 descriptor builders, request handling, and skcipher registrations for ecb(sm4), cbc(sm4), and ctr(sm4) on CCP devices.
Quoted 5 lines
drivers/crypto/ccp/ccp-crypto-sm4.c new file +0
-0
[ ... ]
drivers/crypto/ccp/ccp-crypto-sm4.c line 93
Quoted 5 lines
drivers/crypto/ccp/ccp-crypto-sm4.c new file +5
-0+if (mode ==CCP_SM4_ALG_MODE_CTR ) { + cmd->engine =CCP_ENGINE_SM4_CTR ; + cmd->u.sm4_ctr.action = action; + cmd->u.sm4_ctr.size =63 ; + cmd->u.sm4_ctr.step =1 ;
[Severity: Medium]
Does setting cmd->u.sm4_ctr.size to 63 restrict the counter increment to the lower 64 bits of the 16-byte IV?
The crypto API ctr(sm4) algorithm treats the counter block as a 128-bit big-endian integer (crypto_inc(ctrblk, 16)), and CCP AES-CTR sets its 7-bit size field to AES_BLOCK_SIZE * BITS_PER_BYTE - 1 (127). With size = 63, requests whose counter wraps the lower 64 bits will not propagate the carry into the upper 64 bits of the counter stream or the returned req->iv, causing self-test failures against the generic implementation under CONFIG_CRYPTO_MANAGER_EXTRA_TESTS.
If the SM4-CTR engine supports a 128-bit counter width via its 7-bit size field, this should be SM4_BLOCK_SIZE * BITS_PER_BYTE - 1; if the hardware is limited to 64 bits, requests crossing a 64-bit counter boundary need a software fallback.
[ ... ]
Quoted 17 lines
drivers/crypto/ccp/ccp-crypto-sm4.c new file +17
-0+static const struct skcipher_alg ccp_sm4_defaults = { + .setkey = ccp_sm4_setkey, + .encrypt = ccp_sm4_encrypt, + .decrypt = ccp_sm4_decrypt, + .min_keysize =SM4_KEY_SIZE , + .max_keysize =SM4_KEY_SIZE , + .init = ccp_sm4_init_tfm, + + .base.cra_flags =CRYPTO_ALG_ASYNC | +CRYPTO_ALG_ALLOCATES_MEMORY | +CRYPTO_ALG_KERN_DRIVER_ONLY | +CRYPTO_ALG_NEED_FALLBACK , + .base.cra_blocksize =SM4_BLOCK_SIZE , + .base.cra_ctxsize =sizeof (struct ccp_ctx ) +CRYPTO_DMA_PADDING , + .base.cra_priority =CCP_CRA_PRIORITY , + .base.cra_module =THIS_MODULE , +};
[ ... ]
Quoted 24 lines
drivers/crypto/ccp/ccp-crypto-sm4.c new file +24
-0+/* SM4-CTR is a separate engine on the CCP */ +static const struct ccp_sm4_def sm4_ctr_alg = { + .mode =CCP_SM4_ALG_MODE_CTR , + .name ="ctr(sm4)" , + .driver_name ="ctr-sm4-ccp" , + .blocksize =1 , + .ivsize =SM4_BLOCK_SIZE , + .alg_defaults = &ccp_sm4_defaults, +}; + +static int ccp_register_sm4_alg (struct list_head *head, +const struct ccp_sm4_def *def) +{ +struct ccp_crypto_skcipher_alg *ccp_alg; +struct skcipher_alg *alg; +int ret; + + ccp_alg =kzalloc_obj (*ccp_alg); +if (!ccp_alg) +return -ENOMEM ; + +INIT_LIST_HEAD (&ccp_alg->entry); + + ccp_alg->mode = def->mode;
drivers/crypto/ccp/ccp-crypto-sm4.c line 225
Quoted 10 lines
drivers/crypto/ccp/ccp-crypto-sm4.c new file +10
-0+ +/* copy the defaults and override as necessary */ + alg = &ccp_alg->alg; + *alg = *def->alg_defaults; +strscpy (alg->base.cra_name, def->name); +strscpy (alg->base.cra_driver_name, def->driver_name); + alg->base.cra_blocksize = def->blocksize; + alg->ivsize = def->ivsize; + + ret =crypto_register_skcipher (alg);
[Severity: Medium]
Should ccp_sm4_defaults set .chunksize = SM4_BLOCK_SIZE?
For ctr(sm4), ccp_register_sm4_alg() sets alg->base.cra_blocksize to 1 while alg->chunksize remains 0, so skcipher_prepare_alg_common() defaults chunksize to 1 instead of SM4_BLOCK_SIZE. Streaming callers such as AF_ALG _skcipher_recvmsg() use crypto_skcipher_chunksize() to align non-final MSG_MORE chunks to a block boundary; with chunksize == 1, a partial block submitted in a non-final call advances the 16-byte hardware counter IV to the next block and discards the remaining keystream bytes of that block, corrupting subsequent output.
Quoted 4 lines
drivers/crypto/ccp/ccp-ops.c +0
-0
[ ... ]
Quoted 6 lines
drivers/crypto/ccp/ccp-ops.c +6
-0+static int ccp_run_sm4_cmd (struct ccp_cmd_queue *cmd_q,struct ccp_cmd *cmd) +{ +struct ccp_sm4_engine *sm4 = &cmd->u.sm4; +struct ccp_dm_workarea iv_key; +struct ccp_data src, dst; +struct ccp_op op;
[ ... ]
drivers/crypto/ccp/ccp-ops.c ccp_run_ecc_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd) · line 2631
Quoted 7 lines
drivers/crypto/ccp/ccp-ops.c +7
-0+static int ccp_run_sm4_ctr_cmd (struct ccp_cmd_queue *cmd_q, +struct ccp_cmd *cmd) +{ +struct ccp_sm4_ctr_engine *sm4_ctr = &cmd->u.sm4_ctr; +struct ccp_dm_workarea iv_key; +struct ccp_data src, dst; +struct ccp_op op;
[Severity: Low]
Should ccp_run_sm4_cmd() and ccp_run_sm4_ctr_cmd() be marked noinline_for_stack like the other per-engine handlers in this file?
Each function allocates roughly 400 bytes of local structures on the stack and has a single call site in ccp_run_cmd(); 72c8117adfce ("crypto: ccp - Reduce maximum stack usage") marked the other ccp_run_*_cmd() handlers noinline_for_stack to prevent the compiler from combining their stack frames in ccp_run_cmd() and tripping -Wframe-larger-than.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
PATCH 11/11 crypto: ccp - add SM3 engine and ahash algorithms
In reply to Depei YangAdd the SM3 command builder, the ccp-ops request handling and the ahash front end, including hmac(sm3). sm3 and hmac(sm3) are registered when a device advertises the SM3 engine.
CRYPTO_DEV_SP_CCP selects CRYPTO_SM3 so that the shash used to hash HMAC keys longer than the block size is always available, as it does for CRYPTO_SHA1 and CRYPTO_SHA256.
--- drivers/crypto/ccp/Kconfig | 1 + drivers/crypto/ccp/Makefile | 3 +- drivers/crypto/ccp/ccp-crypto-main.c | 6 + drivers/crypto/ccp/ccp-crypto-sm3.c | 468 +++++++++++++++++++++++++++ drivers/crypto/ccp/ccp-crypto.h | 1 - drivers/crypto/ccp/ccp-dev-v5.c | 37 +++ drivers/crypto/ccp/ccp-dev.h | 1 + drivers/crypto/ccp/ccp-ops.c | 191 +++++++++++ 8 files changed, 706 insertions(+), 2 deletions(-) create mode 100644 drivers/crypto/ccp/ccp-crypto-sm3.c
drivers/crypto/ccp/Kconfig +1 -0
@@ -15,6 +15,7 @@ config CRYPTO_DEV_SP_CCP select DMA_ENGINE select CRYPTO_SHA1 select CRYPTO_SHA256 + select CRYPTO_SM3 help Provides the support for AMD Cryptographic Coprocessor (CCP) device which can be used to offload encryption operations such as SHA, AES
drivers/crypto/ccp/Makefile +2 -1
@@ -29,4 +29,5 @@ ccp-crypto-objs := ccp-crypto-main.o \ ccp-crypto-des3.o \ ccp-crypto-rsa.o \ ccp-crypto-sha.o \ - ccp-crypto-sm4.o + ccp-crypto-sm4.o \ + ccp-crypto-sm3.o
drivers/crypto/ccp/ccp-crypto-main.c +6 -0
@@ -367,6 +367,12 @@ static int ccp_register_algs(void)return ret; } +if (!sm_disable &&ccp_has_cap (CCP_CAPS_SM3 )) { + ret =ccp_register_sm3_algs (&hash_algs); +if (ret) +return ret; + } +if (!sm_disable &&ccp_has_cap (CCP_CAPS_SM4 )) { ret =ccp_register_sm4_algs (&skcipher_algs);if (ret)
drivers/crypto/ccp/ccp-crypto-sm3.c new file +468 -0
468 lines not on this page · raw message
drivers/crypto/ccp/ccp-crypto.h +0 -1
@@ -262,7 +262,6 @@ struct ccp_rsa_req_ctx {/***** SM3 related defines *****/ struct ccp_sm3_ctx { -u32 key_len;u8 key[SM3_BLOCK_SIZE ];u8 ipad[SM3_BLOCK_SIZE ];
drivers/crypto/ccp/ccp-dev-v5.c +37 -0
@@ -632,6 +632,42 @@ static int ccp5_perform_ecc(struct ccp_op *op)return ccp5_do_cmd (&desc, op->cmd_q); } +static int ccp5_perform_sm3 (struct ccp_op *op) +{ +struct ccp5_desc desc; +union ccp_function function; + +memset (&desc,0 ,Q_DESC_SIZE ); + +CCP5_CMD_ENGINE (&desc) =CCP_ENGINE_SM3 ; + +CCP5_CMD_SOC (&desc) = op->soc; +CCP5_CMD_IOC (&desc) = op->ioc; +CCP5_CMD_INIT (&desc) = op->init; +CCP5_CMD_EOM (&desc) = op->eom; +CCP5_CMD_PROT (&desc) =0 ; + + function.raw =0 ; +CCP_SM3_TYPE (&function) = op->u.sm3.type; +CCP5_CMD_FUNCTION (&desc) = function.raw; + +CCP5_CMD_LEN (&desc) = op->src.u.dma.length; + +CCP5_CMD_SRC_LO (&desc) =ccp_addr_lo (&op->src.u.dma); +CCP5_CMD_SRC_HI (&desc) =ccp_addr_hi (&op->src.u.dma); +CCP5_CMD_SRC_MEM (&desc) =CCP_MEMTYPE_SYSTEM ; +CCP5_CMD_LSB_ID (&desc) = op->sb_ctx; + +if (op->eom) { +CCP5_CMD_SM3_LO (&desc) =lower_32_bits (op->u.sm3.msg_bits); +CCP5_CMD_SM3_HI (&desc) =upper_32_bits (op->u.sm3.msg_bits); + } + +ccp5_submit_cmd (&desc, op->cmd_q); + +return 0 ; +} +static int ccp5_perform_sm4_common (struct ccp_op *op,enum ccp_engine engine,u32 function) {
@@ -1222,6 +1258,7 @@ static const struct ccp_actions ccp5_actions = { .passthru = ccp5_perform_passthru, .ecc = ccp5_perform_ecc, .run = ccp5_run_cmd, + .sm3 = ccp5_perform_sm3, .sm4 = ccp5_perform_sm4, .sm4_ctr = ccp5_perform_sm4_ctr, .sballoc = ccp_lsb_alloc,
drivers/crypto/ccp/ccp-dev.h +1 -0
@@ -683,6 +683,7 @@ struct ccp_actions {int (*passthru)(struct ccp_op *);int (*ecc)(struct ccp_op *);int (*run)(struct ccp_op *op); +int (*sm3)(struct ccp_op *op);int (*sm4)(struct ccp_op *op);int (*sm4_ctr)(struct ccp_op *op);u32 (*sballoc)(struct ccp_cmd_queue *,unsigned int );
drivers/crypto/ccp/ccp-ops.c +191 -0
209 lines not on this page · raw message
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH 11/11] crypto: ccp - add SM3 engine and ahash algorithms
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] crypto: ccp: missing CRYPTO_HMAC select breaks hmac(sm3) fallback init --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] crypto: ccp: missing CRYPTO_HMAC select breaks hmac(sm3) fallback init
--
commit f58b08a6f33547b17d76105dad73af6381c5f603
Author: Depei Yang <[email protected]>
crypto: ccp - add SM3 engine and ahash algorithms
This commit adds the SM3 engine command builder, request handling, and sm3 and hmac(sm3) ahash algorithms to the CCP driver.
drivers/crypto/ccp/Kconfig config CRYPTO_DEV_SP_CCP · line 21
Quoted 12 lines
drivers/crypto/ccp/Kconfig +1
-0@@ -15,6 +15,7 @@ config CRYPTO_DEV_SP_CCP select DMA_ENGINE select CRYPTO_SHA1 select CRYPTO_SHA256 + select CRYPTO_SM3 help Provides the support for AMD Cryptographic Coprocessor (CCP) device which can be used to offload encryption operations such as SHA, AES
[Severity: Medium]
Should CRYPTO_DEV_SP_CCP (or CRYPTO_DEV_CCP_CRYPTO) also select CRYPTO_HMAC?
ccp_register_sm3_alg() and ccp_register_hmac_sm3_alg() register hmac-sm3-ccp with CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK, so crypto_ahash_init_tfm() allocates a synchronous fallback for hmac(sm3) whenever a transform is initialized (including during registration self-tests). Unlike CRYPTO_SHA1 and CRYPTO_SHA256, which directly register synchronous hmac(sha*) shash algorithms, CRYPTO_SM3 only registers sm3-lib and relies on the hmac template from CONFIG_CRYPTO_HMAC to provide hmac(sm3). Without CONFIG_CRYPTO_HMAC enabled, allocating hmac(sm3) fails with -ENOENT.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11