diff --git a/Cargo.lock b/Cargo.lock index caf0be3..f6d5987 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -145,6 +145,7 @@ dependencies = [ "blobby", "hex-literal", "hmac", + "kdf", "sha1", "sha2", ] @@ -190,6 +191,12 @@ dependencies = [ "sha2", ] +[[package]] +name = "kdf" +version = "0.1.0-pre.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4852c654c9650d06a4293146ead04bedcf29861dc0de1115ca1492b7a27c50aa" + [[package]] name = "libc" version = "0.2.178" diff --git a/hkdf/Cargo.toml b/hkdf/Cargo.toml index 2fe4eb7..77f35c7 100644 --- a/hkdf/Cargo.toml +++ b/hkdf/Cargo.toml @@ -15,6 +15,9 @@ rust-version = "1.85" [dependencies] hmac = "0.13.0-rc.3" +# optional dependencies +kdf = { version = "0.1.0-pre.1", optional = true } + [dev-dependencies] blobby = "0.4" hex-literal = "1" diff --git a/hkdf/src/lib.rs b/hkdf/src/lib.rs index f4fe166..de5c52a 100644 --- a/hkdf/src/lib.rs +++ b/hkdf/src/lib.rs @@ -20,6 +20,9 @@ pub use errors::{InvalidLength, InvalidPrkLength}; pub use hmac; pub use hmac_impl::HmacImpl; +#[cfg(feature = "kdf")] +pub use kdf::{self, Kdf}; + /// [`GenericHkdfExtract`] variant which uses [`Hmac`] for the underlying HMAC implementation. pub type HkdfExtract = GenericHkdfExtract>; /// [`GenericHkdf`] variant which uses [`Hmac`] for the underlying HMAC implementation. @@ -62,6 +65,16 @@ impl GenericHkdfExtract { } } +#[cfg(feature = "kdf")] +impl Kdf for GenericHkdfExtract { + fn derive_key(&self, secret: &[u8], info: &[u8], out: &mut [u8]) -> kdf::Result<()> { + let mut extract = self.clone(); + extract.input_ikm(secret); + let (_, hkdf) = extract.finalize(); + hkdf.expand(info, out).map_err(|_| kdf::Error) + } +} + /// Structure representing the HKDF, capable of HKDF-Expand and HKDF-Extract operations. /// Recommendations for the correct usage of the parameters can be found in the /// [crate root](index.html#usage).