Inclusion of aes-perf benchmark tool into xtest

1. The way of invoking aes-perf tool is just the same as sha-perf
$ xtest --aes-perf
2. Fixed issues with one-whitespace-lines and >80character-lines
3. Added two examples of test cases for aes-perf
4. Minor fixes

Reviewed-by: Jerome Forissier <jerome.forissier@linaro.org>
Signed-off-by: Igor Opaniuk <igor.opaniuk@linaro.org>
diff --git a/host/xtest/Makefile b/host/xtest/Makefile
index 98933ae..368d826 100644
--- a/host/xtest/Makefile
+++ b/host/xtest/Makefile
@@ -38,6 +38,7 @@
 	xtest_main.c \
 	xtest_test.c \
 	sha_perf.c \
+	aes_perf.c \
 	adbg/src/adbg_case.c \
 	adbg/src/adbg_enum.c \
 	adbg/src/adbg_expect.c \
@@ -78,6 +79,7 @@
 CFLAGS += -I../../ta/concurrent/include
 CFLAGS += -I../../ta/concurrent_large/include
 CFLAGS += -I../../ta/sha_perf/include
+CFLAGS += -I../../ta/aes_perf/include
 ifdef CFG_GP_PACKAGE_PATH
 CFLAGS += -I../../ta/GP_TTA_Arithmetical
 CFLAGS += -I../../ta/GP_TTA_Crypto
diff --git a/host/xtest/aes_perf.c b/host/xtest/aes_perf.c
new file mode 100644
index 0000000..44bdbce
--- /dev/null
+++ b/host/xtest/aes_perf.c
@@ -0,0 +1,474 @@
+/*
+ * 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 <tee_client_api.h>
+#include "ta_aes_perf.h"
+#include "aes_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;
+/*
+ * in_shm and out_shm are both IN/OUT to support dynamically choosing
+ * in_place == 1 or in_place == 0.
+ */
+static TEEC_SharedMemory in_shm = {
+	.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT
+};
+static TEEC_SharedMemory out_shm = {
+	.flags = TEEC_MEM_INPUT | 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_AES_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 *mode_str(uint32_t mode)
+{
+	switch (mode) {
+	case TA_AES_ECB:
+		return "ECB";
+	case TA_AES_CBC:
+		return "CBC";
+	case TA_AES_CTR:
+		return "CTR";
+	case TA_AES_XTS:
+		return "XTS";
+	default:
+		return "???";
+	}
+}
+
+#define _TO_STR(x) #x
+#define TO_STR(x) _TO_STR(x)
+
+static void usage(const char *progname, int keysize, int mode,
+				size_t size, int warmup, unsigned int l, unsigned int n)
+{
+	fprintf(stderr, "AES 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] [-m mode] [-k keysize] ", progname);
+	fprintf(stderr, "[-s bufsize] [-r] [-i] [-n loops] [-l iloops] \n");
+	fprintf(stderr, "[-w warmup_time]\n");
+	fprintf(stderr, "Options:\n");
+	fprintf(stderr, "  -h    Print this help and exit\n");
+	fprintf(stderr, "  -i    Use same buffer for input and output (in ");
+	fprintf(stderr, "place)\n");
+	fprintf(stderr, "  -k    Key size in bits: 128, 192 or 256 [%u]\n",
+			keysize);
+	fprintf(stderr, "  -l    Inner loop iterations (TA calls ");
+	fprintf(stderr, "TEE_CipherUpdate() <x> times) [%u]\n", l);
+	fprintf(stderr, "  -m    AES mode: ECB, CBC, CTR, XTS [%s]\n",
+			mode_str(mode));
+	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, "  -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);
+}
+
+static void alloc_shm(size_t sz, int in_place)
+{
+	TEEC_Result res;
+
+	in_shm.buffer = NULL;
+	in_shm.size = sz;
+	res = TEEC_AllocateSharedMemory(&ctx, &in_shm);
+	check_res(res, "TEEC_AllocateSharedMemory");
+
+	if (!in_place) {
+		out_shm.buffer = NULL;
+		out_shm.size = sz;
+		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 void get_current_time(struct timespec *ts)
+{
+	if (clock_gettime(CLOCK_MONOTONIC, ts) < 0) {
+		perror("clock_gettime");
+		exit(1);
+	}
+}
+
+static uint64_t timespec_to_ns(struct timespec *ts)
+{
+	return ((uint64_t)ts->tv_sec * 1000000000) + ts->tv_nsec;
+}
+
+static uint64_t timespec_diff_ns(struct timespec *start, struct timespec *end)
+{
+	return timespec_to_ns(end) - timespec_to_ns(start);
+}
+
+static uint64_t run_test_once(void *in, size_t size, TEEC_Operation *op,
+			  int random_in)
+{
+	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_AES_PERF_CMD_PROCESS, op,
+				 &ret_origin);
+	check_res(res, "TEEC_InvokeCommand");
+	get_current_time(&t1);
+
+	return timespec_diff_ns(&t0, &t1);
+}
+
+static void prepare_key(int decrypt, int keysize, int mode)
+{
+	TEEC_Result res;
+	uint32_t ret_origin;
+	TEEC_Operation op;
+
+	memset(&op, 0, sizeof(op));
+	op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_VALUE_INPUT,
+					 TEEC_NONE, TEEC_NONE);
+	op.params[0].value.a = decrypt;
+	op.params[0].value.b = keysize;
+	op.params[1].value.a = mode;
+	res = TEEC_InvokeCommand(&sess, TA_AES_PERF_CMD_PREPARE_KEY, &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));
+}
+
+/* Encryption test: buffer of tsize byte. Run test n times. */
+void aes_perf_run_test(int mode, int keysize, int decrypt, size_t size,
+				unsigned int n, unsigned int l, int random_in,
+				int in_place, int warmup, int verbosity)
+{
+	uint64_t t;
+	struct statistics stats;
+	struct timespec ts;
+	TEEC_Operation op;
+	int n0 = n;
+
+	vverbose("aes-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_key(decrypt, keysize, mode);
+
+	memset(&stats, 0, sizeof(stats));
+
+	alloc_shm(size, in_place);
+
+	if (!random_in)
+		memset(in_shm.buffer, 0, size);
+
+	memset(&op, 0, sizeof(op));
+	/* Using INOUT to handle the case in_place == 1 */
+	op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INOUT,
+					 TEEC_MEMREF_PARTIAL_INOUT,
+					 TEEC_VALUE_INPUT, TEEC_NONE);
+	op.params[0].memref.parent = &in_shm;
+	op.params[0].memref.offset = 0;
+	op.params[0].memref.size = size;
+	op.params[1].memref.parent = in_place ? &in_shm : &out_shm;
+	op.params[1].memref.offset = 0;
+	op.params[1].memref.size = size;
+	op.params[2].value.a = l;
+
+	verbose("Starting test: %s, %scrypt, keysize=%u bits, size=%zu bytes, ",
+		mode_str(mode), (decrypt ? "de" : "en"), keysize, size);
+	verbose("random=%s, ", yesno(random_in));
+	verbose("in place=%s, ", yesno(in_place));
+	verbose("inner loops=%u, loops=%u, warm-up=%u s\n", l, n, warmup);
+
+	if (warmup)
+		do_warmup(warmup);
+
+	while (n-- > 0) {
+		t = run_test_once(in_shm.buffer, size, &op, random_in);
+		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();
+}
+
+#define NEXT_ARG(i) \
+	do { \
+		if (++i == argc) { \
+			fprintf(stderr, "%s: %s: missing argument\n", \
+				argv[0], argv[i-1]); \
+			return 1; \
+		} \
+	} while (0);
+
+int aes_perf_runner_cmd_parser(int argc, char *argv[])
+{
+	int i;
+
+	/*
+	* Command line parameters
+	*/
+
+	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 decrypt = 0;		/* Encrypt by default, -d to decrypt */
+	int keysize = 128;	/* AES key size (-k) */
+	int mode = TA_AES_ECB;	/* AES mode (-m) */
+	int random_in = 0;	/* Get input data from /dev/urandom (-r) */
+	int in_place = 0;	/* 1: use same buffer for in and out (-i) */
+	int warmup = 2;		/* Start with a 2-second busy loop (-w) */
+
+	/* Parse command line */
+	for (i = 1; i < argc; i++) {
+		if (!strcmp(argv[i], "-h")) {
+			usage(argv[0], keysize, mode, size, warmup, l, n);
+			return 0;
+		}
+	}
+	for (i = 1; i < argc; i++) {
+		if (!strcmp(argv[i], "-d")) {
+			decrypt = 1;
+		} else if (!strcmp(argv[i], "-i")) {
+			in_place = 1;
+		} else if (!strcmp(argv[i], "-k")) {
+			NEXT_ARG(i);
+			keysize = atoi(argv[i]);
+			if (keysize != AES_128 && keysize != AES_192 &&
+				keysize != AES_256) {
+				fprintf(stderr, "%s: invalid key size\n",
+					argv[0]);
+				usage(argv[0], keysize, mode, size, warmup, l, n);
+				return 1;
+			}
+		} else if (!strcmp(argv[i], "-l")) {
+			NEXT_ARG(i);
+			l = atoi(argv[i]);
+		} else if (!strcmp(argv[i], "-m")) {
+			NEXT_ARG(i);
+			if (!strcasecmp(argv[i], "ECB"))
+				mode = TA_AES_ECB;
+			else if (!strcasecmp(argv[i], "CBC"))
+				mode = TA_AES_CBC;
+			else if (!strcasecmp(argv[i], "CTR"))
+				mode = TA_AES_CTR;
+			else if (!strcasecmp(argv[i], "XTS"))
+				mode = TA_AES_XTS;
+			else {
+				fprintf(stderr, "%s, invalid mode\n",
+					argv[0]);
+				usage(argv[0], keysize, mode, 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], "-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], keysize, mode, size, warmup, l, n);
+			return 1;
+		}
+	}
+
+
+	aes_perf_run_test(mode, keysize, decrypt, size, n, l, random_in,
+					in_place, warmup, verbosity);
+
+	return 0;
+}
diff --git a/host/xtest/aes_perf.h b/host/xtest/aes_perf.h
new file mode 100644
index 0000000..c4be316
--- /dev/null
+++ b/host/xtest/aes_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_AES_PERF_H
+#define XTEST_AES_PERF_H
+
+#include "ta_aes_perf.h"
+
+int aes_perf_runner_cmd_parser(int argc, char *argv[]);
+void aes_perf_run_test(int mode, int keysize, int decrypt, size_t size,
+				unsigned int n, unsigned int l, int random_in,
+				int in_place, int warmup, int verbosity);
+
+#endif /* XTEST_AES_PERF_H */
diff --git a/host/xtest/xtest_benchmark_2000.c b/host/xtest/xtest_benchmark_2000.c
index b10dfe9..4af1788 100644
--- a/host/xtest/xtest_benchmark_2000.c
+++ b/host/xtest/xtest_benchmark_2000.c
@@ -19,19 +19,34 @@
 #include "xtest_helpers.h"
 
 #include <sha_perf.h>
+#include <aes_perf.h>
 #include <util.h>
 
-#define SHA_PERF_COUNT 5000	/* Number of measurements */	
+#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
 
+#define AES_PERF_COUNT 5000	/* Number of measurements */
+#define AES_PERF_WARMUP 2 /* Start with a 2-second busy loop  */
+#define AES_PERF_LOOPS 1 /* Inner loops */
+#define AES_PERF_RANDOM_IN 1 /* Get input data from /dev/urandom */
+#define AES_PERF_VERBOSITY 0
+#define AES_PERF_INPLACE 0
 
-
+/* SHA bechmarks */
 static void xtest_tee_benchmark_2001(ADBG_Case_t *Case_p);
 static void xtest_tee_benchmark_2002(ADBG_Case_t *Case_p);
 
+/* AES benchmarks */
+static void xtest_tee_benchmark_2011(ADBG_Case_t *Case_p);
+static void xtest_tee_benchmark_2012(ADBG_Case_t *Case_p);
+
+/* ----------------------------------------------------------------------- */
+/* -------------------------- SHA Benchmarks ----------------------------- */
+/* ----------------------------------------------------------------------- */
+
 static void xtest_tee_benchmark_2001(ADBG_Case_t *c)
 {
 	UNUSED(c);
@@ -40,8 +55,8 @@
 	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_run_test(algo, size, SHA_PERF_COUNT,
+								SHA_PERF_LOOPS, SHA_PERF_RANDOM_IN, offset,
 								SHA_PERF_WARMUP, SHA_PERF_VERBOSITY);
 
 }
@@ -54,8 +69,8 @@
 	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_run_test(algo, size, SHA_PERF_COUNT,
+								SHA_PERF_LOOPS, SHA_PERF_RANDOM_IN, offset,
 								SHA_PERF_WARMUP, SHA_PERF_VERBOSITY);
 
 }
@@ -78,3 +93,59 @@
 		/* How to implement */ ""
 		);
 
