xtest:Rename --sha-perf to --hash-perf and add SM3

Rename --sha-perf to --hash-perf and add SM3

Signed-off-by: yuzexi <yuzexi@hisilicon.com>
Reviewed-by: Xiaoxu Zeng <zengxiaoxu@huawei.com>
Reviewed-by: Jerome Forissier <jerome.forissier@linaro.org>
Reviewed-by: Etienne Carriere <etienne.carriere@linaro.org>
Reviewed-by: wangyuan <wangyuan46@huawei.com>
diff --git a/Android.mk b/Android.mk
index fe6de43..feb05c6 100644
--- a/Android.mk
+++ b/Android.mk
@@ -51,7 +51,7 @@
 	regression_6000.c \
 	regression_8000.c \
 	regression_8100.c \
-	sha_perf.c \
+	hash_perf.c \
 	stats.c \
 	xtest_helpers.c \
 	xtest_main.c \
@@ -107,7 +107,7 @@
 		$(LOCAL_PATH)/ta/storage_benchmark/include \
 		$(LOCAL_PATH)/ta/concurrent/include \
 		$(LOCAL_PATH)/ta/concurrent_large/include \
-		$(LOCAL_PATH)/ta/sha_perf/include \
+		$(LOCAL_PATH)/ta/hash_perf/include \
 		$(LOCAL_PATH)/ta/aes_perf/include \
 		$(LOCAL_PATH)/ta/socket/include \
 		$(LOCAL_PATH)/ta/sdp_basic/include \
diff --git a/host/xtest/CMakeLists.txt b/host/xtest/CMakeLists.txt
index 320d336..776787b 100644
--- a/host/xtest/CMakeLists.txt
+++ b/host/xtest/CMakeLists.txt
@@ -55,7 +55,7 @@
 	regression_6000.c
 	regression_8000.c
 	regression_8100.c
-	sha_perf.c
+	hash_perf.c
 	stats.c
 	xtest_helpers.c
 	xtest_main.c
diff --git a/host/xtest/Makefile b/host/xtest/Makefile
index 36c7e3d..8505629 100644
--- a/host/xtest/Makefile
+++ b/host/xtest/Makefile
@@ -76,7 +76,7 @@
 	regression_6000.c \
 	regression_8000.c \
 	regression_8100.c \
-	sha_perf.c \
+	hash_perf.c \
 	stats.c \
 	xtest_helpers.c \
 	xtest_main.c \
@@ -129,7 +129,7 @@
 CFLAGS += -I../../ta/storage_benchmark/include
 CFLAGS += -I../../ta/concurrent/include
 CFLAGS += -I../../ta/concurrent_large/include
-CFLAGS += -I../../ta/sha_perf/include
+CFLAGS += -I../../ta/hash_perf/include
 CFLAGS += -I../../ta/aes_perf/include
 CFLAGS += -I../../ta/socket/include
 CFLAGS += -I../../ta/sdp_basic/include
diff --git a/host/xtest/benchmark_2000.c b/host/xtest/benchmark_2000.c
index 83f3ea9..da9db74 100644
--- a/host/xtest/benchmark_2000.c
+++ b/host/xtest/benchmark_2000.c
@@ -33,7 +33,7 @@
 	size_t size = 1024;	/* Buffer size */
 	int offset = 0;          /* Buffer offset wrt. alloc'ed address */
 
