From: Longfang Liu liulongfang@huawei.com
Update the performance test code of sec's software mode, and optimize the performance test code of sec's software mode by splitting sub-functions.
Signed-off-by: Longfang Liu liulongfang@huawei.com Signed-off-by: Zhiqi Song songzhiqi1@huawei.com --- uadk_tool/benchmark/sec_soft_benchmark.c | 790 +++++++++++++++-------- 1 file changed, 517 insertions(+), 273 deletions(-)
diff --git a/uadk_tool/benchmark/sec_soft_benchmark.c b/uadk_tool/benchmark/sec_soft_benchmark.c index a622329..c7870aa 100644 --- a/uadk_tool/benchmark/sec_soft_benchmark.c +++ b/uadk_tool/benchmark/sec_soft_benchmark.c @@ -36,17 +36,20 @@ struct thread_pool { typedef struct soft_thread_res { const EVP_CIPHER *evp_cipher; const EVP_MD *evp_md; + ENGINE *engine; u32 subtype; u32 mode; u32 keysize; u32 optype; u32 td_id; u32 engine_flag; + u32 sync_mode; } soft_thread;
typedef struct soft_jobs_res { const EVP_CIPHER *evp_cipher; const EVP_MD *evp_md; + ENGINE *engine; u32 subtype; u32 mode; u32 keysize; @@ -56,12 +59,20 @@ typedef struct soft_jobs_res { u32 use_engine; } jobs_data;
+typedef struct soft_loop_args { + ASYNC_JOB *in_job; + ASYNC_WAIT_CTX *wait_ctx; + bool job_valid; +} jobs_args; + #define MAX_IVK_LENTH 64 #define DEF_IVK_DATA 0xAA +#define MAX_JOBS_NUM MAX_CTX_NUM
static unsigned int g_thread_num; static unsigned int g_ctxnum; static unsigned int g_pktlen; +static unsigned int g_jobsnum;
static int init_soft_bd_pool(void) { @@ -396,25 +407,18 @@ static int sec_soft_param_parse(soft_thread *tddata, struct acc_option *options) return 0; }
-static int sec_soft_jobfunc(void *args) +static int sec_soft_cipher_jobfunc(void *args) { jobs_data *jdata = (jobs_data *)args; const EVP_CIPHER *evp_cipher = jdata->evp_cipher; - const EVP_MD *evp_md = jdata->evp_md; u32 optype = jdata->optype; u32 jid = jdata->jobid; struct bd_pool *soft_pool; u8 *priv_iv, *priv_key; int ret, outl, i = 0; EVP_CIPHER_CTX *ctx; - EVP_MD_CTX *md_ctx; - HMAC_CTX *hm_ctx; ASYNC_JOB *currjob; - u8 faketag[16] = {0xcc}; - u8 aad[13] = {0xcc}; - u8 tag[12] = {0}; - u8 mac[EVP_MAX_MD_SIZE] = {0x00}; - u32 ssl_size = 0; + u32 count = 0; u8 *src, *dst;
currjob = ASYNC_get_current_job(); @@ -423,7 +427,7 @@ static int sec_soft_jobfunc(void *args) return 0; }
- if (!evp_cipher && !evp_md) { + if (!evp_cipher) { SSL_TST_PRT("Error: openssl not support!\n"); return 0; } @@ -438,54 +442,110 @@ static int sec_soft_jobfunc(void *args) memset(priv_iv, DEF_IVK_DATA, MAX_IVK_LENTH); memset(priv_key, DEF_IVK_DATA, MAX_IVK_LENTH);
- switch(jdata->subtype) { - case CIPHER_TYPE: - ctx = EVP_CIPHER_CTX_new(); - if (!ctx) - return 0; - - EVP_CIPHER_CTX_set_padding(ctx, 0); + ctx = EVP_CIPHER_CTX_new(); + if (!ctx) + return 0;
- ret = EVP_CipherInit_ex(ctx, evp_cipher, NULL, priv_key, priv_iv, optype); - if (ret != 1) { - SSL_TST_PRT("Error: EVP_CipherInit_ex fail ret: %d\n", ret); - break; - } + EVP_CIPHER_CTX_init(ctx);
+ while (1) { i = jid % MAX_POOL_LENTH; src = soft_pool->bds[i].src; dst = soft_pool->bds[i].dst; - if (optype) - ret = EVP_DecryptUpdate(ctx, dst, &outl, src, g_pktlen); - else - ret = EVP_EncryptUpdate(ctx, dst, &outl, src, g_pktlen); - if (ret != 1) - EVP_CipherInit_ex(ctx, NULL, NULL, NULL, priv_iv, -1);
- if (optype) + if (optype) { + ret = EVP_CipherInit_ex(ctx, evp_cipher, NULL, priv_key, priv_iv, optype); + if (ret != 1) { + SSL_TST_PRT("Error: EVP_CipherInit_ex fail ret: %d\n", ret); + break; + } + + ret = EVP_DecryptUpdate(ctx, dst, &outl, src, g_pktlen); + if (ret != 1) + break; + EVP_CIPHER_CTX_set_padding(ctx, 0); EVP_DecryptFinal_ex(ctx, dst, &outl); - else + } else { + ret = EVP_EncryptInit_ex(ctx, evp_cipher, jdata->engine, priv_key, priv_iv); + if (ret != 1) + break; + + ret = EVP_EncryptUpdate(ctx, dst, &outl, src, g_pktlen); + if (ret != 1) + break; + EVP_CIPHER_CTX_set_padding(ctx, EVP_PADDING_ZERO); EVP_EncryptFinal_ex(ctx, dst, &outl); + }
- EVP_CIPHER_CTX_free(ctx); - break; - case AEAD_TYPE: - ctx = EVP_CIPHER_CTX_new(); - if (!ctx) - return 0; + count++; + if (get_run_state() == 0) + break; + }
- EVP_CIPHER_CTX_set_padding(ctx, 0); + EVP_CIPHER_CTX_cleanup(ctx); + EVP_CIPHER_CTX_free(ctx);
- ret = EVP_CipherInit_ex(ctx, evp_cipher, NULL, priv_key, priv_iv, optype); - if (ret != 1) { - SSL_TST_PRT("Error: AEAD EVP_CipherInit_ex fail ret: %d\n", ret); - break; - } + add_recv_data(count, g_pktlen);
+ return 0; +} + +static int sec_soft_aead_jobfunc(void *args) +{ + jobs_data *jdata = (jobs_data *)args; + const EVP_CIPHER *evp_cipher = jdata->evp_cipher; + u32 optype = jdata->optype; + u32 jid = jdata->jobid; + struct bd_pool *soft_pool; + u8 *priv_iv, *priv_key; + int ret, outl, i = 0; + EVP_CIPHER_CTX *ctx; + ASYNC_JOB *currjob; + u8 faketag[16] = {0xcc}; + u8 aad[13] = {0xcc}; + u8 tag[12] = {0}; + u32 count = 0; + u8 *src, *dst; + + currjob = ASYNC_get_current_job(); + if (!currjob) { + SSL_TST_PRT("Error: not executing within a job\n"); + return 0; + } + + if (!evp_cipher) { + SSL_TST_PRT("Error: openssl not support!\n"); + return 0; + } + + if (jdata->td_id > g_thread_num) + return 0; + + soft_pool = &g_soft_pool.pool[jdata->td_id]; + priv_iv = &g_soft_pool.iv[jdata->td_id]; + priv_key = &g_soft_pool.key[jdata->td_id]; + + memset(priv_iv, DEF_IVK_DATA, MAX_IVK_LENTH); + memset(priv_key, DEF_IVK_DATA, MAX_IVK_LENTH); + + ctx = EVP_CIPHER_CTX_new(); + if (!ctx) + return 0; + + EVP_CIPHER_CTX_set_padding(ctx, 0); + + ret = EVP_CipherInit_ex(ctx, evp_cipher, NULL, priv_key, priv_iv, optype); + if (ret != 1) { + SSL_TST_PRT("Error: AEAD EVP_CipherInit_ex fail ret: %d\n", ret); + return 0; + } + + while (1) { if (jdata->mode == WD_CIPHER_CCM) { i = jid % MAX_POOL_LENTH; src = soft_pool->bds[i].src; dst = soft_pool->bds[i].dst; + if (optype) { EVP_CIPHER_CTX_ctrl(ctx, 0x11, sizeof(tag), tag); /* reset iv */ @@ -508,8 +568,7 @@ static int sec_soft_jobfunc(void *args) dst = soft_pool->bds[i].dst; if (optype) { EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, priv_iv); - EVP_CIPHER_CTX_ctrl(ctx, 0x11, - sizeof(faketag), tag); + EVP_CIPHER_CTX_ctrl(ctx, 0x11, sizeof(faketag), tag); EVP_DecryptUpdate(ctx, NULL, &outl, aad, sizeof(aad)); EVP_DecryptUpdate(ctx, dst, &outl, src, g_pktlen); EVP_DecryptFinal_ex(ctx, dst + outl, &outl); @@ -520,53 +579,105 @@ static int sec_soft_jobfunc(void *args) EVP_DecryptFinal_ex(ctx, dst + outl, &outl); } } - EVP_CIPHER_CTX_free(ctx); + count++; + if (get_run_state() == 0) + break; + } + EVP_CIPHER_CTX_free(ctx);
- break; - case DIGEST_TYPE: - if (!optype) { //normal mode - md_ctx = EVP_MD_CTX_new(); - if (!md_ctx) - return 0; + add_recv_data(count, g_pktlen); + + return 0; +} + +static int sec_soft_digest_jobfunc(void *args) +{ + jobs_data *jdata = (jobs_data *)args; + const EVP_MD *evp_md = jdata->evp_md; + u32 optype = jdata->optype; + u32 jid = jdata->jobid; + struct bd_pool *soft_pool; + u8 mac[EVP_MAX_MD_SIZE] = {0x00}; + EVP_MD_CTX *md_ctx; + HMAC_CTX *hm_ctx; + ASYNC_JOB *currjob; + u32 ssl_size = 0; + u8 *priv_key, *src; + u32 count = 0; + int i = 0; + + currjob = ASYNC_get_current_job(); + if (!currjob) { + SSL_TST_PRT("Error: not executing within a job\n"); + return 0; + } + + if (!evp_md) { + SSL_TST_PRT("Error: openssl not support!\n"); + return 0; + } + + if (jdata->td_id > g_thread_num) + return 0; + + soft_pool = &g_soft_pool.pool[jdata->td_id]; + priv_key = &g_soft_pool.key[jdata->td_id];
+ memset(priv_key, DEF_IVK_DATA, MAX_IVK_LENTH); + + if (!optype) { //normal mode + md_ctx = EVP_MD_CTX_new(); + if (!md_ctx) + return 0; + + while (1) { i = jid % MAX_POOL_LENTH; src = soft_pool->bds[i].src;
EVP_DigestInit_ex(md_ctx, evp_md, NULL); EVP_DigestUpdate(md_ctx, src, g_pktlen); EVP_DigestFinal_ex(md_ctx, mac, &ssl_size); - // EVP_Digest(src, g_pktlen, mac, &ssl_size, evp_md, NULL);
+ count++; + if (get_run_state() == 0) + break; + } EVP_MD_CTX_free(md_ctx); - } else { //hmac mode - hm_ctx = HMAC_CTX_new(); - if (!hm_ctx) - return 0; - + } else { //hmac mode + hm_ctx = HMAC_CTX_new(); + if (!hm_ctx) + return 0; + while (1) { i = jid % MAX_POOL_LENTH; src = soft_pool->bds[i].src;
HMAC_Init_ex(hm_ctx, priv_key, jdata->keysize, evp_md, NULL); HMAC_Update(hm_ctx, src, g_pktlen); HMAC_Final(hm_ctx, mac, &ssl_size); - // HMAC(evp_md, priv_key, jdata->keysize, src, g_pktlen, mac, &ssl_size);
- HMAC_CTX_free(hm_ctx); + count++; + if (get_run_state() == 0) + break; } - break; + HMAC_CTX_free(hm_ctx); } - return 0; }
static void *sec_soft_async_run(void *arg) { + typedef int (*sec_soft_run)(void *arg); + sec_soft_run sec_soft_jobfunc = NULL; soft_thread *pdata = (soft_thread *)arg; - ASYNC_WAIT_CTX *waitctx = NULL; - ASYNC_JOB *job = NULL; - int ret, jobret = 0; + jobs_args loop_jobs[MAX_JOBS_NUM]; + OSSL_ASYNC_FD waitfd = 0; jobs_data jobdata; - u32 count = 0; + fd_set waitfdset; + size_t numfds = 0; + int i, j, k, ret; + int jobret = 0; + u32 valid_jobs = 0; + u32 jobs_num;
jobdata.evp_cipher = pdata->evp_cipher; jobdata.evp_md = pdata->evp_md; @@ -575,31 +686,177 @@ static void *sec_soft_async_run(void *arg) jobdata.optype = pdata->optype; jobdata.subtype = pdata->subtype; jobdata.td_id = pdata->td_id; + jobdata.engine = pdata->engine;
- waitctx = ASYNC_WAIT_CTX_new(); - if (!waitctx) { - SSL_TST_PRT("Error: create ASYNC_WAIT_CTX failed\n"); + jobs_num = g_jobsnum; + if (jobs_num > MAX_JOBS_NUM) { + SSL_TST_PRT("Error: check async jobs num failed.\n"); return NULL; } + memset(loop_jobs, 0x0, sizeof(jobs_args) * MAX_JOBS_NUM);
- while (1) { - jobdata.jobid = count; - ret = ASYNC_start_job(&job, waitctx, &jobret, sec_soft_jobfunc, - (void *)&jobdata, sizeof(jobs_data)); + switch (pdata->subtype) { + case CIPHER_TYPE: + sec_soft_jobfunc = sec_soft_cipher_jobfunc; + break; + case AEAD_TYPE: + sec_soft_jobfunc = sec_soft_aead_jobfunc; + break; + case DIGEST_TYPE: + sec_soft_jobfunc = sec_soft_digest_jobfunc; + break; + } + + /* one thread for one job */ + for (i = 0; i < jobs_num; i++) { + loop_jobs[i].wait_ctx = ASYNC_WAIT_CTX_new(); + if (!loop_jobs[i].wait_ctx) { + SSL_TST_PRT("Error: create ASYNC_WAIT_CTX failed\n"); + goto async_error; + } + + jobdata.jobid = i; + ret = ASYNC_start_job(&loop_jobs[i].in_job, loop_jobs[i].wait_ctx, &jobret, + sec_soft_jobfunc, (void *)&jobdata, sizeof(jobs_data)); switch(ret) { case ASYNC_ERR: - SSL_TST_PRT("Error: start soft async job err. \n"); - goto exit_pause; + SSL_TST_PRT("Error: start soft async job err.\n"); + break; case ASYNC_NO_JOBS: - SSL_TST_PRT("Error: can't get soft async job from job pool. \n"); - goto exit_pause; + SSL_TST_PRT("Error: can't get soft async job from job pool.\n"); + break; case ASYNC_PAUSE: - SSL_TST_PRT("Info: job was paused \n"); + loop_jobs[i].job_valid = true; + valid_jobs++; break; case ASYNC_FINISH: break; default: - SSL_TST_PRT("Error: do soft async job err. \n"); + SSL_TST_PRT("Error: do soft async job err.\n"); + } + } + + j = valid_jobs; + while (j > 0) { + for (i = 0; i < jobs_num; i++) { + FD_ZERO(&waitfdset); + if (!loop_jobs[i].job_valid) + continue; + + /* Wait for the job to be woken */ + if (!ASYNC_WAIT_CTX_get_all_fds(loop_jobs[i].wait_ctx, NULL, &numfds) || + numfds > 1) { + SSL_TST_PRT("Error: unexpected number of fds.\n"); + continue; + } + ASYNC_WAIT_CTX_get_all_fds(loop_jobs[i].wait_ctx, &waitfd, &numfds); + FD_SET(waitfd, &waitfdset); + + ret = select(waitfd + 1, &waitfdset, NULL, NULL, NULL); + if (ret == -1) { + SSL_TST_PRT("Error: select soft async job error.\n"); + goto async_finish; + } else if (ret == 0 || (ret == -1 && errno == EINTR)) { + SSL_TST_PRT("Infor: select soft async job result continue.\n"); + continue; + } + + jobdata.jobid = i; + ret = ASYNC_start_job(&loop_jobs[i].in_job, loop_jobs[i].wait_ctx, &jobret, + sec_soft_jobfunc, (void *)&jobdata, sizeof(jobs_data)); + switch(ret) { + case ASYNC_ERR: + loop_jobs[i].job_valid = false; + j--; + SSL_TST_PRT("Error: restart soft async job err.\n"); + break; + case ASYNC_NO_JOBS: + SSL_TST_PRT("Error: can't get soft async job from job pool.\n"); + break; + case ASYNC_PAUSE: + break; + case ASYNC_FINISH: + loop_jobs[i].job_valid = false; + j--; + break; + default: + SSL_TST_PRT("Error: do soft async job err.\n"); + } + } + } + +async_finish: + i = jobs_num; +async_error: + for (k = 0; k < i; k++) + ASYNC_WAIT_CTX_free(loop_jobs[k].wait_ctx); + + add_send_complete(); + + return NULL; +} + +static void *sec_soft_cipher_sync(void *arg) +{ + soft_thread *pdata = (soft_thread *)arg; + const EVP_CIPHER *evp_cipher = pdata->evp_cipher; + u32 optype = pdata->optype; + struct bd_pool *soft_pool; + u8 *priv_iv, *priv_key; + EVP_CIPHER_CTX *ctx = NULL; + u32 count = 0; + u8 *src, *dst; + int ret, i = 0; + int outl = 0; + + if (!evp_cipher) { + SSL_TST_PRT("Error: openssl not support!\n"); + return NULL; + } + + if (pdata->td_id > g_thread_num) + return NULL; + + soft_pool = &g_soft_pool.pool[pdata->td_id]; + priv_iv = &g_soft_pool.iv[pdata->td_id]; + priv_key = &g_soft_pool.key[pdata->td_id]; + + memset(priv_iv, DEF_IVK_DATA, MAX_IVK_LENTH); + memset(priv_key, DEF_IVK_DATA, MAX_IVK_LENTH); + + ctx = EVP_CIPHER_CTX_new(); + if (!ctx) + return NULL; + + EVP_CIPHER_CTX_init(ctx); + + while (1) { + i = count % MAX_POOL_LENTH; + src = soft_pool->bds[i].src; + dst = soft_pool->bds[i].dst; + + if (optype) { + ret = EVP_DecryptInit_ex(ctx, evp_cipher, pdata->engine, priv_key, priv_iv); + if (ret != 1) + break; + + ret = EVP_DecryptUpdate(ctx, dst, &outl, src, g_pktlen); + if (ret != 1) + break; + + EVP_CIPHER_CTX_set_padding(ctx, EVP_PADDING_ZERO); + EVP_DecryptFinal_ex(ctx, dst, &outl); + } else { + ret = EVP_EncryptInit_ex(ctx, evp_cipher, pdata->engine, priv_key, priv_iv); + if (ret != 1) + break; + + ret = EVP_EncryptUpdate(ctx, dst, &outl, src, g_pktlen); + if (ret != 1) + break; + + EVP_CIPHER_CTX_set_padding(ctx, EVP_PADDING_ZERO); + EVP_EncryptFinal_ex(ctx, dst, &outl); }
count++; @@ -607,36 +864,31 @@ static void *sec_soft_async_run(void *arg) break; }
-exit_pause: - ASYNC_WAIT_CTX_free(waitctx); + EVP_CIPHER_CTX_cleanup(ctx); + EVP_CIPHER_CTX_free(ctx);
add_recv_data(count, g_pktlen);
return NULL; }
-static void *sec_soft_sync_run(void *arg) +static void *sec_soft_aead_sync(void *arg) { soft_thread *pdata = (soft_thread *)arg; const EVP_CIPHER *evp_cipher = pdata->evp_cipher; - const EVP_MD *evp_md = pdata->evp_md; u32 optype = pdata->optype; - u8 mac[EVP_MAX_MD_SIZE] = {0x00}; struct bd_pool *soft_pool; u8 *priv_iv, *priv_key; EVP_CIPHER_CTX *ctx = NULL; - EVP_MD_CTX *md_ctx = NULL; - HMAC_CTX *hm_ctx = NULL; u8 faketag[16] = {0xcc}; u8 aad[13] = {0xcc}; u8 tag[12] = {0}; - u32 ssl_size = 0; u32 count = 0; u8 *src, *dst; int ret, i = 0; int outl = 0;
- if (!evp_cipher && !evp_md) { + if (!evp_cipher) { SSL_TST_PRT("Error: openssl not support!\n"); return NULL; } @@ -651,14 +903,13 @@ static void *sec_soft_sync_run(void *arg) memset(priv_iv, DEF_IVK_DATA, MAX_IVK_LENTH); memset(priv_key, DEF_IVK_DATA, MAX_IVK_LENTH);
- switch(pdata->subtype) { - case CIPHER_TYPE: - ctx = EVP_CIPHER_CTX_new(); - if (!ctx) - return NULL; + ctx = EVP_CIPHER_CTX_new(); + if (!ctx) + return NULL;
- EVP_CIPHER_CTX_set_padding(ctx, 0); + EVP_CIPHER_CTX_set_padding(ctx, 0);
+ if (pdata->mode == WD_CIPHER_CCM) { while (1) { i = count % MAX_POOL_LENTH; src = soft_pool->bds[i].src; @@ -682,130 +933,168 @@ static void *sec_soft_sync_run(void *arg) if (get_run_state() == 0) break; } + } else { + while (1) { + i = count % MAX_POOL_LENTH; + src = soft_pool->bds[i].src; + dst = soft_pool->bds[i].dst;
- EVP_CIPHER_CTX_free(ctx); + (void)EVP_CipherInit_ex(ctx, evp_cipher, pdata->engine, priv_key, priv_iv, optype);
- break; - case AEAD_TYPE: - ctx = EVP_CIPHER_CTX_new(); - if (!ctx) + if (optype) { + EVP_DecryptInit_ex(ctx, NULL, pdata->engine, NULL, priv_iv); + EVP_CIPHER_CTX_ctrl(ctx, 0x11, sizeof(faketag), tag); + EVP_DecryptUpdate(ctx, NULL, &outl, aad, sizeof(aad)); + EVP_DecryptUpdate(ctx, dst, &outl, src, g_pktlen); + ret = EVP_DecryptFinal_ex(ctx, dst + outl, &outl); + } else { + EVP_DecryptInit_ex(ctx, NULL, pdata->engine, NULL, priv_iv); + EVP_DecryptUpdate(ctx, NULL, &outl, aad, sizeof(aad)); + EVP_EncryptUpdate(ctx, NULL, &outl, NULL, g_pktlen); + ret = EVP_DecryptFinal_ex(ctx, dst + outl, &outl); + } + if (ret != 1) + EVP_CipherInit_ex(ctx, evp_cipher, pdata->engine, priv_key, priv_iv, optype); + + count++; + if (get_run_state() == 0) + break; + } + } + EVP_CIPHER_CTX_free(ctx); + + add_recv_data(count, g_pktlen); + + return NULL; +} + +static void *sec_soft_digest_sync(void *arg) +{ + soft_thread *pdata = (soft_thread *)arg; + const EVP_CIPHER *evp_cipher = pdata->evp_cipher; + const EVP_MD *evp_md = pdata->evp_md; + u32 optype = pdata->optype; + u8 mac[EVP_MAX_MD_SIZE] = {0x00}; + struct bd_pool *soft_pool; + EVP_MD_CTX *md_ctx = NULL; + HMAC_CTX *hm_ctx = NULL; + u8 *priv_key, *src; + u32 ssl_size = 0; + u32 count = 0; + int i = 0; + + if (!evp_cipher && !evp_md) { + SSL_TST_PRT("Error: openssl not support!\n"); + return NULL; + } + + if (pdata->td_id > g_thread_num) + return NULL; + + soft_pool = &g_soft_pool.pool[pdata->td_id]; + priv_key = &g_soft_pool.key[pdata->td_id]; + memset(priv_key, DEF_IVK_DATA, MAX_IVK_LENTH); + + if (!optype) { + md_ctx = EVP_MD_CTX_new(); + if (!md_ctx) return NULL;
- EVP_CIPHER_CTX_set_padding(ctx, 0); - - if (pdata->mode == WD_CIPHER_CCM) { - while (1) { - i = count % MAX_POOL_LENTH; - src = soft_pool->bds[i].src; - dst = soft_pool->bds[i].dst; - - (void)EVP_CipherInit_ex(ctx, evp_cipher, NULL, priv_key, priv_iv, optype); - - if (optype) { - EVP_CIPHER_CTX_ctrl(ctx, 0x11, sizeof(tag), tag); - /* reset iv */ - EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, priv_iv); - ret = EVP_DecryptUpdate(ctx, dst, &outl, src, g_pktlen); - } else { - /* restore iv length field */ - EVP_EncryptUpdate(ctx, NULL, &outl, NULL, g_pktlen); - /* counter is reset on every update */ - ret = EVP_EncryptUpdate(ctx, dst, &outl, src, g_pktlen); - } - if (ret != 1) - EVP_CipherInit_ex(ctx, evp_cipher, NULL, priv_key, priv_iv, optype); - - if (optype) - EVP_DecryptFinal_ex(ctx, dst, &outl); - else - EVP_EncryptFinal_ex(ctx, dst, &outl); - - count++; - if (get_run_state() == 0) - break; - } - } else { - while (1) { - i = count % MAX_POOL_LENTH; - src = soft_pool->bds[i].src; - dst = soft_pool->bds[i].dst; - - (void)EVP_CipherInit_ex(ctx, evp_cipher, NULL, priv_key, priv_iv, optype); - - if (optype) { - EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, priv_iv); - EVP_CIPHER_CTX_ctrl(ctx, 0x11, - sizeof(faketag), tag); - EVP_DecryptUpdate(ctx, NULL, &outl, aad, sizeof(aad)); - EVP_DecryptUpdate(ctx, dst, &outl, src, g_pktlen); - ret = EVP_DecryptFinal_ex(ctx, dst + outl, &outl); - } else { - EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, priv_iv); - EVP_DecryptUpdate(ctx, NULL, &outl, aad, sizeof(aad)); - EVP_EncryptUpdate(ctx, NULL, &outl, NULL, g_pktlen); - ret = EVP_DecryptFinal_ex(ctx, dst + outl, &outl); - } - if (ret != 1) - EVP_CipherInit_ex(ctx, evp_cipher, NULL, priv_key, priv_iv, optype); - - count++; - if (get_run_state() == 0) - break; - } + while (1) { + i = count % MAX_POOL_LENTH; + src = soft_pool->bds[i].src; + + EVP_DigestInit_ex(md_ctx, evp_md, NULL); + EVP_DigestUpdate(md_ctx, src, g_pktlen); + EVP_DigestFinal_ex(md_ctx, mac, &ssl_size); + + count++; + if (get_run_state() == 0) + break; } - EVP_CIPHER_CTX_free(ctx); + EVP_MD_CTX_free(md_ctx); + } else { //hmac mode + hm_ctx = HMAC_CTX_new(); + if (!hm_ctx) + return NULL; + + while (1) { + i = count % MAX_POOL_LENTH; + src = soft_pool->bds[i].src; + + HMAC_Init_ex(hm_ctx, priv_key, pdata->keysize, evp_md, NULL); + HMAC_Update(hm_ctx, src, g_pktlen); + HMAC_Final(hm_ctx, mac, &ssl_size); + + count++; + if (get_run_state() == 0) + break; + } + HMAC_CTX_free(hm_ctx); + } + add_recv_data(count, g_pktlen); + + return NULL; +} + +static int uadk_engine_register(soft_thread *options, char *engine_name) +{ + if (!options->engine_flag) + return 0; + + ERR_load_ENGINE_strings(); + OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_DYNAMIC, NULL);
+ options->engine = ENGINE_by_id(engine_name); + if (!options->engine) { + SSL_TST_PRT("setup uadk engine failed!\n"); + return -EINVAL; + } + + ENGINE_init(options->engine); + switch(options->subtype) { + case CIPHER_TYPE: + ENGINE_register_ciphers(options->engine); break; + case AEAD_TYPE: + SSL_TST_PRT("Openssl not support AEAD ALG!\n"); + return -EINVAL; case DIGEST_TYPE: - if (!optype) { //normal mode - md_ctx = EVP_MD_CTX_new(); - if (!md_ctx) - return NULL; - - while (1) { - i = count % MAX_POOL_LENTH; - src = soft_pool->bds[i].src; - - EVP_DigestInit_ex(md_ctx, evp_md, NULL); - EVP_DigestUpdate(md_ctx, src, g_pktlen); - EVP_DigestFinal_ex(md_ctx, mac, &ssl_size); - // EVP_Digest(src, g_pktlen, mac, &ssl_size, evp_md, NULL); - - count++; - if (get_run_state() == 0) - break; - } - EVP_MD_CTX_free(md_ctx); - } else { //hmac mode - hm_ctx = HMAC_CTX_new(); - if (!hm_ctx) - return NULL; - - while (1) { - i = count % MAX_POOL_LENTH; - src = soft_pool->bds[i].src; - - HMAC_Init_ex(hm_ctx, priv_key, pdata->keysize, evp_md, NULL); - HMAC_Update(hm_ctx, src, g_pktlen); - HMAC_Final(hm_ctx, mac, &ssl_size); - // HMAC(evp_md, priv_key, pdata->keysize, src, g_pktlen, mac, &ssl_size); - - count++; - if (get_run_state() == 0) - break; - } - HMAC_CTX_free(hm_ctx); - } + ENGINE_register_digests(options->engine); break; + default: + return -EINVAL; } + ENGINE_free(options->engine);
- add_recv_data(count, g_pktlen); + return 0; +}
- return NULL; +static void uadk_engine_unregister(soft_thread *options) +{ + if (!options->engine_flag) + return; + + switch(options->subtype) { + case CIPHER_TYPE: + ENGINE_unregister_ciphers(options->engine); + break; + case AEAD_TYPE: + // ENGINE_unregister_ciphers(e); //openssl not support aead + break; + case DIGEST_TYPE: + ENGINE_unregister_digests(options->engine); + break; + default: + return; + } + ENGINE_free(options->engine); }
int sec_soft_sync_threads(struct acc_option *options) { + typedef void *(*sec_sync_run)(void *arg); + sec_sync_run soft_sec_sync_run = NULL; soft_thread threads_args[THREADS_NUM]; soft_thread threads_option; pthread_t tdid[THREADS_NUM]; @@ -816,6 +1105,24 @@ int sec_soft_sync_threads(struct acc_option *options) if (ret) return ret;
+ threads_option.engine_flag = options->engine_flag; + threads_option.sync_mode = options->syncmode; + ret = uadk_engine_register(&threads_option, options->engine); + if (ret) + return ret; + + switch (options->subtype) { + case CIPHER_TYPE: + soft_sec_sync_run = sec_soft_cipher_sync; + break; + case AEAD_TYPE: + soft_sec_sync_run = sec_soft_aead_sync; + break; + case DIGEST_TYPE: + soft_sec_sync_run = sec_soft_digest_sync; + break; + } + for (i = 0; i < g_thread_num; i++) { threads_args[i].evp_cipher = threads_option.evp_cipher; threads_args[i].evp_md = threads_option.evp_md; @@ -825,7 +1132,7 @@ int sec_soft_sync_threads(struct acc_option *options) threads_args[i].optype = threads_option.optype; threads_args[i].td_id = i; threads_args[i].engine_flag = options->engine_flag; - ret = pthread_create(&tdid[i], NULL, sec_soft_sync_run, &threads_args[i]); + ret = pthread_create(&tdid[i], NULL, soft_sec_sync_run, &threads_args[i]); if (ret) { SSL_TST_PRT("Create sync thread fail!\n"); goto sync_error; @@ -842,8 +1149,8 @@ int sec_soft_sync_threads(struct acc_option *options) }
sync_error: + uadk_engine_unregister(&threads_option); return ret; - }
int sec_soft_async_threads(struct acc_option *options) @@ -858,6 +1165,12 @@ int sec_soft_async_threads(struct acc_option *options) if (ret) return ret;
+ threads_option.engine_flag = options->engine_flag; + threads_option.sync_mode = options->syncmode; + ret = uadk_engine_register(&threads_option, options->engine); + if (ret) + return ret; + for (i = 0; i < g_thread_num; i++) { threads_args[i].evp_cipher = threads_option.evp_cipher; threads_args[i].evp_md = threads_option.evp_md; @@ -884,74 +1197,10 @@ int sec_soft_async_threads(struct acc_option *options) }
async_error: + uadk_engine_unregister(&threads_option); return ret; }
-static int uadk_engine_register(struct acc_option *options) -{ - ENGINE *e = NULL; - - if (!options->engine_flag) - return 0; - - ERR_load_ENGINE_strings(); - OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_DYNAMIC, NULL); - - e = ENGINE_by_id(options->engine); - if (!e) { - SSL_TST_PRT("setup uadk engine failed!\n"); - return -EINVAL; - } - - ENGINE_init(e); - switch(options->subtype) { - case CIPHER_TYPE: - ENGINE_register_ciphers(e); - break; - case AEAD_TYPE: - SSL_TST_PRT("Openssl not support AEAD ALG!\n"); - return -EINVAL; - case DIGEST_TYPE: - ENGINE_register_digests(e); - break; - default: - return -EINVAL; - } - ENGINE_free(e); - - return 0; -} - -static void uadk_engine_unregister(struct acc_option *options) -{ - ENGINE *e = NULL; - - if (!options->engine_flag) - return; - - e = ENGINE_by_id(options->engine); - if (!e) { - SSL_TST_PRT("Error: not find uadk engine \n"); - return; - } - - ENGINE_init(e); - switch(options->subtype) { - case CIPHER_TYPE: - ENGINE_unregister_ciphers(e); - break; - case AEAD_TYPE: - // ENGINE_unregister_ciphers(e); //openssl not support aead - break; - case DIGEST_TYPE: - ENGINE_unregister_digests(e); - break; - default: - return; - } - ENGINE_free(e); -} - int sec_soft_benchmark(struct acc_option *options) { u32 ptime; @@ -969,10 +1218,6 @@ int sec_soft_benchmark(struct acc_option *options) if (ret) return ret;
- ret = uadk_engine_register(options); - if (ret) - return ret; - get_pid_cpu_time(&ptime); time_start(options->times); if (options->syncmode) @@ -983,7 +1228,6 @@ int sec_soft_benchmark(struct acc_option *options) if (ret) return ret;
- uadk_engine_unregister(options); free_soft_bd_pool();
return 0;