Skip to content

Commit

Permalink
greq: add structure layout tests
Browse files Browse the repository at this point in the history
Add tests to verify that the layout for the attestation report and
the SNP_GUEST_REQUEST command structures are correct.

Signed-off-by: Carlos López <carlos.lopez@suse.com>
  • Loading branch information
00xc committed Nov 23, 2023
1 parent de614dc commit c9dfb80
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ test = { version = "0.1.0", path = "test" }
default = ["enable-stacktrace"]
enable-stacktrace = []
enable-gdb = ["dep:gdbstub", "dep:gdbstub_arch"]

[dev-dependencies]
memoffset = "0.9.0"
23 changes: 23 additions & 0 deletions src/greq/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,29 @@ mod tests {
use super::*;
use crate::mm::alloc::{TestRootMem, DEFAULT_TEST_MEMORY_SIZE};
use crate::sev::secrets_page::VMPCK_SIZE;
use memoffset::offset_of;

#[test]
fn test_snp_guest_request_hdr_offsets() {
assert_eq!(offset_of!(SnpGuestRequestMsgHdr, authtag), 0);
assert_eq!(offset_of!(SnpGuestRequestMsgHdr, msg_seqno), 0x20);
assert_eq!(offset_of!(SnpGuestRequestMsgHdr, rsvd1), 0x28);
assert_eq!(offset_of!(SnpGuestRequestMsgHdr, algo), 0x30);
assert_eq!(offset_of!(SnpGuestRequestMsgHdr, hdr_version), 0x31);
assert_eq!(offset_of!(SnpGuestRequestMsgHdr, hdr_sz), 0x32);
assert_eq!(offset_of!(SnpGuestRequestMsgHdr, msg_type), 0x34);
assert_eq!(offset_of!(SnpGuestRequestMsgHdr, msg_version), 0x35);
assert_eq!(offset_of!(SnpGuestRequestMsgHdr, msg_sz), 0x36);
assert_eq!(offset_of!(SnpGuestRequestMsgHdr, rsvd2), 0x38);
assert_eq!(offset_of!(SnpGuestRequestMsgHdr, msg_vmpck), 0x3c);
assert_eq!(offset_of!(SnpGuestRequestMsgHdr, rsvd3), 0x3d);
}

#[test]
fn test_snp_guest_request_msg_offsets() {
assert_eq!(offset_of!(SnpGuestRequestMsg, hdr), 0);
assert_eq!(offset_of!(SnpGuestRequestMsg, pld), 0x60);
}

#[test]
fn test_requestmsg_boxed_new() {
Expand Down
56 changes: 56 additions & 0 deletions src/greq/pld_report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,59 @@ pub struct AttestationReport {
/// Signature of bytes 0h to 29Fh inclusive of this report
signature: Signature,
}

#[cfg(test)]
mod tests {
use super::*;
use memoffset::offset_of;

#[test]
fn test_snp_report_request_offsets() {
assert_eq!(offset_of!(SnpReportRequest, user_data), 0x0);
assert_eq!(offset_of!(SnpReportRequest, vmpl), 0x40);
assert_eq!(offset_of!(SnpReportRequest, flags), 0x44);
assert_eq!(offset_of!(SnpReportRequest, rsvd), 0x48);
}

#[test]
fn test_snp_report_response_offsets() {
assert_eq!(offset_of!(SnpReportResponse, status), 0x0);
assert_eq!(offset_of!(SnpReportResponse, report_size), 0x4);
assert_eq!(offset_of!(SnpReportResponse, _reserved), 0x8);
assert_eq!(offset_of!(SnpReportResponse, report), 0x20);
}

#[test]
fn test_ecdsa_p384_sha384_signature_offsets() {
assert_eq!(offset_of!(Signature, r), 0x0);
assert_eq!(offset_of!(Signature, s), 0x48);
assert_eq!(offset_of!(Signature, reserved), 0x90);
}

#[test]
fn test_attestation_report_offsets() {
assert_eq!(offset_of!(AttestationReport, version), 0x0);
assert_eq!(offset_of!(AttestationReport, guest_svn), 0x4);
assert_eq!(offset_of!(AttestationReport, policy), 0x8);
assert_eq!(offset_of!(AttestationReport, family_id), 0x10);
assert_eq!(offset_of!(AttestationReport, image_id), 0x20);
assert_eq!(offset_of!(AttestationReport, vmpl), 0x30);
assert_eq!(offset_of!(AttestationReport, signature_algo), 0x34);
assert_eq!(offset_of!(AttestationReport, platform_version), 0x38);
assert_eq!(offset_of!(AttestationReport, platform_info), 0x40);
assert_eq!(offset_of!(AttestationReport, flags), 0x48);
assert_eq!(offset_of!(AttestationReport, reserved0), 0x4c);
assert_eq!(offset_of!(AttestationReport, report_data), 0x50);
assert_eq!(offset_of!(AttestationReport, measurement), 0x90);
assert_eq!(offset_of!(AttestationReport, host_data), 0xc0);
assert_eq!(offset_of!(AttestationReport, id_key_digest), 0xe0);
assert_eq!(offset_of!(AttestationReport, author_key_digest), 0x110);
assert_eq!(offset_of!(AttestationReport, report_id), 0x140);
assert_eq!(offset_of!(AttestationReport, report_id_ma), 0x160);
assert_eq!(offset_of!(AttestationReport, reported_tcb), 0x180);
assert_eq!(offset_of!(AttestationReport, reserved1), 0x188);
assert_eq!(offset_of!(AttestationReport, chip_id), 0x1a0);
assert_eq!(offset_of!(AttestationReport, reserved2), 0x1e0);
assert_eq!(offset_of!(AttestationReport, signature), 0x2a0);
}
}

0 comments on commit c9dfb80

Please sign in to comment.