blob: 97184f735ebdddbc23ba847f1448313d395e664a [file] [log] [blame]
Alistair Francis84bc9552020-03-24 10:54:49 -07001#![no_std]
2
3use core::fmt::Write;
4use libtock::hmac::{HmacDataBuffer, HmacDestBuffer, HmacKeyBuffer};
5use libtock::result::TockResult;
6use libtock::syscalls;
7
8#[libtock::main]
9async fn main() -> TockResult<()> {
10 let mut drivers = libtock::retrieve_drivers()?;
11 let mut console = drivers.console.create_console();
12 writeln!(console, "Starting HMAC example")?;
13 let hmac_driver = drivers.hmac.init_driver()?;
14
15 writeln!(console, "Loading in 0 key")?;
16 let mut key_buffer = HmacKeyBuffer::default();
17 let _key_buffer = hmac_driver.init_key_buffer(&mut key_buffer)?;
18 writeln!(console, " done")?;
19
20 writeln!(console, "Creating data buffer")?;
21 let mut data_buffer = HmacDataBuffer::default();
22 let data: &[u8; 72] =
23 b"A language empowering everyone to build reliable and efficient software.";
24
25 for (i, d) in data.iter().enumerate() {
26 data_buffer.buffer[i] = *d;
27 }
28 let _data_buffer = hmac_driver.init_data_buffer(&mut data_buffer)?;
29 writeln!(console, " done")?;
30
31 writeln!(console, "Creating dest buffer")?;
32 let mut dest_buffer = HmacDestBuffer::default();
33 let dest_buffer = hmac_driver.init_dest_buffer(&mut dest_buffer)?;
34 writeln!(console, " done")?;
35
36 let mut temp_buffer = [0; libtock::hmac::DEST_BUFFER_SIZE];
37
38 writeln!(console, "Setting callback and running")?;
39 let mut callback = |_result, _digest| {
40 writeln!(console, "HMAC Complete, printing digest").unwrap();
41 dest_buffer.read_bytes(&mut temp_buffer[..]);
42
43 for buf in temp_buffer.iter().take(libtock::hmac::DEST_BUFFER_SIZE) {
44 write!(console, "{:x}", *buf).unwrap();
45 }
46 };
47
48 let _subscription = hmac_driver.subscribe(&mut callback)?;
49
50 hmac_driver.run()?;
51
52 loop {
53 unsafe { syscalls::raw::yieldk() };
54 }
55}