tui: F8 rotate prekey + Node::rotate_prekey (hard cut, drops ratchet state)
All checks were successful
ci / test (pull_request) Successful in 5m0s
ci / sdk (pull_request) Successful in 1m11s
android-ci / android (pull_request) Successful in 8m36s

The signed prekey was set at invite creation and never rotated: a long-lived
static is a long-lived forward-secrecy window. F8 now rotates it.

- Node::rotate_prekey(): new X25519 keypair, written through the existing
  replace_identity_keys prekey columns (no store change needed), live Keys
  updated, new pubkey returned for the invite re-encode. Identity untouched.
- It drops every stored ratchet session: the bootstrap epoch is derived from
  the prekey DH, so each friend has to F3-paste the new invite. Nothing is
  pushed to peers \u2014 there is no in-band prekey update.
- TUI: F8 on Main opens the confirm screen (type ROTATE, same prompt as F4),
  then re-opens the F2 share path with the new invite; help overlay + footer
  hint list it.
- Docs: THREAT_MODEL gains a section on the F8 hard cut and the prekey-bound
  epoch bullet; README key table and PROTOCOL 'Roles and keys' follow.

Tests: node-level rotation test (identity/pk/stored row/ratchet drop/invite),
tests/rotate.rs screen text + prompt, tui_chrome help needs F8.
This commit is contained in:
rust-dev 2026-09-17 20:48:10 -04:00
commit 6827b87a29
No known key found for this signature in database
7 changed files with 177 additions and 2 deletions

View file

@ -8,6 +8,7 @@ use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
use futures::StreamExt;
use futures::io::{AsyncRead, AsyncWrite};
use hkdf::Hkdf;
use rand::rngs::OsRng;
use sha2::Sha256;
use tor_cell::relaycell::msg::Connected;
use tor_hsservice::{RunningOnionService, handle_rend_requests};
@ -877,6 +878,34 @@ impl Node {
})
}
/// Rotate the long-lived signed prekey (F8). Hard cut, like the locator
/// rotate: the prekey is our Noise static and the bootstrap ratchet epoch is
/// derived from the prekey DH, so every stored session is dead the moment
/// this returns and every friend has to F3-paste the new invite. Nothing is
/// pushed to peers — a new prekey only travels inside a signed invite. The
/// identity key (and therefore the fingerprint) does not move; the caller
/// re-encodes the F2 invite around the returned pubkey.
pub fn rotate_prekey(&self) -> Result<[u8; 32], String> {
let mut k = self.keys()?;
let sk = StaticSecret::random_from_rng(OsRng);
let pk = X25519Public::from(&sk).to_bytes();
{
let store = self.store.lock().map_err(|e| e.to_string())?;
// `replace_identity_keys` rewrites the prekey columns too; the
// identity values passed back are the ones already stored.
store
.replace_identity_keys(&k.identity_sk, &k.identity_pk, &sk.to_bytes(), &pk)
.map_err(|e| e.to_string())?;
for f in store.list_friends().map_err(|e| e.to_string())? {
store.clear_ratchet(&f.pubkey).map_err(|e| e.to_string())?;
}
}
k.prekey_sk = sk.to_bytes();
k.prekey_pk = pk;
*self.keys.lock().map_err(|e| e.to_string())? = k;
Ok(pk)
}
/// One-shot file to a friend. Fail closed; no outbox, no resume.
/// Chunks are paced (`file::CHUNK_PACE`) with bounded per-chunk retry so
/// a 1 MiB transfer fits the receiver's rendezvous token bucket.
@ -1823,5 +1852,55 @@ mod tests {
"the sender's chat must show the tip they just paid"
);
}
/// F8: rotate the long-lived signed prekey. The identity must not move, the
/// stored/dialed prekey must, every prekey-bound ratchet epoch must be
/// dropped, and the F2 invite must be re-encodable around the new prekey.
#[tokio::test]
async fn rotating_the_prekey_keeps_identity_and_drops_stale_ratchet_state() {
let dir = tempfile::tempdir().unwrap();
let node = offline_node(&dir.path().join("bob"), Arc::new(NoDial));
let peer = Keys::generate();
let old_identity = node.identity_pk();
let old_prekey = node_prekey(&node);
*node.onion.lock().unwrap() = format!("{}.onion", "a".repeat(56));
{
let store = node.store.lock().unwrap();
befriend(&store, &peer.identity_pk, &peer.prekey_pk, "peer.onion");
store
.save_ratchet(&peer.identity_pk, b"epoch-bound-to-the-old-prekey")
.expect("seed ratchet state");
}
let new_prekey = node.rotate_prekey().expect("rotate prekey");
assert_ne!(new_prekey, old_prekey, "the prekey must actually change");
assert_eq!(node_prekey(&node), new_prekey, "the live keys follow");
assert_eq!(
node.identity_pk(),
old_identity,
"identity = pubkey, it stays"
);
{
let store = node.store.lock().unwrap();
assert_eq!(
store.self_identity().unwrap().prekey_pk,
new_prekey.to_vec(),
"the self row carries the new prekey"
);
assert!(
store.load_ratchet(&peer.identity_pk).unwrap().is_none(),
"the bootstrap epoch is prekey-bound, so the stored state is stale"
);
}
let payload = qr::decode(&node.qr_payload().expect("re-encode the invite"))
.expect("the re-encoded invite decodes");
assert_eq!(payload.signed_prekey, new_prekey.to_vec());
assert_eq!(
payload.pubkey,
old_identity.to_vec(),
"the new invite still names the same identity"
);
}
}