README.md
1# Teaclave TrustZone SDK
2
3[](LICENSE)
4[](https://github.com/apache/incubator-teaclave-trustzone-sdk/releases)
5[](https://teaclave.apache.org/)
6
7Teaclave TrustZone SDK (Rust OP-TEE TrustZone SDK) provides abilities to build
8safe TrustZone applications in Rust. The SDK is based on the
9[OP-TEE](https://www.op-tee.org/) project which follows
10[GlobalPlatform](https://globalplatform.org/) [TEE
11specifications](https://globalplatform.org/specs-library/tee-internal-core-api-specification/)
12and provides ergonomic APIs. In addition, it enables the capability to write
13TrustZone applications with Rust's standard library (std) and many third-party
14libraries (i.e., crates). Teaclave TrustZone SDK is a sub-project of [Apache
15Teaclave (incubating)](https://teaclave.apache.org/).
16
17## Table of Contents
18
19- [Quick start with the OP-TEE Repo for QEMUv8](#quick-start-with-the-op-tee-repo-for-qemuv8)
20- [Getting started](#getting-started)
21 - [Environment](#environment)
22 - [Develop with QEMUv8](#develop-with-qemuv8)
23 - [Develop on other platforms](#develop-on-other-platforms)
24 - [Build & Install](#build--install)
25 - [Run Rust Applications](#run-rust-applications)
26 - [Run Rust Applications in QEMUv8](#run-rust-applications-in-qemuv8)
27 - [Run Rust Applications on other platforms](#run-rust-applications-on-other-platforms)
28- [Documentation](#documentation)
29- [Publication](#publication)
30- [Contributing](#contributing)
31- [Community](#community)
32
33## Quick start with the OP-TEE Repo for QEMUv8
34
35Teaclave TrustZone SDK has been integrated into the OP-TEE Repo since OP-TEE
36Release 3.15.0 (18/Oct/21). The aarch64 Rust examples are built and installed
37into OP-TEE's default filesystem for QEMUv8. Follow [this
38documentation](https://optee.readthedocs.io/en/latest/building/optee_with_rust.html)
39to set up the OP-TEE repo and try the Rust examples!
40
41## Getting started
42
43### Environment
44
45To get started with Teaclave TrustZone SDK, you could choose either [QEMU for
46Armv8-A](#develop-with-qemuv8) (QEMUv8) or [other
47platforms](#develop-on-other-platforms) ([platforms OP-TEE
48supported](https://optee.readthedocs.io/en/latest/general/platforms.html)) as
49your development environment.
50
51#### Develop with QEMUv8
52
53The OP-TEE libraries are needed when building Rust applications, so you should
54finish the [Quick start with the OP-TEE Repo for
55QEMUv8](#quick-start-with-the-op-tee-repo-for-qemuv8) part first. Then
56initialize the building environment in Teaclave TrustZone SDK, build Rust
57applications and copy them into the target's filesystem.
58
59Teaclave TrustZone SDK is located in `[YOUR_OPTEE_DIR]/optee_rust/`. Teaclave
60TrustZone SDK in OP-TEE repo is pinned to the release version. Alternatively,
61you can try the develop version using `git pull`:
62
63```sh
64cd [YOUR_OPTEE_DIR]/optee_rust/
65git pull github master
66```
67
68#### Develop on other platforms
69
70If you are building trusted applications for other platforms ([platforms OP-TEE
71supported](https://optee.readthedocs.io/en/latest/general/platforms.html)). QEMU
72and the filesystem in the OP-TEE repo are not needed. You can follow these
73steps to clone the project and build applications independently from the
74complete OP-TEE repo. In this case, the necessary OP-TEE libraries are
75initialized in the setup process.
76
771. The complete list of prerequisites can be found here: [OP-TEE
78Prerequisites](https://optee.readthedocs.io/en/latest/building/prerequisites.html).
79
80``` sh
81# install dependencies
82sudo apt-get install android-tools-adb android-tools-fastboot autoconf \
83 automake bc bison build-essential ccache cscope curl device-tree-compiler \
84 expect flex ftp-upload gdisk iasl libattr1-dev libc6:i386 libcap-dev \
85 libfdt-dev libftdi-dev libglib2.0-dev libhidapi-dev libncurses5-dev \
86 libpixman-1-dev libssl-dev libstdc++6:i386 libtool libz1:i386 make \
87 mtools netcat python-crypto python3-crypto python-pyelftools \
88 python3-pycryptodome python3-pyelftools python-serial python3-serial \
89 rsync unzip uuid-dev xdg-utils xterm xz-utils zlib1g-dev
90```
91
92Alternatively, you can use a docker container built with our
93[Dockerfile](Dockerfile).
94
952. After installing dependencies or building the Docker image, fetch the source
96 code from the official GitHub repository:
97
98``` sh
99# clone the project
100git clone https://github.com/apache/incubator-teaclave-trustzone-sdk.git
101cd incubator-teaclave-trustzone-sdk
102```
103
104### Build & Install
105
106To build the project, the Rust environment and several related submodules are
107required.
108
1091. By default, the `OPTEE_DIR` is `incubator-teaclave-trustzone-sdk/optee/`.
110 OP-TEE submodules (`optee_os`, `optee_client` and `build`) will be initialized
111automatically in `setup.sh`.
112
113If you are building within QEMUv8 or already have the [OP-TEE
114repository](https://github.com/OP-TEE) cloned somewhere, you can set the OP-TEE
115root directory with:
116
117```sh
118export OPTEE_DIR=[YOUR_OPTEE_DIR]
119```
120
121Note: your OPTEE root directory should have `build/`, `optee_os/` and
122`optee_client/` as sub-directory.
123
1242. Run the script as follows to install the Rust environment and initialize
125 submodules:
126
127```sh
128./setup.sh
129```
130
1313. Before building examples, the environment should be properly set up with:
132
133``` sh
134source environment
135```
136
137Note: by default, the target platform is `aarch64`. If you want to build for the
138`arm` target, you can setup `ARCH` before the `source environment` command:
139
140```sh
141export ARCH=arm
142source environment
143```
144
1454. Before building rust examples and applications, you need to build OP-TEE
146 libraries using:
147
148``` sh
149make optee
150```
151
1525. Run this command to build all Rust examples:
153
154``` sh
155make examples
156```
157
158Or build your own CA and TA:
159
160```sh
161make -C examples/[YOUR_APPLICATION]
162```
163
164Besides, you can collect all example CAs and TAs to
165`/incubator-teaclave-trustzone-sdk/out`:
166
167```sh
168make examples-install
169```
170
171### Run Rust Applications
172
173Considering the platform has been chosen
174([QEMUv8](#run-rust-applications-in-qemuv8) or
175[other](#run-rust-applications-on-other-platforms)), the ways to run the Rust
176applications are different.
177
178#### Run Rust Applications in QEMUv8
179
1801. The shared folder is needed to share CAs and TAs with the QEMU guest system.
181Recompile QEMU in OP-TEE to enable QEMU VirtFS:
182
183```sh
184(cd $OPTEE_DIR/build && make QEMU_VIRTFS_ENABLE=y qemu)
185```
186
1872. Copy all the Rust examples or your own applications to the shared folder:
188
189```sh
190mkdir shared_folder
191cd [YOUR_OPTEE_DIR]/optee_rust/ && make examples-install)
192cp -r [YOUR_OPTEE_DIR]/optee_rust/out/* shared_folder/
193```
194
1953. Run QEMU:
196
197```sh
198(cd $OPTEE_DIR/build && make run-only QEMU_VIRTFS_ENABLE=y
199QEMU_VIRTFS_HOST_DIR=$(pwd)/shared_folder)
200```
201
2024. After the QEMU has been booted, you need to mount the shared folder in the
203QEMU guest system (username: root), in order to access the compiled CA/TA from
204QEMU. Run the command as follows in the QEMU guest terminal:
205
206```sh
207mkdir shared && mount -t 9p -o trans=virtio host shared
208```
209
2105. Then run CA and TA as [this
211documentation](https://optee.readthedocs.io/en/latest/building/optee_with_rust.html)
212describes.
213
214#### Run Rust Applications on other platforms
215
216Copy the applications to your platform and run.
217
218## Documentation
219
220- [Overview of OP-TEE Rust
221 Examples](https://teaclave.apache.org/trustzone-sdk-docs/overview-of-optee-rust-examples/)
222- [Debugging OP-TEE
223 TA](https://teaclave.apache.org/trustzone-sdk-docs/debugging-optee-ta.md/)
224- [Host API
225 Reference](https://teaclave.apache.org/api-docs/trustzone-sdk/optee-teec/)
226- [TA API
227 Reference](https://teaclave.apache.org/api-docs/trustzone-sdk/optee-utee/)
228
229## Publication
230
231More details about the design and implementation can be found in our paper
232published in ACSAC 2020:
233[RusTEE: Developing Memory-Safe ARM TrustZone
234Applications](https://csis.gmu.edu/ksun/publications/ACSAC20_RusTEE_2020.pdf).
235Here is the BiBTeX record for your reference.
236
237```bibtex
238@inproceedings{wan20rustee,
239 author = "Shengye Wan and Mingshen Sun and Kun Sun and Ning Zhang and Xu
240He",
241 title = "{RusTEE: Developing Memory-Safe ARM TrustZone Applications}",
242 booktitle = "Proceedings of the 36th Annual Computer Security Applications
243Conference",
244 series = "ACSAC '20",
245 year = "2020",
246 month = "12",
247}
248```
249
250## Contributing
251
252Teaclave is open source in [The Apache
253Way](https://www.apache.org/theapacheway/),
254we aim to create a project that is maintained and owned by the community. All
255kinds of contributions are welcome.
256Thanks to our [contributors](https://teaclave.apache.org/contributors/).
257
258## Community
259
260- Join us on our [mailing
261 list](https://lists.apache.org/list.html?dev@teaclave.apache.org).
262- Follow us at [@ApacheTeaclave](https://twitter.com/ApacheTeaclave).
263- See [more](https://teaclave.apache.org/community/).
264