From 1cec36d038b1232f19c5d9c4a82a1c52ae56be8e Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 14 Jan 2024 22:10:28 +0800 Subject: [PATCH] format services/engine-sha512 --- services/engine-sha512/src/api.rs | 5 +- services/engine-sha512/src/api/rkyv_enum.rs | 2 +- services/engine-sha512/src/consts.rs | 10 +- services/engine-sha512/src/lib.rs | 5 +- services/engine-sha512/src/main.rs | 172 ++++++++---------- services/engine-sha512/src/sha256.rs | 48 ++--- services/engine-sha512/src/sha256/soft.rs | 74 ++------ services/engine-sha512/src/sha512.rs | 127 +++++-------- services/engine-sha512/src/sha512/soft.rs | 53 ++---- services/engine-sha512/src/sha512_hosted.rs | 79 ++------ .../engine-sha512/src/sha512_hosted/soft.rs | 53 ++---- 11 files changed, 196 insertions(+), 432 deletions(-) diff --git a/services/engine-sha512/src/api.rs b/services/engine-sha512/src/api.rs index cd7c02f96..1d24b8b2d 100644 --- a/services/engine-sha512/src/api.rs +++ b/services/engine-sha512/src/api.rs @@ -19,8 +19,9 @@ pub(crate) enum Sha2Config { #[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] pub(crate) struct Sha2Update { pub id: [u32; 3], // our unique identifier so the server knows who the request is coming from - pub buffer: [u8; 3968], // leave one SHA chunk-sized space for overhead, so the whole message fits in one page of memory - pub len: u16, // length of just this buffer, fits in 16 bits + pub buffer: [u8; 3968], /* leave one SHA chunk-sized space for overhead, so the whole message fits in one + * page of memory */ + pub len: u16, // length of just this buffer, fits in 16 bits } #[allow(dead_code)] diff --git a/services/engine-sha512/src/api/rkyv_enum.rs b/services/engine-sha512/src/api/rkyv_enum.rs index e5ebbe054..182f03e8c 100644 --- a/services/engine-sha512/src/api/rkyv_enum.rs +++ b/services/engine-sha512/src/api/rkyv_enum.rs @@ -19,4 +19,4 @@ pub enum Sha2Result { SuspendError, Uninitialized, IdMismatch, -} \ No newline at end of file +} diff --git a/services/engine-sha512/src/consts.rs b/services/engine-sha512/src/consts.rs index b26a0f4bc..bb93becbd 100644 --- a/services/engine-sha512/src/consts.rs +++ b/services/engine-sha512/src/consts.rs @@ -168,13 +168,11 @@ pub const K64X2: [[u64; 2]; 40] = [ [K64[79], K64[78]], ]; -pub static H224: [u32; STATE_LEN] = [ - 0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4, -]; +pub static H224: [u32; STATE_LEN] = + [0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4]; -pub static H256: [u32; STATE_LEN] = [ - 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19, -]; +pub static H256: [u32; STATE_LEN] = + [0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19]; pub static H384: [u64; STATE_LEN] = [ 0xcbbb9d5dc1059ed8, diff --git a/services/engine-sha512/src/lib.rs b/services/engine-sha512/src/lib.rs index 31c30dfe3..d13ff637a 100644 --- a/services/engine-sha512/src/lib.rs +++ b/services/engine-sha512/src/lib.rs @@ -9,9 +9,8 @@ mod sha256; mod sha512; #[cfg(not(target_os = "xous"))] mod sha512_hosted; -#[cfg(not(target_os = "xous"))] -use sha512_hosted as sha512; - pub use digest::{self, Digest}; pub use sha256::{Sha224, Sha256}; pub use sha512::{Sha384, Sha512, Sha512Trunc224, Sha512Trunc256}; +#[cfg(not(target_os = "xous"))] +use sha512_hosted as sha512; diff --git a/services/engine-sha512/src/main.rs b/services/engine-sha512/src/main.rs index 8492cc192..61d1a36ad 100644 --- a/services/engine-sha512/src/main.rs +++ b/services/engine-sha512/src/main.rs @@ -4,28 +4,27 @@ #![cfg_attr(target_os = "none", no_main)] mod api; -use api::*; +use core::sync::atomic::{AtomicBool, Ordering}; +use api::*; +use log::info; use num_traits::{FromPrimitive, ToPrimitive}; use xous::msg_blocking_scalar_unpack; use xous_ipc::Buffer; -use log::info; - -use core::sync::atomic::{AtomicBool, Ordering}; - -#[cfg(any(feature="precursor", feature="renode"))] +#[cfg(any(feature = "precursor", feature = "renode"))] mod implementation { - use crate::api::Sha2Config; - use utralib::generated::*; - #[cfg(feature = "event_wait")] - use crate::api::Opcode; #[cfg(feature = "event_wait")] use num_traits::*; + use utralib::generated::*; - // 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. + #[cfg(feature = "event_wait")] + use crate::api::Opcode; + use crate::api::Sha2Config; + + // 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. pub(crate) struct Engine512 { csr: utralib::CSR, fifo: xous::MemoryRange, @@ -43,19 +42,21 @@ mod implementation { #[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() - // this message may not be strictly necessary but we added it in an attempt to get the wait_event() to pick up the - // interrupt; unfortunately, the interrupt completes before wait_event() finishes processing. + // note we are done and clear the pending request, as this is used as a "wait" until interrupt + // mechanism with xous::wait_event() this message may not be strictly necessary but we added + // it in an attempt to get the wait_event() to pick up the interrupt; unfortunately, the + // interrupt completes before wait_event() finishes processing. if let Some(conn) = engine512.handler_conn { - xous::try_send_message(conn, - xous::Message::new_scalar(Opcode::IrqEvent.to_usize().unwrap(), 0, 0, 0, 0)).map(|_|()).unwrap(); + xous::try_send_message( + conn, + xous::Message::new_scalar(Opcode::IrqEvent.to_usize().unwrap(), 0, 0, 0, 0), + ) + .map(|_| ()) + .unwrap(); } else { log::error!("|handle_event_irq: COM interrupt, but no connection for notification!") } - engine512.csr.wo( - utra::sha512::EV_PENDING, - engine512.csr.r(utra::sha512::EV_PENDING), - ); + engine512.csr.wo(utra::sha512::EV_PENDING, engine512.csr.r(utra::sha512::EV_PENDING)); } impl Engine512 { @@ -76,30 +77,21 @@ mod implementation { .expect("couldn't map Engine512 CSR range"); #[cfg(not(feature = "event_wait"))] - let engine512 = Engine512 { - csr: CSR::new(csr.as_mut_ptr() as *mut u32), - fifo, - }; + let engine512 = Engine512 { csr: CSR::new(csr.as_mut_ptr() as *mut u32), fifo }; #[cfg(feature = "event_wait")] - let engine512 = Engine512 { - csr: CSR::new(csr.as_mut_ptr() as *mut u32), - fifo, - handler_conn: _handler_conn, - }; + let engine512 = + Engine512 { csr: CSR::new(csr.as_mut_ptr() as *mut u32), fifo, handler_conn: _handler_conn }; engine512 } - pub (crate) fn init(&mut self) { + pub(crate) fn init(&mut self) { #[cfg(feature = "event_wait")] - xous::claim_interrupt( - utra::sha512::SHA512_IRQ, - handle_irq, - self as *mut Engine512 as *mut usize, - ) - .expect("couldn't claim irq"); + xous::claim_interrupt(utra::sha512::SHA512_IRQ, handle_irq, self as *mut Engine512 as *mut usize) + .expect("couldn't claim irq"); // 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 it + // instead, we rely on every usage of this mechanism to explicitly set this enable bit before + // relying upon it #[cfg(feature = "event_wait")] self.csr.wfo(utra::sha512::EV_ENABLE_SHA512_DONE, 1); @@ -183,17 +175,15 @@ mod implementation { self.csr.wfo(utra::sha512::POWER_ON, 1); #[cfg(feature = "event_wait")] { - self.csr.wo( - utra::sha512::EV_PENDING, - self.csr.r(utra::sha512::EV_PENDING), - ); + self.csr.wo(utra::sha512::EV_PENDING, self.csr.r(utra::sha512::EV_PENDING)); self.csr.wfo(utra::sha512::EV_ENABLE_SHA512_DONE, 1); log::trace!("starting sha512_done"); self.csr.wfo(utra::sha512::COMMAND_HASH_PROCESS, 1); while self.csr.rf(utra::sha512::FIFO_RUNNING) == 1 { - // race condition between when this kicks off, and when the IRQ event comes in; in the end, - // the overhead of wait_event() is longer than it takes for the computation to finish, - // so the interrupt arrives *before* this call happens. + // race condition between when this kicks off, and when the IRQ event comes in; in the + // end, the overhead of wait_event() is longer than it takes for the + // computation to finish, so the interrupt arrives *before* this call + // happens. xous::wait_event(); } log::info!("moving on"); @@ -202,7 +192,8 @@ mod implementation { { self.csr.wfo(utra::sha512::COMMAND_HASH_PROCESS, 1); while self.csr.rf(utra::sha512::EV_PENDING_SHA512_DONE) == 0 { - // don't even call the OS idle, the computation is 2us and the syscall takes longer than that. + // don't even call the OS idle, the computation is 2us and the syscall takes longer than + // that. } self.csr.wfo(utra::sha512::EV_PENDING_SHA512_DONE, 1); } @@ -249,9 +240,7 @@ mod implementation { pub(crate) fn is_idle(&mut self) -> bool { self.csr.wfo(utra::sha512::POWER_ON, 1); - if self.csr.rf(utra::sha512::CONFIG_SHA_EN) == 0 - && self.csr.rf(utra::sha512::FIFO_RUNNING) == 0 - { + if self.csr.rf(utra::sha512::CONFIG_SHA_EN) == 0 && self.csr.rf(utra::sha512::FIFO_RUNNING) == 0 { self.csr.wfo(utra::sha512::POWER_ON, 0); true } else { @@ -265,27 +254,30 @@ mod implementation { // a stub to try to avoid breaking hosted mode for as long as possible. #[cfg(not(target_os = "xous"))] mod implementation { - use crate::Sha2Config; use log::info; + use crate::Sha2Config; + pub(crate) struct Engine512 {} impl Engine512 { - pub(crate) fn new(_handler_conn: Option) -> Engine512 { - Engine512 {} - } + pub(crate) fn new(_handler_conn: Option) -> Engine512 { Engine512 {} } + pub(crate) fn init(&mut self) {} + pub(crate) fn suspend(&self) {} + pub(crate) fn resume(&self) {} + pub(crate) fn reset(&self) {} + 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 - } + + pub(crate) fn finalize(&mut self) -> ([u8; 64], u64) { ([0; 64], 0) } + + pub(crate) fn is_idle(&mut self) -> bool { false } } } @@ -299,8 +291,13 @@ fn susres_thread(sid0: usize, sid1: usize, sid2: usize, sid3: usize) { // 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(Some(susres::SuspendOrder::Late), &xns, api::SusResOps::SuspendResume as u32, sr_cid) - .expect("couldn't create suspend/resume object"); + let mut susres = susres::Susres::new( + Some(susres::SuspendOrder::Late), + &xns, + api::SusResOps::SuspendResume as u32, + sr_cid, + ) + .expect("couldn't create suspend/resume object"); log::trace!("starting Sha512 suspend/resume manager loop"); loop { @@ -312,11 +309,7 @@ fn susres_thread(sid0: usize, sid1: usize, sid2: usize, sid3: usize) { while HASH_IN_PROGRESS.load(Ordering::Relaxed) { xous::yield_slice(); } - if susres - .suspend_until_resume(token) - .expect("couldn't execute suspend/resume") - == false - { + 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); @@ -345,14 +338,12 @@ fn main() -> ! { let xns = xous_names::XousNames::new().unwrap(); // anyone is allowed to connect to this service; authentication by tokens used - let engine512_sid = xns - .register_name(api::SERVER_NAME_SHA512, None) - .expect("can't register server"); + let engine512_sid = xns.register_name(api::SERVER_NAME_SHA512, None).expect("can't register server"); log::trace!("registered with NS -- {:?}", engine512_sid); - #[cfg(feature="event_wait")] + #[cfg(feature = "event_wait")] let mut engine512 = Box::new(Engine512::new(xous::connect(engine512_sid).ok())); - #[cfg(not(feature="event_wait"))] + #[cfg(not(feature = "event_wait"))] let mut engine512 = Box::new(Engine512::new(None)); engine512.init(); @@ -362,14 +353,8 @@ fn main() -> ! { // 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"); + 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 = None; @@ -412,8 +397,7 @@ fn main() -> ! { } }), Some(Opcode::Update) => { - let buffer = - unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; + let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; let update = buffer.as_flat::().unwrap(); match client_id { Some(id) => { @@ -422,7 +406,9 @@ fn main() -> ! { } } _ => { - log::error!("Received a SHA-2 block, but the client ID did not match! Ignoring block."); + log::error!( + "Received a SHA-2 block, but the client ID did not match! Ignoring block." + ); } } } @@ -431,9 +417,8 @@ fn main() -> ! { log::info!("sha512 job {}", job_count); // leave this here for now so we can confirm HW accel is being used when we think it is! } job_count += 1; - let mut buffer = unsafe { - Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) - }; + let mut buffer = + unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; let mut finalized = buffer.to_original::().unwrap(); match client_id { Some(id) => { @@ -465,14 +450,10 @@ fn main() -> ! { } } _ => { - log::error!( - "Received a SHA-2 finalize call, but we aren't doing a hash. Ignoring." - ); + 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"); + buffer.replace(finalized).expect("couldn't return hash result"); } Some(Opcode::IsIdle) => msg_blocking_scalar_unpack!(msg, _, _, _, _, { if engine512.is_idle() { @@ -510,11 +491,8 @@ fn main() -> ! { // 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(); + xous::send_message(quitconn, xous::Message::new_scalar(SusResOps::Quit.to_usize().unwrap(), 0, 0, 0, 0)) + .unwrap(); unsafe { xous::disconnect(quitconn).unwrap(); } diff --git a/services/engine-sha512/src/sha256.rs b/services/engine-sha512/src/sha256.rs index a423510f3..a49a2d3f1 100644 --- a/services/engine-sha512/src/sha256.rs +++ b/services/engine-sha512/src/sha256.rs @@ -1,13 +1,14 @@ //! SHA-256 mod soft; -use soft::compress; - -use crate::consts::{H224, H256, STATE_LEN}; -use block_buffer::BlockBuffer; use core::slice::from_ref; + +use block_buffer::BlockBuffer; use digest::consts::{U28, U32, U64}; use digest::generic_array::GenericArray; use digest::{BlockInput, FixedOutputDirty, Reset, Update}; +use soft::compress; + +use crate::consts::{H224, H256, STATE_LEN}; type BlockSize = U64; @@ -21,13 +22,7 @@ struct Engine256 { } impl Engine256 { - fn new(h: &[u32; STATE_LEN]) -> Engine256 { - Engine256 { - len: 0, - buffer: Default::default(), - state: *h, - } - } + fn new(h: &[u32; STATE_LEN]) -> Engine256 { Engine256 { len: 0, buffer: Default::default(), state: *h } } fn update(&mut self, input: &[u8]) { // Assumes that input.len() can be converted to u64 without overflow @@ -39,8 +34,7 @@ impl Engine256 { fn finish(&mut self) { let s = &mut self.state; let l = self.len; - self.buffer - .len64_padding_be(l, |b| compress256(s, from_ref(b))); + self.buffer.len64_padding_be(l, |b| compress256(s, from_ref(b))); } fn reset(&mut self, h: &[u32; STATE_LEN]) { @@ -57,11 +51,7 @@ pub struct Sha256 { } impl Default for Sha256 { - fn default() -> Self { - Sha256 { - engine: Engine256::new(&H256), - } - } + fn default() -> Self { Sha256 { engine: Engine256::new(&H256) } } } impl BlockInput for Sha256 { @@ -69,9 +59,7 @@ impl BlockInput for Sha256 { } impl Update for Sha256 { - fn update(&mut self, input: impl AsRef<[u8]>) { - self.engine.update(input.as_ref()); - } + fn update(&mut self, input: impl AsRef<[u8]>) { self.engine.update(input.as_ref()); } } impl FixedOutputDirty for Sha256 { @@ -87,9 +75,7 @@ impl FixedOutputDirty for Sha256 { } impl Reset for Sha256 { - fn reset(&mut self) { - self.engine.reset(&H256); - } + fn reset(&mut self) { self.engine.reset(&H256); } } /// The SHA-256 hash algorithm with the SHA-224 initial hash value. The result @@ -100,11 +86,7 @@ pub struct Sha224 { } impl Default for Sha224 { - fn default() -> Self { - Sha224 { - engine: Engine256::new(&H224), - } - } + fn default() -> Self { Sha224 { engine: Engine256::new(&H224) } } } impl BlockInput for Sha224 { @@ -112,9 +94,7 @@ impl BlockInput for Sha224 { } impl Update for Sha224 { - fn update(&mut self, input: impl AsRef<[u8]>) { - self.engine.update(input.as_ref()); - } + fn update(&mut self, input: impl AsRef<[u8]>) { self.engine.update(input.as_ref()); } } impl FixedOutputDirty for Sha224 { @@ -130,9 +110,7 @@ impl FixedOutputDirty for Sha224 { } impl Reset for Sha224 { - fn reset(&mut self) { - self.engine.reset(&H224); - } + fn reset(&mut self) { self.engine.reset(&H224); } } opaque_debug::implement!(Sha224); diff --git a/services/engine-sha512/src/sha256/soft.rs b/services/engine-sha512/src/sha256/soft.rs index 0ab1f2b17..d37c9582a 100644 --- a/services/engine-sha512/src/sha256/soft.rs +++ b/services/engine-sha512/src/sha256/soft.rs @@ -1,44 +1,28 @@ #![allow(clippy::many_single_char_names)] -use crate::consts::BLOCK_LEN; use core::convert::TryInto; -#[inline(always)] -fn shl(v: [u32; 4], o: u32) -> [u32; 4] { - [v[0] >> o, v[1] >> o, v[2] >> o, v[3] >> o] -} +use crate::consts::BLOCK_LEN; #[inline(always)] -fn shr(v: [u32; 4], o: u32) -> [u32; 4] { - [v[0] << o, v[1] << o, v[2] << o, v[3] << o] -} +fn shl(v: [u32; 4], o: u32) -> [u32; 4] { [v[0] >> o, v[1] >> o, v[2] >> o, v[3] >> o] } #[inline(always)] -fn or(a: [u32; 4], b: [u32; 4]) -> [u32; 4] { - [a[0] | b[0], a[1] | b[1], a[2] | b[2], a[3] | b[3]] -} +fn shr(v: [u32; 4], o: u32) -> [u32; 4] { [v[0] << o, v[1] << o, v[2] << o, v[3] << o] } #[inline(always)] -fn xor(a: [u32; 4], b: [u32; 4]) -> [u32; 4] { - [a[0] ^ b[0], a[1] ^ b[1], a[2] ^ b[2], a[3] ^ b[3]] -} +fn or(a: [u32; 4], b: [u32; 4]) -> [u32; 4] { [a[0] | b[0], a[1] | b[1], a[2] | b[2], a[3] | b[3]] } + +#[inline(always)] +fn xor(a: [u32; 4], b: [u32; 4]) -> [u32; 4] { [a[0] ^ b[0], a[1] ^ b[1], a[2] ^ b[2], a[3] ^ b[3]] } #[inline(always)] fn add(a: [u32; 4], b: [u32; 4]) -> [u32; 4] { - [ - a[0].wrapping_add(b[0]), - a[1].wrapping_add(b[1]), - a[2].wrapping_add(b[2]), - a[3].wrapping_add(b[3]), - ] + [a[0].wrapping_add(b[0]), a[1].wrapping_add(b[1]), a[2].wrapping_add(b[2]), a[3].wrapping_add(b[3])] } -fn sha256load(v2: [u32; 4], v3: [u32; 4]) -> [u32; 4] { - [v3[3], v2[0], v2[1], v2[2]] -} +fn sha256load(v2: [u32; 4], v3: [u32; 4]) -> [u32; 4] { [v3[3], v2[0], v2[1], v2[2]] } -fn sha256swap(v0: [u32; 4]) -> [u32; 4] { - [v0[2], v0[3], v0[0], v0[1]] -} +fn sha256swap(v0: [u32; 4]) -> [u32; 4] { [v0[2], v0[3], v0[0], v0[1]] } #[inline(never)] // bunnie: reduce binary size fn sha256msg1(v0: [u32; 4], v1: [u32; 4]) -> [u32; 4] { // sigma 0 on vectors @@ -98,38 +82,14 @@ fn sha256_digest_round_x2(cdgh: [u32; 4], abef: [u32; 4], wk: [u32; 4]) -> [u32; let [c0, d0, g0, h0] = cdgh; // a round - let x0 = big_sigma1!(e0) - .wrapping_add(bool3ary_202!(e0, f0, g0)) - .wrapping_add(wk0) - .wrapping_add(h0); + let x0 = big_sigma1!(e0).wrapping_add(bool3ary_202!(e0, f0, g0)).wrapping_add(wk0).wrapping_add(h0); let y0 = big_sigma0!(a0).wrapping_add(bool3ary_232!(a0, b0, c0)); - let (a1, b1, c1, d1, e1, f1, g1, h1) = ( - x0.wrapping_add(y0), - a0, - b0, - c0, - x0.wrapping_add(d0), - e0, - f0, - g0, - ); + let (a1, b1, c1, d1, e1, f1, g1, h1) = (x0.wrapping_add(y0), a0, b0, c0, x0.wrapping_add(d0), e0, f0, g0); // a round - let x1 = big_sigma1!(e1) - .wrapping_add(bool3ary_202!(e1, f1, g1)) - .wrapping_add(wk1) - .wrapping_add(h1); + let x1 = big_sigma1!(e1).wrapping_add(bool3ary_202!(e1, f1, g1)).wrapping_add(wk1).wrapping_add(h1); let y1 = big_sigma0!(a1).wrapping_add(bool3ary_232!(a1, b1, c1)); - let (a2, b2, _, _, e2, f2, _, _) = ( - x1.wrapping_add(y1), - a1, - b1, - c1, - x1.wrapping_add(d1), - e1, - f1, - g1, - ); + let (a2, b2, _, _, e2, f2, _, _) = (x1.wrapping_add(y1), a1, b1, c1, x1.wrapping_add(d1), e1, f1, g1); [a2, b2, e2, f2] } @@ -151,11 +111,7 @@ macro_rules! rounds4 { } macro_rules! schedule_rounds4 { - ( - $abef:ident, $cdgh:ident, - $w0:expr, $w1:expr, $w2:expr, $w3:expr, $w4:expr, - $i: expr - ) => {{ + ($abef:ident, $cdgh:ident, $w0:expr, $w1:expr, $w2:expr, $w3:expr, $w4:expr, $i:expr) => {{ $w4 = schedule($w0, $w1, $w2, $w3); rounds4!($abef, $cdgh, $w4, $i); }}; diff --git a/services/engine-sha512/src/sha512.rs b/services/engine-sha512/src/sha512.rs index 6a8f0a619..0776c28ca 100644 --- a/services/engine-sha512/src/sha512.rs +++ b/services/engine-sha512/src/sha512.rs @@ -1,14 +1,14 @@ //! SHA-512 mod soft; -use crate::consts::*; -use soft::compress; +use core::sync::atomic::{AtomicU32, Ordering}; -use crate::api::*; use num_traits::ToPrimitive; +use soft::compress; use xous::{send_message, Message}; use xous_ipc::Buffer; -use core::sync::atomic::{AtomicU32, Ordering}; +use crate::api::*; +use crate::consts::*; /// we have to make the HW_CONN static because the Digest crate assumes you can clone objects /// and recycle them. However, it's not a problem for every server to have a unique connection /// to the hasher service, if that's what it comes down to. The burden for tracking connections is on the @@ -18,8 +18,9 @@ static HW_CONN: AtomicU32 = AtomicU32::new(0); /// a unique-enough random ID number to prove we own our connection to the hashing engine hardware static TOKEN: [AtomicU32; 3] = [AtomicU32::new(0), AtomicU32::new(0), AtomicU32::new(0)]; -use block_buffer::BlockBuffer; use core::slice::from_ref; + +use block_buffer::BlockBuffer; use digest::consts::{U128, U28, U32, U48, U64}; use digest::generic_array::GenericArray; use digest::{BlockInput, FixedOutputDirty, Reset, Update}; @@ -39,13 +40,7 @@ struct Engine512 { } impl Engine512 { - fn new(h: &[u64; STATE_LEN]) -> Engine512 { - Engine512 { - len: 0, - buffer: Default::default(), - state: *h, - } - } + fn new(h: &[u64; STATE_LEN]) -> Engine512 { Engine512 { len: 0, buffer: Default::default(), state: *h } } fn update(&mut self, input: &[u8]) { self.len += (input.len() as u128) << 3; @@ -55,8 +50,7 @@ impl Engine512 { fn finish(&mut self) { let s = &mut self.state; - self.buffer - .len128_padding_be(self.len, |d| compress512(s, from_ref(d))); + self.buffer.len128_padding_be(self.len, |d| compress512(s, from_ref(d))); } fn reset(&mut self, h: &[u64; crate::consts::STATE_LEN]) { @@ -95,11 +89,7 @@ macro_rules! sha512_comms { ) .expect("Couldn't make IsIdle query"); if let xous::Result::Scalar1(result) = response { - if result != 0 { - Ok(true) - } else { - Ok(false) - } + if result != 0 { Ok(true) } else { Ok(false) } } else { Err(xous::Error::InternalError) } @@ -107,36 +97,21 @@ macro_rules! sha512_comms { pub fn acquire_suspend_lock(&self) -> Result { let response = send_message( self.ensure_conn(), - Message::new_blocking_scalar( - Opcode::AcquireSuspendLock.to_usize().unwrap(), - 0, - 0, - 0, - 0, - ), + Message::new_blocking_scalar(Opcode::AcquireSuspendLock.to_usize().unwrap(), 0, 0, 0, 0), ) .expect("Couldn't issue AcquireSuspendLock message"); if let xous::Result::Scalar1(result) = response { - if result != 0 { - Ok(true) - } else { - Ok(false) - } + if result != 0 { Ok(true) } else { Ok(false) } } else { Err(xous::Error::InternalError) } } pub fn abort_suspend(&self) -> Result<(), xous::Error> { - // we ignore the result and just turn it into () once we get anything back, as abort_suspend "can't fail" + // we ignore the result and just turn it into () once we get anything back, as abort_suspend + // "can't fail" send_message( self.ensure_conn(), - Message::new_blocking_scalar( - Opcode::AbortSuspendLock.to_usize().unwrap(), - 0, - 0, - 0, - 0, - ), + Message::new_blocking_scalar(Opcode::AbortSuspendLock.to_usize().unwrap(), 0, 0, 0, 0), ) .map(|_| ()) } @@ -215,6 +190,9 @@ pub struct Sha512 { length: u64, } impl Sha512 { + // make all the boilerplate comms code shared between all sizes of digest + sha512_comms!(); + // use this function instead of default for more control over configuration of the hardware engine pub fn new() -> Self { Sha512 { @@ -225,6 +203,7 @@ impl Sha512 { length: 0, } } + pub fn new_with_strategy(strat: FallbackStrategy) -> Self { Sha512 { use_soft: false, @@ -234,8 +213,6 @@ impl Sha512 { length: 0, } } - // make all the boilerplate comms code shared between all sizes of digest - sha512_comms!(); } impl Default for Sha512 { @@ -268,7 +245,8 @@ impl Update for Sha512 { self.engine.update(input.as_ref()); } else { for chunk in input.as_ref().chunks(3968) { - // one SHA512 block (128 bytes) short of 4096 to give space for struct overhead in page remap handling + // one SHA512 block (128 bytes) short of 4096 to give space for struct overhead in page remap + // handling let mut update = Sha2Update { id: [ TOKEN[0].load(Ordering::Relaxed), @@ -311,19 +289,15 @@ impl FixedOutputDirty for Sha512 { result: Sha2Result::Uninitialized, length_in_bits: None, }; - let mut buf = - Buffer::into_buf(result).expect("couldn't map memory for the return buffer"); - buf.lend_mut(self.ensure_conn(), Opcode::Finalize.to_u32().unwrap()) - .expect("couldn't finalize"); + let mut buf = Buffer::into_buf(result).expect("couldn't map memory for the return buffer"); + buf.lend_mut(self.ensure_conn(), Opcode::Finalize.to_u32().unwrap()).expect("couldn't finalize"); let returned: Sha2Finalize = buf.to_original().expect("couldn't decode return buffer"); match returned.result { Sha2Result::Sha512Result(s) => { log::debug!("bits hashed: {}", self.length); if self.length - != returned - .length_in_bits - .expect("hardware did not return a length field!") + != returned.length_in_bits.expect("hardware did not return a length field!") { panic!("Sha512 hardware did not hash as many bits as we had expected!") } @@ -377,6 +351,9 @@ pub struct Sha512Trunc256 { length: u64, } impl Sha512Trunc256 { + // make all the boilerplate comms code shared between all sizes of digest + sha512_comms!(); + // use this function instead of default for more control over configuration of the hardware engine pub fn new() -> Self { Sha512Trunc256 { @@ -387,6 +364,7 @@ impl Sha512Trunc256 { length: 0, } } + pub fn new_with_strategy(strat: FallbackStrategy) -> Self { Sha512Trunc256 { use_soft: false, @@ -396,8 +374,6 @@ impl Sha512Trunc256 { length: 0, } } - // make all the boilerplate comms code shared between all sizes of digest - sha512_comms!(); } impl Drop for Sha512Trunc256 { @@ -409,9 +385,7 @@ impl Drop for Sha512Trunc256 { } impl Default for Sha512Trunc256 { - fn default() -> Self { - Sha512Trunc256::new_with_strategy(FallbackStrategy::HardwareThenSoftware) - } + fn default() -> Self { Sha512Trunc256::new_with_strategy(FallbackStrategy::HardwareThenSoftware) } } impl BlockInput for Sha512Trunc256 { @@ -425,7 +399,8 @@ impl Update for Sha512Trunc256 { self.engine.update(input.as_ref()); } else { for chunk in input.as_ref().chunks(3968) { - // one SHA512 block (128 bytes) short of 4096 to give space for struct overhead in page remap handling + // one SHA512 block (128 bytes) short of 4096 to give space for struct overhead in page remap + // handling let mut update = Sha2Update { id: [ TOKEN[0].load(Ordering::Relaxed), @@ -468,18 +443,14 @@ impl FixedOutputDirty for Sha512Trunc256 { result: Sha2Result::Uninitialized, length_in_bits: None, }; - let mut buf = - Buffer::into_buf(result).expect("couldn't map memory for the return buffer"); - buf.lend_mut(self.ensure_conn(), Opcode::Finalize.to_u32().unwrap()) - .expect("couldn't finalize"); + let mut buf = Buffer::into_buf(result).expect("couldn't map memory for the return buffer"); + buf.lend_mut(self.ensure_conn(), Opcode::Finalize.to_u32().unwrap()).expect("couldn't finalize"); let returned: Sha2Finalize = buf.to_original().expect("couldn't decode return buffer"); match returned.result { Sha2Result::Sha512Trunc256Result(s) => { if self.length - != returned - .length_in_bits - .expect("hardware did not return a length field!") + != returned.length_in_bits.expect("hardware did not return a length field!") { panic!("Sha512 hardware did not hash as many bits as we had expected!") } @@ -515,8 +486,8 @@ impl Reset for Sha512Trunc256 { } ////////////////////////////////////////////// -//////////////// the following modes are software-only emulation, as we currently do not have the constants in hardware to support these. -////////////////////////////////////////////// +//////////////// the following modes are software-only emulation, as we currently do not have the constants in +//////////////// hardware to support these. ////////////////////////////// /// The SHA-512 hash algorithm with the SHA-384 initial hash value. The result /// is truncated to 384 bits. @@ -526,11 +497,7 @@ pub struct Sha384 { } impl Default for Sha384 { - fn default() -> Self { - Sha384 { - engine: Engine512::new(&H384), - } - } + fn default() -> Self { Sha384 { engine: Engine512::new(&H384) } } } impl BlockInput for Sha384 { @@ -538,9 +505,7 @@ impl BlockInput for Sha384 { } impl Update for Sha384 { - fn update(&mut self, input: impl AsRef<[u8]>) { - self.engine.update(input.as_ref()); - } + fn update(&mut self, input: impl AsRef<[u8]>) { self.engine.update(input.as_ref()); } } impl FixedOutputDirty for Sha384 { @@ -556,9 +521,7 @@ impl FixedOutputDirty for Sha384 { } impl Reset for Sha384 { - fn reset(&mut self) { - self.engine.reset(&H384); - } + fn reset(&mut self) { self.engine.reset(&H384); } } /// The SHA-512 hash algorithm with the SHA-512/224 initial hash value. @@ -569,11 +532,7 @@ pub struct Sha512Trunc224 { } impl Default for Sha512Trunc224 { - fn default() -> Self { - Sha512Trunc224 { - engine: Engine512::new(&H512_TRUNC_224), - } - } + fn default() -> Self { Sha512Trunc224 { engine: Engine512::new(&H512_TRUNC_224) } } } impl BlockInput for Sha512Trunc224 { @@ -581,9 +540,7 @@ impl BlockInput for Sha512Trunc224 { } impl Update for Sha512Trunc224 { - fn update(&mut self, input: impl AsRef<[u8]>) { - self.engine.update(input.as_ref()); - } + fn update(&mut self, input: impl AsRef<[u8]>) { self.engine.update(input.as_ref()); } } impl FixedOutputDirty for Sha512Trunc224 { @@ -600,9 +557,7 @@ impl FixedOutputDirty for Sha512Trunc224 { } impl Reset for Sha512Trunc224 { - fn reset(&mut self) { - self.engine.reset(&H512_TRUNC_224); - } + fn reset(&mut self) { self.engine.reset(&H512_TRUNC_224); } } opaque_debug::implement!(Sha384); diff --git a/services/engine-sha512/src/sha512/soft.rs b/services/engine-sha512/src/sha512/soft.rs index 7cead514f..2d4ef8bd9 100644 --- a/services/engine-sha512/src/sha512/soft.rs +++ b/services/engine-sha512/src/sha512/soft.rs @@ -4,57 +4,38 @@ License is Apache 2.0 */ #![allow(clippy::many_single_char_names)] -use crate::consts::{BLOCK_LEN, K64X2}; use core::convert::TryInto; -fn add(a: [u64; 2], b: [u64; 2]) -> [u64; 2] { - [a[0].wrapping_add(b[0]), a[1].wrapping_add(b[1])] -} +use crate::consts::{BLOCK_LEN, K64X2}; + +fn add(a: [u64; 2], b: [u64; 2]) -> [u64; 2] { [a[0].wrapping_add(b[0]), a[1].wrapping_add(b[1])] } /// Not an intrinsic, but works like an unaligned load. -fn sha512load(v0: [u64; 2], v1: [u64; 2]) -> [u64; 2] { - [v1[1], v0[0]] -} +fn sha512load(v0: [u64; 2], v1: [u64; 2]) -> [u64; 2] { [v1[1], v0[0]] } /// Performs 2 rounds of the SHA-512 message schedule update. #[inline(never)] // bunnie: reduce binary size pub fn sha512_schedule_x2(v0: [u64; 2], v1: [u64; 2], v4to5: [u64; 2], v7: [u64; 2]) -> [u64; 2] { // sigma 0 - fn sigma0(x: u64) -> u64 { - ((x << 63) | (x >> 1)) ^ ((x << 56) | (x >> 8)) ^ (x >> 7) - } + fn sigma0(x: u64) -> u64 { ((x << 63) | (x >> 1)) ^ ((x << 56) | (x >> 8)) ^ (x >> 7) } // sigma 1 - fn sigma1(x: u64) -> u64 { - ((x << 45) | (x >> 19)) ^ ((x << 3) | (x >> 61)) ^ (x >> 6) - } + fn sigma1(x: u64) -> u64 { ((x << 45) | (x >> 19)) ^ ((x << 3) | (x >> 61)) ^ (x >> 6) } let [w1, w0] = v0; let [_, w2] = v1; let [w10, w9] = v4to5; let [w15, w14] = v7; - let w16 = sigma1(w14) - .wrapping_add(w9) - .wrapping_add(sigma0(w1)) - .wrapping_add(w0); - let w17 = sigma1(w15) - .wrapping_add(w10) - .wrapping_add(sigma0(w2)) - .wrapping_add(w1); + let w16 = sigma1(w14).wrapping_add(w9).wrapping_add(sigma0(w1)).wrapping_add(w0); + let w17 = sigma1(w15).wrapping_add(w10).wrapping_add(sigma0(w2)).wrapping_add(w1); [w17, w16] } /// Performs one round of the SHA-512 message block digest. #[inline(never)] // bunnie: reduce binary size -pub fn sha512_digest_round( - ae: [u64; 2], - bf: [u64; 2], - cg: [u64; 2], - dh: [u64; 2], - wk0: u64, -) -> [u64; 2] { +pub fn sha512_digest_round(ae: [u64; 2], bf: [u64; 2], cg: [u64; 2], dh: [u64; 2], wk0: u64) -> [u64; 2] { macro_rules! big_sigma0 { ($a:expr) => { ($a.rotate_right(28) ^ $a.rotate_right(34) ^ $a.rotate_right(39)) @@ -82,21 +63,9 @@ pub fn sha512_digest_round( let [d0, h0] = dh; // a round - let x0 = big_sigma1!(e0) - .wrapping_add(bool3ary_202!(e0, f0, g0)) - .wrapping_add(wk0) - .wrapping_add(h0); + let x0 = big_sigma1!(e0).wrapping_add(bool3ary_202!(e0, f0, g0)).wrapping_add(wk0).wrapping_add(h0); let y0 = big_sigma0!(a0).wrapping_add(bool3ary_232!(a0, b0, c0)); - let (a1, _, _, _, e1, _, _, _) = ( - x0.wrapping_add(y0), - a0, - b0, - c0, - x0.wrapping_add(d0), - e0, - f0, - g0, - ); + let (a1, _, _, _, e1, _, _, _) = (x0.wrapping_add(y0), a0, b0, c0, x0.wrapping_add(d0), e0, f0, g0); [a1, e1] } diff --git a/services/engine-sha512/src/sha512_hosted.rs b/services/engine-sha512/src/sha512_hosted.rs index d9e034760..ef2cbdd2f 100644 --- a/services/engine-sha512/src/sha512_hosted.rs +++ b/services/engine-sha512/src/sha512_hosted.rs @@ -1,12 +1,14 @@ //! SHA-512 -use crate::consts::{H384, H512, H512_TRUNC_224, H512_TRUNC_256, STATE_LEN}; -use crate::FallbackStrategy; -use block_buffer::BlockBuffer; use core::slice::from_ref; + +use block_buffer::BlockBuffer; use digest::consts::{U128, U28, U32, U48, U64}; use digest::generic_array::GenericArray; use digest::{BlockInput, FixedOutputDirty, Reset, Update}; +use crate::consts::{H384, H512, H512_TRUNC_224, H512_TRUNC_256, STATE_LEN}; +use crate::FallbackStrategy; + type BlockSize = U128; /// Structure that keeps state of the Sha-512 operation and @@ -19,13 +21,7 @@ struct Engine512 { } impl Engine512 { - fn new(h: &[u64; STATE_LEN]) -> Engine512 { - Engine512 { - len: 0, - buffer: Default::default(), - state: *h, - } - } + fn new(h: &[u64; STATE_LEN]) -> Engine512 { Engine512 { len: 0, buffer: Default::default(), state: *h } } fn update(&mut self, input: &[u8]) { self.len += (input.len() as u128) << 3; @@ -35,8 +31,7 @@ impl Engine512 { fn finish(&mut self) { let s = &mut self.state; - self.buffer - .len128_padding_be(self.len, |d| compress512(s, from_ref(d))); + self.buffer.len128_padding_be(self.len, |d| compress512(s, from_ref(d))); } fn reset(&mut self, h: &[u64; STATE_LEN]) { @@ -53,11 +48,7 @@ pub struct Sha512 { } impl Default for Sha512 { - fn default() -> Self { - Sha512 { - engine: Engine512::new(&H512), - } - } + fn default() -> Self { Sha512 { engine: Engine512::new(&H512) } } } impl BlockInput for Sha512 { @@ -65,9 +56,7 @@ impl BlockInput for Sha512 { } impl Update for Sha512 { - fn update(&mut self, input: impl AsRef<[u8]>) { - self.engine.update(input.as_ref()); - } + fn update(&mut self, input: impl AsRef<[u8]>) { self.engine.update(input.as_ref()); } } impl FixedOutputDirty for Sha512 { @@ -83,9 +72,7 @@ impl FixedOutputDirty for Sha512 { } impl Reset for Sha512 { - fn reset(&mut self) { - self.engine.reset(&H512); - } + fn reset(&mut self) { self.engine.reset(&H512); } } /// The SHA-512 hash algorithm with the SHA-384 initial hash value. The result @@ -96,11 +83,7 @@ pub struct Sha384 { } impl Default for Sha384 { - fn default() -> Self { - Sha384 { - engine: Engine512::new(&H384), - } - } + fn default() -> Self { Sha384 { engine: Engine512::new(&H384) } } } impl BlockInput for Sha384 { @@ -108,9 +91,7 @@ impl BlockInput for Sha384 { } impl Update for Sha384 { - fn update(&mut self, input: impl AsRef<[u8]>) { - self.engine.update(input.as_ref()); - } + fn update(&mut self, input: impl AsRef<[u8]>) { self.engine.update(input.as_ref()); } } impl FixedOutputDirty for Sha384 { @@ -126,9 +107,7 @@ impl FixedOutputDirty for Sha384 { } impl Reset for Sha384 { - fn reset(&mut self) { - self.engine.reset(&H384); - } + fn reset(&mut self) { self.engine.reset(&H384); } } /// The SHA-512 hash algorithm with the SHA-512/256 initial hash value. The @@ -139,17 +118,11 @@ pub struct Sha512Trunc256 { } impl Sha512Trunc256 { pub fn new_with_strategy(_strat: FallbackStrategy) -> Self { - Sha512Trunc256 { - engine: Engine512::new(&H512_TRUNC_256), - } + Sha512Trunc256 { engine: Engine512::new(&H512_TRUNC_256) } } } impl Default for Sha512Trunc256 { - fn default() -> Self { - Sha512Trunc256 { - engine: Engine512::new(&H512_TRUNC_256), - } - } + fn default() -> Self { Sha512Trunc256 { engine: Engine512::new(&H512_TRUNC_256) } } } impl BlockInput for Sha512Trunc256 { @@ -157,9 +130,7 @@ impl BlockInput for Sha512Trunc256 { } impl Update for Sha512Trunc256 { - fn update(&mut self, input: impl AsRef<[u8]>) { - self.engine.update(input.as_ref()); - } + fn update(&mut self, input: impl AsRef<[u8]>) { self.engine.update(input.as_ref()); } } impl FixedOutputDirty for Sha512Trunc256 { @@ -175,9 +146,7 @@ impl FixedOutputDirty for Sha512Trunc256 { } impl Reset for Sha512Trunc256 { - fn reset(&mut self) { - self.engine.reset(&H512_TRUNC_256); - } + fn reset(&mut self) { self.engine.reset(&H512_TRUNC_256); } } /// The SHA-512 hash algorithm with the SHA-512/224 initial hash value. @@ -188,11 +157,7 @@ pub struct Sha512Trunc224 { } impl Default for Sha512Trunc224 { - fn default() -> Self { - Sha512Trunc224 { - engine: Engine512::new(&H512_TRUNC_224), - } - } + fn default() -> Self { Sha512Trunc224 { engine: Engine512::new(&H512_TRUNC_224) } } } impl BlockInput for Sha512Trunc224 { @@ -200,9 +165,7 @@ impl BlockInput for Sha512Trunc224 { } impl Update for Sha512Trunc224 { - fn update(&mut self, input: impl AsRef<[u8]>) { - self.engine.update(input.as_ref()); - } + fn update(&mut self, input: impl AsRef<[u8]>) { self.engine.update(input.as_ref()); } } impl FixedOutputDirty for Sha512Trunc224 { @@ -219,9 +182,7 @@ impl FixedOutputDirty for Sha512Trunc224 { } impl Reset for Sha512Trunc224 { - fn reset(&mut self) { - self.engine.reset(&H512_TRUNC_224); - } + fn reset(&mut self) { self.engine.reset(&H512_TRUNC_224); } } opaque_debug::implement!(Sha384); diff --git a/services/engine-sha512/src/sha512_hosted/soft.rs b/services/engine-sha512/src/sha512_hosted/soft.rs index ab6d56831..50d4d1e1f 100644 --- a/services/engine-sha512/src/sha512_hosted/soft.rs +++ b/services/engine-sha512/src/sha512_hosted/soft.rs @@ -1,53 +1,34 @@ #![allow(clippy::many_single_char_names)] -use crate::consts::{BLOCK_LEN, K64X2}; use core::convert::TryInto; -fn add(a: [u64; 2], b: [u64; 2]) -> [u64; 2] { - [a[0].wrapping_add(b[0]), a[1].wrapping_add(b[1])] -} +use crate::consts::{BLOCK_LEN, K64X2}; + +fn add(a: [u64; 2], b: [u64; 2]) -> [u64; 2] { [a[0].wrapping_add(b[0]), a[1].wrapping_add(b[1])] } /// Not an intrinsic, but works like an unaligned load. -fn sha512load(v0: [u64; 2], v1: [u64; 2]) -> [u64; 2] { - [v1[1], v0[0]] -} +fn sha512load(v0: [u64; 2], v1: [u64; 2]) -> [u64; 2] { [v1[1], v0[0]] } /// Performs 2 rounds of the SHA-512 message schedule update. pub fn sha512_schedule_x2(v0: [u64; 2], v1: [u64; 2], v4to5: [u64; 2], v7: [u64; 2]) -> [u64; 2] { // sigma 0 - fn sigma0(x: u64) -> u64 { - ((x << 63) | (x >> 1)) ^ ((x << 56) | (x >> 8)) ^ (x >> 7) - } + fn sigma0(x: u64) -> u64 { ((x << 63) | (x >> 1)) ^ ((x << 56) | (x >> 8)) ^ (x >> 7) } // sigma 1 - fn sigma1(x: u64) -> u64 { - ((x << 45) | (x >> 19)) ^ ((x << 3) | (x >> 61)) ^ (x >> 6) - } + fn sigma1(x: u64) -> u64 { ((x << 45) | (x >> 19)) ^ ((x << 3) | (x >> 61)) ^ (x >> 6) } let [w1, w0] = v0; let [_, w2] = v1; let [w10, w9] = v4to5; let [w15, w14] = v7; - let w16 = sigma1(w14) - .wrapping_add(w9) - .wrapping_add(sigma0(w1)) - .wrapping_add(w0); - let w17 = sigma1(w15) - .wrapping_add(w10) - .wrapping_add(sigma0(w2)) - .wrapping_add(w1); + let w16 = sigma1(w14).wrapping_add(w9).wrapping_add(sigma0(w1)).wrapping_add(w0); + let w17 = sigma1(w15).wrapping_add(w10).wrapping_add(sigma0(w2)).wrapping_add(w1); [w17, w16] } /// Performs one round of the SHA-512 message block digest. -pub fn sha512_digest_round( - ae: [u64; 2], - bf: [u64; 2], - cg: [u64; 2], - dh: [u64; 2], - wk0: u64, -) -> [u64; 2] { +pub fn sha512_digest_round(ae: [u64; 2], bf: [u64; 2], cg: [u64; 2], dh: [u64; 2], wk0: u64) -> [u64; 2] { macro_rules! big_sigma0 { ($a:expr) => { ($a.rotate_right(28) ^ $a.rotate_right(34) ^ $a.rotate_right(39)) @@ -75,21 +56,9 @@ pub fn sha512_digest_round( let [d0, h0] = dh; // a round - let x0 = big_sigma1!(e0) - .wrapping_add(bool3ary_202!(e0, f0, g0)) - .wrapping_add(wk0) - .wrapping_add(h0); + let x0 = big_sigma1!(e0).wrapping_add(bool3ary_202!(e0, f0, g0)).wrapping_add(wk0).wrapping_add(h0); let y0 = big_sigma0!(a0).wrapping_add(bool3ary_232!(a0, b0, c0)); - let (a1, _, _, _, e1, _, _, _) = ( - x0.wrapping_add(y0), - a0, - b0, - c0, - x0.wrapping_add(d0), - e0, - f0, - g0, - ); + let (a1, _, _, _, e1, _, _, _) = (x0.wrapping_add(y0), a0, b0, c0, x0.wrapping_add(d0), e0, f0, g0); [a1, e1] }