Skip to content
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 41 additions & 32 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ rustls = "0.22.4"
webpki-roots = "0.26.6"
reqwest = "0.12.9"
thiserror = "2.0.12"
clap = "4.5"

untrusted-host = { path = "crates/untrusted-host" }

Expand Down
7 changes: 6 additions & 1 deletion bin/zktls-pairs/sgx/enclave.edl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ enclave {
from "sgx_dcap.edl" import *;

trusted {
public sgx_status_t trusted_execution();
public sgx_status_t trusted_execution(
[in, size=file_path_len] const uint8_t* file_path,
size_t file_path_len
);
};

untrusted {
Expand All @@ -31,6 +34,8 @@ enclave {
);

void ocall_read_from_file(
[in, size=filename_len] const uint8_t* filename_bytes,
size_t filename_len,
[out, size=pairs_list_buffer_len] char* pairs_list_buffer,
size_t pairs_list_buffer_len,
[out] size_t* pairs_list_actual_len
Expand Down
24 changes: 22 additions & 2 deletions bin/zktls-pairs/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
extern crate mock_lib;

use automata_sgx_sdk::types::SgxStatus;
use clap::{Arg, Command};

automata_sgx_sdk::enclave! {
name: Enclave,
ecall: {
fn trusted_execution() -> SgxStatus;
fn trusted_execution(file_path_ptr: *const u8, file_path_len: u32) -> SgxStatus;
}
}

fn main() -> anyhow::Result<()> {
let matches = Command::new("")
.arg(
Arg::new("pairs_file_path")
.long("pairs_file_path")
.required(true)
.help("Path to the file with pairs")
.num_args(1),
)
.get_matches();

let pairs_file_path = matches
.get_one::<String>("pairs_file_path")
.ok_or(anyhow::anyhow!("Required parameter not found"))?;

println!("Path to the pairs file: {}", pairs_file_path);

let mut path_bytes = pairs_file_path.clone().into_bytes();
path_bytes.push(0);

let result = Enclave::new()
.trusted_execution()
.trusted_execution(path_bytes.as_ptr(), path_bytes.len() as u32)
.map_err(|e| anyhow::anyhow!("{:?}", e))?;
if !result.is_success() {
println!("{:?}", result);
Expand Down
9 changes: 8 additions & 1 deletion crates/enclave/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ extern "C" {
);

fn ocall_read_from_file(
filename_bytes: *const u8,
filename_len: usize,
pairs_list_buffer: *mut u8,
pairs_list_buffer_len: usize,
pairs_list_actual_len: *mut usize,
Expand All @@ -42,7 +44,10 @@ pub(crate) const BINANCE_API_HOST: &str = "data-api.binance.vision";
pub(crate) const HARDCODED_DECIMALS: u32 = 8;

#[no_mangle]
pub unsafe extern "C" fn trusted_execution() -> SgxStatus {
pub unsafe extern "C" fn trusted_execution(
file_path_ptr: *const u8,
file_path_len: usize,
) -> SgxStatus {
let env_filter = tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info"));

Expand All @@ -56,6 +61,8 @@ pub unsafe extern "C" fn trusted_execution() -> SgxStatus {
let mut pairs_list_actual_len: usize = 0;

ocall_read_from_file(
file_path_ptr,
file_path_len,
pairs_list_buffer.as_mut_ptr(),
pairs_list_buffer.len(),
&mut pairs_list_actual_len as *mut usize,
Expand Down
14 changes: 12 additions & 2 deletions crates/untrusted-host/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ extern crate core;

use core::slice;
use std::{
borrow::Cow,
ffi::CStr,
fs,
io::{Read, Write},
Expand Down Expand Up @@ -123,20 +124,29 @@ pub unsafe fn ocall_write_to_file(

#[no_mangle]
pub unsafe fn ocall_read_from_file(
filename_bytes: *const u8,
filename_len: usize,
pairs_list_buffer: *mut u8,
pairs_list_buffer_len: usize,
pairs_list_actual_len: *mut usize,
) {
tracing::debug!("=============== Untrusted read_from_file =================");

let pairs_list_path = "pairs/list.txt";
let filename: Cow<str> = if !filename_bytes.is_null() && filename_len > 0 {
let slice = unsafe { slice::from_raw_parts(filename_bytes, filename_len) };
String::from_utf8_lossy(slice)
} else {
Cow::Borrowed("pairs/list.txt")
};

let pairs_list = fs::read(pairs_list_path).expect("Unable to read file");
tracing::info!("Reading from file: {}", filename);
let pairs_list = fs::read(filename.as_ref()).expect("Unable to read file");

assert!(
pairs_list.len() <= pairs_list_buffer_len,
"pairs list does not fit into pairs_list_buffer!"
);

ptr::copy_nonoverlapping(pairs_list.as_ptr(), pairs_list_buffer, pairs_list.len());
*pairs_list_actual_len = pairs_list.len();

Expand Down