Skip to content

Commit

Permalink
x
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Lagergren <[email protected]>
  • Loading branch information
elagergren-spideroak committed Jan 23, 2025
1 parent 35e1ebb commit cced819
Show file tree
Hide file tree
Showing 4 changed files with 422 additions and 136 deletions.
22 changes: 22 additions & 0 deletions crates/acvp/src/vectors/sha2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#![cfg_attr(docsrs, doc(cfg(feature = "sha2")))]

use alloc::{string::String, vec::Vec};
use core::fmt;

use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -46,6 +47,27 @@ pub enum MctVersion {
Alternate,
}

impl MctVersion {
/// Shorthand for `self == MctVersion::Standard`.
pub const fn is_std(self) -> bool {
matches!(self, Self::Standard)
}

/// Shorthand for `self == MctVersion::Alternate`.
pub const fn is_alt(self) -> bool {
matches!(self, Self::Alternate)
}
}

impl fmt::Display for MctVersion {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Standard => write!(f, "standard"),
Self::Alternate => write!(f, "alternate"),
}
}
}

/// SHA-2 tests.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
Expand Down
22 changes: 22 additions & 0 deletions crates/acvp/src/vectors/sha3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#![cfg_attr(docsrs, doc(cfg(feature = "sha3")))]

use alloc::vec::Vec;
use core::fmt;

use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -40,6 +41,27 @@ pub enum MctVersion {
Alternate,
}

impl MctVersion {
/// Shorthand for `self == MctVersion::Standard`.
pub const fn is_std(self) -> bool {
matches!(self, Self::Standard)
}

/// Shorthand for `self == MctVersion::Alternate`.
pub const fn is_alt(self) -> bool {
matches!(self, Self::Alternate)
}
}

impl fmt::Display for MctVersion {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Standard => write!(f, "standard"),
Self::Alternate => write!(f, "alternate"),
}
}
}

/// SHA-3 tests.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
Expand Down
29 changes: 26 additions & 3 deletions crates/crypto/src/hash.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
//! Cryptographic hash functions.
#![forbid(unsafe_code)]

use core::{
fmt::{self, Debug},
num::NonZeroU16,
ops::{Deref, DerefMut},
ptr,
};

use generic_array::{ArrayLength, GenericArray, IntoArrayLength};
use generic_array::{ArrayLength, GenericArray, IntoArrayLength, LengthError};
use sha3_utils::{encode_string, right_encode_bytes};
use subtle::{Choice, ConstantTimeEq};
use typenum::{
Expand Down Expand Up @@ -119,6 +118,21 @@ impl<N: ArrayLength> Digest<N> {
Self::new(GenericArray::from_array(digest))
}

/// Creates a hash digest from a slice.
#[inline]
pub const fn try_from_slice(digest: &[u8]) -> Result<&Self, LengthError> {
match GenericArray::<u8, N>::try_from_slice(digest) {
Ok(array) => {
// SAFETY: `Self` is a `#[repr(transparent)]` wrapper
// around `GenericArray<u8, N>`, so they both have the
// same layout in memory.
let digest = unsafe { &*(ptr::from_ref(array).cast::<Self>()) };
Ok(digest)
}
Err(err) => Err(err),
}
}

/// Returns the length of the hash digest.
#[inline]
#[allow(clippy::len_without_is_empty)]
Expand Down Expand Up @@ -201,6 +215,15 @@ impl<N: ArrayLength> ConstantTimeEq for Digest<N> {
}
}

impl<'a, N: ArrayLength> TryFrom<&'a [u8]> for &'a Digest<N> {
type Error = LengthError;

#[inline]
fn try_from(digest: &'a [u8]) -> Result<Self, Self::Error> {
Digest::try_from_slice(digest)
}
}

/// A cryptographic hash over a set of strings such that each
/// element is unambiguously encoded per [NIST SP 800-185].
///
Expand Down
Loading

0 comments on commit cced819

Please sign in to comment.