Skip to content
This repository has been archived by the owner on Oct 26, 2021. It is now read-only.

Implement SGX measure() #324

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ backend-sgx = ["sgx"]

[dependencies]
sev = { git = "https://github.com/enarx/sev", features = ["openssl"], optional = true }
sgx = { git = "https://github.com/enarx/sgx", rev = "e805b51", features = ["asm", "crypto"], optional = true }
sgx = { git = "https://github.com/lkatalin/sgx", branch = "hasher", features = ["asm", "crypto"], optional = true }
koine = { git = "https://github.com/enarx/koine", optional = true }
x86_64 = { version = "0.11", default-features = false, features = ["stable"], optional = true }
kvm-bindings = { version = "0.3", optional = true }
Expand Down
82 changes: 82 additions & 0 deletions src/backend/sgx/builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// SPDX-License-Identifier: Apache-2.0

use crate::binary::Component;

use anyhow::Result;
use lset::Span;
use primordial::Page;
use sgx::enclave::{Builder, Segment};
use sgx::types::{
page::{Flags, SecInfo},
tcs::Tcs,
};

/// Creates and loads an enclave, then returns the Builder.
pub fn builder(mut shim: Component, mut code: Component) -> Result<Builder> {
// Calculate the memory layout for the enclave.
let layout = crate::backend::sgx::shim::Layout::calculate(shim.region(), code.region());

// Relocate the shim binary.
shim.entry += layout.shim.start;
for seg in shim.segments.iter_mut() {
seg.dst += layout.shim.start;
}

// Relocate the code binary.
code.entry += layout.code.start;
for seg in code.segments.iter_mut() {
seg.dst += layout.code.start;
}

// Create SSAs and TCS.
let ssas = vec![Page::default(); 2];
let tcs = Tcs::new(
shim.entry - layout.enclave.start,
Page::size() * 2, // SSAs after Layout (see below)
ssas.len() as _,
);

let internal = vec![
// TCS
Segment {
si: SecInfo::tcs(),
dst: layout.prefix.start,
src: vec![Page::copy(tcs)],
},
// Layout
Segment {
si: SecInfo::reg(Flags::R),
dst: layout.prefix.start + Page::size(),
src: vec![Page::copy(layout)],
},
// SSAs
Segment {
si: SecInfo::reg(Flags::R | Flags::W),
dst: layout.prefix.start + Page::size() * 2,
src: ssas,
},
// Heap
Segment {
si: SecInfo::reg(Flags::R | Flags::W | Flags::X),
dst: layout.heap.start,
src: vec![Page::default(); Span::from(layout.heap).count / Page::size()],
},
// Stack
Segment {
si: SecInfo::reg(Flags::R | Flags::W),
dst: layout.stack.start,
src: vec![Page::default(); Span::from(layout.stack).count / Page::size()],
},
];

let shim_segs: Vec<_> = shim.segments.into_iter().map(Segment::from).collect();
let code_segs: Vec<_> = code.segments.into_iter().map(Segment::from).collect();

// Initiate the enclave building process.
let mut builder = Builder::new(layout.enclave).expect("Unable to create builder");
builder.load(&internal)?;
builder.load(&shim_segs)?;
builder.load(&code_segs)?;

Ok(builder)
}
95 changes: 22 additions & 73 deletions src/backend/sgx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,22 @@ use crate::sallyport;
use crate::syscall::{SYS_ENARX_CPUID, SYS_ENARX_ERESUME, SYS_ENARX_GETATT};

use anyhow::{anyhow, Result};
use lset::Span;
use primordial::Page;
use sgx::enclave::{Builder, Enclave, Entry, Registers, Segment};
use sgx::enclave::{Enclave, Entry, Registers, Segment};
use sgx::types::{
page::{Flags, SecInfo},
sig::{Author, Parameters},
ssa::Exception,
tcs::Tcs,
};

use std::arch::x86_64::__cpuid_count;
use std::convert::TryInto;
use std::path::Path;
use std::sync::{Arc, RwLock};

use openssl::{bn, rsa};

mod attestation;
mod builder;
mod data;
mod shim;

Expand Down Expand Up @@ -73,78 +74,26 @@ impl crate::backend::Backend for Backend {
}

/// Create a keep instance on this backend
fn build(&self, mut code: Component, _sock: Option<&Path>) -> Result<Arc<dyn Keep>> {
let mut shim = Component::from_bytes(SHIM)?;

// Calculate the memory layout for the enclave.
let layout = crate::backend::sgx::shim::Layout::calculate(shim.region(), code.region());

// Relocate the shim binary.
shim.entry += layout.shim.start;
for seg in shim.segments.iter_mut() {
seg.dst += layout.shim.start;
}

// Relocate the code binary.
code.entry += layout.code.start;
for seg in code.segments.iter_mut() {
seg.dst += layout.code.start;
}

// Create SSAs and TCS.
let ssas = vec![Page::default(); 2];
let tcs = Tcs::new(
shim.entry - layout.enclave.start,
Page::size() * 2, // SSAs after Layout (see below)
ssas.len() as _,
);

let internal = vec![
// TCS
Segment {
si: SecInfo::tcs(),
dst: layout.prefix.start,
src: vec![Page::copy(tcs)],
},
// Layout
Segment {
si: SecInfo::reg(Flags::R),
dst: layout.prefix.start + Page::size(),
src: vec![Page::copy(layout)],
},
// SSAs
Segment {
si: SecInfo::reg(Flags::R | Flags::W),
dst: layout.prefix.start + Page::size() * 2,
src: ssas,
},
// Heap
Segment {
si: SecInfo::reg(Flags::R | Flags::W | Flags::X),
dst: layout.heap.start,
src: vec![Page::default(); Span::from(layout.heap).count / Page::size()],
},
// Stack
Segment {
si: SecInfo::reg(Flags::R | Flags::W),
dst: layout.stack.start,
src: vec![Page::default(); Span::from(layout.stack).count / Page::size()],
},
];

let shim_segs: Vec<_> = shim.segments.into_iter().map(Segment::from).collect();
let code_segs: Vec<_> = code.segments.into_iter().map(Segment::from).collect();

// Initiate the enclave building process.
let mut builder = Builder::new(layout.enclave).expect("Unable to create builder");
builder.load(&internal)?;
builder.load(&shim_segs)?;
builder.load(&code_segs)?;
fn build(&self, code: Component, _sock: Option<&Path>) -> Result<Arc<dyn Keep>> {
let shim = Component::from_bytes(SHIM)?;
let builder = builder::builder(shim, code)?;
Ok(builder.build()?)
}

fn measure(&self, mut _code: Component) -> Result<String> {
unimplemented!()
fn measure(&self, code: Component) -> Result<String> {
let shim = Component::from_bytes(SHIM)?;

let builder = builder::builder(shim, code)?;

// Use Builder's hasher to get enclave measurement.
let hasher = builder.hasher();
let vendor = Author::new(0, 0);
let exp = bn::BigNum::from_u32(3u32)?;
let key = rsa::Rsa::generate_with_e(3072, &exp)?;
let sig = hasher.finish(Parameters::default()).sign(vendor, key)?;
let mrenclave = sig.measurement().mrenclave();
let json = format!(r#"{{ "backend": "sgx", "mrenclave": {:?} }}"#, mrenclave);
Ok(json)
}
}

Expand Down