1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License..

use crate::arch::{Enclu, Secinfo};
use crate::se::{
    AlignKey, AlignKeyRequest, AlignReport, AlignReport2Mac, AlignReportData, AlignTargetInfo,
};
use core::arch::asm;
use core::mem::MaybeUninit;

pub struct EncluInst;

impl EncluInst {
    pub fn ereport(ti: &AlignTargetInfo, rd: &AlignReportData) -> Result<AlignReport, u32> {
        unsafe {
            let mut report = MaybeUninit::uninit();
            asm!(
                "xchg rbx, {0}",
                "enclu",
                "mov rbx, {0}",
                inout(reg) ti => _,
                in("eax") Enclu::EReport as u32,
                in("rcx") rd,
                in("rdx") report.as_mut_ptr(),
                options(preserves_flags, nostack),
            );
            Ok(report.assume_init())
        }
    }

    pub fn everify_report2(r: &AlignReport2Mac) -> Result<(), u32> {
        extern "C" {
            fn everifyreport2(r: *const AlignReport2Mac) -> u32;
        }
        let error = unsafe { everifyreport2(r) };
        if error == 0 {
            Ok(())
        } else {
            Err(error)
        }
    }

    pub fn egetkey(kr: &AlignKeyRequest) -> Result<AlignKey, u32> {
        unsafe {
            let mut key = MaybeUninit::uninit();
            let error;
            asm!(
                "xchg rbx, {0}",
                "enclu",
                "mov rbx, {0}",
                inout(reg) kr => _,
                inlateout("eax") Enclu::EGetkey as u32 => error,
                in("rcx") key.as_mut_ptr(),
                options(nostack),
            );
            if error == 0 {
                Ok(key.assume_init())
            } else {
                Err(error)
            }
        }
    }

    pub fn eaccept(info: &Secinfo, addr: usize) -> Result<(), u32> {
        unsafe {
            let error;
            asm!(
                "xchg rbx, {0}",
                "enclu",
                "mov rbx, {0}",
                inout(reg) info => _,
                inlateout("eax") Enclu::EAccept as u32 => error,
                in("rcx") addr,
                options(nostack),
            );
            match error {
                0 => Ok(()),
                _ => Err(error),
            }
        }
    }

    pub fn emodpe(info: &Secinfo, addr: usize) -> Result<(), u32> {
        unsafe {
            asm!(
                "xchg rbx, {0}",
                "enclu",
                "mov rbx, {0}",
                inout(reg) info => _,
                in("eax") Enclu::EModpe as u32,
                in("rcx") addr,
                options(preserves_flags, nostack),
            );
            Ok(())
        }
    }
}