Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions rust/edlcodegen/crates/samples/helloworld/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ windows = "0.62.2"

# TODO: once edlcodegen-tools is published we should be able to use the version in crates.io
edlcodegen-tools = { git = "https://github.com/microsoft/VbsEnclaveTooling" }
tracelogging = { version = "1.2.4", default-features = false }
vbsenclave-sdk-enclave = { path = "../../../../sdk/crates/libs/sdk-enclave" }
vbsenclave-sdk-host = { path = "../../../../sdk/crates/libs/sdk-host" }
windows-enclave = { path = "../../../../sdk/crates/libs/windows-enclave" }
3 changes: 3 additions & 0 deletions rust/edlcodegen/crates/samples/helloworld/enclave/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ edlcodegen-tools = { workspace = true }
[dependencies]
# Note: To generate crate run rust/crates/samples/helloworld/generate_edl_crates.ps1 at least once
test_enclave_gen = {path = "generated/test_gen" }
tracelogging = { workspace = true, features = ["macros", "etw", "kernel_mode"]}
vbsenclave-sdk-enclave = { workspace = true }
windows-enclave = { workspace = true }
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,20 @@ use test_enclave_gen::implementation::trusted::Trusted;
use test_enclave_gen::AbiError;
use test_enclave_gen::stubs::untrusted::print;
use alloc::string::ToString;
use tracelogging as tlg;
use crate::{HELLO_WORLD_PROVIDER};

pub struct EnclaveImpl{}

