blob: 51851282852cd461bf43f4c3f8933cb2d1739493 [file] [log] [blame]
use crate::callback::CallbackSubscription;
use crate::callback::Consumer;
use crate::result::TockResult;
use crate::syscalls;
use core::marker::PhantomData;
use libtock_core::shared_memory::SharedMemory;
const DRIVER_NUMBER: usize = 0x40003;
pub const KEY_BUFFER_SIZE: usize = 32;
pub const DATA_BUFFER_SIZE: usize = 256;
pub const DEST_BUFFER_SIZE: usize = 32;
mod command_nr {
pub const SET_ALGORITHM: usize = 0;
pub const RUN: usize = 1;
}
mod subscribe_nr {
pub const SUBSCRIBE_CALLBACK: usize = 0;
}
mod allow_nr {
pub const KEY: usize = 0;
pub const DATA: usize = 1;
pub const DEST: usize = 2;
}
#[non_exhaustive]
pub struct HmacDriverFactory;
impl HmacDriverFactory {
pub fn init_driver(&mut self) -> TockResult<HmacDriver> {
let hmac = HmacDriver {
lifetime: PhantomData,
};
Ok(hmac)
}
}
struct HmacEventConsumer;
impl<CB: FnMut(usize, usize)> Consumer<CB> for HmacEventConsumer {
fn consume(callback: &mut CB, result: usize, digest: usize, _: usize) {
callback(result, digest);
}
}
pub struct HmacKeyBuffer {
buffer: [u8; KEY_BUFFER_SIZE],
}
impl Default for HmacKeyBuffer {
fn default() -> Self {
HmacKeyBuffer {
buffer: [0; KEY_BUFFER_SIZE],
}
}
}
pub struct HmacDataBuffer {
pub buffer: [u8; DATA_BUFFER_SIZE],
}
impl Default for HmacDataBuffer {
fn default() -> Self {
HmacDataBuffer {
buffer: [0; DATA_BUFFER_SIZE],
}
}
}
pub struct HmacDestBuffer {
buffer: [u8; DEST_BUFFER_SIZE],
}
impl Default for HmacDestBuffer {
fn default() -> Self {
HmacDestBuffer {
buffer: [0; DEST_BUFFER_SIZE],
}
}
}
pub struct HmacDriver<'a> {
lifetime: PhantomData<&'a ()>,
}
impl<'a> HmacDriver<'a> {
pub fn init_key_buffer(&self, buffer: &'a mut HmacKeyBuffer) -> TockResult<SharedMemory> {
syscalls::allow(DRIVER_NUMBER, allow_nr::KEY, &mut buffer.buffer).map_err(Into::into)
}
pub fn init_data_buffer(&self, buffer: &'a mut HmacDataBuffer) -> TockResult<SharedMemory> {
syscalls::allow(DRIVER_NUMBER, allow_nr::DATA, &mut buffer.buffer).map_err(Into::into)
}
pub fn init_dest_buffer(&self, buffer: &'a mut HmacDestBuffer) -> TockResult<SharedMemory> {
syscalls::allow(DRIVER_NUMBER, allow_nr::DEST, &mut buffer.buffer).map_err(Into::into)
}
pub fn subscribe<CB: FnMut(usize, usize)>(
&self,
callback: &'a mut CB,
) -> TockResult<CallbackSubscription> {
syscalls::subscribe::<HmacEventConsumer, _>(
DRIVER_NUMBER,
subscribe_nr::SUBSCRIBE_CALLBACK,
callback,
)
.map_err(Into::into)
}
pub fn set_algorithm(&self, hash: usize) -> TockResult<()> {
syscalls::command(DRIVER_NUMBER, command_nr::SET_ALGORITHM, hash, 0)?;
Ok(())
}
pub fn run(&self) -> TockResult<()> {
syscalls::command(DRIVER_NUMBER, command_nr::RUN, 0, 0)?;
Ok(())
}
}