Alistair Francis | be40918 | 2020-03-24 10:54:32 -0700 | [diff] [blame] | 1 | use crate::callback::CallbackSubscription; |
| 2 | use crate::callback::Consumer; |
| 3 | use crate::result::TockResult; |
| 4 | use crate::syscalls; |
| 5 | use core::marker::PhantomData; |
Alistair Francis | 914ef89 | 2020-09-18 08:13:57 -0700 | [diff] [blame] | 6 | use core::ops::Deref; |
| 7 | use core::ops::DerefMut; |
Alistair Francis | be40918 | 2020-03-24 10:54:32 -0700 | [diff] [blame] | 8 | use libtock_core::shared_memory::SharedMemory; |
| 9 | |
| 10 | const DRIVER_NUMBER: usize = 0x40003; |
| 11 | |
| 12 | pub const KEY_BUFFER_SIZE: usize = 32; |
| 13 | pub const DATA_BUFFER_SIZE: usize = 256; |
| 14 | pub const DEST_BUFFER_SIZE: usize = 32; |
| 15 | |
| 16 | mod command_nr { |
| 17 | pub const SET_ALGORITHM: usize = 0; |
| 18 | pub const RUN: usize = 1; |
| 19 | } |
| 20 | |
| 21 | mod subscribe_nr { |
| 22 | pub const SUBSCRIBE_CALLBACK: usize = 0; |
| 23 | } |
| 24 | |
| 25 | mod allow_nr { |
| 26 | pub const KEY: usize = 0; |
| 27 | pub const DATA: usize = 1; |
| 28 | pub const DEST: usize = 2; |
| 29 | } |
| 30 | |
| 31 | #[non_exhaustive] |
| 32 | pub struct HmacDriverFactory; |
| 33 | |
| 34 | impl HmacDriverFactory { |
| 35 | pub fn init_driver(&mut self) -> TockResult<HmacDriver> { |
| 36 | let hmac = HmacDriver { |
| 37 | lifetime: PhantomData, |
| 38 | }; |
| 39 | Ok(hmac) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | struct HmacEventConsumer; |
| 44 | |
| 45 | impl<CB: FnMut(usize, usize)> Consumer<CB> for HmacEventConsumer { |
| 46 | fn consume(callback: &mut CB, result: usize, digest: usize, _: usize) { |
| 47 | callback(result, digest); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | pub struct HmacKeyBuffer { |
| 52 | buffer: [u8; KEY_BUFFER_SIZE], |
| 53 | } |
| 54 | |
| 55 | impl Default for HmacKeyBuffer { |
| 56 | fn default() -> Self { |
| 57 | HmacKeyBuffer { |
| 58 | buffer: [0; KEY_BUFFER_SIZE], |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
Alistair Francis | 914ef89 | 2020-09-18 08:13:57 -0700 | [diff] [blame] | 63 | impl Deref for HmacKeyBuffer { |
| 64 | type Target = [u8; KEY_BUFFER_SIZE]; |
| 65 | |
| 66 | fn deref(&self) -> &Self::Target { |
| 67 | &self.buffer |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | impl DerefMut for HmacKeyBuffer { |
| 72 | fn deref_mut(&mut self) -> &mut Self::Target { |
| 73 | &mut self.buffer |
| 74 | } |
| 75 | } |
| 76 | |
Alistair Francis | be40918 | 2020-03-24 10:54:32 -0700 | [diff] [blame] | 77 | pub struct HmacDataBuffer { |
Alistair Francis | 914ef89 | 2020-09-18 08:13:57 -0700 | [diff] [blame] | 78 | buffer: [u8; DATA_BUFFER_SIZE], |
Alistair Francis | be40918 | 2020-03-24 10:54:32 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | impl Default for HmacDataBuffer { |
| 82 | fn default() -> Self { |
| 83 | HmacDataBuffer { |
| 84 | buffer: [0; DATA_BUFFER_SIZE], |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
Alistair Francis | 914ef89 | 2020-09-18 08:13:57 -0700 | [diff] [blame] | 89 | impl Deref for HmacDataBuffer { |
| 90 | type Target = [u8; DATA_BUFFER_SIZE]; |
| 91 | |
| 92 | fn deref(&self) -> &Self::Target { |
| 93 | &self.buffer |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | impl DerefMut for HmacDataBuffer { |
| 98 | fn deref_mut(&mut self) -> &mut Self::Target { |
| 99 | &mut self.buffer |
| 100 | } |
| 101 | } |
| 102 | |
Alistair Francis | be40918 | 2020-03-24 10:54:32 -0700 | [diff] [blame] | 103 | pub struct HmacDestBuffer { |
| 104 | buffer: [u8; DEST_BUFFER_SIZE], |
| 105 | } |
| 106 | |
| 107 | impl Default for HmacDestBuffer { |
| 108 | fn default() -> Self { |
| 109 | HmacDestBuffer { |
| 110 | buffer: [0; DEST_BUFFER_SIZE], |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
Alistair Francis | 914ef89 | 2020-09-18 08:13:57 -0700 | [diff] [blame] | 115 | impl Deref for HmacDestBuffer { |
| 116 | type Target = [u8; DEST_BUFFER_SIZE]; |
| 117 | |
| 118 | fn deref(&self) -> &Self::Target { |
| 119 | &self.buffer |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | impl DerefMut for HmacDestBuffer { |
| 124 | fn deref_mut(&mut self) -> &mut Self::Target { |
| 125 | &mut self.buffer |
| 126 | } |
| 127 | } |
| 128 | |
Alistair Francis | be40918 | 2020-03-24 10:54:32 -0700 | [diff] [blame] | 129 | pub struct HmacDriver<'a> { |
| 130 | lifetime: PhantomData<&'a ()>, |
| 131 | } |
| 132 | |
| 133 | impl<'a> HmacDriver<'a> { |
| 134 | pub fn init_key_buffer(&self, buffer: &'a mut HmacKeyBuffer) -> TockResult<SharedMemory> { |
| 135 | syscalls::allow(DRIVER_NUMBER, allow_nr::KEY, &mut buffer.buffer).map_err(Into::into) |
| 136 | } |
| 137 | |
| 138 | pub fn init_data_buffer(&self, buffer: &'a mut HmacDataBuffer) -> TockResult<SharedMemory> { |
| 139 | syscalls::allow(DRIVER_NUMBER, allow_nr::DATA, &mut buffer.buffer).map_err(Into::into) |
| 140 | } |
| 141 | |
| 142 | pub fn init_dest_buffer(&self, buffer: &'a mut HmacDestBuffer) -> TockResult<SharedMemory> { |
| 143 | syscalls::allow(DRIVER_NUMBER, allow_nr::DEST, &mut buffer.buffer).map_err(Into::into) |
| 144 | } |
| 145 | |
Johnathan Van Why | 3d43eaa | 2020-06-19 16:16:18 -0700 | [diff] [blame] | 146 | pub fn subscribe<CB: FnMut(usize, usize)>( |
Alistair Francis | be40918 | 2020-03-24 10:54:32 -0700 | [diff] [blame] | 147 | &self, |
| 148 | callback: &'a mut CB, |
| 149 | ) -> TockResult<CallbackSubscription> { |
| 150 | syscalls::subscribe::<HmacEventConsumer, _>( |
| 151 | DRIVER_NUMBER, |
| 152 | subscribe_nr::SUBSCRIBE_CALLBACK, |
| 153 | callback, |
| 154 | ) |
| 155 | .map_err(Into::into) |
| 156 | } |
| 157 | |
| 158 | pub fn set_algorithm(&self, hash: usize) -> TockResult<()> { |
| 159 | syscalls::command(DRIVER_NUMBER, command_nr::SET_ALGORITHM, hash, 0)?; |
| 160 | Ok(()) |
| 161 | } |
| 162 | |
| 163 | pub fn run(&self) -> TockResult<()> { |
| 164 | syscalls::command(DRIVER_NUMBER, command_nr::RUN, 0, 0)?; |
| 165 | Ok(()) |
| 166 | } |
| 167 | } |