Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

refactor(webcrypto): move HKDF off OpenSSL

ldm0e787b4e0da98
diff --git a/moli-crypto/src/hkdf.rs b/moli-crypto/src/hkdf.rs
new file mode 100644
index 000000000..4af0d3975
--- /dev/null
+++ b/moli-crypto/src/hkdf.rs
@@ -0,0 +1,102 @@
+use std::fmt;
+
+use aws_lc_rs::hkdf::{
+ self, HKDF_SHA1_FOR_LEGACY_USE_ONLY, HKDF_SHA256, HKDF_SHA384, HKDF_SHA512, KeyType,
+};
+
+use crate::DigestAlgorithm;
+
+#[derive(Clone, Copy)]
+struct OutputLength(usize);
+
+impl KeyType for OutputLength {
+ fn len(&self) -> usize {
+ self.0
+ }
+}
+
+#[derive(Clone, Copy, Debug, Eq, PartialEq)]
+pub struct HkdfError;
+
+impl fmt::Display for HkdfError {
+ fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
+ formatter.write_str("HKDF derivation failed")
+ }
+}
+
+impl std::error::Error for HkdfError {}
+
+pub fn derive_hkdf_bytes(
+ algorithm: DigestAlgorithm,
+ secret: &[u8],
+ salt: &[u8],
+ info: &[u8],
+ output_len: usize,
+) -> Result<Vec<u8>, HkdfError> {
+ if output_len == 0 {
+ return Ok(Vec::new());
+ }
+
+ let algorithm = match algorithm {
+ DigestAlgorithm::Sha1 => HKDF_SHA1_FOR_LEGACY_USE_ONLY,
+ DigestAlgorithm::Sha256 => HKDF_SHA256,
+ DigestAlgorithm::Sha384 => HKDF_SHA384,
+ DigestAlgorithm::Sha512 => HKDF_SHA512,
+ };
+ let salt = hkdf::Salt::new(algorithm, salt);
+ let pseudo_random_key = salt.extract(secret);
+ let info = [info];
+ let output_key_material = pseudo_random_key
+ .expand(&info, OutputLength(output_len))
+ .map_err(|_| HkdfError)?;
+ let mut output = vec![0_u8; output_len];
+ output_key_material
+ .fill(&mut output)
+ .map_err(|_| HkdfError)?;
+ Ok(output)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn derives_rfc5869_sha256_vector() {
+ let output = derive_hkdf_bytes(
+ DigestAlgorithm::Sha256,
+ &[0x0b; 22],
+ &[
+ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c,
+ ],
+ &[0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9],
+ 42,
+ )
+ .expect("RFC 5869 vector should derive");
+
+ assert_eq!(
+ output,
+ [
+ 0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a, 0x90, 0x43, 0x4f, 0x64, 0xd0, 0x36,
+ 0x2f, 0x2a, 0x2d, 0x2d, 0x0a, 0x90, 0xcf, 0x1a, 0x5a, 0x4c, 0x5d, 0xb0, 0x2d, 0x56,
+ 0xec, 0xc4, 0xc5, 0xbf, 0x34, 0x00, 0x72, 0x08, 0xd5, 0xb8, 0x87, 0x18, 0x58, 0x65,
+ ]
+ );
+ }
+
+ #[test]
+ fn accepts_empty_webcrypto_inputs_and_output() {
+ assert_eq!(
+ derive_hkdf_bytes(DigestAlgorithm::Sha256, &[], &[], &[], 32)
+ .expect("empty WebCrypto HKDF inputs should derive"),
+ [
+ 0xeb, 0x70, 0xf0, 0x1d, 0xed, 0xe9, 0xaf, 0xaf, 0xa4, 0x49, 0xee, 0xe1, 0xb1, 0x28,
+ 0x65, 0x04, 0xe1, 0xf6, 0x23, 0x88, 0xb3, 0xf7, 0xdd, 0x4f, 0x95, 0x66, 0x97, 0xb0,
+ 0xe8, 0x28, 0xfe, 0x18,
+ ]
+ );
+ assert_eq!(
+ derive_hkdf_bytes(DigestAlgorithm::Sha256, &[], &[], &[], 0),
+ Ok(Vec::new())
+ );
+ }
+}
diff --git a/moli-crypto/src/lib.rs b/moli-crypto/src/lib.rs
index c52fdb80c..959a31ee5 100644
--- a/moli-crypto/src/lib.rs
+++ b/moli-crypto/src/lib.rs
@@ -6,8 +6,10 @@
mod digest;
mod ed25519;
+mod hkdf;
mod random;
pub use digest::{DigestAlgorithm, Sha256Context, sha1_digest, sha256_digest, sha256_hex};
pub use ed25519::{Ed25519Error, Ed25519SigningKey};
+pub use hkdf::{HkdfError, derive_hkdf_bytes};
pub use random::fill_secure_random;
diff --git a/moli-webcrypto/src/hash.rs b/moli-webcrypto/src/hash.rs
index 83c1bf273..7fe827495 100644
--- a/moli-webcrypto/src/hash.rs
+++ b/moli-webcrypto/src/hash.rs
@@ -1,8 +1,5 @@
use moli_crypto::DigestAlgorithm;
-use openssl::{
- hash::MessageDigest,
- md::{Md, MdRef},
-};
+use openssl::hash::MessageDigest;
use crate::{WebCryptoError, limits::ensure_digest_operation_bytes};
@@ -51,7 +48,7 @@ impl WebCryptoHashAlgorithm {
self.digest_algorithm().output_len_bytes()
}
- fn digest_algorithm(self) -> DigestAlgorithm {
+ pub(crate) fn digest_algorithm(self) -> DigestAlgorithm {
match self {
Self::Sha1 => DigestAlgorithm::Sha1,
Self::Sha256 => DigestAlgorithm::Sha256,
@@ -60,8 +57,8 @@ impl WebCryptoHashAlgorithm {
}
}
- // Keep the WebCrypto-to-OpenSSL mapping for algorithms that consume an
- // OpenSSL digest descriptor (HMAC, RSA, HKDF, and PBKDF2) in one place.
+ // Keep the WebCrypto-to-OpenSSL mapping for algorithms that still consume
+ // an OpenSSL digest descriptor (HMAC, RSA, and PBKDF2) in one place.
pub(crate) fn message_digest(self) -> MessageDigest {
match self {
Self::Sha1 => MessageDigest::sha1(),
@@ -70,13 +67,4 @@ impl WebCryptoHashAlgorithm {
Self::Sha512 => MessageDigest::sha512(),
}
}
-
- pub(crate) fn md_ref(self) -> &'static MdRef {
- match self {
- Self::Sha1 => Md::sha1(),
- Self::Sha256 => Md::sha256(),
- Self::Sha384 => Md::sha384(),
- Self::Sha512 => Md::sha512(),
- }
- }
}
diff --git a/moli-webcrypto/src/kdf.rs b/moli-webcrypto/src/kdf.rs
index c5033e40f..7c68faabd 100644
--- a/moli-webcrypto/src/kdf.rs
+++ b/moli-webcrypto/src/kdf.rs
@@ -1,4 +1,5 @@
-use openssl::{pkcs5, pkey::Id, pkey_ctx::PkeyCtx};
+use moli_crypto::derive_hkdf_bytes;
+use openssl::pkcs5;
use crate::limits::ensure_kdf_parameter_bytes;
use crate::{WebCryptoError, WebCryptoHashAlgorithm};
@@ -28,22 +29,8 @@ pub fn derive_hkdf_bits(
return Err(WebCryptoError::Operation);
}
- let mut okm_bytes = vec![0_u8; length_bytes];
- let mut ctx = PkeyCtx::new_id(Id::HKDF).map_err(|_| WebCryptoError::Operation)?;
- ctx.derive_init().map_err(|_| WebCryptoError::Operation)?;
- ctx.set_hkdf_md(hash.md_ref())
- .map_err(|_| WebCryptoError::Operation)?;
- ctx.set_hkdf_key(base_key)
- .map_err(|_| WebCryptoError::Operation)?;
- ctx.set_hkdf_salt(salt)
- .map_err(|_| WebCryptoError::Operation)?;
- if !info.is_empty() {
- ctx.add_hkdf_info(info)
- .map_err(|_| WebCryptoError::Operation)?;
- }
- ctx.derive(Some(&mut okm_bytes))
- .map_err(|_| WebCryptoError::Operation)?;
- Ok(okm_bytes)
+ derive_hkdf_bytes(hash.digest_algorithm(), base_key, salt, info, length_bytes)
+ .map_err(|_| WebCryptoError::Operation)
}
pub fn derive_pbkdf2_bits(