Raw access to trusted application secure storage

This change provides basics for creating, reading and deleting an
object in a TA secure storage.

The TA implementation shows how to use the GPD TEE Internal Core API
for some basic data persistent object manipulations.

A TA command allows to create an object in the TA secure storage.
A TA command allows to read an object in the TA secure storage.
A TA command allows to delete an object from the TA secure storage.

This example does not cover all the possibilities of secure storage API
provided to TAs: seeking into a object data stream, manipulating
key material objects instead of raw data objects, etc.

Signed-off-by: Etienne Carriere <etienne.carriere@linaro.org>
Acked-by: Jerome Forissier <jerome.forissier@linaro.org>
Acked-by: Jens Wiklander <jens.wiklander@linaro.org>
diff --git a/secure_storage/ta/Android.mk b/secure_storage/ta/Android.mk
new file mode 100644
index 0000000..86aa1af
--- /dev/null
+++ b/secure_storage/ta/Android.mk
@@ -0,0 +1,4 @@
+LOCAL_PATH := $(call my-dir)
+
+local_module := f4e750bb-1437-4fbf-8785-8d3580c34994.ta
+include $(BUILD_OPTEE_MK)
diff --git a/secure_storage/ta/Makefile b/secure_storage/ta/Makefile
new file mode 100644
index 0000000..3c25f4a
--- /dev/null
+++ b/secure_storage/ta/Makefile
@@ -0,0 +1,13 @@
+CFG_TEE_TA_LOG_LEVEL ?= 2
+CPPFLAGS += -DCFG_TEE_TA_LOG_LEVEL=$(CFG_TEE_TA_LOG_LEVEL)
+
+# The UUID for the Trusted Application
+BINARY=f4e750bb-1437-4fbf-8785-8d3580c34994
+
+-include $(TA_DEV_KIT_DIR)/mk/ta_dev_kit.mk
+
+ifeq ($(wildcard $(TA_DEV_KIT_DIR)/mk/ta_dev_kit.mk), )
+clean:
+	@echo 'Note: $$(TA_DEV_KIT_DIR)/mk/ta_dev_kit.mk not found, cannot clean TA'
+	@echo 'Note: TA_DEV_KIT_DIR=$(TA_DEV_KIT_DIR)'
+endif
diff --git a/secure_storage/ta/include/secure_storage_ta.h b/secure_storage/ta/include/secure_storage_ta.h
new file mode 100644
index 0000000..52f7424
--- /dev/null
+++ b/secure_storage/ta/include/secure_storage_ta.h
@@ -0,0 +1,61 @@
+/*
+ * Copyright (c) 2017, 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 __SECURE_STORAGE_H__
+#define __SECURE_STORAGE_H__
+
+/* UUID of the trusted application */
+#define TA_SECURE_STORAGE_UUID \
+		{ 0xf4e750bb, 0x1437, 0x4fbf, \
+			{ 0x87, 0x85, 0x8d, 0x35, 0x80, 0xc3, 0x49, 0x94 } }
+/*
+ * TA_SECURE_STORAGE_CMD_READ_RAW - Create and fill a secure storage file
+ * param[0] (memref) ID used the identify the persistent object
+ * param[1] (memref) Raw data dumped from the persistent object
+ * param[2] unused
+ * param[3] unused
+ */
+#define TA_SECURE_STORAGE_CMD_READ_RAW		0
+
+/*
+ * TA_SECURE_STORAGE_CMD_WRITE_RAW - Create and fill a secure storage file
+ * param[0] (memref) ID used the identify the persistent object
+ * param[1] (memref) Raw data to be writen in the persistent object
+ * param[2] unused
+ * param[3] unused
+ */
+#define TA_SECURE_STORAGE_CMD_WRITE_RAW		1
+
+/*
+ * TA_SECURE_STORAGE_CMD_DELETE - Delete a persistent object
+ * param[0] (memref) ID used the identify the persistent object
+ * param[1] unused
+ * param[2] unused
+ * param[3] unused
+ */
+#define TA_SECURE_STORAGE_CMD_DELETE		2
+
+#endif /* __SECURE_STORAGE_H__ */
diff --git a/secure_storage/ta/secure_storage_ta.c b/secure_storage/ta/secure_storage_ta.c
new file mode 100644
index 0000000..b038a9d
--- /dev/null
+++ b/secure_storage/ta/secure_storage_ta.c
@@ -0,0 +1,240 @@
+/*
+ * Copyright (c) 2017, 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 <secure_storage_ta.h>
+#include <tee_internal_api.h>
+#include <tee_internal_api_extensions.h>
+
+static TEE_Result delete_object(uint32_t param_types, TEE_Param params[4])
+{
+	const uint32_t exp_param_types =
+		TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INPUT,
+				TEE_PARAM_TYPE_NONE,
+				TEE_PARAM_TYPE_NONE,
+				TEE_PARAM_TYPE_NONE);
+	TEE_ObjectHandle object;
+	TEE_Result res;
+	char *obj_id;
+	size_t obj_id_sz;
+
+	/*
+	 * Safely get the invocation parameters
+	 */
+	if (param_types != exp_param_types)
+		return TEE_ERROR_BAD_PARAMETERS;
+
+	obj_id = (char *)params[0].memref.buffer;
+	obj_id_sz = params[0].memref.size;
+
+	/*
+	 * Check object exists and delete it
+	 */
+	res = TEE_OpenPersistentObject(TEE_STORAGE_PRIVATE,
+					obj_id, obj_id_sz,
+					TEE_DATA_FLAG_ACCESS_READ |
+					TEE_DATA_FLAG_ACCESS_WRITE_META, /* we must be allowed to delete it */
+					&object);
+	if (res != TEE_SUCCESS) {
+		EMSG("Failed to open persistent object, res=0x%08x", res);
+		return res;
+	}
+
+	TEE_CloseAndDeletePersistentObject1(object);
+
+	return res;
+}
+
+static TEE_Result create_raw_object(uint32_t param_types, TEE_Param params[4])
+{
+	const uint32_t exp_param_types =
+		TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INPUT,
+				TEE_PARAM_TYPE_MEMREF_INPUT,
+				TEE_PARAM_TYPE_NONE,
+				TEE_PARAM_TYPE_NONE);
+	TEE_ObjectHandle object;
+	TEE_Result res;
+	char *obj_id;
+	size_t obj_id_sz;
+	char *data;
+	size_t data_sz;
+	uint32_t obj_data_flag;
+
+	/*
+	 * Safely get the invocation parameters
+	 */
+	if (param_types != exp_param_types)
+		return TEE_ERROR_BAD_PARAMETERS;
+
+	obj_id = (char *)params[0].memref.buffer;
+	obj_id_sz = params[0].memref.size;
+
+	data = (char *)params[1].memref.buffer;
+	data_sz = params[1].memref.size;
+
+	/*
+	 * Create object in secure storage and fill with data
+	 */
+	obj_data_flag = TEE_DATA_FLAG_ACCESS_READ |		/* we can later read the oject */
+			TEE_DATA_FLAG_ACCESS_WRITE |		/* we can later write into the object */
+			TEE_DATA_FLAG_ACCESS_WRITE_META |	/* we can later destroy or rename the object */
+			TEE_DATA_FLAG_OVERWRITE;		/* destroy existing object of same ID */
+
+	res = TEE_CreatePersistentObject(TEE_STORAGE_PRIVATE,
+					obj_id, obj_id_sz,
+					obj_data_flag,
+					TEE_HANDLE_NULL,
+					NULL, 0,		/* we may not fill it right now */
+					&object);
+	if (res != TEE_SUCCESS) {
+		EMSG("TEE_CreatePersistentObject failed 0x%08x", res);
+		return res;
+	}
+
+	res = TEE_WriteObjectData(object, data, data_sz);
+	if (res != TEE_SUCCESS) {
+		EMSG("TEE_WriteObjectData failed 0x%08x", res);
+		TEE_CloseAndDeletePersistentObject1(object);
+	} else {
+		TEE_CloseObject(object);
+	}
+	return res;
+}
+
+static TEE_Result read_raw_object(uint32_t param_types, TEE_Param params[4])
+{
+	const uint32_t exp_param_types =
+		TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INPUT,
+				TEE_PARAM_TYPE_MEMREF_OUTPUT,
+				TEE_PARAM_TYPE_NONE,
+				TEE_PARAM_TYPE_NONE);
+	TEE_ObjectHandle object;
+	TEE_ObjectInfo object_info;
+	TEE_Result res;
+	size_t read_bytes;
+	char *obj_id;
+	size_t obj_id_sz;
+	char *data;
+	size_t data_sz;
+
+	/*
+	 * Safely get the invocation parameters
+	 */
+	if (param_types != exp_param_types)
+		return TEE_ERROR_BAD_PARAMETERS;
+
+	obj_id = (char *)params[0].memref.buffer;
+	obj_id_sz = params[0].memref.size;
+
+	data = (char *)params[1].memref.buffer;
+	data_sz = params[1].memref.size;
+
+	/*
+	 * Check the object exist and can be dumped into output buffer
+	 * then dump it.
+	 */
+	res = TEE_OpenPersistentObject(TEE_STORAGE_PRIVATE,
+					obj_id, obj_id_sz,
+					TEE_DATA_FLAG_ACCESS_READ |
+					TEE_DATA_FLAG_SHARE_READ,
+					&object);
+	if (res != TEE_SUCCESS) {
+		EMSG("Failed to open persistent object, res=0x%08x", res);
+		return res;
+	}
+
+	res = TEE_GetObjectInfo1(object, &object_info);
+	if (res != TEE_SUCCESS) {
+		EMSG("Failed to create persistent object, res=0x%08x", res);
+		goto exit;
+	}
+
+	if (object_info.dataSize > data_sz) {
+		/*
+		 * Provided buffer is too short.
+		 * Return the expected size together with status "short buffer"
+		 */
+		params[1].memref.size = object_info.dataSize;
+		res = TEE_ERROR_SHORT_BUFFER;
+		goto exit;
+	}
+
+	res = TEE_ReadObjectData(object, data, object_info.dataSize,
+				 &read_bytes);
+	if (res != TEE_SUCCESS || read_bytes != object_info.dataSize) {
+		EMSG("TEE_ReadObjectData failed 0x%08x, read %u over %u",
+				res, read_bytes, object_info.dataSize);
+		goto exit;
+	}
+
+	/* Return the number of byte effectively filled */
+	params[1].memref.size = read_bytes;
+exit:
+	TEE_CloseObject(object);
+	return res;
+}
+
+TEE_Result TA_CreateEntryPoint(void)
+{
+	/* Nothing to do */
+	return TEE_SUCCESS;
+}
+
+void TA_DestroyEntryPoint(void)
+{
+	/* Nothing to do */
+}
+
+TEE_Result TA_OpenSessionEntryPoint(uint32_t __unused param_types,
+				    TEE_Param __unused params[4],
+				    void __unused **session)
+{
+	/* Nothing to do */
+	return TEE_SUCCESS;
+}
+
+void TA_CloseSessionEntryPoint(void __unused *session)
+{
+	/* Nothing to do */
+}
+
+TEE_Result TA_InvokeCommandEntryPoint(void __unused *session,
+				      uint32_t command,
+				      uint32_t param_types,
+				      TEE_Param params[4])
+{
+	switch (command) {
+	case TA_SECURE_STORAGE_CMD_WRITE_RAW:
+		return create_raw_object(param_types, params);
+	case TA_SECURE_STORAGE_CMD_READ_RAW:
+		return read_raw_object(param_types, params);
+	case TA_SECURE_STORAGE_CMD_DELETE:
+		return delete_object(param_types, params);
+	default:
+		EMSG("Command ID 0x%x is not supported", command);
+		return TEE_ERROR_NOT_SUPPORTED;
+	}
+}
diff --git a/secure_storage/ta/sub.mk b/secure_storage/ta/sub.mk
new file mode 100644
index 0000000..e1f19de
--- /dev/null
+++ b/secure_storage/ta/sub.mk
@@ -0,0 +1,2 @@
+global-incdirs-y += include
+srcs-y += secure_storage_ta.c
diff --git a/secure_storage/ta/user_ta_header_defines.h b/secure_storage/ta/user_ta_header_defines.h
new file mode 100644
index 0000000..cc69e72
--- /dev/null
+++ b/secure_storage/ta/user_ta_header_defines.h
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2017, 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.
+ */
+
+/*
+ * The name of this file must not be modified
+ */
+
+#ifndef USER_TA_HEADER_DEFINES_H
+#define USER_TA_HEADER_DEFINES_H
+
+#include <secure_storage_ta.h>
+
+#define TA_UUID				TA_SECURE_STORAGE_UUID
+
+#define TA_FLAGS			(TA_FLAG_EXEC_DDR | TA_FLAG_SINGLE_INSTANCE)
+#define TA_STACK_SIZE			(2 * 1024)
+#define TA_DATA_SIZE			(32 * 1024)
+
+#define TA_CURRENT_TA_EXT_PROPERTIES \
+    { "gp.ta.description", USER_TA_PROP_TYPE_STRING, \
+        "Example of TA writing/reading data from its secure storage" }, \
+    { "gp.ta.version", USER_TA_PROP_TYPE_U32, &(const uint32_t){ 0x0010 } }
+
+#endif /*USER_TA_HEADER_DEFINES_H*/