blob: 1fa6b849f1312134f7e49e9bccb4d80d868e359a [file] [log] [blame]
Alistair Francisbe409182020-03-24 10:54:32 -07001use crate::callback::CallbackSubscription;
2use crate::callback::Consumer;
3use crate::result::TockResult;
4use crate::syscalls;
5use core::marker::PhantomData;
Alistair Francis914ef892020-09-18 08:13:57 -07006use core::ops::Deref;
7use core::ops::DerefMut;
Alistair Francisbe409182020-03-24 10:54:32 -07008use libtock_core::shared_memory::SharedMemory;
9
10const DRIVER_NUMBER: usize = 0x40003;
11
12pub const KEY_BUFFER_SIZE: usize = 32;
13pub const DATA_BUFFER_SIZE: usize = 256;
14pub const DEST_BUFFER_SIZE: usize = 32;
15
16mod command_nr {
17 pub const SET_ALGORITHM: usize = 0;
18 pub const RUN: usize = 1;
19}
20
21mod subscribe_nr {
22 pub const SUBSCRIBE_CALLBACK: usize = 0;
23}
24
25mod 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]
32pub struct HmacDriverFactory;
33
34impl HmacDriverFactory {
35 pub fn init_driver(&mut self) -> TockResult<HmacDriver> {
36 let hmac = HmacDriver {
37 lifetime: PhantomData,
38 };
39 Ok(hmac)
40 }
41}
42
43struct HmacEventConsumer;
44
45impl<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
51pub struct HmacKeyBuffer {
52 buffer: [u8; KEY_BUFFER_SIZE],
53}
54
55impl Default for HmacKeyBuffer {
56 fn default() -> Self {
57 HmacKeyBuffer {
58 buffer: [0; KEY_BUFFER_SIZE],
59 }
60 }
61}
62
Alistair Francis914ef892020-09-18 08:13:57 -070063impl Deref for HmacKeyBuffer {
64 type Target = [u8; KEY_BUFFER_SIZE];
65
66 fn deref(&self) -> &Self::Target {
67 &self.buffer
68 }
69}
70
71impl DerefMut for HmacKeyBuffer {
72 fn deref_mut(&mut self) -> &mut Self::Target {
73 &mut self.buffer
74 }
75}
76
Alistair Francisbe409182020-03-24 10:54:32 -070077pub struct HmacDataBuffer {
Alistair Francis914ef892020-09-18 08:13:57 -070078 buffer: [u8; DATA_BUFFER_SIZE],
Alistair Francisbe409182020-03-24 10:54:32 -070079}
80
81impl Default for HmacDataBuffer {
82 fn default() -> Self {
83 HmacDataBuffer {
84 buffer: [0; DATA_BUFFER_SIZE],
85 }
86 }
87}
88
Alistair Francis914ef892020-09-18 08:13:57 -070089impl Deref for HmacDataBuffer {
90 type Target = [u8; DATA_BUFFER_SIZE];
91
92 fn deref(&self) -> &Self::Target {
93 &self.buffer
94 }
95}
96
97impl DerefMut for HmacDataBuffer {
98 fn deref_mut(&mut self) -> &mut Self::Target {
99 &mut self.buffer
100 }
101}
102
Alistair Francisbe409182020-03-24 10:54:32 -0700103pub struct HmacDestBuffer {
104 buffer: [u8; DEST_BUFFER_SIZE],
105}
106
107impl Default for HmacDestBuffer {
108 fn default() -> Self {
109 HmacDestBuffer {
110 buffer: [0; DEST_BUFFER_SIZE],
111 }
112 }
113}
114
Alistair Francis914ef892020-09-18 08:13:57 -0700115impl Deref for HmacDestBuffer {
116 type Target = [u8; DEST_BUFFER_SIZE];
117
118 fn deref(&self) -> &Self::Target {
119 &self.buffer
120 }
121}
122
123impl DerefMut for HmacDestBuffer {
124 fn deref_mut(&mut self) -> &mut Self::Target {
125 &mut self.buffer
126 }
127}
128
Alistair Francisbe409182020-03-24 10:54:32 -0700129pub struct HmacDriver<'a> {
130 lifetime: PhantomData<&'a ()>,
131}
132
133impl<'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 Why3d43eaa2020-06-19 16:16:18 -0700146 pub fn subscribe<CB: FnMut(usize, usize)>(
Alistair Francisbe409182020-03-24 10:54:32 -0700147 &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}