Add aead test
build:
./autogen.sh;
./conf.sh;
make -j;
test:
while true;do strace -o /root/log ./uadk_tool test --m sec --aead 0 --sync --optype 0 --pktlen 16 --keylen 16 --times 1 --multi 1; cat /root/log|grep "222222" && exit ;done
Signed-off-by: Wenkai Lin <linwenkai6(a)hisilicon.com>
---
 .gitignore                |   1 +
 Makefile.am               |   2 +-
 uadk_tool/test/test_sec.c | 197 ++++++++++++++++++--------------------
 3 files changed, 96 insertions(+), 104 deletions(-)
diff --git a/.gitignore b/.gitignore
index 6a1eb49..4d90d26 100644
--- a/.gitignore
+++ b/.gitignore
@@ -176,6 +176,7 @@ v1/test/hisi_zip_test/wd_zip_test
 v1/test/hisi_zip_test_sgl/sgl_test
 v1/test/hisi_zip_test_sgl/wd_zip_test_sgl
 v1/test/test_mm/test_wd_mem
+v1/test
 *.gcda
 *.ut
 *.eml
diff --git a/Makefile.am b/Makefile.am
index d81e8cc..e574c43 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,6 +1,6 @@
 ACLOCAL_AMFLAGS = -I m4 -I./include
 AUTOMAKE_OPTIONS = foreign subdir-objects
-AM_CFLAGS=-Wall -Werror -fno-strict-aliasing -I$(top_srcdir)/include
+AM_CFLAGS=-fPIC -Wall -Werror -fno-strict-aliasing -I$(top_srcdir)/include
 CLEANFILES =
 
 if WITH_LOG_FILE
diff --git a/uadk_tool/test/test_sec.c b/uadk_tool/test/test_sec.c
index b00a933..53051de 100644
--- a/uadk_tool/test/test_sec.c
+++ b/uadk_tool/test/test_sec.c
@@ -11,6 +11,7 @@
 #include <sys/time.h>
 #include <getopt.h>
 #include <numa.h>
+#include <stdarg.h>
 
 #include "sec_template_tv.h"
 #include "test_sec.h"
@@ -2880,6 +2881,39 @@ int get_aead_resource(struct aead_testvec **alg_tv,
 	return 0;
 }
 
+
+/* Allocates memory and generates random data. */
+int v2_key_from_random(int cnt, ...)
+{
+	srand((unsigned)time(NULL) * rand());
+        int k = 0;
+        unsigned int i;
+        va_list p_args;
+        va_start(p_args, cnt);
+        unsigned char **dst;
+        unsigned char *src;
+        unsigned int size;
+
+        while (k < cnt / 2) {
+                dst = va_arg(p_args,
+                unsigned char **);
+                size = va_arg(p_args,
+                unsigned int);
+                src = malloc(size);
+                *dst = src;
+                memset(src, 0, size);
+                for (i = 0; i < size; i++) {
+			src[i] = rand();
+                }
+                if (*dst == NULL) {
+                        return -1;
+                }
+                k++;
+        }
+        va_end(p_args);
+        return 0;
+}
+
 static int sec_aead_sync_once(void)
 {
 	struct wd_aead_sess_setup setup = {0};
@@ -2892,11 +2926,11 @@ static int sec_aead_sync_once(void)
 	float speed, time_used;
 	unsigned long cnt = g_times;
 	__u16 mac_bytes;
-	__u16 auth_size;
-	__u16 in_size;
+	__u32 in_size;
 	__u16 iv_len;
 	int ret;
-	size_t unit_sz;
+	struct sched_params param = {0};
+	void *tmp_src = NULL, *tmp_dst = NULL;
 
 	/* config setup */
 	ret = init_aead_ctx_config(CTX_TYPE_ENCRYPT, CTX_MODE_SYNC);
@@ -2914,60 +2948,35 @@ static int sec_aead_sync_once(void)
 		return ret;
 	}
 
+	setup.sched_param = ¶m;
 	h_sess = wd_aead_alloc_sess(&setup);
 	if (!h_sess) {
 		ret = -EINVAL;
 		goto out;
 	}
 
