# Adding a software example in the Keystone framework


> This post has been written with the help of [@OussamaELmnaouri2001](https://github.com/OussamaELmnaouri2001).

The Keystone framework provides [some examples](https://github.com/keystone-enclave/keystone/tree/master/examples). The goal of this blogpost is to explain how we can add a new example and running it easily.

[@OussamaELmnaouri2001](https://github.com/OussamaELmnaouri2001) has a repository with some examples: https://github.com/OussamaELmnaouri2001/Keystone-Examples. In this tutorial, we will add [addition example](https://github.com/OussamaELmnaouri2001/Keystone-Examples/tree/master/addition).

## Requirements

It is assumed that the Keystone framework repository has been cloned on the host:
```bash
KEYSTONE_REPO=$PWD/keystone
git clone https://github.com/keystone-enclave/keystone
cd keystone
git checkout 88c49ee
git submodule update --init --recursive
```

```bash
cd $PWD
git clone https://github.com/OussamaELmnaouri2001/Keystone-Examples
```

## Adding an example
We will copy the existing addition example in the Keystone repository

```bash
cd $PWD
cp -R Keystone-Examples/addition keystone/examples
```

If you want to copy and modify an original example from the Keystone repository, the main modification is related to the `CMakeLists.txt` file included in the example directory. For instance, for the `hello` example: https://github.com/keystone-enclave/keystone/blob/master/examples/hello/CMakeLists.txt

You just need to modify binary and file names to match the new example. Basically, just replace the `hello` string by yours:

```bash
set(eapp_bin hello)
set(eapp_src eapp/hello.c)
set(host_bin hello-runner)
set(host_src host/host.cpp)
set(package_name "hello.ke")
set(package_script "./hello-runner hello eyrie-rt loader.bin")
set(eyrie_plugins "io_syscall linux_syscall env_setup")
```

Then, we need to modify a [CMakeLists.txt](https://github.com/keystone-enclave/keystone/blob/master/examples/CMakeLists.txt) to compile the addition as for other existing examples:

```bash
nano keystone/examples/CMakeLists.txt
```

```diff
@@ -35,3 +35,4 @@ add_subdirectory(hello)
 add_subdirectory(hello-native)
 add_subdirectory(attestation)
 add_subdirectory(tests)
+add_subdirectory(addition)
```

## Compiling the new image and running the example

```bash
cd $PWD/keystone
# Compile the new image, faster than the first compilation
make -j$(nproc)
# Running the image in QEmu
make run
```

In the QEmu prompt, examples are still in the original location:

```bash
$ modprobe keystone-driver
$ /usr/share/keystone/examples/addition.ke    
```

Of course, you can debug this new example as explained in [a previous post](https://pcotret.gitlab.io/blog/keystone_debug/).
