├── README.md ├── ta ├── Android.mk ├── Makefile ├── sub.mk ├── include │ ├── my_test_ta.h │ └── my_test_handle.h ├── user_ta_header_defines.h ├── my_test.c └── my_test_handle.c ├── Makefile ├── doc ├── Makefile ├── close_session_and_finalize_context.msc ├── invoke_command.msc └── open_session.msc ├── host ├── Makefile ├── my_test_ca.h └── main.c ├── Android.mk ├── optee_my_test_qemu_2.6.0.patch └── optee_my_test_common_2.6.0.patch /README.md: -------------------------------------------------------------------------------- 1 | 本git中包含了OPEN-TEE中的一个简单的TA和CA的demo代码 2 | 3 | -------------------------------------------------------------------------------- /ta/Android.mk: -------------------------------------------------------------------------------- 1 | LOCAL_PATH := $(call my-dir) 2 | 3 | local_module := 8aaaf200-2450-11e4-abe2-0002a5d5c51b.ta 4 | include $(BUILD_OPTEE_MK) 5 | -------------------------------------------------------------------------------- /ta/Makefile: -------------------------------------------------------------------------------- 1 | CFG_TEE_TA_LOG_LEVEL ?= 4 2 | CPPFLAGS += -DCFG_TEE_TA_LOG_LEVEL=$(CFG_TEE_TA_LOG_LEVEL) 3 | 4 | # The UUID for the Trusted Application 5 | BINARY=9269fadd-99d5-4afb-a1dc-ee3e9c61b04c 6 | 7 | include $(TA_DEV_KIT_DIR)/mk/ta_dev_kit.mk 8 | -------------------------------------------------------------------------------- /ta/sub.mk: -------------------------------------------------------------------------------- 1 | global-incdirs-y += include 2 | #global-incdirs-y += ../host/include 3 | srcs-y += my_test.c 4 | srcs-y += my_test_handle.c 5 | 6 | # To remove a certain compiler flag, add a line like this 7 | #cflags-template_ta.c-y += -Wno-strict-prototypes 8 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | export V?=0 3 | 4 | .PHONY: all 5 | all: 6 | $(MAKE) -C host CROSS_COMPILE="$(HOST_CROSS_COMPILE)" 7 | $(MAKE) -C ta CROSS_COMPILE="$(TA_CROSS_COMPILE)" 8 | 9 | .PHONY: clean 10 | clean: 11 | $(MAKE) -C host clean 12 | $(MAKE) -C ta clean 13 | -------------------------------------------------------------------------------- /doc/Makefile: -------------------------------------------------------------------------------- 1 | 2 | .PHONY: all 3 | all: 4 | 5 | MSC_SRCS = open_session.msc invoke_command.msc \ 6 | close_session_and_finalize_context.msc 7 | 8 | PNGS += $(MSC_SRCS:.msc=.png) 9 | 10 | # Disable all builtin rules 11 | .SUFFIXES: 12 | .SUFFIXES: .png .msc 13 | 14 | all: $(PNGS) 15 | 16 | %.png : %.msc 17 | mscgen -T png $< 18 | 19 | .PHONY: clean 20 | clean: 21 | rm -f $(PNGS) 22 | -------------------------------------------------------------------------------- /doc/close_session_and_finalize_context.msc: -------------------------------------------------------------------------------- 1 | msc { 2 | hscale = "0.9", wordwraparcs = on; 3 | 4 | a [label="hello_world\nuser space"], 5 | b [label="hello world\nTEE Driver"], 6 | f [label="TEE Core"], 7 | g [label="TA"]; 8 | 9 | ||| ; 10 | a=>b [ label = "Close\nSession" ]; 11 | b=>f [ label = "Close\nSession" ]; 12 | f=>g [ label = "TA_CloseSessionEntryPoint" ]; 13 | g>>f ; 14 | f>>b ; 15 | b>>a ; 16 | a=>b [ label = "Finalize\nContext" ]; 17 | b>>a ; 18 | } 19 | -------------------------------------------------------------------------------- /doc/invoke_command.msc: -------------------------------------------------------------------------------- 1 | msc { 2 | hscale = "0.9", wordwraparcs = on; 3 | 4 | a [label="hello_world\nuser space"], 5 | b [label="hello world\nTEE Driver"], 6 | f [label="TEE Core"], 7 | g [label="TA"]; 8 | 9 | ||| ; 10 | a=>b [ label = "Invoke\nCommand" ]; 11 | b=>f [ label = "Invoke\nCommand" ]; 12 | f=>g [ label = "TA_InvokeCommandEntryPoint" ]; 13 | g>>f [ label = "ret: result" ]; 14 | f>>b [ label = "ret: result" ]; 15 | b>>a [ label = "ret: result" ]; 16 | } 17 | -------------------------------------------------------------------------------- /host/Makefile: -------------------------------------------------------------------------------- 1 | CC = $(CROSS_COMPILE)gcc 2 | LD = $(CROSS_COMPILE)ld 3 | AR = $(CROSS_COMPILE)ar 4 | NM = $(CROSS_COMPILE)nm 5 | OBJCOPY = $(CROSS_COMPILE)objcopy 6 | OBJDUMP = $(CROSS_COMPILE)objdump 7 | READELF = $(CROSS_COMPILE)readelf 8 | 9 | OBJS = main.o 10 | 11 | CFLAGS += -Wall -I../ta/include -I$(TEEC_EXPORT)/include -I./include 12 | #Add/link other required libraries here 13 | LDADD += -lteec -L$(TEEC_EXPORT)/lib 14 | 15 | BINARY=my_test 16 | 17 | .PHONY: all 18 | all: $(BINARY) 19 | 20 | $(BINARY): $(OBJS) 21 | $(CC) $(LDADD) -o $@ $< 22 | 23 | .PHONY: clean 24 | clean: 25 | rm -f $(OBJS) $(BINARY) 26 | -------------------------------------------------------------------------------- /doc/open_session.msc: -------------------------------------------------------------------------------- 1 | msc { 2 | hscale = "0.9", wordwraparcs = on; 3 | 4 | a [label="hello_world\nuser space"], 5 | b [label="hello world\nTEE Driver"], 6 | c [label="tee-supplicant"], 7 | f [label="TEE Core"], 8 | g [label="TA"]; 9 | 10 | ||| ; 11 | a=>b [ label = "Open\nSession" ]; 12 | b=>f [ label = "Open\nSession" ]; 13 | f=>f [ label = "Looking for TA" ]; 14 | f=>c [ label = "Load TA" ]; 15 | c=>c [ label = "Loading TA" ]; 16 | c>>f [ label = "Load TA" ]; 17 | f=>f [ label = "Load TA" ]; 18 | f=>g [ label = "TA_OpenSessionEntryPoint" ]; 19 | g>>f ; 20 | f>>b [ label = "ret: session" ]; 21 | b>>a [ label = "ret: session" ]; 22 | } 23 | -------------------------------------------------------------------------------- /Android.mk: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Android optee-hello-world makefile # 3 | ################################################################################ 4 | LOCAL_PATH := $(call my-dir) 5 | 6 | CFG_TEEC_PUBLIC_INCLUDE = $(LOCAL_PATH)/../optee_client/public 7 | 8 | ################################################################################ 9 | # Build hello world # 10 | ################################################################################ 11 | include $(CLEAR_VARS) 12 | LOCAL_CFLAGS += -DANDROID_BUILD 13 | LOCAL_CFLAGS += -Wall 14 | 15 | LOCAL_SRC_FILES += host/main.c 16 | 17 | LOCAL_C_INCLUDES := $(LOCAL_PATH)/ta/include \ 18 | $(CFG_TEEC_PUBLIC_INCLUDE) \ 19 | 20 | LOCAL_SHARED_LIBRARIES := libteec 21 | LOCAL_MODULE := tee_helloworld 22 | LOCAL_MODULE_TAGS := optional 23 | include $(BUILD_EXECUTABLE) 24 | 25 | include $(LOCAL_PATH)/ta/Android.mk 26 | -------------------------------------------------------------------------------- /optee_my_test_qemu_2.6.0.patch: -------------------------------------------------------------------------------- 1 | diff --git a/qemu.mk b/qemu.mk 2 | index 54b5882..9a4a1a2 100644 3 | --- a/qemu.mk 4 | +++ b/qemu.mk 5 | @@ -28,7 +28,7 @@ ifeq ($(CFG_TEE_BENCHMARK),y) 6 | all: benchmark-app 7 | clean: benchmark-app-clean 8 | endif 9 | -all: bios-qemu qemu soc-term optee-examples 10 | +all: bios-qemu qemu soc-term optee-examples optee_my_test 11 | clean: bios-qemu-clean busybox-clean linux-clean optee-os-clean \ 12 | optee-client-clean qemu-clean soc-term-clean check-clean \ 13 | optee-examples-clean 14 | @@ -140,6 +140,18 @@ optee-examples: optee-examples-common 15 | 16 | optee-examples-clean: optee-examples-clean-common 17 | 18 | + 19 | +################################################################################ 20 | +# optee_my_test 21 | +################################################################################ 22 | +optee_my_test: optee_my_test-common 23 | + 24 | +optee_my_test-clean: optee_my_test-clean-common 25 | + 26 | +################################################################################ 27 | + 28 | + 29 | + 30 | ################################################################################ 31 | # benchmark 32 | ################################################################################ 33 | -------------------------------------------------------------------------------- /ta/include/my_test_ta.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, Linaro Limited 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | #ifndef TA_MY_TEST_H 28 | #define TA_MY_TEST_H 29 | 30 | /* This UUID is generated with uuidgen 31 | the ITU-T UUID generator at http://www.itu.int/ITU-T/asn1/uuid.html */ 32 | /* 9269fadd-99d5-4afb-a1dc-ee3e9c61b04c */ 33 | #define TA_MY_TEST_UUID { 0x9269fadd, 0x99d5, 0x4afb, \ 34 | { 0xa1, 0xdc, 0xee, 0x3e, 0x9c, 0x61, 0xb0, 0x4c} } 35 | 36 | /* The Trusted Application Function ID(s) implemented in this TA */ 37 | #define TA_MY_TEST_CMD_INC_VALUE 0 38 | #define TA_MY_TEST_CMD_HASH 1 39 | #define TA_MY_TEST_CMD_RANDOM 2 40 | 41 | 42 | #define FAIL -1 43 | #define OK 0 44 | #define TEE_ALG_INVALID 0xFFFFFFFFU 45 | 46 | 47 | #endif /*TA_HELLO_WORLD_H*/ 48 | -------------------------------------------------------------------------------- /ta/user_ta_header_defines.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, Linaro Limited 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | /* 29 | * The name of this file must not be modified 30 | */ 31 | 32 | #ifndef USER_TA_HEADER_DEFINES_H 33 | #define USER_TA_HEADER_DEFINES_H 34 | 35 | #include /* To get the TA_MY_TEST_UUID define */ 36 | 37 | #define TA_UUID TA_MY_TEST_UUID 38 | 39 | #define TA_FLAGS (TA_FLAG_MULTI_SESSION | TA_FLAG_EXEC_DDR) 40 | #define TA_STACK_SIZE (2 * 1024) 41 | #define TA_DATA_SIZE (32 * 1024) 42 | 43 | #define TA_CURRENT_TA_EXT_PROPERTIES \ 44 | { "gp.ta.description", USER_TA_PROP_TYPE_STRING, \ 45 | "My Test TA" }, \ 46 | { "gp.ta.version", USER_TA_PROP_TYPE_U32, &(const uint32_t){ 0x0010 } } 47 | 48 | #endif /*USER_TA_HEADER_DEFINES_H*/ 49 | -------------------------------------------------------------------------------- /optee_my_test_common_2.6.0.patch: -------------------------------------------------------------------------------- 1 | diff --git a/common.mk b/common.mk 2 | index 15116ca..6af2f0c 100644 3 | --- a/common.mk 4 | +++ b/common.mk 5 | @@ -19,6 +19,7 @@ OPTEE_TEST_OUT_PATH ?= $(ROOT)/optee_test/out 6 | OPTEE_EXAMPLES_PATH ?= $(ROOT)/optee_examples 7 | BENCHMARK_APP_PATH ?= $(ROOT)/optee_benchmark 8 | LIBYAML_LIB_PATH ?= $(BENCHMARK_APP_PATH)/libyaml/out/lib 9 | +OPTEE_MYTEST_PATH ?= $(ROOT)/optee_my_test 10 | 11 | # default high verbosity. slow uarts shall specify lower if prefered 12 | CFG_TEE_CORE_LOG_LEVEL ?= 3 13 | @@ -311,7 +312,7 @@ OPTEE_OS_CLEAN_COMMON_FLAGS ?= $(OPTEE_OS_COMMON_EXTRA_FLAGS) 14 | ifeq ($(CFG_TEE_BENCHMARK),y) 15 | optee-os-clean-common: benchmark-app-clean-common 16 | endif 17 | -optee-os-clean-common: xtest-clean optee-examples-clean 18 | +optee-os-clean-common: xtest-clean optee-examples-clean optee_my_test-clean 19 | $(MAKE) -C $(OPTEE_OS_PATH) $(OPTEE_OS_CLEAN_COMMON_FLAGS) clean 20 | 21 | OPTEE_CLIENT_COMMON_FLAGS ?= CROSS_COMPILE=$(CROSS_COMPILE_NS_USER) \ 22 | @@ -376,6 +377,24 @@ optee-examples-clean-common: 23 | $(OPTEE_EXAMPLES_CLEAN_COMMON_FLAGS) clean 24 | 25 | ################################################################################ 26 | +# optee_my_test 27 | +################################################################################ 28 | +OPTEE_MYTEST_COMMON_FLAGS ?= HOST_CROSS_COMPILE=$(CROSS_COMPILE_NS_USER)\ 29 | + TA_CROSS_COMPILE=$(CROSS_COMPILE_S_USER) \ 30 | + TA_DEV_KIT_DIR=$(OPTEE_OS_TA_DEV_KIT_DIR) \ 31 | + TEEC_EXPORT=$(OPTEE_CLIENT_EXPORT) 32 | + 33 | +.PHONY: optee_my_test-common 34 | +optee_my_test-common: optee-os optee-client 35 | + $(MAKE) -C $(OPTEE_MYTEST_PATH) $(OPTEE_MYTEST_COMMON_FLAGS) 36 | + 37 | +OPTEE_MYTEST_CLEAN_COMMON_FLAGS ?= TA_DEV_KIT_DIR=$(OPTEE_OS_TA_DEV_KIT_DIR) 38 | + 39 | +.PHONY: optee_my_test-clean-common 40 | +optee_my_test-clean-common: 41 | + $(MAKE) -C $(OPTEE_MYTEST_PATH) $(OPTEE_MYTEST_CLEAN_COMMON_FLAGS) clean 42 | + 43 | +################################################################################ 44 | # benchmark_app 45 | ################################################################################ 46 | BENCHMARK_APP_COMMON_FLAGS ?= CROSS_COMPILE=$(CROSS_COMPILE_NS_USER) \ 47 | @@ -414,7 +433,7 @@ ifeq ($(CFG_TEE_BENCHMARK),y) 48 | filelist-tee-common: benchmark-app 49 | endif 50 | filelist-tee-common: fl:=$(GEN_ROOTFS_FILELIST) 51 | -filelist-tee-common: optee-client xtest optee-examples 52 | +filelist-tee-common: optee-client xtest optee-examples optee_my_test 53 | @echo "# filelist-tee-common /start" > $(fl) 54 | @echo "dir /lib/optee_armtz 755 0 0" >> $(fl) 55 | @if [ -e $(OPTEE_EXAMPLES_PATH)/out/ca ]; then \ 56 | @@ -429,6 +448,14 @@ filelist-tee-common: optee-client xtest optee-examples 57 | "$$file 755 0 0" >> $(fl); \ 58 | done; \ 59 | fi 60 | + @echo "#optee_mytest " >> $(fl) 61 | + @if [ -e $(OPTEE_MYTEST_PATH)/host/my_test ]; then \ 62 | + echo "file /bin/my_test" \ 63 | + "$(OPTEE_MYTEST_PATH)/host/my_test 755 0 0" >> $(fl); \ 64 | + echo "file /lib/optee_armtz/9269fadd-99d5-4afb-a1dc-ee3e9c61b04c.ta" \ 65 | + "$(OPTEE_MYTEST_PATH)/ta/9269fadd-99d5-4afb-a1dc-ee3e9c61b04c.ta 444 0 0" \ 66 | + >> $(fl); \ 67 | + fi 68 | @echo "# xtest / optee_test" >> $(fl) 69 | @find $(OPTEE_TEST_OUT_PATH) -type f -name "xtest" | \ 70 | sed 's/\(.*\)/file \/bin\/xtest \1 755 0 0/g' >> $(fl) 71 | -------------------------------------------------------------------------------- /host/my_test_ca.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************************/ 2 | /* COPYRIGHT INFORMATION */ 3 | /* This program contains proprietary information which is a trade */ 4 | /* secret of whaley Co., Ltd.and also is protected as an unpublished */ 5 | /* work under applicable Copyright laws. Recipient is to retain this */ 6 | /* program in confidence and is not permitted to use or make copies thereof */ 7 | /* other than as permitted in a written agreement written agreement with */ 8 | /* whaley Co., Ltd. */ 9 | /* */ 10 | /* All rights reserved. shuaifengyun@126.com */ 11 | /****************************************************************************************/ 12 | /* 13 | **************************************************************************************** 14 | * 15 | * test.h 16 | * 17 | * Filename : test.h 18 | * Programmer(s) : system BSP 19 | * Filename : test.h 20 | * Author : Shuai Fengyun 21 | * Mail : shuaifengyun@126.cn 22 | * Create Time : 2017年05月04日 星期四 11时50分54秒 23 | **************************************************************************************** 24 | */ 25 | 26 | #ifndef MOUDLE_NAME_H_ 27 | #define MOUDLE_NAME_H_ 28 | 29 | 30 | 31 | 32 | /* 33 | ******************************************************************************* 34 | * INCLUDE FILES 35 | ******************************************************************************* 36 | */ 37 | 38 | 39 | 40 | 41 | 42 | /* 43 | ******************************************************************************* 44 | * MACRO DEFINITION USED ONLY BY THIS MODULE 45 | ******************************************************************************* 46 | */ 47 | #define TA_MY_TEST_UUID { 0x9269fadd, 0x99d5, 0x4afb, \ 48 | {0xa1, 0xdc, 0xee, 0x3e, 0x9c, 0x61, 0xb0, 0x4c}} 49 | 50 | #define TA_MY_TEST_CMD_INC_VALUE 0 51 | #define TA_MY_TEST_CMD_HASH 1 52 | #define TA_MY_TEST_CMD_RANDOM 2 53 | 54 | 55 | #define FAIL -1 56 | #define OK 0 57 | 58 | 59 | /* 60 | ******************************************************************************* 61 | * STRUCTRUE DEFINITION USED ONLY BY THIS MODULE 62 | ******************************************************************************* 63 | */ 64 | /* SHA operation type */ 65 | typedef enum 66 | { 67 | EN_OP_SHA1 = 1, 68 | EN_OP_SHA224, 69 | EN_OP_SHA256, 70 | EN_OP_SHA384, 71 | EN_OP_SHA512, 72 | EN_OP_SHA_INVALID 73 | }EN_SHA_MODE; 74 | 75 | 76 | /* Define the type of variable */ 77 | typedef unsigned char UINT8; /**< Typedef for 8bits unsigned integer */ 78 | typedef unsigned short UINT16; /**< Typedef for 16bits unsigned integer */ 79 | typedef uint32_t UINT32; /**< Typedef for 32bits unsigned integer */ 80 | typedef signed char INT8; /**< Typedef for 8bits signed integer */ 81 | typedef signed short INT16; /**< Typedef for 16bits signed integer */ 82 | typedef signed int INT32; /**< Typedef for 32bits signed integer */ 83 | typedef char CHAR; /**< Typedef for char */ 84 | 85 | 86 | #ifndef MODULE_NAME_C_ 87 | 88 | 89 | /* 90 | ******************************************************************************* 91 | * VARIABLES SUPPLIED BY THIS MODULE 92 | ******************************************************************************* 93 | */ 94 | 95 | 96 | 97 | 98 | 99 | /* 100 | ******************************************************************************* 101 | * FUNCTIONS SUPPLIED BY THIS MODULE 102 | ******************************************************************************* 103 | */ 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | #endif 126 | 127 | #endif /* MOUDLE_NAME_H*/ 128 | -------------------------------------------------------------------------------- /ta/include/my_test_handle.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************************/ 2 | /* COPYRIGHT INFORMATION */ 3 | /* This program contains proprietary information which is a trade */ 4 | /* secret of whaley Co., Ltd.and also is protected as an unpublished */ 5 | /* work under applicable Copyright laws. Recipient is to retain this */ 6 | /* program in confidence and is not permitted to use or make copies thereof */ 7 | /* other than as permitted in a written agreement written agreement with */ 8 | /* whaley Co., Ltd. */ 9 | /* */ 10 | /* All rights reserved. shuaifengyun@126.com */ 11 | /****************************************************************************************/ 12 | /* 13 | **************************************************************************************** 14 | * 15 | * my_test_handle.h 16 | * 17 | * Filename : my_test_handle.h 18 | * Programmer(s) : system BSP 19 | * Filename : my_test_handle.h 20 | * Author : Shuai Fengyun 21 | * Mail : shuaifengyun@126.com 22 | * Create Time : 2017年05月04日 星期四 10时48分11秒 23 | **************************************************************************************** 24 | */ 25 | 26 | #ifndef MOUDLE_MY_TEST_HANDLE_H_ 27 | #define MOUDLE_MY_TEST_HANDLE_H_ 28 | 29 | 30 | 31 | 32 | /* 33 | ******************************************************************************* 34 | * INCLUDE FILES 35 | ******************************************************************************* 36 | */ 37 | #include "tee_internal_api.h" 38 | #include "tee_api_defines.h" 39 | #include "trace.h" 40 | #include "tee_api_defines_extensions.h" 41 | 42 | 43 | 44 | 45 | /* 46 | ******************************************************************************* 47 | * MACRO DEFINITION USED ONLY BY THIS MODULE 48 | ******************************************************************************* 49 | */ 50 | 51 | 52 | 53 | 54 | 55 | /* 56 | ******************************************************************************* 57 | * STRUCTRUE DEFINITION USED ONLY BY THIS MODULE 58 | ******************************************************************************* 59 | */ 60 | /* SHA operation type */ 61 | typedef enum 62 | { 63 | EN_OP_SHA1 = 1, 64 | EN_OP_SHA224, 65 | EN_OP_SHA256, 66 | EN_OP_SHA384, 67 | EN_OP_SHA512, 68 | EN_OP_SHA_INVALID 69 | }EN_SHA_MODE; 70 | 71 | 72 | /* Define the type of variable */ 73 | typedef unsigned char UINT8; /**< Typedef for 8bits unsigned integer */ 74 | typedef unsigned short UINT16; /**< Typedef for 16bits unsigned integer */ 75 | typedef uint32_t UINT32; /**< Typedef for 32bits unsigned integer */ 76 | typedef signed char INT8; /**< Typedef for 8bits signed integer */ 77 | typedef signed short INT16; /**< Typedef for 16bits signed integer */ 78 | typedef signed int INT32; /**< Typedef for 32bits signed integer */ 79 | typedef char CHAR; /**< Typedef for char */ 80 | typedef uint32_t TEE_CRYPTO_ALGORITHM_ID; 81 | 82 | 83 | 84 | 85 | 86 | 87 | /* 88 | ******************************************************************************* 89 | * VARIABLES SUPPLIED BY THIS MODULE 90 | ******************************************************************************* 91 | */ 92 | 93 | 94 | 95 | 96 | 97 | /* 98 | ******************************************************************************* 99 | * FUNCTIONS SUPPLIED BY THIS MODULE 100 | ******************************************************************************* 101 | */ 102 | extern int g_CryptoTaHandle_Sha(uint32_t paramTypes, TEE_Param params[4]); 103 | extern int g_CryptoTaHandle_Random(uint32_t paramTypes, TEE_Param params[4]); 104 | extern void g_TA_printf(CHAR* buf, UINT32 len); 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | #endif /* MOUDLE_NAME_H*/ 127 | -------------------------------------------------------------------------------- /ta/my_test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, Linaro Limited 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #define STR_TRACE_USER_TA "MY_TEST" 29 | 30 | #include 31 | #include 32 | #include 33 | 34 | #include "my_test_ta.h" 35 | #include "my_test_handle.h" 36 | 37 | /* 38 | * Called when the instance of the TA is created. This is the first call in 39 | * the TA. 40 | */ 41 | TEE_Result TA_CreateEntryPoint(void) 42 | { 43 | DMSG("has been called"); 44 | return TEE_SUCCESS; 45 | } 46 | 47 | /* 48 | * Called when the instance of the TA is destroyed if the TA has not 49 | * crashed or panicked. This is the last call in the TA. 50 | */ 51 | void TA_DestroyEntryPoint(void) 52 | { 53 | DMSG("has been called"); 54 | } 55 | 56 | /* 57 | * Called when a new session is opened to the TA. *sess_ctx can be updated 58 | * with a value to be able to identify this session in subsequent calls to the 59 | * TA. In this function you will normally do the global initialization for the 60 | * TA. 61 | */ 62 | TEE_Result TA_OpenSessionEntryPoint(uint32_t param_types, 63 | TEE_Param __maybe_unused params[4], 64 | void __maybe_unused **sess_ctx) 65 | { 66 | uint32_t exp_param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_NONE, 67 | TEE_PARAM_TYPE_NONE, 68 | TEE_PARAM_TYPE_NONE, 69 | TEE_PARAM_TYPE_NONE); 70 | if (param_types != exp_param_types) 71 | return TEE_ERROR_BAD_PARAMETERS; 72 | 73 | /* Unused parameters */ 74 | (void)¶ms; 75 | (void)&sess_ctx; 76 | 77 | /* 78 | * The DMSG() macro is non-standard, TEE Internal API doesn't 79 | * specify any means to logging from a TA. 80 | */ 81 | DMSG("Hello World!\n"); 82 | 83 | /* If return value != TEE_SUCCESS the session will not be created. */ 84 | return TEE_SUCCESS; 85 | } 86 | 87 | /* 88 | * Called when a session is closed, sess_ctx hold the value that was 89 | * assigned by TA_OpenSessionEntryPoint(). 90 | */ 91 | void TA_CloseSessionEntryPoint(void __maybe_unused *sess_ctx) 92 | { 93 | (void)&sess_ctx; /* Unused parameter */ 94 | DMSG("Goodbye!\n"); 95 | } 96 | 97 | static TEE_Result inc_value(uint32_t param_types, 98 | TEE_Param params[4]) 99 | { 100 | uint32_t exp_param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INOUT, 101 | TEE_PARAM_TYPE_NONE, 102 | TEE_PARAM_TYPE_NONE, 103 | TEE_PARAM_TYPE_NONE); 104 | 105 | DMSG("has been called"); 106 | if (param_types != exp_param_types) 107 | return TEE_ERROR_BAD_PARAMETERS; 108 | 109 | DMSG("Got value: %u from NW", params[0].value.a); 110 | params[0].value.a++; 111 | DMSG("Increase value to: %u", params[0].value.a); 112 | return TEE_SUCCESS; 113 | } 114 | 115 | /* 116 | * Called when a TA is invoked. sess_ctx hold that value that was 117 | * assigned by TA_OpenSessionEntryPoint(). The rest of the paramters 118 | * comes from normal world. 119 | */ 120 | TEE_Result TA_InvokeCommandEntryPoint(void __maybe_unused *sess_ctx, 121 | uint32_t cmd_id, 122 | uint32_t param_types, TEE_Param params[4]) 123 | { 124 | TEE_Result l_ret = TEE_SUCCESS; 125 | int l_RetVal = -1; 126 | (void)&sess_ctx; /* Unused parameter */ 127 | 128 | switch (cmd_id) 129 | { 130 | case TA_MY_TEST_CMD_INC_VALUE: 131 | return inc_value(param_types, params); 132 | case TA_MY_TEST_CMD_HASH: 133 | l_RetVal = g_CryptoTaHandle_Sha(param_types, params); 134 | break; 135 | case TA_MY_TEST_CMD_RANDOM: 136 | l_RetVal = g_CryptoTaHandle_Random(param_types, params); 137 | break; 138 | default: 139 | return TEE_ERROR_BAD_PARAMETERS; 140 | } 141 | if(-1 == l_RetVal) 142 | { 143 | l_ret = TEE_ERROR_BAD_PARAMETERS; 144 | } 145 | else 146 | { 147 | l_ret = TEE_SUCCESS; 148 | } 149 | 150 | return l_ret; 151 | } 152 | -------------------------------------------------------------------------------- /ta/my_test_handle.c: -------------------------------------------------------------------------------- 1 | 2 | /****************************************************************************************/ 3 | /* COPYRIGHT INFORMATION */ 4 | /* This program contains proprietary information which is a trade */ 5 | /* secret of whaley Co., Ltd.and also is protected as an unpublished */ 6 | /* work under applicable Copyright laws. Recipient is to retain this */ 7 | /* program in confidence and is not permitted to use or make copies thereof */ 8 | /* other than as permitted in a written agreement written agreement with */ 9 | /* whaley Co., Ltd. */ 10 | /* */ 11 | /* All rights reserved. shuaifengyun@126.com */ 12 | /****************************************************************************************/ 13 | /* 14 | **************************************************************************************** 15 | * 16 | * my_test_handle.c 17 | * 18 | * Filename : my_test_handle.c 19 | * Programmer(s) : system BSP 20 | * Filename : my_test_handle.c 21 | * Author : Shuai Fengyun 22 | * Mail : shuaifengyun@126.com 23 | * Create Time : 2017年05月04日 星期四 10时43分49秒 24 | **************************************************************************************** 25 | */ 26 | 27 | #define MOUDLE_MY_TEST_HANDLE_C_ 28 | 29 | /** @defgroup MODULE_NAME_INFOR 30 | * @{ 31 | */ 32 | 33 | /* 34 | ******************************************************************************* 35 | * INCLUDE FILES 36 | ******************************************************************************* 37 | */ 38 | #include "tee_internal_api.h" 39 | #include "tee_api_defines.h" 40 | #include "trace.h" 41 | #include "tee_api_defines_extensions.h" 42 | #include "my_test_handle.h" 43 | #include "my_test_ta.h" 44 | 45 | 46 | 47 | /* 48 | ******************************************************************************* 49 | * FUNCTIONS SUPPLIED BY THIS MODULE 50 | ******************************************************************************* 51 | */ 52 | 53 | 54 | 55 | 56 | 57 | /* 58 | ******************************************************************************* 59 | * VARIABLES SUPPLIED BY THIS MODULE 60 | ******************************************************************************* 61 | */ 62 | 63 | 64 | 65 | 66 | 67 | /* 68 | ******************************************************************************* 69 | * FUNCTIONS USED ONLY BY THIS MODULE 70 | ******************************************************************************* 71 | */ 72 | 73 | 74 | 75 | 76 | 77 | /* 78 | ******************************************************************************* 79 | * VARIABLES USED ONLY BY THIS MODULE 80 | ******************************************************************************* 81 | */ 82 | 83 | 84 | 85 | 86 | 87 | /* 88 | ******************************************************************************* 89 | * FUNCTIONS IMPLEMENT 90 | ******************************************************************************* 91 | */ 92 | 93 | void g_TA_printf(CHAR* buf, UINT32 len) 94 | { 95 | UINT32 index = 0U; 96 | for(index =0U; index < len; index++) 97 | { 98 | if(index < 15U) 99 | { 100 | 101 | } 102 | else if(0U == index%16U) 103 | { 104 | DMSG("\n"); 105 | } 106 | else 107 | { 108 | 109 | } 110 | DMSG("0x%02x, ", (buf[index] & 0xFFU)); 111 | } 112 | DMSG("\n\n"); 113 | } 114 | 115 | 116 | 117 | /** @ingroup MOUDLE_NAME_C_ 118 | *- #Description This function for handle command. 119 | * @param pMsg [IN] The received request message 120 | * - Type: MBX_Msg * 121 | * - Range: N/A. 122 | * 123 | * @return void 124 | * @retval void 125 | * 126 | * 127 | */ 128 | static int l_CryptoTaHash_sha(EN_SHA_MODE shaMode, CHAR* input, UINT32 inLen, CHAR* output, UINT32* pOutLen) 129 | { 130 | TEE_Result ret; 131 | TEE_OperationHandle l_OperationHandle; 132 | TEE_CRYPTO_ALGORITHM_ID l_AlgorithmId; 133 | int l_RetVal = OK; 134 | 135 | DMSG("Input data just like follow(0x%x), 0x%x:\n", inLen, (UINT32)(output)); 136 | //g_TA_Printf(input, 20); 137 | 138 | /**1) Set the algorithm variable */ 139 | switch(shaMode) 140 | { 141 | case EN_OP_SHA1: 142 | DMSG("The md is sha1@!!!!\n"); 143 | l_AlgorithmId = TEE_ALG_SHA1; 144 | break; 145 | case EN_OP_SHA256: 146 | l_AlgorithmId = TEE_ALG_SHA256; 147 | break; 148 | default: 149 | l_AlgorithmId = TEE_ALG_INVALID ; 150 | l_RetVal = FAIL; 151 | goto cleanup_1; 152 | DMSG("Invalid sha mode\n"); 153 | break; 154 | } 155 | 156 | /**2) Allocate the operation handle */ 157 | ret = TEE_AllocateOperation(&l_OperationHandle, l_AlgorithmId, TEE_MODE_DIGEST, 0); 158 | if(ret != TEE_SUCCESS) 159 | { 160 | DMSG("Allocate SHA operation handle fail\n"); 161 | l_RetVal = FAIL; 162 | goto cleanup_1; 163 | } 164 | 165 | TEE_DigestUpdate(l_OperationHandle, input, inLen); 166 | 167 | /**4) Do the final sha operation */ 168 | ret = TEE_DigestDoFinal(l_OperationHandle, NULL, 0, output, pOutLen); 169 | DMSG("The out put length is :%d\n", *pOutLen); 170 | DMSG("The return value is :0x%x\n", ret); 171 | //ret = TEE_DigestDoFinal(l_OperationHandle, l_pInputTmp, inLen, output, pOutLen); 172 | if(ret != TEE_SUCCESS) 173 | { 174 | DMSG("Do the final sha operation fail\n"); 175 | l_RetVal = FAIL; 176 | goto cleanup_2; 177 | } 178 | 179 | DMSG("Hash value just like folloe:\n"); 180 | g_TA_printf(output, *pOutLen); 181 | 182 | /**5) Do the clean up operation& return the result */ 183 | cleanup_2: 184 | TEE_FreeOperation(l_OperationHandle); 185 | cleanup_1: 186 | return l_RetVal; 187 | } 188 | 189 | static void l_CryptoTaOther_Random(UINT32 len, CHAR* output) 190 | { 191 | DMSG("Entry random\n"); 192 | TEE_GenerateRandom(output, len); 193 | } 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | int g_CryptoTaHandle_Sha(uint32_t paramTypes, TEE_Param params[4]) 202 | { 203 | EN_SHA_MODE l_shaMode; 204 | CHAR* l_InputData= NULL; 205 | CHAR* l_OutPut = NULL; 206 | UINT32 l_InputLen = 0U; 207 | UINT32 l_OutputLen = 0U; 208 | uint32_t temp; 209 | temp = paramTypes; 210 | 211 | DMSG("%d\n", temp); 212 | 213 | /**1) Get the sha mode, input data info & output info */ 214 | l_InputData = params[0].memref.buffer; 215 | l_InputLen = params[0].memref.size; 216 | l_shaMode = params[1].value.a; 217 | l_OutPut = params[2].memref.buffer; 218 | l_OutputLen = params[2].memref.size; 219 | 220 | /**2) Do sha operation */ 221 | l_CryptoTaHash_sha(l_shaMode, l_InputData, l_InputLen, l_OutPut, &l_OutputLen); 222 | 223 | return OK; 224 | } 225 | 226 | 227 | 228 | int g_CryptoTaHandle_Random(uint32_t paramTypes, TEE_Param params[4]) 229 | { 230 | UINT32 l_RandomLen = 0U; 231 | CHAR* l_pBuf = NULL; 232 | uint32_t temp; 233 | temp = paramTypes; 234 | 235 | DMSG("%d\n", temp); 236 | 237 | /**1) Get the request length & point of responding buffer */ 238 | l_RandomLen = params[0].memref.size; 239 | l_pBuf = params[0].memref.buffer; 240 | 241 | l_CryptoTaOther_Random(l_RandomLen, l_pBuf); 242 | 243 | return OK; 244 | } 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | /** 254 | * @} 255 | */ 256 | -------------------------------------------------------------------------------- /host/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, Linaro Limited 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | 33 | /* OP-TEE TEE client API (built by optee_client) */ 34 | #include 35 | 36 | /* To the the UUID (found the the TA's h-file(s)) */ 37 | #include "my_test_ca.h" 38 | 39 | 40 | 41 | static int g_TaskInitFlag = -1; /* Flag if the task done initialize operation */ 42 | TEEC_UUID svc_id = TA_MY_TEST_UUID; 43 | TEEC_Context g_TaskContext; 44 | CHAR g_RandomOut[512] = {0}; 45 | /* Buffer for sha operation */ 46 | CHAR g_ShaTestBuf[] ={ 47 | 'Y', 'o', 'u', ' ', 'y', 'o', 'u', ' ', 'c', 'h', 'e', 'c', 'k', ' ', 'n', 'o', 48 | 'w', 'j', 'i', 'a', 'n', ' ', 'b', 'i', 'n', 'g', ' ', 'g', 'u', 'o', ' ', 'z', 49 | 'i', ' ', 'l', 'a', 'i', ' ', 'y', 'i', ' ', 't', 'a', 'o', '!', '!', '!', '!'}; 50 | CHAR g_ShaOutput[80] = {0}; 51 | 52 | CHAR g_Sha1Result[] = 53 | { 54 | 0x21, 0x9b, 0x5b, 0x8b, 0x25, 0x6f, 0x0e, 0x52, 0xcb, 0x2f, 0xfe, 0xfd, 0x6c, 0x47, 0xd7, 0xb4, 55 | 0x44, 0x00, 0x57, 0xc3 56 | }; 57 | 58 | 59 | CHAR g_Sha256Result[] = 60 | { 61 | 0xda, 0x52, 0xe9, 0xc2, 0x53, 0xae, 0x03, 0x30, 0xbd, 0x97, 0x3f, 0xa5, 0xf3, 0xea, 0x51, 0x1d, 62 | 0x31, 0x0a, 0xdf, 0x1f, 0x0a, 0xc0, 0x0e, 0x62, 0x0f, 0x2d, 0x5e, 0x99, 0xf5, 0xc8, 0x6b, 0x8f 63 | }; 64 | 65 | 66 | 67 | 68 | 69 | 70 | void g_CA_PrintfBuffer(CHAR* buf, UINT32 len) 71 | { 72 | UINT32 index = 0U; 73 | for(index = 0U; index < len; index++) 74 | { 75 | if(index < 15U) 76 | { 77 | } 78 | else if(0U == index%16U) 79 | { 80 | printf("\n"); 81 | 82 | } 83 | else 84 | { 85 | } 86 | 87 | printf("0x%02x, ", (buf[index] & 0x000000FFU)); 88 | 89 | 90 | } 91 | printf("\n"); 92 | 93 | } 94 | 95 | 96 | int l_CryptoVerifyCa_TaskInit(void) 97 | { 98 | TEEC_Result result; 99 | int l_RetVal = OK; 100 | 101 | /**1) Check if need to do task initialization operation */ 102 | if(-1 == g_TaskInitFlag) 103 | { 104 | result = TEEC_InitializeContext(NULL, &g_TaskContext); 105 | if(result != TEEC_SUCCESS) 106 | { 107 | printf("InitializeContext failed, ReturnCode=0x%x\n", result); 108 | l_RetVal= FAIL; 109 | } 110 | else 111 | { 112 | g_TaskInitFlag = 0; 113 | printf("InitializeContext success\n"); 114 | l_RetVal = OK; 115 | } 116 | } 117 | 118 | return l_RetVal; 119 | } 120 | 121 | 122 | int l_CryptoVerifyCa_OpenSession(TEEC_Session* session) 123 | { 124 | TEEC_Result result; 125 | int l_RetVal = FAIL; 126 | uint32_t origin; 127 | 128 | result = TEEC_OpenSession(&g_TaskContext, session, &svc_id, 129 | TEEC_LOGIN_PUBLIC, NULL, NULL, &origin); 130 | if(result != TEEC_SUCCESS) 131 | { 132 | printf("OpenSession failed, ReturnCode=0x%x, ReturnOrigin=0x%x\n", result, origin); 133 | g_TaskInitFlag = -1; 134 | l_RetVal = FAIL; 135 | } 136 | else 137 | { 138 | printf("OpenSession success\n"); 139 | l_RetVal = OK; 140 | } 141 | 142 | return l_RetVal; 143 | } 144 | 145 | 146 | int l_CryptoVerifyCa_SendCommand(TEEC_Operation* operation, TEEC_Session* session, uint32_t commandID) 147 | { 148 | TEEC_Result result; 149 | int l_RetVal = FAIL; 150 | uint32_t origin; 151 | 152 | result = TEEC_InvokeCommand(session, commandID, operation, &origin); 153 | if (result != TEEC_SUCCESS) 154 | { 155 | printf("InvokeCommand failed, ReturnCode=0x%x, ReturnOrigin=0x%x\n", result, origin); 156 | l_RetVal = FAIL; 157 | } 158 | else 159 | { 160 | printf("InvokeCommand success\n"); 161 | l_RetVal = OK; 162 | } 163 | 164 | 165 | return l_RetVal; 166 | } 167 | 168 | 169 | void g_CryptoVerifyCa_Helloworld(void) 170 | { 171 | TEEC_Session l_session; /* Define the session of TA&CA */ 172 | TEEC_Operation l_operation; /* Define the operation for communicating between TA&CA */ 173 | int l_RetVal = FAIL; /* Define the return value of function */ 174 | 175 | /**1) Initialize this task */ 176 | l_RetVal = l_CryptoVerifyCa_TaskInit(); 177 | if(FAIL == l_RetVal) 178 | { 179 | goto cleanup_1; 180 | } 181 | 182 | /**2) Open session */ 183 | l_RetVal = l_CryptoVerifyCa_OpenSession(&l_session); 184 | if(FAIL == l_RetVal) 185 | { 186 | goto cleanup_2; 187 | } 188 | 189 | /* Clear the TEEC_Operation struct */ 190 | memset(&l_operation, 0, sizeof(TEEC_Operation)); 191 | 192 | /* 193 | * Prepare the argument. Pass a value in the first parameter, 194 | * the remaining three parameters are unused. 195 | */ 196 | l_operation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INOUT, TEEC_NONE, 197 | TEEC_NONE, TEEC_NONE); 198 | l_operation.params[0].value.a = 42; 199 | 200 | /**4) Send command to TA */ 201 | l_RetVal = l_CryptoVerifyCa_SendCommand(&l_operation, &l_session, TA_MY_TEST_CMD_INC_VALUE); 202 | if(FAIL == l_RetVal) 203 | { 204 | goto cleanup_3; 205 | } 206 | 207 | /**5) The clean up operation */ 208 | cleanup_3: 209 | TEEC_CloseSession(&l_session); 210 | cleanup_2: 211 | TEEC_FinalizeContext(&g_TaskContext); 212 | cleanup_1: 213 | printf("over\n"); 214 | 215 | } 216 | 217 | int g_CryptoVerifyCa_Random(UINT32 len, CHAR* output) 218 | { 219 | TEEC_Session l_session; /* Define the session of TA&CA */ 220 | TEEC_Operation l_operation; /* Define the operation for communicating between TA&CA */ 221 | int l_RetVal = FAIL; /* Define the return value of function */ 222 | 223 | /**1) Initialize this task */ 224 | l_RetVal = l_CryptoVerifyCa_TaskInit(); 225 | if(FAIL == l_RetVal) 226 | { 227 | goto cleanup_1; 228 | } 229 | 230 | /**2) Open session */ 231 | l_RetVal = l_CryptoVerifyCa_OpenSession(&l_session); 232 | if(FAIL == l_RetVal) 233 | { 234 | goto cleanup_2; 235 | } 236 | 237 | /**3) Set the communication context between CA&TA */ 238 | memset(&l_operation, 0x0, sizeof(TEEC_Operation)); 239 | l_operation.started = 1; 240 | l_operation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_OUTPUT,TEEC_NONE, 241 | TEEC_NONE, TEEC_NONE); 242 | l_operation.params[0].tmpref.size = len; 243 | l_operation.params[0].tmpref.buffer = output; 244 | 245 | /**4) Send command to TA */ 246 | l_RetVal = l_CryptoVerifyCa_SendCommand(&l_operation, &l_session, TA_MY_TEST_CMD_RANDOM); 247 | if(FAIL == l_RetVal) 248 | { 249 | goto cleanup_3; 250 | } 251 | 252 | /**5) The clean up operation */ 253 | cleanup_3: 254 | TEEC_CloseSession(&l_session); 255 | cleanup_2: 256 | TEEC_FinalizeContext(&g_TaskContext); 257 | cleanup_1: 258 | return l_RetVal; 259 | } 260 | 261 | 262 | 263 | 264 | int g_CryptoVerifyCa_Sha(CHAR* pData, UINT32 len, EN_SHA_MODE shaMode, CHAR* output, UINT32 outLen) 265 | { 266 | TEEC_Session l_session; /* Define the session of TA&CA */ 267 | TEEC_Operation l_operation; /* Define the operation for communicating between TA&CA */ 268 | int l_RetVal = FAIL; /* Define the return value of function */ 269 | 270 | /**1) Initialize this task */ 271 | l_RetVal = l_CryptoVerifyCa_TaskInit(); 272 | if(FAIL == l_RetVal) 273 | { 274 | goto cleanup_1; 275 | } 276 | 277 | /**2) Open session */ 278 | l_RetVal = l_CryptoVerifyCa_OpenSession(&l_session); 279 | if(FAIL == l_RetVal) 280 | { 281 | goto cleanup_2; 282 | } 283 | 284 | /**3) Set the communication context between CA&TA */ 285 | memset(&l_operation, 0x0, sizeof(TEEC_Operation)); 286 | l_operation.started = 1; 287 | l_operation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,TEEC_VALUE_INPUT, 288 | TEEC_MEMREF_TEMP_OUTPUT, TEEC_NONE); 289 | l_operation.params[0].tmpref.size = len; 290 | l_operation.params[0].tmpref.buffer = pData; 291 | l_operation.params[1].value.a = shaMode; 292 | l_operation.params[2].tmpref.size = outLen; 293 | l_operation.params[2].tmpref.buffer = output; 294 | 295 | /**4) Send command to TA */ 296 | l_RetVal = l_CryptoVerifyCa_SendCommand(&l_operation, &l_session, TA_MY_TEST_CMD_HASH); 297 | printf("The respond data length is 0x%02x\n", outLen); 298 | if(FAIL == l_RetVal) 299 | { 300 | goto cleanup_3; 301 | } 302 | 303 | /**5) The clean up operation */ 304 | cleanup_3: 305 | TEEC_CloseSession(&l_session); 306 | cleanup_2: 307 | TEEC_FinalizeContext(&g_TaskContext); 308 | cleanup_1: 309 | return l_RetVal; 310 | } 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | int main(int argc, char *argv[]) 319 | { 320 | 321 | if(0 == memcmp(argv[1], "helloworld", 10)) 322 | { 323 | printf("Entry get helloworld CA\n"); 324 | g_CryptoVerifyCa_Helloworld(); 325 | printf("The Respond helloworld from TA just like follow:\n"); 326 | } 327 | 328 | if(0 == memcmp(argv[1], "sha1", 4)) 329 | { 330 | printf("Entry sha1 CA\n"); 331 | g_CryptoVerifyCa_Sha(g_ShaTestBuf, sizeof(g_ShaTestBuf), EN_OP_SHA1, g_ShaOutput, 20); 332 | printf("The Respond hash data from TA just like follow:\n"); 333 | g_CA_PrintfBuffer(g_ShaOutput, 20); 334 | } 335 | 336 | 337 | 338 | if(0 == memcmp(argv[1], "sha256", 6)) 339 | { 340 | 341 | printf("Entry sha256 CA\n"); 342 | g_CryptoVerifyCa_Sha(g_ShaTestBuf, sizeof(g_ShaTestBuf), EN_OP_SHA256, g_ShaOutput, 32); 343 | printf("The Respond hash data from TA just like follow:\n"); 344 | g_CA_PrintfBuffer(g_ShaOutput, 32); 345 | } 346 | 347 | 348 | return 0; 349 | } 350 | 351 | 352 | 353 | --------------------------------------------------------------------------------