+
+/* ----------------------------------------------------------------------- */
+/* -------------------------- AES Benchmarks ----------------------------- */
+/* ----------------------------------------------------------------------- */
+
+static void xtest_tee_benchmark_2011(ADBG_Case_t *c)
+{
+	UNUSED(c);
+
+	int mode = TA_AES_ECB;	/* AES mode */
+	int decrypt = 0; /* Encrypt */
+	int keysize = AES_128;
+	size_t size = 1024;	/* Buffer size */
+
+
+	aes_perf_run_test(mode, keysize, decrypt, size, AES_PERF_COUNT,
+						AES_PERF_LOOPS, AES_PERF_RANDOM_IN, AES_PERF_INPLACE,
+						AES_PERF_WARMUP, AES_PERF_VERBOSITY);
+
+}
+
+static void xtest_tee_benchmark_2012(ADBG_Case_t *c)
+{
+	UNUSED(c);
+
+	int mode = TA_AES_ECB;	/* AES mode */
+	int decrypt = 0; /* Encrypt */
+	int keysize = AES_128;
+	size_t size = 1024;	/* Buffer size */
+
+	aes_perf_run_test(mode, keysize, decrypt, size, AES_PERF_COUNT,
+						AES_PERF_LOOPS, AES_PERF_RANDOM_IN, AES_PERF_INPLACE,
+						AES_PERF_WARMUP, AES_PERF_VERBOSITY);
+
+
+
+}
+
+ADBG_CASE_DEFINE(XTEST_TEE_BENCHMARK_2011, xtest_tee_benchmark_2011,
+		/* Title */
+		"TEE AES Performance test (TA_AES_ECB)",
+		/* Short description */
+		"Short description...",
+		/* Requirement IDs */ "",
+		/* How to implement */ ""
+		);
+
+ADBG_CASE_DEFINE(XTEST_TEE_BENCHMARK_2012, xtest_tee_benchmark_2012,
+		/* Title */
+		"TEE AES Performance test (TA_AES_CBC)",
+		/* Short description */
+		"Short description...",
+		/* Requirement IDs */ "",
+		/* How to implement */ ""
+		);
+
diff --git a/host/xtest/xtest_main.c b/host/xtest/xtest_main.c
index 6d3897d..ec61314 100644
--- a/host/xtest/xtest_main.c
+++ b/host/xtest/xtest_main.c
@@ -22,6 +22,7 @@
 
 /* include here shandalone tests */
 #include "sha_perf.h"
