Skip to content
This repository was archived by the owner on May 9, 2022. It is now read-only.

Commit 2dbba24

Browse files
longtomjrPiDelport
andcommitted
feat(exec_enclave): add sample function scaffolding
Co-authored-by: Pi Delport <[email protected]>
1 parent b2f083b commit 2dbba24

File tree

5 files changed

+95
-0
lines changed

5 files changed

+95
-0
lines changed

rtc_exec_enclave/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rtc_exec_enclave/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ cbindgen = "0.19.0"
1515

1616
# See "Pinning SGX dependencies" in HACKING.md
1717
[target.'cfg(not(target_env = "sgx"))'.dependencies]
18+
sgx_tcrypto = { git = "https://github.com/apache/incubator-teaclave-sgx-sdk.git", rev = "b9d1bda" }
1819
sgx_types = { git = "https://github.com/apache/incubator-teaclave-sgx-sdk.git", rev = "b9d1bda", features = ["extra_traits"] }
1920
sgx_tstd = { git = "https://github.com/apache/incubator-teaclave-sgx-sdk.git", rev = "b9d1bda", features = ["backtrace"] }
2021

rtc_exec_enclave/src/lib.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,55 @@
33
#![deny(unsafe_op_in_unsafe_fn)]
44
#![deny(clippy::mem_forget)]
55

6+
mod sample_functions;
7+
mod types;
8+
69
#[cfg(not(target_env = "sgx"))]
710
extern crate sgx_tstd as std;
811

12+
use std::boxed::Box;
13+
use std::vec;
14+
915
pub use rtc_tenclave::dh::*;
1016
pub use rtc_tenclave::enclave::*;
17+
18+
use crate::types::*;
19+
20+
pub struct Token {
21+
binary_hash: [u8; 32],
22+
}
23+
24+
#[allow(clippy::result_unit_err)]
25+
pub fn request_execution(token: Token, _params: ()) -> Result<Box<[u8]>, ()> {
26+
let exec_module = match get_module_by_id(token.binary_hash) {
27+
Some(val) => val,
28+
None => return Err(()),
29+
};
30+
let data = get_data(&token);
31+
32+
// as_ptr() does not take ownership, so the data will be dropped at the end of this function
33+
let result = unsafe { exec_module.call(data.as_ptr(), data.len()) };
34+
35+
match result {
36+
Ok(val) => Ok(val),
37+
// XXX: The error handling here should take into account the Traps from WASM once we call
38+
// WASM functions
39+
Err(_) => Err(()),
40+
}
41+
}
42+
43+
// XXX: This is placeholder until we completed the data retrieval flow and know what values
44+
// we need to pass through to the data enclave
45+
fn get_data(_token: &Token) -> Box<[u8]> {
46+
vec![123; 43].into_boxed_slice()
47+
}
48+
49+
// XXX: The implementation is only for the sample functions currently
50+
pub(crate) fn get_module_by_id(module_id: [u8; 32]) -> Option<Box<dyn ExecModule>> {
51+
use sample_functions::*;
52+
match module_id {
53+
SHA256_HASH_MODULE_ID => Some(Box::new(Sha256HashModule)),
54+
MEDIAN_MODULE_ID => Some(Box::new(MedianModule)),
55+
_ => None,
56+
}
57+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use crate::types::*;
2+
use core::slice;
3+
4+
pub(crate) trait SampleExecModule {
5+
fn call_safe(&self, dataset: &[u8]) -> ExecResult;
6+
}
7+
8+
impl<T> ExecModule for T
9+
where
10+
T: SampleExecModule,
11+
{
12+
unsafe fn call(&self, dataset_ptr: *const u8, dataset_len: usize) -> ExecResult {
13+
let dataset = unsafe { slice::from_raw_parts(dataset_ptr, dataset_len) };
14+
self.call_safe(dataset)
15+
}
16+
}
17+
18+
pub(crate) const SHA256_HASH_MODULE_ID: [u8; 32] = [1u8; 32];
19+
pub(crate) struct Sha256HashModule;
20+
21+
impl SampleExecModule for Sha256HashModule {
22+
fn call_safe(&self, dataset: &[u8]) -> ExecResult {
23+
match sgx_tcrypto::rsgx_sha256_slice(dataset) {
24+
Ok(res) => Ok(res.to_vec().into_boxed_slice()),
25+
Err(_) => Err(()),
26+
}
27+
}
28+
}
29+
30+
pub(crate) const MEDIAN_MODULE_ID: [u8; 32] = [2u8; 32];
31+
pub(crate) struct MedianModule;
32+
33+
impl SampleExecModule for MedianModule {
34+
fn call_safe(&self, _dataset: &[u8]) -> ExecResult {
35+
todo!()
36+
}
37+
}

rtc_exec_enclave/src/types.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use std::boxed::Box;
2+
3+
pub(crate) type CallReturnValue = Box<[u8]>;
4+
5+
pub(crate) type ExecResult = core::result::Result<CallReturnValue, ()>;
6+
7+
pub(crate) trait ExecModule {
8+
unsafe fn call(&self, dataset_ptr: *const u8, dataset_len: usize) -> ExecResult;
9+
}

0 commit comments

Comments
 (0)