-	/* should set key */
-	dump_mem("req src key--->:", WD_FLAT_BUF, (void *)tv->key, tv->klen);
+	unsigned char *input = NULL, *iv = NULL, *key = NULL, *assoc = NULL;
+	unsigned int key_size = 16;
+	unsigned int iv_size = 16;
+	unsigned int input_size = 1048576;
+	unsigned int assoc_size = 65279;
+	ret = v2_key_from_random(8, &input, input_size, &iv, iv_size, &key, key_size, &assoc, assoc_size);
+
 	if (setup.cmode == WD_CIPHER_CCM || setup.cmode == WD_CIPHER_GCM) {
-		ret = wd_aead_set_ckey(h_sess, (const __u8*)tv->key, tv->klen);
+		ret = wd_aead_set_ckey(h_sess, (const __u8*)key, 16);
 		if (ret) {
 			SEC_TST_PRT("aead sess set key failed!\n");
 			goto out_key;
 		}
-	} else {
-		// AEAD template's cipher key is the tail data
-		ret = wd_aead_set_ckey(h_sess, (__u8*)tv->key + 0x28, 0x10);
-		if (ret) {
-			SEC_TST_PRT("set cipher key fail!\n");
-			goto out_key;
-		}
-		// AEAD template's auth key is the mid data
-		ret = wd_aead_set_akey(h_sess, (__u8*)tv->key + 0x08, 0x20);
-		if (ret) {
-			SEC_TST_PRT("set auth key fail!\n");
-			goto out_key;
-		}
 	}
 
-	auth_size = (__u16)(tv->clen - tv->plen);
-	mac_bytes = auth_size;
-	ret = wd_aead_set_authsize(h_sess, auth_size);
+	mac_bytes = 16;
+	ret = wd_aead_set_authsize(h_sess, mac_bytes);
 	if (ret) {
-		SEC_TST_PRT("set auth size fail, authsize: %u\n", auth_size);
+		SEC_TST_PRT("set auth size fail, authsize: %u\n", mac_bytes);
 		goto out_key;
 	}
 