+#include "aes_perf.h"
 
 #ifdef WITH_GP_TESTS
 #include "adbg_entry_declare.h"
@@ -127,6 +128,10 @@
 /* SHA benchmarks */
 ADBG_SUITE_ENTRY(XTEST_TEE_BENCHMARK_2001, NULL)
 ADBG_SUITE_ENTRY(XTEST_TEE_BENCHMARK_2002, NULL)
+
+/* AES benchmarks */
+ADBG_SUITE_ENTRY(XTEST_TEE_BENCHMARK_2011, NULL)
+ADBG_SUITE_ENTRY(XTEST_TEE_BENCHMARK_2012, NULL)
 ADBG_SUITE_DEFINE_END()
 
 
@@ -151,6 +156,9 @@
 	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");
+	printf("\t--aes-perf         AES performance testing tool for OP-TEE\n");
+	printf("\t--aes perf -h      show usage of AES performance testing tool\n");
+	printf("\n");
 }
 
 int main(int argc, char *argv[])
@@ -163,9 +171,10 @@
 
 	opterr = 0;
 
-	if (argc > 1 && !strcmp(argv[1], "--sha-perf")) {
+	if (argc > 1 && !strcmp(argv[1], "--sha-perf"))
 		return sha_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]);
 
 	while ((opt = getopt(argc, argv, "d:l:t:h")) != -1)
 		switch (opt) {
diff --git a/host/xtest/xtest_test.h b/host/xtest/xtest_test.h
index 067b9e1..d3fe081 100644
--- a/host/xtest/xtest_test.h
+++ b/host/xtest/xtest_test.h
@@ -105,9 +105,14 @@
 ADBG_CASE_DECLARE(XTEST_TEE_BENCHMARK_1002);
 ADBG_CASE_DECLARE(XTEST_TEE_BENCHMARK_1003);
 
-
+/* SHA benchmarks */
 ADBG_CASE_DECLARE(XTEST_TEE_BENCHMARK_2001);
 ADBG_CASE_DECLARE(XTEST_TEE_BENCHMARK_2002);
+
+/* AES benchmarks */
+ADBG_CASE_DECLARE(XTEST_TEE_BENCHMARK_2011);
+ADBG_CASE_DECLARE(XTEST_TEE_BENCHMARK_2012);
+
 #ifdef WITH_GP_TESTS
 #include "adbg_case_declare.h"
 ADBG_CASE_DECLARE_AUTO_GENERATED_TESTS()
diff --git a/ta/Makefile b/ta/Makefile
index 96f0236..d5e80f3 100644
--- a/ta/Makefile
+++ b/ta/Makefile
@@ -19,7 +19,8 @@
 	   concurrent \
 	   concurrent_large \
 	   storage_benchmark \
-	   sha_perf
+	   sha_perf \
+	   aes_perf
 
 ifdef CFG_GP_PACKAGE_PATH
 TA_DIRS += GP_TTA_Arithmetical \
diff --git a/ta/aes_perf/Android.mk b/ta/aes_perf/Android.mk
new file mode 100644
index 0000000..54d3449
--- /dev/null
+++ b/ta/aes_perf/Android.mk
@@ -0,0 +1,4 @@
+LOCAL_PATH := $(call my-dir)
+
+local_module := e626662e-c0e2-485c-b8c809fbce6edf3d
+include $(BUILD_OPTEE_MK)
diff --git a/ta/aes_perf/Makefile b/ta/aes_perf/Makefile
new file mode 100644
index 0000000..294d872
--- /dev/null
+++ b/ta/aes_perf/Makefile
@@ -0,0 +1,3 @@
+BINARY = e626662e-c0e2-485c-b8c809fbce6edf3d
+include ../ta_common.mk
+
diff --git a/ta/aes_perf/include/ta_aes_perf.h b/ta/aes_perf/include/ta_aes_perf.h
new file mode 100644
index 0000000..edea29c
--- /dev/null
+++ b/ta/aes_perf/include/ta_aes_perf.h
@@ -0,0 +1,57 @@
+/*
+ * 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_AES_PERF_H
+#define TA_AES_PERF_H
+
+#define TA_AES_PERF_UUID { 0xe626662e, 0xc0e2, 0x485c, \
+	{ 0xb8, 0xc8, 0x09, 0xfb, 0xce, 0x6e, 0xdf, 0x3d } }
+
+/*
+ * Commands implemented by the TA
+ */
+
+#define TA_AES_PERF_CMD_PREPARE_KEY	0
+#define TA_AES_PERF_CMD_PROCESS		1
+
+/*
+ * Supported AES modes of operation
+ */
+
+#define TA_AES_ECB	0
+#define TA_AES_CBC	1
+#define TA_AES_CTR	2
+#define TA_AES_XTS	3
+
+/*
+ * AES key sizes
+ */
+#define AES_128	128
+#define AES_192	192
+#define AES_256	256
+
+#endif /* TA_AES_PERF_H */
diff --git a/ta/aes_perf/include/ta_aes_perf_priv.h b/ta/aes_perf/include/ta_aes_perf_priv.h
new file mode 100644
index 0000000..412f7e3
--- /dev/null
+++ b/ta/aes_perf/include/ta_aes_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_EAS_PERF_PRIV_H
+#define TA_EAS_PERF_PRIV_H
+
+#include <tee_api.h>
+
+TEE_Result cmd_prepare_key(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_EAS_PERF_PRIV_H */
diff --git a/ta/aes_perf/include/user_ta_header_defines.h b/ta/aes_perf/include/user_ta_header_defines.h
new file mode 100644
index 0000000..dbf8f6c
--- /dev/null
+++ b/ta/aes_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_aes_perf.h"
+
+#define TA_UUID TA_AES_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/aes_perf/sub.mk b/ta/aes_perf/sub.mk
new file mode 100644
index 0000000..8553db6
--- /dev/null
+++ b/ta/aes_perf/sub.mk
@@ -0,0 +1,3 @@
+global-incdirs-y += include
+srcs-y += ta_entry.c
+srcs-y += ta_aes_perf.c
diff --git a/ta/aes_perf/ta_aes_perf.c b/ta/aes_perf/ta_aes_perf.c
new file mode 100644
index 0000000..381160d
--- /dev/null
+++ b/ta/aes_perf/ta_aes_perf.c
@@ -0,0 +1,187 @@
+/*
+ * 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_aes_perf.h"
+#include "ta_aes_perf_priv.h"
+
+#define CHECK(res, name, action) do {			\
+		if ((res) != TEE_SUCCESS) {		\
+			DMSG(name ": 0x%08x", (res));	\
+			action				\
+		}					\
+	} while(0)
+
+static uint8_t iv[] = { 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7,
+			0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF };
+static int use_iv;
+
+static TEE_OperationHandle crypto_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 exp_param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INOUT,
+						   TEE_PARAM_TYPE_MEMREF_INOUT,
+						   TEE_PARAM_TYPE_VALUE_INPUT,
+						   TEE_PARAM_TYPE_NONE);
+
+	if (param_types != exp_param_types)
+		return TEE_ERROR_BAD_PARAMETERS;
+
+	in = params[0].memref.buffer;
+	insz = params[0].memref.size;
+	out = params[1].memref.buffer;
+	outsz = params[1].memref.size;
+	n = params[2].value.a;
+
+	while (n--) {
+		res = TEE_CipherUpdate(crypto_op, in, insz, out, &outsz);
+		CHECK(res, "TEE_CipherUpdate", return res;);
+	}
+	return TEE_SUCCESS;
+}
+
+TEE_Result cmd_prepare_key(uint32_t param_types, TEE_Param params[4])
+{
+	TEE_Result res;
+	TEE_ObjectHandle hkey;
+	TEE_ObjectHandle hkey2;
+	TEE_Attribute attr;
+	uint32_t mode;
+	uint32_t op_keysize;
+	uint32_t keysize;
+	uint32_t algo;
+	static uint8_t aes_key[] = { 0x00, 0x01, 0x02, 0x03,
+				     0x04, 0x05, 0x06, 0x07,
+				     0x08, 0x09, 0x0A, 0x0B,
+				     0x0C, 0x0D, 0x0E, 0x0F,
+				     0x10, 0x11, 0x12, 0x13,
+				     0x14, 0x15, 0x16, 0x17,
+				     0x18, 0x19, 0x1A, 0x1B,
+				     0x1C, 0x1D, 0x1E, 0x1F };
+	static uint8_t aes_key2[] = { 0x20, 0x21, 0x22, 0x23,
+				      0x24, 0x25, 0x26, 0x27,
+				      0x28, 0x29, 0x2A, 0x2B,
+				      0x2C, 0x2D, 0x2E, 0x2F,
+				      0x30, 0x31, 0x32, 0x33,
+				      0x34, 0x35, 0x36, 0x37,
+				      0x38, 0x39, 0x3A, 0x3B,
+				      0x3C, 0x3D, 0x3E, 0x3F };
+
+	uint32_t exp_param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT,
+						   TEE_PARAM_TYPE_VALUE_INPUT,
+						   TEE_PARAM_TYPE_NONE,
+						   TEE_PARAM_TYPE_NONE);
+	if (param_types != exp_param_types)
+		return TEE_ERROR_BAD_PARAMETERS;
+
+	mode = params[0].value.a ? TEE_MODE_DECRYPT : TEE_MODE_ENCRYPT;
+	keysize = params[0].value.b;
+	op_keysize = keysize;
+
+	switch (params[1].value.a) {
+	case TA_AES_ECB:
+		algo = TEE_ALG_AES_ECB_NOPAD;
+		use_iv = 0;
+		break;
+	case TA_AES_CBC:
+		algo = TEE_ALG_AES_CBC_NOPAD;
+		use_iv = 1;
+		break;
+	case TA_AES_CTR:
+		algo = TEE_ALG_AES_CTR;
+		use_iv = 1;
+		break;
+	case TA_AES_XTS:
+		algo = TEE_ALG_AES_XTS;
+		use_iv = 1;
+		op_keysize *= 2;
+		break;
+	default:
+		return TEE_ERROR_BAD_PARAMETERS;
+	}
+
+	cmd_clean_res();
+
+	res = TEE_AllocateOperation(&crypto_op, algo, mode, op_keysize);
+	CHECK(res, "TEE_AllocateOperation", return res;);
+
+	res = TEE_AllocateTransientObject(TEE_TYPE_AES, keysize, &hkey);
+	CHECK(res, "TEE_AllocateTransientObject", return res;);
+
+	attr.attributeID = TEE_ATTR_SECRET_VALUE;
+	attr.content.ref.buffer = aes_key;
+	attr.content.ref.length = keysize / 8;
+
+	res = TEE_PopulateTransientObject(hkey, &attr, 1);
+	CHECK(res, "TEE_PopulateTransientObject", return res;);
+
+	if (algo == TEE_ALG_AES_XTS) {
+		res = TEE_AllocateTransientObject(TEE_TYPE_AES, keysize,
+						  &hkey2);
+		CHECK(res, "TEE_AllocateTransientObject", return res;);
+
+		attr.content.ref.buffer = aes_key2;
+
+		res = TEE_PopulateTransientObject(hkey2, &attr, 1);
+		CHECK(res, "TEE_PopulateTransientObject", return res;);
+
+		res = TEE_SetOperationKey2(crypto_op, hkey, hkey2);
+		CHECK(res, "TEE_SetOperationKey2", return res;);
+
+		TEE_FreeTransientObject(hkey2);
+	} else {
+		res = TEE_SetOperationKey(crypto_op, hkey);
+		CHECK(res, "TEE_SetOperationKey", return res;);
+	}
+
+	TEE_FreeTransientObject(hkey);
+
+	if (use_iv)
+		TEE_CipherInit(crypto_op, iv, sizeof(iv));
+	else
+		TEE_CipherInit(crypto_op, NULL, 0);
+
+	return TEE_SUCCESS;
+}
+
+void cmd_clean_res(void)
+{
+	if (crypto_op)
+		TEE_FreeOperation(crypto_op);
+}
diff --git a/ta/aes_perf/ta_entry.c b/ta/aes_perf/ta_entry.c
new file mode 100644
index 0000000..c920295
--- /dev/null
+++ b/ta/aes_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_aes_perf.h"
+#include "ta_aes_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_AES_PERF_CMD_PREPARE_KEY:
+		return cmd_prepare_key(nParamTypes, pParams);
+
+	case TA_AES_PERF_CMD_PROCESS:
+		return cmd_process(nParamTypes, pParams);
+
+	default:
+		return TEE_ERROR_BAD_PARAMETERS;
+	}
+}