capsules_extra/tutorials/
encryption_oracle_chkpt1.rs1use kernel::grant::{AllowRoCount, AllowRwCount, Grant, UpcallCount};
6use kernel::syscall::{CommandReturn, SyscallDriver};
7use kernel::ErrorCode;
8use kernel::ProcessId;
9
10pub static KEY: &[u8; kernel::hil::symmetric_encryption::AES128_KEY_SIZE] = b"InsecureAESKey12";
11
12#[derive(Default)]
13pub struct ProcessState {
14    request_pending: bool,
15}
16
17pub struct EncryptionOracleDriver {
18    process_grants: Grant<ProcessState, UpcallCount<0>, AllowRoCount<0>, AllowRwCount<0>>,
19}
20
21impl EncryptionOracleDriver {
22    pub fn new(
24        process_grants: Grant<ProcessState, UpcallCount<0>, AllowRoCount<0>, AllowRwCount<0>>,
25    ) -> Self {
26        EncryptionOracleDriver { process_grants }
27    }
28}
29
30impl SyscallDriver for EncryptionOracleDriver {
31    fn command(
32        &self,
33        command_num: usize,
34        _data1: usize,
35        _data2: usize,
36        processid: ProcessId,
37    ) -> CommandReturn {
38        match command_num {
39            0 => CommandReturn::success(),
41
42            1 => self
44                .process_grants
45                .enter(processid, |grant, _kernel_data| {
46                    grant.request_pending = true;
47                    CommandReturn::success()
48                })
49                .unwrap_or_else(|err| err.into()),
50
51            _ => CommandReturn::failure(ErrorCode::NOSUPPORT),
53        }
54    }
55
56    fn allocate_grant(&self, processid: ProcessId) -> Result<(), kernel::process::Error> {
57        self.process_grants.enter(processid, |_, _| {})
58    }
59}