Skip to content

digest: add newtype macros #1799

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 23 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
19 changes: 0 additions & 19 deletions digest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,6 @@ let hash = Sha256::digest(b"my message");
println!("Result: {:x}", hash);
```

### Hashing `Read`-able objects

If you want to hash data from [`Read`][3] trait (e.g. from file) you can rely on
implementation of [`Write`][4] trait (requires enabled-by-default `std` feature):

```rust
use sha2::{Sha256, Digest};
use std::{fs, io};

let mut file = fs::File::open(&path)?;
let mut hasher = Sha256::new();
let n = io::copy(&mut file, &mut hasher)?;
let hash = hasher.finalize();

println!("Path: {}", path);
println!("Bytes processed: {}", n);
println!("Hash value: {:x}", hash);
```

### Generic code

You can write generic code over `Digest` (or other traits from `digest` crate)
Expand Down
3 changes: 2 additions & 1 deletion digest/src/core_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//! higher-level traits.
use crate::InvalidOutputSize;

pub use block_buffer::{Eager, Lazy};
pub use crypto_common::{AlgorithmName, Block, BlockSizeUser, OutputSizeUser, Reset};

use block_buffer::{BlockBuffer, BufferKind};
Expand All @@ -15,7 +16,7 @@ mod rt_variable;
mod wrapper;
mod xof_reader;

pub use ct_variable::CtVariableCoreWrapper;
pub use ct_variable::CtOutWrapper;
pub use rt_variable::RtVariableCoreWrapper;
pub use wrapper::{CoreProxy, CoreWrapper};
pub use xof_reader::XofReaderCoreWrapper;
Expand Down
36 changes: 18 additions & 18 deletions digest/src/core_api/ct_variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ use crypto_common::{
hazmat::{DeserializeStateError, SerializableState, SerializedState, SubSerializedStateSize},
typenum::{IsLess, IsLessOrEqual, Le, LeEq, NonZero, Sum, U1, U256},
};
/// Wrapper around [`VariableOutputCore`] which selects output size
/// at compile time.

/// Wrapper around [`VariableOutputCore`] which selects output size at compile time.
#[derive(Clone)]
pub struct CtVariableCoreWrapper<T, OutSize>
pub struct CtOutWrapper<T, OutSize>
where
T: VariableOutputCore,
OutSize: ArraySize + IsLessOrEqual<T::OutputSize>,
Expand All @@ -29,7 +29,7 @@ where
_out: PhantomData<OutSize>,
}

impl<T, OutSize> HashMarker for CtVariableCoreWrapper<T, OutSize>
impl<T, OutSize> HashMarker for CtOutWrapper<T, OutSize>
where
T: VariableOutputCore + HashMarker,
OutSize: ArraySize + IsLessOrEqual<T::OutputSize>,
Expand All @@ -38,15 +38,15 @@ where
}

#[cfg(feature = "mac")]
impl<T, OutSize> MacMarker for CtVariableCoreWrapper<T, OutSize>
impl<T, OutSize> MacMarker for CtOutWrapper<T, OutSize>
where
T: VariableOutputCore + MacMarker,
OutSize: ArraySize + IsLessOrEqual<T::OutputSize>,
LeEq<OutSize, T::OutputSize>: NonZero,
{
}

impl<T, OutSize> CollisionResistance for CtVariableCoreWrapper<T, OutSize>
impl<T, OutSize> CollisionResistance for CtOutWrapper<T, OutSize>
where
T: VariableOutputCore + CollisionResistance,
OutSize: ArraySize + IsLessOrEqual<T::OutputSize>,
Expand All @@ -55,7 +55,7 @@ where
type CollisionResistance = T::CollisionResistance;
}

impl<T, OutSize> BlockSizeUser for CtVariableCoreWrapper<T, OutSize>
impl<T, OutSize> BlockSizeUser for CtOutWrapper<T, OutSize>
where
T: VariableOutputCore,
OutSize: ArraySize + IsLessOrEqual<T::OutputSize>,
Expand All @@ -64,7 +64,7 @@ where
type BlockSize = T::BlockSize;
}

impl<T, OutSize> UpdateCore for CtVariableCoreWrapper<T, OutSize>
impl<T, OutSize> UpdateCore for CtOutWrapper<T, OutSize>
where
T: VariableOutputCore,
OutSize: ArraySize + IsLessOrEqual<T::OutputSize>,
Expand All @@ -76,7 +76,7 @@ where
}
}

impl<T, OutSize> OutputSizeUser for CtVariableCoreWrapper<T, OutSize>
impl<T, OutSize> OutputSizeUser for CtOutWrapper<T, OutSize>
where
T: VariableOutputCore,
OutSize: ArraySize + IsLessOrEqual<T::OutputSize>,
Expand All @@ -85,7 +85,7 @@ where
type OutputSize = OutSize;
}

impl<T, OutSize> BufferKindUser for CtVariableCoreWrapper<T, OutSize>
impl<T, OutSize> BufferKindUser for CtOutWrapper<T, OutSize>
where
T: VariableOutputCore,
OutSize: ArraySize + IsLessOrEqual<T::OutputSize>,
Expand All @@ -94,7 +94,7 @@ where
type BufferKind = T::BufferKind;
}

impl<T, OutSize> FixedOutputCore for CtVariableCoreWrapper<T, OutSize>
impl<T, OutSize> FixedOutputCore for CtOutWrapper<T, OutSize>
where
T: VariableOutputCore,
OutSize: ArraySize + IsLessOrEqual<T::OutputSize>,
Expand All @@ -117,7 +117,7 @@ where
}
}

impl<T, OutSize> Default for CtVariableCoreWrapper<T, OutSize>
impl<T, OutSize> Default for CtOutWrapper<T, OutSize>
where
T: VariableOutputCore,
OutSize: ArraySize + IsLessOrEqual<T::OutputSize>,
Expand All @@ -132,7 +132,7 @@ where
}
}

impl<T, OutSize> CustomizedInit for CtVariableCoreWrapper<T, OutSize>
impl<T, OutSize> CustomizedInit for CtOutWrapper<T, OutSize>
where
T: VariableOutputCore + VarOutputCustomized,
OutSize: ArraySize + IsLessOrEqual<T::OutputSize>,
Expand All @@ -147,7 +147,7 @@ where
}
}

impl<T, OutSize> Reset for CtVariableCoreWrapper<T, OutSize>
impl<T, OutSize> Reset for CtOutWrapper<T, OutSize>
where
T: VariableOutputCore,
OutSize: ArraySize + IsLessOrEqual<T::OutputSize>,
Expand All @@ -159,7 +159,7 @@ where
}
}

impl<T, OutSize> AlgorithmName for CtVariableCoreWrapper<T, OutSize>
impl<T, OutSize> AlgorithmName for CtOutWrapper<T, OutSize>
where
T: VariableOutputCore + AlgorithmName,
OutSize: ArraySize + IsLessOrEqual<T::OutputSize>,
Expand All @@ -173,15 +173,15 @@ where
}

#[cfg(feature = "zeroize")]
impl<T, OutSize> zeroize::ZeroizeOnDrop for CtVariableCoreWrapper<T, OutSize>
impl<T, OutSize> zeroize::ZeroizeOnDrop for CtOutWrapper<T, OutSize>
where
T: VariableOutputCore + zeroize::ZeroizeOnDrop,
OutSize: ArraySize + IsLessOrEqual<T::OutputSize>,
LeEq<OutSize, T::OutputSize>: NonZero,
{
}

impl<T, OutSize> fmt::Debug for CtVariableCoreWrapper<T, OutSize>
impl<T, OutSize> fmt::Debug for CtOutWrapper<T, OutSize>
where
T: VariableOutputCore + AlgorithmName,
OutSize: ArraySize + IsLessOrEqual<T::OutputSize>,
Expand All @@ -195,7 +195,7 @@ where
type CtVariableCoreWrapperSerializedStateSize<T> =
Sum<<T as SerializableState>::SerializedStateSize, U1>;

impl<T, OutSize> SerializableState for CtVariableCoreWrapper<T, OutSize>
impl<T, OutSize> SerializableState for CtOutWrapper<T, OutSize>
where
T: VariableOutputCore + SerializableState,
OutSize: ArraySize + IsLessOrEqual<T::OutputSize>,
Expand Down
15 changes: 11 additions & 4 deletions digest/src/core_api/rt_variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ use crypto_common::{
#[cfg(feature = "zeroize")]
use zeroize::ZeroizeOnDrop;

/// Wrapper around [`VariableOutputCore`] which selects output size
/// at run time.
/// Wrapper around [`VariableOutputCore`] which selects output size at run time.
#[derive(Clone)]
pub struct RtVariableCoreWrapper<T: VariableOutputCore> {
core: T,
Expand Down Expand Up @@ -64,11 +63,19 @@ impl<T: VariableOutputCore> BlockSizeUser for RtVariableCoreWrapper<T> {
type BlockSize = T::BlockSize;
}

impl<T: VariableOutputCore + Reset> Reset for RtVariableCoreWrapper<T> {
// TODO: remove because the hasher may be customized?
impl<T: VariableOutputCore> Reset for RtVariableCoreWrapper<T> {
#[inline]
fn reset(&mut self) {
self.buffer.reset();
self.core.reset();
self.core = T::new(self.output_size as usize).unwrap();
}
}

impl<T: VariableOutputCore + AlgorithmName> AlgorithmName for RtVariableCoreWrapper<T> {
fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result {
T::write_alg_name(f)?;
f.write_str("Var")
}
}

Expand Down
19 changes: 9 additions & 10 deletions digest/src/core_api/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ impl<T: ExtendableOutputCore + Reset> ExtendableOutputReset for CoreWrapper<T> {
}
}

impl<T: BufferKindUser + AlgorithmName> AlgorithmName for CoreWrapper<T> {
#[inline]
fn write_alg_name(f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
T::write_alg_name(f)
}
}

impl<T: BufferKindUser> Drop for CoreWrapper<T> {
#[inline]
fn drop(&mut self) {
Expand Down Expand Up @@ -214,20 +221,12 @@ where
}
}

/// A proxy trait to a core type implemented by [`CoreWrapper`]
// TODO: replace with an inherent associated type on stabilization:
// https://github.com/rust-lang/rust/issues/8995
pub trait CoreProxy: sealed::Sealed {
/// A proxy trait to a core type.
pub trait CoreProxy {
/// Type wrapped by [`CoreWrapper`].
type Core;
}

mod sealed {
pub trait Sealed {}
}

impl<T: BufferKindUser> sealed::Sealed for CoreWrapper<T> {}

impl<T: BufferKindUser> CoreProxy for CoreWrapper<T> {
type Core = T;
}
1 change: 1 addition & 0 deletions digest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pub mod core_api;
mod digest;
#[cfg(feature = "mac")]
mod mac;
mod newtype;
mod xof_fixed;

#[cfg(feature = "core-api")]
Expand Down
4 changes: 4 additions & 0 deletions digest/src/newtype.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mod fixed;
mod variable_ct;
mod variable_rt;
mod xof;
Loading