Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add De/Serialize to Report #366

Merged
merged 1 commit into from
Jul 18, 2023
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
12 changes: 7 additions & 5 deletions core/sys/types/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ use mc_sgx_core_build::SgxParseCallbacks;
const CORE_TYPES: &[&str] = &[
"_attributes_t",
"_key_request_t",
"_report_body_t",
"_report_t",
"_sgx_cpu_svn_t",
"_sgx_key_id_t",
"_sgx_measurement_t",
"_sgx_report_data_t",
"_sgx_misc_attribute_t",
"_status_t",
"sgx_config_id_t",
Expand Down Expand Up @@ -105,15 +103,19 @@ fn main() {
"sgx_qe_report_info_t",
"sgx_quote_nonce_t",
"sgx_report_t",
"sgx_report_body_t",
"sgx_key_id_t",
"sgx_cpu_svn_t",
"sgx_measurement_t",
"sgx_report_data_t",
"sgx_attributes_t",
])
.dynamically_sized_types(["sgx_quote_t"])
.serialize_types(["sgx_measurement_t", "sgx_attributes_t"])
.serialize_types([
"sgx_measurement_t",
"sgx_attributes_t",
"sgx_report_t",
"sgx_key_id_t",
"sgx_cpu_svn_t",
])
.derive_default([
"sgx_report_t",
"sgx_attributes_t",
Expand Down
32 changes: 32 additions & 0 deletions core/sys/types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,38 @@ impl Default for sgx_att_key_id_ext_t {
}
}

#[serde_as]
#[repr(C)]
#[derive(PartialEq, Hash, Clone, Eq, Copy, Debug, Serialize, Deserialize)]
pub struct sgx_report_data_t {
#[serde_as(as = "Bytes")]
pub d: [u8; SGX_REPORT_DATA_SIZE],
}

#[serde_as]
#[repr(C)]
#[derive(Hash, Copy, Eq, Debug, PartialEq, Clone, Serialize, Deserialize)]
pub struct sgx_report_body_t {
pub cpu_svn: sgx_cpu_svn_t,
pub misc_select: sgx_misc_select_t,
pub reserved1: [u8; SGX_REPORT_BODY_RESERVED1_BYTES],
pub isv_ext_prod_id: sgx_isvext_prod_id_t,
pub attributes: sgx_attributes_t,
pub mr_enclave: sgx_measurement_t,
pub reserved2: [u8; SGX_REPORT_BODY_RESERVED2_BYTES],
pub mr_signer: sgx_measurement_t,
pub reserved3: [u8; SGX_REPORT_BODY_RESERVED3_BYTES],
#[serde_as(as = "Bytes")]
pub config_id: sgx_config_id_t,
pub isv_prod_id: sgx_prod_id_t,
pub isv_svn: sgx_isv_svn_t,
pub config_svn: sgx_config_svn_t,
#[serde_as(as = "Bytes")]
pub reserved4: [u8; SGX_REPORT_BODY_RESERVED4_BYTES],
pub isv_family_id: sgx_isvfamily_id_t,
pub report_data: sgx_report_data_t,
}

impl Default for sgx_report_body_t {
fn default() -> Self {
Self {
Expand Down
21 changes: 20 additions & 1 deletion core/types/src/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use mc_sgx_core_sys_types::{
};
use nom::bytes::complete::take;
use nom::number::complete::{le_u16, le_u32, le_u64};
use serde::{Deserialize, Serialize};

/// MAC
#[derive(Default, Debug, Clone, Hash, PartialEq, Eq)]
Expand Down Expand Up @@ -245,7 +246,7 @@ impl TryFrom<&[u8]> for ReportBody {

/// An enclave Report
#[repr(transparent)]
#[derive(Default, Debug, Clone, Hash, PartialEq, Eq)]
#[derive(Default, Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct Report(sgx_report_t);

impl Report {
Expand Down Expand Up @@ -465,6 +466,7 @@ mod test {
})
);
}

#[test]
fn report_from_sgx_report() {
let mut body = ReportBody::default();
Expand All @@ -480,6 +482,23 @@ mod test {
assert_eq!(report.mac(), Mac([5u8; SGX_MAC_SIZE]));
}

#[test]
fn report_serialized_from_sgx_report() {
let mut body = ReportBody::default();
body.0.isv_prod_id = 4;
let sgx_report = sgx_report_t {
body: body.clone().into(),
key_id: KeyId::from([5u8; SGX_KEYID_SIZE]).into(),
mac: [6u8; SGX_MAC_SIZE],
};
let bytes = serde_cbor::to_vec(&sgx_report).expect("Failed to serialize");
let report: Report =
serde_cbor::from_slice(bytes.as_slice()).expect("Failed to deserialize");
assert_eq!(report.body(), body);
assert_eq!(report.key_id(), KeyId::from([5u8; SGX_KEYID_SIZE]));
assert_eq!(report.mac(), Mac([6u8; SGX_MAC_SIZE]));
}

#[test]
fn sgx_report_default() {
let report = Report::default();
Expand Down