use crypto::{HmacAlgorithm, RTCCrypto, SecretVec};
use zeroize::{Zeroize, Zeroizing};
use crate::cipher_suite::{CipherSuite, NONCE_LEN};
use crate::error::{Error, Result, crypto_error};
const KEY_LABEL: &[u8] = b"SFrame 1.0 Secret key ";
const SALT_LABEL: &[u8] = b"SFrame 1.0 Secret salt ";
const RATCHET_LABEL: &[u8] = b"SFrame 1.0 Ratchet";
const RTP_STREAM_LABEL: &[u8] = b"SFrame 1.0 RTP Stream";
const MAX_HASH_LEN: usize = 64;
pub(crate) fn hkdf_extract(
crypto: &dyn RTCCrypto,
algorithm: HmacAlgorithm,
ikm: &[u8],
) -> Result<Zeroizing<[u8; MAX_HASH_LEN]>> {
let hash_len = algorithm.output_len();
let salt = [0u8; MAX_HASH_LEN];
let mut prk = Zeroizing::new([0u8; MAX_HASH_LEN]);
crypto
.new_hmac(algorithm, &salt[..hash_len])
.map_err(crypto_error)?
.sign(&[ikm], &mut prk[..hash_len])
.map_err(crypto_error)?;
Ok(prk)
}
pub(crate) fn hkdf_extract_salted(
crypto: &dyn RTCCrypto,
algorithm: HmacAlgorithm,
salt: &[u8],
ikm: &[u8],
) -> Result<Zeroizing<[u8; MAX_HASH_LEN]>> {
let hash_len = algorithm.output_len();
let mut prk = Zeroizing::new([0u8; MAX_HASH_LEN]);
crypto
.new_hmac(algorithm, salt)
.map_err(crypto_error)?
.sign(&[ikm], &mut prk[..hash_len])
.map_err(crypto_error)?;
Ok(prk)
}
pub(crate) fn hkdf_expand(
crypto: &dyn RTCCrypto,
algorithm: HmacAlgorithm,
prk: &[u8],
info: &[&[u8]],
out: &mut [u8],
) -> Result<()> {
let hash_len = algorithm.output_len();
debug_assert!(out.len() <= 255 * hash_len);
let mut mac = crypto.new_hmac(algorithm, prk).map_err(crypto_error)?;
let mut block = Zeroizing::new([0u8; MAX_HASH_LEN]);
let mut previous = Zeroizing::new([0u8; MAX_HASH_LEN]);
let mut previous_len = 0;
for (index, chunk) in out.chunks_mut(hash_len).enumerate() {
let counter = [u8::try_from(index + 1).map_err(|_| Error::KeyIdLayout)?];
let mut input: Vec<&[u8]> = Vec::with_capacity(info.len() + 2);
input.push(&previous[..previous_len]);
input.extend_from_slice(info);
input.push(&counter);
mac.sign(&input, &mut block[..hash_len])
.map_err(crypto_error)?;
chunk.copy_from_slice(&block[..chunk.len()]);
previous[..hash_len].copy_from_slice(&block[..hash_len]);
previous_len = hash_len;
}
Ok(())
}
pub(crate) struct KeySalt {
pub(crate) key: Zeroizing<Vec<u8>>,
pub(crate) salt: Zeroizing<[u8; NONCE_LEN]>,
}
pub(crate) fn derive_key_salt(
crypto: &dyn RTCCrypto,
suite: CipherSuite,
kid: u64,
base_key: &[u8],
) -> Result<KeySalt> {
if base_key.is_empty() {
return Err(Error::EmptyBaseKey);
}
let algorithm = suite.hmac();
let secret = hkdf_extract(crypto, algorithm, base_key)?;
let secret = &secret[..suite.hash_len()];
let kid = kid.to_be_bytes();
let suite_id = suite.id().to_be_bytes();
let mut key = Zeroizing::new(vec![0u8; suite.key_len()]);
hkdf_expand(
crypto,
algorithm,
secret,
&[KEY_LABEL, &kid, &suite_id],
&mut key,
)?;
let mut salt = Zeroizing::new([0u8; NONCE_LEN]);
hkdf_expand(
crypto,
algorithm,
secret,
&[SALT_LABEL, &kid, &suite_id],
&mut salt[..],
)?;
Ok(KeySalt { key, salt })
}
pub fn ratchet_base_key(
crypto: &dyn RTCCrypto,
suite: CipherSuite,
base_key: &[u8],
) -> Result<SecretVec> {
if base_key.is_empty() {
return Err(Error::EmptyBaseKey);
}
let algorithm = suite.hmac();
let secret = hkdf_extract(crypto, algorithm, base_key)?;
let mut next = vec![0u8; suite.hash_len()];
let expanded = hkdf_expand(
crypto,
algorithm,
&secret[..suite.hash_len()],
&[RATCHET_LABEL],
&mut next,
);
if let Err(error) = expanded {
next.zeroize();
return Err(error);
}
Ok(SecretVec::new(next))
}
pub fn derive_ssrc_key(
crypto: &dyn RTCCrypto,
suite: CipherSuite,
base_key: &[u8],
ssrc: u32,
) -> Result<SecretVec> {
if base_key.is_empty() {
return Err(Error::EmptyBaseKey);
}
let algorithm = suite.hmac();
let prk = hkdf_extract_salted(crypto, algorithm, &ssrc.to_be_bytes(), base_key)?;
let mut key = vec![0u8; suite.hash_len()];
let expanded = hkdf_expand(
crypto,
algorithm,
&prk[..suite.hash_len()],
&[RTP_STREAM_LABEL],
&mut key,
);
if let Err(error) = expanded {
key.zeroize();
return Err(error);
}
Ok(SecretVec::new(key))
}