Skip to content

Commit cd6df6d

Browse files
authored
hkdf: impl kdf::Kdf for GenericHkdfExtract (#173)
Due to the `&self` requirement in the `Kdf` trait, this seems like the only existing type that a `Kdf` impl makes sense for, since `derive_key` needs to be able to input IKM. The "salt" parameter of `Kdf::derive_key` has been mapped to the HKDF `info` parameter, as what HKDF calls the "salt" (the non-secret value that goes into the extract step) has already been (potentially) configured via `&self`, whereas the "info" parameter (the non-secret value that goes into the expand step) is the one we can actually configure through this API.
1 parent 03d63d3 commit cd6df6d

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

hkdf/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ rust-version = "1.85"
1515
[dependencies]
1616
hmac = "0.13.0-rc.3"
1717

18+
# optional dependencies
19+
kdf = { version = "0.1.0-pre.1", optional = true }
20+
1821
[dev-dependencies]
1922
blobby = "0.4"
2023
hex-literal = "1"

hkdf/src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ pub use errors::{InvalidLength, InvalidPrkLength};
2020
pub use hmac;
2121
pub use hmac_impl::HmacImpl;
2222

23+
#[cfg(feature = "kdf")]
24+
pub use kdf::{self, Kdf};
25+
2326
/// [`GenericHkdfExtract`] variant which uses [`Hmac`] for the underlying HMAC implementation.
2427
pub type HkdfExtract<H> = GenericHkdfExtract<Hmac<H>>;
2528
/// [`GenericHkdf`] variant which uses [`Hmac`] for the underlying HMAC implementation.
@@ -62,6 +65,16 @@ impl<H: HmacImpl> GenericHkdfExtract<H> {
6265
}
6366
}
6467

68+
#[cfg(feature = "kdf")]
69+
impl<H: HmacImpl> Kdf for GenericHkdfExtract<H> {
70+
fn derive_key(&self, secret: &[u8], info: &[u8], out: &mut [u8]) -> kdf::Result<()> {
71+
let mut extract = self.clone();
72+
extract.input_ikm(secret);
73+
let (_, hkdf) = extract.finalize();
74+
hkdf.expand(info, out).map_err(|_| kdf::Error)
75+
}
76+
}
77+
6578
/// Structure representing the HKDF, capable of HKDF-Expand and HKDF-Extract operations.
6679
/// Recommendations for the correct usage of the parameters can be found in the
6780
/// [crate root](index.html#usage).

0 commit comments

Comments
 (0)