Compiling and Running your First Enclave¶
You can find a sample app in the
hello-world directory of your SDK. You can use this app as a template
for your own if you want a quick start. We will cover:
- How to set up your machine.
- How to compile and run the sample app.
- How to write the sample app.
SGX enclaves can be used in one of four modes, in order of increasing realism:
- Mock: your enclave class is created in the host JVM and no native or SGX specific code is used. This provides a pure Java development experience.
- Simulation: an enclave is compiled to native machine code and loaded, but SGX hardware doesn't need to be present.
- Debug: the enclave is loaded using SGX hardware and drivers, but with a back door that allows debugger access to the memory.
- Release: the enclave is loaded using SGX hardware and drivers, and there's no back door. This is the real deal.
Only release mode locks out the host and provides the standard SGX security model.
Setting up your machine¶
For this tutorial you will need Java 8 or 11 (your choice). If you use IntelliJ IDEA the IDE can download both a JDK and the Gradle build system for you, so you don't need anything to get started except the IDE itself (the free Community Edition works fine).
Currently, we support developing enclaves on Windows, macOS and Linux. However, there are a few platform specific differences to be aware of.
Firstly, you need a Linux environment to build and execute enclaves, including for native testing. This is because enclaves are Linux shared libraries with special extensions. If you are not using Linux you will need to install Docker. On Windows and macOS, Conclave uses Docker to build the enclave in a Linux environment. Instructions are provided below to show you how to use Docker on Windows and macOS to run your entire application in "simulation mode". Alternatively, for day to day development building an enclave in mock mode is plenty sufficient and allows you to debug into enclave calls as well. Compiling a real enclave is only needed for integration testing against the embedded JVM, or real deployment.
Secondly, when building enclaves Conclave internally uses the C++ compiler gcc. This is automatically installed when building on Windows and macOS but on Linux you need to make sure you have installed gcc yourself. If your build system uses the aptitude package manager then you can install everything you need with this command:
Enclaves can run in simulation mode without requiring any special setup of Linux or SGX capable hardware. However you of course get no hardware protections. To run against real SGX hardware you must perform some additional machine setup.
Compiling the sample enclave¶
Step 1: Import the project
Step 2: Look at the Conclave SDK's top level directory
Step 3: When notified that there's a Gradle build script, click "hello-world" to import the project.
Step 4: Double-click on
:host:assemble. This is the second
assemble in the screenshot of Intellij's Gradle window below. Voila! You have just built your first enclave.
Now explore the
As normal with Gradle, the
assemble task has bundled the program into a zip, with startup scripts. These scripts are
nothing special - they just set up the classpath. You could also e.g. make a fat JAR if you want.
Alternatively you can build you application from the command line as described in the next section.
Selecting your mode¶
In the sample app, the
assemble task will build the app for simulation mode by default.
-PenclaveMode argument to configure the mode.
If you are using SGX hardware, you can build the app for debug mode with the command:
If working from inside IntelliJ, start the assemble task for the host project from the tree on the right hand side,
and then edit the created run config. Add the
-PenclaveMode=debug flag to the arguments section of the run config.
If you want to debug into the enclave, or you are running on an OS other than Linux then you can build the app for mock mode with the command:
For release mode, the sample app has been configured (in the
build.gradle of the
enclave subproject) to use external
signing. This means it must be built in multiple stages:
See Enclave signing for more information.
1 2 3 4 5 6 7 8
Got this far? Join the community!¶
There's a public mailing list for discussion of using Conclave and we also welcome general SGX talk. A Slack channel is available where you can find the development team during UK office hours (GMT 0900-1700).
Confused about various Gradle commands (
run)? Here is an explanation.¶
build will build a client, an enclave and a host. This command will also run unit tests (if there are any) for the corresponding components.
The build results will go to
client/build/distribution/client.tar (or .zip) for the client,
host/build/distribution/host.tar (or .zip) for the host (an enclave is bundled with the host).
These archives contain everything required to run your app (except the JRE) and can be shipped to the customer as-is.
assemble will do the same job as
build except that it won't run unit tests. You can assemble all at once or do it separately for the client and for the host.
installDist will behave similarly to assemble except that unlike build or assemble, it won't produce the archive files.
Instead, it will create a
client/build/install directories, whose contents are effectively unpacked host.tar and/or client.tar files.
On Linux you can run your app in two slightly different ways - standalone or using Gradle:
1 2 3
./gradlew host:installDist cd host/build/install ./host/bin/host
1 2 3
./gradlew client:installDist cd client/build/install ./client/bin/client
./gradlew :client:run --args="Reverse me"
run task is a part of the Application plugin.
Running the host¶
Just run the host app like any app - no special customisation or setup is required with Conclave! Here we will run a shell script generated by Gradle that starts the JVM:
1 2 3
Gradle can also create
.tar.gz files suitable for copying to the Linux host, fat JARs, WAR files for deployment into
servlet containers and various other ways to deploy your app.
At this time using the JPMS tool
jlink is not tested.
On macOS there is a script that lets you run Gradle (and by extension anything it runs) inside a Linux environment based on the Conclave build container. It wraps Docker and makes it simpler to work with.
Just use the
../scripts/container-gradle script as a replacement for
gradlew. You might want to add it to your
$PATH variable. To run the host, use this command:
For more information see the Container Gradle page.
Don't be surprised if the application gets built again from scratch! We do this to ensure the build and runtime environments match.
On Windows you can still test locally in simulation mode using a Docker container. However you may need to configure
mounts and other parameters yourself. Refer to the
scripts/container-gradle file to see how this is done on macOS.
This will build the app on your Windows machine, then mount the app into a Linux container and get a shell. Next, run the app from within the container:
Please consult the Docker reference manual to understand
what switches you can pass to the
docker run command.
If your Linux machine (or container) doesn't have SGX, you should see something like the following. Don't worry, you can still complete the tutorial because we are using simulation mode:
You can proceed to Running the client when you see the following:
If you are using IntelliJ, you may want to create a launch configuration to incorporate the
deploy stages. If using the
container-gradle script on macOS, IntelliJ does expect the command to launch Gradle to be called
What you can do is rename
gradlew to something else, then copy the
scripts/container-gradle script to be called
gradlew in your project root. Finally, edit the last line of the script to start the renamed script. Therefore
IntelliJ will run the
container-gradle script whilst thinking it's running normal Gradle.
Running the client¶
The host has opened up a TCP port which will now listen for requests from remote clients. So, let's run the client app:
Docker is only required to run the host on macOS or windows. The client can be run without Docker.
The host will load the enclave, obtain its remote attestation (the
EnclaveInstanceInfo object), print it out,
and ask the enclave to reverse a string. You should see the following output from the host:
1 2 3 4 5 6 7 8 9 10 11 12
The client will connect to the host, download the
EnclaveInstanceInfo, check it, print it out, and then send an encrypted string to reverse. The host will deliver this encrypted string to the enclave,
and the enclave will send back an encrypted response as a
PostMail command. The host will extract an encrypted data from the
PostMail command and send it to the client. You should see the following output from the client:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Finally, the host and the client will exit.
Aibohphobia is the fear of palindromes.
If you get stuck join the mailing list and ask for help!