Inclusion of sha-perf benchmark tool into xtest (SWG-185)

Reviewed-by: Jerome Forissier <jerome.forissier@linaro.org>
Signed-off-by: Igor Opaniuk <igor.opaniuk@linaro.org>
diff --git a/Android.mk b/Android.mk
index 0e1c1e1..8374cd5 100644
--- a/Android.mk
+++ b/Android.mk
@@ -53,6 +53,7 @@
 		$(LOCAL_PATH)/ta/sims/include \
 		$(LOCAL_PATH)/ta/storage/include \
 		$(LOCAL_PATH)/ta/storage_benchmark/include \
+		$(LOCAL_PATH)/ta/sha_perf/include
 
 # Include configuration file generated by OP-TEE OS (CFG_* macros)
 LOCAL_CFLAGS += -include conf.h
diff --git a/host/xtest/Makefile b/host/xtest/Makefile
index 6badf4c..98933ae 100644
--- a/host/xtest/Makefile
+++ b/host/xtest/Makefile
@@ -33,9 +33,11 @@
 	xtest_10000.c \
 	xtest_20000.c \
 	xtest_benchmark_1000.c \
+	xtest_benchmark_2000.c \
 	xtest_helpers.c \
 	xtest_main.c \
 	xtest_test.c \
+	sha_perf.c \
 	adbg/src/adbg_case.c \
 	adbg/src/adbg_enum.c \
 	adbg/src/adbg_expect.c \
@@ -75,6 +77,7 @@
 CFLAGS += -I../../ta/storage_benchmark/include
 CFLAGS += -I../../ta/concurrent/include
 CFLAGS += -I../../ta/concurrent_large/include
+CFLAGS += -I../../ta/sha_perf/include
 ifdef CFG_GP_PACKAGE_PATH
 CFLAGS += -I../../ta/GP_TTA_Arithmetical
 CFLAGS += -I../../ta/GP_TTA_Crypto
@@ -129,7 +132,7 @@
 CFLAGS += -g3
 
 LDFLAGS += -L$(OPTEE_CLIENT_EXPORT)/lib -lteec
-LDFLAGS += -lpthread
+LDFLAGS += -lpthread -lm
 
 .PHONY: all
 all: xtest
