From: Junchong Pan <panjunchong@h-partners.com> Signed-off-by: Junchong Pan <panjunchong@h-partners.com> --- Makefile.am | 2 +- test/hisi_hpre_test/Makefile.am | 1 + uadk_tool/Makefile.am | 14 +- uadk_tool/benchmark/sec_soft3_benchmark.c | 1431 +++++++++++++++++++++ uadk_tool/benchmark/sec_soft3_benchmark.h | 8 + uadk_tool/benchmark/uadk_benchmark.c | 18 +- uadk_tool/test/comp_lib.c | 21 +- uadk_tool/test/comp_lib.h | 10 + uadk_tool/test/comp_main.c | 8 +- uadk_tool/test/uadk_test.c | 4 +- 10 files changed, 1499 insertions(+), 18 deletions(-) create mode 100644 uadk_tool/benchmark/sec_soft3_benchmark.c create mode 100644 uadk_tool/benchmark/sec_soft3_benchmark.h diff --git a/Makefile.am b/Makefile.am index 84c97e9..fe619f3 100644 --- a/Makefile.am +++ b/Makefile.am @@ -3,7 +3,7 @@ AUTOMAKE_OPTIONS = foreign subdir-objects AM_CFLAGS=-std=gnu11 -Wall -Werror -Wextra -Wno-unused-parameter -Wfloat-equal \ -fno-common -fno-strict-aliasing -I$(top_srcdir)/include AM_CFLAGS+=-fPIC -fPIE -pie -fstack-protector-strong -D_FORTIFY_SOURCE=2 \ - -O2 -ftrapv -Wl,-z,relro,-z,now -Wl,-s + -O0 -ftrapv -Wl,-z,relro,-z,now -g AM_CFLAGS += -Wall -Wuninitialized -Wno-error -Wno-error=format -Wundef \ -Wunused -Wdate-time -Wfloat-equal -Wshadow -Wvla -Wdisabled-optimization \ -Wempty-body -Wignored-qualifiers -Wimplicit-fallthrough=3 -Wtype-limits \ diff --git a/test/hisi_hpre_test/Makefile.am b/test/hisi_hpre_test/Makefile.am index 6cc8a37..e32ac95 100644 --- a/test/hisi_hpre_test/Makefile.am +++ b/test/hisi_hpre_test/Makefile.am @@ -14,5 +14,6 @@ endif test_hisi_hpre_LDFLAGS=-Wl,-rpath,'/usr/local/lib' if WITH_OPENSSL_DIR AM_CFLAGS+= -DWITH_OPENSSL_DIR +test_hisi_hpre_LDADD+= $(with_openssl_dir)/libcrypto.so.3 test_hisi_hpre_LDADD+= $(with_openssl_dir)/libcrypto_wd.so endif diff --git a/uadk_tool/Makefile.am b/uadk_tool/Makefile.am index 6fd9d95..ee7dea4 100644 --- a/uadk_tool/Makefile.am +++ b/uadk_tool/Makefile.am @@ -1,9 +1,9 @@ ACLOCAL_AMFLAGS = -I m4 -I./include AUTOMAKE_OPTIONS = foreign subdir-objects -AM_CFLAGS=-Wall -Werror -fno-strict-aliasing -I$(top_srcdir) -I$(top_srcdir)/benchmark/include \ +AM_CFLAGS=-Wall -fno-strict-aliasing -I$(top_srcdir) -I$(top_srcdir)/benchmark/include \ -pthread AM_CFLAGS += -fPIC -fPIE -pie -fstack-protector-strong -D_FORTIFY_SOURCE=2 \ --O2 -ftrapv -Wl,-z,now -Wl,-s +-O0 -ftrapv -Wl,-z,now -g #AUTOMAKE_OPTIONS = subdir-objects @@ -50,14 +50,18 @@ endif if WITH_OPENSSL_DIR AM_CFLAGS+= -DWITH_OPENSSL_DIR -I$(with_openssl_dir)/include +AM_CFLAGS+= -DHAVE_OPENSSL3=$(shell echo '#include <openssl/opensslv.h>' | \ + $(CC) -E -I$(with_openssl_dir)/include - | grep 'OPENSSL_VERSION_MAJOR' | sed 's/#define OPENSSL_VERSION_MAJOR //') + +uadk_tool_SOURCES+=test/comp_main.c test/comp_main.h test/comp_lib.c test/comp_lib.h +# uadk_tool_SOURCES+=benchmark/sec_soft_benchmark.c benchmark/sec_soft_benchmark.h +uadk_tool_SOURCES+=benchmark/sec_soft3_benchmark.c benchmark/sec_soft3_benchmark.h -uadk_tool_SOURCES+=benchmark/sec_soft_benchmark.c benchmark/sec_soft_benchmark.h \ - test/comp_main.c test/comp_main.h test/comp_lib.c test/comp_lib.h if WD_STATIC_DRV uadk_tool_LDADD+= $(with_openssl_dir)/libcrypto.a else -uadk_tool_LDADD+= $(with_openssl_dir)/libcrypto.so.1.1 +uadk_tool_LDADD+= $(with_openssl_dir)/libcrypto.so.3 endif endif diff --git a/uadk_tool/benchmark/sec_soft3_benchmark.c b/uadk_tool/benchmark/sec_soft3_benchmark.c new file mode 100644 index 0000000..8d40074 --- /dev/null +++ b/uadk_tool/benchmark/sec_soft3_benchmark.c @@ -0,0 +1,1431 @@ +/* SPDX-License-Identifier: Apache-2.0 */ + +#define _GNU_SOURCE +#include "openssl/async.h" +#include "openssl/bn.h" +#include "openssl/crypto.h" +#include "openssl/engine.h" +#include "openssl/err.h" +#include "openssl/evp.h" +#include "openssl/ossl_typ.h" +#include "openssl/objects.h" +#include "openssl/ssl.h" +#include "openssl/provider.h" +#include "openssl/x509.h" +#include "openssl/param_build.h" + +#include "include/wd_cipher.h" +#include "include/wd_digest.h" +#include "sec_soft3_benchmark.h" + +#define SSL_TST_PRT printf +#define ENV_STRING_LEN 256 + +struct soft_bd { + u8 *src; + u8 *dst; +}; + +struct bd_pool { + struct soft_bd *bds; +}; + +struct thread_pool { + struct bd_pool *pool; + u8 *iv; + u8 *key; +} g_soft_pool; + +typedef struct soft_thread_res { + const EVP_CIPHER *evp_cipher; + const EVP_MD *evp_md; + OSSL_PROVIDER *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; + OSSL_PROVIDER *engine; + u32 subtype; + u32 mode; + u32 keysize; + u32 optype; + u32 td_id; + u32 jobid; +} 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_jobsnum; +static unsigned int g_pktlen; + +static int init_soft_bd_pool(void) +{ + unsigned long step; + int fill_size; + int i, j; + + // make the block not align to 4K + step = sizeof(char) * g_pktlen * 2; + if (g_pktlen > MAX_IVK_LENTH) + fill_size = MAX_IVK_LENTH; + else + fill_size = g_pktlen; + + g_soft_pool.iv = malloc(g_thread_num * MAX_IVK_LENTH * sizeof(char)); + g_soft_pool.key = malloc(g_thread_num * MAX_IVK_LENTH * sizeof(char)); + + g_soft_pool.pool = malloc(g_thread_num * sizeof(struct bd_pool)); + if (!g_soft_pool.pool) { + SSL_TST_PRT("init openssl pool alloc thread failed!\n"); + return -ENOMEM; + } else { + for (i = 0; i < g_thread_num; i++) { + g_soft_pool.pool[i].bds = malloc(MAX_POOL_LENTH * + sizeof(struct soft_bd)); + if (!g_soft_pool.pool[i].bds) { + SSL_TST_PRT("init openssl bds alloc failed!\n"); + goto malloc_error1; + } + for (j = 0; j < MAX_POOL_LENTH; j++) { + g_soft_pool.pool[i].bds[j].src = malloc(step); + if (!g_soft_pool.pool[i].bds[j].src) + goto malloc_error2; + g_soft_pool.pool[i].bds[j].dst = malloc(step); + if (!g_soft_pool.pool[i].bds[j].dst) + goto malloc_error3; + + get_rand_data(g_soft_pool.pool[i].bds[j].src, fill_size); + } + } + } + + return 0; + +malloc_error3: + free(g_soft_pool.pool[i].bds[j].src); +malloc_error2: + for (j--; j >= 0; j--) { + free(g_soft_pool.pool[i].bds[j].src); + free(g_soft_pool.pool[i].bds[j].dst); + } +malloc_error1: + for (i--; i >= 0; i--) { + for (j = 0; j < MAX_POOL_LENTH; j++) { + free(g_soft_pool.pool[i].bds[j].src); + free(g_soft_pool.pool[i].bds[j].dst); + } + free(g_soft_pool.pool[i].bds); + g_soft_pool.pool[i].bds = NULL; + } + free(g_soft_pool.pool); + g_soft_pool.pool = NULL; + + free(g_soft_pool.iv); + free(g_soft_pool.key); + + SSL_TST_PRT("init openssl bd pool alloc failed!\n"); + return -ENOMEM; +} + +static void free_soft_bd_pool(void) +{ + int i, j; + + for (i = 0; i < g_thread_num; i++) { + if (g_soft_pool.pool[i].bds) { + for (j = 0; j < MAX_POOL_LENTH; j++) { + free(g_soft_pool.pool[i].bds[j].src); + free(g_soft_pool.pool[i].bds[j].dst); + } + } + free(g_soft_pool.pool[i].bds); + g_soft_pool.pool[i].bds = NULL; + } + free(g_soft_pool.pool); + g_soft_pool.pool = NULL; + + free(g_soft_pool.iv); + free(g_soft_pool.key); +} + +/*-------------------------------openssl benchmark main code-------------------------------------*/ +static int sec_soft_param_parse(soft_thread *tddata, struct acc_option *options) +{ + u32 algtype = options->algtype; + u32 optype = options->optype; + u8 keysize = 0; + u8 mode; + + tddata->evp_cipher = NULL; + tddata->evp_md = NULL; + + switch(algtype) { + case AES_128_ECB: + keysize = 16; + mode = WD_CIPHER_ECB; + tddata->evp_cipher = EVP_aes_128_ecb(); + break; + case AES_192_ECB: + keysize = 24; + mode = WD_CIPHER_ECB; + tddata->evp_cipher = EVP_aes_192_ecb(); + break; + case AES_256_ECB: + keysize = 32; + mode = WD_CIPHER_ECB; + tddata->evp_cipher = EVP_aes_256_ecb(); + break; + case AES_128_CBC: + keysize = 16; + mode = WD_CIPHER_CBC; + tddata->evp_cipher = EVP_aes_128_cbc(); + break; + case AES_192_CBC: + keysize = 24; + mode = WD_CIPHER_CBC; + tddata->evp_cipher = EVP_aes_192_cbc(); + break; + case AES_256_CBC: + keysize = 32; + mode = WD_CIPHER_CBC; + tddata->evp_cipher = EVP_aes_256_cbc(); + break; + case AES_128_CTR: + keysize = 16; + mode = WD_CIPHER_CTR; + tddata->evp_cipher = EVP_aes_128_ctr(); + break; + case AES_192_CTR: + keysize = 24; + mode = WD_CIPHER_CTR; + tddata->evp_cipher = EVP_aes_192_ctr(); + break; + case AES_256_CTR: + keysize = 32; + mode = WD_CIPHER_CTR; + tddata->evp_cipher = EVP_aes_256_ctr(); + break; + case AES_128_OFB: + keysize = 16; + mode = WD_CIPHER_OFB; + tddata->evp_cipher = EVP_aes_128_ofb(); + break; + case AES_192_OFB: + keysize = 24; + mode = WD_CIPHER_OFB; + tddata->evp_cipher = EVP_aes_192_ofb(); + break; + case AES_256_OFB: + keysize = 32; + mode = WD_CIPHER_OFB; + tddata->evp_cipher = EVP_aes_256_ofb(); + break; + case AES_128_CFB: + keysize = 16; + mode = WD_CIPHER_CFB; + tddata->evp_cipher = EVP_aes_128_cfb(); + break; + case AES_192_CFB: + keysize = 24; + mode = WD_CIPHER_CFB; + tddata->evp_cipher = EVP_aes_192_cfb(); + break; + case AES_256_CFB: + keysize = 32; + mode = WD_CIPHER_CFB; + tddata->evp_cipher = EVP_aes_256_cfb(); + break; + case AES_256_XTS: + keysize = 32; + mode = WD_CIPHER_XTS; + tddata->evp_cipher = EVP_aes_128_xts(); + break; + case AES_512_XTS: + keysize = 64; + mode = WD_CIPHER_XTS; + tddata->evp_cipher = EVP_aes_256_xts(); + break; + case DES3_128_ECB: + keysize = 16; + mode = WD_CIPHER_ECB; + tddata->evp_cipher = EVP_des_ede_ecb(); + break; + case DES3_192_ECB: + keysize = 24; + mode = WD_CIPHER_ECB; + tddata->evp_cipher = EVP_des_ede3_ecb(); + break; + case DES3_128_CBC: + keysize = 16; + mode = WD_CIPHER_CBC; + tddata->evp_cipher = EVP_des_ede_cbc(); + break; + case DES3_192_CBC: + keysize = 24; + mode = WD_CIPHER_CBC; + tddata->evp_cipher = EVP_des_ede3_cbc(); + break; +#ifndef OPENSSL_NO_SM4 + case SM4_128_ECB: + keysize = 16; + mode = WD_CIPHER_ECB; + tddata->evp_cipher = EVP_sm4_ecb(); + break; + case SM4_128_CBC: + keysize = 16; + mode = WD_CIPHER_CBC; + tddata->evp_cipher = EVP_sm4_cbc(); + break; + case SM4_128_CTR: + keysize = 16; + mode = WD_CIPHER_CTR; + tddata->evp_cipher = EVP_sm4_ctr(); + break; + case SM4_128_OFB: + keysize = 16; + mode = WD_CIPHER_OFB; + tddata->evp_cipher = EVP_sm4_ofb(); + break; + case SM4_128_CFB: + keysize = 16; + mode = WD_CIPHER_CFB; + tddata->evp_cipher = EVP_sm4_cfb128(); + break; + case SM4_128_XTS: + keysize = 16; + mode = WD_CIPHER_XTS; + break; +#endif + case AES_128_CCM: + keysize = 16; + mode = WD_CIPHER_CCM; + tddata->evp_cipher = EVP_aes_128_ccm(); + break; + case AES_192_CCM: + keysize = 24; + mode = WD_CIPHER_CCM; + tddata->evp_cipher = EVP_aes_192_ccm(); + break; + case AES_256_CCM: + keysize = 32; + mode = WD_CIPHER_CCM; + tddata->evp_cipher = EVP_aes_256_ccm(); + break; + case AES_128_GCM: + keysize = 16; + mode = WD_CIPHER_GCM; + tddata->evp_cipher = EVP_aes_128_gcm(); + break; + case AES_192_GCM: + keysize = 24; + mode = WD_CIPHER_GCM; + tddata->evp_cipher = EVP_aes_192_gcm(); + break; + case AES_256_GCM: + keysize = 32; + mode = WD_CIPHER_GCM; + tddata->evp_cipher = EVP_aes_256_gcm(); + break; + case AES_128_CBC_SHA256_HMAC: + keysize = 16; + mode = WD_CIPHER_CBC; + tddata->evp_cipher = EVP_aes_128_cbc(); + break; + case AES_192_CBC_SHA256_HMAC: + keysize = 24; + mode = WD_CIPHER_CBC; + tddata->evp_cipher = EVP_aes_192_cbc(); + break; + case AES_256_CBC_SHA256_HMAC: + keysize = 32; + mode = WD_CIPHER_CBC; + tddata->evp_cipher = EVP_aes_256_cbc(); + break; +#ifndef OPENSSL_NO_SM4 + case SM4_128_CCM: + keysize = 16; + mode = WD_CIPHER_CCM; + break; + case SM4_128_GCM: + keysize = 16; + mode = WD_CIPHER_GCM; + break; +#endif + case SM3_ALG: // digest mode is optype + keysize = 4; + mode = optype; + tddata->evp_md = EVP_sm3(); + break; + case MD5_ALG: + keysize = 4; + mode = optype; + tddata->evp_md = EVP_md5(); + break; + case SHA1_ALG: + keysize = 4; + mode = optype; + tddata->evp_md = EVP_sha1(); + break; + case SHA256_ALG: + keysize = 4; + mode = optype; + tddata->evp_md = EVP_sha256(); + break; + case SHA224_ALG: + keysize = 4; + mode = optype; + tddata->evp_md = EVP_sha224(); + break; + case SHA384_ALG: + keysize = 4; + mode = optype; + tddata->evp_md = EVP_sha384(); + break; + case SHA512_ALG: + keysize = 4; + mode = optype; + tddata->evp_md = EVP_sha512(); + break; + case SHA512_224: + keysize = 4; + mode = optype; + tddata->evp_md = EVP_sha512_224(); + break; + case SHA512_256: + keysize = 4; + mode = optype; + tddata->evp_md = EVP_sha512_256(); + break; + default: + SSL_TST_PRT("Fail to set sec alg\n"); + return -EINVAL; + } + + tddata->mode = mode; + tddata->keysize = keysize; + tddata->optype = options->optype; + tddata->subtype = options->subtype; + + return 0; +} + +static int sec_soft_cipher_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; + EVP_CIPHER_CTX *ctx = NULL; + ASYNC_JOB *currjob; + 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) { + SSL_TST_PRT("Error: thread id %d out of range (max %d)\n", 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) { + SSL_TST_PRT("Error: EVP_CIPHER_CTX_new failed\n"); + return 0; + } + + OSSL_PARAM params[4]; + size_t iv_len = EVP_CIPHER_get_iv_length(evp_cipher); + + params[0] = OSSL_PARAM_construct_octet_string("key", priv_key, MAX_IVK_LENTH); + params[1] = OSSL_PARAM_construct_octet_string("iv", priv_iv, iv_len); + params[2] = OSSL_PARAM_construct_int("padding", 0); + params[3] = OSSL_PARAM_construct_end(); + + while (1) { + i = jid % MAX_POOL_LENTH; + src = soft_pool->bds[i].src; + dst = soft_pool->bds[i].dst; + + if (optype) { + EVP_DecryptInit_ex2(ctx, evp_cipher, priv_key, priv_iv, NULL); + EVP_DecryptUpdate(ctx, dst, &outl, src, g_pktlen); + EVP_DecryptFinal_ex(ctx, dst, &outl); + } else { + EVP_EncryptInit_ex2(ctx, evp_cipher, priv_key, priv_iv, NULL); + EVP_EncryptUpdate(ctx, dst, &outl, src, g_pktlen); + EVP_EncryptFinal_ex(ctx, dst, &outl); + } + + ret = EVP_CIPHER_CTX_set_params(ctx, params); + if (ret != 1) { + SSL_TST_PRT("Error: EVP_CIPHER_CTX_set_params failed\n"); + break; + } + + count++; + if (get_run_state() == 0) + break; + } + + EVP_CIPHER_CTX_free(ctx); + + 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; + EVP_CIPHER_CTX *ctx = NULL; + ASYNC_JOB *currjob; + u8 aad[13] = {0xcc}; + u8 in_tag[12] = {0}; + u8 out_tag[EVP_MAX_IV_LENGTH] = {0}; + u32 count = 0; + u8 *src, *dst; + size_t taglen = 0; + + 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) { + SSL_TST_PRT("Error: thread id %d out of range (max %d)\n", 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) { + SSL_TST_PRT("Error: EVP_CIPHER_CTX_new failed\n"); + return 0; + } + + while (1) { + i = jid % MAX_POOL_LENTH; + src = soft_pool->bds[i].src; + dst = soft_pool->bds[i].dst; + + OSSL_PARAM core_params[5]; + int iv_len = EVP_CIPHER_get_iv_length(evp_cipher); + size_t key_len = MAX_IVK_LENTH; + + + memset(core_params, 0, sizeof(core_params)); + + core_params[0] = OSSL_PARAM_construct_octet_string("key", priv_key, key_len); + core_params[1] = OSSL_PARAM_construct_octet_string("iv", priv_iv, iv_len); + + core_params[2] = OSSL_PARAM_construct_int("padding", 0); + core_params[3] = OSSL_PARAM_construct_end(); + + if (optype) { + EVP_DecryptInit_ex2(ctx, evp_cipher, priv_key, priv_iv, core_params); + EVP_DecryptUpdate(ctx, dst, &outl, src, g_pktlen); + EVP_DecryptFinal_ex(ctx, dst + outl, &outl); + } else { + EVP_EncryptInit_ex2(ctx, evp_cipher, priv_key, priv_iv, core_params); + EVP_EncryptUpdate(ctx, dst, &outl, src, g_pktlen); + EVP_EncryptFinal_ex(ctx, dst + outl, &outl); + } + + if (jdata->mode == WD_CIPHER_CCM) { + + taglen = 12; + OSSL_PARAM ccm_specific_params[3]; + ccm_specific_params[0] = OSSL_PARAM_construct_size_t("taglen", &taglen); + + int ccm_l = (iv_len - 1) - 1; + ccm_specific_params[1] = OSSL_PARAM_construct_int("L", &ccm_l); + ccm_specific_params[2] = OSSL_PARAM_construct_end(); + + ret = EVP_CIPHER_CTX_set_params(ctx, ccm_specific_params); + if (ret != 1) { + SSL_TST_PRT("Error: CCM EVP_CIPHER_CTX_set_params failed\n"); + break; + } + } else { + + taglen = 16; + OSSL_PARAM gcm_specific_params[3]; + gcm_specific_params[0] = OSSL_PARAM_construct_size_t("taglen", &taglen); + + if (optype) { + gcm_specific_params[1] = OSSL_PARAM_construct_octet_string("tag", in_tag, sizeof(in_tag)); + } else { + gcm_specific_params[1] = OSSL_PARAM_construct_octet_string("tag", out_tag, 0); + } + gcm_specific_params[2] = OSSL_PARAM_construct_end(); + + ret = EVP_CIPHER_CTX_set_params(ctx, gcm_specific_params); + if (ret != 1) { + SSL_TST_PRT("Error: GCM EVP_CIPHER_CTX_set_params failed\n"); + break; + } + + OSSL_PARAM aad_params[] = { + OSSL_PARAM_construct_octet_string("aad", aad, sizeof(aad)), + OSSL_PARAM_construct_end() + }; + ret = EVP_CIPHER_CTX_set_params(ctx, aad_params); + if (ret != 1) { + SSL_TST_PRT("Error: GCM EVP_CIPHER_CTX_set_params(aad) failed\n"); + break; + } + } + + if (!optype && jdata->mode != WD_CIPHER_CCM) { + OSSL_PARAM get_tag_params[] = { + OSSL_PARAM_construct_octet_string("tag", out_tag, sizeof(out_tag)), + OSSL_PARAM_construct_end() + }; + (void)EVP_CIPHER_CTX_get_params(ctx, get_tag_params); + } + + count++; + if (get_run_state() == 0) + break; + } + + EVP_CIPHER_CTX_free(ctx); + 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; + EVP_MAC *f_mac = NULL; + EVP_MAC_CTX *m_ctx = NULL; + ASYNC_JOB *currjob; + u32 ssl_size = 0; + u8 *priv_key, *src; + u32 count = 0; + size_t required_len = 0; + int i; + + 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 evp_md not support!\n"); + return 0; + } + + if (jdata->td_id > g_thread_num) { + SSL_TST_PRT("Error: thread id out of range\n"); + 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) { + md_ctx = EVP_MD_CTX_new(); + if (!md_ctx) { + SSL_TST_PRT("Error: EVP_MD_CTX_new failed\n"); + return 0; + } + + OSSL_PARAM params[3]; + params[0] = OSSL_PARAM_construct_utf8_string("digest", (char *)EVP_MD_get0_name(evp_md), 0); + params[1] = OSSL_PARAM_construct_octet_string("key", priv_key, jdata->keysize); + params[2] = OSSL_PARAM_construct_end(); + + while (1) { + i = jid % MAX_POOL_LENTH; + src = soft_pool->bds[i].src; + + EVP_DigestInit_ex2(md_ctx, evp_md, params); + EVP_DigestUpdate(md_ctx, src, g_pktlen); + EVP_DigestFinal_ex(md_ctx, mac, &ssl_size); + + count++; + if (get_run_state() == 0) + break; + } + EVP_MD_CTX_free(md_ctx); + + } else { + f_mac = EVP_MAC_fetch(NULL, "HMAC", NULL); + m_ctx = EVP_MAC_CTX_new(f_mac); + if (!m_ctx) { + SSL_TST_PRT("Error: EVP_MAC_CTX_new failed\n"); + EVP_MAC_free(f_mac); + return 0; + } + + OSSL_PARAM params[3]; + params[0] = OSSL_PARAM_construct_utf8_string("digest", "SHA256", 0); + params[1] = OSSL_PARAM_construct_octet_string("key", priv_key, jdata->keysize); + params[2] = OSSL_PARAM_construct_end(); + + while (1) { + i = jid % MAX_POOL_LENTH; + src = soft_pool->bds[i].src; + + EVP_MAC_init(m_ctx, priv_key, jdata->keysize, params); + EVP_MAC_update(m_ctx, src, g_pktlen); + EVP_MAC_final(m_ctx, mac, &required_len, EVP_MAX_MD_SIZE); + count++; + if (get_run_state() == 0) + break; + } + + EVP_MAC_CTX_free(m_ctx); + EVP_MAC_free(f_mac); + } + + add_recv_data(count, g_pktlen); + + 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; + jobs_args loop_jobs[MAX_JOBS_NUM]; + OSSL_ASYNC_FD waitfd = 0; + jobs_data jobdata; + 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; + jobdata.keysize = pdata->keysize; + jobdata.mode = pdata->mode; + jobdata.optype = pdata->optype; + jobdata.subtype = pdata->subtype; + jobdata.td_id = pdata->td_id; + jobdata.engine = pdata->engine; + + 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); + + 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"); + break; + case ASYNC_NO_JOBS: + SSL_TST_PRT("Error: can't get soft async job from job pool.\n"); + break; + case ASYNC_PAUSE: + loop_jobs[i].job_valid = true; + valid_jobs++; + break; + case ASYNC_FINISH: + break; + default: + 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; + int outl = 0; + + if (!evp_cipher) { + SSL_TST_PRT("Error: openssl not support!\n"); + return NULL; + } + + if (pdata->td_id > g_thread_num) { + SSL_TST_PRT("Error: thread id %d out of range (max %d)\n", 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) { + SSL_TST_PRT("Error: EVP_CIPHER_CTX_new failed\n"); + return NULL; + } + + size_t iv_len = EVP_CIPHER_get_iv_length(evp_cipher); + size_t key_len = MAX_IVK_LENTH; + + while (1) { + u32 local_count = count % MAX_POOL_LENTH; + src = soft_pool->bds[local_count].src; + dst = soft_pool->bds[local_count].dst; + + if (optype) { + EVP_DecryptInit_ex2(ctx, evp_cipher, priv_key, priv_iv, NULL); + EVP_DecryptUpdate(ctx, dst, &outl, src, g_pktlen); + EVP_CIPHER_CTX_set_padding(ctx, EVP_PADDING_ZERO); + EVP_DecryptFinal_ex(ctx, dst, &outl); + } else { + EVP_EncryptInit_ex2(ctx, evp_cipher, priv_key, priv_iv, NULL); + EVP_EncryptUpdate(ctx, dst, &outl, src, g_pktlen); + EVP_CIPHER_CTX_set_padding(ctx, EVP_PADDING_ZERO); + EVP_EncryptFinal_ex(ctx, dst, &outl); + } + + count++; + if (get_run_state() == 0) + break; + } + + EVP_CIPHER_CTX_free(ctx); + cal_avg_latency(count); + add_recv_data(count, g_pktlen); + + return NULL; +} + +static void *sec_soft_aead_sync(void *arg) +{ + soft_thread *pdata = (soft_thread *)arg; + const EVP_CIPHER *evp_cipher = pdata->evp_cipher; + u32 optype = pdata->optype; + u32 mode = pdata->mode; + struct bd_pool *soft_pool; + u8 *priv_iv, *priv_key; + EVP_CIPHER_CTX *ctx = NULL; + u8 aad[13] = {0xcc}; + u8 tag[12] = {0}; + u8 out_tag[EVP_MAX_IV_LENGTH] = {0}; + u32 count = 0; + u8 *src, *dst; + int ret, i; + int outl = 0; + size_t taglen = 0; + int ccm_l = 0; + + if (!evp_cipher) { + SSL_TST_PRT("Error: openssl not support!\n"); + return NULL; + } + + if (pdata->td_id > g_thread_num) { + SSL_TST_PRT("Error: thread id out of range\n"); + 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) { + SSL_TST_PRT("Error: EVP_CIPHER_CTX_new failed\n"); + return NULL; + } + + while (1) { + i = count % MAX_POOL_LENTH; + src = soft_pool->bds[i].src; + dst = soft_pool->bds[i].dst; + + OSSL_PARAM params[10]; + int param_idx = 0; + + params[param_idx++] = OSSL_PARAM_construct_utf8_string("cipher", (char *)EVP_CIPHER_get0_name(evp_cipher), 0); + params[param_idx++] = OSSL_PARAM_construct_octet_string("key", priv_key, MAX_IVK_LENTH); + params[param_idx++] = OSSL_PARAM_construct_octet_string("iv", priv_iv, EVP_CIPHER_get_iv_length(evp_cipher)); + + if (mode == WD_CIPHER_CCM) { + taglen = 12; + params[param_idx++] = OSSL_PARAM_construct_size_t("taglen", &taglen); + ccm_l = (EVP_CIPHER_get_iv_length(evp_cipher) - 1) - 1; + params[param_idx++] = OSSL_PARAM_construct_int("ccm_l", &ccm_l); + } else { + taglen = 16; + params[param_idx++] = OSSL_PARAM_construct_size_t("taglen", &taglen); + ccm_l = (EVP_CIPHER_get_iv_length(evp_cipher) - 1) - 1; + params[param_idx++] = OSSL_PARAM_construct_int("ccm_l", &ccm_l); + } + params[param_idx++] = OSSL_PARAM_construct_end(); + + if (optype) { + ret = EVP_DecryptInit_ex2(ctx, evp_cipher, priv_key, priv_iv, params); + } else { + ret = EVP_EncryptInit_ex2(ctx, evp_cipher, priv_key, priv_iv, params); + } + + if (optype) { + OSSL_PARAM tag_params[] = { + OSSL_PARAM_construct_octet_string("tag", tag, sizeof(tag)), + OSSL_PARAM_construct_end() + }; + ret = EVP_CIPHER_CTX_set_params(ctx, tag_params); + if (ret != 1) { + SSL_TST_PRT("Error: EVP_CIPHER_CTX_set_params(tag) failed on decrypt\n"); + break; + } + + OSSL_PARAM aad_params[] = { + OSSL_PARAM_construct_octet_string("aad", aad, sizeof(aad)), + OSSL_PARAM_construct_end() + }; + ret = EVP_CIPHER_CTX_set_params(ctx, aad_params); + if (ret != 1) { + SSL_TST_PRT("Error: EVP_CIPHER_CTX_set_params(aad) failed on decrypt\n"); + break; + } + + EVP_DecryptUpdate(ctx, dst, &outl, src, g_pktlen); + EVP_DecryptFinal_ex(ctx, dst + outl, &outl); + + } else { + OSSL_PARAM aad_params[] = { + OSSL_PARAM_construct_octet_string("aad", aad, sizeof(aad)), + OSSL_PARAM_construct_end() + }; + ret = EVP_CIPHER_CTX_set_params(ctx, aad_params); + if (ret != 1) { + SSL_TST_PRT("Error: EVP_CIPHER_CTX_set_params(aad) failed on encrypt\n"); + break; + } + + EVP_EncryptUpdate(ctx, dst, &outl, src, g_pktlen); + EVP_EncryptFinal_ex(ctx, dst + outl, &outl); + } + + if (!optype && mode != WD_CIPHER_CCM) { + OSSL_PARAM get_tag_params[] = { + OSSL_PARAM_construct_octet_string("tag", out_tag, sizeof(out_tag)), + OSSL_PARAM_construct_end() + }; + (void)EVP_CIPHER_CTX_get_params(ctx, get_tag_params); + } + + count++; + if (get_run_state() == 0) + break; + } + + EVP_CIPHER_CTX_free(ctx); + + cal_avg_latency(count); + 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; + EVP_MAC *f_mac = NULL; + EVP_MAC_CTX *m_ctx = NULL; + u8 *priv_key, *src; + size_t mac_size = 0; + u32 count = 0; + int i; + int ret = 0; + u32 ssl_size = 0; + + if (!evp_cipher && !evp_md) { + SSL_TST_PRT("Error: openssl not support! No cipher or md provided.\n"); + return NULL; + } + + if (pdata->td_id > g_thread_num) { + SSL_TST_PRT("Error: thread id %d out of range (max %d)\n", 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) { + SSL_TST_PRT("Error: EVP_MD_CTX_new failed\n"); + return NULL; + } + + OSSL_PARAM digest_params[] = { + OSSL_PARAM_construct_utf8_string("digest", (char *)EVP_MD_get0_name(evp_md), 0), + OSSL_PARAM_construct_end() + }; + + while (1) { + i = count % MAX_POOL_LENTH; + src = soft_pool->bds[i].src; + + ret = EVP_DigestInit_ex2(md_ctx, evp_md, digest_params); + if (ret != 1) { + SSL_TST_PRT("Error: EVP_DigestInit_ex2 failed\n"); + break; + } + + ret = EVP_DigestUpdate(md_ctx, src, g_pktlen); + if (ret != 1) { + SSL_TST_PRT("Error: EVP_DigestUpdate failed\n"); + break; + } + + ret = EVP_DigestFinal_ex(md_ctx, mac, &ssl_size); + if (ret != 1) { + SSL_TST_PRT("Error: EVP_DigestFinal_ex failed\n"); + break; + } + + count++; + if (get_run_state() == 0) + break; + } + + EVP_MD_CTX_free(md_ctx); + } else { + f_mac = EVP_MAC_fetch(NULL, "HMAC", NULL); + if (!f_mac) { + SSL_TST_PRT("Error: EVP_MAC_fetch(HMAC) failed\n"); + return NULL; + } + + m_ctx = EVP_MAC_CTX_new(f_mac); + if (!m_ctx) { + SSL_TST_PRT("Error: EVP_MAC_CTX_new failed\n"); + EVP_MAC_free(f_mac); + return NULL; + } + + OSSL_PARAM mac_params[] = { + OSSL_PARAM_construct_utf8_string("digest", "SHA256", 0), + OSSL_PARAM_construct_octet_string("key", priv_key, pdata->keysize), + OSSL_PARAM_construct_end() + }; + + while (1) { + i = count % MAX_POOL_LENTH; + src = soft_pool->bds[i].src; + + ret = EVP_MAC_init(m_ctx, priv_key, pdata->keysize, mac_params); + if (ret != 1) { + SSL_TST_PRT("Error: EVP_MAC_init failed\n"); + break; + } + + ret = EVP_MAC_update(m_ctx, src, g_pktlen); + if (ret != 1) { + SSL_TST_PRT("Error: EVP_MAC_update failed\n"); + break; + } + + ret = EVP_MAC_final(m_ctx, mac, &mac_size, sizeof(mac)); + if (ret != 1) { + SSL_TST_PRT("Error: EVP_MAC_final failed\n"); + break; + } + + count++; + if (get_run_state() == 0) + break; + } + + EVP_MAC_CTX_free(m_ctx); + EVP_MAC_free(f_mac); + } + + if(optype && !f_mac) { + return NULL; + } + if(!optype && !md_ctx) { + return NULL; + } + + cal_avg_latency(count); + add_recv_data(count, g_pktlen); + + return NULL; +} + +static void uadk_engine_set_env(soft_thread *options) +{ + char env_string[ENV_STRING_LEN] = {0}; + char *var_name = NULL; + + switch(options->subtype) { + case CIPHER_TYPE: + var_name = "WD_CIPHER_CTX_NUM"; + break; + case AEAD_TYPE: + var_name = "WD_CIPHER_CTX_NUM"; + break; + case DIGEST_TYPE: + var_name = "WD_DIGEST_CTX_NUM"; + break; + default: + return; + } + + unsetenv(var_name); + + /* uadk will request ctxs from env param */ + if (options->sync_mode) // async mode + (void)snprintf(env_string, ENV_STRING_LEN, "%s%d%s%d%s", + "async:", g_jobsnum,"@0,async:", g_jobsnum, "@2"); + else + (void)snprintf(env_string, ENV_STRING_LEN, "%s%d%s%d%s", + "sync:", g_jobsnum,"@0,sync:", g_jobsnum, "@2"); + (void)setenv(var_name, env_string, 1); +} + +static int uadk_provider_register(soft_thread *options, char *engine_name) +{ + if (!options->engine_flag) + return 0; + + /* Set env param for uadk engine */ + uadk_engine_set_env(options); + OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL); + + options->engine = OSSL_PROVIDER_load(NULL, engine_name); + if (!options->engine) { + SSL_TST_PRT("setup uadk engine failed!\n"); + return -EINVAL; + } + + return 0; +} + +static void uadk_provider_unregister(soft_thread *options) +{ + if (!options->engine_flag) + return; + + if (options->engine) { + OSSL_PROVIDER_unload(options->engine); + options->engine = NULL; + } +} + +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]; + int i, ret; + + /* Alg param parse and set to thread data */ + ret = sec_soft_param_parse(&threads_option, options); + if (ret) + return ret; + + threads_option.engine_flag = options->engine_flag; + threads_option.sync_mode = options->syncmode; + ret = uadk_provider_register(&threads_option, options->engine); + if (ret) + return ret; + + + switch (options->subtype) { + case CIPHER_TYPE: + SSL_TST_PRT(" --- CIPHER_TYPE\n"); + soft_sec_sync_run = sec_soft_cipher_sync; + break; + case AEAD_TYPE: + SSL_TST_PRT(" --- AEAD_TYPE\n"); + soft_sec_sync_run = sec_soft_aead_sync; + break; + case DIGEST_TYPE: + SSL_TST_PRT(" --- DIGEST_TYPE\n"); + 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; + threads_args[i].subtype = threads_option.subtype; + threads_args[i].mode = threads_option.mode; + threads_args[i].keysize = threads_option.keysize; + threads_args[i].optype = threads_option.optype; + threads_args[i].td_id = i; + threads_args[i].engine_flag = options->engine_flag; + threads_args[i].engine = threads_option.engine; + 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; + } + } + + + /* Join thread */ + for (i = 0; i < g_thread_num; i++) { + ret = pthread_join(tdid[i], NULL); + if (ret) { + SSL_TST_PRT("Join sync thread fail!\n"); + goto sync_error; + } + } + +sync_error: + uadk_provider_unregister(&threads_option); + return ret; +} + +int sec_soft_async_threads(struct acc_option *options) +{ + soft_thread threads_args[THREADS_NUM]; + soft_thread threads_option; + pthread_t tdid[THREADS_NUM]; + int i, ret; + + /* Alg param parse and set to thread data */ + ret = sec_soft_param_parse(&threads_option, options); + if (ret) + return ret; + + threads_option.engine_flag = options->engine_flag; + threads_option.sync_mode = options->syncmode; + ret = uadk_provider_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; + threads_args[i].subtype = threads_option.subtype; + threads_args[i].mode = threads_option.mode; + threads_args[i].keysize = threads_option.keysize; + threads_args[i].optype = threads_option.optype; + threads_args[i].td_id = i; + threads_args[i].engine_flag = options->engine_flag; + threads_args[i].engine = threads_option.engine; + ret = pthread_create(&tdid[i], NULL, sec_soft_async_run, &threads_args[i]); + if (ret) { + SSL_TST_PRT("Create async thread fail!\n"); + goto async_error; + } + } + + /* Join thread */ + for (i = 0; i < g_thread_num; i++) { + ret = pthread_join(tdid[i], NULL); + if (ret) { + SSL_TST_PRT("Join async thread fail!\n"); + goto async_error; + } + } + +async_error: + uadk_provider_unregister(&threads_option); + return ret; +} + +int sec_soft3_benchmark(struct acc_option *options) +{ + u32 ptime; + int ret; + + signal(SIGSEGV, segmentfault_handler); + g_thread_num = options->threads; + g_pktlen = options->pktlen; + g_jobsnum = options->ctxnums; + if (options->optype > WD_CIPHER_DECRYPTION) { + SSL_TST_PRT("SEC optype error: %u\n", options->optype); + return -EINVAL; + } + + ret = init_soft_bd_pool(); + if (ret) + return ret; + + get_pid_cpu_time(&ptime); + time_start(options->times); + if (options->syncmode) + ret = sec_soft_async_threads(options); + else + ret = sec_soft_sync_threads(options); + cal_perfermance_data(options, ptime); + if (ret) + return ret; + + free_soft_bd_pool(); + + return 0; +} diff --git a/uadk_tool/benchmark/sec_soft3_benchmark.h b/uadk_tool/benchmark/sec_soft3_benchmark.h new file mode 100644 index 0000000..f10bcb2 --- /dev/null +++ b/uadk_tool/benchmark/sec_soft3_benchmark.h @@ -0,0 +1,8 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +#ifndef SEC_SOFT3_BENCHMARK_H +#define SEC_SOFT3_BENCHMARK_H + +#include "uadk_benchmark.h" + +extern int sec_soft3_benchmark(struct acc_option *options); +#endif /* SEC_SOFT_BENCHMARK_H */ diff --git a/uadk_tool/benchmark/uadk_benchmark.c b/uadk_tool/benchmark/uadk_benchmark.c index d8f066f..756c3ad 100644 --- a/uadk_tool/benchmark/uadk_benchmark.c +++ b/uadk_tool/benchmark/uadk_benchmark.c @@ -8,7 +8,11 @@ #include "uadk_benchmark.h" #include "sec_uadk_benchmark.h" #include "sec_wd_benchmark.h" -#include "sec_soft_benchmark.h" +// # if OPENSSL_VERSION_NUMBER < 0x30000000L +// #include "sec_soft_benchmark.h" +// # else +#include "sec_soft3_benchmark.h" +// # endif #include "hpre_uadk_benchmark.h" #include "hpre_wd_benchmark.h" @@ -568,11 +572,17 @@ static int benchmark_run(struct acc_option *option) ret = sec_wd_benchmark(option); } usleep(20000); -#ifdef HAVE_CRYPTO +// #ifdef HAVE_CRYPTO +// # if OPENSSL_VERSION_NUMBER < 0x30000000L +// if (option->modetype == SOFT_MODE) { +// ret = sec_soft_benchmark(option); +// } +// # else if (option->modetype == SOFT_MODE) { - ret = sec_soft_benchmark(option); + ret = sec_soft3_benchmark(option); } -#endif +// # endif +// #endif break; case HPRE_TYPE: if (option->modetype == SVA_MODE) { diff --git a/uadk_tool/test/comp_lib.c b/uadk_tool/test/comp_lib.c index 014aa98..39bc526 100644 --- a/uadk_tool/test/comp_lib.c +++ b/uadk_tool/test/comp_lib.c @@ -10,6 +10,12 @@ #include <math.h> #include <sys/mman.h> #include <zlib.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +// #include "openssl/md5.h" + + #include "comp_lib.h" @@ -255,9 +261,18 @@ int calculate_md5(comp_md5_t *md5, const void *buf, size_t len) { if (!md5 || !buf || !len) return -EINVAL; + +# if OPENSSL_VERSION_NUMBER < 0x30000000L MD5_Init(&md5->md5_ctx); MD5_Update(&md5->md5_ctx, buf, len); MD5_Final(md5->md, &md5->md5_ctx); +# else + md5->ctx = EVP_MD_CTX_new(); + EVP_DigestInit_ex2(md5->ctx, EVP_md5(), NULL); + EVP_DigestUpdate(md5->ctx, buf, len); + EVP_DigestFinal_ex(md5->ctx, md5->md, NULL); + EVP_MD_CTX_free(md5->ctx); +# endif return 0; } @@ -425,7 +440,7 @@ static int chunk_inflate(void *in, size_t in_sz, void *out, size_t *out_sz, do { ret = inflate(&strm, Z_NO_FLUSH); if ((ret < 0) || (ret == Z_NEED_DICT)) { - COMP_TST_PRT("zlib error %d - %s\n", ret, strm.msg); + COMP_TST_PRT("zlib error %d - %s %s\n", ret, strm.msg, __func__); goto out; } if (!strm.avail_out) { @@ -1319,7 +1334,7 @@ int hizip_check_output(void *buf, size_t size, size_t *checked, do { ret = inflate(&stream, Z_NO_FLUSH); if (ret < 0 || ret == Z_NEED_DICT) { - COMP_TST_PRT("zlib error %d - %s\n", ret, stream.msg); + COMP_TST_PRT("zlib error %d - %s %s\n", ret, stream.msg, __func__); ret = -ENOSR; break; } @@ -1387,7 +1402,7 @@ int zlib_deflate(void *output, unsigned int out_size, do { ret = deflate(&stream, Z_FINISH); if (ret == Z_STREAM_ERROR || ret == Z_BUF_ERROR) { - COMP_TST_PRT("zlib error %d - %s\n", ret, stream.msg); + COMP_TST_PRT("zlib error %d - %s %s\n", ret, stream.msg, __func__); ret = -ENOSR; break; } else if (!stream.avail_in) { diff --git a/uadk_tool/test/comp_lib.h b/uadk_tool/test/comp_lib.h index c37a226..9eb7a31 100644 --- a/uadk_tool/test/comp_lib.h +++ b/uadk_tool/test/comp_lib.h @@ -22,6 +22,12 @@ #include "include/wd_comp.h" #include "include/wd_sched.h" +# if OPENSSL_VERSION_NUMBER >= 0x30000000L +#include "openssl/opensslv.h" +#include "openssl/evp.h" +#include "openssl/ossl_typ.h" +# endif + #define SYS_ERR_COND(cond, msg, ...) \ do { \ if (cond) { \ @@ -120,7 +126,11 @@ struct test_options { }; typedef struct _comp_md5_t { +# if OPENSSL_VERSION_NUMBER < 0x30000000L MD5_CTX md5_ctx; +# else + EVP_MD_CTX *ctx; +# endif unsigned char md[MD5_DIGEST_LENGTH]; } comp_md5_t; diff --git a/uadk_tool/test/comp_main.c b/uadk_tool/test/comp_main.c index 24f52d2..81b213a 100644 --- a/uadk_tool/test/comp_main.c +++ b/uadk_tool/test/comp_main.c @@ -86,7 +86,7 @@ static void *sw_dfl_hw_ifl(void *arg) void *tbuf; size_t tbuf_sz; chunk_list_t *tlist; - comp_md5_t final_md5 = {{0}}; + comp_md5_t final_md5 = {0}; int i, ret; __u32 tout_sz; @@ -230,7 +230,7 @@ static void *hw_dfl_sw_ifl(void *arg) void *tbuf; size_t tbuf_sz; chunk_list_t *tlist; - comp_md5_t final_md5 = {{0}}; + comp_md5_t final_md5 = {0}; int i, ret; __u32 tmp_sz; @@ -374,7 +374,7 @@ static void *hw_dfl_hw_ifl(void *arg) void *tbuf; size_t tbuf_sz; chunk_list_t *tlist; - comp_md5_t final_md5 = {{0}}; + comp_md5_t final_md5 = {0}; int i, ret; __u32 tmp_sz, tout_sz; @@ -886,6 +886,7 @@ static int test_hw(struct test_options *opts, char *model) goto out_src; opts->total_len = statbuf.st_size; info.in_size = opts->total_len; + COMP_TST_PRT("in_size : %d\n", info.in_size); if (ifl_flag) info.out_size = opts->total_len * INFLATION_RATIO; else @@ -898,6 +899,7 @@ static int test_hw(struct test_options *opts, char *model) if (opts->is_file) { ret = load_file_data(&info); + COMP_TST_PRT("file_size : %d\n", ret); if (ret < 0) goto out_buf; } else { diff --git a/uadk_tool/test/uadk_test.c b/uadk_tool/test/uadk_test.c index 536861a..aa885d1 100644 --- a/uadk_tool/test/uadk_test.c +++ b/uadk_tool/test/uadk_test.c @@ -63,10 +63,10 @@ void acc_test_run(int argc, char *argv[]) (void)test_hpre_entry(argc, argv); } else if (!strcmp(input_module, "sec")) { (void)test_sec_entry(argc, argv); -#ifdef HAVE_CRYPTO +// #ifdef HAVE_CRYPTO } else if (!strcmp(input_module, "zip")) { (void)test_comp_entry(argc, argv); -#endif +// #endif } else { print_test_help(); printf("failed to parse module parameter!\n"); -- 2.33.0