Skip to content

Commit 0fa9b08

Browse files
committed
sdk/oe: minimal bindings for OpenEnclave SDK
1 parent 2a06a95 commit 0fa9b08

14 files changed

+406
-0
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@
1010
[submodule "sdk/bare-sgx"]
1111
path = sdk/bare-sgx
1212
url = https://github.com/jovanbulck/bare-sgx.git
13+
[submodule "sdk/oe/openenclave"]
14+
path = sdk/oe/openenclave
15+
url = https://github.com/openenclave/openenclave.git

app/oe/.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
*_t.*
2+
*_u.*
3+
*.pem
4+
*_args.h
5+
*.signed
6+
enclave/enclave
7+
host/helloworld_host

app/oe/Makefile

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Copyright (c) Open Enclave SDK contributors.
2+
# Licensed under the MIT License.
3+
4+
.PHONY: all build clean run simulate
5+
6+
OE_CRYPTO_LIB := mbedtls
7+
export OE_CRYPTO_LIB
8+
9+
all: build
10+
11+
build:
12+
$(MAKE) -C enclave
13+
$(MAKE) -C host
14+
15+
clean:
16+
$(MAKE) -C enclave clean
17+
$(MAKE) -C host clean
18+
19+
run:
20+
host/helloworld_host ./enclave/enclave.signed
21+
22+
simulate:
23+
host/helloworld_host ./enclave/enclave.signed --simulate
24+

app/oe/enclave/Makefile

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Copyright (c) Open Enclave SDK contributors.
2+
# Licensed under the MIT License.
3+
4+
include ../config.mk
5+
6+
CRYPTO_LDFLAGS := $(shell pkg-config oeenclave-$(COMPILER) --variable=${OE_CRYPTO_LIB}libs)
7+
8+
ifeq ($(LVI_MITIGATION), ControlFlow)
9+
ifeq ($(LVI_MITIGATION_BINDIR),)
10+
$(error LVI_MITIGATION_BINDIR is not set)
11+
endif
12+
# Only run once.
13+
ifeq (,$(findstring $(LVI_MITIGATION_BINDIR),$(CC)))
14+
CC := $(LVI_MITIGATION_BINDIR)/$(CC)
15+
endif
16+
COMPILER := $(COMPILER)-lvi-cfg
17+
CRYPTO_LDFLAGS := $(shell pkg-config oeenclave-$(COMPILER) --variable=${OE_CRYPTO_LIB}libslvicfg)
18+
endif
19+
20+
ifeq ($(OE_CRYPTO_LIB),openssl_3)
21+
CFLAGS=$(shell pkg-config oeenclave-$(COMPILER) --variable=${OE_CRYPTO_LIB}flags)
22+
else
23+
CFLAGS=$(shell pkg-config oeenclave-$(COMPILER) --cflags)
24+
endif
25+
LDFLAGS=$(shell pkg-config oeenclave-$(COMPILER) --libs)
26+
INCDIR=$(shell pkg-config oeenclave-$(COMPILER) --variable=includedir)
27+
28+
all:
29+
$(MAKE) build
30+
$(MAKE) keys
31+
$(MAKE) sign
32+
33+
build:
34+
@ echo "Compilers used: $(CC), $(CXX)"
35+
oeedger8r ../helloworld.edl --trusted \
36+
--search-path $(INCDIR) \
37+
--search-path $(INCDIR)/openenclave/edl/sgx
38+
$(CC) -g -c $(CFLAGS) -DOE_API_VERSION=2 enc.c -o enc.o
39+
$(CC) -g -c $(CFLAGS) -DOE_API_VERSION=2 helloworld_t.c -o helloworld_t.o
40+
$(CC) -o enclave helloworld_t.o enc.o $(LDFLAGS) $(CRYPTO_LDFLAGS)
41+
42+
sign:
43+
oesign sign -e enclave -c helloworld.conf -k private.pem
44+
45+
clean:
46+
rm -f enc.o enclave enclave.signed private.pem public.pem helloworld_t.o helloworld_t.h helloworld_t.c helloworld_args.h
47+
48+
keys:
49+
openssl genrsa -out private.pem -3 3072
50+
openssl rsa -in private.pem -pubout -out public.pem

app/oe/enclave/enc.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) Open Enclave SDK contributors.
2+
// Licensed under the MIT License.
3+
4+
#include <stdio.h>
5+
6+
// Include the trusted helloworld header that is generated
7+
// during the build. This file is generated by calling the
8+
// sdk tool oeedger8r against the helloworld.edl file.
9+
#include "helloworld_t.h"
10+
11+
// This is the function that the host calls. It prints
12+
// a message in the enclave before calling back out to
13+
// the host to print a message from there too.
14+
void enclave_helloworld()
15+
{
16+
// Print a message from the enclave. Note that this
17+
// does not directly call fprintf, but calls into the
18+
// host and calls fprintf from there. This is because
19+
// the fprintf function is not part of the enclave
20+
// as it requires support from the kernel.
21+
fprintf(stdout, "Hello world from the enclave\n");
22+
23+
// Call back into the host
24+
oe_result_t result = host_helloworld();
25+
if (result != OE_OK)
26+
{
27+
fprintf(
28+
stderr,
29+
"Call to host_helloworld failed: result=%u (%s)\n",
30+
result,
31+
oe_result_str(result));
32+
}
33+
}

