Synchronize interface layer code to ensure that basic functions are consistent before adding new functions
Signed-off-by: Longfang Liu liulongfang@huawei.com --- include/wd_dh.h | 8 ++++- include/wd_ecc.h | 2 ++ include/wd_rsa.h | 5 +++ wd_aead.c | 38 +++++++++------------ wd_agg.c | 13 +++---- wd_dh.c | 32 ++++++++++++------ wd_ecc.c | 35 ++++++++++--------- wd_mempool.c | 11 ++++-- wd_rsa.c | 88 ++++++++++++++++++++++++++---------------------- wd_zlibwrapper.c | 30 +++++++++++------ 10 files changed, 152 insertions(+), 110 deletions(-)
diff --git a/include/wd_dh.h b/include/wd_dh.h index afc2f7c..12206c8 100644 --- a/include/wd_dh.h +++ b/include/wd_dh.h @@ -8,13 +8,19 @@ #define __WD_DH_H
#include <stdbool.h> +#include <asm/types.h>
+#include "wd.h" #include "wd_alg_common.h"
#ifdef __cplusplus extern "C" { #endif
+#define BYTE_BITS 8 +#define BYTE_BITS_SHIFT 3 +#define GET_NEGATIVE(val) (0 - (val)) + typedef void (*wd_dh_cb_t)(void *cb_param);
enum wd_dh_op_type { @@ -24,7 +30,7 @@ enum wd_dh_op_type { };
struct wd_dh_sess_setup { - __u16 key_bits; /* DH key bites */ + __u16 key_bits; /* DH key bits */ bool is_g2; /* is g2 mode or not */ void *sched_param; }; diff --git a/include/wd_ecc.h b/include/wd_ecc.h index 6f670e2..23fc809 100644 --- a/include/wd_ecc.h +++ b/include/wd_ecc.h @@ -9,7 +9,9 @@
#include <stdlib.h> #include <errno.h> +#include <asm/types.h>
+#include "wd.h" #include "wd_alg_common.h"
#ifdef __cplusplus diff --git a/include/wd_rsa.h b/include/wd_rsa.h index 733d0b7..200ff14 100644 --- a/include/wd_rsa.h +++ b/include/wd_rsa.h @@ -7,18 +7,23 @@ #ifndef __WD_RSA_H #define __WD_RSA_H
+#include <stdlib.h> #include <stdbool.h>
+#include "wd.h" #include "wd_alg_common.h"
#ifdef __cplusplus extern "C" { #endif
+#define BYTE_BITS 8 +#define BYTE_BITS_SHIFT 3 #define CRT_PARAMS_SZ(key_size) ((5 * (key_size)) >> 1) #define CRT_GEN_PARAMS_SZ(key_size) ((7 * (key_size)) >> 1) #define GEN_PARAMS_SZ(key_size) ((key_size) << 1) #define CRT_PARAM_SZ(key_size) ((key_size) >> 1) +#define GET_NEGATIVE(val) (0 - (val))
typedef void (*wd_rsa_cb_t)(void *cb_param);
diff --git a/wd_aead.c b/wd_aead.c index 608f0e6..7eb217a 100644 --- a/wd_aead.c +++ b/wd_aead.c @@ -7,6 +7,7 @@ #include <stdlib.h> #include <pthread.h> #include <limits.h> +#include "wd_util.h" #include "include/drv/wd_aead_drv.h" #include "wd_aead.h"
@@ -34,6 +35,7 @@ struct wd_aead_setting { struct wd_sched sched; struct wd_alg_driver *driver; struct wd_async_msg_pool pool; + void *priv; void *dlhandle; void *dlh_list; } wd_aead_setting; @@ -465,7 +467,8 @@ static int wd_aead_init_nolock(struct wd_ctx_config *config, struct wd_sched *sc goto out_clear_sched;
ret = wd_alg_init_driver(&wd_aead_setting.config, - wd_aead_setting.driver); + wd_aead_setting.driver, + &wd_aead_setting.priv); if (ret) goto out_clear_pool;
@@ -514,30 +517,21 @@ out_clear_init: return ret; }
-static int wd_aead_uninit_nolock(void) +static void wd_aead_uninit_nolock(void) { - enum wd_status status; - - wd_alg_get_init(&wd_aead_setting.status, &status); - if (status == WD_UNINIT) - return -WD_EINVAL; - wd_uninit_async_request_pool(&wd_aead_setting.pool); wd_clear_sched(&wd_aead_setting.sched); wd_alg_uninit_driver(&wd_aead_setting.config, - wd_aead_setting.driver); - - return 0; + wd_aead_setting.driver, + &wd_aead_setting.priv); }
void wd_aead_uninit(void) { - int ret; - - ret = wd_aead_uninit_nolock(); - if (ret) + if (!wd_aead_setting.priv) return;
+ wd_aead_uninit_nolock(); wd_aead_close_driver(WD_TYPE_V1); wd_alg_clear_init(&wd_aead_setting.status); } @@ -643,12 +637,10 @@ out_uninit:
void wd_aead_uninit2(void) { - int ret; - - ret = wd_aead_uninit_nolock(); - if (ret) + if (!wd_aead_setting.priv) return;
+ wd_aead_uninit_nolock(); wd_alg_attrs_uninit(&wd_aead_init_attrs); wd_alg_drv_unbind(wd_aead_setting.driver); wd_aead_close_driver(WD_TYPE_V2); @@ -735,8 +727,8 @@ static int send_recv_sync(struct wd_ctx_internal *ctx, msg_handle.recv = wd_aead_setting.driver->recv;
pthread_spin_lock(&ctx->lock); - ret = wd_handle_msg_sync(wd_aead_setting.driver, &msg_handle, ctx->ctx, - msg, NULL, wd_aead_setting.config.epoll_en); + ret = wd_handle_msg_sync(&msg_handle, ctx->ctx, msg, NULL, + wd_aead_setting.config.epoll_en); pthread_spin_unlock(&ctx->lock);
return ret; @@ -811,7 +803,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_alg_driver_send(wd_aead_setting.driver, 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"); @@ -860,7 +852,7 @@ int wd_aead_poll_ctx(__u32 idx, __u32 expt, __u32 *count) ctx = config->ctxs + idx;
do { - ret = wd_alg_driver_recv(wd_aead_setting.driver, ctx->ctx, &resp_msg); + ret = wd_aead_setting.driver->recv(ctx->ctx, &resp_msg); if (ret == -WD_EAGAIN) { return ret; } else if (ret < 0) { diff --git a/wd_agg.c b/wd_agg.c index b536efe..e0d7bbf 100644 --- a/wd_agg.c +++ b/wd_agg.c @@ -592,7 +592,8 @@ static int wd_agg_alg_init(struct wd_ctx_config *config, struct wd_sched *sched) if (ret < 0) goto out_clear_sched;
- ret = wd_alg_init_driver(&wd_agg_setting.config, wd_agg_setting.driver); + ret = wd_alg_init_driver(&wd_agg_setting.config, wd_agg_setting.driver, + &wd_agg_setting.priv); if (ret) goto out_clear_pool;
@@ -620,7 +621,8 @@ static int wd_agg_alg_uninit(void) /* Unset config, sched, driver */ wd_clear_sched(&wd_agg_setting.sched);
- wd_alg_uninit_driver(&wd_agg_setting.config, wd_agg_setting.driver); + wd_alg_uninit_driver(&wd_agg_setting.config, wd_agg_setting.driver, + &wd_agg_setting.priv);
return WD_SUCCESS; } @@ -1098,8 +1100,7 @@ static int wd_agg_sync_job(struct wd_agg_sess *sess, struct wd_agg_req *req, msg_handle.recv = wd_agg_setting.driver->recv;
pthread_spin_lock(&ctx->lock); - ret = wd_handle_msg_sync(wd_agg_setting.driver, &msg_handle, ctx->ctx, - msg, NULL, config->epoll_en); + ret = wd_handle_msg_sync(&msg_handle, ctx->ctx, msg, NULL, config->epoll_en); pthread_spin_unlock(&ctx->lock);
return ret; @@ -1201,7 +1202,7 @@ static int wd_agg_async_job(struct wd_agg_sess *sess, struct wd_agg_req *req, bo else fill_request_msg_output(msg, req, sess, false); msg->tag = msg_id; - ret = wd_alg_driver_send(wd_agg_setting.driver, ctx->ctx, msg); + ret = wd_agg_setting.driver->send(ctx->ctx, msg); if (unlikely(ret < 0)) { if (ret != -WD_EBUSY) WD_ERR("wd agg async send err!\n"); @@ -1528,7 +1529,7 @@ static int wd_agg_poll_ctx(__u32 idx, __u32 expt, __u32 *count) ctx = config->ctxs + idx;
do { - ret = wd_alg_driver_recv(wd_agg_setting.driver, ctx->ctx, &resp_msg); + ret = wd_agg_setting.driver->recv(ctx->ctx, &resp_msg); if (ret == -WD_EAGAIN) { return ret; } else if (unlikely(ret < 0)) { diff --git a/wd_dh.c b/wd_dh.c index 043c3be..2e4b5ed 100644 --- a/wd_dh.c +++ b/wd_dh.c @@ -14,6 +14,7 @@
#include "include/drv/wd_dh_drv.h" #include "wd_util.h" +#include "wd_dh.h"
#define DH_MAX_KEY_SIZE 512 #define WD_DH_G2 2 @@ -34,6 +35,7 @@ static struct wd_dh_setting { struct wd_sched sched; struct wd_async_msg_pool pool; struct wd_alg_driver *driver; + void *priv; void *dlhandle; void *dlh_list; } wd_dh_setting; @@ -84,6 +86,13 @@ static int wd_dh_open_driver(int init_type) return WD_SUCCESS; }
+ /* + * Compatible with the normal acquisition of device + * drivers in the init interface. + */ + if (wd_dh_setting.dlh_list) + return WD_SUCCESS; + ret = wd_get_lib_file_path("libhisi_hpre.so", lib_path, false); if (ret) return ret; @@ -140,7 +149,8 @@ static int wd_dh_common_init(struct wd_ctx_config *config, struct wd_sched *sche goto out_clear_sched;
ret = wd_alg_init_driver(&wd_dh_setting.config, - wd_dh_setting.driver); + wd_dh_setting.driver, + &wd_dh_setting.priv); if (ret) goto out_clear_pool;
@@ -157,11 +167,10 @@ out_clear_ctx_config:
static int wd_dh_common_uninit(void) { - enum wd_status status; - - wd_alg_get_init(&wd_dh_setting.status, &status); - if (status == WD_UNINIT) + if (!wd_dh_setting.priv) { + WD_ERR("invalid: repeat uninit dh!\n"); return -WD_EINVAL; + }
/* uninit async request pool */ wd_uninit_async_request_pool(&wd_dh_setting.pool); @@ -169,7 +178,8 @@ static int wd_dh_common_uninit(void) /* unset config, sched, driver */ wd_clear_sched(&wd_dh_setting.sched); wd_alg_uninit_driver(&wd_dh_setting.config, - wd_dh_setting.driver); + wd_dh_setting.driver, + &wd_dh_setting.priv);
return WD_SUCCESS; } @@ -385,8 +395,8 @@ int wd_do_dh_sync(handle_t sess, struct wd_dh_req *req) msg_handle.recv = wd_dh_setting.driver->recv;
pthread_spin_lock(&ctx->lock); - ret = wd_handle_msg_sync(wd_dh_setting.driver, &msg_handle, ctx->ctx, - &msg, &balance, wd_dh_setting.config.epoll_en); + ret = wd_handle_msg_sync(&msg_handle, ctx->ctx, &msg, &balance, + wd_dh_setting.config.epoll_en); pthread_spin_unlock(&ctx->lock); if (unlikely(ret)) return ret; @@ -432,7 +442,7 @@ int wd_do_dh_async(handle_t sess, struct wd_dh_req *req) goto fail_with_msg; msg->tag = mid;
- ret = wd_alg_driver_send(wd_dh_setting.driver, ctx->ctx, msg); + ret = wd_dh_setting.driver->send(ctx->ctx, msg); if (unlikely(ret)) { if (ret != -WD_EBUSY) WD_ERR("failed to send dh BD, hw is err!\n"); @@ -483,7 +493,7 @@ int wd_dh_poll_ctx(__u32 idx, __u32 expt, __u32 *count) ctx = config->ctxs + idx;
do { - ret = wd_alg_driver_recv(wd_dh_setting.driver, ctx->ctx, &rcv_msg); + ret = wd_dh_setting.driver->recv(ctx->ctx, &rcv_msg); if (ret == -WD_EAGAIN) { return ret; } else if (unlikely(ret)) { @@ -616,7 +626,7 @@ handle_t wd_dh_alloc_sess(struct wd_dh_sess_setup *setup) sess->sched_key = (void *)wd_dh_setting.sched.sched_init( wd_dh_setting.sched.h_sched_ctx, setup->sched_param); if (WD_IS_ERR(sess->sched_key)) { - WD_ERR("failed to init session schedule key!\n"); + WD_ERR("failed to init dh session schedule key!\n"); goto sched_err; }
diff --git a/wd_ecc.c b/wd_ecc.c index b1712c5..a937ff9 100644 --- a/wd_ecc.c +++ b/wd_ecc.c @@ -7,12 +7,13 @@ #include <dirent.h> #include <errno.h> #include <pthread.h> -#include <stdlib.h> #include <stddef.h> +#include <stdlib.h> #include <string.h> #include <time.h> #include <dlfcn.h>
+#include "wd_util.h" #include "include/drv/wd_ecc_drv.h" #include "include/wd_ecc_curve.h" #include "wd_ecc.h" @@ -65,6 +66,7 @@ static struct wd_ecc_setting { struct wd_sched sched; struct wd_async_msg_pool pool; struct wd_alg_driver *driver; + void *priv; void *dlhandle; void *dlh_list; } wd_ecc_setting; @@ -203,7 +205,8 @@ static int wd_ecc_common_init(struct wd_ctx_config *config, struct wd_sched *sch goto out_clear_sched;
ret = wd_alg_init_driver(&wd_ecc_setting.config, - wd_ecc_setting.driver); + wd_ecc_setting.driver, + &wd_ecc_setting.priv); if (ret) goto out_clear_pool;
@@ -220,11 +223,10 @@ out_clear_ctx_config:
static int wd_ecc_common_uninit(void) { - enum wd_status status; - - wd_alg_get_init(&wd_ecc_setting.status, &status); - if (status == WD_UNINIT) + if (!wd_ecc_setting.priv) { + WD_ERR("invalid: repeat uninit ecc!\n"); return -WD_EINVAL; + }
/* uninit async request pool */ wd_uninit_async_request_pool(&wd_ecc_setting.pool); @@ -232,7 +234,8 @@ static int wd_ecc_common_uninit(void) /* unset config, sched, driver */ wd_clear_sched(&wd_ecc_setting.sched); wd_alg_uninit_driver(&wd_ecc_setting.config, - wd_ecc_setting.driver); + wd_ecc_setting.driver, + &wd_ecc_setting.priv);
return WD_SUCCESS; } @@ -296,7 +299,7 @@ int wd_ecc_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_para return state;
if (!alg || sched_type >= SCHED_POLICY_BUTT || - task_type < 0 || task_type >= TASK_MAX_TYPE) { + task_type < 0 || task_type >= TASK_MAX_TYPE) { WD_ERR("invalid: input param is wrong!\n"); goto out_clear_init; } @@ -1197,7 +1200,7 @@ handle_t wd_ecc_alloc_sess(struct wd_ecc_sess_setup *setup) sess->sched_key = (void *)wd_ecc_setting.sched.sched_init( wd_ecc_setting.sched.h_sched_ctx, setup->sched_param); if (WD_IS_ERR(sess->sched_key)) { - WD_ERR("failed to init session schedule key!\n"); + WD_ERR("failed to init ecc session schedule key!\n"); goto sched_err; }
@@ -1504,7 +1507,7 @@ static int fill_ecc_msg(struct wd_ecc_msg *msg, struct wd_ecc_req *req, void *key = NULL;
memcpy(&msg->req, req, sizeof(msg->req)); - msg->hash = sess->setup.hash; + memcpy(&msg->hash, &sess->setup.hash, sizeof(msg->hash)); msg->key_bytes = sess->key_size; msg->curve_id = sess->setup.cv.cfg.id; msg->result = WD_EINVAL; @@ -1590,8 +1593,8 @@ int wd_do_ecc_sync(handle_t h_sess, struct wd_ecc_req *req) msg_handle.recv = wd_ecc_setting.driver->recv;
pthread_spin_lock(&ctx->lock); - ret = wd_handle_msg_sync(wd_ecc_setting.driver, &msg_handle, ctx->ctx, &msg, - &balance, wd_ecc_setting.config.epoll_en); + ret = wd_handle_msg_sync(&msg_handle, ctx->ctx, &msg, &balance, + wd_ecc_setting.config.epoll_en); pthread_spin_unlock(&ctx->lock); if (unlikely(ret)) return ret; @@ -1655,10 +1658,10 @@ static int set_sign_in_param(struct wd_ecc_sign_in *sin,
static int generate_random(struct wd_ecc_sess *sess, struct wd_dtb *k) { - struct wd_rand_mt rand_t = sess->setup.rand; + struct wd_rand_mt *rand_t = &sess->setup.rand; int ret;
- ret = rand_t.cb(k->data, k->dsize, rand_t.usr); + ret = rand_t->cb(k->data, k->dsize, rand_t->usr); if (ret) WD_ERR("failed to do rand cb, ret = %d!\n", ret);
@@ -2278,7 +2281,7 @@ int wd_do_ecc_async(handle_t sess, struct wd_ecc_req *req) goto fail_with_msg; msg->tag = mid;
- ret = wd_alg_driver_send(wd_ecc_setting.driver, ctx->ctx, msg); + ret = wd_ecc_setting.driver->send(ctx->ctx, msg); if (unlikely(ret)) { if (ret != -WD_EBUSY) WD_ERR("failed to send ecc BD, hw is err!\n"); @@ -2327,7 +2330,7 @@ int wd_ecc_poll_ctx(__u32 idx, __u32 expt, __u32 *count) ctx = config->ctxs + idx;
do { - ret = wd_alg_driver_recv(wd_ecc_setting.driver, ctx->ctx, &recv_msg); + ret = wd_ecc_setting.driver->recv(ctx->ctx, &recv_msg); if (ret == -WD_EAGAIN) { return ret; } else if (ret < 0) { diff --git a/wd_mempool.c b/wd_mempool.c index 22db843..af4182a 100644 --- a/wd_mempool.c +++ b/wd_mempool.c @@ -56,7 +56,7 @@ static inline int wd_atomic_test_add(struct wd_ref *ref, int a, int u) c = __atomic_load_n(&ref->ref, __ATOMIC_RELAXED); if (c == u) break; - } while (!__atomic_compare_exchange_n(&ref->ref, (__u32 *)&c, c + a, true, + } while (! __atomic_compare_exchange_n(&ref->ref, &c, c + a, true, __ATOMIC_RELAXED, __ATOMIC_RELAXED));
return c; @@ -215,7 +215,9 @@ static struct bitmap *create_bitmap(int bits) static void destroy_bitmap(struct bitmap *bm) { if (bm) { - free(bm->map); + if (bm->map) + free(bm->map); + free(bm); } } @@ -283,6 +285,11 @@ static int test_bit(struct bitmap *bm, unsigned int nr) return !(*p & mask); }
+inline static size_t wd_get_page_size(void) +{ + return sysconf(_SC_PAGESIZE); +} + void *wd_block_alloc(handle_t blkpool) { struct blkpool *bp = (struct blkpool*)blkpool; diff --git a/wd_rsa.c b/wd_rsa.c index 366e766..c6b032e 100644 --- a/wd_rsa.c +++ b/wd_rsa.c @@ -13,6 +13,7 @@ #include <dlfcn.h>
#include "include/drv/wd_rsa_drv.h" +#include "wd_util.h" #include "wd_rsa.h"
#define RSA_MAX_KEY_SIZE 512 @@ -45,10 +46,8 @@ struct wd_rsa_prikey2 { };
struct wd_rsa_prikey { - union { - struct wd_rsa_prikey1 pkey1; - struct wd_rsa_prikey2 pkey2; - } pkey; + struct wd_rsa_prikey1 pkey1; + struct wd_rsa_prikey2 pkey2; };
/* RSA private key parameter types */ @@ -75,6 +74,7 @@ static struct wd_rsa_setting { struct wd_sched sched; struct wd_async_msg_pool pool; struct wd_alg_driver *driver; + void *priv; void *dlhandle; void *dlh_list; } wd_rsa_setting; @@ -125,6 +125,13 @@ static int wd_rsa_open_driver(int init_type) return WD_SUCCESS; }
+ /* + * Compatible with the normal acquisition of device + * drivers in the init interface. + */ + if (wd_rsa_setting.dlh_list) + return WD_SUCCESS; + ret = wd_get_lib_file_path("libhisi_hpre.so", lib_path, false); if (ret) return ret; @@ -180,7 +187,8 @@ static int wd_rsa_common_init(struct wd_ctx_config *config, struct wd_sched *sch goto out_clear_sched;
ret = wd_alg_init_driver(&wd_rsa_setting.config, - wd_rsa_setting.driver); + wd_rsa_setting.driver, + &wd_rsa_setting.priv); if (ret) goto out_clear_pool;
@@ -197,11 +205,10 @@ out_clear_ctx_config:
static int wd_rsa_common_uninit(void) { - enum wd_status status; - - wd_alg_get_init(&wd_rsa_setting.status, &status); - if (status == WD_UNINIT) + if (!wd_rsa_setting.priv) { + WD_ERR("invalid: repeat uninit rsa!\n"); return -WD_EINVAL; + }
/* uninit async request pool */ wd_uninit_async_request_pool(&wd_rsa_setting.pool); @@ -209,7 +216,8 @@ static int wd_rsa_common_uninit(void) /* unset config, sched, driver */ wd_clear_sched(&wd_rsa_setting.sched); wd_alg_uninit_driver(&wd_rsa_setting.config, - wd_rsa_setting.driver); + wd_rsa_setting.driver, + &wd_rsa_setting.priv);
return WD_SUCCESS; } @@ -446,8 +454,8 @@ int wd_do_rsa_sync(handle_t h_sess, struct wd_rsa_req *req) msg_handle.recv = wd_rsa_setting.driver->recv;
pthread_spin_lock(&ctx->lock); - ret = wd_handle_msg_sync(wd_rsa_setting.driver, &msg_handle, ctx->ctx, &msg, - &balance, wd_rsa_setting.config.epoll_en); + ret = wd_handle_msg_sync(&msg_handle, ctx->ctx, &msg, &balance, + wd_rsa_setting.config.epoll_en); pthread_spin_unlock(&ctx->lock); if (unlikely(ret)) return ret; @@ -493,7 +501,7 @@ int wd_do_rsa_async(handle_t sess, struct wd_rsa_req *req) goto fail_with_msg; msg->tag = mid;
- ret = wd_alg_driver_send(wd_rsa_setting.driver, ctx->ctx, msg); + ret = wd_rsa_setting.driver->send(ctx->ctx, msg); if (unlikely(ret)) { if (ret != -WD_EBUSY) WD_ERR("failed to send rsa BD, hw is err!\n"); @@ -542,7 +550,7 @@ int wd_rsa_poll_ctx(__u32 idx, __u32 expt, __u32 *count) ctx = config->ctxs + idx;
do { - ret = wd_alg_driver_recv(wd_rsa_setting.driver, ctx->ctx, &recv_msg); + ret = wd_rsa_setting.driver->recv(ctx->ctx, &recv_msg); if (ret == -WD_EAGAIN) { return ret; } else if (ret < 0) { @@ -863,7 +871,7 @@ static int create_sess_key(struct wd_rsa_sess_setup *setup, WD_ERR("failed to alloc sess prikey2!\n"); return -WD_ENOMEM; } - pkey2 = &sess->prikey->pkey.pkey2; + pkey2 = &sess->prikey->pkey2; memset(sess->prikey, 0, len); init_pkey2(pkey2, sess->key_size); } else { @@ -874,7 +882,7 @@ static int create_sess_key(struct wd_rsa_sess_setup *setup, WD_ERR("failed to alloc sess prikey1!\n"); return -WD_ENOMEM; } - pkey1 = &sess->prikey->pkey.pkey1; + pkey1 = &sess->prikey->pkey1; memset(sess->prikey, 0, len); init_pkey1(pkey1, sess->key_size); } @@ -905,9 +913,9 @@ static void del_sess_key(struct wd_rsa_sess *sess) }
if (sess->setup.is_crt) - wd_memset_zero(prk->pkey.pkey2.data, CRT_PARAMS_SZ(sess->key_size)); + wd_memset_zero(prk->pkey2.data, CRT_PARAMS_SZ(sess->key_size)); else - wd_memset_zero(prk->pkey.pkey1.data, GEN_PARAMS_SZ(sess->key_size)); + wd_memset_zero(prk->pkey1.data, GEN_PARAMS_SZ(sess->key_size)); free(sess->prikey); free(sess->pubkey); } @@ -954,7 +962,7 @@ handle_t wd_rsa_alloc_sess(struct wd_rsa_sess_setup *setup) sess->sched_key = (void *)wd_rsa_setting.sched.sched_init( wd_rsa_setting.sched.h_sched_ctx, setup->sched_param); if (WD_IS_ERR(sess->sched_key)) { - WD_ERR("failed to init session schedule key!\n"); + WD_ERR("failed to init rsa session schedule key!\n"); goto sched_err; }
@@ -1051,6 +1059,24 @@ void wd_rsa_get_pubkey_params(struct wd_rsa_pubkey *pbk, struct wd_dtb **e, *n = &pbk->n; }
+void wd_rsa_get_prikey_params(struct wd_rsa_prikey *pvk, struct wd_dtb **d, + struct wd_dtb **n) +{ + struct wd_rsa_prikey1 *pkey1; + + if (!pvk) { + WD_ERR("invalid: pvk is NULL!\n"); + return; + } + + pkey1 = &pvk->pkey1; + + if (d) + *d = &pkey1->d; + if (n) + *n = &pkey1->n; +} + int wd_rsa_set_prikey_params(handle_t sess, struct wd_dtb *d, struct wd_dtb *n) { struct wd_rsa_prikey1 *pkey1; @@ -1060,7 +1086,7 @@ int wd_rsa_set_prikey_params(handle_t sess, struct wd_dtb *d, struct wd_dtb *n) WD_ERR("invalid: sess err in set rsa private key1!\n"); return -WD_EINVAL; } - pkey1 = &c->prikey->pkey.pkey1; + pkey1 = &c->prikey->pkey1; if (d) { if (!d->dsize || !d->data || d->dsize > pkey1->key_size) { WD_ERR("invalid: d err in set rsa private key1!\n"); @@ -1085,24 +1111,6 @@ int wd_rsa_set_prikey_params(handle_t sess, struct wd_dtb *d, struct wd_dtb *n) return WD_SUCCESS; }
-void wd_rsa_get_prikey_params(struct wd_rsa_prikey *pvk, struct wd_dtb **d, - struct wd_dtb **n) -{ - struct wd_rsa_prikey1 *pkey1; - - if (!pvk) { - WD_ERR("invalid: pvk is NULL!\n"); - return; - } - - pkey1 = &pvk->pkey.pkey1; - - if (d) - *d = &pkey1->d; - if (n) - *n = &pkey1->n; -} - static int rsa_set_param(struct wd_dtb *src, struct wd_dtb *dst) { if (!src || !dst || dst->dsize > src->bsize) @@ -1167,7 +1175,7 @@ int wd_rsa_set_crt_prikey_params(handle_t sess, struct wd_dtb *dq, return ret; }
- pkey2 = &c->prikey->pkey.pkey2; + pkey2 = &c->prikey->pkey2; ret = rsa_prikey2_param_set(pkey2, dq, WD_CRT_PRIKEY_DQ); if (ret) { WD_ERR("failed to set dq for rsa private key2!\n"); @@ -1213,7 +1221,7 @@ void wd_rsa_get_crt_prikey_params(struct wd_rsa_prikey *pvk, return; }
- pkey2 = &pvk->pkey.pkey2; + pkey2 = &pvk->pkey2;
if (dq) *dq = &pkey2->dq; diff --git a/wd_zlibwrapper.c b/wd_zlibwrapper.c index b3b315f..f5834e7 100644 --- a/wd_zlibwrapper.c +++ b/wd_zlibwrapper.c @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 /* - * Copyright 2022 Huawei Technologies Co.,Ltd. All rights reserved. + * Copyright 2023 Huawei Technologies Co.,Ltd. All rights reserved. */
/* === Dependencies === */ @@ -102,23 +102,27 @@ static int wd_zlib_analy_alg(int windowbits, int *alg, int *windowsize) static const int WBINS_ZLIB_4K = 12; static const int WBINS_GZIP_4K = 27; static const int WBINS_DEFLATE_4K = -12; + int ret = Z_STREAM_ERROR;
if ((windowbits >= ZLIB_MIN_WBITS) && (windowbits <= ZLIB_MAX_WBITS)) { *alg = WD_ZLIB; *windowsize = max(windowbits - WBINS_ZLIB_4K, WD_COMP_WS_4K); + ret = Z_OK; } else if ((windowbits >= GZIP_MIN_WBITS) && (windowbits <= GZIP_MAX_WBITS)) { *alg = WD_GZIP; *windowsize = max(windowbits - WBINS_GZIP_4K, WD_COMP_WS_4K); + ret = Z_OK; } else if ((windowbits >= DEFLATE_MIN_WBITS) && (windowbits <= DEFLATE_MAX_WBITS)) { *alg = WD_DEFLATE; *windowsize = max(windowbits - WBINS_DEFLATE_4K, WD_COMP_WS_4K); + ret = Z_OK; } else { - return Z_STREAM_ERROR; + return ret; }
*windowsize = *windowsize == WD_COMP_WS_24K ? WD_COMP_WS_32K : *windowsize;
- return Z_OK; + return ret; }
static int wd_zlib_alloc_sess(z_streamp strm, int level, int windowbits, enum wd_comp_op_type type) @@ -160,9 +164,6 @@ static int wd_zlib_init(z_streamp strm, int level, int windowbits, enum wd_comp_ { int ret;
- if (unlikely(!strm)) - return Z_STREAM_ERROR; - pthread_mutex_lock(&wd_zlib_mutex); ret = wd_zlib_uadk_init(); if (unlikely(ret < 0)) @@ -193,9 +194,6 @@ static int wd_zlib_uninit(z_streamp strm) { int ret;
- if (unlikely(!strm)) - return Z_STREAM_ERROR; - wd_zlib_free_sess(strm);
pthread_mutex_lock(&wd_zlib_mutex); @@ -257,6 +255,9 @@ static int wd_zlib_do_request(z_streamp strm, int flush, enum wd_comp_op_type ty /* === Compression === */ int wd_deflate_init(z_streamp strm, int level, int windowbits) { + if (!strm) + return Z_STREAM_ERROR; + pthread_atfork(NULL, NULL, wd_zlib_unlock);
return wd_zlib_init(strm, level, windowbits, WD_DIR_COMPRESS); @@ -272,7 +273,7 @@ int wd_deflate(z_streamp strm, int flush)
int wd_deflate_reset(z_streamp strm) { - if (unlikely(!strm)) + if (!strm) return Z_STREAM_ERROR;
wd_comp_reset_sess((handle_t)strm->reserved); @@ -285,13 +286,17 @@ int wd_deflate_reset(z_streamp strm)
int wd_deflate_end(z_streamp strm) { + if (!strm) + return Z_STREAM_ERROR; + return wd_zlib_uninit(strm); }
/* === Decompression === */ int wd_inflate_init(z_streamp strm, int windowbits) { - pthread_atfork(NULL, NULL, wd_zlib_unlock); + if (!strm) + return Z_STREAM_ERROR;
return wd_zlib_init(strm, 0, windowbits, WD_DIR_DECOMPRESS); } @@ -319,5 +324,8 @@ int wd_inflate_reset(z_streamp strm)
int wd_inflate_end(z_streamp strm) { + if (!strm) + return Z_STREAM_ERROR; + return wd_zlib_uninit(strm); }