Skip to content

Commit 691da66

Browse files
amey-raghatateAkshay-Belsare
authored andcommitted
ecdh: new example demonstrating ECDH shared secret derivation
- Introduced a new example utilizing the TEE_ALG_ECDH_DERIVE_SHARED_SECRET algorithm. - This example demonstrates how to establish a shared secret using Elliptic Curve Diffie-Hellman (ECDH). - Provides a reference for secure key exchange and cryptographic operations with ECDH in OP-TEE. Signed-off-by: Amey Avinash Raghatate <[email protected]> State: waiting Link: linaro-swg#135
1 parent 0d075cc commit 691da66

File tree

11 files changed

+464
-0
lines changed

11 files changed

+464
-0
lines changed

ecdh/Android.mk

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
LOCAL_PATH := $(call my-dir)
2+
3+
include $(CLEAR_VARS)
4+
LOCAL_CFLAGS += -DANDROID_BUILD
5+
LOCAL_CFLAGS += -Wall
6+
7+
LOCAL_SRC_FILES += host/main.c
8+
9+
LOCAL_C_INCLUDES := $(LOCAL_PATH)/ta/include
10+
11+
LOCAL_SHARED_LIBRARIES := libteec
12+
LOCAL_MODULE := optee_example_ecdh
13+
LOCAL_VENDOR_MODULE := true
14+
LOCAL_MODULE_TAGS := optional
15+
include $(BUILD_EXECUTABLE)
16+
17+
include $(LOCAL_PATH)/ta/Android.mk

ecdh/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
project (optee_example_ecdh C)
2+
3+
set (SRC host/main.c)
4+
5+
add_executable (${PROJECT_NAME} ${SRC})
6+
7+
target_include_directories(${PROJECT_NAME}
8+
PRIVATE ta/include
9+
PRIVATE include)
10+
11+
target_link_libraries (${PROJECT_NAME} PRIVATE teec)
12+
13+
install (TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR})

ecdh/Makefile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export V ?= 0
2+
3+
# If _HOST or _TA specific compilers are not specified, then use CROSS_COMPILE
4+
HOST_CROSS_COMPILE ?= $(CROSS_COMPILE)
5+
TA_CROSS_COMPILE ?= $(CROSS_COMPILE)
6+
7+
.PHONY: all
8+
all:
9+
$(MAKE) -C host CROSS_COMPILE="$(HOST_CROSS_COMPILE)" --no-builtin-variables
10+
$(MAKE) -C ta CROSS_COMPILE="$(TA_CROSS_COMPILE)" LDFLAGS=""
11+
12+
.PHONY: clean
13+
clean:
14+
$(MAKE) -C host clean
15+
$(MAKE) -C ta clean

ecdh/host/Makefile

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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./include
12+
CFLAGS += -I$(TEEC_EXPORT)/include
13+
LDADD += -lteec -L$(TEEC_EXPORT)/lib
14+
15+
BINARY = optee_example_ecdh
16+
17+
.PHONY: all
18+
all: $(BINARY)
19+
20+
$(BINARY): $(OBJS)
21+
$(CC) $(LDFLAGS) -o $@ $< $(LDADD)
22+
23+
.PHONY: clean
24+
clean:
25+
rm -f $(OBJS) $(BINARY)
26+
27+
%.o: %.c
28+
$(CC) $(CFLAGS) -c $< -o $@