app/oe/enclave/helloworld.conf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Copyright (c) Open Enclave SDK contributors.
2+
# Licensed under the MIT License.
3+
4+
# Enclave settings:
5+
Debug=1
6+
NumHeapPages=1024
7+
NumStackPages=1024
8+
NumTCS=1
9+
ProductID=1
10+
SecurityVersion=1

app/oe/helloworld.edl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) Open Enclave SDK contributors.
2+
// Licensed under the MIT License.
3+
4+
enclave {
5+
from "openenclave/edl/syscall.edl" import *;
6+
from "platform.edl" import *;
7+
8+
trusted {
9+
public void enclave_helloworld();
10+
};
11+
12+
untrusted {
13+
void host_helloworld();
14+
};
15+
};
16+
17+

app/oe/host/Makefile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright (c) Open Enclave SDK contributors.
2+
# Licensed under the MIT License.
3+
4+
include ../config.mk
5+
6+
LIBSGXSTEP_DIR=../../../
7+
LIBSGXSTEP=$(LIBSGXSTEP_DIR)/libsgxstep
8+
9+
CFLAGS=$(shell pkg-config oehost-$(COMPILER) --cflags) -I$(LIBSGXSTEP_DIR)
10+
LDFLAGS=$(shell pkg-config oehost-$(COMPILER) --libs) -lsgx-step -pthread -L$(LIBSGXSTEP) -lelf
11+
INCDIR=$(shell pkg-config oehost-$(COMPILER) --variable=includedir)
12+
13+
build:
14+
@ echo "Compilers used: $(CC), $(CXX)"
15+
oeedger8r ../helloworld.edl --untrusted \
16+
--search-path $(INCDIR) \
17+
--search-path $(INCDIR)/openenclave/edl/sgx
18+
$(CC) -g -c $(CFLAGS) host.c
19+
$(CC) -g -c $(CFLAGS) helloworld_u.c
20+
$(CC) -o helloworld_host helloworld_u.o host.o $(LDFLAGS)
21+
22+
clean:
23+
rm -f helloworld_host host.o helloworld_u.o helloworld_u.c helloworld_u.h helloworld_args.h

