]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/sgx/ext/arch.rs
Fix undefined behavior
[rust.git] / src / libstd / sys / sgx / ext / arch.rs
1 //! SGX-specific access to architectural features.
2 //!
3 //! The functionality in this module is further documented in the Intel
4 //! Software Developer's Manual, Volume 3, Chapter 40.
5 #![unstable(feature = "sgx_platform", issue = "56975")]
6
7 use mem::MaybeUninit;
8
9 /// Wrapper struct to force 16-byte alignment.
10 #[repr(align(16))]
11 #[unstable(feature = "sgx_platform", issue = "56975")]
12 pub struct Align16<T>(pub T);
13
14 /// Wrapper struct to force 128-byte alignment.
15 #[repr(align(128))]
16 #[unstable(feature = "sgx_platform", issue = "56975")]
17 pub struct Align128<T>(pub T);
18
19 /// Wrapper struct to force 512-byte alignment.
20 #[repr(align(512))]
21 #[unstable(feature = "sgx_platform", issue = "56975")]
22 pub struct Align512<T>(pub T);
23
24 const ENCLU_EREPORT: u32 = 0;
25 const ENCLU_EGETKEY: u32 = 1;
26
27 /// Call the `EGETKEY` instruction to obtain a 128-bit secret key.
28 #[unstable(feature = "sgx_platform", issue = "56975")]
29 pub fn egetkey(request: &Align512<[u8; 512]>) -> Result<Align16<[u8; 16]>, u32> {
30     unsafe {
31         let mut out = MaybeUninit::uninitialized();
32         let error;
33
34         asm!(
35             "enclu"
36             : "={eax}"(error)
37             : "{eax}"(ENCLU_EGETKEY),
38               "{rbx}"(request),
39               "{rcx}"(out.as_mut_ptr())
40             : "flags"
41         );
42
43         match error {
44             0 => Ok(out.into_inner()),
45             err => Err(err),
46         }
47     }
48 }
49
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 #[unstable(feature = "sgx_platform", issue = "56975")]
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::uninitialized();
62
63         asm!(
64             "enclu"
65             : /* no output registers */
66             : "{eax}"(ENCLU_EREPORT),
67               "{rbx}"(targetinfo),
68               "{rcx}"(reportdata),
69               "{rdx}"(report.as_mut_ptr())
70         );
71
72         report.into_inner()
73     }
74 }