cycles-quartz/core/quartz/src/attestor.rs

63 lines
1.5 KiB
Rust
Raw Normal View History

2024-02-26 10:56:55 +00:00
use std::{
fs::{read, File},
io::{Error as IoError, Write},
};
2024-02-27 22:30:36 +00:00
use quartz_cw::{
msg::execute::attested::HasUserData,
state::{MrEnclave, UserData},
};
2024-02-26 10:56:55 +00:00
pub trait Attestor {
type Error: ToString;
fn quote(&self, user_data: impl HasUserData) -> Result<Vec<u8>, Self::Error>;
2024-02-27 22:30:36 +00:00
fn mr_enclave(&self) -> Result<MrEnclave, Self::Error>;
2024-02-26 10:56:55 +00:00
}
#[derive(Clone, PartialEq, Debug)]
pub struct EpidAttestor;
impl Attestor for EpidAttestor {
type Error = IoError;
fn quote(&self, user_data: impl HasUserData) -> Result<Vec<u8>, Self::Error> {
let user_data = user_data.user_data();
let mut user_report_data = File::create("/dev/attestation/user_report_data")?;
user_report_data.write_all(user_data.as_slice())?;
user_report_data.flush()?;
read("/dev/attestation/quote")
}
2024-02-27 22:30:36 +00:00
fn mr_enclave(&self) -> Result<MrEnclave, Self::Error> {
let quote = self.quote(NullUserData)?;
Ok(quote[112..(112 + 32)]
.try_into()
.expect("hardcoded array size"))
}
2024-02-26 10:56:55 +00:00
}
2024-02-27 19:52:23 +00:00
#[derive(Clone, PartialEq, Debug)]
pub struct MockAttestor;
impl Attestor for MockAttestor {
type Error = String;
fn quote(&self, _user_data: impl HasUserData) -> Result<Vec<u8>, Self::Error> {
Ok(vec![])
}
2024-02-27 22:30:36 +00:00
fn mr_enclave(&self) -> Result<MrEnclave, Self::Error> {
Ok([0u8; 32])
}
}
struct NullUserData;
impl HasUserData for NullUserData {
fn user_data(&self) -> UserData {
[0u8; 64]
}
2024-02-27 19:52:23 +00:00
}