Skip to content

Commit

Permalink
update hmac
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov committed Nov 19, 2021
1 parent 5ba039f commit cb7a19b
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 29 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ members = [
]

[patch.crates-io]
digest = { git = "https://github.com/RustCrypto/traits/", branch = "new_traits" }
block-buffer = { git = "https://github.com/RustCrypto/utils", branch = "pad_error" }
digest = { git = "https://github.com/RustCrypto/traits/", branch = "digest/v0.10" }
block-buffer = { git = "https://github.com/RustCrypto/utils", branch = "block-buffer/v0.10" }
sha-1 = { git = "https://github.com/RustCrypto/hashes/", branch = "digest/v0.10.0-pre" }
sha2 = { git = "https://github.com/RustCrypto/hashes/", branch = "digest/v0.10.0-pre" }
hmac = { git = "https://github.com/RustCrypto/MACs/", branch = "new_traits" }
hmac = { git = "https://github.com/RustCrypto/MACs/", branch = "hmac/v0.12" }
77 changes: 57 additions & 20 deletions hkdf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,21 @@ extern crate std;
use core::fmt;
use hmac::digest::{
block_buffer::Eager,
core_api::{AlgorithmName, BufferKindUser, CoreProxy, FixedOutputCore, UpdateCore},
generic_array::typenum::Unsigned,
Digest, FixedOutput, KeyInit, Output, Update,
core_api::{
AlgorithmName, BlockSizeUser, BufferKindUser, CoreProxy, FixedOutputCore, OutputSizeUser,
UpdateCore,
},
generic_array::typenum::{IsLess, Le, NonZero, Unsigned, U256},
FixedOutput, HashMarker, KeyInit, Output, Update,
};
use hmac::Hmac;

/// Error that is returned when supplied pseudorandom key (PRK) is not long enough.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[derive(Copy, Clone, Debug)]
pub struct InvalidPrkLength;

/// Structure for InvalidLength, used for output error handling.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[derive(Copy, Clone, Debug)]
pub struct InvalidLength;

/// Structure representing the streaming context of an HKDF-Extract operation
Expand All @@ -124,20 +127,34 @@ pub struct InvalidLength;
#[derive(Clone)]
pub struct HkdfExtract<D>
where
D: CoreProxy + Digest,
D::Core: UpdateCore + FixedOutputCore + BufferKindUser<BufferKind = Eager> + Default + Clone,
D: CoreProxy,
D::Core: HashMarker
+ UpdateCore
+ FixedOutputCore
+ BufferKindUser<BufferKind = Eager>
+ Default
+ Clone,
<D::Core as BlockSizeUser>::BlockSize: IsLess<U256>,
Le<<D::Core as BlockSizeUser>::BlockSize, U256>: NonZero,
{
hmac: Hmac<D>,
}

impl<D> HkdfExtract<D>
where
D: CoreProxy + Digest,
D::Core: UpdateCore + FixedOutputCore + BufferKindUser<BufferKind = Eager> + Default + Clone,
D: CoreProxy,
D::Core: HashMarker
+ UpdateCore
+ FixedOutputCore
+ BufferKindUser<BufferKind = Eager>
+ Default
+ Clone,
<D::Core as BlockSizeUser>::BlockSize: IsLess<U256>,
Le<<D::Core as BlockSizeUser>::BlockSize, U256>: NonZero,
{
/// Initiates the HKDF-Extract context with the given optional salt
pub fn new(salt: Option<&[u8]>) -> HkdfExtract<D> {
let default_salt = Output::<D>::default();
let default_salt = Output::<D::Core>::default();
let salt = salt.unwrap_or(&default_salt);
let hmac = Hmac::<D>::new_from_slice(salt).expect("HMAC can take a key of any size");
HkdfExtract { hmac }
Expand All @@ -159,13 +176,16 @@ where

impl<D> fmt::Debug for HkdfExtract<D>
where
D: CoreProxy + Digest,
D::Core: AlgorithmName
D: CoreProxy,
D::Core: HashMarker
+ AlgorithmName
+ UpdateCore
+ FixedOutputCore
+ BufferKindUser<BufferKind = Eager>
+ Default
+ Clone,
<D::Core as BlockSizeUser>::BlockSize: IsLess<U256>,
Le<<D::Core as BlockSizeUser>::BlockSize, U256>: NonZero,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("HkdfExtract<")?;
Expand All @@ -178,16 +198,30 @@ where
#[derive(Clone)]
pub struct Hkdf<D>
where
D: CoreProxy + Digest,
D::Core: UpdateCore + FixedOutputCore + BufferKindUser<BufferKind = Eager> + Default + Clone,
D: CoreProxy,
D::Core: HashMarker
+ UpdateCore
+ FixedOutputCore
+ BufferKindUser<BufferKind = Eager>
+ Default
+ Clone,
<D::Core as BlockSizeUser>::BlockSize: IsLess<U256>,
Le<<D::Core as BlockSizeUser>::BlockSize, U256>: NonZero,
{
hmac: Hmac<D>,
}

impl<D> Hkdf<D>
where
D: CoreProxy + Digest,
D::Core: UpdateCore + FixedOutputCore + BufferKindUser<BufferKind = Eager> + Default + Clone,
D: CoreProxy,
D::Core: HashMarker
+ UpdateCore
+ FixedOutputCore
+ BufferKindUser<BufferKind = Eager>
+ Default
+ Clone,
<D::Core as BlockSizeUser>::BlockSize: IsLess<U256>,
Le<<D::Core as BlockSizeUser>::BlockSize, U256>: NonZero,
{
/// Convenience method for [`extract`][Hkdf::extract] when the generated
/// pseudorandom key can be ignored and only HKDF-Expand operation is needed. This is the most
Expand All @@ -201,7 +235,7 @@ where
/// as per section 3.3 from RFC5869.
pub fn from_prk(prk: &[u8]) -> Result<Hkdf<D>, InvalidPrkLength> {
// section 2.3 specifies that prk must be "at least HashLen octets"
if prk.len() < D::OutputSize::to_usize() {
if prk.len() < <D::Core as OutputSizeUser>::OutputSize::to_usize() {
return Err(InvalidPrkLength);
}

Expand All @@ -228,7 +262,7 @@ where
) -> Result<(), InvalidLength> {
let mut prev: Option<Output<D::Core>> = None;

let chunk_len = D::OutputSize::USIZE;
let chunk_len = <D::Core as OutputSizeUser>::OutputSize::USIZE;
if okm.len() > chunk_len * 255 {
return Err(InvalidLength);
}
Expand Down Expand Up @@ -269,13 +303,16 @@ where

impl<D> fmt::Debug for Hkdf<D>
where
D: CoreProxy + Digest,
D::Core: AlgorithmName
D: CoreProxy,
D::Core: HashMarker
+ AlgorithmName
+ UpdateCore
+ FixedOutputCore
+ BufferKindUser<BufferKind = Eager>
+ Default
+ Clone,
<D::Core as BlockSizeUser>::BlockSize: IsLess<U256>,
Le<<D::Core as BlockSizeUser>::BlockSize, U256>: NonZero,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Hkdf<")?;
Expand Down

0 comments on commit cb7a19b

Please sign in to comment.