capsules_extra/tutorials/
encryption_oracle_chkpt1.rs

1// Licensed under the Apache License, Version 2.0 or the MIT License.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// Copyright Tock Contributors 2022.
4
5use 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    /// Create a new instance of our encryption oracle userspace driver:
23    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            // Check whether the driver is present:
40            0 => CommandReturn::success(),
41
42            // Request the decryption operation:
43            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            // Unknown command number, return a NOSUPPORT error
52            _ => 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}