ecdh/host/main.c

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// SPDX-License-Identifier: BSD-2-Clause
2+
/*
3+
* Copyright (c) 2025, Advanced Micro Devices, Inc. All rights reserved.
4+
*/
5+
6+
#include <err.h>
7+
#include <stdio.h>
8+
#include <stdlib.h>
9+
#include <string.h>
10+
#include <tee_client_api.h>
11+
12+
#include <ecdh_ta.h>
13+
14+
static void hexdump(const void *p, size_t len)
15+
{
16+
const unsigned char *b = (const unsigned char *)p;
17+
18+
for (size_t i = 0; i < len; i++) {
19+
printf("%02x", b[i]);
20+
if ((i + 1) % 32 == 0)
21+
printf("\n");
22+
}
23+
if (len % 32)
24+
printf("\n");
25+
}
26+
27+
int main(void)
28+
{
29+
TEEC_Result res;
30+
TEEC_Context ctx;
31+
TEEC_Session sess;
32+
TEEC_Operation op;
33+
TEEC_UUID uuid = TA_ECDH_UUID;
34+
uint32_t err_origin;
35+
36+
res = TEEC_InitializeContext(NULL, &ctx);
37+
if (res != TEEC_SUCCESS)
38+
errx(1, "TEEC_InitializeContext failed with code 0x%x", res);
39+
40+
res = TEEC_OpenSession(&ctx, &sess, &uuid, TEEC_LOGIN_PUBLIC, NULL,
41+
NULL, &err_origin);
42+
if (res != TEEC_SUCCESS)
43+
errx(1, "TEEC_OpenSession failed 0x%x origin 0x%x",
44+
res, err_origin);
45+
46+
uint32_t curve = TA_ECC_CURVE_NIST_P384;
47+
48+
uint8_t secret[ECDH_BUF_BYTES];
49+
50+
memset(&op, 0, sizeof(op));
51+
op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INOUT,
52+
TEEC_NONE,
53+
TEEC_NONE,
54+
TEEC_MEMREF_TEMP_OUTPUT);
55+
56+
op.params[0].value.a = curve; /* IN: curve id */
57+
op.params[0].value.b = 0; /* OUT: secret len */
58+
op.params[3].tmpref.buffer = secret; /* OUT buffer for secret */
59+
op.params[3].tmpref.size = sizeof(secret);
60+
61+
res = TEEC_InvokeCommand(&sess, TA_ECDH_CMD_DERIVE_SELFTEST,
62+
&op, &err_origin);
63+
if (res == TEEC_ERROR_SHORT_BUFFER) {
64+
/* Resize and try once more with the exact size TA asked for */
65+
size_t need = op.params[0].value.b;
66+
67+
if (need > sizeof(secret))
68+
errx(1, "Required secret buffer too big: %zu", need);
69+
70+
op.params[3].tmpref.size = need;
71+
res = TEEC_InvokeCommand(&sess, TA_ECDH_CMD_DERIVE_SELFTEST,
72+
&op, &err_origin);
73+
}
74+
75+
if (res != TEEC_SUCCESS)
76+
errx(1, "Invoke TA_ECDH_CMD_DERIVE_SELFTEST failed 0x%x origin 0x%x",
77+
res, err_origin);
78+
79+
size_t secret_len = op.params[0].value.b;
80+
81+
printf("ECDH shared secret (%zu bytes) on curve id %u:\n",
82+
secret_len, curve);
83+
hexdump(secret, secret_len);
84+
85+
TEEC_CloseSession(&sess);
86+
TEEC_FinalizeContext(&ctx);
87+
return 0;
88+
}

ecdh/ta/Android.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
LOCAL_PATH := $(call my-dir)
2+
local_module := 50c82425-94da-4072-a3e0-58ef063767c0.ta
3+
include $(BUILD_OPTEE_MK)

ecdh/ta/Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CFG_TEE_TA_LOG_LEVEL ?= 4
2+
CFG_TA_OPTEE_CORE_API_COMPAT_1_1=y
3+
4+
# The UUID for the Trusted Application
5+
BINARY=50c82425-94da-4072-a3e0-58ef063767c0
6+
7+
-include $(TA_DEV_KIT_DIR)/mk/ta_dev_kit.mk
8+
9+
ifeq ($(wildcard $(TA_DEV_KIT_DIR)/mk/ta_dev_kit.mk), )
10+
clean:
11+
@echo 'Note: $$(TA_DEV_KIT_DIR)/mk/ta_dev_kit.mk not found, cannot clean TA'
12+
@echo 'Note: TA_DEV_KIT_DIR=$(TA_DEV_KIT_DIR)'
13+
endif

0 commit comments

Comments
 (0)