-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(SHA256): Add initial hardware acceleration impl for SHA256
- Currently, esp-wifi needs to be patched to work with this hal revision
- Loading branch information
1 parent
ea41ee7
commit ab4ec67
Showing
27 changed files
with
239 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
typedef struct mbedtls_sha256_context { | ||
void* peripheral; | ||
void* hasher; | ||
} mbedtls_sha256_context; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
//! Re-export SHA Hardware implementation based on availability. | ||
//! | ||
//! # Hardware support | ||
//! This does not reflect the current implementation but rather the support | ||
//! for the SHA modes on the specific SoCs. | ||
//! | ||
//! | Modes | esp32 | esp32c3 | esp32s2 | esp32s3 | | ||
//! | ------- | ------- | ------- | ------- | ------- | | ||
//! | SHA1 | ✓ | ✓ | ✓ | ✓ | | ||
//! | SHA224 | x | ✓ | ✓ | ✓ | | ||
//! | SHA256 | ✓ | ✓ | ✓ | ✓ | | ||
//! | SHA384 | ✓ | x | ✓ | ✓ | | ||
//! | SHA512 | ✓ | x | ✓ | ✓ | | ||
|
||
mod sha256; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use crate::hal::peripherals::SHA; | ||
use crate::hal::sha::Sha; | ||
use crate::hal::sha::ShaMode; | ||
use esp_mbedtls_sys::c_types::*; | ||
use nb::block; | ||
|
||
#[repr(C)] | ||
pub struct mbedtls_sha256_context<'a> { | ||
peripheral: SHA, | ||
hasher: Sha<'a>, | ||
} | ||
|
||
#[no_mangle] | ||
pub unsafe extern "C" fn mbedtls_sha256_init(ctx: *mut mbedtls_sha256_context) { | ||
(*ctx).peripheral = SHA::steal(); | ||
} | ||
|
||
#[no_mangle] | ||
pub unsafe extern "C" fn mbedtls_sha256_free(_ctx: *mut mbedtls_sha256_context) { | ||
// Do nothing | ||
} | ||
|
||
#[no_mangle] | ||
pub unsafe extern "C" fn mbedtls_sha256_clone<'a>( | ||
dst: *mut mbedtls_sha256_context<'a>, | ||
src: *const mbedtls_sha256_context<'a>, | ||
) { | ||
core::ptr::copy_nonoverlapping(src, dst, 1) | ||
} | ||
|
||
#[allow(unused_variables)] | ||
#[no_mangle] | ||
pub unsafe extern "C" fn mbedtls_sha256_starts( | ||
ctx: *mut mbedtls_sha256_context, | ||
is224: c_int, | ||
) -> c_int { | ||
#[cfg(not(feature = "esp32"))] | ||
let mode = if is224 == 1 { | ||
ShaMode::SHA224 | ||
} else { | ||
ShaMode::SHA256 | ||
}; | ||
|
||
#[cfg(feature = "esp32")] | ||
let mode = ShaMode::SHA256; | ||
|
||
let hasher = Sha::new(&mut (*ctx).peripheral, mode); | ||
|
||
(*ctx).hasher = hasher; | ||
0 | ||
} | ||
|
||
#[no_mangle] | ||
pub unsafe extern "C" fn mbedtls_sha256_update( | ||
ctx: *mut mbedtls_sha256_context, | ||
input: *const c_uchar, | ||
ilen: usize, | ||
) -> c_int { | ||
let slice = core::ptr::slice_from_raw_parts(input as *const u8, ilen as usize); | ||
let mut remaining = &*slice; | ||
|
||
while remaining.len() > 0 { | ||
remaining = block!((*ctx).hasher.update(remaining)).unwrap(); | ||
} | ||
0 | ||
} | ||
|
||
#[no_mangle] | ||
pub unsafe extern "C" fn mbedtls_sha256_finish( | ||
ctx: *mut mbedtls_sha256_context, | ||
output: *mut c_uchar, | ||
) -> c_int { | ||
let mut data = [0u8; 32]; | ||
block!((*ctx).hasher.finish(&mut data)).unwrap(); | ||
core::ptr::copy_nonoverlapping(data.as_ptr(), output, data.len()); | ||
0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.