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);