diff --git a/docs/PROTOCOL.md b/docs/PROTOCOL.md index bda5f94..8e1ab30 100644 --- a/docs/PROTOCOL.md +++ b/docs/PROTOCOL.md @@ -144,20 +144,6 @@ 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. @@ -169,18 +155,7 @@ 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 -- 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. +- signed bytes: `onion || LF || ascii(ts)` (the `loc ` prefix is **not** signed) Apply only if **all** of: @@ -200,18 +175,7 @@ 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). 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. +(see invoices). Signed bytes: the four fields joined by LF (no `prf ` prefix). Apply only for an existing friend, valid signature, newer `ts`. Not a public profile. Not a directory. @@ -224,18 +188,7 @@ 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. 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. +valid Keccak checksum. Signed bytes: the four fields joined by LF. The frame is a signed claim, not a payment. @@ -245,19 +198,6 @@ 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 aa25574..1248e25 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_v2(onion, ts)).to_bytes().to_vec(); + let sig = sk.sign(&sign_msg(onion, ts)).to_bytes().to_vec(); Ok(Loc { onion: onion.to_string(), ts, @@ -50,12 +50,8 @@ pub fn verify(identity_pk: &[u8], loc: &Loc) -> bool { let Ok(vk) = VerifyingKey::from_bytes(&pk) else { return false; }; - let signature = Signature::from_bytes(&sig); - vk.verify(&sign_msg_v2(&loc.onion, loc.ts), &signature) + vk.verify(&sign_msg(&loc.onion, loc.ts), &Signature::from_bytes(&sig)) .is_ok() - || vk - .verify(&sign_msg_v1(&loc.onion, loc.ts), &signature) - .is_ok() } pub fn encode(loc: &Loc) -> Vec { @@ -81,29 +77,13 @@ pub fn decode(pt: &[u8]) -> Option { Some(Loc { onion, ts, sig }) } -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 { +fn sign_msg(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 ae17390..2f79d43 100644 --- a/src/pay.rs +++ b/src/pay.rs @@ -179,10 +179,7 @@ pub fn sign_invoice( ts: i64, ) -> Result { check_invoice_fields(amount_atomic, address, memo)?; - let sig = sign_bytes( - identity_sk, - &inv_sign_msg_v2(amount_atomic, address, memo, ts), - )?; + let sig = sign_bytes(identity_sk, &inv_sign_msg(amount_atomic, address, memo, ts))?; Ok(Invoice { amount_atomic: amount_atomic.to_string(), address: address.to_string(), @@ -198,11 +195,7 @@ pub fn verify_invoice(identity_pk: &[u8], inv: &Invoice) -> bool { } verify_bytes( identity_pk, - &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_sign_msg(&inv.amount_atomic, &inv.address, &inv.memo, inv.ts), &inv.sig, ) } @@ -250,10 +243,7 @@ pub fn sign_receipt( ts: i64, ) -> Result { check_receipt_fields(txid, amount_atomic, address)?; - let sig = sign_bytes( - identity_sk, - &rcp_sign_msg_v2(txid, amount_atomic, address, ts), - )?; + let sig = sign_bytes(identity_sk, &rcp_sign_msg(txid, amount_atomic, address, ts))?; Ok(Receipt { txid: txid.to_string(), amount_atomic: amount_atomic.to_string(), @@ -269,11 +259,7 @@ pub fn verify_receipt(identity_pk: &[u8], rcp: &Receipt) -> bool { } verify_bytes( identity_pk, - &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_sign_msg(&rcp.txid, &rcp.amount_atomic, &rcp.address, rcp.ts), &rcp.sig, ) } @@ -330,19 +316,7 @@ fn check_receipt_fields(txid: &str, amount_atomic: &str, address: &str) -> Resul check_address(address) } -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 { +fn inv_sign_msg(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()); @@ -353,19 +327,7 @@ fn inv_sign_msg_v1(amount_atomic: &str, address: &str, memo: &str, ts: i64) -> V msg } -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 { +fn rcp_sign_msg(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()); @@ -376,15 +338,6 @@ fn rcp_sign_msg_v1(txid: &str, amount_atomic: &str, address: &str, ts: i64) -> V 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 a22d0ef..81fc7d5 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_v2(display_name, bio, xmr_addr, ts)) + .sign(&sign_msg(display_name, bio, xmr_addr, ts)) .to_bytes() .to_vec(); Ok(Profile { @@ -73,18 +73,11 @@ 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_v2(&prf.display_name, &prf.bio, &prf.xmr_addr, prf.ts), - &signature, + &sign_msg(&prf.display_name, &prf.bio, &prf.xmr_addr, prf.ts), + &Signature::from_bytes(&sig), ) .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 { @@ -138,19 +131,7 @@ fn check_fields(display_name: &str, bio: &str, xmr_addr: &str) -> Result<()> { Ok(()) } -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 { +fn sign_msg(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()); @@ -161,15 +142,6 @@ fn sign_msg_v1(display_name: &str, bio: &str, xmr_addr: &str, ts: i64) -> Vec 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 deleted file mode 100644 index a3aaac1..0000000 --- a/tests/app_sig.rs +++ /dev/null @@ -1,200 +0,0 @@ -//! 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" - ); -}