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
24 changes: 8 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,15 @@ members = [

[profile.dev]
opt-level = 2

[patch.crates-io]
cipher = { git = "https://github.com/RustCrypto/traits.git" }
crypto-common = { git = "https://github.com/RustCrypto/traits.git" }
digest = { git = "https://github.com/RustCrypto/traits.git" }

cmac = { git = "https://github.com/RustCrypto/MACs.git" }
hmac = { git = "https://github.com/RustCrypto/MACs.git" }

belt-hash = { git = "https://github.com/RustCrypto/hashes.git" }
sha1 = { git = "https://github.com/RustCrypto/hashes.git" }
sha2 = { git = "https://github.com/RustCrypto/hashes.git" }
2 changes: 1 addition & 1 deletion bake-kdf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#![warn(missing_docs, rust_2018_idioms)]

use belt_hash::digest::FixedOutput;
use belt_hash::{BeltHash, Digest, belt_compress};
use belt_hash::{BeltHash, Digest, block_api::belt_compress};

/// `belt-keyexpand` key expansion algorithm described in STB 34.101.34-2020 8.1.2.
///
Expand Down
19 changes: 9 additions & 10 deletions hkdf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,11 @@ where
impl<H, I> fmt::Debug for HkdfExtract<H, I>
where
H: OutputSizeUser,
I: HmacImpl<H>,
I::Core: AlgorithmName,
I: HmacImpl<H> + AlgorithmName,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("HkdfExtract<")?;
<I::Core as AlgorithmName>::write_alg_name(f)?;
<I as AlgorithmName>::write_alg_name(f)?;
f.write_str("> { ... }")
}
}
Expand All @@ -185,7 +184,7 @@ where
/// [crate root](index.html#usage).
#[derive(Clone)]
pub struct Hkdf<H: OutputSizeUser, I: HmacImpl<H> = Hmac<H>> {
hmac: I::Core,
hmac: I,
_pd: PhantomData<H>,
}

Expand All @@ -206,7 +205,7 @@ impl<H: OutputSizeUser, I: HmacImpl<H>> Hkdf<H, I> {
return Err(InvalidPrkLength);
}
Ok(Self {
hmac: I::new_core(prk),
hmac: I::new_from_slice(prk),
_pd: PhantomData,
})
}
Expand Down Expand Up @@ -235,7 +234,7 @@ impl<H: OutputSizeUser, I: HmacImpl<H>> Hkdf<H, I> {
}

for (block_n, block) in okm.chunks_mut(chunk_len).enumerate() {
let mut hmac = I::from_core(&self.hmac);
let mut hmac = self.hmac.clone();

if let Some(ref prev) = prev {
hmac.update(prev)
Expand Down Expand Up @@ -272,16 +271,16 @@ impl<H, I> fmt::Debug for Hkdf<H, I>
where
H: OutputSizeUser,
I: HmacImpl<H>,
I::Core: AlgorithmName,
I: AlgorithmName,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Hkdf<")?;
<I::Core as AlgorithmName>::write_alg_name(f)?;
<I as AlgorithmName>::write_alg_name(f)?;
f.write_str("> { ... }")
}
}

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

impl<H: OutputSizeUser, T: sealed::Sealed<H>> HmacImpl<H> for T {}
impl<H: OutputSizeUser, T: sealed::Sealed<H> + Clone> HmacImpl<H> for T {}
34 changes: 2 additions & 32 deletions hkdf/src/sealed.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
use hmac::digest::{
Digest, FixedOutput, KeyInit, Output, Update,
core_api::{BlockSizeUser, CoreWrapper, OutputSizeUser},
block_api::{BlockSizeUser, OutputSizeUser},
};
use hmac::{EagerHash, Hmac, HmacCore, SimpleHmac};
use hmac::{Hmac, SimpleHmac, block_api::EagerHash};

pub trait Sealed<H: OutputSizeUser> {
type Core: Clone;

fn new_from_slice(key: &[u8]) -> Self;

fn new_core(key: &[u8]) -> Self::Core;

fn from_core(core: &Self::Core) -> Self;

fn update(&mut self, data: &[u8]);

fn finalize(self) -> Output<H>;
Expand All @@ -22,23 +16,11 @@ impl<H> Sealed<H> for Hmac<H>
where
H: EagerHash + OutputSizeUser,
{
type Core = HmacCore<H>;

#[inline(always)]
fn new_from_slice(key: &[u8]) -> Self {
KeyInit::new_from_slice(key).expect("HMAC can take a key of any size")
}

#[inline(always)]
fn new_core(key: &[u8]) -> Self::Core {
HmacCore::new_from_slice(key).expect("HMAC can take a key of any size")
}

#[inline(always)]
fn from_core(core: &Self::Core) -> Self {
CoreWrapper::from_core(core.clone())
}

#[inline(always)]
fn update(&mut self, data: &[u8]) {
Update::update(self, data);
Expand All @@ -54,23 +36,11 @@ where
}

impl<H: Digest + BlockSizeUser + Clone> Sealed<H> for SimpleHmac<H> {
type Core = Self;

#[inline(always)]
fn new_from_slice(key: &[u8]) -> Self {
KeyInit::new_from_slice(key).expect("HMAC can take a key of any size")
}

#[inline(always)]
fn new_core(key: &[u8]) -> Self::Core {
KeyInit::new_from_slice(key).expect("HMAC can take a key of any size")
}

#[inline(always)]
fn from_core(core: &Self::Core) -> Self {
core.clone()
}

#[inline(always)]
fn update(&mut self, data: &[u8]) {
Update::update(self, data);
Expand Down
2 changes: 1 addition & 1 deletion hkdf/tests/rfc5869.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use hex_literal::hex;
use hkdf::{Hkdf, hmac::EagerHash};
use hkdf::{Hkdf, hmac::block_api::EagerHash};
use sha1::Sha1;
use sha2::{Sha256, digest::OutputSizeUser};

Expand Down