-	sha_perf_run_test(algo, size, CRYPTO_DEF_COUNT,
+	hash_perf_run_test(algo, size, CRYPTO_DEF_COUNT,
 				CRYPTO_DEF_LOOPS, CRYPTO_USE_RANDOM, offset,
 				CRYPTO_DEF_WARMUP, CRYPTO_DEF_VERBOSITY);
 
@@ -47,7 +47,7 @@
 	size_t size = 4096;	/* Buffer size */
 	int offset = 0;          /* Buffer offset wrt. alloc'ed address */
 
-	sha_perf_run_test(algo, size, CRYPTO_DEF_COUNT,
+	hash_perf_run_test(algo, size, CRYPTO_DEF_COUNT,
 				CRYPTO_DEF_LOOPS, CRYPTO_USE_RANDOM, offset,
 				CRYPTO_DEF_WARMUP, CRYPTO_DEF_VERBOSITY);
 
diff --git a/host/xtest/crypto_common.h b/host/xtest/crypto_common.h
index 9117c90..68debea 100644
--- a/host/xtest/crypto_common.h
+++ b/host/xtest/crypto_common.h
@@ -8,9 +8,7 @@
 #define XTEST_CRYPTO_COMMON_H
 
 #include "ta_aes_perf.h"
-#include "ta_sha_perf.h"
-
-
+#include "ta_hash_perf.h"
 
 
 #define AES_PERF_INPLACE 0
@@ -44,8 +42,8 @@
 		       size_t unit, unsigned int n, unsigned int l,
 		       int random_in, int in_place, int warmup, int verbosity);
 
-int sha_perf_runner_cmd_parser(int argc, char *argv[]);
-void sha_perf_run_test(int algo, size_t size, unsigned int n,
+int hash_perf_runner_cmd_parser(int argc, char *argv[]);
+void hash_perf_run_test(int algo, size_t size, unsigned int n,
 				unsigned int l, int random_in, int offset,
 				int warmup, int verbosity);
 
diff --git a/host/xtest/sha_perf.c b/host/xtest/hash_perf.c
similarity index 93%
rename from host/xtest/sha_perf.c
rename to host/xtest/hash_perf.c
index c9656d0..baeb5cc 100644
--- a/host/xtest/sha_perf.c
+++ b/host/xtest/hash_perf.c
@@ -52,7 +52,7 @@
 static void open_ta(void)
 {
 	TEEC_Result res = TEEC_ERROR_GENERIC;
-	TEEC_UUID uuid = TA_SHA_PERF_UUID;
+	TEEC_UUID uuid = TA_HASH_PERF_UUID;
 	uint32_t err_origin = 0;
 
 	res = TEEC_InitializeContext(NULL, &ctx);
@@ -118,6 +118,8 @@
 		return "SHA384";
 	case TA_SHA_SHA512:
 		return "SHA512";
+	case TA_SM3:
+		return "SM3";
 	default:
 		return "???";
 	}
@@ -136,6 +138,8 @@
 		return 48;
 	case TA_SHA_SHA512:
 		return 64;
+	case TA_SM3:
+		return 32;
 	default:
 		return 0;
 	}
@@ -144,7 +148,6 @@
 #define _TO_STR(x) #x
 #define TO_STR(x) _TO_STR(x)
 
-
 static void alloc_shm(size_t sz, uint32_t algo, int offset)
 {
 	TEEC_Result res = TEEC_ERROR_GENERIC;
@@ -213,7 +216,7 @@
 	return ns;
 }
 
-static uint64_t run_test_once(void *in, size_t size,  int random_in,
+static uint64_t run_test_once(void *in, size_t size, int random_in,
 			      TEEC_Operation *op)
 {
 	struct timespec t0 = { };
@@ -225,7 +228,7 @@
 		read_random(in, size);
 
 	get_current_time(&t0);
-	res = TEEC_InvokeCommand(&sess, TA_SHA_PERF_CMD_PROCESS, op,
+	res = TEEC_InvokeCommand(&sess, TA_HASH_PERF_CMD_PROCESS, op,
 				 &ret_origin);
 	check_res(res, "TEEC_InvokeCommand", &ret_origin);
 	get_current_time(&t1);
@@ -242,7 +245,7 @@
 	op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE,
 					 TEEC_NONE, TEEC_NONE);
 	op.params[0].value.a = algo;
-	res = TEEC_InvokeCommand(&sess, TA_SHA_PERF_CMD_PREPARE_OP, &op,
+	res = TEEC_InvokeCommand(&sess, TA_HASH_PERF_CMD_PREPARE_OP, &op,
 				 &ret_origin);
 	check_res(res, "TEEC_InvokeCommand", &ret_origin);
 }
@@ -283,7 +286,7 @@
  * warmup - Start with a-second busy loop
  * verbosity - Verbosity level
  * */
-extern void sha_perf_run_test(int algo, size_t size, unsigned int n,
+extern void hash_perf_run_test(int algo, size_t size, unsigned int n,
 				unsigned int l, int random_in, int offset,
 				int warmup, int verbosity)
 {
@@ -294,7 +297,7 @@
 	struct timespec ts = { };
 	double sd = 0;
 
-	vverbose("sha-perf\n");
+	vverbose("hash-perf\n");
 	if (clock_getres(CLOCK_MONOTONIC, &ts) < 0) {
 		perror("clock_getres");
 		return;
@@ -357,10 +360,10 @@
 	fprintf(stderr, "Usage: %s [-h]\n", progname);
 	fprintf(stderr, "Usage: %s [-a ALGO] [-l LOOP] [-n LOOP] [-r] [-s SIZE]", progname);
 	fprintf(stderr, " [-v [-v]] [-w SEC]\n");
-	fprintf(stderr, "SHA performance testing tool for OP-TEE\n");
+	fprintf(stderr, "Hash performance testing tool for OP-TEE\n");
 	fprintf(stderr, "\n");
 	fprintf(stderr, "Options:\n");
-	fprintf(stderr, "  -a ALGO          Algorithm (SHA1, SHA224, SHA256, SHA384, SHA512) [%s]\n", algo_str(algo));
+	fprintf(stderr, "  -a ALGO          Algorithm (SHA1, SHA224, SHA256, SHA384, SHA512, SM3) [%s]\n", algo_str(algo));
 	fprintf(stderr, "  -h|--help Print this help and exit\n");
 	fprintf(stderr, "  -l LOOP          Inner loop iterations (TA calls TEE_DigestDoFinal() <x> times) [%u]\n", l);
 	fprintf(stderr, "  -n LOOP          Outer test loop iterations [%u]\n", n);
@@ -381,9 +384,7 @@
 		} \
 	} while (0);
 
-
-
-extern int sha_perf_runner_cmd_parser(int argc, char *argv[])
+extern int hash_perf_runner_cmd_parser(int argc, char *argv[])
 {
 	int i = 0;
 	/* Command line params */
@@ -421,6 +422,8 @@
 				algo = TA_SHA_SHA384;
 			else if (!strcasecmp(argv[i], "SHA512"))
 				algo = TA_SHA_SHA512;
+			else if (!strcasecmp(argv[i], "SM3"))
+				algo = TA_SM3;
 			else {
 				fprintf(stderr, "%s, invalid algorithm\n",
 					argv[0]);
@@ -453,7 +456,7 @@
 		}
 	}
 
-	sha_perf_run_test(algo, size, n, l, random_in, offset, warmup, verbosity);
+	hash_perf_run_test(algo, size, n, l, random_in, offset, warmup, verbosity);
 
 	return 0;
 }
diff --git a/host/xtest/xtest_main.c b/host/xtest/xtest_main.c
index c61f4db..9771ae0 100644
--- a/host/xtest/xtest_main.c
+++ b/host/xtest/xtest_main.c
@@ -98,7 +98,8 @@
 	printf("\t                   run. A substring match is performed. May be\n");
 	printf("\t                   specified several times.\n");
 	printf("applets:\n");
-	printf("\t--sha-perf [opts]  SHA performance testing tool (-h for usage)\n");
+	printf("\t--sha-perf [opts]  Deprecated, same as --hash-perf\n");
+	printf("\t--hash-perf [opts] Hash performance testing tool (-h for usage)\n");
 	printf("\t--aes-perf [opts]  AES performance testing tool (-h for usage)\n");
 #ifdef CFG_SECSTOR_TA_MGMT_PTA
 	printf("\t--install-ta [directory or list of TAs]\n");
@@ -153,7 +154,9 @@
 	init_ossl();
 
 	if (argc > 1 && !strcmp(argv[1], "--sha-perf"))
-		return sha_perf_runner_cmd_parser(argc-1, &argv[1]);
+		return hash_perf_runner_cmd_parser(argc-1, &argv[1]);
+	else if (argc > 1 && !strcmp(argv[1], "--hash-perf"))
+		return hash_perf_runner_cmd_parser(argc-1, &argv[1]);
 	else if (argc > 1 && !strcmp(argv[1], "--aes-perf"))
 		return aes_perf_runner_cmd_parser(argc-1, &argv[1]);
 #ifdef CFG_SECSTOR_TA_MGMT_PTA
diff --git a/ta/CMakeLists.txt b/ta/CMakeLists.txt
index 044d41a..9301067 100644
--- a/ta/CMakeLists.txt
+++ b/ta/CMakeLists.txt
@@ -13,7 +13,7 @@
 	INTERFACE os_test/include
 	INTERFACE rpc_test/include
 	INTERFACE sdp_basic/include
-	INTERFACE sha_perf/include
+	INTERFACE hash_perf/include
 	INTERFACE sims/include
 	INTERFACE miss/include
 	INTERFACE sims_keepalive/include
diff --git a/ta/Makefile b/ta/Makefile
index 20f22e6..aca2c88 100644
--- a/ta/Makefile
+++ b/ta/Makefile
@@ -28,7 +28,7 @@
 	   concurrent \
 	   concurrent_large \
 	   storage_benchmark \
-	   sha_perf \
+	   hash_perf \
 	   aes_perf \
 	   socket \
 	   supp_plugin \
@@ -46,7 +46,7 @@
 all: ta
 
 .PHONY: ta
-ta: 
+ta:
 	$(q)$(foreach dir,$(TA_DIRS), $(MAKE) -C $(dir) O=$(out-dir)/ta/$(dir) &&) true
 
 # remove build directories including ta/<ta-name>/ directories.
diff --git a/ta/sha_perf/Android.mk b/ta/hash_perf/Android.mk
similarity index 100%
rename from ta/sha_perf/Android.mk
rename to ta/hash_perf/Android.mk
diff --git a/ta/sha_perf/Makefile b/ta/hash_perf/Makefile
similarity index 100%
rename from ta/sha_perf/Makefile
rename to ta/hash_perf/Makefile
diff --git a/ta/sha_perf/include/ta_sha_perf.h b/ta/hash_perf/include/ta_hash_perf.h
similarity index 61%
rename from ta/sha_perf/include/ta_sha_perf.h
rename to ta/hash_perf/include/ta_hash_perf.h
index 80ad41e..e997698 100644
--- a/ta/sha_perf/include/ta_sha_perf.h
+++ b/ta/hash_perf/include/ta_hash_perf.h
@@ -4,18 +4,18 @@
  * All rights reserved.
  */
 
-#ifndef TA_SHA_PERF_H
-#define TA_SHA_PERF_H
+#ifndef TA_HASH_PERF_H
+#define TA_HASH_PERF_H
 
-#define TA_SHA_PERF_UUID { 0x614789f2, 0x39c0, 0x4ebf, \
+#define TA_HASH_PERF_UUID { 0x614789f2, 0x39c0, 0x4ebf, \
 	{ 0xb2, 0x35, 0x92, 0xb3, 0x2a, 0xc1, 0x07, 0xed } }
 
 /*
  * Commands implemented by the TA
  */
 
-#define TA_SHA_PERF_CMD_PREPARE_OP	0
-#define TA_SHA_PERF_CMD_PROCESS		1
+#define TA_HASH_PERF_CMD_PREPARE_OP	0
+#define TA_HASH_PERF_CMD_PROCESS	1
 
 /*
  * Supported algorithms
@@ -26,5 +26,6 @@
 #define TA_SHA_SHA256	2
 #define TA_SHA_SHA384	3
 #define TA_SHA_SHA512	4
+#define TA_SM3		5
 
-#endif /* TA_SHA_PERF_H */
+#endif /* TA_HASH_PERF_H */
diff --git a/ta/sha_perf/include/ta_sha_perf_priv.h b/ta/hash_perf/include/ta_hash_perf_priv.h
similarity index 76%
rename from ta/sha_perf/include/ta_sha_perf_priv.h
rename to ta/hash_perf/include/ta_hash_perf_priv.h
index d0f583d..b9059b4 100644
--- a/ta/sha_perf/include/ta_sha_perf_priv.h
+++ b/ta/hash_perf/include/ta_hash_perf_priv.h
@@ -4,8 +4,8 @@
  * All rights reserved.
  */
 
-#ifndef TA_SHA_PERF_PRIV_H
-#define TA_SHA_PERF_PRIV_H
+#ifndef TA_HASH_PERF_PRIV_H
+#define TA_HASH_PERF_PRIV_H
 
 #include <tee_api.h>
 
@@ -13,4 +13,4 @@
 TEE_Result cmd_process(uint32_t param_types, TEE_Param params[4]);
 void cmd_clean_res(void);
 
-#endif /* TA_SHA_PERF_PRIV_H */
+#endif /* TA_HASH_PERF_PRIV_H */
diff --git a/ta/sha_perf/include/user_ta_header_defines.h b/ta/hash_perf/include/user_ta_header_defines.h
similarity index 83%
rename from ta/sha_perf/include/user_ta_header_defines.h
rename to ta/hash_perf/include/user_ta_header_defines.h
index 639ebb7..494afc0 100644
--- a/ta/sha_perf/include/user_ta_header_defines.h
+++ b/ta/hash_perf/include/user_ta_header_defines.h
@@ -7,9 +7,9 @@
 #ifndef USER_TA_HEADER_DEFINES_H
 #define USER_TA_HEADER_DEFINES_H
 
-#include "ta_sha_perf.h"
+#include "ta_hash_perf.h"
 
-#define TA_UUID TA_SHA_PERF_UUID
+#define TA_UUID TA_HASH_PERF_UUID
 
 #define TA_FLAGS		(TA_FLAG_USER_MODE | TA_FLAG_EXEC_DDR)
 #define TA_STACK_SIZE		(2 * 1024)
diff --git a/ta/sha_perf/sub.mk b/ta/hash_perf/sub.mk
similarity index 66%
rename from ta/sha_perf/sub.mk
rename to ta/hash_perf/sub.mk
index c0aa4b6..d7d5236 100644
--- a/ta/sha_perf/sub.mk
+++ b/ta/hash_perf/sub.mk
@@ -1,3 +1,3 @@
 global-incdirs-y += include
-srcs-y += ta_sha_perf.c
+srcs-y += ta_hash_perf.c
 srcs-y += ta_entry.c
diff --git a/ta/sha_perf/ta_entry.c b/ta/hash_perf/ta_entry.c
similarity index 89%
rename from ta/sha_perf/ta_entry.c
rename to ta/hash_perf/ta_entry.c
index e61eab7..167c7de 100644
--- a/ta/sha_perf/ta_entry.c
+++ b/ta/hash_perf/ta_entry.c
@@ -6,8 +6,8 @@
 
 #include <tee_ta_api.h>
 
-#include "ta_sha_perf.h"
-#include "ta_sha_perf_priv.h"
+#include "ta_hash_perf.h"
+#include "ta_hash_perf_priv.h"
 
 /*
  * Trusted Application Entry Points
@@ -39,7 +39,7 @@
 void TA_CloseSessionEntryPoint(void *pSessionContext)
 {
 	(void)pSessionContext;
-	
+
 	cmd_clean_res();
 }
 
@@ -51,10 +51,10 @@
 	(void)pSessionContext;
 
 	switch (nCommandID) {
-	case TA_SHA_PERF_CMD_PREPARE_OP:
+	case TA_HASH_PERF_CMD_PREPARE_OP:
 		return cmd_prepare_op(nParamTypes, pParams);
 
-	case TA_SHA_PERF_CMD_PROCESS:
+	case TA_HASH_PERF_CMD_PROCESS:
 		return cmd_process(nParamTypes, pParams);
 
 	default:
diff --git a/ta/sha_perf/ta_sha_perf.c b/ta/hash_perf/ta_hash_perf.c
similarity index 86%
rename from ta/sha_perf/ta_sha_perf.c
rename to ta/hash_perf/ta_hash_perf.c
index 01083df..4da8369 100644
--- a/ta/sha_perf/ta_sha_perf.c
+++ b/ta/hash_perf/ta_hash_perf.c
@@ -9,17 +9,17 @@
 #include <string.h>
 #include <trace.h>
 
-#include "ta_sha_perf.h"
-#include "ta_sha_perf_priv.h"
+#include "ta_hash_perf.h"
+#include "ta_hash_perf_priv.h"
 
-#define CHECK(res, name, action) do {			\
-		if ((res) != TEE_SUCCESS) {		\
-			DMSG(name ": 0x%08x", (res));	\
-			action				\
-		}					\
+#define CHECK(res, name, action) do {			 \
+		if ((res) != TEE_SUCCESS) {		 \
+			DMSG(name ": %#08"PRIx32, (res));\
+			action				 \
+		}					 \
 	} while(0)
 
-static TEE_OperationHandle digest_op = NULL;
+static TEE_OperationHandle digest_op;
 
 TEE_Result cmd_process(uint32_t param_types, TEE_Param params[4])
 {
@@ -49,6 +49,7 @@
 		res = TEE_DigestDoFinal(digest_op, in, insz, out, &outsz);
 		CHECK(res, "TEE_DigestDoFinal", return res;);
 	}
+
 	return TEE_SUCCESS;
 }
 
@@ -80,6 +81,9 @@
 	case TA_SHA_SHA512:
 		algo = TEE_ALG_SHA512;
 		break;
+	case TA_SM3:
+		algo = TEE_ALG_SM3;
+		break;
 	default:
 		return TEE_ERROR_BAD_PARAMETERS;
 	}
@@ -93,9 +97,8 @@
 	return TEE_SUCCESS;
 }
 
-
 void cmd_clean_res(void)
 {
 	if (digest_op)
-		TEE_FreeOperation(digest_op);	
+		TEE_FreeOperation(digest_op);
 }