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.
This commit is contained in:
parent
00bb95675f
commit
c741b660e4
26 changed files with 1637 additions and 177 deletions
72
src/frame.rs
72
src/frame.rs
|
|
@ -1,6 +1,9 @@
|
|||
use futures::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
|
||||
|
||||
pub const VERSION: u8 = 1;
|
||||
/// PQ handshake prelude. Body is `uint16 ct_len` (big-endian) followed by
|
||||
/// exactly `ct_len` bytes. Only the opening frame of a session may use it.
|
||||
pub const VERSION_PRELUDE: u8 = 2;
|
||||
pub const MAX_FRAME: usize = 65535;
|
||||
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
|
@ -53,11 +56,65 @@ pub async fn write_frame<W: AsyncWrite + Unpin>(w: &mut W, body: &[u8]) -> Resul
|
|||
}
|
||||
|
||||
pub async fn read_frame<R: AsyncRead + Unpin>(r: &mut R) -> Result<Vec<u8>> {
|
||||
let (version, body) = read_versioned(r).await?;
|
||||
if version != VERSION {
|
||||
return Err(Error("bad version".into()));
|
||||
}
|
||||
Ok(body)
|
||||
}
|
||||
|
||||
/// First frame of a handshake. Version 1 is Noise msg 1 (classical). Version 2
|
||||
/// is the PQ prelude; the ciphertext is exactly `ct_len` bytes and must be the
|
||||
/// ML-KEM-768 ciphertext length.
|
||||
pub enum Opening {
|
||||
Classical(Vec<u8>),
|
||||
Prelude(Vec<u8>),
|
||||
}
|
||||
|
||||
pub async fn read_opening<R: AsyncRead + Unpin>(r: &mut R) -> Result<Opening> {
|
||||
let (version, body) = read_versioned(r).await?;
|
||||
match version {
|
||||
VERSION => Ok(Opening::Classical(body)),
|
||||
VERSION_PRELUDE => Ok(Opening::Prelude(parse_prelude(&body)?)),
|
||||
_ => Err(Error("bad version".into())),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn write_prelude<W: AsyncWrite + Unpin>(w: &mut W, ct: &[u8]) -> Result<()> {
|
||||
if ct.len() > u16::MAX as usize {
|
||||
return Err(Error("frame too large".into()));
|
||||
}
|
||||
let mut body = Vec::with_capacity(2 + ct.len());
|
||||
body.extend_from_slice(&(ct.len() as u16).to_be_bytes());
|
||||
body.extend_from_slice(ct);
|
||||
let framed = encode_version(VERSION_PRELUDE, &body)?;
|
||||
w.write_all(&framed)
|
||||
.await
|
||||
.map_err(|e| Error(e.to_string()))?;
|
||||
w.flush().await.map_err(|e| Error(e.to_string()))
|
||||
}
|
||||
|
||||
pub fn parse_prelude(body: &[u8]) -> Result<Vec<u8>> {
|
||||
if body.len() < 2 {
|
||||
return Err(Error("short frame".into()));
|
||||
}
|
||||
let ct_len = u16::from_be_bytes(body[..2].try_into().expect("2 bytes")) as usize;
|
||||
if body.len() != 2 + ct_len {
|
||||
return Err(Error("length mismatch".into()));
|
||||
}
|
||||
if ct_len != crate::kem::CT_LEN {
|
||||
return Err(Error("bad prelude".into()));
|
||||
}
|
||||
Ok(body[2..].to_vec())
|
||||
}
|
||||
|
||||
async fn read_versioned<R: AsyncRead + Unpin>(r: &mut R) -> Result<(u8, Vec<u8>)> {
|
||||
let mut hdr = [0u8; 5];
|
||||
r.read_exact(&mut hdr)
|
||||
.await
|
||||
.map_err(|e| Error(e.to_string()))?;
|
||||
if hdr[0] != VERSION {
|
||||
let version = hdr[0];
|
||||
if version != VERSION && version != VERSION_PRELUDE {
|
||||
return Err(Error("bad version".into()));
|
||||
}
|
||||
let len = u32::from_be_bytes(hdr[1..5].try_into().expect("4 bytes")) as usize;
|
||||
|
|
@ -68,5 +125,16 @@ pub async fn read_frame<R: AsyncRead + Unpin>(r: &mut R) -> Result<Vec<u8>> {
|
|||
r.read_exact(&mut body)
|
||||
.await
|
||||
.map_err(|e| Error(e.to_string()))?;
|
||||
Ok(body)
|
||||
Ok((version, body))
|
||||
}
|
||||
|
||||
fn encode_version(version: u8, body: &[u8]) -> Result<Vec<u8>> {
|
||||
if body.len() > MAX_FRAME {
|
||||
return Err(Error("frame too large".into()));
|
||||
}
|
||||
let mut out = Vec::with_capacity(5 + body.len());
|
||||
out.push(version);
|
||||
out.extend_from_slice(&(body.len() as u32).to_be_bytes());
|
||||
out.extend_from_slice(body);
|
||||
Ok(out)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue