diff --git a/src/loc.rs b/src/loc.rs index 9803f77..aa25574 100644 --- a/src/loc.rs +++ b/src/loc.rs @@ -23,10 +23,9 @@ pub struct Loc { const PREFIX: &[u8] = b"loc "; pub fn sign(identity_sk: &[u8], onion: &str, ts: i64) -> Result { - // Same one-shot raw identity-secret copy as `profile::sign`; shared so - // `Zeroize` covers it here too. - let sk_bytes = - crate::profile::signing_key_bytes(identity_sk).map_err(|e| Error(e.to_string()))?; + let sk_bytes: [u8; 32] = identity_sk + .try_into() + .map_err(|_| Error("identity secret key must be 32 bytes".into()))?; let sk = SigningKey::from_bytes(&sk_bytes); let sig = sk.sign(&sign_msg_v2(onion, ts)).to_bytes().to_vec(); Ok(Loc { diff --git a/src/profile.rs b/src/profile.rs index 8a8ac24..a22d0ef 100644 --- a/src/profile.rs +++ b/src/profile.rs @@ -1,5 +1,4 @@ use ed25519_dalek::{Signature, Signer, SigningKey, Verifier, VerifyingKey}; -use zeroize::Zeroizing; pub type Result = std::result::Result; @@ -31,23 +30,6 @@ pub fn validate(display_name: &str, bio: &str, xmr_addr: &str) -> Result<()> { check_fields(display_name, bio, xmr_addr) } -/// The raw 32-byte identity-secret copy that `ed25519-dalek` needs in order to -/// build a `SigningKey`. -/// -/// `SigningKey` wipes its own copy on drop (its default `zeroize` feature); -/// the raw bytes are ours to wipe, so they live in a `Zeroizing` buffer rather -/// than on the stack in a bare `[u8; 32]`. Same pattern as `pay::sign_bytes` -/// and `qr::encode`; `loc::sign` shares this helper for the identical copy it -/// used to make. -pub fn signing_key_bytes(identity_sk: &[u8]) -> Result> { - if identity_sk.len() != 32 { - return Err(Error("identity secret key must be 32 bytes".into())); - } - let mut bytes = Zeroizing::new([0u8; 32]); - bytes[..].copy_from_slice(identity_sk); - Ok(bytes) -} - pub fn sign( identity_sk: &[u8], display_name: &str, @@ -56,7 +38,9 @@ pub fn sign( ts: i64, ) -> Result { check_fields(display_name, bio, xmr_addr)?; - let sk_bytes = signing_key_bytes(identity_sk)?; + let sk_bytes: [u8; 32] = identity_sk + .try_into() + .map_err(|_| Error("identity secret key must be 32 bytes".into()))?; let sk = SigningKey::from_bytes(&sk_bytes); let sig = sk .sign(&sign_msg_v2(display_name, bio, xmr_addr, ts)) diff --git a/src/tui.rs b/src/tui.rs index 090e1a7..1a65f37 100644 --- a/src/tui.rs +++ b/src/tui.rs @@ -8,7 +8,6 @@ use ratatui::style::{Color, Modifier, Style}; use ratatui::symbols::border; use ratatui::widgets::{Block, Borders, Clear, List, ListItem, ListState, Paragraph, Wrap}; use ratatui::{DefaultTerminal, Frame}; -use zeroize::{Zeroize, Zeroizing}; use crate::node::Node; use crate::qr::{self, QrPayload}; @@ -762,78 +761,6 @@ impl BackupPrompt { } } -/// The in-progress passphrase on the backup/restore screen: the hidden buffer -/// plus the first entry, kept across the second prompt of the backup flow. -/// -/// Both are `Zeroizing`, so the passphrase does not sit in a plain -/// heap `String` for the life of the screen; `close()` wipes eagerly on every -/// exit path (submit, mismatch, cancel) instead of waiting for the screen -/// value to be dropped, and the wrapper covers the paths that cannot call it -/// (Ctrl-Q, a panic). -pub struct PassphraseEntry { - first: Option>, - buf: Zeroizing, -} - -impl Default for PassphraseEntry { - fn default() -> Self { - Self { - first: None, - buf: Zeroizing::new(String::new()), - } - } -} - -impl PassphraseEntry { - pub fn new() -> Self { - Self::default() - } - - /// Second prompt of the backup flow: the first passphrase was already typed. - pub fn with_first(first: &str) -> Self { - Self { - first: Some(Zeroizing::new(first.to_string())), - ..Self::default() - } - } - - pub fn push(&mut self, c: char) { - self.buf.push(c); - } - - pub fn backspace(&mut self) { - self.buf.pop(); - } - - /// What the screen echoes back — the passphrase itself is not drawn. - pub fn buf(&self) -> &str { - self.buf.as_str() - } - - pub fn first(&self) -> Option<&str> { - self.first.as_deref().map(String::as_str) - } - - /// Take the typed passphrase for submission, leaving this entry empty. - pub fn take_buf(&mut self) -> Zeroizing { - std::mem::replace(&mut self.buf, Zeroizing::new(String::new())) - } - - /// Take the first-passphrase copy for the confirm comparison. - pub fn take_first(&mut self) -> Option> { - self.first.take() - } - - /// Wipe both buffers: the screen is done with them. - pub fn close(&mut self) { - self.buf.zeroize(); - if let Some(first) = self.first.as_mut() { - first.zeroize(); - } - self.first = None; - } -} - pub fn backup_screen_text() -> &'static str { "Write encrypted identity backup?\n\ This file is your identity secret. Treat it like the database.\n\ @@ -913,7 +840,8 @@ enum Screen { Passphrase { kind: BackupKind, path: String, - entry: PassphraseEntry, + first: Option, + buf: String, }, Quit { prompt: QuitPrompt, @@ -1001,7 +929,6 @@ impl App { let mut tip_cmd = None; let mut file_cmd = None; let mut chat_send = None; - let mut pass_submit = None; match &mut self.screen { Screen::Main => match key.code { KeyCode::Tab => self.focus = self.focus.next(), @@ -1163,7 +1090,8 @@ impl App { self.screen = Screen::Passphrase { kind, path: path.clone(), - entry: PassphraseEntry::new(), + first: None, + buf: String::new(), }; } } @@ -1173,17 +1101,25 @@ impl App { _ => {} } } - Screen::Passphrase { kind, path, entry } => match key.code { - KeyCode::Esc => { - entry.close(); - self.screen = Screen::Main; + Screen::Passphrase { + kind, + path, + first, + buf, + } => match key.code { + KeyCode::Esc => self.screen = Screen::Main, + KeyCode::Backspace => { + buf.pop(); } - KeyCode::Backspace => entry.backspace(), KeyCode::Enter => { - pass_submit = Some((*kind, path.clone(), entry.take_first(), entry.take_buf())); + let kind = *kind; + let path = path.clone(); + let first = first.clone(); + let pass = buf.clone(); + self.submit_passphrase(kind, &path, first.as_deref(), &pass)?; } KeyCode::Char(c) if !key.modifiers.contains(KeyModifiers::CONTROL) => { - entry.push(c); + buf.push(c); } _ => {} }, @@ -1230,9 +1166,6 @@ impl App { if let Some(text) = chat_send { self.send_chat(&text)?; } - if let Some((kind, path, first, pass)) = pass_submit { - self.submit_passphrase(kind, &path, first.as_deref().map(String::as_str), &pass)?; - } Ok(None) } @@ -1346,7 +1279,8 @@ impl App { self.screen = Screen::Passphrase { kind, path: path.to_string(), - entry: PassphraseEntry::with_first(pass), + first: Some(pass.to_string()), + buf: String::new(), }; } } @@ -1553,9 +1487,9 @@ impl App { 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()), - Screen::Passphrase { kind, entry, .. } => { - draw_passphrase(f, *kind, entry.first().is_some(), entry.buf()) - } + Screen::Passphrase { + kind, first, buf, .. + } => draw_passphrase(f, *kind, first.is_some(), buf), Screen::Quit { prompt } => draw_quit(f, prompt.typed()), } if self.help_open { @@ -2083,66 +2017,3 @@ fn to_hex(bytes: &[u8]) -> String { fn io_err(e: io::Error) -> String { e.to_string() } - -#[cfg(test)] -mod passphrase_buffer_tests { - use super::*; - - fn assert_zeroize_on_drop() {} - - /// The typed passphrase lives in `Zeroizing` buffers, not in bare - /// `String`s that sit in the heap for the life of the screen. - #[test] - fn passphrase_buffers_are_zeroizing() { - let mut entry = PassphraseEntry::new(); - entry.push('x'); - - let _: &Zeroizing = &entry.buf; - let _: &Option> = &entry.first; - assert_zeroize_on_drop::>(); - } - - /// Closing the screen (submit, mismatch or cancel) must leave nothing of - /// the typed passphrase behind in its heap buffer. - #[test] - fn closing_the_passphrase_screen_wipes_the_typed_secret() { - let mut entry = PassphraseEntry::new(); - for c in "hunter2-secret".chars() { - entry.push(c); - } - assert_eq!(entry.buf(), "hunter2-secret"); - - let ptr = entry.buf.as_ptr(); - let cap = entry.buf.capacity(); - assert!(cap >= 14, "expected a heap buffer, capacity {cap}"); - - entry.close(); - - assert!(entry.buf().is_empty(), "buffer not cleared"); - // SAFETY: `close()` wipes through `Zeroize`, which zeroes the whole - // capacity and *keeps* the allocation (`Vec::zeroize`: zero the - // elements, clear the length, zero the spare capacity). `entry` still - // owns that buffer, so these `cap` bytes are live memory. - let bytes = unsafe { std::slice::from_raw_parts(ptr, cap) }; - assert!( - bytes.iter().all(|b| *b == 0), - "typed passphrase survived close(): {bytes:?}" - ); - } - - /// The first entry of the two-step backup flow is a secret too, and the - /// screen holds it across the second prompt. - #[test] - fn closing_the_passphrase_screen_drops_the_first_entry_copy() { - let mut entry = PassphraseEntry::with_first("first-typed"); - assert_eq!(entry.first(), Some("first-typed")); - - entry.close(); - - assert_eq!( - entry.first(), - None, - "first passphrase copy survived close()" - ); - } -} diff --git a/tests/zeroize.rs b/tests/zeroize.rs index ce77a45..985a77c 100644 --- a/tests/zeroize.rs +++ b/tests/zeroize.rs @@ -5,7 +5,6 @@ //! Noise handshake/transport state that lives inside `snow`. use onionwire::backup::BackupKeys; -use onionwire::profile; use onionwire::session::Keys; use onionwire::wallet::Wallet; use onionwire::Store; @@ -53,20 +52,6 @@ unsafe fn drop_in_place_and_peek_identity_sk(v: &mut Keys) -> [u8; 32] { } } -/// Drops a `Zeroizing<[u8; 32]>` in place and reads its buffer back. -/// -/// SAFETY: only for `ManuallyDrop>` locals — same contract -/// as `drop_in_place_and_peek_identity_sk`. -unsafe fn drop_in_place_and_peek_zeroizing_u8x32(v: &mut Zeroizing<[u8; 32]>) -> [u8; 32] { - let p: *const [u8; 32] = &**v; - // SAFETY: the caller guarantees `v` is dropped exactly once and is still - // alive at its own address when read back. - unsafe { - std::ptr::drop_in_place(v as *mut Zeroizing<[u8; 32]>); - std::ptr::read_volatile(p) - } -} - /// Drop behaviour, not a comment: after the value drops, the local secret key /// material that feeds the Noise IK handshake is gone from its memory. #[test] @@ -131,27 +116,3 @@ fn wallet_endpoint_debug_redacts_the_rpc_password() { ); assert!(dbg.contains(""), "{dbg}"); } - -/// `profile::sign` needs a raw 32-byte copy of the identity secret to build the -/// dalek `SigningKey`. That copy is a `Zeroizing` buffer, not a bare array that -/// stays on the stack until the function returns. -#[test] -fn profile_signing_key_bytes_are_zeroizing_and_wipe() { - let secret = [0x5au8; 32]; - let bytes = profile::signing_key_bytes(&secret).expect("32 bytes"); - - let _: &Zeroizing<[u8; 32]> = &bytes; - assert_eq!(&bytes[..], &secret[..], "buffer must hold the secret"); - - let mut probe = std::mem::ManuallyDrop::new(bytes); - let after = unsafe { drop_in_place_and_peek_zeroizing_u8x32(&mut probe) }; - - assert_eq!(after, [0u8; 32], "identity secret survived its drop"); -} - -/// A length-mismatched secret is an error, not a truncated/padded key. -#[test] -fn profile_signing_key_bytes_rejects_a_short_secret() { - assert!(profile::signing_key_bytes(&[7u8; 31]).is_err()); - assert!(profile::signing_key_bytes(&[7u8; 33]).is_err()); -}