Some bugfix and cleanup.
Zhiqi Song (6): uadk/v1: fix security view issues of hpre uadk: cleanup issues of hpre uadk/v1/hpre: add parameter check of setup uadk/ecc: fix the use of random k in sm2 sign uadk/v1: cleanup common part code style uadk/sec: add return value judgement
drv/hisi_hpre.c | 103 +++++++++++++++++++++++----------------- drv/hisi_sec.c | 4 ++ v1/drv/hisi_hpre_udrv.c | 31 ++++++------ v1/drv/hisi_qm_udrv.c | 16 +++---- v1/wd_adapter.c | 18 +++---- v1/wd_bmm.c | 13 +++-- v1/wd_dh.c | 3 +- v1/wd_ecc.c | 17 +++++-- v1/wd_rsa.c | 6 ++- v1/wd_sgl.c | 27 +++++++++-- wd_dh.c | 16 +++---- wd_ecc.c | 34 ++++++++----- wd_rsa.c | 14 +++--- 13 files changed, 181 insertions(+), 121 deletions(-)
1. Add parameter check when fill rsa pubkey. 2. Modify addr seqence in DMA_ADDR(). 3. Modify redundant code.
Signed-off-by: Zhiqi Song songzhiqi1@huawei.com --- v1/drv/hisi_hpre_udrv.c | 22 +++++++++++++--------- v1/wd_ecc.c | 11 ++++++++--- v1/wd_rsa.c | 3 +++ 3 files changed, 24 insertions(+), 12 deletions(-)
diff --git a/v1/drv/hisi_hpre_udrv.c b/v1/drv/hisi_hpre_udrv.c index 193ba56..92ca8fa 100644 --- a/v1/drv/hisi_hpre_udrv.c +++ b/v1/drv/hisi_hpre_udrv.c @@ -40,7 +40,7 @@ #define HPRE_SM2_ENC 0xE #define HPRE_SM2_DEC 0xF
-#define SM2_SQE_NUM 2 +#define SM2_SQE_NUM 2
static bool is_hpre_bin_fmt(const char *data, int dsz, int bsz) { @@ -196,6 +196,11 @@ static int qm_fill_rsa_pubkey(struct wcrypto_rsa_pubkey *pubkey, void **data) int ret;
wcrypto_get_rsa_pubkey_params(pubkey, &wd_e, &wd_n); + if (unlikely(!wd_e || !wd_n)) { + WD_ERR("failed to get rsa pubkey params!\n"); + return -WD_EINVAL; + } + ret = qm_crypto_bin_to_hpre_bin(wd_e->data, (const char *)wd_e->data, wd_e->bsize, wd_e->dsize, "rsa pubkey e"); if (unlikely(ret)) @@ -321,7 +326,7 @@ static void rsa_key_unmap(struct wcrypto_rsa_msg *msg, struct wd_queue *q, struct wcrypto_rsa_kg_out *key = (void *)msg->key; uintptr_t phy;
- phy = DMA_ADDR(hw_msg->low_key, hw_msg->hi_key); + phy = DMA_ADDR(hw_msg->hi_key, hw_msg->low_key); phy -= (uintptr_t)va - (uintptr_t)key;
drv_iova_unmap(q, msg->key, (void *)phy, size); @@ -588,7 +593,7 @@ static int fill_dh_g_param(struct wd_queue *q, struct wcrypto_dh_msg *msg, static void dh_g_unmap(struct wcrypto_dh_msg *msg, struct wd_queue *q, struct hisi_hpre_sqe *hw_msg) { - uintptr_t phy = DMA_ADDR(hw_msg->low_in, hw_msg->hi_in); + uintptr_t phy = DMA_ADDR(hw_msg->hi_in, hw_msg->low_in); if (phy) drv_iova_unmap(q, msg->g, (void *)phy, msg->key_bytes); } @@ -596,7 +601,7 @@ static void dh_g_unmap(struct wcrypto_dh_msg *msg, struct wd_queue *q, static void dh_xp_unmap(struct wcrypto_dh_msg *msg, struct wd_queue *q, struct hisi_hpre_sqe *hw_msg) { - uintptr_t phy = DMA_ADDR(hw_msg->low_key, hw_msg->hi_key); + uintptr_t phy = DMA_ADDR(hw_msg->hi_key, hw_msg->low_key);
drv_iova_unmap(q, msg->x_p, (void *)phy, GEN_PARAMS_SZ_UL(msg->key_bytes)); } @@ -999,7 +1004,7 @@ static void ecc_key_unmap(struct wcrypto_ecc_msg *msg, struct wd_queue *q, { uintptr_t phy;
- phy = DMA_ADDR(hw_msg->low_key, hw_msg->hi_key); + phy = DMA_ADDR(hw_msg->hi_key, hw_msg->low_key); drv_iova_unmap(q, va, (void *)phy, size); }
@@ -1577,8 +1582,7 @@ static int ecc_verf_out_transfer(struct wcrypto_ecc_msg *msg, { __u32 result = hw_msg->low_out;
- result >>= 1; - result &= 1; + result = (result >> 1) & 1; if (!result) msg->result = WD_VERIFY_ERR;
@@ -1658,7 +1662,7 @@ static int qm_fill_ecc_sqe_general(void *message, struct qm_queue_info *info, hw_msg = (struct hisi_hpre_sqe *)sqe;
memset(hw_msg, 0, sizeof(struct hisi_hpre_sqe)); - hw_msg->task_len1 = msg->key_bytes / BYTE_BITS - 0x1; + hw_msg->task_len1 = ((msg->key_bytes) >> BYTE_BITS_SHIFT) - 0x1;
/* prepare algorithm */ ret = qm_ecc_prepare_alg(hw_msg, msg); @@ -2318,7 +2322,7 @@ static int sm2_convert_enc_out(struct wcrypto_ecc_msg *src, /* enc origin out data fmt: * | x1y1(2*256bit) | x2y2(2*256bit) | other | * final out data fmt: - * | c1(2*256bit) | c2(plaintext size) | c3(256bit) | + * | c1(2*256bit) | c3(256bit) | c2(plaintext size) | */ x2y2.x.data = (void *)second->out; x2y2.x.dsize = ksz; diff --git a/v1/wd_ecc.c b/v1/wd_ecc.c index c4fab63..4151901 100644 --- a/v1/wd_ecc.c +++ b/v1/wd_ecc.c @@ -428,7 +428,7 @@ static struct wcrypto_ecc_in *create_sm2_sign_in(struct wcrypto_ecc_ctx *ctx,
hsz = get_hw_keysize(ctx->key_size); len = sizeof(struct wcrypto_ecc_in) - + ECC_SIGN_IN_PARAM_NUM * hsz + (__u64)m_len; + + ECC_SIGN_IN_PARAM_NUM * hsz + m_len; in = br_alloc(br, len); if (unlikely(!in)) { WD_ERR("failed to br alloc, sz = %llu!\n", len); @@ -1802,6 +1802,11 @@ static int generate_random(struct wcrypto_ecc_ctx *ctx, struct wd_dtb *k) struct wcrypto_rand_mt *rand_mt = &ctx->setup.rand; int ret;
+ if (!rand_mt->cb) { + WD_ERR("failed to get rand cb!\n"); + return -WD_EINVAL; + } + ret = rand_mt->cb(k->data, k->dsize, rand_mt->usr); if (unlikely(ret)) WD_ERR("failed to rand cb: ret = %d!\n", ret); @@ -1932,7 +1937,7 @@ static struct wcrypto_ecc_in *new_sign_in(struct wcrypto_ecc_ctx *ctx, return NULL;
sin = &ecc_in->param.sin; - if (!k && cx->setup.rand.cb) { + if (!k) { ret = generate_random(cx, &sin->k); if (unlikely(ret)) goto release_in; @@ -2018,7 +2023,7 @@ static struct wcrypto_ecc_in *create_sm2_verf_in(struct wcrypto_ecc_ctx *ctx,
hsz = get_hw_keysize(ctx->key_size); len = sizeof(struct wcrypto_ecc_in) + ECC_VERF_IN_PARAM_NUM * hsz + - (__u64)m_len; + m_len; in = br_alloc(br, len); if (unlikely(!in)) { WD_ERR("failed to br alloc, sz = %llu!\n", len); diff --git a/v1/wd_rsa.c b/v1/wd_rsa.c index 4a2a5b5..97f0c68 100644 --- a/v1/wd_rsa.c +++ b/v1/wd_rsa.c @@ -798,6 +798,9 @@ static int rsa_prikey2_param_set(struct wcrypto_rsa_prikey2 *pkey2, case WD_CRT_PRIKEY_Q: ret = rsa_set_param(&pkey2->q, param); break; + default: + ret = -WD_EINVAL; + break; }
return ret;
1. Remove redundant code. 2. Use safer and more efficient method to distinguish algorithms. 3. Modify return value style.
Signed-off-by: Zhiqi Song songzhiqi1@huawei.com --- drv/hisi_hpre.c | 103 ++++++++++++++++++++++++++++-------------------- wd_dh.c | 16 ++++---- wd_ecc.c | 33 +++++++++------- wd_rsa.c | 14 +++---- 4 files changed, 94 insertions(+), 72 deletions(-)
diff --git a/drv/hisi_hpre.c b/drv/hisi_hpre.c index c7bb70e..d788200 100644 --- a/drv/hisi_hpre.c +++ b/drv/hisi_hpre.c @@ -39,6 +39,8 @@ #define GEN_PARAMS_SZ(key_size) ((key_size) << 1) #define CRT_PARAM_SZ(key_size) ((key_size) >> 1)
+#define WD_TRANS_FAIL 0 + enum hpre_alg_type { HPRE_ALG_NC_NCRT = 0x0, HPRE_ALG_NC_CRT = 0x1, @@ -64,6 +66,12 @@ enum hpre_alg_type { HPRE_ALG_SM2_DEC = 0x15 };
+enum hpre_alg_name { + WD_RSA, + WD_DH, + WD_ECC +}; + /* put vendor hardware message as a user interface is not suitable here */ struct hisi_hpre_sqe { __u32 alg : 5; @@ -101,24 +109,34 @@ struct hisi_hpre_ctx { struct wd_ctx_config_internal config; };
-static void dump_hpre_msg(void *msg, const char *s) +static void dump_hpre_msg(void *msg, int alg) { struct wd_rsa_msg *rsa_msg; struct wd_ecc_msg *ecc_msg; struct wd_dh_msg *dh_msg;
- WD_ERR("dump %s alg message after a task error occurs.\n", s); - - if (!strcmp(s, "rsa")) { + switch (alg) { + case WD_RSA: rsa_msg = (struct wd_rsa_msg *)msg; - WD_ERR("key_bytes:%u key_type:%u\n", rsa_msg->key_bytes, rsa_msg->key_type); - } else if (!strcmp(s, "ecc")) { - ecc_msg = (struct wd_ecc_msg *)msg; - WD_ERR("key_bytes:%u curve_id:%u\n", ecc_msg->key_bytes, ecc_msg->curve_id); - } else if (!strcmp(s, "dh")) { + WD_ERR("dump RSA alg message after a task error occurs\n" + "key_bytes:%u key_type:%u\n", rsa_msg->key_bytes, + rsa_msg->key_type); + break; + case WD_DH: dh_msg = (struct wd_dh_msg *)msg; - WD_ERR("gbytes:%u key_bytes:%u is_g2:%u\n", dh_msg->gbytes, - dh_msg->key_bytes, dh_msg->is_g2); + WD_ERR("dump DH alg message after a task error occurs\n" + "gbytes:%u key_bytes:%u is_g2:%u\n", dh_msg->gbytes, + dh_msg->key_bytes, dh_msg->is_g2); + break; + case WD_ECC: + ecc_msg = (struct wd_ecc_msg *)msg; + WD_ERR("dump ECC alg message after a task error occurs\n" + "key_bytes:%u curve_id:%u\n", ecc_msg->key_bytes, + ecc_msg->curve_id); + break; + default: + WD_ERR("invalid alg!\n"); + break; } }
@@ -181,7 +199,7 @@ static int hpre_bin_to_crypto_bin(char *dst, const char *src, __u32 b_size,
if (!dst || !src || !b_size) { WD_ERR("invalid: %s trans to crypto bin parameters err!\n", p_name); - return 0; + return WD_TRANS_FAIL; }
while (!src[j] && k < (b_size - 1)) @@ -487,7 +505,7 @@ static int hpre_init_qm_priv(struct wd_ctx_config_internal *config, config->ctxs[i].sqn = qm_priv->sqn; }
- return 0; + return WD_SUCCESS; out: for (j = 0; j < i; j++) { h_qp = (handle_t)wd_ctx_get_priv(config->ctxs[j].ctx); @@ -506,7 +524,7 @@ static int hpre_rsa_dh_init(struct wd_alg_driver *drv, void *conf)
if (priv) { /* return if already inited */ - return 0; + return WD_SUCCESS; }
if (!config->ctx_num) { @@ -525,7 +543,7 @@ static int hpre_rsa_dh_init(struct wd_alg_driver *drv, void *conf) return ret; }
- return 0; + return WD_SUCCESS; }
static int hpre_ecc_init(struct wd_alg_driver *drv, void *conf) @@ -537,7 +555,7 @@ static int hpre_ecc_init(struct wd_alg_driver *drv, void *conf)
if (priv) { /* return if already inited */ - return 0; + return WD_SUCCESS; }
if (!config->ctx_num) { @@ -556,7 +574,7 @@ static int hpre_ecc_init(struct wd_alg_driver *drv, void *conf) return ret; }
- return 0; + return WD_SUCCESS; }
static void hpre_exit(struct wd_alg_driver *drv) @@ -676,9 +694,9 @@ static int rsa_recv(struct wd_alg_driver *drv, handle_t ctx, void *rsa_msg) }
if (unlikely(msg->result != WD_SUCCESS)) - dump_hpre_msg(temp_msg, "rsa"); + dump_hpre_msg(temp_msg, WD_RSA);
- return 0; + return WD_SUCCESS; }
static int fill_dh_xp_params(struct wd_dh_msg *msg, @@ -819,9 +837,9 @@ static int dh_recv(struct wd_alg_driver *drv, handle_t ctx, void *dh_msg) }
if (unlikely(msg->result != WD_SUCCESS)) - dump_hpre_msg(temp_msg, "dh"); + dump_hpre_msg(temp_msg, WD_DH);
- return 0; + return WD_SUCCESS; }
static int ecc_prepare_alg(struct wd_ecc_msg *msg, @@ -862,7 +880,7 @@ static int ecc_prepare_alg(struct wd_ecc_msg *msg, return -WD_EINVAL; }
- return 0; + return WD_SUCCESS; }
static int trans_cv_param_to_hpre_bin(struct wd_dtb *p, struct wd_dtb *a, @@ -997,7 +1015,7 @@ static int ecc_prepare_prikey(struct wd_ecc_key *key, void **data, int id)
*data = p->data;
- return 0; + return WD_SUCCESS; }
static int trans_pub_to_hpre_bin(struct wd_ecc_point *pub) @@ -1038,7 +1056,7 @@ static int ecc_prepare_pubkey(struct wd_ecc_key *key, void **data)
*data = p->data;
- return 0; + return WD_SUCCESS; }
static bool is_prikey_used(__u8 op_type) @@ -1071,7 +1089,7 @@ static int ecc_prepare_key(struct wd_ecc_msg *msg, hw_msg->low_key = LW_U32((uintptr_t)data); hw_msg->hi_key = HI_U32((uintptr_t)data);
- return 0; + return WD_SUCCESS; }
static void ecc_get_io_len(__u32 atype, __u32 hsz, size_t *ilen, @@ -1167,7 +1185,7 @@ static int ecc_prepare_sign_in(struct wd_ecc_msg *msg,
*data = e->data;
- return 0; + return WD_SUCCESS; }
static int ecc_prepare_verf_in(struct wd_ecc_msg *msg, @@ -1209,7 +1227,7 @@ static int ecc_prepare_verf_in(struct wd_ecc_msg *msg,
*data = e->data;
- return 0; + return WD_SUCCESS; }
static int sm2_prepare_enc_in(struct wd_ecc_msg *msg, @@ -1232,7 +1250,7 @@ static int sm2_prepare_enc_in(struct wd_ecc_msg *msg, hw_msg->sm2_mlen = ein->plaintext.dsize - 1; *data = k->data;
- return 0; + return WD_SUCCESS; }
static int sm2_prepare_dec_in(struct wd_ecc_msg *msg, @@ -1256,7 +1274,7 @@ static int sm2_prepare_dec_in(struct wd_ecc_msg *msg, hw_msg->sm2_mlen = din->c2.dsize - 1; *data = c1->x.data;
- return 0; + return WD_SUCCESS; }
static int ecc_prepare_dh_gen_in(struct wd_ecc_msg *msg, @@ -1277,7 +1295,7 @@ static int ecc_prepare_dh_gen_in(struct wd_ecc_msg *msg,
*data = in->x.data;
- return 0; + return WD_SUCCESS; }
static int ecc_prepare_dh_compute_in(struct wd_ecc_msg *msg, @@ -1299,7 +1317,7 @@ static int ecc_prepare_dh_compute_in(struct wd_ecc_msg *msg,
*data = pbk->x.data;
- return 0; + return WD_SUCCESS; }
static int u_is_in_p(struct wd_ecc_msg *msg) @@ -1333,7 +1351,7 @@ static int u_is_in_p(struct wd_ecc_msg *msg) return -WD_EINVAL; }
- return 0; + return WD_SUCCESS; }
static int ecc_prepare_in(struct wd_ecc_msg *msg, @@ -1390,7 +1408,7 @@ static int ecc_prepare_dh_out(struct wd_ecc_out *out, void **data)
*data = dh_out->x.data;
- return 0; + return WD_SUCCESS; }
static int ecc_prepare_out(struct wd_ecc_msg *msg, void **data) @@ -1458,13 +1476,14 @@ static int ecc_prepare_iot(struct wd_ecc_msg *msg, return ret; }
+ /* op_type WD_ECDSA_VERIFY and WD_SM2_VERIFY do not need data */ if (!data) - return 0; + return WD_SUCCESS;
hw_msg->low_out = LW_U32((uintptr_t)data); hw_msg->hi_out = HI_U32((uintptr_t)data);
- return 0; + return WD_SUCCESS; }
static __u32 get_hw_keysz(__u32 ksz) @@ -1526,7 +1545,7 @@ static int set_param(struct wd_dtb *dst, const struct wd_dtb *src, memset(dst->data, 0, dst->bsize); memcpy(dst->data, src->data, src->dsize);
- return 0; + return WD_SUCCESS; }
static int set_prikey(struct wd_ecc_prikey *prikey, @@ -1592,7 +1611,6 @@ static struct wd_ecc_out *create_ecdh_out(struct wd_ecc_msg *msg) dh_out->out.y.data = out->data; dh_out->out.y.dsize = msg->key_bytes; dh_out->out.y.bsize = hsz; - out->size = data_sz;
memcpy(out->data + data_sz, &msg, sizeof(void *));
@@ -1619,7 +1637,7 @@ static int init_req(struct wd_ecc_msg *dst, struct wd_ecc_msg *src, else dst->req.src = (void *)&pubkey->pub;
- return 0; + return WD_SUCCESS; }
static struct wd_ecc_msg *create_req(struct wd_ecc_msg *src, __u8 req_idx) @@ -1695,7 +1713,7 @@ static int split_req(struct wd_ecc_msg *src, struct wd_ecc_msg **dst) return -WD_ENOMEM; }
- return 0; + return WD_SUCCESS; }
static int ecc_fill(struct wd_ecc_msg *msg, struct hisi_hpre_sqe *hw_msg) @@ -1935,8 +1953,7 @@ static int ecc_verf_out_transfer(struct wd_ecc_msg *msg, { __u32 result = hw_msg->low_out;
- result >>= 1; - result &= 1; + result = (result >> 1) & 1; if (!result) msg->result = WD_VERIFY_ERR;
@@ -2317,7 +2334,7 @@ static int ecc_sqe_parse(struct hisi_qp *qp, struct wd_ecc_msg *msg, return ret;
dump_err_msg: - dump_hpre_msg(temp_msg, "ecc"); + dump_hpre_msg(temp_msg, WD_ECC);
return ret; } @@ -2470,7 +2487,7 @@ static int ecc_recv(struct wd_alg_driver *drv, handle_t ctx, void *ecc_msg)
static int hpre_get_usage(void *param) { - return 0; + return WD_SUCCESS; }
#define GEN_HPRE_ALG_DRIVER(hpre_alg_name) \ diff --git a/wd_dh.c b/wd_dh.c index 40a52e5..3eb5f6d 100644 --- a/wd_dh.c +++ b/wd_dh.c @@ -77,7 +77,7 @@ static int wd_dh_open_driver(void)
wd_dh_setting.driver = driver;
- return 0; + return WD_SUCCESS; }
static void wd_dh_clear_status(void) @@ -114,7 +114,7 @@ static int wd_dh_common_init(struct wd_ctx_config *config, struct wd_sched *sche if (ret) goto out_clear_pool;
- return 0; + return WD_SUCCESS;
out_clear_pool: wd_uninit_async_request_pool(&wd_dh_setting.pool); @@ -135,7 +135,7 @@ static int wd_dh_common_uninit(void) wd_alg_uninit_driver(&wd_dh_setting.config, wd_dh_setting.driver);
- return 0; + return WD_SUCCESS; }
int wd_dh_init(struct wd_ctx_config *config, struct wd_sched *sched) @@ -162,7 +162,7 @@ int wd_dh_init(struct wd_ctx_config *config, struct wd_sched *sched)
wd_alg_set_init(&wd_dh_setting.status);
- return 0; + return WD_SUCCESS;
out_close_driver: wd_dh_close_driver(); @@ -262,7 +262,7 @@ int wd_dh_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_param wd_alg_set_init(&wd_dh_setting.status); wd_ctx_param_uninit(&dh_ctx_params);
- return 0; + return WD_SUCCESS;
out_params_uninit: wd_ctx_param_uninit(&dh_ctx_params); @@ -318,7 +318,7 @@ static int fill_dh_msg(struct wd_dh_msg *msg, struct wd_dh_req *req, return -WD_EINVAL; }
- return 0; + return WD_SUCCESS; }
int wd_do_dh_sync(handle_t sess, struct wd_dh_req *req) @@ -414,7 +414,7 @@ int wd_do_dh_async(handle_t sess, struct wd_dh_req *req) if (ret) goto fail_with_msg;
- return 0; + return WD_SUCCESS;
fail_with_msg: wd_put_msg_to_pool(&wd_dh_setting.pool, idx, mid); @@ -502,7 +502,7 @@ int wd_dh_get_mode(handle_t sess, __u8 *alg_mode)
*alg_mode = ((struct wd_dh_sess *)sess)->setup.is_g2;
- return 0; + return WD_SUCCESS; }
__u32 wd_dh_key_bits(handle_t sess) diff --git a/wd_ecc.c b/wd_ecc.c index 4323e54..8c053fd 100644 --- a/wd_ecc.c +++ b/wd_ecc.c @@ -131,7 +131,7 @@ static int wd_ecc_open_driver(void)
wd_ecc_setting.driver = driver;
- return 0; + return WD_SUCCESS; }
static bool is_alg_support(const char *alg) @@ -177,7 +177,7 @@ static int wd_ecc_common_init(struct wd_ctx_config *config, struct wd_sched *sch if (ret) goto out_clear_pool;
- return 0; + return WD_SUCCESS;
out_clear_pool: wd_uninit_async_request_pool(&wd_ecc_setting.pool); @@ -198,7 +198,7 @@ static int wd_ecc_common_uninit(void) wd_alg_uninit_driver(&wd_ecc_setting.config, wd_ecc_setting.driver);
- return 0; + return WD_SUCCESS; }
int wd_ecc_init(struct wd_ctx_config *config, struct wd_sched *sched) @@ -225,7 +225,7 @@ int wd_ecc_init(struct wd_ctx_config *config, struct wd_sched *sched)
wd_alg_set_init(&wd_ecc_setting.status);
- return 0; + return WD_SUCCESS;
out_close_driver: wd_ecc_close_driver(); @@ -327,7 +327,7 @@ int wd_ecc_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_para wd_alg_set_init(&wd_ecc_setting.status); wd_ctx_param_uninit(&ecc_ctx_params);
- return 0; + return WD_SUCCESS;
out_params_uninit: wd_ctx_param_uninit(&ecc_ctx_params); @@ -819,7 +819,7 @@ static int set_param_single(struct wd_dtb *dst, const struct wd_dtb *src, memset(dst->data, 0, dst->bsize); memcpy(dst->data, src->data, src->dsize);
- return 0; + return WD_SUCCESS; }
int wd_ecc_get_key_bits(handle_t sess) @@ -897,7 +897,7 @@ static int set_curve_param(struct wd_ecc_key *key, return -WD_EINVAL; }
- return 0; + return WD_SUCCESS; }
static const struct wd_ecc_curve_list *find_curve_list(__u32 id) @@ -934,7 +934,7 @@ static int fill_param_by_id(struct wd_ecc_curve *c, key_size = BITS_TO_BYTES(item->key_bits); memcpy(c->p.data, item->data, CURVE_PARAM_NUM * key_size);
- return 0; + return WD_SUCCESS; }
static void setup_curve_cfg(struct wd_ecc_sess_setup *setup) @@ -1056,7 +1056,7 @@ static int create_sess_key(struct wd_ecc_sess_setup *setup, goto free_d; }
- return 0; + return WD_SUCCESS;
free_d: release_ecc_d(sess); @@ -1107,7 +1107,7 @@ static int setup_param_check(struct wd_ecc_sess_setup *setup) return -WD_EINVAL; }
- return 0; + return WD_SUCCESS; }
static void del_sess_key(struct wd_ecc_sess *sess) @@ -1619,7 +1619,7 @@ static int set_sign_in_param(struct wd_ecc_sign_in *sin, return ret; }
- return 0; + return WD_SUCCESS; }
static int generate_random(struct wd_ecc_sess *sess, struct wd_dtb *k) @@ -1627,6 +1627,11 @@ static int generate_random(struct wd_ecc_sess *sess, struct wd_dtb *k) struct wd_rand_mt rand_t = sess->setup.rand; int ret;
+ if (!rand_t.cb) { + WD_ERR("failed to get rand cb!\n"); + return -WD_EINVAL; + } + ret = rand_t.cb(k->data, k->dsize, rand_t.usr); if (ret) WD_ERR("failed to do rand cb, ret = %d!\n", ret); @@ -1756,7 +1761,7 @@ static struct wd_ecc_in *new_sign_in(struct wd_ecc_sess *sess, return NULL;
sin = &ecc_in->param.sin; - if (!k && sess_t->setup.rand.cb) { + if (!k) { ret = generate_random(sess_t, &sin->k); if (ret) goto release_in; @@ -1818,7 +1823,7 @@ static int set_verf_in_param(struct wd_ecc_verf_in *vin, if (ret) return ret;
- return 0; + return WD_SUCCESS; }
static struct wd_ecc_in *create_sm2_verf_in(struct wd_ecc_sess *sess, @@ -2250,7 +2255,7 @@ int wd_do_ecc_async(handle_t sess, struct wd_ecc_req *req) if (ret) goto fail_with_msg;
- return 0; + return WD_SUCCESS;
fail_with_msg: wd_put_msg_to_pool(&wd_ecc_setting.pool, idx, mid); diff --git a/wd_rsa.c b/wd_rsa.c index 1813676..7a16f05 100644 --- a/wd_rsa.c +++ b/wd_rsa.c @@ -116,7 +116,7 @@ static int wd_rsa_open_driver(void)
wd_rsa_setting.driver = driver;
- return 0; + return WD_SUCCESS; }
static void wd_rsa_clear_status(void) @@ -152,7 +152,7 @@ static int wd_rsa_common_init(struct wd_ctx_config *config, struct wd_sched *sch if (ret) goto out_clear_pool;
- return 0; + return WD_SUCCESS;
out_clear_pool: wd_uninit_async_request_pool(&wd_rsa_setting.pool); @@ -173,7 +173,7 @@ static int wd_rsa_common_uninit(void) wd_alg_uninit_driver(&wd_rsa_setting.config, wd_rsa_setting.driver);
- return 0; + return WD_SUCCESS; }
int wd_rsa_init(struct wd_ctx_config *config, struct wd_sched *sched) @@ -200,7 +200,7 @@ int wd_rsa_init(struct wd_ctx_config *config, struct wd_sched *sched)
wd_alg_set_init(&wd_rsa_setting.status);
- return 0; + return WD_SUCCESS;
out_close_driver: wd_rsa_close_driver(); @@ -300,7 +300,7 @@ int wd_rsa_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_para wd_alg_set_init(&wd_rsa_setting.status); wd_ctx_param_uninit(&rsa_ctx_params);
- return 0; + return WD_SUCCESS;
out_params_uninit: wd_ctx_param_uninit(&rsa_ctx_params); @@ -377,7 +377,7 @@ static int fill_rsa_msg(struct wd_rsa_msg *msg, struct wd_rsa_req *req,
msg->key = key;
- return 0; + return WD_SUCCESS; }
int wd_do_rsa_sync(handle_t h_sess, struct wd_rsa_req *req) @@ -473,7 +473,7 @@ int wd_do_rsa_async(handle_t sess, struct wd_rsa_req *req) if (ret) goto fail_with_msg;
- return 0; + return WD_SUCCESS;
fail_with_msg: wd_put_msg_to_pool(&wd_rsa_setting.pool, idx, mid);
Add setup parameter check when create ctx, and remove some redundant code.
Signed-off-by: Zhiqi Song songzhiqi1@huawei.com --- v1/drv/hisi_hpre_udrv.c | 9 ++++----- v1/wd_dh.c | 3 ++- v1/wd_ecc.c | 3 ++- v1/wd_rsa.c | 3 ++- 4 files changed, 10 insertions(+), 8 deletions(-)
diff --git a/v1/drv/hisi_hpre_udrv.c b/v1/drv/hisi_hpre_udrv.c index 92ca8fa..00ea665 100644 --- a/v1/drv/hisi_hpre_udrv.c +++ b/v1/drv/hisi_hpre_udrv.c @@ -292,7 +292,6 @@ static int qm_rsa_out_transfer(struct wcrypto_rsa_msg *msg, if (hw_msg->alg == HPRE_ALG_KG_CRT) { msg->out_bytes = CRT_GEN_PARAMS_SZ(kbytes); *in_len = GEN_PARAMS_SZ_UL(kbytes); - *out_len = msg->out_bytes; wcrypto_get_rsa_kg_out_crt_params(key, &qinv, &dq, &dp); ret = qm_tri_bin_transfer(&qinv, &dq, &dp, "rsa kg qinv&dp&dq"); if (unlikely(ret)) @@ -302,9 +301,7 @@ static int qm_rsa_out_transfer(struct wcrypto_rsa_msg *msg, dq.dsize, dp.dsize); } else if (hw_msg->alg == HPRE_ALG_KG_STD) { msg->out_bytes = GEN_PARAMS_SZ(kbytes); - *out_len = msg->out_bytes; *in_len = GEN_PARAMS_SZ_UL(kbytes); - wcrypto_get_rsa_kg_out_params(key, &d, &n); ret = qm_tri_bin_transfer(&d, &n, NULL, "rsa kg d & n"); if (unlikely(ret)) @@ -314,8 +311,10 @@ static int qm_rsa_out_transfer(struct wcrypto_rsa_msg *msg, } else { *in_len = kbytes; msg->out_bytes = kbytes; - *out_len = msg->out_bytes; } + + *out_len = msg->out_bytes; + return WD_SUCCESS; }
@@ -1913,7 +1912,7 @@ static int fill_sm2_enc_sqe(void *msg, struct qm_queue_info *info, __u16 idx) }
/* split message into two inner request msg - * firest msg used to compute k * g + * first msg used to compute k * g * second msg used to compute k * pb */ ret = split_req(info, req_src, req_dst); diff --git a/v1/wd_dh.c b/v1/wd_dh.c index 9ed0e0d..aab26d6 100644 --- a/v1/wd_dh.c +++ b/v1/wd_dh.c @@ -61,7 +61,8 @@ static int create_ctx_param_check(struct wd_queue *q, return -WD_EINVAL; }
- if (!setup->br.alloc || !setup->br.free) { + if (!setup->br.alloc || !setup->br.free || + !setup->br.iova_map || !setup->br.iova_unmap) { WD_ERR("create dh ctx user mm br err!\n"); return -WD_EINVAL; } diff --git a/v1/wd_ecc.c b/v1/wd_ecc.c index 4151901..36c507a 100644 --- a/v1/wd_ecc.c +++ b/v1/wd_ecc.c @@ -1011,7 +1011,8 @@ static int param_check(struct wd_queue *q, struct wcrypto_ecc_ctx_setup *setup) return -WD_EINVAL; }
- if (unlikely(!setup->br.alloc || !setup->br.free)) { + if (unlikely(!setup->br.alloc || !setup->br.free || + !setup->br.iova_map || !setup->br.iova_unmap)) { WD_ERR("user mm br error!\n"); return -WD_EINVAL; } diff --git a/v1/wd_rsa.c b/v1/wd_rsa.c index 97f0c68..efa02ac 100644 --- a/v1/wd_rsa.c +++ b/v1/wd_rsa.c @@ -554,7 +554,8 @@ static int check_q_setup(struct wd_queue *q, struct wcrypto_rsa_ctx_setup *setup return -WD_EINVAL; }
- if (!setup->br.alloc || !setup->br.free) { + if (!setup->br.alloc || !setup->br.free || + !setup->br.iova_map || !setup->br.iova_unmap) { WD_ERR("create rsa ctx user mm br err!\n"); return -WD_EINVAL; }
Fix the problem that can not get random k when do not set the value or callback function. There are three ways to use random k in sm2 sign, fix to support them all in v1 and v2: - If k is not set and cb has been set, use cb to generate random k. - If k is set or has been generated by cb, directly set k. - If k and cb are not set, hw driver should config to generate random k.
Signed-off-by: Zhiqi Song songzhiqi1@huawei.com --- v1/wd_ecc.c | 17 +++++++++-------- wd_ecc.c | 15 +++++++++------ 2 files changed, 18 insertions(+), 14 deletions(-)
diff --git a/v1/wd_ecc.c b/v1/wd_ecc.c index 36c507a..8486007 100644 --- a/v1/wd_ecc.c +++ b/v1/wd_ecc.c @@ -1803,11 +1803,6 @@ static int generate_random(struct wcrypto_ecc_ctx *ctx, struct wd_dtb *k) struct wcrypto_rand_mt *rand_mt = &ctx->setup.rand; int ret;
- if (!rand_mt->cb) { - WD_ERR("failed to get rand cb!\n"); - return -WD_EINVAL; - } - ret = rand_mt->cb(k->data, k->dsize, rand_mt->usr); if (unlikely(ret)) WD_ERR("failed to rand cb: ret = %d!\n", ret); @@ -1938,14 +1933,20 @@ static struct wcrypto_ecc_in *new_sign_in(struct wcrypto_ecc_ctx *ctx, return NULL;
sin = &ecc_in->param.sin; - if (!k) { + sin->k_set = 0; + sin->dgst_set = 0; + + /* + * If k is not set and cb has been set, use cb to generate random k. + * If k is set or has been generated by cb, directly set k. + * If k and cb are not set, hw driver should config to generate random k. + */ + if (!k && cx->setup.rand.cb) { ret = generate_random(cx, &sin->k); if (unlikely(ret)) goto release_in; }
- sin->k_set = 0; - sin->dgst_set = 0; if (k || cx->setup.rand.cb) sin->k_set = 1;
diff --git a/wd_ecc.c b/wd_ecc.c index 8c053fd..cf2cbae 100644 --- a/wd_ecc.c +++ b/wd_ecc.c @@ -1627,11 +1627,6 @@ static int generate_random(struct wd_ecc_sess *sess, struct wd_dtb *k) struct wd_rand_mt rand_t = sess->setup.rand; int ret;
- if (!rand_t.cb) { - WD_ERR("failed to get rand cb!\n"); - return -WD_EINVAL; - } - ret = rand_t.cb(k->data, k->dsize, rand_t.usr); if (ret) WD_ERR("failed to do rand cb, ret = %d!\n", ret); @@ -1761,7 +1756,15 @@ static struct wd_ecc_in *new_sign_in(struct wd_ecc_sess *sess, return NULL;
sin = &ecc_in->param.sin; - if (!k) { + sin->k_set = 0; + sin->dgst_set = 0; + + /* + * If k is not set and cb has been set, use cb to generate random k. + * If k is set or has been generated by cb, directly set k. + * If k and cb are not set, hw driver should config to generate random k. + */ + if (!k && sess_t->setup.rand.cb) { ret = generate_random(sess_t, &sin->k); if (ret) goto release_in;
Cleanup the following issues: 1. Add const to static variables that do not change. 2. Modify indentation and whitespace. 3. Put macro definitions at the beginning of the file. 4. Splitting judgment conditions to make it clearer.
Signed-off-by: Zhiqi Song songzhiqi1@huawei.com --- v1/drv/hisi_qm_udrv.c | 16 ++++++++-------- v1/wd_adapter.c | 18 +++++++++--------- v1/wd_bmm.c | 13 ++++++------- v1/wd_sgl.c | 27 ++++++++++++++++++++++----- 4 files changed, 45 insertions(+), 29 deletions(-)
diff --git a/v1/drv/hisi_qm_udrv.c b/v1/drv/hisi_qm_udrv.c index 6cbcdf5..031a2e1 100644 --- a/v1/drv/hisi_qm_udrv.c +++ b/v1/drv/hisi_qm_udrv.c @@ -244,7 +244,7 @@ static int qm_set_queue_regions(struct wd_queue *q) if (info->sq_base == MAP_FAILED) { info->sq_base = NULL; WD_ERR("mmap dus fail\n"); - return -ENOMEM; + return -WD_ENOMEM; }
info->mmio_base = wd_drv_mmap_qfr(q, WD_UACCE_QFRT_MMIO, 0); @@ -253,7 +253,7 @@ static int qm_set_queue_regions(struct wd_queue *q) info->sq_base = NULL; info->mmio_base = NULL; WD_ERR("mmap mmio fail\n"); - return -ENOMEM; + return -WD_ENOMEM; }
return 0; @@ -439,7 +439,7 @@ static int qm_set_db_info(struct q_info *qinfo) struct qm_queue_info *info = qinfo->priv;
if (strstr(qinfo->hw_type, HISI_QM_API_VER2_BASE) || - strstr(qinfo->hw_type, HISI_QM_API_VER3_BASE)) { + strstr(qinfo->hw_type, HISI_QM_API_VER3_BASE)) { info->db = qm_db_v2; info->doorbell_base = info->mmio_base + QM_V2_DOORBELL_OFFSET; } else if (strstr(qinfo->hw_type, HISI_QM_API_VER_BASE)) { @@ -447,7 +447,7 @@ static int qm_set_db_info(struct q_info *qinfo) info->doorbell_base = info->mmio_base + QM_DOORBELL_OFFSET; } else { WD_ERR("hw version mismatch!\n"); - return -EINVAL; + return -WD_EINVAL; }
return 0; @@ -505,10 +505,10 @@ static int qm_set_queue_info(struct wd_queue *q)
ret = qm_set_queue_regions(q); if (ret) - return -EINVAL; + return -WD_EINVAL; if (!info->sqe_size) { WD_ERR("sqe size =%d err!\n", info->sqe_size); - ret = -EINVAL; + ret = -WD_EINVAL; goto err_with_regions; } info->cq_base = (void *)((uintptr_t)info->sq_base + @@ -520,7 +520,7 @@ static int qm_set_queue_info(struct wd_queue *q) ret = mprotect(info->cq_base, psize, PROT_READ); if (ret) { WD_ERR("cqe mprotect set err!\n"); - ret = -EINVAL; + ret = -WD_EINVAL; goto err_with_regions; }
@@ -549,7 +549,7 @@ int qm_init_queue(struct wd_queue *q) { struct q_info *qinfo = q->qinfo; struct qm_queue_info *info; - int ret = -ENOMEM; + int ret = -WD_ENOMEM;
info = calloc(1, sizeof(*info)); if (!info) { diff --git a/v1/wd_adapter.c b/v1/wd_adapter.c index 6b0b4d8..b9d0c0e 100644 --- a/v1/wd_adapter.c +++ b/v1/wd_adapter.c @@ -28,7 +28,7 @@ #define __ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask)) #define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
-static struct wd_drv_dio_if hw_dio_tbl[] = { { +static const struct wd_drv_dio_if hw_dio_tbl[] = { { .hw_type = "dummy_v1", .open = dummy_set_queue_dio, .close = dummy_unset_queue_dio, @@ -155,13 +155,13 @@ void drv_free_slice(struct wd_queue *q) struct q_info *qinfo = q->qinfo; struct wd_ss_region *rgn;
- while (true) { - rgn = TAILQ_FIRST(&qinfo->ss_list); - if (!rgn) - break; - TAILQ_REMOVE(&qinfo->ss_list, rgn, next); - free(rgn); - } + while (true) { + rgn = TAILQ_FIRST(&qinfo->ss_list); + if (!rgn) + break; + TAILQ_REMOVE(&qinfo->ss_list, rgn, next); + free(rgn); + } }
void drv_add_slice(struct wd_queue *q, struct wd_ss_region *rgn) @@ -188,7 +188,7 @@ void drv_show_ss_slices(struct wd_queue *q) int i = 0;
TAILQ_FOREACH(rgn, qinfo->head, next) { - WD_ERR("slice-%d:size=0x%lx\n", i, rgn->size); + WD_ERR("slice-%d:size = 0x%lx\n", i, rgn->size); i++; } } diff --git a/v1/wd_bmm.c b/v1/wd_bmm.c index c98c487..cdf5f0b 100644 --- a/v1/wd_bmm.c +++ b/v1/wd_bmm.c @@ -29,8 +29,12 @@ #define __ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask)) #define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
-#define TAG_FREE 0x12345678 /* block is free */ -#define TAG_USED 0x87654321 /* block is busy */ +#define TAG_FREE 0x12345678 /* block is free */ +#define TAG_USED 0x87654321 /* block is busy */ +#define MAX_ALIGN_SIZE 0x1000 /* 4KB */ +#define MAX_BLOCK_SIZE 0x10000000 /* 256MB */ +#define BLK_BALANCE_SZ 0x100000ul +#define NUM_TIMES(x) (87 * (x) / 100)
struct wd_blk_hd { unsigned int blk_tag; @@ -67,9 +71,6 @@ static struct wd_blk_hd *wd_blk_head(struct wd_blkpool *pool, void *blk)
static int pool_params_check(struct wd_blkpool_setup *setup) { -#define MAX_ALIGN_SIZE 0x1000 /* 4KB */ -#define MAX_BLOCK_SIZE 0x10000000 /* 256MB */ - if (!setup->block_num || !setup->block_size || setup->block_size > MAX_BLOCK_SIZE) { WD_ERR("Invalid block_size or block_num(%x, %u)!\n", @@ -103,7 +104,6 @@ static int wd_pool_pre_layout(struct wd_queue *q, if (!sp->br.alloc) qinfo = q->qinfo;
-#define BLK_BALANCE_SZ 0x100000ul ret = pool_params_check(sp); if (ret) return ret; @@ -171,7 +171,6 @@ static int wd_pool_init(struct wd_queue *q, struct wd_blkpool *p) * if dma_num <= (1 / 1.15) * user's block_num, we think the pool * is created with failure. */ -#define NUM_TIMES(x) (87 * (x) / 100) if (dma_num <= NUM_TIMES(p->setup.block_num)) { WD_ERR("dma_num = %u, not enough.\n", dma_num); return -WD_EINVAL; diff --git a/v1/wd_sgl.c b/v1/wd_sgl.c index 97e4b73..cb3b8ee 100644 --- a/v1/wd_sgl.c +++ b/v1/wd_sgl.c @@ -349,11 +349,28 @@ static int sgl_params_check(struct wd_sglpool_setup *setup) struct wd_sglpool_setup *sp = setup; __u32 buf_num_need;
- if (!sp->buf_num || !sp->sgl_num || !sp->sge_num_in_sgl || - !sp->buf_num_in_sgl || sp->buf_size < BUF_SIZE_MAX || - sp->buf_num_in_sgl > sp->sge_num_in_sgl || - sp->sgl_num > SGL_NUM_MAX || sp->sge_num_in_sgl > SGE_NUM_MAX) { - WD_ERR("invalid size or num in sgl!\n"); + if (!sp->sgl_num || sp->sgl_num > SGL_NUM_MAX) { + WD_ERR("invalid sgl_num, %u!\n", sp->sgl_num); + return -WD_EINVAL; + } + + if (!sp->sge_num_in_sgl || sp->sge_num_in_sgl > SGE_NUM_MAX) { + WD_ERR("invlaid sge_num_in_sgl, %u\n!", sp->sge_num_in_sgl); + return -WD_EINVAL; + } + + if (!sp->buf_num) { + WD_ERR("invalid buf_num, %u!\n", sp->buf_num); + return -WD_EINVAL; + } + + if (sp->buf_size < BUF_SIZE_MAX) { + WD_ERR("invalid buf_size, %u!\n", sp->buf_size); + return -WD_EINVAL; + } + + if (!sp->buf_num_in_sgl || sp->buf_num_in_sgl > sp->sge_num_in_sgl) { + WD_ERR("invalid buf_num_in_sgl, %u!\n", sp->buf_num_in_sgl); return -WD_EINVAL; }
Add return value judgement of aead_get_aes_key_len().
Signed-off-by: Zhiqi Song songzhiqi1@huawei.com --- drv/hisi_sec.c | 4 ++++ 1 file changed, 4 insertions(+)
diff --git a/drv/hisi_sec.c b/drv/hisi_sec.c index 03e7037..f53823b 100644 --- a/drv/hisi_sec.c +++ b/drv/hisi_sec.c @@ -2069,6 +2069,8 @@ static int fill_aead_bd2_alg(struct wd_aead_msg *msg, case WD_CIPHER_AES: sqe->type2.c_alg = C_ALG_AES; ret = aead_get_aes_key_len(msg, &c_key_len); + if (ret) + return ret; sqe->type2.icvw_kmode = (__u16)c_key_len << SEC_CKEY_OFFSET; break; default: @@ -2638,6 +2640,8 @@ static int fill_aead_bd3_alg(struct wd_aead_msg *msg, case WD_CIPHER_AES: sqe->c_mode_alg |= C_ALG_AES << SEC_CALG_OFFSET_V3; ret = aead_get_aes_key_len(msg, &c_key_len); + if (ret) + return ret; sqe->c_icv_key |= (__u16)c_key_len << SEC_CKEY_OFFSET_V3; break; default: