Skip to content
Merged
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
6 changes: 4 additions & 2 deletions hkdf/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ All notable changes to this project will be documented in this file.
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
### Breaking changes
## 0.13.0 (unreleased)
### Changed
- Removed `std` crate feature ([#105])
- Bump MSRV to 1.81 ([#105])
- Bump `hmac` dependency to v0.13
- Unseal `HmacImpl` trait ([#154])

[#105]: https://github.com/RustCrypto/KDFs/pull/105
[#154]: https://github.com/RustCrypto/KDFs/pull/154

## 0.12.3 (2022-02-17)
### Fixed
Expand Down
27 changes: 16 additions & 11 deletions hkdf/src/sealed.rs → hkdf/src/hmac_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@ use hmac::digest::{
};
use hmac::{EagerHash, Hmac, SimpleHmac};

pub trait Sealed<H: OutputSizeUser> {
/// Trait representing a HMAC implementation.
///
/// Most users should use [`Hmac`] or [`SimpleHmac`].
pub trait HmacImpl<H: OutputSizeUser>: Clone {
/// Create new HMAC state with the given key.
fn new_from_slice(key: &[u8]) -> Self;

/// Update HMAC state.
fn update(&mut self, data: &[u8]);

/// Finalize the HMAC state and get generated tag.
fn finalize(self) -> Output<H>;
}

impl<H: EagerHash> Sealed<H> for Hmac<H> {
impl<H: EagerHash> HmacImpl<H> for Hmac<H> {
#[inline(always)]
fn new_from_slice(key: &[u8]) -> Self {
KeyInit::new_from_slice(key).expect("HMAC can take a key of any size")
Expand All @@ -24,15 +30,16 @@ impl<H: EagerHash> Sealed<H> for Hmac<H> {
}

#[inline(always)]
#[allow(deprecated)] // clone_from_slice
fn finalize(self) -> Output<H> {
// Output<H> and Output<H::Core> are always equal to each other,
// but we can not prove it at type level
Output::<H>::clone_from_slice(&self.finalize_fixed())
Output::<H>::try_from(&self.finalize_fixed()[..])
.expect("Output<H> and Output<Hmac<H>> are always equal to each other")
}
}

impl<H: Digest + BlockSizeUser + Clone> Sealed<H> for SimpleHmac<H> {
impl<H> HmacImpl<H> for SimpleHmac<H>
where
H: Digest + BlockSizeUser + Clone,
{
#[inline(always)]
fn new_from_slice(key: &[u8]) -> Self {
KeyInit::new_from_slice(key).expect("HMAC can take a key of any size")
Expand All @@ -44,10 +51,8 @@ impl<H: Digest + BlockSizeUser + Clone> Sealed<H> for SimpleHmac<H> {
}

#[inline(always)]
#[allow(deprecated)] // clone_from_slice
fn finalize(self) -> Output<H> {
// Output<H> and Output<H::Core> are always equal to each other,
// but we can not prove it at type level
Output::<H>::clone_from_slice(&self.finalize_fixed())
Output::<H>::try_from(&self.finalize_fixed()[..])
.expect("Output<H> and Output<SimpleHmac<H>> are always equal to each other")
}
}
8 changes: 2 additions & 6 deletions hkdf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ use hmac::digest::{
use hmac::{Hmac, SimpleHmac};

mod errors;
mod sealed;
mod hmac_impl;

pub use errors::{InvalidLength, InvalidPrkLength};
pub use hmac_impl::HmacImpl;

/// [`HkdfExtract`] variant which uses [`SimpleHmac`] for underlying HMAC
/// implementation.
Expand Down Expand Up @@ -192,8 +193,3 @@ where
f.write_str("> { ... }")
}
}

/// Sealed trait implemented for [`Hmac`] and [`SimpleHmac`].
pub trait HmacImpl<H: OutputSizeUser>: sealed::Sealed<H> + Clone {}

impl<H: OutputSizeUser, T: sealed::Sealed<H> + Clone> HmacImpl<H> for T {}