Skip to content

Commit 86d66fe

Browse files
committed
feat: Import data into the enclave
1 parent eebad0c commit 86d66fe

File tree

9 files changed

+145
-105
lines changed

9 files changed

+145
-105
lines changed

rust-sgx-workspace/projects/ntc-tee-server/app/Enclave_u.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,7 @@ static const struct {
933933
(void*)Enclave_u_fstatat64_ocall,
934934
}
935935
};
936-
sgx_status_t append_data(sgx_enclave_id_t eid, sgx_status_t* retval, const uint8_t* some_string, size_t len)
936+
sgx_status_t append_data(sgx_enclave_id_t eid, sgx_status_t* retval, const uint8_t* pool_one, size_t len)
937937
{
938938
sgx_status_t status;
939939
ms_append_data_t ms;

rust-sgx-workspace/projects/ntc-tee-server/app/codegen/Enclave_u.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33

44
typedef struct ms_append_data_t {
55
sgx_status_t ms_retval;
6-
const uint8_t* ms_some_string;
6+
const uint8_t* ms_pool_one;
77
size_t ms_len;
8+
const uint8_t* ms_pool_two;
9+
size_t ms_len_two;
810
} ms_append_data_t;
911

1012
typedef struct ms_t_global_init_ecall_t {
@@ -933,12 +935,14 @@ static const struct {
933935
(void*)Enclave_u_fstatat64_ocall,
934936
}
935937
};
936-
sgx_status_t append_data(sgx_enclave_id_t eid, sgx_status_t* retval, const uint8_t* some_string, size_t len)
938+
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)
937939
{
938940
sgx_status_t status;
939941
ms_append_data_t ms;
940-
ms.ms_some_string = some_string;
942+
ms.ms_pool_one = pool_one;
941943
ms.ms_len = len;
944+
ms.ms_pool_two = pool_two;
945+
ms.ms_len_two = len_two;
942946
status = sgx_ecall(eid, 0, &ocall_table_Enclave, &ms);
943947
if (status == SGX_SUCCESS && retval) *retval = ms.ms_retval;
944948
return status;

rust-sgx-workspace/projects/ntc-tee-server/app/codegen/Enclave_u.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ int SGX_UBRIDGE(SGX_NOCONVENTION, u_dirfd_ocall, (int* error, void* dirp));
246246
int SGX_UBRIDGE(SGX_NOCONVENTION, u_fstatat64_ocall, (int* error, int dirfd, const char* pathname, struct stat64_t* buf, int flags));
247247
#endif
248248

249-
sgx_status_t append_data(sgx_enclave_id_t eid, sgx_status_t* retval, const uint8_t* some_string, size_t len);
249+
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);
250250
sgx_status_t t_global_init_ecall(sgx_enclave_id_t eid, uint64_t id, const uint8_t* path, size_t len);
251251
sgx_status_t t_global_exit_ecall(sgx_enclave_id_t eid);
252252

rust-sgx-workspace/projects/ntc-tee-server/app/codegen/Enclave_u.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ extern "C" {
66
pub fn append_data(
77
eid: sgx_enclave_id_t,
88
retval: *mut sgx_status_t,
9-
some_string: *const u8,
9+
pool_one: *const u8,
1010
len: size_t,
11+
pool_two: *const u8,
12+
len_two: size_t,
1113
) -> sgx_status_t;
1214
}

rust-sgx-workspace/projects/ntc-tee-server/app/src/main.rs

Lines changed: 83 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ extern "C" {
1212
fn append_data(
1313
eid: sgx_enclave_id_t,
1414
retval: *mut sgx_status_t,
15-
input_string: *const u8,
16-
input_length: usize,
15+
pool_one: *const u8,
16+
pool_one_length: usize,
17+
pool_two: *const u8,
18+
pool_two_length: usize,
1719
) -> sgx_status_t;
1820

1921
}
@@ -50,16 +52,92 @@ fn main() {
5052
};
5153

