-
Notifications
You must be signed in to change notification settings - Fork 28
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
hkdf: automatically switch between Hmac
and SimpleHmac
#96
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,61 @@ | ||
use hmac::digest::{ | ||
core_api::{BlockSizeUser, CoreWrapper, OutputSizeUser}, | ||
Digest, FixedOutput, KeyInit, Output, Update, | ||
block_buffer::{BufferKind, Eager, Lazy}, | ||
core_api::{BlockSizeUser, BufferKindUser, CoreWrapper, OutputSizeUser}, | ||
Digest, FixedOutput, KeyInit, Update, | ||
}; | ||
use hmac::{EagerHash, Hmac, HmacCore, SimpleHmac}; | ||
|
||
pub trait Sealed<H: OutputSizeUser> { | ||
type Core: Clone; | ||
static EXPECT_MSG: &str = "HMAC can take a key of any size"; | ||
|
||
fn new_from_slice(key: &[u8]) -> Self; | ||
pub trait Sealed: OutputSizeUser + Sized { | ||
type FullHmac: KeyInit + Update + FixedOutput; | ||
type CoreHmac: KeyInit; | ||
|
||
fn new_core(key: &[u8]) -> Self::Core; | ||
fn core_to_full(core: &Self::CoreHmac) -> Self::FullHmac; | ||
|
||
fn from_core(core: &Self::Core) -> Self; | ||
|
||
fn update(&mut self, data: &[u8]); | ||
fn new_core(key: &[u8]) -> Self::CoreHmac { | ||
Self::CoreHmac::new_from_slice(key).expect(EXPECT_MSG) | ||
} | ||
|
||
fn finalize(self) -> Output<H>; | ||
fn new_full(key: &[u8]) -> Self::FullHmac { | ||
Self::FullHmac::new_from_slice(key).expect(EXPECT_MSG) | ||
} | ||
} | ||
|
||
impl<H> Sealed<H> for Hmac<H> | ||
impl<C, K> Sealed for CoreWrapper<C> | ||
where | ||
H: EagerHash + OutputSizeUser, | ||
K: HmacKind<Self> + BufferKind, | ||
C: BufferKindUser<BufferKind = K> + 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") | ||
} | ||
type FullHmac = K::FullHmac; | ||
type CoreHmac = K::CoreHmac; | ||
|
||
#[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()) | ||
fn core_to_full(core: &Self::CoreHmac) -> Self::FullHmac { | ||
K::core_to_full(core) | ||
} | ||
} | ||
|
||
#[inline(always)] | ||
fn update(&mut self, data: &[u8]) { | ||
Update::update(self, data); | ||
} | ||
pub trait HmacKind<H> { | ||
type FullHmac: OutputSizeUser + KeyInit + Update + FixedOutput; | ||
type CoreHmac: KeyInit; | ||
|
||
#[inline(always)] | ||
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()) | ||
} | ||
fn core_to_full(core: &Self::CoreHmac) -> Self::FullHmac; | ||
} | ||
|
||
impl<H: Digest + BlockSizeUser + Clone> Sealed<H> for SimpleHmac<H> { | ||
type Core = Self; | ||
impl<H: EagerHash> HmacKind<H> for Eager { | ||
type FullHmac = Hmac<H>; | ||
type CoreHmac = 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") | ||
fn core_to_full(core: &Self::CoreHmac) -> Self::FullHmac { | ||
CoreWrapper::from_core(core.clone()) | ||
} | ||
} | ||
|
||
#[inline(always)] | ||
fn new_core(key: &[u8]) -> Self::Core { | ||
KeyInit::new_from_slice(key).expect("HMAC can take a key of any size") | ||
} | ||
impl<H: Digest + Clone + BlockSizeUser> HmacKind<H> for Lazy { | ||
type FullHmac = SimpleHmac<H>; | ||
type CoreHmac = SimpleHmac<H>; | ||
|
||
#[inline(always)] | ||
fn from_core(core: &Self::Core) -> Self { | ||
fn core_to_full(core: &Self::CoreHmac) -> Self::FullHmac { | ||
core.clone() | ||
} | ||
|
||
#[inline(always)] | ||
fn update(&mut self, data: &[u8]) { | ||
Update::update(self, data); | ||
} | ||
|
||
#[inline(always)] | ||
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()) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like this would preclude switching to newtypes of
CoreWrapper
for the hash crates.Could this blanket impl be based around the requisite traits instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it's possible to do automatically without specialization. Alternatively, we could introduce yet another trait which would regulate switching between those two HMAC implementations, i.e. we would move this burden to all hash implementation crates.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then I would personally rather have newtypes than this automatic switching.
Have you benchmarked how much
SimpleHmac
actually degrades performance for the short inputs in HKDF usage?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We already have the
SimpleHkdf
type aliases, which do the job just fine.It's more about structure size, depending on hash,
SimpleHmac
can be twice bigger, especially if thereset
feature is enabled forhmac
.