Skip to content

Commit 35a04aa

Browse files
committed
Refactoring TDX specific types to correct places
- `verify` function now is part of `ReportMac` instead of `TdxReportV1`, although maintaining compatibility that the `verify` function still exists in `TdxReportV1` - Move the internal `tdx_arch` module to be part of entire `arch` module in the crate. - Move `ReportMac` to the main crate level instead of under `tdx` module namespace - Move all `TdxError` crates out as it is more relevant to `tdx-ql` crate. The error from SGX-ISA should be only the `ErrorCode` types. `TdxError` is not part of the ISA.
1 parent 7f24b49 commit 35a04aa

File tree

8 files changed

+203
-221
lines changed

8 files changed

+203
-221
lines changed

intel-sgx/aesm-client/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ test-sgx = []
2727
[dependencies]
2828
# Project dependencies
2929
sgxs = { version = "0.8.0", path = "../sgxs", optional = true }
30-
sgx-isa = { version = "0.4.0", path = "../sgx-isa"}
30+
sgx-isa = { version = "0.4.1", path = "../sgx-isa"}
3131

3232
# External dependencies
3333
byteorder = "1.0" # Unlicense/MIT
@@ -53,6 +53,6 @@ libloading = "0.5.2"
5353
protobuf-codegen = "3" # MIT
5454

5555
[dev-dependencies]
56-
sgx-isa = { version = "0.4.0", path = "../sgx-isa" }
56+
sgx-isa = { version = "0.4.1", path = "../sgx-isa" }
5757
"report-test" = { version = "0.5.0", path = "../report-test" }
5858
"sgxs-loaders" = { version = "0.5.0", path = "../sgxs-loaders" }

intel-sgx/sgx-isa/src/arch.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ pub struct Align16<T>(pub T);
1515
#[repr(align(128))]
1616
pub struct Align128<T>(pub T);
1717

18+
/// Wrapper struct to force 256-byte alignment.
19+
#[repr(align(256))]
20+
pub struct Align256<T>(pub T);
21+
1822
/// Wrapper struct to force 512-byte alignment.
1923
#[repr(align(512))]
2024
pub struct Align512<T>(pub T);
@@ -70,3 +74,26 @@ pub fn ereport(
7074
report.assume_init()
7175
}
7276
}
77+
78+
/// Call the `EVERIFYREPORT2` instruction to verify a REPORT MAC struct.
79+
/// The concrete type is [`crate::ReportMac`].
80+
pub fn everifyreport2(tdx_report_mac: &Align256<[u8; 256]>) -> Result<(), u32> {
81+
unsafe {
82+
let error: u32;
83+
asm!(
84+
"xchg %rbx, {0}",
85+
"enclu",
86+
"mov {0}, %rbx",
87+
"jz 1f",
88+
"xor %eax, %eax",
89+
"1:",
90+
inout(reg) tdx_report_mac => _,
91+
inlateout("eax") Enclu::EVerifyReport2 as u32 => error,
92+
options(att_syntax, nostack),
93+
);
94+
match error {
95+
0 => Ok(()),
96+
err => Err(err),
97+
}
98+
}
99+
}

intel-sgx/sgx-isa/src/large_array_impl.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,3 +288,18 @@ impl ::core::fmt::Debug for Keyrequest {
288288
}
289289
}
290290
}
291+
292+
impl ::core::fmt::Debug for ReportMac {
293+
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
294+
f.debug_struct("ReportMac")
295+
.field("report_type", &self.report_type)
296+
.field("reserved1", &self.reserved1)
297+
.field("cpu_svn", &self.cpu_svn)
298+
.field("tee_tcb_info_hash", &self.tee_tcb_info_hash)
299+
.field("tee_info_hash", &self.tee_info_hash)
300+
.field("report_data", &self.report_data)
301+
.field("reserved2", &self.reserved2)
302+
.field("mac", &self.mac)
303+
.finish()
304+
}
305+
}

