mirror of
https://github.com/esp-rs/esp-hal.git
synced 2025-10-02 06:40:47 +00:00

* Add untested basic SHA for esp-sX/cX chips * Fix ptr type inconsistency for S2 * Add ESP32 impl & fix process_buffer latch issue * Add debug example for SHA accelerator * Clean up no-op buffer prints * Test vector parity (on esp32s3) * Checkpoint for converting to alignment helper * Finish refactoring & additional parity tests on esp32s3 * Remove core_intrinsics requirement for now * Fix case where (src.len() % 4) == 3 * Finish sha2 example with performance comparison (12-61x speedup) * Refactor ESP32 to alignment helper & Clean up example * Prevent out-of-bounds reads in ESP32 version * Revert Cargo debug changes * Remove cargo config.toml * Clean up example * Remove common/rust-toolchain & ignore in future * Might as well use actual size_of const * Remove SHA512/SHA384 for C2/C3 * Directly import nb::block! to remove unused import warning & fix c2 feature detect * Remove stray newlines * Fix esp32c2 having SHA256 * ESP32 also has SHA384 * Remove comments that don't have a purpose * Clean up example & finish() handling * Add examples & add ESP32 free() * Update C2/C3 examples to show accurate algorithm used * Fix busy check for ESP32 * Remove outdated TODO comment * Update PAC for ESP3 and (actually) fix busy check * Refactor ESP32 version to reduce search space * Add debug printlns to sha example & clean up comments * Fix ESP32 version, finally Co-authored-by: ferris <ferris@devdroplets.com> Co-authored-by: Jesse Braham <jesse@beta7.io>
80 lines
3.0 KiB
Rust
80 lines
3.0 KiB
Rust
//! Demonstrates the use of the SHA peripheral and compares the speed of hardware-accelerated and pure software hashing.
|
|
//!
|
|
|
|
#![no_std]
|
|
#![no_main]
|
|
|
|
use esp32s3_hal::{
|
|
clock::ClockControl,
|
|
pac::Peripherals,
|
|
prelude::*,
|
|
timer::TimerGroup,
|
|
Rtc,
|
|
sha::{Sha, ShaMode},
|
|
};
|
|
use nb::block;
|
|
use esp_backtrace as _;
|
|
use esp_println::println;
|
|
use xtensa_lx_rt::entry;
|
|
use sha2::{Sha512, Digest};
|
|
|
|
#[entry]
|
|
fn main() -> ! {
|
|
let peripherals = Peripherals::take().unwrap();
|
|
let system = peripherals.SYSTEM.split();
|
|
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
|
|
|
|
let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks);
|
|
let mut wdt = timer_group0.wdt;
|
|
let mut rtc = Rtc::new(peripherals.RTC_CNTL);
|
|
|
|
// Disable MWDT and RWDT (Watchdog) flash boot protection
|
|
wdt.disable();
|
|
rtc.rwdt.disable();
|
|
|
|
|
|
let source_data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".as_bytes();
|
|
let mut remaining = source_data.clone();
|
|
let mut hasher = Sha::new(peripherals.SHA, ShaMode::SHA512);
|
|
|
|
// Short hashes can be created by decreasing the output buffer to the desired length
|
|
let mut output = [0u8; 64];
|
|
|
|
let pre_calc = xtensa_lx::timer::get_cycle_count();
|
|
// The hardware implementation takes a subslice of the input, and returns the unprocessed parts
|
|
// The unprocessed parts can be input in the next iteration, you can always add more data until
|
|
// finish() is called. After finish() is called update()'s will contribute to a new hash which
|
|
// can be extracted again with finish().
|
|
|
|
while remaining.len() > 0 {
|
|
// Can add println to view progress, however println takes a few orders of magnitude longer than
|
|
// the Sha function itself so not useful for comparing processing time
|
|
// println!("Remaining len: {}", remaining.len());
|
|
|
|
// All the HW Sha functions are infallible so unwrap is fine to use if you use block!
|
|
remaining = block!(hasher.update(remaining)).unwrap();
|
|
}
|
|
|
|
// Finish can be called as many times as desired to get mutliple copies of the output.
|
|
block!(hasher.finish(output.as_mut_slice())).unwrap();
|
|
let post_calc = xtensa_lx::timer::get_cycle_count();
|
|
let hw_time = post_calc - pre_calc;
|
|
println!("Took {} cycles", hw_time);
|
|
println!("SHA512 Hash output {:02x?}", output);
|
|
let _usha = hasher.free();
|
|
|
|
|
|
let pre_calc = xtensa_lx::timer::get_cycle_count();
|
|
let mut hasher = Sha512::new();
|
|
hasher.update(source_data);
|
|
let soft_result = hasher.finalize();
|
|
let post_calc = xtensa_lx::timer::get_cycle_count();
|
|
let soft_time = post_calc - pre_calc;
|
|
println!("Took {} cycles", soft_time);
|
|
println!("SHA512 Hash output {:02x?}", soft_result);
|
|
|
|
println!("HW SHA is {}x faster", soft_time/hw_time);
|
|
|
|
loop {}
|
|
}
|