capsules_extra/tutorials/
encryption_oracle_chkpt2.rs1use 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 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 0 => CommandReturn::success(),
51
52 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 _ => 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}