Skip to content

Commit

Permalink
Add std-only examples
Browse files Browse the repository at this point in the history
  • Loading branch information
DemesneGH committed Oct 15, 2024
1 parent 3d1545e commit de90847
Show file tree
Hide file tree
Showing 112 changed files with 4,763 additions and 2 deletions.
1 change: 1 addition & 0 deletions .licenserc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ header:
- 'KEYS'
- 'DISCLAIMER-WIP'
- '*.json'
- 'examples/tls_server-rs/ta/test-ca/**'
10 changes: 10 additions & 0 deletions ci/ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,14 @@ pushd ../tests
./test_supp_plugin.sh
./test_error_handling.sh

# Run std only tests
if [ "$STD" ]; then
./test_serde.sh
./test_message_passing_interface.sh
./test_tcp_client.sh
./test_udp_socket.sh
./test_tls_client.sh
./test_tls_server.sh
fi

popd
2 changes: 1 addition & 1 deletion environment
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,4 @@ then
else
echo -e "Error: OPTEE_CLIENT_EXPORT=$OPTEE_CLIENT_EXPORT does not exist, please set the correct OPTEE_CLIENT_EXPORT or run \"$ ./build_optee_libraries.sh optee/\" then try again\n"
unset OPTEE_DIR
fi
fi
33 changes: 33 additions & 0 deletions examples/message_passing_interface-rs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

# If _HOST or _TA specific compiler/target are not specified, then use common
# compiler/target for both
CROSS_COMPILE_HOST ?= aarch64-linux-gnu-
CROSS_COMPILE_TA ?= aarch64-linux-gnu-
TARGET_HOST ?= aarch64-unknown-linux-gnu
TARGET_TA ?= aarch64-unknown-linux-gnu

all:
$(q)make -C host TARGET_HOST=$(TARGET_HOST) \
CROSS_COMPILE_HOST=$(CROSS_COMPILE_HOST)
$(q)make -C ta TARGET_TA=$(TARGET_TA) \
CROSS_COMPILE_TA=$(CROSS_COMPILE_TA)

clean:
$(q)make -C host clean
$(q)make -C ta clean
33 changes: 33 additions & 0 deletions examples/message_passing_interface-rs/host/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

[package]
name = "message_passing_interface-rs"
version = "0.3.0"
authors = ["Teaclave Contributors <[email protected]>"]
license = "Apache-2.0"
repository = "https://github.com/apache/incubator-teaclave-trustzone-sdk.git"
description = "An example of Rust OP-TEE TrustZone SDK."
edition = "2018"

[dependencies]
url = "2.5.0"
proto = { path = "../proto" }
optee-teec = { path = "../../../optee-teec" }

[profile.release]
lto = true
38 changes: 38 additions & 0 deletions examples/message_passing_interface-rs/host/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

NAME := message_passing_interface-rs

TARGET_HOST ?= aarch64-unknown-linux-gnu
CROSS_COMPILE_HOST ?= aarch64-linux-gnu-
OBJCOPY := $(CROSS_COMPILE_HOST)objcopy
LINKER_CFG := target.$(TARGET_HOST).linker=\"$(CROSS_COMPILE_HOST)gcc\"

OUT_DIR := $(CURDIR)/target/$(TARGET_HOST)/release


all: host strip

host:
@cargo build --target $(TARGET_HOST) --release --config $(LINKER_CFG)

strip: host
@$(OBJCOPY) --strip-unneeded $(OUT_DIR)/$(NAME) $(OUT_DIR)/$(NAME)

clean:
@cargo clean

80 changes: 80 additions & 0 deletions examples/message_passing_interface-rs/host/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

use optee_teec::{Context, Operation, ParamNone, ParamTmpRef, ParamType, ParamValue, Uuid};
use proto;
use url;

type Result<T> = optee_teec::Result<T>;

pub struct EnclaveClient {
uuid: String,
context: optee_teec::Context,
buffer: Vec<u8>,
}

