Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions digest/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## 0.11.0 (UNRELEASED)
### Added
- `CustomizedInit` trait ([#1334])
- `VariableOutputCoreCustomized` trait ([#1787], [#2043])
- `buffer_fixed`, `buffer_ct_variable`, `buffer_rt_variable`, and `buffer_xof` macros ([#1799])
- `CollisionResistance` trait ([#1820])

Expand All @@ -24,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `CoreWrapper`, `RtVariableCoreWrapper`, and `XofReaderCoreWrapper` types ([#1799])
- `HashReader` and `HashWriter` are moved to the `digest-io` crate ([#1809])
- `io::Write/Read` implementations in favor of the `digest_io::IoWrapper` type ([#1809])
- `VariableOutput` trait ([#2043])

[#1173]: https://github.com/RustCrypto/traits/pull/1173
[#1334]: https://github.com/RustCrypto/traits/pull/1334
Expand All @@ -33,6 +35,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[#1820]: https://github.com/RustCrypto/traits/pull/1820
[#1953]: https://github.com/RustCrypto/traits/pull/1953
[#1958]: https://github.com/RustCrypto/traits/pull/1958
[#2043]: https://github.com/RustCrypto/traits/pull/2043

## 0.10.7 (2023-05-19)
### Changed
Expand Down
6 changes: 6 additions & 0 deletions digest/src/block_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ pub trait VariableOutputCore: UpdateCore + OutputSizeUser + BufferKindUser + Siz
fn finalize_variable_core(&mut self, buffer: &mut Buffer<Self>, out: &mut Output<Self>);
}

/// Trait adding customization string to hash functions with variable output.
pub trait VariableOutputCoreCustomized: VariableOutputCore {
/// Create new hasher instance with the given customization string and output size.
fn new_customized(customization: &[u8], output_size: usize) -> Self;
}

/// Type which used for defining truncation side in the [`VariableOutputCore`]
/// trait.
#[derive(Copy, Clone, Debug)]
Expand Down
6 changes: 3 additions & 3 deletions digest/src/block_api/ct_variable.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use super::{
AlgorithmName, Buffer, BufferKindUser, FixedOutputCore, Reset, TruncSide, UpdateCore,
VariableOutputCore,
VariableOutputCore, VariableOutputCoreCustomized,
};
#[cfg(feature = "mac")]
use crate::MacMarker;
use crate::{CollisionResistance, CustomizedInit, HashMarker, VarOutputCustomized};
use crate::{CollisionResistance, CustomizedInit, HashMarker};
use core::{fmt, marker::PhantomData};
use crypto_common::{
Block, BlockSizeUser, OutputSizeUser,
Expand Down Expand Up @@ -120,7 +120,7 @@ where

impl<T, OutSize> CustomizedInit for CtOutWrapper<T, OutSize>
where
T: VariableOutputCore + VarOutputCustomized,
T: VariableOutputCoreCustomized,
OutSize: ArraySize + IsLessOrEqual<T::OutputSize, Output = True>,
{
#[inline]
Expand Down
3 changes: 1 addition & 2 deletions digest/src/buffer_macros.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
mod fixed;
mod variable_ct;
mod variable_rt;
mod variable;
mod xof;
188 changes: 0 additions & 188 deletions digest/src/buffer_macros/variable_rt.rs

This file was deleted.

2 changes: 0 additions & 2 deletions digest/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ mod fixed;
#[cfg(feature = "mac")]
mod mac;
mod rng;
mod variable;
mod xof;

pub use fixed::*;
#[cfg(feature = "mac")]
pub use mac::*;
pub use variable::*;
pub use xof::*;

/// Test vector for hash functions
Expand Down
85 changes: 3 additions & 82 deletions digest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
//! - **High-level convenience traits**: [`Digest`], [`DynDigest`], [`Mac`].
//! Wrappers around lower-level traits for most common use-cases. Users should
//! usually prefer using these traits.
//! - **Mid-level traits**: [`Update`], [`FixedOutput`], [`FixedOutputReset`],
//! [`ExtendableOutput`], [`ExtendableOutputReset`], [`XofReader`],
//! [`VariableOutput`], [`Reset`], [`KeyInit`], and [`InnerInit`]. These
//! traits atomically describe available functionality of an algorithm.
//! - **Mid-level traits**: [`Update`], [`FixedOutput`], [`FixedOutputReset`], [`ExtendableOutput`],
//! [`ExtendableOutputReset`], [`XofReader`], [`Reset`], [`KeyInit`], and [`InnerInit`].
//! These traits atomically describe available functionality of an algorithm.
//! - **Marker traits**: [`HashMarker`], [`MacMarker`]. Used to distinguish
//! different algorithm classes.
//! - **Low-level traits** defined in the [`block_api`] module. These traits
Expand Down Expand Up @@ -207,90 +206,12 @@ pub trait ExtendableOutputReset: ExtendableOutput + Reset {
}
}

/// Trait for hash functions with variable-size output.
pub trait VariableOutput: Sized + Update {
/// Maximum size of output hash in bytes.
const MAX_OUTPUT_SIZE: usize;

/// Create new hasher instance with the given output size in bytes.
///
/// It will return `Err(InvalidOutputSize)` in case if hasher can not return
/// hash of the specified output size.
fn new(output_size: usize) -> Result<Self, InvalidOutputSize>;

/// Get output size in bytes of the hasher instance provided to the `new` method
fn output_size(&self) -> usize;

/// Write result into the output buffer.
///
/// Returns `Err(InvalidOutputSize)` if `out` size is not equal to
/// `self.output_size()`.
fn finalize_variable(self, out: &mut [u8]) -> Result<(), InvalidBufferSize>;

/// Compute hash of `data` and write it to `output`.
///
/// Length of the output hash is determined by `output`. If `output` is
/// bigger than `Self::MAX_OUTPUT_SIZE`, this method returns
/// `InvalidOutputSize`.
fn digest_variable(
input: impl AsRef<[u8]>,
output: &mut [u8],
) -> Result<(), InvalidOutputSize> {
let mut hasher = Self::new(output.len())?;
hasher.update(input.as_ref());
hasher
.finalize_variable(output)
.map_err(|_| InvalidOutputSize)
}

/// Retrieve result into a boxed slice and consume hasher.
///
/// `Box<[u8]>` is used instead of `Vec<u8>` to save stack space, since
/// they have size of 2 and 3 words respectively.
#[cfg(feature = "alloc")]
fn finalize_boxed(self) -> Box<[u8]> {
let n = self.output_size();
let mut buf = vec![0u8; n].into_boxed_slice();
self.finalize_variable(&mut buf)
.expect("buf length is equal to output_size");
buf
}
}

/// Trait for hash functions with variable-size output able to reset themselves.
pub trait VariableOutputReset: VariableOutput + Reset {
/// Write result into the output buffer and reset the hasher state.
///
/// Returns `Err(InvalidOutputSize)` if `out` size is not equal to
/// `self.output_size()`.
fn finalize_variable_reset(&mut self, out: &mut [u8]) -> Result<(), InvalidBufferSize>;

/// Retrieve result into a boxed slice and reset the hasher state.
///
/// `Box<[u8]>` is used instead of `Vec<u8>` to save stack space, since
/// they have size of 2 and 3 words respectively.
#[cfg(feature = "alloc")]
fn finalize_boxed_reset(&mut self) -> Box<[u8]> {
let n = self.output_size();
let mut buf = vec![0u8; n].into_boxed_slice();
self.finalize_variable_reset(&mut buf)
.expect("buf length is equal to output_size");
buf
}
}

/// Trait for hash functions with customization string for domain separation.
pub trait CustomizedInit: Sized {
/// Create new hasher instance with the given customization string.
fn new_customized(customization: &[u8]) -> Self;
}

/// Trait adding customization string to hash functions with variable output.
pub trait VarOutputCustomized: Sized {
/// Create new hasher instance with the given customization string and output size.
fn new_customized(customization: &[u8], output_size: usize) -> Self;
}

/// Types with a certain collision resistance.
pub trait CollisionResistance {
/// Collision resistance in bytes. This applies to an output size of `CollisionResistance * 2`.
Expand Down