2026-09-10 01:54:19 -04:00
|
|
|
//! M2 QR: roundtrip, garbage → Err, same-k rescan does not duplicate.
|
|
|
|
|
|
|
|
|
|
use std::sync::{Mutex, MutexGuard};
|
|
|
|
|
|
2026-09-10 17:19:50 -04:00
|
|
|
use onionwire::qr;
|
2026-09-10 19:52:44 -04:00
|
|
|
use onionwire::Store;
|
2026-09-10 01:54:19 -04:00
|
|
|
|
|
|
|
|
static ENV_LOCK: Mutex<()> = Mutex::new(());
|
|
|
|
|
|
|
|
|
|
struct TempHome {
|
|
|
|
|
_dir: tempfile::TempDir,
|
|
|
|
|
_guard: MutexGuard<'static, ()>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl TempHome {
|
|
|
|
|
fn new() -> Self {
|
|
|
|
|
let dir = tempfile::tempdir().expect("tempdir");
|
2026-09-10 19:52:44 -04:00
|
|
|
let guard = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
|
2026-09-10 01:54:19 -04:00
|
|
|
unsafe {
|
|
|
|
|
std::env::set_var("ONIONWIRE_HOME", dir.path());
|
2026-09-10 17:19:50 -04:00
|
|
|
std::env::set_var("ONIONWIRE_STORE_PASSPHRASE", "onionwire-test");
|
2026-09-10 01:54:19 -04:00
|
|
|
}
|
|
|
|
|
Self {
|
|
|
|
|
_dir: dir,
|
|
|
|
|
_guard: guard,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Drop for TempHome {
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
unsafe {
|
|
|
|
|
std::env::remove_var("ONIONWIRE_HOME");
|
2026-09-10 17:19:50 -04:00
|
|
|
std::env::remove_var("ONIONWIRE_STORE_PASSPHRASE");
|
2026-09-10 01:54:19 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn v3onion(tag: &str) -> String {
|
2026-09-10 19:52:44 -04:00
|
|
|
let mut addr = vec![b'a'; 56];
|
|
|
|
|
let bytes: Vec<u8> = tag
|
|
|
|
|
.bytes()
|
|
|
|
|
.map(|b| match b {
|
|
|
|
|
b'a'..=b'z' | b'2'..=b'7' => b,
|
|
|
|
|
_ => b'a',
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
let n = bytes.len().min(56);
|
|
|
|
|
addr[..n].copy_from_slice(&bytes[..n]);
|
|
|
|
|
format!("{}.onion", String::from_utf8(addr).expect("base32"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn invite_fields(raw: &str) -> (String, String, String, String) {
|
transport: ML-KEM-768 hybrid prelude and onionwire:v2 invites
Adds an ML-KEM-768 install key beside the ed25519 identity and x25519
prekey. A friend added from a v2 invite always gets a version-2 PQ
prelude before Noise msg 1, and both sides bind (ct, ss_kem) into the
Noise prologue (onionwire-v2 \0 ct \0 ss_kem). The row decides: a
kem_pk friend without a prelude is rejected with 'prelude required',
a prelude for a classical row with 'unexpected prelude'.
Invites are now onionwire:v2 with kem=<base64url pk>; decode still
accepts v1 (no kem) and yields a classical friend. F8 rotates the
KEM key with the prekey and hard-cuts stored ratchet state. The TUI
labels a legacy friend 'classical'.
Docs: PROTOCOL (keys, framing, prologues, no-downgrade, invite v2),
THREAT_MODEL (what PQ transport does and does not buy; epoch 0 stays
classical), README (invite size, F8, legacy caveat). SDK exposes pq on
InviteInfo/FriendInfo and gains a v2 decode round-trip test.
2026-10-20 11:15:46 -04:00
|
|
|
let rest = raw.strip_prefix("onionwire:v2:").or_else(|| raw.strip_prefix("onionwire:v1:")).expect("prefix");
|
2026-09-10 19:52:44 -04:00
|
|
|
let mut k = String::new();
|
|
|
|
|
let mut o = String::new();
|
|
|
|
|
let mut spk = String::new();
|
|
|
|
|
let mut sig = String::new();
|
|
|
|
|
for part in rest.split(':') {
|
|
|
|
|
if let Some(v) = part.strip_prefix("k=") {
|
|
|
|
|
k = v.to_string();
|
|
|
|
|
} else if let Some(v) = part.strip_prefix("o=") {
|
|
|
|
|
o = v.to_string();
|
|
|
|
|
} else if let Some(v) = part.strip_prefix("spk=") {
|
|
|
|
|
spk = v.to_string();
|
|
|
|
|
} else if let Some(v) = part.strip_prefix("sig=") {
|
|
|
|
|
sig = v.to_string();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
(k, o, spk, sig)
|
2026-09-10 01:54:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn encode_decode_roundtrip() {
|
|
|
|
|
let _home = TempHome::new();
|
|
|
|
|
let store = Store::open().expect("open");
|
|
|
|
|
let me = store.self_identity().expect("self");
|
|
|
|
|
let spk = [7u8; 32];
|
|
|
|
|
let onion = v3onion("alice-locator");
|
transport: ML-KEM-768 hybrid prelude and onionwire:v2 invites
Adds an ML-KEM-768 install key beside the ed25519 identity and x25519
prekey. A friend added from a v2 invite always gets a version-2 PQ
prelude before Noise msg 1, and both sides bind (ct, ss_kem) into the
Noise prologue (onionwire-v2 \0 ct \0 ss_kem). The row decides: a
kem_pk friend without a prelude is rejected with 'prelude required',
a prelude for a classical row with 'unexpected prelude'.
Invites are now onionwire:v2 with kem=<base64url pk>; decode still
accepts v1 (no kem) and yields a classical friend. F8 rotates the
KEM key with the prekey and hard-cuts stored ratchet state. The TUI
labels a legacy friend 'classical'.
Docs: PROTOCOL (keys, framing, prologues, no-downgrade, invite v2),
THREAT_MODEL (what PQ transport does and does not buy; epoch 0 stays
classical), README (invite size, F8, legacy caveat). SDK exposes pq on
InviteInfo/FriendInfo and gains a v2 decode round-trip test.
2026-10-20 11:15:46 -04:00
|
|
|
let raw = qr::encode(&me.identity_sk, &onion, &spk, &me.kem_pk).expect("encode");
|
|
|
|
|
assert!(raw.starts_with("onionwire:v2:k="), "payload prefix: {raw}");
|
|
|
|
|
assert!(raw.contains(":kem="));
|
2026-09-10 01:54:19 -04:00
|
|
|
assert!(raw.contains(":o="));
|
|
|
|
|
assert!(raw.contains(":spk="));
|
|
|
|
|
assert!(raw.contains(":sig="));
|
|
|
|
|
|
|
|
|
|
let p = qr::decode(&raw).expect("decode");
|
|
|
|
|
assert_eq!(p.pubkey, me.identity_pk);
|
|
|
|
|
assert_eq!(p.onion, onion);
|
|
|
|
|
assert_eq!(p.signed_prekey, spk);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn garbage_is_err() {
|
|
|
|
|
assert!(qr::decode("not a qr").is_err());
|
|
|
|
|
assert!(qr::decode("").is_err());
|
|
|
|
|
assert!(qr::decode("onionwire:v1:k=aa").is_err());
|
|
|
|
|
assert!(qr::decode("🔥").is_err());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn missing_sig_is_err() {
|
|
|
|
|
let _home = TempHome::new();
|
|
|
|
|
let store = Store::open().expect("open");
|
|
|
|
|
let me = store.self_identity().expect("self");
|
transport: ML-KEM-768 hybrid prelude and onionwire:v2 invites
Adds an ML-KEM-768 install key beside the ed25519 identity and x25519
prekey. A friend added from a v2 invite always gets a version-2 PQ
prelude before Noise msg 1, and both sides bind (ct, ss_kem) into the
Noise prologue (onionwire-v2 \0 ct \0 ss_kem). The row decides: a
kem_pk friend without a prelude is rejected with 'prelude required',
a prelude for a classical row with 'unexpected prelude'.
Invites are now onionwire:v2 with kem=<base64url pk>; decode still
accepts v1 (no kem) and yields a classical friend. F8 rotates the
KEM key with the prekey and hard-cuts stored ratchet state. The TUI
labels a legacy friend 'classical'.
Docs: PROTOCOL (keys, framing, prologues, no-downgrade, invite v2),
THREAT_MODEL (what PQ transport does and does not buy; epoch 0 stays
classical), README (invite size, F8, legacy caveat). SDK exposes pq on
InviteInfo/FriendInfo and gains a v2 decode round-trip test.
2026-10-20 11:15:46 -04:00
|
|
|
let raw = qr::encode(&me.identity_sk, &v3onion("x"), &[1u8; 32], &me.kem_pk).expect("encode");
|
2026-09-10 01:54:19 -04:00
|
|
|
let stripped = raw.rsplit_once(":sig=").expect("sig field").0;
|
|
|
|
|
assert!(qr::decode(stripped).is_err());
|
|
|
|
|
assert!(qr::decode(&format!("{stripped}:sig=")).is_err());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn bad_sig_is_err() {
|
|
|
|
|
let _home = TempHome::new();
|
|
|
|
|
let store = Store::open().expect("open");
|
|
|
|
|
let me = store.self_identity().expect("self");
|
transport: ML-KEM-768 hybrid prelude and onionwire:v2 invites
Adds an ML-KEM-768 install key beside the ed25519 identity and x25519
prekey. A friend added from a v2 invite always gets a version-2 PQ
prelude before Noise msg 1, and both sides bind (ct, ss_kem) into the
Noise prologue (onionwire-v2 \0 ct \0 ss_kem). The row decides: a
kem_pk friend without a prelude is rejected with 'prelude required',
a prelude for a classical row with 'unexpected prelude'.
Invites are now onionwire:v2 with kem=<base64url pk>; decode still
accepts v1 (no kem) and yields a classical friend. F8 rotates the
KEM key with the prekey and hard-cuts stored ratchet state. The TUI
labels a legacy friend 'classical'.
Docs: PROTOCOL (keys, framing, prologues, no-downgrade, invite v2),
THREAT_MODEL (what PQ transport does and does not buy; epoch 0 stays
classical), README (invite size, F8, legacy caveat). SDK exposes pq on
InviteInfo/FriendInfo and gains a v2 decode round-trip test.
2026-10-20 11:15:46 -04:00
|
|
|
let raw = qr::encode(&me.identity_sk, &v3onion("x"), &[1u8; 32], &me.kem_pk).expect("encode");
|
2026-09-10 01:54:19 -04:00
|
|
|
let (head, sig) = raw.rsplit_once(":sig=").expect("sig field");
|
|
|
|
|
let mut chars: Vec<char> = sig.chars().collect();
|
|
|
|
|
let last = chars.last_mut().expect("sig chars");
|
|
|
|
|
*last = if *last == '0' { '1' } else { '0' };
|
|
|
|
|
let flipped: String = chars.into_iter().collect();
|
|
|
|
|
assert!(qr::decode(&format!("{head}:sig={flipped}")).is_err());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn same_k_rescan_updates_onion_not_duplicate() {
|
|
|
|
|
let _home = TempHome::new();
|
|
|
|
|
let store = Store::open().expect("open");
|
|
|
|
|
let me = store.self_identity().expect("self");
|
|
|
|
|
let spk = [9u8; 32];
|
transport: ML-KEM-768 hybrid prelude and onionwire:v2 invites
Adds an ML-KEM-768 install key beside the ed25519 identity and x25519
prekey. A friend added from a v2 invite always gets a version-2 PQ
prelude before Noise msg 1, and both sides bind (ct, ss_kem) into the
Noise prologue (onionwire-v2 \0 ct \0 ss_kem). The row decides: a
kem_pk friend without a prelude is rejected with 'prelude required',
a prelude for a classical row with 'unexpected prelude'.
Invites are now onionwire:v2 with kem=<base64url pk>; decode still
accepts v1 (no kem) and yields a classical friend. F8 rotates the
KEM key with the prekey and hard-cuts stored ratchet state. The TUI
labels a legacy friend 'classical'.
Docs: PROTOCOL (keys, framing, prologues, no-downgrade, invite v2),
THREAT_MODEL (what PQ transport does and does not buy; epoch 0 stays
classical), README (invite size, F8, legacy caveat). SDK exposes pq on
InviteInfo/FriendInfo and gains a v2 decode round-trip test.
2026-10-20 11:15:46 -04:00
|
|
|
let p1 = qr::decode(&qr::encode(&me.identity_sk, &v3onion("old"), &spk, &me.kem_pk).unwrap()).unwrap();
|
2026-09-10 01:54:19 -04:00
|
|
|
store
|
|
|
|
|
.upsert_friend(&p1.pubkey, &p1.onion, Some("alice"))
|
|
|
|
|
.unwrap();
|
transport: ML-KEM-768 hybrid prelude and onionwire:v2 invites
Adds an ML-KEM-768 install key beside the ed25519 identity and x25519
prekey. A friend added from a v2 invite always gets a version-2 PQ
prelude before Noise msg 1, and both sides bind (ct, ss_kem) into the
Noise prologue (onionwire-v2 \0 ct \0 ss_kem). The row decides: a
kem_pk friend without a prelude is rejected with 'prelude required',
a prelude for a classical row with 'unexpected prelude'.
Invites are now onionwire:v2 with kem=<base64url pk>; decode still
accepts v1 (no kem) and yields a classical friend. F8 rotates the
KEM key with the prekey and hard-cuts stored ratchet state. The TUI
labels a legacy friend 'classical'.
Docs: PROTOCOL (keys, framing, prologues, no-downgrade, invite v2),
THREAT_MODEL (what PQ transport does and does not buy; epoch 0 stays
classical), README (invite size, F8, legacy caveat). SDK exposes pq on
InviteInfo/FriendInfo and gains a v2 decode round-trip test.
2026-10-20 11:15:46 -04:00
|
|
|
let p2 = qr::decode(&qr::encode(&me.identity_sk, &v3onion("new"), &spk, &me.kem_pk).unwrap()).unwrap();
|
2026-09-10 01:54:19 -04:00
|
|
|
store.upsert_friend(&p2.pubkey, &p2.onion, None).unwrap();
|
|
|
|
|
assert_eq!(store.friend_count().unwrap(), 1);
|
|
|
|
|
let f = store.get_friend(&p2.pubkey).unwrap().unwrap();
|
|
|
|
|
assert_eq!(f.onion, v3onion("new"));
|
|
|
|
|
assert_eq!(f.petname.as_deref(), Some("alice"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn unknown_k_decode_does_not_insert() {
|
|
|
|
|
let _home = TempHome::new();
|
|
|
|
|
let store = Store::open().expect("open");
|
|
|
|
|
let me = store.self_identity().expect("self");
|
transport: ML-KEM-768 hybrid prelude and onionwire:v2 invites
Adds an ML-KEM-768 install key beside the ed25519 identity and x25519
prekey. A friend added from a v2 invite always gets a version-2 PQ
prelude before Noise msg 1, and both sides bind (ct, ss_kem) into the
Noise prologue (onionwire-v2 \0 ct \0 ss_kem). The row decides: a
kem_pk friend without a prelude is rejected with 'prelude required',
a prelude for a classical row with 'unexpected prelude'.
Invites are now onionwire:v2 with kem=<base64url pk>; decode still
accepts v1 (no kem) and yields a classical friend. F8 rotates the
KEM key with the prekey and hard-cuts stored ratchet state. The TUI
labels a legacy friend 'classical'.
Docs: PROTOCOL (keys, framing, prologues, no-downgrade, invite v2),
THREAT_MODEL (what PQ transport does and does not buy; epoch 0 stays
classical), README (invite size, F8, legacy caveat). SDK exposes pq on
InviteInfo/FriendInfo and gains a v2 decode round-trip test.
2026-10-20 11:15:46 -04:00
|
|
|
let raw = qr::encode(&me.identity_sk, &v3onion("x"), &[1u8; 32], &me.kem_pk).unwrap();
|
2026-09-10 01:54:19 -04:00
|
|
|
let _p = qr::decode(&raw).unwrap();
|
|
|
|
|
assert_eq!(store.friend_count().unwrap(), 0);
|
|
|
|
|
}
|
2026-09-10 19:52:44 -04:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn shifted_o_spk_is_err() {
|
|
|
|
|
let _home = TempHome::new();
|
|
|
|
|
let store = Store::open().expect("open");
|
|
|
|
|
let me = store.self_identity().expect("self");
|
|
|
|
|
let onion = v3onion("honest");
|
|
|
|
|
let spk = [0xab; 32];
|
transport: ML-KEM-768 hybrid prelude and onionwire:v2 invites
Adds an ML-KEM-768 install key beside the ed25519 identity and x25519
prekey. A friend added from a v2 invite always gets a version-2 PQ
prelude before Noise msg 1, and both sides bind (ct, ss_kem) into the
Noise prologue (onionwire-v2 \0 ct \0 ss_kem). The row decides: a
kem_pk friend without a prelude is rejected with 'prelude required',
a prelude for a classical row with 'unexpected prelude'.
Invites are now onionwire:v2 with kem=<base64url pk>; decode still
accepts v1 (no kem) and yields a classical friend. F8 rotates the
KEM key with the prekey and hard-cuts stored ratchet state. The TUI
labels a legacy friend 'classical'.
Docs: PROTOCOL (keys, framing, prologues, no-downgrade, invite v2),
THREAT_MODEL (what PQ transport does and does not buy; epoch 0 stays
classical), README (invite size, F8, legacy caveat). SDK exposes pq on
InviteInfo/FriendInfo and gains a v2 decode round-trip test.
2026-10-20 11:15:46 -04:00
|
|
|
let raw = qr::encode(&me.identity_sk, &onion, &spk, &me.kem_pk).expect("encode");
|
2026-09-10 19:52:44 -04:00
|
|
|
let (k, o, spk_hex, sig) = invite_fields(&raw);
|
|
|
|
|
assert_eq!(spk_hex.len(), 64);
|
|
|
|
|
let mutant = format!(
|
|
|
|
|
"onionwire:v1:k={k}:o={o}{}:spk={}:sig={sig}",
|
|
|
|
|
&spk_hex[..8],
|
|
|
|
|
&spk_hex[8..]
|
|
|
|
|
);
|
|
|
|
|
assert!(
|
|
|
|
|
qr::decode(&mutant).is_err(),
|
|
|
|
|
"shifted o/spk must not verify"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn encode_rejects_spk_not_32() {
|
|
|
|
|
let _home = TempHome::new();
|
|
|
|
|
let store = Store::open().expect("open");
|
|
|
|
|
let me = store.self_identity().expect("self");
|
|
|
|
|
let onion = v3onion("spk");
|
transport: ML-KEM-768 hybrid prelude and onionwire:v2 invites
Adds an ML-KEM-768 install key beside the ed25519 identity and x25519
prekey. A friend added from a v2 invite always gets a version-2 PQ
prelude before Noise msg 1, and both sides bind (ct, ss_kem) into the
Noise prologue (onionwire-v2 \0 ct \0 ss_kem). The row decides: a
kem_pk friend without a prelude is rejected with 'prelude required',
a prelude for a classical row with 'unexpected prelude'.
Invites are now onionwire:v2 with kem=<base64url pk>; decode still
accepts v1 (no kem) and yields a classical friend. F8 rotates the
KEM key with the prekey and hard-cuts stored ratchet state. The TUI
labels a legacy friend 'classical'.
Docs: PROTOCOL (keys, framing, prologues, no-downgrade, invite v2),
THREAT_MODEL (what PQ transport does and does not buy; epoch 0 stays
classical), README (invite size, F8, legacy caveat). SDK exposes pq on
InviteInfo/FriendInfo and gains a v2 decode round-trip test.
2026-10-20 11:15:46 -04:00
|
|
|
assert!(qr::encode(&me.identity_sk, &onion, &[1u8; 31], &me.kem_pk).is_err());
|
|
|
|
|
assert!(qr::encode(&me.identity_sk, &onion, &[1u8; 33], &me.kem_pk).is_err());
|
|
|
|
|
assert!(qr::encode(&me.identity_sk, &onion, &[], &me.kem_pk).is_err());
|
2026-09-10 19:52:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn encode_rejects_garbage_onion() {
|
|
|
|
|
let _home = TempHome::new();
|
|
|
|
|
let store = Store::open().expect("open");
|
|
|
|
|
let me = store.self_identity().expect("self");
|
|
|
|
|
let spk = [2u8; 32];
|
transport: ML-KEM-768 hybrid prelude and onionwire:v2 invites
Adds an ML-KEM-768 install key beside the ed25519 identity and x25519
prekey. A friend added from a v2 invite always gets a version-2 PQ
prelude before Noise msg 1, and both sides bind (ct, ss_kem) into the
Noise prologue (onionwire-v2 \0 ct \0 ss_kem). The row decides: a
kem_pk friend without a prelude is rejected with 'prelude required',
a prelude for a classical row with 'unexpected prelude'.
Invites are now onionwire:v2 with kem=<base64url pk>; decode still
accepts v1 (no kem) and yields a classical friend. F8 rotates the
KEM key with the prekey and hard-cuts stored ratchet state. The TUI
labels a legacy friend 'classical'.
Docs: PROTOCOL (keys, framing, prologues, no-downgrade, invite v2),
THREAT_MODEL (what PQ transport does and does not buy; epoch 0 stays
classical), README (invite size, F8, legacy caveat). SDK exposes pq on
InviteInfo/FriendInfo and gains a v2 decode round-trip test.
2026-10-20 11:15:46 -04:00
|
|
|
assert!(qr::encode(&me.identity_sk, "not-an-onion", &spk, &me.kem_pk).is_err());
|
|
|
|
|
assert!(qr::encode(&me.identity_sk, "alice.onion", &spk, &me.kem_pk).is_err());
|
|
|
|
|
assert!(qr::encode(&me.identity_sk, &format!("{}.onion", "A".repeat(56)), &spk, &me.kem_pk).is_err());
|
2026-09-10 19:52:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn decode_rejects_spk_len_and_garbage_onion() {
|
|
|
|
|
let _home = TempHome::new();
|
|
|
|
|
let store = Store::open().expect("open");
|
|
|
|
|
let me = store.self_identity().expect("self");
|
transport: ML-KEM-768 hybrid prelude and onionwire:v2 invites
Adds an ML-KEM-768 install key beside the ed25519 identity and x25519
prekey. A friend added from a v2 invite always gets a version-2 PQ
prelude before Noise msg 1, and both sides bind (ct, ss_kem) into the
Noise prologue (onionwire-v2 \0 ct \0 ss_kem). The row decides: a
kem_pk friend without a prelude is rejected with 'prelude required',
a prelude for a classical row with 'unexpected prelude'.
Invites are now onionwire:v2 with kem=<base64url pk>; decode still
accepts v1 (no kem) and yields a classical friend. F8 rotates the
KEM key with the prekey and hard-cuts stored ratchet state. The TUI
labels a legacy friend 'classical'.
Docs: PROTOCOL (keys, framing, prologues, no-downgrade, invite v2),
THREAT_MODEL (what PQ transport does and does not buy; epoch 0 stays
classical), README (invite size, F8, legacy caveat). SDK exposes pq on
InviteInfo/FriendInfo and gains a v2 decode round-trip test.
2026-10-20 11:15:46 -04:00
|
|
|
let raw = qr::encode(&me.identity_sk, &v3onion("ok"), &[3u8; 32], &me.kem_pk).expect("encode");
|
2026-09-10 19:52:44 -04:00
|
|
|
let (k, o, spk, sig) = invite_fields(&raw);
|
|
|
|
|
let short_spk = format!("onionwire:v1:k={k}:o={o}:spk={}:sig={sig}", &spk[..62]);
|
|
|
|
|
assert!(qr::decode(&short_spk).is_err());
|
|
|
|
|
let garbage_o = format!("onionwire:v1:k={k}:o=nope.onion:spk={spk}:sig={sig}");
|
|
|
|
|
assert!(qr::decode(&garbage_o).is_err());
|
|
|
|
|
}
|
|
|
|
|
|
transport: ML-KEM-768 hybrid prelude and onionwire:v2 invites
Adds an ML-KEM-768 install key beside the ed25519 identity and x25519
prekey. A friend added from a v2 invite always gets a version-2 PQ
prelude before Noise msg 1, and both sides bind (ct, ss_kem) into the
Noise prologue (onionwire-v2 \0 ct \0 ss_kem). The row decides: a
kem_pk friend without a prelude is rejected with 'prelude required',
a prelude for a classical row with 'unexpected prelude'.
Invites are now onionwire:v2 with kem=<base64url pk>; decode still
accepts v1 (no kem) and yields a classical friend. F8 rotates the
KEM key with the prekey and hard-cuts stored ratchet state. The TUI
labels a legacy friend 'classical'.
Docs: PROTOCOL (keys, framing, prologues, no-downgrade, invite v2),
THREAT_MODEL (what PQ transport does and does not buy; epoch 0 stays
classical), README (invite size, F8, legacy caveat). SDK exposes pq on
InviteInfo/FriendInfo and gains a v2 decode round-trip test.
2026-10-20 11:15:46 -04:00
|
|
|
fn hex(bytes: &[u8]) -> String {
|
|
|
|
|
bytes.iter().map(|b| format!("{b:02x}")).collect()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A genuine legacy v1 invite: signature over the unconcatenated `k || o || spk`
|
|
|
|
|
/// (the historical layout), no `kem=` field.
|
|
|
|
|
fn legacy_v1(sk: &[u8; 32], onion: &str, spk: &[u8; 32]) -> String {
|
|
|
|
|
use ed25519_dalek::{Signer, SigningKey};
|
|
|
|
|
let signing = SigningKey::from_bytes(sk);
|
|
|
|
|
let k = hex(&signing.verifying_key().to_bytes());
|
|
|
|
|
let spk_hex = hex(spk);
|
|
|
|
|
let mut msg = Vec::new();
|
|
|
|
|
msg.extend_from_slice(k.as_bytes());
|
|
|
|
|
msg.extend_from_slice(onion.as_bytes());
|
|
|
|
|
msg.extend_from_slice(spk_hex.as_bytes());
|
|
|
|
|
let sig = hex(&signing.sign(&msg).to_bytes());
|
|
|
|
|
format!("onionwire:v1:k={k}:o={onion}:spk={spk_hex}:sig={sig}")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn invite_v2_decode_restores_the_kem_key() {
|
|
|
|
|
let _home = TempHome::new();
|
|
|
|
|
let store = Store::open().expect("open");
|
|
|
|
|
let me = store.self_identity().expect("self");
|
|
|
|
|
let raw = qr::encode(&me.identity_sk, &v3onion("kem"), &[4u8; 32], &me.kem_pk)
|
|
|
|
|
.expect("encode v2");
|
|
|
|
|
let p = qr::decode(&raw).expect("decode v2");
|
|
|
|
|
assert_eq!(p.kem_pk.as_deref(), Some(me.kem_pk.as_slice()));
|
|
|
|
|
assert!(p.kem_pk.as_deref().is_some_and(|k| k.len() == 1184));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn invite_v2_kem_strip_fails_signature_and_v1_downgrade_is_rejected() {
|
|
|
|
|
let _home = TempHome::new();
|
|
|
|
|
let store = Store::open().expect("open");
|
|
|
|
|
let me = store.self_identity().expect("self");
|
|
|
|
|
let raw = qr::encode(&me.identity_sk, &v3onion("strip"), &[5u8; 32], &me.kem_pk)
|
|
|
|
|
.expect("encode v2");
|
|
|
|
|
|
|
|
|
|
// Dropping the field from a v2 invite is not a valid v2 invite at all.
|
|
|
|
|
let stripped: String = raw
|
|
|
|
|
.split(':')
|
|
|
|
|
.filter(|field| !field.starts_with("kem="))
|
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
|
.join(":");
|
|
|
|
|
let err = qr::decode(&stripped).expect_err("v2 without kem must fail");
|
|
|
|
|
assert!(err.to_string().contains("missing kem"), "{err}");
|
|
|
|
|
|
|
|
|
|
// The anti-downgrade property: relabel the same bytes v1 and strip `kem=`.
|
|
|
|
|
// v1 verification covers a different preimage, so this cannot verify.
|
|
|
|
|
let downgraded: String = stripped.replacen("onionwire:v2:", "onionwire:v1:", 1);
|
|
|
|
|
let err = qr::decode(&downgraded).expect_err("v1 downgrade must fail");
|
|
|
|
|
assert!(err.to_string().contains("bad signature"), "{err}");
|
|
|
|
|
|
|
|
|
|
// Substituting a different (valid) encapsulation key must break the
|
|
|
|
|
// signature rather than silently changing who we encapsulate to.
|
|
|
|
|
let other_b64 = {
|
|
|
|
|
// Reuse encode by swapping: encode with the other key under the same
|
|
|
|
|
// identity would re-sign, so substitute in the serialized invite.
|
|
|
|
|
let mut s = raw.clone();
|
|
|
|
|
let start = s.find(":kem=").expect("kem field") + 5;
|
|
|
|
|
let end = start + s[start..].find(':').expect("kem terminator");
|
|
|
|
|
// A different key of the same length; signature still covers this slot.
|
|
|
|
|
s.replace_range(start..end, &"A".repeat(end - start));
|
|
|
|
|
s
|
|
|
|
|
};
|
|
|
|
|
let err = qr::decode(&other_b64).expect_err("kem substitution must fail");
|
|
|
|
|
assert!(err.to_string().contains("bad signature"), "{err}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn invite_v1_still_decodes_to_a_classical_friend_row() {
|
|
|
|
|
let _home = TempHome::new();
|
|
|
|
|
let store = Store::open().expect("open");
|
|
|
|
|
let me = store.self_identity().expect("self");
|
|
|
|
|
let sk: [u8; 32] = me.identity_sk.as_slice().try_into().expect("32-byte sk");
|
|
|
|
|
let onion = v3onion("legacy");
|
|
|
|
|
let spk = [8u8; 32];
|
|
|
|
|
let raw = legacy_v1(&sk, &onion, &spk);
|
|
|
|
|
let p = qr::decode(&raw).expect("legacy v1 decodes");
|
|
|
|
|
assert!(p.kem_pk.is_none(), "a v1 invite is a classical friend");
|
|
|
|
|
assert_eq!(p.signed_prekey, spk);
|
|
|
|
|
|
|
|
|
|
store.upsert_friend(&p.pubkey, &p.onion, None).expect("upsert");
|
|
|
|
|
store.set_friend_prekey(&p.pubkey, &p.signed_prekey).expect("prekey");
|
|
|
|
|
store
|
|
|
|
|
.set_friend_kem(&p.pubkey, p.kem_pk.as_deref())
|
|
|
|
|
.expect("clear kem");
|
|
|
|
|
let f = store.get_friend(&p.pubkey).expect("get").expect("row");
|
|
|
|
|
assert!(f.kem_pk.is_none(), "legacy friend row has no PQ key");
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-10 19:52:44 -04:00
|
|
|
#[test]
|
|
|
|
|
fn huge_hex_is_err_quickly() {
|
|
|
|
|
let huge = format!(
|
|
|
|
|
"onionwire:v1:k={}:o=x:spk=yy:sig=zz",
|
|
|
|
|
"aa".repeat(1024 * 1024)
|
|
|
|
|
);
|
|
|
|
|
let t = std::time::Instant::now();
|
|
|
|
|
assert!(qr::decode(&huge).is_err());
|
|
|
|
|
assert!(
|
|
|
|
|
t.elapsed() < std::time::Duration::from_millis(250),
|
|
|
|
|
"decode must fail closed before a multi-MB alloc"
|
|
|
|
|
);
|
|
|
|
|
}
|
pay: domain-separate tip and gate legacy signature acceptance
`tip` was the one payment frame with no domain tag: sign_tip signed the
LF-joined fields, so its signature could be confused with any other
newline-joined preimage. Mirror inv/rcp: sign_tip now emits
"onionwire-tip-v1" || 0x00-separated txid/amount/address/memo/ts, and
verify_tip tries that form then the historical layout.
Legacy acceptance is now one switch instead of nine call sites:
`pay::LEGACY_SIGS` (default true) plus `pay::legacy_sigs()`. Every v1 /
concat fallback reads it — the `onionwire:v1` invite string and its two
historical signature forms, `loc`, `inv`, `rcp`, and `tip`. `prf` is not
gated here because src/profile.rs is outside this card's file scope; the
spec says so.
Tests: legacy-on keeps every untagged form verifying (incl. a new tip
case); legacy-off rejects each untagged form while the tagged form still
verifies (loc, inv, rcp, tip, invite); sign_tip is pinned to the tagged
layout.
docs/PROTOCOL.md gains the emitted/accepted table per signed frame and
the mixed-fleet consequence.
2026-10-22 22:23:44 -04:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn legacy_off_rejects_v1_invite_but_v2_still_decodes() {
|
|
|
|
|
let _home = TempHome::new();
|
|
|
|
|
let store = Store::open().expect("open");
|
|
|
|
|
let me = store.self_identity().expect("self");
|
|
|
|
|
let sk: [u8; 32] = me.identity_sk.as_slice().try_into().expect("32-byte sk");
|
|
|
|
|
let onion = v3onion("switch");
|
|
|
|
|
let raw_v1 = legacy_v1(&sk, &onion, &[8u8; 32]);
|
|
|
|
|
let raw_v2 = qr::encode(&me.identity_sk, &onion, &[8u8; 32], &me.kem_pk).expect("encode v2");
|
|
|
|
|
|
|
|
|
|
onionwire::pay::with_legacy_sigs(false, || {
|
|
|
|
|
assert!(
|
|
|
|
|
qr::decode(&raw_v1).is_err(),
|
|
|
|
|
"a v1 invite must be rejected with legacy off"
|
|
|
|
|
);
|
|
|
|
|
let p = qr::decode(&raw_v2).expect("v2 invite still decodes with legacy off");
|
|
|
|
|
assert!(p.kem_pk.is_some(), "v2 invite keeps its PQ key");
|
|
|
|
|
});
|
|
|
|
|
// Legacy acceptance returns with the default switch state.
|
|
|
|
|
assert!(qr::decode(&raw_v1).is_ok());
|
|
|
|
|
}
|