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

Commit

Permalink
Implement SGX measure()
Browse files Browse the repository at this point in the history
Signed-off-by: Lily Sturmann <[email protected]>
  • Loading branch information
lkatalin committed Jan 31, 2021
1 parent 2a2847f commit 0a5bad4
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 75 deletions.
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

0 comments on commit 0a5bad4

Please sign in to comment.