Peripheral ref/sha (#312)

* Add SHA to list of peripherals to be created

* Refactor SHA peripheral to use PeripheralRef

* Update SHA examples to get them building again
This commit is contained in:
Jesse Braham 2022-12-14 04:19:53 -08:00 committed by GitHub
parent 7817e270c2
commit 220f812625
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 15 additions and 15 deletions

View File

@ -55,6 +55,7 @@ mod peripherals {
I2C0, I2C0,
I2C1, I2C1,
RNG, RNG,
SHA,
SPI0, SPI0,
SPI1, SPI1,
SPI2, SPI2,

View File

@ -36,6 +36,7 @@ mod peripherals {
crate::create_peripherals! { crate::create_peripherals! {
I2C0, I2C0,
RNG, RNG,
SHA,
SPI0, SPI0,
SPI1, SPI1,
SPI2, SPI2,

View File

@ -47,6 +47,7 @@ mod peripherals {
crate::create_peripherals! { crate::create_peripherals! {
I2C0, I2C0,
RNG, RNG,
SHA,
SPI0, SPI0,
SPI1, SPI1,
SPI2, SPI2,

View File

@ -53,6 +53,7 @@ mod peripherals {
I2C0, I2C0,
I2C1, I2C1,
RNG, RNG,
SHA,
SPI0, SPI0,
SPI1, SPI1,
SPI2, SPI2,

View File

@ -64,6 +64,7 @@ mod peripherals {
I2C0, I2C0,
I2C1, I2C1,
RNG, RNG,
SHA,
SPI0, SPI0,
SPI1, SPI1,
SPI2, SPI2,

View File

@ -1,6 +1,9 @@
use core::convert::Infallible; use core::convert::Infallible;
use crate::pac::SHA; use crate::{
peripheral::{Peripheral, PeripheralRef},
peripherals::SHA,
};
// All the hash algorithms introduced in FIPS PUB 180-4 Spec. // All the hash algorithms introduced in FIPS PUB 180-4 Spec.
// SHA-1 // SHA-1
@ -157,9 +160,8 @@ impl AlignmentHelper {
} }
} }
#[derive(Debug)] pub struct Sha<'d> {
pub struct Sha { sha: PeripheralRef<'d, SHA>,
sha: SHA,
mode: ShaMode, mode: ShaMode,
alignment_helper: AlignmentHelper, alignment_helper: AlignmentHelper,
cursor: usize, cursor: usize,
@ -224,8 +226,10 @@ fn mode_as_bits(mode: ShaMode) -> u8 {
// This implementation might fail after u32::MAX/8 bytes, to increase please see // This implementation might fail after u32::MAX/8 bytes, to increase please see
// ::finish() length/self.cursor usage // ::finish() length/self.cursor usage
impl Sha { impl<'d> Sha<'d> {
pub fn new(sha: SHA, mode: ShaMode) -> Self { pub fn new(sha: impl Peripheral<P = SHA> + 'd, mode: ShaMode) -> Self {
crate::into_ref!(sha);
// Setup SHA Mode // Setup SHA Mode
#[cfg(not(esp32))] #[cfg(not(esp32))]
sha.mode sha.mode
@ -508,8 +512,4 @@ impl Sha {
Ok(()) Ok(())
} }
pub fn free(self) -> SHA {
self.sha
}
} }

View File

@ -66,7 +66,6 @@ fn main() -> ! {
let hw_time = post_calc - pre_calc; let hw_time = post_calc - pre_calc;
println!("Took {} cycles", hw_time); println!("Took {} cycles", hw_time);
println!("SHA512 Hash output {:02x?}", output); println!("SHA512 Hash output {:02x?}", output);
let _usha = hasher.free();
let pre_calc = xtensa_lx::timer::get_cycle_count(); let pre_calc = xtensa_lx::timer::get_cycle_count();
let mut hasher = Sha512::new(); let mut hasher = Sha512::new();

View File

@ -65,7 +65,6 @@ fn main() -> ! {
// let hw_time = post_calc - pre_calc; // let hw_time = post_calc - pre_calc;
// println!("Took {} cycles", hw_time); // println!("Took {} cycles", hw_time);
println!("SHA256 Hash output {:02x?}", output); println!("SHA256 Hash output {:02x?}", output);
let _usha = hasher.free();
// let pre_calc = xtensa_lx::timer::get_cycle_count(); // let pre_calc = xtensa_lx::timer::get_cycle_count();
let mut hasher = Sha256::new(); let mut hasher = Sha256::new();

View File

@ -65,7 +65,6 @@ fn main() -> ! {
// let hw_time = post_calc - pre_calc; // let hw_time = post_calc - pre_calc;
// println!("Took {} cycles", hw_time); // println!("Took {} cycles", hw_time);
println!("SHA256 Hash output {:02x?}", output); println!("SHA256 Hash output {:02x?}", output);
let _usha = hasher.free();
// let pre_calc = xtensa_lx::timer::get_cycle_count(); // let pre_calc = xtensa_lx::timer::get_cycle_count();
let mut hasher = Sha256::new(); let mut hasher = Sha256::new();

View File

@ -65,7 +65,6 @@ fn main() -> ! {
let hw_time = post_calc - pre_calc; let hw_time = post_calc - pre_calc;
println!("Took {} cycles", hw_time); println!("Took {} cycles", hw_time);
println!("SHA512 Hash output {:02x?}", output); println!("SHA512 Hash output {:02x?}", output);
let _usha = hasher.free();
let pre_calc = xtensa_lx::timer::get_cycle_count(); let pre_calc = xtensa_lx::timer::get_cycle_count();
let mut hasher = Sha512::new(); let mut hasher = Sha512::new();

View File

@ -65,7 +65,6 @@ fn main() -> ! {
let hw_time = post_calc - pre_calc; let hw_time = post_calc - pre_calc;
println!("Took {} cycles", hw_time); println!("Took {} cycles", hw_time);
println!("SHA512 Hash output {:02x?}", output); println!("SHA512 Hash output {:02x?}", output);
let _usha = hasher.free();
let pre_calc = xtensa_lx::timer::get_cycle_count(); let pre_calc = xtensa_lx::timer::get_cycle_count();
let mut hasher = Sha512::new(); let mut hasher = Sha512::new();