use serde::Serialize;
const CLASS_ID_GUID: &str = "7fb00ee4-a7ff-11ed-9e2f-00155d09de56";
#[derive(Serialize, Debug)]
pub struct Environment {
pub class_id: String,
}
#[derive(Serialize, Debug, Clone)]
pub struct SnpLaunchMeasurement {
#[serde(
serialize_with = "hex::serde::serialize_upper",
deserialize_with = "hex::serde::deserialize"
)]
pub snp_ld: [u8; 48],
}
#[derive(Serialize, Debug, Clone)]
pub struct TdxLaunchMeasurement {
#[serde(
serialize_with = "hex::serde::serialize_upper",
deserialize_with = "hex::serde::deserialize"
)]
pub tdx_mrtd: [u8; 48],
}
#[derive(Serialize, Debug, Clone)]
pub struct VbsLaunchMeasurement {
#[serde(
serialize_with = "hex::serde::serialize_upper",
deserialize_with = "hex::serde::deserialize"
)]
pub vbs_boot_digest: [u8; 32],
}
#[derive(Serialize, Debug, Clone)]
pub struct BuildInfo {
pub debug_build: bool,
}
#[derive(Serialize, Debug, Clone)]
pub struct SnpEndorsement {
pub snp_isvsvn: u32,
pub build_info: BuildInfo,
}
#[derive(Serialize, Debug, Clone)]
pub struct TdxEndorsement {
pub tdx_isvsvn: u32,
pub build_info: BuildInfo,
}
#[derive(Serialize, Debug, Clone)]
pub struct VbsEndorsement {
pub vbs_isvsvn: u32,
pub build_info: BuildInfo,
}
#[derive(Serialize, Debug, Clone)]
pub struct MeasurementInstance<R, E> {
pub reference: R,
pub endorsement: E,
}
#[derive(Serialize, Debug)]
pub struct BaseMeasurement<R, E> {
pub environment: Environment,
pub series: Vec<MeasurementInstance<R, E>>,
}
#[derive(Serialize, Debug)]
#[serde(untagged)]
pub enum Measurement {
Snp(BaseMeasurement<SnpLaunchMeasurement, SnpEndorsement>),
Tdx(BaseMeasurement<TdxLaunchMeasurement, TdxEndorsement>),
Vbs(BaseMeasurement<VbsLaunchMeasurement, VbsEndorsement>),
}
pub type SnpMeasurement = BaseMeasurement<SnpLaunchMeasurement, SnpEndorsement>;
pub type TdxMeasurement = BaseMeasurement<TdxLaunchMeasurement, TdxEndorsement>;
pub type VbsMeasurement = BaseMeasurement<VbsLaunchMeasurement, VbsEndorsement>;
impl SnpMeasurement {
pub fn new(ld: [u8; 48], svn: u32, debug_enabled: bool) -> Self {
let info = BuildInfo {
debug_build: debug_enabled,
};
let measurements: MeasurementInstance<_, _> = MeasurementInstance {
reference: SnpLaunchMeasurement { snp_ld: ld },
endorsement: SnpEndorsement {
snp_isvsvn: svn,
build_info: info,
},
};
BaseMeasurement {
environment: Environment {
class_id: CLASS_ID_GUID.to_string(),
},
series: [measurements].to_vec(),
}
}
}
impl TdxMeasurement {
pub fn new(mrtd: [u8; 48], svn: u32, debug_enabled: bool) -> Self {
let info = BuildInfo {
debug_build: debug_enabled,
};
let measurements: MeasurementInstance<_, _> = MeasurementInstance {
reference: TdxLaunchMeasurement { tdx_mrtd: mrtd },
endorsement: TdxEndorsement {
tdx_isvsvn: svn,
build_info: info,
},
};
BaseMeasurement {
environment: Environment {
class_id: CLASS_ID_GUID.to_string(),
},
series: [measurements].to_vec(),
}
}
}
impl VbsMeasurement {
pub fn new(digest: [u8; 32], svn: u32, debug_enabled: bool) -> Self {
let info = BuildInfo {
debug_build: debug_enabled,
};
let measurements: MeasurementInstance<_, _> = MeasurementInstance {
reference: VbsLaunchMeasurement {
vbs_boot_digest: digest,
},
endorsement: VbsEndorsement {
vbs_isvsvn: svn,
build_info: info,
},
};
BaseMeasurement {
environment: Environment {
class_id: CLASS_ID_GUID.to_string(),
},
series: [measurements].to_vec(),
}
}
}