intel-sgx/sgx-isa/src/lib.rs

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ pub enum ErrorCode {
305305
PageAttributesMismatch = 19,
306306
PageNotModifiable = 20,
307307
PageNotDebuggable = 21,
308+
InvalidReportMacStruct = 28,
308309
InvalidCpusvn = 32,
309310
InvalidIsvsvn = 64,
310311
UnmaskedEvent = 128,
@@ -702,7 +703,7 @@ impl Report {
702703
/// implementation of the verifying function.
703704
///
704705
/// Care should be taken that `check_mac` prevents timing attacks,
705-
/// in particular that the comparison happens in constant time.
706+
/// in particular that the comparison happens in constant time.
706707
#[cfg(target_env = "sgx")]
707708
pub fn verify<F, R>(&self, check_mac: F) -> R
708709
where
@@ -808,6 +809,100 @@ impl Default for Keypolicy {
808809
}
809810
}
810811

812+
struct_def! {
813+
/// Rust definition of `REPORTTYPE` from `REPORTMACSTRUCT`.
814+
///
815+
/// Ref: Intel® Trust Domain CPU Architectural Extensions, table 2-4.
816+
/// Version: 343754-002US, MAY 2021
817+
/// Link: <https://cdrdv2.intel.com/v1/dl/getContent/733582>
818+
#[repr(C, align(4))]
819+
#[derive(Clone, Debug, Default, Eq, PartialEq)]
820+
pub struct TeeReportType {
821+
/// Trusted Execution Environment(TEE) type:
822+
/// 0x00: SGX Legacy REPORT TYPE
823+
/// 0x7F-0x01: Reserved
824+
/// 0x80: Reserved
825+
/// 0x81: TEE Report type 2
826+
/// 0xFF-0x82: Reserved
827+
pub report_type: u8,
828+
/// TYPE-specific subtype, Stage1: value is 0
829+
pub subtype: u8,
830+
/// TYPE-specific version, Stage1: value is 0
831+
pub version: u8,
832+
pub reserved: u8,
833+
}
834+
}
835+
836+
impl TeeReportType {
837+
pub const UNPADDED_SIZE: usize = 4;
838+
}
839+
840+
/// SHA384 hash size in bytes
841+
pub const HASH_384_SIZE: usize = 48;
842+
/// SHA384 hash
843+
pub type Sha384Hash = [u8; HASH_384_SIZE];
844+
845+
pub const CPU_SVN_SIZE: usize = 16;
846+
pub const REPORT_MAC_STRUCT_SIZE: usize = 256;
847+
pub const REPORT_MAC_STRUCT_RESERVED1_BYTES: usize = 12;
848+
pub const REPORT_MAC_STRUCT_RESERVED2_BYTES: usize = 32;
849+
pub const REPORT_DATA_SIZE: usize = 64;
850+
851+
/// Message SHA 256 HASH Code - 32 bytes
852+
pub const TEE_MAC_SIZE: usize = 32;
853+
854+
855+
struct_def! {
856+
/// Rust definition of `REPORTMACSTRUCT`, used by TDX `TDREPORT_STRUCT`
857+
/// and the future 256BITSGX
858+
///
859+
/// Ref: Intel® Trust Domain CPU Architectural Extensions, table 2-5.
860+
/// Version: 343754-002US, MAY 2021
861+
/// Link TDX: <https://cdrdv2.intel.com/v1/dl/getContent/733582>
862+
/// Link 256BITSGX: <https://cdrdv2-public.intel.com/851355/319433-057-architecture-instruction-set-extensions-programming-reference.pdf>
863+
#[repr(C, align(256))]
864+
#[cfg_attr(
865+
feature = "large_array_derive",
866+
derive(Clone, Debug, Eq, PartialEq)
867+
)]
868+
pub struct ReportMac {
869+
/// ( 0) TEE Report type
870+
pub report_type: TeeReportType,
871+
/// ( 4) Reserved, must be zero
872+
pub reserved1: [u8; REPORT_MAC_STRUCT_RESERVED1_BYTES],
873+
/// ( 16) Security Version of the CPU
874+
pub cpu_svn: [u8; CPU_SVN_SIZE],
875+
/// ( 32) SHA384 of TEE_TCB_INFO for TEEs
876+
pub tee_tcb_info_hash: Sha384Hash,
877+
/// ( 80) SHA384 of TEE_INFO
878+
pub tee_info_hash: Sha384Hash,
879+
/// (128) Data provided by the user
880+
pub report_data: [u8; REPORT_DATA_SIZE],
881+
/// (192) Reserved, must be zero
882+
pub reserved2: [u8; REPORT_MAC_STRUCT_RESERVED2_BYTES],
883+
/// (224) The Message Authentication Code over this structure
884+
pub mac: [u8; TEE_MAC_SIZE],
885+
}
886+
}
887+
888+
impl ReportMac {
889+
pub const UNPADDED_SIZE: usize = 256;
890+
891+
#[cfg(target_env = "sgx")]
892+
pub fn verify(&self) -> Result<(), ErrorCode> {
893+
arch::everifyreport2(self.as_ref())
894+
// Same as `egetkey` reasoning: unwrap is okay here
895+
.map_err(|e| ErrorCode::try_from(e).unwrap())
896+
}
897+
}
898+
899+
#[cfg(target_env = "sgx")]
900+
impl AsRef<arch::Align256<[u8; ReportMac::UNPADDED_SIZE]>> for ReportMac {
901+
fn as_ref(&self) -> &arch::Align256<[u8; Self::UNPADDED_SIZE]> {
902+
unsafe { &*(self as *const _ as *const _) }
903+
}
904+
}
905+
811906
#[test]
812907
fn test_eq() {
813908
let mut a = Keyrequest::default();

0 commit comments

Comments
 (0)