fix: harden channel access and refresh server dependencies

This commit is contained in:
Loki 2026-10-24 13:57:29 +02:00
parent 84df32a9de
commit b14f036bdb
Signed by: boba
GPG key ID: 253067914055423B
23 changed files with 1242 additions and 720 deletions

View file

@ -16,12 +16,12 @@ pub(super) fn encrypt_with_key(
seq: u64,
plaintext: &[u8],
) -> Vec<u8> {
let cipher = ChaCha20Poly1305::new(Key::from_slice(key));
let cipher = ChaCha20Poly1305::new(&Key::from(*key));
let nonce_bytes = make_nonce(cid, seq);
let nonce = Nonce::from_slice(&nonce_bytes);
let nonce = Nonce::from(nonce_bytes);
cipher
.encrypt(
nonce,
&nonce,
Payload {
msg: plaintext,
aad: &[],
@ -36,12 +36,12 @@ pub(super) fn decrypt_with_key(
seq: u64,
ciphertext: &[u8],
) -> anyhow::Result<Vec<u8>> {
let cipher = ChaCha20Poly1305::new(Key::from_slice(key));
let cipher = ChaCha20Poly1305::new(&Key::from(*key));
let nonce_bytes = make_nonce(cid, seq);
let nonce = Nonce::from_slice(&nonce_bytes);
let nonce = Nonce::from(nonce_bytes);
cipher
.decrypt(
nonce,
&nonce,
Payload {
msg: ciphertext,
aad: &[],
@ -49,3 +49,36 @@ pub(super) fn decrypt_with_key(
)
.map_err(|e| anyhow::anyhow!("decryption failed: {e:?}"))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn wire_ciphertext_matches_independent_chacha20_poly1305_vector() {
let key = std::array::from_fn(|index| index as u8);
let cid = [0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43];
let seq = 0x4746_4544;
let plaintext = b"VNOX dependency migration";
// Cross-checked with Python cryptography's ChaCha20Poly1305 implementation,
// IETF nonce 070000004041424344454647, and empty associated data.
let expected = hex::decode(
"1fa23e7a312509b0fc8729521811348ff880daa5598c43a57635975f81278abdf4461b25af86aee1c3",
)
.unwrap();
assert_eq!(encrypt_with_key(&key, &cid, seq, plaintext), expected);
assert_eq!(
decrypt_with_key(&key, &cid, seq, &expected).unwrap(),
plaintext
);
}
#[test]
fn tampered_ciphertext_is_rejected() {
let key = [7; 32];
let cid = [9; 8];
let mut ciphertext = encrypt_with_key(&key, &cid, 42, b"authenticated");
ciphertext[0] ^= 1;
assert!(decrypt_with_key(&key, &cid, 42, &ciphertext).is_err());
}
}

View file

@ -43,7 +43,7 @@ impl SessionCrypto {
}
pub fn new_ephemeral() -> (EphemeralSecret, PublicKey) {
let mut rng = rand::thread_rng();
let mut rng = rand::rng();
let sk = EphemeralSecret::random_from_rng(&mut rng);
let pk = PublicKey::from(&sk);
(sk, pk)
@ -96,7 +96,7 @@ mod tests {
#[test]
fn ecdh_derives_same_secret_on_both_sides() {
let mut rng = rand::thread_rng();
let mut rng = rand::rng();
let alice_sk = EphemeralSecret::random_from_rng(&mut rng);
let alice_pk = PublicKey::from(&alice_sk);