From 6bdc82af93bfa369c4ee2f190d11df20cb115bad Mon Sep 17 00:00:00 2001 From: DeFiDude <59237470+DeFiDude@users.noreply.github.com> Date: Sun, 25 Oct 2026 11:23:44 -0600 Subject: [PATCH] identity: encode ratchet files into a pre-sized buffer and correct key-file notes --- CHANGELOG.md | 2 +- api/migrations/1.4.md | 8 +++-- crates/rns-identity/src/identity.rs | 5 +-- crates/rns-identity/src/ratchet.rs | 52 +++++++++++++---------------- 4 files changed, 32 insertions(+), 35 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eebc4e6..38524b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,7 +32,7 @@ - Remote management Links refuse Resources other than requests and responses, as Python Reticulum's do. - `rnsd-rs` never auto-connects a discovered interface at an onion address, at 127.0.0.1 or 0.0.0.0, or at a host and port that an interface already connects to, as in Python Reticulum. - Interface stats name the network an auto-connected interface was discovered through (`autoconnect_source`), as in Python Reticulum. -- Key material and decrypted data are wiped from memory after use, and identity files larger than 1 KiB are refused. +- Decryption, key derivation and identity and ratchet file handling no longer leave copies of keys or plaintext in freed memory, and identity files larger than 1 KiB are refused. - A 64-byte identity file always loads as a raw key, as in Python Reticulum. About one key in 29,000 began with bytes that were misread as the older envelope format, and its identity failed to load. - Writing, verifying and recovering state files retries a read that a signal interrupts instead of failing. diff --git a/api/migrations/1.4.md b/api/migrations/1.4.md index c65faa2..2cd7fdc 100644 --- a/api/migrations/1.4.md +++ b/api/migrations/1.4.md @@ -115,9 +115,11 @@ rns-link gains the target dependencies `libc` (unix) and `windows-sys` `hkdf::derive_key_64` returns `Zeroizing<[u8; 64]>` and `hkdf::hkdf_sha256` returns `Zeroizing>`, so derived keys are wiped when they are dropped. Pass `&key[..]` where a slice is expected and compare `&okm[..]` with a plain -`Vec`; the bytes are unchanged. `token::decrypt` still returns `Vec` and wipes its buffer -when decryption fails; wrap a secret plaintext in `Zeroizing`. -`Identity::from_file` refuses a file larger than 1 KiB without reading it. +`Vec`; the bytes are unchanged. `Zeroizing<[u8; 64]>` is not `Copy`, so code +that copied the key borrows it instead. `token::decrypt` still returns +`Vec` and wipes its buffer when decryption fails; wrap a secret plaintext +in `Zeroizing`. `Identity::from_file` reads at most 1025 bytes and refuses a +file larger than 1 KiB. rns-crypto enables the `zeroize` features of `aes` and `cbc`, so AES key schedules and CBC state are wiped when dropped; dependents that build with `--locked` refresh their lock files. diff --git a/crates/rns-identity/src/identity.rs b/crates/rns-identity/src/identity.rs index 66a7773..f63c1f5 100644 --- a/crates/rns-identity/src/identity.rs +++ b/crates/rns-identity/src/identity.rs @@ -200,7 +200,7 @@ impl Identity { /// Load an identity from file, accepting either the raw 64-byte private key /// that Python and [`Self::to_file`] write, or the older msgpack envelope. - /// A file larger than 1 KiB is refused without being read. + /// A file larger than 1 KiB is refused once 1025 bytes have been read. pub fn from_file(path: &Path) -> Result { let data = persistence::read_secret_file(path, IDENTITY_FILE_MAX_BYTES)?; @@ -443,7 +443,8 @@ impl fmt::Display for Identity { } } -/// Identity files are a raw key or a small envelope; larger files are refused unread. +/// Identity files are a raw key or a small envelope; reading stops one byte +/// past this limit and refuses the file. const IDENTITY_FILE_MAX_BYTES: usize = 1024; // Msgpack envelope wrapping the raw 64-byte private key on disk. diff --git a/crates/rns-identity/src/ratchet.rs b/crates/rns-identity/src/ratchet.rs index 339bd55..3621fc1 100644 --- a/crates/rns-identity/src/ratchet.rs +++ b/crates/rns-identity/src/ratchet.rs @@ -40,6 +40,21 @@ fn pack_ratchet_keys(keys: &[[u8; 32]]) -> std::io::Result>> { Ok(packed) } +fn encode_ring_file(signature: &[u8; 64], packed: &[u8]) -> std::io::Result>> { + let persisted = RatchetRingPersistedWrite { + signature, + ratchets: packed, + }; + // Sized up front: a growing buffer would free copies of the keys. + let mut buf = Zeroizing::new(Vec::with_capacity(RATCHET_FILE_MAX_BYTES)); + rmp_serde::encode::write_named(&mut *buf, &persisted) + .map_err(|e| std::io::Error::other(e.to_string()))?; + if buf.len() > RATCHET_FILE_MAX_BYTES { + return Err(invalid_data("ratchet ring file exceeds size limit")); + } + Ok(buf) +} + fn unpack_ratchet_keys(packed: &[u8]) -> std::io::Result> { if packed.len() > RATCHET_PACKED_MAX_BYTES { return Err(invalid_data("packed ratchet ring exceeds size limit")); @@ -280,18 +295,7 @@ impl RatchetRing { /// should prefer [`Self::save_verified`], which cannot sign the wrong data. pub fn save(&self, path: &Path, signature: &[u8; 64]) -> std::io::Result<()> { let ratchets_packed = pack_ratchet_keys(&self.keys)?; - - let persisted = Zeroizing::new(RatchetRingPersistedWrite { - signature: signature.to_vec(), - ratchets: ratchets_packed.to_vec(), - }); - let buf = Zeroizing::new( - rmp_serde::to_vec_named(&*persisted) - .map_err(|e| std::io::Error::other(e.to_string()))?, - ); - if buf.len() > RATCHET_FILE_MAX_BYTES { - return Err(invalid_data("ratchet ring file exceeds size limit")); - } + let buf = encode_ring_file(signature, &ratchets_packed)?; persistence::atomic_write(path, &buf) } @@ -321,17 +325,7 @@ impl RatchetRing { ) })?; - let persisted = Zeroizing::new(RatchetRingPersistedWrite { - signature: signature.to_vec(), - ratchets: ratchets_packed.to_vec(), - }); - let buf = Zeroizing::new( - rmp_serde::to_vec_named(&*persisted) - .map_err(|e| std::io::Error::other(e.to_string()))?, - ); - if buf.len() > RATCHET_FILE_MAX_BYTES { - return Err(invalid_data("ratchet ring file exceeds size limit")); - } + let buf = encode_ring_file(&signature, &ratchets_packed)?; if durable { persistence::durable_write(path, &buf) .map(|_| ()) @@ -563,12 +557,12 @@ impl PreparedRatchetRotation { } } -#[derive(Serialize, Zeroize)] -struct RatchetRingPersistedWrite { +#[derive(Serialize)] +struct RatchetRingPersistedWrite<'a> { #[serde(with = "serde_bytes")] - signature: Vec, + signature: &'a [u8; 64], #[serde(with = "serde_bytes")] - ratchets: Vec, + ratchets: &'a [u8], } #[derive(Deserialize)] @@ -1152,8 +1146,8 @@ mod tests { let keys = vec![[0x44; 32]; RATCHET_COUNT + 1]; let packed = rmp_serde::to_vec(&keys).unwrap(); let envelope = RatchetRingPersistedWrite { - signature: vec![0u8; 64], - ratchets: packed, + signature: &[0u8; 64], + ratchets: &packed, }; std::fs::write(&path, rmp_serde::to_vec_named(&envelope).unwrap()).unwrap(); assert_eq!(