From fb0efbce3b9e4bec988ecdf5d9c1140e62354ab6 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 21 Nov 2024 02:19:21 +0800 Subject: [PATCH] add helper type for pre-hash digests --- loader/src/platform/cramium/sha512_digest.rs | 85 ++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 loader/src/platform/cramium/sha512_digest.rs diff --git a/loader/src/platform/cramium/sha512_digest.rs b/loader/src/platform/cramium/sha512_digest.rs new file mode 100644 index 000000000..8a2730f5a --- /dev/null +++ b/loader/src/platform/cramium/sha512_digest.rs @@ -0,0 +1,85 @@ +use core::fmt; + +use digest::{ + FixedOutput, HashMarker, InvalidOutputSize, Output, Update, + block_buffer::Eager, + core_api::{ + AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, OutputSizeUser, TruncSide, UpdateCore, + VariableOutputCore, + }, + typenum::{U64, U128}, +}; + +/// Wrap a pre-hash value in a Digest trait +#[derive(Clone)] +pub struct Sha512Prehash { + /// track the length of the message processed so far + hash: Option<[u8; 64]>, +} +impl Sha512Prehash { + // use this function instead of default for more control over configuration of the hardware engine + pub fn new() -> Self { Sha512Prehash { hash: None } } + + #[allow(dead_code)] // not used in hosted mode + pub fn set_prehash(&mut self, hash: [u8; 64]) { self.hash = Some(hash); } +} +impl Default for Sha512Prehash { + fn default() -> Self { Sha512Prehash::new() } +} + +impl HashMarker for Sha512Prehash {} + +impl BlockSizeUser for Sha512Prehash { + type BlockSize = U128; +} + +impl BufferKindUser for Sha512Prehash { + type BufferKind = Eager; +} + +impl OutputSizeUser for Sha512Prehash { + type OutputSize = U64; +} + +impl AlgorithmName for Sha512Prehash { + #[inline] + fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str("Sha512") } +} + +impl fmt::Debug for Sha512Prehash { + #[inline] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str("Sha512Prehash { ... }") } +} + +impl UpdateCore for Sha512Prehash { + fn update_blocks(&mut self, _blocks: &[Block]) { + panic!("Prehash implementation can't take block updates"); + } +} + +impl VariableOutputCore for Sha512Prehash { + const TRUNC_SIDE: TruncSide = TruncSide::Left; + + fn new(_output_size: usize) -> Result { Ok(Self { hash: None }) } + + fn finalize_variable_core(&mut self, _buffer: &mut Buffer, out: &mut Output) { + for (dest, &src) in out.chunks_exact_mut(1).zip(self.hash.unwrap().iter()) { + dest.copy_from_slice(&[src]) + } + } +} + +impl Update for Sha512Prehash { + /// Update state using the provided data. + fn update(&mut self, _data: &[u8]) { + panic!("Prehash implementation can't take block updates"); + } +} +impl FixedOutput for Sha512Prehash { + /// Consume value and write result into provided array. + fn finalize_into(self, out: &mut Output) { + for (dest, &src) in out.chunks_exact_mut(1).zip(self.hash.unwrap().iter()) { + dest.copy_from_slice(&[src]) + } + } +}