awslc: add HKDF-SHA-256/384/512
An empty salt is passed as the explicit zero salt of hash length: same bytes as RFC 5869 and the other providers, and the form AWS-LC-FIPS approves. Refs: R3
This commit is contained in:
parent
72c24def82
commit
92e3eaf22f
|
|
@ -0,0 +1,156 @@
|
||||||
|
//! HKDF-SHA-2 (RFC 5869) from `aws_lc_rs::hkdf`.
|
||||||
|
//!
|
||||||
|
//! Semantics match the wolfSSL and RustCrypto providers byte for byte:
|
||||||
|
//!
|
||||||
|
//! - An empty salt means the all-zero salt of hash length (RFC 5869 §2.2).
|
||||||
|
//! It is passed to AWS-LC explicitly as that zero string: HMAC pads a short
|
||||||
|
//! key with zeros, so the output is identical, and AWS-LC-FIPS approves
|
||||||
|
//! HKDF only with a non-empty salt.
|
||||||
|
//! - The output is capped at 255 × hash length.
|
||||||
|
//!
|
||||||
|
//! The BLAKE2b-512 KDF, Argon2id and KMAC256 are not in aws-lc-rs and return
|
||||||
|
//! [`CryptoError::FeatureNotCompiled`].
|
||||||
|
|
||||||
|
use aws_lc_rs::hkdf::{KeyType, Salt, HKDF_SHA256, HKDF_SHA384, HKDF_SHA512};
|
||||||
|
use 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 (alg, hash_len) = match algo {
|
||||||
|
KdfAlgorithm::Sha256 => (HKDF_SHA256, 32),
|
||||||
|
KdfAlgorithm::Sha384 => (HKDF_SHA384, 48),
|
||||||
|
KdfAlgorithm::Sha512 => (HKDF_SHA512, 64),
|
||||||
|
KdfAlgorithm::Blake2b512 | KdfAlgorithm::Argon2id | KdfAlgorithm::Kmac256 => {
|
||||||
|
return Err(CryptoError::FeatureNotCompiled(format!(
|
||||||
|
"{} is not offered by the awslc provider",
|
||||||
|
algo.name()
|
||||||
|
)))
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let max_len = 255 * hash_len;
|
||||||
|
if length > max_len {
|
||||||
|
return Err(CryptoError::InvalidInput(format!(
|
||||||
|
"HKDF: requested length {length} exceeds maximum {max_len}"
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
let zero_salt = [0u8; 64];
|
||||||
|
let salt = if salt.is_empty() {
|
||||||
|
&zero_salt[..hash_len]
|
||||||
|
} else {
|
||||||
|
salt
|
||||||
|
};
|
||||||
|
// aws-lc-rs keeps its copy of the IKM in a zeroising buffer.
|
||||||
|
let prk = Salt::new(alg, salt).extract(ikm);
|
||||||
|
let mut okm = Zeroizing::new(vec![0u8; length]);
|
||||||
|
prk.expand(&[info], OkmLen(length))
|
||||||
|
.and_then(|o| o.fill(&mut okm))
|
||||||
|
.map_err(|_| CryptoError::InternalError("HKDF expand failed".into()))?;
|
||||||
|
Ok(okm)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The requested output length, as aws-lc-rs wants it.
|
||||||
|
struct OkmLen(usize);
|
||||||
|
|
||||||
|
impl KeyType for OkmLen {
|
||||||
|
fn len(&self) -> usize {
|
||||||
|
self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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_sha256_rfc5869_tc1() {
|
||||||
|
assert_eq!(
|
||||||
|
tc1(KdfAlgorithm::Sha256),
|
||||||
|
"3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf34007208d5b887185865"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn hkdf_sha512_matches_the_other_providers() {
|
||||||
|
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_limit() {
|
||||||
|
assert!(derive_key(KdfAlgorithm::Sha256, b"k", b"", b"", 0).is_ok());
|
||||||
|
assert!(derive_key(KdfAlgorithm::Sha256, b"k", b"", b"", 255 * 32).is_ok());
|
||||||
|
assert!(matches!(
|
||||||
|
derive_key(KdfAlgorithm::Sha256, b"k", b"", b"", 255 * 32 + 1),
|
||||||
|
Err(CryptoError::InvalidInput(_))
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn unoffered_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(_))
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -8,6 +8,7 @@
|
||||||
|
|
||||||
pub mod aead;
|
pub mod aead;
|
||||||
pub mod hash;
|
pub mod hash;
|
||||||
|
pub mod kdf;
|
||||||
pub mod mac;
|
pub mod mac;
|
||||||
|
|
||||||
/// The provider's registry name.
|
/// The provider's registry name.
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue