identity: encode ratchet files into a pre-sized buffer and correct key-file notes

This commit is contained in:
DeFiDude committed 2026-10-25 11:23:44 -06:00
1 parent 215250d2b2
commit 6bdc82af93
4 files changed
+32 -35

No files matched your search

+1 -1
View File
@@ -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.
+5 -3
View File
@@ -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<Vec<u8>>`, 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<u8>` 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<u8>` 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.
+3 -2
View File
@@ -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<Self, IdentityError> {
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.
+23 -29
View File
@@ -40,6 +40,21 @@ fn pack_ratchet_keys(keys: &[[u8; 32]]) -> std::io::Result<Zeroizing<Vec<u8>>> {
Ok(packed)
}
fn encode_ring_file(signature: &[u8; 64], packed: &[u8]) -> std::io::Result<Zeroizing<Vec<u8>>> {
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<Vec<[u8; 32]>> {
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<u8>,
signature: &'a [u8; 64],
#[serde(with = "serde_bytes")]
ratchets: Vec<u8>,
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!(