Skip to content

Commit

Permalink
Add hash customization traits (#1334)
Browse files Browse the repository at this point in the history
Some hash function constructions allow to have a customization string to have domain separation in hash functions. This is the case for [CSHAKE](https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-185.pdf#page=14) and [Blake2](https://www.blake2.net/blake2.pdf#page=6): 

With these new traits, the construction of hash functions with customization strings would be simpler. From currently for CSHAKE:
```rust
let mut hasher = CShake128::from_core(CShake128Core::new(b"test")); 
```

To:
```rust
let mut hasher = CShake128:new_personalization(b"test"));
```
  • Loading branch information
sylvainpelissier committed Apr 12, 2024
1 parent dadaf35 commit a593570
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
4 changes: 4 additions & 0 deletions digest/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## UNRELEASED
### Added
- `CustomizedInit` trait ([#1334]).

### Changed
- `crypto-common` dependency bumped to v0.2 ([#1173])
- Edition changed to 2021 and MSRV bumped to 1.57 ([#1173])
Expand All @@ -14,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `Mac::new`, `Mac::new_from_slice`, and `Mac::generate_key` methods ([#1173])

[#1173]: https://github.com/RustCrypto/traits/pull/1173
[#1334]: https://github.com/RustCrypto/traits/pull/1334

## 0.10.7 (2023-05-19)
### Changed
Expand Down
25 changes: 23 additions & 2 deletions digest/src/core_api/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use super::{
UpdateCore, XofReaderCoreWrapper,
};
use crate::{
ExtendableOutput, ExtendableOutputReset, FixedOutput, FixedOutputReset, HashMarker, Update,
CustomizedInit, ExtendableOutput, ExtendableOutputReset, FixedOutput, FixedOutputReset,
HashMarker, Update,
};
use block_buffer::BlockBuffer;
use core::{
Expand Down Expand Up @@ -160,8 +161,28 @@ impl<T: BufferKindUser> Drop for CoreWrapper<T> {
#[cfg(feature = "zeroize")]
impl<T: BufferKindUser + zeroize::ZeroizeOnDrop> zeroize::ZeroizeOnDrop for CoreWrapper<T> {}

impl<T> CustomizedInit for CoreWrapper<T>
where
T: BufferKindUser + CustomizedInit,
{
type CustomizedExtArg = T::CustomizedExtArg;

#[inline]
fn new_customized(customization: &[u8]) -> Self {
Self::from_core(T::new_customized(customization))
}

#[inline]
fn new_ext_customized(customization_ext: &Self::CustomizedExtArg) -> Self {
Self::from_core(T::new_ext_customized(customization_ext))
}
}

#[cfg(feature = "oid")]
impl<T: BufferKindUser + AssociatedOid> AssociatedOid for CoreWrapper<T> {
impl<T> AssociatedOid for CoreWrapper<T>
where
T: BufferKindUser + AssociatedOid,
{
const OID: ObjectIdentifier = T::OID;
}

Expand Down
15 changes: 15 additions & 0 deletions digest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,21 @@ pub trait VariableOutputReset: VariableOutput + Reset {
}
}

/// Trait for hash functions with customization string for domain separation.
pub trait CustomizedInit: Sized {
// TODO: It would be nice to define a default value equal to `[u8]`, but unfortunately
// associated type defaults are currently unstable

/// Extended customization
type CustomizedExtArg;

/// Create new hasher instance with the given customization string.
fn new_customized(customization: &[u8]) -> Self;

/// Create new hasher instance with the given extended customization.
fn new_ext_customized(customization_ext: &Self::CustomizedExtArg) -> Self;
}

/// The error type used in variable hash traits.
#[derive(Clone, Copy, Debug, Default)]
pub struct InvalidOutputSize;
Expand Down

0 comments on commit a593570

Please sign in to comment.