Skip to content

Commit

Permalink
feat: Import data into the enclave
Browse files Browse the repository at this point in the history
  • Loading branch information
binglekruger committed Nov 16, 2022
1 parent eebad0c commit 86d66fe
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 105 deletions.
2 changes: 1 addition & 1 deletion rust-sgx-workspace/projects/ntc-tee-server/app/Enclave_u.c
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,7 @@ static const struct {
(void*)Enclave_u_fstatat64_ocall,
}
};
sgx_status_t append_data(sgx_enclave_id_t eid, sgx_status_t* retval, const uint8_t* some_string, size_t len)
sgx_status_t append_data(sgx_enclave_id_t eid, sgx_status_t* retval, const uint8_t* pool_one, size_t len)
{
sgx_status_t status;
ms_append_data_t ms;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

typedef struct ms_append_data_t {
sgx_status_t ms_retval;
const uint8_t* ms_some_string;
const uint8_t* ms_pool_one;
size_t ms_len;
const uint8_t* ms_pool_two;
size_t ms_len_two;
} ms_append_data_t;

typedef struct ms_t_global_init_ecall_t {
Expand Down Expand Up @@ -933,12 +935,14 @@ static const struct {
(void*)Enclave_u_fstatat64_ocall,
}
};
sgx_status_t append_data(sgx_enclave_id_t eid, sgx_status_t* retval, const uint8_t* some_string, size_t len)
sgx_status_t append_data(sgx_enclave_id_t eid, sgx_status_t* retval, const uint8_t* pool_one, size_t len, const uint8_t* pool_two, size_t len_two)
{
sgx_status_t status;
ms_append_data_t ms;
ms.ms_some_string = some_string;
ms.ms_pool_one = pool_one;
ms.ms_len = len;
ms.ms_pool_two = pool_two;
ms.ms_len_two = len_two;
status = sgx_ecall(eid, 0, &ocall_table_Enclave, &ms);
if (status == SGX_SUCCESS && retval) *retval = ms.ms_retval;
return status;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ int SGX_UBRIDGE(SGX_NOCONVENTION, u_dirfd_ocall, (int* error, void* dirp));
int SGX_UBRIDGE(SGX_NOCONVENTION, u_fstatat64_ocall, (int* error, int dirfd, const char* pathname, struct stat64_t* buf, int flags));
#endif

sgx_status_t append_data(sgx_enclave_id_t eid, sgx_status_t* retval, const uint8_t* some_string, size_t len);
sgx_status_t append_data(sgx_enclave_id_t eid, sgx_status_t* retval, const uint8_t* pool_one, size_t len, const uint8_t* pool_two, size_t len_two);
sgx_status_t t_global_init_ecall(sgx_enclave_id_t eid, uint64_t id, const uint8_t* path, size_t len);
sgx_status_t t_global_exit_ecall(sgx_enclave_id_t eid);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ extern "C" {
pub fn append_data(
eid: sgx_enclave_id_t,
retval: *mut sgx_status_t,
some_string: *const u8,
pool_one: *const u8,
len: size_t,
pool_two: *const u8,
len_two: size_t,
) -> sgx_status_t;
}
88 changes: 83 additions & 5 deletions rust-sgx-workspace/projects/ntc-tee-server/app/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ extern "C" {
fn append_data(
eid: sgx_enclave_id_t,
retval: *mut sgx_status_t,
input_string: *const u8,
input_length: usize,
pool_one: *const u8,
pool_one_length: usize,
pool_two: *const u8,
pool_two_length: usize,
) -> sgx_status_t;

}
Expand Down Expand Up @@ -50,16 +52,92 @@ fn main() {
};

// Update - send sealed binary data into enclave (from CosmosDB)
let input_string = String::from("Sending this string to the enclave then printing it\n");
// Test pool 1 - Import existing data pool from CosmosDB
let pool_one = r#"
{
"pool": [
{
"ID":1,
"Income":109748.717064175,
"Postal":41,
"WalletID":"9173f117-eaf5-443b-a405-c3d5e2aa5d99"
},
{
"ID":2,
"Income":77356.0675380472,
"Postal":15,
"WalletID":"9f86ae7c-ac1c-4b8e-9daf-bd40ad13e157"
},
{
"ID":3,
"Income":33746.4377853607,
"Postal":11,
"WalletID":"3e77ee27-b25b-4bcc-b9b8-d7107a633ae9"
},
{
"ID":4,
"Income":37657.0518817567,
"Postal":48,
"WalletID":"67be8d2b-c31a-44b6-9cb0-6456072d3353"
},
{
"ID":5,
"Income":27353.1866838295,
"Postal":5,
"WalletID":"412326e0-716a-4233-bae1-4cd199207893"
}
]
}
"#;

// Test pool 2 - Import append pool from CosmosDB (Temp data pool)
let pool_two = r#"
{
"pool": [
{
"ID":6,
"Income":71540.129423949,
"Postal":34,
"WalletID":"dabcbe30-4ed0-4c3b-b6b3-aafb85312963"
},
{
"ID":7,
"Income":7352.38903575921,
"Postal":41,
"WalletID":"fdcd0caa-48bf-446f-b00f-a90d646f55c3"
},
{
"ID":8,
"Income":110742.078725787,
"Postal":7,
"WalletID":"f4f5cd1b-c2ad-4587-905d-47237ebdbd89"
},
{
"ID":9,
"Income":129564.725166652,
"Postal":3,
"WalletID":"7e4e80c4-7e9b-4a6e-8d94-e4f72bde8233"
},
{
"ID":10,
"Income":2964.80996131516,
"Postal":33,
"WalletID":"ecefdd47-aab3-4595-87f7-cf9bbef46a53"
}
]
}
"#;

let mut retval = sgx_status_t::SGX_SUCCESS;

let result = unsafe {
append_data(
enclave.geteid(),
&mut retval,
input_string.as_ptr() as *const u8,
input_string.len(),
pool_one.as_ptr() as *const u8,
pool_one.len(),
pool_two.as_ptr() as *const u8,
pool_two.len(),
)
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ enclave {
from "sgx_backtrace.edl" import *;
trusted
{
public sgx_status_t append_data([in, size=len] const uint8_t* some_string, size_t len);
public sgx_status_t append_data([in, size=len] const uint8_t* pool_one, size_t len, [in, size=len_two] const uint8_t* pool_two, size_t len_two);
};
untrusted
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@

typedef struct ms_append_data_t {
sgx_status_t ms_retval;
const uint8_t* ms_some_string;
const uint8_t* ms_pool_one;
size_t ms_len;
const uint8_t* ms_pool_two;
size_t ms_len_two;
} ms_append_data_t;

typedef struct ms_t_global_init_ecall_t {
Expand Down Expand Up @@ -456,41 +458,65 @@ static sgx_status_t SGX_CDECL sgx_append_data(void* pms)
sgx_lfence();
ms_append_data_t* ms = SGX_CAST(ms_append_data_t*, pms);
sgx_status_t status = SGX_SUCCESS;
const uint8_t* _tmp_some_string = ms->ms_some_string;
const uint8_t* _tmp_pool_one = ms->ms_pool_one;
size_t _tmp_len = ms->ms_len;
size_t _len_some_string = _tmp_len;
uint8_t* _in_some_string = NULL;
size_t _len_pool_one = _tmp_len;
uint8_t* _in_pool_one = NULL;
const uint8_t* _tmp_pool_two = ms->ms_pool_two;
size_t _tmp_len_two = ms->ms_len_two;
size_t _len_pool_two = _tmp_len_two;
uint8_t* _in_pool_two = NULL;

CHECK_UNIQUE_POINTER(_tmp_some_string, _len_some_string);
CHECK_UNIQUE_POINTER(_tmp_pool_one, _len_pool_one);
CHECK_UNIQUE_POINTER(_tmp_pool_two, _len_pool_two);

//
// fence after pointer checks
//
sgx_lfence();

if (_tmp_some_string != NULL && _len_some_string != 0) {
if ( _len_some_string % sizeof(*_tmp_some_string) != 0)
if (_tmp_pool_one != NULL && _len_pool_one != 0) {
if ( _len_pool_one % sizeof(*_tmp_pool_one) != 0)
{
status = SGX_ERROR_INVALID_PARAMETER;
goto err;
}
_in_some_string = (uint8_t*)malloc(_len_some_string);
if (_in_some_string == NULL) {
_in_pool_one = (uint8_t*)malloc(_len_pool_one);
if (_in_pool_one == NULL) {
status = SGX_ERROR_OUT_OF_MEMORY;
goto err;
}

if (memcpy_s(_in_some_string, _len_some_string, _tmp_some_string, _len_some_string)) {
if (memcpy_s(_in_pool_one, _len_pool_one, _tmp_pool_one, _len_pool_one)) {
status = SGX_ERROR_UNEXPECTED;
goto err;
}

}
if (_tmp_pool_two != NULL && _len_pool_two != 0) {
if ( _len_pool_two % sizeof(*_tmp_pool_two) != 0)
{
status = SGX_ERROR_INVALID_PARAMETER;
goto err;
}
_in_pool_two = (uint8_t*)malloc(_len_pool_two);
if (_in_pool_two == NULL) {
status = SGX_ERROR_OUT_OF_MEMORY;
goto err;
}

if (memcpy_s(_in_pool_two, _len_pool_two, _tmp_pool_two, _len_pool_two)) {
status = SGX_ERROR_UNEXPECTED;
goto err;
}

}

ms->ms_retval = append_data((const uint8_t*)_in_some_string, _tmp_len);
ms->ms_retval = append_data((const uint8_t*)_in_pool_one, _tmp_len, (const uint8_t*)_in_pool_two, _tmp_len_two);

err:
if (_in_some_string) free(_in_some_string);
if (_in_pool_one) free(_in_pool_one);
if (_in_pool_two) free(_in_pool_two);
return status;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
extern "C" {
#endif

sgx_status_t append_data(const uint8_t* some_string, size_t len);
sgx_status_t append_data(const uint8_t* pool_one, size_t len, const uint8_t* pool_two, size_t len_two);
void t_global_init_ecall(uint64_t id, const uint8_t* path, size_t len);
void t_global_exit_ecall(void);

Expand Down
90 changes: 10 additions & 80 deletions rust-sgx-workspace/projects/ntc-tee-server/enclave/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ extern crate sgx_types;
extern crate sgx_tstd as std;
extern crate serde_json;

use std::io::{self, Write};
use std::slice;
use std::string::String;
use std::vec::Vec;
Expand All @@ -15,89 +14,20 @@ use serde_json::Value;
use sgx_types::sgx_status_t;

/// # Safety
/// Caller needs to ensure that `some_string` points to a valid slice of length `some_len`
/// Caller needs to ensure that `pool_one` points to a valid slice of length `pool_one_len`
/// Caller needs to ensure that `pool_two` points to a valid slice of length `pool_two_len`
#[no_mangle]
pub unsafe extern "C" fn append_data(some_string: *const u8, some_len: usize) -> sgx_status_t {
let str_slice = unsafe { slice::from_raw_parts(some_string, some_len) };
let _ = io::stdout().write(str_slice);
pub unsafe extern "C" fn append_data(pool_one: *const u8, pool_one_len: usize, pool_two: *const u8, pool_two_len: usize) -> sgx_status_t {
let pool_1_slice = unsafe { slice::from_raw_parts(pool_one, pool_one_len) };
// let _ = io::stdout().write(pool_1_slice);

println!("Message from the enclave");
let pool_2_slice = unsafe { slice::from_raw_parts(pool_two, pool_two_len) };
// let _ = io::stdout().write(pool_2_slice);

// Test pool 1 - Import existing data pool from CosmosDB
let json_pool_1 = r#"
{
"pool": [
{
"ID":1,
"Income":109748.717064175,
"Postal":41,
"WalletID":"9173f117-eaf5-443b-a405-c3d5e2aa5d99"
},
{
"ID":2,
"Income":77356.0675380472,
"Postal":15,
"WalletID":"9f86ae7c-ac1c-4b8e-9daf-bd40ad13e157"
},
{
"ID":3,
"Income":33746.4377853607,
"Postal":11,
"WalletID":"3e77ee27-b25b-4bcc-b9b8-d7107a633ae9"
},
{
"ID":4,
"Income":37657.0518817567,
"Postal":48,
"WalletID":"67be8d2b-c31a-44b6-9cb0-6456072d3353"
},
{
"ID":5,
"Income":27353.1866838295,
"Postal":5,
"WalletID":"412326e0-716a-4233-bae1-4cd199207893"
}
]
}
"#;
let json_pool_1 = std::str::from_utf8(&pool_1_slice).unwrap();
let json_pool_2 = std::str::from_utf8(&pool_2_slice).unwrap();

// Test pool 2 - Import append pool from CosmosDB (Temp data pool)
let json_pool_2 = r#"
{
"pool": [
{
"ID":6,
"Income":71540.129423949,
"Postal":34,
"WalletID":"dabcbe30-4ed0-4c3b-b6b3-aafb85312963"
},
{
"ID":7,
"Income":7352.38903575921,
"Postal":41,
"WalletID":"fdcd0caa-48bf-446f-b00f-a90d646f55c3"
},
{
"ID":8,
"Income":110742.078725787,
"Postal":7,
"WalletID":"f4f5cd1b-c2ad-4587-905d-47237ebdbd89"
},
{
"ID":9,
"Income":129564.725166652,
"Postal":3,
"WalletID":"7e4e80c4-7e9b-4a6e-8d94-e4f72bde8233"
},
{
"ID":10,
"Income":2964.80996131516,
"Postal":33,
"WalletID":"ecefdd47-aab3-4595-87f7-cf9bbef46a53"
}
]
}
"#;
println!("Message from the enclave"); //Remove

let pool_1 = serde_json::from_str::<Value>(&json_pool_1).expect("JSON was not well-formatted");
let pool_2 = serde_json::from_str::<Value>(&json_pool_2).expect("JSON was not well-formatted");
Expand Down

0 comments on commit 86d66fe

Please sign in to comment.