Files
nym/common/crypto/src/hkdf.rs
Jędrzej Stuczyński 2a539dc3cc Upgrade blake3 to v1.3.1
2022-03-01 18:50:05 +00:00

31 lines
813 B
Rust

// Copyright 2021 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0
use hkdf::{
hmac::{
digest::{crypto_common::BlockSizeUser, Digest},
SimpleHmac,
},
Hkdf,
};
/// Perform HKDF `extract` then `expand` as a single step.
pub fn extract_then_expand<D>(
salt: Option<&[u8]>,
ikm: &[u8],
info: Option<&[u8]>,
okm_length: usize,
) -> Result<Vec<u8>, hkdf::InvalidLength>
where
D: Digest + BlockSizeUser + Clone,
{
// TODO: this would need to change if we ever needed the generated pseudorandom key, but
// realistically I don't see any reasons why we might need it
let hkdf = Hkdf::<D, SimpleHmac<D>>::new(salt, ikm);
let mut okm = vec![0u8; okm_length];
hkdf.expand(info.unwrap_or(&[]), &mut okm)?;
Ok(okm)
}