impl Trusted for EnclaveImpl {
fn do_secret_math(val1: u32, val2: u32) -> Result<u32, AbiError> {
tlg::write_event!(
HELLO_WORLD_PROVIDER, // The provider to use for the event.
"do_secret_math_invoked", // Event category bits.
str8("Field1", "do_secret_math invoked!"), // Add a string field to the event.
);

// Print message to console from within the enclave.
print(&"Performing secret math operation inside VTL1...".to_string())?;
Ok(val1 + val2)
}
Expand Down

This file was deleted.

27 changes: 22 additions & 5 deletions rust/edlcodegen/crates/samples/helloworld/enclave/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
#![no_std]
extern crate alloc;
mod edl_impls;
mod enclave_constants;
use enclave_constants::*;
use core::panic::PanicInfo;
use core::panic::{ PanicInfo};
use tracelogging as tlg;
use windows_enclave::vertdll::{
IMAGE_ENCLAVE_FLAG_PRIMARY_IMAGE, ImageEnclaveConfig, IMAGE_ENCLAVE_MINIMUM_CONFIG_SIZE
};

// Developer creates their own panic handler for no_std environments.
// Note: The rust analyzer in VSCode shows red squiggly lines in the IDE.
Expand All @@ -23,10 +25,14 @@ fn panic(_panic: &PanicInfo<'_>) -> ! {
// test_enclave_gen crate.
test_enclave_gen::export_enclave_functions!(edl_impls::EnclaveImpl);

// Export the SDK enclave functions using the macro from the
// vbsenclave_sdk_enclave crate.
vbsenclave_sdk_enclave::export_sdk_enclave_functions!();

pub const ENCLAVE_CONFIG_POLICY_FLAGS: u32 = {
// Enable debuggability of the enclave in debug builds.
#[cfg(debug_assertions)]
{ IMAGE_ENCLAVE_POLICY_DEBUGGABLE }
{ windows_enclave::vertdll::IMAGE_ENCLAVE_POLICY_DEBUGGABLE }

// Disable debuggability in release builds.
#[cfg(not(debug_assertions))]
Expand Down Expand Up @@ -55,11 +61,22 @@ pub static __enclave_config: ImageEnclaveConfig = ImageEnclaveConfig {
enclave_flags: IMAGE_ENCLAVE_FLAG_PRIMARY_IMAGE,
};

tlg::define_provider!(
HELLO_WORLD_PROVIDER,
"HelloWorldProvider",
);

#[unsafe(no_mangle)]
#[allow(non_snake_case)]
pub extern "system" fn DllMain(
_instance: *const core::ffi::c_void,
_reason: u32,
reason: u32,
_reserved: *mut core::ffi::c_void,) -> bool {

// DLL_PROCESS_ATTACH
if reason == 1 {
vbsenclave_sdk_enclave::etw::add_provider(&HELLO_WORLD_PROVIDER);
}

true
}
2 changes: 2 additions & 0 deletions rust/edlcodegen/crates/samples/helloworld/host/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ windows = { workspace = true, features = ["Win32_System_Threading"] }

# Note: To generate crate run rust/crates/samples/helloworld/generate_edl_crates.ps1 at least once
test_host_gen = {path = "generated/test_gen" }
tracelogging = { workspace = true, features = ["macros", "etw"]}
vbsenclave-sdk-host = { workspace = true }
129 changes: 0 additions & 129 deletions rust/edlcodegen/crates/samples/helloworld/host/src/container.rs

This file was deleted.

This file was deleted.

34 changes: 26 additions & 8 deletions rust/edlcodegen/crates/samples/helloworld/host/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,43 @@

use test_host_gen::stubs::trusted::TestVtl0Host;
mod edl_impls;
mod enclave_api_ffi;
mod container;
use crate::container::EnclaveContainer;
use crate::edl_impls::HostImpl;
use vbsenclave_sdk_host::enclave::EnclaveHandle;
use vbsenclave_sdk_host::enclave;

const ENCLAVE_CREATE_INFO_FLAG: u32 = {
#[cfg(debug_assertions)]
{ vbsenclave_sdk_host::enclave::ENCLAVE_CREATE_INFO_FLAG_DEBUG }

#[cfg(not(debug_assertions))]
{ vbsenclave_sdk_host::enclave::ENCLAVE_CREATE_INFO_FLAG_RELEASE }
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Starting VBS Enclave HelloWorld Sample...");

let enclave_container = EnclaveContainer::new()?;
let test_host = TestVtl0Host::new(enclave_container.enclave);
let enclave_handle = EnclaveHandle::create_and_initialize(
"enclave.dll",
enclave::megabytes(256),
None,
ENCLAVE_CREATE_INFO_FLAG,
)?;

let test_host = TestVtl0Host::new(enclave_handle.as_ptr());

// Register the untrusted function implementations for the test enclave.
if let Err(err) = test_host.register_vtl0_callbacks::<HostImpl>() {
panic!("Failed to register VTL0 callbacks: HRESULT: {:x}", err.to_hresult().0);
return Err(format!("Failed to register VTL0 callbacks: HRESULT: {:x}", err.to_hresult().0).into());
}

// Register SDK VTL0 callbacks to enable SDK features in the enclave.
if let Err(err) = vbsenclave_sdk_host::register_sdk_callbacks(enclave_handle.as_ptr()) {
return Err(format!("Failed to register SDK VTL0 callbacks: HRESULT: {:x}", err.to_hresult().0).into());
}

let secret_result = test_host.do_secret_math(42, 58);
if let Err(err) = secret_result {
panic!("do_secret_math failed: HRESULT {:x}", err.to_hresult().0);

return Err(format!("do_secret_math failed: HRESULT {:x}", err.to_hresult().0).into());
}

let secret_value = secret_result.unwrap();
Expand Down
10 changes: 10 additions & 0 deletions rust/scripts/generate_codegen_crates.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ $ErrorActionPreference = "Stop"
--OutputDirectory $EnclaveOutDir `
--ImportDirectories $ImportDirectories

if ($LASTEXITCODE -ne 0) {
# The exe prints out the error code and text on failure.
throw "EdlCodegen failed to generate the enclave crate"
}

# Run codegen for the host crate
& $edlCodeGenToolsPath `
--namespace $Namespace `
Expand All @@ -41,3 +46,8 @@ $ErrorActionPreference = "Stop"
--Vtl0ClassName $Vtl0ClassName `
--OutputDirectory $HostAppOutDir `
--ImportDirectories $ImportDirectories

if ($LASTEXITCODE -ne 0) {
# The exe prints out the error code and text on failure.
throw "EdlCodegen failed to generate the host crate"
}
2 changes: 2 additions & 0 deletions rust/sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ edition = "2024"
edlcodegen-tools = { git = "https://github.com/microsoft/VbsEnclaveTooling" }
regex = "1.11"
spin = "0.10.0"
tracelogging = { version ="1.2.4", default-features = false }
userboundkey-kcm = { version = "0.1.0", path = "crates/libs/userboundkey-kcm" }
uuid = {version = "1.19.0", default-features = false }
vbsenclave-sdk-enclave = { version = "0.1.0", path = "crates/libs/sdk-enclave" }
vbsenclave-sdk-host = { version = "0.1.0", path = "crates/libs/sdk-host" }
widestring = { version = "1.1", default-features = false, features = ["alloc"] }
Expand Down
Loading