2021-05-18 16:39:03 +00:00
|
|
|
#![cfg_attr(target_os = "none", no_std)]
|
|
|
|
|
#![cfg_attr(target_os = "none", no_main)]
|
|
|
|
|
|
|
|
|
|
mod api;
|
2021-05-19 19:29:26 +00:00
|
|
|
use api::*;
|
|
|
|
|
|
2021-05-18 16:39:03 +00:00
|
|
|
|
|
|
|
|
use num_traits::{ToPrimitive, FromPrimitive};
|
|
|
|
|
use xous_ipc::Buffer;
|
2021-05-19 19:29:26 +00:00
|
|
|
use xous::msg_blocking_scalar_unpack;
|
2021-05-18 16:39:03 +00:00
|
|
|
|
|
|
|
|
use log::info;
|
|
|
|
|
|
|
|
|
|
use core::sync::atomic::{AtomicBool, Ordering};
|
|
|
|
|
|
|
|
|
|
#[cfg(target_os = "none")]
|
|
|
|
|
mod implementation {
|
|
|
|
|
use utralib::generated::*;
|
2021-05-19 19:29:26 +00:00
|
|
|
use crate::api::Sha2Config;
|
2021-05-18 16:39:03 +00:00
|
|
|
|
2021-05-19 19:29:26 +00:00
|
|
|
// Note: there is no susres manager for the Sha512 engine, because its state cannot be saved through a full power off
|
|
|
|
|
// instead, we try to delay a suspend until the caller is finished hashing, and if not, we note that and return a failure
|
|
|
|
|
// for the hash result.
|
2021-05-20 19:15:13 +00:00
|
|
|
pub(crate) struct Engine512 {
|
2021-05-18 16:39:03 +00:00
|
|
|
csr: utralib::CSR<u32>,
|
|
|
|
|
fifo: xous::MemoryRange,
|
2021-05-30 08:18:23 +00:00
|
|
|
#[cfg(feature = "event_wait")]
|
|
|
|
|
done: bool,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Note: in theory, xous::wait_event() should be a more efficient way to do this than yield_slice(),
|
|
|
|
|
as it should resume scheduling immediately upon an interrupt being fired by the sha512 engine, instead
|
|
|
|
|
of just waiting until the next time slice which can be some ms away. However, attempts to use this
|
|
|
|
|
feature seem to indicate there is either a configuration or a hardware problem in triggering the
|
|
|
|
|
interrupts for this block. This is a thing to fix later.
|
|
|
|
|
*/
|
|
|
|
|
#[cfg(feature = "event_wait")]
|
|
|
|
|
fn handle_irq(_irq_no: usize, arg: *mut usize) {
|
|
|
|
|
let engine512 = unsafe { &mut *(arg as *mut Engine512) };
|
|
|
|
|
// note we are done and clear the pending request, as this is used as a "wait" until interrupt mechanism with xous::wait_event()
|
|
|
|
|
engine512.done = true;
|
|
|
|
|
engine512.csr
|
|
|
|
|
.wo(utra::sha512::EV_PENDING, engine512.csr.r(utra::sha512::EV_PENDING));
|
2021-05-18 16:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Engine512 {
|
2021-05-20 19:15:13 +00:00
|
|
|
pub(crate) fn new() -> Engine512 {
|
2021-05-18 16:39:03 +00:00
|
|
|
let csr = xous::syscall::map_memory(
|
|
|
|
|
xous::MemoryAddress::new(utra::sha512::HW_SHA512_BASE),
|
|
|
|
|
None,
|
|
|
|
|
4096,
|
|
|
|
|
xous::MemoryFlags::R | xous::MemoryFlags::W,
|
|
|
|
|
)
|
|
|
|
|
.expect("couldn't map Engine512 CSR range");
|
|
|
|
|
let fifo = xous::syscall::map_memory(
|
|
|
|
|
xous::MemoryAddress::new(utralib::HW_SHA512_MEM),
|
|
|
|
|
None,
|
2021-05-20 19:15:13 +00:00
|
|
|
4096,
|
2021-05-18 16:39:03 +00:00
|
|
|
xous::MemoryFlags::R | xous::MemoryFlags::W,
|
|
|
|
|
)
|
|
|
|
|
.expect("couldn't map Engine512 CSR range");
|
|
|
|
|
|
2021-05-30 08:18:23 +00:00
|
|
|
#[cfg(not(feature = "event_wait"))]
|
2021-05-19 19:29:26 +00:00
|
|
|
let engine512 = Engine512 {
|
2021-05-18 16:39:03 +00:00
|
|
|
csr: CSR::new(csr.as_mut_ptr() as *mut u32),
|
|
|
|
|
fifo,
|
|
|
|
|
};
|
|
|
|
|
|
2021-05-30 08:18:23 +00:00
|
|
|
#[cfg(feature = "event_wait")]
|
|
|
|
|
{
|
|
|
|
|
let mut engine512 = Engine512 {
|
|
|
|
|
csr: CSR::new(csr.as_mut_ptr() as *mut u32),
|
|
|
|
|
fifo,
|
|
|
|
|
done: false,
|
|
|
|
|
};
|
|
|
|
|
xous::claim_interrupt(
|
|
|
|
|
utra::sha512::SHA512_IRQ,
|
|
|
|
|
handle_irq,
|
|
|
|
|
(&mut engine512) as *mut Engine512 as *mut usize,
|
|
|
|
|
)
|
|
|
|
|
.expect("couldn't claim irq");
|
2021-05-30 15:07:16 +00:00
|
|
|
// note: we can't use a "susres" manager here because this block has un-suspendable state
|
|
|
|
|
// instead, we rely on every usage of this mechanism to explicitly set this enable bit before relying upon in
|
2021-05-30 08:18:23 +00:00
|
|
|
engine512.csr.wfo(utra::sha512::EV_ENABLE_SHA512_DONE, 1);
|
|
|
|
|
}
|
2021-05-18 16:39:03 +00:00
|
|
|
engine512
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-20 19:15:13 +00:00
|
|
|
pub(crate) fn setup(&mut self, config: Sha2Config) {
|
2021-05-29 16:12:18 +00:00
|
|
|
self.csr.wfo(utra::sha512::POWER_ON, 1);
|
2021-05-19 19:29:26 +00:00
|
|
|
match config {
|
|
|
|
|
Sha2Config::Sha512 => {
|
|
|
|
|
self.csr.wo(utra::sha512::CONFIG,
|
|
|
|
|
self.csr.ms(utra::sha512::CONFIG_DIGEST_SWAP, 1) |
|
|
|
|
|
self.csr.ms(utra::sha512::CONFIG_ENDIAN_SWAP, 1) |
|
|
|
|
|
self.csr.ms(utra::sha512::CONFIG_SHA_EN, 1)
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
Sha2Config::Sha512Trunc256 => {
|
|
|
|
|
self.csr.wo(utra::sha512::CONFIG,
|
|
|
|
|
self.csr.ms(utra::sha512::CONFIG_DIGEST_SWAP, 1) |
|
|
|
|
|
self.csr.ms(utra::sha512::CONFIG_ENDIAN_SWAP, 1) |
|
|
|
|
|
self.csr.ms(utra::sha512::CONFIG_SHA_EN, 1) |
|
|
|
|
|
self.csr.ms(utra::sha512::CONFIG_SELECT_256, 1)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
self.csr.wfo(utra::sha512::COMMAND_HASH_START, 1);
|
|
|
|
|
self.csr.wfo(utra::sha512::EV_ENABLE_SHA512_DONE, 1);
|
2021-05-29 16:12:18 +00:00
|
|
|
self.csr.wfo(utra::sha512::POWER_ON, 0);
|
2021-05-18 16:39:03 +00:00
|
|
|
}
|
2021-05-19 19:29:26 +00:00
|
|
|
|
2021-05-20 19:15:13 +00:00
|
|
|
pub(crate) fn update(&mut self, buf: &[u8]) {
|
2021-05-29 16:12:18 +00:00
|
|
|
self.csr.wfo(utra::sha512::POWER_ON, 1);
|
2021-05-19 19:29:26 +00:00
|
|
|
let sha = self.fifo.as_mut_ptr() as *mut u32;
|
|
|
|
|
let sha_byte = self.fifo.as_mut_ptr() as *mut u8;
|
|
|
|
|
|
2021-05-21 07:56:58 +00:00
|
|
|
// this unsafe version is very slightly faster than the safe version below
|
|
|
|
|
/*
|
|
|
|
|
let src_bfr = buf.as_ptr() as *mut u32;
|
|
|
|
|
for offset in 0 .. buf.len() / 4 {
|
|
|
|
|
unsafe { sha.write_volatile(src_bfr.add(offset).read_volatile()); }
|
|
|
|
|
}
|
|
|
|
|
if (buf.len() % 4) != 0 {
|
|
|
|
|
for index in (buf.len() - (buf.len() % 4))..buf.len() {
|
|
|
|
|
unsafe{ sha_byte.write_volatile(buf[index]); }
|
|
|
|
|
}
|
|
|
|
|
} */
|
2021-05-19 19:29:26 +00:00
|
|
|
for (_reg, chunk) in buf.chunks(4).enumerate() {
|
|
|
|
|
let mut temp: [u8; 4] = Default::default();
|
|
|
|
|
if chunk.len() == 4 {
|
|
|
|
|
temp.copy_from_slice(chunk);
|
|
|
|
|
let dword: u32 = u32::from_le_bytes(temp);
|
|
|
|
|
|
|
|
|
|
while self.csr.rf(utra::sha512::FIFO_ALMOST_FULL) != 0 {
|
|
|
|
|
xous::yield_slice();
|
|
|
|
|
}
|
|
|
|
|
unsafe { sha.write_volatile(dword); }
|
|
|
|
|
} else {
|
|
|
|
|
for index in 0..chunk.len() {
|
|
|
|
|
while self.csr.rf(utra::sha512::FIFO_ALMOST_FULL) != 0 {
|
|
|
|
|
xous::yield_slice();
|
|
|
|
|
}
|
|
|
|
|
unsafe{ sha_byte.write_volatile(chunk[index]); }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-29 16:12:18 +00:00
|
|
|
self.csr.wfo(utra::sha512::POWER_ON, 0);
|
2021-05-19 19:29:26 +00:00
|
|
|
}
|
|
|
|
|
|
2021-05-20 19:15:13 +00:00
|
|
|
pub(crate) fn finalize(&mut self) -> ([u8; 64], u64) {
|
2021-05-29 16:12:18 +00:00
|
|
|
self.csr.wfo(utra::sha512::POWER_ON, 1);
|
2021-05-30 08:18:23 +00:00
|
|
|
#[cfg(feature = "event_wait")]
|
|
|
|
|
{
|
2021-05-30 15:07:16 +00:00
|
|
|
engine512.csr.wfo(utra::sha512::EV_ENABLE_SHA512_DONE, 1);
|
2021-05-30 08:18:23 +00:00
|
|
|
self.done = false;
|
|
|
|
|
self.csr.wfo(utra::sha512::COMMAND_HASH_PROCESS, 1);
|
|
|
|
|
while !self.done {
|
|
|
|
|
log::trace!("waiting for sha512_done");
|
|
|
|
|
xous::wait_event();
|
|
|
|
|
}
|
|
|
|
|
log::trace!("moving on");
|
|
|
|
|
}
|
|
|
|
|
#[cfg(not(feature = "event_wait"))]
|
|
|
|
|
{
|
|
|
|
|
self.csr.wfo(utra::sha512::COMMAND_HASH_PROCESS, 1);
|
|
|
|
|
while self.csr.rf(utra::sha512::EV_PENDING_SHA512_DONE) == 0 {
|
|
|
|
|
xous::yield_slice();
|
|
|
|
|
}
|
|
|
|
|
self.csr.wfo(utra::sha512::EV_PENDING_SHA512_DONE, 1);
|
2021-05-19 19:29:26 +00:00
|
|
|
}
|
|
|
|
|
let length_in_bits: u64 = (self.csr.r(utra::sha512::MSG_LENGTH0) as u64) | ((self.csr.r(utra::sha512::MSG_LENGTH1) as u64) << 32);
|
|
|
|
|
let mut hash: [u8; 64] = [0; 64];
|
|
|
|
|
let digest_regs: [utralib::Register; 16] = [
|
|
|
|
|
utra::sha512::DIGEST00,
|
|
|
|
|
utra::sha512::DIGEST01,
|
|
|
|
|
utra::sha512::DIGEST10,
|
|
|
|
|
utra::sha512::DIGEST11,
|
|
|
|
|
utra::sha512::DIGEST20,
|
|
|
|
|
utra::sha512::DIGEST21,
|
|
|
|
|
utra::sha512::DIGEST30,
|
|
|
|
|
utra::sha512::DIGEST31,
|
|
|
|
|
utra::sha512::DIGEST40,
|
|
|
|
|
utra::sha512::DIGEST41,
|
|
|
|
|
utra::sha512::DIGEST50,
|
|
|
|
|
utra::sha512::DIGEST51,
|
|
|
|
|
utra::sha512::DIGEST60,
|
|
|
|
|
utra::sha512::DIGEST61,
|
|
|
|
|
utra::sha512::DIGEST70,
|
|
|
|
|
utra::sha512::DIGEST71,
|
|
|
|
|
];
|
|
|
|
|
let mut i = 0;
|
|
|
|
|
for ® in digest_regs.iter() {
|
2021-05-20 19:15:13 +00:00
|
|
|
hash[i..i+4].clone_from_slice(&self.csr.r(reg).to_le_bytes());
|
2021-05-19 19:29:26 +00:00
|
|
|
i += 4;
|
|
|
|
|
}
|
|
|
|
|
self.csr.wo(utra::sha512::CONFIG, 0); // clear all config bits, including EN, which resets the unit
|
|
|
|
|
|
2021-05-29 16:12:18 +00:00
|
|
|
self.csr.wfo(utra::sha512::POWER_ON, 0);
|
2021-05-19 19:29:26 +00:00
|
|
|
(hash, length_in_bits)
|
2021-05-18 16:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
2021-05-20 19:15:13 +00:00
|
|
|
pub(crate) fn reset(&mut self) {
|
2021-05-29 16:12:18 +00:00
|
|
|
self.csr.wfo(utra::sha512::POWER_ON, 1);
|
2021-05-19 19:29:26 +00:00
|
|
|
if self.csr.rf(utra::sha512::FIFO_RUNNING) != 0 {
|
|
|
|
|
// if it's running, call digest, then reset
|
|
|
|
|
let sha = self.fifo.as_mut_ptr() as *mut u32;
|
|
|
|
|
unsafe { sha.write_volatile(0x0); } // stuff a dummy byte, in case the hash was empty
|
2021-05-30 08:18:23 +00:00
|
|
|
#[cfg(feature = "event_wait")]
|
|
|
|
|
{
|
2021-05-30 15:07:16 +00:00
|
|
|
engine512.csr.wfo(utra::sha512::EV_ENABLE_SHA512_DONE, 1);
|
2021-05-30 08:18:23 +00:00
|
|
|
self.done = false;
|
|
|
|
|
self.csr.wfo(utra::sha512::COMMAND_HASH_PROCESS, 1);
|
|
|
|
|
while !self.done {
|
|
|
|
|
log::trace!("waiting for sha512_done (reset)");
|
|
|
|
|
xous::wait_event();
|
|
|
|
|
}
|
|
|
|
|
log::trace!("moving on (reset)");
|
|
|
|
|
}
|
|
|
|
|
#[cfg(not(feature = "event_wait"))]
|
|
|
|
|
{
|
|
|
|
|
self.csr.wfo(utra::sha512::COMMAND_HASH_PROCESS, 1);
|
|
|
|
|
while self.csr.rf(utra::sha512::EV_PENDING_SHA512_DONE) == 0 {
|
|
|
|
|
xous::yield_slice();
|
|
|
|
|
}
|
2021-05-19 19:29:26 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// engine is already stopped, just clear the pending bits and reset the config
|
|
|
|
|
}
|
|
|
|
|
self.csr.wfo(utra::sha512::EV_PENDING_SHA512_DONE, 1);
|
|
|
|
|
self.csr.wo(utra::sha512::CONFIG, 0); // clear all config bits, including EN, which resets the unit
|
2021-05-29 16:12:18 +00:00
|
|
|
self.csr.wfo(utra::sha512::POWER_ON, 0);
|
2021-05-19 19:29:26 +00:00
|
|
|
}
|
|
|
|
|
|
2021-05-29 16:12:18 +00:00
|
|
|
pub(crate) fn is_idle(&mut self) -> bool {
|
|
|
|
|
self.csr.wfo(utra::sha512::POWER_ON, 1);
|
2021-05-19 19:29:26 +00:00
|
|
|
if self.csr.rf(utra::sha512::CONFIG_SHA_EN) == 0 && self.csr.rf(utra::sha512::FIFO_RUNNING) == 0 {
|
2021-05-29 16:12:18 +00:00
|
|
|
self.csr.wfo(utra::sha512::POWER_ON, 0);
|
2021-05-19 19:29:26 +00:00
|
|
|
true
|
|
|
|
|
} else {
|
2021-05-29 16:12:18 +00:00
|
|
|
self.csr.wfo(utra::sha512::POWER_ON, 0);
|
2021-05-19 19:29:26 +00:00
|
|
|
false
|
|
|
|
|
}
|
2021-05-18 16:39:03 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// a stub to try to avoid breaking hosted mode for as long as possible.
|
|
|
|
|
#[cfg(not(target_os = "none"))]
|
|
|
|
|
mod implementation {
|
|
|
|
|
use log::info;
|
2021-06-01 19:45:15 +00:00
|
|
|
use crate::Sha2Config;
|
2021-05-18 16:39:03 +00:00
|
|
|
|
2021-05-20 19:15:13 +00:00
|
|
|
pub(crate) struct Engine512 {
|
2021-05-18 16:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Engine512 {
|
2021-05-20 19:15:13 +00:00
|
|
|
pub(crate) fn new() -> Engine512 {
|
2021-05-18 16:39:03 +00:00
|
|
|
Engine512 {
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-20 19:15:13 +00:00
|
|
|
pub(crate) fn suspend(&self) {
|
2021-05-18 16:39:03 +00:00
|
|
|
}
|
2021-05-20 19:15:13 +00:00
|
|
|
pub(crate) fn resume(&self) {
|
2021-05-18 16:39:03 +00:00
|
|
|
}
|
2021-05-20 19:15:13 +00:00
|
|
|
pub(crate) fn reset(&self) {
|
2021-05-18 16:39:03 +00:00
|
|
|
}
|
2021-06-01 19:45:15 +00:00
|
|
|
pub(crate) fn setup(&mut self, _config: Sha2Config) {
|
|
|
|
|
}
|
|
|
|
|
pub(crate) fn update(&mut self, _buf: &[u8]) {
|
|
|
|
|
}
|
|
|
|
|
pub(crate) fn finalize(&mut self) -> ([u8; 64], u64) {
|
|
|
|
|
([0; 64], 0)
|
|
|
|
|
}
|
|
|
|
|
pub(crate) fn is_idle(&mut self) -> bool {
|
|
|
|
|
false
|
|
|
|
|
}
|
2021-05-18 16:39:03 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static HASH_IN_PROGRESS: AtomicBool = AtomicBool::new(false);
|
2021-05-19 19:29:26 +00:00
|
|
|
static SUSPEND_FAILURE: AtomicBool = AtomicBool::new(false);
|
|
|
|
|
static SUSPEND_PENDING: AtomicBool = AtomicBool::new(false);
|
2021-05-18 16:39:03 +00:00
|
|
|
|
|
|
|
|
fn susres_thread(sid0: usize, sid1: usize, sid2: usize, sid3: usize) {
|
|
|
|
|
let susres_sid = xous::SID::from_u32(sid0 as u32, sid1 as u32, sid2 as u32, sid3 as u32);
|
|
|
|
|
let xns = xous_names::XousNames::new().unwrap();
|
|
|
|
|
|
|
|
|
|
// register a suspend/resume listener
|
|
|
|
|
let sr_cid = xous::connect(susres_sid).expect("couldn't create suspend callback connection");
|
|
|
|
|
let mut susres = susres::Susres::new(&xns, api::SusResOps::SuspendResume as u32, sr_cid).expect("couldn't create suspend/resume object");
|
|
|
|
|
|
|
|
|
|
log::trace!("starting Sha512 suspend/resume manager loop");
|
|
|
|
|
loop {
|
2021-05-19 19:29:26 +00:00
|
|
|
let msg = xous::receive_message(susres_sid).unwrap();
|
2021-05-18 16:39:03 +00:00
|
|
|
log::trace!("Message: {:?}", msg);
|
|
|
|
|
match FromPrimitive::from_usize(msg.body.id()) {
|
|
|
|
|
Some(SusResOps::SuspendResume) => xous::msg_scalar_unpack!(msg, token, _, _, _, {
|
2021-05-19 19:29:26 +00:00
|
|
|
SUSPEND_PENDING.store(true, Ordering::Relaxed);
|
2021-05-18 16:39:03 +00:00
|
|
|
while HASH_IN_PROGRESS.load(Ordering::Relaxed) {
|
|
|
|
|
xous::yield_slice();
|
|
|
|
|
}
|
2021-05-19 19:29:26 +00:00
|
|
|
if susres.suspend_until_resume(token).expect("couldn't execute suspend/resume") == false {
|
|
|
|
|
SUSPEND_FAILURE.store(true, Ordering::Relaxed);
|
|
|
|
|
} else {
|
|
|
|
|
SUSPEND_FAILURE.store(false, Ordering::Relaxed);
|
|
|
|
|
}
|
|
|
|
|
SUSPEND_PENDING.store(false, Ordering::Relaxed);
|
2021-05-18 16:39:03 +00:00
|
|
|
}),
|
|
|
|
|
Some(SusResOps::Quit) => {
|
|
|
|
|
log::info!("Received quit opcode, exiting!");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
None => {
|
|
|
|
|
log::error!("Received unknown opcode: {:?}", msg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
xns.unregister_server(susres_sid).unwrap();
|
|
|
|
|
xous::destroy_server(susres_sid).unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[xous::xous_main]
|
|
|
|
|
fn xmain() -> ! {
|
|
|
|
|
use crate::implementation::Engine512;
|
|
|
|
|
|
|
|
|
|
log_server::init_wait().unwrap();
|
2021-05-30 08:18:23 +00:00
|
|
|
log::set_max_level(log::LevelFilter::Trace);
|
2021-05-18 16:39:03 +00:00
|
|
|
info!("my PID is {}", xous::process::id());
|
|
|
|
|
|
|
|
|
|
let xns = xous_names::XousNames::new().unwrap();
|
|
|
|
|
let engine512_sid = xns.register_name(api::SERVER_NAME_SHA512).expect("can't register server");
|
|
|
|
|
log::trace!("registered with NS -- {:?}", engine512_sid);
|
|
|
|
|
|
|
|
|
|
let mut engine512 = Engine512::new();
|
|
|
|
|
|
|
|
|
|
log::trace!("ready to accept requests");
|
|
|
|
|
|
|
|
|
|
// handle suspend/resume with a separate thread, which monitors our in-progress state
|
|
|
|
|
// we can't save hardware state of a hash, so the hash MUST finish before we can suspend.
|
|
|
|
|
let susres_mgr_sid = xous::create_server().unwrap();
|
|
|
|
|
let (sid0, sid1, sid2, sid3) = susres_mgr_sid.to_u32();
|
|
|
|
|
xous::create_thread_4(susres_thread, sid0 as usize, sid1 as usize, sid2 as usize, sid3 as usize).expect("couldn't start susres handler thread");
|
|
|
|
|
|
|
|
|
|
let mut client_id: Option<[u32; 3]> = None;
|
|
|
|
|
let mut mode: Option<Sha2Config> = None;
|
|
|
|
|
|
|
|
|
|
loop {
|
2021-05-19 19:29:26 +00:00
|
|
|
let mut msg = xous::receive_message(engine512_sid).unwrap();
|
2021-05-18 16:39:03 +00:00
|
|
|
match FromPrimitive::from_usize(msg.body.id()) {
|
|
|
|
|
Some(Opcode::AcquireExclusive) => msg_blocking_scalar_unpack!(msg, id0, id1, id2, flags, {
|
2021-05-19 19:29:26 +00:00
|
|
|
if client_id.is_none() && !SUSPEND_PENDING.load(Ordering::Relaxed) {
|
2021-05-18 16:39:03 +00:00
|
|
|
client_id = Some([id0 as u32, id1 as u32, id2 as u32]);
|
2021-05-20 20:35:01 +00:00
|
|
|
//log::trace!("giving {:x?} an exclusive lock", client_id);
|
2021-05-18 16:39:03 +00:00
|
|
|
mode = Some(FromPrimitive::from_usize(flags).unwrap());
|
2021-05-19 19:29:26 +00:00
|
|
|
SUSPEND_FAILURE.store(false, Ordering::Relaxed);
|
2021-05-18 16:39:03 +00:00
|
|
|
HASH_IN_PROGRESS.store(true, Ordering::Relaxed);
|
2021-05-19 19:29:26 +00:00
|
|
|
engine512.setup(mode.unwrap());
|
|
|
|
|
xous::return_scalar(msg.sender, 1).unwrap();
|
2021-05-18 16:39:03 +00:00
|
|
|
} else {
|
2021-05-19 19:29:26 +00:00
|
|
|
xous::return_scalar(msg.sender, 0).unwrap();
|
2021-05-18 16:39:03 +00:00
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
Some(Opcode::Reset) => msg_blocking_scalar_unpack!(msg, r_id0, r_id1, r_id2, _, {
|
|
|
|
|
match client_id {
|
|
|
|
|
Some([id0, id1, id2]) => {
|
|
|
|
|
if id0 == r_id0 as u32 && id1 == r_id1 as u32 && id2 == r_id2 as u32 {
|
2021-05-19 19:29:26 +00:00
|
|
|
SUSPEND_FAILURE.store(false, Ordering::Relaxed);
|
|
|
|
|
HASH_IN_PROGRESS.store(false, Ordering::Relaxed);
|
2021-05-18 16:39:03 +00:00
|
|
|
client_id = None;
|
|
|
|
|
mode = None;
|
|
|
|
|
engine512.reset();
|
2021-05-19 19:29:26 +00:00
|
|
|
xous::return_scalar(msg.sender, 1).unwrap();
|
2021-05-18 16:39:03 +00:00
|
|
|
} else {
|
2021-05-19 19:29:26 +00:00
|
|
|
xous::return_scalar(msg.sender, 0).unwrap();
|
2021-05-18 16:39:03 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_ => {
|
2021-05-19 19:29:26 +00:00
|
|
|
xous::return_scalar(msg.sender, 0).unwrap();
|
2021-05-18 16:39:03 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}),
|
2021-05-19 19:29:26 +00:00
|
|
|
Some(Opcode::Update) => {
|
|
|
|
|
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
|
|
|
|
|
let update = buffer.as_flat::<Sha2Update, _>().unwrap();
|
|
|
|
|
match client_id {
|
|
|
|
|
Some(id) => {
|
|
|
|
|
if id == update.id {
|
|
|
|
|
engine512.update(&update.buffer[..update.len as usize]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_ => {
|
|
|
|
|
log::error!("Received a SHA-2 block, but the client ID did not match! Ignoring block.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Some(Opcode::Finalize) => {
|
|
|
|
|
let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
|
|
|
|
|
let mut finalized = buffer.to_original::<Sha2Finalize, _>().unwrap();
|
|
|
|
|
match client_id {
|
|
|
|
|
Some(id) => {
|
|
|
|
|
if id == finalized.id {
|
|
|
|
|
if SUSPEND_FAILURE.load(Ordering::Relaxed) {
|
|
|
|
|
finalized.result = Sha2Result::SuspendError;
|
|
|
|
|
finalized.length_in_bits = None;
|
|
|
|
|
} else {
|
|
|
|
|
let (hash, length_in_bits) = engine512.finalize();
|
|
|
|
|
match mode {
|
|
|
|
|
Some(Sha2Config::Sha512) => {
|
|
|
|
|
finalized.result = Sha2Result::Sha512Result(hash);
|
|
|
|
|
finalized.length_in_bits = Some(length_in_bits);
|
|
|
|
|
},
|
|
|
|
|
Some(Sha2Config::Sha512Trunc256) => {
|
|
|
|
|
let mut trunc: [u8; 32] = [0; 32];
|
|
|
|
|
trunc.clone_from_slice(&hash[..32]);
|
|
|
|
|
finalized.result = Sha2Result::Sha512Trunc256Result(trunc);
|
|
|
|
|
finalized.length_in_bits = Some(length_in_bits);
|
|
|
|
|
},
|
|
|
|
|
None => {
|
|
|
|
|
finalized.result = Sha2Result::Uninitialized;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
finalized.result = Sha2Result::IdMismatch;
|
|
|
|
|
finalized.length_in_bits = None;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_ => {
|
|
|
|
|
log::error!("Received a SHA-2 finalize call, but we aren't doing a hash. Ignoring.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
buffer.replace(finalized).expect("couldn't return hash result");
|
|
|
|
|
}
|
|
|
|
|
Some(Opcode::IsIdle) => msg_blocking_scalar_unpack!(msg, _, _, _, _, {
|
|
|
|
|
if engine512.is_idle() {
|
|
|
|
|
xous::return_scalar(msg.sender, 1).expect("couldn't return IsIdle query");
|
|
|
|
|
} else {
|
|
|
|
|
xous::return_scalar(msg.sender, 0).expect("couldn't return IsIdle query");
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
Some(Opcode::AcquireSuspendLock) => msg_blocking_scalar_unpack!(msg, _, _, _, _, {
|
|
|
|
|
if client_id.is_none() {
|
|
|
|
|
SUSPEND_PENDING.store(true, Ordering::Relaxed);
|
|
|
|
|
xous::return_scalar(msg.sender, 1).expect("couldn't ack AcquireSuspendLock");
|
|
|
|
|
} else {
|
|
|
|
|
xous::return_scalar(msg.sender, 0).expect("couldn't ack AcquireSuspendLock");
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
Some(Opcode::AbortSuspendLock) => msg_blocking_scalar_unpack!(msg, _, _, _, _, {
|
|
|
|
|
SUSPEND_PENDING.store(false, Ordering::Relaxed);
|
|
|
|
|
xous::return_scalar(msg.sender, 1).expect("couldn't ack AbortSuspendLock");
|
|
|
|
|
}),
|
2021-05-18 16:39:03 +00:00
|
|
|
Some(Opcode::Quit) => {
|
|
|
|
|
log::info!("Received quit opcode, exiting!");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
None => {
|
|
|
|
|
log::error!("couldn't convert opcode");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// clean up our program
|
|
|
|
|
log::trace!("main loop exit, destroying servers");
|
|
|
|
|
let quitconn = xous::connect(susres_mgr_sid).unwrap();
|
|
|
|
|
xous::send_message(quitconn, xous::Message::new_scalar(SusResOps::Quit.to_usize().unwrap(), 0, 0, 0, 0)).unwrap();
|
2021-05-19 19:29:26 +00:00
|
|
|
unsafe{xous::disconnect(quitconn).unwrap();}
|
2021-05-18 16:39:03 +00:00
|
|
|
|
|
|
|
|
xns.unregister_server(engine512_sid).unwrap();
|
|
|
|
|
xous::destroy_server(engine512_sid).unwrap();
|
|
|
|
|
log::trace!("quitting");
|
2021-06-01 19:59:25 +00:00
|
|
|
xous::terminate_process(0)
|
2021-05-18 16:39:03 +00:00
|
|
|
}
|