transport: ML-KEM-768 hybrid prelude and onionwire:v2 invites
All checks were successful
ci / test (pull_request) Successful in 18m32s
android-ci / android (pull_request) Successful in 20m14s
ci / sdk (pull_request) Successful in 3m11s

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:
agent 2026-10-20 11:15:46 -04:00
commit c741b660e4
No known key found for this signature in database
26 changed files with 1637 additions and 177 deletions

96
src/kem.rs Normal file
View file

@ -0,0 +1,96 @@
//! ML-KEM-768 (FIPS 203) install key.
//!
//! The decapsulation key is stored as the 64-byte seed (`DecapsulationKey::to_seed`),
//! the form the crate prefers. The 1184-byte encapsulation key is what an invite
//! carries. Ciphertext is 1088 bytes; the shared secret is 32.
use ml_kem::kem::{Decapsulate, Encapsulate, Kem};
use ml_kem::{DecapsulationKey768, EncapsulationKey768, KeyExport, MlKem768, Seed};
use zeroize::Zeroizing;
pub const PK_LEN: usize = 1184;
pub const CT_LEN: usize = 1088;
pub const SS_LEN: usize = 32;
pub const SEED_LEN: usize = 64;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
pub struct Error(String);
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl std::error::Error for Error {}
/// A freshly generated install key. `seed` wipes on drop.
pub struct Keypair {
pub seed: Zeroizing<[u8; SEED_LEN]>,
pub pk: [u8; PK_LEN],
}
pub fn generate() -> Keypair {
let (dk, ek) = MlKem768::generate_keypair();
let seed_arr = dk.to_seed().expect("generate_keypair yields a seed");
let pk_arr = ek.to_bytes();
let mut seed = Zeroizing::new([0u8; SEED_LEN]);
let mut pk = [0u8; PK_LEN];
seed.copy_from_slice(seed_arr.as_slice());
pk.copy_from_slice(pk_arr.as_slice());
Keypair { seed, pk }
}
/// Canonical encapsulation-key bytes. Rejects a length or a key the crate
/// will not load, so a garbage `kem=` never becomes a friend row.
pub fn encode_pk(pk: &[u8]) -> Result<Vec<u8>> {
let ek = encapsulation_key(pk)?;
Ok(ek.to_bytes().as_slice().to_vec())
}
pub fn decode_pk(bytes: &[u8]) -> Result<Vec<u8>> {
encode_pk(bytes)
}
pub fn encapsulate(pk: &[u8]) -> Result<(Vec<u8>, Vec<u8>)> {
let ek = encapsulation_key(pk)?;
let (ct, ss) = ek.encapsulate();
Ok((ct.as_slice().to_vec(), ss.as_slice().to_vec()))
}
/// Ciphertext bytes. Length is the only check: ML-KEM decapsulation of a
/// wrong ciphertext does not error (implicit rejection), so a bad ct is
/// caught by the handshake prologue, not here.
pub fn encode_ct(ct: &[u8]) -> Result<Vec<u8>> {
if ct.len() != CT_LEN {
return Err(Error("ct length".into()));
}
Ok(ct.to_vec())
}
pub fn decode_ct(bytes: &[u8]) -> Result<Vec<u8>> {
encode_ct(bytes)
}
pub fn decapsulate(seed: &[u8], ct: &[u8]) -> Result<Vec<u8>> {
if ct.len() != CT_LEN {
return Err(Error("ct length".into()));
}
let seed_arr = Seed::try_from(seed).map_err(|_| Error("seed length".into()))?;
let dk = DecapsulationKey768::from_seed(seed_arr);
let ss = dk
.decapsulate_slice(ct)
.map_err(|_| Error("ct length".into()))?;
Ok(ss.as_slice().to_vec())
}
fn encapsulation_key(pk: &[u8]) -> Result<EncapsulationKey768> {
if pk.len() != PK_LEN {
return Err(Error("pk length".into()));
}
let key = ml_kem::kem::Key::<EncapsulationKey768>::try_from(pk)
.map_err(|_| Error("pk length".into()))?;
EncapsulationKey768::new(&key).map_err(|_| Error("invalid encapsulation key".into()))
}