5254
// Update - send sealed binary data into enclave (from CosmosDB)
53-
let input_string = String::from("Sending this string to the enclave then printing it\n");
55+
// Test pool 1 - Import existing data pool from CosmosDB
56+
let pool_one = r#"
57+
{
58+
"pool": [
59+
{
60+
"ID":1,
61+
"Income":109748.717064175,
62+
"Postal":41,
63+
"WalletID":"9173f117-eaf5-443b-a405-c3d5e2aa5d99"
64+
},
65+
{
66+
"ID":2,
67+
"Income":77356.0675380472,
68+
"Postal":15,
69+
"WalletID":"9f86ae7c-ac1c-4b8e-9daf-bd40ad13e157"
70+
},
71+
{
72+
"ID":3,
73+
"Income":33746.4377853607,
74+
"Postal":11,
75+
"WalletID":"3e77ee27-b25b-4bcc-b9b8-d7107a633ae9"
76+
},
77+
{
78+
"ID":4,
79+
"Income":37657.0518817567,
80+
"Postal":48,
81+
"WalletID":"67be8d2b-c31a-44b6-9cb0-6456072d3353"
82+
},
83+
{
84+
"ID":5,
85+
"Income":27353.1866838295,
86+
"Postal":5,
87+
"WalletID":"412326e0-716a-4233-bae1-4cd199207893"
88+
}
89+
]
90+
}
91+
"#;
92+
93+
// Test pool 2 - Import append pool from CosmosDB (Temp data pool)
94+
let pool_two = r#"
95+
{
96+
"pool": [
97+
{
98+
"ID":6,
99+
"Income":71540.129423949,
100+
"Postal":34,
101+
"WalletID":"dabcbe30-4ed0-4c3b-b6b3-aafb85312963"
102+
},
103+
{
104+
"ID":7,
105+
"Income":7352.38903575921,
106+
"Postal":41,
107+
"WalletID":"fdcd0caa-48bf-446f-b00f-a90d646f55c3"
108+
},
109+
{
110+
"ID":8,
111+
"Income":110742.078725787,
112+
"Postal":7,
113+
"WalletID":"f4f5cd1b-c2ad-4587-905d-47237ebdbd89"
114+
},
115+
{
116+
"ID":9,
117+
"Income":129564.725166652,
118+
"Postal":3,
119+
"WalletID":"7e4e80c4-7e9b-4a6e-8d94-e4f72bde8233"
120+
},
121+
{
122+
"ID":10,
123+
"Income":2964.80996131516,
124+
"Postal":33,
125+
"WalletID":"ecefdd47-aab3-4595-87f7-cf9bbef46a53"
126+
}
127+
]
128+
}
129+
"#;
54130

55131
let mut retval = sgx_status_t::SGX_SUCCESS;
56132

57133
let result = unsafe {
58134
append_data(
59135
enclave.geteid(),
60136
&mut retval,
61-
input_string.as_ptr() as *const u8,
62-
input_string.len(),
137+
pool_one.as_ptr() as *const u8,
138+
pool_one.len(),
139+
pool_two.as_ptr() as *const u8,
140+
pool_two.len(),
63141
)
64142
};
65143

