Skip to content

Commit caddfdc

Browse files
committed
examples: Add secure_db_abstraction (std-only)
Add reference implementation for TA that simplifies interaction with secure storage. It provides basic methods for database operations, including `get()`, `put()`, `delete_entries()`, and `list_entries()`, making it easier for developers to store and retrieve data based on Rust Type constraints. The example is std-only for now. Signed-off-by: Yuan Zhuang <yuanz@apache.org>
1 parent d5349ad commit caddfdc

File tree

19 files changed

+888
-0
lines changed

19 files changed

+888
-0
lines changed

ci/ci.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ if [ "$STD" ]; then
4545
./test_tls_client.sh
4646
./test_tls_server.sh
4747
./test_eth_wallet.sh
48+
./test_secure_db_abstraction.sh
4849
fi
4950

5051
popd
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
# If _HOST or _TA specific compiler/target are not specified, then use common
19+
# compiler/target for both
20+
CROSS_COMPILE_HOST ?= aarch64-linux-gnu-
21+
CROSS_COMPILE_TA ?= aarch64-linux-gnu-
22+
TARGET_HOST ?= aarch64-unknown-linux-gnu
23+
TARGET_TA ?= aarch64-unknown-linux-gnu
24+
25+
.PHONY: host ta all clean
26+
27+
all: host ta
28+
29+
host:
30+
$(q)make -C host TARGET=$(TARGET_HOST) \
31+
CROSS_COMPILE=$(CROSS_COMPILE_HOST)
32+
33+
ta:
34+
$(q)make -C ta TARGET=$(TARGET_TA) \
35+
CROSS_COMPILE=$(CROSS_COMPILE_TA)
36+
37+
clean:
38+
$(q)make -C host clean
39+
$(q)make -C ta clean
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
[package]
19+
name = "secure_db_abstraction-rs"
20+
version = "0.1.0"
21+
authors = ["Teaclave Contributors <dev@teaclave.apache.org>"]
22+
license = "Apache-2.0"
23+
repository = "https://github.com/apache/incubator-teaclave-trustzone-sdk.git"
24+
description = "An example of Rust OP-TEE TrustZone SDK."
25+
edition = "2018"
26+
27+
[dependencies]
28+
proto = { path = "../proto" }
29+
optee-teec = { path = "../../../optee-teec" }
30+
31+
[profile.release]
32+
lto = true
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
# STD-ONLY example
19+
20+
NAME := secure_db_abstraction-rs
21+
22+
TARGET ?= aarch64-unknown-linux-gnu
23+
CROSS_COMPILE ?= aarch64-linux-gnu-
24+
OBJCOPY := $(CROSS_COMPILE)objcopy
25+
LINKER_CFG := target.$(TARGET).linker=\"$(CROSS_COMPILE)gcc\"
26+
27+
OUT_DIR := $(CURDIR)/target/$(TARGET)/release
28+
29+
ifeq ($(STD),)
30+
all:
31+
@echo "Please \`export STD=y\` then rerun \`source environment\` to build the STD version"
32+
else
33+
all: host strip
34+
endif
35+
36+
host:
37+
@cargo build --target $(TARGET_HOST) --release --config $(LINKER_CFG)
38+
39+
strip: host
40+
@$(OBJCOPY) --strip-unneeded $(OUT_DIR)/$(NAME) $(OUT_DIR)/$(NAME)
41+
42+
clean:
43+
@cargo clean
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
use optee_teec::{Context, ErrorKind, Operation, ParamNone, Uuid};
19+
use proto::{Command, UUID};
20+
21+
fn main() -> optee_teec::Result<()> {
22+
let mut ctx = Context::new()?;
23+
let uuid =
24+
Uuid::parse_str(UUID).map_err(|_| optee_teec::Error::from(ErrorKind::BadParameters))?;
25+
let mut session = ctx.open_session(uuid)?;
26+
let mut operation = Operation::new(0, ParamNone, ParamNone, ParamNone, ParamNone);
27+
28+
// Nothing to send, just invoke the Test command
29+
session.invoke_command(Command::Test as u32, &mut operation)?;
30+
println!("Success");
31+
Ok(())
32+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
[package]
19+
name = "proto"
20+
version = "0.1.0"
21+
authors = ["Teaclave Contributors <dev@teaclave.apache.org>"]
22+
license = "Apache-2.0"
23+
repository = "https://github.com/apache/incubator-teaclave-trustzone-sdk.git"
24+
description = "Data structures and functions shared by host and TA."
25+
edition = "2018"
26+
27+
[dependencies]
28+
num_enum = { version = "0.7.3", default-features = false }
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
use num_enum::{FromPrimitive, IntoPrimitive};
19+
20+
#[derive(FromPrimitive, IntoPrimitive)]
21+
#[repr(u32)]
22+
pub enum Command {
23+
Test,
24+
#[default]
25+
Unknown,
26+
}
27+
28+
// If Uuid::parse_str() returns an InvalidLength error, there may be an extra
29+
// newline in your uuid.txt file. You can remove it by running
30+
// `truncate -s 36 uuid.txt`.
31+
pub const UUID: &str = &include_str!("../../uuid.txt");
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
[package]
19+
name = "ta"
20+
version = "0.1.0"
21+
authors = ["Teaclave Contributors <dev@teaclave.apache.org>"]
22+
license = "Apache-2.0"
23+
repository = "https://github.com/apache/incubator-teaclave-trustzone-sdk.git"
24+
description = "An example of Rust OP-TEE TrustZone SDK."
25+
edition = "2018"
26+
27+
[dependencies]
28+
proto = { path = "../proto" }
29+
optee-utee-sys = { path = "../../../optee-utee/optee-utee-sys" }
30+
optee-utee = { path = "../../../optee-utee" }
31+
bincode = "1.3.3"
32+
anyhow = "1.0"
33+
serde = { version = "1.0", features = ["derive"] }
34+
35+
[build-dependencies]
36+
proto = { path = "../proto" }
37+
optee-utee-build = { path = "../../../optee-utee-build" }
38+
39+
[profile.release]
40+
panic = "abort"
41+
lto = true
42+
opt-level = 1
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
# STD-ONLY example
19+
20+
UUID ?= $(shell cat "../uuid.txt")
21+
22+
TARGET ?= aarch64-unknown-linux-gnu
23+
CROSS_COMPILE ?= aarch64-linux-gnu-
24+
OBJCOPY := $(CROSS_COMPILE)objcopy
25+
# Configure the linker to use GCC, which works on both cross-compilation and ARM machines
26+
LINKER_CFG := target.$(TARGET).linker=\"$(CROSS_COMPILE)gcc\"
27+
28+
TA_SIGN_KEY ?= $(TA_DEV_KIT_DIR)/keys/default_ta.pem
29+
SIGN := $(TA_DEV_KIT_DIR)/scripts/sign_encrypt.py
30+
OUT_DIR := $(CURDIR)/target/$(TARGET)/release
31+
32+
ifeq ($(STD),)
33+
all:
34+
@echo "Please \`export STD=y\` then rerun \`source environment\` to build the STD version"
35+
else
36+
all: ta strip sign
37+
endif
38+
39+
ta:
40+
@xargo build --target $(TARGET) --release --config $(LINKER_CFG)
41+
42+
strip: ta
43+
@$(OBJCOPY) --strip-unneeded $(OUT_DIR)/ta $(OUT_DIR)/stripped_ta
44+
45+
sign: strip
46+
@$(SIGN) --uuid $(UUID) --key $(TA_SIGN_KEY) --in $(OUT_DIR)/stripped_ta --out $(OUT_DIR)/$(UUID).ta
47+
@echo "SIGN => ${UUID}"
48+
49+
clean:
50+
@cargo clean
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
[dependencies.std]
19+
path = "../../../rust/rust/library/std"
20+
21+
[patch.crates-io]
22+
libc = { path = "../../../rust/libc" }
23+
rustc-std-workspace-core = { path = "../../../rust/rust/library/rustc-std-workspace-core" }
24+
rustc-std-workspace-alloc = { path = "../../../rust/rust/library/rustc-std-workspace-alloc" }

0 commit comments

Comments
 (0)