Skip to content

Commit da8a740

Browse files
committed
Update based on feedback
1 parent eee73c8 commit da8a740

File tree

4 files changed

+94
-93
lines changed

4 files changed

+94
-93
lines changed

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

Lines changed: 74 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,79 +5,96 @@
55
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
66
use super::Enclu;
77
use core::arch::asm;
8-
use core::mem::MaybeUninit;
98

10-
/// Wrapper struct to force 16-byte alignment.
11-
#[repr(align(16))]
12-
pub struct Align16<T>(pub T);
9+
/// Group all functions and types that is already upstreamed in the sgxstd
10+
#[cfg(all(target_env = "sgx", not(feature = "sgxstd")))]
11+
mod upstream {
12+
use super::*;
13+
use core::mem::MaybeUninit;
1314

14-
/// Wrapper struct to force 128-byte alignment.
15-
#[repr(align(128))]
16-
pub struct Align128<T>(pub T);
15+
/// Wrapper struct to force 16-byte alignment.
16+
#[repr(align(16))]
17+
pub struct Align16<T>(pub T);
1718

18-
/// Wrapper struct to force 256-byte alignment.
19-
#[repr(align(256))]
20-
pub struct Align256<T>(pub T);
19+
/// Wrapper struct to force 128-byte alignment.
20+
#[repr(align(128))]
21+
pub struct Align128<T>(pub T);
2122

22-
/// Wrapper struct to force 512-byte alignment.
23-
#[repr(align(512))]
24-
pub struct Align512<T>(pub T);
23+
/// Wrapper struct to force 512-byte alignment.
24+
#[repr(align(512))]
25+
pub struct Align512<T>(pub T);
2526

26-
/// Call the `EGETKEY` instruction to obtain a 128-bit secret key.
27-
pub fn egetkey(request: &Align512<[u8; 512]>) -> Result<Align16<[u8; 16]>, u32> {
28-
unsafe {
29-
let mut out = MaybeUninit::uninit();
30-
let error;
27+
/// Call the `EGETKEY` instruction to obtain a 128-bit secret key.
28+
pub fn egetkey(request: &Align512<[u8; 512]>) -> Result<Align16<[u8; 16]>, u32> {
29+
unsafe {
30+
let mut out = MaybeUninit::uninit();
31+
let error;
3132

32-
asm!(
33-
// rbx is reserved by LLVM
34-
"xchg %rbx, {0}",
35-
"enclu",
36-
"mov {0}, %rbx",
37-
inout(reg) request => _,
38-
inlateout("eax") Enclu::EGetkey as u32 => error,
39-
in("rcx") out.as_mut_ptr(),
40-
options(att_syntax, nostack),
41-
);
33+
asm!(
34+
// rbx is reserved by LLVM
35+
"xchg %rbx, {0}",
36+
"enclu",
37+
"mov {0}, %rbx",
38+
inout(reg) request => _,
39+
inlateout("eax") Enclu::EGetkey as u32 => error,
40+
in("rcx") out.as_mut_ptr(),
41+
options(att_syntax, nostack),
42+
);
4243

43-
match error {
44-
0 => Ok(out.assume_init()),
45-
err => Err(err),
44+
match error {
45+
0 => Ok(out.assume_init()),
46+
err => Err(err),
47+
}
4648
}
4749
}
48-
}
4950

50-
/// Call the `EREPORT` instruction.
51-
///
52-
/// This creates a cryptographic report describing the contents of the current
53-
/// enclave. The report may be verified by the enclave described in
54-
/// `targetinfo`.
55-
pub fn ereport(
56-
targetinfo: &Align512<[u8; 512]>,
57-
reportdata: &Align128<[u8; 64]>,
58-
) -> Align512<[u8; 432]> {
59-
unsafe {
60-
let mut report = MaybeUninit::uninit();
51+
/// Call the `EREPORT` instruction.
52+
///
53+
/// This creates a cryptographic report describing the contents of the current
54+
/// enclave. The report may be verified by the enclave described in
55+
/// `targetinfo`.
56+
pub fn ereport(
57+
targetinfo: &Align512<[u8; 512]>,
58+
reportdata: &Align128<[u8; 64]>,
59+
) -> Align512<[u8; 432]> {
60+
unsafe {
61+
let mut report = MaybeUninit::uninit();
6162

62-
asm!(
63-
// rbx is reserved by LLVM
64-
"xchg %rbx, {0}",
65-
"enclu",
66-
"mov {0}, %rbx",
67-
inout(reg) targetinfo => _,
68-
in("eax") Enclu::EReport as u32,
69-
in("rcx") reportdata,
70-
in("rdx") report.as_mut_ptr(),
71-
options(att_syntax, preserves_flags, nostack),
72-
);
63+
asm!(
64+
// rbx is reserved by LLVM
65+
"xchg %rbx, {0}",
66+
"enclu",
67+
"mov {0}, %rbx",
68+
inout(reg) targetinfo => _,
69+
in("eax") Enclu::EReport as u32,
70+
in("rcx") reportdata,
71+
in("rdx") report.as_mut_ptr(),
72+
options(att_syntax, preserves_flags, nostack),
73+
);
7374

74-
report.assume_init()
75+
report.assume_init()
76+
}
7577
}
7678
}
7779

80+
// Export the function in the `upstream` group if not using `sgxstd`
81+
#[cfg(all(target_env = "sgx", not(feature = "sgxstd")))]
82+
pub use self::upstream::*;
83+
84+
// Export function in the `fortanix_sgx::arch` namespace if using `sgxstd`
85+
#[cfg(all(target_env = "sgx", feature = "sgxstd"))]
86+
pub use std::os::fortanix_sgx::arch::*;
87+
88+
// Functions and types below is not yet upstreamed and will be added to the
89+
// upstream in the future.
90+
91+
/// Wrapper struct to force 256-byte alignment.
92+
#[repr(align(256))]
93+
pub struct Align256<T>(pub T);
94+
7895
/// Call the `EVERIFYREPORT2` instruction to verify a REPORT MAC struct.
7996
/// The concrete type is [`crate::ReportMac`].
80-
pub fn everifyreport2(tdx_report_mac: &Align256<[u8; 256]>) -> Result<(), u32> {
97+
pub fn everifyreport2(report_mac: &Align256<[u8; 256]>) -> Result<(), u32> {
8198
unsafe {
8299
let error: u32;
83100
asm!(
@@ -87,7 +104,7 @@ pub fn everifyreport2(tdx_report_mac: &Align256<[u8; 256]>) -> Result<(), u32> {
87104
"jz 1f",
88105
"xor %eax, %eax",
89106
"1:",
90-
inout(reg) tdx_report_mac => _,
107+
inout(reg) report_mac => _,
91108
inlateout("eax") Enclu::EVerifyReport2 as u32 => error,
92109
options(att_syntax, nostack),
93110
);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,16 +289,16 @@ impl ::core::fmt::Debug for Keyrequest {
289289
}
290290
}
291291

292-
impl ::core::fmt::Debug for ReportMac {
292+
impl ::core::fmt::Debug for ReportMacStruct {
293293
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
294294
f.debug_struct("ReportMac")
295295
.field("report_type", &self.report_type)
296-
.field("reserved1", &self.reserved1)
296+
.field("reserved1", &self._reserved1)
297297
.field("cpu_svn", &self.cpu_svn)
298298
.field("tee_tcb_info_hash", &self.tee_tcb_info_hash)
299299
.field("tee_info_hash", &self.tee_info_hash)
300300
.field("report_data", &self.report_data)
301-
.field("reserved2", &self.reserved2)
301+
.field("reserved2", &self._reserved2)
302302
.field("mac", &self.mac)
303303
.finish()
304304
}

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

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,9 @@ extern crate serde;
2828
#[cfg(feature = "serde")]
2929
use serde::{Serialize, Deserialize};
3030

31-
#[cfg(all(target_env = "sgx", feature = "sgxstd"))]
32-
use std::os::fortanix_sgx::arch;
33-
34-
#[cfg(all(target_env = "sgx", not(feature = "sgxstd")))]
31+
#[cfg(target_env = "sgx")]
3532
mod arch;
3633

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-
4834
use core::{convert::TryFrom, num::TryFromIntError, slice};
4935

5036
#[cfg(feature = "serde")]
@@ -235,7 +221,9 @@ macro_rules! struct_def {
235221
(@align bytes 128 name $name:ident) => {
236222
struct_def!(@align type Align128 name $name);
237223
};
238-
224+
(@align bytes 256 name $name:ident) => {
225+
struct_def!(@align type Align256 name $name);
226+
};
239227
(@align bytes 512 name $name:ident) => {
240228
struct_def!(@align type Align512 name $name);
241229
};
@@ -875,46 +863,39 @@ struct_def! {
875863
#[repr(C, align(256))]
876864
#[cfg_attr(
877865
feature = "large_array_derive",
878-
derive(Clone, Debug, Eq, PartialEq)
866+
derive(Clone, Debug, Default, Eq, PartialEq)
879867
)]
880-
pub struct ReportMac {
868+
pub struct ReportMacStruct {
881869
/// ( 0) TEE Report type
882870
pub report_type: TeeReportType,
883871
/// ( 4) Reserved, must be zero
884-
pub reserved1: [u8; REPORT_MAC_STRUCT_RESERVED1_BYTES],
872+
pub _reserved1: [u8; REPORT_MAC_STRUCT_RESERVED1_BYTES],
885873
/// ( 16) Security Version of the CPU
886-
pub cpu_svn: [u8; CPU_SVN_SIZE],
874+
pub cpusvn: [u8; CPU_SVN_SIZE],
887875
/// ( 32) SHA384 of TEE_TCB_INFO for TEEs
888876
pub tee_tcb_info_hash: Sha384Hash,
889877
/// ( 80) SHA384 of TEE_INFO
890878
pub tee_info_hash: Sha384Hash,
891879
/// (128) Data provided by the user
892880
pub report_data: [u8; REPORT_DATA_SIZE],
893881
/// (192) Reserved, must be zero
894-
pub reserved2: [u8; REPORT_MAC_STRUCT_RESERVED2_BYTES],
882+
pub _reserved2: [u8; REPORT_MAC_STRUCT_RESERVED2_BYTES],
895883
/// (224) The Message Authentication Code over this structure
896884
pub mac: [u8; TEE_MAC_SIZE],
897885
}
898886
}
899887

900-
impl ReportMac {
888+
impl ReportMacStruct {
901889
pub const UNPADDED_SIZE: usize = 256;
902890

903891
#[cfg(target_env = "sgx")]
904892
pub fn verify(&self) -> Result<(), ErrorCode> {
905-
everifyreport2(self.as_ref())
893+
arch::everifyreport2(self.as_ref())
906894
// Same as `egetkey` reasoning: unwrap is okay here
907895
.map_err(|e| ErrorCode::try_from(e).unwrap())
908896
}
909897
}
910898

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-
918899
#[test]
919900
fn test_eq() {
920901
let mut a = Keyrequest::default();

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ use crate::arch;
1212
#[cfg(all(target_env = "sgx", feature = "sgxstd"))]
1313
use std::os::fortanix_sgx::arch;
1414

15-
use crate::{slice, struct_def, ErrorCode, ReportMac, Sha384Hash};
15+
use crate::{slice, struct_def, ReportMacStruct, Sha384Hash};
16+
17+
#[cfg(target_env = "sgx")]
18+
use crate::ErrorCode;
1619

1720
/// SGX Legacy Report Type
1821
pub const SGX_LEGACY_REPORT_TYPE: usize = 0x0;
@@ -26,7 +29,7 @@ pub const TEE_REPORT2_VERSION: usize = 0x0;
2629
pub const TEE_REPORT2_VERSION_SERVICETD: usize = 0x1;
2730

2831
// Ensure new type name is backward compatibe
29-
pub type TdxReportMac = super::ReportMac;
32+
pub type TdxReportMac = super::ReportMacStruct;
3033

3134
/// Size of a TDX report in bytes.
3235
pub const TDX_REPORT_SIZE: usize = 1024;
@@ -168,7 +171,7 @@ struct_def! {
168171
)]
169172
pub struct TdxReportV1 {
170173
/// ( 0) Report mac struct for SGX report type 2
171-
pub report_mac: ReportMac,
174+
pub report_mac: ReportMacStruct,
172175
/// (256) Struct contains details about extra TCB elements not found in CPUSVN
173176
pub tee_tcb_info: TeeTcbInfo,
174177
/// (495) Reserved, must be zero

0 commit comments

Comments
 (0)