app/oe/host/host.c

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Copyright (c) Open Enclave SDK contributors.
2+
// Licensed under the MIT License.
3+
4+
#include <openenclave/host.h>
5+
#include <stdio.h>
6+
7+
// Include the untrusted helloworld header that is generated
8+
// during the build. This file is generated by calling the
9+
// sdk tool oeedger8r against the helloworld.edl file.
10+
#include "helloworld_u.h"
11+
12+
#include "libsgxstep/debug.h"
13+
#include "libsgxstep/enclave.h"
14+
15+
void aep_cb_func(void)
16+
{
17+
uint64_t erip = edbgrd_erip() - (uint64_t)get_enclave_base();
18+
info("^^ enclave RIP=%#lx", erip);
19+
}
20+
21+
bool check_simulate_opt(int* argc, const char* argv[])
22+
{
23+
for (int i = 0; i < *argc; i++)
24+
{
25+
if (strcmp(argv[i], "--simulate") == 0)
26+
{
27+
fprintf(stdout, "Running in simulation mode\n");
28+
memmove(&argv[i], &argv[i + 1], (*argc - i) * sizeof(char*));
29+
(*argc)--;
30+
return true;
31+
}
32+
}
33+
return false;
34+
}
35+
36+
// This is the function that the enclave will call back into to
37+
// print a message.
38+
void host_helloworld()
39+
{
40+
fprintf(stdout, "Enclave called into host to print: Hello World!\n");
41+
}
42+
43+
int main(int argc, const char* argv[])
44+
{
45+
oe_result_t result;
46+
int ret = 1;
47+
oe_enclave_t* enclave = NULL;
48+
49+
uint32_t flags = OE_ENCLAVE_FLAG_DEBUG;
50+
if (check_simulate_opt(&argc, argv))
51+
{
52+
flags |= OE_ENCLAVE_FLAG_SIMULATE;
53+
}
54+
55+
if (argc != 2)
56+
{
57+
fprintf(
58+
stderr, "Usage: %s enclave_image_path [ --simulate ]\n", argv[0]);
59+
goto exit;
60+
}
61+
62+
// Create the enclave
63+
result = oe_create_helloworld_enclave(
64+
argv[1], OE_ENCLAVE_TYPE_AUTO, flags, NULL, 0, &enclave);
65+
if (result != OE_OK)
66+
{
67+
fprintf(
68+
stderr,
69+
"oe_create_helloworld_enclave(): result=%u (%s)\n",
70+
result,
71+
oe_result_str(result));
72+
goto exit;
73+
}
74+
75+
// SGX-Step stuff
76+
register_aep_cb(aep_cb_func);
77+
print_enclave_info();
78+
79+
// Call into the enclave
80+
result = enclave_helloworld(enclave);
81+
if (result != OE_OK)
82+
{
83+
fprintf(
84+
stderr,
85+
"calling into enclave_helloworld failed: result=%u (%s)\n",
86+
result,
87+
oe_result_str(result));
88+
goto exit;
89+
}
90+
91+
ret = 0;
92+
93+
exit:
94+
// Clean up the enclave if we created one
95+
if (enclave)
96+
oe_terminate_enclave(enclave);
97+
98+
return ret;
99+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
From 2cacff71c210c469a424110510a0c3a5f3561ef2 Mon Sep 17 00:00:00 2001
2+
From: Jo Van Bulck <jo.vanbulck@cs.kuleuven.be>
3+
Date: Wed, 15 Jan 2025 13:40:17 +0000
4+
Subject: [PATCH] Minimal SGX-Step bindings
5+
6+
Signed-off-by: Jo Van Bulck <jo.vanbulck@cs.kuleuven.be>
7+
---
8+
host/sgx/asmdefs.h | 2 +-
9+
host/sgx/calls.c | 20 ++++++++++++++++++++
10+
include/openenclave/host.h | 6 ++++++
11+
3 files changed, 27 insertions(+), 1 deletion(-)
12+
13+
diff --git a/host/sgx/asmdefs.h b/host/sgx/asmdefs.h
14+
index 4b78e1989..9332f9c2b 100644
15+
--- a/host/sgx/asmdefs.h
16+
+++ b/host/sgx/asmdefs.h
17+
@@ -35,7 +35,7 @@ oe_result_t oe_enter(
18+
uint64_t* arg4,
19+
oe_enclave_t* enclave);
20+
21+
-extern const uint64_t OE_AEP_ADDRESS;
22+
+extern uint64_t OE_AEP_ADDRESS;
23+
#endif
24+
25+
#if !defined(__ASSEMBLER__) && (_WIN32)
26+
diff --git a/host/sgx/calls.c b/host/sgx/calls.c
27+
index eed0c4dcf..66bfb3cc5 100644
28+
--- a/host/sgx/calls.c
29+
+++ b/host/sgx/calls.c
30+
@@ -578,6 +578,25 @@ static void _release_tcs(oe_enclave_t* enclave, void* tcs)
31+
oe_mutex_unlock(&enclave->lock);
32+
}
33+
34+
+/* minimal SGX-Step bindings */
35+
+
36+
+void* __oe_last_tcs;
37+
+
38+
+void* sgx_get_aep(void)
39+
+{
40+
+ return (void*) OE_AEP_ADDRESS;
41+
+}
42+
+
43+
+void sgx_set_aep(void* aep)
44+
+{
45+
+ OE_AEP_ADDRESS = (uint64_t) aep;
46+
+}
47+
+
48+
+void *sgx_get_tcs(void)
49+
+{
50+
+ return __oe_last_tcs;
51+
+}
52+
+
53+
/*
54+
**==============================================================================
55+
**
56+
@@ -608,6 +627,7 @@ oe_result_t oe_ecall(
57+
/* Assign a oe_sgx_td_t for this operation */
58+
if (!(tcs = _assign_tcs(enclave)))
59+
OE_RAISE(OE_OUT_OF_THREADS);
60+
+ __oe_last_tcs = tcs;
61+
62+
oe_log(
63+
OE_LOG_LEVEL_VERBOSE,
64+
diff --git a/include/openenclave/host.h b/include/openenclave/host.h
65+
index d0f279ec5..864b4b7f5 100644
66+
--- a/include/openenclave/host.h
67+
+++ b/include/openenclave/host.h
68+
@@ -231,6 +231,12 @@ oe_result_t oe_create_enclave(
69+
uint32_t ecall_count,
70+
oe_enclave_t** enclave);
71+
72+
+
73+
+/* minimal SGX-Step bindings */
74+
+void* sgx_get_aep(void);
75+
+void sgx_set_aep(void* aep);
76+
+void *sgx_get_tcs(void);
77+
+
78+
/**
79+
* Terminate an enclave and reclaims its resources.
80+
*
81+
--
82+
2.43.0
83+

sdk/oe/install_llvm11.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
#set -x
3+
4+
# NOTE: 20.04 tarball also works on 24.04
5+
TAR_BASE="clang+llvm-11.0.0-x86_64-linux-gnu-ubuntu-20.04"
6+
TAR_FILE="$TAR_BASE.tar.xz"
7+
TAR_URL="https://github.com/llvm/llvm-project/releases/download/llvmorg-11.0.0/$TAR_FILE"
8+
TMP_DIR="/tmp/llvm-temp"
9+
TARGET_BASE="/usr"
10+
11+
mkdir -p "$TMP_DIR"
12+
cd $TMP_DIR
13+
if [ ! -e $TAR_FILE ]; then
14+
wget $TAR_URL
15+
tar xvf $TAR_FILE
16+
fi
17+
18+
sudo mkdir -p /usr/lib/llvm-11/
19+
sudo cp -r $TMP_DIR/$TAR_BASE/* /usr/lib/llvm-11/
20+
21+
echo "Extraction and moving completed."

0 commit comments

Comments
 (0)