Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

refactor(security): delegate keyring crypto to tinystoragedrivers

Steven Enamakelf33c38aca7b3
diff --git a/crates/openhuman-core/src/security/keyring/crypto.rs b/crates/openhuman-core/src/security/keyring/crypto.rs
index 47a58b0101..d1e2570b10 100644
--- a/crates/openhuman-core/src/security/keyring/crypto.rs
+++ b/crates/openhuman-core/src/security/keyring/crypto.rs
@@ -3,24 +3,19 @@
//! Used by both [`super::encrypted_store::SecretStore`] (config field encryption)
//! and [`super::encrypted_file_backend::EncryptedFileBackend`] (secrets file encryption).
-use chacha20poly1305::aead::{Aead, KeyInit, OsRng};
-use chacha20poly1305::{AeadCore, ChaCha20Poly1305, Key, Nonce};
+use chacha20poly1305::aead::OsRng;
+use tinystoragedrivers::secrets::crypto as secrets_crypto;
pub(super) const NONCE_LEN: usize = 12;
pub(super) const KEY_LEN: usize = 32;
/// Encrypt `plaintext` with ChaCha20-Poly1305. Returns `nonce || ciphertext || tag`.
+///
+/// The cipher is `tinystoragedrivers`' (`secrets::crypto`), the same format
+/// its secret stores read and write.
pub(super) fn chacha20_encrypt(key: &[u8; KEY_LEN], plaintext: &[u8]) -> Result<Vec<u8>, String> {
- let cipher = ChaCha20Poly1305::new(Key::from_slice(key));
- let nonce = ChaCha20Poly1305::generate_nonce(&mut OsRng);
- let ciphertext = cipher
- .encrypt(&nonce, plaintext)
- .map_err(|e| format!("ChaCha20 encryption failed: {e}"))?;
-
- let mut blob = Vec::with_capacity(NONCE_LEN + ciphertext.len());
- blob.extend_from_slice(&nonce);
- blob.extend_from_slice(&ciphertext);
- Ok(blob)
+ secrets_crypto::encrypt(key, plaintext)
+ .map_err(|e| format!("ChaCha20 encryption failed: {e}"))
}
/// Decrypt a `nonce || ciphertext || tag` blob produced by [`chacha20_encrypt`].
@@ -28,11 +23,8 @@ pub(super) fn chacha20_decrypt(key: &[u8; KEY_LEN], blob: &[u8]) -> Result<Vec<u
if blob.len() <= NONCE_LEN {
return Err("encrypted blob too short (missing nonce)".to_string());
}
- let (nonce_bytes, ciphertext) = blob.split_at(NONCE_LEN);
- let nonce = Nonce::from_slice(nonce_bytes);
- let cipher = ChaCha20Poly1305::new(Key::from_slice(key));
- cipher
- .decrypt(nonce, ciphertext)
+ secrets_crypto::decrypt(key, blob)
+ .map(|plaintext| plaintext.to_vec())
.map_err(|_| "decryption failed — wrong key or tampered data".to_string())
}
diff --git a/crates/openhuman-core/src/security/keyring/encrypted_store.rs b/crates/openhuman-core/src/security/keyring/encrypted_store.rs
index e39a2ebf7c..5fe22703b6 100644
--- a/crates/openhuman-core/src/security/keyring/encrypted_store.rs
+++ b/crates/openhuman-core/src/security/keyring/encrypted_store.rs
@@ -74,21 +74,9 @@ impl SecretStore {
return Ok(plaintext.to_string());
}
- let key_bytes = self.load_or_create_key()?;
- let key = Key::from_slice(&key_bytes);
- let cipher = ChaCha20Poly1305::new(key);
-
- let nonce = ChaCha20Poly1305::generate_nonce(&mut OsRng);
- let ciphertext = cipher
- .encrypt(&nonce, plaintext.as_bytes())
- .map_err(|e| anyhow::anyhow!("Encryption failed: {e}"))?;
-
- // Prepend nonce to ciphertext for storage
- let mut blob = Vec::with_capacity(NONCE_LEN + ciphertext.len());
- blob.extend_from_slice(&nonce);
- blob.extend_from_slice(&ciphertext);
-
- Ok(format!("enc2:{}", hex_encode(&blob)))
+ let key = self.cipher_key()?;
+ tinystoragedrivers::secrets::crypto::encrypt_enc2(&key, plaintext.as_bytes())
+ .map_err(|e| anyhow::anyhow!("Encryption failed: {e}"))
}
/// Decrypt a secret.
@@ -148,21 +136,26 @@ impl SecretStore {
blob.len() > NONCE_LEN,
"Encrypted value too short (missing nonce)"
);
-
- let (nonce_bytes, ciphertext) = blob.split_at(NONCE_LEN);
- let nonce = Nonce::from_slice(nonce_bytes);
- let key_bytes = self.load_or_create_key()?;
- let key = Key::from_slice(&key_bytes);
- let cipher = ChaCha20Poly1305::new(key);
-
- let plaintext_bytes = cipher
- .decrypt(nonce, ciphertext)
+ let key = self.cipher_key()?;
+ let plaintext_bytes = tinystoragedrivers::secrets::crypto::decrypt(&key, &blob)
.map_err(|_| anyhow::anyhow!("Decryption failed — wrong key or tampered data"))?;
- String::from_utf8(plaintext_bytes)
+ String::from_utf8(plaintext_bytes.to_vec())
.context("Decrypted secret is not valid UTF-8 — corrupt data")
}
+ /// The config encryption key as the fixed-size array the cipher takes.
+ fn cipher_key(&self) -> Result<Zeroizing<[u8; KEY_LEN]>> {
+ let bytes = self.load_or_create_key()?;
+ let key: [u8; KEY_LEN] = bytes.as_slice().try_into().map_err(|_| {
+ anyhow::anyhow!(
+ "config encryption key is {} bytes, expected {KEY_LEN}",
+ bytes.len()
+ )
+ })?;
+ Ok(Zeroizing::new(key))
+ }
+
/// Decrypt using legacy XOR cipher (insecure, for backward compatibility only).
///
/// This is the single choke-point for the insecure `enc:` format, so it logs