]> git.proxmox.com Git - rustc.git/blob - library/std/src/os/fortanix_sgx/arch.rs
New upstream version 1.54.0+dfsg1
[rustc.git] / library / std / src / os / fortanix_sgx / 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 crate::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::uninit();
32 let error;
33
34 asm!(
35 // rbx is reserved by LLVM
36 "xchg {0}, rbx",
37 "enclu",
38 "mov rbx, {0}",
39 inout(reg) request => _,
40 inlateout("eax") ENCLU_EGETKEY => error,
41 in("rcx") out.as_mut_ptr(),
42 options(nostack),
43 );
44
45 match error {
46 0 => Ok(out.assume_init()),
47 err => Err(err),
48 }
49 }
50 }
51
52 /// Call the `EREPORT` instruction.
53 ///
54 /// This creates a cryptographic report describing the contents of the current
55 /// enclave. The report may be verified by the enclave described in
56 /// `targetinfo`.
57 #[unstable(feature = "sgx_platform", issue = "56975")]
58 pub fn ereport(
59 targetinfo: &Align512<[u8; 512]>,
60 reportdata: &Align128<[u8; 64]>,
61 ) -> Align512<[u8; 432]> {
62 unsafe {
63 let mut report = MaybeUninit::uninit();
64
65 asm!(
66 // rbx is reserved by LLVM
67 "xchg {0}, rbx",
68 "enclu",
69 "mov rbx, {0}",
70 inout(reg) targetinfo => _,
71 in("eax") ENCLU_EREPORT,
72 in("rcx") reportdata,
73 in("rdx") report.as_mut_ptr(),
74 options(preserves_flags, nostack),
75 );
76
77 report.assume_init()
78 }
79 }