rust-sgx-workspace/projects/ntc-tee-server/enclave/Enclave.edl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ enclave {
33
from "sgx_backtrace.edl" import *;
44
trusted
55
{
6-
public sgx_status_t append_data([in, size=len] const uint8_t* some_string, size_t len);
6+
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);
77
};
88
untrusted
99
{

rust-sgx-workspace/projects/ntc-tee-server/enclave/codegen/Enclave_t.c

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@
2929

3030
typedef struct ms_append_data_t {
3131
sgx_status_t ms_retval;
32-
const uint8_t* ms_some_string;
32+
const uint8_t* ms_pool_one;
3333
size_t ms_len;
34+
const uint8_t* ms_pool_two;
35+
size_t ms_len_two;
3436
} ms_append_data_t;
3537

3638
typedef struct ms_t_global_init_ecall_t {
@@ -456,41 +458,65 @@ static sgx_status_t SGX_CDECL sgx_append_data(void* pms)
456458
sgx_lfence();
457459
ms_append_data_t* ms = SGX_CAST(ms_append_data_t*, pms);
458460
sgx_status_t status = SGX_SUCCESS;
459-
const uint8_t* _tmp_some_string = ms->ms_some_string;
461+
const uint8_t* _tmp_pool_one = ms->ms_pool_one;
460462
size_t _tmp_len = ms->ms_len;
461-
size_t _len_some_string = _tmp_len;
462-
uint8_t* _in_some_string = NULL;
463+
size_t _len_pool_one = _tmp_len;
464+
uint8_t* _in_pool_one = NULL;
465+
const uint8_t* _tmp_pool_two = ms->ms_pool_two;
466+
size_t _tmp_len_two = ms->ms_len_two;
467+
size_t _len_pool_two = _tmp_len_two;
468+
uint8_t* _in_pool_two = NULL;
463469

464-
CHECK_UNIQUE_POINTER(_tmp_some_string, _len_some_string);
470+
CHECK_UNIQUE_POINTER(_tmp_pool_one, _len_pool_one);
471+
CHECK_UNIQUE_POINTER(_tmp_pool_two, _len_pool_two);
465472

466473
//
467474
// fence after pointer checks
468475
//
469476
sgx_lfence();
470477

471-
if (_tmp_some_string != NULL && _len_some_string != 0) {
472-
if ( _len_some_string % sizeof(*_tmp_some_string) != 0)
478+
if (_tmp_pool_one != NULL && _len_pool_one != 0) {
479+
if ( _len_pool_one % sizeof(*_tmp_pool_one) != 0)
473480
{
474481
status = SGX_ERROR_INVALID_PARAMETER;
475482
goto err;
476483
}
477-
_in_some_string = (uint8_t*)malloc(_len_some_string);
478-
if (_in_some_string == NULL) {
484+
_in_pool_one = (uint8_t*)malloc(_len_pool_one);
485+
if (_in_pool_one == NULL) {
479486
status = SGX_ERROR_OUT_OF_MEMORY;
480487
goto err;
481488
}
482489

483-
if (memcpy_s(_in_some_string, _len_some_string, _tmp_some_string, _len_some_string)) {
490+
if (memcpy_s(_in_pool_one, _len_pool_one, _tmp_pool_one, _len_pool_one)) {
491+
status = SGX_ERROR_UNEXPECTED;
492+
goto err;
493+
}
494+
495+
}
496+
if (_tmp_pool_two != NULL && _len_pool_two != 0) {
497+
if ( _len_pool_two % sizeof(*_tmp_pool_two) != 0)
498+
{
499+
status = SGX_ERROR_INVALID_PARAMETER;
500+
goto err;
501+
}
502+
_in_pool_two = (uint8_t*)malloc(_len_pool_two);
503+
if (_in_pool_two == NULL) {
504+
status = SGX_ERROR_OUT_OF_MEMORY;
505+
goto err;
506+
}
507+
508+
if (memcpy_s(_in_pool_two, _len_pool_two, _tmp_pool_two, _len_pool_two)) {
484509
status = SGX_ERROR_UNEXPECTED;
485510
goto err;
486511
}
487512

488513
}
489514

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

492517
err:
493-
if (_in_some_string) free(_in_some_string);
518+
if (_in_pool_one) free(_in_pool_one);
519+
if (_in_pool_two) free(_in_pool_two);
494520
return status;
495521
}
496522

rust-sgx-workspace/projects/ntc-tee-server/enclave/codegen/Enclave_t.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
extern "C" {
2121
#endif
2222

23-
sgx_status_t append_data(const uint8_t* some_string, size_t len);
23+
sgx_status_t append_data(const uint8_t* pool_one, size_t len, const uint8_t* pool_two, size_t len_two);
2424
void t_global_init_ecall(uint64_t id, const uint8_t* path, size_t len);
2525
void t_global_exit_ecall(void);
2626

rust-sgx-workspace/projects/ntc-tee-server/enclave/src/lib.rs

Lines changed: 10 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ extern crate sgx_types;
66
extern crate sgx_tstd as std;
77
extern crate serde_json;
88

9-
use std::io::{self, Write};
109
use std::slice;
1110
use std::string::String;
1211
use std::vec::Vec;
@@ -15,89 +14,20 @@ use serde_json::Value;
1514
use sgx_types::sgx_status_t;
1615

1716
/// # Safety
18-
/// Caller needs to ensure that `some_string` points to a valid slice of length `some_len`
17+
/// Caller needs to ensure that `pool_one` points to a valid slice of length `pool_one_len`
18+
/// Caller needs to ensure that `pool_two` points to a valid slice of length `pool_two_len`
1919
#[no_mangle]
20-
pub unsafe extern "C" fn append_data(some_string: *const u8, some_len: usize) -> sgx_status_t {
21-
let str_slice = unsafe { slice::from_raw_parts(some_string, some_len) };
22-
let _ = io::stdout().write(str_slice);
20+
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 {
21+
let pool_1_slice = unsafe { slice::from_raw_parts(pool_one, pool_one_len) };
22+
// let _ = io::stdout().write(pool_1_slice);
2323

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

26-
// Test pool 1 - Import existing data pool from CosmosDB
27-
let json_pool_1 = r#"
28-
{
29-
"pool": [
30-
{
31-
"ID":1,
32-
"Income":109748.717064175,
33-
"Postal":41,
34-
"WalletID":"9173f117-eaf5-443b-a405-c3d5e2aa5d99"
35-
},
36-
{
37-
"ID":2,
38-
"Income":77356.0675380472,
39-
"Postal":15,
40-
"WalletID":"9f86ae7c-ac1c-4b8e-9daf-bd40ad13e157"
41-
},
42-
{
43-
"ID":3,
44-
"Income":33746.4377853607,
45-
"Postal":11,
46-
"WalletID":"3e77ee27-b25b-4bcc-b9b8-d7107a633ae9"
47-
},
48-
{
49-
"ID":4,
50-
"Income":37657.0518817567,
51-
"Postal":48,
52-
"WalletID":"67be8d2b-c31a-44b6-9cb0-6456072d3353"
53-
},
54-
{
55-
"ID":5,
56-
"Income":27353.1866838295,
57-
"Postal":5,
58-
"WalletID":"412326e0-716a-4233-bae1-4cd199207893"
59-
}
60-
]
61-
}
62-
"#;
27+
let json_pool_1 = std::str::from_utf8(&pool_1_slice).unwrap();
28+
let json_pool_2 = std::str::from_utf8(&pool_2_slice).unwrap();
6329

64-
// Test pool 2 - Import append pool from CosmosDB (Temp data pool)
65-
let json_pool_2 = r#"
66-
{
67-
"pool": [
68-
{
69-
"ID":6,
70-
"Income":71540.129423949,
71-
"Postal":34,
72-
"WalletID":"dabcbe30-4ed0-4c3b-b6b3-aafb85312963"
73-
},
74-
{
75-
"ID":7,
76-
"Income":7352.38903575921,
77-
"Postal":41,
78-
"WalletID":"fdcd0caa-48bf-446f-b00f-a90d646f55c3"
79-
},
80-
{
81-
"ID":8,
82-
"Income":110742.078725787,
83-
"Postal":7,
84-
"WalletID":"f4f5cd1b-c2ad-4587-905d-47237ebdbd89"
85-
},
86-
{
87-
"ID":9,
88-
"Income":129564.725166652,
89-
"Postal":3,
90-
"WalletID":"7e4e80c4-7e9b-4a6e-8d94-e4f72bde8233"
91-
},
92-
{
93-
"ID":10,
94-
"Income":2964.80996131516,
95-
"Postal":33,
96-
"WalletID":"ecefdd47-aab3-4595-87f7-cf9bbef46a53"
97-
}
98-
]
99-
}
100-
"#;
30+
println!("Message from the enclave"); //Remove
10131

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

0 commit comments

Comments
 (0)