diff --git a/Makefile b/Makefile index 01d28074..4c1b07b9 100644 --- a/Makefile +++ b/Makefile @@ -198,6 +198,14 @@ hal-test: cd examples/hal-test && cargo build --release cp examples/hal-test/target/wasm32-wasip1/release/hal-test.wasm build/hal-test.wasm +platform-test: + cd examples/platform-test && cargo build --release + cp examples/platform-test/target/wasm32-wasip1/release/platform-test.wasm build/platform-test.wasm + +attestation-test: + cd examples/attestation-test && cargo build --release + cp examples/attestation-test/target/wasm32-wasip1/release/attestation-test.wasm build/attestation-test.wasm + help: @echo "Usage: make " @echo "" diff --git a/embed-proplet/modules/wamr/wasm-micro-runtime b/embed-proplet/modules/wamr/wasm-micro-runtime index 46472ee6..dcf137e9 160000 --- a/embed-proplet/modules/wamr/wasm-micro-runtime +++ b/embed-proplet/modules/wamr/wasm-micro-runtime @@ -1 +1 @@ -Subproject commit 46472ee6c849382f4cb54db75b40bd9492bb6e9b +Subproject commit dcf137e961e7d84ed1fdff90bcc40ccadfc01090 diff --git a/examples/attestation-test/Cargo.lock b/examples/attestation-test/Cargo.lock new file mode 100644 index 00000000..989be15c --- /dev/null +++ b/examples/attestation-test/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "attestation-test" +version = "0.1.0" diff --git a/examples/attestation-test/Cargo.toml b/examples/attestation-test/Cargo.toml new file mode 100644 index 00000000..8b0151b8 --- /dev/null +++ b/examples/attestation-test/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "attestation-test" +version = "0.1.0" +edition = "2021" + +[[bin]] +name = "attestation-test" +path = "src/main.rs" diff --git a/examples/attestation-test/src/main.rs b/examples/attestation-test/src/main.rs new file mode 100644 index 00000000..70fadbc8 --- /dev/null +++ b/examples/attestation-test/src/main.rs @@ -0,0 +1,53 @@ +#[link(wasm_import_module = "elastic:attestation/hal")] +unsafe extern "C" { + #[link_name = "get-attestation"] + fn get_attestation( + nonce_ptr: i32, + nonce_len: i32, + out_ptr: i32, + out_len_ptr: i32, + ) -> i32; +} + +const BUF: usize = 4096; + +fn hex(bytes: &[u8]) -> String { + bytes.iter().map(|b| format!("{b:02x}")).collect() +} + +fn run() -> i32 { + let nonce: [u8; 16] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]; + let mut buf = [0u8; BUF]; + let mut len: u32 = 0; + let rc = unsafe { + get_attestation( + nonce.as_ptr() as i32, + nonce.len() as i32, + buf.as_mut_ptr() as i32, + &mut len as *mut u32 as i32, + ) + }; + if rc < 0 { + eprintln!( + "get-attestation: FAILED (rc={}) msg={}", + rc, + String::from_utf8_lossy(&buf[..len as usize]) + ); + return 1; + } + println!("get-attestation: ok (report len={})", len); + println!("get-attestation report: {}", hex(&buf[..len as usize])); + 0 +} + +#[no_mangle] +pub extern "C" fn run_export() -> i32 { + run() +} + +fn main() { + let result = run(); + if result != 0 { + eprintln!("attestation-test failed with exit code: {}", result); + } +} diff --git a/examples/platform-test/Cargo.lock b/examples/platform-test/Cargo.lock new file mode 100644 index 00000000..923b5d1d --- /dev/null +++ b/examples/platform-test/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "platform-test" +version = "0.1.0" diff --git a/examples/platform-test/Cargo.toml b/examples/platform-test/Cargo.toml new file mode 100644 index 00000000..a982071d --- /dev/null +++ b/examples/platform-test/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "platform-test" +version = "0.1.0" +edition = "2021" + +[[bin]] +name = "platform-test" +path = "src/main.rs" diff --git a/examples/platform-test/src/main.rs b/examples/platform-test/src/main.rs new file mode 100644 index 00000000..7403ea0a --- /dev/null +++ b/examples/platform-test/src/main.rs @@ -0,0 +1,77 @@ +#[link(wasm_import_module = "elastic:tee-hal/platform")] +unsafe extern "C" { + #[link_name = "platform-info"] + fn get_platform_info(out_ptr: i32, out_len_ptr: i32) -> i32; + + fn attestation( + report_data_ptr: i32, + report_data_len: i32, + out_ptr: i32, + out_len_ptr: i32, + ) -> i32; +} + +const BUF: usize = 4096; + +fn hex(bytes: &[u8]) -> String { + bytes.iter().map(|b| format!("{b:02x}")).collect() +} + +fn run() -> i32 { + let mut ok = true; + + { + let mut buf = [0u8; BUF]; + let mut len: u32 = 0; + let rc = + unsafe { get_platform_info(buf.as_mut_ptr() as i32, &mut len as *mut u32 as i32) }; + if rc < 0 { + eprintln!("get-platform-info: FAILED (rc={})", rc); + ok = false; + } else { + println!( + "get-platform-info: {}", + String::from_utf8_lossy(&buf[..len as usize]) + ); + } + } + + { + let report_data = [0x42u8; 32]; + let mut buf = [0u8; BUF]; + let mut len: u32 = 0; + let rc = unsafe { + attestation( + report_data.as_ptr() as i32, + report_data.len() as i32, + buf.as_mut_ptr() as i32, + &mut len as *mut u32 as i32, + ) + }; + if rc < 0 { + eprintln!( + "attestation: FAILED (rc={}) msg={}", + rc, + String::from_utf8_lossy(&buf[..len as usize]) + ); + ok = false; + } else { + println!("attestation: ok (evidence len={})", len); + println!("attestation evidence: {}", hex(&buf[..len as usize])); + } + } + + if ok { 0 } else { 1 } +} + +#[no_mangle] +pub extern "C" fn run_export() -> i32 { + run() +} + +fn main() { + let result = run(); + if result != 0 { + eprintln!("platform-test failed with exit code: {}", result); + } +}