Extend nss-rs with SHA3 support#17
Conversation
5654473 to
c912e80
Compare
| /// # Errors | ||
| /// | ||
| /// Returns an error if NSS initialization fails or the hash operation fails. | ||
| pub fn sha256(data: &[u8]) -> Result<[u8; 32], Error> { |
There was a problem hiding this comment.
I wonder whether this could use generics instead? It's a shame, but const generics currently don't support enums for the value, but you can still define types for each, just like the rust crypto crates do:
struct Sha256;
impl OidTag for Sha256 {
const OID: usize = SECOidTag::SEC_OID_SHA256;
}
impl Hash for Sha256 { // Hash can implement OidTag
const HASHLEN: usize = oid_to_hash_len(Self::OID);
}
pub fn hash<A: Hash>(data: &[u8]) -> Result<[u8; A::HASHLEN], Error> {
// ...
}
pub fn sha256(data:&[u8]) -> Result<[u8; 32], Error> {
hash::<Sha256>(data)
}|
|
||
| /// Returns the output length in bytes for the given hash algorithm. | ||
| #[must_use] | ||
| pub const fn hash_alg_to_hash_len(alg: &HashAlgorithm) -> usize { |
There was a problem hiding this comment.
This should not take a reference. HashAlgorithm is Copy (and likely smaller than the reference is).
| SHA3_512, | ||
| } | ||
|
|
||
| const fn hash_alg_to_oid(alg: &HashAlgorithm) -> SECOidTag::Type { |
There was a problem hiding this comment.
These functions should be a const functions on the type (and take self, see below).
| let data_len: i32 = match i32::try_from(data.len()) { | ||
| Ok(data_len) => data_len, | ||
| _ => return Err(Error::Internal), | ||
| }; |
There was a problem hiding this comment.
This should be able to use the ? operator, exposing integer sizing issues with a distinct type, rather than Error::Internal.
| _ => return Err(Error::Internal), | ||
| }; | ||
| let expected_len = hash_alg_to_hash_len(alg); | ||
| let mut digest = vec![0u8; expected_len]; |
There was a problem hiding this comment.
With a generic type, this could be an array allocated on the stack (faster).
WIP