55 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
66use super :: Enclu ;
77use 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 ) ;
0 commit comments