diff --git a/host/xtest/sha_perf.c b/host/xtest/sha_perf.c
new file mode 100644
index 0000000..8045e7e
--- /dev/null
+++ b/host/xtest/sha_perf.c
@@ -0,0 +1,485 @@
+/*
+ * Copyright (c) 2015, Linaro Limited
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <math.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <time.h>
+#include <unistd.h>
+
+#include <adbg.h>
+#include <tee_client_api.h>
+#include "sha_perf.h"
+
+#define _verbose(lvl, ...)			\
+	do {					\
+		if (verbosity >= lvl) {		\
+			printf(__VA_ARGS__);	\
+			fflush(stdout);		\
+		}				\
+	} while (0)
+
+#define verbose(...)  _verbose(1, __VA_ARGS__)
+#define vverbose(...) _verbose(2, __VA_ARGS__)
+
+/*
+ * TEE client stuff
+ */
+
+static TEEC_Context ctx;
+static TEEC_Session sess;
+static TEEC_SharedMemory in_shm = {
+	.flags = TEEC_MEM_INPUT
+};
+static TEEC_SharedMemory out_shm = {
+	.flags = TEEC_MEM_OUTPUT
+};
+
+static void errx(const char *msg, TEEC_Result res)
+{
+	fprintf(stderr, "%s: 0x%08x", msg, res);
+	exit (1);
+}
+
+static void check_res(TEEC_Result res, const char *errmsg)
+{
+	if (res != TEEC_SUCCESS)
+		errx(errmsg, res);
+}
+
+static void open_ta(void)
+{
+	TEEC_Result res;
+	TEEC_UUID uuid = TA_SHA_PERF_UUID;
+	uint32_t err_origin;
+
+	res = TEEC_InitializeContext(NULL, &ctx);
+	check_res(res,"TEEC_InitializeContext");
+
+	res = TEEC_OpenSession(&ctx, &sess, &uuid, TEEC_LOGIN_PUBLIC, NULL,
+			       NULL, &err_origin);
+	check_res(res,"TEEC_OpenSession");
+}
+
+/*
+ * Statistics
+ *
+ * We want to compute min, max, mean and standard deviation of processing time
+ */
+
+struct statistics {
+	int n;
+	double m;
+	double M2;
+	double min;
+	double max;
+	int initialized;
+};
+
+/* Take new sample into account (Knuth/Welford algorithm) */
+static void update_stats(struct statistics *s, uint64_t t)
+{
+	double x = (double)t;
+	double delta = x - s->m;
+
+	s->n++;
+	s->m += delta/s->n;
+	s->M2 += delta*(x - s->m);
+	if (!s->initialized) {
+		s->min = s->max = x;
+		s->initialized = 1;
+	} else {
+		if (s->min > x)
+			s->min = x;
+		if (s->max < x)
+			s->max = x;
+	}
+}
+
+static double stddev(struct statistics *s)
+{
+	if (s->n < 2)
+		return NAN;
+	return sqrt(s->M2/s->n);
+}
+
+static const char *algo_str(uint32_t algo)
+{
+	switch (algo) {
+	case TA_SHA_SHA1:
+		return "SHA1";
+	case TA_SHA_SHA224:
+		return "SHA224";
+	case TA_SHA_SHA256:
+		return "SHA256";
+	case TA_SHA_SHA384:
+		return "SHA384";
+	case TA_SHA_SHA512:
+		return "SHA512";
+	default:
+		return "???";
+	}
+}
+
+static int hash_size(uint32_t algo)
+{
+	switch (algo) {
+	case TA_SHA_SHA1:
+		return 20;
+	case TA_SHA_SHA224:
+		return 28;
+	case TA_SHA_SHA256:
+		return 32;
+	case TA_SHA_SHA384:
+		return 48;
+	case TA_SHA_SHA512:
+		return 64;
+	default:
+		return 0;
+	}
+}
+
+#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;
+
+	in_shm.buffer = NULL;
+	in_shm.size = sz + offset;
+	res = TEEC_AllocateSharedMemory(&ctx, &in_shm);
+	check_res(res, "TEEC_AllocateSharedMemory");
+
+	out_shm.buffer = NULL;
+	out_shm.size = hash_size(algo);
+	res = TEEC_AllocateSharedMemory(&ctx, &out_shm);
+	check_res(res, "TEEC_AllocateSharedMemory");
+}
+
+static void free_shm(void)
+{
+	TEEC_ReleaseSharedMemory(&in_shm);
+	TEEC_ReleaseSharedMemory(&out_shm);
+}
+
+static ssize_t read_random(void *in, size_t rsize)
+{
+	static int rnd;
+	ssize_t s;
+
+	if (!rnd) {
+		rnd = open("/dev/urandom", O_RDONLY);
+		if (rnd < 0) {
+			perror("open");
+			return 1;
+		}
+	}
+	s = read(rnd, in, rsize);
+	if (s < 0) {
+		perror("read");
+		return 1;
+	}
+	if ((size_t)s != rsize) {
+		printf("read: requested %zu bytes, got %zd\n",
+		       rsize, s);
+	}
+	return 0;
+}
+
+static long get_current_time(struct timespec *ts)
+{
+	if (clock_gettime(CLOCK_MONOTONIC, ts) < 0) {
+		perror("clock_gettime");
+		exit(1);
+	}
+	return 0;
+}
+
+static uint64_t timespec_diff_ns(struct timespec *start, struct timespec *end)
+{
+	uint64_t ns = 0;
+
+	if (end->tv_nsec < start->tv_nsec) {
+		ns += 1000000000 * (end->tv_sec - start->tv_sec - 1);
+		ns += 1000000000 - start->tv_nsec + end->tv_nsec;
+	} else {
+		ns += 1000000000 * (end->tv_sec - start->tv_sec);
+		ns += end->tv_nsec - start->tv_nsec;
+	}
+	return ns;
+}
+
+static uint64_t run_test_once(void *in, size_t size,  int random_in, TEEC_Operation *op)
+{
+	struct timespec t0, t1;
+	TEEC_Result res;
+	uint32_t ret_origin;
+
+	if (random_in)
+		read_random(in, size);
+	get_current_time(&t0);
+	res = TEEC_InvokeCommand(&sess, TA_SHA_PERF_CMD_PROCESS, op,
+				 &ret_origin);
+	check_res(res, "TEEC_InvokeCommand");
+	get_current_time(&t1);
+
+	return timespec_diff_ns(&t0, &t1);
+}
+
+static void prepare_op(int algo)
+{
+	TEEC_Result res;
+	uint32_t ret_origin;
+	TEEC_Operation op;
+
+	memset(&op, 0, sizeof(op));
+	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,
+				 &ret_origin);
+	check_res(res, "TEEC_InvokeCommand");
+}
+
+static void do_warmup(int warmup)
+{
+	struct timespec t0, t;
+	int i;
+
+	get_current_time(&t0);
+	do {
+		for (i = 0; i < 100000; i++)
+			;
+		get_current_time(&t);
+	} while (timespec_diff_ns(&t0, &t) < (uint64_t)warmup * 1000000000);
+}
+
+static const char *yesno(int v)
+{
+	return (v ? "yes" : "no");
+}
+
+static double mb_per_sec(size_t size, double usec)
+{
+	return (1000000000/usec)*((double)size/(1024*1024));
+}
+
+/* Hash test: buffer of size byte. Run test n times.
+ * Entry point for running SHA benchmark 
+ * Params: 
+ * algo - Algorithm
+ * size - Buffer size
+ * n - Number of measurements
+ * l - Amount of inner loops
+ * random_in - Get input from /dev/urandom
+ * offset - Buffer offset wrt. alloc-ed address
+ * warmup - Start with a-second busy loop
+ * verbosity - Verbosity level 
+ * */
+extern void sha_perf_run_test(int algo, size_t size, unsigned int n, 
+				unsigned int l, int random_in, int offset, 
+				int warmup, int verbosity)
+{
+	uint64_t t;
+	struct statistics stats;
+	TEEC_Operation op;
+	int n0 = n;
+	struct timespec ts;
+
+	vverbose("sha-perf version %s\n", TO_STR(VERSION));
+	if (clock_getres(CLOCK_MONOTONIC, &ts) < 0) {
+		perror("clock_getres");
+		return;
+	}
+	vverbose("Clock resolution is %lu ns\n", ts.tv_sec*1000000000 +
+		ts.tv_nsec);
+
+	open_ta();
+	prepare_op(algo);
+	
+	
+	alloc_shm(size, algo, offset);
+
+	if (!random_in)
+		memset((uint8_t *)in_shm.buffer + offset, 0, size);
+
+	memset(&op, 0, sizeof(op));
+	op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
+					 TEEC_MEMREF_PARTIAL_OUTPUT,
+					 TEEC_VALUE_INPUT, TEEC_NONE);
+	op.params[0].memref.parent = &in_shm;
+	op.params[0].memref.offset = 0;
+	op.params[0].memref.size = size + offset;
+	op.params[1].memref.parent = &out_shm;
+	op.params[1].memref.offset = 0;
+	op.params[1].memref.size = hash_size(algo);
+	op.params[2].value.a = l;
+	op.params[2].value.b = offset;
+
+	verbose("Starting test: %s, size=%zu bytes, ",
+		algo_str(algo), size);
+	verbose("random=%s, ", yesno(random_in));
+	verbose("unaligned=%s, ", yesno(offset));
+	verbose("inner loops=%u, loops=%u, warm-up=%u s\n", l, n, warmup);
+
+	if (warmup)
+		do_warmup(warmup);
+
+	memset(&stats, 0, sizeof(stats));
+	while (n-- > 0) {
+		t = run_test_once((uint8_t *)in_shm.buffer + offset, size, random_in, &op);
+		update_stats(&stats, t);
+		if (n % (n0/10) == 0)
+			vverbose("#");
+	}
+	vverbose("\n");
+	printf("min=%gμs max=%gμs mean=%gμs stddev=%gμs (%gMiB/s)\n",
+	       stats.min/1000, stats.max/1000, stats.m/1000,
+	       stddev(&stats)/1000, mb_per_sec(size, stats.m));
+	free_shm();
+}
+
+static void usage(const char *progname, 
+				/* Default params */
+				int algo, size_t size, int warmup, int l, int n)
+{
+	fprintf(stderr, "SHA performance testing tool for OP-TEE (%s)\n\n",
+		TO_STR(VERSION));
+	fprintf(stderr, "Usage:\n");
+	fprintf(stderr, "  %s -h\n", progname);
+	fprintf(stderr, "  %s [-v] [-a algo] ", progname);
+	fprintf(stderr, "[-s bufsize] [-r] [-n loops] [-l iloops] ");
+	fprintf(stderr, "[-w warmup_time]\n");
+	fprintf(stderr, "Options:\n");
+	fprintf(stderr, "  -h    Print this help and exit\n");
+	fprintf(stderr, "  -l    Inner loop iterations (TA hashes ");
+	fprintf(stderr, "the buffer <x> times) [%u]\n", l);
+	fprintf(stderr, "  -a    Algorithm (SHA1, SHA224, SHA256, SHA384, ");
+	fprintf(stderr, "SHA512) [%s]\n", algo_str(algo));
+	fprintf(stderr, "  -n    Outer loop iterations [%u]\n", n);
+	fprintf(stderr, "  -r    Get input data from /dev/urandom ");
+	fprintf(stderr, "(otherwise use zero-filled buffer)\n");
+	fprintf(stderr, "  -s    Buffer size (process <x> bytes at a time) ");
+	fprintf(stderr, "[%zu]\n", size);
+	fprintf(stderr, "  -u    Use unaligned buffer (odd address)\n");
+	fprintf(stderr, "  -v    Be verbose (use twice for greater effect)\n");
+	fprintf(stderr, "  -w    Warm-up time in seconds: execute a busy ");
+	fprintf(stderr, "loop before the test\n");
+	fprintf(stderr, "        to mitigate the effects of cpufreq etc. ");
+	fprintf(stderr, "[%u]\n", warmup);
+}
+
+#define NEXT_ARG(i) \
+	do { \
+		if (++i == argc) { \
+			fprintf(stderr, "%s: %s: missing argument\n", \
+				argv[0], argv[i-1]); \
+			return 1; \
+		} \
+	} while (0);
+
+
+
+extern int sha_perf_runner_cmd_parser(int argc, char *argv[])
+{
+	int i;
+	
+	/* Command line params */
+	size_t size = 1024;	/* Buffer size (-s) */
+	unsigned int n = 5000;	/* Number of measurements (-n) */
+	unsigned int l = 1;	/* Inner loops (-l) */
+	int verbosity = 0;	/* Verbosity (-v) */
+	int algo = TA_SHA_SHA1;	/* Algorithm (-a) */
+	int random_in = 0;	/* Get input data from /dev/urandom (-r) */
+	int warmup = 2;		/* Start with a 2-second busy loop (-w) */
+	int offset = 0;          /* Buffer offset wrt. alloc'ed address (-u) */
+
+
+	/* Parse command line */
+	for (i = 1; i < argc; i++) {
+		if (!strcmp(argv[i], "-h")) {
+			usage(argv[0], algo, size, warmup, l, n);
+			return 0;
+		}
+	}
+	for (i = 1; i < argc; i++) {
+		if (!strcmp(argv[i], "-l")) {
+			NEXT_ARG(i);
+			l = atoi(argv[i]);
+		} else if (!strcmp(argv[i], "-a")) {
+			NEXT_ARG(i);
+			if (!strcasecmp(argv[i], "SHA1"))
+				algo = TA_SHA_SHA1;
+			else if (!strcasecmp(argv[i], "SHA224"))
+				algo = TA_SHA_SHA224;
+			else if (!strcasecmp(argv[i], "SHA256"))
+				algo = TA_SHA_SHA256;
+			else if (!strcasecmp(argv[i], "SHA384"))
+				algo = TA_SHA_SHA384;
+			else if (!strcasecmp(argv[i], "SHA512"))
+				algo = TA_SHA_SHA512;
+			else {
+				fprintf(stderr, "%s, invalid algorithm\n",
+					argv[0]);
+				usage(argv[0], algo, size, warmup, l, n);
+				return 1;
+			}
+		} else if (!strcmp(argv[i], "-n")) {
+			NEXT_ARG(i);
+			n = atoi(argv[i]);
+		} else if (!strcmp(argv[i], "-r")) {
+			random_in = 1;
+		} else if (!strcmp(argv[i], "-s")) {
+			NEXT_ARG(i);
+			size = atoi(argv[i]);
+		} else if (!strcmp(argv[i], "-u")) {
+			offset = 1;
+		} else if (!strcmp(argv[i], "-v")) {
+			verbosity++;
+		} else if (!strcmp(argv[i], "-w")) {
+			NEXT_ARG(i);
+			warmup = atoi(argv[i]);
+		} else {
+			fprintf(stderr, "%s: invalid argument: %s\n",
+				argv[0], argv[i]);
+			usage(argv[0], algo, size, warmup, l, n);
+			return 1;
+		}
+	}
+	
+	sha_perf_run_test(algo, size, n, l, random_in, offset, warmup, verbosity);
+
+	return 0;
+}
diff --git a/host/xtest/sha_perf.h b/host/xtest/sha_perf.h
new file mode 100644
index 0000000..fbdea87
--- /dev/null
+++ b/host/xtest/sha_perf.h
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2015, Linaro Limited
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef XTEST_SHA_PERF_H
+#define XTEST_SHA_PERF_H
+
+#include "ta_sha_perf.h"
+
+int sha_perf_runner_cmd_parser(int argc, char *argv[]);
+void sha_perf_run_test(int algo, size_t size, unsigned int n, 
+						unsigned int l, int random_in, int offset, 
+						int warmup, int verbosity);
+
+#endif /* XTEST_SHA_PERF_H */
diff --git a/host/xtest/xtest_benchmark_2000.c b/host/xtest/xtest_benchmark_2000.c
new file mode 100644
index 0000000..b10dfe9
--- /dev/null
+++ b/host/xtest/xtest_benchmark_2000.c
@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2015, Linaro Limited
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License Version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "xtest_test.h"
+#include "xtest_helpers.h"
+
+#include <sha_perf.h>
+#include <util.h>
+
+#define SHA_PERF_COUNT 5000	/* Number of measurements */	
+#define SHA_PERF_WARMUP 2 /* Start with a 2-second busy loop  */
+#define SHA_PERF_LOOPS 1 /* Inner loops */
+#define SHA_PERF_RANDOM_IN 1 /* Get input data from /dev/urandom */
+#define SHA_PERF_VERBOSITY 0
+
+
+
+static void xtest_tee_benchmark_2001(ADBG_Case_t *Case_p);
+static void xtest_tee_benchmark_2002(ADBG_Case_t *Case_p);
+
+static void xtest_tee_benchmark_2001(ADBG_Case_t *c)
+{
+	UNUSED(c);
+
+	int algo = TA_SHA_SHA1;	/* Algorithm */
+	size_t size = 1024;	/* Buffer size */
+	int offset = 0;          /* Buffer offset wrt. alloc'ed address */
+
+	sha_perf_run_test(algo, size, SHA_PERF_COUNT, 
+								SHA_PERF_LOOPS, SHA_PERF_RANDOM_IN, offset, 
+								SHA_PERF_WARMUP, SHA_PERF_VERBOSITY);
+
+}
+
+static void xtest_tee_benchmark_2002(ADBG_Case_t *c)
+{
+	UNUSED(c);
+
+	int algo = TA_SHA_SHA256;	/* Algorithm */
+	size_t size = 4096;	/* Buffer size */
+	int offset = 0;          /* Buffer offset wrt. alloc'ed address */
+
+	sha_perf_run_test(algo, size, SHA_PERF_COUNT, 
+								SHA_PERF_LOOPS, SHA_PERF_RANDOM_IN, offset, 
+								SHA_PERF_WARMUP, SHA_PERF_VERBOSITY);
+
+}
+
+ADBG_CASE_DEFINE(XTEST_TEE_BENCHMARK_2001, xtest_tee_benchmark_2001,
+		/* Title */
+		"TEE SHA Performance test (TA_SHA_SHA1)",
+		/* Short description */
+		"Hashing 1024 bytes buffer with SHA1 algo, offset = 0",
+		/* Requirement IDs */ "",
+		/* How to implement */ ""
+		);
+
+ADBG_CASE_DEFINE(XTEST_TEE_BENCHMARK_2002, xtest_tee_benchmark_2002,
+		/* Title */
+		"TEE SHA Performance test (TA_SHA_SHA226)",
+		/* Short description */
+		"Hashing 4096 bytes buffer with SHA256 algo, offset = 0",
+		/* Requirement IDs */ "",
+		/* How to implement */ ""
+		);
+
diff --git a/host/xtest/xtest_helpers.h b/host/xtest/xtest_helpers.h
index 8edee8e..2339db2 100644
--- a/host/xtest/xtest_helpers.h
+++ b/host/xtest/xtest_helpers.h
@@ -44,6 +44,7 @@
 /* IO access macro */
 #define  IO(addr)  (*((volatile unsigned long *)(addr)))
 
