polymorph_webcrypto_guest/hkdf_sha2.rs
1//! `hkdf-sha2` derivation parameterization (RFC 5869 over the SHA-2
2//! family).
3//!
4//! This module mints no keys: `prepare` yields a
5//! [`DeriveInput`](crate::DeriveInput), consumed through
6//! [`DeriveInput::derive_bits`](crate::DeriveInput::derive_bits) or a
7//! target interface's `derive_key` (e.g.
8//! [`hmac_sha2::derive_key`](crate::hmac_sha2::derive_key)).
9
10use crate::{bindings, DeriveInput, Error, Ikm};
11
12pub use crate::bindings::sha2::Sha2Variant;
13
14/// Parameterize an HKDF derivation over imported keying material:
15/// HKDF-Extract runs with `salt`, and `info` is bound for the expand step.
16///
17/// An empty `salt` means the RFC's default (a hash-length block of
18/// zeros). The grants are copied from `input`.
19pub async fn prepare(
20 variant: Sha2Variant,
21 input: &Ikm,
22 salt: impl Into<Vec<u8>>,
23 info: impl Into<Vec<u8>>,
24) -> Result<DeriveInput, Error> {
25 Ok(DeriveInput::from_raw(
26 bindings::hkdf_sha2::prepare(variant, input.as_raw(), salt.into(), info.into()).await?,
27 ))
28}
29
30/// Parameterize an HKDF derivation over another derivation's output —
31/// the chaining step, e.g. from an [`AgreementSecretKey::agree`]
32/// (WebCrypto's `deriveKey(ECDH → HKDF)` shape).
33///
34/// The upstream derivation runs at its natural output length, so only
35/// sources that have one chain: an agreement's shared secret does; KDF
36/// sources fail [`Error::Other`], as the platform does. Requires the
37/// upstream input's [`derive_key`](crate::DeriveOptions::derive_key)
38/// grant.
39///
40/// [`AgreementSecretKey::agree`]: crate::AgreementSecretKey::agree
41pub async fn prepare_from(
42 variant: Sha2Variant,
43 input: &DeriveInput,
44 salt: impl Into<Vec<u8>>,
45 info: impl Into<Vec<u8>>,
46) -> Result<DeriveInput, Error> {
47 Ok(DeriveInput::from_raw(
48 bindings::hkdf_sha2::prepare_from(variant, input.as_raw(), salt.into(), info.into())
49 .await?,
50 ))
51}