Merge pull request 't_2fb507c6: F8 rotate prekey (Node::rotate_prekey + invite re-encode)' (#101) from wt/t_2fb507c6 into main
Some checks failed
android-ci / android (push) Successful in 8m28s
ci / test (push) Has been cancelled
ci / sdk (push) Has been cancelled
ci / fuzz (push) Has been cancelled

Reviewed-on: #101
This commit is contained in:
sirius 2026-09-18 01:21:45 +00:00
commit 24b5c93c43
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"
);
}
}

View file

@ -491,6 +491,17 @@ Offline friends CANNOT find you until they F3-paste your new invite.\n\
Type ROTATE to confirm Esc to cancel"
}
/// F8. Hard cut like the locator rotate: the prekey is our Noise static and the
/// ratchet bootstrap is derived from the prekey DH, so nothing short of a fresh
/// invite re-syncs a friend.
pub fn prekey_screen_text() -> &'static str {
"Rotate prekey?\n\
Your identity and onion stay the same.\n\
Every friend must F3-paste your new invite to pick up the new prekey,\n\
or you cannot reach them and they cannot reach you.\n\
Type ROTATE to confirm Esc to cancel"
}
pub fn quit_screen_text() -> &'static str {
"Quit OnionWire?\n\
Message history can be cleared; identity and friends stay.\n\
@ -535,6 +546,7 @@ OnionWire keys\n\
F4 rotate onion locator\n\
F5 selected friend's profile (/who)\n\
F6 wallet (receive QR, create/import)\n\
F8 rotate prekey\n\
/profile edit name, bio, Monero address\n\
/pay <xmr> [memo] invoice to receive\n\
/tip <xmr> [memo] pay selected friend\n\
@ -1185,6 +1197,9 @@ enum Screen {
Rotate {
prompt: RotatePrompt,
},
RotatePrekey {
prompt: RotatePrompt,
},
Wipe {
kind: WipeKind,
prompt: WipePrompt,
@ -1324,6 +1339,11 @@ impl App {
}
KeyCode::F(5) => self.who_open = true,
KeyCode::F(6) => self.open_wallet()?,
KeyCode::F(8) => {
self.screen = Screen::RotatePrekey {
prompt: RotatePrompt::new(),
};
}
KeyCode::Esc => self.composer.clear(),
KeyCode::Backspace => {
if self.focus == Pane::Composer {
@ -1443,6 +1463,18 @@ impl App {
}
_ => {}
},
Screen::RotatePrekey { prompt } => match key.code {
KeyCode::Esc => self.screen = Screen::Main,
KeyCode::Char(c) if !key.modifiers.contains(KeyModifiers::CONTROL) => {
if prompt.on_char(c) == RotateDecision::Confirm {
self.confirm_rotate_prekey()?;
}
}
KeyCode::Backspace => {
prompt.on_char('\u{8}');
}
_ => {}
},
Screen::Wipe { kind, prompt } => {
let kind = *kind;
match key.code {
@ -1715,6 +1747,24 @@ impl App {
}
}
/// F8. Local and synchronous: nothing is pushed to peers — the new prekey
/// only travels inside a fresh signed invite — so the screen goes straight
/// to that invite (the F2 share path) for the user to hand out.
fn confirm_rotate_prekey(&mut self) -> Result<(), String> {
match self.node.rotate_prekey() {
Ok(_) => {
self.status_note =
Some("prekey rotated · friends must F3-paste your new invite".into());
self.open_share()
}
Err(e) => {
self.alert = Some(e);
self.screen = Screen::Main;
Ok(())
}
}
}
fn open_share(&mut self) -> Result<(), String> {
let payload = self.node.qr_payload()?;
self.screen = Screen::Share { payload };
@ -2092,6 +2142,7 @@ impl App {
Screen::Paste { buf, err } => draw_paste(f, buf, err.as_deref()),
Screen::Approve { payload } => draw_approve(f, payload),
Screen::Rotate { prompt } => draw_rotate(f, prompt.typed()),
Screen::RotatePrekey { prompt } => draw_rotate_prekey(f, prompt.typed()),
Screen::Wipe { kind, prompt } => draw_wipe(f, *kind, prompt.typed()),
Screen::Profile { editor } => draw_profile(f, editor),
Screen::ConfirmKeys { kind, prompt, .. } => draw_confirm_keys(f, *kind, prompt.typed()),
@ -2410,6 +2461,7 @@ match screen {
Screen::Paste { .. } => "Enter submit Esc back ? help",
Screen::Approve { .. } => "y accept n reject ? help",
Screen::Rotate { .. } => "type ROTATE Esc cancel ? help",
Screen::RotatePrekey { .. } => "type ROTATE Esc cancel ? help",
Screen::Wipe { kind, .. } => match kind {
WipeKind::Messages => "type WIPE Esc cancel ? help",
WipeKind::All => "type WIPEALL Esc cancel ? help",
@ -2510,6 +2562,17 @@ fn draw_rotate(f: &mut Frame, typed: &str) {
);
}
fn draw_rotate_prekey(f: &mut Frame, typed: &str) {
let body = format!("{}\n\n{typed}", prekey_screen_text());
f.render_widget(
Paragraph::new(body)
.style(style_text())
.wrap(Wrap { trim: false })
.block(themed_block("(o) rotate prekey")),
f.area(),
);
}
fn draw_wipe(f: &mut Frame, kind: WipeKind, typed: &str) {
let title = match kind {
WipeKind::Messages => "(o) wipe",