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/.gitignore b/.gitignore
index e90e7d0..0164fde 100644
--- a/.gitignore
+++ b/.gitignore
@@ -17,3 +17,4 @@
random/host/optee_example_random
aes/host/optee_example_aes
hotp/host/optee_example_hotp
+secure_storage/host/optee_example_secure_storage
diff --git a/README.md b/README.md
index fb96770..100f02f 100644
--- a/README.md
+++ b/README.md
@@ -33,6 +33,12 @@
* Test application: `optee_example_aes`
* Trusted application UUID: 5dbac793-f574-4871-8ad3-04331ec17f24
+Directory **secure_storage/**:
+* A Trusted Application to read/write raw data into the
+OP-TEE secure storage using the GPD TEE Internal Core API.
+* Test application: `optee_example_secure_storage`
+* Trusted application UUID: f4e750bb-1437-4fbf-8785-8d3580c34994
+
## 3. How to build a Trusted Application
[TA basics] documentation presents the basics for implementing and building
an OP-TEE trusted application.
diff --git a/secure_storage/Android.mk b/secure_storage/Android.mk
new file mode 100644
index 0000000..84e540f
--- /dev/null
+++ b/secure_storage/Android.mk
@@ -0,0 +1,19 @@
+LOCAL_PATH := $(call my-dir)
+
+OPTEE_CLIENT_EXPORT ?= $(LOCAL_PATH)/../../optee_client/out/export
+
+include $(CLEAR_VARS)
+LOCAL_CFLAGS += -DANDROID_BUILD
+LOCAL_CFLAGS += -Wall
+
+LOCAL_SRC_FILES += host/main.c
+
+LOCAL_C_INCLUDES := $(LOCAL_PATH)/ta/include \
+ $(OPTEE_CLIENT_EXPORT)/include
+
+LOCAL_SHARED_LIBRARIES := libteec
+LOCAL_MODULE := optee_example_secure_storage
+LOCAL_MODULE_TAGS := optional
+include $(BUILD_EXECUTABLE)
+
+include $(LOCAL_PATH)/ta/Android.mk
diff --git a/secure_storage/Makefile b/secure_storage/Makefile
new file mode 100644
index 0000000..3b9a8dc
--- /dev/null
+++ b/secure_storage/Makefile
@@ -0,0 +1,15 @@
+export V ?= 0
+
+# If _HOST or _TA specific compilers are not specified, then use CROSS_COMPILE
+HOST_CROSS_COMPILE ?= $(CROSS_COMPILE)
+TA_CROSS_COMPILE ?= $(CROSS_COMPILE)
+
+.PHONY: all
+all:
+ $(MAKE) -C host CROSS_COMPILE="$(HOST_CROSS_COMPILE)"
+ $(MAKE) -C ta CROSS_COMPILE="$(TA_CROSS_COMPILE)"
+
+.PHONY: clean
+clean:
+ $(MAKE) -C host clean
+ $(MAKE) -C ta clean
diff --git a/secure_storage/host/Makefile b/secure_storage/host/Makefile
new file mode 100644
index 0000000..5e2c801
--- /dev/null
+++ b/secure_storage/host/Makefile
@@ -0,0 +1,25 @@
+CC = $(CROSS_COMPILE)gcc
+LD = $(CROSS_COMPILE)ld
+AR = $(CROSS_COMPILE)ar
+NM = $(CROSS_COMPILE)nm
+OBJCOPY = $(CROSS_COMPILE)objcopy
+OBJDUMP = $(CROSS_COMPILE)objdump
+READELF = $(CROSS_COMPILE)readelf
+
+OBJS = main.o
+
+CFLAGS += -Wall -I../ta/include -I./include
+CFLAGS += -I$(TEEC_EXPORT)/include
+LDADD += -lteec -L$(TEEC_EXPORT)/lib
+
+BINARY = optee_example_secure_storage
+
+.PHONY: all
+all: $(BINARY)
+
+$(BINARY): $(OBJS)
+ $(CC) -o $@ $< $(LDADD)
+
+.PHONY: clean
+clean:
+ rm -f $(OBJS) $(BINARY)
diff --git a/secure_storage/host/main.c b/secure_storage/host/main.c
new file mode 100644
index 0000000..64b9fae
--- /dev/null
+++ b/secure_storage/host/main.c
@@ -0,0 +1,241 @@
+/*
+ * 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 <err.h>
+#include <stdio.h>
+#include <string.h>
+
+/* OP-TEE TEE client API (built by optee_client) */
+#include <tee_client_api.h>
+
+/* TA API: UUID and command IDs */
+#include <secure_storage_ta.h>
+
+/* TEE resources */
+struct test_ctx {
+ TEEC_Context ctx;
+ TEEC_Session sess;
+};
+
+void prepare_tee_session(struct test_ctx *ctx)
+{
+ TEEC_UUID uuid = TA_SECURE_STORAGE_UUID;
+ uint32_t origin;
+ TEEC_Result res;
+
+ /* Initialize a context connecting us to the TEE */
+ res = TEEC_InitializeContext(NULL, &ctx->ctx);
+ if (res != TEEC_SUCCESS)
+ errx(1, "TEEC_InitializeContext failed with code 0x%x", res);
+
+ /* Open a session with the TA */
+ res = TEEC_OpenSession(&ctx->ctx, &ctx->sess, &uuid,
+ TEEC_LOGIN_PUBLIC, NULL, NULL, &origin);
+ if (res != TEEC_SUCCESS)
+ errx(1, "TEEC_Opensession failed with code 0x%x origin 0x%x",
+ res, origin);
+}
+
+void terminate_tee_session(struct test_ctx *ctx)
+{
+ TEEC_CloseSession(&ctx->sess);
+ TEEC_FinalizeContext(&ctx->ctx);
+}
+
+TEEC_Result read_secure_object(struct test_ctx *ctx, char *id,
+ char *data, size_t data_len)
+{
+ TEEC_Operation op;
+ uint32_t origin;
+ TEEC_Result res;
+ size_t id_len = strlen(id);
+
+ memset(&op, 0, sizeof(op));
+ op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
+ TEEC_MEMREF_TEMP_OUTPUT,
+ TEEC_NONE, TEEC_NONE);
+
+ op.params[0].tmpref.buffer = id;
+ op.params[0].tmpref.size = id_len;
+
+ op.params[1].tmpref.buffer = data;
+ op.params[1].tmpref.size = data_len;
+
+ res = TEEC_InvokeCommand(&ctx->sess,
+ TA_SECURE_STORAGE_CMD_READ_RAW,
+ &op, &origin);
+ switch (res) {
+ case TEEC_SUCCESS:
+ case TEEC_ERROR_SHORT_BUFFER:
+ case TEEC_ERROR_ITEM_NOT_FOUND:
+ break;
+ default:
+ printf("Command READ_RAW failed: 0x%x / %u\n", res, origin);
+ }
+
+ return res;
+}
+
+TEEC_Result write_secure_object(struct test_ctx *ctx, char *id,
+ char *data, size_t data_len)
+{
+ TEEC_Operation op;
+ uint32_t origin;
+ TEEC_Result res;
+ size_t id_len = strlen(id);
+
+ memset(&op, 0, sizeof(op));
+ op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
+ TEEC_MEMREF_TEMP_INPUT,
+ TEEC_NONE, TEEC_NONE);
+
+ op.params[0].tmpref.buffer = id;
+ op.params[0].tmpref.size = id_len;
+
+ op.params[1].tmpref.buffer = data;
+ op.params[1].tmpref.size = data_len;
+
+ res = TEEC_InvokeCommand(&ctx->sess,
+ TA_SECURE_STORAGE_CMD_WRITE_RAW,
+ &op, &origin);
+ if (res != TEEC_SUCCESS)
+ printf("Command WRITE_RAW failed: 0x%x / %u\n", res, origin);
+
+ switch (res) {
+ case TEEC_SUCCESS:
+ break;
+ default:
+ printf("Command WRITE_RAW failed: 0x%x / %u\n", res, origin);
+ }
+
+ return res;
+}
+
+TEEC_Result delete_secure_object(struct test_ctx *ctx, char *id)
+{
+ TEEC_Operation op;
+ uint32_t origin;
+ TEEC_Result res;
+ size_t id_len = strlen(id);
+
+ memset(&op, 0, sizeof(op));
+ op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
+ TEEC_NONE, TEEC_NONE, TEEC_NONE);
+
+ op.params[0].tmpref.buffer = id;
+ op.params[0].tmpref.size = id_len;
+
+ res = TEEC_InvokeCommand(&ctx->sess,
+ TA_SECURE_STORAGE_CMD_DELETE,
+ &op, &origin);
+
+ switch (res) {
+ case TEEC_SUCCESS:
+ case TEEC_ERROR_ITEM_NOT_FOUND:
+ break;
+ default:
+ printf("Command DELETE failed: 0x%x / %u\n", res, origin);
+ }
+
+ return res;
+}
+
+#define TEST_OBJECT_SIZE 7000
+
+int main(int argc, char *argv[])
+{
+ struct test_ctx ctx;
+ char obj1_id[] = "object#1"; /* string identification for the object */
+ char obj2_id[] = "object#2"; /* string identification for the object */
+ char obj1_data[TEST_OBJECT_SIZE];
+ char read_data[TEST_OBJECT_SIZE];
+ TEEC_Result res;
+
+ printf("Prepare session with the TA\n");
+ prepare_tee_session(&ctx);
+
+ /*
+ * Create object, read it, delete it.
+ */
+ printf("\nTest on object \"%s\"\n", obj1_id);
+
+ printf("- Create and load object in the TA secure storage\n");
+
+ memset(obj1_data, 0xA1, sizeof(obj1_data));
+
+ res = write_secure_object(&ctx, obj1_id,
+ obj1_data, sizeof(obj1_data));
+ if (res != TEEC_SUCCESS)
+ errx(1, "Failed to create an object in the secure storage");
+
+ printf("- Read back the object\n");
+
+ res = read_secure_object(&ctx, obj1_id,
+ read_data, sizeof(read_data));
+ if (res != TEEC_SUCCESS)
+ errx(1, "Failed to read an object from the secure storage");
+ if (memcmp(obj1_data, read_data, sizeof(obj1_data)))
+ errx(1, "Unexpected content found in secure storage");
+
+ printf("- Delete the object\n");
+
+ res = delete_secure_object(&ctx, obj1_id);
+ if (res != TEEC_SUCCESS)
+ errx(1, "Failed to delete the object: 0x%x", res);
+
+ /*
+ * Non volatile storage: create object2 if not found, delete it if found
+ */
+ printf("\nTest on object \"%s\"\n", obj2_id);
+
+ res = read_secure_object(&ctx, obj2_id,
+ read_data, sizeof(read_data));
+ if (res != TEEC_SUCCESS && res != TEEC_ERROR_ITEM_NOT_FOUND)
+ errx(1, "Unexpected status when reading an object : 0x%x", res);
+
+ if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
+ char data[] = "This is data stored in the secure storage.\n";
+
+ printf("- Object not found in TA secure storage, create it.\n");
+
+ res = write_secure_object(&ctx, obj2_id,
+ data, sizeof(data));
+ if (res != TEEC_SUCCESS)
+ errx(1, "Failed to create/load an object");
+
+ } else if (res == TEEC_SUCCESS) {
+ printf("- Object found in TA secure storage, delete it.\n");
+
+ res = delete_secure_object(&ctx, obj2_id);
+ if (res != TEEC_SUCCESS)
+ errx(1, "Failed to delete an object");
+ }
+
+ printf("\nWe're done, close and release TEE resources\n");
+ terminate_tee_session(&ctx);
+ return 0;
+}
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*/