diff --git a/docs/PROTOCOL.md b/docs/PROTOCOL.md index 8e1ab30..bda5f94 100644 --- a/docs/PROTOCOL.md +++ b/docs/PROTOCOL.md @@ -144,6 +144,20 @@ Chat MUST NOT begin with three lowercase ASCII letters and a space. Hex on the wire is lowercase on encode; decode accepts either case. No `0x` prefix. +### Application-frame signatures + +`loc`, `prf`, `inv`, and `rcp` each carry an ed25519 signature over the +**fields**, not the 4-byte typed prefix. Encode MUST sign the domain-separated +form (type tag, then each field, `0x00` separators). Verify MUST accept that +form and, for mixed-version peers, the legacy untagged LF-joined layout. +Anything else fails closed. Do not emit the legacy layout. + +The invite string is separate and keeps `onionwire-invite-v1` (see Invite). + +Older builds verify only the untagged bytes, so they reject tagged signatures +(fail closed, no downgrade). Mixed-version fleets break outbound application +frames from a new sender to an old receiver until both sides are updated. + ### `loc ` — locator update Sent when the sender rotated their onion and the receiver is reachable. @@ -155,7 +169,18 @@ loc \n\n - `onion` — v3 onion (56 base32 `a-z2-7` chars + `.onion`) - `ts` — unix seconds, decimal ASCII - `sig_hex` — 64-byte ed25519, 128 hex chars -- signed bytes: `onion || LF || ascii(ts)` (the `loc ` prefix is **not** signed) +- the `loc ` prefix is **not** signed + +**Current signature (encode this):** + +``` +ed25519_sign(identity_sk, + "onionwire-loc-v1" || 0x00 || onion || 0x00 || ascii(ts)) +``` + +**Legacy accept:** a signature over `onion || LF || ascii(ts)` is still +accepted on verify so old loc frames verify. New loc frames MUST use the +domain-separated form. Do not emit legacy. Apply only if **all** of: @@ -175,7 +200,18 @@ prf \n\n\n\n Limits: name ≤ 64 bytes, bio ≤ 512 bytes. No newlines in those fields. `xmr_addr` may be empty; if non-empty it MUST be a checksummed Monero address -(see invoices). Signed bytes: the four fields joined by LF (no `prf ` prefix). +(see invoices). The `prf ` prefix is **not** signed. + +**Current signature (encode this):** + +``` +ed25519_sign(identity_sk, + "onionwire-prf-v1" || 0x00 || name || 0x00 || bio || 0x00 || xmr_addr || 0x00 || ascii(ts)) +``` + +**Legacy accept:** a signature over the four fields joined by LF is still +accepted on verify. New profile frames MUST use the domain-separated form. +Do not emit legacy. Apply only for an existing friend, valid signature, newer `ts`. Not a public profile. Not a directory. @@ -188,7 +224,18 @@ inv \n
\n\n\n `amount_atomic` is decimal piconero (integer string, > 0). `address` is a Monero mainnet/stagenet standard, subaddress, or integrated address with a -valid Keccak checksum. Signed bytes: the four fields joined by LF. +valid Keccak checksum. The `inv ` prefix is **not** signed. + +**Current signature (encode this):** + +``` +ed25519_sign(identity_sk, + "onionwire-inv-v1" || 0x00 || amount_atomic || 0x00 || address || 0x00 || memo || 0x00 || ascii(ts)) +``` + +**Legacy accept:** a signature over the four fields joined by LF is still +accepted on verify. New invoices MUST use the domain-separated form. Do not +emit legacy. The frame is a signed claim, not a payment. @@ -198,6 +245,19 @@ The frame is a signed claim, not a payment. rcp \n\n
\n\n ``` +The `rcp ` prefix is **not** signed. + +**Current signature (encode this):** + +``` +ed25519_sign(identity_sk, + "onionwire-rcp-v1" || 0x00 || txid || 0x00 || amount_atomic || 0x00 || address || 0x00 || ascii(ts)) +``` + +**Legacy accept:** a signature over the four fields joined by LF is still +accepted on verify. New receipts MUST use the domain-separated form. Do not +emit legacy. + Signature proves which friend sent the claim. It does **not** prove money moved. Local `verified=1` only if a user-hosted `monero-wallet-rpc` returns one transfer row with the same non-empty `txid`, `amount`, and `address` diff --git a/src/loc.rs b/src/loc.rs index 1248e25..aa25574 100644 --- a/src/loc.rs +++ b/src/loc.rs @@ -27,7 +27,7 @@ pub fn sign(identity_sk: &[u8], onion: &str, ts: i64) -> Result { .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(onion, ts)).to_bytes().to_vec(); + let sig = sk.sign(&sign_msg_v2(onion, ts)).to_bytes().to_vec(); Ok(Loc { onion: onion.to_string(), ts, @@ -50,8 +50,12 @@ pub fn verify(identity_pk: &[u8], loc: &Loc) -> bool { let Ok(vk) = VerifyingKey::from_bytes(&pk) else { return false; }; - vk.verify(&sign_msg(&loc.onion, loc.ts), &Signature::from_bytes(&sig)) + let signature = Signature::from_bytes(&sig); + vk.verify(&sign_msg_v2(&loc.onion, loc.ts), &signature) .is_ok() + || vk + .verify(&sign_msg_v1(&loc.onion, loc.ts), &signature) + .is_ok() } pub fn encode(loc: &Loc) -> Vec { @@ -77,13 +81,29 @@ pub fn decode(pt: &[u8]) -> Option { Some(Loc { onion, ts, sig }) } -fn sign_msg(onion: &str, ts: i64) -> Vec { +fn sign_msg_v2(onion: &str, ts: i64) -> Vec { + tagged( + b"onionwire-loc-v1", + &[onion.as_bytes(), ts.to_string().as_bytes()], + ) +} + +fn sign_msg_v1(onion: &str, ts: i64) -> Vec { let mut msg = Vec::from(onion.as_bytes()); msg.push(b'\n'); msg.extend_from_slice(ts.to_string().as_bytes()); msg } +fn tagged(tag: &[u8], fields: &[&[u8]]) -> Vec { + let mut msg = Vec::from(tag); + for f in fields { + msg.push(0); + msg.extend_from_slice(f); + } + msg +} + fn to_hex(bytes: &[u8]) -> String { bytes.iter().map(|b| format!("{b:02x}")).collect() } diff --git a/src/pay.rs b/src/pay.rs index 2f79d43..ae17390 100644 --- a/src/pay.rs +++ b/src/pay.rs @@ -179,7 +179,10 @@ pub fn sign_invoice( ts: i64, ) -> Result { check_invoice_fields(amount_atomic, address, memo)?; - let sig = sign_bytes(identity_sk, &inv_sign_msg(amount_atomic, address, memo, ts))?; + let sig = sign_bytes( + identity_sk, + &inv_sign_msg_v2(amount_atomic, address, memo, ts), + )?; Ok(Invoice { amount_atomic: amount_atomic.to_string(), address: address.to_string(), @@ -195,7 +198,11 @@ pub fn verify_invoice(identity_pk: &[u8], inv: &Invoice) -> bool { } verify_bytes( identity_pk, - &inv_sign_msg(&inv.amount_atomic, &inv.address, &inv.memo, inv.ts), + &inv_sign_msg_v2(&inv.amount_atomic, &inv.address, &inv.memo, inv.ts), + &inv.sig, + ) || verify_bytes( + identity_pk, + &inv_sign_msg_v1(&inv.amount_atomic, &inv.address, &inv.memo, inv.ts), &inv.sig, ) } @@ -243,7 +250,10 @@ pub fn sign_receipt( ts: i64, ) -> Result { check_receipt_fields(txid, amount_atomic, address)?; - let sig = sign_bytes(identity_sk, &rcp_sign_msg(txid, amount_atomic, address, ts))?; + let sig = sign_bytes( + identity_sk, + &rcp_sign_msg_v2(txid, amount_atomic, address, ts), + )?; Ok(Receipt { txid: txid.to_string(), amount_atomic: amount_atomic.to_string(), @@ -259,7 +269,11 @@ pub fn verify_receipt(identity_pk: &[u8], rcp: &Receipt) -> bool { } verify_bytes( identity_pk, - &rcp_sign_msg(&rcp.txid, &rcp.amount_atomic, &rcp.address, rcp.ts), + &rcp_sign_msg_v2(&rcp.txid, &rcp.amount_atomic, &rcp.address, rcp.ts), + &rcp.sig, + ) || verify_bytes( + identity_pk, + &rcp_sign_msg_v1(&rcp.txid, &rcp.amount_atomic, &rcp.address, rcp.ts), &rcp.sig, ) } @@ -316,7 +330,19 @@ fn check_receipt_fields(txid: &str, amount_atomic: &str, address: &str) -> Resul check_address(address) } -fn inv_sign_msg(amount_atomic: &str, address: &str, memo: &str, ts: i64) -> Vec { +fn inv_sign_msg_v2(amount_atomic: &str, address: &str, memo: &str, ts: i64) -> Vec { + tagged( + b"onionwire-inv-v1", + &[ + amount_atomic.as_bytes(), + address.as_bytes(), + memo.as_bytes(), + ts.to_string().as_bytes(), + ], + ) +} + +fn inv_sign_msg_v1(amount_atomic: &str, address: &str, memo: &str, ts: i64) -> Vec { let mut msg = Vec::from(amount_atomic.as_bytes()); msg.push(b'\n'); msg.extend_from_slice(address.as_bytes()); @@ -327,7 +353,19 @@ fn inv_sign_msg(amount_atomic: &str, address: &str, memo: &str, ts: i64) -> Vec< msg } -fn rcp_sign_msg(txid: &str, amount_atomic: &str, address: &str, ts: i64) -> Vec { +fn rcp_sign_msg_v2(txid: &str, amount_atomic: &str, address: &str, ts: i64) -> Vec { + tagged( + b"onionwire-rcp-v1", + &[ + txid.as_bytes(), + amount_atomic.as_bytes(), + address.as_bytes(), + ts.to_string().as_bytes(), + ], + ) +} + +fn rcp_sign_msg_v1(txid: &str, amount_atomic: &str, address: &str, ts: i64) -> Vec { let mut msg = Vec::from(txid.as_bytes()); msg.push(b'\n'); msg.extend_from_slice(amount_atomic.as_bytes()); @@ -338,6 +376,15 @@ fn rcp_sign_msg(txid: &str, amount_atomic: &str, address: &str, ts: i64) -> Vec< msg } +fn tagged(tag: &[u8], fields: &[&[u8]]) -> Vec { + let mut msg = Vec::from(tag); + for f in fields { + msg.push(0); + msg.extend_from_slice(f); + } + msg +} + fn sign_bytes(identity_sk: &[u8], msg: &[u8]) -> Result> { let sk_bytes: [u8; 32] = identity_sk .try_into() diff --git a/src/profile.rs b/src/profile.rs index 81fc7d5..a22d0ef 100644 --- a/src/profile.rs +++ b/src/profile.rs @@ -43,7 +43,7 @@ pub fn sign( .map_err(|_| Error("identity secret key must be 32 bytes".into()))?; let sk = SigningKey::from_bytes(&sk_bytes); let sig = sk - .sign(&sign_msg(display_name, bio, xmr_addr, ts)) + .sign(&sign_msg_v2(display_name, bio, xmr_addr, ts)) .to_bytes() .to_vec(); Ok(Profile { @@ -73,11 +73,18 @@ pub fn verify(identity_pk: &[u8], prf: &Profile) -> bool { let Ok(vk) = VerifyingKey::from_bytes(&pk) else { return false; }; + let signature = Signature::from_bytes(&sig); vk.verify( - &sign_msg(&prf.display_name, &prf.bio, &prf.xmr_addr, prf.ts), - &Signature::from_bytes(&sig), + &sign_msg_v2(&prf.display_name, &prf.bio, &prf.xmr_addr, prf.ts), + &signature, ) .is_ok() + || vk + .verify( + &sign_msg_v1(&prf.display_name, &prf.bio, &prf.xmr_addr, prf.ts), + &signature, + ) + .is_ok() } pub fn encode(prf: &Profile) -> Vec { @@ -131,7 +138,19 @@ fn check_fields(display_name: &str, bio: &str, xmr_addr: &str) -> Result<()> { Ok(()) } -fn sign_msg(display_name: &str, bio: &str, xmr_addr: &str, ts: i64) -> Vec { +fn sign_msg_v2(display_name: &str, bio: &str, xmr_addr: &str, ts: i64) -> Vec { + tagged( + b"onionwire-prf-v1", + &[ + display_name.as_bytes(), + bio.as_bytes(), + xmr_addr.as_bytes(), + ts.to_string().as_bytes(), + ], + ) +} + +fn sign_msg_v1(display_name: &str, bio: &str, xmr_addr: &str, ts: i64) -> Vec { let mut msg = Vec::from(display_name.as_bytes()); msg.push(b'\n'); msg.extend_from_slice(bio.as_bytes()); @@ -142,6 +161,15 @@ fn sign_msg(display_name: &str, bio: &str, xmr_addr: &str, ts: i64) -> Vec { msg } +fn tagged(tag: &[u8], fields: &[&[u8]]) -> Vec { + let mut msg = Vec::from(tag); + for f in fields { + msg.push(0); + msg.extend_from_slice(f); + } + msg +} + fn to_hex(bytes: &[u8]) -> String { bytes.iter().map(|b| format!("{b:02x}")).collect() } diff --git a/tests/app_sig.rs b/tests/app_sig.rs new file mode 100644 index 0000000..a3aaac1 --- /dev/null +++ b/tests/app_sig.rs @@ -0,0 +1,200 @@ +//! Domain-separated application-frame signatures (loc/prf/inv/rcp). +//! +//! Encode still emits typed prefixes and LF-joined fields. Only the signed +//! bytes change: tagged `0x00`-separated form on emit, dual-accept of the +//! legacy untagged LF layout on verify. + +use ed25519_dalek::{Signer, SigningKey}; +use onionwire::loc::{self, Loc}; +use onionwire::pay::{self, Invoice, Receipt}; +use onionwire::profile::{self, Profile}; +use rand::rngs::OsRng; + +const MAINNET_STD: &str = "4AdUndXHHZ6cfufTMvppY6JwXNouMBzSkbLYfpAV5Usx3skxNgYeYTRj5UzqtReoS44qo9mtmXCqY45DJ852K5Jv2684Rge"; + +fn keypair() -> (SigningKey, [u8; 32]) { + let sk = SigningKey::generate(&mut OsRng); + let pk = sk.verifying_key().to_bytes(); + (sk, pk) +} + +#[test] +fn tagged_loc_roundtrip() { + let (sk, pk) = keypair(); + let loc = loc::sign(&sk.to_bytes(), "abc.onion", 42).unwrap(); + let parsed = loc::decode(&loc::encode(&loc)).expect("loc frame"); + assert!(loc::verify(&pk, &parsed)); + assert_eq!(parsed.onion, "abc.onion"); + assert_eq!(parsed.ts, 42); +} + +#[test] +fn tagged_prf_roundtrip() { + let (sk, pk) = keypair(); + let prf = profile::sign(&sk.to_bytes(), "Ali", "hi", "", 2_000_000_000).unwrap(); + let parsed = profile::decode(&profile::encode(&prf)).expect("prf frame"); + assert!(profile::verify(&pk, &parsed)); + assert_eq!(parsed.display_name, "Ali"); + assert_eq!(parsed.bio, "hi"); + assert_eq!(parsed.xmr_addr, ""); + assert_eq!(parsed.ts, 2_000_000_000); +} + +#[test] +fn tagged_inv_roundtrip() { + let (sk, pk) = keypair(); + let inv = pay::sign_invoice(&sk.to_bytes(), "120000000000", MAINNET_STD, "coffee", 42).unwrap(); + let parsed = pay::decode_invoice(&pay::encode_invoice(&inv)).expect("inv frame"); + assert!(pay::verify_invoice(&pk, &parsed)); + assert_eq!(parsed.amount_atomic, "120000000000"); + assert_eq!(parsed.address, MAINNET_STD); + assert_eq!(parsed.memo, "coffee"); + assert_eq!(parsed.ts, 42); +} + +#[test] +fn tagged_rcp_roundtrip() { + let (sk, pk) = keypair(); + let rcp = pay::sign_receipt(&sk.to_bytes(), "deadbeef", "1", MAINNET_STD, 99).unwrap(); + let parsed = pay::decode_receipt(&pay::encode_receipt(&rcp)).expect("rcp frame"); + assert!(pay::verify_receipt(&pk, &parsed)); + assert_eq!(parsed.txid, "deadbeef"); + assert_eq!(parsed.amount_atomic, "1"); + assert_eq!(parsed.address, MAINNET_STD); + assert_eq!(parsed.ts, 99); +} + +#[test] +fn legacy_untagged_loc_still_verifies() { + let (sk, pk) = keypair(); + let onion = "abc.onion"; + let ts = 42i64; + let mut msg = Vec::from(onion.as_bytes()); + msg.push(b'\n'); + msg.extend_from_slice(ts.to_string().as_bytes()); + let loc = Loc { + onion: onion.into(), + ts, + sig: sk.sign(&msg).to_bytes().to_vec(), + }; + assert!( + loc::verify(&pk, &loc), + "legacy loc LF layout must still verify" + ); +} + +#[test] +fn legacy_untagged_prf_still_verifies() { + let (sk, pk) = keypair(); + let ts = 2_000_000_000i64; + let mut msg = Vec::from(b"Ali".as_slice()); + msg.push(b'\n'); + msg.extend_from_slice(b"hi"); + msg.push(b'\n'); + msg.extend_from_slice(b""); + msg.push(b'\n'); + msg.extend_from_slice(ts.to_string().as_bytes()); + let prf = Profile { + display_name: "Ali".into(), + bio: "hi".into(), + xmr_addr: String::new(), + ts, + sig: sk.sign(&msg).to_bytes().to_vec(), + }; + assert!( + profile::verify(&pk, &prf), + "legacy prf LF layout must still verify" + ); +} + +#[test] +fn legacy_untagged_inv_still_verifies() { + let (sk, pk) = keypair(); + let ts = 42i64; + let mut msg = Vec::from(b"1".as_slice()); + msg.push(b'\n'); + msg.extend_from_slice(MAINNET_STD.as_bytes()); + msg.push(b'\n'); + msg.extend_from_slice(b"coffee"); + msg.push(b'\n'); + msg.extend_from_slice(ts.to_string().as_bytes()); + let inv = Invoice { + amount_atomic: "1".into(), + address: MAINNET_STD.into(), + memo: "coffee".into(), + ts, + sig: sk.sign(&msg).to_bytes().to_vec(), + }; + assert!( + pay::verify_invoice(&pk, &inv), + "legacy inv LF layout must still verify" + ); +} + +#[test] +fn legacy_untagged_rcp_still_verifies() { + let (sk, pk) = keypair(); + let ts = 99i64; + let mut msg = Vec::from(b"deadbeef".as_slice()); + msg.push(b'\n'); + msg.extend_from_slice(b"1"); + msg.push(b'\n'); + msg.extend_from_slice(MAINNET_STD.as_bytes()); + msg.push(b'\n'); + msg.extend_from_slice(ts.to_string().as_bytes()); + let rcp = Receipt { + txid: "deadbeef".into(), + amount_atomic: "1".into(), + address: MAINNET_STD.into(), + ts, + sig: sk.sign(&msg).to_bytes().to_vec(), + }; + assert!( + pay::verify_receipt(&pk, &rcp), + "legacy rcp LF layout must still verify" + ); +} + +/// prf and rcp both sign four fields. Without a type tag, a profile whose +/// fields are also a valid receipt (name=txid, bio=amount, xmr=address) +/// verifies as a receipt under the same key. +#[test] +fn prf_signature_does_not_verify_as_rcp() { + let (sk, pk) = keypair(); + let ts = 42i64; + let prf = profile::sign(&sk.to_bytes(), "deadbeef", "1", MAINNET_STD, ts).unwrap(); + assert!(profile::verify(&pk, &prf)); + let rcp = Receipt { + txid: "deadbeef".into(), + amount_atomic: "1".into(), + address: MAINNET_STD.into(), + ts, + sig: prf.sig.clone(), + }; + assert!( + !pay::verify_receipt(&pk, &rcp), + "prf-shaped payload must not verify as rcp" + ); +} + +/// inv field order is amount/address/memo/ts; rcp is txid/amount/address/ts. +/// Reinterpret the invoice fields in order as a receipt and the signature +/// must not verify (and field checks fail closed on a non-decimal amount). +#[test] +fn inv_signature_does_not_verify_as_rcp() { + let (sk, pk) = keypair(); + let ts = 42i64; + let inv = pay::sign_invoice(&sk.to_bytes(), "1", MAINNET_STD, MAINNET_STD, ts).unwrap(); + assert!(pay::verify_invoice(&pk, &inv)); + let rcp = Receipt { + txid: "1".into(), + amount_atomic: MAINNET_STD.into(), + address: MAINNET_STD.into(), + ts, + sig: inv.sig.clone(), + }; + assert!( + !pay::verify_receipt(&pk, &rcp), + "inv-shaped payload must not verify as rcp" + ); +}