compress/cipher/rsa/ecc already support the init2 and dynamic load, now add support for digest/aead algs.
--- v3->v4: 1)fix bug alg match for init2.
v2->v3: 1)add alg match for init2.
v1->v2: 1)fix bug for aead init testcase. 2)tinyfix for code style. --- Hao Fang (6): uadk/digest: introduce the init2 interface for digest uadk/aead: introduce the init2 interface for aead uadk/digest: adapt the module dynamic load for the digest algs uadk/aead: adadpt the module dynamic load for aead algs uadk/tools: add init2 test for digest algs uadk/tool: add init2 test for aead algs
drv/hisi_sec.c | 105 +++++++++---- include/drv/wd_aead_drv.h | 26 ---- include/drv/wd_digest_drv.h | 26 ---- include/wd_aead.h | 28 ++++ include/wd_digest.h | 28 ++++ libwd_crypto.map | 6 + uadk_tool/test/test_sec.c | 249 ++++++++++++++++++++++++++++--- wd_aead.c | 288 +++++++++++++++++++++++++++--------- wd_digest.c | 286 ++++++++++++++++++++++++++--------- 9 files changed, 794 insertions(+), 248 deletions(-)
The basic init process is complex for users who need to know the device, scheduler, etc.
So introduce the init2 interface just for simplifying the initialization process when user use the digest algorithm.
Signed-off-by: Hao Fang fanghao11@huawei.com --- include/wd_digest.h | 28 +++++++++++ libwd_crypto.map | 3 ++ wd_digest.c | 113 +++++++++++++++++++++++++++++++++++++------- 3 files changed, 127 insertions(+), 17 deletions(-)
diff --git a/include/wd_digest.h b/include/wd_digest.h index a44328e..f938295 100644 --- a/include/wd_digest.h +++ b/include/wd_digest.h @@ -142,6 +142,34 @@ struct wd_digest_tag {
int wd_digest_init(struct wd_ctx_config *config, struct wd_sched *sched); void wd_digest_uninit(void); +/** + * wd_digest_init2_() - A simplify interface to initializate uadk + * digest operation. This interface keeps most functions of + * wd_digest_init(). Users just need to descripe the deployment of + * business scenarios. Then the initialization will request appropriate + * resources to support the business scenarios. + * To make the initializate simpler, ctx_params support set NULL. + * And then the function will set them as driver's default. + * Please do not use this interface with wd_digest_init() together, or + * some resources may be leak. + * + * @alg: The algorithm users want to use. + * @sched_type: The scheduling type users want to use. + * @task_type: Task types, including soft computing, hardware and hybrid computing. + * @ctx_params: The ctxs resources users want to use. Include per operation + * type ctx numbers and business process run numa. + * + * Return 0 if succeed and others if fail. + */ +int wd_digest_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_params *ctx_params); + +#define wd_digest_init2(alg, sched_type, task_type) \ + wd_digest_init2(alg, sched_type, task_type, NULL) + +/** + * wd_digest_uninit2() - Uninitialise ctx configuration and scheduler. + */ +void wd_digest_uninit2(void);
/** * wd_digest_alloc_sess() - Create a digest session. diff --git a/libwd_crypto.map b/libwd_crypto.map index a5dd688..e28dd79 100644 --- a/libwd_crypto.map +++ b/libwd_crypto.map @@ -45,6 +45,9 @@ global:
wd_digest_init; wd_digest_uninit; + wd_digest_init2; + wd_digest_init2_; + wd_digest_uninit2; wd_digest_alloc_sess; wd_digest_free_sess; wd_do_digest_sync; diff --git a/wd_digest.c b/wd_digest.c index 8c01709..03d3ace 100644 --- a/wd_digest.c +++ b/wd_digest.c @@ -66,6 +66,17 @@ struct wd_digest_sess { };
struct wd_env_config wd_digest_env_config; +static struct wd_init_attrs wd_digest_init_attrs; + +static struct wd_ctx_nums wd_digest_ctx_num[] = { + {1, 1}, {} +}; + +static struct wd_ctx_params wd_digest_ctx_params = { + .op_type_num = 1, + .ctx_set_num = wd_digest_ctx_num, + .bmp = NULL, +};
#ifdef WD_STATIC_DRV static void wd_digest_set_static_drv(void) @@ -188,30 +199,20 @@ static void wd_digest_clear_status(void) wd_alg_clear_init(&wd_digest_setting.status); }
-int wd_digest_init(struct wd_ctx_config *config, struct wd_sched *sched) +static int wd_digest_init_nolock(struct wd_ctx_config *config, + struct wd_sched *sched) { void *priv; - bool flag; int ret;
- pthread_atfork(NULL, NULL, wd_digest_clear_status); - - flag = wd_alg_try_init(&wd_digest_setting.status); - if (!flag) - return 0; - - ret = wd_init_param_check(config, sched); - if (ret) - goto out_clear_init; - ret = wd_set_epoll_en("WD_DIGEST_EPOLL_EN", &wd_digest_setting.config.epoll_en); if (ret < 0) - goto out_clear_init; + return ret;
ret = wd_init_ctx_config(&wd_digest_setting.config, config); if (ret < 0) - goto out_clear_init; + return ret;
ret = wd_init_sched(&wd_digest_setting.sched, sched); if (ret < 0) @@ -243,8 +244,6 @@ int wd_digest_init(struct wd_ctx_config *config, struct wd_sched *sched) goto out_free_priv; }
- wd_alg_set_init(&wd_digest_setting.status); - return 0;
out_free_priv: @@ -256,12 +255,39 @@ out_clear_sched: wd_clear_sched(&wd_digest_setting.sched); out_clear_ctx_config: wd_clear_ctx_config(&wd_digest_setting.config); + + return ret; +} + +int wd_digest_init(struct wd_ctx_config *config, struct wd_sched *sched) +{ + bool flag; + int ret; + + pthread_atfork(NULL, NULL, wd_digest_clear_status); + + flag = wd_alg_try_init(&wd_digest_setting.status); + if (!flag) + return 0; + + ret = wd_init_param_check(config, sched); + if (ret) + goto out_clear_init; + + ret = wd_digest_init_nolock(config, sched); + if (ret) + goto out_clear_init; + + wd_alg_set_init(&wd_digest_setting.status); + + return 0; + out_clear_init: wd_alg_clear_init(&wd_digest_setting.status); return ret; }
-void wd_digest_uninit(void) +static void wd_digest_uninit_nolock(void) { void *priv = wd_digest_setting.priv;
@@ -276,6 +302,59 @@ void wd_digest_uninit(void)
wd_clear_sched(&wd_digest_setting.sched); wd_clear_ctx_config(&wd_digest_setting.config); +} + +void wd_digest_uninit(void) +{ + wd_digest_uninit_nolock(); + wd_alg_clear_init(&wd_digest_setting.status); +} + +int wd_digest_init2_(char *alg, __u32 sched_type, int task_type, + struct wd_ctx_params *ctx_params) +{ + bool flag; + int ret; + + pthread_atfork(NULL, NULL, wd_digest_clear_status); + + flag = wd_alg_try_init(&wd_digest_setting.status); + if (!flag) + return 0; + + if (!alg || sched_type > SCHED_POLICY_BUTT || + task_type < 0 || task_type > TASK_MAX_TYPE) { + WD_ERR("invalid: input param is wrong!\n"); + ret = -WD_EINVAL; + goto out_uninit; + } + + wd_digest_init_attrs.alg = alg; + wd_digest_init_attrs.sched_type = sched_type; + wd_digest_init_attrs.ctx_params = ctx_params ? ctx_params : &wd_digest_ctx_params; + wd_digest_init_attrs.alg_init = wd_digest_init_nolock; + wd_digest_init_attrs.alg_poll_ctx = wd_digest_poll_ctx; + ret = wd_alg_attrs_init(&wd_digest_init_attrs); + if (ret) { + WD_ERR("fail to init alg attrs.\n"); + goto out_uninit; + } + + wd_alg_set_init(&wd_digest_setting.status); + + return 0; + +out_uninit: + wd_alg_clear_init(&wd_digest_setting.status); + return ret; +} + +void wd_digest_uninit2(void) +{ + wd_digest_uninit_nolock(); + + wd_alg_attrs_uninit(&wd_digest_init_attrs); + wd_alg_clear_init(&wd_digest_setting.status); }
The basic init process is complex for users who need to know the device, scheduler, etc.
So introduce the init2 interface just for simplifying the initialization process when user use the aead algorithm.
Signed-off-by: Hao Fang fanghao11@huawei.com --- include/wd_aead.h | 28 ++++++++++++ libwd_crypto.map | 3 ++ wd_aead.c | 113 +++++++++++++++++++++++++++++++++++++++------- 3 files changed, 127 insertions(+), 17 deletions(-)
diff --git a/include/wd_aead.h b/include/wd_aead.h index ba7d062..ef643dc 100644 --- a/include/wd_aead.h +++ b/include/wd_aead.h @@ -96,6 +96,34 @@ int wd_aead_init(struct wd_ctx_config *config, struct wd_sched *sched); */ void wd_aead_uninit(void);
+/** + * wd_aead_init2_() - A simplify interface to initializate uadk + * aead operation. This interface keeps most functions of + * wd_aead_init(). Users just need to descripe the deployment of + * business scenarios. Then the initialization will request appropriate + * resources to support the business scenarios. + * To make the initializate simpler, ctx_params support set NULL. + * And then the function will set them as driver's default. + * Please do not use this interface with wd_aead_init() together, or + * some resources may be leak. + * + * @alg: The algorithm users want to use. + * @sched_type: The scheduling type users want to use. + * @task_type: Task types, including soft computing, hardware and hybrid computing. + * @ctx_params: The ctxs resources users want to use. Include per operation + * type ctx numbers and business process run numa. + * + * Return 0 if succeed and others if fail. + */ +int wd_aead_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_params *ctx_params); + +#define wd_aead_init2(alg, sched_type, task_type) \ + wd_aead_init2(alg, sched_type, task_type, NULL) + +/** + * wd_aead_uninit2() - Uninitialise ctx configuration and scheduler. + */ +void wd_aead_uninit2(void); /** * wd_aead_alloc_sess() Allocate a wd aead session * @ setup Parameters to setup this session. diff --git a/libwd_crypto.map b/libwd_crypto.map index e28dd79..e8555c9 100644 --- a/libwd_crypto.map +++ b/libwd_crypto.map @@ -23,6 +23,9 @@ global:
wd_aead_init; wd_aead_uninit; + wd_aead_init2; + wd_aead_init2_; + wd_aead_uninit2; wd_aead_alloc_sess; wd_aead_free_sess; wd_aead_set_ckey; diff --git a/wd_aead.c b/wd_aead.c index 9b80922..8b63daa 100644 --- a/wd_aead.c +++ b/wd_aead.c @@ -56,6 +56,17 @@ struct wd_aead_sess { };
struct wd_env_config wd_aead_env_config; +static struct wd_init_attrs wd_aead_init_attrs; + +static struct wd_ctx_nums wd_aead_ctx_num[] = { + {1, 1}, {} +}; + +static struct wd_ctx_params wd_aead_ctx_params = { + .op_type_num = 1, + .ctx_set_num = wd_aead_ctx_num, + .bmp = NULL, +};
#ifdef WD_STATIC_DRV static void wd_aead_set_static_drv(void) @@ -394,30 +405,19 @@ static void wd_aead_clear_status(void) wd_alg_clear_init(&wd_aead_setting.status); }
-int wd_aead_init(struct wd_ctx_config *config, struct wd_sched *sched) +static int wd_aead_init_nolock(struct wd_ctx_config *config, struct wd_sched *sched) { void *priv; - bool flag; int ret;
- pthread_atfork(NULL, NULL, wd_aead_clear_status); - - flag = wd_alg_try_init(&wd_aead_setting.status); - if (!flag) - return 0; - - ret = wd_init_param_check(config, sched); - if (ret) - goto out_clear_init; - ret = wd_set_epoll_en("WD_AEAD_EPOLL_EN", &wd_aead_setting.config.epoll_en); if (ret < 0) - goto out_clear_init; + return ret;
ret = wd_init_ctx_config(&wd_aead_setting.config, config); if (ret) - goto out_clear_init; + return ret;
ret = wd_init_sched(&wd_aead_setting.sched, sched); if (ret < 0) @@ -449,8 +449,6 @@ int wd_aead_init(struct wd_ctx_config *config, struct wd_sched *sched) goto out_free_priv; }
- wd_alg_set_init(&wd_aead_setting.status); - return 0;
out_free_priv: @@ -462,12 +460,39 @@ out_clear_sched: wd_clear_sched(&wd_aead_setting.sched); out_clear_ctx_config: wd_clear_ctx_config(&wd_aead_setting.config); + + return ret; +} + +int wd_aead_init(struct wd_ctx_config *config, struct wd_sched *sched) +{ + bool flag; + int ret; + + pthread_atfork(NULL, NULL, wd_aead_clear_status); + + flag = wd_alg_try_init(&wd_aead_setting.status); + if (!flag) + return 0; + + ret = wd_init_param_check(config, sched); + if (ret) + goto out_clear_init; + + ret = wd_aead_init_nolock(config, sched); + if (ret) + goto out_clear_init; + + wd_alg_set_init(&wd_aead_setting.status); + + return 0; + out_clear_init: wd_alg_clear_init(&wd_aead_setting.status); return ret; }
-void wd_aead_uninit(void) +static void wd_aead_uninit_nolock(void) { void *priv = wd_aead_setting.priv;
@@ -481,6 +506,60 @@ void wd_aead_uninit(void) wd_uninit_async_request_pool(&wd_aead_setting.pool); wd_clear_sched(&wd_aead_setting.sched); wd_clear_ctx_config(&wd_aead_setting.config); +} + +void wd_aead_uninit(void) +{ + wd_aead_uninit_nolock(); + wd_alg_clear_init(&wd_aead_setting.status); +} + + +int wd_aead_init2_(char *alg, __u32 sched_type, int task_type, + struct wd_ctx_params *ctx_params) +{ + bool flag; + int ret; + + pthread_atfork(NULL, NULL, wd_aead_clear_status); + + flag = wd_alg_try_init(&wd_aead_setting.status); + if (!flag) + return 0; + + if (!alg || sched_type > SCHED_POLICY_BUTT || + task_type < 0 || task_type > TASK_MAX_TYPE) { + WD_ERR("invalid: input param is wrong!\n"); + ret = -WD_EINVAL; + goto out_uninit; + } + + wd_aead_init_attrs.alg = alg; + wd_aead_init_attrs.sched_type = sched_type; + wd_aead_init_attrs.ctx_params = ctx_params ? ctx_params : &wd_aead_ctx_params; + wd_aead_init_attrs.alg_init = wd_aead_init_nolock; + wd_aead_init_attrs.alg_poll_ctx = wd_aead_poll_ctx; + ret = wd_alg_attrs_init(&wd_aead_init_attrs); + if (ret) { + WD_ERR("fail to init alg attrs.\n"); + goto out_uninit; + } + + wd_alg_set_init(&wd_aead_setting.status); + + return 0; + +out_uninit: + wd_alg_clear_init(&wd_aead_setting.status); + return ret; +} + +void wd_aead_uninit2(void) +{ + wd_aead_uninit_nolock(); + + wd_alg_attrs_uninit(&wd_aead_init_attrs); + wd_alg_clear_init(&wd_aead_setting.status); }
After adding the digest module of the init2 interface, combine the driver module dynamic load in the initialization process, transform HiSilicon digest driver, and implemented using the dynamic loading function connection between driver and algorithm layer.
Signed-off-by: Hao Fang fanghao11@huawei.com --- drv/hisi_sec.c | 58 +++++++--- include/drv/wd_digest_drv.h | 26 ----- wd_digest.c | 219 ++++++++++++++++++++++++------------ 3 files changed, 190 insertions(+), 113 deletions(-)
diff --git a/drv/hisi_sec.c b/drv/hisi_sec.c index a84cc7a..eda7aa9 100644 --- a/drv/hisi_sec.c +++ b/drv/hisi_sec.c @@ -565,6 +565,22 @@ static struct wd_alg_driver cipher_alg_driver[] = { GEN_SEC_ALG_DRIVER("cbc-cs3(sm4)"), };
+static struct wd_alg_driver digest_alg_driver[] = { + GEN_SEC_ALG_DRIVER("sm3"), + GEN_SEC_ALG_DRIVER("md5"), + GEN_SEC_ALG_DRIVER("sha1"), + GEN_SEC_ALG_DRIVER("sha224"), + GEN_SEC_ALG_DRIVER("sha256"), + GEN_SEC_ALG_DRIVER("sha384"), + GEN_SEC_ALG_DRIVER("sha512"), + GEN_SEC_ALG_DRIVER("sha512-224"), + GEN_SEC_ALG_DRIVER("sha512-256"), + GEN_SEC_ALG_DRIVER("aes-xcbc-mac-96"), + GEN_SEC_ALG_DRIVER("aes-xcbc-prf-128"), + GEN_SEC_ALG_DRIVER("aes-cmac"), + GEN_SEC_ALG_DRIVER("aes-gmac"), +}; + static void dump_sec_msg(void *msg, const char *alg) { struct wd_cipher_msg *cmsg; @@ -1698,16 +1714,6 @@ int hisi_sec_digest_recv(handle_t ctx, void *digest_msg) return 0; }
-static struct wd_digest_driver hisi_digest_driver = { - .drv_name = "hisi_sec2", - .alg_name = "digest", - .drv_ctx_size = sizeof(struct hisi_sec_ctx), - .init = hisi_sec_init, - .exit = hisi_sec_exit, -}; - -WD_DIGEST_SET_DRIVER(hisi_digest_driver); - static int hmac_key_len_check(struct wd_digest_msg *msg) { switch (msg->alg) { @@ -2625,8 +2631,11 @@ static void hisi_sec_driver_adapter(struct hisi_qp *qp) cipher_alg_driver[i].recv = hisi_sec_cipher_recv; }
- hisi_digest_driver.digest_send = hisi_sec_digest_send; - hisi_digest_driver.digest_recv = hisi_sec_digest_recv; + alg_num = ARRAY_SIZE(digest_alg_driver); + for (i = 0; i < alg_num; i++) { + digest_alg_driver[i].send = hisi_sec_digest_send; + digest_alg_driver[i].recv = hisi_sec_digest_recv; + }
hisi_aead_driver.aead_send = hisi_sec_aead_send; hisi_aead_driver.aead_recv = hisi_sec_aead_recv; @@ -2638,8 +2647,11 @@ static void hisi_sec_driver_adapter(struct hisi_qp *qp) cipher_alg_driver[i].recv = hisi_sec_cipher_recv_v3; }
- hisi_digest_driver.digest_send = hisi_sec_digest_send_v3; - hisi_digest_driver.digest_recv = hisi_sec_digest_recv_v3; + alg_num = ARRAY_SIZE(digest_alg_driver); + for (i = 0; i < alg_num; i++) { + digest_alg_driver[i].send = hisi_sec_digest_send_v3; + digest_alg_driver[i].recv = hisi_sec_digest_recv_v3; + }
hisi_aead_driver.aead_send = hisi_sec_aead_send_v3; hisi_aead_driver.aead_recv = hisi_sec_aead_recv_v3; @@ -2709,25 +2721,39 @@ void hisi_sec_exit(void *priv)
static void __attribute__((constructor)) hisi_sec2_probe(void) { - int alg_num = ARRAY_SIZE(cipher_alg_driver); + int alg_num; int i, ret;
WD_INFO("Info: register SEC alg drivers!\n");
+ alg_num = ARRAY_SIZE(cipher_alg_driver); for (i = 0; i < alg_num; i++) { ret = wd_alg_driver_register(&cipher_alg_driver[i]); if (ret) WD_ERR("Error: register SEC %s failed!\n", cipher_alg_driver[i].alg_name); } + + alg_num = ARRAY_SIZE(digest_alg_driver); + for (i = 0; i < alg_num; i++) { + ret = wd_alg_driver_register(&digest_alg_driver[i]); + if (ret) + WD_ERR("Error: register SEC %s failed!\n", + digest_alg_driver[i].alg_name); + } }
static void __attribute__((destructor)) hisi_sec2_remove(void) { - int alg_num = ARRAY_SIZE(cipher_alg_driver); + int alg_num; int i;
+ alg_num = ARRAY_SIZE(cipher_alg_driver); for (i = 0; i < alg_num; i++) wd_alg_driver_unregister(&cipher_alg_driver[i]); + + alg_num = ARRAY_SIZE(digest_alg_driver); + for (i = 0; i < alg_num; i++) + wd_alg_driver_unregister(&digest_alg_driver[i]); }
diff --git a/include/drv/wd_digest_drv.h b/include/drv/wd_digest_drv.h index 96b32e2..3c4477d 100644 --- a/include/drv/wd_digest_drv.h +++ b/include/drv/wd_digest_drv.h @@ -51,34 +51,8 @@ struct wd_digest_msg { __u64 long_data_len; };
-struct wd_digest_driver { - const char *drv_name; - const char *alg_name; - __u32 drv_ctx_size; - int (*init)(void *conf, void *priv); - void (*exit)(void *priv); - int (*digest_send)(handle_t ctx, void *digest_msg); - int (*digest_recv)(handle_t ctx, void *digest_msg); -}; - -void wd_digest_set_driver(struct wd_digest_driver *drv); -struct wd_digest_driver *wd_digest_get_driver(void); struct wd_digest_msg *wd_digest_get_msg(__u32 idx, __u32 tag);
-#ifdef WD_STATIC_DRV -#define WD_DIGEST_SET_DRIVER(drv) \ -struct wd_digest_driver *wd_digest_get_driver(void) \ -{ \ - return &drv; \ -} -#else -#define WD_DIGEST_SET_DRIVER(drv) \ -static void __attribute__((constructor)) set_digest_drivers(void) \ -{ \ - wd_digest_set_driver(&(drv)); \ -} -#endif - #ifdef __cplusplus } #endif diff --git a/wd_digest.c b/wd_digest.c index 03d3ace..d179549 100644 --- a/wd_digest.c +++ b/wd_digest.c @@ -37,15 +37,22 @@ static int g_digest_mac_full_len[WD_DIGEST_TYPE_MAX] = { WD_DIGEST_SHA512_224_FULL_LEN, WD_DIGEST_SHA512_256_FULL_LEN };
+/* These algs's name need correct match with digest alg type */ +static char *wd_digest_alg_name[WD_DIGEST_TYPE_MAX] = { + "sm3", "md5", "sha1", "sha256", "sha224", "sha384", + "sha512", "sha512-224", "sha512-256", "aes-xcbc-mac-96", + "aes-xcbc-prf-128", "aes-cmac", "aes-gmac" +}; + struct wd_digest_setting { enum wd_status status; struct wd_ctx_config_internal config; struct wd_sched sched; - struct wd_digest_driver *driver; + struct wd_alg_driver *driver; struct wd_async_msg_pool pool; - void *sched_ctx; void *priv; void *dlhandle; + void *dlh_list; } wd_digest_setting;
struct wd_digest_sess { @@ -68,42 +75,49 @@ struct wd_digest_sess { struct wd_env_config wd_digest_env_config; static struct wd_init_attrs wd_digest_init_attrs;
-static struct wd_ctx_nums wd_digest_ctx_num[] = { - {1, 1}, {} -}; - -static struct wd_ctx_params wd_digest_ctx_params = { - .op_type_num = 1, - .ctx_set_num = wd_digest_ctx_num, - .bmp = NULL, -}; - -#ifdef WD_STATIC_DRV -static void wd_digest_set_static_drv(void) +static void wd_digest_close_driver(void) { - wd_digest_setting.driver = wd_digest_get_driver(); - if (!wd_digest_setting.driver) - WD_ERR("failed to get driver!\n"); + if (wd_digest_setting.dlhandle) { + wd_release_drv(wd_digest_setting.driver); + dlclose(wd_digest_setting.dlhandle); + wd_digest_setting.dlhandle = NULL; + } } -#else -static void __attribute__((constructor)) wd_digest_open_driver(void) + +static int wd_digest_open_driver(void) { - /* Fix me: vendor driver should be put in /usr/lib/wd/ */ - wd_digest_setting.dlhandle = dlopen("libhisi_sec.so", RTLD_NOW); - if (!wd_digest_setting.dlhandle) + struct wd_alg_driver *driver = NULL; + const char *alg_name = "sm3"; + char lib_path[PATH_STR_SIZE]; + int ret; + + /* + * Compatible with the normal acquisition of device + * drivers in the init interface + */ + if (wd_digest_setting.dlh_list) + return 0; + + ret = wd_get_lib_file_path("libhisi_sec.so", lib_path, false); + if (ret) + return ret; + + wd_digest_setting.dlhandle = dlopen(lib_path, RTLD_NOW); + if (!wd_digest_setting.dlhandle) { WD_ERR("failed to open libhisi_sec.so, %s\n", dlerror()); -} + return -WD_EINVAL; + }
-static void __attribute__((destructor)) wd_digest_close_driver(void) -{ - if (wd_digest_setting.dlhandle) - dlclose(wd_digest_setting.dlhandle); -} -#endif + driver = wd_request_drv(alg_name, false); + if (!driver) { + wd_digest_close_driver(); + WD_ERR("failed to get %s driver support\n", alg_name); + return -WD_EINVAL; + }
-void wd_digest_set_driver(struct wd_digest_driver *drv) -{ - wd_digest_setting.driver = drv; + wd_digest_setting.driver = driver; + + return 0; }
static int aes_key_len_check(__u32 length) @@ -154,29 +168,46 @@ int wd_digest_set_key(handle_t h_sess, const __u8 *key, __u32 key_len) handle_t wd_digest_alloc_sess(struct wd_digest_sess_setup *setup) { struct wd_digest_sess *sess = NULL; + bool ret;
if (unlikely(!setup)) { WD_ERR("failed to check alloc sess param!\n"); return (handle_t)0; }
+ if (setup->alg >= WD_DIGEST_TYPE_MAX) { + WD_ERR("failed to check algorithm setup!\n"); + return (handle_t)0; + } + sess = malloc(sizeof(struct wd_digest_sess)); if (!sess) return (handle_t)0; memset(sess, 0, sizeof(struct wd_digest_sess));
+ sess->alg_name = wd_digest_alg_name[setup->alg]; sess->alg = setup->alg; sess->mode = setup->mode; + ret = wd_drv_alg_support(sess->alg_name, wd_digest_setting.driver); + if (!ret) { + WD_ERR("failed to support this algorithm: %s!\n", sess->alg_name); + goto err_sess; + } /* Some simple scheduler don't need scheduling parameters */ sess->sched_key = (void *)wd_digest_setting.sched.sched_init( wd_digest_setting.sched.h_sched_ctx, setup->sched_param); if (WD_IS_ERR(sess->sched_key)) { WD_ERR("failed to init session schedule key!\n"); - free(sess); - return (handle_t)0; + goto err_sess; }
return (handle_t)sess; + +err_sess: + if (sess->sched_key) + free(sess->sched_key); + free(sess); + return (handle_t)0; }
void wd_digest_free_sess(handle_t h_sess) @@ -202,7 +233,6 @@ static void wd_digest_clear_status(void) static int wd_digest_init_nolock(struct wd_ctx_config *config, struct wd_sched *sched) { - void *priv; int ret;
ret = wd_set_epoll_en("WD_DIGEST_EPOLL_EN", @@ -218,11 +248,6 @@ static int wd_digest_init_nolock(struct wd_ctx_config *config, if (ret < 0) goto out_clear_ctx_config;
- /* set driver */ -#ifdef WD_STATIC_DRV - wd_digest_set_static_drv(); -#endif - /* allocate async pool for every ctx */ ret = wd_init_async_request_pool(&wd_digest_setting.pool, config->ctx_num, WD_POOL_MAX_ENTRIES, @@ -230,25 +255,14 @@ static int wd_digest_init_nolock(struct wd_ctx_config *config, if (ret < 0) goto out_clear_sched;
- /* init ctx related resources in specific driver */ - priv = calloc(1, wd_digest_setting.driver->drv_ctx_size); - if (!priv) { - ret = -WD_ENOMEM; + ret = wd_alg_init_driver(&wd_digest_setting.config, + wd_digest_setting.driver, + &wd_digest_setting.priv); + if (ret) goto out_clear_pool; - } - wd_digest_setting.priv = priv; - - ret = wd_digest_setting.driver->init(&wd_digest_setting.config, priv); - if (ret < 0) { - WD_ERR("failed to init digest dirver!\n"); - goto out_free_priv; - }
return 0;
-out_free_priv: - free(priv); - wd_digest_setting.priv = NULL; out_clear_pool: wd_uninit_async_request_pool(&wd_digest_setting.pool); out_clear_sched: @@ -274,14 +288,20 @@ int wd_digest_init(struct wd_ctx_config *config, struct wd_sched *sched) if (ret) goto out_clear_init;
- ret = wd_digest_init_nolock(config, sched); + ret = wd_digest_open_driver(); if (ret) goto out_clear_init;
+ ret = wd_digest_init_nolock(config, sched); + if (ret) + goto out_close_driver; + wd_alg_set_init(&wd_digest_setting.status);
return 0;
+out_close_driver: + wd_digest_close_driver(); out_clear_init: wd_alg_clear_init(&wd_digest_setting.status); return ret; @@ -294,27 +314,26 @@ static void wd_digest_uninit_nolock(void) if (!priv) return;
- wd_digest_setting.driver->exit(priv); - wd_digest_setting.priv = NULL; - free(priv); - wd_uninit_async_request_pool(&wd_digest_setting.pool); - wd_clear_sched(&wd_digest_setting.sched); - wd_clear_ctx_config(&wd_digest_setting.config); + wd_alg_uninit_driver(&wd_digest_setting.config, + wd_digest_setting.driver, &priv); }
void wd_digest_uninit(void) { wd_digest_uninit_nolock(); + wd_digest_close_driver(); wd_alg_clear_init(&wd_digest_setting.status); }
int wd_digest_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_params *ctx_params) { + struct wd_ctx_nums digest_ctx_num[1] = {0}; + struct wd_ctx_params digest_ctx_params = {0}; + int ret = 0; bool flag; - int ret;
pthread_atfork(NULL, NULL, wd_digest_clear_status);
@@ -329,21 +348,78 @@ int wd_digest_init2_(char *alg, __u32 sched_type, int task_type, goto out_uninit; }
+ flag = false; + for (int i = 0; i < WD_DIGEST_TYPE_MAX; i++) { + if (!strcmp(alg, wd_digest_alg_name[i])) { + flag = true; + break; + } + } + + if (!flag) { + WD_ERR("invalid: digest:%s unsupported!\n", alg); + ret = -WD_EINVAL; + goto out_uninit; + } + + /* + * Driver lib file path could set by env param. + * than open them by wd_dlopen_drv() + * use NULL means dynamic query path + */ + wd_digest_setting.dlh_list = wd_dlopen_drv(NULL); + if (!wd_digest_setting.dlh_list) { + WD_ERR("fail to open driver lib files.\n"); + goto out_uninit; + } + +res_retry: + memset(&wd_digest_setting.config, 0, sizeof(struct wd_ctx_config_internal)); + + /* Get alg driver and dev name */ + wd_digest_setting.driver = wd_alg_drv_bind(task_type, alg); + if (!wd_digest_setting.driver) { + WD_ERR("fail to bind a valid driver.\n"); + ret = -WD_EINVAL; + goto out_dlopen; + } + + ret = wd_ctx_param_init(&digest_ctx_params, ctx_params, + digest_ctx_num, wd_digest_setting.driver, 1); + if (ret) { + if (ret == -WD_EAGAIN) { + wd_disable_drv(wd_digest_setting.driver); + wd_alg_drv_unbind(wd_digest_setting.driver); + goto res_retry; + } + goto out_driver; + } + wd_digest_init_attrs.alg = alg; wd_digest_init_attrs.sched_type = sched_type; - wd_digest_init_attrs.ctx_params = ctx_params ? ctx_params : &wd_digest_ctx_params; + wd_digest_init_attrs.driver = wd_digest_setting.driver; + wd_digest_init_attrs.ctx_params = &digest_ctx_params; wd_digest_init_attrs.alg_init = wd_digest_init_nolock; wd_digest_init_attrs.alg_poll_ctx = wd_digest_poll_ctx; ret = wd_alg_attrs_init(&wd_digest_init_attrs); if (ret) { + if (ret == -WD_ENODEV) { + wd_disable_drv(wd_digest_setting.driver); + wd_alg_drv_unbind(wd_digest_setting.driver); + goto res_retry; + } WD_ERR("fail to init alg attrs.\n"); - goto out_uninit; + goto out_driver; }
wd_alg_set_init(&wd_digest_setting.status);
return 0;
+out_driver: + wd_alg_drv_unbind(wd_digest_setting.driver); +out_dlopen: + wd_dlclose_drv(wd_digest_setting.dlh_list); out_uninit: wd_alg_clear_init(&wd_digest_setting.status); return ret; @@ -352,9 +428,10 @@ out_uninit: void wd_digest_uninit2(void) { wd_digest_uninit_nolock(); - wd_alg_attrs_uninit(&wd_digest_init_attrs); - + wd_alg_drv_unbind(wd_digest_setting.driver); + wd_dlclose_drv(wd_digest_setting.dlh_list); + wd_digest_setting.dlh_list = NULL; wd_alg_clear_init(&wd_digest_setting.status); }
@@ -483,8 +560,8 @@ static int send_recv_sync(struct wd_ctx_internal *ctx, struct wd_digest_sess *ds struct wd_msg_handle msg_handle; int ret;
- msg_handle.send = wd_digest_setting.driver->digest_send; - msg_handle.recv = wd_digest_setting.driver->digest_recv; + msg_handle.send = wd_digest_setting.driver->send; + msg_handle.recv = wd_digest_setting.driver->recv;
pthread_spin_lock(&ctx->lock); ret = wd_handle_msg_sync(&msg_handle, ctx->ctx, msg, @@ -572,7 +649,7 @@ int wd_do_digest_async(handle_t h_sess, struct wd_digest_req *req) fill_request_msg(msg, req, dsess); msg->tag = msg_id;
- ret = wd_digest_setting.driver->digest_send(ctx->ctx, msg); + ret = wd_digest_setting.driver->send(ctx->ctx, msg); if (unlikely(ret < 0)) { if (ret != -WD_EBUSY) WD_ERR("failed to send BD, hw is err!\n"); @@ -620,7 +697,7 @@ int wd_digest_poll_ctx(__u32 idx, __u32 expt, __u32 *count) ctx = config->ctxs + idx;
do { - ret = wd_digest_setting.driver->digest_recv(ctx->ctx, + ret = wd_digest_setting.driver->recv(ctx->ctx, &recv_msg); if (ret == -WD_EAGAIN) { return ret;
After adding the aead module of the init2 interface, combine the driver module dynamic load in the initialization process, transform HiSilicon aead driver, and implemented using the dynamic loading function connection between driver and algorithm layer.
Signed-off-by: Hao Fang fanghao11@huawei.com --- drv/hisi_sec.c | 47 +++++--- include/drv/wd_aead_drv.h | 26 ----- wd_aead.c | 226 ++++++++++++++++++++++++++------------ 3 files changed, 188 insertions(+), 111 deletions(-)
diff --git a/drv/hisi_sec.c b/drv/hisi_sec.c index eda7aa9..3d5fab0 100644 --- a/drv/hisi_sec.c +++ b/drv/hisi_sec.c @@ -581,6 +581,15 @@ static struct wd_alg_driver digest_alg_driver[] = { GEN_SEC_ALG_DRIVER("aes-gmac"), };
+static struct wd_alg_driver aead_alg_driver[] = { + GEN_SEC_ALG_DRIVER("ccm(aes)"), + GEN_SEC_ALG_DRIVER("gcm(aes)"), + GEN_SEC_ALG_DRIVER("authenc(hmac(sha256),cbc(aes))"), + GEN_SEC_ALG_DRIVER("ccm(sm4)"), + GEN_SEC_ALG_DRIVER("gcm(sm4)"), + GEN_SEC_ALG_DRIVER("authenc(hmac(sha256),cbc(sm4))"), +}; + static void dump_sec_msg(void *msg, const char *alg) { struct wd_cipher_msg *cmsg; @@ -2349,16 +2358,6 @@ int hisi_sec_aead_recv(handle_t ctx, void *aead_msg) return 0; }
-static struct wd_aead_driver hisi_aead_driver = { - .drv_name = "hisi_sec2", - .alg_name = "aead", - .drv_ctx_size = sizeof(struct hisi_sec_ctx), - .init = hisi_sec_init, - .exit = hisi_sec_exit, -}; - -WD_AEAD_SET_DRIVER(hisi_aead_driver); - static int fill_aead_bd3_alg(struct wd_aead_msg *msg, struct hisi_sec_sqe3 *sqe) { @@ -2636,9 +2635,11 @@ static void hisi_sec_driver_adapter(struct hisi_qp *qp) digest_alg_driver[i].send = hisi_sec_digest_send; digest_alg_driver[i].recv = hisi_sec_digest_recv; } - - hisi_aead_driver.aead_send = hisi_sec_aead_send; - hisi_aead_driver.aead_recv = hisi_sec_aead_recv; + alg_num = ARRAY_SIZE(aead_alg_driver); + for (i = 0; i < alg_num; i++) { + aead_alg_driver[i].send = hisi_sec_aead_send; + aead_alg_driver[i].recv = hisi_sec_aead_recv; + } } else { WD_INFO("hisi sec init HIP09!\n"); alg_num = ARRAY_SIZE(cipher_alg_driver); @@ -2652,9 +2653,11 @@ static void hisi_sec_driver_adapter(struct hisi_qp *qp) digest_alg_driver[i].send = hisi_sec_digest_send_v3; digest_alg_driver[i].recv = hisi_sec_digest_recv_v3; } - - hisi_aead_driver.aead_send = hisi_sec_aead_send_v3; - hisi_aead_driver.aead_recv = hisi_sec_aead_recv_v3; + alg_num = ARRAY_SIZE(aead_alg_driver); + for (i = 0; i < alg_num; i++) { + aead_alg_driver[i].send = hisi_sec_aead_send_v3; + aead_alg_driver[i].recv = hisi_sec_aead_recv_v3; + } } }
@@ -2741,6 +2744,14 @@ static void __attribute__((constructor)) hisi_sec2_probe(void) WD_ERR("Error: register SEC %s failed!\n", digest_alg_driver[i].alg_name); } + + alg_num = ARRAY_SIZE(aead_alg_driver); + for (i = 0; i < alg_num; i++) { + ret = wd_alg_driver_register(&aead_alg_driver[i]); + if (ret) + WD_ERR("Error: register SEC %s failed!\n", + aead_alg_driver[i].alg_name); + } }
static void __attribute__((destructor)) hisi_sec2_remove(void) @@ -2755,5 +2766,9 @@ static void __attribute__((destructor)) hisi_sec2_remove(void) alg_num = ARRAY_SIZE(digest_alg_driver); for (i = 0; i < alg_num; i++) wd_alg_driver_unregister(&digest_alg_driver[i]); + + alg_num = ARRAY_SIZE(aead_alg_driver); + for (i = 0; i < alg_num; i++) + wd_alg_driver_unregister(&aead_alg_driver[i]); }
diff --git a/include/drv/wd_aead_drv.h b/include/drv/wd_aead_drv.h index 8446238..d2ecb22 100644 --- a/include/drv/wd_aead_drv.h +++ b/include/drv/wd_aead_drv.h @@ -63,34 +63,8 @@ struct wd_aead_msg { __u8 *mac; };
-struct wd_aead_driver { - const char *drv_name; - const char *alg_name; - __u32 drv_ctx_size; - int (*init)(void *conf, void *priv); - void (*exit)(void *priv); - int (*aead_send)(handle_t ctx, void *aead_msg); - int (*aead_recv)(handle_t ctx, void *aead_msg); -}; - -void wd_aead_set_driver(struct wd_aead_driver *drv); -struct wd_aead_driver *wd_aead_get_driver(void); struct wd_aead_msg *wd_aead_get_msg(__u32 idx, __u32 tag);
-#ifdef WD_STATIC_DRV -#define WD_AEAD_SET_DRIVER(drv) \ -struct wd_aead_driver *wd_aead_get_driver(void) \ -{ \ - return &drv; \ -} -#else -#define WD_AEAD_SET_DRIVER(drv) \ -static void __attribute__((constructor)) set_aead_driver(void) \ -{ \ - wd_aead_set_driver(&(drv)); \ -} -#endif - #ifdef __cplusplus } #endif diff --git a/wd_aead.c b/wd_aead.c index 8b63daa..41a5a47 100644 --- a/wd_aead.c +++ b/wd_aead.c @@ -29,15 +29,23 @@ static int g_aead_mac_len[WD_DIGEST_TYPE_MAX] = { WD_DIGEST_SHA512_224_LEN, WD_DIGEST_SHA512_256_LEN };
+/* These algs's name need correct match with alg/mode type */ +static char *wd_aead_alg_name[WD_CIPHER_ALG_TYPE_MAX][WD_CIPHER_MODE_TYPE_MAX] = { + {"", "authenc(hmac(sha256),cbc(sm4))", "", "", "", "", "", "", "", + "ccm(sm4)", "gcm(sm4)"}, + {"", "authenc(hmac(sha256),cbc(aes))", "", "", "", "", "", "", "", + "ccm(aes)", "gcm(aes)"} +}; + struct wd_aead_setting { enum wd_status status; struct wd_ctx_config_internal config; struct wd_sched sched; - struct wd_aead_driver *driver; + struct wd_alg_driver *driver; struct wd_async_msg_pool pool; - void *sched_ctx; void *priv; void *dlhandle; + void *dlh_list; } wd_aead_setting;
struct wd_aead_sess { @@ -58,41 +66,49 @@ struct wd_aead_sess { struct wd_env_config wd_aead_env_config; static struct wd_init_attrs wd_aead_init_attrs;
-static struct wd_ctx_nums wd_aead_ctx_num[] = { - {1, 1}, {} -}; - -static struct wd_ctx_params wd_aead_ctx_params = { - .op_type_num = 1, - .ctx_set_num = wd_aead_ctx_num, - .bmp = NULL, -}; - -#ifdef WD_STATIC_DRV -static void wd_aead_set_static_drv(void) +static void wd_aead_close_driver(void) { - wd_aead_setting.driver = wd_aead_get_driver(); - if (!wd_aead_setting.driver) - WD_ERR("failed to get driver!\n"); + if (wd_aead_setting.dlhandle) { + wd_release_drv(wd_aead_setting.driver); + dlclose(wd_aead_setting.dlhandle); + wd_aead_setting.dlhandle = NULL; + } } -#else -static void __attribute__((constructor)) wd_aead_open_driver(void) + +static int wd_aead_open_driver(void) { - wd_aead_setting.dlhandle = dlopen("libhisi_sec.so", RTLD_NOW); - if (!wd_aead_setting.dlhandle) + struct wd_alg_driver *driver = NULL; + const char *alg_name = "gcm(aes)"; + char lib_path[PATH_STR_SIZE]; + int ret; + + /* + * Compatible with the normal acquisition of device + * drivers in the init interface + */ + if (wd_aead_setting.dlh_list) + return 0; + + ret = wd_get_lib_file_path("libhisi_sec.so", lib_path, false); + if (ret) + return ret; + + wd_aead_setting.dlhandle = dlopen(lib_path, RTLD_NOW); + if (!wd_aead_setting.dlhandle) { WD_ERR("failed to open libhisi_sec.so, %s\n", dlerror()); -} + return -WD_EINVAL; + }
-static void __attribute__((destructor)) wd_aead_close_driver(void) -{ - if (wd_aead_setting.dlhandle) - dlclose(wd_aead_setting.dlhandle); -} -#endif + driver = wd_request_drv(alg_name, false); + if (!driver) { + wd_aead_close_driver(); + WD_ERR("failed to get %s driver support\n", alg_name); + return -WD_EINVAL; + }
-void wd_aead_set_driver(struct wd_aead_driver *drv) -{ - wd_aead_setting.driver = drv; + wd_aead_setting.driver = driver; + + return 0; }
static int aes_key_len_check(__u32 length) @@ -272,12 +288,19 @@ int wd_aead_get_maxauthsize(handle_t h_sess) handle_t wd_aead_alloc_sess(struct wd_aead_sess_setup *setup) { struct wd_aead_sess *sess = NULL; + bool ret;
if (unlikely(!setup)) { WD_ERR("failed to check session input parameter!\n"); return (handle_t)0; }
+ if (setup->calg >= WD_CIPHER_ALG_TYPE_MAX || + setup->cmode >= WD_CIPHER_MODE_TYPE_MAX) { + WD_ERR("failed to check algorithm setup!\n"); + return (handle_t)0; + } + sess = malloc(sizeof(struct wd_aead_sess)); if (!sess) { WD_ERR("failed to alloc session memory!\n"); @@ -285,20 +308,32 @@ handle_t wd_aead_alloc_sess(struct wd_aead_sess_setup *setup) } memset(sess, 0, sizeof(struct wd_aead_sess));
+ sess->alg_name = wd_aead_alg_name[setup->calg][setup->cmode]; sess->calg = setup->calg; sess->cmode = setup->cmode; sess->dalg = setup->dalg; sess->dmode = setup->dmode; + ret = wd_drv_alg_support(sess->alg_name, wd_aead_setting.driver); + if (!ret) { + WD_ERR("failed to support this algorithm: %s!\n", sess->alg_name); + goto err_sess; + } + /* Some simple scheduler don't need scheduling parameters */ sess->sched_key = (void *)wd_aead_setting.sched.sched_init( wd_aead_setting.sched.h_sched_ctx, setup->sched_param); if (WD_IS_ERR(sess->sched_key)) { WD_ERR("failed to init session schedule key!\n"); - free(sess); - return (handle_t)0; + goto err_sess; }
return (handle_t)sess; +err_sess: + if (sess->sched_key) + free(sess->sched_key); + free(sess); + return (handle_t)0; + }
void wd_aead_free_sess(handle_t h_sess) @@ -407,7 +442,6 @@ static void wd_aead_clear_status(void)
static int wd_aead_init_nolock(struct wd_ctx_config *config, struct wd_sched *sched) { - void *priv; int ret;
ret = wd_set_epoll_en("WD_AEAD_EPOLL_EN", @@ -423,11 +457,6 @@ static int wd_aead_init_nolock(struct wd_ctx_config *config, struct wd_sched *sc if (ret < 0) goto out_clear_ctx_config;
- /* set driver */ -#ifdef WD_STATIC_DRV - wd_aead_set_static_drv(); -#endif - /* init async request pool */ ret = wd_init_async_request_pool(&wd_aead_setting.pool, config->ctx_num, WD_POOL_MAX_ENTRIES, @@ -435,25 +464,14 @@ static int wd_aead_init_nolock(struct wd_ctx_config *config, struct wd_sched *sc if (ret < 0) goto out_clear_sched;
- /* init ctx related resources in specific driver */ - priv = calloc(1, wd_aead_setting.driver->drv_ctx_size); - if (!priv) { - ret = -WD_ENOMEM; + ret = wd_alg_init_driver(&wd_aead_setting.config, + wd_aead_setting.driver, + &wd_aead_setting.priv); + if (ret) goto out_clear_pool; - } - wd_aead_setting.priv = priv; - - ret = wd_aead_setting.driver->init(&wd_aead_setting.config, priv); - if (ret < 0) { - WD_ERR("failed to init aead dirver!\n"); - goto out_free_priv; - }
return 0;
-out_free_priv: - free(priv); - wd_aead_setting.priv = NULL; out_clear_pool: wd_uninit_async_request_pool(&wd_aead_setting.pool); out_clear_sched: @@ -479,14 +497,20 @@ int wd_aead_init(struct wd_ctx_config *config, struct wd_sched *sched) if (ret) goto out_clear_init;
- ret = wd_aead_init_nolock(config, sched); + ret = wd_aead_open_driver(); if (ret) goto out_clear_init;
+ ret = wd_aead_init_nolock(config, sched); + if (ret) + goto out_close_driver; + wd_alg_set_init(&wd_aead_setting.status);
return 0;
+out_close_driver: + wd_aead_close_driver(); out_clear_init: wd_alg_clear_init(&wd_aead_setting.status); return ret; @@ -499,18 +523,16 @@ static void wd_aead_uninit_nolock(void) if (!priv) return;
- wd_aead_setting.driver->exit(priv); - wd_aead_setting.priv = NULL; - free(priv); - wd_uninit_async_request_pool(&wd_aead_setting.pool); wd_clear_sched(&wd_aead_setting.sched); - wd_clear_ctx_config(&wd_aead_setting.config); + wd_alg_uninit_driver(&wd_aead_setting.config, + wd_aead_setting.driver, &priv); }
void wd_aead_uninit(void) { wd_aead_uninit_nolock(); + wd_aead_close_driver(); wd_alg_clear_init(&wd_aead_setting.status); }
@@ -518,8 +540,10 @@ void wd_aead_uninit(void) int wd_aead_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_params *ctx_params) { + struct wd_ctx_nums aead_ctx_num[WD_DIGEST_CIPHER_DECRYPTION + 1] = {0}; + struct wd_ctx_params aead_ctx_params = {0}; + int ret = 0; bool flag; - int ret;
pthread_atfork(NULL, NULL, wd_aead_clear_status);
@@ -534,21 +558,84 @@ int wd_aead_init2_(char *alg, __u32 sched_type, int task_type, goto out_uninit; }
+ flag = false; + for (int i = 0; i < WD_CIPHER_ALG_TYPE_MAX; i++) { + for (int j = 0; j < WD_CIPHER_MODE_TYPE_MAX; j++) { + if (!wd_aead_alg_name[i][j]) + continue; + if (!strcmp(alg, wd_aead_alg_name[i][j])) { + i = WD_CIPHER_ALG_TYPE_MAX; + flag = true; + break; + } + } + } + + if (!flag) { + WD_ERR("invalid: aead:%s unsupported!\n", alg); + ret = -WD_EINVAL; + goto out_uninit; + } + + /* + * Driver lib file path could set by env param. + * than open them by wd_dlopen_drv() + * use NULL means dynamic query path + */ + wd_aead_setting.dlh_list = wd_dlopen_drv(NULL); + if (!wd_aead_setting.dlh_list) { + WD_ERR("fail to open driver lib files.\n"); + goto out_uninit; + } + +res_retry: + memset(&wd_aead_setting.config, 0, sizeof(struct wd_ctx_config_internal)); + + /* Get alg driver and dev name */ + wd_aead_setting.driver = wd_alg_drv_bind(task_type, alg); + if (!wd_aead_setting.driver) { + WD_ERR("fail to bind a valid driver.\n"); + ret = -WD_EINVAL; + goto out_dlopen; + } + + ret = wd_ctx_param_init(&aead_ctx_params, ctx_params, + aead_ctx_num, wd_aead_setting.driver, + WD_DIGEST_CIPHER_DECRYPTION + 1); + if (ret) { + if (ret == -WD_EAGAIN) { + wd_disable_drv(wd_aead_setting.driver); + wd_alg_drv_unbind(wd_aead_setting.driver); + goto res_retry; + } + goto out_driver; + } + wd_aead_init_attrs.alg = alg; wd_aead_init_attrs.sched_type = sched_type; - wd_aead_init_attrs.ctx_params = ctx_params ? ctx_params : &wd_aead_ctx_params; + wd_aead_init_attrs.driver = wd_aead_setting.driver; + wd_aead_init_attrs.ctx_params = &aead_ctx_params; wd_aead_init_attrs.alg_init = wd_aead_init_nolock; wd_aead_init_attrs.alg_poll_ctx = wd_aead_poll_ctx; ret = wd_alg_attrs_init(&wd_aead_init_attrs); if (ret) { + if (ret == -WD_ENODEV) { + wd_disable_drv(wd_aead_setting.driver); + wd_alg_drv_unbind(wd_aead_setting.driver); + goto res_retry; + } WD_ERR("fail to init alg attrs.\n"); - goto out_uninit; + goto out_driver; }
wd_alg_set_init(&wd_aead_setting.status);
return 0;
+out_driver: + wd_alg_drv_unbind(wd_aead_setting.driver); +out_dlopen: + wd_dlclose_drv(wd_aead_setting.dlh_list); out_uninit: wd_alg_clear_init(&wd_aead_setting.status); return ret; @@ -557,9 +644,10 @@ out_uninit: void wd_aead_uninit2(void) { wd_aead_uninit_nolock(); - wd_alg_attrs_uninit(&wd_aead_init_attrs); - + wd_alg_drv_unbind(wd_aead_setting.driver); + wd_dlclose_drv(wd_aead_setting.dlh_list); + wd_aead_setting.dlh_list = NULL; wd_alg_clear_init(&wd_aead_setting.status); }
@@ -596,8 +684,8 @@ static int send_recv_sync(struct wd_ctx_internal *ctx, struct wd_msg_handle msg_handle; int ret;
- msg_handle.send = wd_aead_setting.driver->aead_send; - msg_handle.recv = wd_aead_setting.driver->aead_recv; + msg_handle.send = wd_aead_setting.driver->send; + msg_handle.recv = wd_aead_setting.driver->recv;
pthread_spin_lock(&ctx->lock); ret = wd_handle_msg_sync(&msg_handle, ctx->ctx, msg, NULL, @@ -676,7 +764,7 @@ int wd_do_aead_async(handle_t h_sess, struct wd_aead_req *req) fill_request_msg(msg, req, sess); msg->tag = msg_id;
- ret = wd_aead_setting.driver->aead_send(ctx->ctx, msg); + ret = wd_aead_setting.driver->send(ctx->ctx, msg); if (unlikely(ret < 0)) { if (ret != -WD_EBUSY) WD_ERR("failed to send BD, hw is err!\n"); @@ -724,7 +812,7 @@ int wd_aead_poll_ctx(__u32 idx, __u32 expt, __u32 *count) ctx = config->ctxs + idx;
do { - ret = wd_aead_setting.driver->aead_recv(ctx->ctx, &resp_msg); + ret = wd_aead_setting.driver->recv(ctx->ctx, &resp_msg); if (ret == -WD_EAGAIN) { return ret; } else if (ret < 0) {
add testcase for init2 just use cmd --init 2. default or --init 1 for init interface.
Signed-off-by: Hao Fang fanghao11@huawei.com --- uadk_tool/test/test_sec.c | 152 +++++++++++++++++++++++++++++++++----- 1 file changed, 132 insertions(+), 20 deletions(-)
diff --git a/uadk_tool/test/test_sec.c b/uadk_tool/test/test_sec.c index 9fe007e..4dcc7ad 100644 --- a/uadk_tool/test/test_sec.c +++ b/uadk_tool/test/test_sec.c @@ -59,6 +59,7 @@ static unsigned int g_use_env; static unsigned int g_ctxnum; static unsigned int g_data_fmt = WD_FLAT_BUF; static unsigned int g_sgl_num = 0; +static unsigned int g_init; static pthread_spinlock_t lock = 0;
static struct hash_testvec g_long_hash_tv; @@ -85,6 +86,25 @@ enum digest_type { LOCAL_AES_XCBC_MAC_96, };
+char *digest_names[MAX_ALGO_PER_TYPE] = { + "sm3", + "md5", + "sha1", + "sha256", + "sha224", + "sha384", + "sha512", + "sha512-224", + "sha512-256", + "aes-cmac", + "aes-gmac", /* --digest 10: test aes-gmac-128 */ + "aes-gmac", /* --digest 11: test aes-gmac-192 */ + "aes-gmac", /* --digest 12: test aes-gmac-256 */ + "aes-xcbc-mac-96", + "aes-xcbc-prf-128", + "ccm(aes)", /* --digest 15: for error alg test */ +}; + struct sva_bd { char *src; char *dst; @@ -138,6 +158,7 @@ struct test_sec_option { __u32 stream_mode; __u32 sgl_num; __u32 use_env; + __u32 init; };
//static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; @@ -649,21 +670,6 @@ static void uninit_config(void) wd_sched_rr_release(g_sched); }
-static void digest_uninit_config(void) -{ - int i; - - if (g_use_env) { - wd_digest_env_uninit(); - return; - } - - wd_digest_uninit(); - for (i = 0; i < g_ctx_cfg.ctx_num; i++) - wd_release_ctx(g_ctx_cfg.ctxs[i].ctx); - free(g_ctx_cfg.ctxs); -} - static int test_sec_cipher_sync_once(void) { struct cipher_testvec *tv = NULL; @@ -1400,7 +1406,7 @@ static __u32 sched_digest_pick_next_ctx(handle_t h_sched_ctx, return 0; }
-static int init_digest_ctx_config(int type, int mode) +static int digest_init1(int type, int mode) { struct uacce_dev_list *list; struct wd_sched sched; @@ -1450,6 +1456,108 @@ out: return ret; }
+static int digest_init2(int type, int mode) +{ + struct wd_ctx_nums *ctx_set_num; + struct wd_ctx_params cparams; + int ret; + + if (g_testalg >= MAX_ALGO_PER_TYPE) + return -WD_EINVAL; + + ctx_set_num = calloc(1, sizeof(*ctx_set_num)); + if (!ctx_set_num) { + WD_ERR("failed to alloc ctx_set_size!\n"); + return -WD_ENOMEM; + } + + cparams.op_type_num = 1; + cparams.ctx_set_num = ctx_set_num; + cparams.bmp = numa_allocate_nodemask(); + if (!cparams.bmp) { + WD_ERR("failed to create nodemask!\n"); + ret = -WD_ENOMEM; + goto out_freectx; + } + + numa_bitmask_setall(cparams.bmp); + + if (mode == CTX_MODE_SYNC) + ctx_set_num->sync_ctx_num = g_ctxnum; + + if (mode == CTX_MODE_ASYNC) + ctx_set_num->async_ctx_num = g_ctxnum; + + ret = wd_digest_init2_(digest_names[g_testalg], 0, 0, &cparams); + if (ret) + goto out_freebmp; + +out_freebmp: + numa_free_nodemask(cparams.bmp); + +out_freectx: + free(ctx_set_num); + + return ret; +} + +static int init_digest_ctx_config(int type, int mode) +{ + int ret = -1; + + switch (g_init) { + case 0: + case 1: + SEC_TST_PRT("uadk entry init1!\n"); + ret = digest_init1(type, mode); + break; + case 2: + SEC_TST_PRT("uadk entry init2!\n"); + ret = digest_init2(type, mode); + break; + default: + SEC_TST_PRT("unsupported init-type%u!\n", g_init); + break; + } + + return ret; +} + +static void digest_uninit1(void) +{ + int i; + + if (g_use_env) { + wd_digest_env_uninit(); + return; + } + + wd_digest_uninit(); + for (i = 0; i < g_ctx_cfg.ctx_num; i++) + wd_release_ctx(g_ctx_cfg.ctxs[i].ctx); + free(g_ctx_cfg.ctxs); +} + +static void digest_uninit2(void) +{ + wd_digest_uninit2(); +} + +static void digest_uninit_config(void) +{ + switch (g_init) { + case 0: + case 1: + digest_uninit1(); + break; + case 2: + digest_uninit2(); + break; + default: + SEC_TST_PRT("unsupported uninit-type%u!\n", g_init); + } +} + int get_digest_resource(struct hash_testvec **alg_tv, int* alg, int* mode) { struct hash_testvec *tmp_tv; @@ -4083,6 +4191,7 @@ static void test_sec_cmd_parse(int argc, char *argv[], struct test_sec_option *o int c;
static struct option long_options[] = { + {"help", no_argument, 0, 0}, {"cipher", required_argument, 0, 1}, {"digest", required_argument, 0, 2}, {"aead", required_argument, 0, 3}, @@ -4100,7 +4209,7 @@ static void test_sec_cmd_parse(int argc, char *argv[], struct test_sec_option *o {"stream", no_argument, 0, 15}, {"sglnum", required_argument, 0, 16}, {"use_env", no_argument, 0, 17}, - {"help", no_argument, 0, 18}, + {"init", required_argument, 0, 18}, {0, 0, 0, 0} };
@@ -4110,6 +4219,9 @@ static void test_sec_cmd_parse(int argc, char *argv[], struct test_sec_option *o break;
switch (c) { + case 0: + print_help(); + exit(-1); case 1: option->algclass = CIPHER_CLASS; option->algtype = strtol(optarg, NULL, 0); @@ -4165,8 +4277,8 @@ static void test_sec_cmd_parse(int argc, char *argv[], struct test_sec_option *o option->use_env = 1; break; case 18: - print_help(); - exit(-1); + option->init = strtol(optarg, NULL, 0); + break; default: SEC_TST_PRT("bad input parameter, exit\n"); print_help(); @@ -4216,7 +4328,7 @@ static int test_sec_option_convert(struct test_sec_option *option) g_data_fmt = option->sgl_num ? WD_SGL_BUF : WD_FLAT_BUF; g_sgl_num = option->sgl_num; g_stream = option->stream_mode; - + g_init = option->init; SEC_TST_PRT("set global times is %lld\n", g_times);
g_thread_num = option->xmulti ? option->xmulti : 1;
add testcase cmd --init 2 for init2. default or --init 1 for init.
Signed-off-by: Hao Fang fanghao11@huawei.com --- uadk_tool/test/test_sec.c | 102 +++++++++++++++++++++++++++++++++++++- 1 file changed, 100 insertions(+), 2 deletions(-)
diff --git a/uadk_tool/test/test_sec.c b/uadk_tool/test/test_sec.c index 4dcc7ad..334f231 100644 --- a/uadk_tool/test/test_sec.c +++ b/uadk_tool/test/test_sec.c @@ -105,6 +105,17 @@ char *digest_names[MAX_ALGO_PER_TYPE] = { "ccm(aes)", /* --digest 15: for error alg test */ };
+char *aead_names[MAX_ALGO_PER_TYPE] = { + "ccm(aes)", + "gcm(aes)", + "authenc(hmac(sha256),cbc(aes))", + "ccm(sm4)", + "gcm(sm4)", + "authenc(hmac(sha256),cbc(sm4))", + "sm3", /*--aead 6: for error alg test */ + "authenc(hmac(sha3),cbc(aes))", /* --aead 7: for error alg test */ +}; + struct sva_bd { char *src; char *dst; @@ -2658,7 +2669,7 @@ static __u32 sched_aead_pick_next_ctx(handle_t h_sched_ctx, return 0; }
-static int init_aead_ctx_config(int type, int mode) +static int aead_init1(int type, int mode) { struct uacce_dev_list *list; struct wd_sched sched; @@ -2708,7 +2719,74 @@ out: return ret; }
-static void aead_uninit_config(void) +static int aead_init2(int type, int mode) +{ + struct wd_ctx_nums *ctx_set_num; + struct wd_ctx_params cparams; + int ret; + + if (g_testalg >= MAX_ALGO_PER_TYPE) + return -WD_EINVAL; + + ctx_set_num = calloc(1, sizeof(*ctx_set_num)); + if (!ctx_set_num) { + WD_ERR("failed to alloc ctx_set_size!\n"); + return -WD_ENOMEM; + } + + cparams.op_type_num = 1; + cparams.ctx_set_num = ctx_set_num; + cparams.bmp = numa_allocate_nodemask(); + if (!cparams.bmp) { + WD_ERR("failed to create nodemask!\n"); + ret = -WD_ENOMEM; + goto out_freectx; + } + + numa_bitmask_setall(cparams.bmp); + + if (mode == CTX_MODE_SYNC) + ctx_set_num->sync_ctx_num = g_ctxnum; + + if (mode == CTX_MODE_ASYNC) + ctx_set_num->async_ctx_num = g_ctxnum; + + ret = wd_aead_init2_(aead_names[g_testalg], 0, 0, &cparams); + if (ret) + goto out_freebmp; + +out_freebmp: + numa_free_nodemask(cparams.bmp); + +out_freectx: + free(ctx_set_num); + + return ret; +} + +static int init_aead_ctx_config(int type, int mode) +{ + int ret = -1; + + switch (g_init) { + case 0: + case 1: + SEC_TST_PRT("uadk entry init1!\n"); + ret = aead_init1(type, mode); + break; + case 2: + SEC_TST_PRT("uadk entry init2!\n"); + ret = aead_init2(type, mode); + break; + default: + SEC_TST_PRT("unsupported aead init-type%u!\n", g_init); + break; + } + + return ret; +} + +static void aead_uninit1(void) { int i;
@@ -2723,6 +2801,26 @@ static void aead_uninit_config(void) free(g_ctx_cfg.ctxs); }
+static void aead_uninit2(void) +{ + wd_aead_uninit2(); +} + +static void aead_uninit_config(void) +{ + switch (g_init) { + case 0: + case 1: + aead_uninit1(); + break; + case 2: + aead_uninit2(); + break; + default: + SEC_TST_PRT("unsupported aead uninit-type%u!\n", g_init); + } +} + int get_aead_resource(struct aead_testvec **alg_tv, int* alg, int* mode, int* dalg, int* dmode) {