Skip to content

Commit c2c686a

Browse files
authored
hkdf: unseal HmacImpl (#154)
1 parent a3b1244 commit c2c686a

3 files changed

Lines changed: 22 additions & 19 deletions

File tree

hkdf/CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7-
## UNRELEASED
8-
### Breaking changes
7+
## 0.13.0 (unreleased)
8+
### Changed
99
- Removed `std` crate feature ([#105])
1010
- Bump MSRV to 1.81 ([#105])
1111
- Bump `hmac` dependency to v0.13
12+
- Unseal `HmacImpl` trait ([#154])
1213

1314
[#105]: https://github.com/RustCrypto/KDFs/pull/105
15+
[#154]: https://github.com/RustCrypto/KDFs/pull/154
1416

1517
## 0.12.3 (2022-02-17)
1618
### Fixed
Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,21 @@ use hmac::digest::{
44
};
55
use hmac::{EagerHash, Hmac, SimpleHmac};
66

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

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

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

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

2632
#[inline(always)]
27-
#[allow(deprecated)] // clone_from_slice
2833
fn finalize(self) -> Output<H> {
29-
// Output<H> and Output<H::Core> are always equal to each other,
30-
// but we can not prove it at type level
31-
Output::<H>::clone_from_slice(&self.finalize_fixed())
34+
Output::<H>::try_from(&self.finalize_fixed()[..])
35+
.expect("Output<H> and Output<Hmac<H>> are always equal to each other")
3236
}
3337
}
3438

35-
impl<H: Digest + BlockSizeUser + Clone> Sealed<H> for SimpleHmac<H> {
39+
impl<H> HmacImpl<H> for SimpleHmac<H>
40+
where
41+
H: Digest + BlockSizeUser + Clone,
42+
{
3643
#[inline(always)]
3744
fn new_from_slice(key: &[u8]) -> Self {
3845
KeyInit::new_from_slice(key).expect("HMAC can take a key of any size")
@@ -44,10 +51,8 @@ impl<H: Digest + BlockSizeUser + Clone> Sealed<H> for SimpleHmac<H> {
4451
}
4552

4653
#[inline(always)]
47-
#[allow(deprecated)] // clone_from_slice
4854
fn finalize(self) -> Output<H> {
49-
// Output<H> and Output<H::Core> are always equal to each other,
50-
// but we can not prove it at type level
51-
Output::<H>::clone_from_slice(&self.finalize_fixed())
55+
Output::<H>::try_from(&self.finalize_fixed()[..])
56+
.expect("Output<H> and Output<SimpleHmac<H>> are always equal to each other")
5257
}
5358
}

hkdf/src/lib.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ use hmac::digest::{
1818
use hmac::{Hmac, SimpleHmac};
1919

2020
mod errors;
21-
mod sealed;
21+
mod hmac_impl;
2222

2323
pub use errors::{InvalidLength, InvalidPrkLength};
24+
pub use hmac_impl::HmacImpl;
2425

2526
/// [`HkdfExtract`] variant which uses [`SimpleHmac`] for underlying HMAC
2627
/// implementation.
@@ -192,8 +193,3 @@ where
192193
f.write_str("> { ... }")
193194
}
194195
}
195-
196-
/// Sealed trait implemented for [`Hmac`] and [`SimpleHmac`].
197-
pub trait HmacImpl<H: OutputSizeUser>: sealed::Sealed<H> + Clone {}
198-
199-
impl<H: OutputSizeUser, T: sealed::Sealed<H> + Clone> HmacImpl<H> for T {}

0 commit comments

Comments
 (0)