-	// test the auth size
-	ret = wd_aead_get_authsize(h_sess);
-	if (ret != auth_size) {
-		SEC_TST_PRT("get auth size fail!\n");
-		goto out_key;
-	}
-
-	ret = wd_aead_get_maxauthsize(h_sess);
-	if (ret < auth_size) {
-		SEC_TST_PRT("get max auth size fail!\n");
-		goto out_key;
-	}
-	SEC_TST_PRT("aead get max auth size: %u\n", ret);
-
-	if (setup.cmode == WD_CIPHER_CCM || setup.cmode == WD_CIPHER_GCM)
-		mac_bytes = 16;
-
 	req.mac = malloc(mac_bytes);
 	if (!req.mac) {
 		SEC_TST_PRT("req iv mem malloc failed!\n");
@@ -2977,68 +2986,23 @@ static int sec_aead_sync_once(void)
 	memset(req.mac, 0, mac_bytes);
 	req.mac_bytes = mac_bytes;
 
-	if (g_direction == 0)
-		req.op_type = WD_CIPHER_ENCRYPTION_DIGEST;
-	else
-		req.op_type = WD_CIPHER_DECRYPTION_DIGEST;
-
-	req.assoc_bytes = tv->alen;
-	if (g_direction == 0) {
-		in_size = req.assoc_bytes + tv->plen;
-	} else {
-		in_size = req.assoc_bytes + tv->clen;
-	}
-	if (in_size > BUFF_SIZE) {
-		SEC_TST_PRT("alloc in buffer block size too small!\n");
-		goto out_mac;
-	}
-	unit_sz = cal_unit_sz(in_size, g_sgl_num);
-	void *src  = create_buf(WD_FLAT_BUF, in_size, unit_sz);
-	if (!src) {
-		ret = -ENOMEM;
-		goto out_src;
-	}
-
-	memset(src, 0, in_size);
-	// copy the assoc data in the front of in data
-	SEC_TST_PRT("aead set assoc_bytes: %u\n", req.assoc_bytes);
-	if (g_direction == 0) {
-		memcpy(src, tv->assoc, tv->alen);
-		memcpy((src + req.assoc_bytes), tv->ptext, tv->plen);
-		req.in_bytes = tv->plen;
-	} else {
-		memcpy(src, tv->assoc, tv->alen);
-		memcpy(src + req.assoc_bytes, tv->ctext, tv->clen - auth_size);
-		req.in_bytes = tv->clen - auth_size;
-		memcpy(req.mac, tv->ctext + tv->clen - auth_size, auth_size);
-	}
-
-	dump_mem("mac addr src is:", 0, req.mac, auth_size);
-
-	req.src = create_buf(g_data_fmt, in_size, unit_sz);
+	in_size = 1113855;
+	req.src = malloc(in_size);
 	if (!req.src) {
 		ret = -ENOMEM;
 		goto out_src;
 	}
-	copy_mem(g_data_fmt, req.src, WD_FLAT_BUF, src,
-			(size_t)(req.in_bytes + tv->alen));
-	free(src);
-	SEC_TST_PRT("aead req src len: %u\n", tv->alen + req.in_bytes);
-	dump_mem("aead req src in--->", g_data_fmt, req.src, tv->alen + req.in_bytes);
 
-	if (g_direction == 0) {
-		req.out_bytes = req.assoc_bytes + tv->clen - auth_size;
-	} else {
-		req.out_bytes = req.assoc_bytes + tv->plen;
-	}
+	memcpy(req.src, assoc, assoc_size);
+	memcpy(req.src + assoc_size, input, input_size);
 
-	// alloc out buffer memory
-	unit_sz = cal_unit_sz(req.out_bytes, g_sgl_num);
-	req.dst = create_buf(g_data_fmt, req.out_bytes, unit_sz);
+	req.out_bytes = 1113855;
+	req.dst = malloc(req.out_bytes);
 	if (!req.dst) {
 		ret = -ENOMEM;
 		goto out_dst;
 	}
+	memset(req.dst, 0, req.out_bytes);
 
 	req.iv = malloc(AES_BLOCK_SIZE);
 	if (!req.iv) {
@@ -3046,18 +3010,42 @@ static int sec_aead_sync_once(void)
 		ret = -ENOMEM;
 		goto out_iv;
 	}
-	if (setup.cmode == WD_CIPHER_GCM)
-		iv_len = GCM_IV_SIZE;
-	else
-		iv_len = AES_BLOCK_SIZE;
+
+	iv_len = 16;
 	req.iv_bytes = iv_len;
-	memcpy(req.iv, tv->iv, iv_len);
+	memcpy(req.iv, iv, 16);
 
 	req.data_fmt = g_data_fmt;
 
 	gettimeofday(&start_tval, NULL);
 	while (cnt) {
+		/* do encrypt first, device use req.src and req.dst to read and write data */
+		req.op_type = WD_CIPHER_ENCRYPTION_DIGEST;
+		req.assoc_bytes = 65279;
+		req.out_bytes = 1113855;
+		req.in_bytes = 1048576;
 		ret = wd_do_aead_sync(h_sess, &req);
+		if (ret || req.state) {
+			SEC_TST_PRT("wd_do_aead_sync 111111111 failed!!!!!!!!!!!!\n");
+			goto out_job;
+		}
+
+		/* do decrypt, need to do much times memcpy here */
+		tmp_src = malloc(req.out_bytes);
+		tmp_dst = malloc(req.out_bytes);
+		memcpy(tmp_src, req.src, req.out_bytes);
+		memcpy(tmp_dst, req.dst, req.out_bytes);
+		memcpy(req.iv, iv, 16);
+		memcpy(req.src, tmp_dst, req.out_bytes);
+		req.op_type = WD_CIPHER_DECRYPTION_DIGEST;
+
+		ret = wd_do_aead_sync(h_sess, &req);
+		memcpy(tmp_dst, req.dst + req.assoc_bytes, req.in_bytes);
+		if (ret || req.state || memcmp(tmp_dst, tmp_src + req.assoc_bytes, req.in_bytes)) {
+			WD_CONSOLE("wd_do_aead_sync 222222222 failed!!!!!!!!!!!!\n");
+			goto out_job;
+		}
+
 		cnt--;
 	}
 	gettimeofday(&cur_tval, NULL);
@@ -3070,16 +3058,19 @@ static int sec_aead_sync_once(void)
 	SEC_TST_PRT("Pro-%d, thread_id-%d, speed:%0.3f ops, Perf: %ld KB/s\n", getpid(),
 			(int)syscall(__NR_gettid), speed, Perf);
 
-	dump_mem("aead dump out addr is:", g_data_fmt, req.dst, req.out_bytes);
-	dump_mem("aead dump mac addr is:", 0, req.mac, auth_size);
-
+out_job:
+	free(input);
+	free(assoc);
+	free(iv);
+	free(key);
+	free(tmp_dst);
+	free(tmp_src);
 	free(req.iv);
 out_iv:
-	free_buf(g_data_fmt, req.dst);
+	free(req.dst);
 out_dst:
-	free_buf(g_data_fmt, req.src);
+	free(req.src);
 out_src:
-out_mac:
 	free(req.mac);
 out_key:
 	wd_aead_free_sess(h_sess);
-- 
2.30.0