Skip to content

Commit

Permalink
remove all remaining allows
Browse files Browse the repository at this point in the history
  • Loading branch information
playfulFence committed Aug 22, 2024
1 parent fccabc3 commit 9aae9c6
Show file tree
Hide file tree
Showing 7 changed files with 514 additions and 44 deletions.
2 changes: 1 addition & 1 deletion esp-hal/src/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ mod peripheral_macros {
#[derive(Debug)]
#[allow(non_camel_case_types)]
/// Represents a virtual peripheral with no associated hardware.
///
///
/// This struct is generated by the `create_peripheral!` macro when the peripheral
/// is defined as virtual.
pub struct $name { _inner: () }
Expand Down
72 changes: 60 additions & 12 deletions esp-hal/src/sha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@
//! ## Implementation State
//! - DMA-SHA Mode is not supported.
#![allow(missing_docs)] // TODO: Remove when able

use core::{convert::Infallible, marker::PhantomData};

/// Re-export digest for convenience
Expand Down Expand Up @@ -99,10 +97,17 @@ impl crate::InterruptConfigurable for Context<crate::Blocking> {
}

impl<DM: crate::Mode> Context<DM> {
/// Indicates if the SHA context is in the first run.
///
/// Returns `true` if this is the first time processing data with the SHA
/// instance, otherwise returns `false`.
pub fn first_run(&self) -> bool {
self.first_run
}

/// Indicates if the SHA context has finished processing the data.
///
/// Returns `true` if the SHA calculation is complete, otherwise returns.
pub fn finished(&self) -> bool {
self.finished
}
Expand All @@ -122,8 +127,14 @@ impl<DM: crate::Mode> Context<DM> {
// - This means that we need to buffer bytes coming in up to 4 u8's in order
// to create a full u32

// This implementation might fail after u32::MAX/8 bytes, to increase please see
// ::finish() length/self.cursor usage
/// Trait for defining the behavior of a SHA algorithm instance.
///
/// This trait encapsulates the operations and configuration for a specific SHA
/// algorithm and provides methods for processing data buffers and calculating
/// the final hash.
///
/// This implementation might fail after u32::MAX/8 bytes, to increase please
/// see ::finish() length/self.cursor usage
pub trait Sha<DM: crate::Mode>: core::ops::DerefMut<Target = Context<DM>> {
/// Constant containing the name of the algorithm as a string.
const ALGORITHM: &'static str;
Expand All @@ -132,29 +143,49 @@ pub trait Sha<DM: crate::Mode>: core::ops::DerefMut<Target = Context<DM>> {
#[cfg(not(esp32))]
fn mode_as_bits() -> u8;

/// Returns the length of the chunk that the algorithm processes at a time.
///
/// For example, in SHA-256, this would typically return 64 bytes.
fn chunk_length(&self) -> usize;

/// Returns the length of the resulting digest produced by the algorithm.
///
/// For example, in SHA-256, this would return 32 bytes.
fn digest_length(&self) -> usize;

/// ESP32 requires that a control register to be written to calculate the
/// final SHA hash.
#[cfg(esp32)]
fn load_reg(&self);

/// Checks if the SHA peripheral is busy processing data.
///
/// Returns `true` if the SHA peripheral is busy, `false` otherwise.
/// ESP32 uses a different register per hash mode.
#[cfg(esp32)]
fn is_busy(&self) -> bool;

/// Checks if the SHA peripheral is busy processing data.
///
/// Returns `true` if the SHA peripheral is busy, `false` otherwise.
#[cfg(not(esp32))]
fn is_busy(&self) -> bool {
// Safety: This is safe because we only read `SHA_BUSY_REG`
let sha = unsafe { crate::peripherals::SHA::steal() };
sha.busy().read().bits() != 0
}

/// Processes the data buffer and updates the hash state.
///
/// This method is platform-specific and differs for ESP32 and non-ESP32
/// platforms.
#[cfg(esp32)]
fn process_buffer(&mut self);

/// Processes the data buffer and updates the hash state.
///
/// This method is platform-specific and differs for ESP32 and non-ESP32
/// platforms.
#[cfg(not(esp32))]
fn process_buffer(&mut self) {
// Safety: This is safe because digest state is restored and saved between
Expand Down Expand Up @@ -193,6 +224,11 @@ pub trait Sha<DM: crate::Mode>: core::ops::DerefMut<Target = Context<DM>> {
self.saved_digest.replace(saved_digest);
}

/// Flushes any remaining data from the internal buffer to the SHA
/// peripheral.
///
/// Returns a `Result` indicating whether the flush was successful or if the
/// operation would block.
fn flush_data(&mut self) -> nb::Result<(), Infallible> {
if self.is_busy() {
return Err(nb::Error::WouldBlock);
Expand Down Expand Up @@ -234,8 +270,9 @@ pub trait Sha<DM: crate::Mode>: core::ops::DerefMut<Target = Context<DM>> {
Ok(())
}

// This function ensures that incoming data is aligned to u32 (due to issues
// with cpy_mem<u8>)
/// Writes data into the SHA buffer.
/// This function ensures that incoming data is aligned to u32 (due to
/// issues with cpy_mem<u8>)
fn write_data<'a>(&mut self, incoming: &'a [u8]) -> nb::Result<&'a [u8], Infallible> {
let mod_cursor = self.cursor % self.chunk_length();

Expand Down Expand Up @@ -273,6 +310,7 @@ pub trait Sha<DM: crate::Mode>: core::ops::DerefMut<Target = Context<DM>> {
Ok(remaining)
}

/// Updates the SHA context with the provided data buffer.
fn update<'a>(&mut self, buffer: &'a [u8]) -> nb::Result<&'a [u8], Infallible> {
if self.is_busy() {
return Err(nb::Error::WouldBlock);
Expand All @@ -285,12 +323,12 @@ pub trait Sha<DM: crate::Mode>: core::ops::DerefMut<Target = Context<DM>> {
Ok(remaining)
}

// Finish of the calculation (if not alreaedy) and copy result to output
// After `finish()` is called `update()`s will contribute to a new hash which
// can be calculated again with `finish()`.
//
// Typically output is expected to be the size of digest_length(), but smaller
// inputs can be given to get a "short hash"
/// Finish of the calculation (if not already) and copy result to output
/// After `finish()` is called `update()`s will contribute to a new hash
/// which can be calculated again with `finish()`.
///
/// Typically output is expected to be the size of digest_length(), but
/// smaller inputs can be given to get a "short hash"
fn finish(&mut self, output: &mut [u8]) -> nb::Result<(), Infallible> {
// The main purpose of this function is to dynamically generate padding for the
// input. Padding: Append "1" bit, Pad zeros until 512/1024 filled
Expand Down Expand Up @@ -393,6 +431,16 @@ pub trait Sha<DM: crate::Mode>: core::ops::DerefMut<Target = Context<DM>> {
/// and a set of parameters
macro_rules! impl_sha {
($name: ident, $mode_bits: tt, $digest_length: tt, $chunk_length: tt) => {
/// A SHA implementation struct.
///
/// This struct is generated by the macro and represents a specific SHA hashing
/// algorithm (e.g., SHA-256, SHA-1). It manages the context and state required
/// for processing data using the selected hashing algorithm. It is generic over
/// the mode of operation, which could be either blocking or non-blocking modes.
///
/// The struct provides various functionalities such as initializing the hashing
/// process, updating the internal state with new data, and finalizing the
/// hashing operation to generate the final digest.
pub struct $name<DM: crate::Mode>(Context<DM>);

impl $name<crate::Blocking> {
Expand Down
Loading

0 comments on commit 9aae9c6

Please sign in to comment.