impl EnclaveClient {
pub fn open(url: &str) -> Result<Self> {
let url = url::Url::parse(url).unwrap();
match url.scheme() {
"trustzone-enclave" => Self::open_uuid(url.host_str().unwrap()),
_ => unimplemented!(),
}
}

fn open_uuid(uuid: &str) -> Result<Self> {
let context = Context::new()?;
Ok(Self {
uuid: uuid.to_string(),
context: context,
buffer: vec![0; 128],
})
}

pub fn invoke(&mut self, input: &proto::EnclaveInput) -> Result<proto::EnclaveOutput> {
let command_id = input.command as u32;
let mut serialized_input = proto::serde_json::to_vec(input).unwrap();

let p0 = ParamTmpRef::new_input(serialized_input.as_mut_slice());
let p1 = ParamTmpRef::new_output(&mut self.buffer);
let p2 = ParamValue::new(0, 0, ParamType::ValueInout);

let mut operation = Operation::new(0, p0, p1, p2, ParamNone);

let uuid = Uuid::parse_str(&self.uuid).unwrap();
let mut session = self.context.open_session(uuid)?;
session.invoke_command(command_id, &mut operation)?;
let len = operation.parameters().2.a() as usize;

let output: proto::EnclaveOutput =
proto::serde_json::from_slice(&self.buffer[0..len]).unwrap();
Ok(output)
}
}

fn main() -> optee_teec::Result<()> {
let url = format!("trustzone-enclave://{}", proto::UUID);
let mut enclave = EnclaveClient::open(&url).unwrap();
let input = proto::EnclaveInput {
command: proto::Command::Hello,
message: String::from("World!"),
};
let output = enclave.invoke(&input).unwrap();
println!("{:?}", output);

Ok(())
}
32 changes: 32 additions & 0 deletions examples/message_passing_interface-rs/proto/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

[package]
name = "proto"
version = "0.3.0"
authors = ["Teaclave Contributors <[email protected]>"]
license = "Apache-2.0"
repository = "https://github.com/apache/incubator-teaclave-trustzone-sdk.git"
description = "Data structures and functions shared by host and TA."
edition = "2018"

[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

[build-dependencies]
uuid = { version = "1.8", default-features = false }
36 changes: 36 additions & 0 deletions examples/message_passing_interface-rs/proto/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

use std::fs;
use std::path::PathBuf;
use std::fs::File;
use std::env;
use std::io::Write;

fn main() {
let uuid = match fs::read_to_string("../uuid.txt") {
Ok(u) => {
u.trim().to_string()
},
Err(_) => {
panic!("Cannot find uuid.txt");
}
};
let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
let mut buffer = File::create(out.join("uuid.txt")).unwrap();
write!(buffer, "{}", uuid).unwrap();
}
51 changes: 51 additions & 0 deletions examples/message_passing_interface-rs/proto/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

use serde::{Serialize, Deserialize};
pub use serde_json;

#[derive(Serialize, Deserialize, Debug, Copy, Clone)]
pub enum Command {
Hello,
Bye,
Unknown,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct EnclaveInput {
pub command: Command,
pub message: String
}

#[derive(Serialize, Deserialize, Debug)]
pub struct EnclaveOutput {
pub message: String
}

impl From<u32> for Command {
#[inline]
fn from(value: u32) -> Command {
match value {
0 => Command::Hello,
1 => Command::Bye,
_ => Command::Unknown,
}
}
}


pub const UUID: &str = &include_str!(concat!(env!("OUT_DIR"), "/uuid.txt"));
39 changes: 39 additions & 0 deletions examples/message_passing_interface-rs/ta/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

[package]
name = "ta"
version = "0.3.0"
authors = ["Teaclave Contributors <[email protected]>"]
license = "Apache-2.0"
repository = "https://github.com/apache/incubator-teaclave-trustzone-sdk.git"
description = "An example of Rust OP-TEE TrustZone SDK."
edition = "2018"

[dependencies]
proto = { path = "../proto" }
optee-utee-sys = { path = "../../../optee-utee/optee-utee-sys" }
optee-utee = { path = "../../../optee-utee" }

[build-dependencies]
uuid = { version = "1.8", default-features = false }
proto = { path = "../proto" }

[profile.release]
panic = "abort"
lto = false
opt-level = 1
Loading

0 comments on commit de90847

Please sign in to comment.