Skip to content

Commit eee73c8

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 eee73c8

File tree

8 files changed

+216
-222
lines changed

8 files changed

+216
-222
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: 109 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,22 @@ use serde::{Serialize, Deserialize};
3030

3131
#[cfg(all(target_env = "sgx", feature = "sgxstd"))]
3232
use std::os::fortanix_sgx::arch;
33+
3334
#[cfg(all(target_env = "sgx", not(feature = "sgxstd")))]
3435
mod arch;
35-
use core::{convert::TryFrom, num::TryFromIntError, slice};
3636

37+
// Compatibility layer before the `EVERIFYREPORT2` is upstreamed
38+
#[cfg(all(target_env = "sgx", feature = "sgxstd"))]
39+
#[path ="arch.rs"]
40+
mod non_std_arch;
41+
42+
#[cfg(all(target_env = "sgx", feature = "sgxstd"))]
43+
use non_std_arch::{Align256, everifyreport2};
44+
45+
#[cfg(all(target_env = "sgx", not(feature = "sgxstd")))]
46+
use arch::{Align256, everifyreport2};
47+
48+
use core::{convert::TryFrom, num::TryFromIntError, slice};
3749

3850
#[cfg(feature = "serde")]
3951
mod array_64 {
@@ -305,6 +317,7 @@ pub enum ErrorCode {
305317
PageAttributesMismatch = 19,
306318
PageNotModifiable = 20,
307319
PageNotDebuggable = 21,
320+
InvalidReportMacStruct = 28,
308321
InvalidCpusvn = 32,
309322
InvalidIsvsvn = 64,
310323
UnmaskedEvent = 128,
@@ -702,7 +715,7 @@ impl Report {
702715
/// implementation of the verifying function.
703716
///
704717
/// Care should be taken that `check_mac` prevents timing attacks,
705-
/// in particular that the comparison happens in constant time.
718+
/// in particular that the comparison happens in constant time.
706719
#[cfg(target_env = "sgx")]
707720
pub fn verify<F, R>(&self, check_mac: F) -> R
708721
where
@@ -808,6 +821,100 @@ impl Default for Keypolicy {
808821
}
809822
}
810823

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

0 commit comments

Comments
 (0)