153 lines
4.7 KiB
Rust
153 lines
4.7 KiB
Rust
//! KDFs: HKDF-SHA-256/384/512 (RFC 5869) from BoringSSL's `HKDF_extract`
|
||
//! and `HKDF_expand`.
|
||
//!
|
||
//! Semantics match the wolfSSL and RustCrypto providers byte for byte: an
|
||
//! empty salt is the all-zero salt of hash length (RFC 5869 §2.2), and the
|
||
//! output is capped at 255 × hash length. BoringSSL has no Argon2id,
|
||
//! BLAKE2b or KMAC, so those IDs return [`CryptoError::FeatureNotCompiled`].
|
||
//!
|
||
//! The PRK is wiped after Expand. `boring` returns it by value in a plain
|
||
//! stack buffer, so a copy may outlive this function on the stack until it
|
||
//! is overwritten; avoiding that would need `unsafe` FFI.
|
||
|
||
use boring::hash::MessageDigest;
|
||
use boring::hkdf::HkdfSuite;
|
||
use zeroize::{Zeroize, Zeroizing};
|
||
|
||
use ccc_crypto_core::{algorithms::KdfAlgorithm, error::CryptoError};
|
||
|
||
/// Derive `length` bytes from `ikm`.
|
||
pub fn derive_key(
|
||
algo: KdfAlgorithm,
|
||
ikm: &[u8],
|
||
salt: &[u8],
|
||
info: &[u8],
|
||
length: usize,
|
||
) -> Result<Zeroizing<Vec<u8>>, CryptoError> {
|
||
let md = match algo {
|
||
KdfAlgorithm::Sha256 => MessageDigest::sha256(),
|
||
KdfAlgorithm::Sha384 => MessageDigest::sha384(),
|
||
KdfAlgorithm::Sha512 => MessageDigest::sha512(),
|
||
KdfAlgorithm::Blake2b512 | KdfAlgorithm::Argon2id | KdfAlgorithm::Kmac256 => {
|
||
return Err(CryptoError::FeatureNotCompiled(format!(
|
||
"{} is not in BoringSSL",
|
||
algo.name()
|
||
)))
|
||
}
|
||
};
|
||
hkdf(md, ikm, salt, info, length)
|
||
}
|
||
|
||
fn hkdf(
|
||
md: MessageDigest,
|
||
ikm: &[u8],
|
||
salt: &[u8],
|
||
info: &[u8],
|
||
length: usize,
|
||
) -> Result<Zeroizing<Vec<u8>>, CryptoError> {
|
||
let suite = HkdfSuite::new(md);
|
||
let max_len = 255 * suite.prk_size();
|
||
if length > max_len {
|
||
return Err(CryptoError::InvalidInput(format!(
|
||
"HKDF: requested length {length} exceeds maximum {max_len}"
|
||
)));
|
||
}
|
||
let internal = |e: boring::error::ErrorStack| CryptoError::InternalError(format!("HKDF: {e}"));
|
||
// BoringSSL treats an empty salt as HashLen zero bytes, as RFC 5869 says.
|
||
let mut prk = suite.extract(salt, ikm).map_err(internal)?;
|
||
let mut okm = Zeroizing::new(vec![0u8; length]);
|
||
let res = suite.expand(&prk, info, &mut okm);
|
||
prk.zeroize();
|
||
res.map_err(internal)?;
|
||
Ok(okm)
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
|
||
fn h(s: &str) -> Vec<u8> {
|
||
hex::decode(s).unwrap()
|
||
}
|
||
|
||
fn tc1(algo: KdfAlgorithm) -> String {
|
||
let okm = derive_key(
|
||
algo,
|
||
&h("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"),
|
||
&h("000102030405060708090a0b0c"),
|
||
&h("f0f1f2f3f4f5f6f7f8f9"),
|
||
42,
|
||
)
|
||
.unwrap();
|
||
hex::encode(&*okm)
|
||
}
|
||
|
||
#[test]
|
||
fn hkdf_rfc5869_tc1_inputs() {
|
||
assert_eq!(
|
||
tc1(KdfAlgorithm::Sha256),
|
||
"3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf34007208d5b887185865"
|
||
);
|
||
// The SHA-512 value the wolfSSL HKDF-SHA-512 fix pinned.
|
||
assert_eq!(
|
||
tc1(KdfAlgorithm::Sha512),
|
||
"832390086cda71fb47625bb5ceb168e4c8e26a1a16ed34d9fc7fe92c1481579338da362cb8d9f925d7cb"
|
||
);
|
||
}
|
||
|
||
#[test]
|
||
fn hkdf_sha256_rfc5869_tc3_empty_salt_and_info() {
|
||
let okm = derive_key(
|
||
KdfAlgorithm::Sha256,
|
||
&h("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"),
|
||
b"",
|
||
b"",
|
||
42,
|
||
)
|
||
.unwrap();
|
||
assert_eq!(
|
||
hex::encode(&*okm),
|
||
"8da4e775a563c18f715f802a063c5a31b8a11f5c5ee1879ec3454e5f3c738d2d9d201395faa4b61a96c8"
|
||
);
|
||
}
|
||
|
||
#[test]
|
||
fn hkdf_empty_salt_is_zero_salt() {
|
||
for (algo, n) in [
|
||
(KdfAlgorithm::Sha256, 32),
|
||
(KdfAlgorithm::Sha384, 48),
|
||
(KdfAlgorithm::Sha512, 64),
|
||
] {
|
||
let a = derive_key(algo, b"ikm", b"", b"info", 32).unwrap();
|
||
let b = derive_key(algo, b"ikm", &vec![0u8; n], b"info", 32).unwrap();
|
||
assert_eq!(*a, *b);
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn hkdf_length_limits() {
|
||
assert!(derive_key(KdfAlgorithm::Sha256, b"k", b"", b"", 0)
|
||
.unwrap()
|
||
.is_empty());
|
||
assert!(derive_key(KdfAlgorithm::Sha512, b"k", b"", b"", 255 * 64).is_ok());
|
||
assert!(matches!(
|
||
derive_key(KdfAlgorithm::Sha256, b"k", b"", b"", 255 * 32 + 1),
|
||
Err(CryptoError::InvalidInput(_))
|
||
));
|
||
}
|
||
|
||
#[test]
|
||
fn missing_kdfs_are_not_compiled() {
|
||
for algo in [
|
||
KdfAlgorithm::Blake2b512,
|
||
KdfAlgorithm::Argon2id,
|
||
KdfAlgorithm::Kmac256,
|
||
] {
|
||
assert!(matches!(
|
||
derive_key(algo, b"k", b"saltsalt", b"", 32),
|
||
Err(CryptoError::FeatureNotCompiled(_))
|
||
));
|
||
}
|
||
}
|
||
}
|