
From: Weili Qian <qianweili@huawei.com> The trng module is not required. Therefore, remove the trng module code. Signed-off-by: Weili Qian <qianweili@huawei.com> Signed-off-by: Qi Tao <taoqi10@huawei.com> --- Makefile.am | 4 +- configure.ac | 1 - uadk_tool/Makefile.am | 1 - uadk_tool/benchmark/trng_wd_benchmark.c | 333 ------------ uadk_tool/benchmark/trng_wd_benchmark.h | 7 - uadk_tool/benchmark/uadk_benchmark.c | 15 - uadk_tool/benchmark/uadk_benchmark.h | 1 - v1/drv/hisi_rng_udrv.c | 167 ------ v1/drv/hisi_rng_udrv.h | 40 -- v1/libwd.map | 5 - v1/test/Makefile.am | 1 - v1/test/hisi_trng_test/Makefile.am | 20 - v1/test/hisi_trng_test/test_hisi_trngk.c | 155 ------ v1/test/hisi_trng_test/test_hisi_trngp.c | 137 ----- v1/test/hisi_trng_test/test_hisi_trngu.c | 624 ----------------------- v1/wd.h | 2 +- v1/wd_adapter.c | 7 - v1/wd_rng.c | 296 ----------- v1/wd_rng.h | 76 --- 19 files changed, 2 insertions(+), 1890 deletions(-) delete mode 100644 uadk_tool/benchmark/trng_wd_benchmark.c delete mode 100644 uadk_tool/benchmark/trng_wd_benchmark.h delete mode 100644 v1/drv/hisi_rng_udrv.c delete mode 100644 v1/drv/hisi_rng_udrv.h delete mode 100644 v1/test/hisi_trng_test/Makefile.am delete mode 100755 v1/test/hisi_trng_test/test_hisi_trngk.c delete mode 100644 v1/test/hisi_trng_test/test_hisi_trngp.c delete mode 100755 v1/test/hisi_trng_test/test_hisi_trngu.c delete mode 100644 v1/wd_rng.c delete mode 100644 v1/wd_rng.h diff --git a/Makefile.am b/Makefile.am index 9725f4dd..c0b0a81f 100644 --- a/Makefile.am +++ b/Makefile.am @@ -59,7 +59,6 @@ uadk_drivers_LTLIBRARIES=libhisi_sec.la libhisi_hpre.la libhisi_zip.la \ libwd_la_SOURCES=wd.c wd_mempool.c wd.h wd_alg.c wd_alg.h \ v1/wd.c v1/wd.h v1/wd_adapter.c v1/wd_adapter.h \ - v1/wd_rng.c v1/wd_rng.h \ v1/wd_rsa.c v1/wd_rsa.h \ v1/wd_aead.c v1/wd_aead.h \ v1/wd_dh.c v1/wd_dh.h \ @@ -75,8 +74,7 @@ libwd_la_SOURCES=wd.c wd_mempool.c wd.h wd_alg.c wd_alg.h \ v1/drv/hisi_qm_udrv.c v1/drv/hisi_qm_udrv.h \ v1/drv/hisi_zip_udrv.c v1/drv/hisi_zip_udrv.h \ v1/drv/hisi_hpre_udrv.c v1/drv/hisi_hpre_udrv.h \ - v1/drv/hisi_sec_udrv.c v1/drv/hisi_sec_udrv.h \ - v1/drv/hisi_rng_udrv.c v1/drv/hisi_rng_udrv.h + v1/drv/hisi_sec_udrv.c v1/drv/hisi_sec_udrv.h libwd_dae_la_SOURCES=wd_dae.h wd_agg.h wd_agg_drv.h wd_agg.c \ wd_util.c wd_util.h wd_sched.c wd_sched.h wd.c wd.h diff --git a/configure.ac b/configure.ac index 285ac029..ed060d8e 100644 --- a/configure.ac +++ b/configure.ac @@ -102,7 +102,6 @@ AC_CONFIG_FILES([Makefile v1/test/bmm_test/Makefile v1/test/test_mm/Makefile v1/test/hisi_hpre_test/Makefile - v1/test/hisi_trng_test/Makefile v1/test/hisi_sec_test/Makefile v1/test/hisi_sec_test_sgl/Makefile v1/test/hisi_zip_test/Makefile diff --git a/uadk_tool/Makefile.am b/uadk_tool/Makefile.am index ea830098..6fd9d95f 100644 --- a/uadk_tool/Makefile.am +++ b/uadk_tool/Makefile.am @@ -17,7 +17,6 @@ uadk_tool_SOURCES=uadk_tool.c dfx/uadk_dfx.c dfx/uadk_dfx.h \ benchmark/hpre_wd_benchmark.c hpre_wd_benchmark.h \ benchmark/zip_uadk_benchmark.c benchmark/zip_uadk_benchmark.h \ benchmark/zip_wd_benchmark.c benchmark/zip_wd_benchmark.h \ - benchmark/trng_wd_benchmark.c benchmark/trng_wd_benchmark.h \ test/uadk_test.c test/uadk_test.h \ test/test_sec.c test/test_sec.h test/sec_template_tv.h diff --git a/uadk_tool/benchmark/trng_wd_benchmark.c b/uadk_tool/benchmark/trng_wd_benchmark.c deleted file mode 100644 index 2f058d41..00000000 --- a/uadk_tool/benchmark/trng_wd_benchmark.c +++ /dev/null @@ -1,333 +0,0 @@ -/* SPDX-License-Identifier: Apache-2.0 */ - -#include <numa.h> -#include "uadk_benchmark.h" - -#include "trng_wd_benchmark.h" -#include "v1/wd.h" -#include "v1/wd_rng.h" - -struct thread_bd_res { - struct wd_queue *queue; - void *out; - __u32 in_bytes; -}; - -struct thread_queue_res { - struct thread_bd_res *bd_res; -}; - -struct wd_thread_res { - u32 td_id; - u32 pollid; -}; - -struct trng_async_tag { - void *ctx; - int optype; -}; - -static unsigned int g_thread_num; -static struct thread_queue_res g_thread_queue; - -static int init_trng_wd_queue(struct acc_option *options) -{ - int i, ret; - - g_thread_queue.bd_res = malloc(g_thread_num * sizeof(struct thread_bd_res)); - if (!g_thread_queue.bd_res) { - printf("failed to malloc thread res memory!\n"); - return -ENOMEM; - } - - for (i = 0; i < g_thread_num; i++) { - g_thread_queue.bd_res[i].queue = malloc(sizeof(struct wd_queue)); - if (!g_thread_queue.bd_res[i].queue) { - ret = -ENOMEM; - goto free_mem; - } - - g_thread_queue.bd_res[i].queue->capa.alg = options->algclass; - /* nodemask need to be clean */ - g_thread_queue.bd_res[i].queue->node_mask = 0x0; - memset(g_thread_queue.bd_res[i].queue->dev_path, 0x0, PATH_STR_SIZE); - if (strlen(options->device) != 0) { - ret = snprintf(g_thread_queue.bd_res[i].queue->dev_path, - PATH_STR_SIZE, "%s", options->device); - if (ret < 0) { - WD_ERR("failed to copy dev file path!\n"); - return -WD_EINVAL; - } - } - - g_thread_queue.bd_res[i].in_bytes = options->pktlen; - g_thread_queue.bd_res[i].out = malloc(options->pktlen); - if (!g_thread_queue.bd_res[i].queue) { - free(g_thread_queue.bd_res[i].queue); - ret = -ENOMEM; - goto free_mem; - } - - ret = wd_request_queue(g_thread_queue.bd_res[i].queue); - if (ret) { - printf("failed to request queue %d, ret = %d!\n", i, ret); - free(g_thread_queue.bd_res[i].out); - free(g_thread_queue.bd_res[i].queue); - goto free_mem; - } - } - - return 0; - -free_mem: - for (i = i - 1; i >= 0; i--) { - wd_release_queue(g_thread_queue.bd_res[i].queue); - free(g_thread_queue.bd_res[i].out); - free(g_thread_queue.bd_res[i].queue); - } - - free(g_thread_queue.bd_res); - return ret; -} - -static void uninit_trng_wd_queue(void) -{ - int j; - - for (j = 0; j < g_thread_num; j++) { - wd_release_queue(g_thread_queue.bd_res[j].queue); - free(g_thread_queue.bd_res[j].out); - free(g_thread_queue.bd_res[j].queue); - } - - free(g_thread_queue.bd_res); -} - -static void *trng_wd_sync_run(void *arg) -{ - struct wd_thread_res *pdata = (struct wd_thread_res *)arg; - struct wcrypto_rng_ctx_setup trng_setup; - struct wcrypto_rng_op_data opdata; - struct wd_queue *queue; - void *ctx = NULL; - u32 count = 0; - int ret; - - queue = g_thread_queue.bd_res[pdata->td_id].queue; - ctx = wcrypto_create_rng_ctx(queue, &trng_setup); - if (!ctx) - return NULL; - - memset(&opdata, 0, sizeof(opdata)); - opdata.in_bytes = g_thread_queue.bd_res[pdata->td_id].in_bytes; - opdata.out = g_thread_queue.bd_res[pdata->td_id].out; - opdata.op_type = WCRYPTO_TRNG_GEN; - - do { - ret = wcrypto_do_rng(ctx, &opdata, NULL); - if (ret) { - printf("failed to do rng task, ret: %d\n", ret); - goto ctx_release; - } - - count++; - if (get_run_state() == 0) - break; - } while (true); - -ctx_release: - wcrypto_del_rng_ctx(ctx); - add_recv_data(count, opdata.in_bytes); - - return NULL; -} - -static void trng_wd_sync_threads(void) -{ - struct wd_thread_res threads_args[THREADS_NUM]; - pthread_t tdid[THREADS_NUM]; - int i, ret; - - for (i = 0; i < g_thread_num; i++) { - threads_args[i].td_id = i; - ret = pthread_create(&tdid[i], NULL, trng_wd_sync_run, &threads_args[i]); - if (ret) { - printf("failed to create sync thread!\n"); - return; - } - } - - /* join thread */ - for (i = 0; i < g_thread_num; i++) { - ret = pthread_join(tdid[i], NULL); - if (ret) { - printf("failed to join sync thread!\n"); - return; - } - } -} - -void *wd_trng_poll(void *data) -{ - struct wd_thread_res *pdata = (struct wd_thread_res *)data; - struct wd_queue *queue; - u32 last_time = 2; // poll need one more recv time - u32 count = 0; - u32 in_bytes; - int recv; - - in_bytes = g_thread_queue.bd_res[pdata->pollid].in_bytes; - queue = g_thread_queue.bd_res[pdata->pollid].queue; - - while (last_time) { - recv = wcrypto_rng_poll(queue, ACC_QUEUE_SIZE); - if (recv < 0) { - printf("failed to recv bd, ret: %d!\n", recv); - goto recv_error; - } - count += recv; - - if (get_run_state() == 0) - last_time--; - } - -recv_error: - add_recv_data(count, in_bytes); - - return NULL; -} - -static void *trng_async_cb(const void *msg, void *tag) -{ - return NULL; -} - -static void *wd_trng_async_run(void *arg) -{ - struct wd_thread_res *pdata = (struct wd_thread_res *)arg; - struct wcrypto_rng_ctx_setup trng_setup; - struct wcrypto_rng_op_data opdata; - struct trng_async_tag *tag = NULL; - struct wd_queue *queue; - void *ctx = NULL; - int ret, i; - - memset(&opdata, 0, sizeof(opdata)); - - queue = g_thread_queue.bd_res[pdata->td_id].queue; - trng_setup.cb = (void *)trng_async_cb; - - ctx = wcrypto_create_rng_ctx(queue, &trng_setup); - if (!ctx) - return NULL; - - opdata.in_bytes = g_thread_queue.bd_res[pdata->td_id].in_bytes; - opdata.out = g_thread_queue.bd_res[pdata->td_id].out; - opdata.op_type = WCRYPTO_TRNG_GEN; - - tag = malloc(sizeof(*tag) * MAX_POOL_LENTH); - if (!tag) { - printf("failed to malloc dh tag!\n"); - goto free_ctx; - } - tag->ctx = ctx; - - do { - ret = wcrypto_do_rng(ctx, &opdata, tag); - if (ret && ret != -WD_EBUSY) { - printf("failed to send trng task, ret = %d!\n", ret); - break; - } - - if (get_run_state() == 0) - break; - } while (true); - - /* Release memory after all tasks are complete. */ - i = 0; - while (get_recv_time() != g_thread_num) { - if (i++ >= MAX_TRY_CNT) { - printf("failed to wait poll thread finish!\n"); - break; - } - - usleep(SEND_USLEEP); - } - - if (tag) - free(tag); -free_ctx: - wcrypto_del_rng_ctx(ctx); - add_send_complete(); - - return NULL; -} - -static void trng_wd_async_threads(void) -{ - struct wd_thread_res threads_args[THREADS_NUM]; - pthread_t tdid[THREADS_NUM]; - pthread_t pollid[THREADS_NUM]; - int i, ret; - - for (i = 0; i < g_thread_num; i++) { - threads_args[i].pollid = i; - /* poll thread */ - ret = pthread_create(&pollid[i], NULL, wd_trng_poll, &threads_args[i]); - if (ret) { - printf("failed to create poll thread!\n"); - return; - } - } - - for (i = 0; i < g_thread_num; i++) { - threads_args[i].td_id = i; - ret = pthread_create(&tdid[i], NULL, wd_trng_async_run, &threads_args[i]); - if (ret) { - printf("failed to create async thread!\n"); - return; - } - } - - /* join thread */ - for (i = 0; i < g_thread_num; i++) { - ret = pthread_join(tdid[i], NULL); - if (ret) { - printf("failed to join async thread!\n"); - return; - } - } - - for (i = 0; i < g_thread_num; i++) { - ret = pthread_join(pollid[i], NULL); - if (ret) { - printf("failed to join poll thread!\n"); - return; - } - } -} - -int trng_wd_benchmark(struct acc_option *options) -{ - u32 ptime; - int ret; - - signal(SIGSEGV, segmentfault_handler); - g_thread_num = options->threads; - - ret = init_trng_wd_queue(options); - if (ret) - return ret; - - get_pid_cpu_time(&ptime); - time_start(options->times); - if (options->syncmode) - trng_wd_async_threads(); - else - trng_wd_sync_threads(); - cal_perfermance_data(options, ptime); - - uninit_trng_wd_queue(); - - return 0; -} diff --git a/uadk_tool/benchmark/trng_wd_benchmark.h b/uadk_tool/benchmark/trng_wd_benchmark.h deleted file mode 100644 index 49453c81..00000000 --- a/uadk_tool/benchmark/trng_wd_benchmark.h +++ /dev/null @@ -1,7 +0,0 @@ -/* SPDX-License-Identifier: Apache-2.0 */ - -#ifndef TRNG_WD_BENCHMARK_H -#define TRNG_WD_BENCHMARK_H - -extern int trng_wd_benchmark(struct acc_option *options); -#endif /* TRNG_WD_BENCHMARK_H */ diff --git a/uadk_tool/benchmark/uadk_benchmark.c b/uadk_tool/benchmark/uadk_benchmark.c index 16980616..e6cc2ddf 100644 --- a/uadk_tool/benchmark/uadk_benchmark.c +++ b/uadk_tool/benchmark/uadk_benchmark.c @@ -16,8 +16,6 @@ #include "zip_uadk_benchmark.h" #include "zip_wd_benchmark.h" -#include "trng_wd_benchmark.h" - #define TABLE_SPACE_SIZE 8 /*----------------------------------------head struct--------------------------------------------------------*/ @@ -153,7 +151,6 @@ static struct acc_alg_item alg_options[] = { {"sha512", "sha512", SHA512_ALG}, {"sha512-224", "sha512-224", SHA512_224}, {"sha512-256", "sha512-256", SHA512_256}, - {"trng", "trng", TRNG}, {"", "", ALG_MAX} }; @@ -385,11 +382,6 @@ static void parse_alg_param(struct acc_option *option) option->acctype = HPRE_TYPE; option->subtype = X448_TYPE; break; - case TRNG: - snprintf(option->algclass, MAX_ALG_NAME, "%s", "trng"); - option->acctype = TRNG_TYPE; - option->subtype = DEFAULT_TYPE; - break; default: if (option->algtype <= RSA_4096_CRT) { snprintf(option->algclass, MAX_ALG_NAME, "%s", "rsa"); @@ -518,13 +510,6 @@ static int benchmark_run(struct acc_option *option) ret = zip_wd_benchmark(option); } break; - case TRNG_TYPE: - if (option->modetype == SVA_MODE) - ACC_TST_PRT("TRNG not support sva mode..\n"); - else if (option->modetype == NOSVA_MODE) - ret = trng_wd_benchmark(option); - - break; } return ret; diff --git a/uadk_tool/benchmark/uadk_benchmark.h b/uadk_tool/benchmark/uadk_benchmark.h index c4042355..39725724 100644 --- a/uadk_tool/benchmark/uadk_benchmark.h +++ b/uadk_tool/benchmark/uadk_benchmark.h @@ -87,7 +87,6 @@ enum acc_type { SEC_TYPE, HPRE_TYPE, ZIP_TYPE, - TRNG_TYPE, }; enum acc_init_type { diff --git a/v1/drv/hisi_rng_udrv.c b/v1/drv/hisi_rng_udrv.c deleted file mode 100644 index 605ef275..00000000 --- a/v1/drv/hisi_rng_udrv.c +++ /dev/null @@ -1,167 +0,0 @@ -/* - * Copyright 2018-2019 Huawei Technologies Co.,Ltd.All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include <stdlib.h> -#include <unistd.h> -#include <stdio.h> -#include <pthread.h> -#include <sys/mman.h> -#include <string.h> -#include <stdint.h> -#include <sys/epoll.h> -#include <sys/eventfd.h> -#include <sys/types.h> - -#include "hisi_rng_udrv.h" - -#define HISI_RNG_BYTES 4 -#define MAX_RETRY_COUNTS 8 -#define RNG_NUM_OFFSET 0x00F0 - -int rng_init_queue(struct wd_queue *q) -{ - struct q_info *qinfo = q->qinfo; - struct rng_queue_info *info; - int ret; - - info = calloc(1, sizeof(*info)); - if (!info) { - WD_ERR("no mem!\n"); - return -ENOMEM; - } - - ret = pthread_spin_init(&info->lock, PTHREAD_PROCESS_PRIVATE); - if (ret) { - free(info); - WD_ERR("failed to init rng qinfo lock!\n"); - return ret; - } - - qinfo->priv = info; - info->mmio_base = wd_drv_mmap_qfr(q, WD_UACCE_QFRT_MMIO, 0); - if (info->mmio_base == MAP_FAILED) { - info->mmio_base = NULL; - qinfo->priv = NULL; - pthread_spin_destroy(&info->lock); - free(info); - WD_ERR("mmap trng mmio fail\n"); - return -ENOMEM; - } - - return 0; -} - -void rng_uninit_queue(struct wd_queue *q) -{ - struct q_info *qinfo = q->qinfo; - struct rng_queue_info *info = qinfo->priv; - - wd_drv_unmmap_qfr(q, info->mmio_base, WD_UACCE_QFRT_MMIO, 0); - - free(qinfo->priv); - qinfo->priv = NULL; - pthread_spin_destroy(&info->lock); -} - -int rng_send(struct wd_queue *q, void **req, __u32 num) -{ - struct q_info *qinfo = q->qinfo; - struct rng_queue_info *info = qinfo->priv; - - pthread_spin_lock(&info->lock); - if (!info->req_cache[info->send_idx]) { - info->req_cache[info->send_idx] = req[0]; - info->send_idx++; - pthread_spin_unlock(&info->lock); - return 0; - } - pthread_spin_unlock(&info->lock); - - WD_ERR("queue is full!\n"); - return -WD_EBUSY; -} - -static int rng_read(struct rng_queue_info *info, struct wcrypto_rng_msg *msg) -{ - __u32 max = msg->in_bytes; - __u32 currsize = 0; - int recv_count = 0; - __u32 val; - - do { - val = wd_reg_read((void *)((uintptr_t)info->mmio_base + - RNG_NUM_OFFSET)); - if (!val) { - if (++recv_count > MAX_RETRY_COUNTS) { - WD_ERR("read random data timeout\n"); - break; - } - - usleep(1); - continue; - } - - recv_count = 0; - if (max - currsize >= HISI_RNG_BYTES) { - memcpy(msg->out + currsize, &val, HISI_RNG_BYTES); - currsize += HISI_RNG_BYTES; - if (currsize == max) - break; - continue; - } - - memcpy(msg->out + currsize, &val, max - currsize); - currsize = max; - } while (currsize < max); - - return currsize; -} - -int rng_recv(struct wd_queue *q, void **resp, __u32 num) -{ - struct q_info *qinfo = q->qinfo; - struct rng_queue_info *info = qinfo->priv; - __u16 usr = (__u16)(uintptr_t)*resp; - struct wcrypto_rng_msg *msg; - struct wcrypto_cb_tag *tag; - __u32 currsize = 0; - - pthread_spin_lock(&info->lock); - msg = info->req_cache[info->recv_idx]; - if (!msg) { - pthread_spin_unlock(&info->lock); - return 0; - } - - info->req_cache[info->recv_idx] = NULL; - info->recv_idx++; - pthread_spin_unlock(&info->lock); - - tag = (void *)(uintptr_t)msg->usr_tag; - if (usr && tag->ctx_id != usr) - return 0; - - currsize = rng_read(info, msg); - if (!currsize) { - WD_ERR("random data err!\n"); - return -WD_EINVAL; - } - - msg->out_bytes = currsize; - *resp = msg; - - return 1; -} diff --git a/v1/drv/hisi_rng_udrv.h b/v1/drv/hisi_rng_udrv.h deleted file mode 100644 index 3efa10e9..00000000 --- a/v1/drv/hisi_rng_udrv.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright 2018-2019 Huawei Technologies Co.,Ltd.All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __HISI_RNG_UDRV_H__ -#define __HISI_RNG_UDRV_H__ - -#include <linux/types.h> -#include "v1/wd.h" -#include "v1/wd_util.h" -#include "v1/wd_rng.h" - -#define TRNG_Q_DEPTH 256 - -struct rng_queue_info { - void *mmio_base; - void *req_cache[TRNG_Q_DEPTH]; - __u8 send_idx; - __u8 recv_idx; - pthread_spinlock_t lock; -}; - -int rng_init_queue(struct wd_queue *q); -void rng_uninit_queue(struct wd_queue *q); -int rng_send(struct wd_queue *q, void **req, __u32 num); -int rng_recv(struct wd_queue *q, void **resp, __u32 num); - -#endif diff --git a/v1/libwd.map b/v1/libwd.map index d53201b5..6c544793 100644 --- a/v1/libwd.map +++ b/v1/libwd.map @@ -133,11 +133,6 @@ global: wcrypto_rsa_poll; wcrypto_del_rsa_ctx; - wcrypto_create_rng_ctx; - wcrypto_del_rng_ctx; - wcrypto_do_rng; - wcrypto_rng_poll; - wd_sglpool_create; wd_sglpool_destroy; wd_alloc_sgl; diff --git a/v1/test/Makefile.am b/v1/test/Makefile.am index 8c262f57..6fa23713 100644 --- a/v1/test/Makefile.am +++ b/v1/test/Makefile.am @@ -4,6 +4,5 @@ SUBDIRS=. test_mm bmm_test SUBDIRS+=hisi_zip_test_sgl -SUBDIRS+=hisi_trng_test if HAVE_CRYPTO SUBDIRS+=hisi_hpre_test diff --git a/v1/test/hisi_trng_test/Makefile.am b/v1/test/hisi_trng_test/Makefile.am deleted file mode 100644 index a9887fc5..00000000 --- a/v1/test/hisi_trng_test/Makefile.am +++ /dev/null @@ -1,20 +0,0 @@ -AM_CFLAGS=-Wall -Werror -O0 -fno-strict-aliasing -I$(top_srcdir)/include -I$(srcdir) -pthread - -if HAVE_CRYPTO -bin_PROGRAMS=test_hisi_trngu_v1 test_hisi_trngk_v1 test_hisi_trngp_v1 - -test_hisi_trngu_v1_SOURCES=test_hisi_trngu.c -test_hisi_trngk_v1_SOURCES=test_hisi_trngk.c -test_hisi_trngp_v1_SOURCES=test_hisi_trngp.c - -if WD_STATIC_DRV -test_hisi_trngu_v1_LDADD=../../../.libs/libwd.la -test_hisi_trngk_v1_LDADD=../../../.libs/libwd.la -test_hisi_trngp_v1_LDADD=../../../.libs/libwd.la -else -test_hisi_trngu_v1_LDADD=../../../.libs/libwd.so -test_hisi_trngk_v1_LDADD=../../../.libs/libwd.so -test_hisi_trngp_v1_LDADD=../../../.libs/libwd.so -endif - -endif diff --git a/v1/test/hisi_trng_test/test_hisi_trngk.c b/v1/test/hisi_trng_test/test_hisi_trngk.c deleted file mode 100755 index ae719e55..00000000 --- a/v1/test/hisi_trng_test/test_hisi_trngk.c +++ /dev/null @@ -1,155 +0,0 @@ -#include <assert.h> -#include <stdio.h> -#include <string.h> -#include <fcntl.h> -#define __USE_GNU -#include <sched.h> -#include <pthread.h> -#include <sys/mman.h> -#include <sys/time.h> -#include <unistd.h> -#include <getopt.h> -#include <stdlib.h> -#include <sys/stat.h> -#include <sys/types.h> -#include <sys/time.h> -#include <semaphore.h> - - -struct thread_info -{ - pthread_t thread_id; - unsigned int size; - unsigned int num; - int addr; - -}; - -void *trng_thread(void *args) -{ - - int fd = -1; - int fd_w = -1; - int ret; - unsigned int input; - struct thread_info *tinfo = args; - input = tinfo->size; - unsigned int *data = (unsigned int*)malloc(sizeof(unsigned int) * input); - - if(!data) - return NULL; - - if (tinfo->addr == 0){ - -// printf("Now try to get %d bytes random number from /dev/hwrng.\n", input * 4); - fd = open ("/dev/hwrng", O_RDONLY); - } - else if (tinfo->addr == 1){ -// printf("Now try to get %d bytes random number from /dev/random.\n", input * 4); - fd = open ("/dev/random", O_RDONLY); - } - - if (fd <0 ) { - printf("can not open\n"); - return NULL; - } - - fd_w = open ("/root/trng_file", O_WRONLY|O_CREAT|O_APPEND,0777); - if (fd_w <0 ) { - printf("can not open trng_file\n"); - return NULL; - } - memset(data, 0, sizeof(int) * input); - ret = read(fd, data, input); - if (ret < 0) { - printf("read error %d\n", ret); - return NULL; - } - ret =write(fd_w,data,input); - if (ret < 0) { - printf("write error %d\n", ret); - return NULL; - } - - close(fd); - close(fd_w); - - return NULL; -} - - -void trng_test(int addr,int num,unsigned int si,int thread_num) -{ - - int i; - void *ret = NULL; - struct thread_info *tinfo; - tinfo = calloc(thread_num, sizeof(struct thread_info)); - - if (tinfo == NULL) - { - printf("calloc fail...\n"); - return; - } - - for (i = 0; i<thread_num; ++i) - { - tinfo[i].thread_id = i; - tinfo[i].addr = addr; - tinfo[i].num = num; - tinfo[i].size = si; - - if ((pthread_create(&tinfo[i].thread_id,NULL,trng_thread, (void *)&tinfo[i])) != 0) - { - return; - } - } - - for (i=0; i<thread_num; ++i) - { - if (pthread_join(tinfo[i].thread_id, &ret) != 0) - { - printf("thread is not exit....\n"); - return; - } - //printf("thread exit coid %d\n", (int *)ret); - free(ret); - } - free(tinfo); -} - - - - -int main (int argc, char* argv[]) { - - int opt; - int addr = 0, num = 0, thread_num = 0; - unsigned int si = 0; - - while ((opt = getopt(argc, argv, "hri:p:s:")) != -1) { - switch (opt) { - case 'h': - addr = 0; - break; - case 'r': - addr = 1; - break; - case 'i': - num = atoi(optarg); - break; - case 'p': - thread_num = atoi(optarg); - break; - case 's': - si = (unsigned int)atoi(optarg); - break; - default: - break; - } - } - - trng_test(addr,num,si,thread_num); - - return 0; -} diff --git a/v1/test/hisi_trng_test/test_hisi_trngp.c b/v1/test/hisi_trng_test/test_hisi_trngp.c deleted file mode 100644 index 2330b1e1..00000000 --- a/v1/test/hisi_trng_test/test_hisi_trngp.c +++ /dev/null @@ -1,137 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <fcntl.h> -#include <sys/stat.h> -#include <sys/types.h> -#include <sys/time.h> -#include <pthread.h> -#include <unistd.h> - -static int input; -static int thread_num; -struct thread_info -{ - pthread_t thread_id; - unsigned int size; - int num; -}; - -void *trng_thread(void *args) -{ - int j; - int fd = -1; - int data; - int ret; - struct thread_info *tinfo = args; - int si; - int num; - int size; - int fd_w = -1; - si = tinfo->size; - num = tinfo->num; - size=si/num; - printf("Now try to get bytes random number from /dev/random.\n"); - fd = open("/dev/random", O_RDONLY); - if (fd <0 ) { - printf("can not open\n"); - return NULL; - } - for (j = 0; j< size; j++) { - ret = read(fd, &data, 1); - if (ret < 0) { - printf("read error %d\n", ret); - return NULL; - } -// else if (ret < 1) -// goto rd_ag; -// if (!data) { -// printf("read data error!\n"); -// return data; -// } - printf("the read num:%x\n",data); - } - fd_w = open ("/root/trng_file", O_RDWR | O_CREAT |O_APPEND , 0777); - if (fd_w <0 ) { - printf("can not open trng_file\n"); - return NULL; - } - ret = write(fd_w,&data,size); - if (ret < 0) { - printf("write error %d\n", ret); - return NULL; - } - close(fd); - close(fd_w); - return NULL; -} - -void trng_test(int input,int thread_num) -{ - int i; - void *ret = NULL; - struct thread_info *tinfo; - tinfo = calloc(thread_num, sizeof(struct thread_info)); - if(tinfo == NULL) - { - printf("calloc fail...\n"); - return; - } - for(i = 0; i<thread_num; ++i) - { - tinfo[i].thread_id = i; - tinfo[i].num=thread_num; -// tinfo[i].addr = addr; -// tinfo[i].num = num; - tinfo[i].size = input; - if((pthread_create(&tinfo[i].thread_id,NULL,trng_thread, (void *)&tinfo[i])) != 0) - { - return; - } - } - - for(i=0; i<thread_num; ++i) - { - if(pthread_join(tinfo[i].thread_id, &ret) != 0) - { - printf("thread is not exit....\n"); - return; - } -// printf("thread exit coid %d\n", (int *)ret); - free(ret); - } - free(tinfo); -} - -int main (int argc, char* argv[]) -{ - struct timeval start_tval, end_tval; - float time,speed; - int fd_f=-1; - fd_f = open ("/root/trng_file", O_RDWR | O_CREAT |O_TRUNC, 0777); - if (fd_f <0 ) { - printf("can not open trng_file\n"); - return fd_f; - } - input = strtoul(argv[1], NULL, 10); - if (input <= 0){ - printf("input error!\n"); - return -1; - } - thread_num = strtoul((char *)argv[2], NULL, 10); - if (thread_num <= 0 || thread_num > 128) { - printf("Invalid threads num:%d!\n",thread_num); - printf("Now set threads num as 2\n"); - thread_num = 2; - } - gettimeofday(&start_tval, NULL); - trng_test(input,thread_num); - gettimeofday(&end_tval, NULL); - time = (float)((end_tval.tv_sec - start_tval.tv_sec) * 1000000 + - (end_tval.tv_usec - start_tval.tv_usec)); - speed = input/(time / 1000000); - printf("read random speed: %0.0f time\n", time); - printf("read random speed: %0.0f bytes/s\n", speed); - close(fd_f); - return 0; -} diff --git a/v1/test/hisi_trng_test/test_hisi_trngu.c b/v1/test/hisi_trng_test/test_hisi_trngu.c deleted file mode 100755 index 86aa8a9b..00000000 --- a/v1/test/hisi_trng_test/test_hisi_trngu.c +++ /dev/null @@ -1,624 +0,0 @@ -/* - * Copyright 2019 Huawei Technologies Co.,Ltd.All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include <stdio.h> -#include <string.h> -#include <fcntl.h> -#define __USE_GNU -#include <sched.h> -#include <pthread.h> -#include <sys/mman.h> -#include <sys/types.h> -#include <sys/syscall.h> -#include <sys/stat.h> -#include <sys/time.h> -#include <unistd.h> -#include <semaphore.h> - -#include "../../wd.h" -#include "../../wd_rng.h" - -#define RNG_TST_PRT printf -#define BN_ULONG unsigned long -#define TEST_MAX_THRD 128 -#define MAX_TRY_TIMES 10000 -#define LOG_INTVL_NUM 8 -#define TEST_CNT 10 - -static int q_num = 1; -static int ctx_num_per_q = 1; - -enum alg_op_type { - TRNG_GEN, - TRNG_AGEN, -}; - -struct trng_user_tag_info { - int pid; - int thread_id; -}; - -struct test_trng_pthread_dt { - int cpu_id; - int thread_num; - void *q; -}; - -static struct test_trng_pthread_dt test_thrds_data[TEST_MAX_THRD]; -static pthread_t system_test_thrds[TEST_MAX_THRD]; -static unsigned int g_input; - - -static inline int _get_cpu_id(int thr, __u64 core_mask) -{ - __u64 i; - int cnt = 0; - - for (i = 1; i < 64; i++) { - if (core_mask & (0x1ull << i)) { - if (thr == cnt) - return i; - cnt++; - } - } - - return 0; -} - -static inline int _get_one_bits(__u64 val) -{ - int count = 0; - - while (val) { - if (val % 2 == 1) - count++; - val = val / 2; - } - - return count; -} - -void *_trng_sys_test_thread(void *data) -{ - int ret, cpuid, i = 0; - struct test_trng_pthread_dt *pdata = data; - struct wcrypto_rng_ctx_setup setup; - struct wcrypto_rng_op_data opdata; - int pid = getpid(); - int thread_id = (int)syscall(__NR_gettid); - struct wd_queue *q; - int *out_data; - void *ctx = NULL; - void *tag = NULL; - - cpu_set_t mask; - CPU_ZERO(&mask); - cpuid = pdata->cpu_id; - q = pdata->q; - CPU_SET(cpuid, &mask); - - if (cpuid) { - ret = pthread_setaffinity_np(pthread_self(), - sizeof(mask), &mask); - if (ret < 0) { - RNG_TST_PRT("Proc-%d, thrd-%d:set affinity fail!\n", - pid, thread_id); - return NULL; - } - RNG_TST_PRT("Proc-%d, thrd-%d bind to cpu-%d!\n", - pid, thread_id, cpuid); - } - - memset(&setup, 0, sizeof(setup)); - memset(&opdata, 0, sizeof(opdata)); - ctx = wcrypto_create_rng_ctx(q, &setup); - if (!ctx) { - RNG_TST_PRT("Proc-%d, %d-TD:create %s ctx fail!\n", - pid, thread_id, q->capa.alg); - ret = -EINVAL; - goto fail_release; - } - - out_data = malloc(g_input); - if(!out_data) { - RNG_TST_PRT("malloc out_data memory fail!\n"); - } - RNG_TST_PRT("request queue fail5!\n"); - - while (1) { - opdata.in_bytes = g_input; - opdata.out = out_data; - ret = wcrypto_do_rng(ctx, &opdata, tag); - if (ret < 0) { - RNG_TST_PRT("Proc-%d, T-%d:trng %d fail!\n", pid, thread_id, i); - goto fail_release; - } - RNG_TST_PRT("the read data size %d!\n", opdata.out_bytes); - i++; - } -fail_release: - if (opdata.out) - free(opdata.out); - if (ctx) - wcrypto_del_rng_ctx(ctx); - return NULL; -} - - -static int trng_sys_test(int thread_num, __u64 lcore_mask, - __u64 hcore_mask) -{ - int i, ret, cnt = 0, j; - struct wd_queue *q; - int h_cpuid, qidx; - - q = malloc(q_num * sizeof(struct wd_queue)); - if (!q) { - RNG_TST_PRT("malloc q memory fail!\n"); - return -ENOMEM; - } - memset(q, 0, q_num * sizeof(struct wd_queue)); - - for (j = 0; j < q_num; j++) { - q[j].capa.alg = "trng"; - ret = wd_request_queue(&q[j]); - if (ret) { - RNG_TST_PRT("request queue %d fail!\n", j); - return ret; - } - } - RNG_TST_PRT("request queue fail!\n"); - if (_get_one_bits(lcore_mask) > 0) - cnt = _get_one_bits(lcore_mask); - else if (_get_one_bits(lcore_mask) == 0 && - _get_one_bits(hcore_mask) == 0) - cnt = thread_num; - - for (i = 0; i < cnt; i++) { - qidx = i / ctx_num_per_q; - test_thrds_data[i].q = &q[qidx]; - test_thrds_data[i].thread_num = thread_num; - test_thrds_data[i].cpu_id = _get_cpu_id(i, lcore_mask); - ret = pthread_create(&system_test_thrds[i], NULL, - _trng_sys_test_thread, &test_thrds_data[i]); - if (ret) { - RNG_TST_PRT("Create %dth thread fail!\n", i); - return ret; - } - } - RNG_TST_PRT("request queue fail2!\n"); - for (i = 0; i < thread_num - cnt; i++) { - h_cpuid = _get_cpu_id(i, hcore_mask); - if (h_cpuid > 0) - h_cpuid += 64; - - qidx = (i + cnt) / ctx_num_per_q; - test_thrds_data[i + cnt].q = &q[qidx]; - test_thrds_data[i + cnt].thread_num = thread_num; - test_thrds_data[i + cnt].cpu_id = h_cpuid; - ret = pthread_create(&system_test_thrds[i + cnt], NULL, - _trng_sys_test_thread, &test_thrds_data[i + cnt]); - if (ret) { - RNG_TST_PRT("Create %dth thread fail!\n", i); - return ret; - } - } - RNG_TST_PRT("request queue fail3!\n"); - for (i = 0; i < thread_num; i++) { - ret = pthread_join(system_test_thrds[i], NULL); - if (ret) { - RNG_TST_PRT("Join %dth thread fail!\n", i); - return ret; - } - } - free(q); - return 0; -} - - -static void _trng_cb(const void *message, void *tag) -{ - const struct wcrypto_rng_msg *msg = message; - struct trng_user_tag_info* pSwData = (struct trng_user_tag_info*)tag; - struct wcrypto_rng_op_data opdata; - int pid, threadId; - - if (NULL == pSwData) { - RNG_TST_PRT("pSwData NULL!\n"); - return; - } - memset(&opdata, 0, sizeof(opdata)); - - opdata.out = (void *)msg->out; - opdata.out_bytes = msg->out_bytes; - pid = pSwData->pid; - threadId = pSwData->thread_id; - RNG_TST_PRT("Proc-%d, %d-TD trng\n", pid, threadId); - RNG_TST_PRT("the random number size :%d\n", opdata.out_bytes); - - if (opdata.out) - free(opdata.out); - - if (pSwData) - free(pSwData); -} - -static void *_trng_asys_test_thread(void *data) -{ - int ret, cpuid; - struct test_trng_pthread_dt *pdata = data; - struct wd_queue *q = NULL; - cpu_set_t mask; - struct wcrypto_rng_ctx_setup setup; - struct wcrypto_rng_ctx *ctx = NULL; - struct trng_user_tag_info *tag = NULL; - struct wcrypto_rng_op_data opdata; - int pid = getpid(); - int thread_id = (int)syscall(__NR_gettid); - int *out_data; - int i = 0; - - CPU_ZERO(&mask); - cpuid = pdata->cpu_id; - q = (struct wd_queue *)pdata->q; - CPU_SET(cpuid, &mask); - - if (!q) { - RNG_TST_PRT("q null!\n"); - return NULL; - } - if (cpuid) { - ret = pthread_setaffinity_np(pthread_self(), - sizeof(mask), &mask); - if (ret < 0) { - RNG_TST_PRT("Proc-%d, thrd-%d:set affinity fail!\n", - pid, thread_id); - return NULL; - } - RNG_TST_PRT("Proc-%d, thrd-%d bind to cpu-%d!\n", - pid, thread_id, cpuid); - } - - q->capa.alg = "trng"; - memset(&setup, 0, sizeof(setup)); - memset(&opdata, 0, sizeof(opdata)); - setup.cb = _trng_cb; - ctx = wcrypto_create_rng_ctx(q, &setup); - if (!ctx) { - RNG_TST_PRT("Proc-%d, %d-TD:create %s ctx fail!\n", - pid, thread_id, q->capa.alg); - goto fail_release; - } - - while(1) { - tag = malloc(sizeof(struct trng_user_tag_info)); - if (!tag) { - RNG_TST_PRT("malloc tag fail!\n"); - goto fail_release; - } - - tag->pid = pid; - tag->thread_id = thread_id; - - out_data = malloc(g_input); - if(!out_data) { - RNG_TST_PRT("malloc fail\n"); - return 0; - } - - opdata.in_bytes = g_input; - opdata.out = out_data; - try_again: - ret = wcrypto_do_rng(ctx, &opdata, tag); - if (ret == -WD_EBUSY) { - usleep(100); - goto try_again; - } else if(ret) { - RNG_TST_PRT("Proc-%d, T-%d:trng %d fail!\n", pid, thread_id, i); - goto fail_release; - } - i++; - } -fail_release: - wcrypto_del_rng_ctx(ctx); - return NULL; -} - -static void* _trng_async_poll_test_thread(void *data) -{ - struct test_trng_pthread_dt *pdata = data; - struct wd_queue *q = pdata->q; - int ret, cpuid; - int pid = getpid(); - cpu_set_t mask; - int thread_id = (int)syscall(__NR_gettid); - - CPU_ZERO(&mask); - cpuid = pdata->cpu_id; - CPU_SET(cpuid, &mask); - if (cpuid) { - ret = pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask); - if (ret < 0) { - RNG_TST_PRT("Proc-%d, thrd-%d:set affinity fail!\n", - pid, thread_id); - return NULL; - } - RNG_TST_PRT("Proc-%d, poll thrd-%d bind to cpu-%d!\n", - pid, thread_id, cpuid); - } - - while (1) { - ret = wcrypto_rng_poll(q, 1); - if (ret < 0) { - break; - } - } - - return NULL; -} - -static int trng_asys_test(int thread_num, __u64 lcore_mask, __u64 hcore_mask) -{ - int i, ret, cnt = 0; - struct wd_queue q; - int h_cpuid; - - memset(&q, 0, sizeof(q)); - - q.capa.alg = "trng"; - ret = wd_request_queue(&q); - if (ret) { - RNG_TST_PRT("request queue fail!\n"); - return ret; - } - - if (_get_one_bits(lcore_mask) > 0) - cnt = _get_one_bits(lcore_mask); - else if (_get_one_bits(lcore_mask) == 0 && - _get_one_bits(hcore_mask) == 0) - cnt = thread_num; - - test_thrds_data[0].q= &q; - test_thrds_data[0].thread_num = 1; - test_thrds_data[0].cpu_id = _get_cpu_id(0, lcore_mask); - ret = pthread_create(&system_test_thrds[0], NULL, - _trng_async_poll_test_thread, &test_thrds_data[0]); - if (ret) { - RNG_TST_PRT("Create poll thread fail!\n"); - return ret; - } - - for (i = 1; i <= cnt; i++) { - test_thrds_data[i].q = &q; - test_thrds_data[i].thread_num = thread_num; - test_thrds_data[i].cpu_id = _get_cpu_id(i, lcore_mask); - ret = pthread_create(&system_test_thrds[i], NULL, - _trng_asys_test_thread, &test_thrds_data[i]); - if (ret) { - RNG_TST_PRT("Create %dth thread fail!\n", i); - return ret; - } - } - - for (i = 1; i <= thread_num - cnt; i++) { - h_cpuid = _get_cpu_id(i, hcore_mask); - if (h_cpuid > 0) - h_cpuid += 64; - test_thrds_data[i + cnt].q = &q; - test_thrds_data[i + cnt].thread_num = thread_num; - test_thrds_data[i + cnt].cpu_id = h_cpuid; - ret = pthread_create(&system_test_thrds[i + cnt], NULL, - _trng_asys_test_thread, &test_thrds_data[i + cnt]); - if (ret) { - RNG_TST_PRT("Create %dth thread fail!\n", i); - return ret; - } - } - - for (i = 0; i < thread_num; i++) { - ret = pthread_join(system_test_thrds[i], NULL); - if (ret) { - RNG_TST_PRT("Join %dth thread fail!\n", i); - return ret; - } - } - - wd_release_queue(&q); - return 0; - -} -int main(int argc, char *argv[]) -{ - struct wcrypto_rng_ctx *ctx; - struct wcrypto_rng_op_data opdata; - struct wcrypto_rng_ctx_setup setup; - enum alg_op_type alg_op_type = TRNG_GEN; - int thread_num, bits; - __u64 core_mask[2]; - struct wd_queue q; - void *tag = NULL; - int *data; - int ret; - int fd = -1; - int fd_w = -1; - if (!argv[1]) { - RNG_TST_PRT("pls printf the size of the random data!\n"); - return -WD_EINVAL; - } - g_input = (unsigned int)strtoul(argv[1], NULL, 10); - printf("g_input:%d\n",g_input); - //if (g_input <= 0){ - // printf("input error!\n"); - // return -WD_EINVAL; - //} - if (argv[2]) { - if(!strcmp(argv[2], "-system-gen")) { - alg_op_type = TRNG_GEN; - RNG_TST_PRT("Now doing system random number gen test!\n"); - } else if(!strcmp(argv[2], "-system-agen")) { - alg_op_type = TRNG_AGEN; - RNG_TST_PRT("Now doing system random number agen test!\n"); - } - - thread_num = strtoul((char *)argv[3], NULL, 10); - if (thread_num <= 0 || thread_num > TEST_MAX_THRD) { - RNG_TST_PRT("Invalid threads num:%d!\n", - thread_num); - RNG_TST_PRT("Now set threads num as 2\n"); - thread_num = 2; - } - - if (strcmp(argv[4], "-c")) { - RNG_TST_PRT("./test_hisi_trng --help get details\n"); - return -EINVAL; - } - if (argv[5][0] != '0' || argv[5][1] != 'x') { - RNG_TST_PRT("Err:coremask should be hex!\n"); - return -EINVAL; - } - - if (strlen(argv[5]) > 34) { - RNG_TST_PRT("Warning: coremask is cut!\n"); - argv[5][34] = 0; - } - - if (strlen(argv[5]) <= 18) { - core_mask[0] = strtoull(argv[5], NULL, 16); - if (core_mask[0] & 0x1) { - RNG_TST_PRT("Warn:cannot bind to core 0,\n"); - RNG_TST_PRT("now run without binding\n"); - core_mask[0] = 0x0; /* no binding */ - } - core_mask[1] = 0; - } else { - int offset = 0; - char *temp; - - offset = strlen(argv[5]) - 16; - core_mask[0] = strtoull(&argv[5][offset], NULL, 16); - if (core_mask[0] & 0x1) { - RNG_TST_PRT("Warn:cannot bind to core 0,\n"); - RNG_TST_PRT("now run without binding\n"); - core_mask[0] = 0x0; /* no binding */ - } - temp = malloc(64); - strcpy(temp, argv[5]); - temp[offset] = 0; - core_mask[1] = strtoull(temp, NULL, 16); - free(temp); - } - - bits = _get_one_bits(core_mask[0]); - bits += _get_one_bits(core_mask[1]); - if (thread_num > bits) { - RNG_TST_PRT("Coremask not covers all thrds,\n"); - RNG_TST_PRT("Bind first %d thrds!\n", bits); - } else if (thread_num < bits) { - RNG_TST_PRT("Coremask overflow,\n"); - RNG_TST_PRT("Just try to bind all thrds!\n"); - } - - if (argv[6]) { - ctx_num_per_q = strtoul(argv[6], NULL, 10); - if (ctx_num_per_q <= 0) { - RNG_TST_PRT("Invalid ctx num per queue:%s!\n", - argv[6]); - RNG_TST_PRT("Now ctx num per queue is set as 1!\n"); - ctx_num_per_q = 1; - } - } else { - RNG_TST_PRT("Now ctx num per queue is set as 1!\n"); - ctx_num_per_q = 1; - } - - q_num = (thread_num - 1) / ctx_num_per_q + 1; - - RNG_TST_PRT("Proc-%d: starts %d threads bind to %s\n", - getpid(), thread_num, argv[5]); - RNG_TST_PRT(" lcoremask=0x%llx, hcoremask=0x%llx\n", - core_mask[0], core_mask[1]); - if(alg_op_type == TRNG_GEN) - return trng_sys_test(thread_num, core_mask[0], - core_mask[1]); - - return trng_asys_test(thread_num, core_mask[0], - core_mask[1]); - } - - RNG_TST_PRT("Now try to get %d bytes random number.\n", g_input); - - data = malloc(g_input); - if (!data) { - RNG_TST_PRT("malloc data failed.\n"); - return -1; - } - - memset((void *)&q, 0, sizeof(q)); - memset(&setup, 0, sizeof(setup)); - memset(&opdata, 0, sizeof(opdata)); - - q.capa.alg = "trng"; - ret = wd_request_queue(&q); - if (ret) { - RNG_TST_PRT("request queue fail!\n"); - return ret; - } - ctx = wcrypto_create_rng_ctx(&q, &setup); - if (!ctx) { - ret = -ENOMEM; - RNG_TST_PRT("create trng ctx fail!\n"); - goto release_q; - } - - opdata.in_bytes = g_input; - opdata.out = data; - ret = wcrypto_do_rng(ctx, &opdata, tag); - if (ret != 1) { - RNG_TST_PRT("a wd_do_trng fail!\n"); - goto del_ctx; - } - - RNG_TST_PRT("random_data size= %d.\n", opdata.out_bytes); - fd_w = open ("/root/trng_file", O_RDWR|O_CREAT|O_TRUNC,0777); - if (fd_w <0 ) { - printf("can not open trng_file\n"); - return fd_w; - } - /*fd = open ("/dev/random", O_RDONLY); - if (fd <0 ) { - printf("can not open\n"); - return fd; - }*/ - /*ret = read(fd, data, g_input); - if (ret < 0) { - printf("read error %d\n", ret); - return ret; - }*/ - ret = write(fd_w,opdata.out,opdata.out_bytes); - if (ret < 0) { - printf("write error %d\n", ret); - return ret; - } - close(fd); - close(fd_w); -del_ctx: - wcrypto_del_rng_ctx(ctx); - -release_q: - wd_release_queue(&q); - free(data); - return ret; -} diff --git a/v1/wd.h b/v1/wd.h index 4618a8cb..90e2321a 100644 --- a/v1/wd.h +++ b/v1/wd.h @@ -181,7 +181,7 @@ struct wd_capa { * Other capabilities. * 0~15 bits: number of cookies that the user wants to allocate. * Optional, user can set value based on the number of requests and system memory, - * 1~1024 is valid. If the value is not set or invalid, the default value 64 (rng is 256) + * 1~1024 is valid. If the value is not set or invalid, the default value 64 * is used to initialize cookies. */ __u32 flags; diff --git a/v1/wd_adapter.c b/v1/wd_adapter.c index 07fe6591..f9d5f041 100644 --- a/v1/wd_adapter.c +++ b/v1/wd_adapter.c @@ -20,7 +20,6 @@ #include "v1/wd_util.h" #include "v1/drv/hisi_qm_udrv.h" -#include "v1/drv/hisi_rng_udrv.h" #include "v1/wd_adapter.h" #define __ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask)) @@ -86,12 +85,6 @@ static const struct wd_drv_dio_if hw_dio_tbl[] = { { .init_sgl = qm_init_hwsgl_mem, .uninit_sgl = qm_uninit_hwsgl_mem, .sgl_merge = qm_merge_hwsgl, - }, { - .hw_type = "hisi-trng-v2", - .open = rng_init_queue, - .close = rng_uninit_queue, - .send = rng_send, - .recv = rng_recv, }, }; diff --git a/v1/wd_rng.c b/v1/wd_rng.c deleted file mode 100644 index 7a89cd15..00000000 --- a/v1/wd_rng.c +++ /dev/null @@ -1,296 +0,0 @@ -/* - * Copyright 2019 Huawei Technologies Co.,Ltd.All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include <stdlib.h> -#include <string.h> -#include <unistd.h> -#include <errno.h> - -#include <sys/types.h> -#include <sys/mman.h> - -#include "wd.h" -#include "wd_util.h" -#include "wd_rng.h" - -#define RNG_RESEND_CNT 8 -#define RNG_RECV_CNT 8 -#define WD_RNG_CTX_COOKIE_NUM 256 - -struct wcrypto_rng_cookie { - struct wcrypto_cb_tag tag; - struct wcrypto_rng_msg msg; -}; - -struct wcrypto_rng_ctx { - struct wd_cookie_pool pool; - unsigned long ctx_id; - struct wd_queue *q; - struct wcrypto_rng_ctx_setup setup; -}; - -static int wcrypto_setup_qinfo(struct wcrypto_rng_ctx_setup *setup, - struct wd_queue *q, __u32 *ctx_id) -{ - struct q_info *qinfo; - int ret = -WD_EINVAL; - - if (!q || !q->qinfo || !setup) { - WD_ERR("input parameter err!\n"); - return ret; - } - - if (strcmp(q->capa.alg, "trng")) { - WD_ERR("algorithm mismatch!\n"); - return ret; - } - qinfo = q->qinfo; - /* lock at ctx creating */ - wd_spinlock(&qinfo->qlock); - if (qinfo->ctx_num >= WD_MAX_CTX_NUM) { - WD_ERR("create too many trng ctx!\n"); - goto unlock; - } - - ret = wd_alloc_id(qinfo->ctx_id, WD_MAX_CTX_NUM, ctx_id, 0, - WD_MAX_CTX_NUM); - if (ret) { - WD_ERR("err: alloc ctx id fail!\n"); - goto unlock; - } - qinfo->ctx_num++; - ret = WD_SUCCESS; -unlock: - wd_unspinlock(&qinfo->qlock); - return ret; -} - -void *wcrypto_create_rng_ctx(struct wd_queue *q, - struct wcrypto_rng_ctx_setup *setup) -{ - struct wcrypto_rng_cookie *cookie; - struct wcrypto_rng_ctx *ctx; - struct q_info *qinfo; - __u32 cookies_num, i; - __u32 ctx_id = 0; - int ret; - - if (wcrypto_setup_qinfo(setup, q, &ctx_id)) - return NULL; - - ctx = calloc(1, sizeof(struct wcrypto_rng_ctx)); - if (!ctx) { - WD_ERR("alloc ctx memory fail!\n"); - goto free_ctx_id; - } - memcpy(&ctx->setup, setup, sizeof(*setup)); - ctx->q = q; - ctx->ctx_id = ctx_id + 1; - - cookies_num = wd_get_ctx_cookies_num(q->capa.flags, WD_RNG_CTX_COOKIE_NUM); - ret = wd_init_cookie_pool(&ctx->pool, - sizeof(struct wcrypto_rng_cookie), cookies_num); - if (ret) { - WD_ERR("fail to init cookie pool!\n"); - free(ctx); - goto free_ctx_id; - } - for (i = 0; i < cookies_num; i++) { - cookie = (void *)((uintptr_t)ctx->pool.cookies + - i * ctx->pool.cookies_size); - cookie->msg.alg_type = WCRYPTO_RNG; - cookie->tag.ctx = ctx; - cookie->tag.ctx_id = ctx->ctx_id; - cookie->msg.usr_tag = (uintptr_t)&cookie->tag; - } - - return ctx; - -free_ctx_id: - qinfo = q->qinfo; - wd_spinlock(&qinfo->qlock); - qinfo->ctx_num--; - wd_free_id(qinfo->ctx_id, WD_MAX_CTX_NUM, ctx_id, WD_MAX_CTX_NUM); - wd_unspinlock(&qinfo->qlock); - - return NULL; -} - -void wcrypto_del_rng_ctx(void *ctx) -{ - struct wcrypto_rng_ctx *cx; - struct q_info *qinfo; - - if (!ctx) { - WD_ERR("delete trng ctx is NULL!\n"); - return; - } - - cx = ctx; - qinfo = cx->q->qinfo; - - wd_uninit_cookie_pool(&cx->pool); - wd_spinlock(&qinfo->qlock); - if (qinfo->ctx_num <= 0) { - wd_unspinlock(&qinfo->qlock); - WD_ERR("repeat delete trng ctx!\n"); - return; - } - qinfo->ctx_num--; - wd_free_id(qinfo->ctx_id, WD_MAX_CTX_NUM, cx->ctx_id - 1, - WD_MAX_CTX_NUM); - wd_unspinlock(&qinfo->qlock); - - free(ctx); -} - -int wcrypto_rng_poll(struct wd_queue *q, unsigned int num) -{ - struct wcrypto_rng_msg *resp = NULL; - struct wcrypto_rng_ctx *ctx; - struct wcrypto_cb_tag *tag; - unsigned int tmp = num; - int count = 0; - int ret; - - if (!q) { - WD_ERR("%s(): input parameter err!\n", __func__); - return -WD_EINVAL; - } - - do { - ret = wd_recv(q, (void **)&resp); - if (!ret) - break; - - if (ret < 0) { - WD_ERR("recv err at trng poll!\n"); - return ret; - } - - count++; - tag = (void *)(uintptr_t)resp->usr_tag; - ctx = tag->ctx; - ctx->setup.cb(resp, tag->tag); - wd_put_cookies(&ctx->pool, (void **)&tag, 1); - resp = NULL; - } while (--tmp); - - return count; -} - -static int wcrypto_do_prepare(struct wcrypto_rng_cookie **cookie_addr, - struct wcrypto_rng_op_data *opdata, - struct wcrypto_rng_msg **req_addr, - struct wcrypto_rng_ctx *ctxt, - void *tag) -{ - struct wcrypto_rng_cookie *cookie; - struct wcrypto_rng_msg *req; - int ret; - - if (unlikely(!ctxt || !opdata)) { - WD_ERR("invalid: rng input parameter err!\n"); - return -WD_EINVAL; - } - - if (unlikely((opdata->in_bytes && !opdata->out))) { - WD_ERR("invalid: dst addr is NULL when in_bytes is non-zero!!\n"); - return -WD_EINVAL; - } - - ret = wd_get_cookies(&ctxt->pool, (void **)&cookie, 1); - if (ret) - return ret; - - if (tag) { - if (!ctxt->setup.cb) { - WD_ERR("invalid: ctx call back is null!\n"); - wd_put_cookies(&ctxt->pool, (void **)&cookie, 1); - return -WD_EINVAL; - } - cookie->tag.tag = tag; - } - - req = &cookie->msg; - req->in_bytes = opdata->in_bytes; - req->out = opdata->out; - *cookie_addr = cookie; - *req_addr = req; - - return 0; -} - -int wcrypto_do_rng(void *ctx, struct wcrypto_rng_op_data *opdata, void *tag) -{ - struct wcrypto_rng_ctx *ctxt = ctx; - struct wcrypto_rng_cookie *cookie; - struct wcrypto_rng_msg *req; - struct wcrypto_rng_msg *resp; - uint32_t tx_cnt = 0; - uint32_t rx_cnt = 0; - int ret = 0; - - ret = wcrypto_do_prepare(&cookie, opdata, &req, ctxt, tag); - if (ret) - return ret; - - do { - ret = wd_send(ctxt->q, req); - if (!ret) { - break; - } else if (ret == -WD_EBUSY) { - if (++tx_cnt > RNG_RESEND_CNT) { - WD_ERR("do trng send cnt %u, exit!\n", tx_cnt); - goto fail_with_cookie; - } - - usleep(1); - } else { - WD_ERR("do rng wd_send err!\n"); - goto fail_with_cookie; - } - } while (true); - - if (tag) - return ret; - - resp = (void *)(uintptr_t)ctxt->ctx_id; - - do { - ret = wd_recv(ctxt->q, (void **)&resp); - if (ret > 0) { - break; - } else if (!ret) { - if (++rx_cnt > RNG_RECV_CNT) { - WD_ERR("do trng recv cnt %u, exit!\n", rx_cnt); - ret = -WD_ETIMEDOUT; - goto fail_with_cookie; - } - - usleep(1); - } else { - WD_ERR("do trng recv err!\n"); - goto fail_with_cookie; - } - } while (true); - - opdata->out_bytes = resp->out_bytes; - ret = WD_SUCCESS; -fail_with_cookie: - wd_put_cookies(&ctxt->pool, (void **)&cookie, 1); - return ret; -} diff --git a/v1/wd_rng.h b/v1/wd_rng.h deleted file mode 100644 index fcde26d7..00000000 --- a/v1/wd_rng.h +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Copyright 2019 Huawei Technologies Co.,Ltd.All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __WD_RNG_H -#define __WD_RNG_H - -#include "wd.h" -#include "wd_digest.h" -#include "wd_cipher.h" - -#ifdef __cplusplus -extern "C" { -#endif - -struct wcrypto_rng_ctx_setup { - wcrypto_cb cb; - __u16 data_fmt; /* Data format, denoted by enum wd_buff_type */ - enum wcrypto_type type; /* Please refer to the definition of enum */ - enum wcrypto_cipher_alg calg; /* DRBG cipher algorithm */ - enum wcrypto_cipher_mode cmode; /* DRBG cipher mode */ - enum wcrypto_digest_alg dalg; /* DRBG digest algorithm */ - enum wcrypto_digest_mode dmode; /* DRBG digest mode */ -}; - -struct wcrypto_rng_msg { - __u8 alg_type; /* Denoted by enum wcrypto_type */ - __u8 op_type; /* Denoted by enum wcrypto_rng_op_type */ - __u8 data_fmt; /* Data format, denoted by enum wd_buff_type */ - __u8 result; /* Data format, denoted by WD error code */ - __u8 *out; /* Result address */ - __u8 *in; /* Input address */ - __u32 out_bytes; /* output bytes */ - __u32 in_bytes; /* input bytes */ - __u64 usr_tag; /* user identifier */ -}; - -enum wcrypto_rng_op_type { - WCRYPTO_RNG_INVALID, /* Invalid RNG operational type */ - WCRYPTO_DRBG_RESEED, /* seed operation */ - WCRYPTO_DRBG_GEN, /* deterministic random number generation */ - WCRYPTO_TRNG_GEN, /* true random number generation */ -}; - -struct wcrypto_rng_op_data { - enum wcrypto_rng_op_type op_type; - __u32 status; /* Operation result status */ - void *in; /* input */ - void *out; /* output */ - __u32 in_bytes; /* input bytes */ - __u32 out_bytes; /* output bytes */ -}; - -void *wcrypto_create_rng_ctx(struct wd_queue *q, - struct wcrypto_rng_ctx_setup *setup); -void wcrypto_del_rng_ctx(void *ctx); -int wcrypto_do_rng(void *ctx, struct wcrypto_rng_op_data *opdata, void *tag); -int wcrypto_rng_poll(struct wd_queue *q, unsigned int num); - -#ifdef __cplusplus -} -#endif - -#endif -- 2.33.0