capsules_extra/tutorials/
encryption_oracle_chkpt2.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::hil::symmetric_encryption::{AES128Ctr, AES128};
7use kernel::syscall::{CommandReturn, SyscallDriver};
8use kernel::ErrorCode;
9use kernel::ProcessId;
10
11pub const DRIVER_NUM: usize = 0x99999;
12
13pub static KEY: &[u8; kernel::hil::symmetric_encryption::AES128_KEY_SIZE] = b"InsecureAESKey12";
14
15#[derive(Default)]
16pub struct ProcessState {
17    request_pending: bool,
18}
19
20pub struct EncryptionOracleDriver<'a, A: AES128<'a> + AES128Ctr> {
21    aes: &'a A,
22    process_grants: Grant<ProcessState, UpcallCount<0>, AllowRoCount<0>, AllowRwCount<0>>,
23}
24
25impl<'a, A: AES128<'a> + AES128Ctr> EncryptionOracleDriver<'a, A> {
26    /// Create a new instance of our encryption oracle userspace driver:
27    pub fn new(
28        aes: &'a A,
29        _source_buffer: &'static mut [u8],
30        _dest_buffer: &'static mut [u8],
31        process_grants: Grant<ProcessState, UpcallCount<0>, AllowRoCount<0>, AllowRwCount<0>>,
32    ) -> Self {
33        EncryptionOracleDriver {
34            aes,
35            process_grants,
36        }
37    }
38}
39
40impl<'a, A: AES128<'a> + AES128Ctr> SyscallDriver for EncryptionOracleDriver<'a, A> {
41    fn command(
42        &self,
43        command_num: usize,
44        _data1: usize,
45        _data2: usize,
46        processid: ProcessId,
47    ) -> CommandReturn {
48        match command_num {
49            // Check whether the driver is present:
50            0 => CommandReturn::success(),
51
52            // Request the decryption operation:
53            1 => self
54                .process_grants
55                .enter(processid, |grant, _kernel_data| {
56                    grant.request_pending = true;
57                    CommandReturn::success()
58                })
59                .unwrap_or_else(|err| err.into()),
60
61            // Unknown command number, return a NOSUPPORT error
62            _ => CommandReturn::failure(ErrorCode::NOSUPPORT),
63        }
64    }
65
66    fn allocate_grant(&self, processid: ProcessId) -> Result<(), kernel::process::Error> {
67        self.process_grants.enter(processid, |_, _| {})
68    }
69}