+#define UNUSED(x) (void)(x)
 /*
  * Helpers for commands towards the crypt TA
  */
diff --git a/host/xtest/xtest_main.c b/host/xtest/xtest_main.c
index ade4f52..6d3897d 100644
--- a/host/xtest/xtest_main.c
+++ b/host/xtest/xtest_main.c
@@ -19,6 +19,10 @@
 #include <adbg.h>
 #include "xtest_test.h"
 #include "xtest_helpers.h"
+
+/* include here shandalone tests */
+#include "sha_perf.h"
+
 #ifdef WITH_GP_TESTS
 #include "adbg_entry_declare.h"
 #endif
@@ -113,13 +117,19 @@
 
 
 ADBG_SUITE_DECLARE(XTEST_TEE_BENCHMARK)
-
 ADBG_SUITE_DEFINE_BEGIN(XTEST_TEE_BENCHMARK, NULL)
+
+/* Storage benchmarks */
 ADBG_SUITE_ENTRY(XTEST_TEE_BENCHMARK_1001, NULL)
 ADBG_SUITE_ENTRY(XTEST_TEE_BENCHMARK_1002, NULL)
 ADBG_SUITE_ENTRY(XTEST_TEE_BENCHMARK_1003, NULL)
+
+/* SHA benchmarks */
+ADBG_SUITE_ENTRY(XTEST_TEE_BENCHMARK_2001, NULL)
+ADBG_SUITE_ENTRY(XTEST_TEE_BENCHMARK_2002, NULL)
 ADBG_SUITE_DEFINE_END()
 
+
 char *_device = NULL;
 unsigned int level = 0;
 static const char glevel[] = "0";
@@ -137,6 +147,9 @@
 	printf("\t-t <test_suite>    available test suite: regression, benchmark\n");
 	printf("\t                   default value = %s\n", gsuitename);
 	printf("\t-h                 show usage\n");
+	printf("applets:\n");
+	printf("\t--sha-perf         SHA performance testing tool for OP-TEE\n");
+	printf("\t--sha perf -h      show usage of SHA performance testing tool\n");
 	printf("\n");
 }
 
@@ -150,6 +163,10 @@
 
 	opterr = 0;
 
+	if (argc > 1 && !strcmp(argv[1], "--sha-perf")) {
+		return sha_perf_runner_cmd_parser(argc-1, &argv[1]);
+	}
+
 	while ((opt = getopt(argc, argv, "d:l:t:h")) != -1)
 		switch (opt) {
 		case 'd':
diff --git a/host/xtest/xtest_test.h b/host/xtest/xtest_test.h
index 6f90b14..067b9e1 100644
--- a/host/xtest/xtest_test.h
+++ b/host/xtest/xtest_test.h
@@ -105,6 +105,9 @@
 ADBG_CASE_DECLARE(XTEST_TEE_BENCHMARK_1002);
 ADBG_CASE_DECLARE(XTEST_TEE_BENCHMARK_1003);
 
+
+ADBG_CASE_DECLARE(XTEST_TEE_BENCHMARK_2001);
+ADBG_CASE_DECLARE(XTEST_TEE_BENCHMARK_2002);
 #ifdef WITH_GP_TESTS
 #include "adbg_case_declare.h"
 ADBG_CASE_DECLARE_AUTO_GENERATED_TESTS()
diff --git a/ta/Makefile b/ta/Makefile
index 1cc01be..96f0236 100644
--- a/ta/Makefile
+++ b/ta/Makefile
@@ -18,7 +18,8 @@
 	   storage2 \
 	   concurrent \
 	   concurrent_large \
-	   storage_benchmark
+	   storage_benchmark \
+	   sha_perf
 
 ifdef CFG_GP_PACKAGE_PATH
 TA_DIRS += GP_TTA_Arithmetical \
diff --git a/ta/sha_perf/Android.mk b/ta/sha_perf/Android.mk
new file mode 100644
index 0000000..f6cf4e8
--- /dev/null
+++ b/ta/sha_perf/Android.mk
@@ -0,0 +1,4 @@
+LOCAL_PATH := $(call my-dir)
+
+local_module := 614789f2-39c0-4ebf-b23592b32ac107ed
+include $(BUILD_OPTEE_MK)
diff --git a/ta/sha_perf/Makefile b/ta/sha_perf/Makefile
new file mode 100644
index 0000000..ce5e908
--- /dev/null
+++ b/ta/sha_perf/Makefile
@@ -0,0 +1,3 @@
+BINARY = 614789f2-39c0-4ebf-b23592b32ac107ed
+include ../ta_common.mk
+
diff --git a/ta/sha_perf/include/ta_sha_perf.h b/ta/sha_perf/include/ta_sha_perf.h
new file mode 100644
index 0000000..46716b2
--- /dev/null
+++ b/ta/sha_perf/include/ta_sha_perf.h
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2015, Linaro Limited
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef TA_SHA_PERF_H
+#define TA_SHA_PERF_H
+
+#define TA_SHA_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
+
+/*
+ * Supported algorithms
+ */
+
+#define TA_SHA_SHA1	0
+#define TA_SHA_SHA224	1
+#define TA_SHA_SHA256	2
+#define TA_SHA_SHA384	3
+#define TA_SHA_SHA512	4
+
+#endif /* TA_SHA_PERF_H */
diff --git a/ta/sha_perf/include/ta_sha_perf_priv.h b/ta/sha_perf/include/ta_sha_perf_priv.h
new file mode 100644
index 0000000..5b900d7
--- /dev/null
+++ b/ta/sha_perf/include/ta_sha_perf_priv.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2015, Linaro Limited
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef TA_SHA_PERF_PRIV_H
+#define TA_SHA_PERF_PRIV_H
+
+#include <tee_api.h>
+
+TEE_Result cmd_prepare_op(uint32_t param_types, TEE_Param params[4]);
+TEE_Result cmd_process(uint32_t param_types, TEE_Param params[4]);
+void cmd_clean_res(void);
+
+#endif /* TA_SHA_PERF_PRIV_H */
diff --git a/ta/sha_perf/include/user_ta_header_defines.h b/ta/sha_perf/include/user_ta_header_defines.h
new file mode 100644
index 0000000..b03fdd2
--- /dev/null
+++ b/ta/sha_perf/include/user_ta_header_defines.h
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2015, Linaro Limited
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef USER_TA_HEADER_DEFINES_H
+#define USER_TA_HEADER_DEFINES_H
+
+#include "ta_sha_perf.h"
+
+#define TA_UUID TA_SHA_PERF_UUID
+
+#define TA_FLAGS		(TA_FLAG_USER_MODE | TA_FLAG_EXEC_DDR)
+#define TA_STACK_SIZE		(2 * 1024)
+#define TA_DATA_SIZE		(32 * 1024)
+
+#endif
diff --git a/ta/sha_perf/sub.mk b/ta/sha_perf/sub.mk
new file mode 100644
index 0000000..c0aa4b6
--- /dev/null
+++ b/ta/sha_perf/sub.mk
@@ -0,0 +1,3 @@
+global-incdirs-y += include
+srcs-y += ta_sha_perf.c
+srcs-y += ta_entry.c
diff --git a/ta/sha_perf/ta_entry.c b/ta/sha_perf/ta_entry.c
new file mode 100644
index 0000000..906a8ca
--- /dev/null
+++ b/ta/sha_perf/ta_entry.c
@@ -0,0 +1,84 @@
+/*
+ * Copyright (c) 2015, Linaro Limited
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <tee_ta_api.h>
+
+#include "ta_sha_perf.h"
+#include "ta_sha_perf_priv.h"
+
+/*
+ * Trusted Application Entry Points
+ */
+
+/* Called each time a new instance is created */
+TEE_Result TA_CreateEntryPoint(void)
+{
+	return TEE_SUCCESS;
+}
+
+/* Called each time an instance is destroyed */
+void TA_DestroyEntryPoint(void)
+{
+}
+
+/* Called each time a session is opened */
+TEE_Result TA_OpenSessionEntryPoint(uint32_t nParamTypes,
+				    TEE_Param pParams[4],
+				    void **ppSessionContext)
+{
+	(void)nParamTypes;
+	(void)pParams;
+	(void)ppSessionContext;
+	return TEE_SUCCESS;
+}
+
+/* Called each time a session is closed */
+void TA_CloseSessionEntryPoint(void *pSessionContext)
+{
+	(void)pSessionContext;
+	
+	cmd_clean_res();
+}
+
+/* Called when a command is invoked */
+TEE_Result TA_InvokeCommandEntryPoint(void *pSessionContext,
+				      uint32_t nCommandID, uint32_t nParamTypes,
+				      TEE_Param pParams[4])
+{
+	(void)pSessionContext;
+
+	switch (nCommandID) {
+	case TA_SHA_PERF_CMD_PREPARE_OP:
+		return cmd_prepare_op(nParamTypes, pParams);
+
+	case TA_SHA_PERF_CMD_PROCESS:
+		return cmd_process(nParamTypes, pParams);
+
+	default:
+		return TEE_ERROR_BAD_PARAMETERS;
+	}
+}
diff --git a/ta/sha_perf/ta_sha_perf.c b/ta/sha_perf/ta_sha_perf.c
new file mode 100644
index 0000000..df0d308
--- /dev/null
+++ b/ta/sha_perf/ta_sha_perf.c
@@ -0,0 +1,121 @@
+/*
+ * Copyright (c) 2015, Linaro Limited
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <tee_internal_api.h>
+#include <tee_ta_api.h>
+#include <string.h>
+#include <trace.h>
+
+#include "ta_sha_perf.h"
+#include "ta_sha_perf_priv.h"
+
+#define CHECK(res, name, action) do {			\
+		if ((res) != TEE_SUCCESS) {		\
+			DMSG(name ": 0x%08x", (res));	\
+			action				\
+		}					\
+	} while(0)
+
+static TEE_OperationHandle digest_op = NULL;
+
+TEE_Result cmd_process(uint32_t param_types, TEE_Param params[4])
+{
+	TEE_Result res;
+	int n;
+	void *in, *out;
+	uint32_t insz;
+	uint32_t outsz;
+	uint32_t offset;
+	uint32_t exp_param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INPUT,
+						   TEE_PARAM_TYPE_MEMREF_OUTPUT,
+						   TEE_PARAM_TYPE_VALUE_INPUT,
+						   TEE_PARAM_TYPE_NONE);
+
+	if (param_types != exp_param_types)
+		return TEE_ERROR_BAD_PARAMETERS;
+
+	offset = params[2].value.b;
+	in = (uint8_t *)params[0].memref.buffer + offset;
+	insz = params[0].memref.size - offset;
+	out = params[1].memref.buffer;
+	outsz = params[1].memref.size;
+	n = params[2].value.a;
+
+	while (n--) {
+		res = TEE_DigestDoFinal(digest_op, in, insz, out, &outsz);
+		CHECK(res, "TEE_DigestDoFinal", return res;);
+	}
+	return TEE_SUCCESS;
+}
+
+TEE_Result cmd_prepare_op(uint32_t param_types, TEE_Param params[4])
+{
+	TEE_Result res;
+	uint32_t algo;
+
+	uint32_t exp_param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT,
+						   TEE_PARAM_TYPE_NONE,
+						   TEE_PARAM_TYPE_NONE,
+						   TEE_PARAM_TYPE_NONE);
+	if (param_types != exp_param_types)
+		return TEE_ERROR_BAD_PARAMETERS;
+
+	switch (params[0].value.a) {
+	case TA_SHA_SHA1:
+		algo = TEE_ALG_SHA1;
+		break;
+	case TA_SHA_SHA224:
+		algo = TEE_ALG_SHA224;
+		break;
+	case TA_SHA_SHA256:
+		algo = TEE_ALG_SHA256;
+		break;
+	case TA_SHA_SHA384:
+		algo = TEE_ALG_SHA384;
+		break;
+	case TA_SHA_SHA512:
+		algo = TEE_ALG_SHA512;
+		break;
+	default:
+		return TEE_ERROR_BAD_PARAMETERS;
+	}
+
+	if (digest_op)
+		TEE_FreeOperation(digest_op);
+
+	res = TEE_AllocateOperation(&digest_op, algo, TEE_MODE_DIGEST, 0);
+	CHECK(res, "TEE_AllocateOperation", return res;);
+
+	return TEE_SUCCESS;
+}
+
+
+void cmd_clean_res(void)
+{
+	if (digest_op)
+		TEE_FreeOperation(digest_op);	
+}